Logo AND Algorithmique Numérique Distribuée

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