Logo AND Algorithmique Numérique Distribuée

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