Logo AND Algorithmique Numérique Distribuée

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