Logo AND Algorithmique Numérique Distribuée

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