Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[project-description] Fix extraction of the ns-3 version.
[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 static bool mmalloc_heap_differ(const RemoteProcess& process, StateComparator& state, const Snapshot& snapshot1,
205                                 const Snapshot& snapshot2)
206 {
207   /* Check busy blocks */
208   size_t i1 = 1;
209
210   malloc_info heapinfo_temp1;
211   malloc_info heapinfo_temp2;
212   malloc_info heapinfo_temp2b;
213
214   const Region* heap_region1 = MC_get_heap_region(snapshot1);
215   const Region* heap_region2 = MC_get_heap_region(snapshot2);
216
217   // This is the address of std_heap->heapinfo in the application process:
218   uint64_t heapinfo_address = process.heap_address.address() + offsetof(s_xbt_mheap_t, heapinfo);
219
220   // This is in snapshot do not use them directly:
221   const malloc_info* heapinfos1 = snapshot1.read(remote<malloc_info*>(heapinfo_address));
222   const malloc_info* heapinfos2 = snapshot2.read(remote<malloc_info*>(heapinfo_address));
223
224   while (i1 < state.heaplimit) {
225     const auto* heapinfo1 =
226         static_cast<malloc_info*>(heap_region1->read(&heapinfo_temp1, &heapinfos1[i1], sizeof(malloc_info)));
227     const auto* heapinfo2 =
228         static_cast<malloc_info*>(heap_region2->read(&heapinfo_temp2, &heapinfos2[i1], sizeof(malloc_info)));
229
230     if (heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type == MMALLOC_TYPE_HEAPINFO) {      /* Free block */
231       i1 ++;
232       continue;
233     }
234
235     xbt_assert(heapinfo1->type >= 0, "Unknown mmalloc block type: %d", heapinfo1->type);
236
237     void* addr_block1 = (ADDR2UINT(i1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
238
239     if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED) { /* Large block */
240       if (is_stack(process, addr_block1)) {
241         for (size_t k = 0; k < heapinfo1->busy_block.size; k++)
242           state.equals_to_<1>(i1 + k, 0) = HeapArea(i1, -1);
243         for (size_t k = 0; k < heapinfo2->busy_block.size; k++)
244           state.equals_to_<2>(i1 + k, 0) = HeapArea(i1, -1);
245         i1 += heapinfo1->busy_block.size;
246         continue;
247       }
248
249       if (state.equals_to_<1>(i1, 0).valid_) {
250         i1++;
251         continue;
252       }
253
254       size_t i2 = 1;
255       bool equal = false;
256
257       /* Try first to associate to same block in the other heap */
258       if (heapinfo2->type == heapinfo1->type && state.equals_to_<2>(i1, 0).valid_ == 0) {
259         const void* addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
260         if (not heap_area_differ(process, state, addr_block1, addr_block2, snapshot1, snapshot2, nullptr, nullptr, 0)) {
261           for (size_t k = 1; k < heapinfo2->busy_block.size; k++)
262             state.equals_to_<2>(i1 + k, 0) = HeapArea(i1, -1);
263           for (size_t k = 1; k < heapinfo1->busy_block.size; k++)
264             state.equals_to_<1>(i1 + k, 0) = HeapArea(i1, -1);
265           equal = true;
266           i1 += heapinfo1->busy_block.size;
267         }
268       }
269
270       while (i2 < state.heaplimit && not equal) {
271         const void* addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
272
273         if (i2 == i1) {
274           i2++;
275           continue;
276         }
277
278         const auto* heapinfo2b =
279             static_cast<malloc_info*>(heap_region2->read(&heapinfo_temp2b, &heapinfos2[i2], sizeof(malloc_info)));
280
281         if (heapinfo2b->type != MMALLOC_TYPE_UNFRAGMENTED) {
282           i2++;
283           continue;
284         }
285
286         if (state.equals_to_<2>(i2, 0).valid_) {
287           i2++;
288           continue;
289         }
290
291         if (not heap_area_differ(process, state, addr_block1, addr_block2, snapshot1, snapshot2, nullptr, nullptr, 0)) {
292           for (size_t k = 1; k < heapinfo2b->busy_block.size; k++)
293             state.equals_to_<2>(i2 + 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(i2, -1);
296           equal = true;
297           i1 += heapinfo1->busy_block.size;
298         }
299         i2++;
300       }
301
302       if (not equal) {
303         XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1, heapinfo1->busy_block.busy_size, addr_block1);
304         return true;
305       }
306     } else { /* Fragmented block */
307       for (size_t j1 = 0; j1 < (size_t)(BLOCKSIZE >> heapinfo1->type); j1++) {
308         if (heapinfo1->busy_frag.frag_size[j1] == -1) /* Free fragment_ */
309           continue;
310
311         if (state.equals_to_<1>(i1, j1).valid_)
312           continue;
313
314         void* addr_frag1 = (char*)addr_block1 + (j1 << heapinfo1->type);
315
316         size_t i2 = 1;
317         bool equal = false;
318
319         /* Try first to associate to same fragment_ in the other heap */
320         if (heapinfo2->type == heapinfo1->type && not state.equals_to_<2>(i1, j1).valid_) {
321           const void* addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
322           const void* addr_frag2  = (const char*)addr_block2 + (j1 << heapinfo2->type);
323           if (not heap_area_differ(process, state, addr_frag1, addr_frag2, snapshot1, snapshot2, nullptr, nullptr, 0))
324             equal = true;
325         }
326
327         while (i2 < state.heaplimit && not equal) {
328           const auto* heapinfo2b =
329               static_cast<malloc_info*>(heap_region2->read(&heapinfo_temp2b, &heapinfos2[i2], sizeof(malloc_info)));
330
331           if (heapinfo2b->type == MMALLOC_TYPE_FREE || heapinfo2b->type == MMALLOC_TYPE_HEAPINFO) {
332             i2 ++;
333             continue;
334           }
335
336           // We currently do not match fragments with unfragmented blocks (maybe we should).
337           if (heapinfo2b->type == MMALLOC_TYPE_UNFRAGMENTED) {
338             i2++;
339             continue;
340           }
341
342           xbt_assert(heapinfo2b->type >= 0, "Unknown mmalloc block type: %d", heapinfo2b->type);
343
344           for (size_t j2 = 0; j2 < (size_t)(BLOCKSIZE >> heapinfo2b->type); j2++) {
345             if (i2 == i1 && j2 == j1)
346               continue;
347
348             if (state.equals_to_<2>(i2, j2).valid_)
349               continue;
350
351             const void* addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
352             const void* addr_frag2  = (const char*)addr_block2 + (j2 << heapinfo2b->type);
353
354             if (not heap_area_differ(process, state, addr_frag1, addr_frag2, snapshot1, snapshot2, nullptr, nullptr,
355                                      0)) {
356               equal = true;
357               break;
358             }
359           }
360           i2++;
361         }
362
363         if (not equal) {
364           XBT_DEBUG("Block %zu, fragment_ %zu not found (size_used = %zd, address = %p)\n", i1, j1,
365                     heapinfo1->busy_frag.frag_size[j1], addr_frag1);
366           return true;
367         }
368       }
369       i1++;
370     }
371   }
372
373   /* All blocks/fragments are equal to another block/fragment_ ? */
374   for (size_t i = 1; i < state.heaplimit; i++) {
375     const auto* heapinfo1 =
376         static_cast<malloc_info*>(heap_region1->read(&heapinfo_temp1, &heapinfos1[i], sizeof(malloc_info)));
377
378     if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED && i1 == state.heaplimit && heapinfo1->busy_block.busy_size > 0 &&
379         not state.equals_to_<1>(i, 0).valid_) {
380       XBT_DEBUG("Block %zu not found (size used = %zu)", i, heapinfo1->busy_block.busy_size);
381       return true;
382     }
383
384     if (heapinfo1->type <= 0)
385       continue;
386     for (size_t j = 0; j < (size_t)(BLOCKSIZE >> heapinfo1->type); j++)
387       if (i1 == state.heaplimit && heapinfo1->busy_frag.frag_size[j] > 0 && not state.equals_to_<1>(i, j).valid_) {
388         XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)", i, j, heapinfo1->busy_frag.frag_size[j]);
389         return true;
390       }
391   }
392
393   for (size_t i = 1; i < state.heaplimit; i++) {
394     const auto* heapinfo2 =
395         static_cast<malloc_info*>(heap_region2->read(&heapinfo_temp2, &heapinfos2[i], sizeof(malloc_info)));
396     if (heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED && i1 == state.heaplimit && heapinfo2->busy_block.busy_size > 0 &&
397         not state.equals_to_<2>(i, 0).valid_) {
398       XBT_DEBUG("Block %zu not found (size used = %zu)", i,
399                 heapinfo2->busy_block.busy_size);
400       return true;
401     }
402
403     if (heapinfo2->type <= 0)
404       continue;
405
406     for (size_t j = 0; j < (size_t)(BLOCKSIZE >> heapinfo2->type); j++)
407       if (i1 == state.heaplimit && heapinfo2->busy_frag.frag_size[j] > 0 && not state.equals_to_<2>(i, j).valid_) {
408         XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)",
409           i, j, heapinfo2->busy_frag.frag_size[j]);
410         return true;
411       }
412   }
413   return false;
414 }
415
416 /**
417  *
418  * @param state
419  * @param real_area1     Process address for state 1
420  * @param real_area2     Process address for state 2
421  * @param snapshot1      Snapshot of state 1
422  * @param snapshot2      Snapshot of state 2
423  * @param previous
424  * @param size
425  * @param check_ignore
426  * @return true when different, false otherwise (same or unknown)
427  */
428 static bool heap_area_differ_without_type(const RemoteProcess& process, StateComparator& state, const void* real_area1,
429                                           const void* real_area2, const Snapshot& snapshot1, const Snapshot& snapshot2,
430                                           HeapLocationPairs* previous, int size, int check_ignore)
431 {
432   const Region* heap_region1  = MC_get_heap_region(snapshot1);
433   const Region* heap_region2  = MC_get_heap_region(snapshot2);
434
435   for (int i = 0; i < size; ) {
436     if (check_ignore > 0) {
437       ssize_t ignore1 = heap_comparison_ignore_size(state.processStates[0].to_ignore, (const char*)real_area1 + i);
438       if (ignore1 != -1) {
439         ssize_t ignore2 = heap_comparison_ignore_size(state.processStates[1].to_ignore, (const char*)real_area2 + i);
440         if (ignore2 == ignore1) {
441           if (ignore1 == 0) {
442             return false;
443           } else {
444             i = i + ignore2;
445             check_ignore--;
446             continue;
447           }
448         }
449       }
450     }
451
452     if (MC_snapshot_region_memcmp((const char*)real_area1 + i, heap_region1, (const char*)real_area2 + i, heap_region2,
453                                   1) != 0) {
454       int pointer_align = (i / sizeof(void *)) * sizeof(void *);
455       const void* addr_pointed1 = snapshot1.read(remote((void* const*)((const char*)real_area1 + pointer_align)));
456       const void* addr_pointed2 = snapshot2.read(remote((void* const*)((const char*)real_area2 + pointer_align)));
457
458       if (process.in_maestro_stack(remote(addr_pointed1)) && process.in_maestro_stack(remote(addr_pointed2))) {
459         i = pointer_align + sizeof(void *);
460         continue;
461       }
462
463       if (snapshot1.on_heap(addr_pointed1) && snapshot2.on_heap(addr_pointed2)) {
464         // Both addresses are in the heap:
465         if (heap_area_differ(process, state, addr_pointed1, addr_pointed2, snapshot1, snapshot2, previous, nullptr, 0))
466           return true;
467         i = pointer_align + sizeof(void *);
468         continue;
469       }
470       return true;
471     }
472     i++;
473   }
474   return false;
475 }
476
477 /**
478  *
479  * @param state
480  * @param real_area1     Process address for state 1
481  * @param real_area2     Process address for state 2
482  * @param snapshot1      Snapshot of state 1
483  * @param snapshot2      Snapshot of state 2
484  * @param previous
485  * @param type
486  * @param area_size      either a byte_size or an elements_count (?)
487  * @param check_ignore
488  * @param pointer_level
489  * @return               true when different, false otherwise (same or unknown)
490  */
491 static bool heap_area_differ_with_type(const simgrid::mc::RemoteProcess& process, StateComparator& state,
492                                        const void* real_area1, const void* real_area2, const Snapshot& snapshot1,
493                                        const Snapshot& snapshot2, HeapLocationPairs* previous, const Type* type,
494                                        int area_size, int check_ignore, int pointer_level)
495 {
496   // HACK: This should not happen but in practice, there are some
497   // DW_TAG_typedef without an associated DW_AT_type:
498   //<1><538832>: Abbrev Number: 111 (DW_TAG_typedef)
499   //    <538833>   DW_AT_name        : (indirect string, offset: 0x2292f3): gregset_t
500   //    <538837>   DW_AT_decl_file   : 98
501   //    <538838>   DW_AT_decl_line   : 37
502   if (type == nullptr)
503     return false;
504
505   if (is_stack(process, real_area1) && is_stack(process, real_area2))
506     return false;
507
508   if (check_ignore > 0) {
509     ssize_t ignore1 = heap_comparison_ignore_size(state.processStates[0].to_ignore, real_area1);
510     if (ignore1 > 0 && heap_comparison_ignore_size(state.processStates[1].to_ignore, real_area2) == ignore1)
511       return false;
512   }
513
514   const Type* subtype;
515   const Type* subsubtype;
516   int elm_size;
517   const void* addr_pointed1;
518   const void* addr_pointed2;
519
520   const Region* heap_region1 = MC_get_heap_region(snapshot1);
521   const Region* heap_region2 = MC_get_heap_region(snapshot2);
522
523   switch (type->type) {
524     case DW_TAG_unspecified_type:
525       return true;
526
527     case DW_TAG_base_type:
528       if (not type->name.empty() && type->name == "char") { /* String, hence random (arbitrary ?) size */
529         if (real_area1 == real_area2)
530           return false;
531         else
532           return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, area_size) != 0;
533       } else {
534         if (area_size != -1 && type->byte_size != area_size)
535           return false;
536         else
537           return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
538       }
539
540     case DW_TAG_enumeration_type:
541       if (area_size != -1 && type->byte_size != area_size)
542         return false;
543       return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
544
545     case DW_TAG_typedef:
546     case DW_TAG_const_type:
547     case DW_TAG_volatile_type:
548       return heap_area_differ_with_type(process, state, real_area1, real_area2, snapshot1, snapshot2, previous,
549                                         type->subtype, area_size, check_ignore, pointer_level);
550
551     case DW_TAG_array_type:
552       subtype = type->subtype;
553       switch (subtype->type) {
554         case DW_TAG_unspecified_type:
555           return true;
556
557         case DW_TAG_base_type:
558         case DW_TAG_enumeration_type:
559         case DW_TAG_pointer_type:
560         case DW_TAG_reference_type:
561         case DW_TAG_rvalue_reference_type:
562         case DW_TAG_structure_type:
563         case DW_TAG_class_type:
564         case DW_TAG_union_type:
565           if (subtype->full_type)
566             subtype = subtype->full_type;
567           elm_size  = subtype->byte_size;
568           break;
569         // TODO, just remove the type indirection?
570         case DW_TAG_const_type:
571         case DW_TAG_typedef:
572         case DW_TAG_volatile_type:
573           subsubtype = subtype->subtype;
574           if (subsubtype->full_type)
575             subsubtype = subsubtype->full_type;
576           elm_size     = subsubtype->byte_size;
577           break;
578         default:
579           return false;
580       }
581       for (int i = 0; i < type->element_count; i++) {
582         // TODO, add support for variable stride (DW_AT_byte_stride)
583         if (heap_area_differ_with_type(process, state, (const char*)real_area1 + (i * elm_size),
584                                        (const char*)real_area2 + (i * elm_size), snapshot1, snapshot2, previous,
585                                        type->subtype, subtype->byte_size, check_ignore, pointer_level))
586           return true;
587       }
588       return false;
589
590     case DW_TAG_reference_type:
591     case DW_TAG_rvalue_reference_type:
592     case DW_TAG_pointer_type:
593       if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
594         addr_pointed1 = snapshot1.read(remote((void* const*)real_area1));
595         addr_pointed2 = snapshot2.read(remote((void* const*)real_area2));
596         return (addr_pointed1 != addr_pointed2);
597       }
598       pointer_level++;
599       if (pointer_level <= 1) {
600         addr_pointed1 = snapshot1.read(remote((void* const*)real_area1));
601         addr_pointed2 = snapshot2.read(remote((void* const*)real_area2));
602         if (snapshot1.on_heap(addr_pointed1) && snapshot2.on_heap(addr_pointed2))
603           return heap_area_differ(process, state, addr_pointed1, addr_pointed2, snapshot1, snapshot2, previous,
604                                   type->subtype, pointer_level);
605         else
606           return (addr_pointed1 != addr_pointed2);
607       }
608       for (size_t i = 0; i < (area_size / sizeof(void*)); i++) {
609         addr_pointed1 = snapshot1.read(remote((void* const*)((const char*)real_area1 + i * sizeof(void*))));
610         addr_pointed2 = snapshot2.read(remote((void* const*)((const char*)real_area2 + i * sizeof(void*))));
611         bool differ   = snapshot1.on_heap(addr_pointed1) && snapshot2.on_heap(addr_pointed2)
612                             ? heap_area_differ(process, state, addr_pointed1, addr_pointed2, snapshot1, snapshot2,
613                                              previous, type->subtype, pointer_level)
614                             : addr_pointed1 != addr_pointed2;
615         if (differ)
616           return true;
617       }
618       return false;
619
620     case DW_TAG_structure_type:
621     case DW_TAG_class_type:
622       if (type->full_type)
623         type = type->full_type;
624       if (type->byte_size == 0)
625         return false;
626       if (area_size != -1 && type->byte_size != area_size) {
627         if (area_size <= type->byte_size || area_size % type->byte_size != 0)
628           return false;
629         for (size_t i = 0; i < (size_t)(area_size / type->byte_size); i++) {
630           if (heap_area_differ_with_type(process, state, (const char*)real_area1 + i * type->byte_size,
631                                          (const char*)real_area2 + i * type->byte_size, snapshot1, snapshot2, previous,
632                                          type, -1, check_ignore, 0))
633             return true;
634         }
635         } else {
636           for (const simgrid::mc::Member& member : type->members) {
637             // TODO, optimize this? (for the offset case)
638             const void* real_member1 = dwarf::resolve_member(real_area1, type, &member, &snapshot1);
639             const void* real_member2 = dwarf::resolve_member(real_area2, type, &member, &snapshot2);
640             if (heap_area_differ_with_type(process, state, real_member1, real_member2, snapshot1, snapshot2, previous,
641                                            member.type, -1, check_ignore, 0))
642               return true;
643           }
644         }
645         return false;
646
647     case DW_TAG_union_type:
648       return heap_area_differ_without_type(process, state, real_area1, real_area2, snapshot1, snapshot2, previous,
649                                            type->byte_size, check_ignore);
650
651     default:
652       THROW_IMPOSSIBLE;
653   }
654 }
655
656 /** Infer the type of a part of the block from the type of the block
657  *
658  * TODO, handle DW_TAG_array_type as well as arrays of the object ((*p)[5], p[5])
659  *
660  * TODO, handle subfields ((*p).bar.foo, (*p)[5].bar…)
661  *
662  * @param  type               DWARF type ID of the root address
663  * @param  area_size
664  * @return                    DWARF type ID for given offset
665  */
666 static Type* get_offset_type(void* real_base_address, Type* type, int offset, int area_size, const Snapshot& snapshot)
667 {
668   // Beginning of the block, the inferred variable type if the type of the block:
669   if (offset == 0)
670     return type;
671
672   switch (type->type) {
673   case DW_TAG_structure_type:
674   case DW_TAG_class_type:
675     if (type->full_type)
676       type = type->full_type;
677     if (area_size != -1 && type->byte_size != area_size) {
678       if (area_size > type->byte_size && area_size % type->byte_size == 0)
679         return type;
680       else
681         return nullptr;
682     }
683
684     for (const simgrid::mc::Member& member : type->members) {
685       if (member.has_offset_location()) {
686         // We have the offset, use it directly (shortcut):
687         if (member.offset() == offset)
688           return member.type;
689       } else {
690         void* real_member = dwarf::resolve_member(real_base_address, type, &member, &snapshot);
691         if ((char*)real_member - (char*)real_base_address == offset)
692           return member.type;
693       }
694     }
695     return nullptr;
696
697   default:
698     /* FIXME: other cases ? */
699     return nullptr;
700   }
701 }
702
703 /**
704  *
705  * @param area1          Process address for state 1
706  * @param area2          Process address for state 2
707  * @param snapshot1      Snapshot of state 1
708  * @param snapshot2      Snapshot of state 2
709  * @param previous       Pairs of blocks already compared on the current path (or nullptr)
710  * @param type_id        Type of variable
711  * @param pointer_level
712  * @return true when different, false otherwise (same or unknown)
713  */
714 static bool heap_area_differ(const RemoteProcess& process, StateComparator& state, const void* area1, const void* area2,
715                              const Snapshot& snapshot1, const Snapshot& snapshot2, HeapLocationPairs* previous,
716                              Type* type, int pointer_level)
717 {
718   ssize_t block1;
719   ssize_t block2;
720   ssize_t size;
721   int check_ignore = 0;
722
723   int type_size = -1;
724   int offset1   = 0;
725   int offset2   = 0;
726   int new_size1 = -1;
727   int new_size2 = -1;
728
729   Type* new_type1 = nullptr;
730
731   bool match_pairs = false;
732
733   // This is the address of std_heap->heapinfo in the application process:
734   uint64_t heapinfo_address = process.heap_address.address() + offsetof(s_xbt_mheap_t, heapinfo);
735
736   const malloc_info* heapinfos1 = snapshot1.read(remote<malloc_info*>(heapinfo_address));
737   const malloc_info* heapinfos2 = snapshot2.read(remote<malloc_info*>(heapinfo_address));
738
739   malloc_info heapinfo_temp1;
740   malloc_info heapinfo_temp2;
741
742   simgrid::mc::HeapLocationPairs current;
743   if (previous == nullptr) {
744     previous = &current;
745     match_pairs = true;
746   }
747
748   // Get block number:
749   block1 = ((const char*)area1 - (const char*)state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
750   block2 = ((const char*)area2 - (const char*)state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
751
752   // If either block is a stack block:
753   if (is_block_stack(process, (int)block1) && is_block_stack(process, (int)block2)) {
754     previous->insert(HeapLocationPair{{HeapLocation(block1, -1), HeapLocation(block2, -1)}});
755     if (match_pairs)
756       state.match_equals(previous);
757     return false;
758   }
759
760   // If either block is not in the expected area of memory:
761   if (((const char*)area1 < (const char*)state.std_heap_copy.heapbase) ||
762       (block1 > (ssize_t)state.processStates[0].heapsize) || (block1 < 1) ||
763       ((const char*)area2 < (const char*)state.std_heap_copy.heapbase) ||
764       (block2 > (ssize_t)state.processStates[1].heapsize) || (block2 < 1)) {
765     return true;
766   }
767
768   // Process address of the block:
769   void* real_addr_block1 = (ADDR2UINT(block1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
770   void* real_addr_block2 = (ADDR2UINT(block2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
771
772   if (type) {
773     if (type->full_type)
774       type = type->full_type;
775
776     // This assume that for "boring" types (volatile ...) byte_size is absent:
777     while (type->byte_size == 0 && type->subtype != nullptr)
778       type = type->subtype;
779
780     // Find type_size:
781     if (type->type == DW_TAG_pointer_type ||
782         (type->type == DW_TAG_base_type && not type->name.empty() && type->name == "char"))
783       type_size = -1;
784     else
785       type_size = type->byte_size;
786   }
787
788   const Region* heap_region1 = MC_get_heap_region(snapshot1);
789   const Region* heap_region2 = MC_get_heap_region(snapshot2);
790
791   const auto* heapinfo1 =
792       static_cast<malloc_info*>(heap_region1->read(&heapinfo_temp1, &heapinfos1[block1], sizeof(malloc_info)));
793   const auto* heapinfo2 =
794       static_cast<malloc_info*>(heap_region2->read(&heapinfo_temp2, &heapinfos2[block2], sizeof(malloc_info)));
795
796   if ((heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type==MMALLOC_TYPE_HEAPINFO)
797     && (heapinfo2->type == MMALLOC_TYPE_FREE || heapinfo2->type ==MMALLOC_TYPE_HEAPINFO)) {
798     /* Free block */
799     if (match_pairs)
800       state.match_equals(previous);
801     return false;
802   }
803
804   if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED && heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
805     /* Complete block */
806
807     // TODO, lookup variable type from block type as done for fragmented blocks
808
809     if (state.equals_to_<1>(block1, 0).valid_ && state.equals_to_<2>(block2, 0).valid_ &&
810         state.blocksEqual(block1, block2)) {
811       if (match_pairs)
812         state.match_equals(previous);
813       return false;
814     }
815
816     if (type_size != -1 && type_size != (ssize_t)heapinfo1->busy_block.busy_size &&
817         type_size != (ssize_t)heapinfo2->busy_block.busy_size &&
818         (type->name.empty() ||
819          type->name == "struct s_smx_context")) { // FIXME: there is no struct s_smx_context anymore
820       if (match_pairs)
821         state.match_equals(previous);
822       return false;
823     }
824
825     if (heapinfo1->busy_block.size != heapinfo2->busy_block.size ||
826         heapinfo1->busy_block.busy_size != heapinfo2->busy_block.busy_size)
827       return true;
828
829     if (not previous->insert(HeapLocationPair{{HeapLocation(block1, -1), HeapLocation(block2, -1)}}).second) {
830       if (match_pairs)
831         state.match_equals(previous);
832       return false;
833     }
834
835     size = heapinfo1->busy_block.busy_size;
836
837     // Remember (basic) type inference.
838     // The current data structure only allows us to do this for the whole block.
839     if (type != nullptr && area1 == real_addr_block1)
840       state.types_<1>(block1, 0) = type;
841     if (type != nullptr && area2 == real_addr_block2)
842       state.types_<2>(block2, 0) = type;
843
844     if (size <= 0) {
845       if (match_pairs)
846         state.match_equals(previous);
847       return false;
848     }
849
850     if (heapinfo1->busy_block.ignore > 0 && heapinfo2->busy_block.ignore == heapinfo1->busy_block.ignore)
851       check_ignore = heapinfo1->busy_block.ignore;
852
853   } else if ((heapinfo1->type > 0) && (heapinfo2->type > 0)) {      /* Fragmented block */
854     // Fragment number:
855     ssize_t frag1 = (ADDR2UINT(area1) % BLOCKSIZE) >> heapinfo1->type;
856     ssize_t frag2 = (ADDR2UINT(area2) % BLOCKSIZE) >> heapinfo2->type;
857
858     // Process address of the fragment_:
859     void* real_addr_frag1 = (char*)real_addr_block1 + (frag1 << heapinfo1->type);
860     void* real_addr_frag2 = (char*)real_addr_block2 + (frag2 << heapinfo2->type);
861
862     // Check the size of the fragments against the size of the type:
863     if (type_size != -1) {
864       if (heapinfo1->busy_frag.frag_size[frag1] == -1 || heapinfo2->busy_frag.frag_size[frag2] == -1) {
865         if (match_pairs)
866           state.match_equals(previous);
867         return false;
868       }
869       // ?
870       if (type_size != heapinfo1->busy_frag.frag_size[frag1]
871           || type_size != heapinfo2->busy_frag.frag_size[frag2]) {
872         if (match_pairs)
873           state.match_equals(previous);
874         return false;
875       }
876     }
877
878     // Check if the blocks are already matched together:
879     if (state.equals_to_<1>(block1, frag1).valid_ && state.equals_to_<2>(block2, frag2).valid_ &&
880         state.fragmentsEqual(block1, frag1, block2, frag2)) {
881       if (match_pairs)
882         state.match_equals(previous);
883       return false;
884     }
885     // Compare the size of both fragments:
886     if (heapinfo1->busy_frag.frag_size[frag1] != heapinfo2->busy_frag.frag_size[frag2]) {
887       if (type_size == -1) {
888         if (match_pairs)
889           state.match_equals(previous);
890         return false;
891       } else
892         return true;
893     }
894
895     // Size of the fragment_:
896     size = heapinfo1->busy_frag.frag_size[frag1];
897
898     // Remember (basic) type inference.
899     // The current data structure only allows us to do this for the whole fragment_.
900     if (type != nullptr && area1 == real_addr_frag1)
901       state.types_<1>(block1, frag1) = type;
902     if (type != nullptr && area2 == real_addr_frag2)
903       state.types_<2>(block2, frag2) = type;
904
905     // The type of the variable is already known:
906     if (type) {
907       new_type1 = type;
908     }
909     // Type inference from the block type.
910     else if (state.types_<1>(block1, frag1) != nullptr || state.types_<2>(block2, frag2) != nullptr) {
911       Type* new_type2 = nullptr;
912
913       offset1 = (const char*)area1 - (const char*)real_addr_frag1;
914       offset2 = (const char*)area2 - (const char*)real_addr_frag2;
915
916       if (state.types_<1>(block1, frag1) != nullptr && state.types_<2>(block2, frag2) != nullptr) {
917         new_type1 = get_offset_type(real_addr_frag1, state.types_<1>(block1, frag1), offset1, size, snapshot1);
918         new_type2 = get_offset_type(real_addr_frag2, state.types_<2>(block2, frag2), offset1, size, snapshot2);
919       } else if (state.types_<1>(block1, frag1) != nullptr) {
920         new_type1 = get_offset_type(real_addr_frag1, state.types_<1>(block1, frag1), offset1, size, snapshot1);
921         new_type2 = get_offset_type(real_addr_frag2, state.types_<1>(block1, frag1), offset2, size, snapshot2);
922       } else if (state.types_<2>(block2, frag2) != nullptr) {
923         new_type1 = get_offset_type(real_addr_frag1, state.types_<2>(block2, frag2), offset1, size, snapshot1);
924         new_type2 = get_offset_type(real_addr_frag2, state.types_<2>(block2, frag2), offset2, size, snapshot2);
925       } else {
926         if (match_pairs)
927           state.match_equals(previous);
928         return false;
929       }
930
931       if (new_type1 != nullptr && new_type2 != nullptr && new_type1 != new_type2) {
932         type = new_type1;
933         while (type->byte_size == 0 && type->subtype != nullptr)
934           type = type->subtype;
935         new_size1 = type->byte_size;
936
937         type = new_type2;
938         while (type->byte_size == 0 && type->subtype != nullptr)
939           type = type->subtype;
940         new_size2 = type->byte_size;
941
942       } else {
943         if (match_pairs)
944           state.match_equals(previous);
945         return false;
946       }
947     }
948
949     if (new_size1 > 0 && new_size1 == new_size2) {
950       type = new_type1;
951       size = new_size1;
952     }
953
954     if (offset1 == 0 && offset2 == 0 &&
955         not previous->insert(HeapLocationPair{{HeapLocation(block1, frag1), HeapLocation(block2, frag2)}}).second) {
956       if (match_pairs)
957         state.match_equals(previous);
958       return false;
959     }
960
961     if (size <= 0) {
962       if (match_pairs)
963         state.match_equals(previous);
964       return false;
965     }
966
967     if ((heapinfo1->busy_frag.ignore[frag1] > 0) &&
968         (heapinfo2->busy_frag.ignore[frag2] == heapinfo1->busy_frag.ignore[frag1]))
969       check_ignore = heapinfo1->busy_frag.ignore[frag1];
970   } else
971     return true;
972
973   /* Start comparison */
974   if (type ? heap_area_differ_with_type(process, state, area1, area2, snapshot1, snapshot2, previous, type, size,
975                                         check_ignore, pointer_level)
976            : heap_area_differ_without_type(process, state, area1, area2, snapshot1, snapshot2, previous, size,
977                                            check_ignore))
978     return true;
979
980   if (match_pairs)
981     state.match_equals(previous);
982   return false;
983 }
984 } // namespace simgrid::mc
985
986 /************************** Snapshot comparison *******************************/
987 /******************************************************************************/
988
989 static bool areas_differ_with_type(const simgrid::mc::RemoteProcess& process, simgrid::mc::StateComparator& state,
990                                    const void* real_area1, const simgrid::mc::Snapshot& snapshot1,
991                                    simgrid::mc::Region* region1, const void* real_area2,
992                                    const simgrid::mc::Snapshot& snapshot2, simgrid::mc::Region* region2,
993                                    const simgrid::mc::Type* type, int pointer_level)
994 {
995   const simgrid::mc::Type* subtype;
996   const simgrid::mc::Type* subsubtype;
997   int elm_size;
998
999   xbt_assert(type != nullptr);
1000   switch (type->type) {
1001     case DW_TAG_unspecified_type:
1002       return true;
1003
1004     case DW_TAG_base_type:
1005     case DW_TAG_enumeration_type:
1006     case DW_TAG_union_type:
1007       return MC_snapshot_region_memcmp(real_area1, region1, real_area2, region2, type->byte_size) != 0;
1008     case DW_TAG_typedef:
1009     case DW_TAG_volatile_type:
1010     case DW_TAG_const_type:
1011       return areas_differ_with_type(process, state, real_area1, snapshot1, region1, real_area2, snapshot2, region2,
1012                                     type->subtype, pointer_level);
1013     case DW_TAG_array_type:
1014       subtype = type->subtype;
1015       switch (subtype->type) {
1016         case DW_TAG_unspecified_type:
1017           return true;
1018
1019         case DW_TAG_base_type:
1020         case DW_TAG_enumeration_type:
1021         case DW_TAG_pointer_type:
1022         case DW_TAG_reference_type:
1023         case DW_TAG_rvalue_reference_type:
1024         case DW_TAG_structure_type:
1025         case DW_TAG_class_type:
1026         case DW_TAG_union_type:
1027           if (subtype->full_type)
1028             subtype = subtype->full_type;
1029           elm_size  = subtype->byte_size;
1030           break;
1031         case DW_TAG_const_type:
1032         case DW_TAG_typedef:
1033         case DW_TAG_volatile_type:
1034           subsubtype = subtype->subtype;
1035           if (subsubtype->full_type)
1036             subsubtype = subsubtype->full_type;
1037           elm_size     = subsubtype->byte_size;
1038           break;
1039         default:
1040           return false;
1041       }
1042       for (int i = 0; i < type->element_count; i++) {
1043         size_t off = i * elm_size;
1044         if (areas_differ_with_type(process, state, (const char*)real_area1 + off, snapshot1, region1,
1045                                    (const char*)real_area2 + off, snapshot2, region2, type->subtype, pointer_level))
1046           return true;
1047       }
1048       break;
1049     case DW_TAG_pointer_type:
1050     case DW_TAG_reference_type:
1051     case DW_TAG_rvalue_reference_type: {
1052       const void* addr_pointed1 = MC_region_read_pointer(region1, real_area1);
1053       const void* addr_pointed2 = MC_region_read_pointer(region2, real_area2);
1054
1055       if (type->subtype && type->subtype->type == DW_TAG_subroutine_type)
1056         return (addr_pointed1 != addr_pointed2);
1057       if (addr_pointed1 == nullptr && addr_pointed2 == nullptr)
1058         return false;
1059       if (addr_pointed1 == nullptr || addr_pointed2 == nullptr)
1060         return true;
1061       if (not state.compared_pointers.insert(std::make_pair(addr_pointed1, addr_pointed2)).second)
1062         return false;
1063
1064       pointer_level++;
1065
1066       // Some cases are not handled here:
1067       // * the pointers lead to different areas (one to the heap, the other to the RW segment ...)
1068       // * a pointer leads to the read-only segment of the current object
1069       // * a pointer lead to a different ELF object
1070
1071       if (snapshot1.on_heap(addr_pointed1)) {
1072         if (not snapshot2.on_heap(addr_pointed2))
1073           return true;
1074         // The pointers are both in the heap:
1075         return simgrid::mc::heap_area_differ(process, state, addr_pointed1, addr_pointed2, snapshot1, snapshot2,
1076                                              nullptr, type->subtype, pointer_level);
1077
1078       } else if (region1->contain(simgrid::mc::remote(addr_pointed1))) {
1079         // The pointers are both in the current object R/W segment:
1080         if (not region2->contain(simgrid::mc::remote(addr_pointed2)))
1081           return true;
1082         if (not type->type_id)
1083           return (addr_pointed1 != addr_pointed2);
1084         else
1085           return areas_differ_with_type(process, state, addr_pointed1, snapshot1, region1, addr_pointed2, snapshot2,
1086                                         region2, type->subtype, pointer_level);
1087       } else {
1088         // TODO, We do not handle very well the case where
1089         // it belongs to a different (non-heap) region from the current one.
1090
1091         return (addr_pointed1 != addr_pointed2);
1092       }
1093     }
1094     case DW_TAG_structure_type:
1095     case DW_TAG_class_type:
1096       for (const simgrid::mc::Member& member : type->members) {
1097         const void* member1             = simgrid::dwarf::resolve_member(real_area1, type, &member, &snapshot1);
1098         const void* member2             = simgrid::dwarf::resolve_member(real_area2, type, &member, &snapshot2);
1099         simgrid::mc::Region* subregion1 = snapshot1.get_region(member1, region1); // region1 is hinted
1100         simgrid::mc::Region* subregion2 = snapshot2.get_region(member2, region2); // region2 is hinted
1101         if (areas_differ_with_type(process, state, member1, snapshot1, subregion1, member2, snapshot2, subregion2,
1102                                    member.type, pointer_level))
1103           return true;
1104       }
1105       break;
1106     case DW_TAG_subroutine_type:
1107       return false;
1108     default:
1109       XBT_VERB("Unknown case: %d", type->type);
1110       break;
1111   }
1112
1113   return false;
1114 }
1115
1116 static bool global_variables_differ(const simgrid::mc::RemoteProcess& process, simgrid::mc::StateComparator& state,
1117                                     const simgrid::mc::ObjectInformation* object_info, simgrid::mc::Region* r1,
1118                                     simgrid::mc::Region* r2, const simgrid::mc::Snapshot& snapshot1,
1119                                     const simgrid::mc::Snapshot& snapshot2)
1120 {
1121   xbt_assert(r1 && r2, "Missing region.");
1122
1123   const std::vector<simgrid::mc::Variable>& variables = object_info->global_variables;
1124
1125   for (simgrid::mc::Variable const& current_var : variables) {
1126     // If the variable is not in this object, skip it:
1127     // We do not expect to find a pointer to something which is not reachable
1128     // by the global variables.
1129     if ((char*)current_var.address < object_info->start_rw || (char*)current_var.address > object_info->end_rw)
1130       continue;
1131
1132     const simgrid::mc::Type* bvariable_type = current_var.type;
1133     if (areas_differ_with_type(process, state, current_var.address, snapshot1, r1, current_var.address, snapshot2, r2,
1134                                bvariable_type, 0)) {
1135       XBT_VERB("Global variable %s (%p) is different between snapshots", current_var.name.c_str(), current_var.address);
1136       return true;
1137     }
1138   }
1139
1140   return false;
1141 }
1142
1143 static bool local_variables_differ(const simgrid::mc::RemoteProcess& process, simgrid::mc::StateComparator& state,
1144                                    const simgrid::mc::Snapshot& snapshot1, const simgrid::mc::Snapshot& snapshot2,
1145                                    const_mc_snapshot_stack_t stack1, const_mc_snapshot_stack_t stack2)
1146 {
1147   if (stack1->local_variables.size() != stack2->local_variables.size()) {
1148     XBT_VERB("Different number of local variables");
1149     return true;
1150   }
1151
1152   for (unsigned int cursor = 0; cursor < stack1->local_variables.size(); cursor++) {
1153     const_local_variable_t current_var1 = &stack1->local_variables[cursor];
1154     const_local_variable_t current_var2 = &stack2->local_variables[cursor];
1155     if (current_var1->name != current_var2->name || current_var1->subprogram != current_var2->subprogram ||
1156         current_var1->ip != current_var2->ip) {
1157       // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1158       XBT_VERB("Different name of variable (%s - %s) or frame (%s - %s) or ip (%lu - %lu)", current_var1->name.c_str(),
1159                current_var2->name.c_str(), current_var1->subprogram->name.c_str(),
1160                current_var2->subprogram->name.c_str(), current_var1->ip, current_var2->ip);
1161       return true;
1162     }
1163
1164     if (areas_differ_with_type(process, state, current_var1->address, snapshot1,
1165                                snapshot1.get_region(current_var1->address), current_var2->address, snapshot2,
1166                                snapshot2.get_region(current_var2->address), current_var1->type, 0)) {
1167       XBT_VERB("Local variable %s (%p - %p) in frame %s is different between snapshots", current_var1->name.c_str(),
1168                current_var1->address, current_var2->address, current_var1->subprogram->name.c_str());
1169       return true;
1170     }
1171   }
1172   return false;
1173 }
1174
1175 namespace simgrid::mc {
1176
1177 bool snapshot_equal(const Snapshot* s1, const Snapshot* s2)
1178 {
1179   // TODO, make this a field of ModelChecker or something similar
1180   static StateComparator state_comparator;
1181
1182   const RemoteProcess& process = mc_model_checker->get_remote_process();
1183
1184   if (s1->hash_ != s2->hash_) {
1185     XBT_VERB("(%ld - %ld) Different hash: 0x%" PRIx64 "--0x%" PRIx64, s1->num_state_, s2->num_state_, s1->hash_,
1186              s2->hash_);
1187     return false;
1188   }
1189   XBT_VERB("(%ld - %ld) Same hash: 0x%" PRIx64, s1->num_state_, s2->num_state_, s1->hash_);
1190
1191   /* Compare enabled processes */
1192   if (s1->enabled_processes_ != s2->enabled_processes_) {
1193     XBT_VERB("(%ld - %ld) Different amount of enabled processes", s1->num_state_, s2->num_state_);
1194     return false;
1195   }
1196
1197   /* Compare size of stacks */
1198   for (unsigned long i = 0; i < s1->stacks_.size(); i++) {
1199     size_t size_used1 = s1->stack_sizes_[i];
1200     size_t size_used2 = s2->stack_sizes_[i];
1201     if (size_used1 != size_used2) {
1202       XBT_VERB("(%ld - %ld) Different size used in stacks: %zu - %zu", s1->num_state_, s2->num_state_, size_used1,
1203                size_used2);
1204       return false;
1205     }
1206   }
1207
1208   /* Init heap information used in heap comparison algorithm */
1209   const s_xbt_mheap_t* heap1 = static_cast<xbt_mheap_t>(
1210       s1->read_bytes(alloca(sizeof(s_xbt_mheap_t)), sizeof(s_xbt_mheap_t), process.heap_address, ReadOptions::lazy()));
1211   const s_xbt_mheap_t* heap2 = static_cast<xbt_mheap_t>(
1212       s2->read_bytes(alloca(sizeof(s_xbt_mheap_t)), sizeof(s_xbt_mheap_t), process.heap_address, ReadOptions::lazy()));
1213   if (state_comparator.initHeapInformation(heap1, heap2, s1->to_ignore_, s2->to_ignore_) == -1) {
1214     XBT_VERB("(%ld - %ld) Different heap information", s1->num_state_, s2->num_state_);
1215     return false;
1216   }
1217
1218   /* Stacks comparison */
1219   for (unsigned int cursor = 0; cursor < s1->stacks_.size(); cursor++) {
1220     const_mc_snapshot_stack_t stack1 = &s1->stacks_[cursor];
1221     const_mc_snapshot_stack_t stack2 = &s2->stacks_[cursor];
1222
1223     if (local_variables_differ(process, state_comparator, *s1, *s2, stack1, stack2)) {
1224       XBT_VERB("(%ld - %ld) Different local variables between stacks %u", s1->num_state_, s2->num_state_, cursor + 1);
1225       return false;
1226     }
1227   }
1228
1229   size_t regions_count = s1->snapshot_regions_.size();
1230   if (regions_count != s2->snapshot_regions_.size())
1231     return false;
1232
1233   for (size_t k = 0; k != regions_count; ++k) {
1234     Region* region1 = s1->snapshot_regions_[k].get();
1235     Region* region2 = s2->snapshot_regions_[k].get();
1236
1237     // Preconditions:
1238     if (region1->region_type() != RegionType::Data)
1239       continue;
1240
1241     xbt_assert(region1->region_type() == region2->region_type());
1242     xbt_assert(region1->object_info() == region2->object_info());
1243     xbt_assert(region1->object_info());
1244
1245     /* Compare global variables */
1246     if (global_variables_differ(process, state_comparator, region1->object_info(), region1, region2, *s1, *s2)) {
1247       std::string const& name = region1->object_info()->file_name;
1248       XBT_VERB("(%ld - %ld) Different global variables in %s", s1->num_state_, s2->num_state_, name.c_str());
1249       return false;
1250     }
1251   }
1252
1253   /* Compare heap */
1254   if (mmalloc_heap_differ(process, state_comparator, *s1, *s2)) {
1255     XBT_VERB("(%ld - %ld) Different heap (mmalloc_compare)", s1->num_state_, s2->num_state_);
1256     return false;
1257   }
1258
1259   XBT_VERB("(%ld - %ld) No difference found", s1->num_state_, s2->num_state_);
1260
1261   return true;
1262 }
1263 } // namespace simgrid::mc