Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : fix SIMCALL_MUTEX_LOCK with MC
[simgrid.git] / src / mc / mc_liveness.c
1 /* Copyright (c) 2011-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include <unistd.h>
8 #include <sys/wait.h>
9
10 #include <xbt/dynar.h>
11 #include <xbt/automaton.h>
12
13 #include "mc_request.h"
14 #include "mc_liveness.h"
15 #include "mc_private.h"
16 #include "mc_record.h"
17
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_liveness, mc,
19                                 "Logging specific to algorithms for liveness properties verification");
20
21 /********* Global variables *********/
22
23 xbt_dynar_t acceptance_pairs;
24 xbt_parmap_t parmap;
25
26 /********* Static functions *********/
27
28 static xbt_dynar_t get_atomic_propositions_values()
29 {
30   int res;
31   int_f_void_t f;
32   unsigned int cursor = 0;
33   xbt_automaton_propositional_symbol_t ps = NULL;
34   xbt_dynar_t values = xbt_dynar_new(sizeof(int), NULL);
35
36   xbt_dynar_foreach(_mc_property_automaton->propositional_symbols, cursor, ps) {
37     f = (int_f_void_t) ps->function;
38     res = f();
39     xbt_dynar_push_as(values, int, res);
40   }
41
42   return values;
43 }
44
45
46 static mc_visited_pair_t is_reached_acceptance_pair(mc_pair_t pair){
47
48   int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
49
50   MC_SET_MC_HEAP;
51
52   mc_visited_pair_t new_pair = NULL;
53   new_pair = MC_visited_pair_new(pair->num, pair->automaton_state, pair->atomic_propositions, pair->graph_state);
54   new_pair->acceptance_pair = 1;
55
56   if (xbt_dynar_is_empty(acceptance_pairs)) {
57
58     xbt_dynar_push(acceptance_pairs, &new_pair);
59
60   } else {
61
62     int min = -1, max = -1, index;
63     //int res;
64     mc_visited_pair_t pair_test;
65     int cursor;
66
67     index = get_search_interval(acceptance_pairs, new_pair, &min, &max);
68
69     if (min != -1 && max != -1) {       // Acceptance pair with same number of processes and same heap bytes used exists
70
71       // Parallell implementation
72       /*res = xbt_parmap_mc_apply(parmap, snapshot_compare, xbt_dynar_get_ptr(acceptance_pairs, min), (max-min)+1, pair);
73          if(res != -1){
74          if(!raw_mem_set)
75          MC_SET_STD_HEAP;
76          return ((mc_pair_t)xbt_dynar_get_as(acceptance_pairs, (min+res)-1, mc_pair_t))->num;
77          } */
78
79       cursor = min;
80       if(pair->search_cycle == 1){
81         while (cursor <= max) {
82           pair_test = (mc_visited_pair_t) xbt_dynar_get_as(acceptance_pairs, cursor, mc_visited_pair_t);
83           if (xbt_automaton_state_compare(pair_test->automaton_state, new_pair->automaton_state) == 0) {
84             if (xbt_automaton_propositional_symbols_compare_value(pair_test->atomic_propositions, new_pair->atomic_propositions) == 0) {
85               if (snapshot_compare(pair_test, new_pair) == 0) {
86                 XBT_INFO("Pair %d already reached (equal to pair %d) !", new_pair->num, pair_test->num);
87                 xbt_fifo_shift(mc_stack);
88                 if (dot_output != NULL)
89                   fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_global_state->prev_pair, pair_test->num, initial_global_state->prev_req);
90
91                 if (!raw_mem_set)
92                   MC_SET_STD_HEAP;
93
94                 return NULL;
95               }
96             }
97           }
98           cursor++;
99         }
100       }
101       xbt_dynar_insert_at(acceptance_pairs, min, &new_pair);
102     } else {
103       pair_test = (mc_visited_pair_t) xbt_dynar_get_as(acceptance_pairs, index, mc_visited_pair_t);
104       if (pair_test->nb_processes < new_pair->nb_processes) {
105         xbt_dynar_insert_at(acceptance_pairs, index + 1, &new_pair);
106       } else {
107         if (pair_test->heap_bytes_used < new_pair->heap_bytes_used)
108           xbt_dynar_insert_at(acceptance_pairs, index + 1, &new_pair);
109         else
110           xbt_dynar_insert_at(acceptance_pairs, index, &new_pair);
111       }
112     }
113
114   }
115
116   if (!raw_mem_set)
117     MC_SET_STD_HEAP;
118
119   return new_pair;
120
121 }
122
123 static void remove_acceptance_pair(int pair_num) {
124
125   int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
126
127   MC_SET_MC_HEAP;
128
129   unsigned int cursor = 0;
130   mc_visited_pair_t pair_test = NULL;
131   int pair_found = 0;
132
133   xbt_dynar_foreach(acceptance_pairs, cursor, pair_test) {
134     if (pair_test->num == pair_num) {
135        pair_found = 1;
136       break;
137     }
138   }
139
140   if(pair_found == 1) {
141     xbt_dynar_remove_at(acceptance_pairs, cursor, &pair_test);
142
143     pair_test->acceptance_removed = 1;
144
145     if (_sg_mc_visited == 0 || pair_test->visited_removed == 1) 
146       MC_visited_pair_delete(pair_test);
147
148   }
149
150   if (!raw_mem_set)
151     MC_SET_STD_HEAP;
152 }
153
154
155 static int MC_automaton_evaluate_label(xbt_automaton_exp_label_t l,
156                                        xbt_dynar_t atomic_propositions_values)
157 {
158
159   switch (l->type) {
160   case 0:{
161       int left_res = MC_automaton_evaluate_label(l->u.or_and.left_exp, atomic_propositions_values);
162       int right_res = MC_automaton_evaluate_label(l->u.or_and.right_exp, atomic_propositions_values);
163       return (left_res || right_res);
164     }
165   case 1:{
166       int left_res = MC_automaton_evaluate_label(l->u.or_and.left_exp, atomic_propositions_values);
167       int right_res = MC_automaton_evaluate_label(l->u.or_and.right_exp, atomic_propositions_values);
168       return (left_res && right_res);
169     }
170   case 2:{
171       int res = MC_automaton_evaluate_label(l->u.exp_not, atomic_propositions_values);
172       return (!res);
173     }
174   case 3:{
175       unsigned int cursor = 0;
176       xbt_automaton_propositional_symbol_t p = NULL;
177       xbt_dynar_foreach(_mc_property_automaton->propositional_symbols, cursor, p) {
178         if (strcmp(p->pred, l->u.predicat) == 0)
179           return (int) xbt_dynar_get_as(atomic_propositions_values, cursor, int);
180       }
181       return -1;
182     }
183   case 4:{
184       return 2;
185     }
186   default:
187     return -1;
188   }
189 }
190
191 void MC_pre_modelcheck_liveness(void) {
192
193   initial_global_state->raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
194
195   mc_pair_t initial_pair = NULL;
196   smx_process_t process;
197
198   MC_wait_for_requests();
199
200   MC_SET_MC_HEAP;
201
202   acceptance_pairs = xbt_dynar_new(sizeof(mc_visited_pair_t), NULL);
203   if(_sg_mc_visited > 0)
204     visited_pairs = xbt_dynar_new(sizeof(mc_visited_pair_t), NULL);
205
206   initial_global_state->snapshot = MC_take_snapshot(0);
207   initial_global_state->prev_pair = 0;
208
209   unsigned int cursor = 0;
210   xbt_automaton_state_t automaton_state;
211   
212   xbt_dynar_foreach(_mc_property_automaton->states, cursor, automaton_state) {
213     if (automaton_state->type == -1) {  /* Initial automaton state */
214
215       initial_pair = MC_pair_new();
216       initial_pair->automaton_state = automaton_state;
217       initial_pair->graph_state = MC_state_new();
218       initial_pair->atomic_propositions = get_atomic_propositions_values();
219       initial_pair->depth = 1;
220
221       /* Get enabled processes and insert them in the interleave set of the graph_state */
222       xbt_swag_foreach(process, simix_global->process_list) {
223         if (MC_process_is_enabled(process)) {
224           MC_state_interleave_process(initial_pair->graph_state, process);
225         }
226       }
227
228       initial_pair->requests = MC_state_interleave_size(initial_pair->graph_state);
229       initial_pair->search_cycle = 0;
230
231       xbt_fifo_unshift(mc_stack, initial_pair);
232     }
233   }
234
235   MC_SET_STD_HEAP;
236   
237   MC_modelcheck_liveness();
238
239   if (initial_global_state->raw_mem_set)
240     MC_SET_MC_HEAP;
241
242
243 }
244
245 void MC_modelcheck_liveness() {
246
247   smx_process_t process = NULL;
248   mc_pair_t current_pair = NULL;
249   int value, res, visited_num = -1;
250   smx_simcall_t req = NULL;
251   xbt_automaton_transition_t transition_succ = NULL;
252   int cursor = 0;
253   mc_pair_t next_pair = NULL;
254   xbt_dynar_t prop_values = NULL;
255   mc_visited_pair_t reached_pair = NULL;
256   
257   while(xbt_fifo_size(mc_stack) > 0){
258
259     /* Get current pair */
260     current_pair = (mc_pair_t) xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack));
261
262     /* Update current state in buchi automaton */
263     _mc_property_automaton->current_state = current_pair->automaton_state;
264
265     XBT_DEBUG("********************* ( Depth = %d, search_cycle = %d, interleave size %d, pair_num %d)",
266        current_pair->depth, current_pair->search_cycle,
267        MC_state_interleave_size(current_pair->graph_state), current_pair->num);
268
269     if (current_pair->requests > 0) {
270
271       if (current_pair->automaton_state->type == 1 && current_pair->exploration_started == 0) {
272         /* If new acceptance pair, return new pair */
273         if ((reached_pair = is_reached_acceptance_pair(current_pair)) == NULL) {
274           int counter_example_depth = current_pair->depth;
275           XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
276           XBT_INFO("|             ACCEPTANCE CYCLE            |");
277           XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
278           XBT_INFO("Counter-example that violates formula :");
279           MC_show_stack_liveness(mc_stack);
280           MC_dump_stack_liveness(mc_stack);
281           MC_print_statistics(mc_stats);
282           XBT_INFO("Counter-example depth : %d", counter_example_depth);
283           xbt_abort();
284         }
285       }
286
287       /* Pair already visited ? stop the exploration on the current path */
288       if ((current_pair->exploration_started == 0) && (visited_num = is_visited_pair(reached_pair, current_pair)) != -1) {
289
290         if (dot_output != NULL){
291           MC_SET_MC_HEAP;
292           fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_global_state->prev_pair, visited_num, initial_global_state->prev_req);
293           fflush(dot_output);
294           MC_SET_STD_HEAP;
295         }
296
297         XBT_DEBUG("Pair already visited (equal to pair %d), exploration on the current path stopped.", visited_num);
298         MC_SET_MC_HEAP;
299         current_pair->requests = 0;
300         MC_SET_STD_HEAP;
301         goto backtracking;
302         
303       }else{
304
305         req = MC_state_get_request(current_pair->graph_state, &value);
306
307          if (dot_output != NULL) {
308            MC_SET_MC_HEAP;
309            if (initial_global_state->prev_pair != 0 && initial_global_state->prev_pair != current_pair->num) {
310              fprintf(dot_output, "\"%d\" -> \"%d\" [%s];\n", initial_global_state->prev_pair, current_pair->num, initial_global_state->prev_req);
311              xbt_free(initial_global_state->prev_req);
312            }
313            initial_global_state->prev_pair = current_pair->num;
314            initial_global_state->prev_req = MC_request_get_dot_output(req, value);
315            if (current_pair->search_cycle)
316              fprintf(dot_output, "%d [shape=doublecircle];\n", current_pair->num);
317            fflush(dot_output);
318            MC_SET_STD_HEAP;
319          }
320
321          char* req_str = MC_request_to_string(req, value);  
322          XBT_DEBUG("Execute: %s", req_str);                 
323          xbt_free(req_str);
324
325          /* Set request as executed */
326          MC_state_set_executed_request(current_pair->graph_state, req, value);
327
328          /* Update mc_stats */
329          mc_stats->executed_transitions++;
330          if(current_pair->exploration_started == 0)
331            mc_stats->visited_pairs++;
332
333          /* Answer the request */
334          SIMIX_simcall_handle(req, value);
335          
336          /* Wait for requests (schedules processes) */
337          MC_wait_for_requests();
338
339          MC_SET_MC_HEAP;
340
341          current_pair->requests--;
342          current_pair->exploration_started = 1;
343
344          /* Get values of atomic propositions (variables used in the property formula) */ 
345          prop_values = get_atomic_propositions_values();
346
347          /* Evaluate enabled/true transitions in automaton according to atomic propositions values and create new pairs */
348          cursor = xbt_dynar_length(current_pair->automaton_state->out) - 1;
349          while (cursor >= 0) {
350            transition_succ = (xbt_automaton_transition_t)xbt_dynar_get_as(current_pair->automaton_state->out, cursor, xbt_automaton_transition_t);
351            res = MC_automaton_evaluate_label(transition_succ->label, prop_values);
352            if (res == 1 || res == 2) { /* 1 = True transition (always enabled), 2 = enabled transition according to atomic prop values */
353               next_pair = MC_pair_new();
354               next_pair->graph_state = MC_state_new();
355               next_pair->automaton_state = transition_succ->dst;
356               next_pair->atomic_propositions = get_atomic_propositions_values();
357               next_pair->depth = current_pair->depth + 1;
358               /* Get enabled processes and insert them in the interleave set of the next graph_state */
359               xbt_swag_foreach(process, simix_global->process_list) {
360                 if (MC_process_is_enabled(process)) {
361                   MC_state_interleave_process(next_pair->graph_state, process);
362                 }
363               }
364
365               next_pair->requests = MC_state_interleave_size(next_pair->graph_state);
366
367               /* FIXME : get search_cycle value for each acceptant state */
368               if (next_pair->automaton_state->type == 1 || current_pair->search_cycle)
369                 next_pair->search_cycle = 1;
370
371               /* Add new pair to the exploration stack */
372               xbt_fifo_unshift(mc_stack, next_pair);
373
374            }
375            cursor--;
376          }
377          
378          MC_SET_STD_HEAP;
379         
380       } /* End of visited_pair test */
381       
382     } else {
383
384     backtracking:
385       if(visited_num == -1)
386         XBT_DEBUG("No more request to execute. Looking for backtracking point.");
387     
388       MC_SET_MC_HEAP;
389     
390       xbt_dynar_free(&prop_values);
391     
392       /* Traverse the stack backwards until a pair with a non empty interleave
393          set is found, deleting all the pairs that have it empty in the way. */
394       while ((current_pair = xbt_fifo_shift(mc_stack)) != NULL) {
395         if (current_pair->requests > 0) {
396           /* We found a backtracking point */
397           XBT_DEBUG("Backtracking to depth %d", current_pair->depth);
398           xbt_fifo_unshift(mc_stack, current_pair);
399           MC_SET_STD_HEAP;
400           MC_replay_liveness(mc_stack);
401           XBT_DEBUG("Backtracking done");
402           break;
403         }else{
404           /* Delete pair */
405           XBT_DEBUG("Delete pair %d at depth %d", current_pair->num, current_pair->depth);
406           if (current_pair->automaton_state->type == 1) 
407             remove_acceptance_pair(current_pair->num);
408           MC_pair_delete(current_pair);
409         }
410       }
411     
412       MC_SET_STD_HEAP;
413     } /* End of if (current_pair->requests > 0) else ... */
414     
415   } /* End of while(xbt_fifo_size(mc_stack) > 0) */
416   
417 }