Logo AND Algorithmique Numérique Distribuée

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