Logo AND Algorithmique Numérique Distribuée

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