Logo AND Algorithmique Numérique Distribuée

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