Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : cleanups in mc_snapshot structure
[simgrid.git] / src / mc / mc_compare.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
8 #include "xbt/mmalloc.h"
9
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_compare, mc,
11                                 "Logging specific to mc_compare");
12
13 static int heap_region_compare(void *d1, void *d2, size_t size);
14
15 static int is_heap_equality(xbt_dynar_t equals, void *a1, void *a2);
16 static size_t heap_ignore_size(void *address);
17
18 static void stack_region_free(stack_region_t s);
19 static void heap_equality_free(heap_equality_t e);
20
21 static int is_stack_ignore_variable(char *frame, char *var_name);
22 static int compare_local_variables(char *s1, char *s2);
23 static int compare_global_variables(int region_type, void *d1, void *d2);
24
25 static size_t heap_ignore_size(void *address){
26   unsigned int cursor = 0;
27   int start = 0;
28   int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
29   mc_heap_ignore_region_t region;
30
31   while(start <= end){
32     cursor = (start + end) / 2;
33     region = (mc_heap_ignore_region_t)xbt_dynar_get_as(mc_heap_comparison_ignore, cursor, mc_heap_ignore_region_t);
34     if(region->address == address)
35       return region->size;
36     if(region->address < address)
37       start = cursor + 1;
38     if(region->address > address)
39       end = cursor - 1;   
40   }
41
42   return 0;
43 }
44
45 static int compare_global_variables(int region_type, void *d1, void *d2){
46
47   unsigned int cursor = 0;
48   size_t offset; 
49   int i = 0;
50   global_variable_t current_var; 
51   int pointer_align; 
52   void *addr_pointed1 = NULL, *addr_pointed2 = NULL;
53   int res_compare = 0;
54
55   if(region_type == 1){ /* libsimgrid */
56     xbt_dynar_foreach(mc_global_variables, cursor, current_var){
57       if(current_var->address < start_data_libsimgrid)
58         continue;
59       offset = (char *)current_var->address - (char *)start_data_libsimgrid;
60       i = 0;
61       while(i < current_var->size){
62         if(memcmp((char*)d1 + offset + i, (char*)d2 + offset + i, 1) != 0){
63           pointer_align = (i / sizeof(void*)) * sizeof(void*); 
64           addr_pointed1 = *((void **)((char *)d1 + offset + pointer_align));
65           addr_pointed2 = *((void **)((char *)d2 + offset + pointer_align));
66           if((addr_pointed1 > start_plt_libsimgrid && addr_pointed1 < end_plt_libsimgrid) || (addr_pointed2 > start_plt_libsimgrid && addr_pointed2 < end_plt_libsimgrid)){
67             i = current_var->size;
68             continue;
69           }else{
70             if((addr_pointed1 > std_heap) && ((char *)addr_pointed1 < (char *)std_heap + STD_HEAP_SIZE) && (addr_pointed2 > std_heap) && ((char *)addr_pointed2 < (char *)std_heap + STD_HEAP_SIZE)){
71               res_compare = compare_area(addr_pointed1, addr_pointed2, NULL);
72               if(res_compare == 1){
73                 #ifdef MC_VERBOSE
74                   XBT_VERB("Different global variable in libsimgrid : %s at addresses %p - %p (size = %zu)", current_var->name, (char *)d1+offset, (char *)d2+offset, current_var->size);
75                 #endif
76                 return 1;
77               }
78             }else{
79               #ifdef MC_VERBOSE
80                 XBT_VERB("Different global variable in libsimgrid : %s at addresses %p - %p (size = %zu)", current_var->name, (char *)d1+offset, (char *)d2+offset, current_var->size);
81               #endif
82               return 1;
83             }
84            
85           }
86         } 
87         i++;
88       }
89     }
90   }else{ /* binary */
91     xbt_dynar_foreach(mc_global_variables, cursor, current_var){
92       if(current_var->address > start_data_libsimgrid)
93         break;
94       offset = (char *)current_var->address - (char *)start_data_binary;
95       i = 0;
96       while(i < current_var->size){
97         if(memcmp((char*)d1 + offset + i, (char*)d2 + offset + i, 1) != 0){
98           pointer_align = (i / sizeof(void*)) * sizeof(void*); 
99           addr_pointed1 = *((void **)((char *)d1 + offset + pointer_align));
100           addr_pointed2 = *((void **)((char *)d2 + offset + pointer_align));
101           if((addr_pointed1 > start_plt_binary && addr_pointed1 < end_plt_binary) || (addr_pointed2 > start_plt_binary && addr_pointed2 < end_plt_binary)){
102             i = current_var->size;
103             continue;
104           }else{
105             if((addr_pointed1 > std_heap) && ((char *)addr_pointed1 < (char *)std_heap + STD_HEAP_SIZE) && (addr_pointed2 > std_heap) && ((char *)addr_pointed2 < (char *)std_heap + STD_HEAP_SIZE)){
106               res_compare = compare_area(addr_pointed1, addr_pointed2, NULL);
107               if(res_compare == 1){
108                 #ifdef MC_VERBOSE
109                   XBT_VERB("Different global variable in binary : %s at addresses %p - %p (size = %zu)", current_var->name, (char *)d1+offset, (char *)d2+offset, current_var->size);
110                 #endif
111                 return 1;
112               }
113             }else{
114               #ifdef MC_VERBOSE
115                 XBT_VERB("Different global variable in binary : %s at addresses %p - %p (size = %zu)", current_var->name, (char *)d1+offset, (char *)d2+offset, current_var->size);
116               #endif
117               return 1;
118             }
119           }
120         } 
121         i++;
122       }
123       
124     }
125   }
126
127   return 0;
128 }
129
130 static int heap_region_compare(void *d1, void *d2, size_t size){
131   
132   size_t i = 0;
133   
134   for(i=0; i<size; i++){
135     if(memcmp(((char *)d1) + i, ((char *)d2) + i, 1) != 0){
136       if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_verbose)){
137         XBT_VERB("Different byte (offset=%zu) (%p - %p) in heap region", i, (char *)d1 + i, (char *)d2 + i);
138       }
139       return 1;
140     }
141   }
142   
143   return 0;
144 }
145
146 static void stack_region_free(stack_region_t s){
147   if(s){
148     xbt_free(s->process_name);
149     xbt_free(s);
150   }
151 }
152
153 void stack_region_free_voidp(void *s){
154   stack_region_free((stack_region_t) * (void **) s);
155 }
156
157 static void heap_equality_free(heap_equality_t e){
158   xbt_free(e);
159 }
160
161 void heap_equality_free_voidp(void *e){
162   heap_equality_free((heap_equality_t) * (void **) e);
163 }
164
165 int SIMIX_pre_mc_compare_snapshots(smx_simcall_t simcall,
166                                    mc_snapshot_t s1, mc_snapshot_t s2){
167   return snapshot_compare(s1, s2);
168 }
169
170 int snapshot_compare(mc_snapshot_t s1, mc_snapshot_t s2){
171
172   int raw_mem = (mmalloc_get_current_heap() == raw_heap);
173   
174   MC_SET_RAW_MEM;
175      
176   int errors = 0;
177
178   xbt_os_timer_t global_timer = xbt_os_timer_new();
179   xbt_os_timer_t timer = xbt_os_timer_new();
180
181   xbt_os_timer_start(global_timer);
182
183   #ifdef MC_DEBUG
184     xbt_os_timer_start(timer);
185   #endif
186
187   /* Compare number of processes */
188   if(s1->nb_processes != s2->nb_processes){
189     #ifdef MC_DEBUG
190       xbt_os_timer_stop(timer);
191       mc_comp_times->nb_processes_comparison_time =  xbt_os_timer_elapsed(timer);
192       XBT_DEBUG("Different number of processes : %d - %d", s1->nb_processes, s2->nb_processes);
193       errors++;
194     #else
195       #ifdef MC_VERBOSE
196         XBT_VERB("Different number of processes : %d - %d", s1->nb_processes, s2->nb_processes);
197       #endif
198      
199       xbt_os_timer_free(timer);
200       xbt_os_timer_stop(global_timer);
201       mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
202       xbt_os_timer_free(global_timer);
203
204       if(!raw_mem)
205         MC_UNSET_RAW_MEM;
206
207       return 1;
208     #endif
209   }
210
211   #ifdef MC_DEBUG
212     xbt_os_timer_start(timer);
213   #endif
214
215   /* Compare number of bytes used in each heap */
216   if(s1->heap_bytes_used != s2->heap_bytes_used){
217     #ifdef MC_DEBUG
218       xbt_os_timer_stop(timer);
219       mc_comp_times->bytes_used_comparison_time = xbt_os_timer_elapsed(timer);
220       XBT_DEBUG("Different number of bytes used in each heap : %zu - %zu", s1->heap_bytes_used, s2->heap_bytes_used);
221       errors++;
222     #else
223       #ifdef MC_VERBOSE
224         XBT_VERB("Different number of bytes used in each heap : %zu - %zu", s1->heap_bytes_used, s2->heap_bytes_used);
225       #endif
226
227       xbt_os_timer_free(timer);
228       xbt_os_timer_stop(global_timer);
229       mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
230       xbt_os_timer_free(global_timer);
231
232       if(!raw_mem)
233         MC_UNSET_RAW_MEM;
234
235       return 1;
236     #endif
237   }else{
238     #ifdef MC_DEBUG
239       xbt_os_timer_stop(timer);
240     #endif
241   }
242   
243   #ifdef MC_DEBUG
244     xbt_os_timer_start(timer);
245   #endif
246   
247   /* Compare size of stacks */
248   int i = 0;
249   size_t size_used1, size_used2;
250   int is_diff = 0;
251   while(i < xbt_dynar_length(s1->stacks)){
252     size_used1 = s1->stack_sizes[i];
253     size_used2 = s2->stack_sizes[i];
254     if(size_used1 != size_used2){
255     #ifdef MC_DEBUG
256       if(is_diff == 0){
257         xbt_os_timer_stop(timer);
258         mc_comp_times->stacks_sizes_comparison_time = xbt_os_timer_elapsed(timer);
259       }
260       XBT_DEBUG("Different size used in stacks : %zu - %zu", size_used1, size_used2);
261       errors++;
262       is_diff = 1;
263     #else
264       #ifdef MC_VERBOSE
265         XBT_VERB("Different size used in stacks : %zu - %zu", size_used1, size_used2);
266       #endif
267  
268       xbt_os_timer_free(timer);
269       xbt_os_timer_stop(global_timer);
270       mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
271       xbt_os_timer_free(global_timer);
272
273       if(!raw_mem)
274         MC_UNSET_RAW_MEM;
275
276       return 1;
277     #endif  
278     }
279     i++;
280   }
281
282   #ifdef MC_DEBUG
283     if(is_diff == 0)
284       xbt_os_timer_stop(timer);
285     xbt_os_timer_start(timer);
286   #endif
287
288   /* Init heap information used in heap comparison algorithm */
289   init_heap_information((xbt_mheap_t)s1->regions[0]->data, (xbt_mheap_t)s2->regions[0]->data, s1->to_ignore, s2->to_ignore);
290
291   /* Compare binary global variables */
292   is_diff = compare_global_variables(2, s1->regions[2]->data, s2->regions[2]->data);
293   if(is_diff != 0){
294     #ifdef MC_DEBUG
295       xbt_os_timer_stop(timer);
296       mc_comp_times->binary_global_variables_comparison_time = xbt_os_timer_elapsed(timer);
297       XBT_DEBUG("Different global variables in binary"); 
298       errors++; 
299     #else
300       #ifdef MC_VERBOSE
301         XBT_VERB("Different global variables in binary"); 
302       #endif
303     
304       xbt_os_timer_free(timer);
305       xbt_os_timer_stop(global_timer);
306       mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
307       xbt_os_timer_free(global_timer);
308     
309       if(!raw_mem)
310         MC_UNSET_RAW_MEM;
311
312       return 1;
313     #endif
314   }
315
316   #ifdef MC_DEBUG
317     if(is_diff == 0)
318       xbt_os_timer_stop(timer);
319     xbt_os_timer_start(timer);
320   #endif
321
322   /* Compare libsimgrid global variables */
323   is_diff = compare_global_variables(1, s1->regions[1]->data, s2->regions[1]->data);
324   if(is_diff != 0){
325     #ifdef MC_DEBUG
326       xbt_os_timer_stop(timer);
327       mc_comp_times->libsimgrid_global_variables_comparison_time = xbt_os_timer_elapsed(timer); 
328       XBT_DEBUG("Different global variables in libsimgrid"); 
329       errors++; 
330     #else
331       #ifdef MC_VERBOSE
332         XBT_VERB("Different global variables in libsimgrid"); 
333       #endif
334     
335       xbt_os_timer_free(timer);
336       xbt_os_timer_stop(global_timer);
337       mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
338       xbt_os_timer_free(global_timer);
339     
340       if(!raw_mem)
341         MC_UNSET_RAW_MEM;
342
343       return 1;
344     #endif
345   }  
346
347   #ifdef MC_DEBUG
348     if(is_diff == 0)
349       xbt_os_timer_stop(timer);
350     xbt_os_timer_start(timer);
351   #endif
352
353     /* Stacks comparison */
354     unsigned int  cursor = 0;
355     int diff_local = 0;
356     is_diff = 0;
357     
358     while(cursor < xbt_dynar_length(s1->stacks)){
359       diff_local = compare_local_variables(((mc_snapshot_stack_t)xbt_dynar_get_as(s1->stacks, cursor, mc_snapshot_stack_t))->local_variables->data, ((mc_snapshot_stack_t)xbt_dynar_get_as(s2->stacks, cursor, mc_snapshot_stack_t))->local_variables->data);
360       if(diff_local > 0){
361         #ifdef MC_DEBUG
362           if(is_diff == 0){
363             xbt_os_timer_stop(timer);
364             mc_comp_times->stacks_comparison_time = xbt_os_timer_elapsed(timer); 
365           }
366           XBT_DEBUG("Different local variables between stacks %d", cursor + 1);
367           errors++;
368           is_diff = 1;
369         #else
370         
371           #ifdef MC_VERBOSE
372             XBT_VERB("Different local variables between stacks %d", cursor + 1);
373           #endif
374           
375             reset_heap_information();
376             xbt_os_timer_free(timer);
377             xbt_os_timer_stop(global_timer);
378             mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
379             xbt_os_timer_free(global_timer);
380             
381             if(!raw_mem)
382               MC_UNSET_RAW_MEM;
383             
384             return 1;
385         #endif
386       }
387       cursor++;
388     }
389     
390     #ifdef MC_DEBUG
391       xbt_os_timer_start(timer);
392     #endif
393
394   /* Compare heap */
395  
396   if(mmalloc_compare_heap((xbt_mheap_t)s1->regions[0]->data, (xbt_mheap_t)s2->regions[0]->data)){
397
398     #ifdef MC_DEBUG
399       xbt_os_timer_stop(timer);
400       mc_comp_times->heap_comparison_time = xbt_os_timer_elapsed(timer); 
401       XBT_DEBUG("Different heap (mmalloc_compare)");
402       errors++;
403     #else
404
405       xbt_os_timer_free(timer);
406  
407       #ifdef MC_VERBOSE
408         XBT_VERB("Different heap (mmalloc_compare)");
409       #endif
410        
411       reset_heap_information();
412       xbt_os_timer_stop(global_timer);
413       mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
414       xbt_os_timer_free(global_timer);
415
416       if(!raw_mem)
417         MC_UNSET_RAW_MEM;
418
419       return 1;
420     #endif
421   }else{
422     #ifdef MC_DEBUG
423       xbt_os_timer_stop(timer);
424     #endif
425   }
426
427   reset_heap_information();
428    
429   xbt_os_timer_free(timer);
430
431   #ifdef MC_VERBOSE
432     xbt_os_timer_stop(global_timer);
433     mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
434   #endif
435
436   xbt_os_timer_free(global_timer);
437
438   #ifdef MC_DEBUG
439     print_comparison_times();
440   #endif
441
442   if(!raw_mem)
443     MC_UNSET_RAW_MEM;
444
445   return errors > 0;
446   
447 }
448
449 static int is_stack_ignore_variable(char *frame, char *var_name){
450
451   unsigned int cursor = 0;
452   int start = 0;
453   int end = xbt_dynar_length(mc_stack_comparison_ignore) - 1;
454   mc_stack_ignore_variable_t current_var;
455
456   while(start <= end){
457     cursor = (start + end) / 2;
458     current_var = (mc_stack_ignore_variable_t)xbt_dynar_get_as(mc_stack_comparison_ignore, cursor, mc_stack_ignore_variable_t);
459     if(strcmp(current_var->frame, frame) == 0 || strcmp(current_var->frame, "*") == 0){
460       if(strcmp(current_var->var_name, var_name) == 0)
461         return 1;
462       if(strcmp(current_var->var_name, var_name) < 0)
463         start = cursor + 1;
464       if(strcmp(current_var->var_name, var_name) > 0)
465         end = cursor - 1;
466     }else if(strcmp(current_var->frame, frame) < 0){
467       start = cursor + 1;
468     }else if(strcmp(current_var->frame, frame) > 0){
469       end = cursor - 1; 
470     }
471   }
472
473   return 0;
474 }
475
476 static int compare_local_variables(char *s1, char *s2){
477   
478   xbt_dynar_t tokens1 = xbt_str_split(s1, NULL);
479   xbt_dynar_t tokens2 = xbt_str_split(s2, NULL);
480
481   xbt_dynar_t s_tokens1, s_tokens2;
482   unsigned int cursor = 0;
483   void *addr1, *addr2;
484   char *frame_name1 = NULL, *frame_name2 = NULL;
485   int res_compare = 0;
486
487   #ifdef MC_VERBOSE
488     char *var_name;
489   #endif
490
491   while(cursor < xbt_dynar_length(tokens1)){
492     s_tokens1 = xbt_str_split(xbt_dynar_get_as(tokens1, cursor, char *), "=");
493     s_tokens2 = xbt_str_split(xbt_dynar_get_as(tokens2, cursor, char *), "=");
494     if(xbt_dynar_length(s_tokens1) > 1 && xbt_dynar_length(s_tokens2) > 1){
495       #ifdef MC_VERBOSE
496         var_name = xbt_dynar_get_as(s_tokens1, 0, char *);
497       #endif
498       if((strcmp(xbt_dynar_get_as(s_tokens1, 0, char *), "frame_name") == 0) && (strcmp(xbt_dynar_get_as(s_tokens2, 0, char *), "frame_name") == 0)){
499         xbt_free(frame_name1);
500         xbt_free(frame_name2);
501         frame_name1 = strdup(xbt_dynar_get_as(s_tokens1, 1, char *));
502         frame_name2 = strdup(xbt_dynar_get_as(s_tokens2, 1, char *));
503       }
504       addr1 = (void *) strtoul(xbt_dynar_get_as(s_tokens1, 1, char *), NULL, 16);
505       addr2 = (void *) strtoul(xbt_dynar_get_as(s_tokens2, 1, char *), NULL, 16);
506       if(addr1 > std_heap && (char *)addr1 <= (char *)std_heap + STD_HEAP_SIZE && addr2 > std_heap && (char *)addr2 <= (char *)std_heap + STD_HEAP_SIZE){
507         res_compare = compare_area(addr1, addr2, NULL);
508         if(res_compare == 1){
509           if(is_stack_ignore_variable(frame_name1, xbt_dynar_get_as(s_tokens1, 0, char *)) && is_stack_ignore_variable(frame_name2, xbt_dynar_get_as(s_tokens2, 0, char *))){
510             xbt_dynar_free(&s_tokens1);
511             xbt_dynar_free(&s_tokens2);
512             cursor++;
513             continue;
514           }else {
515             #ifdef MC_VERBOSE
516               XBT_VERB("Different local variable : %s at addresses %p - %p", var_name, addr1, addr2);
517             #endif
518             xbt_dynar_free(&s_tokens1);
519             xbt_dynar_free(&s_tokens2);
520             xbt_dynar_free(&tokens1);
521             xbt_dynar_free(&tokens2);
522             xbt_free(frame_name1);
523             xbt_free(frame_name2);
524             return 1;
525           }
526         }
527       }else{
528         if(strcmp(xbt_dynar_get_as(s_tokens1, 1, char *), xbt_dynar_get_as(s_tokens2, 1, char *)) != 0){
529           if(is_stack_ignore_variable(frame_name1, xbt_dynar_get_as(s_tokens1, 0, char *)) && is_stack_ignore_variable(frame_name2, xbt_dynar_get_as(s_tokens2, 0, char *))){
530             xbt_dynar_free(&s_tokens1);
531             xbt_dynar_free(&s_tokens2);
532             cursor++;
533             continue;
534           }else {
535             #ifdef MC_VERBOSE
536               XBT_VERB("Different local variable : %s (%s - %s)", var_name, xbt_dynar_get_as(s_tokens1, 1, char *), xbt_dynar_get_as(s_tokens2, 1, char *));
537             #endif
538             xbt_dynar_free(&s_tokens1);
539             xbt_dynar_free(&s_tokens2);
540             xbt_dynar_free(&tokens1);
541             xbt_dynar_free(&tokens2);
542             xbt_free(frame_name1);
543             xbt_free(frame_name2);
544             return 1;
545           }
546         }
547       }
548     }
549     xbt_dynar_free(&s_tokens1);
550     xbt_dynar_free(&s_tokens2);
551          
552     cursor++;
553   }
554
555   xbt_free(frame_name1);
556   xbt_free(frame_name2);
557   xbt_dynar_free(&tokens1);
558   xbt_dynar_free(&tokens2);
559   return 0;
560   
561 }
562
563 static int is_heap_equality(xbt_dynar_t equals, void *a1, void *a2){
564
565   unsigned int cursor = 0;
566   int start = 0;
567   int end = xbt_dynar_length(equals) - 1;
568   heap_equality_t current_equality;
569
570   while(start <= end){
571     cursor = (start + end) / 2;
572     current_equality = (heap_equality_t)xbt_dynar_get_as(equals, cursor, heap_equality_t);
573     if(current_equality->address1 == a1){
574       if(current_equality->address2 == a2)
575         return 1;
576       if(current_equality->address2 < a2)
577         start = cursor + 1;
578       if(current_equality->address2 > a2)
579         end = cursor - 1;
580     }
581     if(current_equality->address1 < a1)
582       start = cursor + 1;
583     if(current_equality->address1 > a1)
584       end = cursor - 1; 
585   }
586
587   return 0;
588
589 }
590
591 int MC_compare_snapshots(void *s1, void *s2){
592   
593   MC_ignore_stack("self", "simcall_BODY_mc_snapshot");
594
595   return simcall_mc_compare_snapshots(s1, s2);
596
597 }
598
599 void print_comparison_times(){
600   XBT_DEBUG("*** Comparison times ***");
601   XBT_DEBUG("- Nb processes : %f", mc_comp_times->nb_processes_comparison_time);
602   XBT_DEBUG("- Nb bytes used : %f", mc_comp_times->bytes_used_comparison_time);
603   XBT_DEBUG("- Stacks sizes : %f", mc_comp_times->stacks_sizes_comparison_time);
604   XBT_DEBUG("- Binary global variables : %f", mc_comp_times->binary_global_variables_comparison_time);
605   XBT_DEBUG("- Libsimgrid global variables : %f", mc_comp_times->libsimgrid_global_variables_comparison_time);
606   XBT_DEBUG("- Heap : %f", mc_comp_times->heap_comparison_time);
607   XBT_DEBUG("- Stacks : %f", mc_comp_times->stacks_comparison_time);
608 }