Logo AND Algorithmique Numérique Distribuée

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