Logo AND Algorithmique Numérique Distribuée

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