Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
mc: reduce the visibility of <dwarf.h> by a tiny bit
[simgrid.git] / src / mc / compare.cpp
1 /* Copyright (c) 2008-2019. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 /** \file compare.cpp Memory snapshooting and comparison                    */
7
8 #include "src/mc/mc_config.hpp"
9 #include "src/mc/mc_private.hpp"
10 #include "src/mc/mc_smx.hpp"
11 #include "src/mc/sosp/Snapshot.hpp"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_compare, xbt, "Logging specific to mc_compare in mc");
14
15 namespace simgrid {
16 namespace mc {
17
18 struct HeapLocation;
19 typedef std::array<HeapLocation, 2> HeapLocationPair;
20 typedef std::set<HeapLocationPair> HeapLocationPairs;
21 struct HeapArea;
22 struct ProcessComparisonState;
23 struct StateComparator;
24
25 static int compare_heap_area(StateComparator& state, const void* area1, const void* area2, Snapshot* snapshot1,
26                              Snapshot* snapshot2, HeapLocationPairs* previous, Type* type, int pointer_level);
27 }
28 }
29
30 using simgrid::mc::remote;
31
32 /*********************************** Heap comparison ***********************************/
33 /***************************************************************************************/
34
35 namespace simgrid {
36 namespace mc {
37
38 class HeapLocation {
39 public:
40   int block_    = 0;
41   int fragment_ = 0;
42
43   HeapLocation() = default;
44   HeapLocation(int block, int fragment = 0) : block_(block), fragment_(fragment) {}
45
46   bool operator==(HeapLocation const& that) const
47   {
48     return block_ == that.block_ && fragment_ == that.fragment_;
49   }
50   bool operator<(HeapLocation const& that) const
51   {
52     return std::make_pair(block_, fragment_) < std::make_pair(that.block_, that.fragment_);
53   }
54 };
55
56 static inline
57 HeapLocationPair makeHeapLocationPair(int block1, int fragment1, int block2, int fragment2)
58 {
59   return simgrid::mc::HeapLocationPair{{
60     simgrid::mc::HeapLocation(block1, fragment1),
61     simgrid::mc::HeapLocation(block2, fragment2)
62   }};
63 }
64
65 class HeapArea : public HeapLocation {
66 public:
67   bool valid_ = false;
68   HeapArea() = default;
69   explicit HeapArea(int block) : valid_(true) { block_ = block; }
70   HeapArea(int block, int fragment) : valid_(true)
71   {
72     block_    = block;
73     fragment_ = fragment;
74   }
75 };
76
77 class ProcessComparisonState {
78 public:
79   std::vector<simgrid::mc::IgnoredHeapRegion>* to_ignore = nullptr;
80   std::vector<HeapArea> equals_to;
81   std::vector<simgrid::mc::Type*> types;
82   std::size_t heapsize = 0;
83
84   void initHeapInformation(xbt_mheap_t heap, std::vector<simgrid::mc::IgnoredHeapRegion>* i);
85 };
86
87 namespace {
88
89 /** A hash which works with more stuff
90  *
91  *  It can hash pairs: the standard hash currently doesn't include this.
92  */
93 template <class X> class hash : public std::hash<X> {
94 };
95
96 template <class X, class Y> class hash<std::pair<X, Y>> {
97 public:
98   std::size_t operator()(std::pair<X,Y>const& x) const
99   {
100     hash<X> h1;
101     hash<X> h2;
102     return h1(x.first) ^ h2(x.second);
103   }
104 };
105
106 }
107
108 class StateComparator {
109 public:
110   s_xbt_mheap_t std_heap_copy;
111   std::size_t heaplimit;
112   std::array<ProcessComparisonState, 2> processStates;
113
114   std::unordered_set<std::pair<void*, void*>, hash<std::pair<void*, void*>>> compared_pointers;
115
116   void clear()
117   {
118     compared_pointers.clear();
119   }
120
121   int initHeapInformation(
122     xbt_mheap_t heap1, xbt_mheap_t heap2,
123     std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
124     std::vector<simgrid::mc::IgnoredHeapRegion>* i2);
125
126   HeapArea& equals_to1_(std::size_t i, std::size_t j)
127   {
128     return processStates[0].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
129   }
130   HeapArea& equals_to2_(std::size_t i, std::size_t j)
131   {
132     return processStates[1].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
133   }
134   Type*& types1_(std::size_t i, std::size_t j)
135   {
136     return processStates[0].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
137   }
138   Type*& types2_(std::size_t i, std::size_t j)
139   {
140     return processStates[1].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
141   }
142
143   HeapArea const& equals_to1_(std::size_t i, std::size_t j) const
144   {
145     return processStates[0].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
146   }
147   HeapArea const& equals_to2_(std::size_t i, std::size_t j) const
148   {
149     return processStates[1].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
150   }
151   Type* const& types1_(std::size_t i, std::size_t j) const
152   {
153     return processStates[0].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
154   }
155   Type* const& types2_(std::size_t i, std::size_t j) const
156   {
157     return processStates[1].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
158   }
159
160   /** Check whether two blocks are known to be matching
161    *
162    *  @param b1     Block of state 1
163    *  @param b2     Block of state 2
164    *  @return       if the blocks are known to be matching
165    */
166   bool blocksEqual(int b1, int b2) const
167   {
168     return this->equals_to1_(b1, 0).block_ == b2 && this->equals_to2_(b2, 0).block_ == b1;
169   }
170
171   /** Check whether two fragments are known to be matching
172    *
173    *  @param b1     Block of state 1
174    *  @param f1     Fragment of state 1
175    *  @param b2     Block of state 2
176    *  @param f2     Fragment of state 2
177    *  @return       if the fragments are known to be matching
178    */
179   int fragmentsEqual(int b1, int f1, int b2, int f2) const
180   {
181     return this->equals_to1_(b1, f1).block_ == b2 && this->equals_to1_(b1, f1).fragment_ == f2 &&
182            this->equals_to2_(b2, f2).block_ == b1 && this->equals_to2_(b2, f2).fragment_ == f1;
183   }
184
185   void match_equals(HeapLocationPairs* list);
186 };
187
188 }
189 }
190
191 /************************************************************************************/
192
193 static ssize_t heap_comparison_ignore_size(
194   std::vector<simgrid::mc::IgnoredHeapRegion>* ignore_list,
195   const void *address)
196 {
197   int start = 0;
198   int end = ignore_list->size() - 1;
199
200   while (start <= end) {
201     unsigned int cursor = (start + end) / 2;
202     simgrid::mc::IgnoredHeapRegion const& region = (*ignore_list)[cursor];
203     if (region.address == address)
204       return region.size;
205     if (region.address < address)
206       start = cursor + 1;
207     if (region.address > address)
208       end = cursor - 1;
209   }
210
211   return -1;
212 }
213
214 static bool is_stack(const void *address)
215 {
216   for (auto const& stack : mc_model_checker->process().stack_areas())
217     if (address == stack.address)
218       return true;
219   return false;
220 }
221
222 // TODO, this should depend on the snapshot?
223 static bool is_block_stack(int block)
224 {
225   for (auto const& stack : mc_model_checker->process().stack_areas())
226     if (block == stack.block)
227       return true;
228   return false;
229 }
230
231 namespace simgrid {
232 namespace mc {
233
234 void StateComparator::match_equals(HeapLocationPairs* list)
235 {
236   for (auto const& pair : *list) {
237     if (pair[0].fragment_ != -1) {
238       this->equals_to1_(pair[0].block_, pair[0].fragment_) = simgrid::mc::HeapArea(pair[1].block_, pair[1].fragment_);
239       this->equals_to2_(pair[1].block_, pair[1].fragment_) = simgrid::mc::HeapArea(pair[0].block_, pair[0].fragment_);
240     } else {
241       this->equals_to1_(pair[0].block_, 0) = simgrid::mc::HeapArea(pair[1].block_, pair[1].fragment_);
242       this->equals_to2_(pair[1].block_, 0) = simgrid::mc::HeapArea(pair[0].block_, pair[0].fragment_);
243     }
244   }
245 }
246
247 void ProcessComparisonState::initHeapInformation(xbt_mheap_t heap,
248                         std::vector<simgrid::mc::IgnoredHeapRegion>* i)
249 {
250   auto heaplimit  = heap->heaplimit;
251   this->heapsize  = heap->heapsize;
252   this->to_ignore = i;
253   this->equals_to.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, HeapArea());
254   this->types.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, nullptr);
255 }
256
257 int StateComparator::initHeapInformation(xbt_mheap_t heap1, xbt_mheap_t heap2,
258                           std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
259                           std::vector<simgrid::mc::IgnoredHeapRegion>* i2)
260 {
261   if ((heap1->heaplimit != heap2->heaplimit) || (heap1->heapsize != heap2->heapsize))
262     return -1;
263   this->heaplimit     = heap1->heaplimit;
264   this->std_heap_copy = *mc_model_checker->process().get_heap();
265   this->processStates[0].initHeapInformation(heap1, i1);
266   this->processStates[1].initHeapInformation(heap2, i2);
267   return 0;
268 }
269
270 // TODO, have a robust way to find it in O(1)
271 static inline Region* MC_get_heap_region(Snapshot* snapshot)
272 {
273   for (auto const& region : snapshot->snapshot_regions_)
274     if (region->region_type() == simgrid::mc::RegionType::Heap)
275       return region.get();
276   xbt_die("No heap region");
277 }
278
279 static bool mmalloc_heap_equal(simgrid::mc::StateComparator& state, simgrid::mc::Snapshot* snapshot1,
280                                simgrid::mc::Snapshot* snapshot2)
281 {
282   simgrid::mc::RemoteClient* process = &mc_model_checker->process();
283
284   /* Check busy blocks */
285   size_t i1 = 1;
286
287   malloc_info heapinfo_temp1;
288   malloc_info heapinfo_temp2;
289   malloc_info heapinfo_temp2b;
290
291   simgrid::mc::Region* heap_region1 = MC_get_heap_region(snapshot1);
292   simgrid::mc::Region* heap_region2 = MC_get_heap_region(snapshot2);
293
294   // This is the address of std_heap->heapinfo in the application process:
295   void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
296
297   // This is in snapshot do not use them directly:
298   const malloc_info* heapinfos1 =
299       snapshot1->read<malloc_info*>(RemotePtr<malloc_info*>((std::uint64_t)heapinfo_address));
300   const malloc_info* heapinfos2 =
301       snapshot2->read<malloc_info*>(RemotePtr<malloc_info*>((std::uint64_t)heapinfo_address));
302
303   while (i1 < state.heaplimit) {
304
305     const malloc_info* heapinfo1 =
306         (const malloc_info*)heap_region1->read(&heapinfo_temp1, &heapinfos1[i1], sizeof(malloc_info));
307     const malloc_info* heapinfo2 =
308         (const malloc_info*)heap_region2->read(&heapinfo_temp2, &heapinfos2[i1], sizeof(malloc_info));
309
310     if (heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type == MMALLOC_TYPE_HEAPINFO) {      /* Free block */
311       i1 ++;
312       continue;
313     }
314
315     if (heapinfo1->type < 0) {
316       fprintf(stderr, "Unkown mmalloc block type.\n");
317       abort();
318     }
319
320     void* addr_block1 = ((void*)(((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase));
321
322     if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED) {       /* Large block */
323
324       if (is_stack(addr_block1)) {
325         for (size_t k = 0; k < heapinfo1->busy_block.size; k++)
326           state.equals_to1_(i1 + k, 0) = HeapArea(i1, -1);
327         for (size_t k = 0; k < heapinfo2->busy_block.size; k++)
328           state.equals_to2_(i1 + k, 0) = HeapArea(i1, -1);
329         i1 += heapinfo1->busy_block.size;
330         continue;
331       }
332
333       if (state.equals_to1_(i1, 0).valid_) {
334         i1++;
335         continue;
336       }
337
338       size_t i2 = 1;
339       bool equal = false;
340
341       /* Try first to associate to same block in the other heap */
342       if (heapinfo2->type == heapinfo1->type && state.equals_to2_(i1, 0).valid_ == 0) {
343         void* addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
344         int res_compare = compare_heap_area(state, addr_block1, addr_block2, snapshot1, snapshot2, nullptr, nullptr, 0);
345         if (res_compare != 1) {
346           for (size_t k = 1; k < heapinfo2->busy_block.size; k++)
347             state.equals_to2_(i1 + k, 0) = HeapArea(i1, -1);
348           for (size_t k = 1; k < heapinfo1->busy_block.size; k++)
349             state.equals_to1_(i1 + k, 0) = HeapArea(i1, -1);
350           equal = true;
351           i1 += heapinfo1->busy_block.size;
352         }
353       }
354
355       while (i2 < state.heaplimit && not equal) {
356
357         void* addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
358
359         if (i2 == i1) {
360           i2++;
361           continue;
362         }
363
364         const malloc_info* heapinfo2b =
365             (const malloc_info*)heap_region2->read(&heapinfo_temp2b, &heapinfos2[i2], sizeof(malloc_info));
366
367         if (heapinfo2b->type != MMALLOC_TYPE_UNFRAGMENTED) {
368           i2++;
369           continue;
370         }
371
372         if (state.equals_to2_(i2, 0).valid_) {
373           i2++;
374           continue;
375         }
376
377         int res_compare = compare_heap_area(state, addr_block1, addr_block2, snapshot1, snapshot2, nullptr, nullptr, 0);
378
379         if (res_compare != 1) {
380           for (size_t k = 1; k < heapinfo2b->busy_block.size; k++)
381             state.equals_to2_(i2 + k, 0) = HeapArea(i1, -1);
382           for (size_t k = 1; k < heapinfo1->busy_block.size; k++)
383             state.equals_to1_(i1 + k, 0) = HeapArea(i2, -1);
384           equal = true;
385           i1 += heapinfo1->busy_block.size;
386         }
387
388         i2++;
389       }
390
391       if (not equal) {
392         XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1, heapinfo1->busy_block.busy_size, addr_block1);
393         i1 = state.heaplimit + 1;
394         return false;
395       }
396
397     } else {                    /* Fragmented block */
398
399       for (size_t j1 = 0; j1 < (size_t)(BLOCKSIZE >> heapinfo1->type); j1++) {
400
401         if (heapinfo1->busy_frag.frag_size[j1] == -1) /* Free fragment_ */
402           continue;
403
404         if (state.equals_to1_(i1, j1).valid_)
405           continue;
406
407         void* addr_frag1 = (void*)((char*)addr_block1 + (j1 << heapinfo1->type));
408
409         size_t i2 = 1;
410         bool equal = false;
411
412         /* Try first to associate to same fragment_ in the other heap */
413         if (heapinfo2->type == heapinfo1->type && not state.equals_to2_(i1, j1).valid_) {
414           void* addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
415           void* addr_frag2  = (void*)((char*)addr_block2 + (j1 << heapinfo2->type));
416           int res_compare = compare_heap_area(state, addr_frag1, addr_frag2, snapshot1, snapshot2, nullptr, nullptr, 0);
417           if (res_compare != 1)
418             equal = true;
419         }
420
421         while (i2 < state.heaplimit && not equal) {
422
423           const malloc_info* heapinfo2b =
424               (const malloc_info*)heap_region2->read(&heapinfo_temp2b, &heapinfos2[i2], sizeof(malloc_info));
425
426           if (heapinfo2b->type == MMALLOC_TYPE_FREE || heapinfo2b->type == MMALLOC_TYPE_HEAPINFO) {
427             i2 ++;
428             continue;
429           }
430
431           // We currently do not match fragments with unfragmented blocks (maybe we should).
432           if (heapinfo2b->type == MMALLOC_TYPE_UNFRAGMENTED) {
433             i2++;
434             continue;
435           }
436
437           if (heapinfo2b->type < 0) {
438             fprintf(stderr, "Unknown mmalloc block type.\n");
439             abort();
440           }
441
442           for (size_t j2 = 0; j2 < (size_t)(BLOCKSIZE >> heapinfo2b->type); j2++) {
443
444             if (i2 == i1 && j2 == j1)
445               continue;
446
447             if (state.equals_to2_(i2, j2).valid_)
448               continue;
449
450             void* addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
451             void* addr_frag2  = (void*)((char*)addr_block2 + (j2 << heapinfo2b->type));
452
453             int res_compare =
454                 compare_heap_area(state, addr_frag1, addr_frag2, snapshot2, snapshot2, nullptr, nullptr, 0);
455             if (res_compare != 1) {
456               equal = true;
457               break;
458             }
459           }
460
461           i2++;
462         }
463
464         if (not equal) {
465           XBT_DEBUG("Block %zu, fragment_ %zu not found (size_used = %zd, address = %p)\n", i1, j1,
466                     heapinfo1->busy_frag.frag_size[j1], addr_frag1);
467           i1 = state.heaplimit + 1;
468           return false;
469         }
470       }
471
472       i1++;
473     }
474   }
475
476   /* All blocks/fragments are equal to another block/fragment_ ? */
477   for (size_t i = 1; i < state.heaplimit; i++) {
478     const malloc_info* heapinfo1 =
479         (const malloc_info*)heap_region1->read(&heapinfo_temp1, &heapinfos1[i], sizeof(malloc_info));
480
481     if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED && i1 == state.heaplimit && heapinfo1->busy_block.busy_size > 0 &&
482         not state.equals_to1_(i, 0).valid_) {
483       XBT_DEBUG("Block %zu not found (size used = %zu)", i, heapinfo1->busy_block.busy_size);
484       return false;
485     }
486
487     if (heapinfo1->type <= 0)
488       continue;
489     for (size_t j = 0; j < (size_t)(BLOCKSIZE >> heapinfo1->type); j++)
490       if (i1 == state.heaplimit && heapinfo1->busy_frag.frag_size[j] > 0 && not state.equals_to1_(i, j).valid_) {
491         XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)", i, j, heapinfo1->busy_frag.frag_size[j]);
492         return false;
493       }
494   }
495
496   for (size_t i = 1; i < state.heaplimit; i++) {
497     const malloc_info* heapinfo2 =
498         (const malloc_info*)heap_region2->read(&heapinfo_temp2, &heapinfos2[i], sizeof(malloc_info));
499     if (heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED && i1 == state.heaplimit && heapinfo2->busy_block.busy_size > 0 &&
500         not state.equals_to2_(i, 0).valid_) {
501       XBT_DEBUG("Block %zu not found (size used = %zu)", i,
502                 heapinfo2->busy_block.busy_size);
503       return false;
504     }
505
506     if (heapinfo2->type <= 0)
507       continue;
508
509     for (size_t j = 0; j < (size_t)(BLOCKSIZE >> heapinfo2->type); j++)
510       if (i1 == state.heaplimit && heapinfo2->busy_frag.frag_size[j] > 0 && not state.equals_to2_(i, j).valid_) {
511         XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)",
512           i, j, heapinfo2->busy_frag.frag_size[j]);
513         return false;
514       }
515   }
516
517   return true;
518 }
519
520 /**
521  *
522  * @param state
523  * @param real_area1     Process address for state 1
524  * @param real_area2     Process address for state 2
525  * @param snapshot1      Snapshot of state 1
526  * @param snapshot2      Snapshot of state 2
527  * @param previous
528  * @param size
529  * @param check_ignore
530  */
531 static bool heap_area_equal_without_type(simgrid::mc::StateComparator& state, const void* real_area1,
532                                          const void* real_area2, simgrid::mc::Snapshot* snapshot1,
533                                          simgrid::mc::Snapshot* snapshot2, HeapLocationPairs* previous, int size,
534                                          int check_ignore)
535 {
536   simgrid::mc::RemoteClient* process = &mc_model_checker->process();
537   simgrid::mc::Region* heap_region1  = MC_get_heap_region(snapshot1);
538   simgrid::mc::Region* heap_region2  = MC_get_heap_region(snapshot2);
539
540   for (int i = 0; i < size; ) {
541
542     if (check_ignore > 0) {
543       ssize_t ignore1 = heap_comparison_ignore_size(state.processStates[0].to_ignore, (char*)real_area1 + i);
544       if (ignore1 != -1) {
545         ssize_t ignore2 = heap_comparison_ignore_size(state.processStates[1].to_ignore, (char*)real_area2 + i);
546         if (ignore2 == ignore1) {
547           if (ignore1 == 0) {
548             check_ignore--;
549             return true;
550           } else {
551             i = i + ignore2;
552             check_ignore--;
553             continue;
554           }
555         }
556       }
557     }
558
559     if (MC_snapshot_region_memcmp(((char *) real_area1) + i, heap_region1, ((char *) real_area2) + i, heap_region2, 1) != 0) {
560
561       int pointer_align = (i / sizeof(void *)) * sizeof(void *);
562       const void* addr_pointed1 = snapshot1->read(remote((void**)((char*)real_area1 + pointer_align)));
563       const void* addr_pointed2 = snapshot2->read(remote((void**)((char*)real_area2 + pointer_align)));
564
565       if (process->in_maestro_stack(remote(addr_pointed1))
566         && process->in_maestro_stack(remote(addr_pointed2))) {
567         i = pointer_align + sizeof(void *);
568         continue;
569       }
570
571       if (addr_pointed1 > state.std_heap_copy.heapbase
572            && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
573            && addr_pointed2 > state.std_heap_copy.heapbase
574            && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)) {
575         // Both addresses are in the heap:
576         int res_compare =
577             compare_heap_area(state, addr_pointed1, addr_pointed2, snapshot1, snapshot2, previous, nullptr, 0);
578         if (res_compare == 1)
579           return false;
580         i = pointer_align + sizeof(void *);
581         continue;
582       }
583
584       return false;
585     }
586
587     i++;
588   }
589
590   return true;
591 }
592
593 /**
594  *
595  * @param state
596  * @param real_area1     Process address for state 1
597  * @param real_area2     Process address for state 2
598  * @param snapshot1      Snapshot of state 1
599  * @param snapshot2      Snapshot of state 2
600  * @param previous
601  * @param type
602  * @param area_size      either a byte_size or an elements_count (?)
603  * @param check_ignore
604  * @param pointer_level
605  * @return               0 (same), 1 (different), -1 (unknown)
606  */
607 static int compare_heap_area_with_type(simgrid::mc::StateComparator& state, const void* real_area1,
608                                        const void* real_area2, simgrid::mc::Snapshot* snapshot1,
609                                        simgrid::mc::Snapshot* snapshot2, HeapLocationPairs* previous,
610                                        simgrid::mc::Type* type, int area_size, int check_ignore, int pointer_level)
611 {
612   // HACK: This should not happen but in pratice, there are some
613   // DW_TAG_typedef without an associated DW_AT_type:
614   //<1><538832>: Abbrev Number: 111 (DW_TAG_typedef)
615   //    <538833>   DW_AT_name        : (indirect string, offset: 0x2292f3): gregset_t
616   //    <538837>   DW_AT_decl_file   : 98
617   //    <538838>   DW_AT_decl_line   : 37
618   if (type == nullptr)
619     return 0;
620
621   if (is_stack(real_area1) && is_stack(real_area2))
622     return 0;
623
624   if (check_ignore > 0) {
625     ssize_t ignore1 = heap_comparison_ignore_size(state.processStates[0].to_ignore, real_area1);
626     if (ignore1 > 0 && heap_comparison_ignore_size(state.processStates[1].to_ignore, real_area2) == ignore1)
627       return 0;
628   }
629
630   simgrid::mc::Type* subtype;
631   simgrid::mc::Type* subsubtype;
632   int elm_size;
633   const void* addr_pointed1;
634   const void* addr_pointed2;
635
636   simgrid::mc::Region* heap_region1 = MC_get_heap_region(snapshot1);
637   simgrid::mc::Region* heap_region2 = MC_get_heap_region(snapshot2);
638
639   switch (type->type) {
640     case DW_TAG_unspecified_type:
641       return 1;
642
643     case DW_TAG_base_type:
644       if (not type->name.empty() && type->name == "char") { /* String, hence random (arbitrary ?) size */
645         if (real_area1 == real_area2)
646           return -1;
647         else
648           return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, area_size) != 0;
649       } else {
650         if (area_size != -1 && type->byte_size != area_size)
651           return -1;
652         else
653           return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
654       }
655
656     case DW_TAG_enumeration_type:
657       if (area_size != -1 && type->byte_size != area_size)
658         return -1;
659       return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
660
661     case DW_TAG_typedef:
662     case DW_TAG_const_type:
663     case DW_TAG_volatile_type:
664       return compare_heap_area_with_type(state, real_area1, real_area2, snapshot1, snapshot2, previous, type->subtype,
665                                          area_size, check_ignore, pointer_level);
666
667     case DW_TAG_array_type:
668       subtype = type->subtype;
669       switch (subtype->type) {
670         case DW_TAG_unspecified_type:
671           return 1;
672
673         case DW_TAG_base_type:
674         case DW_TAG_enumeration_type:
675         case DW_TAG_pointer_type:
676         case DW_TAG_reference_type:
677         case DW_TAG_rvalue_reference_type:
678         case DW_TAG_structure_type:
679         case DW_TAG_class_type:
680         case DW_TAG_union_type:
681           if (subtype->full_type)
682             subtype = subtype->full_type;
683           elm_size  = subtype->byte_size;
684           break;
685         // TODO, just remove the type indirection?
686         case DW_TAG_const_type:
687         case DW_TAG_typedef:
688         case DW_TAG_volatile_type:
689           subsubtype = subtype->subtype;
690           if (subsubtype->full_type)
691             subsubtype = subsubtype->full_type;
692           elm_size     = subsubtype->byte_size;
693           break;
694         default:
695           return 0;
696       }
697       for (int i = 0; i < type->element_count; i++) {
698         // TODO, add support for variable stride (DW_AT_byte_stride)
699         int res = compare_heap_area_with_type(state, (char*)real_area1 + (i * elm_size),
700                                               (char*)real_area2 + (i * elm_size), snapshot1, snapshot2, previous,
701                                               type->subtype, subtype->byte_size, check_ignore, pointer_level);
702         if (res == 1)
703           return res;
704       }
705       return 0;
706
707     case DW_TAG_reference_type:
708     case DW_TAG_rvalue_reference_type:
709     case DW_TAG_pointer_type:
710       if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
711         addr_pointed1 = snapshot1->read(remote((void**)real_area1));
712         addr_pointed2 = snapshot2->read(remote((void**)real_area2));
713         return (addr_pointed1 != addr_pointed2);
714       }
715       pointer_level++;
716       if (pointer_level <= 1) {
717         addr_pointed1 = snapshot1->read(remote((void**)real_area1));
718         addr_pointed2 = snapshot2->read(remote((void**)real_area2));
719         if (addr_pointed1 > state.std_heap_copy.heapbase && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1) &&
720             addr_pointed2 > state.std_heap_copy.heapbase && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
721           return compare_heap_area(state, addr_pointed1, addr_pointed2, snapshot1, snapshot2, previous, type->subtype,
722                                    pointer_level);
723         else
724           return (addr_pointed1 != addr_pointed2);
725       }
726       for (size_t i = 0; i < (area_size / sizeof(void*)); i++) {
727         addr_pointed1 = snapshot1->read(remote((void**)((char*)real_area1 + i * sizeof(void*))));
728         addr_pointed2 = snapshot2->read(remote((void**)((char*)real_area2 + i * sizeof(void*))));
729         int res;
730         if (addr_pointed1 > state.std_heap_copy.heapbase && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1) &&
731             addr_pointed2 > state.std_heap_copy.heapbase && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
732           res = compare_heap_area(state, addr_pointed1, addr_pointed2, snapshot1, snapshot2, previous, type->subtype,
733                                   pointer_level);
734         else
735           res = (addr_pointed1 != addr_pointed2);
736         if (res == 1)
737           return res;
738       }
739       return 0;
740
741     case DW_TAG_structure_type:
742     case DW_TAG_class_type:
743       if (type->full_type)
744         type = type->full_type;
745       if (area_size != -1 && type->byte_size != area_size) {
746         if (area_size <= type->byte_size || area_size % type->byte_size != 0)
747           return -1;
748         for (size_t i = 0; i < (size_t)(area_size / type->byte_size); i++) {
749           int res = compare_heap_area_with_type(state, (char*)real_area1 + i * type->byte_size,
750                                                 (char*)real_area2 + i * type->byte_size, snapshot1, snapshot2, previous,
751                                                 type, -1, check_ignore, 0);
752           if (res == 1)
753             return res;
754         }
755         } else {
756           for (simgrid::mc::Member& member : type->members) {
757             // TODO, optimize this? (for the offset case)
758             void* real_member1 =
759                 simgrid::dwarf::resolve_member(real_area1, type, &member, (simgrid::mc::AddressSpace*)snapshot1);
760             void* real_member2 =
761                 simgrid::dwarf::resolve_member(real_area2, type, &member, (simgrid::mc::AddressSpace*)snapshot2);
762             int res = compare_heap_area_with_type(state, real_member1, real_member2, snapshot1, snapshot2, previous,
763                                                   member.type, -1, check_ignore, 0);
764             if (res == 1)
765               return res;
766           }
767         }
768         return 0;
769
770     case DW_TAG_union_type:
771       return not heap_area_equal_without_type(state, real_area1, real_area2, snapshot1, snapshot2, previous,
772                                               type->byte_size, check_ignore);
773   }
774   return 0;
775 }
776
777 /** Infer the type of a part of the block from the type of the block
778  *
779  * TODO, handle DW_TAG_array_type as well as arrays of the object ((*p)[5], p[5])
780  *
781  * TODO, handle subfields ((*p).bar.foo, (*p)[5].bar…)
782  *
783  * @param  type               DWARF type ID of the root address
784  * @param  area_size
785  * @return                    DWARF type ID for given offset
786  */
787 static simgrid::mc::Type* get_offset_type(void* real_base_address, simgrid::mc::Type* type, int offset, int area_size,
788                                           simgrid::mc::Snapshot* snapshot)
789 {
790
791   // Beginning of the block, the infered variable type if the type of the block:
792   if (offset == 0)
793     return type;
794
795   switch (type->type) {
796
797   case DW_TAG_structure_type:
798   case DW_TAG_class_type:
799     if (type->full_type)
800       type = type->full_type;
801     if (area_size != -1 && type->byte_size != area_size) {
802       if (area_size > type->byte_size && area_size % type->byte_size == 0)
803         return type;
804       else
805         return nullptr;
806     }
807
808     for (simgrid::mc::Member& member : type->members) {
809       if (member.has_offset_location()) {
810         // We have the offset, use it directly (shortcut):
811         if (member.offset() == offset)
812           return member.type;
813       } else {
814         void* real_member = simgrid::dwarf::resolve_member(real_base_address, type, &member, snapshot);
815         if ((char*)real_member - (char*)real_base_address == offset)
816           return member.type;
817       }
818     }
819     return nullptr;
820
821   default:
822     /* FIXME: other cases ? */
823     return nullptr;
824
825   }
826 }
827
828 /**
829  *
830  * @param area1          Process address for state 1
831  * @param area2          Process address for state 2
832  * @param snapshot1      Snapshot of state 1
833  * @param snapshot2      Snapshot of state 2
834  * @param previous       Pairs of blocks already compared on the current path (or nullptr)
835  * @param type_id        Type of variable
836  * @param pointer_level
837  * @return 0 (same), 1 (different), -1
838  */
839 static int compare_heap_area(simgrid::mc::StateComparator& state, const void* area1, const void* area2,
840                              simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2,
841                              HeapLocationPairs* previous, simgrid::mc::Type* type, int pointer_level)
842 {
843   simgrid::mc::RemoteClient* process = &mc_model_checker->process();
844
845   ssize_t block1;
846   ssize_t block2;
847   ssize_t size;
848   int check_ignore = 0;
849
850   int type_size = -1;
851   int offset1   = 0;
852   int offset2   = 0;
853   int new_size1 = -1;
854   int new_size2 = -1;
855
856   simgrid::mc::Type* new_type1 = nullptr;
857   simgrid::mc::Type* new_type2 = nullptr;
858
859   bool match_pairs = false;
860
861   // This is the address of std_heap->heapinfo in the application process:
862   void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
863
864   const malloc_info* heapinfos1 = snapshot1->read(remote((const malloc_info**)heapinfo_address));
865   const malloc_info* heapinfos2 = snapshot2->read(remote((const malloc_info**)heapinfo_address));
866
867   malloc_info heapinfo_temp1;
868   malloc_info heapinfo_temp2;
869
870   simgrid::mc::HeapLocationPairs current;
871   if (previous == nullptr) {
872     previous = &current;
873     match_pairs = true;
874   }
875
876   // Get block number:
877   block1 = ((char*)area1 - (char*)state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
878   block2 = ((char*)area2 - (char*)state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
879
880   // If either block is a stack block:
881   if (is_block_stack((int) block1) && is_block_stack((int) block2)) {
882     previous->insert(simgrid::mc::makeHeapLocationPair(block1, -1, block2, -1));
883     if (match_pairs)
884       state.match_equals(previous);
885     return 0;
886   }
887
888   // If either block is not in the expected area of memory:
889   if (((char*)area1 < (char*)state.std_heap_copy.heapbase) || (block1 > (ssize_t)state.processStates[0].heapsize) ||
890       (block1 < 1) || ((char*)area2 < (char*)state.std_heap_copy.heapbase) ||
891       (block2 > (ssize_t)state.processStates[1].heapsize) || (block2 < 1)) {
892     return 1;
893   }
894
895   // Process address of the block:
896   void* real_addr_block1 = (ADDR2UINT(block1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
897   void* real_addr_block2 = (ADDR2UINT(block2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
898
899   if (type) {
900     if (type->full_type)
901       type = type->full_type;
902
903     // This assume that for "boring" types (volatile ...) byte_size is absent:
904     while (type->byte_size == 0 && type->subtype != nullptr)
905       type = type->subtype;
906
907     // Find type_size:
908     if (type->type == DW_TAG_pointer_type ||
909         (type->type == DW_TAG_base_type && not type->name.empty() && type->name == "char"))
910       type_size = -1;
911     else
912       type_size = type->byte_size;
913
914   }
915
916   simgrid::mc::Region* heap_region1 = MC_get_heap_region(snapshot1);
917   simgrid::mc::Region* heap_region2 = MC_get_heap_region(snapshot2);
918
919   const malloc_info* heapinfo1 =
920       (const malloc_info*)heap_region1->read(&heapinfo_temp1, &heapinfos1[block1], sizeof(malloc_info));
921   const malloc_info* heapinfo2 =
922       (const malloc_info*)heap_region2->read(&heapinfo_temp2, &heapinfos2[block2], sizeof(malloc_info));
923
924   if ((heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type==MMALLOC_TYPE_HEAPINFO)
925     && (heapinfo2->type == MMALLOC_TYPE_FREE || heapinfo2->type ==MMALLOC_TYPE_HEAPINFO)) {
926     /* Free block */
927     if (match_pairs)
928       state.match_equals(previous);
929     return 0;
930   }
931
932   if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED && heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
933     /* Complete block */
934
935     // TODO, lookup variable type from block type as done for fragmented blocks
936
937     if (state.equals_to1_(block1, 0).valid_ && state.equals_to2_(block2, 0).valid_ &&
938         state.blocksEqual(block1, block2)) {
939       if (match_pairs)
940         state.match_equals(previous);
941       return 0;
942     }
943
944     if (type_size != -1 && type_size != (ssize_t)heapinfo1->busy_block.busy_size &&
945         type_size != (ssize_t)heapinfo2->busy_block.busy_size &&
946         (type->name.empty() || type->name == "struct s_smx_context")) {
947       if (match_pairs)
948         state.match_equals(previous);
949       return -1;
950     }
951
952     if (heapinfo1->busy_block.size != heapinfo2->busy_block.size)
953       return 1;
954     if (heapinfo1->busy_block.busy_size != heapinfo2->busy_block.busy_size)
955       return 1;
956
957     if (not previous->insert(simgrid::mc::makeHeapLocationPair(block1, -1, block2, -1)).second) {
958       if (match_pairs)
959         state.match_equals(previous);
960       return 0;
961     }
962
963     size = heapinfo1->busy_block.busy_size;
964
965     // Remember (basic) type inference.
966     // The current data structure only allows us to do this for the whole block.
967     if (type != nullptr && area1 == real_addr_block1)
968       state.types1_(block1, 0) = type;
969     if (type != nullptr && area2 == real_addr_block2)
970       state.types2_(block2, 0) = type;
971
972     if (size <= 0) {
973       if (match_pairs)
974         state.match_equals(previous);
975       return 0;
976     }
977
978     if (heapinfo1->busy_block.ignore > 0
979         && heapinfo2->busy_block.ignore == heapinfo1->busy_block.ignore)
980       check_ignore = heapinfo1->busy_block.ignore;
981
982   } else if ((heapinfo1->type > 0) && (heapinfo2->type > 0)) {      /* Fragmented block */
983
984     // Fragment number:
985     ssize_t frag1 = ((uintptr_t)(ADDR2UINT(area1) % (BLOCKSIZE))) >> heapinfo1->type;
986     ssize_t frag2 = ((uintptr_t)(ADDR2UINT(area2) % (BLOCKSIZE))) >> heapinfo2->type;
987
988     // Process address of the fragment_:
989     void* real_addr_frag1 = (void*)((char*)real_addr_block1 + (frag1 << heapinfo1->type));
990     void* real_addr_frag2 = (void*)((char*)real_addr_block2 + (frag2 << heapinfo2->type));
991
992     // Check the size of the fragments against the size of the type:
993     if (type_size != -1) {
994       if (heapinfo1->busy_frag.frag_size[frag1] == -1 || heapinfo2->busy_frag.frag_size[frag2] == -1) {
995         if (match_pairs)
996           state.match_equals(previous);
997         return -1;
998       }
999       // ?
1000       if (type_size != heapinfo1->busy_frag.frag_size[frag1]
1001           || type_size != heapinfo2->busy_frag.frag_size[frag2]) {
1002         if (match_pairs)
1003           state.match_equals(previous);
1004         return -1;
1005       }
1006     }
1007
1008     // Check if the blocks are already matched together:
1009     if (state.equals_to1_(block1, frag1).valid_ && state.equals_to2_(block2, frag2).valid_ && offset1 == offset2 &&
1010         state.fragmentsEqual(block1, frag1, block2, frag2)) {
1011       if (match_pairs)
1012         state.match_equals(previous);
1013       return 0;
1014     }
1015     // Compare the size of both fragments:
1016     if (heapinfo1->busy_frag.frag_size[frag1] != heapinfo2->busy_frag.frag_size[frag2]) {
1017       if (type_size == -1) {
1018         if (match_pairs)
1019           state.match_equals(previous);
1020         return -1;
1021       } else
1022         return 1;
1023     }
1024
1025     // Size of the fragment_:
1026     size = heapinfo1->busy_frag.frag_size[frag1];
1027
1028     // Remember (basic) type inference.
1029     // The current data structure only allows us to do this for the whole fragment_.
1030     if (type != nullptr && area1 == real_addr_frag1)
1031       state.types1_(block1, frag1) = type;
1032     if (type != nullptr && area2 == real_addr_frag2)
1033       state.types2_(block2, frag2) = type;
1034
1035     // The type of the variable is already known:
1036     if (type) {
1037       new_type1 = new_type2 = type;
1038     }
1039     // Type inference from the block type.
1040     else if (state.types1_(block1, frag1) != nullptr || state.types2_(block2, frag2) != nullptr) {
1041
1042       offset1 = (char*)area1 - (char*)real_addr_frag1;
1043       offset2 = (char*)area2 - (char*)real_addr_frag2;
1044
1045       if (state.types1_(block1, frag1) != nullptr && state.types2_(block2, frag2) != nullptr) {
1046         new_type1 = get_offset_type(real_addr_frag1, state.types1_(block1, frag1), offset1, size, snapshot1);
1047         new_type2 = get_offset_type(real_addr_frag2, state.types2_(block2, frag2), offset1, size, snapshot2);
1048       } else if (state.types1_(block1, frag1) != nullptr) {
1049         new_type1 = get_offset_type(real_addr_frag1, state.types1_(block1, frag1), offset1, size, snapshot1);
1050         new_type2 = get_offset_type(real_addr_frag2, state.types1_(block1, frag1), offset2, size, snapshot2);
1051       } else if (state.types2_(block2, frag2) != nullptr) {
1052         new_type1 = get_offset_type(real_addr_frag1, state.types2_(block2, frag2), offset1, size, snapshot1);
1053         new_type2 = get_offset_type(real_addr_frag2, state.types2_(block2, frag2), offset2, size, snapshot2);
1054       } else {
1055         if (match_pairs)
1056           state.match_equals(previous);
1057         return -1;
1058       }
1059
1060       if (new_type1 != nullptr && new_type2 != nullptr && new_type1 != new_type2) {
1061
1062         type = new_type1;
1063         while (type->byte_size == 0 && type->subtype != nullptr)
1064           type = type->subtype;
1065         new_size1 = type->byte_size;
1066
1067         type = new_type2;
1068         while (type->byte_size == 0 && type->subtype != nullptr)
1069           type = type->subtype;
1070         new_size2 = type->byte_size;
1071
1072       } else {
1073         if (match_pairs)
1074           state.match_equals(previous);
1075         return -1;
1076       }
1077     }
1078
1079     if (new_size1 > 0 && new_size1 == new_size2) {
1080       type = new_type1;
1081       size = new_size1;
1082     }
1083
1084     if (offset1 == 0 && offset2 == 0 &&
1085         not previous->insert(simgrid::mc::makeHeapLocationPair(block1, frag1, block2, frag2)).second) {
1086       if (match_pairs)
1087         state.match_equals(previous);
1088       return 0;
1089     }
1090
1091     if (size <= 0) {
1092       if (match_pairs)
1093         state.match_equals(previous);
1094       return 0;
1095     }
1096
1097     if ((heapinfo1->busy_frag.ignore[frag1] > 0) &&
1098         (heapinfo2->busy_frag.ignore[frag2] == heapinfo1->busy_frag.ignore[frag1]))
1099       check_ignore = heapinfo1->busy_frag.ignore[frag1];
1100
1101   } else
1102     return 1;
1103
1104
1105   /* Start comparison */
1106   int res_compare;
1107   if (type)
1108     res_compare = compare_heap_area_with_type(state, area1, area2, snapshot1, snapshot2, previous, type, size,
1109                                               check_ignore, pointer_level);
1110   else
1111     res_compare =
1112         not heap_area_equal_without_type(state, area1, area2, snapshot1, snapshot2, previous, size, check_ignore);
1113
1114   if (res_compare == 1)
1115     return res_compare;
1116
1117   if (match_pairs)
1118     state.match_equals(previous);
1119   return 0;
1120 }
1121
1122 }
1123 }
1124
1125 /************************** Snapshot comparison *******************************/
1126 /******************************************************************************/
1127
1128 static int compare_areas_with_type(simgrid::mc::StateComparator& state, void* real_area1,
1129                                    simgrid::mc::Snapshot* snapshot1, simgrid::mc::Region* region1, void* real_area2,
1130                                    simgrid::mc::Snapshot* snapshot2, simgrid::mc::Region* region2,
1131                                    simgrid::mc::Type* type, int pointer_level)
1132 {
1133   simgrid::mc::RemoteClient* process = &mc_model_checker->process();
1134
1135   simgrid::mc::Type* subtype;
1136   simgrid::mc::Type* subsubtype;
1137   int elm_size;
1138   int i;
1139   int res;
1140
1141   xbt_assert(type != nullptr);
1142   switch (type->type) {
1143     case DW_TAG_unspecified_type:
1144       return 1;
1145
1146     case DW_TAG_base_type:
1147     case DW_TAG_enumeration_type:
1148     case DW_TAG_union_type:
1149       return MC_snapshot_region_memcmp(real_area1, region1, real_area2, region2, type->byte_size) != 0;
1150     case DW_TAG_typedef:
1151     case DW_TAG_volatile_type:
1152     case DW_TAG_const_type:
1153       return compare_areas_with_type(state, real_area1, snapshot1, region1, real_area2, snapshot2, region2,
1154                                      type->subtype, pointer_level);
1155     case DW_TAG_array_type:
1156       subtype = type->subtype;
1157       switch (subtype->type) {
1158         case DW_TAG_unspecified_type:
1159           return 1;
1160
1161         case DW_TAG_base_type:
1162         case DW_TAG_enumeration_type:
1163         case DW_TAG_pointer_type:
1164         case DW_TAG_reference_type:
1165         case DW_TAG_rvalue_reference_type:
1166         case DW_TAG_structure_type:
1167         case DW_TAG_class_type:
1168         case DW_TAG_union_type:
1169           if (subtype->full_type)
1170             subtype = subtype->full_type;
1171           elm_size  = subtype->byte_size;
1172           break;
1173         case DW_TAG_const_type:
1174         case DW_TAG_typedef:
1175         case DW_TAG_volatile_type:
1176           subsubtype = subtype->subtype;
1177           if (subsubtype->full_type)
1178             subsubtype = subsubtype->full_type;
1179           elm_size     = subsubtype->byte_size;
1180           break;
1181         default:
1182           return 0;
1183       }
1184       for (i = 0; i < type->element_count; i++) {
1185         size_t off = i * elm_size;
1186         res = compare_areas_with_type(state, (char*)real_area1 + off, snapshot1, region1, (char*)real_area2 + off,
1187                                       snapshot2, region2, type->subtype, pointer_level);
1188         if (res == 1)
1189           return res;
1190       }
1191       break;
1192     case DW_TAG_pointer_type:
1193     case DW_TAG_reference_type:
1194     case DW_TAG_rvalue_reference_type: {
1195       void* addr_pointed1 = MC_region_read_pointer(region1, real_area1);
1196       void* addr_pointed2 = MC_region_read_pointer(region2, real_area2);
1197
1198       if (type->subtype && type->subtype->type == DW_TAG_subroutine_type)
1199         return (addr_pointed1 != addr_pointed2);
1200       if (addr_pointed1 == nullptr && addr_pointed2 == nullptr)
1201         return 0;
1202       if (addr_pointed1 == nullptr || addr_pointed2 == nullptr)
1203         return 1;
1204       if (not state.compared_pointers.insert(std::make_pair(addr_pointed1, addr_pointed2)).second)
1205         return 0;
1206
1207       pointer_level++;
1208
1209       // Some cases are not handled here:
1210       // * the pointers lead to different areas (one to the heap, the other to the RW segment ...)
1211       // * a pointer leads to the read-only segment of the current object
1212       // * a pointer lead to a different ELF object
1213
1214       if (addr_pointed1 > process->heap_address && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)) {
1215         if (not(addr_pointed2 > process->heap_address && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)))
1216           return 1;
1217         // The pointers are both in the heap:
1218         return simgrid::mc::compare_heap_area(state, addr_pointed1, addr_pointed2, snapshot1, snapshot2, nullptr,
1219                                               type->subtype, pointer_level);
1220
1221       } else if (region1->contain(simgrid::mc::remote(addr_pointed1))) {
1222         // The pointers are both in the current object R/W segment:
1223         if (not region2->contain(simgrid::mc::remote(addr_pointed2)))
1224           return 1;
1225         if (not type->type_id)
1226           return (addr_pointed1 != addr_pointed2);
1227         else
1228           return compare_areas_with_type(state, addr_pointed1, snapshot1, region1, addr_pointed2, snapshot2, region2,
1229                                          type->subtype, pointer_level);
1230       } else {
1231
1232         // TODO, We do not handle very well the case where
1233         // it belongs to a different (non-heap) region from the current one.
1234
1235         return (addr_pointed1 != addr_pointed2);
1236       }
1237     }
1238     case DW_TAG_structure_type:
1239     case DW_TAG_class_type:
1240       for (simgrid::mc::Member& member : type->members) {
1241         void* member1                   = simgrid::dwarf::resolve_member(real_area1, type, &member, snapshot1);
1242         void* member2                   = simgrid::dwarf::resolve_member(real_area2, type, &member, snapshot2);
1243         simgrid::mc::Region* subregion1 = snapshot1->get_region(member1, region1); // region1 is hinted
1244         simgrid::mc::Region* subregion2 = snapshot2->get_region(member2, region2); // region2 is hinted
1245         res = compare_areas_with_type(state, member1, snapshot1, subregion1, member2, snapshot2, subregion2,
1246                                       member.type, pointer_level);
1247         if (res == 1)
1248           return res;
1249       }
1250       break;
1251     case DW_TAG_subroutine_type:
1252       return -1;
1253     default:
1254       XBT_VERB("Unknown case: %d", type->type);
1255       break;
1256   }
1257
1258   return 0;
1259 }
1260
1261 static bool global_variables_equal(simgrid::mc::StateComparator& state, simgrid::mc::ObjectInformation* object_info,
1262                                    simgrid::mc::Region* r1, simgrid::mc::Region* r2, simgrid::mc::Snapshot* snapshot1,
1263                                    simgrid::mc::Snapshot* snapshot2)
1264 {
1265   xbt_assert(r1 && r2, "Missing region.");
1266
1267   std::vector<simgrid::mc::Variable>& variables = object_info->global_variables;
1268
1269   for (simgrid::mc::Variable const& current_var : variables) {
1270
1271     // If the variable is not in this object, skip it:
1272     // We do not expect to find a pointer to something which is not reachable
1273     // by the global variables.
1274     if ((char *) current_var.address < (char *) object_info->start_rw
1275         || (char *) current_var.address > (char *) object_info->end_rw)
1276       continue;
1277
1278     simgrid::mc::Type* bvariable_type = current_var.type;
1279     int res = compare_areas_with_type(state, (char*)current_var.address, snapshot1, r1, (char*)current_var.address,
1280                                       snapshot2, r2, bvariable_type, 0);
1281     if (res == 1) {
1282       XBT_VERB("Global variable %s (%p) is different between snapshots",
1283                current_var.name.c_str(),
1284                (char *) current_var.address);
1285       return false;
1286     }
1287   }
1288
1289   return true;
1290 }
1291
1292 static bool local_variables_equal(simgrid::mc::StateComparator& state, simgrid::mc::Snapshot* snapshot1,
1293                                   simgrid::mc::Snapshot* snapshot2, mc_snapshot_stack_t stack1,
1294                                   mc_snapshot_stack_t stack2)
1295 {
1296   if (stack1->local_variables.size() != stack2->local_variables.size()) {
1297     XBT_VERB("Different number of local variables");
1298     return false;
1299   }
1300
1301   for (unsigned int cursor = 0; cursor < stack1->local_variables.size(); cursor++) {
1302     local_variable_t current_var1 = &stack1->local_variables[cursor];
1303     local_variable_t current_var2 = &stack2->local_variables[cursor];
1304     if (current_var1->name != current_var2->name || current_var1->subprogram != current_var2->subprogram ||
1305         current_var1->ip != current_var2->ip) {
1306       // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1307       XBT_VERB("Different name of variable (%s - %s) "
1308                "or frame (%s - %s) or ip (%lu - %lu)",
1309                current_var1->name.c_str(), current_var2->name.c_str(), current_var1->subprogram->name.c_str(),
1310                current_var2->subprogram->name.c_str(), current_var1->ip, current_var2->ip);
1311       return false;
1312     }
1313
1314     if (compare_areas_with_type(state, current_var1->address, snapshot1, snapshot1->get_region(current_var1->address),
1315                                 current_var2->address, snapshot2, snapshot2->get_region(current_var2->address),
1316                                 current_var1->type, 0) == 1) {
1317       XBT_VERB("Local variable %s (%p - %p) in frame %s "
1318                "is different between snapshots",
1319                current_var1->name.c_str(), current_var1->address, current_var2->address,
1320                current_var1->subprogram->name.c_str());
1321       return false;
1322     }
1323     }
1324     return true;
1325 }
1326
1327 namespace simgrid {
1328 namespace mc {
1329
1330 static std::unique_ptr<simgrid::mc::StateComparator> state_comparator;
1331
1332 bool snapshot_equal(Snapshot* s1, Snapshot* s2)
1333 {
1334   // TODO, make this a field of ModelChecker or something similar
1335   if (state_comparator == nullptr)
1336     state_comparator.reset(new StateComparator());
1337   else
1338     state_comparator->clear();
1339
1340   RemoteClient* process = &mc_model_checker->process();
1341
1342   if (s1->hash_ != s2->hash_) {
1343     XBT_VERB("(%d - %d) Different hash: 0x%" PRIx64 "--0x%" PRIx64, s1->num_state_, s2->num_state_, s1->hash_,
1344              s2->hash_);
1345     return false;
1346     } else
1347       XBT_VERB("(%d - %d) Same hash: 0x%" PRIx64, s1->num_state_, s2->num_state_, s1->hash_);
1348
1349   /* Compare enabled processes */
1350   if (s1->enabled_processes_ != s2->enabled_processes_) {
1351     XBT_VERB("(%d - %d) Different amount of enabled processes", s1->num_state_, s2->num_state_);
1352     return false;
1353   }
1354
1355   /* Compare size of stacks */
1356   for (unsigned long i = 0; i < s1->stacks_.size(); i++) {
1357     size_t size_used1 = s1->stack_sizes_[i];
1358     size_t size_used2 = s2->stack_sizes_[i];
1359     if (size_used1 != size_used2) {
1360       XBT_VERB("(%d - %d) Different size used in stacks: %zu - %zu", s1->num_state_, s2->num_state_, size_used1,
1361                size_used2);
1362       return false;
1363     }
1364   }
1365
1366   /* Init heap information used in heap comparison algorithm */
1367   xbt_mheap_t heap1 = (xbt_mheap_t)s1->read_bytes(alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1368                                                   remote(process->heap_address), simgrid::mc::ReadOptions::lazy());
1369   xbt_mheap_t heap2 = (xbt_mheap_t)s2->read_bytes(alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1370                                                   remote(process->heap_address), simgrid::mc::ReadOptions::lazy());
1371   int res_init = state_comparator->initHeapInformation(heap1, heap2, &s1->to_ignore_, &s2->to_ignore_);
1372
1373   if (res_init == -1) {
1374     XBT_VERB("(%d - %d) Different heap information", s1->num_state_, s2->num_state_);
1375     return false;
1376   }
1377
1378   /* Stacks comparison */
1379   for (unsigned int cursor = 0; cursor < s1->stacks_.size(); cursor++) {
1380     mc_snapshot_stack_t stack1 = &s1->stacks_[cursor];
1381     mc_snapshot_stack_t stack2 = &s2->stacks_[cursor];
1382
1383     if (not local_variables_equal(*state_comparator, s1, s2, stack1, stack2)) {
1384       XBT_VERB("(%d - %d) Different local variables between stacks %u", s1->num_state_, s2->num_state_, cursor + 1);
1385       return false;
1386     }
1387   }
1388
1389   size_t regions_count = s1->snapshot_regions_.size();
1390   if (regions_count != s2->snapshot_regions_.size())
1391     return false;
1392
1393   for (size_t k = 0; k != regions_count; ++k) {
1394     Region* region1 = s1->snapshot_regions_[k].get();
1395     Region* region2 = s2->snapshot_regions_[k].get();
1396
1397     // Preconditions:
1398     if (region1->region_type() != RegionType::Data)
1399       continue;
1400
1401     xbt_assert(region1->region_type() == region2->region_type());
1402     xbt_assert(region1->object_info() == region2->object_info());
1403     xbt_assert(region1->object_info());
1404
1405     /* Compare global variables */
1406     if (not global_variables_equal(*state_comparator, region1->object_info(), region1, region2, s1, s2)) {
1407       std::string const& name = region1->object_info()->file_name;
1408       XBT_VERB("(%d - %d) Different global variables in %s", s1->num_state_, s2->num_state_, name.c_str());
1409       return false;
1410     }
1411   }
1412
1413   /* Compare heap */
1414   if (not mmalloc_heap_equal(*state_comparator, s1, s2)) {
1415     XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", s1->num_state_, s2->num_state_);
1416     return false;
1417   }
1418
1419     XBT_VERB("(%d - %d) No difference found", s1->num_state_, s2->num_state_);
1420
1421     return true;
1422 }
1423
1424 }
1425 }