1 /* Copyright (c) 2008-2012 Da SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "mc_private.h"
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_liveness, mc,
11 "Logging specific to algorithms for liveness properties verification");
13 xbt_dynar_t reached_pairs;
14 xbt_dynar_t successors;
16 int create_dump(int pair)
18 // Try to enable core dumps
19 struct rlimit core_limit;
20 core_limit.rlim_cur = RLIM_INFINITY;
21 core_limit.rlim_max = RLIM_INFINITY;
23 if(setrlimit(RLIMIT_CORE, &core_limit) < 0)
24 fprintf(stderr, "setrlimit: %s\nWarning: core dumps may be truncated or non-existant\n", strerror(errno));
29 // We are the child process -- run the actual program
34 // An error occurred, shouldn't happen
39 // We are the parent process -- wait for the child process to exit
42 if(WIFSIGNALED(status) && WCOREDUMP(status)){
43 char *core_name = malloc(20);
44 sprintf(core_name,"core_%d", pair);
45 rename("core", core_name);
53 int reached(xbt_state_t st){
55 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
59 mc_pair_reached_t new_pair = NULL;
60 new_pair = xbt_new0(s_mc_pair_reached_t, 1);
61 new_pair->nb = xbt_dynar_length(reached_pairs) + 1;
62 new_pair->automaton_state = st;
63 new_pair->prop_ato = xbt_dynar_new(sizeof(int), NULL);
64 new_pair->system_state = xbt_new0(s_mc_snapshot_t, 1);
65 MC_take_snapshot_liveness(new_pair->system_state);
67 /* Get values of propositional symbols */
70 unsigned int cursor = 0;
71 xbt_propositional_symbol_t ps = NULL;
72 xbt_dynar_foreach(_mc_property_automaton->propositional_symbols, cursor, ps){
73 f = (int_f_void_t)ps->function;
75 xbt_dynar_push_as(new_pair->prop_ato, int, res);
80 if(xbt_dynar_is_empty(reached_pairs) || !compare){
83 /* New pair reached */
84 xbt_dynar_push(reached_pairs, &new_pair);
94 mc_pair_reached_t pair_test = NULL;
96 xbt_dynar_foreach(reached_pairs, cursor, pair_test){
97 XBT_INFO("Pair reached #%d", pair_test->nb);
98 if(automaton_state_compare(pair_test->automaton_state, st) == 0){
99 if(propositional_symbols_compare_value(pair_test->prop_ato, new_pair->prop_ato) == 0){
100 if(snapshot_compare(new_pair->system_state, pair_test->system_state) == 0){
110 XBT_INFO("Different values of propositional symbols");
113 XBT_INFO("Different automaton state");
117 /* New pair reached */
118 xbt_dynar_push(reached_pairs, &new_pair);
135 void set_pair_reached(xbt_state_t st){
137 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
141 mc_pair_reached_t pair = NULL;
142 pair = xbt_new0(s_mc_pair_reached_t, 1);
143 pair->nb = xbt_dynar_length(reached_pairs) + 1;
144 pair->automaton_state = st;
145 pair->prop_ato = xbt_dynar_new(sizeof(int), NULL);
146 pair->system_state = xbt_new0(s_mc_snapshot_t, 1);
147 MC_take_snapshot_liveness(pair->system_state);
149 /* Get values of propositional symbols */
150 unsigned int cursor = 0;
151 xbt_propositional_symbol_t ps = NULL;
155 xbt_dynar_foreach(_mc_property_automaton->propositional_symbols, cursor, ps){
156 f = (int_f_void_t)ps->function;
158 xbt_dynar_push_as(pair->prop_ato, int, res);
161 xbt_dynar_push(reached_pairs, &pair);
172 void MC_pair_delete(mc_pair_t pair){
173 xbt_free(pair->graph_state->proc_status);
174 xbt_free(pair->graph_state);
180 int MC_automaton_evaluate_label(xbt_exp_label_t l){
184 int left_res = MC_automaton_evaluate_label(l->u.or_and.left_exp);
185 int right_res = MC_automaton_evaluate_label(l->u.or_and.right_exp);
186 return (left_res || right_res);
189 int left_res = MC_automaton_evaluate_label(l->u.or_and.left_exp);
190 int right_res = MC_automaton_evaluate_label(l->u.or_and.right_exp);
191 return (left_res && right_res);
194 int res = MC_automaton_evaluate_label(l->u.exp_not);
198 unsigned int cursor = 0;
199 xbt_propositional_symbol_t p = NULL;
201 xbt_dynar_foreach(_mc_property_automaton->propositional_symbols, cursor, p){
202 if(strcmp(p->pred, l->u.predicat) == 0){
203 f = (int_f_void_t)p->function;
218 /********************* Double-DFS stateless *******************/
220 void pair_stateless_free(mc_pair_stateless_t pair){
224 void pair_stateless_free_voidp(void *p){
225 pair_stateless_free((mc_pair_stateless_t) * (void **) p);
228 mc_pair_stateless_t new_pair_stateless(mc_state_t sg, xbt_state_t st, int r){
229 mc_pair_stateless_t p = NULL;
230 p = xbt_new0(s_mc_pair_stateless_t, 1);
231 p->automaton_state = st;
234 mc_stats_pair->expanded_pairs++;
238 void pair_reached_free(mc_pair_reached_t pair){
240 pair->automaton_state = NULL;
241 xbt_dynar_free(&(pair->prop_ato));
242 MC_free_snapshot(pair->system_state);
247 void pair_reached_free_voidp(void *p){
248 pair_reached_free((mc_pair_reached_t) * (void **) p);
251 void MC_ddfs_init(void){
253 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
255 XBT_INFO("**************************************************");
256 XBT_INFO("Double-DFS init");
257 XBT_INFO("**************************************************");
259 mc_pair_stateless_t mc_initial_pair = NULL;
260 mc_state_t initial_graph_state = NULL;
261 smx_process_t process;
264 MC_wait_for_requests();
268 initial_graph_state = MC_state_pair_new();
269 xbt_swag_foreach(process, simix_global->process_list){
270 if(MC_process_is_enabled(process)){
271 MC_state_interleave_process(initial_graph_state, process);
275 reached_pairs = xbt_dynar_new(sizeof(mc_pair_reached_t), pair_reached_free_voidp);
276 successors = xbt_dynar_new(sizeof(mc_pair_stateless_t), NULL);
278 /* Save the initial state */
279 initial_snapshot_liveness = xbt_new0(s_mc_snapshot_t, 1);
280 MC_take_snapshot_liveness(initial_snapshot_liveness);
284 /* Get .plt section (start and end addresses) for data libsimgrid and data program comparison */
285 get_libsimgrid_plt_section();
286 get_binary_plt_section();
288 unsigned int cursor = 0;
291 xbt_dynar_foreach(_mc_property_automaton->states, cursor, state){
292 if(state->type == -1){
295 mc_initial_pair = new_pair_stateless(initial_graph_state, state, MC_state_interleave_size(initial_graph_state));
296 xbt_fifo_unshift(mc_stack_liveness, mc_initial_pair);
300 MC_restore_snapshot(initial_snapshot_liveness);
307 if(state->type == 2){
310 mc_initial_pair = new_pair_stateless(initial_graph_state, state, MC_state_interleave_size(initial_graph_state));
311 xbt_fifo_unshift(mc_stack_liveness, mc_initial_pair);
314 set_pair_reached(state);
317 MC_restore_snapshot(initial_snapshot_liveness);
336 void MC_ddfs(int search_cycle){
338 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
340 smx_process_t process;
341 mc_pair_stateless_t current_pair = NULL;
343 if(xbt_fifo_size(mc_stack_liveness) == 0)
347 /* Get current pair */
348 current_pair = (mc_pair_stateless_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack_liveness));
350 /* Update current state in buchi automaton */
351 _mc_property_automaton->current_state = current_pair->automaton_state;
354 XBT_INFO("********************* ( Depth = %d, search_cycle = %d )", xbt_fifo_size(mc_stack_liveness), search_cycle);
356 mc_stats_pair->visited_pairs++;
361 mc_state_t next_graph_state = NULL;
362 smx_simcall_t req = NULL;
365 xbt_transition_t transition_succ;
366 unsigned int cursor = 0;
369 mc_pair_stateless_t next_pair = NULL;
370 mc_pair_stateless_t pair_succ;
372 if(xbt_fifo_size(mc_stack_liveness) < MAX_DEPTH_LIVENESS){
374 if(current_pair->requests > 0){
376 while((req = MC_state_get_request(current_pair->graph_state, &value)) != NULL){
378 /* Debug information */
380 req_str = MC_request_to_string(req, value);
381 XBT_INFO("Execute: %s", req_str);
384 MC_state_set_executed_request(current_pair->graph_state, req, value);
386 /* Answer the request */
387 SIMIX_simcall_pre(req, value);
389 /* Wait for requests (schedules processes) */
390 MC_wait_for_requests();
394 /* Create the new expanded graph_state */
395 next_graph_state = MC_state_pair_new();
397 /* Get enabled process and insert it in the interleave set of the next graph_state */
398 xbt_swag_foreach(process, simix_global->process_list){
399 if(MC_process_is_enabled(process)){
400 MC_state_interleave_process(next_graph_state, process);
404 xbt_dynar_reset(successors);
410 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
412 res = MC_automaton_evaluate_label(transition_succ->label);
414 if(res == 1){ // enabled transition in automaton
416 next_pair = new_pair_stateless(next_graph_state, transition_succ->dst, MC_state_interleave_size(next_graph_state));
417 xbt_dynar_push(successors, &next_pair);
425 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
427 res = MC_automaton_evaluate_label(transition_succ->label);
429 if(res == 2){ // true transition in automaton
431 next_pair = new_pair_stateless(next_graph_state, transition_succ->dst, MC_state_interleave_size(next_graph_state));
432 xbt_dynar_push(successors, &next_pair);
440 xbt_dynar_foreach(successors, cursor, pair_succ){
442 if(search_cycle == 1){
444 if((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2)){
446 if(reached(pair_succ->automaton_state)){
448 XBT_INFO("Next pair (depth = %d, %u interleave) already reached !", xbt_fifo_size(mc_stack_liveness) + 1, MC_state_interleave_size(pair_succ->graph_state));
450 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
451 XBT_INFO("| ACCEPTANCE CYCLE |");
452 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
453 XBT_INFO("Counter-example that violates formula :");
454 MC_show_stack_liveness(mc_stack_liveness);
455 MC_dump_stack_liveness(mc_stack_liveness);
456 MC_print_statistics_pairs(mc_stats_pair);
461 XBT_INFO("Next pair (depth =%d) -> Acceptance pair : graph=%p, automaton=%p(%s)", xbt_fifo_size(mc_stack_liveness) + 1, pair_succ->graph_state, pair_succ->automaton_state, pair_succ->automaton_state->id);
463 XBT_INFO("Reached pairs : %lu", xbt_dynar_length(reached_pairs));
466 xbt_fifo_unshift(mc_stack_liveness, pair_succ);
469 MC_ddfs(search_cycle);
476 xbt_fifo_unshift(mc_stack_liveness, pair_succ);
479 MC_ddfs(search_cycle);
485 if(((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2))){
487 XBT_INFO("Next pair (depth =%d) -> Acceptance pair : graph=%p, automaton=%p(%s)", xbt_fifo_size(mc_stack_liveness) + 1, pair_succ->graph_state, pair_succ->automaton_state, pair_succ->automaton_state->id);
489 set_pair_reached(pair_succ->automaton_state);
493 XBT_INFO("Reached pairs : %lu", xbt_dynar_length(reached_pairs));
498 xbt_fifo_unshift(mc_stack_liveness, pair_succ);
501 MC_ddfs(search_cycle);
506 /* Restore system before checking others successors */
507 if(cursor != (xbt_dynar_length(successors) - 1))
508 MC_replay_liveness(mc_stack_liveness, 1);
513 if(MC_state_interleave_size(current_pair->graph_state) > 0){
514 XBT_INFO("Backtracking to depth %d", xbt_fifo_size(mc_stack_liveness));
515 MC_replay_liveness(mc_stack_liveness, 0);
520 }else{ /*No request to execute, search evolution in Büchi automaton */
524 /* Create the new expanded graph_state */
525 next_graph_state = MC_state_pair_new();
527 xbt_dynar_reset(successors);
533 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
535 res = MC_automaton_evaluate_label(transition_succ->label);
537 if(res == 1){ // enabled transition in automaton
539 next_pair = new_pair_stateless(next_graph_state, transition_succ->dst, MC_state_interleave_size(next_graph_state));
540 xbt_dynar_push(successors, &next_pair);
548 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
550 res = MC_automaton_evaluate_label(transition_succ->label);
552 if(res == 2){ // true transition in automaton
554 next_pair = new_pair_stateless(next_graph_state, transition_succ->dst, MC_state_interleave_size(next_graph_state));
555 xbt_dynar_push(successors, &next_pair);
563 xbt_dynar_foreach(successors, cursor, pair_succ){
565 if(search_cycle == 1){
567 if((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2)){
569 if(reached(pair_succ->automaton_state)){
571 XBT_INFO("Next pair (depth = %d) already reached !", xbt_fifo_size(mc_stack_liveness) + 1);
573 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
574 XBT_INFO("| ACCEPTANCE CYCLE |");
575 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
576 XBT_INFO("Counter-example that violates formula :");
577 MC_show_stack_liveness(mc_stack_liveness);
578 MC_dump_stack_liveness(mc_stack_liveness);
579 MC_print_statistics_pairs(mc_stats_pair);
584 XBT_INFO("Next pair (depth = %d) -> Acceptance pair : graph=%p, automaton=%p(%s)", xbt_fifo_size(mc_stack_liveness) + 1, pair_succ->graph_state, pair_succ->automaton_state, pair_succ->automaton_state->id);
586 XBT_INFO("Reached pairs : %lu", xbt_dynar_length(reached_pairs));
589 xbt_fifo_unshift(mc_stack_liveness, pair_succ);
592 MC_ddfs(search_cycle);
599 xbt_fifo_unshift(mc_stack_liveness, pair_succ);
602 MC_ddfs(search_cycle);
609 if(((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2))){
611 set_pair_reached(pair_succ->automaton_state);
615 XBT_INFO("Reached pairs : %lu", xbt_dynar_length(reached_pairs));
620 xbt_fifo_unshift(mc_stack_liveness, pair_succ);
623 MC_ddfs(search_cycle);
627 /* Restore system before checking others successors */
628 if(cursor != xbt_dynar_length(successors) - 1)
629 MC_replay_liveness(mc_stack_liveness, 1);
638 XBT_INFO("Max depth reached");
642 if(xbt_fifo_size(mc_stack_liveness) == MAX_DEPTH_LIVENESS ){
643 XBT_INFO("Pair (graph=%p, automaton =%p, search_cycle = %d, depth = %d) shifted in stack, maximum depth reached", current_pair->graph_state, current_pair->automaton_state, search_cycle, xbt_fifo_size(mc_stack_liveness) );
645 XBT_INFO("Pair (graph=%p, automaton =%p, search_cycle = %d, depth = %d) shifted in stack", current_pair->graph_state, current_pair->automaton_state, search_cycle, xbt_fifo_size(mc_stack_liveness) );
650 xbt_fifo_shift(mc_stack_liveness);
651 if((current_pair->automaton_state->type == 1) || (current_pair->automaton_state->type == 2)){
652 xbt_dynar_pop(reached_pairs, NULL);