Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Remove useless header file
[simgrid.git] / src / mc / compare.cpp
1 /* Copyright (c) 2008-2016. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 /** \file mc_compare.cpp Memory snapshooting and comparison                 */
8
9 #include <cinttypes>
10
11 #include <array>
12 #include <memory>
13 #include <utility>
14 #include <unordered_set>
15
16 #include <xbt/sysdep.h>
17 #include <xbt/dynar.h>
18 #include <xbt/mmalloc.h>
19
20 #include <mc/mc.h>
21 #include <mc/datatypes.h>
22
23 #include "src/internal_config.h"
24
25 #include "src/xbt/mmalloc/mmprivate.h"
26 #include "src/xbt/ex_interface.h"
27
28 #if HAVE_SMPI
29 #include "src/smpi/private.h"
30 #endif
31
32 #include "src/mc/mc_forward.hpp"
33 #include "src/mc/mc_safety.h"
34 #include "src/mc/mc_private.h"
35 #include "src/mc/mc_smx.h"
36 #include "src/mc/mc_dwarf.hpp"
37 #include "src/mc/Frame.hpp"
38 #include "src/mc/ObjectInformation.hpp"
39 #include "src/mc/Variable.hpp"
40 #include "src/mc/mc_private.h"
41 #include "src/mc/mc_snapshot.h"
42 #include "src/mc/mc_dwarf.hpp"
43 #include "src/mc/Type.hpp"
44
45 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_compare, xbt,
46                                 "Logging specific to mc_compare in mc");
47
48 namespace simgrid {
49 namespace mc {
50
51 struct ProcessComparisonState;
52 struct StateComparator;
53
54 static int compare_heap_area(
55   int process_index, const void *area1, const void* area2,
56   Snapshot* snapshot1, Snapshot* snapshot2,
57   xbt_dynar_t previous, Type* type, int pointer_level);
58
59 static void reset_heap_information(void);
60
61 }
62 }
63
64 using simgrid::mc::remote;
65
66 /*********************************** Heap comparison ***********************************/
67 /***************************************************************************************/
68
69 namespace simgrid {
70 namespace mc {
71
72 struct ProcessComparisonState {
73   std::vector<simgrid::mc::IgnoredHeapRegion>* to_ignore = nullptr;
74   std::vector<s_heap_area_t> equals_to;
75   std::vector<simgrid::mc::Type*> types;
76   std::size_t heapsize = 0;
77
78   void initHeapInformation(xbt_mheap_t heap,
79                           std::vector<simgrid::mc::IgnoredHeapRegion>* i);
80 };
81
82 struct StateComparator {
83   s_xbt_mheap_t std_heap_copy;
84   std::size_t heaplimit;
85   std::array<ProcessComparisonState, 2> processStates;
86
87   int initHeapInformation(
88     xbt_mheap_t heap1, xbt_mheap_t heap2,
89     std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
90     std::vector<simgrid::mc::IgnoredHeapRegion>* i2);
91
92   s_heap_area_t& equals_to1_(std::size_t i, std::size_t j)
93   {
94     return processStates[0].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
95   }
96   s_heap_area_t& equals_to2_(std::size_t i, std::size_t j)
97   {
98     return processStates[1].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
99   }
100   Type*& types1_(std::size_t i, std::size_t j)
101   {
102     return processStates[0].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
103   }
104   Type*& types2_(std::size_t i, std::size_t j)
105   {
106     return processStates[1].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
107   }
108
109   s_heap_area_t const& equals_to1_(std::size_t i, std::size_t j) const
110   {
111     return processStates[0].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
112   }
113   s_heap_area_t const& equals_to2_(std::size_t i, std::size_t j) const
114   {
115     return processStates[1].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
116   }
117   Type* const& types1_(std::size_t i, std::size_t j) const
118   {
119     return processStates[0].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
120   }
121   Type* const& types2_(std::size_t i, std::size_t j) const
122   {
123     return processStates[1].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
124   }
125
126   /** Check whether two blocks are known to be matching
127    *
128    *  @param state  State used
129    *  @param b1     Block of state 1
130    *  @param b2     Block of state 2
131    *  @return       if the blocks are known to be matching
132    */
133   bool blocksEqual(int b1, int b2) const
134   {
135     return this->equals_to1_(b1, 0).block == b2
136         && this->equals_to2_(b2, 0).block == b1;
137   }
138
139   /** Check whether two fragments are known to be matching
140    *
141    *  @param state  State used
142    *  @param b1     Block of state 1
143    *  @param f1     Fragment of state 1
144    *  @param b2     Block of state 2
145    *  @param f2     Fragment of state 2
146    *  @return       if the fragments are known to be matching
147    */
148   int fragmentsEqual(int b1, int f1, int b2, int f2) const
149   {
150     return this->equals_to1_(b1, f1).block == b2
151         && this->equals_to1_(b1, f1).fragment == f2
152         && this->equals_to2_(b2, f2).block == b1
153         && this->equals_to2_(b2, f2).fragment == f1;
154   }
155
156   void match_equals(xbt_dynar_t list);
157 };
158
159 }
160 }
161
162 // TODO, make this a field of ModelChecker or something similar
163 static std::unique_ptr<simgrid::mc::StateComparator> mc_diff_info;
164
165 /*********************************** Free functions ************************************/
166
167 static void heap_area_pair_free(heap_area_pair_t pair)
168 {
169   xbt_free(pair);
170   pair = nullptr;
171 }
172
173 static void heap_area_pair_free_voidp(void *d)
174 {
175   heap_area_pair_free((heap_area_pair_t) * (void **) d);
176 }
177
178 static void heap_area_free(heap_area_t area)
179 {
180   xbt_free(area);
181   area = nullptr;
182 }
183
184 /************************************************************************************/
185
186 static s_heap_area_t make_heap_area(int block, int fragment)
187 {
188   s_heap_area_t area;
189   area.valid = 1;
190   area.block = block;
191   area.fragment = fragment;
192   return area;
193 }
194
195 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   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     return 0;
217
218   heap_area_pair_t pair = nullptr;
219   pair = xbt_new0(s_heap_area_pair_t, 1);
220   pair->block1 = block1;
221   pair->fragment1 = fragment1;
222   pair->block2 = block2;
223   pair->fragment2 = fragment2;
224   xbt_dynar_push(list, &pair);
225   return 1;
226 }
227
228 static ssize_t heap_comparison_ignore_size(
229   std::vector<simgrid::mc::IgnoredHeapRegion>* ignore_list,
230   const void *address)
231 {
232   int start = 0;
233   int end = ignore_list->size() - 1;
234
235   while (start <= end) {
236     unsigned int cursor = (start + end) / 2;
237     simgrid::mc::IgnoredHeapRegion const& region = (*ignore_list)[cursor];
238     if (region.address == address)
239       return region.size;
240     if (region.address < address)
241       start = cursor + 1;
242     if (region.address > address)
243       end = cursor - 1;
244   }
245
246   return -1;
247 }
248
249 static bool is_stack(const void *address)
250 {
251   for (auto const& stack : mc_model_checker->process().stack_areas())
252     if (address == stack.address)
253       return true;
254   return false;
255 }
256
257 // TODO, this should depend on the snapshot?
258 static bool is_block_stack(int block)
259 {
260   for (auto const& stack : mc_model_checker->process().stack_areas())
261     if (block == stack.block)
262       return true;
263   return false;
264 }
265
266 namespace simgrid {
267 namespace mc {
268
269 void StateComparator::match_equals(xbt_dynar_t list)
270 {
271   unsigned int cursor = 0;
272   heap_area_pair_t current_pair;
273
274   xbt_dynar_foreach(list, cursor, current_pair) {
275     if (current_pair->fragment1 != -1) {
276       this->equals_to1_(current_pair->block1, current_pair->fragment1) =
277           make_heap_area(current_pair->block2, current_pair->fragment2);
278       this->equals_to2_(current_pair->block2, current_pair->fragment2) =
279           make_heap_area(current_pair->block1, current_pair->fragment1);
280     } else {
281       this->equals_to1_(current_pair->block1, 0) =
282           make_heap_area(current_pair->block2, current_pair->fragment2);
283       this->equals_to2_(current_pair->block2, 0) =
284           make_heap_area(current_pair->block1, current_pair->fragment1);
285     }
286   }
287 }
288
289 void ProcessComparisonState::initHeapInformation(xbt_mheap_t heap,
290                         std::vector<simgrid::mc::IgnoredHeapRegion>* i)
291 {
292   auto heaplimit = ((struct mdesc *) heap)->heaplimit;
293   this->heapsize = ((struct mdesc *) heap)->heapsize;
294   this->to_ignore = i;
295   this->equals_to.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, s_heap_area {0, 0, 0});
296   this->types.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, nullptr);
297 }
298
299 int StateComparator::initHeapInformation(xbt_mheap_t heap1, xbt_mheap_t heap2,
300                           std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
301                           std::vector<simgrid::mc::IgnoredHeapRegion>* i2)
302 {
303   if ((((struct mdesc *) heap1)->heaplimit !=
304        ((struct mdesc *) heap2)->heaplimit)
305       ||
306       ((((struct mdesc *) heap1)->heapsize !=
307         ((struct mdesc *) heap2)->heapsize)))
308     return -1;
309   this->heaplimit = ((struct mdesc *) heap1)->heaplimit;
310   this->std_heap_copy = *mc_model_checker->process().get_heap();
311   this->processStates[0].initHeapInformation(heap1, i1);
312   this->processStates[1].initHeapInformation(heap2, i2);
313   return 0;
314 }
315
316 static
317 int init_heap_information(xbt_mheap_t heap1, xbt_mheap_t heap2,
318                           std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
319                           std::vector<simgrid::mc::IgnoredHeapRegion>* i2)
320 {
321   if (mc_diff_info == nullptr)
322     mc_diff_info = std::unique_ptr<StateComparator>(new StateComparator());
323   return mc_diff_info->initHeapInformation(heap1, heap2, i1, i2);
324 }
325
326 static inline
327 void reset_heap_information()
328 {
329
330 }
331
332 // TODO, have a robust way to find it in O(1)
333 static inline
334 mc_mem_region_t MC_get_heap_region(simgrid::mc::Snapshot* snapshot)
335 {
336   for (auto& region : snapshot->snapshot_regions)
337     if (region->region_type() == simgrid::mc::RegionType::Heap)
338       return region.get();
339   xbt_die("No heap region");
340 }
341
342 static
343 int mmalloc_compare_heap(simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2)
344 {
345   simgrid::mc::Process* process = &mc_model_checker->process();
346   simgrid::mc::StateComparator *state = mc_diff_info.get();
347
348   /* Start comparison */
349   size_t i1, i2, j1, j2, k;
350   void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
351   int nb_diff1 = 0, nb_diff2 = 0;
352
353   int equal, res_compare = 0;
354
355   /* Check busy blocks */
356
357   i1 = 1;
358
359   malloc_info heapinfo_temp1, heapinfo_temp2;
360   malloc_info heapinfo_temp2b;
361
362   mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
363   mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
364
365   // This is the address of std_heap->heapinfo in the application process:
366   void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
367
368   // This is in snapshot do not use them directly:
369   const malloc_info* heapinfos1 = snapshot1->read<malloc_info*>(
370     (std::uint64_t)heapinfo_address, simgrid::mc::ProcessIndexMissing);
371   const malloc_info* heapinfos2 = snapshot2->read<malloc_info*>(
372     (std::uint64_t)heapinfo_address, simgrid::mc::ProcessIndexMissing);
373
374   while (i1 < state->heaplimit) {
375
376     const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(heap_region1, &heapinfo_temp1, &heapinfos1[i1], sizeof(malloc_info));
377     const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2, &heapinfos2[i1], sizeof(malloc_info));
378
379     if (heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type == MMALLOC_TYPE_HEAPINFO) {      /* Free block */
380       i1 ++;
381       continue;
382     }
383
384     if (heapinfo1->type < 0) {
385       fprintf(stderr, "Unkown mmalloc block type.\n");
386       abort();
387     }
388
389     addr_block1 =
390         ((void *) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE +
391                    (char *) state->std_heap_copy.heapbase));
392
393     if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED) {       /* Large block */
394
395       if (is_stack(addr_block1)) {
396         for (k = 0; k < heapinfo1->busy_block.size; k++)
397           state->equals_to1_(i1 + k, 0) = make_heap_area(i1, -1);
398         for (k = 0; k < heapinfo2->busy_block.size; k++)
399           state->equals_to2_(i1 + k, 0) = make_heap_area(i1, -1);
400         i1 += heapinfo1->busy_block.size;
401         continue;
402       }
403
404       if (state->equals_to1_(i1, 0).valid) {
405         i1++;
406         continue;
407       }
408
409       i2 = 1;
410       equal = 0;
411       res_compare = 0;
412
413       /* Try first to associate to same block in the other heap */
414       if (heapinfo2->type == heapinfo1->type
415         && state->equals_to2_(i1, 0).valid == 0) {
416         addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE +
417                        (char *) state->std_heap_copy.heapbase;
418         res_compare =
419             compare_heap_area(simgrid::mc::ProcessIndexMissing, addr_block1, addr_block2, snapshot1, snapshot2,
420                               nullptr, nullptr, 0);
421         if (res_compare != 1) {
422           for (k = 1; k < heapinfo2->busy_block.size; k++)
423             state->equals_to2_(i1 + k, 0) = make_heap_area(i1, -1);
424           for (k = 1; k < heapinfo1->busy_block.size; k++)
425             state->equals_to1_(i1 + k, 0) = make_heap_area(i1, -1);
426           equal = 1;
427           i1 += heapinfo1->busy_block.size;
428         }
429       }
430
431       while (i2 < state->heaplimit && !equal) {
432
433         addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE +
434                        (char *) state->std_heap_copy.heapbase;
435
436         if (i2 == i1) {
437           i2++;
438           continue;
439         }
440
441         const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2b, &heapinfos2[i2], sizeof(malloc_info));
442
443         if (heapinfo2b->type != MMALLOC_TYPE_UNFRAGMENTED) {
444           i2++;
445           continue;
446         }
447
448         if (state->equals_to2_(i2, 0).valid) {
449           i2++;
450           continue;
451         }
452
453         res_compare =
454             compare_heap_area(simgrid::mc::ProcessIndexMissing, addr_block1, addr_block2, snapshot1, snapshot2,
455                               nullptr, nullptr, 0);
456
457         if (res_compare != 1) {
458           for (k = 1; k < heapinfo2b->busy_block.size; k++)
459             state->equals_to2_(i2 + k, 0) = make_heap_area(i1, -1);
460           for (k = 1; k < heapinfo1->busy_block.size; k++)
461             state->equals_to1_(i1 + k, 0) = make_heap_area(i2, -1);
462           equal = 1;
463           i1 += heapinfo1->busy_block.size;
464         }
465
466         i2++;
467
468       }
469
470       if (!equal) {
471         XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1,
472                   heapinfo1->busy_block.busy_size, addr_block1);
473         i1 = state->heaplimit + 1;
474         nb_diff1++;
475         //i1++;
476       }
477
478     } else {                    /* Fragmented block */
479
480       for (j1 = 0; j1 < (size_t) (BLOCKSIZE >> heapinfo1->type); j1++) {
481
482         if (heapinfo1->busy_frag.frag_size[j1] == -1) /* Free fragment */
483           continue;
484
485         if (state->equals_to1_(i1, j1).valid)
486           continue;
487
488         addr_frag1 =
489             (void *) ((char *) addr_block1 + (j1 << heapinfo1->type));
490
491         i2 = 1;
492         equal = 0;
493
494         /* Try first to associate to same fragment in the other heap */
495         if (heapinfo2->type == heapinfo1->type
496             && state->equals_to2_(i1, j1).valid == 0) {
497           addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE +
498                          (char *) state->std_heap_copy.heapbase;
499           addr_frag2 =
500               (void *) ((char *) addr_block2 +
501                         (j1 << heapinfo2->type));
502           res_compare =
503               compare_heap_area(simgrid::mc::ProcessIndexMissing, addr_frag1, addr_frag2, snapshot1, snapshot2,
504                                 nullptr, nullptr, 0);
505           if (res_compare != 1)
506             equal = 1;
507         }
508
509
510
511         while (i2 < state->heaplimit && !equal) {
512
513           const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(
514             heap_region2, &heapinfo_temp2b, &heapinfos2[i2],
515             sizeof(malloc_info));
516
517           if (heapinfo2b->type == MMALLOC_TYPE_FREE || heapinfo2b->type == MMALLOC_TYPE_HEAPINFO) {
518             i2 ++;
519             continue;
520           }
521
522           // We currently do not match fragments with unfragmented blocks (maybe we should).
523           if (heapinfo2b->type == MMALLOC_TYPE_UNFRAGMENTED) {
524             i2++;
525             continue;
526           }
527
528           if (heapinfo2b->type < 0) {
529             fprintf(stderr, "Unkown mmalloc block type.\n");
530             abort();
531           }
532
533           for (j2 = 0; j2 < (size_t) (BLOCKSIZE >> heapinfo2b->type);
534                j2++) {
535
536             if (i2 == i1 && j2 == j1)
537               continue;
538
539             if (state->equals_to2_(i2, j2).valid)
540               continue;
541
542             addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE +
543                            (char *) state->std_heap_copy.heapbase;
544             addr_frag2 =
545                 (void *) ((char *) addr_block2 +
546                           (j2 << heapinfo2b->type));
547
548             res_compare =
549                 compare_heap_area(simgrid::mc::ProcessIndexMissing, addr_frag1, addr_frag2, snapshot2, snapshot2,
550                                   nullptr, nullptr, 0);
551
552             if (res_compare != 1) {
553               equal = 1;
554               break;
555             }
556
557           }
558
559           i2++;
560
561         }
562
563         if (!equal) {
564           XBT_DEBUG
565               ("Block %zu, fragment %zu not found (size_used = %zd, address = %p)\n",
566                i1, j1, heapinfo1->busy_frag.frag_size[j1],
567                addr_frag1);
568           i2 = state->heaplimit + 1;
569           i1 = state->heaplimit + 1;
570           nb_diff1++;
571           break;
572         }
573
574       }
575
576       i1++;
577
578     }
579
580   }
581
582   /* All blocks/fragments are equal to another block/fragment ? */
583   size_t i = 1, j = 0;
584
585   for(i = 1; i < state->heaplimit; i++) {
586     const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
587       heap_region1, &heapinfo_temp1, &heapinfos1[i], sizeof(malloc_info));
588
589     if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED
590         && i1 == state->heaplimit
591         && heapinfo1->busy_block.busy_size > 0
592         && state->equals_to1_(i, 0).valid == 0) {
593       XBT_DEBUG("Block %zu not found (size used = %zu)", i,
594                 heapinfo1->busy_block.busy_size);
595       nb_diff1++;
596     }
597
598     if (heapinfo1->type <= 0)
599       continue;
600     for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo1->type); j++)
601       if (i1 == state->heaplimit
602           && heapinfo1->busy_frag.frag_size[j] > 0
603           && state->equals_to1_(i, j).valid == 0) {
604         XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)",
605           i, j, heapinfo1->busy_frag.frag_size[j]);
606         nb_diff1++;
607       }
608   }
609
610   if (i1 == state->heaplimit)
611     XBT_DEBUG("Number of blocks/fragments not found in heap1 : %d", nb_diff1);
612
613   for (i=1; i < state->heaplimit; i++) {
614     const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
615       heap_region2, &heapinfo_temp2, &heapinfos2[i], sizeof(malloc_info));
616     if (heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED
617         && i1 == state->heaplimit
618         && heapinfo2->busy_block.busy_size > 0
619         && state->equals_to2_(i, 0).valid == 0) {
620       XBT_DEBUG("Block %zu not found (size used = %zu)", i,
621                 heapinfo2->busy_block.busy_size);
622       nb_diff2++;
623     }
624
625     if (heapinfo2->type <= 0)
626       continue;
627
628     for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo2->type); j++)
629       if (i1 == state->heaplimit
630           && heapinfo2->busy_frag.frag_size[j] > 0
631           && state->equals_to2_(i, j).valid == 0) {
632         XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)",
633           i, j, heapinfo2->busy_frag.frag_size[j]);
634         nb_diff2++;
635       }
636
637   }
638
639   if (i1 == state->heaplimit)
640     XBT_DEBUG("Number of blocks/fragments not found in heap2 : %d", nb_diff2);
641
642   return nb_diff1 > 0 || nb_diff2 > 0;
643 }
644
645 /**
646  *
647  * @param state
648  * @param real_area1     Process address for state 1
649  * @param real_area2     Process address for state 2
650  * @param snapshot1      Snapshot of state 1
651  * @param snapshot2      Snapshot of state 2
652  * @param previous
653  * @param size
654  * @param check_ignore
655  */
656 static int compare_heap_area_without_type(
657   simgrid::mc::StateComparator *state, int process_index,
658   const void *real_area1, const void *real_area2,
659   simgrid::mc::Snapshot* snapshot1,
660   simgrid::mc::Snapshot* snapshot2,
661   xbt_dynar_t previous, int size,
662   int check_ignore)
663 {
664   simgrid::mc::Process* process = &mc_model_checker->process();
665   mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
666   mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
667
668   for (int i = 0; i < size; ) {
669
670     if (check_ignore > 0) {
671       ssize_t ignore1 = heap_comparison_ignore_size(
672         state->processStates[0].to_ignore, (char *) real_area1 + i);
673       if (ignore1 != -1) {
674         ssize_t ignore2 = heap_comparison_ignore_size(
675           state->processStates[1].to_ignore, (char *) real_area2 + i);
676         if (ignore2 == ignore1) {
677           if (ignore1 == 0) {
678             check_ignore--;
679             return 0;
680           } else {
681             i = i + ignore2;
682             check_ignore--;
683             continue;
684           }
685         }
686       }
687     }
688
689     if (MC_snapshot_region_memcmp(((char *) real_area1) + i, heap_region1, ((char *) real_area2) + i, heap_region2, 1) != 0) {
690
691       int pointer_align = (i / sizeof(void *)) * sizeof(void *);
692       const void* addr_pointed1 = snapshot1->read(
693         remote((void**)((char *) real_area1 + pointer_align)), process_index);
694       const void* addr_pointed2 = snapshot2->read(
695         remote((void**)((char *) real_area2 + pointer_align)), process_index);
696
697       if (process->in_maestro_stack(remote(addr_pointed1))
698         && process->in_maestro_stack(remote(addr_pointed2))) {
699         i = pointer_align + sizeof(void *);
700         continue;
701       }
702
703       if (addr_pointed1 > state->std_heap_copy.heapbase
704            && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
705            && addr_pointed2 > state->std_heap_copy.heapbase
706            && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)) {
707         // Both addreses are in the heap:
708         int res_compare =
709             compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
710                               snapshot2, previous, nullptr, 0);
711         if (res_compare == 1)
712           return res_compare;
713         i = pointer_align + sizeof(void *);
714         continue;
715       }
716
717       return 1;
718     }
719
720     i++;
721   }
722
723   return 0;
724 }
725
726 /**
727  *
728  * @param state
729  * @param real_area1     Process address for state 1
730  * @param real_area2     Process address for state 2
731  * @param snapshot1      Snapshot of state 1
732  * @param snapshot2      Snapshot of state 2
733  * @param previous
734  * @param type_id
735  * @param area_size      either a byte_size or an elements_count (?)
736  * @param check_ignore
737  * @param pointer_level
738  * @return               0 (same), 1 (different), -1 (unknown)
739  */
740 static int compare_heap_area_with_type(
741   simgrid::mc::StateComparator *state, int process_index,
742   const void *real_area1, const void *real_area2,
743   simgrid::mc::Snapshot* snapshot1,
744   simgrid::mc::Snapshot* snapshot2,
745   xbt_dynar_t previous, simgrid::mc::Type* type,
746   int area_size, int check_ignore,
747   int pointer_level)
748 {
749 top:
750
751   // HACK: This should not happen but in pratice, there are some
752   // DW_TAG_typedef without an associated DW_AT_type:
753   //<1><538832>: Abbrev Number: 111 (DW_TAG_typedef)
754   //    <538833>   DW_AT_name        : (indirect string, offset: 0x2292f3): gregset_t
755   //    <538837>   DW_AT_decl_file   : 98
756   //    <538838>   DW_AT_decl_line   : 37
757   if (type == nullptr)
758     return 0;
759
760   if (is_stack(real_area1) && is_stack(real_area2))
761     return 0;
762
763   if (check_ignore > 0) {
764     ssize_t ignore1 = heap_comparison_ignore_size(
765       state->processStates[0].to_ignore, real_area1);
766     if (ignore1 > 0
767         && heap_comparison_ignore_size(
768           state->processStates[1].to_ignore, real_area2) == ignore1)
769       return 0;
770   }
771
772   simgrid::mc::Type *subtype, *subsubtype;
773   int res, elm_size;
774   const void *addr_pointed1, *addr_pointed2;
775
776   mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
777   mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
778
779   switch (type->type) {
780   case DW_TAG_unspecified_type:
781     return 1;
782
783   case DW_TAG_base_type:
784     if (!type->name.empty() && type->name == "char") {        /* String, hence random (arbitrary ?) size */
785       if (real_area1 == real_area2)
786         return -1;
787       else
788         return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, area_size) != 0;
789     } else {
790       if (area_size != -1 && type->byte_size != area_size)
791         return -1;
792       else
793         return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
794     }
795     break;
796
797   case DW_TAG_enumeration_type:
798     if (area_size != -1 && type->byte_size != area_size)
799       return -1;
800     return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
801
802   case DW_TAG_typedef:
803   case DW_TAG_const_type:
804   case DW_TAG_volatile_type:
805     // Poor man's TCO:
806     type = type->subtype;
807     goto top;
808
809   case DW_TAG_array_type:
810     subtype = type->subtype;
811     switch (subtype->type) {
812     case DW_TAG_unspecified_type:
813       return 1;
814
815     case DW_TAG_base_type:
816     case DW_TAG_enumeration_type:
817     case DW_TAG_pointer_type:
818     case DW_TAG_reference_type:
819     case DW_TAG_rvalue_reference_type:
820     case DW_TAG_structure_type:
821     case DW_TAG_class_type:
822     case DW_TAG_union_type:
823       if (subtype->full_type)
824         subtype = subtype->full_type;
825       elm_size = subtype->byte_size;
826       break;
827       // TODO, just remove the type indirection?
828     case DW_TAG_const_type:
829     case DW_TAG_typedef:
830     case DW_TAG_volatile_type:
831       subsubtype = subtype->subtype;
832       if (subsubtype->full_type)
833         subsubtype = subsubtype->full_type;
834       elm_size = subsubtype->byte_size;
835       break;
836     default:
837       return 0;
838       break;
839     }
840     for (int i = 0; i < type->element_count; i++) {
841       // TODO, add support for variable stride (DW_AT_byte_stride)
842       res =
843           compare_heap_area_with_type(state, process_index,
844                                       (char *) real_area1 + (i * elm_size),
845                                       (char *) real_area2 + (i * elm_size),
846                                       snapshot1, snapshot2, previous,
847                                       type->subtype, subtype->byte_size,
848                                       check_ignore, pointer_level);
849       if (res == 1)
850         return res;
851     }
852     return 0;
853
854   case DW_TAG_reference_type:
855   case DW_TAG_rvalue_reference_type:
856   case DW_TAG_pointer_type:
857     if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
858       addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
859       addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
860       return (addr_pointed1 != addr_pointed2);
861     }
862     pointer_level++;
863     if (pointer_level <= 1) {
864       addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
865       addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
866       if (addr_pointed1 > state->std_heap_copy.heapbase
867           && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
868           && addr_pointed2 > state->std_heap_copy.heapbase
869           && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
870         return compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
871                                  snapshot2, previous, type->subtype,
872                                  pointer_level);
873       else
874         return (addr_pointed1 != addr_pointed2);
875     }
876     for (size_t i = 0; i < (area_size / sizeof(void *)); i++) {
877       addr_pointed1 = snapshot1->read(
878         remote((void**)((char*) real_area1 + i * sizeof(void *))),
879         process_index);
880       addr_pointed2 = snapshot2->read(
881         remote((void**)((char*) real_area2 + i * sizeof(void *))),
882         process_index);
883       if (addr_pointed1 > state->std_heap_copy.heapbase
884           && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
885           && addr_pointed2 > state->std_heap_copy.heapbase
886           && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
887         res =
888             compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
889                               snapshot2, previous, type->subtype,
890                               pointer_level);
891       else
892         res = (addr_pointed1 != addr_pointed2);
893       if (res == 1)
894         return res;
895     }
896     return 0;
897
898   case DW_TAG_structure_type:
899   case DW_TAG_class_type:
900     if (type->full_type)
901       type = type->full_type;
902     if (area_size != -1 && type->byte_size != area_size) {
903       if (area_size <= type->byte_size || area_size % type->byte_size != 0)
904         return -1;
905       for (size_t i = 0; i < (size_t)(area_size / type->byte_size); i++) {
906         int res = compare_heap_area_with_type(state, process_index,
907                     (char *) real_area1 + i * type->byte_size,
908                     (char *) real_area2 + i * type->byte_size,
909                     snapshot1, snapshot2, previous, type, -1,
910                     check_ignore, 0);
911         if (res == 1)
912           return res;
913       }
914     } else {
915       for(simgrid::mc::Member& member : type->members) {
916         // TODO, optimize this? (for the offset case)
917         void *real_member1 = simgrid::dwarf::resolve_member(
918           real_area1, type, &member, (simgrid::mc::AddressSpace*) snapshot1, process_index);
919         void *real_member2 = simgrid::dwarf::resolve_member(
920             real_area2, type, &member, (simgrid::mc::AddressSpace*) snapshot2, process_index);
921         int res = compare_heap_area_with_type(
922                     state, process_index, real_member1, real_member2,
923                     snapshot1, snapshot2,
924                     previous, member.type, -1,
925                     check_ignore, 0);
926         if (res == 1)
927           return res;
928       }
929     }
930     return 0;
931
932   case DW_TAG_union_type:
933     return compare_heap_area_without_type(state, process_index, real_area1, real_area2,
934                                           snapshot1, snapshot2, previous,
935                                           type->byte_size, check_ignore);
936     return 0;
937
938   default:
939     return 0;
940   }
941
942   xbt_die("Unreachable");
943 }
944
945 /** Infer the type of a part of the block from the type of the block
946  *
947  * TODO, handle DW_TAG_array_type as well as arrays of the object ((*p)[5], p[5])
948  *
949  * TODO, handle subfields ((*p).bar.foo, (*p)[5].bar…)
950  *
951  * @param  type_id            DWARF type ID of the root address
952  * @param  area_size
953  * @return                    DWARF type ID for given offset
954  */
955 static simgrid::mc::Type* get_offset_type(void *real_base_address, simgrid::mc::Type* type,
956                                  int offset, int area_size,
957                                  simgrid::mc::Snapshot* snapshot, int process_index)
958 {
959
960   // Beginning of the block, the infered variable type if the type of the block:
961   if (offset == 0)
962     return type;
963
964   switch (type->type) {
965
966   case DW_TAG_structure_type:
967   case DW_TAG_class_type:
968     if (type->full_type)
969       type = type->full_type;
970     if (area_size != -1 && type->byte_size != area_size) {
971       if (area_size > type->byte_size && area_size % type->byte_size == 0)
972         return type;
973       else
974         return nullptr;
975     }
976
977     for(simgrid::mc::Member& member : type->members) {
978       if (member.has_offset_location()) {
979         // We have the offset, use it directly (shortcut):
980         if (member.offset() == offset)
981           return member.type;
982       } else {
983         void *real_member = simgrid::dwarf::resolve_member(
984           real_base_address, type, &member, snapshot, process_index);
985         if ((char*) real_member - (char *) real_base_address == offset)
986           return member.type;
987       }
988     }
989     return nullptr;
990
991   default:
992     /* FIXME : other cases ? */
993     return nullptr;
994
995   }
996 }
997
998 /**
999  *
1000  * @param area1          Process address for state 1
1001  * @param area2          Process address for state 2
1002  * @param snapshot1      Snapshot of state 1
1003  * @param snapshot2      Snapshot of state 2
1004  * @param previous       Pairs of blocks already compared on the current path (or nullptr)
1005  * @param type_id        Type of variable
1006  * @param pointer_level
1007  * @return 0 (same), 1 (different), -1
1008  */
1009 static
1010 int compare_heap_area(int process_index, const void *area1, const void *area2, simgrid::mc::Snapshot* snapshot1,
1011                       simgrid::mc::Snapshot* snapshot2, xbt_dynar_t previous,
1012                       simgrid::mc::Type* type, int pointer_level)
1013 {
1014   simgrid::mc::Process* process = &mc_model_checker->process();
1015
1016   simgrid::mc::StateComparator *state = mc_diff_info.get();
1017
1018   int res_compare;
1019   ssize_t block1, frag1, block2, frag2;
1020   ssize_t size;
1021   int check_ignore = 0;
1022
1023   void *real_addr_block1, *real_addr_block2, *real_addr_frag1, *real_addr_frag2;
1024   int type_size = -1;
1025   int offset1 = 0, offset2 = 0;
1026   int new_size1 = -1, new_size2 = -1;
1027   simgrid::mc::Type *new_type1 = nullptr, *new_type2 = nullptr;
1028
1029   int match_pairs = 0;
1030
1031   // This is the address of std_heap->heapinfo in the application process:
1032   void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
1033
1034   const malloc_info* heapinfos1 = snapshot1->read(
1035     remote((const malloc_info**)heapinfo_address), process_index);
1036   const malloc_info* heapinfos2 = snapshot2->read(
1037     remote((const malloc_info**)heapinfo_address), process_index);
1038
1039   malloc_info heapinfo_temp1, heapinfo_temp2;
1040
1041   if (previous == nullptr) {
1042     previous =
1043         xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
1044     match_pairs = 1;
1045   }
1046   // Get block number:
1047   block1 =
1048       ((char *) area1 -
1049        (char *) state->std_heap_copy.heapbase) / BLOCKSIZE + 1;
1050   block2 =
1051       ((char *) area2 -
1052        (char *) state->std_heap_copy.heapbase) / BLOCKSIZE + 1;
1053
1054   // If either block is a stack block:
1055   if (is_block_stack((int) block1) && is_block_stack((int) block2)) {
1056     add_heap_area_pair(previous, block1, -1, block2, -1);
1057     if (match_pairs) {
1058       state->match_equals(previous);
1059       xbt_dynar_free(&previous);
1060     }
1061     return 0;
1062   }
1063
1064   // If either block is not in the expected area of memory:
1065   if (((char *) area1 < (char *) state->std_heap_copy.heapbase)
1066       || (block1 > (ssize_t) state->processStates[0].heapsize) || (block1 < 1)
1067       || ((char *) area2 < (char *) state->std_heap_copy.heapbase)
1068       || (block2 > (ssize_t) state->processStates[1].heapsize) || (block2 < 1)) {
1069     if (match_pairs)
1070       xbt_dynar_free(&previous);
1071     return 1;
1072   }
1073
1074   // Process address of the block:
1075   real_addr_block1 = (ADDR2UINT(block1) - 1) * BLOCKSIZE +
1076                  (char *) state->std_heap_copy.heapbase;
1077   real_addr_block2 = (ADDR2UINT(block2) - 1) * BLOCKSIZE +
1078                  (char *) state->std_heap_copy.heapbase;
1079
1080   if (type) {
1081
1082     if (type->full_type)
1083       type = type->full_type;
1084
1085     // This assume that for "boring" types (volatile ...) byte_size is absent:
1086     while (type->byte_size == 0 && type->subtype != nullptr)
1087       type = type->subtype;
1088
1089     // Find type_size:
1090     if (type->type == DW_TAG_pointer_type
1091         || (type->type == DW_TAG_base_type && !type->name.empty()
1092             && type->name == "char"))
1093       type_size = -1;
1094     else
1095       type_size = type->byte_size;
1096
1097   }
1098
1099   mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
1100   mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
1101
1102   const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
1103     heap_region1, &heapinfo_temp1, &heapinfos1[block1], sizeof(malloc_info));
1104   const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
1105     heap_region2, &heapinfo_temp2, &heapinfos2[block2], sizeof(malloc_info));
1106
1107   if ((heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type==MMALLOC_TYPE_HEAPINFO)
1108     && (heapinfo2->type == MMALLOC_TYPE_FREE || heapinfo2->type ==MMALLOC_TYPE_HEAPINFO)) {
1109     /* Free block */
1110     if (match_pairs) {
1111       state->match_equals(previous);
1112       xbt_dynar_free(&previous);
1113     }
1114     return 0;
1115   }
1116
1117   if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED
1118     && heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
1119     /* Complete block */
1120
1121     // TODO, lookup variable type from block type as done for fragmented blocks
1122
1123     offset1 = (char *) area1 - (char *) real_addr_block1;
1124     offset2 = (char *) area2 - (char *) real_addr_block2;
1125
1126     if (state->equals_to1_(block1, 0).valid
1127         && state->equals_to2_(block2, 0).valid
1128         && state->blocksEqual(block1, block2)) {
1129       if (match_pairs) {
1130         state->match_equals(previous);
1131         xbt_dynar_free(&previous);
1132       }
1133       return 0;
1134     }
1135
1136     if (type_size != -1) {
1137       if (type_size != (ssize_t) heapinfo1->busy_block.busy_size
1138           && type_size != (ssize_t)   heapinfo2->busy_block.busy_size
1139           && (type->name.empty() || type->name == "struct s_smx_context")) {
1140         if (match_pairs) {
1141           state->match_equals(previous);
1142           xbt_dynar_free(&previous);
1143         }
1144         return -1;
1145       }
1146     }
1147
1148     if (heapinfo1->busy_block.size != heapinfo2->busy_block.size) {
1149       if (match_pairs)
1150         xbt_dynar_free(&previous);
1151       return 1;
1152     }
1153
1154     if (heapinfo1->busy_block.busy_size != heapinfo2->busy_block.busy_size) {
1155       if (match_pairs)
1156         xbt_dynar_free(&previous);
1157       return 1;
1158     }
1159
1160     if (!add_heap_area_pair(previous, block1, -1, block2, -1)) {
1161       if (match_pairs) {
1162         state->match_equals(previous);
1163         xbt_dynar_free(&previous);
1164       }
1165       return 0;
1166     }
1167
1168     size = heapinfo1->busy_block.busy_size;
1169
1170     // Remember (basic) type inference.
1171     // The current data structure only allows us to do this for the whole block.
1172     if (type != nullptr && area1 == real_addr_block1)
1173       state->types1_(block1, 0) = type;
1174     if (type != nullptr && area2 == real_addr_block2)
1175       state->types2_(block2, 0) = type;
1176
1177     if (size <= 0) {
1178       if (match_pairs) {
1179         state->match_equals(previous);
1180         xbt_dynar_free(&previous);
1181       }
1182       return 0;
1183     }
1184
1185     frag1 = -1;
1186     frag2 = -1;
1187
1188     if (heapinfo1->busy_block.ignore > 0
1189         && heapinfo2->busy_block.ignore == heapinfo1->busy_block.ignore)
1190       check_ignore = heapinfo1->busy_block.ignore;
1191
1192   } else if ((heapinfo1->type > 0) && (heapinfo2->type > 0)) {      /* Fragmented block */
1193
1194     // Fragment number:
1195     frag1 =
1196         ((uintptr_t) (ADDR2UINT(area1) % (BLOCKSIZE))) >> heapinfo1->type;
1197     frag2 =
1198         ((uintptr_t) (ADDR2UINT(area2) % (BLOCKSIZE))) >> heapinfo2->type;
1199
1200     // Process address of the fragment:
1201     real_addr_frag1 =
1202         (void *) ((char *) real_addr_block1 +
1203                   (frag1 << heapinfo1->type));
1204     real_addr_frag2 =
1205         (void *) ((char *) real_addr_block2 +
1206                   (frag2 << heapinfo2->type));
1207
1208     // Check the size of the fragments against the size of the type:
1209     if (type_size != -1) {
1210       if (heapinfo1->busy_frag.frag_size[frag1] == -1
1211           || heapinfo2->busy_frag.frag_size[frag2] == -1) {
1212         if (match_pairs) {
1213           state->match_equals(previous);
1214           xbt_dynar_free(&previous);
1215         }
1216         return -1;
1217       }
1218       // ?
1219       if (type_size != heapinfo1->busy_frag.frag_size[frag1]
1220           || type_size != heapinfo2->busy_frag.frag_size[frag2]) {
1221         if (match_pairs) {
1222           state->match_equals(previous);
1223           xbt_dynar_free(&previous);
1224         }
1225         return -1;
1226       }
1227     }
1228
1229     // Check if the blocks are already matched together:
1230     if (state->equals_to1_(block1, frag1).valid
1231         && state->equals_to2_(block2, frag2).valid) {
1232       if (offset1==offset2 && state->fragmentsEqual(block1, frag1, block2, frag2)) {
1233         if (match_pairs) {
1234           state->match_equals(previous);
1235           xbt_dynar_free(&previous);
1236         }
1237         return 0;
1238       }
1239     }
1240     // Compare the size of both fragments:
1241     if (heapinfo1->busy_frag.frag_size[frag1] !=
1242         heapinfo2->busy_frag.frag_size[frag2]) {
1243       if (type_size == -1) {
1244         if (match_pairs) {
1245           state->match_equals(previous);
1246           xbt_dynar_free(&previous);
1247         }
1248         return -1;
1249       } else {
1250         if (match_pairs)
1251           xbt_dynar_free(&previous);
1252         return 1;
1253       }
1254     }
1255
1256     // Size of the fragment:
1257     size = heapinfo1->busy_frag.frag_size[frag1];
1258
1259     // Remember (basic) type inference.
1260     // The current data structure only allows us to do this for the whole fragment.
1261     if (type != nullptr && area1 == real_addr_frag1)
1262       state->types1_(block1, frag1) = type;
1263     if (type != nullptr && area2 == real_addr_frag2)
1264       state->types2_(block2, frag2) = type;
1265
1266     // The type of the variable is already known:
1267     if (type) {
1268       new_type1 = type;
1269       new_type2 = type;
1270     }
1271     // Type inference from the block type.
1272     else if (state->types1_(block1, frag1) != nullptr
1273              || state->types2_(block2, frag2) != nullptr) {
1274
1275       offset1 = (char *) area1 - (char *) real_addr_frag1;
1276       offset2 = (char *) area2 - (char *) real_addr_frag2;
1277
1278       if (state->types1_(block1, frag1) != nullptr
1279           && state->types2_(block2, frag2) != nullptr) {
1280         new_type1 =
1281             get_offset_type(real_addr_frag1, state->types1_(block1, frag1),
1282                             offset1, size, snapshot1, process_index);
1283         new_type2 =
1284             get_offset_type(real_addr_frag2, state->types2_(block2, frag2),
1285                             offset1, size, snapshot2, process_index);
1286       } else if (state->types1_(block1, frag1) != nullptr) {
1287         new_type1 =
1288             get_offset_type(real_addr_frag1, state->types1_(block1, frag1),
1289                             offset1, size, snapshot1, process_index);
1290         new_type2 =
1291             get_offset_type(real_addr_frag2, state->types1_(block1, frag1),
1292                             offset2, size, snapshot2, process_index);
1293       } else if (state->types2_(block2, frag2) != nullptr) {
1294         new_type1 =
1295             get_offset_type(real_addr_frag1, state->types2_(block2, frag2),
1296                             offset1, size, snapshot1, process_index);
1297         new_type2 =
1298             get_offset_type(real_addr_frag2, state->types2_(block2, frag2),
1299                             offset2, size, snapshot2, process_index);
1300       } else {
1301         if (match_pairs) {
1302           state->match_equals(previous);
1303           xbt_dynar_free(&previous);
1304         }
1305         return -1;
1306       }
1307
1308       if (new_type1 != nullptr && new_type2 != nullptr && new_type1 != new_type2) {
1309
1310         type = new_type1;
1311         while (type->byte_size == 0 && type->subtype != nullptr)
1312           type = type->subtype;
1313         new_size1 = type->byte_size;
1314
1315         type = new_type2;
1316         while (type->byte_size == 0 && type->subtype != nullptr)
1317           type = type->subtype;
1318         new_size2 = type->byte_size;
1319
1320       } else {
1321         if (match_pairs) {
1322           state->match_equals(previous);
1323           xbt_dynar_free(&previous);
1324         }
1325         return -1;
1326       }
1327     }
1328
1329     if (new_size1 > 0 && new_size1 == new_size2) {
1330       type = new_type1;
1331       size = new_size1;
1332     }
1333
1334     if (offset1 == 0 && offset2 == 0
1335       && !add_heap_area_pair(previous, block1, frag1, block2, frag2)) {
1336         if (match_pairs) {
1337           state->match_equals(previous);
1338           xbt_dynar_free(&previous);
1339         }
1340         return 0;
1341       }
1342
1343     if (size <= 0) {
1344       if (match_pairs) {
1345         state->match_equals(previous);
1346         xbt_dynar_free(&previous);
1347       }
1348       return 0;
1349     }
1350
1351     if ((heapinfo1->busy_frag.ignore[frag1] > 0)
1352         && (heapinfo2->busy_frag.ignore[frag2] ==
1353             heapinfo1->busy_frag.ignore[frag1]))
1354       check_ignore = heapinfo1->busy_frag.ignore[frag1];
1355
1356   } else {
1357     if (match_pairs)
1358       xbt_dynar_free(&previous);
1359     return 1;
1360   }
1361
1362
1363   /* Start comparison */
1364   if (type)
1365     res_compare =
1366         compare_heap_area_with_type(state, process_index, area1, area2, snapshot1, snapshot2,
1367                                     previous, type, size, check_ignore,
1368                                     pointer_level);
1369   else
1370     res_compare =
1371         compare_heap_area_without_type(state, process_index, area1, area2, snapshot1, snapshot2,
1372                                        previous, size, check_ignore);
1373
1374   if (res_compare == 1) {
1375     if (match_pairs)
1376       xbt_dynar_free(&previous);
1377     return res_compare;
1378   }
1379
1380   if (match_pairs) {
1381     state->match_equals(previous);
1382     xbt_dynar_free(&previous);
1383   }
1384
1385   return 0;
1386 }
1387
1388 }
1389 }
1390
1391 namespace simgrid {
1392 namespace mc {
1393
1394 /** A hash which works with more stuff
1395  *
1396  *  It can hash pairs: the standard hash currently doesn't include this.
1397  */
1398 template<class X> struct hash : public std::hash<X> {};
1399
1400 template<class X, class Y>
1401 struct hash<std::pair<X,Y>> {
1402   std::size_t operator()(std::pair<X,Y>const& x) const
1403   {
1404     struct hash<X> h1;
1405     struct hash<X> h2;
1406     return h1(x.first) ^ h2(x.second);
1407   }
1408 };
1409
1410 struct ComparisonState {
1411   std::unordered_set<std::pair<void*, void*>, hash<std::pair<void*, void*>>> compared_pointers;
1412 };
1413
1414 }
1415 }
1416
1417 using simgrid::mc::ComparisonState;
1418
1419 /************************** Snapshot comparison *******************************/
1420 /******************************************************************************/
1421
1422 static int compare_areas_with_type(ComparisonState& state,
1423                                    int process_index,
1424                                    void* real_area1, simgrid::mc::Snapshot* snapshot1, mc_mem_region_t region1,
1425                                    void* real_area2, simgrid::mc::Snapshot* snapshot2, mc_mem_region_t region2,
1426                                    simgrid::mc::Type* type, int pointer_level)
1427 {
1428   simgrid::mc::Process* process = &mc_model_checker->process();
1429
1430   simgrid::mc::Type* subtype;
1431   simgrid::mc::Type* subsubtype;
1432   int elm_size, i, res;
1433
1434   top:
1435   switch (type->type) {
1436   case DW_TAG_unspecified_type:
1437     return 1;
1438
1439   case DW_TAG_base_type:
1440   case DW_TAG_enumeration_type:
1441   case DW_TAG_union_type:
1442   {
1443     return MC_snapshot_region_memcmp(
1444       real_area1, region1, real_area2, region2,
1445       type->byte_size) != 0;
1446   }
1447   case DW_TAG_typedef:
1448   case DW_TAG_volatile_type:
1449   case DW_TAG_const_type:
1450     // Poor man's TCO:
1451     type = type->subtype;
1452     goto top;
1453   case DW_TAG_array_type:
1454     subtype = type->subtype;
1455     switch (subtype->type) {
1456     case DW_TAG_unspecified_type:
1457       return 1;
1458
1459     case DW_TAG_base_type:
1460     case DW_TAG_enumeration_type:
1461     case DW_TAG_pointer_type:
1462     case DW_TAG_reference_type:
1463     case DW_TAG_rvalue_reference_type:
1464     case DW_TAG_structure_type:
1465     case DW_TAG_class_type:
1466     case DW_TAG_union_type:
1467       if (subtype->full_type)
1468         subtype = subtype->full_type;
1469       elm_size = subtype->byte_size;
1470       break;
1471     case DW_TAG_const_type:
1472     case DW_TAG_typedef:
1473     case DW_TAG_volatile_type:
1474       subsubtype = subtype->subtype;
1475       if (subsubtype->full_type)
1476         subsubtype = subsubtype->full_type;
1477       elm_size = subsubtype->byte_size;
1478       break;
1479     default:
1480       return 0;
1481       break;
1482     }
1483     for (i = 0; i < type->element_count; i++) {
1484       size_t off = i * elm_size;
1485       res = compare_areas_with_type(state, process_index,
1486             (char*) real_area1 + off, snapshot1, region1,
1487             (char*) real_area2 + off, snapshot2, region2,
1488             type->subtype, pointer_level);
1489       if (res == 1)
1490         return res;
1491     }
1492     break;
1493   case DW_TAG_pointer_type:
1494   case DW_TAG_reference_type:
1495   case DW_TAG_rvalue_reference_type:
1496   {
1497     void* addr_pointed1 = MC_region_read_pointer(region1, real_area1);
1498     void* addr_pointed2 = MC_region_read_pointer(region2, real_area2);
1499
1500     if (type->subtype && type->subtype->type == DW_TAG_subroutine_type)
1501       return (addr_pointed1 != addr_pointed2);
1502     if (addr_pointed1 == nullptr && addr_pointed2 == nullptr)
1503       return 0;
1504     if (addr_pointed1 == nullptr || addr_pointed2 == nullptr)
1505       return 1;
1506     if (!state.compared_pointers.insert(
1507         std::make_pair(addr_pointed1, addr_pointed2)).second)
1508       return 0;
1509
1510     pointer_level++;
1511
1512       // Some cases are not handled here:
1513       // * the pointers lead to different areas (one to the heap, the other to the RW segment ...);
1514       // * a pointer leads to the read-only segment of the current object;
1515       // * a pointer lead to a different ELF object.
1516
1517       if (addr_pointed1 > process->heap_address
1518           && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)) {
1519         if (!
1520             (addr_pointed2 > process->heap_address
1521              && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)))
1522           return 1;
1523         // The pointers are both in the heap:
1524         return simgrid::mc::compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
1525                                  snapshot2, nullptr, type->subtype, pointer_level);
1526       }
1527
1528       // The pointers are both in the current object R/W segment:
1529       else if (region1->contain(simgrid::mc::remote(addr_pointed1))) {
1530         if (!region2->contain(simgrid::mc::remote(addr_pointed2)))
1531           return 1;
1532         if (!type->type_id)
1533           return (addr_pointed1 != addr_pointed2);
1534         else
1535           return compare_areas_with_type(state, process_index,
1536                                          addr_pointed1, snapshot1, region1,
1537                                          addr_pointed2, snapshot2, region2,
1538                                          type->subtype, pointer_level);
1539       }
1540
1541       // TODO, We do not handle very well the case where
1542       // it belongs to a different (non-heap) region from the current one.
1543
1544       else
1545         return (addr_pointed1 != addr_pointed2);
1546
1547     break;
1548   }
1549   case DW_TAG_structure_type:
1550   case DW_TAG_class_type:
1551     for(simgrid::mc::Member& member : type->members) {
1552       void *member1 = simgrid::dwarf::resolve_member(
1553         real_area1, type, &member, snapshot1, process_index);
1554       void *member2 = simgrid::dwarf::resolve_member(
1555         real_area2, type, &member, snapshot2, process_index);
1556       mc_mem_region_t subregion1 = mc_get_region_hinted(member1, snapshot1, process_index, region1);
1557       mc_mem_region_t subregion2 = mc_get_region_hinted(member2, snapshot2, process_index, region2);
1558       res =
1559           compare_areas_with_type(state, process_index,
1560                                   member1, snapshot1, subregion1,
1561                                   member2, snapshot2, subregion2,
1562                                   member.type, pointer_level);
1563       if (res == 1)
1564         return res;
1565     }
1566     break;
1567   case DW_TAG_subroutine_type:
1568     return -1;
1569     break;
1570   default:
1571     XBT_VERB("Unknown case : %d", type->type);
1572     break;
1573   }
1574
1575   return 0;
1576 }
1577
1578 static int compare_global_variables(simgrid::mc::ObjectInformation* object_info,
1579                                     int process_index,
1580                                     mc_mem_region_t r1,
1581                                     mc_mem_region_t r2, simgrid::mc::Snapshot* snapshot1,
1582                                     simgrid::mc::Snapshot* snapshot2)
1583 {
1584   xbt_assert(r1 && r2, "Missing region.");
1585
1586 #if HAVE_SMPI
1587   if (r1->storage_type() == simgrid::mc::StorageType::Privatized) {
1588     xbt_assert(process_index >= 0);
1589     if (r2->storage_type() != simgrid::mc::StorageType::Privatized)
1590       return 1;
1591
1592     size_t process_count = MC_smpi_process_count();
1593     xbt_assert(process_count == r1->privatized_data().size()
1594       && process_count == r2->privatized_data().size());
1595
1596     // Compare the global variables separately for each simulates process:
1597     for (size_t process_index = 0; process_index < process_count; process_index++) {
1598       int is_diff = compare_global_variables(object_info, process_index,
1599         &r1->privatized_data()[process_index],
1600         &r2->privatized_data()[process_index],
1601         snapshot1, snapshot2);
1602       if (is_diff) return 1;
1603     }
1604     return 0;
1605   }
1606 #else
1607   xbt_assert(r1->storage_type() != simgrid::mc::StorageType::Privatized);
1608 #endif
1609   xbt_assert(r2->storage_type() != simgrid::mc::StorageType::Privatized);
1610
1611   ComparisonState state;
1612
1613   std::vector<simgrid::mc::Variable>& variables = object_info->global_variables;
1614
1615   for (simgrid::mc::Variable& current_var : variables) {
1616
1617     // If the variable is not in this object, skip it:
1618     // We do not expect to find a pointer to something which is not reachable
1619     // by the global variables.
1620     if ((char *) current_var.address < (char *) object_info->start_rw
1621         || (char *) current_var.address > (char *) object_info->end_rw)
1622       continue;
1623
1624     simgrid::mc::Type* bvariable_type = current_var.type;
1625     int res =
1626         compare_areas_with_type(state, process_index,
1627                                 (char *) current_var.address, snapshot1, r1,
1628                                 (char *) current_var.address, snapshot2, r2,
1629                                 bvariable_type, 0);
1630     if (res == 1) {
1631       XBT_VERB("Global variable %s (%p) is different between snapshots",
1632                current_var.name.c_str(),
1633                (char *) current_var.address);
1634       return 1;
1635     }
1636
1637   }
1638
1639   return 0;
1640
1641 }
1642
1643 static int compare_local_variables(int process_index,
1644                                    simgrid::mc::Snapshot* snapshot1,
1645                                    simgrid::mc::Snapshot* snapshot2,
1646                                    mc_snapshot_stack_t stack1,
1647                                    mc_snapshot_stack_t stack2)
1648 {
1649   ComparisonState state;
1650
1651   if (stack1->local_variables.size() != stack2->local_variables.size()) {
1652     XBT_VERB("Different number of local variables");
1653     return 1;
1654   }
1655
1656     unsigned int cursor = 0;
1657     local_variable_t current_var1, current_var2;
1658     int res;
1659     while (cursor < stack1->local_variables.size()) {
1660       current_var1 = &stack1->local_variables[cursor];
1661       current_var2 = &stack1->local_variables[cursor];
1662       if (current_var1->name != current_var2->name
1663           || current_var1->subprogram != current_var2->subprogram
1664           || current_var1->ip != current_var2->ip) {
1665         // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1666         XBT_VERB
1667             ("Different name of variable (%s - %s) "
1668              "or frame (%s - %s) or ip (%lu - %lu)",
1669              current_var1->name.c_str(),
1670              current_var2->name.c_str(),
1671              current_var1->subprogram->name.c_str(),
1672              current_var2->subprogram->name.c_str(),
1673              current_var1->ip, current_var2->ip);
1674         return 1;
1675       }
1676       // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1677
1678         simgrid::mc::Type* subtype = current_var1->type;
1679         res =
1680             compare_areas_with_type(state, process_index,
1681                                     current_var1->address, snapshot1, mc_get_snapshot_region(current_var1->address, snapshot1, process_index),
1682                                     current_var2->address, snapshot2, mc_get_snapshot_region(current_var2->address, snapshot2, process_index),
1683                                     subtype, 0);
1684
1685       if (res == 1) {
1686         // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1687         XBT_VERB
1688             ("Local variable %s (%p - %p) in frame %s "
1689              "is different between snapshots",
1690              current_var1->name.c_str(),
1691              current_var1->address,
1692              current_var2->address,
1693              current_var1->subprogram->name.c_str());
1694         return res;
1695       }
1696       cursor++;
1697     }
1698     return 0;
1699 }
1700
1701 namespace simgrid {
1702 namespace mc {
1703
1704 int snapshot_compare(int num1, simgrid::mc::Snapshot* s1, int num2, simgrid::mc::Snapshot* s2)
1705 {
1706   simgrid::mc::Process* process = &mc_model_checker->process();
1707
1708   int errors = 0;
1709   int res_init;
1710
1711   int hash_result = 0;
1712   if (_sg_mc_hash) {
1713     hash_result = (s1->hash != s2->hash);
1714     if (hash_result) {
1715       XBT_VERB("(%d - %d) Different hash : 0x%" PRIx64 "--0x%" PRIx64, num1,
1716                num2, s1->hash, s2->hash);
1717 #ifndef MC_DEBUG
1718       return 1;
1719 #endif
1720     } else
1721       XBT_VERB("(%d - %d) Same hash : 0x%" PRIx64, num1, num2, s1->hash);
1722   }
1723
1724   /* Compare enabled processes */
1725   if (s1->enabled_processes != s2->enabled_processes) {
1726       XBT_VERB("(%d - %d) Different enabled processes", num1, num2);
1727       // return 1; ??
1728   }
1729
1730   unsigned long i = 0;
1731   size_t size_used1, size_used2;
1732   int is_diff = 0;
1733
1734   /* Compare size of stacks */
1735   while (i < s1->stacks.size()) {
1736     size_used1 = s1->stack_sizes[i];
1737     size_used2 = s2->stack_sizes[i];
1738     if (size_used1 != size_used2) {
1739 #ifdef MC_DEBUG
1740       XBT_DEBUG("(%d - %d) Different size used in stacks : %zu - %zu", num1,
1741                 num2, size_used1, size_used2);
1742       errors++;
1743       is_diff = 1;
1744 #else
1745 #ifdef MC_VERBOSE
1746       XBT_VERB("(%d - %d) Different size used in stacks : %zu - %zu", num1,
1747                num2, size_used1, size_used2);
1748 #endif
1749       return 1;
1750 #endif
1751     }
1752     i++;
1753   }
1754
1755   /* Init heap information used in heap comparison algorithm */
1756   xbt_mheap_t heap1 = (xbt_mheap_t)s1->read_bytes(
1757     alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1758     remote(process->heap_address),
1759     simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1760   xbt_mheap_t heap2 = (xbt_mheap_t)s2->read_bytes(
1761     alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1762     remote(process->heap_address),
1763     simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1764   res_init = simgrid::mc::init_heap_information(heap1, heap2, &s1->to_ignore, &s2->to_ignore);
1765   if (res_init == -1) {
1766 #ifdef MC_DEBUG
1767     XBT_DEBUG("(%d - %d) Different heap information", num1, num2);
1768     errors++;
1769 #else
1770 #ifdef MC_VERBOSE
1771     XBT_VERB("(%d - %d) Different heap information", num1, num2);
1772 #endif
1773
1774     return 1;
1775 #endif
1776   }
1777
1778   /* Stacks comparison */
1779   unsigned cursor = 0;
1780   int diff_local = 0;
1781 #ifdef MC_DEBUG
1782   is_diff = 0;
1783 #endif
1784   mc_snapshot_stack_t stack1, stack2;
1785   while (cursor < s1->stacks.size()) {
1786     stack1 = &s1->stacks[cursor];
1787     stack2 = &s2->stacks[cursor];
1788
1789     if (stack1->process_index != stack2->process_index) {
1790       diff_local = 1;
1791       XBT_DEBUG("(%d - %d) Stacks with different process index (%i vs %i)", num1, num2,
1792         stack1->process_index, stack2->process_index);
1793     }
1794     else diff_local =
1795         compare_local_variables(stack1->process_index, s1, s2, stack1, stack2);
1796     if (diff_local > 0) {
1797 #ifdef MC_DEBUG
1798       XBT_DEBUG("(%d - %d) Different local variables between stacks %d", num1,
1799                 num2, cursor + 1);
1800       errors++;
1801       is_diff = 1;
1802 #else
1803
1804 #ifdef MC_VERBOSE
1805       XBT_VERB("(%d - %d) Different local variables between stacks %d", num1,
1806                num2, cursor + 1);
1807 #endif
1808
1809       simgrid::mc::reset_heap_information();
1810
1811       return 1;
1812 #endif
1813     }
1814     cursor++;
1815   }
1816
1817   size_t regions_count = s1->snapshot_regions.size();
1818   // TODO, raise a difference instead?
1819   xbt_assert(regions_count == s2->snapshot_regions.size());
1820
1821   for (size_t k = 0; k != regions_count; ++k) {
1822     mc_mem_region_t region1 = s1->snapshot_regions[k].get();
1823     mc_mem_region_t region2 = s2->snapshot_regions[k].get();
1824
1825     // Preconditions:
1826     if (region1->region_type() != simgrid::mc::RegionType::Data)
1827       continue;
1828
1829     xbt_assert(region1->region_type() == region2->region_type());
1830     xbt_assert(region1->object_info() == region2->object_info());
1831     xbt_assert(region1->object_info());
1832
1833     std::string const& name = region1->object_info()->file_name;
1834
1835     /* Compare global variables */
1836     is_diff =
1837       compare_global_variables(region1->object_info(),
1838         simgrid::mc::ProcessIndexDisabled,
1839         region1, region2,
1840         s1, s2);
1841
1842     if (is_diff != 0) {
1843 #ifdef MC_DEBUG
1844       XBT_DEBUG("(%d - %d) Different global variables in %s",
1845         num1, num2, name.c_str());
1846       errors++;
1847 #else
1848 #ifdef MC_VERBOSE
1849       XBT_VERB("(%d - %d) Different global variables in %s",
1850         num1, num2, name.c_str());
1851 #endif
1852
1853       return 1;
1854 #endif
1855     }
1856   }
1857
1858   /* Compare heap */
1859   if (simgrid::mc::mmalloc_compare_heap(s1, s2) > 0) {
1860
1861 #ifdef MC_DEBUG
1862     XBT_DEBUG("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
1863     errors++;
1864 #else
1865
1866 #ifdef MC_VERBOSE
1867     XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
1868 #endif
1869
1870     return 1;
1871 #endif
1872   }
1873
1874   simgrid::mc::reset_heap_information();
1875
1876 #ifdef MC_VERBOSE
1877   if (errors || hash_result)
1878     XBT_VERB("(%d - %d) Difference found", num1, num2);
1879   else
1880     XBT_VERB("(%d - %d) No difference found", num1, num2);
1881 #endif
1882
1883 #if defined(MC_DEBUG) && defined(MC_VERBOSE)
1884   if (_sg_mc_hash) {
1885     // * false positive SHOULD be avoided.
1886     // * There MUST not be any false negative.
1887
1888     XBT_VERB("(%d - %d) State equality hash test is %s %s", num1, num2,
1889              (hash_result != 0) == (errors != 0) ? "true" : "false",
1890              !hash_result ? "positive" : "negative");
1891   }
1892 #endif
1893
1894   return errors > 0 || hash_result;
1895 }
1896
1897 }
1898 }