Logo AND Algorithmique Numérique Distribuée

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