Logo AND Algorithmique Numérique Distribuée

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