Logo AND Algorithmique Numérique Distribuée

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