Logo AND Algorithmique Numérique Distribuée

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