Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Avoid looking up region in mc_diff (we are in the heap)
[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   xbt_dynar_t previous =
426       xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
427
428   int equal, res_compare = 0;
429
430   /* Check busy blocks */
431
432   i1 = 1;
433
434   malloc_info heapinfo_temp1, heapinfo_temp2;
435   malloc_info heapinfo_temp2b;
436
437   mc_mem_region_t heap_region1 = snapshot1->regions[0];
438   mc_mem_region_t heap_region2 = snapshot2->regions[0];
439
440   // This is in snapshot do not use them directly:
441   malloc_info* heapinfos1 = mc_snapshot_read_pointer(&((xbt_mheap_t)std_heap)->heapinfo, snapshot1);
442   malloc_info* heapinfos2 = mc_snapshot_read_pointer(&((xbt_mheap_t)std_heap)->heapinfo, snapshot2);
443
444   while (i1 <= state->heaplimit) {
445
446     // TODO, lookup in the correct region in order to speed it up:
447     malloc_info* heapinfo1 = mc_snapshot_read_region(&heapinfos1[i1], heap_region1, &heapinfo_temp1, sizeof(malloc_info));
448     malloc_info* heapinfo2 = mc_snapshot_read_region(&heapinfos2[i1], heap_region2, &heapinfo_temp2, sizeof(malloc_info));
449
450     if (heapinfo1->type == -1) {      /* Free block */
451       i1++;
452       continue;
453     }
454
455     addr_block1 =
456         ((void *) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE +
457                    (char *) ((xbt_mheap_t) state->s_heap)->heapbase));
458
459     if (heapinfo1->type == 0) {       /* Large block */
460
461       if (is_stack(addr_block1)) {
462         for (k = 0; k < heapinfo1->busy_block.size; k++)
463           state->equals_to1_(i1 + k, 0) = make_heap_area(i1, -1);
464         for (k = 0; k < heapinfo2->busy_block.size; k++)
465           state->equals_to2_(i1 + k, 0) = make_heap_area(i1, -1);
466         i1 += heapinfo1->busy_block.size;
467         continue;
468       }
469
470       if (state->equals_to1_(i1, 0).valid) {
471         i1++;
472         continue;
473       }
474
475       i2 = 1;
476       equal = 0;
477       res_compare = 0;
478
479       /* Try first to associate to same block in the other heap */
480       if (heapinfo2->type == heapinfo1->type) {
481
482         if (state->equals_to2_(i1, 0).valid == 0) {
483
484           addr_block2 =
485               ((void *) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE +
486                          (char *) ((xbt_mheap_t) state->s_heap)->heapbase));
487
488           res_compare =
489               compare_heap_area(addr_block1, addr_block2, snapshot1, snapshot2,
490                                 NULL, NULL, 0);
491
492           if (res_compare != 1) {
493             for (k = 1; k < heapinfo2->busy_block.size; k++)
494               state->equals_to2_(i1 + k, 0) = make_heap_area(i1, -1);
495             for (k = 1; k < heapinfo1->busy_block.size; k++)
496               state->equals_to1_(i1 + k, 0) = make_heap_area(i1, -1);
497             equal = 1;
498             i1 += heapinfo1->busy_block.size;
499           }
500
501           xbt_dynar_reset(previous);
502
503         }
504
505       }
506
507       while (i2 <= state->heaplimit && !equal) {
508
509         addr_block2 =
510             ((void *) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE +
511                        (char *) ((xbt_mheap_t) state->s_heap)->heapbase));
512
513         if (i2 == i1) {
514           i2++;
515           continue;
516         }
517
518         malloc_info* heapinfo2b = mc_snapshot_read_region(&heapinfos2[i2], heap_region2, &heapinfo_temp2b, sizeof(malloc_info));
519
520         if (heapinfo2b->type != 0) {
521           i2++;
522           continue;
523         }
524
525         if (state->equals_to2_(i2, 0).valid) {
526           i2++;
527           continue;
528         }
529
530         res_compare =
531             compare_heap_area(addr_block1, addr_block2, snapshot1, snapshot2,
532                               NULL, NULL, 0);
533
534         if (res_compare != 1) {
535           for (k = 1; k < heapinfo2b->busy_block.size; k++)
536             state->equals_to2_(i2 + k, 0) = make_heap_area(i1, -1);
537           for (k = 1; k < heapinfo1->busy_block.size; k++)
538             state->equals_to1_(i1 + k, 0) = make_heap_area(i2, -1);
539           equal = 1;
540           i1 += heapinfo1->busy_block.size;
541         }
542
543         xbt_dynar_reset(previous);
544
545         i2++;
546
547       }
548
549       if (!equal) {
550         XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1,
551                   heapinfo1->busy_block.busy_size, addr_block1);
552         i1 = state->heaplimit + 1;
553         nb_diff1++;
554         //i1++;
555       }
556
557     } else {                    /* Fragmented block */
558
559       for (j1 = 0; j1 < (size_t) (BLOCKSIZE >> heapinfo1->type); j1++) {
560
561         if (heapinfo1->busy_frag.frag_size[j1] == -1) /* Free fragment */
562           continue;
563
564         if (state->equals_to1_(i1, j1).valid)
565           continue;
566
567         addr_frag1 =
568             (void *) ((char *) addr_block1 + (j1 << heapinfo1->type));
569
570         i2 = 1;
571         equal = 0;
572
573         /* Try first to associate to same fragment in the other heap */
574         if (heapinfo2->type == heapinfo1->type) {
575
576           if (state->equals_to2_(i1, j1).valid == 0) {
577
578             addr_block2 =
579                 ((void *) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE +
580                            (char *) ((xbt_mheap_t) state->s_heap)->heapbase));
581             addr_frag2 =
582                 (void *) ((char *) addr_block2 +
583                           (j1 << ((xbt_mheap_t) state->s_heap)->heapinfo[i1].
584                            type));
585
586             res_compare =
587                 compare_heap_area(addr_frag1, addr_frag2, snapshot1, snapshot2,
588                                   NULL, NULL, 0);
589
590             if (res_compare != 1)
591               equal = 1;
592
593             xbt_dynar_reset(previous);
594
595           }
596
597         }
598
599         while (i2 <= state->heaplimit && !equal) {
600
601           malloc_info* heapinfo2b = mc_snapshot_read_region(&heapinfos2[i2], heap_region2, &heapinfo_temp2b, sizeof(malloc_info));
602           if (heapinfo2b->type <= 0) {
603             i2++;
604             continue;
605           }
606
607           for (j2 = 0; j2 < (size_t) (BLOCKSIZE >> heapinfo2b->type);
608                j2++) {
609
610             if (i2 == i1 && j2 == j1)
611               continue;
612
613             if (state->equals_to2_(i2, j2).valid)
614               continue;
615
616             addr_block2 =
617                 ((void *) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE +
618                            (char *) ((xbt_mheap_t) state->s_heap)->heapbase));
619             addr_frag2 =
620                 (void *) ((char *) addr_block2 +
621                           (j2 << ((xbt_mheap_t) state->s_heap)->heapinfo[i2].
622                            type));
623
624             res_compare =
625                 compare_heap_area(addr_frag1, addr_frag2, snapshot2, snapshot2,
626                                   NULL, NULL, 0);
627
628             if (res_compare != 1) {
629               equal = 1;
630               xbt_dynar_reset(previous);
631               break;
632             }
633
634             xbt_dynar_reset(previous);
635
636           }
637
638           i2++;
639
640         }
641
642         if (!equal) {
643           XBT_DEBUG
644               ("Block %zu, fragment %zu not found (size_used = %zd, address = %p)\n",
645                i1, j1, heapinfo1->busy_frag.frag_size[j1],
646                addr_frag1);
647           i2 = state->heaplimit + 1;
648           i1 = state->heaplimit + 1;
649           nb_diff1++;
650           break;
651         }
652
653       }
654
655       i1++;
656
657     }
658
659   }
660
661   /* All blocks/fragments are equal to another block/fragment ? */
662   size_t i = 1, j = 0;
663
664   for(i = 1; i <= state->heaplimit; i++) {
665     malloc_info* heapinfo1 = mc_snapshot_read_region(&heapinfos1[i], heap_region1, &heapinfo_temp1, sizeof(malloc_info));
666     if (heapinfo1->type == 0) {
667       if (i1 == state->heaplimit) {
668         if (heapinfo1->busy_block.busy_size > 0) {
669           if (state->equals_to1_(i, 0).valid == 0) {
670             if (XBT_LOG_ISENABLED(mc_diff, xbt_log_priority_debug)) {
671               // TODO, add address
672               XBT_DEBUG("Block %zu not found (size used = %zu)", i,
673                         heapinfo1->busy_block.busy_size);
674               //mmalloc_backtrace_block_display((void*)heapinfo1, i);
675             }
676             nb_diff1++;
677           }
678         }
679       }
680     }
681     if (heapinfo1->type > 0) {
682       for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo1->type); j++) {
683         if (i1 == state->heaplimit) {
684           if (heapinfo1->busy_frag.frag_size[j] > 0) {
685             if (state->equals_to1_(i, j).valid == 0) {
686               if (XBT_LOG_ISENABLED(mc_diff, xbt_log_priority_debug)) {
687                 // TODO, print fragment address
688                 XBT_DEBUG
689                     ("Block %zu, Fragment %zu not found (size used = %zd)",
690                      i, j,
691                      heapinfo1->busy_frag.frag_size[j]);
692                 //mmalloc_backtrace_fragment_display((void*)heapinfo1, i, j);
693               }
694               nb_diff1++;
695             }
696           }
697         }
698       }
699     }
700   }
701
702   if (i1 == state->heaplimit)
703     XBT_DEBUG("Number of blocks/fragments not found in heap1 : %d", nb_diff1);
704
705   for (i=1; i <= state->heaplimit; i++) {
706     malloc_info* heapinfo2 = mc_snapshot_read_region(&heapinfos2[i], heap_region2, &heapinfo_temp2, sizeof(malloc_info));
707     if (heapinfo2->type == 0) {
708       if (i1 == state->heaplimit) {
709         if (heapinfo2->busy_block.busy_size > 0) {
710           if (state->equals_to2_(i, 0).valid == 0) {
711             if (XBT_LOG_ISENABLED(mc_diff, xbt_log_priority_debug)) {
712               // TODO, print address of the block
713               XBT_DEBUG("Block %zu not found (size used = %zu)", i,
714                         heapinfo2->busy_block.busy_size);
715               //mmalloc_backtrace_block_display((void*)heapinfo2, i);
716             }
717             nb_diff2++;
718           }
719         }
720       }
721     }
722     if (heapinfo2->type > 0) {
723       for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo2->type); j++) {
724         if (i1 == state->heaplimit) {
725           if (heapinfo2->busy_frag.frag_size[j] > 0) {
726             if (state->equals_to2_(i, j).valid == 0) {
727               if (XBT_LOG_ISENABLED(mc_diff, xbt_log_priority_debug)) {
728                 // TODO, print address of the block
729                 XBT_DEBUG
730                     ("Block %zu, Fragment %zu not found (size used = %zd)",
731                      i, j,
732                      heapinfo2->busy_frag.frag_size[j]);
733                 //mmalloc_backtrace_fragment_display((void*)heapinfo2, i, j);
734               }
735               nb_diff2++;
736             }
737           }
738         }
739       }
740     }
741   }
742
743   if (i1 == state->heaplimit)
744     XBT_DEBUG("Number of blocks/fragments not found in heap2 : %d", nb_diff2);
745
746   xbt_dynar_free(&previous);
747   return ((nb_diff1 > 0) || (nb_diff2 > 0));
748 }
749
750 /**
751  *
752  * @param state
753  * @param real_area1     Process address for state 1
754  * @param real_area2     Process address for state 2
755  * @param snapshot1      Snapshot of state 1
756  * @param snapshot2      Snapshot of state 2
757  * @param previous
758  * @param size
759  * @param check_ignore
760  */
761 static int compare_heap_area_without_type(struct s_mc_diff *state,
762                                           void *real_area1, void *real_area2,
763                                           mc_snapshot_t snapshot1,
764                                           mc_snapshot_t snapshot2,
765                                           xbt_dynar_t previous, int size,
766                                           int check_ignore)
767 {
768
769   int i = 0;
770   void *addr_pointed1, *addr_pointed2;
771   int pointer_align, res_compare;
772   ssize_t ignore1, ignore2;
773
774   while (i < size) {
775
776     if (check_ignore > 0) {
777       if ((ignore1 =
778            heap_comparison_ignore_size(state->to_ignore1,
779                                        (char *) real_area1 + i)) != -1) {
780         if ((ignore2 =
781              heap_comparison_ignore_size(state->to_ignore2,
782                                          (char *) real_area2 + i)) == ignore1) {
783           if (ignore1 == 0) {
784             check_ignore--;
785             return 0;
786           } else {
787             i = i + ignore2;
788             check_ignore--;
789             continue;
790           }
791         }
792       }
793     }
794
795     if (mc_snapshot_memcp(((char *) real_area1) + i, snapshot1, ((char *) real_area2) + i, snapshot2, 1) != 0) {
796
797       pointer_align = (i / sizeof(void *)) * sizeof(void *);
798       addr_pointed1 = mc_snapshot_read_pointer((char *) real_area1 + pointer_align, snapshot1);
799       addr_pointed2 = mc_snapshot_read_pointer((char *) real_area2 + pointer_align, snapshot2);
800
801       if (addr_pointed1 > maestro_stack_start
802           && addr_pointed1 < maestro_stack_end
803           && addr_pointed2 > maestro_stack_start
804           && addr_pointed2 < maestro_stack_end) {
805         i = pointer_align + sizeof(void *);
806         continue;
807       } else if (addr_pointed1 > state->s_heap
808                  && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
809                  && addr_pointed2 > state->s_heap
810                  && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)) {
811         // Both addreses are in the heap:
812         res_compare =
813             compare_heap_area(addr_pointed1, addr_pointed2, snapshot1,
814                               snapshot2, previous, NULL, 0);
815         if (res_compare == 1) {
816           return res_compare;
817         }
818         i = pointer_align + sizeof(void *);
819         continue;
820       } else {
821         return 1;
822       }
823
824     }
825
826     i++;
827
828   }
829
830   return 0;
831
832 }
833
834 /**
835  *
836  * @param state
837  * @param real_area1     Process address for state 1
838  * @param real_area2     Process address for state 2
839  * @param snapshot1      Snapshot of state 1
840  * @param snapshot2      Snapshot of state 2
841  * @param previous
842  * @param type_id
843  * @param area_size      either a byte_size or an elements_count (?)
844  * @param check_ignore
845  * @param pointer_level
846  * @return               0 (same), 1 (different), -1 (unknown)
847  */
848 static int compare_heap_area_with_type(struct s_mc_diff *state,
849                                        void *real_area1, void *real_area2,
850                                        mc_snapshot_t snapshot1,
851                                        mc_snapshot_t snapshot2,
852                                        xbt_dynar_t previous, dw_type_t type,
853                                        int area_size, int check_ignore,
854                                        int pointer_level)
855 {
856
857   if (is_stack(real_area1) && is_stack(real_area2))
858     return 0;
859
860   ssize_t ignore1, ignore2;
861
862   if ((check_ignore > 0)
863       && ((ignore1 = heap_comparison_ignore_size(state->to_ignore1, real_area1))
864           > 0)
865       && ((ignore2 = heap_comparison_ignore_size(state->to_ignore2, real_area2))
866           == ignore1)) {
867     return 0;
868   }
869
870   dw_type_t subtype, subsubtype;
871   int res, elm_size, i;
872   unsigned int cursor = 0;
873   dw_type_t member;
874   void *addr_pointed1, *addr_pointed2;;
875
876   switch (type->type) {
877   case DW_TAG_unspecified_type:
878     return 1;
879
880   case DW_TAG_base_type:
881     if (type->name != NULL && strcmp(type->name, "char") == 0) {        /* String, hence random (arbitrary ?) size */
882       if (real_area1 == real_area2)
883         return -1;
884       else
885         return (mc_snapshot_memcp(real_area1, snapshot1, real_area2, snapshot2, area_size) != 0);
886     } else {
887       if (area_size != -1 && type->byte_size != area_size)
888         return -1;
889       else {
890         return (mc_snapshot_memcp(real_area1, snapshot1, real_area2, snapshot2, type->byte_size) != 0);
891       }
892     }
893     break;
894   case DW_TAG_enumeration_type:
895     if (area_size != -1 && type->byte_size != area_size)
896       return -1;
897     else
898       return (mc_snapshot_memcp(real_area1, snapshot1, real_area2, snapshot2, type->byte_size) != 0);
899     break;
900   case DW_TAG_typedef:
901   case DW_TAG_const_type:
902   case DW_TAG_volatile_type:
903     return compare_heap_area_with_type(state, real_area1, real_area2,
904                                        snapshot1, snapshot2, previous,
905                                        type->subtype, area_size, check_ignore,
906                                        pointer_level);
907     break;
908   case DW_TAG_array_type:
909     subtype = type->subtype;
910     switch (subtype->type) {
911     case DW_TAG_unspecified_type:
912       return 1;
913
914     case DW_TAG_base_type:
915     case DW_TAG_enumeration_type:
916     case DW_TAG_pointer_type:
917     case DW_TAG_reference_type:
918     case DW_TAG_rvalue_reference_type:
919     case DW_TAG_structure_type:
920     case DW_TAG_class_type:
921     case DW_TAG_union_type:
922       if (subtype->full_type)
923         subtype = subtype->full_type;
924       elm_size = subtype->byte_size;
925       break;
926       // TODO, just remove the type indirection?
927     case DW_TAG_const_type:
928     case DW_TAG_typedef:
929     case DW_TAG_volatile_type:
930       subsubtype = subtype->subtype;
931       if (subsubtype->full_type)
932         subsubtype = subsubtype->full_type;
933       elm_size = subsubtype->byte_size;
934       break;
935     default:
936       return 0;
937       break;
938     }
939     for (i = 0; i < type->element_count; i++) {
940       // TODO, add support for variable stride (DW_AT_byte_stride)
941       res =
942           compare_heap_area_with_type(state,
943                                       (char *) real_area1 + (i * elm_size),
944                                       (char *) real_area2 + (i * elm_size),
945                                       snapshot1, snapshot2, previous,
946                                       type->subtype, subtype->byte_size,
947                                       check_ignore, pointer_level);
948       if (res == 1)
949         return res;
950     }
951     break;
952   case DW_TAG_reference_type:
953   case DW_TAG_rvalue_reference_type:
954   case DW_TAG_pointer_type:
955     if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
956       addr_pointed1 = mc_snapshot_read_pointer(real_area1, snapshot1);
957       addr_pointed2 = mc_snapshot_read_pointer(real_area2, snapshot2);
958       return (addr_pointed1 != addr_pointed2);;
959     } else {
960       pointer_level++;
961       if (pointer_level > 1) {  /* Array of pointers */
962         for (i = 0; i < (area_size / sizeof(void *)); i++) {
963           addr_pointed1 = mc_snapshot_read_pointer((char*) real_area1 + i * sizeof(void *), snapshot1);
964           addr_pointed2 = mc_snapshot_read_pointer((char*) real_area2 + i * sizeof(void *), snapshot2);
965           if (addr_pointed1 > state->s_heap
966               && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
967               && addr_pointed2 > state->s_heap
968               && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
969             res =
970                 compare_heap_area(addr_pointed1, addr_pointed2, snapshot1,
971                                   snapshot2, previous, type->subtype,
972                                   pointer_level);
973           else
974             res = (addr_pointed1 != addr_pointed2);
975           if (res == 1)
976             return res;
977         }
978       } else {
979         addr_pointed1 = mc_snapshot_read_pointer(real_area1, snapshot1);
980         addr_pointed2 = mc_snapshot_read_pointer(real_area2, snapshot2);
981         if (addr_pointed1 > state->s_heap
982             && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
983             && addr_pointed2 > state->s_heap
984             && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
985           return compare_heap_area(addr_pointed1, addr_pointed2, snapshot1,
986                                    snapshot2, previous, type->subtype,
987                                    pointer_level);
988         else
989           return (addr_pointed1 != addr_pointed2);
990       }
991     }
992     break;
993   case DW_TAG_structure_type:
994   case DW_TAG_class_type:
995     if (type->full_type)
996       type = type->full_type;
997     if (area_size != -1 && type->byte_size != area_size) {
998       if (area_size > type->byte_size && area_size % type->byte_size == 0) {
999         for (i = 0; i < (area_size / type->byte_size); i++) {
1000           res =
1001               compare_heap_area_with_type(state,
1002                                           (char *) real_area1 + i * type->byte_size,
1003                                           (char *) real_area2 + i * type->byte_size,
1004                                           snapshot1, snapshot2, previous, type, -1,
1005                                           check_ignore, 0);
1006           if (res == 1)
1007             return res;
1008         }
1009       } else {
1010         return -1;
1011       }
1012     } else {
1013       cursor = 0;
1014       xbt_dynar_foreach(type->members, cursor, member) {
1015         // TODO, optimize this? (for the offset case)
1016         char *real_member1 =
1017             mc_member_resolve(real_area1, type, member, snapshot1);
1018         char *real_member2 =
1019             mc_member_resolve(real_area2, type, member, snapshot2);
1020         res =
1021             compare_heap_area_with_type(state, real_member1, real_member2,
1022                                         snapshot1, snapshot2,
1023                                         previous, member->subtype, -1,
1024                                         check_ignore, 0);
1025         if (res == 1) {
1026           return res;
1027         }
1028       }
1029     }
1030     break;
1031   case DW_TAG_union_type:
1032     return compare_heap_area_without_type(state, real_area1, real_area2,
1033                                           snapshot1, snapshot2, previous,
1034                                           type->byte_size, check_ignore);
1035     break;
1036   default:
1037     break;
1038   }
1039
1040   return 0;
1041
1042 }
1043
1044 /** Infer the type of a part of the block from the type of the block
1045  *
1046  * TODO, handle DW_TAG_array_type as well as arrays of the object ((*p)[5], p[5])
1047  *
1048  * TODO, handle subfields ((*p).bar.foo, (*p)[5].bar…)
1049  *
1050  * @param  type_id            DWARF type ID of the root address
1051  * @param  area_size
1052  * @return                    DWARF type ID for given offset
1053  */
1054 static dw_type_t get_offset_type(void *real_base_address, dw_type_t type,
1055                                  int offset, int area_size,
1056                                  mc_snapshot_t snapshot)
1057 {
1058
1059   // Beginning of the block, the infered variable type if the type of the block:
1060   if (offset == 0)
1061     return type;
1062
1063   switch (type->type) {
1064   case DW_TAG_structure_type:
1065   case DW_TAG_class_type:
1066     if (type->full_type)
1067       type = type->full_type;
1068
1069     if (area_size != -1 && type->byte_size != area_size) {
1070       if (area_size > type->byte_size && area_size % type->byte_size == 0)
1071         return type;
1072       else
1073         return NULL;
1074     } else {
1075       unsigned int cursor = 0;
1076       dw_type_t member;
1077       xbt_dynar_foreach(type->members, cursor, member) {
1078
1079         if (!member->location.size) {
1080           // We have the offset, use it directly (shortcut):
1081           if (member->offset == offset)
1082             return member->subtype;
1083         } else {
1084           char *real_member =
1085               mc_member_resolve(real_base_address, type, member, snapshot);
1086           if (real_member - (char *) real_base_address == offset)
1087             return member->subtype;
1088         }
1089
1090       }
1091       return NULL;
1092     }
1093     break;
1094   default:
1095     /* FIXME : other cases ? */
1096     return NULL;
1097     break;
1098   }
1099 }
1100
1101 /**
1102  *
1103  * @param area1          Process address for state 1
1104  * @param area2          Process address for state 2
1105  * @param snapshot1      Snapshot of state 1
1106  * @param snapshot2      Snapshot of state 2
1107  * @param previous       Pairs of blocks already compared on the current path (or NULL)
1108  * @param type_id        Type of variable
1109  * @param pointer_level
1110  * @return 0 (same), 1 (different), -1
1111  */
1112 int compare_heap_area(void *area1, void *area2, mc_snapshot_t snapshot1,
1113                       mc_snapshot_t snapshot2, xbt_dynar_t previous,
1114                       dw_type_t type, int pointer_level)
1115 {
1116
1117   struct s_mc_diff *state = mc_diff_info;
1118
1119   int res_compare;
1120   ssize_t block1, frag1, block2, frag2;
1121   ssize_t size;
1122   int check_ignore = 0;
1123
1124   void *real_addr_block1, *real_addr_block2, *real_addr_frag1, *real_addr_frag2;
1125
1126   int type_size = -1;
1127   int offset1 = 0, offset2 = 0;
1128   int new_size1 = -1, new_size2 = -1;
1129   dw_type_t new_type1 = NULL, new_type2 = NULL;
1130
1131   int match_pairs = 0;
1132
1133   malloc_info* heapinfos1 = mc_snapshot_read_pointer(&((xbt_mheap_t)std_heap)->heapinfo, snapshot1);
1134   malloc_info* heapinfos2 = mc_snapshot_read_pointer(&((xbt_mheap_t)std_heap)->heapinfo, snapshot2);
1135
1136   malloc_info heapinfo_temp1, heapinfo_temp2;
1137
1138   if (previous == NULL) {
1139     previous =
1140         xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
1141     match_pairs = 1;
1142   }
1143   // Get block number:
1144   block1 =
1145       ((char *) area1 -
1146        (char *) ((xbt_mheap_t) state->s_heap)->heapbase) / BLOCKSIZE + 1;
1147   block2 =
1148       ((char *) area2 -
1149        (char *) ((xbt_mheap_t) state->s_heap)->heapbase) / BLOCKSIZE + 1;
1150
1151   // If either block is a stack block:
1152   if (is_block_stack((int) block1) && is_block_stack((int) block2)) {
1153     add_heap_area_pair(previous, block1, -1, block2, -1);
1154     if (match_pairs) {
1155       match_equals(state, previous);
1156       xbt_dynar_free(&previous);
1157     }
1158     return 0;
1159   }
1160   // If either block is not in the expected area of memory:
1161   if (((char *) area1 < (char *) ((xbt_mheap_t) state->s_heap)->heapbase)
1162       || (block1 > state->heapsize1) || (block1 < 1)
1163       || ((char *) area2 < (char *) ((xbt_mheap_t) state->s_heap)->heapbase)
1164       || (block2 > state->heapsize2) || (block2 < 1)) {
1165     if (match_pairs) {
1166       xbt_dynar_free(&previous);
1167     }
1168     return 1;
1169   }
1170
1171   // Process address of the block:
1172   real_addr_block1 =
1173       ((void *) (((ADDR2UINT(block1)) - 1) * BLOCKSIZE +
1174                  (char *) ((xbt_mheap_t) state->s_heap)->heapbase));
1175   real_addr_block2 =
1176       ((void *) (((ADDR2UINT(block2)) - 1) * BLOCKSIZE +
1177                  (char *) ((xbt_mheap_t) state->s_heap)->heapbase));
1178
1179   if (type) {
1180
1181     if (type->full_type)
1182       type = type->full_type;
1183
1184     // This assume that for "boring" types (volatile ...) byte_size is absent:
1185     while (type->byte_size == 0 && type->subtype != NULL)
1186       type = type->subtype;
1187
1188     // Find type_size:
1189     if ((type->type == DW_TAG_pointer_type)
1190         || ((type->type == DW_TAG_base_type) && type->name != NULL
1191             && (!strcmp(type->name, "char"))))
1192       type_size = -1;
1193     else
1194       type_size = type->byte_size;
1195
1196   }
1197
1198   mc_mem_region_t heap_region1 = snapshot1->regions[0];
1199   mc_mem_region_t heap_region2 = snapshot2->regions[0];
1200
1201   malloc_info* heapinfo1 = mc_snapshot_read_region(&heapinfos1[block1], heap_region1, &heapinfo_temp1, sizeof(malloc_info));
1202   malloc_info* heapinfo2 = mc_snapshot_read_region(&heapinfos2[block2], heap_region2, &heapinfo_temp2, sizeof(malloc_info));
1203
1204   if ((heapinfo1->type == -1) && (heapinfo2->type == -1)) { /* Free block */
1205
1206     if (match_pairs) {
1207       match_equals(state, previous);
1208       xbt_dynar_free(&previous);
1209     }
1210     return 0;
1211
1212   } else if ((heapinfo1->type == 0) && (heapinfo2->type == 0)) {    /* Complete block */
1213
1214     // TODO, lookup variable type from block type as done for fragmented blocks
1215
1216     if (state->equals_to1_(block1, 0).valid
1217         && state->equals_to2_(block2, 0).valid) {
1218       if (equal_blocks(state, block1, block2)) {
1219         if (match_pairs) {
1220           match_equals(state, previous);
1221           xbt_dynar_free(&previous);
1222         }
1223         return 0;
1224       }
1225     }
1226
1227     if (type_size != -1) {
1228       if (type_size != heapinfo1->busy_block.busy_size
1229           && type_size != heapinfo2->busy_block.busy_size
1230           && type->name != NULL && !strcmp(type->name, "s_smx_context")) {
1231         if (match_pairs) {
1232           match_equals(state, previous);
1233           xbt_dynar_free(&previous);
1234         }
1235         return -1;
1236       }
1237     }
1238
1239     if (heapinfo1->busy_block.size !=
1240         heapinfo2->busy_block.size) {
1241       if (match_pairs) {
1242         xbt_dynar_free(&previous);
1243       }
1244       return 1;
1245     }
1246
1247     if (heapinfo1->busy_block.busy_size !=
1248         heapinfo2->busy_block.busy_size) {
1249       if (match_pairs) {
1250         xbt_dynar_free(&previous);
1251       }
1252       return 1;
1253     }
1254
1255     if (!add_heap_area_pair(previous, block1, -1, block2, -1)) {
1256       if (match_pairs) {
1257         match_equals(state, previous);
1258         xbt_dynar_free(&previous);
1259       }
1260       return 0;
1261     }
1262
1263     size = heapinfo1->busy_block.busy_size;
1264
1265     // Remember (basic) type inference.
1266     // The current data structure only allows us to do this for the whole block.
1267     if (type != NULL && area1 == real_addr_block1) {
1268       state->types1_(block1, 0) = type;
1269     }
1270     if (type != NULL && area2 == real_addr_block2) {
1271       state->types2_(block2, 0) = type;
1272     }
1273
1274     if (size <= 0) {
1275       if (match_pairs) {
1276         match_equals(state, previous);
1277         xbt_dynar_free(&previous);
1278       }
1279       return 0;
1280     }
1281
1282     frag1 = -1;
1283     frag2 = -1;
1284
1285     if ((heapinfo1->busy_block.ignore > 0)
1286         && (heapinfo2->busy_block.ignore ==
1287             heapinfo1->busy_block.ignore))
1288       check_ignore = heapinfo1->busy_block.ignore;
1289
1290   } else if ((heapinfo1->type > 0) && (heapinfo2->type > 0)) {      /* Fragmented block */
1291
1292     // Fragment number:
1293     frag1 =
1294         ((uintptr_t) (ADDR2UINT(area1) % (BLOCKSIZE))) >> heapinfo1->type;
1295     frag2 =
1296         ((uintptr_t) (ADDR2UINT(area2) % (BLOCKSIZE))) >> heapinfo2->type;
1297
1298     // Process address of the fragment:
1299     real_addr_frag1 =
1300         (void *) ((char *) real_addr_block1 +
1301                   (frag1 << ((xbt_mheap_t) state->s_heap)->heapinfo[block1].
1302                    type));
1303     real_addr_frag2 =
1304         (void *) ((char *) real_addr_block2 +
1305                   (frag2 << ((xbt_mheap_t) state->s_heap)->heapinfo[block2].
1306                    type));
1307
1308     // Check the size of the fragments against the size of the type:
1309     if (type_size != -1) {
1310       if (heapinfo1->busy_frag.frag_size[frag1] == -1
1311           || heapinfo2->busy_frag.frag_size[frag2] == -1) {
1312         if (match_pairs) {
1313           match_equals(state, previous);
1314           xbt_dynar_free(&previous);
1315         }
1316         return -1;
1317       }
1318       if (type_size != heapinfo1->busy_frag.frag_size[frag1]
1319           || type_size != heapinfo2->busy_frag.frag_size[frag2]) {
1320         if (match_pairs) {
1321           match_equals(state, previous);
1322           xbt_dynar_free(&previous);
1323         }
1324         return -1;
1325       }
1326     }
1327     // Check if the blocks are already matched together:
1328     if (state->equals_to1_(block1, frag1).valid
1329         && state->equals_to2_(block2, frag2).valid) {
1330       if (equal_fragments(state, block1, frag1, block2, frag2)) {
1331         if (match_pairs) {
1332           match_equals(state, previous);
1333           xbt_dynar_free(&previous);
1334         }
1335         return 0;
1336       }
1337     }
1338     // Compare the size of both fragments:
1339     if (heapinfo1->busy_frag.frag_size[frag1] !=
1340         heapinfo2->busy_frag.frag_size[frag2]) {
1341       if (type_size == -1) {
1342         if (match_pairs) {
1343           match_equals(state, previous);
1344           xbt_dynar_free(&previous);
1345         }
1346         return -1;
1347       } else {
1348         if (match_pairs) {
1349           xbt_dynar_free(&previous);
1350         }
1351         return 1;
1352       }
1353     }
1354     // Size of the fragment:
1355     size = heapinfo1->busy_frag.frag_size[frag1];
1356
1357     // Remember (basic) type inference.
1358     // The current data structure only allows us to do this for the whole block.
1359     if (type != NULL && area1 == real_addr_frag1) {
1360       state->types1_(block1, frag1) = type;
1361     }
1362     if (type != NULL && area2 == real_addr_frag2) {
1363       state->types2_(block2, frag2) = type;
1364     }
1365     // The type of the variable is already known:
1366     if (type) {
1367       new_type1 = type;
1368       new_type2 = type;
1369     }
1370     // Type inference from the block type.
1371     else if (state->types1_(block1, frag1) != NULL
1372              || state->types2_(block2, frag2) != NULL) {
1373
1374       offset1 = (char *) area1 - (char *) real_addr_frag1;
1375       offset2 = (char *) area2 - (char *) real_addr_frag2;
1376
1377       if (state->types1_(block1, frag1) != NULL
1378           && state->types2_(block2, frag2) != NULL) {
1379         new_type1 =
1380             get_offset_type(real_addr_frag1, state->types1_(block1, frag1),
1381                             offset1, size, snapshot1);
1382         new_type2 =
1383             get_offset_type(real_addr_frag2, state->types2_(block2, frag2),
1384                             offset1, size, snapshot2);
1385       } else if (state->types1_(block1, frag1) != NULL) {
1386         new_type1 =
1387             get_offset_type(real_addr_frag1, state->types1_(block1, frag1),
1388                             offset1, size, snapshot1);
1389         new_type2 =
1390             get_offset_type(real_addr_frag2, state->types1_(block1, frag1),
1391                             offset2, size, snapshot2);
1392       } else if (state->types2_(block2, frag2) != NULL) {
1393         new_type1 =
1394             get_offset_type(real_addr_frag1, state->types2_(block2, frag2),
1395                             offset1, size, snapshot1);
1396         new_type2 =
1397             get_offset_type(real_addr_frag2, state->types2_(block2, frag2),
1398                             offset2, size, snapshot2);
1399       } else {
1400         if (match_pairs) {
1401           match_equals(state, previous);
1402           xbt_dynar_free(&previous);
1403         }
1404         return -1;
1405       }
1406
1407       if (new_type1 != NULL && new_type2 != NULL && new_type1 != new_type2) {
1408
1409         type = new_type1;
1410         while (type->byte_size == 0 && type->subtype != NULL)
1411           type = type->subtype;
1412         new_size1 = type->byte_size;
1413
1414         type = new_type2;
1415         while (type->byte_size == 0 && type->subtype != NULL)
1416           type = type->subtype;
1417         new_size2 = type->byte_size;
1418
1419       } else {
1420         if (match_pairs) {
1421           match_equals(state, previous);
1422           xbt_dynar_free(&previous);
1423         }
1424         return -1;
1425       }
1426     }
1427
1428     if (new_size1 > 0 && new_size1 == new_size2) {
1429       type = new_type1;
1430       size = new_size1;
1431     }
1432
1433     if (offset1 == 0 && offset2 == 0) {
1434       if (!add_heap_area_pair(previous, block1, frag1, block2, frag2)) {
1435         if (match_pairs) {
1436           match_equals(state, previous);
1437           xbt_dynar_free(&previous);
1438         }
1439         return 0;
1440       }
1441     }
1442
1443     if (size <= 0) {
1444       if (match_pairs) {
1445         match_equals(state, previous);
1446         xbt_dynar_free(&previous);
1447       }
1448       return 0;
1449     }
1450
1451     if ((heapinfo1->busy_frag.ignore[frag1] > 0)
1452         && (heapinfo2->busy_frag.ignore[frag2] ==
1453             heapinfo1->busy_frag.ignore[frag1]))
1454       check_ignore = heapinfo1->busy_frag.ignore[frag1];
1455
1456   } else {
1457
1458     if (match_pairs) {
1459       xbt_dynar_free(&previous);
1460     }
1461     return 1;
1462
1463   }
1464
1465
1466   /* Start comparison */
1467   if (type) {
1468     res_compare =
1469         compare_heap_area_with_type(state, area1, area2, snapshot1, snapshot2,
1470                                     previous, type, size, check_ignore,
1471                                     pointer_level);
1472   } else {
1473     res_compare =
1474         compare_heap_area_without_type(state, area1, area2, snapshot1, snapshot2,
1475                                        previous, size, check_ignore);
1476   }
1477   if (res_compare == 1) {
1478     if (match_pairs)
1479       xbt_dynar_free(&previous);
1480     return res_compare;
1481   }
1482
1483   if (match_pairs) {
1484     match_equals(state, previous);
1485     xbt_dynar_free(&previous);
1486   }
1487
1488   return 0;
1489 }
1490
1491 /*********************************************** Miscellaneous ***************************************************/
1492 /****************************************************************************************************************/
1493
1494 // Not used and broken code:
1495 # if 0
1496
1497 // Not used:
1498 static int get_pointed_area_size(void *area, int heap)
1499 {
1500
1501   struct s_mc_diff *state = mc_diff_info;
1502
1503   int block, frag;
1504   malloc_info *heapinfo;
1505
1506   if (heap == 1)
1507     heapinfo = state->heapinfo1;
1508   else
1509     heapinfo = state->heapinfo2;
1510
1511   block =
1512       ((char *) area -
1513        (char *) ((xbt_mheap_t) state->s_heap)->heapbase) / BLOCKSIZE + 1;
1514
1515   if (((char *) area < (char *) ((xbt_mheap_t) state->s_heap)->heapbase)
1516       || (block > state->heapsize1) || (block < 1))
1517     return -1;
1518
1519   if (heapinfo[block].type == -1) {     /* Free block */
1520     return -1;
1521   } else if (heapinfo[block].type == 0) {       /* Complete block */
1522     return (int) heapinfo[block].busy_block.busy_size;
1523   } else {
1524     frag =
1525         ((uintptr_t) (ADDR2UINT(area) % (BLOCKSIZE))) >> heapinfo[block].type;
1526     return (int) heapinfo[block].busy_frag.frag_size[frag];
1527   }
1528 }
1529
1530 // Not used:
1531 char *get_type_description(mc_object_info_t info, char *type_name)
1532 {
1533
1534   xbt_dict_cursor_t dict_cursor;
1535   char *type_origin;
1536   dw_type_t type;
1537
1538   xbt_dict_foreach(info->types, dict_cursor, type_origin, type) {
1539     if (type->name && (strcmp(type->name, type_name) == 0)
1540         && type->byte_size > 0) {
1541       xbt_dict_cursor_free(&dict_cursor);
1542       return type_origin;
1543     }
1544   }
1545
1546   xbt_dict_cursor_free(&dict_cursor);
1547   return NULL;
1548 }
1549
1550
1551 #ifndef max
1552 #define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
1553 #endif
1554
1555 // Not used:
1556 int mmalloc_linear_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2)
1557 {
1558
1559   struct s_mc_diff *state = mc_diff_info;
1560
1561   if (heap1 == NULL && heap1 == NULL) {
1562     XBT_DEBUG("Malloc descriptors null");
1563     return 0;
1564   }
1565
1566   if (heap1->heaplimit != heap2->heaplimit) {
1567     XBT_DEBUG("Different limit of valid info table indices");
1568     return 1;
1569   }
1570
1571   /* Heap information */
1572   state->heaplimit = ((struct mdesc *) heap1)->heaplimit;
1573
1574
1575   // Mamailloute in order to find the base address of the main heap:
1576   state->s_heap =
1577       (char *) mmalloc_get_current_heap() - STD_HEAP_SIZE - xbt_pagesize;
1578
1579   state->heapbase1 = (char *) heap1 + BLOCKSIZE;
1580   state->heapbase2 = (char *) heap2 + BLOCKSIZE;
1581
1582   state->heapinfo1 =
1583       (malloc_info *) ((char *) heap1 +
1584                        ((uintptr_t)
1585                         ((char *) heap1->heapinfo - (char *) state->s_heap)));
1586   state->heapinfo2 =
1587       (malloc_info *) ((char *) heap2 +
1588                        ((uintptr_t)
1589                         ((char *) heap2->heapinfo - (char *) state->s_heap)));
1590
1591   state->heapsize1 = heap1->heapsize;
1592   state->heapsize2 = heap2->heapsize;
1593
1594   /* Start comparison */
1595   size_t i, j, k;
1596   void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
1597
1598   int distance = 0;
1599
1600   /* Check busy blocks */
1601
1602   i = 1;
1603
1604   while (i <= state->heaplimit) {
1605
1606     addr_block1 =
1607         ((void *) (((ADDR2UINT(i)) - 1) * BLOCKSIZE +
1608                    (char *) state->heapbase1));
1609     addr_block2 =
1610         ((void *) (((ADDR2UINT(i)) - 1) * BLOCKSIZE +
1611                    (char *) state->heapbase2));
1612
1613     if (state->heapinfo1[i].type != state->heapinfo2[i].type) {
1614
1615       distance += BLOCKSIZE;
1616       XBT_DEBUG("Different type of blocks (%zu) : %d - %d -> distance = %d", i,
1617                 state->heapinfo1[i].type, state->heapinfo2[i].type, distance);
1618       i++;
1619
1620     } else {
1621
1622       if (state->heapinfo1[i].type == -1) {     /* Free block */
1623         i++;
1624         continue;
1625       }
1626
1627       if (state->heapinfo1[i].type == 0) {      /* Large block */
1628
1629         if (state->heapinfo1[i].busy_block.size !=
1630             state->heapinfo2[i].busy_block.size) {
1631           distance +=
1632               BLOCKSIZE * max(state->heapinfo1[i].busy_block.size,
1633                               state->heapinfo2[i].busy_block.size);
1634           i += max(state->heapinfo1[i].busy_block.size,
1635                    state->heapinfo2[i].busy_block.size);
1636           XBT_DEBUG
1637               ("Different larger of cluster at block %zu : %zu - %zu -> distance = %d",
1638                i, state->heapinfo1[i].busy_block.size,
1639                state->heapinfo2[i].busy_block.size, distance);
1640           continue;
1641         }
1642
1643         /*if(heapinfo1[i].busy_block.busy_size != heapinfo2[i].busy_block.busy_size){
1644            distance += max(heapinfo1[i].busy_block.busy_size, heapinfo2[i].busy_block.busy_size);
1645            i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
1646            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);
1647            continue;
1648            } */
1649
1650         k = 0;
1651
1652         //while(k < (heapinfo1[i].busy_block.busy_size)){
1653         while (k < state->heapinfo1[i].busy_block.size * BLOCKSIZE) {
1654           if (memcmp((char *) addr_block1 + k, (char *) addr_block2 + k, 1) !=
1655               0) {
1656             distance++;
1657           }
1658           k++;
1659         }
1660
1661         i++;
1662
1663       } else {                  /* Fragmented block */
1664
1665         for (j = 0; j < (size_t) (BLOCKSIZE >> state->heapinfo1[i].type); j++) {
1666
1667           addr_frag1 =
1668               (void *) ((char *) addr_block1 + (j << state->heapinfo1[i].type));
1669           addr_frag2 =
1670               (void *) ((char *) addr_block2 + (j << state->heapinfo2[i].type));
1671
1672           if (state->heapinfo1[i].busy_frag.frag_size[j] == 0
1673               && state->heapinfo2[i].busy_frag.frag_size[j] == 0) {
1674             continue;
1675           }
1676
1677
1678           /*if(heapinfo1[i].busy_frag.frag_size[j] != heapinfo2[i].busy_frag.frag_size[j]){
1679              distance += max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j]);
1680              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); 
1681              continue;
1682              } */
1683
1684           k = 0;
1685
1686           //while(k < max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j])){
1687           while (k < (BLOCKSIZE / (BLOCKSIZE >> state->heapinfo1[i].type))) {
1688             if (memcmp((char *) addr_frag1 + k, (char *) addr_frag2 + k, 1) !=
1689                 0) {
1690               distance++;
1691             }
1692             k++;
1693           }
1694
1695         }
1696
1697         i++;
1698
1699       }
1700
1701     }
1702
1703   }
1704
1705   return distance;
1706
1707 }
1708 #endif