Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Cleanups in class mc::Region
[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
280 int mmalloc_compare_heap(
281   simgrid::mc::StateComparator& state, simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2)
282 {
283   simgrid::mc::RemoteClient* process = &mc_model_checker->process();
284
285   /* Start comparison */
286   int nb_diff1 = 0;
287   int nb_diff2 = 0;
288   bool equal;
289
290   /* Check busy blocks */
291   size_t i1 = 1;
292
293   malloc_info heapinfo_temp1;
294   malloc_info heapinfo_temp2;
295   malloc_info heapinfo_temp2b;
296
297   simgrid::mc::Region* heap_region1 = MC_get_heap_region(snapshot1);
298   simgrid::mc::Region* heap_region2 = MC_get_heap_region(snapshot2);
299
300   // This is the address of std_heap->heapinfo in the application process:
301   void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
302
303   // This is in snapshot do not use them directly:
304   const malloc_info* heapinfos1 =
305       snapshot1->read<malloc_info*>(RemotePtr<malloc_info*>((std::uint64_t)heapinfo_address));
306   const malloc_info* heapinfos2 =
307       snapshot2->read<malloc_info*>(RemotePtr<malloc_info*>((std::uint64_t)heapinfo_address));
308
309   while (i1 < state.heaplimit) {
310
311     const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(heap_region1, &heapinfo_temp1, &heapinfos1[i1], sizeof(malloc_info));
312     const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2, &heapinfos2[i1], sizeof(malloc_info));
313
314     if (heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type == MMALLOC_TYPE_HEAPINFO) {      /* Free block */
315       i1 ++;
316       continue;
317     }
318
319     if (heapinfo1->type < 0) {
320       fprintf(stderr, "Unkown mmalloc block type.\n");
321       abort();
322     }
323
324     void* addr_block1 = ((void*)(((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase));
325
326     if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED) {       /* Large block */
327
328       if (is_stack(addr_block1)) {
329         for (size_t k = 0; k < heapinfo1->busy_block.size; k++)
330           state.equals_to1_(i1 + k, 0) = HeapArea(i1, -1);
331         for (size_t k = 0; k < heapinfo2->busy_block.size; k++)
332           state.equals_to2_(i1 + k, 0) = HeapArea(i1, -1);
333         i1 += heapinfo1->busy_block.size;
334         continue;
335       }
336
337       if (state.equals_to1_(i1, 0).valid_) {
338         i1++;
339         continue;
340       }
341
342       size_t i2 = 1;
343       equal     = false;
344
345       /* Try first to associate to same block in the other heap */
346       if (heapinfo2->type == heapinfo1->type && state.equals_to2_(i1, 0).valid_ == 0) {
347         void* addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
348         int res_compare = compare_heap_area(state, addr_block1, addr_block2, snapshot1, snapshot2, nullptr, nullptr, 0);
349         if (res_compare != 1) {
350           for (size_t k = 1; k < heapinfo2->busy_block.size; k++)
351             state.equals_to2_(i1 + k, 0) = HeapArea(i1, -1);
352           for (size_t k = 1; k < heapinfo1->busy_block.size; k++)
353             state.equals_to1_(i1 + k, 0) = HeapArea(i1, -1);
354           equal = true;
355           i1 += heapinfo1->busy_block.size;
356         }
357       }
358
359       while (i2 < state.heaplimit && not equal) {
360
361         void* addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
362
363         if (i2 == i1) {
364           i2++;
365           continue;
366         }
367
368         const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2b, &heapinfos2[i2], sizeof(malloc_info));
369
370         if (heapinfo2b->type != MMALLOC_TYPE_UNFRAGMENTED) {
371           i2++;
372           continue;
373         }
374
375         if (state.equals_to2_(i2, 0).valid_) {
376           i2++;
377           continue;
378         }
379
380         int res_compare = compare_heap_area(state, addr_block1, addr_block2, snapshot1, snapshot2, nullptr, nullptr, 0);
381
382         if (res_compare != 1) {
383           for (size_t k = 1; k < heapinfo2b->busy_block.size; k++)
384             state.equals_to2_(i2 + k, 0) = HeapArea(i1, -1);
385           for (size_t k = 1; k < heapinfo1->busy_block.size; k++)
386             state.equals_to1_(i1 + k, 0) = HeapArea(i2, -1);
387           equal = true;
388           i1 += heapinfo1->busy_block.size;
389         }
390
391         i2++;
392       }
393
394       if (not equal) {
395         XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1, heapinfo1->busy_block.busy_size, addr_block1);
396         i1 = state.heaplimit + 1;
397         nb_diff1++;
398       }
399
400     } else {                    /* Fragmented block */
401
402       for (size_t j1 = 0; j1 < (size_t)(BLOCKSIZE >> heapinfo1->type); j1++) {
403
404         if (heapinfo1->busy_frag.frag_size[j1] == -1) /* Free fragment_ */
405           continue;
406
407         if (state.equals_to1_(i1, j1).valid_)
408           continue;
409
410         void* addr_frag1 = (void*)((char*)addr_block1 + (j1 << heapinfo1->type));
411
412         size_t i2 = 1;
413         equal     = false;
414
415         /* Try first to associate to same fragment_ in the other heap */
416         if (heapinfo2->type == heapinfo1->type && not state.equals_to2_(i1, j1).valid_) {
417           void* addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
418           void* addr_frag2  = (void*)((char*)addr_block2 + (j1 << heapinfo2->type));
419           int res_compare = compare_heap_area(state, addr_frag1, addr_frag2, snapshot1, snapshot2, nullptr, nullptr, 0);
420           if (res_compare != 1)
421             equal = true;
422         }
423
424         while (i2 < state.heaplimit && not equal) {
425
426           const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(
427             heap_region2, &heapinfo_temp2b, &heapinfos2[i2],
428             sizeof(malloc_info));
429
430           if (heapinfo2b->type == MMALLOC_TYPE_FREE || heapinfo2b->type == MMALLOC_TYPE_HEAPINFO) {
431             i2 ++;
432             continue;
433           }
434
435           // We currently do not match fragments with unfragmented blocks (maybe we should).
436           if (heapinfo2b->type == MMALLOC_TYPE_UNFRAGMENTED) {
437             i2++;
438             continue;
439           }
440
441           if (heapinfo2b->type < 0) {
442             fprintf(stderr, "Unknown mmalloc block type.\n");
443             abort();
444           }
445
446           for (size_t j2 = 0; j2 < (size_t)(BLOCKSIZE >> heapinfo2b->type); j2++) {
447
448             if (i2 == i1 && j2 == j1)
449               continue;
450
451             if (state.equals_to2_(i2, j2).valid_)
452               continue;
453
454             void* addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
455             void* addr_frag2  = (void*)((char*)addr_block2 + (j2 << heapinfo2b->type));
456
457             int res_compare =
458                 compare_heap_area(state, addr_frag1, addr_frag2, snapshot2, snapshot2, nullptr, nullptr, 0);
459             if (res_compare != 1) {
460               equal = true;
461               break;
462             }
463           }
464
465           i2++;
466         }
467
468         if (not equal) {
469           XBT_DEBUG("Block %zu, fragment_ %zu not found (size_used = %zd, address = %p)\n", i1, j1,
470                     heapinfo1->busy_frag.frag_size[j1], addr_frag1);
471           i1 = state.heaplimit + 1;
472           nb_diff1++;
473           break;
474         }
475       }
476
477       i1++;
478     }
479   }
480
481   /* All blocks/fragments are equal to another block/fragment_ ? */
482   for (size_t i = 1; i < state.heaplimit; i++) {
483     const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
484       heap_region1, &heapinfo_temp1, &heapinfos1[i], sizeof(malloc_info));
485
486     if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED && i1 == state.heaplimit && heapinfo1->busy_block.busy_size > 0 &&
487         not state.equals_to1_(i, 0).valid_) {
488       XBT_DEBUG("Block %zu not found (size used = %zu)", i, heapinfo1->busy_block.busy_size);
489       nb_diff1++;
490     }
491
492     if (heapinfo1->type <= 0)
493       continue;
494     for (size_t j = 0; j < (size_t)(BLOCKSIZE >> heapinfo1->type); j++)
495       if (i1 == state.heaplimit && heapinfo1->busy_frag.frag_size[j] > 0 && not state.equals_to1_(i, j).valid_) {
496         XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)", i, j, heapinfo1->busy_frag.frag_size[j]);
497         nb_diff1++;
498       }
499   }
500
501   if (i1 == state.heaplimit)
502     XBT_DEBUG("Number of blocks/fragments not found in heap1: %d", nb_diff1);
503
504   for (size_t i = 1; i < state.heaplimit; i++) {
505     const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
506       heap_region2, &heapinfo_temp2, &heapinfos2[i], sizeof(malloc_info));
507     if (heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED && i1 == state.heaplimit && heapinfo2->busy_block.busy_size > 0 &&
508         not state.equals_to2_(i, 0).valid_) {
509       XBT_DEBUG("Block %zu not found (size used = %zu)", i,
510                 heapinfo2->busy_block.busy_size);
511       nb_diff2++;
512     }
513
514     if (heapinfo2->type <= 0)
515       continue;
516
517     for (size_t j = 0; j < (size_t)(BLOCKSIZE >> heapinfo2->type); j++)
518       if (i1 == state.heaplimit && heapinfo2->busy_frag.frag_size[j] > 0 && not state.equals_to2_(i, j).valid_) {
519         XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)",
520           i, j, heapinfo2->busy_frag.frag_size[j]);
521         nb_diff2++;
522       }
523
524   }
525
526   if (i1 == state.heaplimit)
527     XBT_DEBUG("Number of blocks/fragments not found in heap2: %d", nb_diff2);
528
529   return nb_diff1 > 0 || nb_diff2 > 0;
530 }
531
532 /**
533  *
534  * @param state
535  * @param real_area1     Process address for state 1
536  * @param real_area2     Process address for state 2
537  * @param snapshot1      Snapshot of state 1
538  * @param snapshot2      Snapshot of state 2
539  * @param previous
540  * @param size
541  * @param check_ignore
542  */
543 static int compare_heap_area_without_type(simgrid::mc::StateComparator& state, const void* real_area1,
544                                           const void* real_area2, simgrid::mc::Snapshot* snapshot1,
545                                           simgrid::mc::Snapshot* snapshot2, HeapLocationPairs* previous, int size,
546                                           int check_ignore)
547 {
548   simgrid::mc::RemoteClient* process = &mc_model_checker->process();
549   simgrid::mc::Region* heap_region1  = MC_get_heap_region(snapshot1);
550   simgrid::mc::Region* heap_region2  = MC_get_heap_region(snapshot2);
551
552   for (int i = 0; i < size; ) {
553
554     if (check_ignore > 0) {
555       ssize_t ignore1 = heap_comparison_ignore_size(
556         state.processStates[0].to_ignore, (char *) real_area1 + i);
557       if (ignore1 != -1) {
558         ssize_t ignore2 = heap_comparison_ignore_size(
559           state.processStates[1].to_ignore, (char *) real_area2 + i);
560         if (ignore2 == ignore1) {
561           if (ignore1 == 0) {
562             check_ignore--;
563             return 0;
564           } else {
565             i = i + ignore2;
566             check_ignore--;
567             continue;
568           }
569         }
570       }
571     }
572
573     if (MC_snapshot_region_memcmp(((char *) real_area1) + i, heap_region1, ((char *) real_area2) + i, heap_region2, 1) != 0) {
574
575       int pointer_align = (i / sizeof(void *)) * sizeof(void *);
576       const void* addr_pointed1 = snapshot1->read(remote((void**)((char*)real_area1 + pointer_align)));
577       const void* addr_pointed2 = snapshot2->read(remote((void**)((char*)real_area2 + pointer_align)));
578
579       if (process->in_maestro_stack(remote(addr_pointed1))
580         && process->in_maestro_stack(remote(addr_pointed2))) {
581         i = pointer_align + sizeof(void *);
582         continue;
583       }
584
585       if (addr_pointed1 > state.std_heap_copy.heapbase
586            && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
587            && addr_pointed2 > state.std_heap_copy.heapbase
588            && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)) {
589         // Both addreses are in the heap:
590         int res_compare =
591             compare_heap_area(state, addr_pointed1, addr_pointed2, snapshot1, snapshot2, previous, nullptr, 0);
592         if (res_compare == 1)
593           return res_compare;
594         i = pointer_align + sizeof(void *);
595         continue;
596       }
597
598       return 1;
599     }
600
601     i++;
602   }
603
604   return 0;
605 }
606
607 /**
608  *
609  * @param state
610  * @param real_area1     Process address for state 1
611  * @param real_area2     Process address for state 2
612  * @param snapshot1      Snapshot of state 1
613  * @param snapshot2      Snapshot of state 2
614  * @param previous
615  * @param type
616  * @param area_size      either a byte_size or an elements_count (?)
617  * @param check_ignore
618  * @param pointer_level
619  * @return               0 (same), 1 (different), -1 (unknown)
620  */
621 static int compare_heap_area_with_type(simgrid::mc::StateComparator& state, const void* real_area1,
622                                        const void* real_area2, simgrid::mc::Snapshot* snapshot1,
623                                        simgrid::mc::Snapshot* snapshot2, HeapLocationPairs* previous,
624                                        simgrid::mc::Type* type, int area_size, int check_ignore, int pointer_level)
625 {
626   do {
627
628     // HACK: This should not happen but in pratice, there are some
629     // DW_TAG_typedef without an associated DW_AT_type:
630     //<1><538832>: Abbrev Number: 111 (DW_TAG_typedef)
631     //    <538833>   DW_AT_name        : (indirect string, offset: 0x2292f3): gregset_t
632     //    <538837>   DW_AT_decl_file   : 98
633     //    <538838>   DW_AT_decl_line   : 37
634     if (type == nullptr)
635       return 0;
636
637     if (is_stack(real_area1) && is_stack(real_area2))
638       return 0;
639
640     if (check_ignore > 0) {
641       ssize_t ignore1 = heap_comparison_ignore_size(state.processStates[0].to_ignore, real_area1);
642       if (ignore1 > 0 && heap_comparison_ignore_size(state.processStates[1].to_ignore, real_area2) == ignore1)
643         return 0;
644     }
645
646     simgrid::mc::Type* subtype;
647     simgrid::mc::Type* subsubtype;
648     int elm_size;
649     const void* addr_pointed1;
650     const void* addr_pointed2;
651
652     simgrid::mc::Region* heap_region1 = MC_get_heap_region(snapshot1);
653     simgrid::mc::Region* heap_region2 = MC_get_heap_region(snapshot2);
654
655     switch (type->type) {
656       case DW_TAG_unspecified_type:
657         return 1;
658
659       case DW_TAG_base_type:
660         if (not type->name.empty() && type->name == "char") { /* String, hence random (arbitrary ?) size */
661           if (real_area1 == real_area2)
662             return -1;
663           else
664             return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, area_size) != 0;
665         } else {
666           if (area_size != -1 && type->byte_size != area_size)
667             return -1;
668           else
669             return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
670         }
671
672       case DW_TAG_enumeration_type:
673         if (area_size != -1 && type->byte_size != area_size)
674           return -1;
675         return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
676
677       case DW_TAG_typedef:
678       case DW_TAG_const_type:
679       case DW_TAG_volatile_type:
680         // Poor man's TCO:
681         type = type->subtype;
682         continue; // restart
683
684       case DW_TAG_array_type:
685         subtype = type->subtype;
686         switch (subtype->type) {
687           case DW_TAG_unspecified_type:
688             return 1;
689
690           case DW_TAG_base_type:
691           case DW_TAG_enumeration_type:
692           case DW_TAG_pointer_type:
693           case DW_TAG_reference_type:
694           case DW_TAG_rvalue_reference_type:
695           case DW_TAG_structure_type:
696           case DW_TAG_class_type:
697           case DW_TAG_union_type:
698             if (subtype->full_type)
699               subtype = subtype->full_type;
700             elm_size  = subtype->byte_size;
701             break;
702           // TODO, just remove the type indirection?
703           case DW_TAG_const_type:
704           case DW_TAG_typedef:
705           case DW_TAG_volatile_type:
706             subsubtype = subtype->subtype;
707             if (subsubtype->full_type)
708               subsubtype = subsubtype->full_type;
709             elm_size     = subsubtype->byte_size;
710             break;
711           default:
712             return 0;
713         }
714         for (int i = 0; i < type->element_count; i++) {
715           // TODO, add support for variable stride (DW_AT_byte_stride)
716           int res = compare_heap_area_with_type(state, (char*)real_area1 + (i * elm_size),
717                                                 (char*)real_area2 + (i * elm_size), snapshot1, snapshot2, previous,
718                                                 type->subtype, subtype->byte_size, check_ignore, pointer_level);
719           if (res == 1)
720             return res;
721         }
722         return 0;
723
724       case DW_TAG_reference_type:
725       case DW_TAG_rvalue_reference_type:
726       case DW_TAG_pointer_type:
727         if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
728           addr_pointed1 = snapshot1->read(remote((void**)real_area1));
729           addr_pointed2 = snapshot2->read(remote((void**)real_area2));
730           return (addr_pointed1 != addr_pointed2);
731         }
732         pointer_level++;
733         if (pointer_level <= 1) {
734           addr_pointed1 = snapshot1->read(remote((void**)real_area1));
735           addr_pointed2 = snapshot2->read(remote((void**)real_area2));
736           if (addr_pointed1 > state.std_heap_copy.heapbase && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1) &&
737               addr_pointed2 > state.std_heap_copy.heapbase && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
738             return compare_heap_area(state, addr_pointed1, addr_pointed2, snapshot1, snapshot2, previous, type->subtype,
739                                      pointer_level);
740           else
741             return (addr_pointed1 != addr_pointed2);
742         }
743         for (size_t i = 0; i < (area_size / sizeof(void*)); i++) {
744           addr_pointed1 = snapshot1->read(remote((void**)((char*)real_area1 + i * sizeof(void*))));
745           addr_pointed2 = snapshot2->read(remote((void**)((char*)real_area2 + i * sizeof(void*))));
746           int res;
747           if (addr_pointed1 > state.std_heap_copy.heapbase && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1) &&
748               addr_pointed2 > state.std_heap_copy.heapbase && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
749             res = compare_heap_area(state, addr_pointed1, addr_pointed2, snapshot1, snapshot2, previous, type->subtype,
750                                     pointer_level);
751           else
752             res = (addr_pointed1 != addr_pointed2);
753           if (res == 1)
754             return res;
755         }
756         return 0;
757
758       case DW_TAG_structure_type:
759       case DW_TAG_class_type:
760         if (type->full_type)
761           type = type->full_type;
762         if (area_size != -1 && type->byte_size != area_size) {
763           if (area_size <= type->byte_size || area_size % type->byte_size != 0)
764             return -1;
765           for (size_t i = 0; i < (size_t)(area_size / type->byte_size); i++) {
766             int res = compare_heap_area_with_type(state, (char*)real_area1 + i * type->byte_size,
767                                                   (char*)real_area2 + i * type->byte_size, snapshot1, snapshot2,
768                                                   previous, type, -1, check_ignore, 0);
769             if (res == 1)
770               return res;
771           }
772         } else {
773           for (simgrid::mc::Member& member : type->members) {
774             // TODO, optimize this? (for the offset case)
775             void* real_member1 =
776                 simgrid::dwarf::resolve_member(real_area1, type, &member, (simgrid::mc::AddressSpace*)snapshot1);
777             void* real_member2 =
778                 simgrid::dwarf::resolve_member(real_area2, type, &member, (simgrid::mc::AddressSpace*)snapshot2);
779             int res = compare_heap_area_with_type(state, real_member1, real_member2, snapshot1, snapshot2, previous,
780                                                   member.type, -1, check_ignore, 0);
781             if (res == 1)
782               return res;
783           }
784         }
785         return 0;
786
787       case DW_TAG_union_type:
788         return compare_heap_area_without_type(state, real_area1, real_area2, snapshot1, snapshot2, previous,
789                                               type->byte_size, check_ignore);
790
791       default:
792         return 0;
793     }
794
795     xbt_die("Unreachable");
796   } while (true);
797 }
798
799 /** Infer the type of a part of the block from the type of the block
800  *
801  * TODO, handle DW_TAG_array_type as well as arrays of the object ((*p)[5], p[5])
802  *
803  * TODO, handle subfields ((*p).bar.foo, (*p)[5].bar…)
804  *
805  * @param  type               DWARF type ID of the root address
806  * @param  area_size
807  * @return                    DWARF type ID for given offset
808  */
809 static simgrid::mc::Type* get_offset_type(void* real_base_address, simgrid::mc::Type* type, int offset, int area_size,
810                                           simgrid::mc::Snapshot* snapshot)
811 {
812
813   // Beginning of the block, the infered variable type if the type of the block:
814   if (offset == 0)
815     return type;
816
817   switch (type->type) {
818
819   case DW_TAG_structure_type:
820   case DW_TAG_class_type:
821     if (type->full_type)
822       type = type->full_type;
823     if (area_size != -1 && type->byte_size != area_size) {
824       if (area_size > type->byte_size && area_size % type->byte_size == 0)
825         return type;
826       else
827         return nullptr;
828     }
829
830     for (simgrid::mc::Member& member : type->members) {
831       if (member.has_offset_location()) {
832         // We have the offset, use it directly (shortcut):
833         if (member.offset() == offset)
834           return member.type;
835       } else {
836         void* real_member = simgrid::dwarf::resolve_member(real_base_address, type, &member, snapshot);
837         if ((char*)real_member - (char*)real_base_address == offset)
838           return member.type;
839       }
840     }
841     return nullptr;
842
843   default:
844     /* FIXME: other cases ? */
845     return nullptr;
846
847   }
848 }
849
850 /**
851  *
852  * @param area1          Process address for state 1
853  * @param area2          Process address for state 2
854  * @param snapshot1      Snapshot of state 1
855  * @param snapshot2      Snapshot of state 2
856  * @param previous       Pairs of blocks already compared on the current path (or nullptr)
857  * @param type_id        Type of variable
858  * @param pointer_level
859  * @return 0 (same), 1 (different), -1
860  */
861 static int compare_heap_area(simgrid::mc::StateComparator& state, const void* area1, const void* area2,
862                              simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2,
863                              HeapLocationPairs* previous, simgrid::mc::Type* type, int pointer_level)
864 {
865   simgrid::mc::RemoteClient* process = &mc_model_checker->process();
866
867   ssize_t block1;
868   ssize_t block2;
869   ssize_t size;
870   int check_ignore = 0;
871
872   int type_size = -1;
873   int offset1   = 0;
874   int offset2   = 0;
875   int new_size1 = -1;
876   int new_size2 = -1;
877
878   simgrid::mc::Type* new_type1 = nullptr;
879   simgrid::mc::Type* new_type2 = nullptr;
880
881   bool match_pairs = false;
882
883   // This is the address of std_heap->heapinfo in the application process:
884   void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
885
886   const malloc_info* heapinfos1 = snapshot1->read(remote((const malloc_info**)heapinfo_address));
887   const malloc_info* heapinfos2 = snapshot2->read(remote((const malloc_info**)heapinfo_address));
888
889   malloc_info heapinfo_temp1;
890   malloc_info heapinfo_temp2;
891
892   simgrid::mc::HeapLocationPairs current;
893   if (previous == nullptr) {
894     previous = &current;
895     match_pairs = true;
896   }
897
898   // Get block number:
899   block1 = ((char*)area1 - (char*)state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
900   block2 = ((char*)area2 - (char*)state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
901
902   // If either block is a stack block:
903   if (is_block_stack((int) block1) && is_block_stack((int) block2)) {
904     previous->insert(simgrid::mc::makeHeapLocationPair(block1, -1, block2, -1));
905     if (match_pairs)
906       state.match_equals(previous);
907     return 0;
908   }
909
910   // If either block is not in the expected area of memory:
911   if (((char*)area1 < (char*)state.std_heap_copy.heapbase) || (block1 > (ssize_t)state.processStates[0].heapsize) ||
912       (block1 < 1) || ((char*)area2 < (char*)state.std_heap_copy.heapbase) ||
913       (block2 > (ssize_t)state.processStates[1].heapsize) || (block2 < 1)) {
914     return 1;
915   }
916
917   // Process address of the block:
918   void* real_addr_block1 = (ADDR2UINT(block1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
919   void* real_addr_block2 = (ADDR2UINT(block2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
920
921   if (type) {
922     if (type->full_type)
923       type = type->full_type;
924
925     // This assume that for "boring" types (volatile ...) byte_size is absent:
926     while (type->byte_size == 0 && type->subtype != nullptr)
927       type = type->subtype;
928
929     // Find type_size:
930     if (type->type == DW_TAG_pointer_type ||
931         (type->type == DW_TAG_base_type && not type->name.empty() && type->name == "char"))
932       type_size = -1;
933     else
934       type_size = type->byte_size;
935
936   }
937
938   simgrid::mc::Region* heap_region1 = MC_get_heap_region(snapshot1);
939   simgrid::mc::Region* heap_region2 = MC_get_heap_region(snapshot2);
940
941   const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
942     heap_region1, &heapinfo_temp1, &heapinfos1[block1], sizeof(malloc_info));
943   const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
944     heap_region2, &heapinfo_temp2, &heapinfos2[block2], sizeof(malloc_info));
945
946   if ((heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type==MMALLOC_TYPE_HEAPINFO)
947     && (heapinfo2->type == MMALLOC_TYPE_FREE || heapinfo2->type ==MMALLOC_TYPE_HEAPINFO)) {
948     /* Free block */
949     if (match_pairs)
950       state.match_equals(previous);
951     return 0;
952   }
953
954   if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED && heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
955     /* Complete block */
956
957     // TODO, lookup variable type from block type as done for fragmented blocks
958
959     if (state.equals_to1_(block1, 0).valid_ && state.equals_to2_(block2, 0).valid_ &&
960         state.blocksEqual(block1, block2)) {
961       if (match_pairs)
962         state.match_equals(previous);
963       return 0;
964     }
965
966     if (type_size != -1 && type_size != (ssize_t)heapinfo1->busy_block.busy_size &&
967         type_size != (ssize_t)heapinfo2->busy_block.busy_size &&
968         (type->name.empty() || type->name == "struct s_smx_context")) {
969       if (match_pairs)
970         state.match_equals(previous);
971       return -1;
972     }
973
974     if (heapinfo1->busy_block.size != heapinfo2->busy_block.size)
975       return 1;
976     if (heapinfo1->busy_block.busy_size != heapinfo2->busy_block.busy_size)
977       return 1;
978
979     if (not previous->insert(simgrid::mc::makeHeapLocationPair(block1, -1, block2, -1)).second) {
980       if (match_pairs)
981         state.match_equals(previous);
982       return 0;
983     }
984
985     size = heapinfo1->busy_block.busy_size;
986
987     // Remember (basic) type inference.
988     // The current data structure only allows us to do this for the whole block.
989     if (type != nullptr && area1 == real_addr_block1)
990       state.types1_(block1, 0) = type;
991     if (type != nullptr && area2 == real_addr_block2)
992       state.types2_(block2, 0) = type;
993
994     if (size <= 0) {
995       if (match_pairs)
996         state.match_equals(previous);
997       return 0;
998     }
999
1000     if (heapinfo1->busy_block.ignore > 0
1001         && heapinfo2->busy_block.ignore == heapinfo1->busy_block.ignore)
1002       check_ignore = heapinfo1->busy_block.ignore;
1003
1004   } else if ((heapinfo1->type > 0) && (heapinfo2->type > 0)) {      /* Fragmented block */
1005
1006     // Fragment number:
1007     ssize_t frag1 = ((uintptr_t)(ADDR2UINT(area1) % (BLOCKSIZE))) >> heapinfo1->type;
1008     ssize_t frag2 = ((uintptr_t)(ADDR2UINT(area2) % (BLOCKSIZE))) >> heapinfo2->type;
1009
1010     // Process address of the fragment_:
1011     void* real_addr_frag1 = (void*)((char*)real_addr_block1 + (frag1 << heapinfo1->type));
1012     void* real_addr_frag2 = (void*)((char*)real_addr_block2 + (frag2 << heapinfo2->type));
1013
1014     // Check the size of the fragments against the size of the type:
1015     if (type_size != -1) {
1016       if (heapinfo1->busy_frag.frag_size[frag1] == -1 || heapinfo2->busy_frag.frag_size[frag2] == -1) {
1017         if (match_pairs)
1018           state.match_equals(previous);
1019         return -1;
1020       }
1021       // ?
1022       if (type_size != heapinfo1->busy_frag.frag_size[frag1]
1023           || type_size != heapinfo2->busy_frag.frag_size[frag2]) {
1024         if (match_pairs)
1025           state.match_equals(previous);
1026         return -1;
1027       }
1028     }
1029
1030     // Check if the blocks are already matched together:
1031     if (state.equals_to1_(block1, frag1).valid_ && state.equals_to2_(block2, frag2).valid_ && offset1 == offset2 &&
1032         state.fragmentsEqual(block1, frag1, block2, frag2)) {
1033       if (match_pairs)
1034         state.match_equals(previous);
1035       return 0;
1036     }
1037     // Compare the size of both fragments:
1038     if (heapinfo1->busy_frag.frag_size[frag1] != heapinfo2->busy_frag.frag_size[frag2]) {
1039       if (type_size == -1) {
1040         if (match_pairs)
1041           state.match_equals(previous);
1042         return -1;
1043       } else
1044         return 1;
1045     }
1046
1047     // Size of the fragment_:
1048     size = heapinfo1->busy_frag.frag_size[frag1];
1049
1050     // Remember (basic) type inference.
1051     // The current data structure only allows us to do this for the whole fragment_.
1052     if (type != nullptr && area1 == real_addr_frag1)
1053       state.types1_(block1, frag1) = type;
1054     if (type != nullptr && area2 == real_addr_frag2)
1055       state.types2_(block2, frag2) = type;
1056
1057     // The type of the variable is already known:
1058     if (type) {
1059       new_type1 = new_type2 = type;
1060     }
1061     // Type inference from the block type.
1062     else if (state.types1_(block1, frag1) != nullptr || state.types2_(block2, frag2) != nullptr) {
1063
1064       offset1 = (char*)area1 - (char*)real_addr_frag1;
1065       offset2 = (char*)area2 - (char*)real_addr_frag2;
1066
1067       if (state.types1_(block1, frag1) != nullptr && state.types2_(block2, frag2) != nullptr) {
1068         new_type1 = get_offset_type(real_addr_frag1, state.types1_(block1, frag1), offset1, size, snapshot1);
1069         new_type2 = get_offset_type(real_addr_frag2, state.types2_(block2, frag2), offset1, size, snapshot2);
1070       } else if (state.types1_(block1, frag1) != nullptr) {
1071         new_type1 = get_offset_type(real_addr_frag1, state.types1_(block1, frag1), offset1, size, snapshot1);
1072         new_type2 = get_offset_type(real_addr_frag2, state.types1_(block1, frag1), offset2, size, snapshot2);
1073       } else if (state.types2_(block2, frag2) != nullptr) {
1074         new_type1 = get_offset_type(real_addr_frag1, state.types2_(block2, frag2), offset1, size, snapshot1);
1075         new_type2 = get_offset_type(real_addr_frag2, state.types2_(block2, frag2), offset2, size, snapshot2);
1076       } else {
1077         if (match_pairs)
1078           state.match_equals(previous);
1079         return -1;
1080       }
1081
1082       if (new_type1 != nullptr && new_type2 != nullptr && new_type1 != new_type2) {
1083
1084         type = new_type1;
1085         while (type->byte_size == 0 && type->subtype != nullptr)
1086           type = type->subtype;
1087         new_size1 = type->byte_size;
1088
1089         type = new_type2;
1090         while (type->byte_size == 0 && type->subtype != nullptr)
1091           type = type->subtype;
1092         new_size2 = type->byte_size;
1093
1094       } else {
1095         if (match_pairs)
1096           state.match_equals(previous);
1097         return -1;
1098       }
1099     }
1100
1101     if (new_size1 > 0 && new_size1 == new_size2) {
1102       type = new_type1;
1103       size = new_size1;
1104     }
1105
1106     if (offset1 == 0 && offset2 == 0 &&
1107         not previous->insert(simgrid::mc::makeHeapLocationPair(block1, frag1, block2, frag2)).second) {
1108       if (match_pairs)
1109         state.match_equals(previous);
1110       return 0;
1111     }
1112
1113     if (size <= 0) {
1114       if (match_pairs)
1115         state.match_equals(previous);
1116       return 0;
1117     }
1118
1119     if ((heapinfo1->busy_frag.ignore[frag1] > 0) &&
1120         (heapinfo2->busy_frag.ignore[frag2] == heapinfo1->busy_frag.ignore[frag1]))
1121       check_ignore = heapinfo1->busy_frag.ignore[frag1];
1122
1123   } else
1124     return 1;
1125
1126
1127   /* Start comparison */
1128   int res_compare;
1129   if (type)
1130     res_compare = compare_heap_area_with_type(state, area1, area2, snapshot1, snapshot2, previous, type, size,
1131                                               check_ignore, pointer_level);
1132   else
1133     res_compare =
1134         compare_heap_area_without_type(state, area1, area2, snapshot1, snapshot2, previous, size, check_ignore);
1135
1136   if (res_compare == 1)
1137     return res_compare;
1138
1139   if (match_pairs)
1140     state.match_equals(previous);
1141   return 0;
1142 }
1143
1144 }
1145 }
1146
1147 /************************** Snapshot comparison *******************************/
1148 /******************************************************************************/
1149
1150 static int compare_areas_with_type(simgrid::mc::StateComparator& state, void* real_area1,
1151                                    simgrid::mc::Snapshot* snapshot1, simgrid::mc::Region* region1, void* real_area2,
1152                                    simgrid::mc::Snapshot* snapshot2, simgrid::mc::Region* region2,
1153                                    simgrid::mc::Type* type, int pointer_level)
1154 {
1155   simgrid::mc::RemoteClient* process = &mc_model_checker->process();
1156
1157   simgrid::mc::Type* subtype;
1158   simgrid::mc::Type* subsubtype;
1159   int elm_size;
1160   int i;
1161   int res;
1162
1163   do {
1164     xbt_assert(type != nullptr);
1165     switch (type->type) {
1166       case DW_TAG_unspecified_type:
1167         return 1;
1168
1169       case DW_TAG_base_type:
1170       case DW_TAG_enumeration_type:
1171       case DW_TAG_union_type:
1172         return MC_snapshot_region_memcmp(real_area1, region1, real_area2, region2, type->byte_size) != 0;
1173       case DW_TAG_typedef:
1174       case DW_TAG_volatile_type:
1175       case DW_TAG_const_type:
1176         // Poor man's TCO:
1177         type = type->subtype;
1178         continue; // restart
1179       case DW_TAG_array_type:
1180         subtype = type->subtype;
1181         switch (subtype->type) {
1182           case DW_TAG_unspecified_type:
1183             return 1;
1184
1185           case DW_TAG_base_type:
1186           case DW_TAG_enumeration_type:
1187           case DW_TAG_pointer_type:
1188           case DW_TAG_reference_type:
1189           case DW_TAG_rvalue_reference_type:
1190           case DW_TAG_structure_type:
1191           case DW_TAG_class_type:
1192           case DW_TAG_union_type:
1193             if (subtype->full_type)
1194               subtype = subtype->full_type;
1195             elm_size  = subtype->byte_size;
1196             break;
1197           case DW_TAG_const_type:
1198           case DW_TAG_typedef:
1199           case DW_TAG_volatile_type:
1200             subsubtype = subtype->subtype;
1201             if (subsubtype->full_type)
1202               subsubtype = subsubtype->full_type;
1203             elm_size     = subsubtype->byte_size;
1204             break;
1205           default:
1206             return 0;
1207         }
1208         for (i = 0; i < type->element_count; i++) {
1209           size_t off = i * elm_size;
1210           res = compare_areas_with_type(state, (char*)real_area1 + off, snapshot1, region1, (char*)real_area2 + off,
1211                                         snapshot2, region2, type->subtype, pointer_level);
1212           if (res == 1)
1213             return res;
1214         }
1215         break;
1216       case DW_TAG_pointer_type:
1217       case DW_TAG_reference_type:
1218       case DW_TAG_rvalue_reference_type: {
1219         void* addr_pointed1 = MC_region_read_pointer(region1, real_area1);
1220         void* addr_pointed2 = MC_region_read_pointer(region2, real_area2);
1221
1222         if (type->subtype && type->subtype->type == DW_TAG_subroutine_type)
1223           return (addr_pointed1 != addr_pointed2);
1224         if (addr_pointed1 == nullptr && addr_pointed2 == nullptr)
1225           return 0;
1226         if (addr_pointed1 == nullptr || addr_pointed2 == nullptr)
1227           return 1;
1228         if (not state.compared_pointers.insert(std::make_pair(addr_pointed1, addr_pointed2)).second)
1229           return 0;
1230
1231         pointer_level++;
1232
1233         // Some cases are not handled here:
1234         // * the pointers lead to different areas (one to the heap, the other to the RW segment ...)
1235         // * a pointer leads to the read-only segment of the current object
1236         // * a pointer lead to a different ELF object
1237
1238         if (addr_pointed1 > process->heap_address && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)) {
1239           if (not(addr_pointed2 > process->heap_address && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)))
1240             return 1;
1241           // The pointers are both in the heap:
1242           return simgrid::mc::compare_heap_area(state, addr_pointed1, addr_pointed2, snapshot1, snapshot2, nullptr,
1243                                                 type->subtype, pointer_level);
1244
1245         } else if (region1->contain(simgrid::mc::remote(addr_pointed1))) {
1246           // The pointers are both in the current object R/W segment:
1247           if (not region2->contain(simgrid::mc::remote(addr_pointed2)))
1248             return 1;
1249           if (not type->type_id)
1250             return (addr_pointed1 != addr_pointed2);
1251           else
1252             return compare_areas_with_type(state, addr_pointed1, snapshot1, region1, addr_pointed2, snapshot2, region2,
1253                                            type->subtype, pointer_level);
1254         } else {
1255
1256           // TODO, We do not handle very well the case where
1257           // it belongs to a different (non-heap) region from the current one.
1258
1259           return (addr_pointed1 != addr_pointed2);
1260         }
1261       }
1262       case DW_TAG_structure_type:
1263       case DW_TAG_class_type:
1264         for (simgrid::mc::Member& member : type->members) {
1265           void* member1 = simgrid::dwarf::resolve_member(real_area1, type, &member, snapshot1);
1266           void* member2 = simgrid::dwarf::resolve_member(real_area2, type, &member, snapshot2);
1267           simgrid::mc::Region* subregion1 = snapshot1->get_region(member1, region1); // region1 is hinted
1268           simgrid::mc::Region* subregion2 = snapshot2->get_region(member2, region2); // region2 is hinted
1269           res = compare_areas_with_type(state, member1, snapshot1, subregion1, member2, snapshot2, subregion2,
1270                                         member.type, pointer_level);
1271           if (res == 1)
1272             return res;
1273         }
1274         break;
1275       case DW_TAG_subroutine_type:
1276         return -1;
1277       default:
1278         XBT_VERB("Unknown case: %d", type->type);
1279         break;
1280     }
1281
1282     return 0;
1283   } while (true);
1284 }
1285
1286 static int compare_global_variables(simgrid::mc::StateComparator& state, simgrid::mc::ObjectInformation* object_info,
1287                                     simgrid::mc::Region* r1, simgrid::mc::Region* r2, simgrid::mc::Snapshot* snapshot1,
1288                                     simgrid::mc::Snapshot* snapshot2)
1289 {
1290   xbt_assert(r1 && r2, "Missing region.");
1291
1292   std::vector<simgrid::mc::Variable>& variables = object_info->global_variables;
1293
1294   for (simgrid::mc::Variable const& current_var : variables) {
1295
1296     // If the variable is not in this object, skip it:
1297     // We do not expect to find a pointer to something which is not reachable
1298     // by the global variables.
1299     if ((char *) current_var.address < (char *) object_info->start_rw
1300         || (char *) current_var.address > (char *) object_info->end_rw)
1301       continue;
1302
1303     simgrid::mc::Type* bvariable_type = current_var.type;
1304     int res = compare_areas_with_type(state, (char*)current_var.address, snapshot1, r1, (char*)current_var.address,
1305                                       snapshot2, r2, bvariable_type, 0);
1306     if (res == 1) {
1307       XBT_VERB("Global variable %s (%p) is different between snapshots",
1308                current_var.name.c_str(),
1309                (char *) current_var.address);
1310       return 1;
1311     }
1312   }
1313
1314   return 0;
1315 }
1316
1317 static int compare_local_variables(simgrid::mc::StateComparator& state,
1318                                    simgrid::mc::Snapshot* snapshot1,
1319                                    simgrid::mc::Snapshot* snapshot2,
1320                                    mc_snapshot_stack_t stack1,
1321                                    mc_snapshot_stack_t stack2)
1322 {
1323   if (stack1->local_variables.size() != stack2->local_variables.size()) {
1324     XBT_VERB("Different number of local variables");
1325     return 1;
1326   }
1327
1328     unsigned int cursor = 0;
1329     local_variable_t current_var1;
1330     local_variable_t current_var2;
1331     while (cursor < stack1->local_variables.size()) {
1332       current_var1 = &stack1->local_variables[cursor];
1333       current_var2 = &stack1->local_variables[cursor];
1334       if (current_var1->name != current_var2->name
1335           || current_var1->subprogram != current_var2->subprogram
1336           || current_var1->ip != current_var2->ip) {
1337         // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1338         XBT_VERB
1339             ("Different name of variable (%s - %s) "
1340              "or frame (%s - %s) or ip (%lu - %lu)",
1341              current_var1->name.c_str(),
1342              current_var2->name.c_str(),
1343              current_var1->subprogram->name.c_str(),
1344              current_var2->subprogram->name.c_str(),
1345              current_var1->ip, current_var2->ip);
1346         return 1;
1347       }
1348       // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1349
1350         simgrid::mc::Type* subtype = current_var1->type;
1351         int res                    = compare_areas_with_type(state, current_var1->address, snapshot1,
1352                                           snapshot1->get_region(current_var1->address), current_var2->address,
1353                                           snapshot2, snapshot2->get_region(current_var2->address), subtype, 0);
1354
1355         if (res == 1) {
1356           // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1357           XBT_VERB("Local variable %s (%p - %p) in frame %s "
1358                    "is different between snapshots",
1359                    current_var1->name.c_str(), current_var1->address, current_var2->address,
1360                    current_var1->subprogram->name.c_str());
1361           return res;
1362       }
1363       cursor++;
1364     }
1365     return 0;
1366 }
1367
1368 namespace simgrid {
1369 namespace mc {
1370
1371 static std::unique_ptr<simgrid::mc::StateComparator> state_comparator;
1372
1373 int snapshot_compare(Snapshot* s1, Snapshot* s2)
1374 {
1375   // TODO, make this a field of ModelChecker or something similar
1376   if (state_comparator == nullptr)
1377     state_comparator.reset(new StateComparator());
1378   else
1379     state_comparator->clear();
1380
1381   RemoteClient* process = &mc_model_checker->process();
1382
1383   int errors = 0;
1384
1385   int hash_result = 0;
1386   if (_sg_mc_hash) {
1387     hash_result = (s1->hash_ != s2->hash_);
1388     if (hash_result) {
1389       XBT_VERB("(%d - %d) Different hash: 0x%" PRIx64 "--0x%" PRIx64, s1->num_state_, s2->num_state_, s1->hash_,
1390                s2->hash_);
1391 #ifndef MC_DEBUG
1392       return 1;
1393 #endif
1394     } else
1395       XBT_VERB("(%d - %d) Same hash: 0x%" PRIx64, s1->num_state_, s2->num_state_, s1->hash_);
1396   }
1397
1398   /* Compare enabled processes */
1399   if (s1->enabled_processes_ != s2->enabled_processes_) {
1400     XBT_VERB("(%d - %d) Different amount of enabled processes", s1->num_state_, s2->num_state_);
1401     return 1;
1402   }
1403
1404   /* Compare size of stacks */
1405   int is_diff = 0;
1406   for (unsigned long i = 0; i < s1->stacks_.size(); i++) {
1407     size_t size_used1 = s1->stack_sizes_[i];
1408     size_t size_used2 = s2->stack_sizes_[i];
1409     if (size_used1 != size_used2) {
1410 #ifdef MC_DEBUG
1411       XBT_DEBUG("(%d - %d) Different size used in stacks: %zu - %zu", s1->num_state, s2->num_state, size_used1,
1412                 size_used2);
1413       errors++;
1414       is_diff = 1;
1415 #else
1416 #ifdef MC_VERBOSE
1417       XBT_VERB("(%d - %d) Different size used in stacks: %zu - %zu", s1->num_state_, s2->num_state_, size_used1,
1418                size_used2);
1419 #endif
1420       return 1;
1421 #endif
1422     }
1423   }
1424   if (is_diff) // do not proceed if there is any stacks that don't match
1425     return 1;
1426
1427   /* Init heap information used in heap comparison algorithm */
1428   xbt_mheap_t heap1 = (xbt_mheap_t)s1->read_bytes(alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1429                                                   remote(process->heap_address), simgrid::mc::ReadOptions::lazy());
1430   xbt_mheap_t heap2 = (xbt_mheap_t)s2->read_bytes(alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1431                                                   remote(process->heap_address), simgrid::mc::ReadOptions::lazy());
1432   int res_init = state_comparator->initHeapInformation(heap1, heap2, &s1->to_ignore_, &s2->to_ignore_);
1433
1434   if (res_init == -1) {
1435 #ifdef MC_DEBUG
1436     XBT_DEBUG("(%d - %d) Different heap information", num1, nus1->num_state, s2->num_statem2);
1437     errors++;
1438 #else
1439 #ifdef MC_VERBOSE
1440     XBT_VERB("(%d - %d) Different heap information", s1->num_state_, s2->num_state_);
1441 #endif
1442
1443     return 1;
1444 #endif
1445   }
1446
1447   /* Stacks comparison */
1448   // int diff_local = 0;
1449   for (unsigned int cursor = 0; cursor < s1->stacks_.size(); cursor++) {
1450     mc_snapshot_stack_t stack1 = &s1->stacks_[cursor];
1451     mc_snapshot_stack_t stack2 = &s2->stacks_[cursor];
1452
1453     if (compare_local_variables(*state_comparator, s1, s2, stack1, stack2) > 0) {
1454 #ifdef MC_DEBUG
1455       XBT_DEBUG("(%d - %d) Different local variables between stacks %d", num1,
1456                 num2, cursor + 1);
1457       errors++;
1458 #else
1459
1460 #ifdef MC_VERBOSE
1461       XBT_VERB("(%d - %d) Different local variables between stacks %u", s1->num_state_, s2->num_state_, cursor + 1);
1462 #endif
1463
1464       return 1;
1465 #endif
1466     }
1467   }
1468
1469   size_t regions_count = s1->snapshot_regions_.size();
1470   // TODO, raise a difference instead?
1471   xbt_assert(regions_count == s2->snapshot_regions_.size());
1472
1473   for (size_t k = 0; k != regions_count; ++k) {
1474     Region* region1 = s1->snapshot_regions_[k].get();
1475     Region* region2 = s2->snapshot_regions_[k].get();
1476
1477     // Preconditions:
1478     if (region1->region_type() != RegionType::Data)
1479       continue;
1480
1481     xbt_assert(region1->region_type() == region2->region_type());
1482     xbt_assert(region1->object_info() == region2->object_info());
1483     xbt_assert(region1->object_info());
1484
1485     /* Compare global variables */
1486     if (compare_global_variables(*state_comparator, region1->object_info(), region1, region2, s1, s2)) {
1487
1488 #ifdef MC_DEBUG
1489       std::string const& name = region1->object_info()->file_name;
1490       XBT_DEBUG("(%d - %d) Different global variables in %s", s1->num_state, s2->num_state, name.c_str());
1491       errors++;
1492 #else
1493 #ifdef MC_VERBOSE
1494       std::string const& name = region1->object_info()->file_name;
1495       XBT_VERB("(%d - %d) Different global variables in %s", s1->num_state_, s2->num_state_, name.c_str());
1496 #endif
1497
1498       return 1;
1499 #endif
1500     }
1501   }
1502
1503   /* Compare heap */
1504   if (mmalloc_compare_heap(*state_comparator, s1, s2) > 0) {
1505
1506 #ifdef MC_DEBUG
1507     XBT_DEBUG("(%d - %d) Different heap (mmalloc_compare)", s1->num_state, s2->num_state);
1508     errors++;
1509 #else
1510
1511 #ifdef MC_VERBOSE
1512     XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", s1->num_state_, s2->num_state_);
1513 #endif
1514     return 1;
1515 #endif
1516   }
1517
1518 #ifdef MC_VERBOSE
1519   if (errors || hash_result)
1520     XBT_VERB("(%d - %d) Difference found", s1->num_state_, s2->num_state_);
1521   else
1522     XBT_VERB("(%d - %d) No difference found", s1->num_state_, s2->num_state_);
1523 #endif
1524
1525 #if defined(MC_DEBUG) && defined(MC_VERBOSE)
1526   if (_sg_mc_hash) {
1527     // * false positive SHOULD be avoided.
1528     // * There MUST not be any false negative.
1529
1530     XBT_VERB("(%d - %d) State equality hash test is %s %s", s1->num_state, s2->num_state,
1531              (hash_result != 0) == (errors != 0) ? "true" : "false", not hash_result ? "positive" : "negative");
1532   }
1533 #endif
1534
1535   return errors > 0 || hash_result;
1536 }
1537
1538 }
1539 }