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 = MC_take_snapshot_liveness();
66 /* Get values of propositional symbols */
69 unsigned int cursor = 0;
70 xbt_propositional_symbol_t ps = NULL;
71 xbt_dynar_foreach(_mc_property_automaton->propositional_symbols, cursor, ps){
72 f = (int_f_void_t)ps->function;
74 xbt_dynar_push_as(new_pair->prop_ato, int, res);
79 if(xbt_dynar_is_empty(reached_pairs)/* || !compare*/){
82 /* New pair reached */
83 xbt_dynar_push(reached_pairs, &new_pair);
96 mc_pair_reached_t pair_test = NULL;
98 xbt_dynar_foreach(reached_pairs, cursor, pair_test){
99 XBT_INFO("Pair reached #%d", pair_test->nb);
100 if(automaton_state_compare(pair_test->automaton_state, st) == 0){
101 if(propositional_symbols_compare_value(pair_test->prop_ato, new_pair->prop_ato) == 0){
102 if(snapshot_compare(new_pair->system_state, pair_test->system_state) == 0){
112 XBT_INFO("Different values of propositional symbols");
115 XBT_INFO("Different automaton state");
119 /* New pair reached */
120 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 = MC_take_snapshot_liveness();
148 /* Get values of propositional symbols */
149 unsigned int cursor = 0;
150 xbt_propositional_symbol_t ps = NULL;
154 xbt_dynar_foreach(_mc_property_automaton->propositional_symbols, cursor, ps){
155 f = (int_f_void_t)ps->function;
157 xbt_dynar_push_as(pair->prop_ato, int, res);
160 xbt_dynar_push(reached_pairs, &pair);
169 void MC_pair_delete(mc_pair_t pair){
170 xbt_free(pair->graph_state->proc_status);
171 xbt_free(pair->graph_state);
177 int MC_automaton_evaluate_label(xbt_exp_label_t l){
181 int left_res = MC_automaton_evaluate_label(l->u.or_and.left_exp);
182 int right_res = MC_automaton_evaluate_label(l->u.or_and.right_exp);
183 return (left_res || right_res);
186 int left_res = MC_automaton_evaluate_label(l->u.or_and.left_exp);
187 int right_res = MC_automaton_evaluate_label(l->u.or_and.right_exp);
188 return (left_res && right_res);
191 int res = MC_automaton_evaluate_label(l->u.exp_not);
195 unsigned int cursor = 0;
196 xbt_propositional_symbol_t p = NULL;
198 xbt_dynar_foreach(_mc_property_automaton->propositional_symbols, cursor, p){
199 if(strcmp(p->pred, l->u.predicat) == 0){
200 f = (int_f_void_t)p->function;
215 /********************* Double-DFS stateless *******************/
217 void pair_stateless_free(mc_pair_stateless_t pair){
218 xbt_free(pair->graph_state->system_state);
219 xbt_free(pair->graph_state->proc_status);
220 xbt_free(pair->graph_state);
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_state_liveness->initial_snapshot = MC_take_snapshot_liveness();
283 unsigned int cursor = 0;
286 xbt_dynar_foreach(_mc_property_automaton->states, cursor, state){
287 if(state->type == -1){
290 mc_initial_pair = new_pair_stateless(initial_graph_state, state, MC_state_interleave_size(initial_graph_state));
291 xbt_fifo_unshift(mc_stack_liveness, mc_initial_pair);
295 MC_restore_snapshot(initial_state_liveness->initial_snapshot);
302 if(state->type == 2){
305 mc_initial_pair = new_pair_stateless(initial_graph_state, state, MC_state_interleave_size(initial_graph_state));
306 xbt_fifo_unshift(mc_stack_liveness, mc_initial_pair);
309 set_pair_reached(state);
312 MC_restore_snapshot(initial_state_liveness->initial_snapshot);
331 void MC_ddfs(int search_cycle){
333 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
335 smx_process_t process;
336 mc_pair_stateless_t current_pair = NULL;
338 if(xbt_fifo_size(mc_stack_liveness) == 0)
342 /* Get current pair */
343 current_pair = (mc_pair_stateless_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack_liveness));
345 /* Update current state in buchi automaton */
346 _mc_property_automaton->current_state = current_pair->automaton_state;
349 XBT_DEBUG("********************* ( Depth = %d, search_cycle = %d )", xbt_fifo_size(mc_stack_liveness), search_cycle);
351 mc_stats_pair->visited_pairs++;
356 mc_state_t next_graph_state = NULL;
357 smx_simcall_t req = NULL;
360 xbt_transition_t transition_succ;
361 unsigned int cursor = 0;
364 mc_pair_stateless_t next_pair = NULL;
365 mc_pair_stateless_t pair_succ;
367 mc_pair_stateless_t remove_pair;
368 mc_pair_reached_t remove_pair_reached;
370 if(xbt_fifo_size(mc_stack_liveness) < MAX_DEPTH_LIVENESS){
372 if(current_pair->requests > 0){
374 while((req = MC_state_get_request(current_pair->graph_state, &value)) != NULL){
376 /* Debug information */
378 req_str = MC_request_to_string(req, value);
379 XBT_DEBUG("Execute: %s", req_str);
382 MC_state_set_executed_request(current_pair->graph_state, req, value);
384 /* Answer the request */
385 SIMIX_simcall_pre(req, value);
387 /* Wait for requests (schedules processes) */
388 MC_wait_for_requests();
392 /* Create the new expanded graph_state */
393 next_graph_state = MC_state_pair_new();
395 /* Get enabled process and insert it in the interleave set of the next graph_state */
397 xbt_swag_foreach(process, simix_global->process_list){
398 if(MC_process_is_enabled(process)){
399 XBT_DEBUG("Process %lu enabled with simcall : %d", process->pid, (&process->simcall)->call);
403 xbt_swag_foreach(process, simix_global->process_list){
404 if(MC_process_is_enabled(process)){
405 MC_state_interleave_process(next_graph_state, process);
409 xbt_dynar_reset(successors);
415 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
417 res = MC_automaton_evaluate_label(transition_succ->label);
419 if(res == 1){ // enabled transition in automaton
421 next_pair = new_pair_stateless(next_graph_state, transition_succ->dst, MC_state_interleave_size(next_graph_state));
422 xbt_dynar_push(successors, &next_pair);
430 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
432 res = MC_automaton_evaluate_label(transition_succ->label);
434 if(res == 2){ // true transition in automaton
436 next_pair = new_pair_stateless(next_graph_state, transition_succ->dst, MC_state_interleave_size(next_graph_state));
437 xbt_dynar_push(successors, &next_pair);
445 xbt_dynar_foreach(successors, cursor, pair_succ){
447 if(search_cycle == 1){
449 if((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2)){
451 if(reached(pair_succ->automaton_state)){
453 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));
455 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
456 XBT_INFO("| ACCEPTANCE CYCLE |");
457 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
458 XBT_INFO("Counter-example that violates formula :");
459 MC_show_stack_liveness(mc_stack_liveness);
460 MC_dump_stack_liveness(mc_stack_liveness);
461 MC_print_statistics_pairs(mc_stats_pair);
466 XBT_DEBUG("Next pair (depth =%d) -> Acceptance pair (%s)", xbt_fifo_size(mc_stack_liveness) + 1, pair_succ->automaton_state->id);
468 XBT_DEBUG("Reached pairs : %lu", xbt_dynar_length(reached_pairs));
471 xbt_fifo_unshift(mc_stack_liveness, pair_succ);
474 MC_ddfs(search_cycle);
481 xbt_fifo_unshift(mc_stack_liveness, pair_succ);
484 MC_ddfs(search_cycle);
490 if(((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2))){
492 XBT_DEBUG("Next pair (depth =%d) -> Acceptance pair (%s)", xbt_fifo_size(mc_stack_liveness) + 1, pair_succ->automaton_state->id);
494 set_pair_reached(pair_succ->automaton_state);
498 XBT_DEBUG("Reached pairs : %lu", xbt_dynar_length(reached_pairs));
503 xbt_fifo_unshift(mc_stack_liveness, pair_succ);
506 MC_ddfs(search_cycle);
511 /* Restore system before checking others successors */
512 if(cursor != (xbt_dynar_length(successors) - 1))
513 MC_replay_liveness(mc_stack_liveness, 1);
518 if(MC_state_interleave_size(current_pair->graph_state) > 0){
519 XBT_DEBUG("Backtracking to depth %d", xbt_fifo_size(mc_stack_liveness));
520 MC_replay_liveness(mc_stack_liveness, 0);
527 XBT_DEBUG("No more request to execute in this state, search evolution in Büchi Automaton.");
531 /* Create the new expanded graph_state */
532 next_graph_state = MC_state_pair_new();
534 xbt_dynar_reset(successors);
540 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
542 res = MC_automaton_evaluate_label(transition_succ->label);
544 if(res == 1){ // enabled transition in automaton
546 next_pair = new_pair_stateless(next_graph_state, transition_succ->dst, MC_state_interleave_size(next_graph_state));
547 xbt_dynar_push(successors, &next_pair);
555 xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
557 res = MC_automaton_evaluate_label(transition_succ->label);
559 if(res == 2){ // true transition in automaton
561 next_pair = new_pair_stateless(next_graph_state, transition_succ->dst, MC_state_interleave_size(next_graph_state));
562 xbt_dynar_push(successors, &next_pair);
570 xbt_dynar_foreach(successors, cursor, pair_succ){
572 if(search_cycle == 1){
574 if((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2)){
576 if(reached(pair_succ->automaton_state)){
578 XBT_INFO("Next pair (depth = %d) already reached !", xbt_fifo_size(mc_stack_liveness) + 1);
580 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
581 XBT_INFO("| ACCEPTANCE CYCLE |");
582 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
583 XBT_INFO("Counter-example that violates formula :");
584 MC_show_stack_liveness(mc_stack_liveness);
585 MC_dump_stack_liveness(mc_stack_liveness);
586 MC_print_statistics_pairs(mc_stats_pair);
591 XBT_INFO("Next pair (depth = %d) -> Acceptance pair (%s)", xbt_fifo_size(mc_stack_liveness) + 1, pair_succ->automaton_state->id);
593 XBT_INFO("Reached pairs : %lu", xbt_dynar_length(reached_pairs));
596 xbt_fifo_unshift(mc_stack_liveness, pair_succ);
599 MC_ddfs(search_cycle);
606 xbt_fifo_unshift(mc_stack_liveness, pair_succ);
609 MC_ddfs(search_cycle);
616 if(((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2))){
618 set_pair_reached(pair_succ->automaton_state);
622 XBT_INFO("Reached pairs : %lu", xbt_dynar_length(reached_pairs));
627 xbt_fifo_unshift(mc_stack_liveness, pair_succ);
630 MC_ddfs(search_cycle);
634 /* Restore system before checking others successors */
635 if(cursor != xbt_dynar_length(successors) - 1)
636 MC_replay_liveness(mc_stack_liveness, 1);
644 XBT_DEBUG("Max depth reached");
648 if(xbt_fifo_size(mc_stack_liveness) == MAX_DEPTH_LIVENESS ){
649 XBT_DEBUG("Pair (depth = %d) shifted in stack, maximum depth reached", xbt_fifo_size(mc_stack_liveness) );
651 XBT_DEBUG("Pair (depth = %d) shifted in stack", xbt_fifo_size(mc_stack_liveness) );
656 remove_pair = xbt_fifo_shift(mc_stack_liveness);
657 xbt_fifo_remove(mc_stack_liveness, remove_pair);
659 if((current_pair->automaton_state->type == 1) || (current_pair->automaton_state->type == 2)){
660 remove_pair_reached = xbt_dynar_pop_as(reached_pairs, mc_pair_reached_t);
661 pair_reached_free(remove_pair_reached);
662 remove_pair_reached = NULL;