Logo AND Algorithmique Numérique Distribuée

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