Logo AND Algorithmique Numérique Distribuée

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