Logo AND Algorithmique Numérique Distribuée

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