Logo AND Algorithmique Numérique Distribuée

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