Logo AND Algorithmique Numérique Distribuée

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