]> AND Public Git Repository - simgrid.git/blob - src/mc/mc_compare.c
Logo AND Algorithmique Numérique Distribuée

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