Logo AND Algorithmique Numérique Distribuée

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