Logo AND Algorithmique Numérique Distribuée

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