Logo AND Algorithmique Numérique Distribuée

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