Logo AND Algorithmique Numérique Distribuée

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