1 /* Copyright (c) 2008-2019. 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_config.hpp"
36 #include "src/mc/mc_dwarf.hpp"
37 #include "src/mc/mc_forward.hpp"
38 #include "src/mc/mc_private.hpp"
39 #include "src/mc/mc_smx.hpp"
40 #include "src/mc/sosp/mc_snapshot.hpp"
42 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_compare, xbt, "Logging specific to mc_compare in mc");
48 typedef std::array<HeapLocation, 2> HeapLocationPair;
49 typedef std::set<HeapLocationPair> HeapLocationPairs;
51 struct ProcessComparisonState;
52 struct StateComparator;
54 static int compare_heap_area(
55 StateComparator& state,
56 int process_index, const void *area1, const void* area2,
57 Snapshot* snapshot1, Snapshot* snapshot2,
58 HeapLocationPairs* previous, Type* type, int pointer_level);
63 using simgrid::mc::remote;
65 /*********************************** Heap comparison ***********************************/
66 /***************************************************************************************/
76 HeapLocation() = default;
77 HeapLocation(int block, int fragment = 0) : block_(block), fragment_(fragment) {}
79 bool operator==(HeapLocation const& that) const
81 return block_ == that.block_ && fragment_ == that.fragment_;
83 bool operator<(HeapLocation const& that) const
85 return std::make_pair(block_, fragment_) < std::make_pair(that.block_, that.fragment_);
90 HeapLocationPair makeHeapLocationPair(int block1, int fragment1, int block2, int fragment2)
92 return simgrid::mc::HeapLocationPair{{
93 simgrid::mc::HeapLocation(block1, fragment1),
94 simgrid::mc::HeapLocation(block2, fragment2)
98 class HeapArea : public HeapLocation {
101 HeapArea() = default;
102 explicit HeapArea(int block) : valid_(true) { block_ = block; }
103 HeapArea(int block, int fragment) : valid_(true)
106 fragment_ = fragment;
110 class ProcessComparisonState {
112 std::vector<simgrid::mc::IgnoredHeapRegion>* to_ignore = nullptr;
113 std::vector<HeapArea> equals_to;
114 std::vector<simgrid::mc::Type*> types;
115 std::size_t heapsize = 0;
117 void initHeapInformation(xbt_mheap_t heap, std::vector<simgrid::mc::IgnoredHeapRegion>* i);
122 /** A hash which works with more stuff
124 * It can hash pairs: the standard hash currently doesn't include this.
126 template <class X> class hash : public std::hash<X> {
129 template <class X, class Y> class hash<std::pair<X, Y>> {
131 std::size_t operator()(std::pair<X,Y>const& x) const
135 return h1(x.first) ^ h2(x.second);
141 class StateComparator {
143 s_xbt_mheap_t std_heap_copy;
144 std::size_t heaplimit;
145 std::array<ProcessComparisonState, 2> processStates;
147 std::unordered_set<std::pair<void*, void*>, hash<std::pair<void*, void*>>> compared_pointers;
151 compared_pointers.clear();
154 int initHeapInformation(
155 xbt_mheap_t heap1, xbt_mheap_t heap2,
156 std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
157 std::vector<simgrid::mc::IgnoredHeapRegion>* i2);
159 HeapArea& equals_to1_(std::size_t i, std::size_t j)
161 return processStates[0].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
163 HeapArea& equals_to2_(std::size_t i, std::size_t j)
165 return processStates[1].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
167 Type*& types1_(std::size_t i, std::size_t j)
169 return processStates[0].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
171 Type*& types2_(std::size_t i, std::size_t j)
173 return processStates[1].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
176 HeapArea const& equals_to1_(std::size_t i, std::size_t j) const
178 return processStates[0].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
180 HeapArea const& equals_to2_(std::size_t i, std::size_t j) const
182 return processStates[1].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
184 Type* const& types1_(std::size_t i, std::size_t j) const
186 return processStates[0].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
188 Type* const& types2_(std::size_t i, std::size_t j) const
190 return processStates[1].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
193 /** Check whether two blocks are known to be matching
195 * @param b1 Block of state 1
196 * @param b2 Block of state 2
197 * @return if the blocks are known to be matching
199 bool blocksEqual(int b1, int b2) const
201 return this->equals_to1_(b1, 0).block_ == b2 && this->equals_to2_(b2, 0).block_ == b1;
204 /** Check whether two fragments are known to be matching
206 * @param b1 Block of state 1
207 * @param f1 Fragment of state 1
208 * @param b2 Block of state 2
209 * @param f2 Fragment of state 2
210 * @return if the fragments are known to be matching
212 int fragmentsEqual(int b1, int f1, int b2, int f2) const
214 return this->equals_to1_(b1, f1).block_ == b2 && this->equals_to1_(b1, f1).fragment_ == f2 &&
215 this->equals_to2_(b2, f2).block_ == b1 && this->equals_to2_(b2, f2).fragment_ == f1;
218 void match_equals(HeapLocationPairs* list);
224 /************************************************************************************/
226 static ssize_t heap_comparison_ignore_size(
227 std::vector<simgrid::mc::IgnoredHeapRegion>* ignore_list,
231 int end = ignore_list->size() - 1;
233 while (start <= end) {
234 unsigned int cursor = (start + end) / 2;
235 simgrid::mc::IgnoredHeapRegion const& region = (*ignore_list)[cursor];
236 if (region.address == address)
238 if (region.address < address)
240 if (region.address > address)
247 static bool is_stack(const void *address)
249 for (auto const& stack : mc_model_checker->process().stack_areas())
250 if (address == stack.address)
255 // TODO, this should depend on the snapshot?
256 static bool is_block_stack(int block)
258 for (auto const& stack : mc_model_checker->process().stack_areas())
259 if (block == stack.block)
267 void StateComparator::match_equals(HeapLocationPairs* list)
269 for (auto const& pair : *list) {
270 if (pair[0].fragment_ != -1) {
271 this->equals_to1_(pair[0].block_, pair[0].fragment_) = simgrid::mc::HeapArea(pair[1].block_, pair[1].fragment_);
272 this->equals_to2_(pair[1].block_, pair[1].fragment_) = simgrid::mc::HeapArea(pair[0].block_, pair[0].fragment_);
274 this->equals_to1_(pair[0].block_, 0) = simgrid::mc::HeapArea(pair[1].block_, pair[1].fragment_);
275 this->equals_to2_(pair[1].block_, 0) = simgrid::mc::HeapArea(pair[0].block_, pair[0].fragment_);
280 void ProcessComparisonState::initHeapInformation(xbt_mheap_t heap,
281 std::vector<simgrid::mc::IgnoredHeapRegion>* i)
283 auto heaplimit = heap->heaplimit;
284 this->heapsize = heap->heapsize;
286 this->equals_to.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, HeapArea());
287 this->types.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, nullptr);
290 int StateComparator::initHeapInformation(xbt_mheap_t heap1, xbt_mheap_t heap2,
291 std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
292 std::vector<simgrid::mc::IgnoredHeapRegion>* i2)
294 if ((heap1->heaplimit != heap2->heaplimit) || (heap1->heapsize != heap2->heapsize))
296 this->heaplimit = heap1->heaplimit;
297 this->std_heap_copy = *mc_model_checker->process().get_heap();
298 this->processStates[0].initHeapInformation(heap1, i1);
299 this->processStates[1].initHeapInformation(heap2, i2);
303 // TODO, have a robust way to find it in O(1)
304 static inline RegionSnapshot* MC_get_heap_region(Snapshot* snapshot)
306 for (auto const& region : snapshot->snapshot_regions_)
307 if (region->region_type() == simgrid::mc::RegionType::Heap)
309 xbt_die("No heap region");
313 int mmalloc_compare_heap(
314 simgrid::mc::StateComparator& state, simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2)
316 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
318 /* Start comparison */
323 /* Check busy blocks */
326 malloc_info heapinfo_temp1;
327 malloc_info heapinfo_temp2;
328 malloc_info heapinfo_temp2b;
330 simgrid::mc::RegionSnapshot* heap_region1 = MC_get_heap_region(snapshot1);
331 simgrid::mc::RegionSnapshot* heap_region2 = MC_get_heap_region(snapshot2);
333 // This is the address of std_heap->heapinfo in the application process:
334 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
336 // This is in snapshot do not use them directly:
337 const malloc_info* heapinfos1 = snapshot1->read<malloc_info*>(
338 RemotePtr<malloc_info*>((std::uint64_t)heapinfo_address), simgrid::mc::ProcessIndexMissing);
339 const malloc_info* heapinfos2 = snapshot2->read<malloc_info*>(
340 RemotePtr<malloc_info*>((std::uint64_t)heapinfo_address), simgrid::mc::ProcessIndexMissing);
342 while (i1 < state.heaplimit) {
344 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(heap_region1, &heapinfo_temp1, &heapinfos1[i1], sizeof(malloc_info));
345 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2, &heapinfos2[i1], sizeof(malloc_info));
347 if (heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type == MMALLOC_TYPE_HEAPINFO) { /* Free block */
352 if (heapinfo1->type < 0) {
353 fprintf(stderr, "Unkown mmalloc block type.\n");
357 void* addr_block1 = ((void*)(((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase));
359 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED) { /* Large block */
361 if (is_stack(addr_block1)) {
362 for (size_t k = 0; k < heapinfo1->busy_block.size; k++)
363 state.equals_to1_(i1 + k, 0) = HeapArea(i1, -1);
364 for (size_t k = 0; k < heapinfo2->busy_block.size; k++)
365 state.equals_to2_(i1 + k, 0) = HeapArea(i1, -1);
366 i1 += heapinfo1->busy_block.size;
370 if (state.equals_to1_(i1, 0).valid_) {
378 /* Try first to associate to same block in the other heap */
379 if (heapinfo2->type == heapinfo1->type && state.equals_to2_(i1, 0).valid_ == 0) {
380 void* addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
381 int res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing, addr_block1, addr_block2,
382 snapshot1, snapshot2, nullptr, nullptr, 0);
383 if (res_compare != 1) {
384 for (size_t k = 1; k < heapinfo2->busy_block.size; k++)
385 state.equals_to2_(i1 + k, 0) = HeapArea(i1, -1);
386 for (size_t k = 1; k < heapinfo1->busy_block.size; k++)
387 state.equals_to1_(i1 + k, 0) = HeapArea(i1, -1);
389 i1 += heapinfo1->busy_block.size;
393 while (i2 < state.heaplimit && not equal) {
395 void* addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
402 const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2b, &heapinfos2[i2], sizeof(malloc_info));
404 if (heapinfo2b->type != MMALLOC_TYPE_UNFRAGMENTED) {
409 if (state.equals_to2_(i2, 0).valid_) {
414 int res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing, addr_block1, addr_block2,
415 snapshot1, snapshot2, nullptr, nullptr, 0);
417 if (res_compare != 1) {
418 for (size_t k = 1; k < heapinfo2b->busy_block.size; k++)
419 state.equals_to2_(i2 + k, 0) = HeapArea(i1, -1);
420 for (size_t k = 1; k < heapinfo1->busy_block.size; k++)
421 state.equals_to1_(i1 + k, 0) = HeapArea(i2, -1);
423 i1 += heapinfo1->busy_block.size;
430 XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1, heapinfo1->busy_block.busy_size, addr_block1);
431 i1 = state.heaplimit + 1;
435 } else { /* Fragmented block */
437 for (size_t j1 = 0; j1 < (size_t)(BLOCKSIZE >> heapinfo1->type); j1++) {
439 if (heapinfo1->busy_frag.frag_size[j1] == -1) /* Free fragment_ */
442 if (state.equals_to1_(i1, j1).valid_)
445 void* addr_frag1 = (void*)((char*)addr_block1 + (j1 << heapinfo1->type));
450 /* Try first to associate to same fragment_ in the other heap */
451 if (heapinfo2->type == heapinfo1->type && not state.equals_to2_(i1, j1).valid_) {
452 void* addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
453 void* addr_frag2 = (void*)((char*)addr_block2 + (j1 << heapinfo2->type));
454 int res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing, addr_frag1, addr_frag2,
455 snapshot1, snapshot2, nullptr, nullptr, 0);
456 if (res_compare != 1)
460 while (i2 < state.heaplimit && not equal) {
462 const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(
463 heap_region2, &heapinfo_temp2b, &heapinfos2[i2],
464 sizeof(malloc_info));
466 if (heapinfo2b->type == MMALLOC_TYPE_FREE || heapinfo2b->type == MMALLOC_TYPE_HEAPINFO) {
471 // We currently do not match fragments with unfragmented blocks (maybe we should).
472 if (heapinfo2b->type == MMALLOC_TYPE_UNFRAGMENTED) {
477 if (heapinfo2b->type < 0) {
478 fprintf(stderr, "Unknown mmalloc block type.\n");
482 for (size_t j2 = 0; j2 < (size_t)(BLOCKSIZE >> heapinfo2b->type); j2++) {
484 if (i2 == i1 && j2 == j1)
487 if (state.equals_to2_(i2, j2).valid_)
490 void* addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
491 void* addr_frag2 = (void*)((char*)addr_block2 + (j2 << heapinfo2b->type));
493 int res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing, addr_frag1, addr_frag2,
494 snapshot2, snapshot2, nullptr, nullptr, 0);
495 if (res_compare != 1) {
505 XBT_DEBUG("Block %zu, fragment_ %zu not found (size_used = %zd, address = %p)\n", i1, j1,
506 heapinfo1->busy_frag.frag_size[j1], addr_frag1);
507 i1 = state.heaplimit + 1;
517 /* All blocks/fragments are equal to another block/fragment_ ? */
518 for (size_t i = 1; i < state.heaplimit; i++) {
519 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
520 heap_region1, &heapinfo_temp1, &heapinfos1[i], sizeof(malloc_info));
522 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED && i1 == state.heaplimit && heapinfo1->busy_block.busy_size > 0 &&
523 not state.equals_to1_(i, 0).valid_) {
524 XBT_DEBUG("Block %zu not found (size used = %zu)", i, heapinfo1->busy_block.busy_size);
528 if (heapinfo1->type <= 0)
530 for (size_t j = 0; j < (size_t)(BLOCKSIZE >> heapinfo1->type); j++)
531 if (i1 == state.heaplimit && heapinfo1->busy_frag.frag_size[j] > 0 && not state.equals_to1_(i, j).valid_) {
532 XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)", i, j, heapinfo1->busy_frag.frag_size[j]);
537 if (i1 == state.heaplimit)
538 XBT_DEBUG("Number of blocks/fragments not found in heap1: %d", nb_diff1);
540 for (size_t i = 1; i < state.heaplimit; i++) {
541 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
542 heap_region2, &heapinfo_temp2, &heapinfos2[i], sizeof(malloc_info));
543 if (heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED && i1 == state.heaplimit && heapinfo2->busy_block.busy_size > 0 &&
544 not state.equals_to2_(i, 0).valid_) {
545 XBT_DEBUG("Block %zu not found (size used = %zu)", i,
546 heapinfo2->busy_block.busy_size);
550 if (heapinfo2->type <= 0)
553 for (size_t j = 0; j < (size_t)(BLOCKSIZE >> heapinfo2->type); j++)
554 if (i1 == state.heaplimit && heapinfo2->busy_frag.frag_size[j] > 0 && not state.equals_to2_(i, j).valid_) {
555 XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)",
556 i, j, heapinfo2->busy_frag.frag_size[j]);
562 if (i1 == state.heaplimit)
563 XBT_DEBUG("Number of blocks/fragments not found in heap2: %d", nb_diff2);
565 return nb_diff1 > 0 || nb_diff2 > 0;
571 * @param real_area1 Process address for state 1
572 * @param real_area2 Process address for state 2
573 * @param snapshot1 Snapshot of state 1
574 * @param snapshot2 Snapshot of state 2
577 * @param check_ignore
579 static int compare_heap_area_without_type(
580 simgrid::mc::StateComparator& state, int process_index,
581 const void *real_area1, const void *real_area2,
582 simgrid::mc::Snapshot* snapshot1,
583 simgrid::mc::Snapshot* snapshot2,
584 HeapLocationPairs* previous, int size,
587 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
588 simgrid::mc::RegionSnapshot* heap_region1 = MC_get_heap_region(snapshot1);
589 simgrid::mc::RegionSnapshot* heap_region2 = MC_get_heap_region(snapshot2);
591 for (int i = 0; i < size; ) {
593 if (check_ignore > 0) {
594 ssize_t ignore1 = heap_comparison_ignore_size(
595 state.processStates[0].to_ignore, (char *) real_area1 + i);
597 ssize_t ignore2 = heap_comparison_ignore_size(
598 state.processStates[1].to_ignore, (char *) real_area2 + i);
599 if (ignore2 == ignore1) {
612 if (MC_snapshot_region_memcmp(((char *) real_area1) + i, heap_region1, ((char *) real_area2) + i, heap_region2, 1) != 0) {
614 int pointer_align = (i / sizeof(void *)) * sizeof(void *);
615 const void* addr_pointed1 = snapshot1->read(
616 remote((void**)((char *) real_area1 + pointer_align)), process_index);
617 const void* addr_pointed2 = snapshot2->read(
618 remote((void**)((char *) real_area2 + pointer_align)), process_index);
620 if (process->in_maestro_stack(remote(addr_pointed1))
621 && process->in_maestro_stack(remote(addr_pointed2))) {
622 i = pointer_align + sizeof(void *);
626 if (addr_pointed1 > state.std_heap_copy.heapbase
627 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
628 && addr_pointed2 > state.std_heap_copy.heapbase
629 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)) {
630 // Both addreses are in the heap:
631 int res_compare = compare_heap_area(state ,process_index,
632 addr_pointed1, addr_pointed2,
633 snapshot1, snapshot2, previous, nullptr, 0);
634 if (res_compare == 1)
636 i = pointer_align + sizeof(void *);
652 * @param real_area1 Process address for state 1
653 * @param real_area2 Process address for state 2
654 * @param snapshot1 Snapshot of state 1
655 * @param snapshot2 Snapshot of state 2
658 * @param area_size either a byte_size or an elements_count (?)
659 * @param check_ignore
660 * @param pointer_level
661 * @return 0 (same), 1 (different), -1 (unknown)
663 static int compare_heap_area_with_type(
664 simgrid::mc::StateComparator& state, int process_index,
665 const void *real_area1, const void *real_area2,
666 simgrid::mc::Snapshot* snapshot1,
667 simgrid::mc::Snapshot* snapshot2,
668 HeapLocationPairs* previous, simgrid::mc::Type* type,
669 int area_size, int check_ignore,
674 // HACK: This should not happen but in pratice, there are some
675 // DW_TAG_typedef without an associated DW_AT_type:
676 //<1><538832>: Abbrev Number: 111 (DW_TAG_typedef)
677 // <538833> DW_AT_name : (indirect string, offset: 0x2292f3): gregset_t
678 // <538837> DW_AT_decl_file : 98
679 // <538838> DW_AT_decl_line : 37
683 if (is_stack(real_area1) && is_stack(real_area2))
686 if (check_ignore > 0) {
687 ssize_t ignore1 = heap_comparison_ignore_size(state.processStates[0].to_ignore, real_area1);
688 if (ignore1 > 0 && heap_comparison_ignore_size(state.processStates[1].to_ignore, real_area2) == ignore1)
692 simgrid::mc::Type* subtype;
693 simgrid::mc::Type* subsubtype;
695 const void* addr_pointed1;
696 const void* addr_pointed2;
698 simgrid::mc::RegionSnapshot* heap_region1 = MC_get_heap_region(snapshot1);
699 simgrid::mc::RegionSnapshot* heap_region2 = MC_get_heap_region(snapshot2);
701 switch (type->type) {
702 case DW_TAG_unspecified_type:
705 case DW_TAG_base_type:
706 if (not type->name.empty() && type->name == "char") { /* String, hence random (arbitrary ?) size */
707 if (real_area1 == real_area2)
710 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, area_size) != 0;
712 if (area_size != -1 && type->byte_size != area_size)
715 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
718 case DW_TAG_enumeration_type:
719 if (area_size != -1 && type->byte_size != area_size)
721 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
724 case DW_TAG_const_type:
725 case DW_TAG_volatile_type:
727 type = type->subtype;
730 case DW_TAG_array_type:
731 subtype = type->subtype;
732 switch (subtype->type) {
733 case DW_TAG_unspecified_type:
736 case DW_TAG_base_type:
737 case DW_TAG_enumeration_type:
738 case DW_TAG_pointer_type:
739 case DW_TAG_reference_type:
740 case DW_TAG_rvalue_reference_type:
741 case DW_TAG_structure_type:
742 case DW_TAG_class_type:
743 case DW_TAG_union_type:
744 if (subtype->full_type)
745 subtype = subtype->full_type;
746 elm_size = subtype->byte_size;
748 // TODO, just remove the type indirection?
749 case DW_TAG_const_type:
751 case DW_TAG_volatile_type:
752 subsubtype = subtype->subtype;
753 if (subsubtype->full_type)
754 subsubtype = subsubtype->full_type;
755 elm_size = subsubtype->byte_size;
760 for (int i = 0; i < type->element_count; i++) {
761 // TODO, add support for variable stride (DW_AT_byte_stride)
762 int res = compare_heap_area_with_type(state, process_index, (char*)real_area1 + (i * elm_size),
763 (char*)real_area2 + (i * elm_size), snapshot1, snapshot2, previous,
764 type->subtype, subtype->byte_size, check_ignore, pointer_level);
770 case DW_TAG_reference_type:
771 case DW_TAG_rvalue_reference_type:
772 case DW_TAG_pointer_type:
773 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
774 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
775 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
776 return (addr_pointed1 != addr_pointed2);
779 if (pointer_level <= 1) {
780 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
781 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
782 if (addr_pointed1 > state.std_heap_copy.heapbase && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1) &&
783 addr_pointed2 > state.std_heap_copy.heapbase && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
784 return compare_heap_area(state, process_index, addr_pointed1, addr_pointed2, snapshot1, snapshot2, previous,
785 type->subtype, pointer_level);
787 return (addr_pointed1 != addr_pointed2);
789 for (size_t i = 0; i < (area_size / sizeof(void*)); i++) {
790 addr_pointed1 = snapshot1->read(remote((void**)((char*)real_area1 + i * sizeof(void*))), process_index);
791 addr_pointed2 = snapshot2->read(remote((void**)((char*)real_area2 + i * sizeof(void*))), process_index);
793 if (addr_pointed1 > state.std_heap_copy.heapbase && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1) &&
794 addr_pointed2 > state.std_heap_copy.heapbase && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
795 res = compare_heap_area(state, process_index, addr_pointed1, addr_pointed2, snapshot1, snapshot2, previous,
796 type->subtype, pointer_level);
798 res = (addr_pointed1 != addr_pointed2);
804 case DW_TAG_structure_type:
805 case DW_TAG_class_type:
807 type = type->full_type;
808 if (area_size != -1 && type->byte_size != area_size) {
809 if (area_size <= type->byte_size || area_size % type->byte_size != 0)
811 for (size_t i = 0; i < (size_t)(area_size / type->byte_size); i++) {
812 int res = compare_heap_area_with_type(state, process_index, (char*)real_area1 + i * type->byte_size,
813 (char*)real_area2 + i * type->byte_size, snapshot1, snapshot2,
814 previous, type, -1, check_ignore, 0);
819 for (simgrid::mc::Member& member : type->members) {
820 // TODO, optimize this? (for the offset case)
821 void* real_member1 = simgrid::dwarf::resolve_member(real_area1, type, &member,
822 (simgrid::mc::AddressSpace*)snapshot1, process_index);
823 void* real_member2 = simgrid::dwarf::resolve_member(real_area2, type, &member,
824 (simgrid::mc::AddressSpace*)snapshot2, process_index);
825 int res = compare_heap_area_with_type(state, process_index, real_member1, real_member2, snapshot1,
826 snapshot2, previous, member.type, -1, check_ignore, 0);
833 case DW_TAG_union_type:
834 return compare_heap_area_without_type(state, process_index, real_area1, real_area2, snapshot1, snapshot2,
835 previous, type->byte_size, check_ignore);
841 xbt_die("Unreachable");
845 /** Infer the type of a part of the block from the type of the block
847 * TODO, handle DW_TAG_array_type as well as arrays of the object ((*p)[5], p[5])
849 * TODO, handle subfields ((*p).bar.foo, (*p)[5].bar…)
851 * @param type DWARF type ID of the root address
853 * @return DWARF type ID for given offset
855 static simgrid::mc::Type* get_offset_type(void *real_base_address, simgrid::mc::Type* type,
856 int offset, int area_size,
857 simgrid::mc::Snapshot* snapshot, int process_index)
860 // Beginning of the block, the infered variable type if the type of the block:
864 switch (type->type) {
866 case DW_TAG_structure_type:
867 case DW_TAG_class_type:
869 type = type->full_type;
870 if (area_size != -1 && type->byte_size != area_size) {
871 if (area_size > type->byte_size && area_size % type->byte_size == 0)
877 for (simgrid::mc::Member& member : type->members) {
878 if (member.has_offset_location()) {
879 // We have the offset, use it directly (shortcut):
880 if (member.offset() == offset)
883 void* real_member = simgrid::dwarf::resolve_member(real_base_address, type, &member, snapshot, process_index);
884 if ((char*)real_member - (char*)real_base_address == offset)
891 /* FIXME: other cases ? */
899 * @param area1 Process address for state 1
900 * @param area2 Process address for state 2
901 * @param snapshot1 Snapshot of state 1
902 * @param snapshot2 Snapshot of state 2
903 * @param previous Pairs of blocks already compared on the current path (or nullptr)
904 * @param type_id Type of variable
905 * @param pointer_level
906 * @return 0 (same), 1 (different), -1
909 int compare_heap_area(simgrid::mc::StateComparator& state, int process_index,
910 const void *area1, const void *area2,
911 simgrid::mc::Snapshot* snapshot1,
912 simgrid::mc::Snapshot* snapshot2,
913 HeapLocationPairs* previous,
914 simgrid::mc::Type* type, int pointer_level)
916 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
921 int check_ignore = 0;
929 simgrid::mc::Type* new_type1 = nullptr;
930 simgrid::mc::Type* new_type2 = nullptr;
932 bool match_pairs = false;
934 // This is the address of std_heap->heapinfo in the application process:
935 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
937 const malloc_info* heapinfos1 = snapshot1->read(remote((const malloc_info**)heapinfo_address), process_index);
938 const malloc_info* heapinfos2 = snapshot2->read(remote((const malloc_info**)heapinfo_address), process_index);
940 malloc_info heapinfo_temp1;
941 malloc_info heapinfo_temp2;
943 simgrid::mc::HeapLocationPairs current;
944 if (previous == nullptr) {
950 block1 = ((char*)area1 - (char*)state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
951 block2 = ((char*)area2 - (char*)state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
953 // If either block is a stack block:
954 if (is_block_stack((int) block1) && is_block_stack((int) block2)) {
955 previous->insert(simgrid::mc::makeHeapLocationPair(block1, -1, block2, -1));
957 state.match_equals(previous);
961 // If either block is not in the expected area of memory:
962 if (((char*)area1 < (char*)state.std_heap_copy.heapbase) || (block1 > (ssize_t)state.processStates[0].heapsize) ||
963 (block1 < 1) || ((char*)area2 < (char*)state.std_heap_copy.heapbase) ||
964 (block2 > (ssize_t)state.processStates[1].heapsize) || (block2 < 1)) {
968 // Process address of the block:
969 void* real_addr_block1 = (ADDR2UINT(block1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
970 void* real_addr_block2 = (ADDR2UINT(block2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
974 type = type->full_type;
976 // This assume that for "boring" types (volatile ...) byte_size is absent:
977 while (type->byte_size == 0 && type->subtype != nullptr)
978 type = type->subtype;
981 if (type->type == DW_TAG_pointer_type ||
982 (type->type == DW_TAG_base_type && not type->name.empty() && type->name == "char"))
985 type_size = type->byte_size;
989 simgrid::mc::RegionSnapshot* heap_region1 = MC_get_heap_region(snapshot1);
990 simgrid::mc::RegionSnapshot* heap_region2 = MC_get_heap_region(snapshot2);
992 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
993 heap_region1, &heapinfo_temp1, &heapinfos1[block1], sizeof(malloc_info));
994 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
995 heap_region2, &heapinfo_temp2, &heapinfos2[block2], sizeof(malloc_info));
997 if ((heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type==MMALLOC_TYPE_HEAPINFO)
998 && (heapinfo2->type == MMALLOC_TYPE_FREE || heapinfo2->type ==MMALLOC_TYPE_HEAPINFO)) {
1001 state.match_equals(previous);
1005 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED && heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
1006 /* Complete block */
1008 // TODO, lookup variable type from block type as done for fragmented blocks
1010 if (state.equals_to1_(block1, 0).valid_ && state.equals_to2_(block2, 0).valid_ &&
1011 state.blocksEqual(block1, block2)) {
1013 state.match_equals(previous);
1017 if (type_size != -1 && type_size != (ssize_t)heapinfo1->busy_block.busy_size &&
1018 type_size != (ssize_t)heapinfo2->busy_block.busy_size &&
1019 (type->name.empty() || type->name == "struct s_smx_context")) {
1021 state.match_equals(previous);
1025 if (heapinfo1->busy_block.size != heapinfo2->busy_block.size)
1027 if (heapinfo1->busy_block.busy_size != heapinfo2->busy_block.busy_size)
1030 if (not previous->insert(simgrid::mc::makeHeapLocationPair(block1, -1, block2, -1)).second) {
1032 state.match_equals(previous);
1036 size = heapinfo1->busy_block.busy_size;
1038 // Remember (basic) type inference.
1039 // The current data structure only allows us to do this for the whole block.
1040 if (type != nullptr && area1 == real_addr_block1)
1041 state.types1_(block1, 0) = type;
1042 if (type != nullptr && area2 == real_addr_block2)
1043 state.types2_(block2, 0) = type;
1047 state.match_equals(previous);
1051 if (heapinfo1->busy_block.ignore > 0
1052 && heapinfo2->busy_block.ignore == heapinfo1->busy_block.ignore)
1053 check_ignore = heapinfo1->busy_block.ignore;
1055 } else if ((heapinfo1->type > 0) && (heapinfo2->type > 0)) { /* Fragmented block */
1058 ssize_t frag1 = ((uintptr_t)(ADDR2UINT(area1) % (BLOCKSIZE))) >> heapinfo1->type;
1059 ssize_t frag2 = ((uintptr_t)(ADDR2UINT(area2) % (BLOCKSIZE))) >> heapinfo2->type;
1061 // Process address of the fragment_:
1062 void* real_addr_frag1 = (void*)((char*)real_addr_block1 + (frag1 << heapinfo1->type));
1063 void* real_addr_frag2 = (void*)((char*)real_addr_block2 + (frag2 << heapinfo2->type));
1065 // Check the size of the fragments against the size of the type:
1066 if (type_size != -1) {
1067 if (heapinfo1->busy_frag.frag_size[frag1] == -1 || heapinfo2->busy_frag.frag_size[frag2] == -1) {
1069 state.match_equals(previous);
1073 if (type_size != heapinfo1->busy_frag.frag_size[frag1]
1074 || type_size != heapinfo2->busy_frag.frag_size[frag2]) {
1076 state.match_equals(previous);
1081 // Check if the blocks are already matched together:
1082 if (state.equals_to1_(block1, frag1).valid_ && state.equals_to2_(block2, frag2).valid_ && offset1 == offset2 &&
1083 state.fragmentsEqual(block1, frag1, block2, frag2)) {
1085 state.match_equals(previous);
1088 // Compare the size of both fragments:
1089 if (heapinfo1->busy_frag.frag_size[frag1] != heapinfo2->busy_frag.frag_size[frag2]) {
1090 if (type_size == -1) {
1092 state.match_equals(previous);
1098 // Size of the fragment_:
1099 size = heapinfo1->busy_frag.frag_size[frag1];
1101 // Remember (basic) type inference.
1102 // The current data structure only allows us to do this for the whole fragment_.
1103 if (type != nullptr && area1 == real_addr_frag1)
1104 state.types1_(block1, frag1) = type;
1105 if (type != nullptr && area2 == real_addr_frag2)
1106 state.types2_(block2, frag2) = type;
1108 // The type of the variable is already known:
1110 new_type1 = new_type2 = type;
1112 // Type inference from the block type.
1113 else if (state.types1_(block1, frag1) != nullptr || state.types2_(block2, frag2) != nullptr) {
1115 offset1 = (char*)area1 - (char*)real_addr_frag1;
1116 offset2 = (char*)area2 - (char*)real_addr_frag2;
1118 if (state.types1_(block1, frag1) != nullptr && state.types2_(block2, frag2) != nullptr) {
1120 get_offset_type(real_addr_frag1, state.types1_(block1, frag1), offset1, size, snapshot1, process_index);
1122 get_offset_type(real_addr_frag2, state.types2_(block2, frag2), offset1, size, snapshot2, process_index);
1123 } else if (state.types1_(block1, frag1) != nullptr) {
1125 get_offset_type(real_addr_frag1, state.types1_(block1, frag1), offset1, size, snapshot1, process_index);
1127 get_offset_type(real_addr_frag2, state.types1_(block1, frag1), offset2, size, snapshot2, process_index);
1128 } else if (state.types2_(block2, frag2) != nullptr) {
1130 get_offset_type(real_addr_frag1, state.types2_(block2, frag2), offset1, size, snapshot1, process_index);
1132 get_offset_type(real_addr_frag2, state.types2_(block2, frag2), offset2, size, snapshot2, process_index);
1135 state.match_equals(previous);
1139 if (new_type1 != nullptr && new_type2 != nullptr && new_type1 != new_type2) {
1142 while (type->byte_size == 0 && type->subtype != nullptr)
1143 type = type->subtype;
1144 new_size1 = type->byte_size;
1147 while (type->byte_size == 0 && type->subtype != nullptr)
1148 type = type->subtype;
1149 new_size2 = type->byte_size;
1153 state.match_equals(previous);
1158 if (new_size1 > 0 && new_size1 == new_size2) {
1163 if (offset1 == 0 && offset2 == 0 &&
1164 not previous->insert(simgrid::mc::makeHeapLocationPair(block1, frag1, block2, frag2)).second) {
1166 state.match_equals(previous);
1172 state.match_equals(previous);
1176 if ((heapinfo1->busy_frag.ignore[frag1] > 0) &&
1177 (heapinfo2->busy_frag.ignore[frag2] == heapinfo1->busy_frag.ignore[frag1]))
1178 check_ignore = heapinfo1->busy_frag.ignore[frag1];
1184 /* Start comparison */
1187 res_compare = compare_heap_area_with_type(state, process_index, area1, area2, snapshot1, snapshot2, previous, type,
1188 size, check_ignore, pointer_level);
1190 res_compare = compare_heap_area_without_type(state, process_index, area1, area2, snapshot1, snapshot2, previous,
1191 size, check_ignore);
1193 if (res_compare == 1)
1197 state.match_equals(previous);
1204 /************************** Snapshot comparison *******************************/
1205 /******************************************************************************/
1207 static int compare_areas_with_type(simgrid::mc::StateComparator& state, int process_index, void* real_area1,
1208 simgrid::mc::Snapshot* snapshot1, simgrid::mc::RegionSnapshot* region1,
1209 void* real_area2, simgrid::mc::Snapshot* snapshot2,
1210 simgrid::mc::RegionSnapshot* region2, simgrid::mc::Type* type, int pointer_level)
1212 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
1214 simgrid::mc::Type* subtype;
1215 simgrid::mc::Type* subsubtype;
1221 xbt_assert(type != nullptr);
1222 switch (type->type) {
1223 case DW_TAG_unspecified_type:
1226 case DW_TAG_base_type:
1227 case DW_TAG_enumeration_type:
1228 case DW_TAG_union_type:
1229 return MC_snapshot_region_memcmp(real_area1, region1, real_area2, region2, type->byte_size) != 0;
1230 case DW_TAG_typedef:
1231 case DW_TAG_volatile_type:
1232 case DW_TAG_const_type:
1234 type = type->subtype;
1235 continue; // restart
1236 case DW_TAG_array_type:
1237 subtype = type->subtype;
1238 switch (subtype->type) {
1239 case DW_TAG_unspecified_type:
1242 case DW_TAG_base_type:
1243 case DW_TAG_enumeration_type:
1244 case DW_TAG_pointer_type:
1245 case DW_TAG_reference_type:
1246 case DW_TAG_rvalue_reference_type:
1247 case DW_TAG_structure_type:
1248 case DW_TAG_class_type:
1249 case DW_TAG_union_type:
1250 if (subtype->full_type)
1251 subtype = subtype->full_type;
1252 elm_size = subtype->byte_size;
1254 case DW_TAG_const_type:
1255 case DW_TAG_typedef:
1256 case DW_TAG_volatile_type:
1257 subsubtype = subtype->subtype;
1258 if (subsubtype->full_type)
1259 subsubtype = subsubtype->full_type;
1260 elm_size = subsubtype->byte_size;
1265 for (i = 0; i < type->element_count; i++) {
1266 size_t off = i * elm_size;
1267 res = compare_areas_with_type(state, process_index, (char*)real_area1 + off, snapshot1, region1,
1268 (char*)real_area2 + off, snapshot2, region2, type->subtype, pointer_level);
1273 case DW_TAG_pointer_type:
1274 case DW_TAG_reference_type:
1275 case DW_TAG_rvalue_reference_type: {
1276 void* addr_pointed1 = MC_region_read_pointer(region1, real_area1);
1277 void* addr_pointed2 = MC_region_read_pointer(region2, real_area2);
1279 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type)
1280 return (addr_pointed1 != addr_pointed2);
1281 if (addr_pointed1 == nullptr && addr_pointed2 == nullptr)
1283 if (addr_pointed1 == nullptr || addr_pointed2 == nullptr)
1285 if (not state.compared_pointers.insert(std::make_pair(addr_pointed1, addr_pointed2)).second)
1290 // Some cases are not handled here:
1291 // * the pointers lead to different areas (one to the heap, the other to the RW segment ...)
1292 // * a pointer leads to the read-only segment of the current object
1293 // * a pointer lead to a different ELF object
1295 if (addr_pointed1 > process->heap_address && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)) {
1296 if (not(addr_pointed2 > process->heap_address && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)))
1298 // The pointers are both in the heap:
1299 return simgrid::mc::compare_heap_area(state, process_index, addr_pointed1, addr_pointed2, snapshot1,
1300 snapshot2, nullptr, type->subtype, pointer_level);
1302 } else if (region1->contain(simgrid::mc::remote(addr_pointed1))) {
1303 // The pointers are both in the current object R/W segment:
1304 if (not region2->contain(simgrid::mc::remote(addr_pointed2)))
1306 if (not type->type_id)
1307 return (addr_pointed1 != addr_pointed2);
1309 return compare_areas_with_type(state, process_index, addr_pointed1, snapshot1, region1, addr_pointed2,
1310 snapshot2, region2, type->subtype, pointer_level);
1313 // TODO, We do not handle very well the case where
1314 // it belongs to a different (non-heap) region from the current one.
1316 return (addr_pointed1 != addr_pointed2);
1319 case DW_TAG_structure_type:
1320 case DW_TAG_class_type:
1321 for (simgrid::mc::Member& member : type->members) {
1322 void* member1 = simgrid::dwarf::resolve_member(real_area1, type, &member, snapshot1, process_index);
1323 void* member2 = simgrid::dwarf::resolve_member(real_area2, type, &member, snapshot2, process_index);
1324 simgrid::mc::RegionSnapshot* subregion1 = snapshot1->get_region(member1, process_index, region1); // region1 is hinted
1325 simgrid::mc::RegionSnapshot* subregion2 = snapshot2->get_region(member2, process_index, region2); // region2 is hinted
1326 res = compare_areas_with_type(state, process_index, member1, snapshot1, subregion1, member2, snapshot2,
1327 subregion2, member.type, pointer_level);
1332 case DW_TAG_subroutine_type:
1335 XBT_VERB("Unknown case: %d", type->type);
1343 static int compare_global_variables(simgrid::mc::StateComparator& state, simgrid::mc::ObjectInformation* object_info,
1344 int process_index, simgrid::mc::RegionSnapshot* r1, simgrid::mc::RegionSnapshot* r2,
1345 simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2)
1347 xbt_assert(r1 && r2, "Missing region.");
1350 if (r1->storage_type() == simgrid::mc::StorageType::Privatized) {
1351 xbt_assert(process_index >= 0);
1352 if (r2->storage_type() != simgrid::mc::StorageType::Privatized)
1355 size_t process_count = MC_smpi_process_count();
1356 xbt_assert(process_count == r1->privatized_data().size()
1357 && process_count == r2->privatized_data().size());
1359 // Compare the global variables separately for each simulates process:
1360 for (size_t i = 0; i < process_count; i++) {
1361 if (compare_global_variables(state, object_info, i, &r1->privatized_data()[i], &r2->privatized_data()[i],
1362 snapshot1, snapshot2))
1368 xbt_assert(r1->storage_type() != simgrid::mc::StorageType::Privatized);
1370 xbt_assert(r2->storage_type() != simgrid::mc::StorageType::Privatized);
1372 std::vector<simgrid::mc::Variable>& variables = object_info->global_variables;
1374 for (simgrid::mc::Variable const& current_var : variables) {
1376 // If the variable is not in this object, skip it:
1377 // We do not expect to find a pointer to something which is not reachable
1378 // by the global variables.
1379 if ((char *) current_var.address < (char *) object_info->start_rw
1380 || (char *) current_var.address > (char *) object_info->end_rw)
1383 simgrid::mc::Type* bvariable_type = current_var.type;
1384 int res = compare_areas_with_type(state, process_index,
1385 (char *) current_var.address, snapshot1, r1,
1386 (char *) current_var.address, snapshot2, r2,
1389 XBT_VERB("Global variable %s (%p) is different between snapshots",
1390 current_var.name.c_str(),
1391 (char *) current_var.address);
1399 static int compare_local_variables(simgrid::mc::StateComparator& state,
1401 simgrid::mc::Snapshot* snapshot1,
1402 simgrid::mc::Snapshot* snapshot2,
1403 mc_snapshot_stack_t stack1,
1404 mc_snapshot_stack_t stack2)
1406 if (stack1->local_variables.size() != stack2->local_variables.size()) {
1407 XBT_VERB("Different number of local variables");
1411 unsigned int cursor = 0;
1412 local_variable_t current_var1;
1413 local_variable_t current_var2;
1414 while (cursor < stack1->local_variables.size()) {
1415 current_var1 = &stack1->local_variables[cursor];
1416 current_var2 = &stack1->local_variables[cursor];
1417 if (current_var1->name != current_var2->name
1418 || current_var1->subprogram != current_var2->subprogram
1419 || current_var1->ip != current_var2->ip) {
1420 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1422 ("Different name of variable (%s - %s) "
1423 "or frame (%s - %s) or ip (%lu - %lu)",
1424 current_var1->name.c_str(),
1425 current_var2->name.c_str(),
1426 current_var1->subprogram->name.c_str(),
1427 current_var2->subprogram->name.c_str(),
1428 current_var1->ip, current_var2->ip);
1431 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1433 simgrid::mc::Type* subtype = current_var1->type;
1435 compare_areas_with_type(state, process_index, current_var1->address, snapshot1,
1436 snapshot1->get_region(current_var1->address, process_index), current_var2->address,
1437 snapshot2, snapshot2->get_region(current_var2->address, process_index), subtype, 0);
1440 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1441 XBT_VERB("Local variable %s (%p - %p) in frame %s "
1442 "is different between snapshots",
1443 current_var1->name.c_str(), current_var1->address, current_var2->address,
1444 current_var1->subprogram->name.c_str());
1455 static std::unique_ptr<simgrid::mc::StateComparator> state_comparator;
1457 int snapshot_compare(Snapshot* s1, Snapshot* s2)
1459 // TODO, make this a field of ModelChecker or something similar
1460 if (state_comparator == nullptr)
1461 state_comparator.reset(new StateComparator());
1463 state_comparator->clear();
1465 RemoteClient* process = &mc_model_checker->process();
1469 int hash_result = 0;
1471 hash_result = (s1->hash_ != s2->hash_);
1473 XBT_VERB("(%d - %d) Different hash: 0x%" PRIx64 "--0x%" PRIx64, s1->num_state_, s2->num_state_, s1->hash_,
1479 XBT_VERB("(%d - %d) Same hash: 0x%" PRIx64, s1->num_state_, s2->num_state_, s1->hash_);
1482 /* Compare enabled processes */
1483 if (s1->enabled_processes_ != s2->enabled_processes_) {
1484 XBT_VERB("(%d - %d) Different amount of enabled processes", s1->num_state_, s2->num_state_);
1488 /* Compare size of stacks */
1490 for (unsigned long i = 0; i < s1->stacks_.size(); i++) {
1491 size_t size_used1 = s1->stack_sizes_[i];
1492 size_t size_used2 = s2->stack_sizes_[i];
1493 if (size_used1 != size_used2) {
1495 XBT_DEBUG("(%d - %d) Different size used in stacks: %zu - %zu", s1->num_state, s2->num_state, size_used1,
1501 XBT_VERB("(%d - %d) Different size used in stacks: %zu - %zu", s1->num_state_, s2->num_state_, size_used1,
1508 if (is_diff) // do not proceed if there is any stacks that don't match
1511 /* Init heap information used in heap comparison algorithm */
1512 xbt_mheap_t heap1 = (xbt_mheap_t)s1->read_bytes(
1513 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1514 remote(process->heap_address),
1515 simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1516 xbt_mheap_t heap2 = (xbt_mheap_t)s2->read_bytes(
1517 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1518 remote(process->heap_address),
1519 simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1520 int res_init = state_comparator->initHeapInformation(heap1, heap2, &s1->to_ignore_, &s2->to_ignore_);
1522 if (res_init == -1) {
1524 XBT_DEBUG("(%d - %d) Different heap information", num1, nus1->num_state, s2->num_statem2);
1528 XBT_VERB("(%d - %d) Different heap information", s1->num_state_, s2->num_state_);
1535 /* Stacks comparison */
1537 for (unsigned int cursor = 0; cursor < s1->stacks_.size(); cursor++) {
1538 mc_snapshot_stack_t stack1 = &s1->stacks_[cursor];
1539 mc_snapshot_stack_t stack2 = &s2->stacks_[cursor];
1541 if (stack1->process_index != stack2->process_index) {
1543 XBT_DEBUG("(%d - %d) Stacks with different process index (%i vs %i)", s1->num_state_, s2->num_state_,
1544 stack1->process_index, stack2->process_index);
1546 else diff_local = compare_local_variables(*state_comparator,
1547 stack1->process_index, s1, s2, stack1, stack2);
1548 if (diff_local > 0) {
1550 XBT_DEBUG("(%d - %d) Different local variables between stacks %d", num1,
1556 XBT_VERB("(%d - %d) Different local variables between stacks %u", s1->num_state_, s2->num_state_, cursor + 1);
1564 size_t regions_count = s1->snapshot_regions_.size();
1565 // TODO, raise a difference instead?
1566 xbt_assert(regions_count == s2->snapshot_regions_.size());
1568 for (size_t k = 0; k != regions_count; ++k) {
1569 RegionSnapshot* region1 = s1->snapshot_regions_[k].get();
1570 RegionSnapshot* region2 = s2->snapshot_regions_[k].get();
1573 if (region1->region_type() != RegionType::Data)
1576 xbt_assert(region1->region_type() == region2->region_type());
1577 xbt_assert(region1->object_info() == region2->object_info());
1578 xbt_assert(region1->object_info());
1580 std::string const& name = region1->object_info()->file_name;
1582 /* Compare global variables */
1583 if (compare_global_variables(*state_comparator, region1->object_info(), simgrid::mc::ProcessIndexDisabled, region1,
1587 XBT_DEBUG("(%d - %d) Different global variables in %s", s1->num_state, s2->num_state, name.c_str());
1591 XBT_VERB("(%d - %d) Different global variables in %s", s1->num_state_, s2->num_state_, name.c_str());
1600 if (mmalloc_compare_heap(*state_comparator, s1, s2) > 0) {
1603 XBT_DEBUG("(%d - %d) Different heap (mmalloc_compare)", s1->num_state, s2->num_state);
1608 XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", s1->num_state_, s2->num_state_);
1615 if (errors || hash_result)
1616 XBT_VERB("(%d - %d) Difference found", s1->num_state_, s2->num_state_);
1618 XBT_VERB("(%d - %d) No difference found", s1->num_state_, s2->num_state_);
1621 #if defined(MC_DEBUG) && defined(MC_VERBOSE)
1623 // * false positive SHOULD be avoided.
1624 // * There MUST not be any false negative.
1626 XBT_VERB("(%d - %d) State equality hash test is %s %s", s1->num_state, s2->num_state,
1627 (hash_result != 0) == (errors != 0) ? "true" : "false", not hash_result ? "positive" : "negative");
1631 return errors > 0 || hash_result;