Logo AND Algorithmique Numérique Distribuée

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