Logo AND Algorithmique Numérique Distribuée

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