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 visited_pairs;
15 xbt_dynar_t successors;
17 int create_dump(int pair)
19 // Try to enable core dumps
20 struct rlimit core_limit;
21 core_limit.rlim_cur = RLIM_INFINITY;
22 core_limit.rlim_max = RLIM_INFINITY;
24 if(setrlimit(RLIMIT_CORE, &core_limit) < 0)
25 fprintf(stderr, "setrlimit: %s\nWarning: core dumps may be truncated or non-existant\n", strerror(errno));
30 // We are the child process -- run the actual program
35 // An error occurred, shouldn't happen
40 // We are the parent process -- wait for the child process to exit
43 if(WIFSIGNALED(status) && WCOREDUMP(status)){
44 char *core_name = xbt_malloc(20);
45 sprintf(core_name,"core_%d", pair);
46 rename("core", core_name);
54 int reached(xbt_state_t st){
56 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
60 mc_pair_reached_t new_pair = NULL;
61 new_pair = xbt_new0(s_mc_pair_reached_t, 1);
62 new_pair->nb = xbt_dynar_length(reached_pairs) + 1;
63 new_pair->automaton_state = st;
64 new_pair->prop_ato = xbt_dynar_new(sizeof(int), NULL);
65 new_pair->system_state = MC_take_snapshot();
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);
97 mc_pair_reached_t pair_test = NULL;
99 xbt_dynar_foreach(reached_pairs, cursor, pair_test){
100 if(XBT_LOG_ISENABLED(mc_liveness, xbt_log_priority_debug))
101 XBT_DEBUG("****** Pair reached #%d ******", pair_test->nb);
102 if(automaton_state_compare(pair_test->automaton_state, st) == 0){
103 if(propositional_symbols_compare_value(pair_test->prop_ato, new_pair->prop_ato) == 0){
104 if(snapshot_compare(new_pair->system_state, pair_test->system_state) == 0){
114 XBT_DEBUG("Different values of propositional symbols");
117 XBT_DEBUG("Different automaton state");
121 /* New pair reached */
122 xbt_dynar_push(reached_pairs, &new_pair);
137 void set_pair_reached(xbt_state_t st){
139 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
143 mc_pair_reached_t pair = NULL;
144 pair = xbt_new0(s_mc_pair_reached_t, 1);
145 pair->nb = xbt_dynar_length(reached_pairs) + 1;
146 pair->automaton_state = st;
147 pair->prop_ato = xbt_dynar_new(sizeof(int), NULL);
148 pair->system_state = MC_take_snapshot();
150 /* Get values of propositional symbols */
151 unsigned int cursor = 0;
152 xbt_propositional_symbol_t ps = NULL;
156 xbt_dynar_foreach(_mc_property_automaton->propositional_symbols, cursor, ps){
157 f = (int_f_void_t)ps->function;
159 xbt_dynar_push_as(pair->prop_ato, int, res);
162 xbt_dynar_push(reached_pairs, &pair);
171 int visited(xbt_state_t st){
173 if(_sg_mc_visited == 0)
176 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
180 mc_pair_visited_t new_pair = NULL;
181 new_pair = xbt_new0(s_mc_pair_visited_t, 1);
182 new_pair->automaton_state = st;
183 new_pair->prop_ato = xbt_dynar_new(sizeof(int), NULL);
184 new_pair->system_state = MC_take_snapshot();
186 /* Get values of propositional symbols */
189 unsigned int cursor = 0;
190 xbt_propositional_symbol_t ps = NULL;
191 xbt_dynar_foreach(_mc_property_automaton->propositional_symbols, cursor, ps){
192 f = (int_f_void_t)ps->function;
194 xbt_dynar_push_as(new_pair->prop_ato, int, res);
199 if(xbt_dynar_is_empty(visited_pairs)){
202 /* New pair visited */
203 xbt_dynar_push(visited_pairs, &new_pair);
216 mc_pair_visited_t pair_test = NULL;
218 xbt_dynar_foreach(visited_pairs, cursor, pair_test){
219 if(XBT_LOG_ISENABLED(mc_liveness, xbt_log_priority_debug))
220 XBT_DEBUG("****** Pair visited #%d ******", cursor + 1);
221 if(automaton_state_compare(pair_test->automaton_state, st) == 0){
222 if(propositional_symbols_compare_value(pair_test->prop_ato, new_pair->prop_ato) == 0){
223 if(snapshot_compare(new_pair->system_state, pair_test->system_state) == 0){
232 XBT_DEBUG("Different values of propositional symbols");
235 XBT_DEBUG("Different automaton state");
239 if(xbt_dynar_length(visited_pairs) == _sg_mc_visited){
240 xbt_dynar_remove_at(visited_pairs, 0, NULL);
243 /* New pair visited */
244 xbt_dynar_push(visited_pairs, &new_pair);
256 void MC_pair_delete(mc_pair_t pair){
257 xbt_free(pair->graph_state->proc_status);
258 xbt_free(pair->graph_state);
264 int MC_automaton_evaluate_label(xbt_exp_label_t l){
268 int left_res = MC_automaton_evaluate_label(l->u.or_and.left_exp);
269 int right_res = MC_automaton_evaluate_label(l->u.or_and.right_exp);
270 return (left_res || right_res);
273 int left_res = MC_automaton_evaluate_label(l->u.or_and.left_exp);
274 int right_res = MC_automaton_evaluate_label(l->u.or_and.right_exp);
275 return (left_res && right_res);
278 int res = MC_automaton_evaluate_label(l->u.exp_not);
282 unsigned int cursor = 0;
283 xbt_propositional_symbol_t p = NULL;
285 xbt_dynar_foreach(_mc_property_automaton->propositional_symbols, cursor, p){
286 if(strcmp(p->pred, l->u.predicat) == 0){
287 f = (int_f_void_t)p->function;
302 /********************* Double-DFS stateless *******************/
304 void pair_visited_free(mc_pair_visited_t pair){
306 pair->automaton_state = NULL;
307 xbt_dynar_free(&(pair->prop_ato));
308 MC_free_snapshot(pair->system_state);
313 void pair_visited_free_voidp(void *p){
314 pair_visited_free((mc_pair_visited_t) * (void **) p);
317 void pair_stateless_free(mc_pair_stateless_t pair){
318 xbt_free(pair->graph_state->system_state);
319 xbt_free(pair->graph_state->proc_status);
320 xbt_free(pair->graph_state);
324 void pair_stateless_free_voidp(void *p){
325 pair_stateless_free((mc_pair_stateless_t) * (void **) p);
328 mc_pair_stateless_t new_pair_stateless(mc_state_t sg, xbt_state_t st, int r){
329 mc_pair_stateless_t p = NULL;
330 p = xbt_new0(s_mc_pair_stateless_t, 1);
331 p->automaton_state = st;
334 mc_stats_pair->expanded_pairs++;
338 void pair_reached_free(mc_pair_reached_t pair){
340 pair->automaton_state = NULL;
341 xbt_dynar_free(&(pair->prop_ato));
342 MC_free_snapshot(pair->system_state);
347 void pair_reached_free_voidp(void *p){
348 pair_reached_free((mc_pair_reached_t) * (void **) p);
351 void MC_ddfs_init(void){
353 initial_state_liveness->raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
355 XBT_DEBUG("**************************************************");
356 XBT_DEBUG("Double-DFS init");
357 XBT_DEBUG("**************************************************");
359 mc_pair_stateless_t mc_initial_pair = NULL;
360 mc_state_t initial_graph_state = NULL;
361 smx_process_t process;
364 MC_wait_for_requests();
368 initial_graph_state = MC_state_pair_new();
369 xbt_swag_foreach(process, simix_global->process_list){
370 if(MC_process_is_enabled(process)){
371 MC_state_interleave_process(initial_graph_state, process);
375 reached_pairs = xbt_dynar_new(sizeof(mc_pair_reached_t), pair_reached_free_voidp);
376 visited_pairs = xbt_dynar_new(sizeof(mc_pair_visited_t), pair_visited_free_voidp);
377 successors = xbt_dynar_new(sizeof(mc_pair_stateless_t), NULL);
379 /* Save the initial state */
380 initial_state_liveness->snapshot = MC_take_snapshot();
384 unsigned int cursor = 0;
387 xbt_dynar_foreach(_mc_property_automaton->states, cursor, state){
388 if(state->type == -1){
391 mc_initial_pair = new_pair_stateless(initial_graph_state, state, MC_state_interleave_size(initial_graph_state));
392 xbt_fifo_unshift(mc_stack_liveness, mc_initial_pair);
396 MC_restore_snapshot(initial_state_liveness->snapshot);
403 if(state->type == 2){
406 mc_initial_pair = new_pair_stateless(initial_graph_state, state, MC_state_interleave_size(initial_graph_state));
407 xbt_fifo_unshift(mc_stack_liveness, mc_initial_pair);
410 set_pair_reached(state);
413 MC_restore_snapshot(initial_state_liveness->snapshot);
423 if(initial_state_liveness->raw_mem_set)
432 void MC_ddfs(int search_cycle){
434 //initial_state_liveness->raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
436 smx_process_t process;
437 mc_pair_stateless_t current_pair = NULL;
439 if(xbt_fifo_size(mc_stack_liveness) == 0)
443 /* Get current pair */
444 current_pair = (mc_pair_stateless_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack_liveness));
446 /* Update current state in buchi automaton */
447 _mc_property_automaton->current_state = current_pair->automaton_state;
450 XBT_DEBUG("********************* ( Depth = %d, search_cycle = %d )", xbt_fifo_size(mc_stack_liveness), search_cycle);
452 mc_stats_pair->visited_pairs++;
457 mc_state_t next_graph_state = NULL;
458 smx_simcall_t req = NULL;
461 xbt_transition_t transition_succ;
462 unsigned int cursor = 0;
465 mc_pair_stateless_t next_pair = NULL;
466 mc_pair_stateless_t pair_succ;
468 mc_pair_stateless_t remove_pair;
469 mc_pair_reached_t remove_pair_reached;
471 if(xbt_fifo_size(mc_stack_liveness) < _sg_mc_max_depth){
473 if(current_pair->requests > 0){
475 while((req = MC_state_get_request(current_pair->graph_state, &value)) != NULL){
477 /* Debug information */
479 req_str = MC_request_to_string(req, value);
480 XBT_DEBUG("Execute: %s", req_str);
483 MC_state_set_executed_request(current_pair->graph_state, req, value);
485 /* Answer the request */
486 SIMIX_simcall_pre(req, value);
488 /* Wait for requests (schedules processes) */
489 MC_wait_for_requests();
493 /* Create the new expanded graph_state */
494 next_graph_state = MC_state_pair_new();
496 /* Get enabled process and insert it in the interleave set of the next graph_state */
497 xbt_swag_foreach(process, simix_global->process_list){
498 if(MC_process_is_enabled(process)){
499 XBT_DEBUG("Process %lu enabled with simcall : %d", process->pid, (&process->simcall)->call);
503 xbt_swag_foreach(process, simix_global->process_list){
504 if(MC_process_is_enabled(process)){
505 MC_state_interleave_process(next_graph_state, process);
509 xbt_dynar_reset(successors);
515 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
517 res = MC_automaton_evaluate_label(transition_succ->label);
519 if(res == 1){ // enabled transition in automaton
521 next_pair = new_pair_stateless(next_graph_state, transition_succ->dst, MC_state_interleave_size(next_graph_state));
522 xbt_dynar_push(successors, &next_pair);
530 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
532 res = MC_automaton_evaluate_label(transition_succ->label);
534 if(res == 2){ // true transition in automaton
536 next_pair = new_pair_stateless(next_graph_state, transition_succ->dst, MC_state_interleave_size(next_graph_state));
537 xbt_dynar_push(successors, &next_pair);
545 xbt_dynar_foreach(successors, cursor, pair_succ){
547 if(search_cycle == 1){
549 if((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2)){
551 if(reached(pair_succ->automaton_state)){
553 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));
555 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
556 XBT_INFO("| ACCEPTANCE CYCLE |");
557 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
558 XBT_INFO("Counter-example that violates formula :");
559 MC_show_stack_liveness(mc_stack_liveness);
560 MC_dump_stack_liveness(mc_stack_liveness);
561 MC_print_statistics_pairs(mc_stats_pair);
566 if(visited(pair_succ->automaton_state)){
568 XBT_DEBUG("Next pair already visited !");
573 XBT_DEBUG("Next pair (depth =%d) -> Acceptance pair (%s)", xbt_fifo_size(mc_stack_liveness) + 1, pair_succ->automaton_state->id);
575 XBT_DEBUG("Reached pairs : %lu", xbt_dynar_length(reached_pairs));
578 xbt_fifo_unshift(mc_stack_liveness, pair_succ);
581 MC_ddfs(search_cycle);
589 if(visited(pair_succ->automaton_state)){
591 XBT_DEBUG("Next pair already visited !");
597 xbt_fifo_unshift(mc_stack_liveness, pair_succ);
600 MC_ddfs(search_cycle);
607 if(visited(pair_succ->automaton_state)){
609 XBT_DEBUG("Next pair already visited !");
614 if(((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2))){
616 XBT_DEBUG("Next pair (depth =%d) -> Acceptance pair (%s)", xbt_fifo_size(mc_stack_liveness) + 1, pair_succ->automaton_state->id);
618 set_pair_reached(pair_succ->automaton_state);
622 XBT_DEBUG("Reached pairs : %lu", xbt_dynar_length(reached_pairs));
627 xbt_fifo_unshift(mc_stack_liveness, pair_succ);
630 MC_ddfs(search_cycle);
636 /* Restore system before checking others successors */
637 if(cursor != (xbt_dynar_length(successors) - 1))
638 MC_replay_liveness(mc_stack_liveness, 1);
642 if(MC_state_interleave_size(current_pair->graph_state) > 0){
643 XBT_DEBUG("Backtracking to depth %d", xbt_fifo_size(mc_stack_liveness));
644 MC_replay_liveness(mc_stack_liveness, 0);
651 XBT_DEBUG("No more request to execute in this state, search evolution in Büchi Automaton.");
655 /* Create the new expanded graph_state */
656 next_graph_state = MC_state_pair_new();
658 xbt_dynar_reset(successors);
664 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
666 res = MC_automaton_evaluate_label(transition_succ->label);
668 if(res == 1){ // enabled transition in automaton
670 next_pair = new_pair_stateless(next_graph_state, transition_succ->dst, MC_state_interleave_size(next_graph_state));
671 xbt_dynar_push(successors, &next_pair);
679 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
681 res = MC_automaton_evaluate_label(transition_succ->label);
683 if(res == 2){ // true transition in automaton
685 next_pair = new_pair_stateless(next_graph_state, transition_succ->dst, MC_state_interleave_size(next_graph_state));
686 xbt_dynar_push(successors, &next_pair);
694 xbt_dynar_foreach(successors, cursor, pair_succ){
696 if(search_cycle == 1){
698 if((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2)){
700 if(reached(pair_succ->automaton_state)){
702 XBT_INFO("Next pair (depth = %d) already reached !", xbt_fifo_size(mc_stack_liveness) + 1);
704 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
705 XBT_INFO("| ACCEPTANCE CYCLE |");
706 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
707 XBT_INFO("Counter-example that violates formula :");
708 MC_show_stack_liveness(mc_stack_liveness);
709 MC_dump_stack_liveness(mc_stack_liveness);
710 MC_print_statistics_pairs(mc_stats_pair);
715 XBT_INFO("Next pair (depth = %d) -> Acceptance pair (%s)", xbt_fifo_size(mc_stack_liveness) + 1, pair_succ->automaton_state->id);
717 XBT_INFO("Reached pairs : %lu", xbt_dynar_length(reached_pairs));
720 xbt_fifo_unshift(mc_stack_liveness, pair_succ);
723 MC_ddfs(search_cycle);
730 xbt_fifo_unshift(mc_stack_liveness, pair_succ);
733 MC_ddfs(search_cycle);
740 if(((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2))){
742 set_pair_reached(pair_succ->automaton_state);
746 XBT_INFO("Reached pairs : %lu", xbt_dynar_length(reached_pairs));
751 xbt_fifo_unshift(mc_stack_liveness, pair_succ);
754 MC_ddfs(search_cycle);
758 /* Restore system before checking others successors */
759 if(cursor != xbt_dynar_length(successors) - 1)
760 MC_replay_liveness(mc_stack_liveness, 1);
768 XBT_WARN("/!\\ Max depth reached ! /!\\ ");
769 if(current_pair->requests > 0){
770 XBT_WARN("/!\\ But, there are still processes to interleave. Model-checker will not be able to ensure the soundness of the verification from now. /!\\ ");
771 XBT_WARN("Notice : the default value of max depth is 1000 but you can change it with cfg=model-check/max_depth:value.");
776 if(xbt_fifo_size(mc_stack_liveness) == _sg_mc_max_depth ){
777 XBT_DEBUG("Pair (depth = %d) shifted in stack, maximum depth reached", xbt_fifo_size(mc_stack_liveness) );
779 XBT_DEBUG("Pair (depth = %d) shifted in stack", xbt_fifo_size(mc_stack_liveness) );
784 remove_pair = xbt_fifo_shift(mc_stack_liveness);
785 xbt_fifo_remove(mc_stack_liveness, remove_pair);
787 if((current_pair->automaton_state->type == 1) || (current_pair->automaton_state->type == 2)){
788 remove_pair_reached = xbt_dynar_pop_as(reached_pairs, mc_pair_reached_t);
789 pair_reached_free(remove_pair_reached);
790 remove_pair_reached = NULL;
794 /*if(initial_state_liveness->raw_mem_set)