Logo AND Algorithmique Numérique Distribuée

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