Logo AND Algorithmique Numérique Distribuée

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