1 /* Copyright (c) 2011-2014. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include <xbt/dynar.h>
11 #include <xbt/automaton.h>
13 #include "mc_request.h"
14 #include "mc_liveness.h"
15 #include "mc_private.h"
16 #include "mc_record.h"
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_liveness, mc,
19 "Logging specific to algorithms for liveness properties verification");
21 /********* Global variables *********/
23 xbt_dynar_t acceptance_pairs;
24 xbt_dynar_t successors;
27 /********* Static functions *********/
29 static xbt_dynar_t get_atomic_propositions_values()
31 unsigned int cursor = 0;
32 xbt_automaton_propositional_symbol_t ps = NULL;
33 xbt_dynar_t values = xbt_dynar_new(sizeof(int), NULL);
34 xbt_dynar_foreach(_mc_property_automaton->propositional_symbols, cursor, ps) {
35 int res = xbt_automaton_propositional_symbol_evaluate(ps);
36 xbt_dynar_push_as(values, int, res);
43 static mc_visited_pair_t is_reached_acceptance_pair(int pair_num,
49 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
51 mc_visited_pair_t pair = NULL;
52 pair = MC_visited_pair_new(pair_num, automaton_state, atomic_propositions);
53 pair->acceptance_pair = 1;
55 if (xbt_dynar_is_empty(acceptance_pairs)) {
57 xbt_dynar_push(acceptance_pairs, &pair);
61 int min = -1, max = -1, index;
63 mc_visited_pair_t pair_test;
66 index = get_search_interval(acceptance_pairs, pair, &min, &max);
68 if (min != -1 && max != -1) { // Acceptance pair with same number of processes and same heap bytes used exists
70 // Parallell implementation
71 /*res = xbt_parmap_mc_apply(parmap, snapshot_compare, xbt_dynar_get_ptr(acceptance_pairs, min), (max-min)+1, pair);
75 return ((mc_pair_t)xbt_dynar_get_as(acceptance_pairs, (min+res)-1, mc_pair_t))->num;
79 while (cursor <= max) {
81 (mc_visited_pair_t) xbt_dynar_get_as(acceptance_pairs, cursor,
83 if (xbt_automaton_state_compare
84 (pair_test->automaton_state, pair->automaton_state) == 0) {
85 if (xbt_automaton_propositional_symbols_compare_value
86 (pair_test->atomic_propositions,
87 pair->atomic_propositions) == 0) {
88 if (snapshot_compare(pair_test, pair) == 0) {
89 XBT_INFO("Pair %d already reached (equal to pair %d) !",
90 pair->num, pair_test->num);
92 xbt_fifo_shift(mc_stack);
93 if (dot_output != NULL)
94 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n",
95 initial_global_state->prev_pair, pair_test->num,
96 initial_global_state->prev_req);
97 mmalloc_set_current_heap(heap);
104 xbt_dynar_insert_at(acceptance_pairs, min, &pair);
107 (mc_visited_pair_t) xbt_dynar_get_as(acceptance_pairs, index,
109 if (pair_test->nb_processes < pair->nb_processes) {
110 xbt_dynar_insert_at(acceptance_pairs, index + 1, &pair);
112 if (pair_test->heap_bytes_used < pair->heap_bytes_used)
113 xbt_dynar_insert_at(acceptance_pairs, index + 1, &pair);
115 xbt_dynar_insert_at(acceptance_pairs, index, &pair);
120 mmalloc_set_current_heap(heap);
124 static void remove_acceptance_pair(int pair_num)
126 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
128 unsigned int cursor = 0;
129 mc_visited_pair_t pair_test = NULL;
131 xbt_dynar_foreach(acceptance_pairs, cursor, pair_test) {
132 if (pair_test->num == pair_num) {
137 xbt_dynar_remove_at(acceptance_pairs, cursor, &pair_test);
139 pair_test->acceptance_removed = 1;
141 if (_sg_mc_visited == 0) {
142 MC_visited_pair_delete(pair_test);
143 } else if (pair_test->visited_removed == 1) {
144 MC_visited_pair_delete(pair_test);
147 mmalloc_set_current_heap(heap);
151 static int MC_automaton_evaluate_label(xbt_automaton_exp_label_t l,
152 xbt_dynar_t atomic_propositions_values)
158 MC_automaton_evaluate_label(l->u.or_and.left_exp,
159 atomic_propositions_values);
161 MC_automaton_evaluate_label(l->u.or_and.right_exp,
162 atomic_propositions_values);
163 return (left_res || right_res);
167 MC_automaton_evaluate_label(l->u.or_and.left_exp,
168 atomic_propositions_values);
170 MC_automaton_evaluate_label(l->u.or_and.right_exp,
171 atomic_propositions_values);
172 return (left_res && right_res);
176 MC_automaton_evaluate_label(l->u.exp_not, atomic_propositions_values);
180 unsigned int cursor = 0;
181 xbt_automaton_propositional_symbol_t p = NULL;
182 xbt_dynar_foreach(_mc_property_automaton->propositional_symbols, cursor,
184 if (strcmp(xbt_automaton_propositional_symbol_get_name(p), l->u.predicat) == 0)
185 return (int) xbt_dynar_get_as(atomic_propositions_values, cursor,
198 void MC_pre_modelcheck_liveness(void)
201 initial_global_state->raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
203 mc_pair_t initial_pair = NULL;
204 smx_process_t process;
206 MC_wait_for_requests();
210 acceptance_pairs = xbt_dynar_new(sizeof(mc_visited_pair_t), NULL);
211 visited_pairs = xbt_dynar_new(sizeof(mc_visited_pair_t), NULL);
212 successors = xbt_dynar_new(sizeof(mc_pair_t), NULL);
214 initial_global_state->snapshot = MC_take_snapshot(0);
215 initial_global_state->prev_pair = 0;
219 unsigned int cursor = 0;
220 xbt_automaton_state_t automaton_state;
222 xbt_dynar_foreach(_mc_property_automaton->states, cursor, automaton_state) {
223 if (automaton_state->type == -1) { /* Initial automaton state */
227 initial_pair = MC_pair_new();
228 initial_pair->automaton_state = automaton_state;
229 initial_pair->graph_state = MC_state_new();
230 initial_pair->atomic_propositions = get_atomic_propositions_values();
232 /* Get enabled processes and insert them in the interleave set of the graph_state */
233 xbt_swag_foreach(process, simix_global->process_list) {
234 if (MC_process_is_enabled(process)) {
235 MC_state_interleave_process(initial_pair->graph_state, process);
239 initial_pair->requests =
240 MC_state_interleave_size(initial_pair->graph_state);
241 initial_pair->search_cycle = 0;
243 xbt_fifo_unshift(mc_stack, initial_pair);
247 MC_modelcheck_liveness();
250 MC_restore_snapshot(initial_global_state->snapshot);
256 if (initial_global_state->raw_mem_set)
263 void MC_modelcheck_liveness()
265 smx_process_t process;
266 mc_pair_t current_pair = NULL;
268 if (xbt_fifo_size(mc_stack) == 0)
271 /* Get current pair */
273 (mc_pair_t) xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack));
275 /* Update current state in buchi automaton */
276 _mc_property_automaton->current_state = current_pair->automaton_state;
279 ("********************* ( Depth = %d, search_cycle = %d, interleave size %d, pair_num %d)",
280 xbt_fifo_size(mc_stack), current_pair->search_cycle,
281 MC_state_interleave_size(current_pair->graph_state), current_pair->num);
283 mc_stats->visited_pairs++;
286 smx_simcall_t req = NULL;
288 xbt_automaton_transition_t transition_succ;
289 unsigned int cursor = 0;
293 mc_pair_t next_pair = NULL;
294 xbt_dynar_t prop_values = NULL;
295 mc_visited_pair_t reached_pair = NULL;
296 int counter_example_depth = 0;
298 if (xbt_fifo_size(mc_stack) < _sg_mc_max_depth) {
300 if (current_pair->requests > 0) {
302 if (current_pair->search_cycle) {
304 if ((current_pair->automaton_state->type == 1)
305 || (current_pair->automaton_state->type == 2)) {
307 is_reached_acceptance_pair(current_pair->num,
308 current_pair->automaton_state,
309 current_pair->atomic_propositions)) ==
312 counter_example_depth = xbt_fifo_size(mc_stack);
313 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
314 XBT_INFO("| ACCEPTANCE CYCLE |");
315 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
316 XBT_INFO("Counter-example that violates formula :");
317 MC_show_stack_liveness(mc_stack);
318 MC_dump_stack_liveness(mc_stack);
319 MC_print_statistics(mc_stats);
320 XBT_INFO("Counter-example depth : %d", counter_example_depth);
328 is_visited_pair(reached_pair, current_pair->num,
329 current_pair->automaton_state,
330 current_pair->atomic_propositions)) != -1) {
333 if (dot_output != NULL)
334 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n",
335 initial_global_state->prev_pair, visited_num,
336 initial_global_state->prev_req);
342 MC_state_get_request(current_pair->graph_state,
346 if (dot_output != NULL) {
347 if (initial_global_state->prev_pair != 0
348 && initial_global_state->prev_pair != current_pair->num) {
349 fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n",
350 initial_global_state->prev_pair, current_pair->num,
351 initial_global_state->prev_req);
352 xbt_free(initial_global_state->prev_req);
354 initial_global_state->prev_pair = current_pair->num;
358 MC_LOG_REQUEST(mc_liveness, req, value);
361 if (dot_output != NULL) {
362 initial_global_state->prev_req =
363 MC_request_get_dot_output(req, value);
364 if (current_pair->search_cycle)
365 fprintf(dot_output, "%d [shape=doublecircle];\n",
370 MC_state_set_executed_request(current_pair->graph_state, req, value);
371 mc_stats->executed_transitions++;
373 /* Answer the request */
374 SIMIX_simcall_handle(req, value);
376 /* Wait for requests (schedules processes) */
377 MC_wait_for_requests();
380 prop_values = get_atomic_propositions_values();
385 /* Evaluate enabled transition according to atomic propositions values */
387 xbt_dynar_foreach(current_pair->automaton_state->out, cursor,
391 MC_automaton_evaluate_label(transition_succ->label,
394 if (res == 1) { // enabled transition in automaton
397 MC_replay_liveness(mc_stack, 1);
401 next_pair = MC_pair_new();
402 next_pair->graph_state = MC_state_new();
403 next_pair->automaton_state = transition_succ->dst;
404 next_pair->atomic_propositions = get_atomic_propositions_values();
406 /* Get enabled processes and insert them in the interleave set of the next graph_state */
407 xbt_swag_foreach(process, simix_global->process_list) {
408 if (MC_process_is_enabled(process)) {
409 MC_state_interleave_process(next_pair->graph_state, process);
413 next_pair->requests =
414 MC_state_interleave_size(next_pair->graph_state);
416 if (next_pair->automaton_state->type == 1
417 || next_pair->automaton_state->type == 2
418 || current_pair->search_cycle)
419 next_pair->search_cycle = 1;
421 xbt_fifo_unshift(mc_stack, next_pair);
423 if (mc_stats->expanded_pairs % 1000000 == 0)
424 XBT_INFO("Expanded pairs : %lu", mc_stats->expanded_pairs);
430 MC_modelcheck_liveness();
436 /* Then, evaluate true transitions (always true, whatever atomic propositions values) */
438 xbt_dynar_foreach(current_pair->automaton_state->out, cursor,
442 MC_automaton_evaluate_label(transition_succ->label,
445 if (res == 2) { // true transition in automaton
448 MC_replay_liveness(mc_stack, 1);
452 next_pair = MC_pair_new();
453 next_pair->graph_state = MC_state_new();
454 next_pair->automaton_state = transition_succ->dst;
455 next_pair->atomic_propositions = get_atomic_propositions_values();
457 /* Get enabled process and insert it in the interleave set of the next graph_state */
458 xbt_swag_foreach(process, simix_global->process_list) {
459 if (MC_process_is_enabled(process)) {
460 MC_state_interleave_process(next_pair->graph_state, process);
464 next_pair->requests =
465 MC_state_interleave_size(next_pair->graph_state);
467 if (next_pair->automaton_state->type == 1
468 || next_pair->automaton_state->type == 2
469 || current_pair->search_cycle)
470 next_pair->search_cycle = 1;
472 xbt_fifo_unshift(mc_stack, next_pair);
474 if (mc_stats->expanded_pairs % 1000000 == 0)
475 XBT_INFO("Expanded pairs : %lu", mc_stats->expanded_pairs);
481 MC_modelcheck_liveness();
487 if (MC_state_interleave_size(current_pair->graph_state) > 0) {
488 XBT_DEBUG("Backtracking to depth %d", xbt_fifo_size(mc_stack));
489 MC_replay_liveness(mc_stack, 0);
500 XBT_WARN("/!\\ Max depth reached ! /!\\ ");
501 if (MC_state_interleave_size(current_pair->graph_state) > 0) {
503 ("/!\\ But, there are still processes to interleave. Model-checker will not be able to ensure the soundness of the verification from now. /!\\ ");
504 if (_sg_mc_max_depth == 1000)
506 ("Notice : the default value of max depth is 1000 but you can change it with cfg=model-check/max_depth:value.");
511 if (xbt_fifo_size(mc_stack) == _sg_mc_max_depth) {
512 XBT_DEBUG("Pair %d (depth = %d) shifted in stack, maximum depth reached",
513 current_pair->num, xbt_fifo_size(mc_stack));
515 XBT_DEBUG("Pair %d (depth = %d) shifted in stack", current_pair->num,
516 xbt_fifo_size(mc_stack));
521 xbt_dynar_free(&prop_values);
522 current_pair = xbt_fifo_shift(mc_stack);
523 if (xbt_fifo_size(mc_stack) != _sg_mc_max_depth - 1
524 && current_pair->requests > 0 && current_pair->search_cycle) {
525 remove_acceptance_pair(current_pair->num);
527 MC_pair_delete(current_pair);