Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d2a30019e36c4c2c865f2db373c4497d177b074a
[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 <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 ProcessComparisonState;
52 struct StateComparator;
53
54 static int compare_heap_area(
55   int process_index, const void *area1, const void* area2,
56   Snapshot* snapshot1, Snapshot* snapshot2,
57   xbt_dynar_t previous, Type* type, int pointer_level);
58
59 static void reset_heap_information(void);
60
61 }
62 }
63
64 using simgrid::mc::remote;
65
66 /*********************************** Heap comparison ***********************************/
67 /***************************************************************************************/
68
69 namespace simgrid {
70 namespace mc {
71
72 struct HeapLocation {
73   int block = 0;
74   int fragment = 0;
75   HeapLocation() {}
76   HeapLocation(int block, int fragment = 0) : block(block), fragment(fragment) {}
77 };
78
79 typedef std::array<HeapLocation, 2> HeapLocationPair;
80
81 struct HeapArea {
82   bool valid = false;
83   int block = 0;
84   int fragment = 0;
85
86   HeapArea() {}
87   HeapArea(int block, int fragment = 0)
88     : valid(true), block(block), fragment(fragment) {}
89 };
90
91 struct ProcessComparisonState {
92   std::vector<simgrid::mc::IgnoredHeapRegion>* to_ignore = nullptr;
93   std::vector<HeapArea> equals_to;
94   std::vector<simgrid::mc::Type*> types;
95   std::size_t heapsize = 0;
96
97   void initHeapInformation(xbt_mheap_t heap,
98                           std::vector<simgrid::mc::IgnoredHeapRegion>* i);
99 };
100
101 struct StateComparator {
102   s_xbt_mheap_t std_heap_copy;
103   std::size_t heaplimit;
104   std::array<ProcessComparisonState, 2> processStates;
105
106   int initHeapInformation(
107     xbt_mheap_t heap1, xbt_mheap_t heap2,
108     std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
109     std::vector<simgrid::mc::IgnoredHeapRegion>* i2);
110
111   HeapArea& equals_to1_(std::size_t i, std::size_t j)
112   {
113     return processStates[0].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
114   }
115   HeapArea& equals_to2_(std::size_t i, std::size_t j)
116   {
117     return processStates[1].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
118   }
119   Type*& types1_(std::size_t i, std::size_t j)
120   {
121     return processStates[0].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
122   }
123   Type*& types2_(std::size_t i, std::size_t j)
124   {
125     return processStates[1].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
126   }
127
128   HeapArea const& equals_to1_(std::size_t i, std::size_t j) const
129   {
130     return processStates[0].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
131   }
132   HeapArea const& equals_to2_(std::size_t i, std::size_t j) const
133   {
134     return processStates[1].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
135   }
136   Type* const& types1_(std::size_t i, std::size_t j) const
137   {
138     return processStates[0].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
139   }
140   Type* const& types2_(std::size_t i, std::size_t j) const
141   {
142     return processStates[1].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
143   }
144
145   /** Check whether two blocks are known to be matching
146    *
147    *  @param state  State used
148    *  @param b1     Block of state 1
149    *  @param b2     Block of state 2
150    *  @return       if the blocks are known to be matching
151    */
152   bool blocksEqual(int b1, int b2) const
153   {
154     return this->equals_to1_(b1, 0).block == b2
155         && this->equals_to2_(b2, 0).block == b1;
156   }
157
158   /** Check whether two fragments are known to be matching
159    *
160    *  @param state  State used
161    *  @param b1     Block of state 1
162    *  @param f1     Fragment of state 1
163    *  @param b2     Block of state 2
164    *  @param f2     Fragment of state 2
165    *  @return       if the fragments are known to be matching
166    */
167   int fragmentsEqual(int b1, int f1, int b2, int f2) const
168   {
169     return this->equals_to1_(b1, f1).block == b2
170         && this->equals_to1_(b1, f1).fragment == f2
171         && this->equals_to2_(b2, f2).block == b1
172         && this->equals_to2_(b2, f2).fragment == f1;
173   }
174
175   void match_equals(xbt_dynar_t list);
176 };
177
178 }
179 }
180
181 // TODO, make this a field of ModelChecker or something similar
182 static std::unique_ptr<simgrid::mc::StateComparator> mc_diff_info;
183
184 /************************************************************************************/
185
186 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1,
187                                  int block2, int fragment2)
188 {
189
190   unsigned int cursor = 0;
191   simgrid::mc::HeapLocationPair* current_pair;
192   xbt_dynar_foreach(list, cursor, current_pair)
193     if ((*current_pair)[0].block == block1
194         && (*current_pair)[1].block == block2
195         && (*current_pair)[0].fragment == fragment1
196         && (*current_pair)[1].fragment == fragment2)
197       return 0;
198   return 1;
199 }
200
201 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1,
202                               int block2, int fragment2)
203 {
204   if (!is_new_heap_area_pair(list, block1, fragment1, block2, fragment2))
205     return 0;
206   simgrid::mc::HeapLocationPair* pair = xbt_new0(simgrid::mc::HeapLocationPair, 1);
207   (*pair)[0].block = block1;
208   (*pair)[0].fragment = fragment1;
209   (*pair)[1].block = block2;
210   (*pair)[1].fragment = fragment2;
211   xbt_dynar_push(list, &pair);
212   return 1;
213 }
214
215 static ssize_t heap_comparison_ignore_size(
216   std::vector<simgrid::mc::IgnoredHeapRegion>* ignore_list,
217   const void *address)
218 {
219   int start = 0;
220   int end = ignore_list->size() - 1;
221
222   while (start <= end) {
223     unsigned int cursor = (start + end) / 2;
224     simgrid::mc::IgnoredHeapRegion const& region = (*ignore_list)[cursor];
225     if (region.address == address)
226       return region.size;
227     if (region.address < address)
228       start = cursor + 1;
229     if (region.address > address)
230       end = cursor - 1;
231   }
232
233   return -1;
234 }
235
236 static bool is_stack(const void *address)
237 {
238   for (auto const& stack : mc_model_checker->process().stack_areas())
239     if (address == stack.address)
240       return true;
241   return false;
242 }
243
244 // TODO, this should depend on the snapshot?
245 static bool is_block_stack(int block)
246 {
247   for (auto const& stack : mc_model_checker->process().stack_areas())
248     if (block == stack.block)
249       return true;
250   return false;
251 }
252
253 namespace simgrid {
254 namespace mc {
255
256 void StateComparator::match_equals(xbt_dynar_t list)
257 {
258   unsigned int cursor = 0;
259   simgrid::mc::HeapLocationPair* current_pair;
260
261   xbt_dynar_foreach(list, cursor, current_pair) {
262     if ((*current_pair)[0].fragment != -1) {
263       this->equals_to1_((*current_pair)[0].block, (*current_pair)[0].fragment) =
264           simgrid::mc::HeapArea((*current_pair)[1].block, (*current_pair)[1].fragment);
265       this->equals_to2_((*current_pair)[1].block, (*current_pair)[1].fragment) =
266           simgrid::mc::HeapArea((*current_pair)[0].block, (*current_pair)[0].fragment);
267     } else {
268       this->equals_to1_((*current_pair)[0].block, 0) =
269           simgrid::mc::HeapArea((*current_pair)[1].block, (*current_pair)[1].fragment);
270       this->equals_to2_((*current_pair)[1].block, 0) =
271           simgrid::mc::HeapArea((*current_pair)[0].block, (*current_pair)[0].fragment);
272     }
273   }
274 }
275
276 void ProcessComparisonState::initHeapInformation(xbt_mheap_t heap,
277                         std::vector<simgrid::mc::IgnoredHeapRegion>* i)
278 {
279   auto heaplimit = ((struct mdesc *) heap)->heaplimit;
280   this->heapsize = ((struct mdesc *) heap)->heapsize;
281   this->to_ignore = i;
282   this->equals_to.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, HeapArea());
283   this->types.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, nullptr);
284 }
285
286 int StateComparator::initHeapInformation(xbt_mheap_t heap1, xbt_mheap_t heap2,
287                           std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
288                           std::vector<simgrid::mc::IgnoredHeapRegion>* i2)
289 {
290   if ((((struct mdesc *) heap1)->heaplimit !=
291        ((struct mdesc *) heap2)->heaplimit)
292       ||
293       ((((struct mdesc *) heap1)->heapsize !=
294         ((struct mdesc *) heap2)->heapsize)))
295     return -1;
296   this->heaplimit = ((struct mdesc *) heap1)->heaplimit;
297   this->std_heap_copy = *mc_model_checker->process().get_heap();
298   this->processStates[0].initHeapInformation(heap1, i1);
299   this->processStates[1].initHeapInformation(heap2, i2);
300   return 0;
301 }
302
303 static
304 int init_heap_information(xbt_mheap_t heap1, xbt_mheap_t heap2,
305                           std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
306                           std::vector<simgrid::mc::IgnoredHeapRegion>* i2)
307 {
308   if (mc_diff_info == nullptr)
309     mc_diff_info = std::unique_ptr<StateComparator>(new StateComparator());
310   return mc_diff_info->initHeapInformation(heap1, heap2, i1, i2);
311 }
312
313 static inline
314 void reset_heap_information()
315 {
316
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(simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2)
331 {
332   simgrid::mc::Process* process = &mc_model_checker->process();
333   simgrid::mc::StateComparator *state = mc_diff_info.get();
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 =
406             compare_heap_area(simgrid::mc::ProcessIndexMissing, 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 =
441             compare_heap_area(simgrid::mc::ProcessIndexMissing, 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 =
490               compare_heap_area(simgrid::mc::ProcessIndexMissing, 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 =
536                 compare_heap_area(simgrid::mc::ProcessIndexMissing, addr_frag1, addr_frag2, snapshot2, snapshot2,
537                                   nullptr, nullptr, 0);
538
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   xbt_dynar_t 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 =
696             compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
697                               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   xbt_dynar_t 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(process_index, addr_pointed1, addr_pointed2, snapshot1,
858                                  snapshot2, previous, type->subtype,
859                                  pointer_level);
860       else
861         return (addr_pointed1 != addr_pointed2);
862     }
863     for (size_t i = 0; i < (area_size / sizeof(void *)); i++) {
864       addr_pointed1 = snapshot1->read(
865         remote((void**)((char*) real_area1 + i * sizeof(void *))),
866         process_index);
867       addr_pointed2 = snapshot2->read(
868         remote((void**)((char*) real_area2 + i * sizeof(void *))),
869         process_index);
870       if (addr_pointed1 > state->std_heap_copy.heapbase
871           && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
872           && addr_pointed2 > state->std_heap_copy.heapbase
873           && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
874         res =
875             compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
876                               snapshot2, previous, type->subtype,
877                               pointer_level);
878       else
879         res = (addr_pointed1 != addr_pointed2);
880       if (res == 1)
881         return res;
882     }
883     return 0;
884
885   case DW_TAG_structure_type:
886   case DW_TAG_class_type:
887     if (type->full_type)
888       type = type->full_type;
889     if (area_size != -1 && type->byte_size != area_size) {
890       if (area_size <= type->byte_size || area_size % type->byte_size != 0)
891         return -1;
892       for (size_t i = 0; i < (size_t)(area_size / type->byte_size); i++) {
893         int res = compare_heap_area_with_type(state, process_index,
894                     (char *) real_area1 + i * type->byte_size,
895                     (char *) real_area2 + i * type->byte_size,
896                     snapshot1, snapshot2, previous, type, -1,
897                     check_ignore, 0);
898         if (res == 1)
899           return res;
900       }
901     } else {
902       for(simgrid::mc::Member& member : type->members) {
903         // TODO, optimize this? (for the offset case)
904         void *real_member1 = simgrid::dwarf::resolve_member(
905           real_area1, type, &member, (simgrid::mc::AddressSpace*) snapshot1, process_index);
906         void *real_member2 = simgrid::dwarf::resolve_member(
907             real_area2, type, &member, (simgrid::mc::AddressSpace*) snapshot2, process_index);
908         int res = compare_heap_area_with_type(
909                     state, process_index, real_member1, real_member2,
910                     snapshot1, snapshot2,
911                     previous, member.type, -1,
912                     check_ignore, 0);
913         if (res == 1)
914           return res;
915       }
916     }
917     return 0;
918
919   case DW_TAG_union_type:
920     return compare_heap_area_without_type(state, process_index, real_area1, real_area2,
921                                           snapshot1, snapshot2, previous,
922                                           type->byte_size, check_ignore);
923     return 0;
924
925   default:
926     return 0;
927   }
928
929   xbt_die("Unreachable");
930 }
931
932 /** Infer the type of a part of the block from the type of the block
933  *
934  * TODO, handle DW_TAG_array_type as well as arrays of the object ((*p)[5], p[5])
935  *
936  * TODO, handle subfields ((*p).bar.foo, (*p)[5].bar…)
937  *
938  * @param  type_id            DWARF type ID of the root address
939  * @param  area_size
940  * @return                    DWARF type ID for given offset
941  */
942 static simgrid::mc::Type* get_offset_type(void *real_base_address, simgrid::mc::Type* type,
943                                  int offset, int area_size,
944                                  simgrid::mc::Snapshot* snapshot, int process_index)
945 {
946
947   // Beginning of the block, the infered variable type if the type of the block:
948   if (offset == 0)
949     return type;
950
951   switch (type->type) {
952
953   case DW_TAG_structure_type:
954   case DW_TAG_class_type:
955     if (type->full_type)
956       type = type->full_type;
957     if (area_size != -1 && type->byte_size != area_size) {
958       if (area_size > type->byte_size && area_size % type->byte_size == 0)
959         return type;
960       else
961         return nullptr;
962     }
963
964     for(simgrid::mc::Member& member : type->members) {
965       if (member.has_offset_location()) {
966         // We have the offset, use it directly (shortcut):
967         if (member.offset() == offset)
968           return member.type;
969       } else {
970         void *real_member = simgrid::dwarf::resolve_member(
971           real_base_address, type, &member, snapshot, process_index);
972         if ((char*) real_member - (char *) real_base_address == offset)
973           return member.type;
974       }
975     }
976     return nullptr;
977
978   default:
979     /* FIXME : other cases ? */
980     return nullptr;
981
982   }
983 }
984
985 /**
986  *
987  * @param area1          Process address for state 1
988  * @param area2          Process address for state 2
989  * @param snapshot1      Snapshot of state 1
990  * @param snapshot2      Snapshot of state 2
991  * @param previous       Pairs of blocks already compared on the current path (or nullptr)
992  * @param type_id        Type of variable
993  * @param pointer_level
994  * @return 0 (same), 1 (different), -1
995  */
996 static
997 int compare_heap_area(int process_index, const void *area1, const void *area2, simgrid::mc::Snapshot* snapshot1,
998                       simgrid::mc::Snapshot* snapshot2, xbt_dynar_t previous,
999                       simgrid::mc::Type* type, int pointer_level)
1000 {
1001   simgrid::mc::Process* process = &mc_model_checker->process();
1002
1003   simgrid::mc::StateComparator *state = mc_diff_info.get();
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   int match_pairs = 0;
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   if (previous == nullptr) {
1029     previous = xbt_dynar_new(sizeof(simgrid::mc::HeapLocationPair*), [](void *d) {
1030       xbt_free((simgrid::mc::HeapLocationPair*) * (void **) d); });
1031     match_pairs = 1;
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     add_heap_area_pair(previous, block1, -1, block2, -1);
1045     if (match_pairs) {
1046       state->match_equals(previous);
1047       xbt_dynar_free(&previous);
1048     }
1049     return 0;
1050   }
1051
1052   // If either block is not in the expected area of memory:
1053   if (((char *) area1 < (char *) state->std_heap_copy.heapbase)
1054       || (block1 > (ssize_t) state->processStates[0].heapsize) || (block1 < 1)
1055       || ((char *) area2 < (char *) state->std_heap_copy.heapbase)
1056       || (block2 > (ssize_t) state->processStates[1].heapsize) || (block2 < 1)) {
1057     if (match_pairs)
1058       xbt_dynar_free(&previous);
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       xbt_dynar_free(&previous);
1101     }
1102     return 0;
1103   }
1104
1105   if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED
1106     && heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
1107     /* Complete block */
1108
1109     // TODO, lookup variable type from block type as done for fragmented blocks
1110
1111     offset1 = (char *) area1 - (char *) real_addr_block1;
1112     offset2 = (char *) area2 - (char *) real_addr_block2;
1113
1114     if (state->equals_to1_(block1, 0).valid
1115         && state->equals_to2_(block2, 0).valid
1116         && state->blocksEqual(block1, block2)) {
1117       if (match_pairs) {
1118         state->match_equals(previous);
1119         xbt_dynar_free(&previous);
1120       }
1121       return 0;
1122     }
1123
1124     if (type_size != -1) {
1125       if (type_size != (ssize_t) heapinfo1->busy_block.busy_size
1126           && type_size != (ssize_t)   heapinfo2->busy_block.busy_size
1127           && (type->name.empty() || type->name == "struct s_smx_context")) {
1128         if (match_pairs) {
1129           state->match_equals(previous);
1130           xbt_dynar_free(&previous);
1131         }
1132         return -1;
1133       }
1134     }
1135
1136     if (heapinfo1->busy_block.size != heapinfo2->busy_block.size) {
1137       if (match_pairs)
1138         xbt_dynar_free(&previous);
1139       return 1;
1140     }
1141
1142     if (heapinfo1->busy_block.busy_size != heapinfo2->busy_block.busy_size) {
1143       if (match_pairs)
1144         xbt_dynar_free(&previous);
1145       return 1;
1146     }
1147
1148     if (!add_heap_area_pair(previous, block1, -1, block2, -1)) {
1149       if (match_pairs) {
1150         state->match_equals(previous);
1151         xbt_dynar_free(&previous);
1152       }
1153       return 0;
1154     }
1155
1156     size = heapinfo1->busy_block.busy_size;
1157
1158     // Remember (basic) type inference.
1159     // The current data structure only allows us to do this for the whole block.
1160     if (type != nullptr && area1 == real_addr_block1)
1161       state->types1_(block1, 0) = type;
1162     if (type != nullptr && area2 == real_addr_block2)
1163       state->types2_(block2, 0) = type;
1164
1165     if (size <= 0) {
1166       if (match_pairs) {
1167         state->match_equals(previous);
1168         xbt_dynar_free(&previous);
1169       }
1170       return 0;
1171     }
1172
1173     frag1 = -1;
1174     frag2 = -1;
1175
1176     if (heapinfo1->busy_block.ignore > 0
1177         && heapinfo2->busy_block.ignore == heapinfo1->busy_block.ignore)
1178       check_ignore = heapinfo1->busy_block.ignore;
1179
1180   } else if ((heapinfo1->type > 0) && (heapinfo2->type > 0)) {      /* Fragmented block */
1181
1182     // Fragment number:
1183     frag1 =
1184         ((uintptr_t) (ADDR2UINT(area1) % (BLOCKSIZE))) >> heapinfo1->type;
1185     frag2 =
1186         ((uintptr_t) (ADDR2UINT(area2) % (BLOCKSIZE))) >> heapinfo2->type;
1187
1188     // Process address of the fragment:
1189     real_addr_frag1 =
1190         (void *) ((char *) real_addr_block1 +
1191                   (frag1 << heapinfo1->type));
1192     real_addr_frag2 =
1193         (void *) ((char *) real_addr_block2 +
1194                   (frag2 << heapinfo2->type));
1195
1196     // Check the size of the fragments against the size of the type:
1197     if (type_size != -1) {
1198       if (heapinfo1->busy_frag.frag_size[frag1] == -1
1199           || heapinfo2->busy_frag.frag_size[frag2] == -1) {
1200         if (match_pairs) {
1201           state->match_equals(previous);
1202           xbt_dynar_free(&previous);
1203         }
1204         return -1;
1205       }
1206       // ?
1207       if (type_size != heapinfo1->busy_frag.frag_size[frag1]
1208           || type_size != heapinfo2->busy_frag.frag_size[frag2]) {
1209         if (match_pairs) {
1210           state->match_equals(previous);
1211           xbt_dynar_free(&previous);
1212         }
1213         return -1;
1214       }
1215     }
1216
1217     // Check if the blocks are already matched together:
1218     if (state->equals_to1_(block1, frag1).valid
1219         && state->equals_to2_(block2, frag2).valid) {
1220       if (offset1==offset2 && state->fragmentsEqual(block1, frag1, block2, frag2)) {
1221         if (match_pairs) {
1222           state->match_equals(previous);
1223           xbt_dynar_free(&previous);
1224         }
1225         return 0;
1226       }
1227     }
1228     // Compare the size of both fragments:
1229     if (heapinfo1->busy_frag.frag_size[frag1] !=
1230         heapinfo2->busy_frag.frag_size[frag2]) {
1231       if (type_size == -1) {
1232         if (match_pairs) {
1233           state->match_equals(previous);
1234           xbt_dynar_free(&previous);
1235         }
1236         return -1;
1237       } else {
1238         if (match_pairs)
1239           xbt_dynar_free(&previous);
1240         return 1;
1241       }
1242     }
1243
1244     // Size of the fragment:
1245     size = heapinfo1->busy_frag.frag_size[frag1];
1246
1247     // Remember (basic) type inference.
1248     // The current data structure only allows us to do this for the whole fragment.
1249     if (type != nullptr && area1 == real_addr_frag1)
1250       state->types1_(block1, frag1) = type;
1251     if (type != nullptr && area2 == real_addr_frag2)
1252       state->types2_(block2, frag2) = type;
1253
1254     // The type of the variable is already known:
1255     if (type) {
1256       new_type1 = type;
1257       new_type2 = type;
1258     }
1259     // Type inference from the block type.
1260     else if (state->types1_(block1, frag1) != nullptr
1261              || state->types2_(block2, frag2) != nullptr) {
1262
1263       offset1 = (char *) area1 - (char *) real_addr_frag1;
1264       offset2 = (char *) area2 - (char *) real_addr_frag2;
1265
1266       if (state->types1_(block1, frag1) != nullptr
1267           && state->types2_(block2, frag2) != nullptr) {
1268         new_type1 =
1269             get_offset_type(real_addr_frag1, state->types1_(block1, frag1),
1270                             offset1, size, snapshot1, process_index);
1271         new_type2 =
1272             get_offset_type(real_addr_frag2, state->types2_(block2, frag2),
1273                             offset1, size, snapshot2, process_index);
1274       } else if (state->types1_(block1, frag1) != nullptr) {
1275         new_type1 =
1276             get_offset_type(real_addr_frag1, state->types1_(block1, frag1),
1277                             offset1, size, snapshot1, process_index);
1278         new_type2 =
1279             get_offset_type(real_addr_frag2, state->types1_(block1, frag1),
1280                             offset2, size, snapshot2, process_index);
1281       } else if (state->types2_(block2, frag2) != nullptr) {
1282         new_type1 =
1283             get_offset_type(real_addr_frag1, state->types2_(block2, frag2),
1284                             offset1, size, snapshot1, process_index);
1285         new_type2 =
1286             get_offset_type(real_addr_frag2, state->types2_(block2, frag2),
1287                             offset2, size, snapshot2, process_index);
1288       } else {
1289         if (match_pairs) {
1290           state->match_equals(previous);
1291           xbt_dynar_free(&previous);
1292         }
1293         return -1;
1294       }
1295
1296       if (new_type1 != nullptr && new_type2 != nullptr && new_type1 != new_type2) {
1297
1298         type = new_type1;
1299         while (type->byte_size == 0 && type->subtype != nullptr)
1300           type = type->subtype;
1301         new_size1 = type->byte_size;
1302
1303         type = new_type2;
1304         while (type->byte_size == 0 && type->subtype != nullptr)
1305           type = type->subtype;
1306         new_size2 = type->byte_size;
1307
1308       } else {
1309         if (match_pairs) {
1310           state->match_equals(previous);
1311           xbt_dynar_free(&previous);
1312         }
1313         return -1;
1314       }
1315     }
1316
1317     if (new_size1 > 0 && new_size1 == new_size2) {
1318       type = new_type1;
1319       size = new_size1;
1320     }
1321
1322     if (offset1 == 0 && offset2 == 0
1323       && !add_heap_area_pair(previous, block1, frag1, block2, frag2)) {
1324         if (match_pairs) {
1325           state->match_equals(previous);
1326           xbt_dynar_free(&previous);
1327         }
1328         return 0;
1329       }
1330
1331     if (size <= 0) {
1332       if (match_pairs) {
1333         state->match_equals(previous);
1334         xbt_dynar_free(&previous);
1335       }
1336       return 0;
1337     }
1338
1339     if ((heapinfo1->busy_frag.ignore[frag1] > 0)
1340         && (heapinfo2->busy_frag.ignore[frag2] ==
1341             heapinfo1->busy_frag.ignore[frag1]))
1342       check_ignore = heapinfo1->busy_frag.ignore[frag1];
1343
1344   } else {
1345     if (match_pairs)
1346       xbt_dynar_free(&previous);
1347     return 1;
1348   }
1349
1350
1351   /* Start comparison */
1352   if (type)
1353     res_compare =
1354         compare_heap_area_with_type(state, process_index, area1, area2, snapshot1, snapshot2,
1355                                     previous, type, size, check_ignore,
1356                                     pointer_level);
1357   else
1358     res_compare =
1359         compare_heap_area_without_type(state, process_index, area1, area2, snapshot1, snapshot2,
1360                                        previous, size, check_ignore);
1361
1362   if (res_compare == 1) {
1363     if (match_pairs)
1364       xbt_dynar_free(&previous);
1365     return res_compare;
1366   }
1367
1368   if (match_pairs) {
1369     state->match_equals(previous);
1370     xbt_dynar_free(&previous);
1371   }
1372
1373   return 0;
1374 }
1375
1376 }
1377 }
1378
1379 namespace simgrid {
1380 namespace mc {
1381
1382 /** A hash which works with more stuff
1383  *
1384  *  It can hash pairs: the standard hash currently doesn't include this.
1385  */
1386 template<class X> struct hash : public std::hash<X> {};
1387
1388 template<class X, class Y>
1389 struct hash<std::pair<X,Y>> {
1390   std::size_t operator()(std::pair<X,Y>const& x) const
1391   {
1392     struct hash<X> h1;
1393     struct hash<X> h2;
1394     return h1(x.first) ^ h2(x.second);
1395   }
1396 };
1397
1398 struct ComparisonState {
1399   std::unordered_set<std::pair<void*, void*>, hash<std::pair<void*, void*>>> compared_pointers;
1400 };
1401
1402 }
1403 }
1404
1405 using simgrid::mc::ComparisonState;
1406
1407 /************************** Snapshot comparison *******************************/
1408 /******************************************************************************/
1409
1410 static int compare_areas_with_type(ComparisonState& state,
1411                                    int process_index,
1412                                    void* real_area1, simgrid::mc::Snapshot* snapshot1, mc_mem_region_t region1,
1413                                    void* real_area2, simgrid::mc::Snapshot* snapshot2, mc_mem_region_t region2,
1414                                    simgrid::mc::Type* type, int pointer_level)
1415 {
1416   simgrid::mc::Process* process = &mc_model_checker->process();
1417
1418   simgrid::mc::Type* subtype;
1419   simgrid::mc::Type* subsubtype;
1420   int elm_size, i, res;
1421
1422   top:
1423   switch (type->type) {
1424   case DW_TAG_unspecified_type:
1425     return 1;
1426
1427   case DW_TAG_base_type:
1428   case DW_TAG_enumeration_type:
1429   case DW_TAG_union_type:
1430   {
1431     return MC_snapshot_region_memcmp(
1432       real_area1, region1, real_area2, region2,
1433       type->byte_size) != 0;
1434   }
1435   case DW_TAG_typedef:
1436   case DW_TAG_volatile_type:
1437   case DW_TAG_const_type:
1438     // Poor man's TCO:
1439     type = type->subtype;
1440     goto top;
1441   case DW_TAG_array_type:
1442     subtype = type->subtype;
1443     switch (subtype->type) {
1444     case DW_TAG_unspecified_type:
1445       return 1;
1446
1447     case DW_TAG_base_type:
1448     case DW_TAG_enumeration_type:
1449     case DW_TAG_pointer_type:
1450     case DW_TAG_reference_type:
1451     case DW_TAG_rvalue_reference_type:
1452     case DW_TAG_structure_type:
1453     case DW_TAG_class_type:
1454     case DW_TAG_union_type:
1455       if (subtype->full_type)
1456         subtype = subtype->full_type;
1457       elm_size = subtype->byte_size;
1458       break;
1459     case DW_TAG_const_type:
1460     case DW_TAG_typedef:
1461     case DW_TAG_volatile_type:
1462       subsubtype = subtype->subtype;
1463       if (subsubtype->full_type)
1464         subsubtype = subsubtype->full_type;
1465       elm_size = subsubtype->byte_size;
1466       break;
1467     default:
1468       return 0;
1469       break;
1470     }
1471     for (i = 0; i < type->element_count; i++) {
1472       size_t off = i * elm_size;
1473       res = compare_areas_with_type(state, process_index,
1474             (char*) real_area1 + off, snapshot1, region1,
1475             (char*) real_area2 + off, snapshot2, region2,
1476             type->subtype, pointer_level);
1477       if (res == 1)
1478         return res;
1479     }
1480     break;
1481   case DW_TAG_pointer_type:
1482   case DW_TAG_reference_type:
1483   case DW_TAG_rvalue_reference_type:
1484   {
1485     void* addr_pointed1 = MC_region_read_pointer(region1, real_area1);
1486     void* addr_pointed2 = MC_region_read_pointer(region2, real_area2);
1487
1488     if (type->subtype && type->subtype->type == DW_TAG_subroutine_type)
1489       return (addr_pointed1 != addr_pointed2);
1490     if (addr_pointed1 == nullptr && addr_pointed2 == nullptr)
1491       return 0;
1492     if (addr_pointed1 == nullptr || addr_pointed2 == nullptr)
1493       return 1;
1494     if (!state.compared_pointers.insert(
1495         std::make_pair(addr_pointed1, addr_pointed2)).second)
1496       return 0;
1497
1498     pointer_level++;
1499
1500       // Some cases are not handled here:
1501       // * the pointers lead to different areas (one to the heap, the other to the RW segment ...);
1502       // * a pointer leads to the read-only segment of the current object;
1503       // * a pointer lead to a different ELF object.
1504
1505       if (addr_pointed1 > process->heap_address
1506           && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)) {
1507         if (!
1508             (addr_pointed2 > process->heap_address
1509              && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)))
1510           return 1;
1511         // The pointers are both in the heap:
1512         return simgrid::mc::compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
1513                                  snapshot2, nullptr, type->subtype, pointer_level);
1514       }
1515
1516       // The pointers are both in the current object R/W segment:
1517       else if (region1->contain(simgrid::mc::remote(addr_pointed1))) {
1518         if (!region2->contain(simgrid::mc::remote(addr_pointed2)))
1519           return 1;
1520         if (!type->type_id)
1521           return (addr_pointed1 != addr_pointed2);
1522         else
1523           return compare_areas_with_type(state, process_index,
1524                                          addr_pointed1, snapshot1, region1,
1525                                          addr_pointed2, snapshot2, region2,
1526                                          type->subtype, pointer_level);
1527       }
1528
1529       // TODO, We do not handle very well the case where
1530       // it belongs to a different (non-heap) region from the current one.
1531
1532       else
1533         return (addr_pointed1 != addr_pointed2);
1534
1535     break;
1536   }
1537   case DW_TAG_structure_type:
1538   case DW_TAG_class_type:
1539     for(simgrid::mc::Member& member : type->members) {
1540       void *member1 = simgrid::dwarf::resolve_member(
1541         real_area1, type, &member, snapshot1, process_index);
1542       void *member2 = simgrid::dwarf::resolve_member(
1543         real_area2, type, &member, snapshot2, process_index);
1544       mc_mem_region_t subregion1 = mc_get_region_hinted(member1, snapshot1, process_index, region1);
1545       mc_mem_region_t subregion2 = mc_get_region_hinted(member2, snapshot2, process_index, region2);
1546       res =
1547           compare_areas_with_type(state, process_index,
1548                                   member1, snapshot1, subregion1,
1549                                   member2, snapshot2, subregion2,
1550                                   member.type, pointer_level);
1551       if (res == 1)
1552         return res;
1553     }
1554     break;
1555   case DW_TAG_subroutine_type:
1556     return -1;
1557     break;
1558   default:
1559     XBT_VERB("Unknown case : %d", type->type);
1560     break;
1561   }
1562
1563   return 0;
1564 }
1565
1566 static int compare_global_variables(simgrid::mc::ObjectInformation* object_info,
1567                                     int process_index,
1568                                     mc_mem_region_t r1,
1569                                     mc_mem_region_t r2, simgrid::mc::Snapshot* snapshot1,
1570                                     simgrid::mc::Snapshot* snapshot2)
1571 {
1572   xbt_assert(r1 && r2, "Missing region.");
1573
1574 #if HAVE_SMPI
1575   if (r1->storage_type() == simgrid::mc::StorageType::Privatized) {
1576     xbt_assert(process_index >= 0);
1577     if (r2->storage_type() != simgrid::mc::StorageType::Privatized)
1578       return 1;
1579
1580     size_t process_count = MC_smpi_process_count();
1581     xbt_assert(process_count == r1->privatized_data().size()
1582       && process_count == r2->privatized_data().size());
1583
1584     // Compare the global variables separately for each simulates process:
1585     for (size_t process_index = 0; process_index < process_count; process_index++) {
1586       int is_diff = compare_global_variables(object_info, process_index,
1587         &r1->privatized_data()[process_index],
1588         &r2->privatized_data()[process_index],
1589         snapshot1, snapshot2);
1590       if (is_diff) return 1;
1591     }
1592     return 0;
1593   }
1594 #else
1595   xbt_assert(r1->storage_type() != simgrid::mc::StorageType::Privatized);
1596 #endif
1597   xbt_assert(r2->storage_type() != simgrid::mc::StorageType::Privatized);
1598
1599   ComparisonState state;
1600
1601   std::vector<simgrid::mc::Variable>& variables = object_info->global_variables;
1602
1603   for (simgrid::mc::Variable& current_var : variables) {
1604
1605     // If the variable is not in this object, skip it:
1606     // We do not expect to find a pointer to something which is not reachable
1607     // by the global variables.
1608     if ((char *) current_var.address < (char *) object_info->start_rw
1609         || (char *) current_var.address > (char *) object_info->end_rw)
1610       continue;
1611
1612     simgrid::mc::Type* bvariable_type = current_var.type;
1613     int res =
1614         compare_areas_with_type(state, process_index,
1615                                 (char *) current_var.address, snapshot1, r1,
1616                                 (char *) current_var.address, snapshot2, r2,
1617                                 bvariable_type, 0);
1618     if (res == 1) {
1619       XBT_VERB("Global variable %s (%p) is different between snapshots",
1620                current_var.name.c_str(),
1621                (char *) current_var.address);
1622       return 1;
1623     }
1624
1625   }
1626
1627   return 0;
1628
1629 }
1630
1631 static int compare_local_variables(int process_index,
1632                                    simgrid::mc::Snapshot* snapshot1,
1633                                    simgrid::mc::Snapshot* snapshot2,
1634                                    mc_snapshot_stack_t stack1,
1635                                    mc_snapshot_stack_t stack2)
1636 {
1637   ComparisonState state;
1638
1639   if (stack1->local_variables.size() != stack2->local_variables.size()) {
1640     XBT_VERB("Different number of local variables");
1641     return 1;
1642   }
1643
1644     unsigned int cursor = 0;
1645     local_variable_t current_var1, current_var2;
1646     int res;
1647     while (cursor < stack1->local_variables.size()) {
1648       current_var1 = &stack1->local_variables[cursor];
1649       current_var2 = &stack1->local_variables[cursor];
1650       if (current_var1->name != current_var2->name
1651           || current_var1->subprogram != current_var2->subprogram
1652           || current_var1->ip != current_var2->ip) {
1653         // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1654         XBT_VERB
1655             ("Different name of variable (%s - %s) "
1656              "or frame (%s - %s) or ip (%lu - %lu)",
1657              current_var1->name.c_str(),
1658              current_var2->name.c_str(),
1659              current_var1->subprogram->name.c_str(),
1660              current_var2->subprogram->name.c_str(),
1661              current_var1->ip, current_var2->ip);
1662         return 1;
1663       }
1664       // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1665
1666         simgrid::mc::Type* subtype = current_var1->type;
1667         res =
1668             compare_areas_with_type(state, process_index,
1669                                     current_var1->address, snapshot1, mc_get_snapshot_region(current_var1->address, snapshot1, process_index),
1670                                     current_var2->address, snapshot2, mc_get_snapshot_region(current_var2->address, snapshot2, process_index),
1671                                     subtype, 0);
1672
1673       if (res == 1) {
1674         // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1675         XBT_VERB
1676             ("Local variable %s (%p - %p) in frame %s "
1677              "is different between snapshots",
1678              current_var1->name.c_str(),
1679              current_var1->address,
1680              current_var2->address,
1681              current_var1->subprogram->name.c_str());
1682         return res;
1683       }
1684       cursor++;
1685     }
1686     return 0;
1687 }
1688
1689 namespace simgrid {
1690 namespace mc {
1691
1692 int snapshot_compare(int num1, simgrid::mc::Snapshot* s1, int num2, simgrid::mc::Snapshot* s2)
1693 {
1694   simgrid::mc::Process* process = &mc_model_checker->process();
1695
1696   int errors = 0;
1697   int res_init;
1698
1699   int hash_result = 0;
1700   if (_sg_mc_hash) {
1701     hash_result = (s1->hash != s2->hash);
1702     if (hash_result) {
1703       XBT_VERB("(%d - %d) Different hash : 0x%" PRIx64 "--0x%" PRIx64, num1,
1704                num2, s1->hash, s2->hash);
1705 #ifndef MC_DEBUG
1706       return 1;
1707 #endif
1708     } else
1709       XBT_VERB("(%d - %d) Same hash : 0x%" PRIx64, num1, num2, s1->hash);
1710   }
1711
1712   /* Compare enabled processes */
1713   if (s1->enabled_processes != s2->enabled_processes) {
1714       XBT_VERB("(%d - %d) Different enabled processes", num1, num2);
1715       // return 1; ??
1716   }
1717
1718   unsigned long i = 0;
1719   size_t size_used1, size_used2;
1720   int is_diff = 0;
1721
1722   /* Compare size of stacks */
1723   while (i < s1->stacks.size()) {
1724     size_used1 = s1->stack_sizes[i];
1725     size_used2 = s2->stack_sizes[i];
1726     if (size_used1 != size_used2) {
1727 #ifdef MC_DEBUG
1728       XBT_DEBUG("(%d - %d) Different size used in stacks : %zu - %zu", num1,
1729                 num2, size_used1, size_used2);
1730       errors++;
1731       is_diff = 1;
1732 #else
1733 #ifdef MC_VERBOSE
1734       XBT_VERB("(%d - %d) Different size used in stacks : %zu - %zu", num1,
1735                num2, size_used1, size_used2);
1736 #endif
1737       return 1;
1738 #endif
1739     }
1740     i++;
1741   }
1742
1743   /* Init heap information used in heap comparison algorithm */
1744   xbt_mheap_t heap1 = (xbt_mheap_t)s1->read_bytes(
1745     alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1746     remote(process->heap_address),
1747     simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1748   xbt_mheap_t heap2 = (xbt_mheap_t)s2->read_bytes(
1749     alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1750     remote(process->heap_address),
1751     simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1752   res_init = simgrid::mc::init_heap_information(heap1, heap2, &s1->to_ignore, &s2->to_ignore);
1753   if (res_init == -1) {
1754 #ifdef MC_DEBUG
1755     XBT_DEBUG("(%d - %d) Different heap information", num1, num2);
1756     errors++;
1757 #else
1758 #ifdef MC_VERBOSE
1759     XBT_VERB("(%d - %d) Different heap information", num1, num2);
1760 #endif
1761
1762     return 1;
1763 #endif
1764   }
1765
1766   /* Stacks comparison */
1767   unsigned cursor = 0;
1768   int diff_local = 0;
1769 #ifdef MC_DEBUG
1770   is_diff = 0;
1771 #endif
1772   mc_snapshot_stack_t stack1, stack2;
1773   while (cursor < s1->stacks.size()) {
1774     stack1 = &s1->stacks[cursor];
1775     stack2 = &s2->stacks[cursor];
1776
1777     if (stack1->process_index != stack2->process_index) {
1778       diff_local = 1;
1779       XBT_DEBUG("(%d - %d) Stacks with different process index (%i vs %i)", num1, num2,
1780         stack1->process_index, stack2->process_index);
1781     }
1782     else diff_local =
1783         compare_local_variables(stack1->process_index, s1, s2, stack1, stack2);
1784     if (diff_local > 0) {
1785 #ifdef MC_DEBUG
1786       XBT_DEBUG("(%d - %d) Different local variables between stacks %d", num1,
1787                 num2, cursor + 1);
1788       errors++;
1789       is_diff = 1;
1790 #else
1791
1792 #ifdef MC_VERBOSE
1793       XBT_VERB("(%d - %d) Different local variables between stacks %d", num1,
1794                num2, cursor + 1);
1795 #endif
1796
1797       simgrid::mc::reset_heap_information();
1798
1799       return 1;
1800 #endif
1801     }
1802     cursor++;
1803   }
1804
1805   size_t regions_count = s1->snapshot_regions.size();
1806   // TODO, raise a difference instead?
1807   xbt_assert(regions_count == s2->snapshot_regions.size());
1808
1809   for (size_t k = 0; k != regions_count; ++k) {
1810     mc_mem_region_t region1 = s1->snapshot_regions[k].get();
1811     mc_mem_region_t region2 = s2->snapshot_regions[k].get();
1812
1813     // Preconditions:
1814     if (region1->region_type() != simgrid::mc::RegionType::Data)
1815       continue;
1816
1817     xbt_assert(region1->region_type() == region2->region_type());
1818     xbt_assert(region1->object_info() == region2->object_info());
1819     xbt_assert(region1->object_info());
1820
1821     std::string const& name = region1->object_info()->file_name;
1822
1823     /* Compare global variables */
1824     is_diff =
1825       compare_global_variables(region1->object_info(),
1826         simgrid::mc::ProcessIndexDisabled,
1827         region1, region2,
1828         s1, s2);
1829
1830     if (is_diff != 0) {
1831 #ifdef MC_DEBUG
1832       XBT_DEBUG("(%d - %d) Different global variables in %s",
1833         num1, num2, name.c_str());
1834       errors++;
1835 #else
1836 #ifdef MC_VERBOSE
1837       XBT_VERB("(%d - %d) Different global variables in %s",
1838         num1, num2, name.c_str());
1839 #endif
1840
1841       return 1;
1842 #endif
1843     }
1844   }
1845
1846   /* Compare heap */
1847   if (simgrid::mc::mmalloc_compare_heap(s1, s2) > 0) {
1848
1849 #ifdef MC_DEBUG
1850     XBT_DEBUG("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
1851     errors++;
1852 #else
1853
1854 #ifdef MC_VERBOSE
1855     XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
1856 #endif
1857
1858     return 1;
1859 #endif
1860   }
1861
1862   simgrid::mc::reset_heap_information();
1863
1864 #ifdef MC_VERBOSE
1865   if (errors || hash_result)
1866     XBT_VERB("(%d - %d) Difference found", num1, num2);
1867   else
1868     XBT_VERB("(%d - %d) No difference found", num1, num2);
1869 #endif
1870
1871 #if defined(MC_DEBUG) && defined(MC_VERBOSE)
1872   if (_sg_mc_hash) {
1873     // * false positive SHOULD be avoided.
1874     // * There MUST not be any false negative.
1875
1876     XBT_VERB("(%d - %d) State equality hash test is %s %s", num1, num2,
1877              (hash_result != 0) == (errors != 0) ? "true" : "false",
1878              !hash_result ? "positive" : "negative");
1879   }
1880 #endif
1881
1882   return errors > 0 || hash_result;
1883 }
1884
1885 }
1886 }