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));
309 mc_stats_pair->visited_pairs++;
312 mc_state_t next_graph_state = NULL;
313 smx_req_t req = NULL;
316 xbt_transition_t transition_succ;
317 unsigned int cursor = 0;
320 mc_pair_stateless_t next_pair = NULL;
321 mc_pair_stateless_t pair_succ;
322 //mc_snapshot_t next_snapshot = NULL;
324 while((req = MC_state_get_request(current_pair->graph_state, &value)) != NULL){
326 /* Debug information */
327 if(XBT_LOG_ISENABLED(mc_liveness, xbt_log_priority_debug)){
328 req_str = MC_request_to_string(req, value);
329 XBT_DEBUG("Execute: %s", req_str);
335 MC_state_set_executed_request(current_pair->graph_state, req, value);
337 /* Answer the request */
338 SIMIX_request_pre(req, value);
340 /* Wait for requests (schedules processes) */
341 MC_wait_for_requests();
346 /* Create the new expanded graph_state */
347 next_graph_state = MC_state_pair_new();
349 /* Get enabled process and insert it in the interleave set of the next graph_state */
350 xbt_swag_foreach(process, simix_global->process_list){
351 if(MC_process_is_enabled(process)){
352 MC_state_interleave_process(next_graph_state, process);
356 xbt_dynar_reset(successors);
362 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
364 res = MC_automaton_evaluate_label(a, transition_succ->label);
366 if(res == 1){ // enabled transition in automaton
368 next_pair = new_pair_stateless(next_graph_state, transition_succ->dst);
369 xbt_dynar_push(successors, &next_pair);
377 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
379 res = MC_automaton_evaluate_label(a, transition_succ->label);
381 if(res == 2){ // true transition in automaton
383 next_pair = new_pair_stateless(next_graph_state, transition_succ->dst);
384 xbt_dynar_push(successors, &next_pair);
391 if(xbt_dynar_length(successors) == 0){
393 next_pair = new_pair_stateless(next_graph_state, current_pair->automaton_state);
394 xbt_dynar_push(successors, &next_pair);
400 xbt_dynar_foreach(successors, cursor, pair_succ){
404 if((search_cycle == 1) && ((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2)) ){
406 XBT_DEBUG("Next pair (depth = %d) already reached !", xbt_fifo_size(mc_stack_liveness_stateless) + 1);
409 MC_take_snapshot(snapshot);
412 if(reached(a, pair_succ->automaton_state, snapshot) == 1){
413 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
414 XBT_INFO("| ACCEPTANCE CYCLE |");
415 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
416 XBT_INFO("Counter-example that violates formula :");
417 MC_show_stack_liveness_stateless(mc_stack_liveness_stateless);
418 MC_dump_stack_liveness_stateless(mc_stack_liveness_stateless);
419 MC_print_statistics_pairs(mc_stats_pair);
424 int interleave = MC_state_interleave_size(pair_succ->graph_state);
426 if((search_cycle == 0) && ((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2)) && ( interleave > 0)){
427 XBT_DEBUG("Take snapshot of acceptance pair (depth = %d)", xbt_fifo_size(mc_stack_liveness_stateless) + 1 );
429 MC_take_snapshot(snapshot);
434 xbt_fifo_unshift(mc_stack_liveness_stateless, pair_succ);
437 MC_ddfs_stateless(a, search_cycle, 0);
439 /* If pair_succ is the last state of the execution (0 interleave), no acceptance cycle possible */
440 if((search_cycle == 0) && ((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2)) && ( interleave > 0)){
442 XBT_DEBUG("Acceptance pair : graph=%p, automaton=%p(%s)", pair_succ->graph_state, pair_succ->automaton_state, pair_succ->automaton_state->id);
445 set_pair_reached(a, pair_succ->automaton_state, snapshot);
448 /* Pair shifted from stack when first MC_ddfs finished and returned at this point */
450 xbt_fifo_unshift(mc_stack_liveness_stateless, pair_succ);
453 MC_ddfs_stateless(a, 1, 1);
455 /* No acceptance cycle with this acceptance pair, we remove it from the list reached_pairs */
457 xbt_dynar_pop(reached_pairs, NULL);
463 if(MC_state_interleave_size(current_pair->graph_state) > 0){
464 XBT_DEBUG("Backtracking to depth %u", xbt_fifo_size(mc_stack_liveness_stateless));
465 MC_replay_liveness(mc_stack_liveness_stateless);
471 xbt_fifo_shift(mc_stack_liveness_stateless);
472 XBT_DEBUG("Pair (graph=%p, automaton =%p, search_cycle = %u) shifted in stack", current_pair->graph_state, current_pair->automaton_state, search_cycle);
477 /********************* Double-DFS stateful without visited state *******************/
480 void MC_ddfs_stateful_init(xbt_automaton_t a){
482 XBT_DEBUG("**************************************************");
483 XBT_DEBUG("Double-DFS stateful without visited state init");
484 XBT_DEBUG("**************************************************");
486 mc_pair_t mc_initial_pair;
487 mc_state_t initial_graph_state;
488 smx_process_t process;
489 mc_snapshot_t init_snapshot;
491 MC_wait_for_requests();
495 initial_graph_state = MC_state_pair_new();
496 xbt_swag_foreach(process, simix_global->process_list){
497 if(MC_process_is_enabled(process)){
498 MC_state_interleave_process(initial_graph_state, process);
502 reached_pairs = xbt_dynar_new(sizeof(mc_pair_reached_t), NULL);
503 successors = xbt_dynar_new(sizeof(mc_pair_t), NULL);
504 current_snapshot = xbt_new0(s_mc_snapshot_t, 1);
505 next_snapshot = xbt_new0(s_mc_snapshot_t, 1);
507 init_snapshot = xbt_new0(s_mc_snapshot_t, 1);
508 MC_take_snapshot(init_snapshot);
512 unsigned int cursor = 0;
513 xbt_state_t state = NULL;
515 xbt_dynar_foreach(a->states, cursor, state){
516 if(state->type == -1){
519 mc_initial_pair = new_pair(init_snapshot, initial_graph_state, state);
520 xbt_fifo_unshift(mc_stack_liveness_stateful, mc_initial_pair);
524 MC_ddfs_stateful(a, 0, 0);
526 MC_restore_snapshot(init_snapshot);
528 MC_ddfs_stateful(a, 0, 0);
531 if(state->type == 2){
534 mc_initial_pair = new_pair(init_snapshot, initial_graph_state, state);
535 xbt_fifo_unshift(mc_stack_liveness_stateful, mc_initial_pair);
539 MC_ddfs_stateful(a, 1, 0);
541 MC_restore_snapshot(init_snapshot);
543 MC_ddfs_stateful(a, 1, 0);
551 void MC_ddfs_stateful(xbt_automaton_t a, int search_cycle, int restore){
553 smx_process_t process = NULL;
554 mc_pair_t current_pair = NULL;
556 if(xbt_fifo_size(mc_stack_liveness_stateful) == 0)
560 current_pair = (mc_pair_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack_liveness_stateful));
561 MC_restore_snapshot(current_pair->system_state);
562 xbt_swag_foreach(process, simix_global->process_list){
563 if(MC_process_is_enabled(process)){
564 MC_state_interleave_process(current_pair->graph_state, process);
570 /* Get current state */
571 current_pair = (mc_pair_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack_liveness_stateful));
574 XBT_DEBUG("********************* ( Depth = %d, search_cycle = %d )", xbt_fifo_size(mc_stack_liveness_stateful), search_cycle);
575 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));
577 a->current_state = current_pair->automaton_state;
581 mc_stats_pair->visited_pairs++;
584 mc_state_t next_graph_state = NULL;
585 smx_req_t req = NULL;
589 xbt_transition_t transition_succ;
593 mc_pair_t next_pair = NULL;
595 while((req = MC_state_get_request(current_pair->graph_state, &value)) != NULL){
598 MC_take_snapshot(current_snapshot);
601 /* Debug information */
602 if(XBT_LOG_ISENABLED(mc_liveness, xbt_log_priority_debug)){
603 req_str = MC_request_to_string(req, value);
604 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));
605 XBT_DEBUG("Execute: %s", req_str);
609 MC_state_set_executed_request(current_pair->graph_state, req, value);
611 /* Answer the request */
612 SIMIX_request_pre(req, value);
614 /* Wait for requests (schedules processes) */
615 MC_wait_for_requests();
618 /* Create the new expanded graph_state */
621 next_graph_state = MC_state_pair_new();
623 /* Get enabled process and insert it in the interleave set of the next graph_state */
624 xbt_swag_foreach(process, simix_global->process_list){
625 if(MC_process_is_enabled(process)){
626 MC_state_interleave_process(next_graph_state, process);
630 MC_take_snapshot(next_snapshot);
632 xbt_dynar_reset(successors);
638 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
640 res = MC_automaton_evaluate_label(a, transition_succ->label);
642 if(res == 1){ // enabled transition in automaton
644 next_pair = new_pair(next_snapshot,next_graph_state, transition_succ->dst);
645 xbt_dynar_push(successors, &next_pair);
652 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
654 res = MC_automaton_evaluate_label(a, transition_succ->label);
656 if(res == 2){ // transition always enabled in automaton
658 next_pair = new_pair(next_snapshot,next_graph_state, transition_succ->dst);
659 xbt_dynar_push(successors, &next_pair);
667 if(xbt_dynar_length(successors) == 0){
670 next_pair = new_pair(next_snapshot, next_graph_state, current_pair->automaton_state);
671 xbt_dynar_push(successors, &next_pair);
677 xbt_dynar_foreach(successors, cursor, pair_succ){
679 if((search_cycle == 1) && ((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2))){
681 if(reached(a, pair_succ->automaton_state, next_snapshot) == 1){
682 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
683 XBT_INFO("| ACCEPTANCE CYCLE |");
684 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
685 XBT_INFO("Counter-example that violates formula :");
686 MC_show_stack_liveness_stateful(mc_stack_liveness_stateful);
687 MC_dump_stack_liveness_stateful(mc_stack_liveness_stateful);
688 MC_print_statistics_pairs(mc_stats_pair);
695 xbt_fifo_unshift(mc_stack_liveness_stateful, pair_succ);
698 MC_ddfs_stateful(a, search_cycle, 0);
701 if((search_cycle == 0) && ((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2))){
703 XBT_DEBUG("Acceptance pair : graph=%p, automaton=%p(%s)", pair_succ->graph_state, pair_succ->automaton_state, pair_succ->automaton_state->id);
704 set_pair_reached(a, pair_succ->automaton_state, next_snapshot);
707 xbt_fifo_unshift(mc_stack_liveness_stateful, pair_succ);
710 MC_ddfs_stateful(a, 1, 1);
714 xbt_dynar_pop(reached_pairs, NULL);
720 if(MC_state_interleave_size(current_pair->graph_state) > 0){
721 XBT_DEBUG("Backtracking to depth %u", xbt_fifo_size(mc_stack_liveness_stateful));
722 MC_restore_snapshot(current_snapshot);
730 xbt_fifo_shift(mc_stack_liveness_stateful);
731 XBT_DEBUG("Pair (graph=%p, automaton =%p) shifted in stack", current_pair->graph_state, current_pair->automaton_state);