Logo AND Algorithmique Numérique Distribuée

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