Logo AND Algorithmique Numérique Distribuée

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