Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c80f0464d060690989acfa90c4c3a3b2ead279d0
[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 data_program_region_compare(void *d1, void *d2, size_t size);
14 static int data_libsimgrid_region_compare(void *d1, void *d2, size_t size);
15 static int heap_region_compare(void *d1, void *d2, size_t size);
16
17 static int compare_stack(stack_region_t s1, stack_region_t s2, void *sp1, void *sp2, void *heap1, void *heap2, xbt_dynar_t equals);
18 static int is_heap_equality(xbt_dynar_t equals, void *a1, void *a2);
19 static size_t heap_ignore_size(void *address);
20
21 static void stack_region_free(stack_region_t s);
22 static void heap_equality_free(heap_equality_t e);
23
24 static int is_stack_ignore_variable(char *frame, char *var_name);
25 static int compare_local_variables(char *s1, char *s2, xbt_dynar_t heap_equals);
26
27 static size_t heap_ignore_size(void *address){
28   unsigned int cursor = 0;
29   int start = 0;
30   int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
31   mc_heap_ignore_region_t region;
32
33   while(start <= end){
34     cursor = (start + end) / 2;
35     region = (mc_heap_ignore_region_t)xbt_dynar_get_as(mc_heap_comparison_ignore, cursor, mc_heap_ignore_region_t);
36     if(region->address == address)
37       return region->size;
38     if(region->address < address)
39       start = cursor + 1;
40     if(region->address > address)
41       end = cursor - 1;   
42   }
43
44   return 0;
45 }
46
47 static int data_program_region_compare(void *d1, void *d2, size_t size){
48
49   size_t i = 0;
50   int pointer_align;
51   void *addr_pointed1 = NULL, *addr_pointed2 = NULL;
52   
53   for(i=0; i<size; i++){
54     if(memcmp(((char *)d1) + i, ((char *)d2) + i, 1) != 0){
55       pointer_align = (i / sizeof(void*)) * sizeof(void*);
56       addr_pointed1 = *((void **)((char *)d1 + pointer_align));
57       addr_pointed2 = *((void **)((char *)d2 + pointer_align));
58       if((addr_pointed1 > start_plt_binary && addr_pointed1 < end_plt_binary) || (addr_pointed2 > start_plt_binary && addr_pointed2 < end_plt_binary)){
59         continue;
60       }else{
61         if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_verbose)){
62           XBT_VERB("Different byte (offset=%zu) (%p - %p) in data program region", i, (char *)d1 + i, (char *)d2 + i);
63           XBT_VERB("Addresses pointed : %p - %p\n", addr_pointed1, addr_pointed2);
64         }
65         return 1;
66       }
67     }
68   }
69   
70   return 0;
71 }
72
73 static int data_libsimgrid_region_compare(void *d1, void *d2, size_t size){
74
75   size_t i = 0, ignore_size = 0;
76   int pointer_align;
77   void *addr_pointed1 = NULL, *addr_pointed2 = NULL;
78
79   for(i=0; i<size; i++){
80     if(memcmp(((char *)d1) + i, ((char *)d2) + i, 1) != 0){
81       if((ignore_size = heap_ignore_size((char *)start_data_libsimgrid+i)) > 0){
82         i = i + ignore_size;
83         continue;
84       }else if((ignore_size = heap_ignore_size((char *)start_bss_libsimgrid+i)) > 0){
85         i = i + ignore_size;
86         continue;
87       }
88       pointer_align = (i / sizeof(void*)) * sizeof(void*);
89       addr_pointed1 = *((void **)((char *)d1 + pointer_align));
90       addr_pointed2 = *((void **)((char *)d2 + pointer_align));
91       if((addr_pointed1 > start_plt_libsimgrid && addr_pointed1 < end_plt_libsimgrid) || (addr_pointed2 > start_plt_libsimgrid && addr_pointed2 < end_plt_libsimgrid)){
92         continue;
93       }else if(addr_pointed1 >= raw_heap && addr_pointed1 <= end_raw_heap && addr_pointed2 >= raw_heap && addr_pointed2 <= end_raw_heap){
94         continue;
95       }else{
96         if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_verbose)){
97           XBT_VERB("Different byte (offset=%zu) (%p - %p) in data libsimgrid region", i, (char *)d1 + i, (char *)d2 + i);
98           XBT_VERB("Addresses pointed : %p - %p\n", addr_pointed1, addr_pointed2);
99         }
100         return 1;
101       }
102     }
103   }
104   
105   return 0;
106 }
107
108 static int heap_region_compare(void *d1, void *d2, size_t size){
109   
110   size_t i = 0;
111   
112   for(i=0; i<size; i++){
113     if(memcmp(((char *)d1) + i, ((char *)d2) + i, 1) != 0){
114       if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_verbose)){
115         XBT_VERB("Different byte (offset=%zu) (%p - %p) in heap region", i, (char *)d1 + i, (char *)d2 + i);
116       }
117       return 1;
118     }
119   }
120   
121   return 0;
122 }
123
124 static void stack_region_free(stack_region_t s){
125   if(s){
126     xbt_free(s->process_name);
127     xbt_free(s);
128   }
129 }
130
131 void stack_region_free_voidp(void *s){
132   stack_region_free((stack_region_t) * (void **) s);
133 }
134
135 static void heap_equality_free(heap_equality_t e){
136   if(e){
137     xbt_free(e);
138   }
139 }
140
141 void heap_equality_free_voidp(void *e){
142   heap_equality_free((heap_equality_t) * (void **) e);
143 }
144
145 int snapshot_compare(mc_snapshot_t s1, mc_snapshot_t s2){
146
147   raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
148   
149   MC_SET_RAW_MEM;
150
151   int errors = 0, i = 0;
152
153   if(s1->num_reg != s2->num_reg){
154     if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_verbose)){
155       XBT_VERB("Different num_reg (s1 = %u, s2 = %u)", s1->num_reg, s2->num_reg);
156     }
157     return 1;
158   }
159
160   int heap_index = 0, data_libsimgrid_index = 0, data_program_index = 0;
161
162   /* Get index of regions */
163   while(i < s1->num_reg){
164     switch(s1->regions[i]->type){
165     case 0:
166       heap_index = i;
167       i++;
168       break;
169     case 1:
170       data_libsimgrid_index = i;
171       i++;
172       while( i < s1->num_reg && s1->regions[i]->type == 1)
173         i++;
174       break;
175     case 2:
176       data_program_index = i;
177       i++;
178       break;
179     }
180   }
181
182   /* Compare number of blocks/fragments used in each heap */
183   size_t chunks_used1 = mmalloc_get_chunks_used((xbt_mheap_t)s1->regions[heap_index]->data);
184   size_t chunks_used2 = mmalloc_get_chunks_used((xbt_mheap_t)s2->regions[heap_index]->data);
185   if(chunks_used1 != chunks_used2){
186     if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_debug)){
187       XBT_DEBUG("Different number of chunks used in each heap : %zu - %zu", chunks_used1, chunks_used2);
188       errors++;
189     }else{
190       if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_verbose))
191         XBT_VERB("Different number of chunks used in each heap : %zu - %zu", chunks_used1, chunks_used2);
192       if(!raw_mem_set)
193         MC_UNSET_RAW_MEM;
194       return 1;
195     }
196   }
197
198   /* Compare size of stacks */
199   unsigned int cursor = 0;
200   void *addr_stack1, *addr_stack2;
201   void *sp1, *sp2;
202   size_t size_used1, size_used2;
203   while(cursor < xbt_dynar_length(stacks_areas)){
204     addr_stack1 = (char *)s1->regions[heap_index]->data + ((char *)((stack_region_t)xbt_dynar_get_as(stacks_areas, cursor, stack_region_t))->address - (char *)std_heap);
205     addr_stack2 = (char *)s2->regions[heap_index]->data + ((char *)((stack_region_t)xbt_dynar_get_as(stacks_areas, cursor, stack_region_t))->address - (char *)std_heap);
206     sp1 = ((mc_snapshot_stack_t)xbt_dynar_get_as(s1->stacks, cursor, mc_snapshot_stack_t))->stack_pointer;
207     sp2 = ((mc_snapshot_stack_t)xbt_dynar_get_as(s2->stacks, cursor, mc_snapshot_stack_t))->stack_pointer;
208     size_used1 = ((stack_region_t)xbt_dynar_get_as(stacks_areas, cursor, stack_region_t))->size - ((char*)sp1 - (char*)addr_stack1);
209     size_used2 = ((stack_region_t)xbt_dynar_get_as(stacks_areas, cursor, stack_region_t))->size - ((char*)sp2 - (char*)addr_stack2);
210     if(size_used1 != size_used2){
211       if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_debug)){
212         XBT_DEBUG("Different size used in stacks : %zu - %zu", size_used1, size_used2);
213         errors++;
214       }else{
215         if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_verbose))
216           XBT_VERB("Different size used in stacks : %zu - %zu", size_used1, size_used2);
217         if(!raw_mem_set)
218           MC_UNSET_RAW_MEM;
219         return 1;
220       }
221     }
222     cursor++;
223   }
224
225   /* Compare program data segment(s) */
226   i = data_program_index;
227   while(i < s1->num_reg && s1->regions[i]->type == 2){
228     if(data_program_region_compare(s1->regions[i]->data, s2->regions[i]->data, s1->regions[i]->size) != 0){
229       if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_debug)){
230         XBT_DEBUG("Different memcmp for data in program");
231         errors++;
232       }else{
233         if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_verbose))
234           XBT_VERB("Different memcmp for data in program");
235         if(!raw_mem_set)
236           MC_UNSET_RAW_MEM;
237         return 1;
238       }
239     }
240     i++;
241   }
242
243   /* Compare libsimgrid data segment(s) */
244   i = data_libsimgrid_index;
245   while(i < s1->num_reg && s1->regions[i]->type == 1){
246     if(data_libsimgrid_region_compare(s1->regions[i]->data, s2->regions[i]->data, s1->regions[i]->size) != 0){
247       if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_debug)){
248         XBT_DEBUG("Different memcmp for data in libsimgrid");
249         errors++;
250       }else{
251         if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_verbose))
252           XBT_VERB("Different memcmp for data in libsimgrid");
253         if(!raw_mem_set)
254           MC_UNSET_RAW_MEM;
255         return 1;
256       }
257     }
258     i++;
259   }
260
261   /* Compare heap */
262   xbt_dynar_t stacks1 = xbt_dynar_new(sizeof(stack_region_t), stack_region_free_voidp);
263   xbt_dynar_t stacks2 = xbt_dynar_new(sizeof(stack_region_t), stack_region_free_voidp);
264   xbt_dynar_t equals = xbt_dynar_new(sizeof(heap_equality_t), heap_equality_free_voidp);
265   
266   void *heap1 = s1->regions[heap_index]->data, *heap2 = s2->regions[heap_index]->data;
267  
268   if(mmalloc_compare_heap((xbt_mheap_t)s1->regions[heap_index]->data, (xbt_mheap_t)s2->regions[heap_index]->data, &stacks1, &stacks2, &equals)){
269
270     if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_debug)){
271       XBT_DEBUG("Different heap (mmalloc_compare)");
272       errors++;
273     }else{
274
275       xbt_dynar_free(&stacks1);
276       xbt_dynar_free(&stacks2);
277       xbt_dynar_free(&equals);
278  
279       if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_verbose))
280         XBT_VERB("Different heap (mmalloc_compare)");
281       if(!raw_mem_set)
282         MC_UNSET_RAW_MEM;
283       return 1;
284     } 
285   }
286
287   /* Stacks comparison */
288   cursor = 0;
289   stack_region_t stack_region1, stack_region2;
290   int diff = 0, diff_local = 0;
291
292   while(cursor < xbt_dynar_length(stacks1)){
293     stack_region1 = (stack_region_t)(xbt_dynar_get_as(stacks1, cursor, stack_region_t));
294     stack_region2 = (stack_region_t)(xbt_dynar_get_as(stacks2, cursor, stack_region_t));
295     sp1 = ((mc_snapshot_stack_t)xbt_dynar_get_as(s1->stacks, cursor, mc_snapshot_stack_t))->stack_pointer;
296     sp2 = ((mc_snapshot_stack_t)xbt_dynar_get_as(s2->stacks, cursor, mc_snapshot_stack_t))->stack_pointer;
297     diff = compare_stack(stack_region1, stack_region2, sp1, sp2, heap1, heap2, equals);
298
299     if(diff > 0){ /* Differences may be due to padding */  
300       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, equals);
301       if(diff_local > 0){
302         if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_debug)){
303           XBT_DEBUG("Different local variables between stacks %d", cursor + 1);
304           errors++;
305         }else{
306           xbt_dynar_free(&stacks1);
307           xbt_dynar_free(&stacks2);
308           xbt_dynar_free(&equals);
309
310           if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_verbose))
311             XBT_VERB("Different local variables between stacks %d", cursor + 1);
312           
313           if(!raw_mem_set)
314             MC_UNSET_RAW_MEM;
315
316           return 1;
317         }
318       }
319     }
320     cursor++;
321   }
322
323   xbt_dynar_free(&stacks1);
324   xbt_dynar_free(&stacks2);
325   xbt_dynar_free(&equals);
326   if(!raw_mem_set)
327     MC_UNSET_RAW_MEM;
328
329   return errors > 0;
330   
331 }
332
333 static int is_stack_ignore_variable(char *frame, char *var_name){
334
335   unsigned int cursor = 0;
336   int start = 0;
337   int end = xbt_dynar_length(mc_stack_comparison_ignore) - 1;
338   mc_stack_ignore_variable_t current_var;
339
340   while(start <= end){
341     cursor = (start + end) / 2;
342     current_var = (mc_stack_ignore_variable_t)xbt_dynar_get_as(mc_stack_comparison_ignore, cursor, mc_stack_ignore_variable_t);
343     if(strcmp(current_var->frame, frame) == 0){
344       if(strcmp(current_var->var_name, var_name) == 0)
345         return 1;
346       if(strcmp(current_var->var_name, var_name) < 0)
347         start = cursor + 1;
348       if(strcmp(current_var->var_name, var_name) > 0)
349         end = cursor - 1;
350     }
351   if(strcmp(current_var->frame, frame) < 0)
352       start = cursor + 1;
353   if(strcmp(current_var->frame, frame) > 0)
354       end = cursor - 1; 
355   }
356
357   return 0;
358 }
359
360 static int compare_local_variables(char *s1, char *s2, xbt_dynar_t heap_equals){
361   
362   xbt_dynar_t tokens1 = xbt_str_split(s1, NULL);
363   xbt_dynar_t tokens2 = xbt_str_split(s2, NULL);
364
365   xbt_dynar_t s_tokens1, s_tokens2;
366   unsigned int cursor = 0;
367   void *addr1, *addr2;
368   char *ip1 = NULL, *ip2 = NULL;
369   
370   while(cursor < xbt_dynar_length(tokens1)){
371     s_tokens1 = xbt_str_split(xbt_dynar_get_as(tokens1, cursor, char *), "=");
372     s_tokens2 = xbt_str_split(xbt_dynar_get_as(tokens2, cursor, char *), "=");
373     if(xbt_dynar_length(s_tokens1) > 1 && xbt_dynar_length(s_tokens2) > 1){
374       if((strcmp(xbt_dynar_get_as(s_tokens1, 0, char *), "ip") == 0) && (strcmp(xbt_dynar_get_as(s_tokens2, 0, char *), "ip") == 0)){
375         ip1 = strdup(xbt_dynar_get_as(s_tokens1, 1, char *));
376         ip2 = strdup(xbt_dynar_get_as(s_tokens2, 1, char *));
377         /*if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_verbose))
378           XBT_VERB("Instruction pointer : %s, Instruction pointer : %s", ip1, ip2);*/
379       }
380       if(strcmp(xbt_dynar_get_as(s_tokens1, 1, char *), xbt_dynar_get_as(s_tokens2, 1, char *)) != 0){   
381         /* Ignore this variable ?  */
382         if(is_stack_ignore_variable(ip1, xbt_dynar_get_as(s_tokens1, 0, char *)) && is_stack_ignore_variable(ip2, xbt_dynar_get_as(s_tokens2, 0, char *))){
383           xbt_dynar_free(&s_tokens1);
384           xbt_dynar_free(&s_tokens2);
385           cursor++;
386           continue;
387         }
388         addr1 = (void *) strtoul(xbt_dynar_get_as(s_tokens1, 1, char *), NULL, 16);
389         addr2 = (void *) strtoul(xbt_dynar_get_as(s_tokens2, 1, char *), NULL, 16);
390         if(is_heap_equality(heap_equals, addr1, addr2) == 0){
391           if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_verbose))
392             XBT_VERB("Variable %s is different between stacks : %s - %s", xbt_dynar_get_as(s_tokens1, 0, char *), xbt_dynar_get_as(s_tokens1, 1, char *), xbt_dynar_get_as(s_tokens2, 1, char *));
393           xbt_dynar_free(&s_tokens1);
394           xbt_dynar_free(&s_tokens2);
395           return 1;
396         }
397       }
398     }
399     xbt_dynar_free(&s_tokens1);
400     xbt_dynar_free(&s_tokens2);
401     cursor++;
402   }
403
404   xbt_dynar_free(&tokens1);
405   xbt_dynar_free(&tokens2);
406   return 0;
407   
408 }
409
410 static int is_heap_equality(xbt_dynar_t equals, void *a1, void *a2){
411
412   unsigned int cursor = 0;
413   int start = 0;
414   int end = xbt_dynar_length(equals) - 1;
415   heap_equality_t current_equality;
416
417   while(start <= end){
418     cursor = (start + end) / 2;
419     current_equality = (heap_equality_t)xbt_dynar_get_as(equals, cursor, heap_equality_t);
420     if(current_equality->address1 == a1){
421       if(current_equality->address2 == a2)
422         return 1;
423       if(current_equality->address2 < a2)
424         start = cursor + 1;
425       if(current_equality->address2 > a2)
426         end = cursor - 1;
427     }
428     if(current_equality->address1 < a1)
429       start = cursor + 1;
430     if(current_equality->address1 > a1)
431       end = cursor - 1; 
432   }
433
434   return 0;
435
436 }
437
438
439 static int compare_stack(stack_region_t s1, stack_region_t s2, void *sp1, void *sp2, void *heap1, void *heap2, xbt_dynar_t equals){
440   
441   size_t k = 0;
442   size_t size_used1 = s1->size - ((char*)sp1 - (char*)s1->address);
443   size_t size_used2 = s2->size - ((char*)sp2 - (char*)s2->address);
444
445   int pointer_align;
446   void *addr_pointed1 = NULL, *addr_pointed2 = NULL;  
447   
448   while(k < size_used1){
449     if(memcmp((char *)s1->address + s1->size - k, (char *)s2->address + s2->size - k, 1) != 0){
450       pointer_align = ((size_used1 - k) / sizeof(void*)) * sizeof(void*);
451       addr_pointed1 = *((void **)(((char*)s1->address + (s1->size - size_used1)) + pointer_align));
452       addr_pointed2 = *((void **)(((char*)s2->address + (s2->size - size_used2)) + pointer_align));
453       if(is_heap_equality(equals, addr_pointed1, addr_pointed2) == 0){
454         if((addr_pointed1 > std_heap) && (addr_pointed1 < (void *)((char *)std_heap + STD_HEAP_SIZE)) && (addr_pointed2 > std_heap) && (addr_pointed2 < (void *)((char *)std_heap + STD_HEAP_SIZE))){
455           if(is_free_area(addr_pointed1, (xbt_mheap_t)heap1) == 0 || is_free_area(addr_pointed2, (xbt_mheap_t)heap2) == 0){
456             return 1;
457           }
458         }else{
459           return 1;
460         } 
461       }
462     } 
463     k++;
464   }
465  
466   return 0;
467 }
468
469 int MC_compare_snapshots(void *s1, void *s2){
470
471   return simcall_mc_compare_snapshots(s1, s2);
472
473 }