Logo AND Algorithmique Numérique Distribuée

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