Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
0e038ca55482ea2c723458d00574422cee1dd294
[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 compare_stack(stack_region_t s1, stack_region_t s2, void *sp1, void *sp2, void *heap1, void *heap2, xbt_dynar_t equals);
16 static int is_heap_equality(xbt_dynar_t equals, void *a1, void *a2);
17 static size_t heap_ignore_size(void *address);
18
19 static void stack_region_free(stack_region_t s);
20 static void heap_equality_free(heap_equality_t e);
21
22 static int is_stack_ignore_variable(char *frame, char *var_name);
23 static int compare_local_variables(char *s1, char *s2, xbt_dynar_t heap_equals);
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
54   if(region_type == 1){ /* libsimgrid */
55     xbt_dynar_foreach(mc_global_variables, cursor, current_var){
56       if(current_var->address < start_data_libsimgrid)
57         continue;
58       offset = (char *)current_var->address - (char *)start_data_libsimgrid;
59       i = 0;
60       while(i < current_var->size){
61         if(memcmp((char*)d1 + offset + i, (char*)d2 + offset + i, 1) != 0){
62           pointer_align = (i / sizeof(void*)) * sizeof(void*); 
63           addr_pointed1 = *((void **)((char *)d1 + offset + pointer_align));
64           addr_pointed2 = *((void **)((char *)d2 + offset + pointer_align));
65           if((addr_pointed1 > start_plt_libsimgrid && addr_pointed1 < end_plt_libsimgrid) || (addr_pointed2 > start_plt_libsimgrid && addr_pointed2 < end_plt_libsimgrid)){
66             i = current_var->size;
67             continue;
68           }else{
69             if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_verbose)){
70               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);
71             }
72             return 1;
73           }
74         } 
75         i++;
76       }
77     }
78   }else{ /* binary */
79     xbt_dynar_foreach(mc_global_variables, cursor, current_var){
80       if(current_var->address > start_data_libsimgrid)
81         break;
82       offset = (char *)current_var->address - (char *)start_data_binary;
83       i = 0;
84       while(i < current_var->size){
85         if(memcmp((char*)d1 + offset + i, (char*)d2 + offset + i, 1) != 0){
86           pointer_align = (i / sizeof(void*)) * sizeof(void*); 
87           addr_pointed1 = *((void **)((char *)d1 + offset + pointer_align));
88           addr_pointed2 = *((void **)((char *)d2 + offset + pointer_align));
89           if((addr_pointed1 > start_plt_binary && addr_pointed1 < end_plt_binary) || (addr_pointed2 > start_plt_binary && addr_pointed2 < end_plt_binary)){
90             i = current_var->size;
91             continue;
92           }else{
93             if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_verbose)){
94               XBT_VERB("Different global variable in binary : %s", current_var->name);
95             }
96             return 1;
97           }
98         } 
99         i++;
100       }
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 SIMIX_pre_mc_compare_snapshots(smx_simcall_t simcall,
146                                    mc_snapshot_t s1, mc_snapshot_t s2){
147   return snapshot_compare(s1, s2, NULL, NULL);
148 }
149
150 int get_heap_region_index(mc_snapshot_t s){
151   int i = 0;
152   while(i < s->num_reg){
153     switch(s->regions[i]->type){
154     case 0:
155       return i;
156       break;
157     default:
158       i++;
159       break;
160     }
161   }
162   return -1;
163 }
164
165 int snapshot_compare(mc_snapshot_t s1, mc_snapshot_t s2, mc_comparison_times_t ct1, mc_comparison_times_t ct2){
166
167   int raw_mem = (mmalloc_get_current_heap() == raw_heap);
168   
169   MC_SET_RAW_MEM;
170       
171   int errors = 0, i = 0;
172
173   if(s1->num_reg != s2->num_reg){
174     if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_verbose)){
175       XBT_VERB("Different num_reg (s1 = %u, s2 = %u)", s1->num_reg, s2->num_reg);
176     }
177     if(!raw_mem)
178       MC_UNSET_RAW_MEM;
179     return 1;
180   }
181
182   int heap_index = 0, data_libsimgrid_index = 0, data_program_index = 0;
183
184   /* Get index of regions */
185   while(i < s1->num_reg){
186     switch(s1->regions[i]->type){
187     case 0:
188       heap_index = i;
189       i++;
190       break;
191     case 1:
192       data_libsimgrid_index = i;
193       i++;
194       while( i < s1->num_reg && s1->regions[i]->type == 1)
195         i++;
196       break;
197     case 2:
198       data_program_index = i;
199       i++;
200       while( i < s1->num_reg && s1->regions[i]->type == 2)
201         i++;
202       break;
203     }
204   }
205
206   if(ct1 != NULL)
207     ct1->nb_comparisons++;
208   if(ct2 != NULL)
209     ct2->nb_comparisons++;
210
211   xbt_os_timer_t global_timer = xbt_os_timer_new();
212   xbt_os_timer_t timer = xbt_os_timer_new();
213
214   xbt_os_timer_start(global_timer);
215
216   if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_debug))
217     xbt_os_timer_start(timer);
218
219   /* Compare number of blocks/fragments used in each heap */
220
221   if(s1->heap_chunks_used != s2->heap_chunks_used){
222
223     if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_debug)){
224       xbt_os_timer_stop(timer);
225       if(ct1 != NULL)
226         xbt_dynar_push_as(ct1->chunks_used_comparison_times, double, xbt_os_timer_elapsed(timer));
227       if(ct2 != NULL)
228         xbt_dynar_push_as(ct2->chunks_used_comparison_times, double, xbt_os_timer_elapsed(timer));
229       XBT_DEBUG("Different number of chunks used in each heap : %zu - %zu", s1->heap_chunks_used, s2->heap_chunks_used);
230       errors++;
231     }else{
232       if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_verbose))
233         XBT_VERB("Different number of chunks used in each heap : %zu - %zu", s1->heap_chunks_used, s2->heap_chunks_used);
234      
235       xbt_os_timer_free(timer);
236       xbt_os_timer_stop(global_timer);
237       if(ct1 != NULL)
238         xbt_dynar_push_as(ct1->snapshot_comparison_times, double, xbt_os_timer_elapsed(global_timer));
239       if(ct2 != NULL)
240         xbt_dynar_push_as(ct2->snapshot_comparison_times, double, xbt_os_timer_elapsed(global_timer));
241       xbt_os_timer_free(global_timer);
242
243       if(!raw_mem)
244         MC_UNSET_RAW_MEM;
245
246       return 1;
247     }
248   }else{
249
250     if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_debug))
251       xbt_os_timer_stop(timer);
252   }
253   
254   if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_debug))
255     xbt_os_timer_start(timer);
256   
257   /* Compare size of stacks */
258
259   unsigned int cursor = 0;
260   size_t size_used1, size_used2;
261   int is_diff = 0;
262   while(cursor < xbt_dynar_length(stacks_areas)){
263     size_used1 = ((mc_snapshot_stack_t)xbt_dynar_get_as(s1->stacks, cursor, mc_snapshot_stack_t))->size_used;
264     size_used2 = ((mc_snapshot_stack_t)xbt_dynar_get_as(s2->stacks, cursor, mc_snapshot_stack_t))->size_used;
265     if(size_used1 != size_used2){
266       if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_debug)){
267         if(is_diff == 0){
268           xbt_os_timer_stop(timer);
269           if(ct1 != NULL)
270             xbt_dynar_push_as(ct1->stacks_sizes_comparison_times, double, xbt_os_timer_elapsed(timer));
271           if(ct2 != NULL)
272             xbt_dynar_push_as(ct2->stacks_sizes_comparison_times, double, xbt_os_timer_elapsed(timer));
273         }
274         XBT_DEBUG("Different size used in stacks : %zu - %zu", size_used1, size_used2);
275         errors++;
276         is_diff = 1;
277       }else{
278         if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_verbose))
279           XBT_VERB("Different size used in stacks : %zu - %zu", size_used1, size_used2);
280  
281         xbt_os_timer_free(timer);
282         xbt_os_timer_stop(global_timer);
283         if(ct1 != NULL)
284           xbt_dynar_push_as(ct1->snapshot_comparison_times, double, xbt_os_timer_elapsed(global_timer));
285         if(ct2 != NULL)
286           xbt_dynar_push_as(ct2->snapshot_comparison_times, double, xbt_os_timer_elapsed(global_timer));
287         xbt_os_timer_free(global_timer);
288
289         if(!raw_mem)
290           MC_UNSET_RAW_MEM;
291
292         return 1;
293       }
294     }
295     cursor++;
296   }
297
298   if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_debug)){
299     if(is_diff == 0)
300       xbt_os_timer_stop(timer);
301     xbt_os_timer_start(timer);
302   }
303
304   /* Compare binary global variables */
305
306   is_diff = compare_global_variables(s1->regions[data_program_index]->type, s1->regions[data_program_index]->data, s2->regions[data_program_index]->data);
307   if(is_diff != 0){
308     if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_debug)){
309       xbt_os_timer_stop(timer);
310       if(ct1 != NULL)
311         xbt_dynar_push_as(ct1->binary_global_variables_comparison_times, double, xbt_os_timer_elapsed(timer));
312       if(ct2 != NULL)
313         xbt_dynar_push_as(ct2->binary_global_variables_comparison_times, double, xbt_os_timer_elapsed(timer));
314       XBT_DEBUG("Different global variables in binary"); 
315       errors++; 
316     }else{
317       if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_verbose))
318         XBT_VERB("Different global variables in binary"); 
319     
320       xbt_os_timer_free(timer);
321       xbt_os_timer_stop(global_timer);
322       if(ct1 != NULL)
323         xbt_dynar_push_as(ct1->snapshot_comparison_times, double, xbt_os_timer_elapsed(global_timer));
324       if(ct2 != NULL)
325         xbt_dynar_push_as(ct2->snapshot_comparison_times, double, xbt_os_timer_elapsed(global_timer));
326       xbt_os_timer_free(global_timer);
327     
328       if(!raw_mem)
329         MC_UNSET_RAW_MEM;
330
331       return 1;
332     }
333   }
334
335   if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_debug)){
336     if(is_diff == 0)
337       xbt_os_timer_stop(timer);
338     xbt_os_timer_start(timer);
339   } 
340
341   /* Compare libsimgrid global variables */
342
343   is_diff = compare_global_variables(s1->regions[data_libsimgrid_index]->type, s1->regions[data_libsimgrid_index]->data, s2->regions[data_libsimgrid_index]->data);
344   if(is_diff != 0){
345     if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_debug)){
346       xbt_os_timer_stop(timer);
347       if(ct1 != NULL)
348         xbt_dynar_push_as(ct1->libsimgrid_global_variables_comparison_times, double, xbt_os_timer_elapsed(timer));
349       if(ct2 != NULL)
350         xbt_dynar_push_as(ct2->libsimgrid_global_variables_comparison_times, double, xbt_os_timer_elapsed(timer));
351       XBT_DEBUG("Different global variables in libsimgrid"); 
352       errors++; 
353     }else{
354       if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_verbose))
355         XBT_VERB("Different global variables in libsimgrid"); 
356     
357       xbt_os_timer_free(timer);
358       xbt_os_timer_stop(global_timer);
359       if(ct1 != NULL)
360         xbt_dynar_push_as(ct1->snapshot_comparison_times, double, xbt_os_timer_elapsed(global_timer));
361       if(ct2 != NULL)
362         xbt_dynar_push_as(ct2->snapshot_comparison_times, double, xbt_os_timer_elapsed(global_timer));
363       xbt_os_timer_free(global_timer);
364     
365       if(!raw_mem)
366         MC_UNSET_RAW_MEM;
367
368       return 1;
369     }
370   }  
371
372   if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_debug)){
373     if(is_diff == 0)
374       xbt_os_timer_stop(timer);
375     xbt_os_timer_start(timer);
376   }
377
378   /* Compare heap */
379
380   xbt_dynar_t stacks1 = xbt_dynar_new(sizeof(stack_region_t), stack_region_free_voidp);
381   xbt_dynar_t stacks2 = xbt_dynar_new(sizeof(stack_region_t), stack_region_free_voidp);
382   xbt_dynar_t equals = xbt_dynar_new(sizeof(heap_equality_t), heap_equality_free_voidp);
383   
384   void *heap1 = s1->regions[heap_index]->data, *heap2 = s2->regions[heap_index]->data;
385  
386   if(mmalloc_compare_heap((xbt_mheap_t)s1->regions[heap_index]->data, (xbt_mheap_t)s2->regions[heap_index]->data, &stacks1, &stacks2, &equals)){
387
388     if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_debug)){
389       xbt_os_timer_stop(timer);
390       if(ct1 != NULL)
391         xbt_dynar_push_as(ct1->heap_comparison_times, double, xbt_os_timer_elapsed(timer));
392       if(ct2 != NULL)
393         xbt_dynar_push_as(ct2->heap_comparison_times, double, xbt_os_timer_elapsed(timer));
394       XBT_DEBUG("Different heap (mmalloc_compare)");
395       errors++;
396     }else{
397
398       xbt_os_timer_free(timer);
399       xbt_dynar_free(&stacks1);
400       xbt_dynar_free(&stacks2);
401       xbt_dynar_free(&equals);
402  
403       if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_verbose))
404         XBT_VERB("Different heap (mmalloc_compare)");
405        
406       xbt_os_timer_stop(global_timer);
407       if(ct1 != NULL)
408         xbt_dynar_push_as(ct1->snapshot_comparison_times, double, xbt_os_timer_elapsed(global_timer));
409       if(ct2 != NULL)
410         xbt_dynar_push_as(ct2->snapshot_comparison_times, double, xbt_os_timer_elapsed(global_timer));
411       xbt_os_timer_free(global_timer);
412
413       if(!raw_mem)
414         MC_UNSET_RAW_MEM;
415
416       return 1;
417     } 
418   }else{
419     if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_debug))
420       xbt_os_timer_stop(timer);
421   }
422
423   if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_debug))
424     xbt_os_timer_start(timer);
425
426   /* Stacks comparison */
427
428   cursor = 0;
429   stack_region_t stack_region1, stack_region2;
430   int diff = 0, diff_local = 0;
431   void *sp1, *sp2;
432   is_diff = 0;
433
434   while(cursor < xbt_dynar_length(stacks1)){
435     stack_region1 = (stack_region_t)(xbt_dynar_get_as(stacks1, cursor, stack_region_t));
436     stack_region2 = (stack_region_t)(xbt_dynar_get_as(stacks2, cursor, stack_region_t));
437     sp1 = ((mc_snapshot_stack_t)xbt_dynar_get_as(s1->stacks, cursor, mc_snapshot_stack_t))->stack_pointer;
438     sp2 = ((mc_snapshot_stack_t)xbt_dynar_get_as(s2->stacks, cursor, mc_snapshot_stack_t))->stack_pointer;
439     diff = compare_stack(stack_region1, stack_region2, sp1, sp2, heap1, heap2, equals);
440
441     if(diff > 0){ /* Differences may be due to padding */  
442       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);
443       if(diff_local > 0){
444         if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_debug)){
445           if(is_diff == 0){
446             xbt_os_timer_stop(timer);
447             if(ct1 != NULL)
448               xbt_dynar_push_as(ct1->stacks_comparison_times, double, xbt_os_timer_elapsed(timer));
449             if(ct2 != NULL)
450               xbt_dynar_push_as(ct2->stacks_comparison_times, double, xbt_os_timer_elapsed(timer));
451           }
452           XBT_DEBUG("Different local variables between stacks %d", cursor + 1);
453           errors++;
454           is_diff = 1;
455         }else{
456           xbt_dynar_free(&stacks1);
457           xbt_dynar_free(&stacks2);
458           xbt_dynar_free(&equals);
459
460           if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_verbose))
461             XBT_VERB("Different local variables between stacks %d", cursor + 1);
462           
463           xbt_os_timer_free(timer);
464           xbt_os_timer_stop(global_timer);
465           if(ct1 != NULL)
466             xbt_dynar_push_as(ct1->snapshot_comparison_times, double, xbt_os_timer_elapsed(global_timer));
467           if(ct2 != NULL)
468             xbt_dynar_push_as(ct2->snapshot_comparison_times, double, xbt_os_timer_elapsed(global_timer));
469           xbt_os_timer_free(global_timer);
470           
471           if(!raw_mem)
472             MC_UNSET_RAW_MEM;
473
474           return 1;
475         }
476       }
477     }
478     cursor++;
479   }
480
481   xbt_dynar_free(&stacks1);
482   xbt_dynar_free(&stacks2);
483   xbt_dynar_free(&equals);
484    
485   xbt_os_timer_free(timer);
486
487   if(!XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_debug)){
488     xbt_os_timer_stop(global_timer);
489     if(ct1 != NULL)
490       xbt_dynar_push_as(ct1->snapshot_comparison_times, double, xbt_os_timer_elapsed(global_timer));
491     if(ct2 != NULL)
492       xbt_dynar_push_as(ct2->snapshot_comparison_times, double, xbt_os_timer_elapsed(global_timer));
493   }
494   xbt_os_timer_free(global_timer);
495
496   if(!raw_mem)
497     MC_UNSET_RAW_MEM;
498
499   return errors > 0;
500   
501 }
502
503 static int is_stack_ignore_variable(char *frame, char *var_name){
504
505   unsigned int cursor = 0;
506   int start = 0;
507   int end = xbt_dynar_length(mc_stack_comparison_ignore) - 1;
508   mc_stack_ignore_variable_t current_var;
509
510   while(start <= end){
511     cursor = (start + end) / 2;
512     current_var = (mc_stack_ignore_variable_t)xbt_dynar_get_as(mc_stack_comparison_ignore, cursor, mc_stack_ignore_variable_t);
513     if(strcmp(current_var->frame, frame) == 0 || strcmp(current_var->frame, "*") == 0){
514       if(strcmp(current_var->var_name, var_name) == 0)
515         return 1;
516       if(strcmp(current_var->var_name, var_name) < 0)
517         start = cursor + 1;
518       if(strcmp(current_var->var_name, var_name) > 0)
519         end = cursor - 1;
520     }else if(strcmp(current_var->frame, frame) < 0){
521       start = cursor + 1;
522     }else if(strcmp(current_var->frame, frame) > 0){
523       end = cursor - 1; 
524     }
525   }
526
527   return 0;
528 }
529
530 static int compare_local_variables(char *s1, char *s2, xbt_dynar_t heap_equals){
531   
532   xbt_dynar_t tokens1 = xbt_str_split(s1, NULL);
533   xbt_dynar_t tokens2 = xbt_str_split(s2, NULL);
534
535   xbt_dynar_t s_tokens1, s_tokens2;
536   unsigned int cursor = 0;
537   void *addr1, *addr2;
538   char *ip1 = NULL, *ip2 = NULL;
539   
540   while(cursor < xbt_dynar_length(tokens1)){
541     s_tokens1 = xbt_str_split(xbt_dynar_get_as(tokens1, cursor, char *), "=");
542     s_tokens2 = xbt_str_split(xbt_dynar_get_as(tokens2, cursor, char *), "=");
543     if(xbt_dynar_length(s_tokens1) > 1 && xbt_dynar_length(s_tokens2) > 1){
544       if((strcmp(xbt_dynar_get_as(s_tokens1, 0, char *), "ip") == 0) && (strcmp(xbt_dynar_get_as(s_tokens2, 0, char *), "ip") == 0)){
545         xbt_free(ip1);
546         xbt_free(ip2);
547         ip1 = strdup(xbt_dynar_get_as(s_tokens1, 1, char *));
548         ip2 = strdup(xbt_dynar_get_as(s_tokens2, 1, char *));
549       }
550       if(strcmp(xbt_dynar_get_as(s_tokens1, 1, char *), xbt_dynar_get_as(s_tokens2, 1, char *)) != 0){   
551         /* Ignore this variable ?  */
552         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 *))){
553           xbt_dynar_free(&s_tokens1);
554           xbt_dynar_free(&s_tokens2);
555           cursor++;
556           continue;
557         }
558         addr1 = (void *) strtoul(xbt_dynar_get_as(s_tokens1, 1, char *), NULL, 16);
559         addr2 = (void *) strtoul(xbt_dynar_get_as(s_tokens2, 1, char *), NULL, 16);
560         if(is_heap_equality(heap_equals, addr1, addr2) == 0){
561           if(XBT_LOG_ISENABLED(mc_compare, xbt_log_priority_verbose))
562             XBT_VERB("Variable %s is different between stacks in %s : %s - %s", xbt_dynar_get_as(s_tokens1, 0, char *), ip1, xbt_dynar_get_as(s_tokens1, 1, char *), xbt_dynar_get_as(s_tokens2, 1, char *));
563           xbt_dynar_free(&s_tokens1);
564           xbt_dynar_free(&s_tokens2);
565           xbt_dynar_free(&tokens1);
566           xbt_dynar_free(&tokens2);
567           xbt_free(ip1);
568           xbt_free(ip2);
569           return 1;
570         }
571       }
572     }
573     xbt_dynar_free(&s_tokens1);
574     xbt_dynar_free(&s_tokens2);
575          
576     cursor++;
577   }
578
579   xbt_free(ip1);
580   xbt_free(ip2);
581   xbt_dynar_free(&tokens1);
582   xbt_dynar_free(&tokens2);
583   return 0;
584   
585 }
586
587 static int is_heap_equality(xbt_dynar_t equals, void *a1, void *a2){
588
589   unsigned int cursor = 0;
590   int start = 0;
591   int end = xbt_dynar_length(equals) - 1;
592   heap_equality_t current_equality;
593
594   while(start <= end){
595     cursor = (start + end) / 2;
596     current_equality = (heap_equality_t)xbt_dynar_get_as(equals, cursor, heap_equality_t);
597     if(current_equality->address1 == a1){
598       if(current_equality->address2 == a2)
599         return 1;
600       if(current_equality->address2 < a2)
601         start = cursor + 1;
602       if(current_equality->address2 > a2)
603         end = cursor - 1;
604     }
605     if(current_equality->address1 < a1)
606       start = cursor + 1;
607     if(current_equality->address1 > a1)
608       end = cursor - 1; 
609   }
610
611   return 0;
612
613 }
614
615
616 static int compare_stack(stack_region_t s1, stack_region_t s2, void *sp1, void *sp2, void *heap1, void *heap2, xbt_dynar_t equals){
617   
618   size_t k = 0;
619   size_t size_used1 = s1->size - ((char*)sp1 - (char*)s1->address);
620   size_t size_used2 = s2->size - ((char*)sp2 - (char*)s2->address);
621
622   int pointer_align;
623   void *addr_pointed1 = NULL, *addr_pointed2 = NULL;  
624   
625   while(k < size_used1){
626     if(memcmp((char *)s1->address + s1->size - k, (char *)s2->address + s2->size - k, 1) != 0){
627       pointer_align = ((size_used1 - k) / sizeof(void*)) * sizeof(void*);
628       addr_pointed1 = *((void **)(((char*)s1->address + (s1->size - size_used1)) + pointer_align));
629       addr_pointed2 = *((void **)(((char*)s2->address + (s2->size - size_used2)) + pointer_align));
630       if(is_heap_equality(equals, addr_pointed1, addr_pointed2) == 0){
631         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))){
632           if(is_free_area(addr_pointed1, (xbt_mheap_t)heap1) == 0 || is_free_area(addr_pointed2, (xbt_mheap_t)heap2) == 0){
633             return 1;
634           }
635         }else{
636           return 1;
637         } 
638       }
639     } 
640     k++;
641   }
642  
643   return 0;
644 }
645
646 int MC_compare_snapshots(void *s1, void *s2){
647   
648   MC_ignore_stack("self", "simcall_BODY_mc_snapshot");
649
650   return simcall_mc_compare_snapshots(s1, s2);
651
652 }