Logo AND Algorithmique Numérique Distribuée

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