Logo AND Algorithmique Numérique Distribuée

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