Logo AND Algorithmique Numérique Distribuée

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