Logo AND Algorithmique Numérique Distribuée

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