Logo AND Algorithmique Numérique Distribuée

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