Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
kill an unreachable statement (thanks sonar)
[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
911   default:
912     return 0;
913   }
914
915   xbt_die("Unreachable");
916 }
917
918 /** Infer the type of a part of the block from the type of the block
919  *
920  * TODO, handle DW_TAG_array_type as well as arrays of the object ((*p)[5], p[5])
921  *
922  * TODO, handle subfields ((*p).bar.foo, (*p)[5].bar…)
923  *
924  * @param  type               DWARF type ID of the root address
925  * @param  area_size
926  * @return                    DWARF type ID for given offset
927  */
928 static simgrid::mc::Type* get_offset_type(void *real_base_address, simgrid::mc::Type* type,
929                                  int offset, int area_size,
930                                  simgrid::mc::Snapshot* snapshot, int process_index)
931 {
932
933   // Beginning of the block, the infered variable type if the type of the block:
934   if (offset == 0)
935     return type;
936
937   switch (type->type) {
938
939   case DW_TAG_structure_type:
940   case DW_TAG_class_type:
941     if (type->full_type)
942       type = type->full_type;
943     if (area_size != -1 && type->byte_size != area_size) {
944       if (area_size > type->byte_size && area_size % type->byte_size == 0)
945         return type;
946       else
947         return nullptr;
948     }
949
950     for(simgrid::mc::Member& member : type->members) {
951       if (member.has_offset_location()) {
952         // We have the offset, use it directly (shortcut):
953         if (member.offset() == offset)
954           return member.type;
955       } else {
956         void *real_member = simgrid::dwarf::resolve_member(
957           real_base_address, type, &member, snapshot, process_index);
958         if ((char*) real_member - (char *) real_base_address == offset)
959           return member.type;
960       }
961     }
962     return nullptr;
963
964   default:
965     /* FIXME: other cases ? */
966     return nullptr;
967
968   }
969 }
970
971 /**
972  *
973  * @param area1          Process address for state 1
974  * @param area2          Process address for state 2
975  * @param snapshot1      Snapshot of state 1
976  * @param snapshot2      Snapshot of state 2
977  * @param previous       Pairs of blocks already compared on the current path (or nullptr)
978  * @param type_id        Type of variable
979  * @param pointer_level
980  * @return 0 (same), 1 (different), -1
981  */
982 static
983 int compare_heap_area(simgrid::mc::StateComparator& state, int process_index,
984                       const void *area1, const void *area2,
985                       simgrid::mc::Snapshot* snapshot1,
986                       simgrid::mc::Snapshot* snapshot2,
987                       HeapLocationPairs* previous,
988                       simgrid::mc::Type* type, int pointer_level)
989 {
990   simgrid::mc::Process* process = &mc_model_checker->process();
991
992   int res_compare;
993   ssize_t block1, frag1, block2, frag2;
994   ssize_t size;
995   int check_ignore = 0;
996
997   void *real_addr_block1, *real_addr_block2, *real_addr_frag1, *real_addr_frag2;
998   int type_size = -1;
999   int offset1 = 0, offset2 = 0;
1000   int new_size1 = -1, new_size2 = -1;
1001   simgrid::mc::Type *new_type1 = nullptr, *new_type2 = nullptr;
1002
1003   bool match_pairs = false;
1004
1005   // This is the address of std_heap->heapinfo in the application process:
1006   void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
1007
1008   const malloc_info* heapinfos1 = snapshot1->read(
1009     remote((const malloc_info**)heapinfo_address), process_index);
1010   const malloc_info* heapinfos2 = snapshot2->read(
1011     remote((const malloc_info**)heapinfo_address), process_index);
1012
1013   malloc_info heapinfo_temp1, heapinfo_temp2;
1014
1015   simgrid::mc::HeapLocationPairs current;
1016   if (previous == nullptr) {
1017     previous = &current;
1018     match_pairs = true;
1019   }
1020
1021   // Get block number:
1022   block1 =
1023       ((char *) area1 -
1024        (char *) state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
1025   block2 =
1026       ((char *) area2 -
1027        (char *) state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
1028
1029   // If either block is a stack block:
1030   if (is_block_stack((int) block1) && is_block_stack((int) block2)) {
1031     previous->insert(simgrid::mc::makeHeapLocationPair(
1032       block1, -1, block2, -1));
1033     if (match_pairs)
1034       state.match_equals(previous);
1035     return 0;
1036   }
1037
1038   // If either block is not in the expected area of memory:
1039   if (((char *) area1 < (char *) state.std_heap_copy.heapbase)
1040       || (block1 > (ssize_t) state.processStates[0].heapsize) || (block1 < 1)
1041       || ((char *) area2 < (char *) state.std_heap_copy.heapbase)
1042       || (block2 > (ssize_t) state.processStates[1].heapsize) || (block2 < 1)) {
1043     return 1;
1044   }
1045
1046   // Process address of the block:
1047   real_addr_block1 = (ADDR2UINT(block1) - 1) * BLOCKSIZE +
1048                  (char *) state.std_heap_copy.heapbase;
1049   real_addr_block2 = (ADDR2UINT(block2) - 1) * BLOCKSIZE +
1050                  (char *) state.std_heap_copy.heapbase;
1051
1052   if (type) {
1053
1054     if (type->full_type)
1055       type = type->full_type;
1056
1057     // This assume that for "boring" types (volatile ...) byte_size is absent:
1058     while (type->byte_size == 0 && type->subtype != nullptr)
1059       type = type->subtype;
1060
1061     // Find type_size:
1062     if (type->type == DW_TAG_pointer_type
1063         || (type->type == DW_TAG_base_type && !type->name.empty()
1064             && type->name == "char"))
1065       type_size = -1;
1066     else
1067       type_size = type->byte_size;
1068
1069   }
1070
1071   mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
1072   mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
1073
1074   const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
1075     heap_region1, &heapinfo_temp1, &heapinfos1[block1], sizeof(malloc_info));
1076   const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
1077     heap_region2, &heapinfo_temp2, &heapinfos2[block2], sizeof(malloc_info));
1078
1079   if ((heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type==MMALLOC_TYPE_HEAPINFO)
1080     && (heapinfo2->type == MMALLOC_TYPE_FREE || heapinfo2->type ==MMALLOC_TYPE_HEAPINFO)) {
1081     /* Free block */
1082     if (match_pairs)
1083       state.match_equals(previous);
1084     return 0;
1085   }
1086
1087   if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED
1088     && heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
1089     /* Complete block */
1090
1091     // TODO, lookup variable type from block type as done for fragmented blocks
1092
1093     offset1 = (char *) area1 - (char *) real_addr_block1;
1094     offset2 = (char *) area2 - (char *) real_addr_block2;
1095
1096     if (state.equals_to1_(block1, 0).valid
1097         && state.equals_to2_(block2, 0).valid
1098         && state.blocksEqual(block1, block2)) {
1099       if (match_pairs)
1100         state.match_equals(previous);
1101       return 0;
1102     }
1103
1104     if (type_size != -1) {
1105       if (type_size != (ssize_t) heapinfo1->busy_block.busy_size
1106           && type_size != (ssize_t)   heapinfo2->busy_block.busy_size
1107           && (type->name.empty() || type->name == "struct s_smx_context")) {
1108         if (match_pairs)
1109           state.match_equals(previous);
1110         return -1;
1111       }
1112     }
1113
1114     if (heapinfo1->busy_block.size != heapinfo2->busy_block.size)
1115       return 1;
1116     if (heapinfo1->busy_block.busy_size != heapinfo2->busy_block.busy_size)
1117       return 1;
1118
1119     if (!previous->insert(simgrid::mc::makeHeapLocationPair(
1120         block1, -1, block2, -1)).second) {
1121       if (match_pairs)
1122         state.match_equals(previous);
1123       return 0;
1124     }
1125
1126     size = heapinfo1->busy_block.busy_size;
1127
1128     // Remember (basic) type inference.
1129     // The current data structure only allows us to do this for the whole block.
1130     if (type != nullptr && area1 == real_addr_block1)
1131       state.types1_(block1, 0) = type;
1132     if (type != nullptr && area2 == real_addr_block2)
1133       state.types2_(block2, 0) = type;
1134
1135     if (size <= 0) {
1136       if (match_pairs)
1137         state.match_equals(previous);
1138       return 0;
1139     }
1140
1141     frag1 = -1;
1142     frag2 = -1;
1143
1144     if (heapinfo1->busy_block.ignore > 0
1145         && heapinfo2->busy_block.ignore == heapinfo1->busy_block.ignore)
1146       check_ignore = heapinfo1->busy_block.ignore;
1147
1148   } else if ((heapinfo1->type > 0) && (heapinfo2->type > 0)) {      /* Fragmented block */
1149
1150     // Fragment number:
1151     frag1 =
1152         ((uintptr_t) (ADDR2UINT(area1) % (BLOCKSIZE))) >> heapinfo1->type;
1153     frag2 =
1154         ((uintptr_t) (ADDR2UINT(area2) % (BLOCKSIZE))) >> heapinfo2->type;
1155
1156     // Process address of the fragment:
1157     real_addr_frag1 =
1158         (void *) ((char *) real_addr_block1 +
1159                   (frag1 << heapinfo1->type));
1160     real_addr_frag2 =
1161         (void *) ((char *) real_addr_block2 +
1162                   (frag2 << heapinfo2->type));
1163
1164     // Check the size of the fragments against the size of the type:
1165     if (type_size != -1) {
1166       if (heapinfo1->busy_frag.frag_size[frag1] == -1
1167           || heapinfo2->busy_frag.frag_size[frag2] == -1) {
1168         if (match_pairs)
1169           state.match_equals(previous);
1170         return -1;
1171       }
1172       // ?
1173       if (type_size != heapinfo1->busy_frag.frag_size[frag1]
1174           || type_size != heapinfo2->busy_frag.frag_size[frag2]) {
1175         if (match_pairs)
1176           state.match_equals(previous);
1177         return -1;
1178       }
1179     }
1180
1181     // Check if the blocks are already matched together:
1182     if (state.equals_to1_(block1, frag1).valid
1183         && state.equals_to2_(block2, frag2).valid) {
1184       if (offset1==offset2 && state.fragmentsEqual(block1, frag1, block2, frag2)) {
1185         if (match_pairs)
1186           state.match_equals(previous);
1187         return 0;
1188       }
1189     }
1190     // Compare the size of both fragments:
1191     if (heapinfo1->busy_frag.frag_size[frag1] !=
1192         heapinfo2->busy_frag.frag_size[frag2]) {
1193       if (type_size == -1) {
1194         if (match_pairs)
1195           state.match_equals(previous);
1196         return -1;
1197       } else
1198         return 1;
1199     }
1200
1201     // Size of the fragment:
1202     size = heapinfo1->busy_frag.frag_size[frag1];
1203
1204     // Remember (basic) type inference.
1205     // The current data structure only allows us to do this for the whole fragment.
1206     if (type != nullptr && area1 == real_addr_frag1)
1207       state.types1_(block1, frag1) = type;
1208     if (type != nullptr && area2 == real_addr_frag2)
1209       state.types2_(block2, frag2) = type;
1210
1211     // The type of the variable is already known:
1212     if (type) {
1213       new_type1 = type;
1214       new_type2 = type;
1215     }
1216     // Type inference from the block type.
1217     else if (state.types1_(block1, frag1) != nullptr
1218              || state.types2_(block2, frag2) != nullptr) {
1219
1220       offset1 = (char *) area1 - (char *) real_addr_frag1;
1221       offset2 = (char *) area2 - (char *) real_addr_frag2;
1222
1223       if (state.types1_(block1, frag1) != nullptr
1224           && state.types2_(block2, frag2) != nullptr) {
1225         new_type1 =
1226             get_offset_type(real_addr_frag1, state.types1_(block1, frag1),
1227                             offset1, size, snapshot1, process_index);
1228         new_type2 =
1229             get_offset_type(real_addr_frag2, state.types2_(block2, frag2),
1230                             offset1, size, snapshot2, process_index);
1231       } else if (state.types1_(block1, frag1) != nullptr) {
1232         new_type1 =
1233             get_offset_type(real_addr_frag1, state.types1_(block1, frag1),
1234                             offset1, size, snapshot1, process_index);
1235         new_type2 =
1236             get_offset_type(real_addr_frag2, state.types1_(block1, frag1),
1237                             offset2, size, snapshot2, process_index);
1238       } else if (state.types2_(block2, frag2) != nullptr) {
1239         new_type1 =
1240             get_offset_type(real_addr_frag1, state.types2_(block2, frag2),
1241                             offset1, size, snapshot1, process_index);
1242         new_type2 =
1243             get_offset_type(real_addr_frag2, state.types2_(block2, frag2),
1244                             offset2, size, snapshot2, process_index);
1245       } else {
1246         if (match_pairs)
1247           state.match_equals(previous);
1248         return -1;
1249       }
1250
1251       if (new_type1 != nullptr && new_type2 != nullptr && new_type1 != new_type2) {
1252
1253         type = new_type1;
1254         while (type->byte_size == 0 && type->subtype != nullptr)
1255           type = type->subtype;
1256         new_size1 = type->byte_size;
1257
1258         type = new_type2;
1259         while (type->byte_size == 0 && type->subtype != nullptr)
1260           type = type->subtype;
1261         new_size2 = type->byte_size;
1262
1263       } else {
1264         if (match_pairs)
1265           state.match_equals(previous);
1266         return -1;
1267       }
1268     }
1269
1270     if (new_size1 > 0 && new_size1 == new_size2) {
1271       type = new_type1;
1272       size = new_size1;
1273     }
1274
1275     if (offset1 == 0 && offset2 == 0
1276       && !previous->insert(simgrid::mc::makeHeapLocationPair(
1277         block1, frag1, block2, frag2)).second) {
1278         if (match_pairs)
1279           state.match_equals(previous);
1280         return 0;
1281       }
1282
1283     if (size <= 0) {
1284       if (match_pairs)
1285         state.match_equals(previous);
1286       return 0;
1287     }
1288
1289     if ((heapinfo1->busy_frag.ignore[frag1] > 0)
1290         && (heapinfo2->busy_frag.ignore[frag2] ==
1291             heapinfo1->busy_frag.ignore[frag1]))
1292       check_ignore = heapinfo1->busy_frag.ignore[frag1];
1293
1294   } else
1295     return 1;
1296
1297
1298   /* Start comparison */
1299   if (type)
1300     res_compare =
1301         compare_heap_area_with_type(state, process_index, area1, area2, snapshot1, snapshot2,
1302                                     previous, type, size, check_ignore,
1303                                     pointer_level);
1304   else
1305     res_compare =
1306         compare_heap_area_without_type(state, process_index, area1, area2, snapshot1, snapshot2,
1307                                        previous, size, check_ignore);
1308
1309   if (res_compare == 1)
1310     return res_compare;
1311
1312   if (match_pairs)
1313     state.match_equals(previous);
1314   return 0;
1315 }
1316
1317 }
1318 }
1319
1320 /************************** Snapshot comparison *******************************/
1321 /******************************************************************************/
1322
1323 static int compare_areas_with_type(simgrid::mc::StateComparator& state,
1324                                    int process_index,
1325                                    void* real_area1, simgrid::mc::Snapshot* snapshot1, mc_mem_region_t region1,
1326                                    void* real_area2, simgrid::mc::Snapshot* snapshot2, mc_mem_region_t region2,
1327                                    simgrid::mc::Type* type, int pointer_level)
1328 {
1329   simgrid::mc::Process* process = &mc_model_checker->process();
1330
1331   simgrid::mc::Type* subtype;
1332   simgrid::mc::Type* subsubtype;
1333   int elm_size, i, res;
1334
1335   top:
1336   switch (type->type) {
1337   case DW_TAG_unspecified_type:
1338     return 1;
1339
1340   case DW_TAG_base_type:
1341   case DW_TAG_enumeration_type:
1342   case DW_TAG_union_type:
1343   {
1344     return MC_snapshot_region_memcmp(
1345       real_area1, region1, real_area2, region2,
1346       type->byte_size) != 0;
1347   }
1348   case DW_TAG_typedef:
1349   case DW_TAG_volatile_type:
1350   case DW_TAG_const_type:
1351     // Poor man's TCO:
1352     type = type->subtype;
1353     goto top;
1354   case DW_TAG_array_type:
1355     subtype = type->subtype;
1356     switch (subtype->type) {
1357     case DW_TAG_unspecified_type:
1358       return 1;
1359
1360     case DW_TAG_base_type:
1361     case DW_TAG_enumeration_type:
1362     case DW_TAG_pointer_type:
1363     case DW_TAG_reference_type:
1364     case DW_TAG_rvalue_reference_type:
1365     case DW_TAG_structure_type:
1366     case DW_TAG_class_type:
1367     case DW_TAG_union_type:
1368       if (subtype->full_type)
1369         subtype = subtype->full_type;
1370       elm_size = subtype->byte_size;
1371       break;
1372     case DW_TAG_const_type:
1373     case DW_TAG_typedef:
1374     case DW_TAG_volatile_type:
1375       subsubtype = subtype->subtype;
1376       if (subsubtype->full_type)
1377         subsubtype = subsubtype->full_type;
1378       elm_size = subsubtype->byte_size;
1379       break;
1380     default:
1381       return 0;
1382       break;
1383     }
1384     for (i = 0; i < type->element_count; i++) {
1385       size_t off = i * elm_size;
1386       res = compare_areas_with_type(state, process_index,
1387             (char*) real_area1 + off, snapshot1, region1,
1388             (char*) real_area2 + off, snapshot2, region2,
1389             type->subtype, pointer_level);
1390       if (res == 1)
1391         return res;
1392     }
1393     break;
1394   case DW_TAG_pointer_type:
1395   case DW_TAG_reference_type:
1396   case DW_TAG_rvalue_reference_type:
1397   {
1398     void* addr_pointed1 = MC_region_read_pointer(region1, real_area1);
1399     void* addr_pointed2 = MC_region_read_pointer(region2, real_area2);
1400
1401     if (type->subtype && type->subtype->type == DW_TAG_subroutine_type)
1402       return (addr_pointed1 != addr_pointed2);
1403     if (addr_pointed1 == nullptr && addr_pointed2 == nullptr)
1404       return 0;
1405     if (addr_pointed1 == nullptr || addr_pointed2 == nullptr)
1406       return 1;
1407     if (!state.compared_pointers.insert(
1408         std::make_pair(addr_pointed1, addr_pointed2)).second)
1409       return 0;
1410
1411     pointer_level++;
1412
1413       // Some cases are not handled here:
1414       // * the pointers lead to different areas (one to the heap, the other to the RW segment ...);
1415       // * a pointer leads to the read-only segment of the current object;
1416       // * a pointer lead to a different ELF object.
1417
1418       if (addr_pointed1 > process->heap_address
1419           && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)) {
1420         if (!
1421             (addr_pointed2 > process->heap_address
1422              && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)))
1423           return 1;
1424         // The pointers are both in the heap:
1425         return simgrid::mc::compare_heap_area(state,
1426           process_index, addr_pointed1, addr_pointed2, snapshot1,
1427           snapshot2, nullptr, type->subtype, pointer_level);
1428       }
1429
1430       // The pointers are both in the current object R/W segment:
1431       else if (region1->contain(simgrid::mc::remote(addr_pointed1))) {
1432         if (!region2->contain(simgrid::mc::remote(addr_pointed2)))
1433           return 1;
1434         if (!type->type_id)
1435           return (addr_pointed1 != addr_pointed2);
1436         else
1437           return compare_areas_with_type(state, process_index,
1438                                          addr_pointed1, snapshot1, region1,
1439                                          addr_pointed2, snapshot2, region2,
1440                                          type->subtype, pointer_level);
1441       }
1442
1443       // TODO, We do not handle very well the case where
1444       // it belongs to a different (non-heap) region from the current one.
1445
1446       else
1447         return (addr_pointed1 != addr_pointed2);
1448
1449     break;
1450   }
1451   case DW_TAG_structure_type:
1452   case DW_TAG_class_type:
1453     for(simgrid::mc::Member& member : type->members) {
1454       void *member1 = simgrid::dwarf::resolve_member(
1455         real_area1, type, &member, snapshot1, process_index);
1456       void *member2 = simgrid::dwarf::resolve_member(
1457         real_area2, type, &member, snapshot2, process_index);
1458       mc_mem_region_t subregion1 = mc_get_region_hinted(member1, snapshot1, process_index, region1);
1459       mc_mem_region_t subregion2 = mc_get_region_hinted(member2, snapshot2, process_index, region2);
1460       res =
1461           compare_areas_with_type(state, process_index,
1462                                   member1, snapshot1, subregion1,
1463                                   member2, snapshot2, subregion2,
1464                                   member.type, pointer_level);
1465       if (res == 1)
1466         return res;
1467     }
1468     break;
1469   case DW_TAG_subroutine_type:
1470     return -1;
1471     break;
1472   default:
1473     XBT_VERB("Unknown case: %d", type->type);
1474     break;
1475   }
1476
1477   return 0;
1478 }
1479
1480 static int compare_global_variables(
1481   simgrid::mc::StateComparator& state,
1482   simgrid::mc::ObjectInformation* object_info,
1483   int process_index,
1484   mc_mem_region_t r1, mc_mem_region_t r2,
1485   simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2)
1486 {
1487   xbt_assert(r1 && r2, "Missing region.");
1488
1489 #if HAVE_SMPI
1490   if (r1->storage_type() == simgrid::mc::StorageType::Privatized) {
1491     xbt_assert(process_index >= 0);
1492     if (r2->storage_type() != simgrid::mc::StorageType::Privatized)
1493       return 1;
1494
1495     size_t process_count = MC_smpi_process_count();
1496     xbt_assert(process_count == r1->privatized_data().size()
1497       && process_count == r2->privatized_data().size());
1498
1499     // Compare the global variables separately for each simulates process:
1500     for (size_t process_index = 0; process_index < process_count; process_index++) {
1501       if (compare_global_variables(state,
1502           object_info, process_index,
1503           &r1->privatized_data()[process_index],
1504           &r2->privatized_data()[process_index],
1505           snapshot1, snapshot2))
1506         return 1;
1507     }
1508     return 0;
1509   }
1510 #else
1511   xbt_assert(r1->storage_type() != simgrid::mc::StorageType::Privatized);
1512 #endif
1513   xbt_assert(r2->storage_type() != simgrid::mc::StorageType::Privatized);
1514
1515   std::vector<simgrid::mc::Variable>& variables = object_info->global_variables;
1516
1517   for (simgrid::mc::Variable& current_var : variables) {
1518
1519     // If the variable is not in this object, skip it:
1520     // We do not expect to find a pointer to something which is not reachable
1521     // by the global variables.
1522     if ((char *) current_var.address < (char *) object_info->start_rw
1523         || (char *) current_var.address > (char *) object_info->end_rw)
1524       continue;
1525
1526     simgrid::mc::Type* bvariable_type = current_var.type;
1527     int res = compare_areas_with_type(state, process_index,
1528                                 (char *) current_var.address, snapshot1, r1,
1529                                 (char *) current_var.address, snapshot2, r2,
1530                                 bvariable_type, 0);
1531     if (res == 1) {
1532       XBT_VERB("Global variable %s (%p) is different between snapshots",
1533                current_var.name.c_str(),
1534                (char *) current_var.address);
1535       return 1;
1536     }
1537
1538   }
1539
1540   return 0;
1541
1542 }
1543
1544 static int compare_local_variables(simgrid::mc::StateComparator& state,
1545                                    int process_index,
1546                                    simgrid::mc::Snapshot* snapshot1,
1547                                    simgrid::mc::Snapshot* snapshot2,
1548                                    mc_snapshot_stack_t stack1,
1549                                    mc_snapshot_stack_t stack2)
1550 {
1551   if (stack1->local_variables.size() != stack2->local_variables.size()) {
1552     XBT_VERB("Different number of local variables");
1553     return 1;
1554   }
1555
1556     unsigned int cursor = 0;
1557     local_variable_t current_var1, current_var2;
1558     int res;
1559     while (cursor < stack1->local_variables.size()) {
1560       current_var1 = &stack1->local_variables[cursor];
1561       current_var2 = &stack1->local_variables[cursor];
1562       if (current_var1->name != current_var2->name
1563           || current_var1->subprogram != current_var2->subprogram
1564           || current_var1->ip != current_var2->ip) {
1565         // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1566         XBT_VERB
1567             ("Different name of variable (%s - %s) "
1568              "or frame (%s - %s) or ip (%lu - %lu)",
1569              current_var1->name.c_str(),
1570              current_var2->name.c_str(),
1571              current_var1->subprogram->name.c_str(),
1572              current_var2->subprogram->name.c_str(),
1573              current_var1->ip, current_var2->ip);
1574         return 1;
1575       }
1576       // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1577
1578         simgrid::mc::Type* subtype = current_var1->type;
1579         res =
1580             compare_areas_with_type(state, process_index,
1581                                     current_var1->address, snapshot1, mc_get_snapshot_region(current_var1->address, snapshot1, process_index),
1582                                     current_var2->address, snapshot2, mc_get_snapshot_region(current_var2->address, snapshot2, process_index),
1583                                     subtype, 0);
1584
1585       if (res == 1) {
1586         // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1587         XBT_VERB
1588             ("Local variable %s (%p - %p) in frame %s "
1589              "is different between snapshots",
1590              current_var1->name.c_str(),
1591              current_var1->address,
1592              current_var2->address,
1593              current_var1->subprogram->name.c_str());
1594         return res;
1595       }
1596       cursor++;
1597     }
1598     return 0;
1599 }
1600
1601 namespace simgrid {
1602 namespace mc {
1603
1604 static std::unique_ptr<simgrid::mc::StateComparator> state_comparator;
1605
1606 int snapshot_compare(int num1, simgrid::mc::Snapshot* s1, int num2, simgrid::mc::Snapshot* s2)
1607 {
1608   // TODO, make this a field of ModelChecker or something similar
1609
1610   if (state_comparator == nullptr)
1611     state_comparator = std::unique_ptr<StateComparator>(new StateComparator());
1612   else
1613     state_comparator->clear();
1614
1615   simgrid::mc::Process* process = &mc_model_checker->process();
1616
1617   int errors = 0;
1618
1619   int hash_result = 0;
1620   if (_sg_mc_hash) {
1621     hash_result = (s1->hash != s2->hash);
1622     if (hash_result) {
1623       XBT_VERB("(%d - %d) Different hash: 0x%" PRIx64 "--0x%" PRIx64, num1, num2, s1->hash, s2->hash);
1624 #ifndef MC_DEBUG
1625       return 1;
1626 #endif
1627     } else
1628       XBT_VERB("(%d - %d) Same hash: 0x%" PRIx64, num1, num2, s1->hash);
1629   }
1630
1631   /* Compare enabled processes */
1632   if (s1->enabled_processes != s2->enabled_processes) {
1633     XBT_VERB("(%d - %d) Different amount of enabled processes", num1, num2);
1634     return 1;
1635   }
1636
1637   /* Compare size of stacks */
1638   int is_diff = 0;
1639   for (unsigned long i = 0; i < s1->stacks.size(); i++) {
1640     size_t size_used1 = s1->stack_sizes[i];
1641     size_t size_used2 = s2->stack_sizes[i];
1642     if (size_used1 != size_used2) {
1643 #ifdef MC_DEBUG
1644       XBT_DEBUG("(%d - %d) Different size used in stacks: %zu - %zu", num1, num2, size_used1, size_used2);
1645       errors++;
1646       is_diff = 1;
1647 #else
1648 #ifdef MC_VERBOSE
1649       XBT_VERB("(%d - %d) Different size used in stacks: %zu - %zu", num1, num2, size_used1, size_used2);
1650 #endif
1651       return 1;
1652 #endif
1653     }
1654   }
1655   if (is_diff) // do not proceed if there is any stacks that don't match
1656     return 1;
1657
1658   /* Init heap information used in heap comparison algorithm */
1659   xbt_mheap_t heap1 = (xbt_mheap_t)s1->read_bytes(
1660     alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1661     remote(process->heap_address),
1662     simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1663   xbt_mheap_t heap2 = (xbt_mheap_t)s2->read_bytes(
1664     alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1665     remote(process->heap_address),
1666     simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1667   int res_init = state_comparator->initHeapInformation(heap1, heap2, &s1->to_ignore, &s2->to_ignore);
1668
1669   if (res_init == -1) {
1670 #ifdef MC_DEBUG
1671     XBT_DEBUG("(%d - %d) Different heap information", num1, num2);
1672     errors++;
1673 #else
1674 #ifdef MC_VERBOSE
1675     XBT_VERB("(%d - %d) Different heap information", num1, num2);
1676 #endif
1677
1678     return 1;
1679 #endif
1680   }
1681
1682   /* Stacks comparison */
1683   int diff_local = 0;
1684   for (unsigned int cursor = 0; cursor < s1->stacks.size(); cursor++) {
1685     mc_snapshot_stack_t stack1 = &s1->stacks[cursor];
1686     mc_snapshot_stack_t stack2 = &s2->stacks[cursor];
1687
1688     if (stack1->process_index != stack2->process_index) {
1689       diff_local = 1;
1690       XBT_DEBUG("(%d - %d) Stacks with different process index (%i vs %i)", num1, num2,
1691         stack1->process_index, stack2->process_index);
1692     }
1693     else diff_local = compare_local_variables(*state_comparator,
1694       stack1->process_index, s1, s2, stack1, stack2);
1695     if (diff_local > 0) {
1696 #ifdef MC_DEBUG
1697       XBT_DEBUG("(%d - %d) Different local variables between stacks %d", num1,
1698                 num2, cursor + 1);
1699       errors++;
1700       is_diff = 1;
1701 #else
1702
1703 #ifdef MC_VERBOSE
1704       XBT_VERB("(%d - %d) Different local variables between stacks %d", num1,
1705                num2, cursor + 1);
1706 #endif
1707
1708       return 1;
1709 #endif
1710     }
1711   }
1712
1713   size_t regions_count = s1->snapshot_regions.size();
1714   // TODO, raise a difference instead?
1715   xbt_assert(regions_count == s2->snapshot_regions.size());
1716
1717   for (size_t k = 0; k != regions_count; ++k) {
1718     mc_mem_region_t region1 = s1->snapshot_regions[k].get();
1719     mc_mem_region_t region2 = s2->snapshot_regions[k].get();
1720
1721     // Preconditions:
1722     if (region1->region_type() != simgrid::mc::RegionType::Data)
1723       continue;
1724
1725     xbt_assert(region1->region_type() == region2->region_type());
1726     xbt_assert(region1->object_info() == region2->object_info());
1727     xbt_assert(region1->object_info());
1728
1729     std::string const& name = region1->object_info()->file_name;
1730
1731     /* Compare global variables */
1732     if (compare_global_variables(*state_comparator, region1->object_info(), simgrid::mc::ProcessIndexDisabled, region1,
1733                                  region2, s1, s2)) {
1734
1735 #ifdef MC_DEBUG
1736       XBT_DEBUG("(%d - %d) Different global variables in %s",
1737         num1, num2, name.c_str());
1738       errors++;
1739 #else
1740 #ifdef MC_VERBOSE
1741       XBT_VERB("(%d - %d) Different global variables in %s",
1742         num1, num2, name.c_str());
1743 #endif
1744
1745       return 1;
1746 #endif
1747     }
1748   }
1749
1750   /* Compare heap */
1751   if (simgrid::mc::mmalloc_compare_heap(*state_comparator, s1, s2) > 0) {
1752
1753 #ifdef MC_DEBUG
1754     XBT_DEBUG("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
1755     errors++;
1756 #else
1757
1758 #ifdef MC_VERBOSE
1759     XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
1760 #endif
1761     return 1;
1762 #endif
1763   }
1764
1765 #ifdef MC_VERBOSE
1766   if (errors || hash_result)
1767     XBT_VERB("(%d - %d) Difference found", num1, num2);
1768   else
1769     XBT_VERB("(%d - %d) No difference found", num1, num2);
1770 #endif
1771
1772 #if defined(MC_DEBUG) && defined(MC_VERBOSE)
1773   if (_sg_mc_hash) {
1774     // * false positive SHOULD be avoided.
1775     // * There MUST not be any false negative.
1776
1777     XBT_VERB("(%d - %d) State equality hash test is %s %s", num1, num2,
1778              (hash_result != 0) == (errors != 0) ? "true" : "false",
1779              !hash_result ? "positive" : "negative");
1780   }
1781 #endif
1782
1783   return errors > 0 || hash_result;
1784 }
1785
1786 }
1787 }