Logo AND Algorithmique Numérique Distribuée

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