Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Kill unused parameter and please Sonar.
[simgrid.git] / src / mc / compare.cpp
1 /* Copyright (c) 2008-2019. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 /** \file compare.cpp Memory snapshooting and comparison                    */
7
8 #include <cinttypes>
9
10 #include <array>
11 #include <memory>
12 #include <set>
13 #include <utility>
14 #include <unordered_set>
15
16 #include "xbt/dynar.h"
17 #include "xbt/sysdep.h"
18 #include <xbt/mmalloc.h>
19
20 #include <mc/mc.h>
21 #include <mc/datatypes.h>
22
23 #include "src/internal_config.h"
24
25 #include "src/xbt/mmalloc/mmprivate.h"
26
27 #if HAVE_SMPI
28 #include "src/smpi/include/private.hpp"
29 #endif
30
31 #include "src/mc/Frame.hpp"
32 #include "src/mc/ObjectInformation.hpp"
33 #include "src/mc/Type.hpp"
34 #include "src/mc/Variable.hpp"
35 #include "src/mc/mc_config.hpp"
36 #include "src/mc/mc_dwarf.hpp"
37 #include "src/mc/mc_forward.hpp"
38 #include "src/mc/mc_private.hpp"
39 #include "src/mc/mc_smx.hpp"
40 #include "src/mc/sosp/mc_snapshot.hpp"
41
42 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_compare, xbt, "Logging specific to mc_compare in mc");
43
44 namespace simgrid {
45 namespace mc {
46
47 struct HeapLocation;
48 typedef std::array<HeapLocation, 2> HeapLocationPair;
49 typedef std::set<HeapLocationPair> HeapLocationPairs;
50 struct HeapArea;
51 struct ProcessComparisonState;
52 struct StateComparator;
53
54 static int compare_heap_area(
55   StateComparator& state,
56   int process_index, const void *area1, const void* area2,
57   Snapshot* snapshot1, Snapshot* snapshot2,
58   HeapLocationPairs* previous, Type* type, int pointer_level);
59
60 }
61 }
62
63 using simgrid::mc::remote;
64
65 /*********************************** Heap comparison ***********************************/
66 /***************************************************************************************/
67
68 namespace simgrid {
69 namespace mc {
70
71 class HeapLocation {
72 public:
73   int block_    = 0;
74   int fragment_ = 0;
75
76   HeapLocation() = default;
77   HeapLocation(int block, int fragment = 0) : block_(block), fragment_(fragment) {}
78
79   bool operator==(HeapLocation const& that) const
80   {
81     return block_ == that.block_ && fragment_ == that.fragment_;
82   }
83   bool operator<(HeapLocation const& that) const
84   {
85     return std::make_pair(block_, fragment_) < std::make_pair(that.block_, that.fragment_);
86   }
87 };
88
89 static inline
90 HeapLocationPair makeHeapLocationPair(int block1, int fragment1, int block2, int fragment2)
91 {
92   return simgrid::mc::HeapLocationPair{{
93     simgrid::mc::HeapLocation(block1, fragment1),
94     simgrid::mc::HeapLocation(block2, fragment2)
95   }};
96 }
97
98 class HeapArea : public HeapLocation {
99 public:
100   bool valid_ = false;
101   HeapArea() = default;
102   explicit HeapArea(int block) : valid_(true) { block_ = block; }
103   HeapArea(int block, int fragment) : valid_(true)
104   {
105     block_    = block;
106     fragment_ = fragment;
107   }
108 };
109
110 class ProcessComparisonState {
111 public:
112   std::vector<simgrid::mc::IgnoredHeapRegion>* to_ignore = nullptr;
113   std::vector<HeapArea> equals_to;
114   std::vector<simgrid::mc::Type*> types;
115   std::size_t heapsize = 0;
116
117   void initHeapInformation(xbt_mheap_t heap, std::vector<simgrid::mc::IgnoredHeapRegion>* i);
118 };
119
120 namespace {
121
122 /** A hash which works with more stuff
123  *
124  *  It can hash pairs: the standard hash currently doesn't include this.
125  */
126 template <class X> class hash : public std::hash<X> {
127 };
128
129 template <class X, class Y> class hash<std::pair<X, Y>> {
130 public:
131   std::size_t operator()(std::pair<X,Y>const& x) const
132   {
133     hash<X> h1;
134     hash<X> h2;
135     return h1(x.first) ^ h2(x.second);
136   }
137 };
138
139 }
140
141 class StateComparator {
142 public:
143   s_xbt_mheap_t std_heap_copy;
144   std::size_t heaplimit;
145   std::array<ProcessComparisonState, 2> processStates;
146
147   std::unordered_set<std::pair<void*, void*>, hash<std::pair<void*, void*>>> compared_pointers;
148
149   void clear()
150   {
151     compared_pointers.clear();
152   }
153
154   int initHeapInformation(
155     xbt_mheap_t heap1, xbt_mheap_t heap2,
156     std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
157     std::vector<simgrid::mc::IgnoredHeapRegion>* i2);
158
159   HeapArea& equals_to1_(std::size_t i, std::size_t j)
160   {
161     return processStates[0].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
162   }
163   HeapArea& equals_to2_(std::size_t i, std::size_t j)
164   {
165     return processStates[1].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
166   }
167   Type*& types1_(std::size_t i, std::size_t j)
168   {
169     return processStates[0].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
170   }
171   Type*& types2_(std::size_t i, std::size_t j)
172   {
173     return processStates[1].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
174   }
175
176   HeapArea const& equals_to1_(std::size_t i, std::size_t j) const
177   {
178     return processStates[0].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
179   }
180   HeapArea const& equals_to2_(std::size_t i, std::size_t j) const
181   {
182     return processStates[1].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
183   }
184   Type* const& types1_(std::size_t i, std::size_t j) const
185   {
186     return processStates[0].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
187   }
188   Type* const& types2_(std::size_t i, std::size_t j) const
189   {
190     return processStates[1].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
191   }
192
193   /** Check whether two blocks are known to be matching
194    *
195    *  @param b1     Block of state 1
196    *  @param b2     Block of state 2
197    *  @return       if the blocks are known to be matching
198    */
199   bool blocksEqual(int b1, int b2) const
200   {
201     return this->equals_to1_(b1, 0).block_ == b2 && this->equals_to2_(b2, 0).block_ == b1;
202   }
203
204   /** Check whether two fragments are known to be matching
205    *
206    *  @param b1     Block of state 1
207    *  @param f1     Fragment of state 1
208    *  @param b2     Block of state 2
209    *  @param f2     Fragment of state 2
210    *  @return       if the fragments are known to be matching
211    */
212   int fragmentsEqual(int b1, int f1, int b2, int f2) const
213   {
214     return this->equals_to1_(b1, f1).block_ == b2 && this->equals_to1_(b1, f1).fragment_ == f2 &&
215            this->equals_to2_(b2, f2).block_ == b1 && this->equals_to2_(b2, f2).fragment_ == f1;
216   }
217
218   void match_equals(HeapLocationPairs* list);
219 };
220
221 }
222 }
223
224 /************************************************************************************/
225
226 static ssize_t heap_comparison_ignore_size(
227   std::vector<simgrid::mc::IgnoredHeapRegion>* ignore_list,
228   const void *address)
229 {
230   int start = 0;
231   int end = ignore_list->size() - 1;
232
233   while (start <= end) {
234     unsigned int cursor = (start + end) / 2;
235     simgrid::mc::IgnoredHeapRegion const& region = (*ignore_list)[cursor];
236     if (region.address == address)
237       return region.size;
238     if (region.address < address)
239       start = cursor + 1;
240     if (region.address > address)
241       end = cursor - 1;
242   }
243
244   return -1;
245 }
246
247 static bool is_stack(const void *address)
248 {
249   for (auto const& stack : mc_model_checker->process().stack_areas())
250     if (address == stack.address)
251       return true;
252   return false;
253 }
254
255 // TODO, this should depend on the snapshot?
256 static bool is_block_stack(int block)
257 {
258   for (auto const& stack : mc_model_checker->process().stack_areas())
259     if (block == stack.block)
260       return true;
261   return false;
262 }
263
264 namespace simgrid {
265 namespace mc {
266
267 void StateComparator::match_equals(HeapLocationPairs* list)
268 {
269   for (auto const& pair : *list) {
270     if (pair[0].fragment_ != -1) {
271       this->equals_to1_(pair[0].block_, pair[0].fragment_) = simgrid::mc::HeapArea(pair[1].block_, pair[1].fragment_);
272       this->equals_to2_(pair[1].block_, pair[1].fragment_) = simgrid::mc::HeapArea(pair[0].block_, pair[0].fragment_);
273     } else {
274       this->equals_to1_(pair[0].block_, 0) = simgrid::mc::HeapArea(pair[1].block_, pair[1].fragment_);
275       this->equals_to2_(pair[1].block_, 0) = simgrid::mc::HeapArea(pair[0].block_, pair[0].fragment_);
276     }
277   }
278 }
279
280 void ProcessComparisonState::initHeapInformation(xbt_mheap_t heap,
281                         std::vector<simgrid::mc::IgnoredHeapRegion>* i)
282 {
283   auto heaplimit  = heap->heaplimit;
284   this->heapsize  = heap->heapsize;
285   this->to_ignore = i;
286   this->equals_to.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, HeapArea());
287   this->types.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, nullptr);
288 }
289
290 int StateComparator::initHeapInformation(xbt_mheap_t heap1, xbt_mheap_t heap2,
291                           std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
292                           std::vector<simgrid::mc::IgnoredHeapRegion>* i2)
293 {
294   if ((heap1->heaplimit != heap2->heaplimit) || (heap1->heapsize != heap2->heapsize))
295     return -1;
296   this->heaplimit     = heap1->heaplimit;
297   this->std_heap_copy = *mc_model_checker->process().get_heap();
298   this->processStates[0].initHeapInformation(heap1, i1);
299   this->processStates[1].initHeapInformation(heap2, i2);
300   return 0;
301 }
302
303 // TODO, have a robust way to find it in O(1)
304 static inline
305 mc_mem_region_t MC_get_heap_region(simgrid::mc::Snapshot* snapshot)
306 {
307   for (auto const& region : snapshot->snapshot_regions)
308     if (region->region_type() == simgrid::mc::RegionType::Heap)
309       return region.get();
310   xbt_die("No heap region");
311 }
312
313 static
314 int mmalloc_compare_heap(
315   simgrid::mc::StateComparator& state, simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2)
316 {
317   simgrid::mc::RemoteClient* process = &mc_model_checker->process();
318
319   /* Start comparison */
320   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       RemotePtr<malloc_info*>((std::uint64_t)heapinfo_address), simgrid::mc::ProcessIndexMissing);
349   const malloc_info* heapinfos2 = snapshot2->read<malloc_info*>(
350       RemotePtr<malloc_info*>((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   do {
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(state.processStates[0].to_ignore, real_area1);
705       if (ignore1 > 0 && heap_comparison_ignore_size(state.processStates[1].to_ignore, real_area2) == ignore1)
706         return 0;
707     }
708
709     simgrid::mc::Type* subtype;
710     simgrid::mc::Type* subsubtype;
711     int res;
712     int elm_size;
713     const void* addr_pointed1;
714     const void* addr_pointed2;
715
716     mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
717     mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
718
719     switch (type->type) {
720       case DW_TAG_unspecified_type:
721         return 1;
722
723       case DW_TAG_base_type:
724         if (not type->name.empty() && type->name == "char") { /* String, hence random (arbitrary ?) size */
725           if (real_area1 == real_area2)
726             return -1;
727           else
728             return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, area_size) != 0;
729         } else {
730           if (area_size != -1 && type->byte_size != area_size)
731             return -1;
732           else
733             return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
734         }
735
736       case DW_TAG_enumeration_type:
737         if (area_size != -1 && type->byte_size != area_size)
738           return -1;
739         return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
740
741       case DW_TAG_typedef:
742       case DW_TAG_const_type:
743       case DW_TAG_volatile_type:
744         // Poor man's TCO:
745         type = type->subtype;
746         continue; // restart
747
748       case DW_TAG_array_type:
749         subtype = type->subtype;
750         switch (subtype->type) {
751           case DW_TAG_unspecified_type:
752             return 1;
753
754           case DW_TAG_base_type:
755           case DW_TAG_enumeration_type:
756           case DW_TAG_pointer_type:
757           case DW_TAG_reference_type:
758           case DW_TAG_rvalue_reference_type:
759           case DW_TAG_structure_type:
760           case DW_TAG_class_type:
761           case DW_TAG_union_type:
762             if (subtype->full_type)
763               subtype = subtype->full_type;
764             elm_size  = subtype->byte_size;
765             break;
766           // TODO, just remove the type indirection?
767           case DW_TAG_const_type:
768           case DW_TAG_typedef:
769           case DW_TAG_volatile_type:
770             subsubtype = subtype->subtype;
771             if (subsubtype->full_type)
772               subsubtype = subsubtype->full_type;
773             elm_size     = subsubtype->byte_size;
774             break;
775           default:
776             return 0;
777         }
778         for (int i = 0; i < type->element_count; i++) {
779           // TODO, add support for variable stride (DW_AT_byte_stride)
780           res = compare_heap_area_with_type(state, process_index, (char*)real_area1 + (i * elm_size),
781                                             (char*)real_area2 + (i * elm_size), snapshot1, snapshot2, previous,
782                                             type->subtype, subtype->byte_size, check_ignore, pointer_level);
783           if (res == 1)
784             return res;
785         }
786         return 0;
787
788       case DW_TAG_reference_type:
789       case DW_TAG_rvalue_reference_type:
790       case DW_TAG_pointer_type:
791         if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
792           addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
793           addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
794           return (addr_pointed1 != addr_pointed2);
795         }
796         pointer_level++;
797         if (pointer_level <= 1) {
798           addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
799           addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
800           if (addr_pointed1 > state.std_heap_copy.heapbase && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1) &&
801               addr_pointed2 > state.std_heap_copy.heapbase && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
802             return compare_heap_area(state, process_index, addr_pointed1, addr_pointed2, snapshot1, snapshot2, previous,
803                                      type->subtype, pointer_level);
804           else
805             return (addr_pointed1 != addr_pointed2);
806         }
807         for (size_t i = 0; i < (area_size / sizeof(void*)); i++) {
808           addr_pointed1 = snapshot1->read(remote((void**)((char*)real_area1 + i * sizeof(void*))), process_index);
809           addr_pointed2 = snapshot2->read(remote((void**)((char*)real_area2 + i * sizeof(void*))), process_index);
810           if (addr_pointed1 > state.std_heap_copy.heapbase && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1) &&
811               addr_pointed2 > state.std_heap_copy.heapbase && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
812             res = compare_heap_area(state, process_index, addr_pointed1, addr_pointed2, snapshot1, snapshot2, previous,
813                                     type->subtype, pointer_level);
814           else
815             res = (addr_pointed1 != addr_pointed2);
816           if (res == 1)
817             return res;
818         }
819         return 0;
820
821       case DW_TAG_structure_type:
822       case DW_TAG_class_type:
823         if (type->full_type)
824           type = type->full_type;
825         if (area_size != -1 && type->byte_size != area_size) {
826           if (area_size <= type->byte_size || area_size % type->byte_size != 0)
827             return -1;
828           for (size_t i = 0; i < (size_t)(area_size / type->byte_size); i++) {
829             int res = compare_heap_area_with_type(state, process_index, (char*)real_area1 + i * type->byte_size,
830                                                   (char*)real_area2 + i * type->byte_size, snapshot1, snapshot2,
831                                                   previous, type, -1, check_ignore, 0);
832             if (res == 1)
833               return res;
834           }
835         } else {
836           for (simgrid::mc::Member& member : type->members) {
837             // TODO, optimize this? (for the offset case)
838             void* real_member1 = simgrid::dwarf::resolve_member(real_area1, type, &member,
839                                                                 (simgrid::mc::AddressSpace*)snapshot1, process_index);
840             void* real_member2 = simgrid::dwarf::resolve_member(real_area2, type, &member,
841                                                                 (simgrid::mc::AddressSpace*)snapshot2, process_index);
842             int res = compare_heap_area_with_type(state, process_index, real_member1, real_member2, snapshot1,
843                                                   snapshot2, previous, member.type, -1, check_ignore, 0);
844             if (res == 1)
845               return res;
846           }
847         }
848         return 0;
849
850       case DW_TAG_union_type:
851         return compare_heap_area_without_type(state, process_index, real_area1, real_area2, snapshot1, snapshot2,
852                                               previous, type->byte_size, check_ignore);
853
854       default:
855         return 0;
856     }
857
858     xbt_die("Unreachable");
859   } while (true);
860 }
861
862 /** Infer the type of a part of the block from the type of the block
863  *
864  * TODO, handle DW_TAG_array_type as well as arrays of the object ((*p)[5], p[5])
865  *
866  * TODO, handle subfields ((*p).bar.foo, (*p)[5].bar…)
867  *
868  * @param  type               DWARF type ID of the root address
869  * @param  area_size
870  * @return                    DWARF type ID for given offset
871  */
872 static simgrid::mc::Type* get_offset_type(void *real_base_address, simgrid::mc::Type* type,
873                                  int offset, int area_size,
874                                  simgrid::mc::Snapshot* snapshot, int process_index)
875 {
876
877   // Beginning of the block, the infered variable type if the type of the block:
878   if (offset == 0)
879     return type;
880
881   switch (type->type) {
882
883   case DW_TAG_structure_type:
884   case DW_TAG_class_type:
885     if (type->full_type)
886       type = type->full_type;
887     if (area_size != -1 && type->byte_size != area_size) {
888       if (area_size > type->byte_size && area_size % type->byte_size == 0)
889         return type;
890       else
891         return nullptr;
892     }
893
894     for (simgrid::mc::Member& member : type->members) {
895       if (member.has_offset_location()) {
896         // We have the offset, use it directly (shortcut):
897         if (member.offset() == offset)
898           return member.type;
899       } else {
900         void* real_member = simgrid::dwarf::resolve_member(real_base_address, type, &member, snapshot, process_index);
901         if ((char*)real_member - (char*)real_base_address == offset)
902           return member.type;
903       }
904     }
905     return nullptr;
906
907   default:
908     /* FIXME: other cases ? */
909     return nullptr;
910
911   }
912 }
913
914 /**
915  *
916  * @param area1          Process address for state 1
917  * @param area2          Process address for state 2
918  * @param snapshot1      Snapshot of state 1
919  * @param snapshot2      Snapshot of state 2
920  * @param previous       Pairs of blocks already compared on the current path (or nullptr)
921  * @param type_id        Type of variable
922  * @param pointer_level
923  * @return 0 (same), 1 (different), -1
924  */
925 static
926 int compare_heap_area(simgrid::mc::StateComparator& state, int process_index,
927                       const void *area1, const void *area2,
928                       simgrid::mc::Snapshot* snapshot1,
929                       simgrid::mc::Snapshot* snapshot2,
930                       HeapLocationPairs* previous,
931                       simgrid::mc::Type* type, int pointer_level)
932 {
933   simgrid::mc::RemoteClient* process = &mc_model_checker->process();
934
935   ssize_t block1;
936   ssize_t block2;
937   ssize_t size;
938   int check_ignore = 0;
939
940   int type_size = -1;
941   int offset1   = 0;
942   int offset2   = 0;
943   int new_size1 = -1;
944   int new_size2 = -1;
945
946   simgrid::mc::Type* new_type1 = nullptr;
947   simgrid::mc::Type* new_type2 = nullptr;
948
949   bool match_pairs = false;
950
951   // This is the address of std_heap->heapinfo in the application process:
952   void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
953
954   const malloc_info* heapinfos1 = snapshot1->read(remote((const malloc_info**)heapinfo_address), process_index);
955   const malloc_info* heapinfos2 = snapshot2->read(remote((const malloc_info**)heapinfo_address), process_index);
956
957   malloc_info heapinfo_temp1;
958   malloc_info heapinfo_temp2;
959
960   simgrid::mc::HeapLocationPairs current;
961   if (previous == nullptr) {
962     previous = &current;
963     match_pairs = true;
964   }
965
966   // Get block number:
967   block1 = ((char*)area1 - (char*)state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
968   block2 = ((char*)area2 - (char*)state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
969
970   // If either block is a stack block:
971   if (is_block_stack((int) block1) && is_block_stack((int) block2)) {
972     previous->insert(simgrid::mc::makeHeapLocationPair(block1, -1, block2, -1));
973     if (match_pairs)
974       state.match_equals(previous);
975     return 0;
976   }
977
978   // If either block is not in the expected area of memory:
979   if (((char*)area1 < (char*)state.std_heap_copy.heapbase) || (block1 > (ssize_t)state.processStates[0].heapsize) ||
980       (block1 < 1) || ((char*)area2 < (char*)state.std_heap_copy.heapbase) ||
981       (block2 > (ssize_t)state.processStates[1].heapsize) || (block2 < 1)) {
982     return 1;
983   }
984
985   // Process address of the block:
986   void* real_addr_block1 = (ADDR2UINT(block1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
987   void* real_addr_block2 = (ADDR2UINT(block2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
988
989   if (type) {
990     if (type->full_type)
991       type = type->full_type;
992
993     // This assume that for "boring" types (volatile ...) byte_size is absent:
994     while (type->byte_size == 0 && type->subtype != nullptr)
995       type = type->subtype;
996
997     // Find type_size:
998     if (type->type == DW_TAG_pointer_type ||
999         (type->type == DW_TAG_base_type && not type->name.empty() && type->name == "char"))
1000       type_size = -1;
1001     else
1002       type_size = type->byte_size;
1003
1004   }
1005
1006   mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
1007   mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
1008
1009   const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
1010     heap_region1, &heapinfo_temp1, &heapinfos1[block1], sizeof(malloc_info));
1011   const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
1012     heap_region2, &heapinfo_temp2, &heapinfos2[block2], sizeof(malloc_info));
1013
1014   if ((heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type==MMALLOC_TYPE_HEAPINFO)
1015     && (heapinfo2->type == MMALLOC_TYPE_FREE || heapinfo2->type ==MMALLOC_TYPE_HEAPINFO)) {
1016     /* Free block */
1017     if (match_pairs)
1018       state.match_equals(previous);
1019     return 0;
1020   }
1021
1022   if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED && heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
1023     /* Complete block */
1024
1025     // TODO, lookup variable type from block type as done for fragmented blocks
1026
1027     if (state.equals_to1_(block1, 0).valid_ && state.equals_to2_(block2, 0).valid_ &&
1028         state.blocksEqual(block1, block2)) {
1029       if (match_pairs)
1030         state.match_equals(previous);
1031       return 0;
1032     }
1033
1034     if (type_size != -1 && type_size != (ssize_t)heapinfo1->busy_block.busy_size &&
1035         type_size != (ssize_t)heapinfo2->busy_block.busy_size &&
1036         (type->name.empty() || type->name == "struct s_smx_context")) {
1037       if (match_pairs)
1038         state.match_equals(previous);
1039       return -1;
1040     }
1041
1042     if (heapinfo1->busy_block.size != heapinfo2->busy_block.size)
1043       return 1;
1044     if (heapinfo1->busy_block.busy_size != heapinfo2->busy_block.busy_size)
1045       return 1;
1046
1047     if (not previous->insert(simgrid::mc::makeHeapLocationPair(block1, -1, block2, -1)).second) {
1048       if (match_pairs)
1049         state.match_equals(previous);
1050       return 0;
1051     }
1052
1053     size = heapinfo1->busy_block.busy_size;
1054
1055     // Remember (basic) type inference.
1056     // The current data structure only allows us to do this for the whole block.
1057     if (type != nullptr && area1 == real_addr_block1)
1058       state.types1_(block1, 0) = type;
1059     if (type != nullptr && area2 == real_addr_block2)
1060       state.types2_(block2, 0) = type;
1061
1062     if (size <= 0) {
1063       if (match_pairs)
1064         state.match_equals(previous);
1065       return 0;
1066     }
1067
1068     if (heapinfo1->busy_block.ignore > 0
1069         && heapinfo2->busy_block.ignore == heapinfo1->busy_block.ignore)
1070       check_ignore = heapinfo1->busy_block.ignore;
1071
1072   } else if ((heapinfo1->type > 0) && (heapinfo2->type > 0)) {      /* Fragmented block */
1073
1074     // Fragment number:
1075     ssize_t frag1 = ((uintptr_t)(ADDR2UINT(area1) % (BLOCKSIZE))) >> heapinfo1->type;
1076     ssize_t frag2 = ((uintptr_t)(ADDR2UINT(area2) % (BLOCKSIZE))) >> heapinfo2->type;
1077
1078     // Process address of the fragment_:
1079     void* real_addr_frag1 = (void*)((char*)real_addr_block1 + (frag1 << heapinfo1->type));
1080     void* real_addr_frag2 = (void*)((char*)real_addr_block2 + (frag2 << heapinfo2->type));
1081
1082     // Check the size of the fragments against the size of the type:
1083     if (type_size != -1) {
1084       if (heapinfo1->busy_frag.frag_size[frag1] == -1 || heapinfo2->busy_frag.frag_size[frag2] == -1) {
1085         if (match_pairs)
1086           state.match_equals(previous);
1087         return -1;
1088       }
1089       // ?
1090       if (type_size != heapinfo1->busy_frag.frag_size[frag1]
1091           || type_size != heapinfo2->busy_frag.frag_size[frag2]) {
1092         if (match_pairs)
1093           state.match_equals(previous);
1094         return -1;
1095       }
1096     }
1097
1098     // Check if the blocks are already matched together:
1099     if (state.equals_to1_(block1, frag1).valid_ && state.equals_to2_(block2, frag2).valid_ && offset1 == offset2 &&
1100         state.fragmentsEqual(block1, frag1, block2, frag2)) {
1101       if (match_pairs)
1102         state.match_equals(previous);
1103       return 0;
1104     }
1105     // Compare the size of both fragments:
1106     if (heapinfo1->busy_frag.frag_size[frag1] != heapinfo2->busy_frag.frag_size[frag2]) {
1107       if (type_size == -1) {
1108         if (match_pairs)
1109           state.match_equals(previous);
1110         return -1;
1111       } else
1112         return 1;
1113     }
1114
1115     // Size of the fragment_:
1116     size = heapinfo1->busy_frag.frag_size[frag1];
1117
1118     // Remember (basic) type inference.
1119     // The current data structure only allows us to do this for the whole fragment_.
1120     if (type != nullptr && area1 == real_addr_frag1)
1121       state.types1_(block1, frag1) = type;
1122     if (type != nullptr && area2 == real_addr_frag2)
1123       state.types2_(block2, frag2) = type;
1124
1125     // The type of the variable is already known:
1126     if (type) {
1127       new_type1 = new_type2 = type;
1128     }
1129     // Type inference from the block type.
1130     else if (state.types1_(block1, frag1) != nullptr || state.types2_(block2, frag2) != nullptr) {
1131
1132       offset1 = (char*)area1 - (char*)real_addr_frag1;
1133       offset2 = (char*)area2 - (char*)real_addr_frag2;
1134
1135       if (state.types1_(block1, frag1) != nullptr && state.types2_(block2, frag2) != nullptr) {
1136         new_type1 =
1137             get_offset_type(real_addr_frag1, state.types1_(block1, frag1), offset1, size, snapshot1, process_index);
1138         new_type2 =
1139             get_offset_type(real_addr_frag2, state.types2_(block2, frag2), offset1, size, snapshot2, process_index);
1140       } else if (state.types1_(block1, frag1) != nullptr) {
1141         new_type1 =
1142             get_offset_type(real_addr_frag1, state.types1_(block1, frag1), offset1, size, snapshot1, process_index);
1143         new_type2 =
1144             get_offset_type(real_addr_frag2, state.types1_(block1, frag1), offset2, size, snapshot2, process_index);
1145       } else if (state.types2_(block2, frag2) != nullptr) {
1146         new_type1 =
1147             get_offset_type(real_addr_frag1, state.types2_(block2, frag2), offset1, size, snapshot1, process_index);
1148         new_type2 =
1149             get_offset_type(real_addr_frag2, state.types2_(block2, frag2), offset2, size, snapshot2, process_index);
1150       } else {
1151         if (match_pairs)
1152           state.match_equals(previous);
1153         return -1;
1154       }
1155
1156       if (new_type1 != nullptr && new_type2 != nullptr && new_type1 != new_type2) {
1157
1158         type = new_type1;
1159         while (type->byte_size == 0 && type->subtype != nullptr)
1160           type = type->subtype;
1161         new_size1 = type->byte_size;
1162
1163         type = new_type2;
1164         while (type->byte_size == 0 && type->subtype != nullptr)
1165           type = type->subtype;
1166         new_size2 = type->byte_size;
1167
1168       } else {
1169         if (match_pairs)
1170           state.match_equals(previous);
1171         return -1;
1172       }
1173     }
1174
1175     if (new_size1 > 0 && new_size1 == new_size2) {
1176       type = new_type1;
1177       size = new_size1;
1178     }
1179
1180     if (offset1 == 0 && offset2 == 0 &&
1181         not previous->insert(simgrid::mc::makeHeapLocationPair(block1, frag1, block2, frag2)).second) {
1182       if (match_pairs)
1183         state.match_equals(previous);
1184       return 0;
1185     }
1186
1187     if (size <= 0) {
1188       if (match_pairs)
1189         state.match_equals(previous);
1190       return 0;
1191     }
1192
1193     if ((heapinfo1->busy_frag.ignore[frag1] > 0) &&
1194         (heapinfo2->busy_frag.ignore[frag2] == heapinfo1->busy_frag.ignore[frag1]))
1195       check_ignore = heapinfo1->busy_frag.ignore[frag1];
1196
1197   } else
1198     return 1;
1199
1200
1201   /* Start comparison */
1202   int res_compare;
1203   if (type)
1204     res_compare = compare_heap_area_with_type(state, process_index, area1, area2, snapshot1, snapshot2, previous, type,
1205                                               size, check_ignore, pointer_level);
1206   else
1207     res_compare = compare_heap_area_without_type(state, process_index, area1, area2, snapshot1, snapshot2, previous,
1208                                                  size, check_ignore);
1209
1210   if (res_compare == 1)
1211     return res_compare;
1212
1213   if (match_pairs)
1214     state.match_equals(previous);
1215   return 0;
1216 }
1217
1218 }
1219 }
1220
1221 /************************** Snapshot comparison *******************************/
1222 /******************************************************************************/
1223
1224 static int compare_areas_with_type(simgrid::mc::StateComparator& state,
1225                                    int process_index,
1226                                    void* real_area1, simgrid::mc::Snapshot* snapshot1, mc_mem_region_t region1,
1227                                    void* real_area2, simgrid::mc::Snapshot* snapshot2, mc_mem_region_t region2,
1228                                    simgrid::mc::Type* type, int pointer_level)
1229 {
1230   simgrid::mc::RemoteClient* process = &mc_model_checker->process();
1231
1232   simgrid::mc::Type* subtype;
1233   simgrid::mc::Type* subsubtype;
1234   int elm_size;
1235   int i;
1236   int res;
1237
1238   do {
1239     switch (type->type) {
1240       case DW_TAG_unspecified_type:
1241         return 1;
1242
1243       case DW_TAG_base_type:
1244       case DW_TAG_enumeration_type:
1245       case DW_TAG_union_type:
1246         return MC_snapshot_region_memcmp(real_area1, region1, real_area2, region2, type->byte_size) != 0;
1247       case DW_TAG_typedef:
1248       case DW_TAG_volatile_type:
1249       case DW_TAG_const_type:
1250         // Poor man's TCO:
1251         type = type->subtype;
1252         continue; // restart
1253       case DW_TAG_array_type:
1254         subtype = type->subtype;
1255         switch (subtype->type) {
1256           case DW_TAG_unspecified_type:
1257             return 1;
1258
1259           case DW_TAG_base_type:
1260           case DW_TAG_enumeration_type:
1261           case DW_TAG_pointer_type:
1262           case DW_TAG_reference_type:
1263           case DW_TAG_rvalue_reference_type:
1264           case DW_TAG_structure_type:
1265           case DW_TAG_class_type:
1266           case DW_TAG_union_type:
1267             if (subtype->full_type)
1268               subtype = subtype->full_type;
1269             elm_size  = subtype->byte_size;
1270             break;
1271           case DW_TAG_const_type:
1272           case DW_TAG_typedef:
1273           case DW_TAG_volatile_type:
1274             subsubtype = subtype->subtype;
1275             if (subsubtype->full_type)
1276               subsubtype = subsubtype->full_type;
1277             elm_size     = subsubtype->byte_size;
1278             break;
1279           default:
1280             return 0;
1281         }
1282         for (i = 0; i < type->element_count; i++) {
1283           size_t off = i * elm_size;
1284           res        = compare_areas_with_type(state, process_index, (char*)real_area1 + off, snapshot1, region1,
1285                                         (char*)real_area2 + off, snapshot2, region2, type->subtype, pointer_level);
1286           if (res == 1)
1287             return res;
1288         }
1289         break;
1290       case DW_TAG_pointer_type:
1291       case DW_TAG_reference_type:
1292       case DW_TAG_rvalue_reference_type: {
1293         void* addr_pointed1 = MC_region_read_pointer(region1, real_area1);
1294         void* addr_pointed2 = MC_region_read_pointer(region2, real_area2);
1295
1296         if (type->subtype && type->subtype->type == DW_TAG_subroutine_type)
1297           return (addr_pointed1 != addr_pointed2);
1298         if (addr_pointed1 == nullptr && addr_pointed2 == nullptr)
1299           return 0;
1300         if (addr_pointed1 == nullptr || addr_pointed2 == nullptr)
1301           return 1;
1302         if (not state.compared_pointers.insert(std::make_pair(addr_pointed1, addr_pointed2)).second)
1303           return 0;
1304
1305         pointer_level++;
1306
1307         // Some cases are not handled here:
1308         // * the pointers lead to different areas (one to the heap, the other to the RW segment ...)
1309         // * a pointer leads to the read-only segment of the current object
1310         // * a pointer lead to a different ELF object
1311
1312         if (addr_pointed1 > process->heap_address && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)) {
1313           if (not(addr_pointed2 > process->heap_address && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)))
1314             return 1;
1315           // The pointers are both in the heap:
1316           return simgrid::mc::compare_heap_area(state, process_index, addr_pointed1, addr_pointed2, snapshot1,
1317                                                 snapshot2, nullptr, type->subtype, pointer_level);
1318
1319         } else if (region1->contain(simgrid::mc::remote(addr_pointed1))) {
1320           // The pointers are both in the current object R/W segment:
1321           if (not region2->contain(simgrid::mc::remote(addr_pointed2)))
1322             return 1;
1323           if (not type->type_id)
1324             return (addr_pointed1 != addr_pointed2);
1325           else
1326             return compare_areas_with_type(state, process_index, addr_pointed1, snapshot1, region1, addr_pointed2,
1327                                            snapshot2, region2, type->subtype, pointer_level);
1328         } else {
1329
1330           // TODO, We do not handle very well the case where
1331           // it belongs to a different (non-heap) region from the current one.
1332
1333           return (addr_pointed1 != addr_pointed2);
1334         }
1335       }
1336       case DW_TAG_structure_type:
1337       case DW_TAG_class_type:
1338         for (simgrid::mc::Member& member : type->members) {
1339           void* member1 = simgrid::dwarf::resolve_member(real_area1, type, &member, snapshot1, process_index);
1340           void* member2 = simgrid::dwarf::resolve_member(real_area2, type, &member, snapshot2, process_index);
1341           mc_mem_region_t subregion1 = mc_get_region_hinted(member1, snapshot1, process_index, region1);
1342           mc_mem_region_t subregion2 = mc_get_region_hinted(member2, snapshot2, process_index, region2);
1343           res = compare_areas_with_type(state, process_index, member1, snapshot1, subregion1, member2, snapshot2,
1344                                         subregion2, member.type, pointer_level);
1345           if (res == 1)
1346             return res;
1347         }
1348         break;
1349       case DW_TAG_subroutine_type:
1350         return -1;
1351       default:
1352         XBT_VERB("Unknown case: %d", type->type);
1353         break;
1354     }
1355
1356     return 0;
1357   } while (true);
1358 }
1359
1360 static int compare_global_variables(
1361   simgrid::mc::StateComparator& state,
1362   simgrid::mc::ObjectInformation* object_info,
1363   int process_index,
1364   mc_mem_region_t r1, mc_mem_region_t r2,
1365   simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2)
1366 {
1367   xbt_assert(r1 && r2, "Missing region.");
1368
1369 #if HAVE_SMPI
1370   if (r1->storage_type() == simgrid::mc::StorageType::Privatized) {
1371     xbt_assert(process_index >= 0);
1372     if (r2->storage_type() != simgrid::mc::StorageType::Privatized)
1373       return 1;
1374
1375     size_t process_count = MC_smpi_process_count();
1376     xbt_assert(process_count == r1->privatized_data().size()
1377       && process_count == r2->privatized_data().size());
1378
1379     // Compare the global variables separately for each simulates process:
1380     for (size_t process_index = 0; process_index < process_count; process_index++) {
1381       if (compare_global_variables(state,
1382           object_info, process_index,
1383           &r1->privatized_data()[process_index],
1384           &r2->privatized_data()[process_index],
1385           snapshot1, snapshot2))
1386         return 1;
1387     }
1388     return 0;
1389   }
1390 #else
1391   xbt_assert(r1->storage_type() != simgrid::mc::StorageType::Privatized);
1392 #endif
1393   xbt_assert(r2->storage_type() != simgrid::mc::StorageType::Privatized);
1394
1395   std::vector<simgrid::mc::Variable>& variables = object_info->global_variables;
1396
1397   for (simgrid::mc::Variable const& current_var : variables) {
1398
1399     // If the variable is not in this object, skip it:
1400     // We do not expect to find a pointer to something which is not reachable
1401     // by the global variables.
1402     if ((char *) current_var.address < (char *) object_info->start_rw
1403         || (char *) current_var.address > (char *) object_info->end_rw)
1404       continue;
1405
1406     simgrid::mc::Type* bvariable_type = current_var.type;
1407     int res = compare_areas_with_type(state, process_index,
1408                                 (char *) current_var.address, snapshot1, r1,
1409                                 (char *) current_var.address, snapshot2, r2,
1410                                 bvariable_type, 0);
1411     if (res == 1) {
1412       XBT_VERB("Global variable %s (%p) is different between snapshots",
1413                current_var.name.c_str(),
1414                (char *) current_var.address);
1415       return 1;
1416     }
1417   }
1418
1419   return 0;
1420 }
1421
1422 static int compare_local_variables(simgrid::mc::StateComparator& state,
1423                                    int process_index,
1424                                    simgrid::mc::Snapshot* snapshot1,
1425                                    simgrid::mc::Snapshot* snapshot2,
1426                                    mc_snapshot_stack_t stack1,
1427                                    mc_snapshot_stack_t stack2)
1428 {
1429   if (stack1->local_variables.size() != stack2->local_variables.size()) {
1430     XBT_VERB("Different number of local variables");
1431     return 1;
1432   }
1433
1434     unsigned int cursor = 0;
1435     local_variable_t current_var1;
1436     local_variable_t current_var2;
1437     while (cursor < stack1->local_variables.size()) {
1438       current_var1 = &stack1->local_variables[cursor];
1439       current_var2 = &stack1->local_variables[cursor];
1440       if (current_var1->name != current_var2->name
1441           || current_var1->subprogram != current_var2->subprogram
1442           || current_var1->ip != current_var2->ip) {
1443         // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1444         XBT_VERB
1445             ("Different name of variable (%s - %s) "
1446              "or frame (%s - %s) or ip (%lu - %lu)",
1447              current_var1->name.c_str(),
1448              current_var2->name.c_str(),
1449              current_var1->subprogram->name.c_str(),
1450              current_var2->subprogram->name.c_str(),
1451              current_var1->ip, current_var2->ip);
1452         return 1;
1453       }
1454       // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1455
1456         simgrid::mc::Type* subtype = current_var1->type;
1457         int res                    = compare_areas_with_type(
1458             state, process_index, current_var1->address, snapshot1,
1459             mc_get_snapshot_region(current_var1->address, snapshot1, process_index), current_var2->address, snapshot2,
1460             mc_get_snapshot_region(current_var2->address, snapshot2, process_index), subtype, 0);
1461
1462         if (res == 1) {
1463           // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1464           XBT_VERB("Local variable %s (%p - %p) in frame %s "
1465                    "is different between snapshots",
1466                    current_var1->name.c_str(), current_var1->address, current_var2->address,
1467                    current_var1->subprogram->name.c_str());
1468           return res;
1469       }
1470       cursor++;
1471     }
1472     return 0;
1473 }
1474
1475 namespace simgrid {
1476 namespace mc {
1477
1478 static std::unique_ptr<simgrid::mc::StateComparator> state_comparator;
1479
1480 int snapshot_compare(int num1, simgrid::mc::Snapshot* s1, int num2, simgrid::mc::Snapshot* s2)
1481 {
1482   // TODO, make this a field of ModelChecker or something similar
1483
1484   if (state_comparator == nullptr)
1485     state_comparator = std::unique_ptr<StateComparator>(new StateComparator());
1486   else
1487     state_comparator->clear();
1488
1489   simgrid::mc::RemoteClient* process = &mc_model_checker->process();
1490
1491   int errors = 0;
1492
1493   int hash_result = 0;
1494   if (_sg_mc_hash) {
1495     hash_result = (s1->hash != s2->hash);
1496     if (hash_result) {
1497       XBT_VERB("(%d - %d) Different hash: 0x%" PRIx64 "--0x%" PRIx64, num1, num2, s1->hash, s2->hash);
1498 #ifndef MC_DEBUG
1499       return 1;
1500 #endif
1501     } else
1502       XBT_VERB("(%d - %d) Same hash: 0x%" PRIx64, num1, num2, s1->hash);
1503   }
1504
1505   /* Compare enabled processes */
1506   if (s1->enabled_processes != s2->enabled_processes) {
1507     XBT_VERB("(%d - %d) Different amount of enabled processes", num1, num2);
1508     return 1;
1509   }
1510
1511   /* Compare size of stacks */
1512   int is_diff = 0;
1513   for (unsigned long i = 0; i < s1->stacks.size(); i++) {
1514     size_t size_used1 = s1->stack_sizes[i];
1515     size_t size_used2 = s2->stack_sizes[i];
1516     if (size_used1 != size_used2) {
1517 #ifdef MC_DEBUG
1518       XBT_DEBUG("(%d - %d) Different size used in stacks: %zu - %zu", num1, num2, size_used1, size_used2);
1519       errors++;
1520       is_diff = 1;
1521 #else
1522 #ifdef MC_VERBOSE
1523       XBT_VERB("(%d - %d) Different size used in stacks: %zu - %zu", num1, num2, size_used1, size_used2);
1524 #endif
1525       return 1;
1526 #endif
1527     }
1528   }
1529   if (is_diff) // do not proceed if there is any stacks that don't match
1530     return 1;
1531
1532   /* Init heap information used in heap comparison algorithm */
1533   xbt_mheap_t heap1 = (xbt_mheap_t)s1->read_bytes(
1534     alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1535     remote(process->heap_address),
1536     simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1537   xbt_mheap_t heap2 = (xbt_mheap_t)s2->read_bytes(
1538     alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1539     remote(process->heap_address),
1540     simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1541   int res_init = state_comparator->initHeapInformation(heap1, heap2, &s1->to_ignore, &s2->to_ignore);
1542
1543   if (res_init == -1) {
1544 #ifdef MC_DEBUG
1545     XBT_DEBUG("(%d - %d) Different heap information", num1, num2);
1546     errors++;
1547 #else
1548 #ifdef MC_VERBOSE
1549     XBT_VERB("(%d - %d) Different heap information", num1, num2);
1550 #endif
1551
1552     return 1;
1553 #endif
1554   }
1555
1556   /* Stacks comparison */
1557   int diff_local = 0;
1558   for (unsigned int cursor = 0; cursor < s1->stacks.size(); cursor++) {
1559     mc_snapshot_stack_t stack1 = &s1->stacks[cursor];
1560     mc_snapshot_stack_t stack2 = &s2->stacks[cursor];
1561
1562     if (stack1->process_index != stack2->process_index) {
1563       diff_local = 1;
1564       XBT_DEBUG("(%d - %d) Stacks with different process index (%i vs %i)", num1, num2,
1565         stack1->process_index, stack2->process_index);
1566     }
1567     else diff_local = compare_local_variables(*state_comparator,
1568       stack1->process_index, s1, s2, stack1, stack2);
1569     if (diff_local > 0) {
1570 #ifdef MC_DEBUG
1571       XBT_DEBUG("(%d - %d) Different local variables between stacks %d", num1,
1572                 num2, cursor + 1);
1573       errors++;
1574 #else
1575
1576 #ifdef MC_VERBOSE
1577       XBT_VERB("(%d - %d) Different local variables between stacks %u", num1, num2, cursor + 1);
1578 #endif
1579
1580       return 1;
1581 #endif
1582     }
1583   }
1584
1585   size_t regions_count = s1->snapshot_regions.size();
1586   // TODO, raise a difference instead?
1587   xbt_assert(regions_count == s2->snapshot_regions.size());
1588
1589   for (size_t k = 0; k != regions_count; ++k) {
1590     mc_mem_region_t region1 = s1->snapshot_regions[k].get();
1591     mc_mem_region_t region2 = s2->snapshot_regions[k].get();
1592
1593     // Preconditions:
1594     if (region1->region_type() != simgrid::mc::RegionType::Data)
1595       continue;
1596
1597     xbt_assert(region1->region_type() == region2->region_type());
1598     xbt_assert(region1->object_info() == region2->object_info());
1599     xbt_assert(region1->object_info());
1600
1601     std::string const& name = region1->object_info()->file_name;
1602
1603     /* Compare global variables */
1604     if (compare_global_variables(*state_comparator, region1->object_info(), simgrid::mc::ProcessIndexDisabled, region1,
1605                                  region2, s1, s2)) {
1606
1607 #ifdef MC_DEBUG
1608       XBT_DEBUG("(%d - %d) Different global variables in %s",
1609         num1, num2, name.c_str());
1610       errors++;
1611 #else
1612 #ifdef MC_VERBOSE
1613       XBT_VERB("(%d - %d) Different global variables in %s",
1614         num1, num2, name.c_str());
1615 #endif
1616
1617       return 1;
1618 #endif
1619     }
1620   }
1621
1622   /* Compare heap */
1623   if (simgrid::mc::mmalloc_compare_heap(*state_comparator, s1, s2) > 0) {
1624
1625 #ifdef MC_DEBUG
1626     XBT_DEBUG("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
1627     errors++;
1628 #else
1629
1630 #ifdef MC_VERBOSE
1631     XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
1632 #endif
1633     return 1;
1634 #endif
1635   }
1636
1637 #ifdef MC_VERBOSE
1638   if (errors || hash_result)
1639     XBT_VERB("(%d - %d) Difference found", num1, num2);
1640   else
1641     XBT_VERB("(%d - %d) No difference found", num1, num2);
1642 #endif
1643
1644 #if defined(MC_DEBUG) && defined(MC_VERBOSE)
1645   if (_sg_mc_hash) {
1646     // * false positive SHOULD be avoided.
1647     // * There MUST not be any false negative.
1648
1649     XBT_VERB("(%d - %d) State equality hash test is %s %s", num1, num2,
1650              (hash_result != 0) == (errors != 0) ? "true" : "false", not hash_result ? "positive" : "negative");
1651   }
1652 #endif
1653
1654   return errors > 0 || hash_result;
1655 }
1656
1657 }
1658 }