1 /* Copyright (c) 2008-2017. The SimGrid Team. All rights reserved. */
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. */
6 /** \file compare.cpp Memory snapshooting and comparison */
14 #include <unordered_set>
16 #include "xbt/dynar.h"
17 #include "xbt/sysdep.h"
18 #include <xbt/mmalloc.h>
21 #include <mc/datatypes.h>
23 #include "src/internal_config.h"
25 #include "src/xbt/mmalloc/mmprivate.h"
28 #include "src/smpi/include/private.hpp"
31 #include "src/mc/Frame.hpp"
32 #include "src/mc/ObjectInformation.hpp"
33 #include "src/mc/Type.hpp"
34 #include "src/mc/Variable.hpp"
35 #include "src/mc/mc_dwarf.hpp"
36 #include "src/mc/mc_forward.hpp"
37 #include "src/mc/mc_private.hpp"
38 #include "src/mc/mc_smx.hpp"
39 #include "src/mc/mc_snapshot.hpp"
41 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_compare, xbt, "Logging specific to mc_compare in mc");
47 typedef std::array<HeapLocation, 2> HeapLocationPair;
48 typedef std::set<HeapLocationPair> HeapLocationPairs;
50 struct ProcessComparisonState;
51 struct StateComparator;
53 static int compare_heap_area(
54 StateComparator& state,
55 int process_index, const void *area1, const void* area2,
56 Snapshot* snapshot1, Snapshot* snapshot2,
57 HeapLocationPairs* previous, Type* type, int pointer_level);
62 using simgrid::mc::remote;
64 /*********************************** Heap comparison ***********************************/
65 /***************************************************************************************/
75 HeapLocation() = default;
76 HeapLocation(int block, int fragment = 0) : block_(block), fragment_(fragment) {}
78 bool operator==(HeapLocation const& that) const
80 return block_ == that.block_ && fragment_ == that.fragment_;
82 bool operator<(HeapLocation const& that) const
84 return std::make_pair(block_, fragment_) < std::make_pair(that.block_, that.fragment_);
89 HeapLocationPair makeHeapLocationPair(int block1, int fragment1, int block2, int fragment2)
91 return simgrid::mc::HeapLocationPair{{
92 simgrid::mc::HeapLocation(block1, fragment1),
93 simgrid::mc::HeapLocation(block2, fragment2)
97 class HeapArea : public HeapLocation {
100 HeapArea() = default;
101 explicit HeapArea(int block) : valid_(true) { block_ = block; }
102 HeapArea(int block, int fragment) : valid_(true)
105 fragment_ = fragment;
109 class ProcessComparisonState {
111 std::vector<simgrid::mc::IgnoredHeapRegion>* to_ignore = nullptr;
112 std::vector<HeapArea> equals_to;
113 std::vector<simgrid::mc::Type*> types;
114 std::size_t heapsize = 0;
116 void initHeapInformation(xbt_mheap_t heap, std::vector<simgrid::mc::IgnoredHeapRegion>* i);
121 /** A hash which works with more stuff
123 * It can hash pairs: the standard hash currently doesn't include this.
125 template<class X> struct hash : public std::hash<X> {};
127 template <class X, class Y> class hash<std::pair<X, Y>> {
129 std::size_t operator()(std::pair<X,Y>const& x) const
133 return h1(x.first) ^ h2(x.second);
139 class StateComparator {
141 s_xbt_mheap_t std_heap_copy;
142 std::size_t heaplimit;
143 std::array<ProcessComparisonState, 2> processStates;
145 std::unordered_set<std::pair<void*, void*>, hash<std::pair<void*, void*>>> compared_pointers;
149 compared_pointers.clear();
152 int initHeapInformation(
153 xbt_mheap_t heap1, xbt_mheap_t heap2,
154 std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
155 std::vector<simgrid::mc::IgnoredHeapRegion>* i2);
157 HeapArea& equals_to1_(std::size_t i, std::size_t j)
159 return processStates[0].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
161 HeapArea& equals_to2_(std::size_t i, std::size_t j)
163 return processStates[1].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
165 Type*& types1_(std::size_t i, std::size_t j)
167 return processStates[0].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
169 Type*& types2_(std::size_t i, std::size_t j)
171 return processStates[1].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
174 HeapArea const& equals_to1_(std::size_t i, std::size_t j) const
176 return processStates[0].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
178 HeapArea const& equals_to2_(std::size_t i, std::size_t j) const
180 return processStates[1].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
182 Type* const& types1_(std::size_t i, std::size_t j) const
184 return processStates[0].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
186 Type* const& types2_(std::size_t i, std::size_t j) const
188 return processStates[1].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
191 /** Check whether two blocks are known to be matching
193 * @param b1 Block of state 1
194 * @param b2 Block of state 2
195 * @return if the blocks are known to be matching
197 bool blocksEqual(int b1, int b2) const
199 return this->equals_to1_(b1, 0).block_ == b2 && this->equals_to2_(b2, 0).block_ == b1;
202 /** Check whether two fragments are known to be matching
204 * @param b1 Block of state 1
205 * @param f1 Fragment of state 1
206 * @param b2 Block of state 2
207 * @param f2 Fragment of state 2
208 * @return if the fragments are known to be matching
210 int fragmentsEqual(int b1, int f1, int b2, int f2) const
212 return this->equals_to1_(b1, f1).block_ == b2 && this->equals_to1_(b1, f1).fragment_ == f2 &&
213 this->equals_to2_(b2, f2).block_ == b1 && this->equals_to2_(b2, f2).fragment_ == f1;
216 void match_equals(HeapLocationPairs* list);
222 /************************************************************************************/
224 static ssize_t heap_comparison_ignore_size(
225 std::vector<simgrid::mc::IgnoredHeapRegion>* ignore_list,
229 int end = ignore_list->size() - 1;
231 while (start <= end) {
232 unsigned int cursor = (start + end) / 2;
233 simgrid::mc::IgnoredHeapRegion const& region = (*ignore_list)[cursor];
234 if (region.address == address)
236 if (region.address < address)
238 if (region.address > address)
245 static bool is_stack(const void *address)
247 for (auto const& stack : mc_model_checker->process().stack_areas())
248 if (address == stack.address)
253 // TODO, this should depend on the snapshot?
254 static bool is_block_stack(int block)
256 for (auto const& stack : mc_model_checker->process().stack_areas())
257 if (block == stack.block)
265 void StateComparator::match_equals(HeapLocationPairs* list)
267 for (auto const& pair : *list) {
268 if (pair[0].fragment_ != -1) {
269 this->equals_to1_(pair[0].block_, pair[0].fragment_) = simgrid::mc::HeapArea(pair[1].block_, pair[1].fragment_);
270 this->equals_to2_(pair[1].block_, pair[1].fragment_) = simgrid::mc::HeapArea(pair[0].block_, pair[0].fragment_);
272 this->equals_to1_(pair[0].block_, 0) = simgrid::mc::HeapArea(pair[1].block_, pair[1].fragment_);
273 this->equals_to2_(pair[1].block_, 0) = simgrid::mc::HeapArea(pair[0].block_, pair[0].fragment_);
278 void ProcessComparisonState::initHeapInformation(xbt_mheap_t heap,
279 std::vector<simgrid::mc::IgnoredHeapRegion>* i)
281 auto heaplimit = heap->heaplimit;
282 this->heapsize = heap->heapsize;
284 this->equals_to.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, HeapArea());
285 this->types.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, nullptr);
288 int StateComparator::initHeapInformation(xbt_mheap_t heap1, xbt_mheap_t heap2,
289 std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
290 std::vector<simgrid::mc::IgnoredHeapRegion>* i2)
292 if ((heap1->heaplimit != heap2->heaplimit) || (heap1->heapsize != heap2->heapsize))
294 this->heaplimit = heap1->heaplimit;
295 this->std_heap_copy = *mc_model_checker->process().get_heap();
296 this->processStates[0].initHeapInformation(heap1, i1);
297 this->processStates[1].initHeapInformation(heap2, i2);
301 // TODO, have a robust way to find it in O(1)
303 mc_mem_region_t MC_get_heap_region(simgrid::mc::Snapshot* snapshot)
305 for (auto const& region : snapshot->snapshot_regions)
306 if (region->region_type() == simgrid::mc::RegionType::Heap)
308 xbt_die("No heap region");
312 int mmalloc_compare_heap(
313 simgrid::mc::StateComparator& state, simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2)
315 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
317 /* Start comparison */
331 /* Check busy blocks */
334 malloc_info heapinfo_temp1;
335 malloc_info heapinfo_temp2;
336 malloc_info heapinfo_temp2b;
338 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
339 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
341 // This is the address of std_heap->heapinfo in the application process:
342 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
344 // This is in snapshot do not use them directly:
345 const malloc_info* heapinfos1 = snapshot1->read<malloc_info*>(
346 (std::uint64_t)heapinfo_address, simgrid::mc::ProcessIndexMissing);
347 const malloc_info* heapinfos2 = snapshot2->read<malloc_info*>(
348 (std::uint64_t)heapinfo_address, simgrid::mc::ProcessIndexMissing);
350 while (i1 < state.heaplimit) {
352 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(heap_region1, &heapinfo_temp1, &heapinfos1[i1], sizeof(malloc_info));
353 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2, &heapinfos2[i1], sizeof(malloc_info));
355 if (heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type == MMALLOC_TYPE_HEAPINFO) { /* Free block */
360 if (heapinfo1->type < 0) {
361 fprintf(stderr, "Unkown mmalloc block type.\n");
365 addr_block1 = ((void*)(((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase));
367 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED) { /* Large block */
369 if (is_stack(addr_block1)) {
370 for (k = 0; k < heapinfo1->busy_block.size; k++)
371 state.equals_to1_(i1 + k, 0) = HeapArea(i1, -1);
372 for (k = 0; k < heapinfo2->busy_block.size; k++)
373 state.equals_to2_(i1 + k, 0) = HeapArea(i1, -1);
374 i1 += heapinfo1->busy_block.size;
378 if (state.equals_to1_(i1, 0).valid_) {
386 /* Try first to associate to same block in the other heap */
387 if (heapinfo2->type == heapinfo1->type && state.equals_to2_(i1, 0).valid_ == 0) {
388 addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
389 int res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing, addr_block1, addr_block2,
390 snapshot1, snapshot2, nullptr, nullptr, 0);
391 if (res_compare != 1) {
392 for (k = 1; k < heapinfo2->busy_block.size; k++)
393 state.equals_to2_(i1 + k, 0) = HeapArea(i1, -1);
394 for (k = 1; k < heapinfo1->busy_block.size; k++)
395 state.equals_to1_(i1 + k, 0) = HeapArea(i1, -1);
397 i1 += heapinfo1->busy_block.size;
401 while (i2 < state.heaplimit && not equal) {
403 addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
410 const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2b, &heapinfos2[i2], sizeof(malloc_info));
412 if (heapinfo2b->type != MMALLOC_TYPE_UNFRAGMENTED) {
417 if (state.equals_to2_(i2, 0).valid_) {
422 int res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing, addr_block1, addr_block2,
423 snapshot1, snapshot2, nullptr, nullptr, 0);
425 if (res_compare != 1) {
426 for (k = 1; k < heapinfo2b->busy_block.size; k++)
427 state.equals_to2_(i2 + k, 0) = HeapArea(i1, -1);
428 for (k = 1; k < heapinfo1->busy_block.size; k++)
429 state.equals_to1_(i1 + k, 0) = HeapArea(i2, -1);
431 i1 += heapinfo1->busy_block.size;
438 XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1, heapinfo1->busy_block.busy_size, addr_block1);
439 i1 = state.heaplimit + 1;
443 } else { /* Fragmented block */
445 for (j1 = 0; j1 < (size_t) (BLOCKSIZE >> heapinfo1->type); j1++) {
447 if (heapinfo1->busy_frag.frag_size[j1] == -1) /* Free fragment_ */
450 if (state.equals_to1_(i1, j1).valid_)
453 addr_frag1 = (void*)((char*)addr_block1 + (j1 << heapinfo1->type));
458 /* Try first to associate to same fragment_ in the other heap */
459 if (heapinfo2->type == heapinfo1->type && not state.equals_to2_(i1, j1).valid_) {
460 addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE +
461 (char *) state.std_heap_copy.heapbase;
463 (void *) ((char *) addr_block2 +
464 (j1 << heapinfo2->type));
465 int res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing, addr_frag1, addr_frag2,
466 snapshot1, snapshot2, nullptr, nullptr, 0);
467 if (res_compare != 1)
471 while (i2 < state.heaplimit && not equal) {
473 const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(
474 heap_region2, &heapinfo_temp2b, &heapinfos2[i2],
475 sizeof(malloc_info));
477 if (heapinfo2b->type == MMALLOC_TYPE_FREE || heapinfo2b->type == MMALLOC_TYPE_HEAPINFO) {
482 // We currently do not match fragments with unfragmented blocks (maybe we should).
483 if (heapinfo2b->type == MMALLOC_TYPE_UNFRAGMENTED) {
488 if (heapinfo2b->type < 0) {
489 fprintf(stderr, "Unknown mmalloc block type.\n");
493 for (j2 = 0; j2 < (size_t) (BLOCKSIZE >> heapinfo2b->type);
496 if (i2 == i1 && j2 == j1)
499 if (state.equals_to2_(i2, j2).valid_)
502 addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
503 addr_frag2 = (void*)((char*)addr_block2 + (j2 << heapinfo2b->type));
505 int res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing, addr_frag1, addr_frag2,
506 snapshot2, snapshot2, nullptr, nullptr, 0);
507 if (res_compare != 1) {
517 XBT_DEBUG("Block %zu, fragment_ %zu not found (size_used = %zd, address = %p)\n", i1, j1,
518 heapinfo1->busy_frag.frag_size[j1], addr_frag1);
519 i1 = state.heaplimit + 1;
529 /* All blocks/fragments are equal to another block/fragment_ ? */
533 for(i = 1; i < state.heaplimit; i++) {
534 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
535 heap_region1, &heapinfo_temp1, &heapinfos1[i], sizeof(malloc_info));
537 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED && i1 == state.heaplimit && heapinfo1->busy_block.busy_size > 0 &&
538 not state.equals_to1_(i, 0).valid_) {
539 XBT_DEBUG("Block %zu not found (size used = %zu)", i, heapinfo1->busy_block.busy_size);
543 if (heapinfo1->type <= 0)
545 for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo1->type); j++)
546 if (i1 == state.heaplimit && heapinfo1->busy_frag.frag_size[j] > 0 && not state.equals_to1_(i, j).valid_) {
547 XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)", i, j, heapinfo1->busy_frag.frag_size[j]);
552 if (i1 == state.heaplimit)
553 XBT_DEBUG("Number of blocks/fragments not found in heap1: %d", nb_diff1);
555 for (i=1; i < state.heaplimit; i++) {
556 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
557 heap_region2, &heapinfo_temp2, &heapinfos2[i], sizeof(malloc_info));
558 if (heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED && i1 == state.heaplimit && heapinfo2->busy_block.busy_size > 0 &&
559 not state.equals_to2_(i, 0).valid_) {
560 XBT_DEBUG("Block %zu not found (size used = %zu)", i,
561 heapinfo2->busy_block.busy_size);
565 if (heapinfo2->type <= 0)
568 for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo2->type); j++)
569 if (i1 == state.heaplimit && heapinfo2->busy_frag.frag_size[j] > 0 && not state.equals_to2_(i, j).valid_) {
570 XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)",
571 i, j, heapinfo2->busy_frag.frag_size[j]);
577 if (i1 == state.heaplimit)
578 XBT_DEBUG("Number of blocks/fragments not found in heap2: %d", nb_diff2);
580 return nb_diff1 > 0 || nb_diff2 > 0;
586 * @param real_area1 Process address for state 1
587 * @param real_area2 Process address for state 2
588 * @param snapshot1 Snapshot of state 1
589 * @param snapshot2 Snapshot of state 2
592 * @param check_ignore
594 static int compare_heap_area_without_type(
595 simgrid::mc::StateComparator& state, int process_index,
596 const void *real_area1, const void *real_area2,
597 simgrid::mc::Snapshot* snapshot1,
598 simgrid::mc::Snapshot* snapshot2,
599 HeapLocationPairs* previous, int size,
602 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
603 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
604 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
606 for (int i = 0; i < size; ) {
608 if (check_ignore > 0) {
609 ssize_t ignore1 = heap_comparison_ignore_size(
610 state.processStates[0].to_ignore, (char *) real_area1 + i);
612 ssize_t ignore2 = heap_comparison_ignore_size(
613 state.processStates[1].to_ignore, (char *) real_area2 + i);
614 if (ignore2 == ignore1) {
627 if (MC_snapshot_region_memcmp(((char *) real_area1) + i, heap_region1, ((char *) real_area2) + i, heap_region2, 1) != 0) {
629 int pointer_align = (i / sizeof(void *)) * sizeof(void *);
630 const void* addr_pointed1 = snapshot1->read(
631 remote((void**)((char *) real_area1 + pointer_align)), process_index);
632 const void* addr_pointed2 = snapshot2->read(
633 remote((void**)((char *) real_area2 + pointer_align)), process_index);
635 if (process->in_maestro_stack(remote(addr_pointed1))
636 && process->in_maestro_stack(remote(addr_pointed2))) {
637 i = pointer_align + sizeof(void *);
641 if (addr_pointed1 > state.std_heap_copy.heapbase
642 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
643 && addr_pointed2 > state.std_heap_copy.heapbase
644 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)) {
645 // Both addreses are in the heap:
646 int res_compare = compare_heap_area(state ,process_index,
647 addr_pointed1, addr_pointed2,
648 snapshot1, snapshot2, previous, nullptr, 0);
649 if (res_compare == 1)
651 i = pointer_align + sizeof(void *);
667 * @param real_area1 Process address for state 1
668 * @param real_area2 Process address for state 2
669 * @param snapshot1 Snapshot of state 1
670 * @param snapshot2 Snapshot of state 2
673 * @param area_size either a byte_size or an elements_count (?)
674 * @param check_ignore
675 * @param pointer_level
676 * @return 0 (same), 1 (different), -1 (unknown)
678 static int compare_heap_area_with_type(
679 simgrid::mc::StateComparator& state, int process_index,
680 const void *real_area1, const void *real_area2,
681 simgrid::mc::Snapshot* snapshot1,
682 simgrid::mc::Snapshot* snapshot2,
683 HeapLocationPairs* previous, simgrid::mc::Type* type,
684 int area_size, int check_ignore,
689 // HACK: This should not happen but in pratice, there are some
690 // DW_TAG_typedef without an associated DW_AT_type:
691 //<1><538832>: Abbrev Number: 111 (DW_TAG_typedef)
692 // <538833> DW_AT_name : (indirect string, offset: 0x2292f3): gregset_t
693 // <538837> DW_AT_decl_file : 98
694 // <538838> DW_AT_decl_line : 37
698 if (is_stack(real_area1) && is_stack(real_area2))
701 if (check_ignore > 0) {
702 ssize_t ignore1 = heap_comparison_ignore_size(state.processStates[0].to_ignore, real_area1);
703 if (ignore1 > 0 && heap_comparison_ignore_size(state.processStates[1].to_ignore, real_area2) == ignore1)
707 simgrid::mc::Type* subtype;
708 simgrid::mc::Type* subsubtype;
711 const void* addr_pointed1;
712 const void* addr_pointed2;
714 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
715 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
717 switch (type->type) {
718 case DW_TAG_unspecified_type:
721 case DW_TAG_base_type:
722 if (not type->name.empty() && type->name == "char") { /* String, hence random (arbitrary ?) size */
723 if (real_area1 == real_area2)
726 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, area_size) != 0;
728 if (area_size != -1 && type->byte_size != area_size)
731 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
735 case DW_TAG_enumeration_type:
736 if (area_size != -1 && type->byte_size != area_size)
738 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
741 case DW_TAG_const_type:
742 case DW_TAG_volatile_type:
744 type = type->subtype;
747 case DW_TAG_array_type:
748 subtype = type->subtype;
749 switch (subtype->type) {
750 case DW_TAG_unspecified_type:
753 case DW_TAG_base_type:
754 case DW_TAG_enumeration_type:
755 case DW_TAG_pointer_type:
756 case DW_TAG_reference_type:
757 case DW_TAG_rvalue_reference_type:
758 case DW_TAG_structure_type:
759 case DW_TAG_class_type:
760 case DW_TAG_union_type:
761 if (subtype->full_type)
762 subtype = subtype->full_type;
763 elm_size = subtype->byte_size;
765 // TODO, just remove the type indirection?
766 case DW_TAG_const_type:
768 case DW_TAG_volatile_type:
769 subsubtype = subtype->subtype;
770 if (subsubtype->full_type)
771 subsubtype = subsubtype->full_type;
772 elm_size = subsubtype->byte_size;
778 for (int i = 0; i < type->element_count; i++) {
779 // TODO, add support for variable stride (DW_AT_byte_stride)
780 res = compare_heap_area_with_type(state, process_index, (char*)real_area1 + (i * elm_size),
781 (char*)real_area2 + (i * elm_size), snapshot1, snapshot2, previous,
782 type->subtype, subtype->byte_size, check_ignore, pointer_level);
788 case DW_TAG_reference_type:
789 case DW_TAG_rvalue_reference_type:
790 case DW_TAG_pointer_type:
791 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
792 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
793 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
794 return (addr_pointed1 != addr_pointed2);
797 if (pointer_level <= 1) {
798 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
799 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
800 if (addr_pointed1 > state.std_heap_copy.heapbase && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1) &&
801 addr_pointed2 > state.std_heap_copy.heapbase && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
802 return compare_heap_area(state, process_index, addr_pointed1, addr_pointed2, snapshot1, snapshot2, previous,
803 type->subtype, pointer_level);
805 return (addr_pointed1 != addr_pointed2);
807 for (size_t i = 0; i < (area_size / sizeof(void*)); i++) {
808 addr_pointed1 = snapshot1->read(remote((void**)((char*)real_area1 + i * sizeof(void*))), process_index);
809 addr_pointed2 = snapshot2->read(remote((void**)((char*)real_area2 + i * sizeof(void*))), process_index);
810 if (addr_pointed1 > state.std_heap_copy.heapbase && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1) &&
811 addr_pointed2 > state.std_heap_copy.heapbase && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
812 res = compare_heap_area(state, process_index, addr_pointed1, addr_pointed2, snapshot1, snapshot2, previous,
813 type->subtype, pointer_level);
815 res = (addr_pointed1 != addr_pointed2);
821 case DW_TAG_structure_type:
822 case DW_TAG_class_type:
824 type = type->full_type;
825 if (area_size != -1 && type->byte_size != area_size) {
826 if (area_size <= type->byte_size || area_size % type->byte_size != 0)
828 for (size_t i = 0; i < (size_t)(area_size / type->byte_size); i++) {
829 int res = compare_heap_area_with_type(state, process_index, (char*)real_area1 + i * type->byte_size,
830 (char*)real_area2 + i * type->byte_size, snapshot1, snapshot2,
831 previous, type, -1, check_ignore, 0);
836 for (simgrid::mc::Member& member : type->members) {
837 // TODO, optimize this? (for the offset case)
838 void* real_member1 = simgrid::dwarf::resolve_member(real_area1, type, &member,
839 (simgrid::mc::AddressSpace*)snapshot1, process_index);
840 void* real_member2 = simgrid::dwarf::resolve_member(real_area2, type, &member,
841 (simgrid::mc::AddressSpace*)snapshot2, process_index);
842 int res = compare_heap_area_with_type(state, process_index, real_member1, real_member2, snapshot1,
843 snapshot2, previous, member.type, -1, check_ignore, 0);
850 case DW_TAG_union_type:
851 return compare_heap_area_without_type(state, process_index, real_area1, real_area2, snapshot1, snapshot2,
852 previous, type->byte_size, check_ignore);
858 xbt_die("Unreachable");
862 /** Infer the type of a part of the block from the type of the block
864 * TODO, handle DW_TAG_array_type as well as arrays of the object ((*p)[5], p[5])
866 * TODO, handle subfields ((*p).bar.foo, (*p)[5].bar…)
868 * @param type DWARF type ID of the root address
870 * @return DWARF type ID for given offset
872 static simgrid::mc::Type* get_offset_type(void *real_base_address, simgrid::mc::Type* type,
873 int offset, int area_size,
874 simgrid::mc::Snapshot* snapshot, int process_index)
877 // Beginning of the block, the infered variable type if the type of the block:
881 switch (type->type) {
883 case DW_TAG_structure_type:
884 case DW_TAG_class_type:
886 type = type->full_type;
887 if (area_size != -1 && type->byte_size != area_size) {
888 if (area_size > type->byte_size && area_size % type->byte_size == 0)
894 for (simgrid::mc::Member& member : type->members) {
895 if (member.has_offset_location()) {
896 // We have the offset, use it directly (shortcut):
897 if (member.offset() == offset)
900 void* real_member = simgrid::dwarf::resolve_member(real_base_address, type, &member, snapshot, process_index);
901 if ((char*)real_member - (char*)real_base_address == offset)
908 /* FIXME: other cases ? */
916 * @param area1 Process address for state 1
917 * @param area2 Process address for state 2
918 * @param snapshot1 Snapshot of state 1
919 * @param snapshot2 Snapshot of state 2
920 * @param previous Pairs of blocks already compared on the current path (or nullptr)
921 * @param type_id Type of variable
922 * @param pointer_level
923 * @return 0 (same), 1 (different), -1
926 int compare_heap_area(simgrid::mc::StateComparator& state, int process_index,
927 const void *area1, const void *area2,
928 simgrid::mc::Snapshot* snapshot1,
929 simgrid::mc::Snapshot* snapshot2,
930 HeapLocationPairs* previous,
931 simgrid::mc::Type* type, int pointer_level)
933 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
938 int check_ignore = 0;
946 simgrid::mc::Type* new_type1 = nullptr;
947 simgrid::mc::Type* new_type2 = nullptr;
949 bool match_pairs = false;
951 // This is the address of std_heap->heapinfo in the application process:
952 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
954 const malloc_info* heapinfos1 = snapshot1->read(remote((const malloc_info**)heapinfo_address), process_index);
955 const malloc_info* heapinfos2 = snapshot2->read(remote((const malloc_info**)heapinfo_address), process_index);
957 malloc_info heapinfo_temp1;
958 malloc_info heapinfo_temp2;
960 simgrid::mc::HeapLocationPairs current;
961 if (previous == nullptr) {
967 block1 = ((char*)area1 - (char*)state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
968 block2 = ((char*)area2 - (char*)state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
970 // If either block is a stack block:
971 if (is_block_stack((int) block1) && is_block_stack((int) block2)) {
972 previous->insert(simgrid::mc::makeHeapLocationPair(block1, -1, block2, -1));
974 state.match_equals(previous);
978 // If either block is not in the expected area of memory:
979 if (((char*)area1 < (char*)state.std_heap_copy.heapbase) || (block1 > (ssize_t)state.processStates[0].heapsize) ||
980 (block1 < 1) || ((char*)area2 < (char*)state.std_heap_copy.heapbase) ||
981 (block2 > (ssize_t)state.processStates[1].heapsize) || (block2 < 1)) {
985 // Process address of the block:
986 void* real_addr_block1 = (ADDR2UINT(block1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
987 void* real_addr_block2 = (ADDR2UINT(block2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
991 type = type->full_type;
993 // This assume that for "boring" types (volatile ...) byte_size is absent:
994 while (type->byte_size == 0 && type->subtype != nullptr)
995 type = type->subtype;
998 if (type->type == DW_TAG_pointer_type ||
999 (type->type == DW_TAG_base_type && not type->name.empty() && type->name == "char"))
1002 type_size = type->byte_size;
1006 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
1007 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
1009 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
1010 heap_region1, &heapinfo_temp1, &heapinfos1[block1], sizeof(malloc_info));
1011 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
1012 heap_region2, &heapinfo_temp2, &heapinfos2[block2], sizeof(malloc_info));
1014 if ((heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type==MMALLOC_TYPE_HEAPINFO)
1015 && (heapinfo2->type == MMALLOC_TYPE_FREE || heapinfo2->type ==MMALLOC_TYPE_HEAPINFO)) {
1018 state.match_equals(previous);
1022 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED && heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
1023 /* Complete block */
1025 // TODO, lookup variable type from block type as done for fragmented blocks
1027 if (state.equals_to1_(block1, 0).valid_ && state.equals_to2_(block2, 0).valid_ &&
1028 state.blocksEqual(block1, block2)) {
1030 state.match_equals(previous);
1034 if (type_size != -1) {
1035 if (type_size != (ssize_t) heapinfo1->busy_block.busy_size
1036 && type_size != (ssize_t) heapinfo2->busy_block.busy_size
1037 && (type->name.empty() || type->name == "struct s_smx_context")) {
1039 state.match_equals(previous);
1044 if (heapinfo1->busy_block.size != heapinfo2->busy_block.size)
1046 if (heapinfo1->busy_block.busy_size != heapinfo2->busy_block.busy_size)
1049 if (not previous->insert(simgrid::mc::makeHeapLocationPair(block1, -1, block2, -1)).second) {
1051 state.match_equals(previous);
1055 size = heapinfo1->busy_block.busy_size;
1057 // Remember (basic) type inference.
1058 // The current data structure only allows us to do this for the whole block.
1059 if (type != nullptr && area1 == real_addr_block1)
1060 state.types1_(block1, 0) = type;
1061 if (type != nullptr && area2 == real_addr_block2)
1062 state.types2_(block2, 0) = type;
1066 state.match_equals(previous);
1070 if (heapinfo1->busy_block.ignore > 0
1071 && heapinfo2->busy_block.ignore == heapinfo1->busy_block.ignore)
1072 check_ignore = heapinfo1->busy_block.ignore;
1074 } else if ((heapinfo1->type > 0) && (heapinfo2->type > 0)) { /* Fragmented block */
1077 ssize_t frag1 = ((uintptr_t)(ADDR2UINT(area1) % (BLOCKSIZE))) >> heapinfo1->type;
1078 ssize_t frag2 = ((uintptr_t)(ADDR2UINT(area2) % (BLOCKSIZE))) >> heapinfo2->type;
1080 // Process address of the fragment_:
1081 void* real_addr_frag1 = (void*)((char*)real_addr_block1 + (frag1 << heapinfo1->type));
1082 void* real_addr_frag2 = (void*)((char*)real_addr_block2 + (frag2 << heapinfo2->type));
1084 // Check the size of the fragments against the size of the type:
1085 if (type_size != -1) {
1086 if (heapinfo1->busy_frag.frag_size[frag1] == -1 || heapinfo2->busy_frag.frag_size[frag2] == -1) {
1088 state.match_equals(previous);
1092 if (type_size != heapinfo1->busy_frag.frag_size[frag1]
1093 || type_size != heapinfo2->busy_frag.frag_size[frag2]) {
1095 state.match_equals(previous);
1100 // Check if the blocks are already matched together:
1101 if (state.equals_to1_(block1, frag1).valid_ && state.equals_to2_(block2, frag2).valid_) {
1102 if (offset1==offset2 && state.fragmentsEqual(block1, frag1, block2, frag2)) {
1104 state.match_equals(previous);
1108 // Compare the size of both fragments:
1109 if (heapinfo1->busy_frag.frag_size[frag1] != heapinfo2->busy_frag.frag_size[frag2]) {
1110 if (type_size == -1) {
1112 state.match_equals(previous);
1118 // Size of the fragment_:
1119 size = heapinfo1->busy_frag.frag_size[frag1];
1121 // Remember (basic) type inference.
1122 // The current data structure only allows us to do this for the whole fragment_.
1123 if (type != nullptr && area1 == real_addr_frag1)
1124 state.types1_(block1, frag1) = type;
1125 if (type != nullptr && area2 == real_addr_frag2)
1126 state.types2_(block2, frag2) = type;
1128 // The type of the variable is already known:
1130 new_type1 = new_type2 = type;
1132 // Type inference from the block type.
1133 else if (state.types1_(block1, frag1) != nullptr || state.types2_(block2, frag2) != nullptr) {
1135 offset1 = (char*)area1 - (char*)real_addr_frag1;
1136 offset2 = (char*)area2 - (char*)real_addr_frag2;
1138 if (state.types1_(block1, frag1) != nullptr && state.types2_(block2, frag2) != nullptr) {
1140 get_offset_type(real_addr_frag1, state.types1_(block1, frag1), offset1, size, snapshot1, process_index);
1142 get_offset_type(real_addr_frag2, state.types2_(block2, frag2), offset1, size, snapshot2, process_index);
1143 } else if (state.types1_(block1, frag1) != nullptr) {
1145 get_offset_type(real_addr_frag1, state.types1_(block1, frag1), offset1, size, snapshot1, process_index);
1147 get_offset_type(real_addr_frag2, state.types1_(block1, frag1), offset2, size, snapshot2, process_index);
1148 } else if (state.types2_(block2, frag2) != nullptr) {
1150 get_offset_type(real_addr_frag1, state.types2_(block2, frag2), offset1, size, snapshot1, process_index);
1152 get_offset_type(real_addr_frag2, state.types2_(block2, frag2), offset2, size, snapshot2, process_index);
1155 state.match_equals(previous);
1159 if (new_type1 != nullptr && new_type2 != nullptr && new_type1 != new_type2) {
1162 while (type->byte_size == 0 && type->subtype != nullptr)
1163 type = type->subtype;
1164 new_size1 = type->byte_size;
1167 while (type->byte_size == 0 && type->subtype != nullptr)
1168 type = type->subtype;
1169 new_size2 = type->byte_size;
1173 state.match_equals(previous);
1178 if (new_size1 > 0 && new_size1 == new_size2) {
1183 if (offset1 == 0 && offset2 == 0 &&
1184 not previous->insert(simgrid::mc::makeHeapLocationPair(block1, frag1, block2, frag2)).second) {
1186 state.match_equals(previous);
1192 state.match_equals(previous);
1196 if ((heapinfo1->busy_frag.ignore[frag1] > 0) &&
1197 (heapinfo2->busy_frag.ignore[frag2] == heapinfo1->busy_frag.ignore[frag1]))
1198 check_ignore = heapinfo1->busy_frag.ignore[frag1];
1204 /* Start comparison */
1207 res_compare = compare_heap_area_with_type(state, process_index, area1, area2, snapshot1, snapshot2, previous, type,
1208 size, check_ignore, pointer_level);
1210 res_compare = compare_heap_area_without_type(state, process_index, area1, area2, snapshot1, snapshot2, previous,
1211 size, check_ignore);
1213 if (res_compare == 1)
1217 state.match_equals(previous);
1224 /************************** Snapshot comparison *******************************/
1225 /******************************************************************************/
1227 static int compare_areas_with_type(simgrid::mc::StateComparator& state,
1229 void* real_area1, simgrid::mc::Snapshot* snapshot1, mc_mem_region_t region1,
1230 void* real_area2, simgrid::mc::Snapshot* snapshot2, mc_mem_region_t region2,
1231 simgrid::mc::Type* type, int pointer_level)
1233 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
1235 simgrid::mc::Type* subtype;
1236 simgrid::mc::Type* subsubtype;
1242 switch (type->type) {
1243 case DW_TAG_unspecified_type:
1246 case DW_TAG_base_type:
1247 case DW_TAG_enumeration_type:
1248 case DW_TAG_union_type:
1249 return MC_snapshot_region_memcmp(real_area1, region1, real_area2, region2, type->byte_size) != 0;
1250 case DW_TAG_typedef:
1251 case DW_TAG_volatile_type:
1252 case DW_TAG_const_type:
1254 type = type->subtype;
1255 continue; // restart
1256 case DW_TAG_array_type:
1257 subtype = type->subtype;
1258 switch (subtype->type) {
1259 case DW_TAG_unspecified_type:
1262 case DW_TAG_base_type:
1263 case DW_TAG_enumeration_type:
1264 case DW_TAG_pointer_type:
1265 case DW_TAG_reference_type:
1266 case DW_TAG_rvalue_reference_type:
1267 case DW_TAG_structure_type:
1268 case DW_TAG_class_type:
1269 case DW_TAG_union_type:
1270 if (subtype->full_type)
1271 subtype = subtype->full_type;
1272 elm_size = subtype->byte_size;
1274 case DW_TAG_const_type:
1275 case DW_TAG_typedef:
1276 case DW_TAG_volatile_type:
1277 subsubtype = subtype->subtype;
1278 if (subsubtype->full_type)
1279 subsubtype = subsubtype->full_type;
1280 elm_size = subsubtype->byte_size;
1286 for (i = 0; i < type->element_count; i++) {
1287 size_t off = i * elm_size;
1288 res = compare_areas_with_type(state, process_index, (char*)real_area1 + off, snapshot1, region1,
1289 (char*)real_area2 + off, snapshot2, region2, type->subtype, pointer_level);
1294 case DW_TAG_pointer_type:
1295 case DW_TAG_reference_type:
1296 case DW_TAG_rvalue_reference_type: {
1297 void* addr_pointed1 = MC_region_read_pointer(region1, real_area1);
1298 void* addr_pointed2 = MC_region_read_pointer(region2, real_area2);
1300 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type)
1301 return (addr_pointed1 != addr_pointed2);
1302 if (addr_pointed1 == nullptr && addr_pointed2 == nullptr)
1304 if (addr_pointed1 == nullptr || addr_pointed2 == nullptr)
1306 if (not state.compared_pointers.insert(std::make_pair(addr_pointed1, addr_pointed2)).second)
1311 // Some cases are not handled here:
1312 // * the pointers lead to different areas (one to the heap, the other to the RW segment ...)
1313 // * a pointer leads to the read-only segment of the current object
1314 // * a pointer lead to a different ELF object
1316 if (addr_pointed1 > process->heap_address && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)) {
1317 if (not(addr_pointed2 > process->heap_address && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)))
1319 // The pointers are both in the heap:
1320 return simgrid::mc::compare_heap_area(state, process_index, addr_pointed1, addr_pointed2, snapshot1,
1321 snapshot2, nullptr, type->subtype, pointer_level);
1323 } else if (region1->contain(simgrid::mc::remote(addr_pointed1))) {
1324 // The pointers are both in the current object R/W segment:
1325 if (not region2->contain(simgrid::mc::remote(addr_pointed2)))
1327 if (not type->type_id)
1328 return (addr_pointed1 != addr_pointed2);
1330 return compare_areas_with_type(state, process_index, addr_pointed1, snapshot1, region1, addr_pointed2,
1331 snapshot2, region2, type->subtype, pointer_level);
1334 // TODO, We do not handle very well the case where
1335 // it belongs to a different (non-heap) region from the current one.
1337 return (addr_pointed1 != addr_pointed2);
1341 case DW_TAG_structure_type:
1342 case DW_TAG_class_type:
1343 for (simgrid::mc::Member& member : type->members) {
1344 void* member1 = simgrid::dwarf::resolve_member(real_area1, type, &member, snapshot1, process_index);
1345 void* member2 = simgrid::dwarf::resolve_member(real_area2, type, &member, snapshot2, process_index);
1346 mc_mem_region_t subregion1 = mc_get_region_hinted(member1, snapshot1, process_index, region1);
1347 mc_mem_region_t subregion2 = mc_get_region_hinted(member2, snapshot2, process_index, region2);
1348 res = compare_areas_with_type(state, process_index, member1, snapshot1, subregion1, member2, snapshot2,
1349 subregion2, member.type, pointer_level);
1354 case DW_TAG_subroutine_type:
1358 XBT_VERB("Unknown case: %d", type->type);
1366 static int compare_global_variables(
1367 simgrid::mc::StateComparator& state,
1368 simgrid::mc::ObjectInformation* object_info,
1370 mc_mem_region_t r1, mc_mem_region_t r2,
1371 simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2)
1373 xbt_assert(r1 && r2, "Missing region.");
1376 if (r1->storage_type() == simgrid::mc::StorageType::Privatized) {
1377 xbt_assert(process_index >= 0);
1378 if (r2->storage_type() != simgrid::mc::StorageType::Privatized)
1381 size_t process_count = MC_smpi_process_count();
1382 xbt_assert(process_count == r1->privatized_data().size()
1383 && process_count == r2->privatized_data().size());
1385 // Compare the global variables separately for each simulates process:
1386 for (size_t process_index = 0; process_index < process_count; process_index++) {
1387 if (compare_global_variables(state,
1388 object_info, process_index,
1389 &r1->privatized_data()[process_index],
1390 &r2->privatized_data()[process_index],
1391 snapshot1, snapshot2))
1397 xbt_assert(r1->storage_type() != simgrid::mc::StorageType::Privatized);
1399 xbt_assert(r2->storage_type() != simgrid::mc::StorageType::Privatized);
1401 std::vector<simgrid::mc::Variable>& variables = object_info->global_variables;
1403 for (simgrid::mc::Variable const& current_var : variables) {
1405 // If the variable is not in this object, skip it:
1406 // We do not expect to find a pointer to something which is not reachable
1407 // by the global variables.
1408 if ((char *) current_var.address < (char *) object_info->start_rw
1409 || (char *) current_var.address > (char *) object_info->end_rw)
1412 simgrid::mc::Type* bvariable_type = current_var.type;
1413 int res = compare_areas_with_type(state, process_index,
1414 (char *) current_var.address, snapshot1, r1,
1415 (char *) current_var.address, snapshot2, r2,
1418 XBT_VERB("Global variable %s (%p) is different between snapshots",
1419 current_var.name.c_str(),
1420 (char *) current_var.address);
1428 static int compare_local_variables(simgrid::mc::StateComparator& state,
1430 simgrid::mc::Snapshot* snapshot1,
1431 simgrid::mc::Snapshot* snapshot2,
1432 mc_snapshot_stack_t stack1,
1433 mc_snapshot_stack_t stack2)
1435 if (stack1->local_variables.size() != stack2->local_variables.size()) {
1436 XBT_VERB("Different number of local variables");
1440 unsigned int cursor = 0;
1441 local_variable_t current_var1;
1442 local_variable_t current_var2;
1443 while (cursor < stack1->local_variables.size()) {
1444 current_var1 = &stack1->local_variables[cursor];
1445 current_var2 = &stack1->local_variables[cursor];
1446 if (current_var1->name != current_var2->name
1447 || current_var1->subprogram != current_var2->subprogram
1448 || current_var1->ip != current_var2->ip) {
1449 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1451 ("Different name of variable (%s - %s) "
1452 "or frame (%s - %s) or ip (%lu - %lu)",
1453 current_var1->name.c_str(),
1454 current_var2->name.c_str(),
1455 current_var1->subprogram->name.c_str(),
1456 current_var2->subprogram->name.c_str(),
1457 current_var1->ip, current_var2->ip);
1460 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1462 simgrid::mc::Type* subtype = current_var1->type;
1463 int res = compare_areas_with_type(
1464 state, process_index, current_var1->address, snapshot1,
1465 mc_get_snapshot_region(current_var1->address, snapshot1, process_index), current_var2->address, snapshot2,
1466 mc_get_snapshot_region(current_var2->address, snapshot2, process_index), subtype, 0);
1469 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1470 XBT_VERB("Local variable %s (%p - %p) in frame %s "
1471 "is different between snapshots",
1472 current_var1->name.c_str(), current_var1->address, current_var2->address,
1473 current_var1->subprogram->name.c_str());
1484 static std::unique_ptr<simgrid::mc::StateComparator> state_comparator;
1486 int snapshot_compare(int num1, simgrid::mc::Snapshot* s1, int num2, simgrid::mc::Snapshot* s2)
1488 // TODO, make this a field of ModelChecker or something similar
1490 if (state_comparator == nullptr)
1491 state_comparator = std::unique_ptr<StateComparator>(new StateComparator());
1493 state_comparator->clear();
1495 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
1499 int hash_result = 0;
1501 hash_result = (s1->hash != s2->hash);
1503 XBT_VERB("(%d - %d) Different hash: 0x%" PRIx64 "--0x%" PRIx64, num1, num2, s1->hash, s2->hash);
1508 XBT_VERB("(%d - %d) Same hash: 0x%" PRIx64, num1, num2, s1->hash);
1511 /* Compare enabled processes */
1512 if (s1->enabled_processes != s2->enabled_processes) {
1513 XBT_VERB("(%d - %d) Different amount of enabled processes", num1, num2);
1517 /* Compare size of stacks */
1519 for (unsigned long i = 0; i < s1->stacks.size(); i++) {
1520 size_t size_used1 = s1->stack_sizes[i];
1521 size_t size_used2 = s2->stack_sizes[i];
1522 if (size_used1 != size_used2) {
1524 XBT_DEBUG("(%d - %d) Different size used in stacks: %zu - %zu", num1, num2, size_used1, size_used2);
1529 XBT_VERB("(%d - %d) Different size used in stacks: %zu - %zu", num1, num2, size_used1, size_used2);
1535 if (is_diff) // do not proceed if there is any stacks that don't match
1538 /* Init heap information used in heap comparison algorithm */
1539 xbt_mheap_t heap1 = (xbt_mheap_t)s1->read_bytes(
1540 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1541 remote(process->heap_address),
1542 simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1543 xbt_mheap_t heap2 = (xbt_mheap_t)s2->read_bytes(
1544 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1545 remote(process->heap_address),
1546 simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1547 int res_init = state_comparator->initHeapInformation(heap1, heap2, &s1->to_ignore, &s2->to_ignore);
1549 if (res_init == -1) {
1551 XBT_DEBUG("(%d - %d) Different heap information", num1, num2);
1555 XBT_VERB("(%d - %d) Different heap information", num1, num2);
1562 /* Stacks comparison */
1564 for (unsigned int cursor = 0; cursor < s1->stacks.size(); cursor++) {
1565 mc_snapshot_stack_t stack1 = &s1->stacks[cursor];
1566 mc_snapshot_stack_t stack2 = &s2->stacks[cursor];
1568 if (stack1->process_index != stack2->process_index) {
1570 XBT_DEBUG("(%d - %d) Stacks with different process index (%i vs %i)", num1, num2,
1571 stack1->process_index, stack2->process_index);
1573 else diff_local = compare_local_variables(*state_comparator,
1574 stack1->process_index, s1, s2, stack1, stack2);
1575 if (diff_local > 0) {
1577 XBT_DEBUG("(%d - %d) Different local variables between stacks %d", num1,
1583 XBT_VERB("(%d - %d) Different local variables between stacks %u", num1, num2, cursor + 1);
1591 size_t regions_count = s1->snapshot_regions.size();
1592 // TODO, raise a difference instead?
1593 xbt_assert(regions_count == s2->snapshot_regions.size());
1595 for (size_t k = 0; k != regions_count; ++k) {
1596 mc_mem_region_t region1 = s1->snapshot_regions[k].get();
1597 mc_mem_region_t region2 = s2->snapshot_regions[k].get();
1600 if (region1->region_type() != simgrid::mc::RegionType::Data)
1603 xbt_assert(region1->region_type() == region2->region_type());
1604 xbt_assert(region1->object_info() == region2->object_info());
1605 xbt_assert(region1->object_info());
1607 std::string const& name = region1->object_info()->file_name;
1609 /* Compare global variables */
1610 if (compare_global_variables(*state_comparator, region1->object_info(), simgrid::mc::ProcessIndexDisabled, region1,
1614 XBT_DEBUG("(%d - %d) Different global variables in %s",
1615 num1, num2, name.c_str());
1619 XBT_VERB("(%d - %d) Different global variables in %s",
1620 num1, num2, name.c_str());
1629 if (simgrid::mc::mmalloc_compare_heap(*state_comparator, s1, s2) > 0) {
1632 XBT_DEBUG("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
1637 XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
1644 if (errors || hash_result)
1645 XBT_VERB("(%d - %d) Difference found", num1, num2);
1647 XBT_VERB("(%d - %d) No difference found", num1, num2);
1650 #if defined(MC_DEBUG) && defined(MC_VERBOSE)
1652 // * false positive SHOULD be avoided.
1653 // * There MUST not be any false negative.
1655 XBT_VERB("(%d - %d) State equality hash test is %s %s", num1, num2,
1656 (hash_result != 0) == (errors != 0) ? "true" : "false", not hash_result ? "positive" : "negative");
1660 return errors > 0 || hash_result;