Logo AND Algorithmique Numérique Distribuée

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