Logo AND Algorithmique Numérique Distribuée

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