Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b8fc44c234c7fa0bf901707d79b6123324ab81a0
[simgrid.git] / src / mc / mc_diff.cpp
1 /* mc_diff - Memory snapshooting and comparison                             */
2
3 /* Copyright (c) 2008-2014. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "xbt/ex_interface.h"   /* internals of backtrace setup */
10 #include "xbt/str.h"
11 #include "mc/mc.h"
12 #include "xbt/mmalloc.h"
13 #include "mc_object_info.h"
14 #include "mc/datatypes.h"
15 #include "mc/mc_private.h"
16 #include "mc/mc_snapshot.h"
17
18 extern "C" {
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_diff, xbt,
21                                 "Logging specific to mc_diff in mc");
22
23 xbt_dynar_t mc_heap_comparison_ignore;
24 xbt_dynar_t stacks_areas;
25
26
27
28 /********************************* Backtrace ***********************************/
29 /******************************************************************************/
30
31 static void mmalloc_backtrace_block_display(void *heapinfo, int block)
32 {
33
34   /* xbt_ex_t e; */
35
36   /* if (((malloc_info *)heapinfo)[block].busy_block.bt_size == 0) { */
37   /*   fprintf(stderr, "No backtrace available for that block, sorry.\n"); */
38   /*   return; */
39   /* } */
40
41   /* memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_block.bt),sizeof(void*)*XBT_BACKTRACE_SIZE); */
42   /* e.used = ((malloc_info *)heapinfo)[block].busy_block.bt_size; */
43
44   /* xbt_ex_setup_backtrace(&e); */
45   /* if (e.used == 0) { */
46   /*   fprintf(stderr, "(backtrace not set)\n"); */
47   /* } else if (e.bt_strings == NULL) { */
48   /*   fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet"); */
49   /* } else { */
50   /*   int i; */
51
52   /*   fprintf(stderr, "Backtrace of where the block %d was malloced (%d frames):\n", block ,e.used); */
53   /*   for (i = 0; i < e.used; i++)       /\* no need to display "xbt_backtrace_display" *\/{ */
54   /*     fprintf(stderr, "%d ---> %s\n",i, e.bt_strings[i] + 4); */
55   /*   } */
56   /* } */
57 }
58
59 static void mmalloc_backtrace_fragment_display(void *heapinfo, int block,
60                                                int frag)
61 {
62
63   /* xbt_ex_t e; */
64
65   /* memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_frag.bt[frag]),sizeof(void*)*XBT_BACKTRACE_SIZE); */
66   /* e.used = XBT_BACKTRACE_SIZE; */
67
68   /* xbt_ex_setup_backtrace(&e); */
69   /* if (e.used == 0) { */
70   /*   fprintf(stderr, "(backtrace not set)\n"); */
71   /* } else if (e.bt_strings == NULL) { */
72   /*   fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet"); */
73   /* } else { */
74   /*   int i; */
75
76   /*   fprintf(stderr, "Backtrace of where the fragment %d in block %d was malloced (%d frames):\n", frag, block ,e.used); */
77   /*   for (i = 0; i < e.used; i++)       /\* no need to display "xbt_backtrace_display" *\/{ */
78   /*     fprintf(stderr, "%d ---> %s\n",i, e.bt_strings[i] + 4); */
79   /*   } */
80   /* } */
81
82 }
83
84 static void mmalloc_backtrace_display(void *addr)
85 {
86
87   /* size_t block, frag_nb; */
88   /* int type; */
89
90   /* xbt_mheap_t heap = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit(); */
91
92   /* block = (((char*) (addr) - (char*) heap -> heapbase) / BLOCKSIZE + 1); */
93
94   /* type = heap->heapinfo[block].type; */
95
96   /* switch(type){ */
97   /* case MMALLOC_TYPE_HEAPINFO :  */
98   /* case MMALLOC_TYPE_FREE : /\* Free block *\/ */
99   /*   fprintf(stderr, "Asked to display the backtrace of a block that is free. I'm puzzled\n"); */
100   /*   xbt_abort(); */
101   /*   break;  */
102   /* case 0: /\* Large block *\/ */
103   /*   mmalloc_backtrace_block_display(heap->heapinfo, block); */
104   /*   break; */
105   /* default: /\* Fragmented block *\/ */
106   /*   frag_nb = RESIDUAL(addr, BLOCKSIZE) >> type; */
107   /*   if(heap->heapinfo[block].busy_frag.frag_size[frag_nb] == -1){ */
108   /*     fprintf(stderr , "Asked to display the backtrace of a fragment that is free. I'm puzzled\n"); */
109   /*     xbt_abort(); */
110   /*   } */
111   /*   mmalloc_backtrace_fragment_display(heap->heapinfo, block, frag_nb); */
112   /*   break; */
113   /* } */
114 }
115
116
117 static int compare_backtrace(int b1, int f1, int b2, int f2)
118 {
119   /*int i = 0;
120      if(f1 != -1){
121      for(i=0; i< XBT_BACKTRACE_SIZE; i++){
122      if(heapinfo1[b1].busy_frag.bt[f1][i] != heapinfo2[b2].busy_frag.bt[f2][i]){
123      //mmalloc_backtrace_fragment_display((void*)heapinfo1, b1, f1);
124      //mmalloc_backtrace_fragment_display((void*)heapinfo2, b2, f2);
125      return 1;
126      }
127      }
128      }else{
129      for(i=0; i< heapinfo1[b1].busy_block.bt_size; i++){
130      if(heapinfo1[b1].busy_block.bt[i] != heapinfo2[b2].busy_block.bt[i]){
131      //mmalloc_backtrace_block_display((void*)heapinfo1, b1);
132      //mmalloc_backtrace_block_display((void*)heapinfo2, b2);
133      return 1;
134      }
135      }
136      } */
137   return 0;
138 }
139
140
141 /*********************************** Heap comparison ***********************************/
142 /***************************************************************************************/
143
144 typedef char *type_name;
145
146 struct s_mc_diff {
147   s_xbt_mheap_t std_heap_copy;
148   size_t heaplimit;
149   // Number of blocks in the heaps:
150   size_t heapsize1, heapsize2;
151   xbt_dynar_t to_ignore1, to_ignore2;
152   s_heap_area_t *equals_to1, *equals_to2;
153   dw_type_t *types1, *types2;
154   size_t available;
155 };
156
157 #define equals_to1_(i,j) equals_to1[ MAX_FRAGMENT_PER_BLOCK*(i) + (j)]
158 #define equals_to2_(i,j) equals_to2[ MAX_FRAGMENT_PER_BLOCK*(i) + (j)]
159 #define types1_(i,j) types1[ MAX_FRAGMENT_PER_BLOCK*(i) + (j)]
160 #define types2_(i,j) types2[ MAX_FRAGMENT_PER_BLOCK*(i) + (j)]
161
162 __thread struct s_mc_diff *mc_diff_info = NULL;
163
164 /*********************************** Free functions ************************************/
165
166 static void heap_area_pair_free(heap_area_pair_t pair)
167 {
168   xbt_free(pair);
169   pair = NULL;
170 }
171
172 static void heap_area_pair_free_voidp(void *d)
173 {
174   heap_area_pair_free((heap_area_pair_t) * (void **) d);
175 }
176
177 static void heap_area_free(heap_area_t area)
178 {
179   xbt_free(area);
180   area = NULL;
181 }
182
183 /************************************************************************************/
184
185 static s_heap_area_t make_heap_area(int block, int fragment)
186 {
187   s_heap_area_t area;
188   area.valid = 1;
189   area.block = block;
190   area.fragment = fragment;
191   return area;
192 }
193
194
195 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1,
196                                  int block2, int fragment2)
197 {
198
199   unsigned int cursor = 0;
200   heap_area_pair_t current_pair;
201
202   xbt_dynar_foreach(list, cursor, current_pair) {
203     if (current_pair->block1 == block1 && current_pair->block2 == block2
204         && current_pair->fragment1 == fragment1
205         && current_pair->fragment2 == fragment2)
206       return 0;
207   }
208
209   return 1;
210 }
211
212 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1,
213                               int block2, int fragment2)
214 {
215
216   if (is_new_heap_area_pair(list, block1, fragment1, block2, fragment2)) {
217     heap_area_pair_t pair = NULL;
218     pair = xbt_new0(s_heap_area_pair_t, 1);
219     pair->block1 = block1;
220     pair->fragment1 = fragment1;
221     pair->block2 = block2;
222     pair->fragment2 = fragment2;
223
224     xbt_dynar_push(list, &pair);
225
226     return 1;
227   }
228
229   return 0;
230 }
231
232 static ssize_t heap_comparison_ignore_size(xbt_dynar_t ignore_list,
233                                            const void *address)
234 {
235
236   unsigned int cursor = 0;
237   int start = 0;
238   int end = xbt_dynar_length(ignore_list) - 1;
239   mc_heap_ignore_region_t region;
240
241   while (start <= end) {
242     cursor = (start + end) / 2;
243     region =
244         (mc_heap_ignore_region_t) xbt_dynar_get_as(ignore_list, cursor,
245                                                    mc_heap_ignore_region_t);
246     if (region->address == address)
247       return region->size;
248     if (region->address < address)
249       start = cursor + 1;
250     if (region->address > address)
251       end = cursor - 1;
252   }
253
254   return -1;
255 }
256
257 static int is_stack(const void *address)
258 {
259   unsigned int cursor = 0;
260   stack_region_t stack;
261
262   xbt_dynar_foreach(stacks_areas, cursor, stack) {
263     if (address == stack->address)
264       return 1;
265   }
266
267   return 0;
268 }
269
270 // TODO, this should depend on the snapshot?
271 static int is_block_stack(int block)
272 {
273   unsigned int cursor = 0;
274   stack_region_t stack;
275
276   xbt_dynar_foreach(stacks_areas, cursor, stack) {
277     if (block == stack->block)
278       return 1;
279   }
280
281   return 0;
282 }
283
284 static void match_equals(struct s_mc_diff *state, xbt_dynar_t list)
285 {
286
287   unsigned int cursor = 0;
288   heap_area_pair_t current_pair;
289
290   xbt_dynar_foreach(list, cursor, current_pair) {
291
292     if (current_pair->fragment1 != -1) {
293
294       state->equals_to1_(current_pair->block1, current_pair->fragment1) =
295           make_heap_area(current_pair->block2, current_pair->fragment2);
296       state->equals_to2_(current_pair->block2, current_pair->fragment2) =
297           make_heap_area(current_pair->block1, current_pair->fragment1);
298
299     } else {
300
301       state->equals_to1_(current_pair->block1, 0) =
302           make_heap_area(current_pair->block2, current_pair->fragment2);
303       state->equals_to2_(current_pair->block2, 0) =
304           make_heap_area(current_pair->block1, current_pair->fragment1);
305
306     }
307
308   }
309 }
310
311 /** Check whether two blocks are known to be matching
312  *
313  *  @param state  State used
314  *  @param b1     Block of state 1
315  *  @param b2     Block of state 2
316  *  @return       if the blocks are known to be matching
317  */
318 static int equal_blocks(struct s_mc_diff *state, int b1, int b2)
319 {
320
321   if (state->equals_to1_(b1, 0).block == b2
322       && state->equals_to2_(b2, 0).block == b1)
323     return 1;
324
325   return 0;
326 }
327
328 /** Check whether two fragments are known to be matching
329  *
330  *  @param state  State used
331  *  @param b1     Block of state 1
332  *  @param f1     Fragment of state 1
333  *  @param b2     Block of state 2
334  *  @param f2     Fragment of state 2
335  *  @return       if the fragments are known to be matching
336  */
337 static int equal_fragments(struct s_mc_diff *state, int b1, int f1, int b2,
338                            int f2)
339 {
340
341   if (state->equals_to1_(b1, f1).block == b2
342       && state->equals_to1_(b1, f1).fragment == f2
343       && state->equals_to2_(b2, f2).block == b1
344       && state->equals_to2_(b2, f2).fragment == f1)
345     return 1;
346
347   return 0;
348 }
349
350 int init_heap_information(xbt_mheap_t heap1, xbt_mheap_t heap2, xbt_dynar_t i1,
351                           xbt_dynar_t i2)
352 {
353   if (mc_diff_info == NULL) {
354     mc_diff_info = xbt_new0(struct s_mc_diff, 1);
355     mc_diff_info->equals_to1 = NULL;
356     mc_diff_info->equals_to2 = NULL;
357     mc_diff_info->types1 = NULL;
358     mc_diff_info->types2 = NULL;
359   }
360   struct s_mc_diff *state = mc_diff_info;
361
362   if ((((struct mdesc *) heap1)->heaplimit !=
363        ((struct mdesc *) heap2)->heaplimit)
364       ||
365       ((((struct mdesc *) heap1)->heapsize !=
366         ((struct mdesc *) heap2)->heapsize)))
367     return -1;
368
369   state->heaplimit = ((struct mdesc *) heap1)->heaplimit;
370   
371   state->std_heap_copy = *MC_process_get_heap(&mc_model_checker->process());
372
373   state->heapsize1 = heap1->heapsize;
374   state->heapsize2 = heap2->heapsize;
375
376   state->to_ignore1 = i1;
377   state->to_ignore2 = i2;
378
379   if (state->heaplimit > state->available) {
380     state->equals_to1 = (s_heap_area_t*)
381         realloc(state->equals_to1,
382                 state->heaplimit * MAX_FRAGMENT_PER_BLOCK *
383                 sizeof(s_heap_area_t));
384     state->types1 = (s_dw_type**)
385         realloc(state->types1,
386                 state->heaplimit * MAX_FRAGMENT_PER_BLOCK *
387                 sizeof(type_name *));
388     state->equals_to2 = (s_heap_area_t*)
389         realloc(state->equals_to2,
390                 state->heaplimit * MAX_FRAGMENT_PER_BLOCK *
391                 sizeof(s_heap_area_t));
392     state->types2 = (s_dw_type**)
393         realloc(state->types2,
394                 state->heaplimit * MAX_FRAGMENT_PER_BLOCK *
395                 sizeof(type_name *));
396     state->available = state->heaplimit;
397   }
398
399   memset(state->equals_to1, 0,
400          state->heaplimit * MAX_FRAGMENT_PER_BLOCK * sizeof(s_heap_area_t));
401   memset(state->equals_to2, 0,
402          state->heaplimit * MAX_FRAGMENT_PER_BLOCK * sizeof(s_heap_area_t));
403   memset(state->types1, 0,
404          state->heaplimit * MAX_FRAGMENT_PER_BLOCK * sizeof(type_name *));
405   memset(state->types2, 0,
406          state->heaplimit * MAX_FRAGMENT_PER_BLOCK * sizeof(type_name *));
407
408   return 0;
409
410 }
411
412 void reset_heap_information()
413 {
414
415 }
416
417 // TODO, have a robust way to find it in O(1)
418 static inline
419 mc_mem_region_t MC_get_heap_region(mc_snapshot_t snapshot)
420 {
421   size_t n = snapshot->snapshot_regions_count;
422   for (size_t i=0; i!=n; ++i) {
423     mc_mem_region_t region = snapshot->snapshot_regions[i];
424     if (region->region_type == MC_REGION_TYPE_HEAP)
425       return region;
426   }
427   xbt_die("No heap region");
428 }
429
430 int mmalloc_compare_heap(mc_snapshot_t snapshot1, mc_snapshot_t snapshot2)
431 {
432   mc_process_t process = &mc_model_checker->process();
433   struct s_mc_diff *state = mc_diff_info;
434
435   /* Start comparison */
436   size_t i1, i2, j1, j2, k;
437   void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
438   int nb_diff1 = 0, nb_diff2 = 0;
439
440   int equal, res_compare = 0;
441
442   /* Check busy blocks */
443
444   i1 = 1;
445
446   malloc_info heapinfo_temp1, heapinfo_temp2;
447   malloc_info heapinfo_temp2b;
448
449   mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
450   mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
451
452   // This is the address of std_heap->heapinfo in the application process:
453   void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
454
455   // This is in snapshot do not use them directly:
456   const malloc_info* heapinfos1 = (const malloc_info*) MC_snapshot_read_pointer(snapshot1, heapinfo_address, MC_PROCESS_INDEX_MISSING);
457   const malloc_info* heapinfos2 = (const malloc_info*) MC_snapshot_read_pointer(snapshot2, heapinfo_address, MC_PROCESS_INDEX_MISSING);
458
459   while (i1 <= state->heaplimit) {
460
461     const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(heap_region1, &heapinfo_temp1, &heapinfos1[i1], sizeof(malloc_info));
462     const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2, &heapinfos2[i1], sizeof(malloc_info));
463
464     if (heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type == MMALLOC_TYPE_HEAPINFO) {      /* Free block */
465       i1 ++;
466       continue;
467     }
468
469     if (heapinfo1->type < 0) {
470       fprintf(stderr, "Unkown mmalloc block type.\n");
471       abort();
472     }
473
474     addr_block1 =
475         ((void *) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE +
476                    (char *) state->std_heap_copy.heapbase));
477
478     if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED) {       /* Large block */
479
480       if (is_stack(addr_block1)) {
481         for (k = 0; k < heapinfo1->busy_block.size; k++)
482           state->equals_to1_(i1 + k, 0) = make_heap_area(i1, -1);
483         for (k = 0; k < heapinfo2->busy_block.size; k++)
484           state->equals_to2_(i1 + k, 0) = make_heap_area(i1, -1);
485         i1 += heapinfo1->busy_block.size;
486         continue;
487       }
488
489       if (state->equals_to1_(i1, 0).valid) {
490         i1++;
491         continue;
492       }
493
494       i2 = 1;
495       equal = 0;
496       res_compare = 0;
497
498       /* Try first to associate to same block in the other heap */
499       if (heapinfo2->type == heapinfo1->type) {
500
501         if (state->equals_to2_(i1, 0).valid == 0) {
502
503           addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE +
504                          (char *) state->std_heap_copy.heapbase;
505
506           res_compare =
507               compare_heap_area(MC_PROCESS_INDEX_MISSING, addr_block1, addr_block2, snapshot1, snapshot2,
508                                 NULL, NULL, 0);
509
510           if (res_compare != 1) {
511             for (k = 1; k < heapinfo2->busy_block.size; k++)
512               state->equals_to2_(i1 + k, 0) = make_heap_area(i1, -1);
513             for (k = 1; k < heapinfo1->busy_block.size; k++)
514               state->equals_to1_(i1 + k, 0) = make_heap_area(i1, -1);
515             equal = 1;
516             i1 += heapinfo1->busy_block.size;
517           }
518
519         }
520
521       }
522
523       while (i2 <= state->heaplimit && !equal) {
524
525         addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE +
526                        (char *) state->std_heap_copy.heapbase;
527
528         if (i2 == i1) {
529           i2++;
530           continue;
531         }
532
533         const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2b, &heapinfos2[i2], sizeof(malloc_info));
534
535         if (heapinfo2b->type != MMALLOC_TYPE_UNFRAGMENTED) {
536           i2++;
537           continue;
538         }
539
540         if (state->equals_to2_(i2, 0).valid) {
541           i2++;
542           continue;
543         }
544
545         res_compare =
546             compare_heap_area(MC_PROCESS_INDEX_MISSING, addr_block1, addr_block2, snapshot1, snapshot2,
547                               NULL, NULL, 0);
548
549         if (res_compare != 1) {
550           for (k = 1; k < heapinfo2b->busy_block.size; k++)
551             state->equals_to2_(i2 + k, 0) = make_heap_area(i1, -1);
552           for (k = 1; k < heapinfo1->busy_block.size; k++)
553             state->equals_to1_(i1 + k, 0) = make_heap_area(i2, -1);
554           equal = 1;
555           i1 += heapinfo1->busy_block.size;
556         }
557
558         i2++;
559
560       }
561
562       if (!equal) {
563         XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1,
564                   heapinfo1->busy_block.busy_size, addr_block1);
565         i1 = state->heaplimit + 1;
566         nb_diff1++;
567         //i1++;
568       }
569
570     } else {                    /* Fragmented block */
571
572       for (j1 = 0; j1 < (size_t) (BLOCKSIZE >> heapinfo1->type); j1++) {
573
574         if (heapinfo1->busy_frag.frag_size[j1] == -1) /* Free fragment */
575           continue;
576
577         if (state->equals_to1_(i1, j1).valid)
578           continue;
579
580         addr_frag1 =
581             (void *) ((char *) addr_block1 + (j1 << heapinfo1->type));
582
583         i2 = 1;
584         equal = 0;
585
586         /* Try first to associate to same fragment in the other heap */
587         if (heapinfo2->type == heapinfo1->type) {
588
589           if (state->equals_to2_(i1, j1).valid == 0) {
590
591             addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE +
592                            (char *) state->std_heap_copy.heapbase;
593             addr_frag2 =
594                 (void *) ((char *) addr_block2 +
595                           (j1 << heapinfo2->type));
596
597             res_compare =
598                 compare_heap_area(MC_PROCESS_INDEX_MISSING, addr_frag1, addr_frag2, snapshot1, snapshot2,
599                                   NULL, NULL, 0);
600
601             if (res_compare != 1)
602               equal = 1;
603
604           }
605
606         }
607
608         while (i2 <= state->heaplimit && !equal) {
609
610           const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(
611             heap_region2, &heapinfo_temp2b, &heapinfos2[i2],
612             sizeof(malloc_info));
613
614           if (heapinfo2b->type == MMALLOC_TYPE_FREE || heapinfo2b->type == MMALLOC_TYPE_HEAPINFO) {
615             i2 ++;
616             continue;
617           }
618
619           // We currently do not match fragments with unfragmented blocks (maybe we should).
620           if (heapinfo2b->type == MMALLOC_TYPE_UNFRAGMENTED) {
621             i2++;
622             continue;
623           }
624
625           if (heapinfo2b->type < 0) {
626             fprintf(stderr, "Unkown mmalloc block type.\n");
627             abort();
628           }
629
630           for (j2 = 0; j2 < (size_t) (BLOCKSIZE >> heapinfo2b->type);
631                j2++) {
632
633             if (i2 == i1 && j2 == j1)
634               continue;
635
636             if (state->equals_to2_(i2, j2).valid)
637               continue;
638
639             addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE +
640                            (char *) state->std_heap_copy.heapbase;
641             addr_frag2 =
642                 (void *) ((char *) addr_block2 +
643                           (j2 << heapinfo2b->type));
644
645             res_compare =
646                 compare_heap_area(MC_PROCESS_INDEX_MISSING, addr_frag1, addr_frag2, snapshot2, snapshot2,
647                                   NULL, NULL, 0);
648
649             if (res_compare != 1) {
650               equal = 1;
651               break;
652             }
653
654           }
655
656           i2++;
657
658         }
659
660         if (!equal) {
661           XBT_DEBUG
662               ("Block %zu, fragment %zu not found (size_used = %zd, address = %p)\n",
663                i1, j1, heapinfo1->busy_frag.frag_size[j1],
664                addr_frag1);
665           i2 = state->heaplimit + 1;
666           i1 = state->heaplimit + 1;
667           nb_diff1++;
668           break;
669         }
670
671       }
672
673       i1++;
674
675     }
676
677   }
678
679   /* All blocks/fragments are equal to another block/fragment ? */
680   size_t i = 1, j = 0;
681
682   for(i = 1; i <= state->heaplimit; i++) {
683     const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
684       heap_region1, &heapinfo_temp1, &heapinfos1[i], sizeof(malloc_info));
685     if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED) {
686       if (i1 == state->heaplimit) {
687         if (heapinfo1->busy_block.busy_size > 0) {
688           if (state->equals_to1_(i, 0).valid == 0) {
689             if (XBT_LOG_ISENABLED(mc_diff, xbt_log_priority_debug)) {
690               // TODO, add address
691               XBT_DEBUG("Block %zu not found (size used = %zu)", i,
692                         heapinfo1->busy_block.busy_size);
693               //mmalloc_backtrace_block_display((void*)heapinfo1, i);
694             }
695             nb_diff1++;
696           }
697         }
698       }
699     }
700     if (heapinfo1->type > 0) {
701       for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo1->type); j++) {
702         if (i1 == state->heaplimit) {
703           if (heapinfo1->busy_frag.frag_size[j] > 0) {
704             if (state->equals_to1_(i, j).valid == 0) {
705               if (XBT_LOG_ISENABLED(mc_diff, xbt_log_priority_debug)) {
706                 // TODO, print fragment address
707                 XBT_DEBUG
708                     ("Block %zu, Fragment %zu not found (size used = %zd)",
709                      i, j,
710                      heapinfo1->busy_frag.frag_size[j]);
711                 //mmalloc_backtrace_fragment_display((void*)heapinfo1, i, j);
712               }
713               nb_diff1++;
714             }
715           }
716         }
717       }
718     }
719   }
720
721   if (i1 == state->heaplimit)
722     XBT_DEBUG("Number of blocks/fragments not found in heap1 : %d", nb_diff1);
723
724   for (i=1; i <= state->heaplimit; i++) {
725     const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
726       heap_region2, &heapinfo_temp2, &heapinfos2[i], sizeof(malloc_info));
727     if (heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
728       if (i1 == state->heaplimit) {
729         if (heapinfo2->busy_block.busy_size > 0) {
730           if (state->equals_to2_(i, 0).valid == 0) {
731             if (XBT_LOG_ISENABLED(mc_diff, xbt_log_priority_debug)) {
732               // TODO, print address of the block
733               XBT_DEBUG("Block %zu not found (size used = %zu)", i,
734                         heapinfo2->busy_block.busy_size);
735               //mmalloc_backtrace_block_display((void*)heapinfo2, i);
736             }
737             nb_diff2++;
738           }
739         }
740       }
741     }
742     if (heapinfo2->type > 0) {
743       for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo2->type); j++) {
744         if (i1 == state->heaplimit) {
745           if (heapinfo2->busy_frag.frag_size[j] > 0) {
746             if (state->equals_to2_(i, j).valid == 0) {
747               if (XBT_LOG_ISENABLED(mc_diff, xbt_log_priority_debug)) {
748                 // TODO, print address of the block
749                 XBT_DEBUG
750                     ("Block %zu, Fragment %zu not found (size used = %zd)",
751                      i, j,
752                      heapinfo2->busy_frag.frag_size[j]);
753                 //mmalloc_backtrace_fragment_display((void*)heapinfo2, i, j);
754               }
755               nb_diff2++;
756             }
757           }
758         }
759       }
760     }
761   }
762
763   if (i1 == state->heaplimit)
764     XBT_DEBUG("Number of blocks/fragments not found in heap2 : %d", nb_diff2);
765
766   return ((nb_diff1 > 0) || (nb_diff2 > 0));
767 }
768
769 /**
770  *
771  * @param state
772  * @param real_area1     Process address for state 1
773  * @param real_area2     Process address for state 2
774  * @param snapshot1      Snapshot of state 1
775  * @param snapshot2      Snapshot of state 2
776  * @param previous
777  * @param size
778  * @param check_ignore
779  */
780 static int compare_heap_area_without_type(struct s_mc_diff *state, int process_index,
781                                           const void *real_area1, const void *real_area2,
782                                           mc_snapshot_t snapshot1,
783                                           mc_snapshot_t snapshot2,
784                                           xbt_dynar_t previous, int size,
785                                           int check_ignore)
786 {
787   mc_process_t process = &mc_model_checker->process();
788
789   int i = 0;
790   const void *addr_pointed1, *addr_pointed2;
791   int pointer_align, res_compare;
792   ssize_t ignore1, ignore2;
793
794   mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
795   mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
796
797   while (i < size) {
798
799     if (check_ignore > 0) {
800       if ((ignore1 =
801            heap_comparison_ignore_size(state->to_ignore1,
802                                        (char *) real_area1 + i)) != -1) {
803         if ((ignore2 =
804              heap_comparison_ignore_size(state->to_ignore2,
805                                          (char *) real_area2 + i)) == ignore1) {
806           if (ignore1 == 0) {
807             check_ignore--;
808             return 0;
809           } else {
810             i = i + ignore2;
811             check_ignore--;
812             continue;
813           }
814         }
815       }
816     }
817
818     if (MC_snapshot_region_memcmp(((char *) real_area1) + i, heap_region1, ((char *) real_area2) + i, heap_region2, 1) != 0) {
819
820       pointer_align = (i / sizeof(void *)) * sizeof(void *);
821       addr_pointed1 = MC_snapshot_read_pointer(snapshot1, (char *) real_area1 + pointer_align, process_index);
822       addr_pointed2 = MC_snapshot_read_pointer(snapshot2, (char *) real_area2 + pointer_align, process_index);
823
824       if (addr_pointed1 > process->maestro_stack_start
825           && addr_pointed1 < process->maestro_stack_end
826           && addr_pointed2 > process->maestro_stack_start
827           && addr_pointed2 < process->maestro_stack_end) {
828         i = pointer_align + sizeof(void *);
829         continue;
830       } else if (addr_pointed1 > state->std_heap_copy.heapbase
831                  && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
832                  && addr_pointed2 > state->std_heap_copy.heapbase
833                  && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)) {
834         // Both addreses are in the heap:
835         res_compare =
836             compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
837                               snapshot2, previous, NULL, 0);
838         if (res_compare == 1) {
839           return res_compare;
840         }
841         i = pointer_align + sizeof(void *);
842         continue;
843       } else {
844         return 1;
845       }
846
847     }
848
849     i++;
850
851   }
852
853   return 0;
854
855 }
856
857 /**
858  *
859  * @param state
860  * @param real_area1     Process address for state 1
861  * @param real_area2     Process address for state 2
862  * @param snapshot1      Snapshot of state 1
863  * @param snapshot2      Snapshot of state 2
864  * @param previous
865  * @param type_id
866  * @param area_size      either a byte_size or an elements_count (?)
867  * @param check_ignore
868  * @param pointer_level
869  * @return               0 (same), 1 (different), -1 (unknown)
870  */
871 static int compare_heap_area_with_type(struct s_mc_diff *state, int process_index,
872                                        const void *real_area1, const void *real_area2,
873                                        mc_snapshot_t snapshot1,
874                                        mc_snapshot_t snapshot2,
875                                        xbt_dynar_t previous, dw_type_t type,
876                                        int area_size, int check_ignore,
877                                        int pointer_level)
878 {
879 top:
880   if (is_stack(real_area1) && is_stack(real_area2))
881     return 0;
882
883   ssize_t ignore1, ignore2;
884
885   if ((check_ignore > 0)
886       && ((ignore1 = heap_comparison_ignore_size(state->to_ignore1, real_area1))
887           > 0)
888       && ((ignore2 = heap_comparison_ignore_size(state->to_ignore2, real_area2))
889           == ignore1)) {
890     return 0;
891   }
892
893   dw_type_t subtype, subsubtype;
894   int res, elm_size;
895   unsigned int cursor = 0;
896   dw_type_t member;
897   const void *addr_pointed1, *addr_pointed2;;
898
899   mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
900   mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
901
902   switch (type->type) {
903   case DW_TAG_unspecified_type:
904     return 1;
905
906   case DW_TAG_base_type:
907     if (type->name != NULL && strcmp(type->name, "char") == 0) {        /* String, hence random (arbitrary ?) size */
908       if (real_area1 == real_area2)
909         return -1;
910       else
911         return (MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, area_size) != 0);
912     } else {
913       if (area_size != -1 && type->byte_size != area_size)
914         return -1;
915       else {
916         return (MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0);
917       }
918     }
919     break;
920   case DW_TAG_enumeration_type:
921     if (area_size != -1 && type->byte_size != area_size)
922       return -1;
923     else
924       return (MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0);
925     break;
926   case DW_TAG_typedef:
927   case DW_TAG_const_type:
928   case DW_TAG_volatile_type:
929     // Poor man's TCO:
930     type = type->subtype;
931     goto top;
932     break;
933   case DW_TAG_array_type:
934     subtype = type->subtype;
935     switch (subtype->type) {
936     case DW_TAG_unspecified_type:
937       return 1;
938
939     case DW_TAG_base_type:
940     case DW_TAG_enumeration_type:
941     case DW_TAG_pointer_type:
942     case DW_TAG_reference_type:
943     case DW_TAG_rvalue_reference_type:
944     case DW_TAG_structure_type:
945     case DW_TAG_class_type:
946     case DW_TAG_union_type:
947       if (subtype->full_type)
948         subtype = subtype->full_type;
949       elm_size = subtype->byte_size;
950       break;
951       // TODO, just remove the type indirection?
952     case DW_TAG_const_type:
953     case DW_TAG_typedef:
954     case DW_TAG_volatile_type:
955       subsubtype = subtype->subtype;
956       if (subsubtype->full_type)
957         subsubtype = subsubtype->full_type;
958       elm_size = subsubtype->byte_size;
959       break;
960     default:
961       return 0;
962       break;
963     }
964     for (int i = 0; i < type->element_count; i++) {
965       // TODO, add support for variable stride (DW_AT_byte_stride)
966       res =
967           compare_heap_area_with_type(state, process_index,
968                                       (char *) real_area1 + (i * elm_size),
969                                       (char *) real_area2 + (i * elm_size),
970                                       snapshot1, snapshot2, previous,
971                                       type->subtype, subtype->byte_size,
972                                       check_ignore, pointer_level);
973       if (res == 1)
974         return res;
975     }
976     break;
977   case DW_TAG_reference_type:
978   case DW_TAG_rvalue_reference_type:
979   case DW_TAG_pointer_type:
980     if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
981       addr_pointed1 = MC_snapshot_read_pointer(snapshot1, real_area1, process_index);
982       addr_pointed2 = MC_snapshot_read_pointer(snapshot2, real_area2, process_index);
983       return (addr_pointed1 != addr_pointed2);;
984     } else {
985       pointer_level++;
986       if (pointer_level > 1) {  /* Array of pointers */
987         for (size_t i = 0; i < (area_size / sizeof(void *)); i++) {
988           addr_pointed1 = MC_snapshot_read_pointer(snapshot1, (char*) real_area1 + i * sizeof(void *), process_index);
989           addr_pointed2 = MC_snapshot_read_pointer(snapshot2, (char*) real_area2 + i * sizeof(void *), process_index);
990           if (addr_pointed1 > state->std_heap_copy.heapbase
991               && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
992               && addr_pointed2 > state->std_heap_copy.heapbase
993               && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
994             res =
995                 compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
996                                   snapshot2, previous, type->subtype,
997                                   pointer_level);
998           else
999             res = (addr_pointed1 != addr_pointed2);
1000           if (res == 1)
1001             return res;
1002         }
1003       } else {
1004         addr_pointed1 = MC_snapshot_read_pointer(snapshot1, real_area1, process_index);
1005         addr_pointed2 = MC_snapshot_read_pointer(snapshot2, real_area2, process_index);
1006         if (addr_pointed1 > state->std_heap_copy.heapbase
1007             && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
1008             && addr_pointed2 > state->std_heap_copy.heapbase
1009             && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
1010           return compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
1011                                    snapshot2, previous, type->subtype,
1012                                    pointer_level);
1013         else
1014           return (addr_pointed1 != addr_pointed2);
1015       }
1016     }
1017     break;
1018   case DW_TAG_structure_type:
1019   case DW_TAG_class_type:
1020     if (type->full_type)
1021       type = type->full_type;
1022     if (area_size != -1 && type->byte_size != area_size) {
1023       if (area_size > type->byte_size && area_size % type->byte_size == 0) {
1024         for (size_t i = 0; i < (size_t)(area_size / type->byte_size); i++) {
1025           res =
1026               compare_heap_area_with_type(state, process_index,
1027                                           (char *) real_area1 + i * type->byte_size,
1028                                           (char *) real_area2 + i * type->byte_size,
1029                                           snapshot1, snapshot2, previous, type, -1,
1030                                           check_ignore, 0);
1031           if (res == 1)
1032             return res;
1033         }
1034       } else {
1035         return -1;
1036       }
1037     } else {
1038       cursor = 0;
1039       xbt_dynar_foreach(type->members, cursor, member) {
1040         // TODO, optimize this? (for the offset case)
1041         void *real_member1 =
1042             mc_member_resolve(real_area1, type, member, (mc_address_space_t) snapshot1, process_index);
1043         void *real_member2 =
1044             mc_member_resolve(real_area2, type, member, (mc_address_space_t) snapshot2, process_index);
1045         res =
1046             compare_heap_area_with_type(state, process_index, real_member1, real_member2,
1047                                         snapshot1, snapshot2,
1048                                         previous, member->subtype, -1,
1049                                         check_ignore, 0);
1050         if (res == 1) {
1051           return res;
1052         }
1053       }
1054     }
1055     break;
1056   case DW_TAG_union_type:
1057     return compare_heap_area_without_type(state, process_index, real_area1, real_area2,
1058                                           snapshot1, snapshot2, previous,
1059                                           type->byte_size, check_ignore);
1060     break;
1061   default:
1062     break;
1063   }
1064
1065   return 0;
1066
1067 }
1068
1069 /** Infer the type of a part of the block from the type of the block
1070  *
1071  * TODO, handle DW_TAG_array_type as well as arrays of the object ((*p)[5], p[5])
1072  *
1073  * TODO, handle subfields ((*p).bar.foo, (*p)[5].bar…)
1074  *
1075  * @param  type_id            DWARF type ID of the root address
1076  * @param  area_size
1077  * @return                    DWARF type ID for given offset
1078  */
1079 static dw_type_t get_offset_type(void *real_base_address, dw_type_t type,
1080                                  int offset, int area_size,
1081                                  mc_snapshot_t snapshot, int process_index)
1082 {
1083
1084   // Beginning of the block, the infered variable type if the type of the block:
1085   if (offset == 0)
1086     return type;
1087
1088   switch (type->type) {
1089   case DW_TAG_structure_type:
1090   case DW_TAG_class_type:
1091     if (type->full_type)
1092       type = type->full_type;
1093
1094     if (area_size != -1 && type->byte_size != area_size) {
1095       if (area_size > type->byte_size && area_size % type->byte_size == 0)
1096         return type;
1097       else
1098         return NULL;
1099     } else {
1100       unsigned int cursor = 0;
1101       dw_type_t member;
1102       xbt_dynar_foreach(type->members, cursor, member) {
1103
1104         if (!member->location.size) {
1105           // We have the offset, use it directly (shortcut):
1106           if (member->offset == offset)
1107             return member->subtype;
1108         } else {
1109           void *real_member =
1110             mc_member_resolve(real_base_address, type, member,
1111               (mc_address_space_t) snapshot, process_index);
1112           if ((char*) real_member - (char *) real_base_address == offset)
1113             return member->subtype;
1114         }
1115
1116       }
1117       return NULL;
1118     }
1119     break;
1120   default:
1121     /* FIXME : other cases ? */
1122     return NULL;
1123     break;
1124   }
1125 }
1126
1127 /**
1128  *
1129  * @param area1          Process address for state 1
1130  * @param area2          Process address for state 2
1131  * @param snapshot1      Snapshot of state 1
1132  * @param snapshot2      Snapshot of state 2
1133  * @param previous       Pairs of blocks already compared on the current path (or NULL)
1134  * @param type_id        Type of variable
1135  * @param pointer_level
1136  * @return 0 (same), 1 (different), -1
1137  */
1138 int compare_heap_area(int process_index, const void *area1, const void *area2, mc_snapshot_t snapshot1,
1139                       mc_snapshot_t snapshot2, xbt_dynar_t previous,
1140                       dw_type_t type, int pointer_level)
1141 {
1142   mc_process_t process = &mc_model_checker->process();
1143
1144   struct s_mc_diff *state = mc_diff_info;
1145
1146   int res_compare;
1147   ssize_t block1, frag1, block2, frag2;
1148   ssize_t size;
1149   int check_ignore = 0;
1150
1151   void *real_addr_block1, *real_addr_block2, *real_addr_frag1, *real_addr_frag2;
1152   int type_size = -1;
1153   int offset1 = 0, offset2 = 0;
1154   int new_size1 = -1, new_size2 = -1;
1155   dw_type_t new_type1 = NULL, new_type2 = NULL;
1156
1157   int match_pairs = 0;
1158
1159   // This is the address of std_heap->heapinfo in the application process:
1160   void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
1161
1162   const malloc_info* heapinfos1 = (const malloc_info*) MC_snapshot_read_pointer(
1163     snapshot1, heapinfo_address, process_index);
1164   const malloc_info* heapinfos2 = (const malloc_info*) MC_snapshot_read_pointer(
1165     snapshot2, heapinfo_address, process_index);
1166
1167   malloc_info heapinfo_temp1, heapinfo_temp2;
1168
1169   if (previous == NULL) {
1170     previous =
1171         xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
1172     match_pairs = 1;
1173   }
1174   // Get block number:
1175   block1 =
1176       ((char *) area1 -
1177        (char *) state->std_heap_copy.heapbase) / BLOCKSIZE + 1;
1178   block2 =
1179       ((char *) area2 -
1180        (char *) state->std_heap_copy.heapbase) / BLOCKSIZE + 1;
1181
1182   // If either block is a stack block:
1183   if (is_block_stack((int) block1) && is_block_stack((int) block2)) {
1184     add_heap_area_pair(previous, block1, -1, block2, -1);
1185     if (match_pairs) {
1186       match_equals(state, previous);
1187       xbt_dynar_free(&previous);
1188     }
1189     return 0;
1190   }
1191   // If either block is not in the expected area of memory:
1192   if (((char *) area1 < (char *) state->std_heap_copy.heapbase)
1193       || (block1 > (ssize_t) state->heapsize1) || (block1 < 1)
1194       || ((char *) area2 < (char *) state->std_heap_copy.heapbase)
1195       || (block2 > (ssize_t) state->heapsize2) || (block2 < 1)) {
1196     if (match_pairs) {
1197       xbt_dynar_free(&previous);
1198     }
1199     return 1;
1200   }
1201
1202   // Process address of the block:
1203   real_addr_block1 = (ADDR2UINT(block1) - 1) * BLOCKSIZE +
1204                  (char *) state->std_heap_copy.heapbase;
1205   real_addr_block2 = (ADDR2UINT(block2) - 1) * BLOCKSIZE +
1206                  (char *) state->std_heap_copy.heapbase;
1207
1208   if (type) {
1209
1210     if (type->full_type)
1211       type = type->full_type;
1212
1213     // This assume that for "boring" types (volatile ...) byte_size is absent:
1214     while (type->byte_size == 0 && type->subtype != NULL)
1215       type = type->subtype;
1216
1217     // Find type_size:
1218     if ((type->type == DW_TAG_pointer_type)
1219         || ((type->type == DW_TAG_base_type) && type->name != NULL
1220             && (!strcmp(type->name, "char"))))
1221       type_size = -1;
1222     else
1223       type_size = type->byte_size;
1224
1225   }
1226
1227   mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
1228   mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
1229
1230   const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
1231     heap_region1, &heapinfo_temp1, &heapinfos1[block1], sizeof(malloc_info));
1232   const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
1233     heap_region2, &heapinfo_temp2, &heapinfos2[block2], sizeof(malloc_info));
1234
1235   if ((heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type==MMALLOC_TYPE_HEAPINFO)
1236     && (heapinfo2->type == MMALLOC_TYPE_FREE || heapinfo2->type ==MMALLOC_TYPE_HEAPINFO)) {
1237
1238     /* Free block */
1239     if (match_pairs) {
1240       match_equals(state, previous);
1241       xbt_dynar_free(&previous);
1242     }
1243     return 0;
1244
1245   } else if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED
1246     && heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
1247     /* Complete block */
1248
1249     // TODO, lookup variable type from block type as done for fragmented blocks
1250
1251     offset1 = (char *) area1 - (char *) real_addr_block1;
1252     offset2 = (char *) area2 - (char *) real_addr_block2;
1253
1254     if (state->equals_to1_(block1, 0).valid
1255         && state->equals_to2_(block2, 0).valid) {
1256       if (equal_blocks(state, block1, block2)) {
1257         if (match_pairs) {
1258           match_equals(state, previous);
1259           xbt_dynar_free(&previous);
1260         }
1261         return 0;
1262       }
1263     }
1264
1265     if (type_size != -1) {
1266       if (type_size != (ssize_t) heapinfo1->busy_block.busy_size
1267           && type_size != (ssize_t)   heapinfo2->busy_block.busy_size
1268           && (type->name == NULL || !strcmp(type->name, "struct s_smx_context"))) {
1269         if (match_pairs) {
1270           match_equals(state, previous);
1271           xbt_dynar_free(&previous);
1272         }
1273         return -1;
1274       }
1275     }
1276
1277     if (heapinfo1->busy_block.size !=
1278         heapinfo2->busy_block.size) {
1279       if (match_pairs) {
1280         xbt_dynar_free(&previous);
1281       }
1282       return 1;
1283     }
1284
1285     if (heapinfo1->busy_block.busy_size !=
1286         heapinfo2->busy_block.busy_size) {
1287       if (match_pairs) {
1288         xbt_dynar_free(&previous);
1289       }
1290       return 1;
1291     }
1292
1293     if (!add_heap_area_pair(previous, block1, -1, block2, -1)) {
1294       if (match_pairs) {
1295         match_equals(state, previous);
1296         xbt_dynar_free(&previous);
1297       }
1298       return 0;
1299     }
1300
1301     size = heapinfo1->busy_block.busy_size;
1302
1303     // Remember (basic) type inference.
1304     // The current data structure only allows us to do this for the whole block.
1305     if (type != NULL && area1 == real_addr_block1) {
1306       state->types1_(block1, 0) = type;
1307     }
1308     if (type != NULL && area2 == real_addr_block2) {
1309       state->types2_(block2, 0) = type;
1310     }
1311
1312     if (size <= 0) {
1313       if (match_pairs) {
1314         match_equals(state, previous);
1315         xbt_dynar_free(&previous);
1316       }
1317       return 0;
1318     }
1319
1320     frag1 = -1;
1321     frag2 = -1;
1322
1323     if ((heapinfo1->busy_block.ignore > 0)
1324         && (heapinfo2->busy_block.ignore ==
1325             heapinfo1->busy_block.ignore))
1326       check_ignore = heapinfo1->busy_block.ignore;
1327
1328   } else if ((heapinfo1->type > 0) && (heapinfo2->type > 0)) {      /* Fragmented block */
1329
1330     // Fragment number:
1331     frag1 =
1332         ((uintptr_t) (ADDR2UINT(area1) % (BLOCKSIZE))) >> heapinfo1->type;
1333     frag2 =
1334         ((uintptr_t) (ADDR2UINT(area2) % (BLOCKSIZE))) >> heapinfo2->type;
1335
1336     // Process address of the fragment:
1337     real_addr_frag1 =
1338         (void *) ((char *) real_addr_block1 +
1339                   (frag1 << heapinfo1->type));
1340     real_addr_frag2 =
1341         (void *) ((char *) real_addr_block2 +
1342                   (frag2 << heapinfo2->type));
1343
1344     // Check the size of the fragments against the size of the type:
1345     if (type_size != -1) {
1346       if (heapinfo1->busy_frag.frag_size[frag1] == -1
1347           || heapinfo2->busy_frag.frag_size[frag2] == -1) {
1348         if (match_pairs) {
1349           match_equals(state, previous);
1350           xbt_dynar_free(&previous);
1351         }
1352         return -1;
1353       }
1354       // ?
1355       if (type_size != heapinfo1->busy_frag.frag_size[frag1]
1356           || type_size != heapinfo2->busy_frag.frag_size[frag2]) {
1357         if (match_pairs) {
1358           match_equals(state, previous);
1359           xbt_dynar_free(&previous);
1360         }
1361         return -1;
1362       }
1363     }
1364
1365     // Check if the blocks are already matched together:
1366     if (state->equals_to1_(block1, frag1).valid
1367         && state->equals_to2_(block2, frag2).valid) {
1368       if (offset1==offset2 && equal_fragments(state, block1, frag1, block2, frag2)) {
1369         if (match_pairs) {
1370           match_equals(state, previous);
1371           xbt_dynar_free(&previous);
1372         }
1373         return 0;
1374       }
1375     }
1376     // Compare the size of both fragments:
1377     if (heapinfo1->busy_frag.frag_size[frag1] !=
1378         heapinfo2->busy_frag.frag_size[frag2]) {
1379       if (type_size == -1) {
1380         if (match_pairs) {
1381           match_equals(state, previous);
1382           xbt_dynar_free(&previous);
1383         }
1384         return -1;
1385       } else {
1386         if (match_pairs) {
1387           xbt_dynar_free(&previous);
1388         }
1389         return 1;
1390       }
1391     }
1392
1393     // Size of the fragment:
1394     size = heapinfo1->busy_frag.frag_size[frag1];
1395
1396     // Remember (basic) type inference.
1397     // The current data structure only allows us to do this for the whole fragment.
1398     if (type != NULL && area1 == real_addr_frag1) {
1399       state->types1_(block1, frag1) = type;
1400     }
1401     if (type != NULL && area2 == real_addr_frag2) {
1402       state->types2_(block2, frag2) = type;
1403     }
1404     // The type of the variable is already known:
1405     if (type) {
1406       new_type1 = type;
1407       new_type2 = type;
1408     }
1409     // Type inference from the block type.
1410     else if (state->types1_(block1, frag1) != NULL
1411              || state->types2_(block2, frag2) != NULL) {
1412
1413       offset1 = (char *) area1 - (char *) real_addr_frag1;
1414       offset2 = (char *) area2 - (char *) real_addr_frag2;
1415
1416       if (state->types1_(block1, frag1) != NULL
1417           && state->types2_(block2, frag2) != NULL) {
1418         new_type1 =
1419             get_offset_type(real_addr_frag1, state->types1_(block1, frag1),
1420                             offset1, size, snapshot1, process_index);
1421         new_type2 =
1422             get_offset_type(real_addr_frag2, state->types2_(block2, frag2),
1423                             offset1, size, snapshot2, process_index);
1424       } else if (state->types1_(block1, frag1) != NULL) {
1425         new_type1 =
1426             get_offset_type(real_addr_frag1, state->types1_(block1, frag1),
1427                             offset1, size, snapshot1, process_index);
1428         new_type2 =
1429             get_offset_type(real_addr_frag2, state->types1_(block1, frag1),
1430                             offset2, size, snapshot2, process_index);
1431       } else if (state->types2_(block2, frag2) != NULL) {
1432         new_type1 =
1433             get_offset_type(real_addr_frag1, state->types2_(block2, frag2),
1434                             offset1, size, snapshot1, process_index);
1435         new_type2 =
1436             get_offset_type(real_addr_frag2, state->types2_(block2, frag2),
1437                             offset2, size, snapshot2, process_index);
1438       } else {
1439         if (match_pairs) {
1440           match_equals(state, previous);
1441           xbt_dynar_free(&previous);
1442         }
1443         return -1;
1444       }
1445
1446       if (new_type1 != NULL && new_type2 != NULL && new_type1 != new_type2) {
1447
1448         type = new_type1;
1449         while (type->byte_size == 0 && type->subtype != NULL)
1450           type = type->subtype;
1451         new_size1 = type->byte_size;
1452
1453         type = new_type2;
1454         while (type->byte_size == 0 && type->subtype != NULL)
1455           type = type->subtype;
1456         new_size2 = type->byte_size;
1457
1458       } else {
1459         if (match_pairs) {
1460           match_equals(state, previous);
1461           xbt_dynar_free(&previous);
1462         }
1463         return -1;
1464       }
1465     }
1466
1467     if (new_size1 > 0 && new_size1 == new_size2) {
1468       type = new_type1;
1469       size = new_size1;
1470     }
1471
1472     if (offset1 == 0 && offset2 == 0) {
1473       if (!add_heap_area_pair(previous, block1, frag1, block2, frag2)) {
1474         if (match_pairs) {
1475           match_equals(state, previous);
1476           xbt_dynar_free(&previous);
1477         }
1478         return 0;
1479       }
1480     }
1481
1482     if (size <= 0) {
1483       if (match_pairs) {
1484         match_equals(state, previous);
1485         xbt_dynar_free(&previous);
1486       }
1487       return 0;
1488     }
1489
1490     if ((heapinfo1->busy_frag.ignore[frag1] > 0)
1491         && (heapinfo2->busy_frag.ignore[frag2] ==
1492             heapinfo1->busy_frag.ignore[frag1]))
1493       check_ignore = heapinfo1->busy_frag.ignore[frag1];
1494
1495   } else {
1496
1497     if (match_pairs) {
1498       xbt_dynar_free(&previous);
1499     }
1500     return 1;
1501
1502   }
1503
1504
1505   /* Start comparison */
1506   if (type) {
1507     res_compare =
1508         compare_heap_area_with_type(state, process_index, area1, area2, snapshot1, snapshot2,
1509                                     previous, type, size, check_ignore,
1510                                     pointer_level);
1511   } else {
1512     res_compare =
1513         compare_heap_area_without_type(state, process_index, area1, area2, snapshot1, snapshot2,
1514                                        previous, size, check_ignore);
1515   }
1516   if (res_compare == 1) {
1517     if (match_pairs)
1518       xbt_dynar_free(&previous);
1519     return res_compare;
1520   }
1521
1522   if (match_pairs) {
1523     match_equals(state, previous);
1524     xbt_dynar_free(&previous);
1525   }
1526
1527   return 0;
1528 }
1529
1530 /*********************************************** Miscellaneous ***************************************************/
1531 /****************************************************************************************************************/
1532
1533 // Not used and broken code:
1534 # if 0
1535
1536 // Not used:
1537 static int get_pointed_area_size(void *area, int heap)
1538 {
1539
1540   struct s_mc_diff *state = mc_diff_info;
1541
1542   int block, frag;
1543   malloc_info *heapinfo;
1544
1545   if (heap == 1)
1546     heapinfo = state->heapinfo1;
1547   else
1548     heapinfo = state->heapinfo2;
1549
1550   block =
1551       ((char *) area -
1552        (char *) state->std_heap_copy.heapbase) / BLOCKSIZE + 1;
1553
1554   if (((char *) area < (char *) state->std_heap_copy.heapbase)
1555       || (block > state->heapsize1) || (block < 1))
1556     return -1;
1557
1558   if (heapinfo[block].type == MMALLOC_TYPE_FREE || heapinfo[block].type == MMALLOC_TYPE_HEAPINFO) {     /* Free block */
1559     return -1;
1560   } else if (heapinfo[block].type == MMALLOC_TYPE_UNFRAGMENTED) {       /* Complete block */
1561     return (int) heapinfo[block].busy_block.busy_size;
1562   } else {
1563     frag =
1564         ((uintptr_t) (ADDR2UINT(area) % (BLOCKSIZE))) >> heapinfo[block].type;
1565     return (int) heapinfo[block].busy_frag.frag_size[frag];
1566   }
1567 }
1568
1569 // Not used:
1570 char *get_type_description(mc_object_info_t info, char *type_name)
1571 {
1572
1573   xbt_dict_cursor_t dict_cursor;
1574   char *type_origin;
1575   dw_type_t type;
1576
1577   xbt_dict_foreach(info->types, dict_cursor, type_origin, type) {
1578     if (type->name && (strcmp(type->name, type_name) == 0)
1579         && type->byte_size > 0) {
1580       xbt_dict_cursor_free(&dict_cursor);
1581       return type_origin;
1582     }
1583   }
1584
1585   xbt_dict_cursor_free(&dict_cursor);
1586   return NULL;
1587 }
1588
1589
1590 #ifndef max
1591 #define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
1592 #endif
1593
1594 // Not used:
1595 int mmalloc_linear_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2)
1596 {
1597
1598   struct s_mc_diff *state = mc_diff_info;
1599
1600   if (heap1 == NULL && heap1 == NULL) {
1601     XBT_DEBUG("Malloc descriptors null");
1602     return 0;
1603   }
1604
1605   if (heap1->heaplimit != heap2->heaplimit) {
1606     XBT_DEBUG("Different limit of valid info table indices");
1607     return 1;
1608   }
1609
1610   /* Heap information */
1611   state->heaplimit = ((struct mdesc *) heap1)->heaplimit;
1612
1613   state->std_heap_copy = *MC_process_get_heap(&mc_model_checker->process());
1614
1615   state->heapbase1 = (char *) heap1 + BLOCKSIZE;
1616   state->heapbase2 = (char *) heap2 + BLOCKSIZE;
1617
1618   state->heapinfo1 =
1619       (malloc_info *) ((char *) heap1 +
1620                        ((uintptr_t)
1621                         ((char *) heap1->heapinfo - (char *) state->s_heap)));
1622   state->heapinfo2 =
1623       (malloc_info *) ((char *) heap2 +
1624                        ((uintptr_t)
1625                         ((char *) heap2->heapinfo - (char *) state->s_heap)));
1626
1627   state->heapsize1 = heap1->heapsize;
1628   state->heapsize2 = heap2->heapsize;
1629
1630   /* Start comparison */
1631   size_t i, j, k;
1632   void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
1633
1634   int distance = 0;
1635
1636   /* Check busy blocks */
1637
1638   i = 1;
1639
1640   while (i <= state->heaplimit) {
1641
1642     addr_block1 =
1643         ((void *) (((ADDR2UINT(i)) - 1) * BLOCKSIZE +
1644                    (char *) state->heapbase1));
1645     addr_block2 =
1646         ((void *) (((ADDR2UINT(i)) - 1) * BLOCKSIZE +
1647                    (char *) state->heapbase2));
1648
1649     if (state->heapinfo1[i].type != state->heapinfo2[i].type) {
1650
1651       distance += BLOCKSIZE;
1652       XBT_DEBUG("Different type of blocks (%zu) : %d - %d -> distance = %d", i,
1653                 state->heapinfo1[i].type, state->heapinfo2[i].type, distance);
1654       i++;
1655
1656     } else {
1657
1658       if (state->heapinfo1[i].type == MMALLOC_TYPE_FREE
1659         || state->heapinfo1[i].type == MMALLOC_TYPE_HAPINFO) {     /* Free block */
1660         i++;
1661         continue;
1662       }
1663
1664       if (state->heapinfo1[i].type == MMALLOC_TYPE_UNFRAGMENTED) {      /* Large block */
1665
1666         if (state->heapinfo1[i].busy_block.size !=
1667             state->heapinfo2[i].busy_block.size) {
1668           distance +=
1669               BLOCKSIZE * max(state->heapinfo1[i].busy_block.size,
1670                               state->heapinfo2[i].busy_block.size);
1671           i += max(state->heapinfo1[i].busy_block.size,
1672                    state->heapinfo2[i].busy_block.size);
1673           XBT_DEBUG
1674               ("Different larger of cluster at block %zu : %zu - %zu -> distance = %d",
1675                i, state->heapinfo1[i].busy_block.size,
1676                state->heapinfo2[i].busy_block.size, distance);
1677           continue;
1678         }
1679
1680         /*if(heapinfo1[i].busy_block.busy_size != heapinfo2[i].busy_block.busy_size){
1681            distance += max(heapinfo1[i].busy_block.busy_size, heapinfo2[i].busy_block.busy_size);
1682            i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
1683            XBT_DEBUG("Different size used oin large cluster at block %zu : %zu - %zu -> distance = %d", i, heapinfo1[i].busy_block.busy_size, heapinfo2[i].busy_block.busy_size, distance);
1684            continue;
1685            } */
1686
1687         k = 0;
1688
1689         //while(k < (heapinfo1[i].busy_block.busy_size)){
1690         while (k < state->heapinfo1[i].busy_block.size * BLOCKSIZE) {
1691           if (memcmp((char *) addr_block1 + k, (char *) addr_block2 + k, 1) !=
1692               0) {
1693             distance++;
1694           }
1695           k++;
1696         }
1697
1698         i++;
1699
1700       } else {                  /* Fragmented block */
1701
1702         for (j = 0; j < (size_t) (BLOCKSIZE >> state->heapinfo1[i].type); j++) {
1703
1704           addr_frag1 =
1705               (void *) ((char *) addr_block1 + (j << state->heapinfo1[i].type));
1706           addr_frag2 =
1707               (void *) ((char *) addr_block2 + (j << state->heapinfo2[i].type));
1708
1709           if (state->heapinfo1[i].busy_frag.frag_size[j] == 0
1710               && state->heapinfo2[i].busy_frag.frag_size[j] == 0) {
1711             continue;
1712           }
1713
1714
1715           /*if(heapinfo1[i].busy_frag.frag_size[j] != heapinfo2[i].busy_frag.frag_size[j]){
1716              distance += max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j]);
1717              XBT_DEBUG("Different size used in fragment %zu in block %zu : %d - %d -> distance = %d", j, i, heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j], distance); 
1718              continue;
1719              } */
1720
1721           k = 0;
1722
1723           //while(k < max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j])){
1724           while (k < (BLOCKSIZE / (BLOCKSIZE >> state->heapinfo1[i].type))) {
1725             if (memcmp((char *) addr_frag1 + k, (char *) addr_frag2 + k, 1) !=
1726                 0) {
1727               distance++;
1728             }
1729             k++;
1730           }
1731
1732         }
1733
1734         i++;
1735
1736       }
1737
1738     }
1739
1740   }
1741
1742   return distance;
1743
1744 }
1745 #endif
1746
1747 }