Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2566334954281ed62d4f089e2a739fb7fb148d2e
[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
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   mc_process_t process = &mc_model_checker->process;
773
774   int i = 0;
775   void *addr_pointed1, *addr_pointed2;
776   int pointer_align, res_compare;
777   ssize_t ignore1, ignore2;
778
779   mc_mem_region_t heap_region1 = snapshot1->regions[0];
780   mc_mem_region_t heap_region2 = snapshot2->regions[0];
781
782   while (i < size) {
783
784     if (check_ignore > 0) {
785       if ((ignore1 =
786            heap_comparison_ignore_size(state->to_ignore1,
787                                        (char *) real_area1 + i)) != -1) {
788         if ((ignore2 =
789              heap_comparison_ignore_size(state->to_ignore2,
790                                          (char *) real_area2 + i)) == ignore1) {
791           if (ignore1 == 0) {
792             check_ignore--;
793             return 0;
794           } else {
795             i = i + ignore2;
796             check_ignore--;
797             continue;
798           }
799         }
800       }
801     }
802
803     if (mc_snapshot_region_memcmp(((char *) real_area1) + i, heap_region1, ((char *) real_area2) + i, heap_region2, 1) != 0) {
804
805       pointer_align = (i / sizeof(void *)) * sizeof(void *);
806       addr_pointed1 = mc_snapshot_read_pointer((char *) real_area1 + pointer_align, snapshot1, process_index);
807       addr_pointed2 = mc_snapshot_read_pointer((char *) real_area2 + pointer_align, snapshot2, process_index);
808
809       if (addr_pointed1 > process->maestro_stack_start
810           && addr_pointed1 < process->maestro_stack_end
811           && addr_pointed2 > process->maestro_stack_start
812           && addr_pointed2 < process->maestro_stack_end) {
813         i = pointer_align + sizeof(void *);
814         continue;
815       } else if (addr_pointed1 > state->s_heap
816                  && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
817                  && addr_pointed2 > state->s_heap
818                  && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)) {
819         // Both addreses are in the heap:
820         res_compare =
821             compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
822                               snapshot2, previous, NULL, 0);
823         if (res_compare == 1) {
824           return res_compare;
825         }
826         i = pointer_align + sizeof(void *);
827         continue;
828       } else {
829         return 1;
830       }
831
832     }
833
834     i++;
835
836   }
837
838   return 0;
839
840 }
841
842 /**
843  *
844  * @param state
845  * @param real_area1     Process address for state 1
846  * @param real_area2     Process address for state 2
847  * @param snapshot1      Snapshot of state 1
848  * @param snapshot2      Snapshot of state 2
849  * @param previous
850  * @param type_id
851  * @param area_size      either a byte_size or an elements_count (?)
852  * @param check_ignore
853  * @param pointer_level
854  * @return               0 (same), 1 (different), -1 (unknown)
855  */
856 static int compare_heap_area_with_type(struct s_mc_diff *state, int process_index,
857                                        void *real_area1, void *real_area2,
858                                        mc_snapshot_t snapshot1,
859                                        mc_snapshot_t snapshot2,
860                                        xbt_dynar_t previous, dw_type_t type,
861                                        int area_size, int check_ignore,
862                                        int pointer_level)
863 {
864 top:
865   if (is_stack(real_area1) && is_stack(real_area2))
866     return 0;
867
868   ssize_t ignore1, ignore2;
869
870   if ((check_ignore > 0)
871       && ((ignore1 = heap_comparison_ignore_size(state->to_ignore1, real_area1))
872           > 0)
873       && ((ignore2 = heap_comparison_ignore_size(state->to_ignore2, real_area2))
874           == ignore1)) {
875     return 0;
876   }
877
878   dw_type_t subtype, subsubtype;
879   int res, elm_size, i;
880   unsigned int cursor = 0;
881   dw_type_t member;
882   void *addr_pointed1, *addr_pointed2;;
883
884   mc_mem_region_t heap_region1 = snapshot1->regions[0];
885   mc_mem_region_t heap_region2 = snapshot2->regions[0];
886
887   switch (type->type) {
888   case DW_TAG_unspecified_type:
889     return 1;
890
891   case DW_TAG_base_type:
892     if (type->name != NULL && strcmp(type->name, "char") == 0) {        /* String, hence random (arbitrary ?) size */
893       if (real_area1 == real_area2)
894         return -1;
895       else
896         return (mc_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, area_size) != 0);
897     } else {
898       if (area_size != -1 && type->byte_size != area_size)
899         return -1;
900       else {
901         return (mc_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0);
902       }
903     }
904     break;
905   case DW_TAG_enumeration_type:
906     if (area_size != -1 && type->byte_size != area_size)
907       return -1;
908     else
909       return (mc_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0);
910     break;
911   case DW_TAG_typedef:
912   case DW_TAG_const_type:
913   case DW_TAG_volatile_type:
914     // Poor man's TCO:
915     type = type->subtype;
916     goto top;
917     break;
918   case DW_TAG_array_type:
919     subtype = type->subtype;
920     switch (subtype->type) {
921     case DW_TAG_unspecified_type:
922       return 1;
923
924     case DW_TAG_base_type:
925     case DW_TAG_enumeration_type:
926     case DW_TAG_pointer_type:
927     case DW_TAG_reference_type:
928     case DW_TAG_rvalue_reference_type:
929     case DW_TAG_structure_type:
930     case DW_TAG_class_type:
931     case DW_TAG_union_type:
932       if (subtype->full_type)
933         subtype = subtype->full_type;
934       elm_size = subtype->byte_size;
935       break;
936       // TODO, just remove the type indirection?
937     case DW_TAG_const_type:
938     case DW_TAG_typedef:
939     case DW_TAG_volatile_type:
940       subsubtype = subtype->subtype;
941       if (subsubtype->full_type)
942         subsubtype = subsubtype->full_type;
943       elm_size = subsubtype->byte_size;
944       break;
945     default:
946       return 0;
947       break;
948     }
949     for (i = 0; i < type->element_count; i++) {
950       // TODO, add support for variable stride (DW_AT_byte_stride)
951       res =
952           compare_heap_area_with_type(state, process_index,
953                                       (char *) real_area1 + (i * elm_size),
954                                       (char *) real_area2 + (i * elm_size),
955                                       snapshot1, snapshot2, previous,
956                                       type->subtype, subtype->byte_size,
957                                       check_ignore, pointer_level);
958       if (res == 1)
959         return res;
960     }
961     break;
962   case DW_TAG_reference_type:
963   case DW_TAG_rvalue_reference_type:
964   case DW_TAG_pointer_type:
965     if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
966       addr_pointed1 = mc_snapshot_read_pointer(real_area1, snapshot1, process_index);
967       addr_pointed2 = mc_snapshot_read_pointer(real_area2, snapshot2, process_index);
968       return (addr_pointed1 != addr_pointed2);;
969     } else {
970       pointer_level++;
971       if (pointer_level > 1) {  /* Array of pointers */
972         for (i = 0; i < (area_size / sizeof(void *)); i++) {
973           addr_pointed1 = mc_snapshot_read_pointer((char*) real_area1 + i * sizeof(void *), snapshot1, process_index);
974           addr_pointed2 = mc_snapshot_read_pointer((char*) real_area2 + i * sizeof(void *), snapshot2, process_index);
975           if (addr_pointed1 > state->s_heap
976               && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
977               && addr_pointed2 > state->s_heap
978               && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
979             res =
980                 compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
981                                   snapshot2, previous, type->subtype,
982                                   pointer_level);
983           else
984             res = (addr_pointed1 != addr_pointed2);
985           if (res == 1)
986             return res;
987         }
988       } else {
989         addr_pointed1 = mc_snapshot_read_pointer(real_area1, snapshot1, process_index);
990         addr_pointed2 = mc_snapshot_read_pointer(real_area2, snapshot2, process_index);
991         if (addr_pointed1 > state->s_heap
992             && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
993             && addr_pointed2 > state->s_heap
994             && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
995           return compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
996                                    snapshot2, previous, type->subtype,
997                                    pointer_level);
998         else
999           return (addr_pointed1 != addr_pointed2);
1000       }
1001     }
1002     break;
1003   case DW_TAG_structure_type:
1004   case DW_TAG_class_type:
1005     if (type->full_type)
1006       type = type->full_type;
1007     if (area_size != -1 && type->byte_size != area_size) {
1008       if (area_size > type->byte_size && area_size % type->byte_size == 0) {
1009         for (i = 0; i < (area_size / type->byte_size); i++) {
1010           res =
1011               compare_heap_area_with_type(state, process_index,
1012                                           (char *) real_area1 + i * type->byte_size,
1013                                           (char *) real_area2 + i * type->byte_size,
1014                                           snapshot1, snapshot2, previous, type, -1,
1015                                           check_ignore, 0);
1016           if (res == 1)
1017             return res;
1018         }
1019       } else {
1020         return -1;
1021       }
1022     } else {
1023       cursor = 0;
1024       xbt_dynar_foreach(type->members, cursor, member) {
1025         // TODO, optimize this? (for the offset case)
1026         char *real_member1 =
1027             mc_member_resolve(real_area1, type, member, snapshot1, process_index);
1028         char *real_member2 =
1029             mc_member_resolve(real_area2, type, member, snapshot2, process_index);
1030         res =
1031             compare_heap_area_with_type(state, process_index, real_member1, real_member2,
1032                                         snapshot1, snapshot2,
1033                                         previous, member->subtype, -1,
1034                                         check_ignore, 0);
1035         if (res == 1) {
1036           return res;
1037         }
1038       }
1039     }
1040     break;
1041   case DW_TAG_union_type:
1042     return compare_heap_area_without_type(state, process_index, real_area1, real_area2,
1043                                           snapshot1, snapshot2, previous,
1044                                           type->byte_size, check_ignore);
1045     break;
1046   default:
1047     break;
1048   }
1049
1050   return 0;
1051
1052 }
1053
1054 /** Infer the type of a part of the block from the type of the block
1055  *
1056  * TODO, handle DW_TAG_array_type as well as arrays of the object ((*p)[5], p[5])
1057  *
1058  * TODO, handle subfields ((*p).bar.foo, (*p)[5].bar…)
1059  *
1060  * @param  type_id            DWARF type ID of the root address
1061  * @param  area_size
1062  * @return                    DWARF type ID for given offset
1063  */
1064 static dw_type_t get_offset_type(void *real_base_address, dw_type_t type,
1065                                  int offset, int area_size,
1066                                  mc_snapshot_t snapshot, int process_index)
1067 {
1068
1069   // Beginning of the block, the infered variable type if the type of the block:
1070   if (offset == 0)
1071     return type;
1072
1073   switch (type->type) {
1074   case DW_TAG_structure_type:
1075   case DW_TAG_class_type:
1076     if (type->full_type)
1077       type = type->full_type;
1078
1079     if (area_size != -1 && type->byte_size != area_size) {
1080       if (area_size > type->byte_size && area_size % type->byte_size == 0)
1081         return type;
1082       else
1083         return NULL;
1084     } else {
1085       unsigned int cursor = 0;
1086       dw_type_t member;
1087       xbt_dynar_foreach(type->members, cursor, member) {
1088
1089         if (!member->location.size) {
1090           // We have the offset, use it directly (shortcut):
1091           if (member->offset == offset)
1092             return member->subtype;
1093         } else {
1094           char *real_member =
1095               mc_member_resolve(real_base_address, type, member, snapshot, process_index);
1096           if (real_member - (char *) real_base_address == offset)
1097             return member->subtype;
1098         }
1099
1100       }
1101       return NULL;
1102     }
1103     break;
1104   default:
1105     /* FIXME : other cases ? */
1106     return NULL;
1107     break;
1108   }
1109 }
1110
1111 /**
1112  *
1113  * @param area1          Process address for state 1
1114  * @param area2          Process address for state 2
1115  * @param snapshot1      Snapshot of state 1
1116  * @param snapshot2      Snapshot of state 2
1117  * @param previous       Pairs of blocks already compared on the current path (or NULL)
1118  * @param type_id        Type of variable
1119  * @param pointer_level
1120  * @return 0 (same), 1 (different), -1
1121  */
1122 int compare_heap_area(int process_index, void *area1, void *area2, mc_snapshot_t snapshot1,
1123                       mc_snapshot_t snapshot2, xbt_dynar_t previous,
1124                       dw_type_t type, int pointer_level)
1125 {
1126
1127   struct s_mc_diff *state = mc_diff_info;
1128
1129   int res_compare;
1130   ssize_t block1, frag1, block2, frag2;
1131   ssize_t size;
1132   int check_ignore = 0;
1133
1134   void *real_addr_block1, *real_addr_block2, *real_addr_frag1, *real_addr_frag2;
1135   int type_size = -1;
1136   int offset1 = 0, offset2 = 0;
1137   int new_size1 = -1, new_size2 = -1;
1138   dw_type_t new_type1 = NULL, new_type2 = NULL;
1139
1140   int match_pairs = 0;
1141
1142   malloc_info* heapinfos1 = mc_snapshot_read_pointer(&std_heap->heapinfo, snapshot1, process_index);
1143   malloc_info* heapinfos2 = mc_snapshot_read_pointer(&std_heap->heapinfo, snapshot2, process_index);
1144
1145   malloc_info heapinfo_temp1, heapinfo_temp2;
1146
1147   if (previous == NULL) {
1148     previous =
1149         xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
1150     match_pairs = 1;
1151   }
1152   // Get block number:
1153   block1 =
1154       ((char *) area1 -
1155        (char *) ((xbt_mheap_t) state->s_heap)->heapbase) / BLOCKSIZE + 1;
1156   block2 =
1157       ((char *) area2 -
1158        (char *) ((xbt_mheap_t) state->s_heap)->heapbase) / BLOCKSIZE + 1;
1159
1160   // If either block is a stack block:
1161   if (is_block_stack((int) block1) && is_block_stack((int) block2)) {
1162     add_heap_area_pair(previous, block1, -1, block2, -1);
1163     if (match_pairs) {
1164       match_equals(state, previous);
1165       xbt_dynar_free(&previous);
1166     }
1167     return 0;
1168   }
1169   // If either block is not in the expected area of memory:
1170   if (((char *) area1 < (char *) ((xbt_mheap_t) state->s_heap)->heapbase)
1171       || (block1 > state->heapsize1) || (block1 < 1)
1172       || ((char *) area2 < (char *) ((xbt_mheap_t) state->s_heap)->heapbase)
1173       || (block2 > state->heapsize2) || (block2 < 1)) {
1174     if (match_pairs) {
1175       xbt_dynar_free(&previous);
1176     }
1177     return 1;
1178   }
1179
1180   // Process address of the block:
1181   real_addr_block1 =
1182       ((void *) (((ADDR2UINT(block1)) - 1) * BLOCKSIZE +
1183                  (char *) ((xbt_mheap_t) state->s_heap)->heapbase));
1184   real_addr_block2 =
1185       ((void *) (((ADDR2UINT(block2)) - 1) * BLOCKSIZE +
1186                  (char *) ((xbt_mheap_t) state->s_heap)->heapbase));
1187
1188   if (type) {
1189
1190     if (type->full_type)
1191       type = type->full_type;
1192
1193     // This assume that for "boring" types (volatile ...) byte_size is absent:
1194     while (type->byte_size == 0 && type->subtype != NULL)
1195       type = type->subtype;
1196
1197     // Find type_size:
1198     if ((type->type == DW_TAG_pointer_type)
1199         || ((type->type == DW_TAG_base_type) && type->name != NULL
1200             && (!strcmp(type->name, "char"))))
1201       type_size = -1;
1202     else
1203       type_size = type->byte_size;
1204
1205   }
1206
1207   mc_mem_region_t heap_region1 = snapshot1->regions[0];
1208   mc_mem_region_t heap_region2 = snapshot2->regions[0];
1209
1210   malloc_info* heapinfo1 = mc_snapshot_read_region(&heapinfos1[block1], heap_region1, &heapinfo_temp1, sizeof(malloc_info));
1211   malloc_info* heapinfo2 = mc_snapshot_read_region(&heapinfos2[block2], heap_region2, &heapinfo_temp2, sizeof(malloc_info));
1212
1213   if ((heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type==MMALLOC_TYPE_HEAPINFO)
1214     && (heapinfo2->type == MMALLOC_TYPE_FREE || heapinfo2->type ==MMALLOC_TYPE_HEAPINFO)) {
1215
1216     /* Free block */
1217     if (match_pairs) {
1218       match_equals(state, previous);
1219       xbt_dynar_free(&previous);
1220     }
1221     return 0;
1222
1223   } else if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED
1224     && heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
1225     /* Complete block */
1226
1227     // TODO, lookup variable type from block type as done for fragmented blocks
1228
1229     offset1 = (char *) area1 - (char *) real_addr_block1;
1230     offset2 = (char *) area2 - (char *) real_addr_block2;
1231
1232     if (state->equals_to1_(block1, 0).valid
1233         && state->equals_to2_(block2, 0).valid) {
1234       if (equal_blocks(state, block1, block2)) {
1235         if (match_pairs) {
1236           match_equals(state, previous);
1237           xbt_dynar_free(&previous);
1238         }
1239         return 0;
1240       }
1241     }
1242
1243     if (type_size != -1) {
1244       if (type_size != heapinfo1->busy_block.busy_size
1245           && type_size != heapinfo2->busy_block.busy_size
1246           && (type->name == NULL || !strcmp(type->name, "struct s_smx_context"))) {
1247         if (match_pairs) {
1248           match_equals(state, previous);
1249           xbt_dynar_free(&previous);
1250         }
1251         return -1;
1252       }
1253     }
1254
1255     if (heapinfo1->busy_block.size !=
1256         heapinfo2->busy_block.size) {
1257       if (match_pairs) {
1258         xbt_dynar_free(&previous);
1259       }
1260       return 1;
1261     }
1262
1263     if (heapinfo1->busy_block.busy_size !=
1264         heapinfo2->busy_block.busy_size) {
1265       if (match_pairs) {
1266         xbt_dynar_free(&previous);
1267       }
1268       return 1;
1269     }
1270
1271     if (!add_heap_area_pair(previous, block1, -1, block2, -1)) {
1272       if (match_pairs) {
1273         match_equals(state, previous);
1274         xbt_dynar_free(&previous);
1275       }
1276       return 0;
1277     }
1278
1279     size = heapinfo1->busy_block.busy_size;
1280
1281     // Remember (basic) type inference.
1282     // The current data structure only allows us to do this for the whole block.
1283     if (type != NULL && area1 == real_addr_block1) {
1284       state->types1_(block1, 0) = type;
1285     }
1286     if (type != NULL && area2 == real_addr_block2) {
1287       state->types2_(block2, 0) = type;
1288     }
1289
1290     if (size <= 0) {
1291       if (match_pairs) {
1292         match_equals(state, previous);
1293         xbt_dynar_free(&previous);
1294       }
1295       return 0;
1296     }
1297
1298     frag1 = -1;
1299     frag2 = -1;
1300
1301     if ((heapinfo1->busy_block.ignore > 0)
1302         && (heapinfo2->busy_block.ignore ==
1303             heapinfo1->busy_block.ignore))
1304       check_ignore = heapinfo1->busy_block.ignore;
1305
1306   } else if ((heapinfo1->type > 0) && (heapinfo2->type > 0)) {      /* Fragmented block */
1307
1308     // Fragment number:
1309     frag1 =
1310         ((uintptr_t) (ADDR2UINT(area1) % (BLOCKSIZE))) >> heapinfo1->type;
1311     frag2 =
1312         ((uintptr_t) (ADDR2UINT(area2) % (BLOCKSIZE))) >> heapinfo2->type;
1313
1314     // Process address of the fragment:
1315     real_addr_frag1 =
1316         (void *) ((char *) real_addr_block1 +
1317                   (frag1 << heapinfo1->type));
1318     real_addr_frag2 =
1319         (void *) ((char *) real_addr_block2 +
1320                   (frag2 << heapinfo2->type));
1321
1322     // Check the size of the fragments against the size of the type:
1323     if (type_size != -1) {
1324       if (heapinfo1->busy_frag.frag_size[frag1] == -1
1325           || heapinfo2->busy_frag.frag_size[frag2] == -1) {
1326         if (match_pairs) {
1327           match_equals(state, previous);
1328           xbt_dynar_free(&previous);
1329         }
1330         return -1;
1331       }
1332       // ?
1333       if (type_size != heapinfo1->busy_frag.frag_size[frag1]
1334           || type_size != heapinfo2->busy_frag.frag_size[frag2]) {
1335         if (match_pairs) {
1336           match_equals(state, previous);
1337           xbt_dynar_free(&previous);
1338         }
1339         return -1;
1340       }
1341     }
1342
1343     // Check if the blocks are already matched together:
1344     if (state->equals_to1_(block1, frag1).valid
1345         && state->equals_to2_(block2, frag2).valid) {
1346       if (offset1==offset2 && equal_fragments(state, block1, frag1, block2, frag2)) {
1347         if (match_pairs) {
1348           match_equals(state, previous);
1349           xbt_dynar_free(&previous);
1350         }
1351         return 0;
1352       }
1353     }
1354     // Compare the size of both fragments:
1355     if (heapinfo1->busy_frag.frag_size[frag1] !=
1356         heapinfo2->busy_frag.frag_size[frag2]) {
1357       if (type_size == -1) {
1358         if (match_pairs) {
1359           match_equals(state, previous);
1360           xbt_dynar_free(&previous);
1361         }
1362         return -1;
1363       } else {
1364         if (match_pairs) {
1365           xbt_dynar_free(&previous);
1366         }
1367         return 1;
1368       }
1369     }
1370
1371     // Size of the fragment:
1372     size = heapinfo1->busy_frag.frag_size[frag1];
1373
1374     // Remember (basic) type inference.
1375     // The current data structure only allows us to do this for the whole fragment.
1376     if (type != NULL && area1 == real_addr_frag1) {
1377       state->types1_(block1, frag1) = type;
1378     }
1379     if (type != NULL && area2 == real_addr_frag2) {
1380       state->types2_(block2, frag2) = type;
1381     }
1382     // The type of the variable is already known:
1383     if (type) {
1384       new_type1 = type;
1385       new_type2 = type;
1386     }
1387     // Type inference from the block type.
1388     else if (state->types1_(block1, frag1) != NULL
1389              || state->types2_(block2, frag2) != NULL) {
1390
1391       offset1 = (char *) area1 - (char *) real_addr_frag1;
1392       offset2 = (char *) area2 - (char *) real_addr_frag2;
1393
1394       if (state->types1_(block1, frag1) != NULL
1395           && state->types2_(block2, frag2) != NULL) {
1396         new_type1 =
1397             get_offset_type(real_addr_frag1, state->types1_(block1, frag1),
1398                             offset1, size, snapshot1, process_index);
1399         new_type2 =
1400             get_offset_type(real_addr_frag2, state->types2_(block2, frag2),
1401                             offset1, size, snapshot2, process_index);
1402       } else if (state->types1_(block1, frag1) != NULL) {
1403         new_type1 =
1404             get_offset_type(real_addr_frag1, state->types1_(block1, frag1),
1405                             offset1, size, snapshot1, process_index);
1406         new_type2 =
1407             get_offset_type(real_addr_frag2, state->types1_(block1, frag1),
1408                             offset2, size, snapshot2, process_index);
1409       } else if (state->types2_(block2, frag2) != NULL) {
1410         new_type1 =
1411             get_offset_type(real_addr_frag1, state->types2_(block2, frag2),
1412                             offset1, size, snapshot1, process_index);
1413         new_type2 =
1414             get_offset_type(real_addr_frag2, state->types2_(block2, frag2),
1415                             offset2, size, snapshot2, process_index);
1416       } else {
1417         if (match_pairs) {
1418           match_equals(state, previous);
1419           xbt_dynar_free(&previous);
1420         }
1421         return -1;
1422       }
1423
1424       if (new_type1 != NULL && new_type2 != NULL && new_type1 != new_type2) {
1425
1426         type = new_type1;
1427         while (type->byte_size == 0 && type->subtype != NULL)
1428           type = type->subtype;
1429         new_size1 = type->byte_size;
1430
1431         type = new_type2;
1432         while (type->byte_size == 0 && type->subtype != NULL)
1433           type = type->subtype;
1434         new_size2 = type->byte_size;
1435
1436       } else {
1437         if (match_pairs) {
1438           match_equals(state, previous);
1439           xbt_dynar_free(&previous);
1440         }
1441         return -1;
1442       }
1443     }
1444
1445     if (new_size1 > 0 && new_size1 == new_size2) {
1446       type = new_type1;
1447       size = new_size1;
1448     }
1449
1450     if (offset1 == 0 && offset2 == 0) {
1451       if (!add_heap_area_pair(previous, block1, frag1, block2, frag2)) {
1452         if (match_pairs) {
1453           match_equals(state, previous);
1454           xbt_dynar_free(&previous);
1455         }
1456         return 0;
1457       }
1458     }
1459
1460     if (size <= 0) {
1461       if (match_pairs) {
1462         match_equals(state, previous);
1463         xbt_dynar_free(&previous);
1464       }
1465       return 0;
1466     }
1467
1468     if ((heapinfo1->busy_frag.ignore[frag1] > 0)
1469         && (heapinfo2->busy_frag.ignore[frag2] ==
1470             heapinfo1->busy_frag.ignore[frag1]))
1471       check_ignore = heapinfo1->busy_frag.ignore[frag1];
1472
1473   } else {
1474
1475     if (match_pairs) {
1476       xbt_dynar_free(&previous);
1477     }
1478     return 1;
1479
1480   }
1481
1482
1483   /* Start comparison */
1484   if (type) {
1485     res_compare =
1486         compare_heap_area_with_type(state, process_index, area1, area2, snapshot1, snapshot2,
1487                                     previous, type, size, check_ignore,
1488                                     pointer_level);
1489   } else {
1490     res_compare =
1491         compare_heap_area_without_type(state, process_index, area1, area2, snapshot1, snapshot2,
1492                                        previous, size, check_ignore);
1493   }
1494   if (res_compare == 1) {
1495     if (match_pairs)
1496       xbt_dynar_free(&previous);
1497     return res_compare;
1498   }
1499
1500   if (match_pairs) {
1501     match_equals(state, previous);
1502     xbt_dynar_free(&previous);
1503   }
1504
1505   return 0;
1506 }
1507
1508 /*********************************************** Miscellaneous ***************************************************/
1509 /****************************************************************************************************************/
1510
1511 // Not used and broken code:
1512 # if 0
1513
1514 // Not used:
1515 static int get_pointed_area_size(void *area, int heap)
1516 {
1517
1518   struct s_mc_diff *state = mc_diff_info;
1519
1520   int block, frag;
1521   malloc_info *heapinfo;
1522
1523   if (heap == 1)
1524     heapinfo = state->heapinfo1;
1525   else
1526     heapinfo = state->heapinfo2;
1527
1528   block =
1529       ((char *) area -
1530        (char *) ((xbt_mheap_t) state->s_heap)->heapbase) / BLOCKSIZE + 1;
1531
1532   if (((char *) area < (char *) ((xbt_mheap_t) state->s_heap)->heapbase)
1533       || (block > state->heapsize1) || (block < 1))
1534     return -1;
1535
1536   if (heapinfo[block].type == MMALLOC_TYPE_FREE || heapinfo[block].type == MMALLOC_TYPE_HEAPINFO) {     /* Free block */
1537     return -1;
1538   } else if (heapinfo[block].type == MMALLOC_TYPE_UNFRAGMENTED) {       /* Complete block */
1539     return (int) heapinfo[block].busy_block.busy_size;
1540   } else {
1541     frag =
1542         ((uintptr_t) (ADDR2UINT(area) % (BLOCKSIZE))) >> heapinfo[block].type;
1543     return (int) heapinfo[block].busy_frag.frag_size[frag];
1544   }
1545 }
1546
1547 // Not used:
1548 char *get_type_description(mc_object_info_t info, char *type_name)
1549 {
1550
1551   xbt_dict_cursor_t dict_cursor;
1552   char *type_origin;
1553   dw_type_t type;
1554
1555   xbt_dict_foreach(info->types, dict_cursor, type_origin, type) {
1556     if (type->name && (strcmp(type->name, type_name) == 0)
1557         && type->byte_size > 0) {
1558       xbt_dict_cursor_free(&dict_cursor);
1559       return type_origin;
1560     }
1561   }
1562
1563   xbt_dict_cursor_free(&dict_cursor);
1564   return NULL;
1565 }
1566
1567
1568 #ifndef max
1569 #define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
1570 #endif
1571
1572 // Not used:
1573 int mmalloc_linear_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2)
1574 {
1575
1576   struct s_mc_diff *state = mc_diff_info;
1577
1578   if (heap1 == NULL && heap1 == NULL) {
1579     XBT_DEBUG("Malloc descriptors null");
1580     return 0;
1581   }
1582
1583   if (heap1->heaplimit != heap2->heaplimit) {
1584     XBT_DEBUG("Different limit of valid info table indices");
1585     return 1;
1586   }
1587
1588   /* Heap information */
1589   state->heaplimit = ((struct mdesc *) heap1)->heaplimit;
1590
1591
1592   // Mamailloute in order to find the base address of the main heap:
1593   state->s_heap =
1594       (char *) mmalloc_get_current_heap() - STD_HEAP_SIZE - xbt_pagesize;
1595
1596   state->heapbase1 = (char *) heap1 + BLOCKSIZE;
1597   state->heapbase2 = (char *) heap2 + BLOCKSIZE;
1598
1599   state->heapinfo1 =
1600       (malloc_info *) ((char *) heap1 +
1601                        ((uintptr_t)
1602                         ((char *) heap1->heapinfo - (char *) state->s_heap)));
1603   state->heapinfo2 =
1604       (malloc_info *) ((char *) heap2 +
1605                        ((uintptr_t)
1606                         ((char *) heap2->heapinfo - (char *) state->s_heap)));
1607
1608   state->heapsize1 = heap1->heapsize;
1609   state->heapsize2 = heap2->heapsize;
1610
1611   /* Start comparison */
1612   size_t i, j, k;
1613   void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
1614
1615   int distance = 0;
1616
1617   /* Check busy blocks */
1618
1619   i = 1;
1620
1621   while (i <= state->heaplimit) {
1622
1623     addr_block1 =
1624         ((void *) (((ADDR2UINT(i)) - 1) * BLOCKSIZE +
1625                    (char *) state->heapbase1));
1626     addr_block2 =
1627         ((void *) (((ADDR2UINT(i)) - 1) * BLOCKSIZE +
1628                    (char *) state->heapbase2));
1629
1630     if (state->heapinfo1[i].type != state->heapinfo2[i].type) {
1631
1632       distance += BLOCKSIZE;
1633       XBT_DEBUG("Different type of blocks (%zu) : %d - %d -> distance = %d", i,
1634                 state->heapinfo1[i].type, state->heapinfo2[i].type, distance);
1635       i++;
1636
1637     } else {
1638
1639       if (state->heapinfo1[i].type == MMALLOC_TYPE_FREE
1640         || state->heapinfo1[i].type == MMALLOC_TYPE_HAPINFO) {     /* Free block */
1641         i++;
1642         continue;
1643       }
1644
1645       if (state->heapinfo1[i].type == MMALLOC_TYPE_UNFRAGMENTED) {      /* Large block */
1646
1647         if (state->heapinfo1[i].busy_block.size !=
1648             state->heapinfo2[i].busy_block.size) {
1649           distance +=
1650               BLOCKSIZE * max(state->heapinfo1[i].busy_block.size,
1651                               state->heapinfo2[i].busy_block.size);
1652           i += max(state->heapinfo1[i].busy_block.size,
1653                    state->heapinfo2[i].busy_block.size);
1654           XBT_DEBUG
1655               ("Different larger of cluster at block %zu : %zu - %zu -> distance = %d",
1656                i, state->heapinfo1[i].busy_block.size,
1657                state->heapinfo2[i].busy_block.size, distance);
1658           continue;
1659         }
1660
1661         /*if(heapinfo1[i].busy_block.busy_size != heapinfo2[i].busy_block.busy_size){
1662            distance += max(heapinfo1[i].busy_block.busy_size, heapinfo2[i].busy_block.busy_size);
1663            i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
1664            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);
1665            continue;
1666            } */
1667
1668         k = 0;
1669
1670         //while(k < (heapinfo1[i].busy_block.busy_size)){
1671         while (k < state->heapinfo1[i].busy_block.size * BLOCKSIZE) {
1672           if (memcmp((char *) addr_block1 + k, (char *) addr_block2 + k, 1) !=
1673               0) {
1674             distance++;
1675           }
1676           k++;
1677         }
1678
1679         i++;
1680
1681       } else {                  /* Fragmented block */
1682
1683         for (j = 0; j < (size_t) (BLOCKSIZE >> state->heapinfo1[i].type); j++) {
1684
1685           addr_frag1 =
1686               (void *) ((char *) addr_block1 + (j << state->heapinfo1[i].type));
1687           addr_frag2 =
1688               (void *) ((char *) addr_block2 + (j << state->heapinfo2[i].type));
1689
1690           if (state->heapinfo1[i].busy_frag.frag_size[j] == 0
1691               && state->heapinfo2[i].busy_frag.frag_size[j] == 0) {
1692             continue;
1693           }
1694
1695
1696           /*if(heapinfo1[i].busy_frag.frag_size[j] != heapinfo2[i].busy_frag.frag_size[j]){
1697              distance += max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j]);
1698              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); 
1699              continue;
1700              } */
1701
1702           k = 0;
1703
1704           //while(k < max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j])){
1705           while (k < (BLOCKSIZE / (BLOCKSIZE >> state->heapinfo1[i].type))) {
1706             if (memcmp((char *) addr_frag1 + k, (char *) addr_frag2 + k, 1) !=
1707                 0) {
1708               distance++;
1709             }
1710             k++;
1711           }
1712
1713         }
1714
1715         i++;
1716
1717       }
1718
1719     }
1720
1721   }
1722
1723   return distance;
1724
1725 }
1726 #endif