Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : verification of current_heap
[simgrid.git] / src / mc / mc_liveness.c
1 /* Copyright (c) 2008-2012 Da SimGrid Team. All rights reserved.            */
2
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. */
5
6 #include "mc_private.h"
7 #include <unistd.h>
8 #include <sys/wait.h>
9
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_liveness, mc,
11                                 "Logging specific to algorithms for liveness properties verification");
12
13 xbt_dynar_t reached_pairs;
14 xbt_dynar_t successors;
15
16 int create_dump(int pair)
17 {
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;
22   
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));
25   
26   int status;
27   switch(fork()){
28   case 0:
29     // We are the child process -- run the actual program
30     xbt_abort();
31     break;
32     
33   case -1:
34     // An error occurred, shouldn't happen
35     perror("fork");
36     return -1;
37     
38   default:
39     // We are the parent process -- wait for the child process to exit
40     if(wait(&status) < 0)
41       perror("wait");
42     if(WIFSIGNALED(status) && WCOREDUMP(status)){
43       char *core_name = malloc(20);
44       sprintf(core_name,"core_%d", pair); 
45       rename("core", core_name);
46       free(core_name);
47     }
48   }
49
50   return 0;
51 }
52
53 int reached(xbt_state_t st){
54
55   raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
56
57   MC_SET_RAW_MEM;
58
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();  
65   
66   /* Get values of propositional symbols */
67   int res;
68   int_f_void_t f;
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;
73     res = (*f)();
74     xbt_dynar_push_as(new_pair->prop_ato, int, res);
75   }
76   
77   MC_UNSET_RAW_MEM;
78   
79   if(xbt_dynar_is_empty(reached_pairs) || !compare){
80
81     MC_SET_RAW_MEM;
82     /* New pair reached */
83     xbt_dynar_push(reached_pairs, &new_pair); 
84     MC_UNSET_RAW_MEM;
85
86     if(raw_mem_set)
87       MC_SET_RAW_MEM;
88  
89     return 0;
90
91   }else{
92
93     MC_SET_RAW_MEM;
94     
95     cursor = 0;
96     mc_pair_reached_t pair_test = NULL;
97      
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){
103             
104             if(raw_mem_set)
105               MC_SET_RAW_MEM;
106             else
107               MC_UNSET_RAW_MEM;
108             
109             return 1;
110           }       
111         }else{
112           XBT_INFO("Different values of propositional symbols");
113         }
114       }else{
115         XBT_INFO("Different automaton state");
116       }
117     }
118
119     /* New pair reached */
120     xbt_dynar_push(reached_pairs, &new_pair); 
121     
122     MC_UNSET_RAW_MEM;
123
124     if(raw_mem_set)
125       MC_SET_RAW_MEM;
126  
127     compare = 0;
128     
129     return 0;
130     
131   }
132 }
133
134
135 void set_pair_reached(xbt_state_t st){
136
137   raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
138  
139   MC_SET_RAW_MEM;
140
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();
147
148   /* Get values of propositional symbols */
149   unsigned int cursor = 0;
150   xbt_propositional_symbol_t ps = NULL;
151   int res;
152   int_f_void_t f;
153
154   xbt_dynar_foreach(_mc_property_automaton->propositional_symbols, cursor, ps){
155     f = (int_f_void_t)ps->function;
156     res = (*f)();
157     xbt_dynar_push_as(pair->prop_ato, int, res);
158   }
159
160   xbt_dynar_push(reached_pairs, &pair); 
161   
162   MC_UNSET_RAW_MEM;
163
164   if(raw_mem_set)
165     MC_SET_RAW_MEM;
166     
167 }
168
169 void MC_pair_delete(mc_pair_t pair){
170   xbt_free(pair->graph_state->proc_status);
171   xbt_free(pair->graph_state);
172   xbt_free(pair);
173 }
174
175
176
177 int MC_automaton_evaluate_label(xbt_exp_label_t l){
178   
179   switch(l->type){
180   case 0 : {
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);
184   }
185   case 1 : {
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);
189   }
190   case 2 : {
191     int res = MC_automaton_evaluate_label(l->u.exp_not);
192     return (!res);
193   }
194   case 3 : { 
195     unsigned int cursor = 0;
196     xbt_propositional_symbol_t p = NULL;
197     int_f_void_t f;
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;
201         return (*f)();
202       }
203     }
204     return -1;
205   }
206   case 4 : {
207     return 2;
208   }
209   default : 
210     return -1;
211   }
212 }
213
214
215 /********************* Double-DFS stateless *******************/
216
217 void pair_stateless_free(mc_pair_stateless_t pair){
218   xbt_free(pair);
219 }
220
221 void pair_stateless_free_voidp(void *p){
222   pair_stateless_free((mc_pair_stateless_t) * (void **) p);
223 }
224
225 mc_pair_stateless_t new_pair_stateless(mc_state_t sg, xbt_state_t st, int r){
226   mc_pair_stateless_t p = NULL;
227   p = xbt_new0(s_mc_pair_stateless_t, 1);
228   p->automaton_state = st;
229   p->graph_state = sg;
230   p->requests = r;
231   mc_stats_pair->expanded_pairs++;
232   return p;
233 }
234
235 void pair_reached_free(mc_pair_reached_t pair){
236   if(pair){
237     pair->automaton_state = NULL;
238     xbt_dynar_free(&(pair->prop_ato));
239     MC_free_snapshot(pair->system_state);
240     xbt_free(pair);
241   }
242 }
243
244 void pair_reached_free_voidp(void *p){
245   pair_reached_free((mc_pair_reached_t) * (void **) p);
246 }
247
248 void MC_ddfs_init(void){
249
250   raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
251
252   XBT_INFO("**************************************************");
253   XBT_INFO("Double-DFS init");
254   XBT_INFO("**************************************************");
255
256   mc_pair_stateless_t mc_initial_pair = NULL;
257   mc_state_t initial_graph_state = NULL;
258   smx_process_t process; 
259
260  
261   MC_wait_for_requests();
262
263   MC_SET_RAW_MEM;
264
265   initial_graph_state = MC_state_pair_new();
266   xbt_swag_foreach(process, simix_global->process_list){
267     if(MC_process_is_enabled(process)){
268       MC_state_interleave_process(initial_graph_state, process);
269     }
270   }
271
272   reached_pairs = xbt_dynar_new(sizeof(mc_pair_reached_t), pair_reached_free_voidp);
273   successors = xbt_dynar_new(sizeof(mc_pair_stateless_t), NULL);
274
275   /* Save the initial state */
276   initial_state_liveness = xbt_new0(s_mc_global_t, 1);
277   initial_state_liveness->initial_snapshot = MC_take_snapshot_liveness();
278
279   MC_UNSET_RAW_MEM; 
280   
281   unsigned int cursor = 0;
282   xbt_state_t state;
283
284   xbt_dynar_foreach(_mc_property_automaton->states, cursor, state){
285     if(state->type == -1){
286       
287       MC_SET_RAW_MEM;
288       mc_initial_pair = new_pair_stateless(initial_graph_state, state, MC_state_interleave_size(initial_graph_state));
289       xbt_fifo_unshift(mc_stack_liveness, mc_initial_pair);
290       MC_UNSET_RAW_MEM;
291       
292       if(cursor != 0){
293         MC_restore_snapshot(initial_state_liveness->initial_snapshot);
294         MC_UNSET_RAW_MEM;
295       }
296
297       MC_ddfs(0);
298
299     }else{
300       if(state->type == 2){
301       
302         MC_SET_RAW_MEM;
303         mc_initial_pair = new_pair_stateless(initial_graph_state, state, MC_state_interleave_size(initial_graph_state));
304         xbt_fifo_unshift(mc_stack_liveness, mc_initial_pair);
305         MC_UNSET_RAW_MEM;
306
307         set_pair_reached(state);
308
309         if(cursor != 0){
310           MC_restore_snapshot(initial_state_liveness->initial_snapshot);
311           MC_UNSET_RAW_MEM;
312         }
313   
314         MC_ddfs(1);
315   
316       }
317     }
318   }
319
320   if(raw_mem_set)
321     MC_SET_RAW_MEM;
322   else
323     MC_UNSET_RAW_MEM;
324   
325
326 }
327
328
329 void MC_ddfs(int search_cycle){
330
331   raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
332
333   smx_process_t process;
334   mc_pair_stateless_t current_pair = NULL;
335
336   if(xbt_fifo_size(mc_stack_liveness) == 0)
337     return;
338
339
340   /* Get current pair */
341   current_pair = (mc_pair_stateless_t)xbt_fifo_get_item_content(xbt_fifo_get_first_item(mc_stack_liveness));
342
343   /* Update current state in buchi automaton */
344   _mc_property_automaton->current_state = current_pair->automaton_state;
345
346  
347   XBT_DEBUG("********************* ( Depth = %d, search_cycle = %d )", xbt_fifo_size(mc_stack_liveness), search_cycle);
348  
349   mc_stats_pair->visited_pairs++;
350
351   //sleep(1);
352
353   int value;
354   mc_state_t next_graph_state = NULL;
355   smx_simcall_t req = NULL;
356   char *req_str;
357
358   xbt_transition_t transition_succ;
359   unsigned int cursor = 0;
360   int res;
361
362   mc_pair_stateless_t next_pair = NULL;
363   mc_pair_stateless_t pair_succ;
364   
365   if(xbt_fifo_size(mc_stack_liveness) < MAX_DEPTH_LIVENESS){
366
367     if(current_pair->requests > 0){
368
369       while((req = MC_state_get_request(current_pair->graph_state, &value)) != NULL){
370    
371         /* Debug information */
372        
373         req_str = MC_request_to_string(req, value);
374         XBT_DEBUG("Execute: %s", req_str);
375         xbt_free(req_str);
376
377         MC_state_set_executed_request(current_pair->graph_state, req, value);   
378
379         /* Answer the request */
380         SIMIX_simcall_pre(req, value);
381
382         /* Wait for requests (schedules processes) */
383         MC_wait_for_requests();
384
385         MC_SET_RAW_MEM;
386
387         /* Create the new expanded graph_state */
388         next_graph_state = MC_state_pair_new();
389
390         /* Get enabled process and insert it in the interleave set of the next graph_state */
391         xbt_swag_foreach(process, simix_global->process_list){
392           if(MC_process_is_enabled(process)){
393             MC_state_interleave_process(next_graph_state, process);
394           }
395         }
396
397         xbt_dynar_reset(successors);
398
399         MC_UNSET_RAW_MEM;
400
401
402         cursor= 0;
403         xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
404
405           res = MC_automaton_evaluate_label(transition_succ->label);
406
407           if(res == 1){ // enabled transition in automaton
408             MC_SET_RAW_MEM;
409             next_pair = new_pair_stateless(next_graph_state, transition_succ->dst, MC_state_interleave_size(next_graph_state));
410             xbt_dynar_push(successors, &next_pair);
411             MC_UNSET_RAW_MEM;
412           }
413
414         }
415
416         cursor = 0;
417    
418         xbt_dynar_foreach(current_pair->automaton_state->out, cursor, transition_succ){
419       
420           res = MC_automaton_evaluate_label(transition_succ->label);
421   
422           if(res == 2){ // true transition in automaton
423             MC_SET_RAW_MEM;
424             next_pair = new_pair_stateless(next_graph_state, transition_succ->dst, MC_state_interleave_size(next_graph_state));
425             xbt_dynar_push(successors, &next_pair);
426             MC_UNSET_RAW_MEM;
427           }
428
429         }
430
431         cursor = 0; 
432   
433         xbt_dynar_foreach(successors, cursor, pair_succ){
434
435           if(search_cycle == 1){
436
437             if((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2)){ 
438           
439               if(reached(pair_succ->automaton_state)){
440         
441                 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));
442
443                 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
444                 XBT_INFO("|             ACCEPTANCE CYCLE            |");
445                 XBT_INFO("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
446                 XBT_INFO("Counter-example that violates formula :");
447                 MC_show_stack_liveness(mc_stack_liveness);
448                 MC_dump_stack_liveness(mc_stack_liveness);
449                 MC_print_statistics_pairs(mc_stats_pair);
450                 xbt_abort();
451
452               }else{
453
454                 XBT_DEBUG("Next pair (depth =%d) -> Acceptance pair (%s)", xbt_fifo_size(mc_stack_liveness) + 1, pair_succ->automaton_state->id);
455
456                 XBT_DEBUG("Reached pairs : %lu", xbt_dynar_length(reached_pairs));
457
458                 MC_SET_RAW_MEM;
459                 xbt_fifo_unshift(mc_stack_liveness, pair_succ);
460                 MC_UNSET_RAW_MEM;
461     
462                 MC_ddfs(search_cycle);
463
464               }
465
466             }else{
467
468               MC_SET_RAW_MEM;
469               xbt_fifo_unshift(mc_stack_liveness, pair_succ);
470               MC_UNSET_RAW_MEM;
471               
472               MC_ddfs(search_cycle);
473                
474             }
475
476           }else{
477     
478             if(((pair_succ->automaton_state->type == 1) || (pair_succ->automaton_state->type == 2))){
479
480               XBT_DEBUG("Next pair (depth =%d) -> Acceptance pair (%s)", xbt_fifo_size(mc_stack_liveness) + 1, pair_succ->automaton_state->id);
481       
482               set_pair_reached(pair_succ->automaton_state); 
483
484               search_cycle = 1;
485
486               XBT_DEBUG("Reached pairs : %lu", xbt_dynar_length(reached_pairs));
487
488             }
489
490             MC_SET_RAW_MEM;
491             xbt_fifo_unshift(mc_stack_liveness, pair_succ);
492             MC_UNSET_RAW_MEM;
493             
494             MC_ddfs(search_cycle);
495            
496           }
497
498    
499           /* Restore system before checking others successors */
500           if(cursor != (xbt_dynar_length(successors) - 1))
501             MC_replay_liveness(mc_stack_liveness, 1);
502   
503     
504         }
505
506         if(MC_state_interleave_size(current_pair->graph_state) > 0){
507           XBT_DEBUG("Backtracking to depth %d", xbt_fifo_size(mc_stack_liveness));
508           MC_replay_liveness(mc_stack_liveness, 0);
509         }
510       }
511
512  
513     }
514     
515   }else{
516     
517     XBT_DEBUG("Max depth reached");
518
519   }
520
521   if(xbt_fifo_size(mc_stack_liveness) == MAX_DEPTH_LIVENESS ){
522     XBT_DEBUG("Pair (depth = %d) shifted in stack, maximum depth reached", xbt_fifo_size(mc_stack_liveness) );
523   }else{
524     XBT_DEBUG("Pair (depth = %d) shifted in stack", xbt_fifo_size(mc_stack_liveness) );
525   }
526
527   
528   MC_SET_RAW_MEM;
529   xbt_fifo_shift(mc_stack_liveness);
530   if((current_pair->automaton_state->type == 1) || (current_pair->automaton_state->type == 2)){
531     xbt_dynar_pop(reached_pairs, NULL);
532   }
533   MC_UNSET_RAW_MEM;
534
535   if(raw_mem_set)
536     MC_SET_RAW_MEM;
537   else
538     MC_UNSET_RAW_MEM;
539
540 }