Logo AND Algorithmique Numérique Distribuée

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