4 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_liveness, mc,
5 "Logging specific to algorithms for liveness properties verification");
7 xbt_dynar_t initial_pairs = NULL;
8 xbt_dynar_t reached_pairs;
9 xbt_dynar_t successors = NULL;
10 extern mc_snapshot_t initial_snapshot;
12 /* Global variables for stateless algorithm */
13 mc_snapshot_t snapshot = NULL;
15 /* Global variables for stateful algorithm */
16 mc_snapshot_t next_snapshot = NULL;
17 mc_snapshot_t current_snapshot = NULL;
20 mc_pair_t new_pair(mc_snapshot_t sn, mc_state_t sg, xbt_state_t st){
22 p = xbt_new0(s_mc_pair_t, 1);
24 p->automaton_state = st;
26 mc_stats_pair->expanded_pairs++;
31 int snapshot_compare(mc_snapshot_t s1, mc_snapshot_t s2){
33 if(s1->num_reg != s2->num_reg)
38 for(i=0 ; i< s1->num_reg ; i++){
40 if(s1->regions[i]->size != s2->regions[i]->size)
43 if(s1->regions[i]->start_addr != s2->regions[i]->start_addr)
46 if(s1->regions[i]->type != s2->regions[i]->type)
49 if(s1->regions[i]->type == 0){
50 if(mmalloc_compare_heap(s1->regions[i]->start_addr, s2->regions[i]->start_addr)){
51 XBT_DEBUG("Different heap (mmalloc_compare)");
56 if(memcmp(s1->regions[i]->data, s2->regions[i]->data, s1->regions[i]->size) != 0){
57 XBT_DEBUG("Different memcmp for data in libsimgrid");
70 int reached(xbt_automaton_t a, xbt_state_t st, mc_snapshot_t s){
73 if(xbt_dynar_is_empty(reached_pairs)){
78 xbt_dynar_t prop_ato = xbt_dynar_new(sizeof(int), NULL);
80 /* Get values of propositional symbols */
81 unsigned int cursor = 0;
82 xbt_propositional_symbol_t ps = NULL;
83 xbt_dynar_foreach(a->propositional_symbols, cursor, ps){
84 int (*f)() = ps->function;
86 xbt_dynar_push_as(prop_ato, int, res);
90 mc_pair_reached_t pair_test;
92 xbt_dynar_foreach(reached_pairs, cursor, pair_test){
93 if(automaton_state_compare(pair_test->automaton_state, st) == 0){
94 if(propositional_symbols_compare_value(pair_test->prop_ato, prop_ato) == 0){
95 if(snapshot_compare(pair_test->system_state, s) == 0){
109 void set_pair_reached(xbt_automaton_t a, xbt_state_t st, mc_snapshot_t sn){
114 mc_pair_reached_t pair = NULL;
115 pair = xbt_new0(s_mc_pair_reached_t, 1);
116 pair->automaton_state = st;
117 pair->prop_ato = xbt_dynar_new(sizeof(int), NULL);
118 pair->system_state = sn;
120 /* Get values of propositional symbols */
121 unsigned int cursor = 0;
122 xbt_propositional_symbol_t ps = NULL;
123 xbt_dynar_foreach(a->propositional_symbols, cursor, ps){
124 int (*f)() = ps->function;
126 xbt_dynar_push_as(pair->prop_ato, int, res);
129 xbt_dynar_push(reached_pairs, &pair);
136 void MC_pair_delete(mc_pair_t pair){
137 xbt_free(pair->graph_state->proc_status);
138 xbt_free(pair->graph_state);
139 //xbt_free(pair->automaton_state); -> FIXME : à implémenter
145 int MC_automaton_evaluate_label(xbt_automaton_t a, xbt_exp_label_t l){
149 int left_res = MC_automaton_evaluate_label(a, l->u.or_and.left_exp);
150 int right_res = MC_automaton_evaluate_label(a, l->u.or_and.right_exp);
151 return (left_res || right_res);
155 int left_res = MC_automaton_evaluate_label(a, l->u.or_and.left_exp);
156 int right_res = MC_automaton_evaluate_label(a, l->u.or_and.right_exp);
157 return (left_res && right_res);
161 int res = MC_automaton_evaluate_label(a, l->u.exp_not);
166 unsigned int cursor = 0;
167 xbt_propositional_symbol_t p = NULL;
168 xbt_dynar_foreach(a->propositional_symbols, cursor, p){
169 if(strcmp(p->pred, l->u.predicat) == 0){
170 int (*f)() = p->function;
190 /********************* Double-DFS stateless *******************/
192 void MC_pair_stateless_delete(mc_pair_stateless_t pair){
193 xbt_free(pair->graph_state->proc_status);
194 xbt_free(pair->graph_state);
195 //xbt_free(pair->automaton_state); -> FIXME : à implémenter
199 mc_pair_stateless_t new_pair_stateless(mc_state_t sg, xbt_state_t st){
200 mc_pair_stateless_t p = NULL;
201 p = xbt_new0(s_mc_pair_stateless_t, 1);
202 p->automaton_state = st;
204 mc_stats_pair->expanded_pairs++;
210 void MC_ddfs_stateless_init(xbt_automaton_t a){
212 XBT_DEBUG("**************************************************");
213 XBT_DEBUG("Double-DFS stateless init");
214 XBT_DEBUG("**************************************************");
216 mc_pair_stateless_t mc_initial_pair = NULL;
217 mc_state_t initial_graph_state = NULL;
218 smx_process_t process;
220 MC_wait_for_requests();
224 initial_graph_state = MC_state_pair_new();
225 xbt_swag_foreach(process, simix_global->process_list){
226 if(MC_process_is_enabled(process)){
227 MC_state_interleave_process(initial_graph_state, process);
231 reached_pairs = xbt_dynar_new(sizeof(mc_pair_reached_t), NULL);
232 successors = xbt_dynar_new(sizeof(mc_pair_stateless_t), NULL);
233 snapshot = xbt_new0(s_mc_snapshot_t, 1);
235 initial_snapshot = xbt_new0(s_mc_snapshot_t, 1);
236 MC_take_snapshot(initial_snapshot);
240 unsigned int cursor = 0;
243 xbt_dynar_foreach(a->states, cursor, state){
244 if(state->type == -1){
247 mc_initial_pair = new_pair_stateless(initial_graph_state, state);
248 xbt_fifo_unshift(mc_stack_liveness_stateless, mc_initial_pair);
252 MC_ddfs_stateless(a, 0, 0);
254 MC_restore_snapshot(initial_snapshot);
256 MC_ddfs_stateless(a, 0, 0);
259 if(state->type == 2){
262 mc_initial_pair = new_pair_stateless(initial_graph_state, state);
263 xbt_fifo_unshift(mc_stack_liveness_stateless, mc_initial_pair);
267 MC_ddfs_stateless(a, 1, 0);
269 MC_restore_snapshot(initial_snapshot);
271 MC_ddfs_stateless(a, 1, 0);
280 void MC_ddfs_stateless(xbt_automaton_t a, int search_cycle, int replay){
282 smx_process_t process;
283 mc_pair_stateless_t current_pair = NULL;
285 if(xbt_fifo_size(mc_stack_liveness_stateless) == 0)
289 MC_replay_liveness(mc_stack_liveness_stateless);
290 current_pair = (mc_pair_stateless_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack_liveness_stateless));
291 xbt_swag_foreach(process, simix_global->process_list){
292 if(MC_process_is_enabled(process)){
293 MC_state_interleave_process(current_pair->graph_state, process);
298 /* Get current pair */
299 current_pair = (mc_pair_stateless_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack_liveness_stateless));
301 /* Update current state in buchi automaton */
302 a->current_state = current_pair->automaton_state;
305 XBT_DEBUG("********************* ( Depth = %d, search_cycle = %d )", xbt_fifo_size(mc_stack_liveness_stateless), search_cycle);
306 XBT_DEBUG("Pair : graph=%p, automaton=%p(%s), %u interleave", current_pair->graph_state, current_pair->automaton_state, current_pair->automaton_state->id, MC_state_interleave_size(current_pair->graph_state));
310 mc_stats_pair->visited_pairs++;
313 mc_state_t next_graph_state = NULL;
314 smx_req_t req = NULL;
317 xbt_transition_t transition_succ;
318 unsigned int cursor = 0;
321 mc_pair_stateless_t next_pair = NULL;
322 mc_pair_stateless_t pair_succ;
323 //mc_snapshot_t next_snapshot = NULL;
325 while((req = MC_state_get_request(current_pair->graph_state, &value)) != NULL){
327 /* Debug information */
328 if(XBT_LOG_ISENABLED(mc_liveness, xbt_log_priority_debug)){
329 req_str = MC_request_to_string(req, value);
330 XBT_DEBUG("Execute: %s", req_str);
336 MC_state_set_executed_request(current_pair->graph_state, req, value);
338 /* Answer the request */
339 SIMIX_request_pre(req, value);
341 /* Wait for requests (schedules processes) */
342 MC_wait_for_requests();
347 /* Create the new expanded graph_state */
348 next_graph_state = MC_state_pair_new();
350 /* Get enabled process and insert it in the interleave set of the next graph_state */
351 xbt_swag_foreach(process, simix_global->process_list){
352 if(MC_process_is_enabled(process)){
353 MC_state_interleave_process(next_graph_state, process);
357 xbt_dynar_reset(successors);
363 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
365 res = MC_automaton_evaluate_label(a, transition_succ->label);
367 if(res == 1){ // enabled transition in automaton
369 next_pair = new_pair_stateless(next_graph_state, transition_succ->dst);
370 xbt_dynar_push(successors, &next_pair);
378 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
380 res = MC_automaton_evaluate_label(a, transition_succ->label);
382 if(res == 2){ // true transition in automaton
384 next_pair = new_pair_stateless(next_graph_state, transition_succ->dst);
385 xbt_dynar_push(successors, &next_pair);
392 if(xbt_dynar_length(successors) == 0){
394 next_pair = new_pair_stateless(next_graph_state, current_pair->automaton_state);
395 xbt_dynar_push(successors, &next_pair);
401 xbt_dynar_foreach(successors, cursor, pair_succ){
405 if((search_cycle == 1) && ((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2)) && (MC_state_interleave_size(pair_succ->graph_state) > 0)){
407 //XBT_DEBUG("Acceptance pair : graph=%p, automaton=%p(%s)", pair_succ->graph_state, pair_succ->automaton_state, pair_succ->automaton_state->id);
410 MC_take_snapshot(snapshot);
413 if(reached(a, pair_succ->automaton_state, snapshot) == 1){
414 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
415 XBT_INFO("| ACCEPTANCE CYCLE |");
416 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
417 XBT_INFO("Counter-example that violates formula :");
418 MC_show_stack_liveness_stateless(mc_stack_liveness_stateless);
419 MC_dump_stack_liveness_stateless(mc_stack_liveness_stateless);
420 MC_print_statistics_pairs(mc_stats_pair);
426 xbt_fifo_unshift(mc_stack_liveness_stateless, pair_succ);
430 MC_ddfs_stateless(a, search_cycle, 0);
432 /* If pair_succ is the last state of the execution (0 interleave), no acceptance cycle possible */
433 if((search_cycle == 0) && ((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2)) && (MC_state_interleave_size(pair_succ->graph_state) > 0)){
435 XBT_DEBUG("Acceptance pair : graph=%p, automaton=%p(%s)", pair_succ->graph_state, pair_succ->automaton_state, pair_succ->automaton_state->id);
438 MC_take_snapshot(snapshot);
441 set_pair_reached(a, pair_succ->automaton_state, snapshot);
444 /* Pair shifted from stack when first MC_ddfs finished and returned at this point */
446 xbt_fifo_unshift(mc_stack_liveness_stateless, pair_succ);
449 MC_ddfs_stateless(a, 1, 1);
451 /* No acceptance cycle with this acceptance pair, we remove it from the list reached_pairs */
453 xbt_dynar_pop(reached_pairs, NULL);
459 if(MC_state_interleave_size(current_pair->graph_state) > 0){
460 XBT_DEBUG("Backtracking to depth %u", xbt_fifo_size(mc_stack_liveness_stateless));
461 MC_replay_liveness(mc_stack_liveness_stateless);
467 xbt_fifo_shift(mc_stack_liveness_stateless);
468 XBT_DEBUG("Pair (graph=%p, automaton =%p, search_cycle = %u) shifted in stack", current_pair->graph_state, current_pair->automaton_state, search_cycle);
473 /********************* Double-DFS stateful without visited state *******************/
476 void MC_ddfs_stateful_init(xbt_automaton_t a){
478 XBT_DEBUG("**************************************************");
479 XBT_DEBUG("Double-DFS stateful without visited state init");
480 XBT_DEBUG("**************************************************");
482 mc_pair_t mc_initial_pair;
483 mc_state_t initial_graph_state;
484 smx_process_t process;
485 mc_snapshot_t init_snapshot;
487 MC_wait_for_requests();
491 initial_graph_state = MC_state_pair_new();
492 xbt_swag_foreach(process, simix_global->process_list){
493 if(MC_process_is_enabled(process)){
494 MC_state_interleave_process(initial_graph_state, process);
498 reached_pairs = xbt_dynar_new(sizeof(mc_pair_reached_t), NULL);
499 successors = xbt_dynar_new(sizeof(mc_pair_t), NULL);
500 current_snapshot = xbt_new0(s_mc_snapshot_t, 1);
501 next_snapshot = xbt_new0(s_mc_snapshot_t, 1);
503 init_snapshot = xbt_new0(s_mc_snapshot_t, 1);
504 MC_take_snapshot(init_snapshot);
508 unsigned int cursor = 0;
509 xbt_state_t state = NULL;
511 xbt_dynar_foreach(a->states, cursor, state){
512 if(state->type == -1){
515 mc_initial_pair = new_pair(init_snapshot, initial_graph_state, state);
516 xbt_fifo_unshift(mc_stack_liveness_stateful, mc_initial_pair);
520 MC_ddfs_stateful(a, 0, 0);
522 MC_restore_snapshot(init_snapshot);
524 MC_ddfs_stateful(a, 0, 0);
527 if(state->type == 2){
530 mc_initial_pair = new_pair(init_snapshot, initial_graph_state, state);
531 xbt_fifo_unshift(mc_stack_liveness_stateful, mc_initial_pair);
535 MC_ddfs_stateful(a, 1, 0);
537 MC_restore_snapshot(init_snapshot);
539 MC_ddfs_stateful(a, 1, 0);
547 void MC_ddfs_stateful(xbt_automaton_t a, int search_cycle, int restore){
549 smx_process_t process = NULL;
550 mc_pair_t current_pair = NULL;
552 if(xbt_fifo_size(mc_stack_liveness_stateful) == 0)
556 current_pair = (mc_pair_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack_liveness_stateful));
557 MC_restore_snapshot(current_pair->system_state);
558 xbt_swag_foreach(process, simix_global->process_list){
559 if(MC_process_is_enabled(process)){
560 MC_state_interleave_process(current_pair->graph_state, process);
566 /* Get current state */
567 current_pair = (mc_pair_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack_liveness_stateful));
570 XBT_DEBUG("********************* ( Depth = %d, search_cycle = %d )", xbt_fifo_size(mc_stack_liveness_stateful), search_cycle);
571 XBT_DEBUG("Pair : graph=%p, automaton=%p(%s), %u interleave", current_pair->graph_state, current_pair->automaton_state, current_pair->automaton_state->id,MC_state_interleave_size(current_pair->graph_state));
573 a->current_state = current_pair->automaton_state;
577 mc_stats_pair->visited_pairs++;
580 mc_state_t next_graph_state = NULL;
581 smx_req_t req = NULL;
585 xbt_transition_t transition_succ;
589 mc_pair_t next_pair = NULL;
591 while((req = MC_state_get_request(current_pair->graph_state, &value)) != NULL){
594 MC_take_snapshot(current_snapshot);
597 /* Debug information */
598 if(XBT_LOG_ISENABLED(mc_liveness, xbt_log_priority_debug)){
599 req_str = MC_request_to_string(req, value);
600 XBT_DEBUG("Pair : graph=%p, automaton=%p(%s), %u interleave", current_pair->graph_state, current_pair->automaton_state, current_pair->automaton_state->id,MC_state_interleave_size(current_pair->graph_state));
601 XBT_DEBUG("Execute: %s", req_str);
605 MC_state_set_executed_request(current_pair->graph_state, req, value);
607 /* Answer the request */
608 SIMIX_request_pre(req, value);
610 /* Wait for requests (schedules processes) */
611 MC_wait_for_requests();
614 /* Create the new expanded graph_state */
617 next_graph_state = MC_state_pair_new();
619 /* Get enabled process and insert it in the interleave set of the next graph_state */
620 xbt_swag_foreach(process, simix_global->process_list){
621 if(MC_process_is_enabled(process)){
622 MC_state_interleave_process(next_graph_state, process);
626 MC_take_snapshot(next_snapshot);
628 xbt_dynar_reset(successors);
634 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
636 res = MC_automaton_evaluate_label(a, transition_succ->label);
638 if(res == 1){ // enabled transition in automaton
640 next_pair = new_pair(next_snapshot,next_graph_state, transition_succ->dst);
641 xbt_dynar_push(successors, &next_pair);
648 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
650 res = MC_automaton_evaluate_label(a, transition_succ->label);
652 if(res == 2){ // transition always enabled in automaton
654 next_pair = new_pair(next_snapshot,next_graph_state, transition_succ->dst);
655 xbt_dynar_push(successors, &next_pair);
663 if(xbt_dynar_length(successors) == 0){
666 next_pair = new_pair(next_snapshot, next_graph_state, current_pair->automaton_state);
667 xbt_dynar_push(successors, &next_pair);
673 xbt_dynar_foreach(successors, cursor, pair_succ){
675 if((search_cycle == 1) && ((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2))){
677 if(reached(a, pair_succ->automaton_state, next_snapshot) == 1){
678 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
679 XBT_INFO("| ACCEPTANCE CYCLE |");
680 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
681 XBT_INFO("Counter-example that violates formula :");
682 MC_show_stack_liveness_stateful(mc_stack_liveness_stateful);
683 MC_dump_stack_liveness_stateful(mc_stack_liveness_stateful);
684 MC_print_statistics_pairs(mc_stats_pair);
691 xbt_fifo_unshift(mc_stack_liveness_stateful, pair_succ);
694 MC_ddfs_stateful(a, search_cycle, 0);
697 if((search_cycle == 0) && ((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2))){
699 XBT_DEBUG("Acceptance pair : graph=%p, automaton=%p(%s)", pair_succ->graph_state, pair_succ->automaton_state, pair_succ->automaton_state->id);
700 set_pair_reached(a, pair_succ->automaton_state, next_snapshot);
703 xbt_fifo_unshift(mc_stack_liveness_stateful, pair_succ);
706 MC_ddfs_stateful(a, 1, 1);
710 xbt_dynar_pop(reached_pairs, NULL);
716 if(MC_state_interleave_size(current_pair->graph_state) > 0){
717 XBT_DEBUG("Backtracking to depth %u", xbt_fifo_size(mc_stack_liveness_stateful));
718 MC_restore_snapshot(current_snapshot);
726 xbt_fifo_shift(mc_stack_liveness_stateful);
727 XBT_DEBUG("Pair (graph=%p, automaton =%p) shifted in stack", current_pair->graph_state, current_pair->automaton_state);