Logo AND Algorithmique Numérique Distribuée

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