Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d5fd2eb32e0473f2d14511878e7b1053d6ac5e34
[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 "src/mc/mc_config.hpp"
9 #include "src/mc/mc_private.hpp"
10 #include "src/mc/mc_smx.hpp"
11 #include "src/mc/sosp/Snapshot.hpp"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_compare, xbt, "Logging specific to mc_compare in mc");
14
15 using simgrid::mc::remote;
16
17 namespace simgrid {
18 namespace mc {
19
20 /*********************************** Heap comparison ***********************************/
21 /***************************************************************************************/
22
23 class HeapLocation {
24 public:
25   int block_    = 0;
26   int fragment_ = 0;
27
28   HeapLocation() = default;
29   HeapLocation(int block, int fragment = 0) : block_(block), fragment_(fragment) {}
30
31   bool operator==(HeapLocation const& that) const
32   {
33     return block_ == that.block_ && fragment_ == that.fragment_;
34   }
35   bool operator<(HeapLocation const& that) const
36   {
37     return std::make_pair(block_, fragment_) < std::make_pair(that.block_, that.fragment_);
38   }
39 };
40
41 typedef std::array<HeapLocation, 2> HeapLocationPair;
42 typedef std::set<HeapLocationPair> HeapLocationPairs;
43
44 class HeapArea : public HeapLocation {
45 public:
46   bool valid_ = false;
47   HeapArea() = default;
48   explicit HeapArea(int block) : valid_(true) { block_ = block; }
49   HeapArea(int block, int fragment) : valid_(true)
50   {
51     block_    = block;
52     fragment_ = fragment;
53   }
54 };
55
56 class ProcessComparisonState {
57 public:
58   std::vector<simgrid::mc::IgnoredHeapRegion>* to_ignore = nullptr;
59   std::vector<HeapArea> equals_to;
60   std::vector<simgrid::mc::Type*> types;
61   std::size_t heapsize = 0;
62
63   void initHeapInformation(xbt_mheap_t heap, std::vector<simgrid::mc::IgnoredHeapRegion>* i);
64 };
65
66 class StateComparator {
67 public:
68   s_xbt_mheap_t std_heap_copy;
69   std::size_t heaplimit;
70   std::array<ProcessComparisonState, 2> processStates;
71
72   std::unordered_set<std::pair<const void*, const void*>, simgrid::xbt::hash<std::pair<const void*, const void*>>>
73       compared_pointers;
74
75   void clear()
76   {
77     compared_pointers.clear();
78   }
79
80   int initHeapInformation(
81     xbt_mheap_t heap1, xbt_mheap_t heap2,
82     std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
83     std::vector<simgrid::mc::IgnoredHeapRegion>* i2);
84
85   template <int rank> HeapArea& equals_to_(std::size_t i, std::size_t j)
86   {
87     return processStates[rank - 1].equals_to[MAX_FRAGMENT_PER_BLOCK * i + j];
88   }
89   template <int rank> Type*& types_(std::size_t i, std::size_t j)
90   {
91     return processStates[rank - 1].types[MAX_FRAGMENT_PER_BLOCK * i + j];
92   }
93
94   template <int rank> HeapArea const& equals_to_(std::size_t i, std::size_t j) const
95   {
96     return processStates[rank - 1].equals_to[MAX_FRAGMENT_PER_BLOCK * i + j];
97   }
98   template <int rank> Type* const& types_(std::size_t i, std::size_t j) const
99   {
100     return processStates[rank - 1].types[MAX_FRAGMENT_PER_BLOCK * i + j];
101   }
102
103   /** Check whether two blocks are known to be matching
104    *
105    *  @param b1     Block of state 1
106    *  @param b2     Block of state 2
107    *  @return       if the blocks are known to be matching
108    */
109   bool blocksEqual(int b1, int b2) const
110   {
111     return this->equals_to_<1>(b1, 0).block_ == b2 && this->equals_to_<2>(b2, 0).block_ == b1;
112   }
113
114   /** Check whether two fragments are known to be matching
115    *
116    *  @param b1     Block of state 1
117    *  @param f1     Fragment of state 1
118    *  @param b2     Block of state 2
119    *  @param f2     Fragment of state 2
120    *  @return       if the fragments are known to be matching
121    */
122   int fragmentsEqual(int b1, int f1, int b2, int f2) const
123   {
124     return this->equals_to_<1>(b1, f1).block_ == b2 && this->equals_to_<1>(b1, f1).fragment_ == f2 &&
125            this->equals_to_<2>(b2, f2).block_ == b1 && this->equals_to_<2>(b2, f2).fragment_ == f1;
126   }
127
128   void match_equals(HeapLocationPairs* list);
129 };
130
131 }
132 }
133
134 /************************************************************************************/
135
136 static ssize_t heap_comparison_ignore_size(
137   std::vector<simgrid::mc::IgnoredHeapRegion>* ignore_list,
138   const void *address)
139 {
140   int start = 0;
141   int end = ignore_list->size() - 1;
142
143   while (start <= end) {
144     unsigned int cursor = (start + end) / 2;
145     simgrid::mc::IgnoredHeapRegion const& region = (*ignore_list)[cursor];
146     if (region.address == address)
147       return region.size;
148     if (region.address < address)
149       start = cursor + 1;
150     if (region.address > address)
151       end = cursor - 1;
152   }
153
154   return -1;
155 }
156
157 static bool is_on_heap(const void* address)
158 {
159   const xbt_mheap_t heap = mc_model_checker->process().get_heap();
160   return address >= heap->heapbase && address < heap->breakval;
161 }
162
163 static bool is_stack(const void *address)
164 {
165   for (auto const& stack : mc_model_checker->process().stack_areas())
166     if (address == stack.address)
167       return true;
168   return false;
169 }
170
171 // TODO, this should depend on the snapshot?
172 static bool is_block_stack(int block)
173 {
174   for (auto const& stack : mc_model_checker->process().stack_areas())
175     if (block == stack.block)
176       return true;
177   return false;
178 }
179
180 namespace simgrid {
181 namespace mc {
182
183 void StateComparator::match_equals(HeapLocationPairs* list)
184 {
185   for (auto const& pair : *list) {
186     if (pair[0].fragment_ != -1) {
187       this->equals_to_<1>(pair[0].block_, pair[0].fragment_) = simgrid::mc::HeapArea(pair[1].block_, pair[1].fragment_);
188       this->equals_to_<2>(pair[1].block_, pair[1].fragment_) = simgrid::mc::HeapArea(pair[0].block_, pair[0].fragment_);
189     } else {
190       this->equals_to_<1>(pair[0].block_, 0) = simgrid::mc::HeapArea(pair[1].block_, pair[1].fragment_);
191       this->equals_to_<2>(pair[1].block_, 0) = simgrid::mc::HeapArea(pair[0].block_, pair[0].fragment_);
192     }
193   }
194 }
195
196 void ProcessComparisonState::initHeapInformation(xbt_mheap_t heap,
197                         std::vector<simgrid::mc::IgnoredHeapRegion>* i)
198 {
199   auto heaplimit  = heap->heaplimit;
200   this->heapsize  = heap->heapsize;
201   this->to_ignore = i;
202   this->equals_to.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, HeapArea());
203   this->types.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, nullptr);
204 }
205
206 int StateComparator::initHeapInformation(xbt_mheap_t heap1, xbt_mheap_t heap2,
207                           std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
208                           std::vector<simgrid::mc::IgnoredHeapRegion>* i2)
209 {
210   if ((heap1->heaplimit != heap2->heaplimit) || (heap1->heapsize != heap2->heapsize))
211     return -1;
212   this->heaplimit     = heap1->heaplimit;
213   this->std_heap_copy = *mc_model_checker->process().get_heap();
214   this->processStates[0].initHeapInformation(heap1, i1);
215   this->processStates[1].initHeapInformation(heap2, i2);
216   return 0;
217 }
218
219 // TODO, have a robust way to find it in O(1)
220 static inline Region* MC_get_heap_region(Snapshot* snapshot)
221 {
222   for (auto const& region : snapshot->snapshot_regions_)
223     if (region->region_type() == simgrid::mc::RegionType::Heap)
224       return region.get();
225   xbt_die("No heap region");
226 }
227
228 static bool heap_area_differ(StateComparator& state, const void* area1, const void* area2, Snapshot* snapshot1,
229                              Snapshot* snapshot2, HeapLocationPairs* previous, Type* type, int pointer_level);
230
231 static bool mmalloc_heap_differ(simgrid::mc::StateComparator& state, simgrid::mc::Snapshot* snapshot1,
232                                 simgrid::mc::Snapshot* snapshot2)
233 {
234   const simgrid::mc::RemoteClient& process = mc_model_checker->process();
235
236   /* Check busy blocks */
237   size_t i1 = 1;
238
239   malloc_info heapinfo_temp1;
240   malloc_info heapinfo_temp2;
241   malloc_info heapinfo_temp2b;
242
243   simgrid::mc::Region* heap_region1 = MC_get_heap_region(snapshot1);
244   simgrid::mc::Region* heap_region2 = MC_get_heap_region(snapshot2);
245
246   // This is the address of std_heap->heapinfo in the application process:
247   void* heapinfo_address = &((xbt_mheap_t)process.heap_address)->heapinfo;
248
249   // This is in snapshot do not use them directly:
250   const malloc_info* heapinfos1 =
251       snapshot1->read<malloc_info*>(RemotePtr<malloc_info*>((std::uint64_t)heapinfo_address));
252   const malloc_info* heapinfos2 =
253       snapshot2->read<malloc_info*>(RemotePtr<malloc_info*>((std::uint64_t)heapinfo_address));
254
255   while (i1 < state.heaplimit) {
256
257     const malloc_info* heapinfo1 =
258         (const malloc_info*)heap_region1->read(&heapinfo_temp1, &heapinfos1[i1], sizeof(malloc_info));
259     const malloc_info* heapinfo2 =
260         (const malloc_info*)heap_region2->read(&heapinfo_temp2, &heapinfos2[i1], sizeof(malloc_info));
261
262     if (heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type == MMALLOC_TYPE_HEAPINFO) {      /* Free block */
263       i1 ++;
264       continue;
265     }
266
267     if (heapinfo1->type < 0) {
268       fprintf(stderr, "Unkown mmalloc block type.\n");
269       abort();
270     }
271
272     void* addr_block1 = ((void*)(((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase));
273
274     if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED) {       /* Large block */
275
276       if (is_stack(addr_block1)) {
277         for (size_t k = 0; k < heapinfo1->busy_block.size; k++)
278           state.equals_to_<1>(i1 + k, 0) = HeapArea(i1, -1);
279         for (size_t k = 0; k < heapinfo2->busy_block.size; k++)
280           state.equals_to_<2>(i1 + k, 0) = HeapArea(i1, -1);
281         i1 += heapinfo1->busy_block.size;
282         continue;
283       }
284
285       if (state.equals_to_<1>(i1, 0).valid_) {
286         i1++;
287         continue;
288       }
289
290       size_t i2 = 1;
291       bool equal = false;
292
293       /* Try first to associate to same block in the other heap */
294       if (heapinfo2->type == heapinfo1->type && state.equals_to_<2>(i1, 0).valid_ == 0) {
295         void* addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
296         if (not heap_area_differ(state, addr_block1, addr_block2, snapshot1, snapshot2, nullptr, nullptr, 0)) {
297           for (size_t k = 1; k < heapinfo2->busy_block.size; k++)
298             state.equals_to_<2>(i1 + k, 0) = HeapArea(i1, -1);
299           for (size_t k = 1; k < heapinfo1->busy_block.size; k++)
300             state.equals_to_<1>(i1 + k, 0) = HeapArea(i1, -1);
301           equal = true;
302           i1 += heapinfo1->busy_block.size;
303         }
304       }
305
306       while (i2 < state.heaplimit && not equal) {
307
308         void* addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
309
310         if (i2 == i1) {
311           i2++;
312           continue;
313         }
314
315         const malloc_info* heapinfo2b =
316             (const malloc_info*)heap_region2->read(&heapinfo_temp2b, &heapinfos2[i2], sizeof(malloc_info));
317
318         if (heapinfo2b->type != MMALLOC_TYPE_UNFRAGMENTED) {
319           i2++;
320           continue;
321         }
322
323         if (state.equals_to_<2>(i2, 0).valid_) {
324           i2++;
325           continue;
326         }
327
328         if (not heap_area_differ(state, addr_block1, addr_block2, snapshot1, snapshot2, nullptr, nullptr, 0)) {
329           for (size_t k = 1; k < heapinfo2b->busy_block.size; k++)
330             state.equals_to_<2>(i2 + k, 0) = HeapArea(i1, -1);
331           for (size_t k = 1; k < heapinfo1->busy_block.size; k++)
332             state.equals_to_<1>(i1 + k, 0) = HeapArea(i2, -1);
333           equal = true;
334           i1 += heapinfo1->busy_block.size;
335         }
336
337         i2++;
338       }
339
340       if (not equal) {
341         XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1, heapinfo1->busy_block.busy_size, addr_block1);
342         return true;
343       }
344
345     } else {                    /* Fragmented block */
346
347       for (size_t j1 = 0; j1 < (size_t)(BLOCKSIZE >> heapinfo1->type); j1++) {
348
349         if (heapinfo1->busy_frag.frag_size[j1] == -1) /* Free fragment_ */
350           continue;
351
352         if (state.equals_to_<1>(i1, j1).valid_)
353           continue;
354
355         void* addr_frag1 = (void*)((char*)addr_block1 + (j1 << heapinfo1->type));
356
357         size_t i2 = 1;
358         bool equal = false;
359
360         /* Try first to associate to same fragment_ in the other heap */
361         if (heapinfo2->type == heapinfo1->type && not state.equals_to_<2>(i1, j1).valid_) {
362           void* addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
363           void* addr_frag2  = (void*)((char*)addr_block2 + (j1 << heapinfo2->type));
364           if (not heap_area_differ(state, addr_frag1, addr_frag2, snapshot1, snapshot2, nullptr, nullptr, 0))
365             equal = true;
366         }
367
368         while (i2 < state.heaplimit && not equal) {
369
370           const malloc_info* heapinfo2b =
371               (const malloc_info*)heap_region2->read(&heapinfo_temp2b, &heapinfos2[i2], sizeof(malloc_info));
372
373           if (heapinfo2b->type == MMALLOC_TYPE_FREE || heapinfo2b->type == MMALLOC_TYPE_HEAPINFO) {
374             i2 ++;
375             continue;
376           }
377
378           // We currently do not match fragments with unfragmented blocks (maybe we should).
379           if (heapinfo2b->type == MMALLOC_TYPE_UNFRAGMENTED) {
380             i2++;
381             continue;
382           }
383
384           if (heapinfo2b->type < 0) {
385             fprintf(stderr, "Unknown mmalloc block type.\n");
386             abort();
387           }
388
389           for (size_t j2 = 0; j2 < (size_t)(BLOCKSIZE >> heapinfo2b->type); j2++) {
390
391             if (i2 == i1 && j2 == j1)
392               continue;
393
394             if (state.equals_to_<2>(i2, j2).valid_)
395               continue;
396
397             void* addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
398             void* addr_frag2  = (void*)((char*)addr_block2 + (j2 << heapinfo2b->type));
399
400             if (not heap_area_differ(state, addr_frag1, addr_frag2, snapshot1, snapshot2, nullptr, nullptr, 0)) {
401               equal = true;
402               break;
403             }
404           }
405
406           i2++;
407         }
408
409         if (not equal) {
410           XBT_DEBUG("Block %zu, fragment_ %zu not found (size_used = %zd, address = %p)\n", i1, j1,
411                     heapinfo1->busy_frag.frag_size[j1], addr_frag1);
412           return true;
413         }
414       }
415
416       i1++;
417     }
418   }
419
420   /* All blocks/fragments are equal to another block/fragment_ ? */
421   for (size_t i = 1; i < state.heaplimit; i++) {
422     const malloc_info* heapinfo1 =
423         (const malloc_info*)heap_region1->read(&heapinfo_temp1, &heapinfos1[i], sizeof(malloc_info));
424
425     if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED && i1 == state.heaplimit && heapinfo1->busy_block.busy_size > 0 &&
426         not state.equals_to_<1>(i, 0).valid_) {
427       XBT_DEBUG("Block %zu not found (size used = %zu)", i, heapinfo1->busy_block.busy_size);
428       return true;
429     }
430
431     if (heapinfo1->type <= 0)
432       continue;
433     for (size_t j = 0; j < (size_t)(BLOCKSIZE >> heapinfo1->type); j++)
434       if (i1 == state.heaplimit && heapinfo1->busy_frag.frag_size[j] > 0 && not state.equals_to_<1>(i, j).valid_) {
435         XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)", i, j, heapinfo1->busy_frag.frag_size[j]);
436         return true;
437       }
438   }
439
440   for (size_t i = 1; i < state.heaplimit; i++) {
441     const malloc_info* heapinfo2 =
442         (const malloc_info*)heap_region2->read(&heapinfo_temp2, &heapinfos2[i], sizeof(malloc_info));
443     if (heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED && i1 == state.heaplimit && heapinfo2->busy_block.busy_size > 0 &&
444         not state.equals_to_<2>(i, 0).valid_) {
445       XBT_DEBUG("Block %zu not found (size used = %zu)", i,
446                 heapinfo2->busy_block.busy_size);
447       return true;
448     }
449
450     if (heapinfo2->type <= 0)
451       continue;
452
453     for (size_t j = 0; j < (size_t)(BLOCKSIZE >> heapinfo2->type); j++)
454       if (i1 == state.heaplimit && heapinfo2->busy_frag.frag_size[j] > 0 && not state.equals_to_<2>(i, j).valid_) {
455         XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)",
456           i, j, heapinfo2->busy_frag.frag_size[j]);
457         return true;
458       }
459   }
460
461   return false;
462 }
463
464 /**
465  *
466  * @param state
467  * @param real_area1     Process address for state 1
468  * @param real_area2     Process address for state 2
469  * @param snapshot1      Snapshot of state 1
470  * @param snapshot2      Snapshot of state 2
471  * @param previous
472  * @param size
473  * @param check_ignore
474  * @return true when different, false otherwise (same or unknown)
475  */
476 static bool heap_area_differ_without_type(simgrid::mc::StateComparator& state, const void* real_area1,
477                                           const void* real_area2, simgrid::mc::Snapshot* snapshot1,
478                                           simgrid::mc::Snapshot* snapshot2, HeapLocationPairs* previous, int size,
479                                           int check_ignore)
480 {
481   const simgrid::mc::RemoteClient& process = mc_model_checker->process();
482   simgrid::mc::Region* heap_region1  = MC_get_heap_region(snapshot1);
483   simgrid::mc::Region* heap_region2  = MC_get_heap_region(snapshot2);
484
485   for (int i = 0; i < size; ) {
486
487     if (check_ignore > 0) {
488       ssize_t ignore1 = heap_comparison_ignore_size(state.processStates[0].to_ignore, (const char*)real_area1 + i);
489       if (ignore1 != -1) {
490         ssize_t ignore2 = heap_comparison_ignore_size(state.processStates[1].to_ignore, (const char*)real_area2 + i);
491         if (ignore2 == ignore1) {
492           if (ignore1 == 0) {
493             check_ignore--;
494             return false;
495           } else {
496             i = i + ignore2;
497             check_ignore--;
498             continue;
499           }
500         }
501       }
502     }
503
504     if (MC_snapshot_region_memcmp((const char*)real_area1 + i, heap_region1, (const char*)real_area2 + i, heap_region2,
505                                   1) != 0) {
506
507       int pointer_align = (i / sizeof(void *)) * sizeof(void *);
508       const void* addr_pointed1 = snapshot1->read(remote((void**)((const char*)real_area1 + pointer_align)));
509       const void* addr_pointed2 = snapshot2->read(remote((void**)((const char*)real_area2 + pointer_align)));
510
511       if (process.in_maestro_stack(remote(addr_pointed1)) && process.in_maestro_stack(remote(addr_pointed2))) {
512         i = pointer_align + sizeof(void *);
513         continue;
514       }
515
516       if (is_on_heap(addr_pointed1) && is_on_heap(addr_pointed2)) {
517         // Both addresses are in the heap:
518         if (heap_area_differ(state, addr_pointed1, addr_pointed2, snapshot1, snapshot2, previous, nullptr, 0))
519           return true;
520         i = pointer_align + sizeof(void *);
521         continue;
522       }
523
524       return true;
525     }
526
527     i++;
528   }
529
530   return false;
531 }
532
533 /**
534  *
535  * @param state
536  * @param real_area1     Process address for state 1
537  * @param real_area2     Process address for state 2
538  * @param snapshot1      Snapshot of state 1
539  * @param snapshot2      Snapshot of state 2
540  * @param previous
541  * @param type
542  * @param area_size      either a byte_size or an elements_count (?)
543  * @param check_ignore
544  * @param pointer_level
545  * @return               true when different, false otherwise (same or unknown)
546  */
547 static bool heap_area_differ_with_type(simgrid::mc::StateComparator& state, const void* real_area1,
548                                        const void* real_area2, simgrid::mc::Snapshot* snapshot1,
549                                        simgrid::mc::Snapshot* snapshot2, HeapLocationPairs* previous,
550                                        simgrid::mc::Type* type, int area_size, int check_ignore, int pointer_level)
551 {
552   // HACK: This should not happen but in pratice, there are some
553   // DW_TAG_typedef without an associated DW_AT_type:
554   //<1><538832>: Abbrev Number: 111 (DW_TAG_typedef)
555   //    <538833>   DW_AT_name        : (indirect string, offset: 0x2292f3): gregset_t
556   //    <538837>   DW_AT_decl_file   : 98
557   //    <538838>   DW_AT_decl_line   : 37
558   if (type == nullptr)
559     return false;
560
561   if (is_stack(real_area1) && is_stack(real_area2))
562     return false;
563
564   if (check_ignore > 0) {
565     ssize_t ignore1 = heap_comparison_ignore_size(state.processStates[0].to_ignore, real_area1);
566     if (ignore1 > 0 && heap_comparison_ignore_size(state.processStates[1].to_ignore, real_area2) == ignore1)
567       return false;
568   }
569
570   simgrid::mc::Type* subtype;
571   simgrid::mc::Type* subsubtype;
572   int elm_size;
573   const void* addr_pointed1;
574   const void* addr_pointed2;
575
576   simgrid::mc::Region* heap_region1 = MC_get_heap_region(snapshot1);
577   simgrid::mc::Region* heap_region2 = MC_get_heap_region(snapshot2);
578
579   switch (type->type) {
580     case DW_TAG_unspecified_type:
581       return true;
582
583     case DW_TAG_base_type:
584       if (not type->name.empty() && type->name == "char") { /* String, hence random (arbitrary ?) size */
585         if (real_area1 == real_area2)
586           return false;
587         else
588           return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, area_size) != 0;
589       } else {
590         if (area_size != -1 && type->byte_size != area_size)
591           return false;
592         else
593           return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
594       }
595
596     case DW_TAG_enumeration_type:
597       if (area_size != -1 && type->byte_size != area_size)
598         return false;
599       return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
600
601     case DW_TAG_typedef:
602     case DW_TAG_const_type:
603     case DW_TAG_volatile_type:
604       return heap_area_differ_with_type(state, real_area1, real_area2, snapshot1, snapshot2, previous, type->subtype,
605                                         area_size, check_ignore, pointer_level);
606
607     case DW_TAG_array_type:
608       subtype = type->subtype;
609       switch (subtype->type) {
610         case DW_TAG_unspecified_type:
611           return true;
612
613         case DW_TAG_base_type:
614         case DW_TAG_enumeration_type:
615         case DW_TAG_pointer_type:
616         case DW_TAG_reference_type:
617         case DW_TAG_rvalue_reference_type:
618         case DW_TAG_structure_type:
619         case DW_TAG_class_type:
620         case DW_TAG_union_type:
621           if (subtype->full_type)
622             subtype = subtype->full_type;
623           elm_size  = subtype->byte_size;
624           break;
625         // TODO, just remove the type indirection?
626         case DW_TAG_const_type:
627         case DW_TAG_typedef:
628         case DW_TAG_volatile_type:
629           subsubtype = subtype->subtype;
630           if (subsubtype->full_type)
631             subsubtype = subsubtype->full_type;
632           elm_size     = subsubtype->byte_size;
633           break;
634         default:
635           return false;
636       }
637       for (int i = 0; i < type->element_count; i++) {
638         // TODO, add support for variable stride (DW_AT_byte_stride)
639         if (heap_area_differ_with_type(state, (const char*)real_area1 + (i * elm_size),
640                                        (const char*)real_area2 + (i * elm_size), snapshot1, snapshot2, previous,
641                                        type->subtype, subtype->byte_size, check_ignore, pointer_level))
642           return true;
643       }
644       return false;
645
646     case DW_TAG_reference_type:
647     case DW_TAG_rvalue_reference_type:
648     case DW_TAG_pointer_type:
649       if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
650         addr_pointed1 = snapshot1->read(remote((void* const*)real_area1));
651         addr_pointed2 = snapshot2->read(remote((void* const*)real_area2));
652         return (addr_pointed1 != addr_pointed2);
653       }
654       pointer_level++;
655       if (pointer_level <= 1) {
656         addr_pointed1 = snapshot1->read(remote((void* const*)real_area1));
657         addr_pointed2 = snapshot2->read(remote((void* const*)real_area2));
658         if (is_on_heap(addr_pointed1) && is_on_heap(addr_pointed2))
659           return heap_area_differ(state, addr_pointed1, addr_pointed2, snapshot1, snapshot2, previous, type->subtype,
660                                   pointer_level);
661         else
662           return (addr_pointed1 != addr_pointed2);
663       }
664       for (size_t i = 0; i < (area_size / sizeof(void*)); i++) {
665         addr_pointed1 = snapshot1->read(remote((void* const*)((const char*)real_area1 + i * sizeof(void*))));
666         addr_pointed2 = snapshot2->read(remote((void* const*)((const char*)real_area2 + i * sizeof(void*))));
667         bool differ   = is_on_heap(addr_pointed1) && is_on_heap(addr_pointed2)
668                           ? heap_area_differ(state, addr_pointed1, addr_pointed2, snapshot1, snapshot2, previous,
669                                              type->subtype, pointer_level)
670                           : addr_pointed1 != addr_pointed2;
671         if (differ)
672           return true;
673       }
674       return false;
675
676     case DW_TAG_structure_type:
677     case DW_TAG_class_type:
678       if (type->full_type)
679         type = type->full_type;
680       if (area_size != -1 && type->byte_size != area_size) {
681         if (area_size <= type->byte_size || area_size % type->byte_size != 0)
682           return false;
683         for (size_t i = 0; i < (size_t)(area_size / type->byte_size); i++) {
684           if (heap_area_differ_with_type(state, (const char*)real_area1 + i * type->byte_size,
685                                          (const char*)real_area2 + i * type->byte_size, snapshot1, snapshot2, previous,
686                                          type, -1, check_ignore, 0))
687             return true;
688         }
689         } else {
690           for (simgrid::mc::Member& member : type->members) {
691             // TODO, optimize this? (for the offset case)
692             void* real_member1 =
693                 simgrid::dwarf::resolve_member(real_area1, type, &member, (simgrid::mc::AddressSpace*)snapshot1);
694             void* real_member2 =
695                 simgrid::dwarf::resolve_member(real_area2, type, &member, (simgrid::mc::AddressSpace*)snapshot2);
696             if (heap_area_differ_with_type(state, real_member1, real_member2, snapshot1, snapshot2, previous,
697                                            member.type, -1, check_ignore, 0))
698               return true;
699           }
700         }
701         return false;
702
703     case DW_TAG_union_type:
704       return heap_area_differ_without_type(state, real_area1, real_area2, snapshot1, snapshot2, previous,
705                                            type->byte_size, check_ignore);
706
707     default:
708       XBT_VERB("Unknown case: %d", type->type);
709       break;
710   }
711   return false;
712 }
713
714 /** Infer the type of a part of the block from the type of the block
715  *
716  * TODO, handle DW_TAG_array_type as well as arrays of the object ((*p)[5], p[5])
717  *
718  * TODO, handle subfields ((*p).bar.foo, (*p)[5].bar…)
719  *
720  * @param  type               DWARF type ID of the root address
721  * @param  area_size
722  * @return                    DWARF type ID for given offset
723  */
724 static simgrid::mc::Type* get_offset_type(void* real_base_address, simgrid::mc::Type* type, int offset, int area_size,
725                                           simgrid::mc::Snapshot* snapshot)
726 {
727
728   // Beginning of the block, the infered variable type if the type of the block:
729   if (offset == 0)
730     return type;
731
732   switch (type->type) {
733
734   case DW_TAG_structure_type:
735   case DW_TAG_class_type:
736     if (type->full_type)
737       type = type->full_type;
738     if (area_size != -1 && type->byte_size != area_size) {
739       if (area_size > type->byte_size && area_size % type->byte_size == 0)
740         return type;
741       else
742         return nullptr;
743     }
744
745     for (simgrid::mc::Member& member : type->members) {
746       if (member.has_offset_location()) {
747         // We have the offset, use it directly (shortcut):
748         if (member.offset() == offset)
749           return member.type;
750       } else {
751         void* real_member = simgrid::dwarf::resolve_member(real_base_address, type, &member, snapshot);
752         if ((char*)real_member - (char*)real_base_address == offset)
753           return member.type;
754       }
755     }
756     return nullptr;
757
758   default:
759     /* FIXME: other cases ? */
760     return nullptr;
761
762   }
763 }
764
765 /**
766  *
767  * @param area1          Process address for state 1
768  * @param area2          Process address for state 2
769  * @param snapshot1      Snapshot of state 1
770  * @param snapshot2      Snapshot of state 2
771  * @param previous       Pairs of blocks already compared on the current path (or nullptr)
772  * @param type_id        Type of variable
773  * @param pointer_level
774  * @return true when different, false otherwise (same or unknown)
775  */
776 static bool heap_area_differ(simgrid::mc::StateComparator& state, const void* area1, const void* area2,
777                              simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2,
778                              HeapLocationPairs* previous, simgrid::mc::Type* type, int pointer_level)
779 {
780   const simgrid::mc::RemoteClient& process = mc_model_checker->process();
781
782   ssize_t block1;
783   ssize_t block2;
784   ssize_t size;
785   int check_ignore = 0;
786
787   int type_size = -1;
788   int offset1   = 0;
789   int offset2   = 0;
790   int new_size1 = -1;
791   int new_size2 = -1;
792
793   simgrid::mc::Type* new_type1 = nullptr;
794   simgrid::mc::Type* new_type2 = nullptr;
795
796   bool match_pairs = false;
797
798   // This is the address of std_heap->heapinfo in the application process:
799   void* heapinfo_address = &((xbt_mheap_t)process.heap_address)->heapinfo;
800
801   const malloc_info* heapinfos1 = snapshot1->read(remote((const malloc_info**)heapinfo_address));
802   const malloc_info* heapinfos2 = snapshot2->read(remote((const malloc_info**)heapinfo_address));
803
804   malloc_info heapinfo_temp1;
805   malloc_info heapinfo_temp2;
806
807   simgrid::mc::HeapLocationPairs current;
808   if (previous == nullptr) {
809     previous = &current;
810     match_pairs = true;
811   }
812
813   // Get block number:
814   block1 = ((char*)area1 - (char*)state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
815   block2 = ((char*)area2 - (char*)state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
816
817   // If either block is a stack block:
818   if (is_block_stack((int) block1) && is_block_stack((int) block2)) {
819     previous->insert(HeapLocationPair{{HeapLocation(block1, -1), HeapLocation(block2, -1)}});
820     if (match_pairs)
821       state.match_equals(previous);
822     return false;
823   }
824
825   // If either block is not in the expected area of memory:
826   if (((char*)area1 < (char*)state.std_heap_copy.heapbase) || (block1 > (ssize_t)state.processStates[0].heapsize) ||
827       (block1 < 1) || ((char*)area2 < (char*)state.std_heap_copy.heapbase) ||
828       (block2 > (ssize_t)state.processStates[1].heapsize) || (block2 < 1)) {
829     return true;
830   }
831
832   // Process address of the block:
833   void* real_addr_block1 = (ADDR2UINT(block1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
834   void* real_addr_block2 = (ADDR2UINT(block2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
835
836   if (type) {
837     if (type->full_type)
838       type = type->full_type;
839
840     // This assume that for "boring" types (volatile ...) byte_size is absent:
841     while (type->byte_size == 0 && type->subtype != nullptr)
842       type = type->subtype;
843
844     // Find type_size:
845     if (type->type == DW_TAG_pointer_type ||
846         (type->type == DW_TAG_base_type && not type->name.empty() && type->name == "char"))
847       type_size = -1;
848     else
849       type_size = type->byte_size;
850
851   }
852
853   simgrid::mc::Region* heap_region1 = MC_get_heap_region(snapshot1);
854   simgrid::mc::Region* heap_region2 = MC_get_heap_region(snapshot2);
855
856   const malloc_info* heapinfo1 =
857       (const malloc_info*)heap_region1->read(&heapinfo_temp1, &heapinfos1[block1], sizeof(malloc_info));
858   const malloc_info* heapinfo2 =
859       (const malloc_info*)heap_region2->read(&heapinfo_temp2, &heapinfos2[block2], sizeof(malloc_info));
860
861   if ((heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type==MMALLOC_TYPE_HEAPINFO)
862     && (heapinfo2->type == MMALLOC_TYPE_FREE || heapinfo2->type ==MMALLOC_TYPE_HEAPINFO)) {
863     /* Free block */
864     if (match_pairs)
865       state.match_equals(previous);
866     return false;
867   }
868
869   if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED && heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
870     /* Complete block */
871
872     // TODO, lookup variable type from block type as done for fragmented blocks
873
874     if (state.equals_to_<1>(block1, 0).valid_ && state.equals_to_<2>(block2, 0).valid_ &&
875         state.blocksEqual(block1, block2)) {
876       if (match_pairs)
877         state.match_equals(previous);
878       return false;
879     }
880
881     if (type_size != -1 && type_size != (ssize_t)heapinfo1->busy_block.busy_size &&
882         type_size != (ssize_t)heapinfo2->busy_block.busy_size &&
883         (type->name.empty() || type->name == "struct s_smx_context")) {
884       if (match_pairs)
885         state.match_equals(previous);
886       return false;
887     }
888
889     if (heapinfo1->busy_block.size != heapinfo2->busy_block.size ||
890         heapinfo1->busy_block.busy_size != heapinfo2->busy_block.busy_size)
891       return true;
892
893     if (not previous->insert(HeapLocationPair{{HeapLocation(block1, -1), HeapLocation(block2, -1)}}).second) {
894       if (match_pairs)
895         state.match_equals(previous);
896       return false;
897     }
898
899     size = heapinfo1->busy_block.busy_size;
900
901     // Remember (basic) type inference.
902     // The current data structure only allows us to do this for the whole block.
903     if (type != nullptr && area1 == real_addr_block1)
904       state.types_<1>(block1, 0) = type;
905     if (type != nullptr && area2 == real_addr_block2)
906       state.types_<2>(block2, 0) = type;
907
908     if (size <= 0) {
909       if (match_pairs)
910         state.match_equals(previous);
911       return false;
912     }
913
914     if (heapinfo1->busy_block.ignore > 0
915         && heapinfo2->busy_block.ignore == heapinfo1->busy_block.ignore)
916       check_ignore = heapinfo1->busy_block.ignore;
917
918   } else if ((heapinfo1->type > 0) && (heapinfo2->type > 0)) {      /* Fragmented block */
919
920     // Fragment number:
921     ssize_t frag1 = ((uintptr_t)(ADDR2UINT(area1) % (BLOCKSIZE))) >> heapinfo1->type;
922     ssize_t frag2 = ((uintptr_t)(ADDR2UINT(area2) % (BLOCKSIZE))) >> heapinfo2->type;
923
924     // Process address of the fragment_:
925     void* real_addr_frag1 = (void*)((char*)real_addr_block1 + (frag1 << heapinfo1->type));
926     void* real_addr_frag2 = (void*)((char*)real_addr_block2 + (frag2 << heapinfo2->type));
927
928     // Check the size of the fragments against the size of the type:
929     if (type_size != -1) {
930       if (heapinfo1->busy_frag.frag_size[frag1] == -1 || heapinfo2->busy_frag.frag_size[frag2] == -1) {
931         if (match_pairs)
932           state.match_equals(previous);
933         return false;
934       }
935       // ?
936       if (type_size != heapinfo1->busy_frag.frag_size[frag1]
937           || type_size != heapinfo2->busy_frag.frag_size[frag2]) {
938         if (match_pairs)
939           state.match_equals(previous);
940         return false;
941       }
942     }
943
944     // Check if the blocks are already matched together:
945     if (state.equals_to_<1>(block1, frag1).valid_ && state.equals_to_<2>(block2, frag2).valid_ && offset1 == offset2 &&
946         state.fragmentsEqual(block1, frag1, block2, frag2)) {
947       if (match_pairs)
948         state.match_equals(previous);
949       return false;
950     }
951     // Compare the size of both fragments:
952     if (heapinfo1->busy_frag.frag_size[frag1] != heapinfo2->busy_frag.frag_size[frag2]) {
953       if (type_size == -1) {
954         if (match_pairs)
955           state.match_equals(previous);
956         return false;
957       } else
958         return true;
959     }
960
961     // Size of the fragment_:
962     size = heapinfo1->busy_frag.frag_size[frag1];
963
964     // Remember (basic) type inference.
965     // The current data structure only allows us to do this for the whole fragment_.
966     if (type != nullptr && area1 == real_addr_frag1)
967       state.types_<1>(block1, frag1) = type;
968     if (type != nullptr && area2 == real_addr_frag2)
969       state.types_<2>(block2, frag2) = type;
970
971     // The type of the variable is already known:
972     if (type) {
973       new_type1 = new_type2 = type;
974     }
975     // Type inference from the block type.
976     else if (state.types_<1>(block1, frag1) != nullptr || state.types_<2>(block2, frag2) != nullptr) {
977
978       offset1 = (char*)area1 - (char*)real_addr_frag1;
979       offset2 = (char*)area2 - (char*)real_addr_frag2;
980
981       if (state.types_<1>(block1, frag1) != nullptr && state.types_<2>(block2, frag2) != nullptr) {
982         new_type1 = get_offset_type(real_addr_frag1, state.types_<1>(block1, frag1), offset1, size, snapshot1);
983         new_type2 = get_offset_type(real_addr_frag2, state.types_<2>(block2, frag2), offset1, size, snapshot2);
984       } else if (state.types_<1>(block1, frag1) != nullptr) {
985         new_type1 = get_offset_type(real_addr_frag1, state.types_<1>(block1, frag1), offset1, size, snapshot1);
986         new_type2 = get_offset_type(real_addr_frag2, state.types_<1>(block1, frag1), offset2, size, snapshot2);
987       } else if (state.types_<2>(block2, frag2) != nullptr) {
988         new_type1 = get_offset_type(real_addr_frag1, state.types_<2>(block2, frag2), offset1, size, snapshot1);
989         new_type2 = get_offset_type(real_addr_frag2, state.types_<2>(block2, frag2), offset2, size, snapshot2);
990       } else {
991         if (match_pairs)
992           state.match_equals(previous);
993         return false;
994       }
995
996       if (new_type1 != nullptr && new_type2 != nullptr && new_type1 != new_type2) {
997
998         type = new_type1;
999         while (type->byte_size == 0 && type->subtype != nullptr)
1000           type = type->subtype;
1001         new_size1 = type->byte_size;
1002
1003         type = new_type2;
1004         while (type->byte_size == 0 && type->subtype != nullptr)
1005           type = type->subtype;
1006         new_size2 = type->byte_size;
1007
1008       } else {
1009         if (match_pairs)
1010           state.match_equals(previous);
1011         return false;
1012       }
1013     }
1014
1015     if (new_size1 > 0 && new_size1 == new_size2) {
1016       type = new_type1;
1017       size = new_size1;
1018     }
1019
1020     if (offset1 == 0 && offset2 == 0 &&
1021         not previous->insert(HeapLocationPair{{HeapLocation(block1, frag1), HeapLocation(block2, frag2)}}).second) {
1022       if (match_pairs)
1023         state.match_equals(previous);
1024       return false;
1025     }
1026
1027     if (size <= 0) {
1028       if (match_pairs)
1029         state.match_equals(previous);
1030       return false;
1031     }
1032
1033     if ((heapinfo1->busy_frag.ignore[frag1] > 0) &&
1034         (heapinfo2->busy_frag.ignore[frag2] == heapinfo1->busy_frag.ignore[frag1]))
1035       check_ignore = heapinfo1->busy_frag.ignore[frag1];
1036
1037   } else
1038     return true;
1039
1040   /* Start comparison */
1041   bool differ =
1042       type ? heap_area_differ_with_type(state, area1, area2, snapshot1, snapshot2, previous, type, size, check_ignore,
1043                                         pointer_level)
1044            : heap_area_differ_without_type(state, area1, area2, snapshot1, snapshot2, previous, size, check_ignore);
1045   if (differ)
1046     return true;
1047
1048   if (match_pairs)
1049     state.match_equals(previous);
1050   return false;
1051 }
1052
1053 }
1054 }
1055
1056 /************************** Snapshot comparison *******************************/
1057 /******************************************************************************/
1058
1059 static bool areas_differ_with_type(simgrid::mc::StateComparator& state, const void* real_area1,
1060                                    simgrid::mc::Snapshot* snapshot1, simgrid::mc::Region* region1,
1061                                    const void* real_area2, simgrid::mc::Snapshot* snapshot2,
1062                                    simgrid::mc::Region* region2, simgrid::mc::Type* type, int pointer_level)
1063 {
1064   simgrid::mc::Type* subtype;
1065   simgrid::mc::Type* subsubtype;
1066   int elm_size;
1067   int i;
1068
1069   xbt_assert(type != nullptr);
1070   switch (type->type) {
1071     case DW_TAG_unspecified_type:
1072       return true;
1073
1074     case DW_TAG_base_type:
1075     case DW_TAG_enumeration_type:
1076     case DW_TAG_union_type:
1077       return MC_snapshot_region_memcmp(real_area1, region1, real_area2, region2, type->byte_size) != 0;
1078     case DW_TAG_typedef:
1079     case DW_TAG_volatile_type:
1080     case DW_TAG_const_type:
1081       return areas_differ_with_type(state, real_area1, snapshot1, region1, real_area2, snapshot2, region2,
1082                                     type->subtype, pointer_level);
1083     case DW_TAG_array_type:
1084       subtype = type->subtype;
1085       switch (subtype->type) {
1086         case DW_TAG_unspecified_type:
1087           return true;
1088
1089         case DW_TAG_base_type:
1090         case DW_TAG_enumeration_type:
1091         case DW_TAG_pointer_type:
1092         case DW_TAG_reference_type:
1093         case DW_TAG_rvalue_reference_type:
1094         case DW_TAG_structure_type:
1095         case DW_TAG_class_type:
1096         case DW_TAG_union_type:
1097           if (subtype->full_type)
1098             subtype = subtype->full_type;
1099           elm_size  = subtype->byte_size;
1100           break;
1101         case DW_TAG_const_type:
1102         case DW_TAG_typedef:
1103         case DW_TAG_volatile_type:
1104           subsubtype = subtype->subtype;
1105           if (subsubtype->full_type)
1106             subsubtype = subsubtype->full_type;
1107           elm_size     = subsubtype->byte_size;
1108           break;
1109         default:
1110           return false;
1111       }
1112       for (i = 0; i < type->element_count; i++) {
1113         size_t off = i * elm_size;
1114         if (areas_differ_with_type(state, (char*)real_area1 + off, snapshot1, region1, (char*)real_area2 + off,
1115                                    snapshot2, region2, type->subtype, pointer_level))
1116           return true;
1117       }
1118       break;
1119     case DW_TAG_pointer_type:
1120     case DW_TAG_reference_type:
1121     case DW_TAG_rvalue_reference_type: {
1122       const void* addr_pointed1 = MC_region_read_pointer(region1, real_area1);
1123       const void* addr_pointed2 = MC_region_read_pointer(region2, real_area2);
1124
1125       if (type->subtype && type->subtype->type == DW_TAG_subroutine_type)
1126         return (addr_pointed1 != addr_pointed2);
1127       if (addr_pointed1 == nullptr && addr_pointed2 == nullptr)
1128         return false;
1129       if (addr_pointed1 == nullptr || addr_pointed2 == nullptr)
1130         return true;
1131       if (not state.compared_pointers.insert(std::make_pair(addr_pointed1, addr_pointed2)).second)
1132         return false;
1133
1134       pointer_level++;
1135
1136       // Some cases are not handled here:
1137       // * the pointers lead to different areas (one to the heap, the other to the RW segment ...)
1138       // * a pointer leads to the read-only segment of the current object
1139       // * a pointer lead to a different ELF object
1140
1141       if (is_on_heap(addr_pointed1)) {
1142         if (not is_on_heap(addr_pointed2))
1143           return true;
1144         // The pointers are both in the heap:
1145         return simgrid::mc::heap_area_differ(state, addr_pointed1, addr_pointed2, snapshot1, snapshot2, nullptr,
1146                                              type->subtype, pointer_level);
1147
1148       } else if (region1->contain(simgrid::mc::remote(addr_pointed1))) {
1149         // The pointers are both in the current object R/W segment:
1150         if (not region2->contain(simgrid::mc::remote(addr_pointed2)))
1151           return true;
1152         if (not type->type_id)
1153           return (addr_pointed1 != addr_pointed2);
1154         else
1155           return areas_differ_with_type(state, addr_pointed1, snapshot1, region1, addr_pointed2, snapshot2, region2,
1156                                         type->subtype, pointer_level);
1157       } else {
1158
1159         // TODO, We do not handle very well the case where
1160         // it belongs to a different (non-heap) region from the current one.
1161
1162         return (addr_pointed1 != addr_pointed2);
1163       }
1164     }
1165     case DW_TAG_structure_type:
1166     case DW_TAG_class_type:
1167       for (simgrid::mc::Member& member : type->members) {
1168         void* member1                   = simgrid::dwarf::resolve_member(real_area1, type, &member, snapshot1);
1169         void* member2                   = simgrid::dwarf::resolve_member(real_area2, type, &member, snapshot2);
1170         simgrid::mc::Region* subregion1 = snapshot1->get_region(member1, region1); // region1 is hinted
1171         simgrid::mc::Region* subregion2 = snapshot2->get_region(member2, region2); // region2 is hinted
1172         if (areas_differ_with_type(state, member1, snapshot1, subregion1, member2, snapshot2, subregion2, member.type,
1173                                    pointer_level))
1174           return true;
1175       }
1176       break;
1177     case DW_TAG_subroutine_type:
1178       return false;
1179     default:
1180       XBT_VERB("Unknown case: %d", type->type);
1181       break;
1182   }
1183
1184   return false;
1185 }
1186
1187 static bool global_variables_differ(simgrid::mc::StateComparator& state, simgrid::mc::ObjectInformation* object_info,
1188                                     simgrid::mc::Region* r1, simgrid::mc::Region* r2, simgrid::mc::Snapshot* snapshot1,
1189                                     simgrid::mc::Snapshot* snapshot2)
1190 {
1191   xbt_assert(r1 && r2, "Missing region.");
1192
1193   std::vector<simgrid::mc::Variable>& variables = object_info->global_variables;
1194
1195   for (simgrid::mc::Variable const& current_var : variables) {
1196
1197     // If the variable is not in this object, skip it:
1198     // We do not expect to find a pointer to something which is not reachable
1199     // by the global variables.
1200     if ((char *) current_var.address < (char *) object_info->start_rw
1201         || (char *) current_var.address > (char *) object_info->end_rw)
1202       continue;
1203
1204     simgrid::mc::Type* bvariable_type = current_var.type;
1205     if (areas_differ_with_type(state, (char*)current_var.address, snapshot1, r1, (char*)current_var.address, snapshot2,
1206                                r2, bvariable_type, 0)) {
1207       XBT_VERB("Global variable %s (%p) is different between snapshots",
1208                current_var.name.c_str(),
1209                (char *) current_var.address);
1210       return true;
1211     }
1212   }
1213
1214   return false;
1215 }
1216
1217 static bool local_variables_differ(simgrid::mc::StateComparator& state, simgrid::mc::Snapshot* snapshot1,
1218                                    simgrid::mc::Snapshot* snapshot2, mc_snapshot_stack_t stack1,
1219                                    mc_snapshot_stack_t stack2)
1220 {
1221   if (stack1->local_variables.size() != stack2->local_variables.size()) {
1222     XBT_VERB("Different number of local variables");
1223     return true;
1224   }
1225
1226   for (unsigned int cursor = 0; cursor < stack1->local_variables.size(); cursor++) {
1227     local_variable_t current_var1 = &stack1->local_variables[cursor];
1228     local_variable_t current_var2 = &stack2->local_variables[cursor];
1229     if (current_var1->name != current_var2->name || current_var1->subprogram != current_var2->subprogram ||
1230         current_var1->ip != current_var2->ip) {
1231       // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1232       XBT_VERB("Different name of variable (%s - %s) "
1233                "or frame (%s - %s) or ip (%lu - %lu)",
1234                current_var1->name.c_str(), current_var2->name.c_str(), current_var1->subprogram->name.c_str(),
1235                current_var2->subprogram->name.c_str(), current_var1->ip, current_var2->ip);
1236       return true;
1237     }
1238
1239     if (areas_differ_with_type(state, current_var1->address, snapshot1, snapshot1->get_region(current_var1->address),
1240                                current_var2->address, snapshot2, snapshot2->get_region(current_var2->address),
1241                                current_var1->type, 0)) {
1242       XBT_VERB("Local variable %s (%p - %p) in frame %s "
1243                "is different between snapshots",
1244                current_var1->name.c_str(), current_var1->address, current_var2->address,
1245                current_var1->subprogram->name.c_str());
1246       return true;
1247     }
1248   }
1249   return false;
1250 }
1251
1252 namespace simgrid {
1253 namespace mc {
1254
1255 bool snapshot_equal(Snapshot* s1, Snapshot* s2)
1256 {
1257   // TODO, make this a field of ModelChecker or something similar
1258   static StateComparator state_comparator;
1259
1260   const RemoteClient& process = mc_model_checker->process();
1261
1262   if (s1->hash_ != s2->hash_) {
1263     XBT_VERB("(%d - %d) Different hash: 0x%" PRIx64 "--0x%" PRIx64, s1->num_state_, s2->num_state_, s1->hash_,
1264              s2->hash_);
1265     return false;
1266   }
1267   XBT_VERB("(%d - %d) Same hash: 0x%" PRIx64, s1->num_state_, s2->num_state_, s1->hash_);
1268
1269   /* Compare enabled processes */
1270   if (s1->enabled_processes_ != s2->enabled_processes_) {
1271     XBT_VERB("(%d - %d) Different amount of enabled processes", s1->num_state_, s2->num_state_);
1272     return false;
1273   }
1274
1275   /* Compare size of stacks */
1276   for (unsigned long i = 0; i < s1->stacks_.size(); i++) {
1277     size_t size_used1 = s1->stack_sizes_[i];
1278     size_t size_used2 = s2->stack_sizes_[i];
1279     if (size_used1 != size_used2) {
1280       XBT_VERB("(%d - %d) Different size used in stacks: %zu - %zu", s1->num_state_, s2->num_state_, size_used1,
1281                size_used2);
1282       return false;
1283     }
1284   }
1285
1286   /* Init heap information used in heap comparison algorithm */
1287   xbt_mheap_t heap1 =
1288       static_cast<xbt_mheap_t>(s1->read_bytes(alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1289                                               remote(process.heap_address), simgrid::mc::ReadOptions::lazy()));
1290   xbt_mheap_t heap2 =
1291       static_cast<xbt_mheap_t>(s2->read_bytes(alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1292                                               remote(process.heap_address), simgrid::mc::ReadOptions::lazy()));
1293   if (state_comparator.initHeapInformation(heap1, heap2, &s1->to_ignore_, &s2->to_ignore_) == -1) {
1294     XBT_VERB("(%d - %d) Different heap information", s1->num_state_, s2->num_state_);
1295     return false;
1296   }
1297
1298   /* Stacks comparison */
1299   for (unsigned int cursor = 0; cursor < s1->stacks_.size(); cursor++) {
1300     mc_snapshot_stack_t stack1 = &s1->stacks_[cursor];
1301     mc_snapshot_stack_t stack2 = &s2->stacks_[cursor];
1302
1303     if (local_variables_differ(state_comparator, s1, s2, stack1, stack2)) {
1304       XBT_VERB("(%d - %d) Different local variables between stacks %u", s1->num_state_, s2->num_state_, cursor + 1);
1305       return false;
1306     }
1307   }
1308
1309   size_t regions_count = s1->snapshot_regions_.size();
1310   if (regions_count != s2->snapshot_regions_.size())
1311     return false;
1312
1313   for (size_t k = 0; k != regions_count; ++k) {
1314     Region* region1 = s1->snapshot_regions_[k].get();
1315     Region* region2 = s2->snapshot_regions_[k].get();
1316
1317     // Preconditions:
1318     if (region1->region_type() != RegionType::Data)
1319       continue;
1320
1321     xbt_assert(region1->region_type() == region2->region_type());
1322     xbt_assert(region1->object_info() == region2->object_info());
1323     xbt_assert(region1->object_info());
1324
1325     /* Compare global variables */
1326     if (global_variables_differ(state_comparator, region1->object_info(), region1, region2, s1, s2)) {
1327       std::string const& name = region1->object_info()->file_name;
1328       XBT_VERB("(%d - %d) Different global variables in %s", s1->num_state_, s2->num_state_, name.c_str());
1329       return false;
1330     }
1331   }
1332
1333   /* Compare heap */
1334   if (mmalloc_heap_differ(state_comparator, s1, s2)) {
1335     XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", s1->num_state_, s2->num_state_);
1336     return false;
1337   }
1338
1339   XBT_VERB("(%d - %d) No difference found", s1->num_state_, s2->num_state_);
1340
1341   return true;
1342 }
1343
1344 }
1345 }