1 /* Copyright (c) 2008-2018. 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)
305 mc_mem_region_t MC_get_heap_region(simgrid::mc::Snapshot* snapshot)
307 for (auto const& region : snapshot->snapshot_regions)
308 if (region->region_type() == simgrid::mc::RegionType::Heap)
310 xbt_die("No heap region");
314 int mmalloc_compare_heap(
315 simgrid::mc::StateComparator& state, simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2)
317 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
319 /* Start comparison */
333 /* Check busy blocks */
336 malloc_info heapinfo_temp1;
337 malloc_info heapinfo_temp2;
338 malloc_info heapinfo_temp2b;
340 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
341 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
343 // This is the address of std_heap->heapinfo in the application process:
344 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
346 // This is in snapshot do not use them directly:
347 const malloc_info* heapinfos1 = snapshot1->read<malloc_info*>(
348 RemotePtr<malloc_info*>((std::uint64_t)heapinfo_address), simgrid::mc::ProcessIndexMissing);
349 const malloc_info* heapinfos2 = snapshot2->read<malloc_info*>(
350 RemotePtr<malloc_info*>((std::uint64_t)heapinfo_address), simgrid::mc::ProcessIndexMissing);
352 while (i1 < state.heaplimit) {
354 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(heap_region1, &heapinfo_temp1, &heapinfos1[i1], sizeof(malloc_info));
355 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2, &heapinfos2[i1], sizeof(malloc_info));
357 if (heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type == MMALLOC_TYPE_HEAPINFO) { /* Free block */
362 if (heapinfo1->type < 0) {
363 fprintf(stderr, "Unkown mmalloc block type.\n");
367 addr_block1 = ((void*)(((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase));
369 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED) { /* Large block */
371 if (is_stack(addr_block1)) {
372 for (k = 0; k < heapinfo1->busy_block.size; k++)
373 state.equals_to1_(i1 + k, 0) = HeapArea(i1, -1);
374 for (k = 0; k < heapinfo2->busy_block.size; k++)
375 state.equals_to2_(i1 + k, 0) = HeapArea(i1, -1);
376 i1 += heapinfo1->busy_block.size;
380 if (state.equals_to1_(i1, 0).valid_) {
388 /* Try first to associate to same block in the other heap */
389 if (heapinfo2->type == heapinfo1->type && state.equals_to2_(i1, 0).valid_ == 0) {
390 addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
391 int res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing, addr_block1, addr_block2,
392 snapshot1, snapshot2, nullptr, nullptr, 0);
393 if (res_compare != 1) {
394 for (k = 1; k < heapinfo2->busy_block.size; k++)
395 state.equals_to2_(i1 + k, 0) = HeapArea(i1, -1);
396 for (k = 1; k < heapinfo1->busy_block.size; k++)
397 state.equals_to1_(i1 + k, 0) = HeapArea(i1, -1);
399 i1 += heapinfo1->busy_block.size;
403 while (i2 < state.heaplimit && not equal) {
405 addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
412 const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2b, &heapinfos2[i2], sizeof(malloc_info));
414 if (heapinfo2b->type != MMALLOC_TYPE_UNFRAGMENTED) {
419 if (state.equals_to2_(i2, 0).valid_) {
424 int res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing, addr_block1, addr_block2,
425 snapshot1, snapshot2, nullptr, nullptr, 0);
427 if (res_compare != 1) {
428 for (k = 1; k < heapinfo2b->busy_block.size; k++)
429 state.equals_to2_(i2 + k, 0) = HeapArea(i1, -1);
430 for (k = 1; k < heapinfo1->busy_block.size; k++)
431 state.equals_to1_(i1 + k, 0) = HeapArea(i2, -1);
433 i1 += heapinfo1->busy_block.size;
440 XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1, heapinfo1->busy_block.busy_size, addr_block1);
441 i1 = state.heaplimit + 1;
445 } else { /* Fragmented block */
447 for (j1 = 0; j1 < (size_t) (BLOCKSIZE >> heapinfo1->type); j1++) {
449 if (heapinfo1->busy_frag.frag_size[j1] == -1) /* Free fragment_ */
452 if (state.equals_to1_(i1, j1).valid_)
455 addr_frag1 = (void*)((char*)addr_block1 + (j1 << heapinfo1->type));
460 /* Try first to associate to same fragment_ in the other heap */
461 if (heapinfo2->type == heapinfo1->type && not state.equals_to2_(i1, j1).valid_) {
462 addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE +
463 (char *) state.std_heap_copy.heapbase;
465 (void *) ((char *) addr_block2 +
466 (j1 << heapinfo2->type));
467 int res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing, addr_frag1, addr_frag2,
468 snapshot1, snapshot2, nullptr, nullptr, 0);
469 if (res_compare != 1)
473 while (i2 < state.heaplimit && not equal) {
475 const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(
476 heap_region2, &heapinfo_temp2b, &heapinfos2[i2],
477 sizeof(malloc_info));
479 if (heapinfo2b->type == MMALLOC_TYPE_FREE || heapinfo2b->type == MMALLOC_TYPE_HEAPINFO) {
484 // We currently do not match fragments with unfragmented blocks (maybe we should).
485 if (heapinfo2b->type == MMALLOC_TYPE_UNFRAGMENTED) {
490 if (heapinfo2b->type < 0) {
491 fprintf(stderr, "Unknown mmalloc block type.\n");
495 for (j2 = 0; j2 < (size_t) (BLOCKSIZE >> heapinfo2b->type);
498 if (i2 == i1 && j2 == j1)
501 if (state.equals_to2_(i2, j2).valid_)
504 addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
505 addr_frag2 = (void*)((char*)addr_block2 + (j2 << heapinfo2b->type));
507 int res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing, addr_frag1, addr_frag2,
508 snapshot2, snapshot2, nullptr, nullptr, 0);
509 if (res_compare != 1) {
519 XBT_DEBUG("Block %zu, fragment_ %zu not found (size_used = %zd, address = %p)\n", i1, j1,
520 heapinfo1->busy_frag.frag_size[j1], addr_frag1);
521 i1 = state.heaplimit + 1;
531 /* All blocks/fragments are equal to another block/fragment_ ? */
535 for(i = 1; i < state.heaplimit; i++) {
536 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
537 heap_region1, &heapinfo_temp1, &heapinfos1[i], sizeof(malloc_info));
539 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED && i1 == state.heaplimit && heapinfo1->busy_block.busy_size > 0 &&
540 not state.equals_to1_(i, 0).valid_) {
541 XBT_DEBUG("Block %zu not found (size used = %zu)", i, heapinfo1->busy_block.busy_size);
545 if (heapinfo1->type <= 0)
547 for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo1->type); j++)
548 if (i1 == state.heaplimit && heapinfo1->busy_frag.frag_size[j] > 0 && not state.equals_to1_(i, j).valid_) {
549 XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)", i, j, heapinfo1->busy_frag.frag_size[j]);
554 if (i1 == state.heaplimit)
555 XBT_DEBUG("Number of blocks/fragments not found in heap1: %d", nb_diff1);
557 for (i=1; i < state.heaplimit; i++) {
558 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
559 heap_region2, &heapinfo_temp2, &heapinfos2[i], sizeof(malloc_info));
560 if (heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED && i1 == state.heaplimit && heapinfo2->busy_block.busy_size > 0 &&
561 not state.equals_to2_(i, 0).valid_) {
562 XBT_DEBUG("Block %zu not found (size used = %zu)", i,
563 heapinfo2->busy_block.busy_size);
567 if (heapinfo2->type <= 0)
570 for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo2->type); j++)
571 if (i1 == state.heaplimit && heapinfo2->busy_frag.frag_size[j] > 0 && not state.equals_to2_(i, j).valid_) {
572 XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)",
573 i, j, heapinfo2->busy_frag.frag_size[j]);
579 if (i1 == state.heaplimit)
580 XBT_DEBUG("Number of blocks/fragments not found in heap2: %d", nb_diff2);
582 return nb_diff1 > 0 || nb_diff2 > 0;
588 * @param real_area1 Process address for state 1
589 * @param real_area2 Process address for state 2
590 * @param snapshot1 Snapshot of state 1
591 * @param snapshot2 Snapshot of state 2
594 * @param check_ignore
596 static int compare_heap_area_without_type(
597 simgrid::mc::StateComparator& state, int process_index,
598 const void *real_area1, const void *real_area2,
599 simgrid::mc::Snapshot* snapshot1,
600 simgrid::mc::Snapshot* snapshot2,
601 HeapLocationPairs* previous, int size,
604 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
605 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
606 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
608 for (int i = 0; i < size; ) {
610 if (check_ignore > 0) {
611 ssize_t ignore1 = heap_comparison_ignore_size(
612 state.processStates[0].to_ignore, (char *) real_area1 + i);
614 ssize_t ignore2 = heap_comparison_ignore_size(
615 state.processStates[1].to_ignore, (char *) real_area2 + i);
616 if (ignore2 == ignore1) {
629 if (MC_snapshot_region_memcmp(((char *) real_area1) + i, heap_region1, ((char *) real_area2) + i, heap_region2, 1) != 0) {
631 int pointer_align = (i / sizeof(void *)) * sizeof(void *);
632 const void* addr_pointed1 = snapshot1->read(
633 remote((void**)((char *) real_area1 + pointer_align)), process_index);
634 const void* addr_pointed2 = snapshot2->read(
635 remote((void**)((char *) real_area2 + pointer_align)), process_index);
637 if (process->in_maestro_stack(remote(addr_pointed1))
638 && process->in_maestro_stack(remote(addr_pointed2))) {
639 i = pointer_align + sizeof(void *);
643 if (addr_pointed1 > state.std_heap_copy.heapbase
644 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
645 && addr_pointed2 > state.std_heap_copy.heapbase
646 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)) {
647 // Both addreses are in the heap:
648 int res_compare = compare_heap_area(state ,process_index,
649 addr_pointed1, addr_pointed2,
650 snapshot1, snapshot2, previous, nullptr, 0);
651 if (res_compare == 1)
653 i = pointer_align + sizeof(void *);
669 * @param real_area1 Process address for state 1
670 * @param real_area2 Process address for state 2
671 * @param snapshot1 Snapshot of state 1
672 * @param snapshot2 Snapshot of state 2
675 * @param area_size either a byte_size or an elements_count (?)
676 * @param check_ignore
677 * @param pointer_level
678 * @return 0 (same), 1 (different), -1 (unknown)
680 static int compare_heap_area_with_type(
681 simgrid::mc::StateComparator& state, int process_index,
682 const void *real_area1, const void *real_area2,
683 simgrid::mc::Snapshot* snapshot1,
684 simgrid::mc::Snapshot* snapshot2,
685 HeapLocationPairs* previous, simgrid::mc::Type* type,
686 int area_size, int check_ignore,
691 // HACK: This should not happen but in pratice, there are some
692 // DW_TAG_typedef without an associated DW_AT_type:
693 //<1><538832>: Abbrev Number: 111 (DW_TAG_typedef)
694 // <538833> DW_AT_name : (indirect string, offset: 0x2292f3): gregset_t
695 // <538837> DW_AT_decl_file : 98
696 // <538838> DW_AT_decl_line : 37
700 if (is_stack(real_area1) && is_stack(real_area2))
703 if (check_ignore > 0) {
704 ssize_t ignore1 = heap_comparison_ignore_size(state.processStates[0].to_ignore, real_area1);
705 if (ignore1 > 0 && heap_comparison_ignore_size(state.processStates[1].to_ignore, real_area2) == ignore1)
709 simgrid::mc::Type* subtype;
710 simgrid::mc::Type* subsubtype;
713 const void* addr_pointed1;
714 const void* addr_pointed2;
716 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
717 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
719 switch (type->type) {
720 case DW_TAG_unspecified_type:
723 case DW_TAG_base_type:
724 if (not type->name.empty() && type->name == "char") { /* String, hence random (arbitrary ?) size */
725 if (real_area1 == real_area2)
728 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, area_size) != 0;
730 if (area_size != -1 && type->byte_size != area_size)
733 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
737 case DW_TAG_enumeration_type:
738 if (area_size != -1 && type->byte_size != area_size)
740 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
743 case DW_TAG_const_type:
744 case DW_TAG_volatile_type:
746 type = type->subtype;
749 case DW_TAG_array_type:
750 subtype = type->subtype;
751 switch (subtype->type) {
752 case DW_TAG_unspecified_type:
755 case DW_TAG_base_type:
756 case DW_TAG_enumeration_type:
757 case DW_TAG_pointer_type:
758 case DW_TAG_reference_type:
759 case DW_TAG_rvalue_reference_type:
760 case DW_TAG_structure_type:
761 case DW_TAG_class_type:
762 case DW_TAG_union_type:
763 if (subtype->full_type)
764 subtype = subtype->full_type;
765 elm_size = subtype->byte_size;
767 // TODO, just remove the type indirection?
768 case DW_TAG_const_type:
770 case DW_TAG_volatile_type:
771 subsubtype = subtype->subtype;
772 if (subsubtype->full_type)
773 subsubtype = subsubtype->full_type;
774 elm_size = subsubtype->byte_size;
780 for (int i = 0; i < type->element_count; i++) {
781 // TODO, add support for variable stride (DW_AT_byte_stride)
782 res = compare_heap_area_with_type(state, process_index, (char*)real_area1 + (i * elm_size),
783 (char*)real_area2 + (i * elm_size), snapshot1, snapshot2, previous,
784 type->subtype, subtype->byte_size, check_ignore, pointer_level);
790 case DW_TAG_reference_type:
791 case DW_TAG_rvalue_reference_type:
792 case DW_TAG_pointer_type:
793 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
794 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
795 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
796 return (addr_pointed1 != addr_pointed2);
799 if (pointer_level <= 1) {
800 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
801 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
802 if (addr_pointed1 > state.std_heap_copy.heapbase && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1) &&
803 addr_pointed2 > state.std_heap_copy.heapbase && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
804 return compare_heap_area(state, process_index, addr_pointed1, addr_pointed2, snapshot1, snapshot2, previous,
805 type->subtype, pointer_level);
807 return (addr_pointed1 != addr_pointed2);
809 for (size_t i = 0; i < (area_size / sizeof(void*)); i++) {
810 addr_pointed1 = snapshot1->read(remote((void**)((char*)real_area1 + i * sizeof(void*))), process_index);
811 addr_pointed2 = snapshot2->read(remote((void**)((char*)real_area2 + i * sizeof(void*))), process_index);
812 if (addr_pointed1 > state.std_heap_copy.heapbase && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1) &&
813 addr_pointed2 > state.std_heap_copy.heapbase && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
814 res = compare_heap_area(state, process_index, addr_pointed1, addr_pointed2, snapshot1, snapshot2, previous,
815 type->subtype, pointer_level);
817 res = (addr_pointed1 != addr_pointed2);
823 case DW_TAG_structure_type:
824 case DW_TAG_class_type:
826 type = type->full_type;
827 if (area_size != -1 && type->byte_size != area_size) {
828 if (area_size <= type->byte_size || area_size % type->byte_size != 0)
830 for (size_t i = 0; i < (size_t)(area_size / type->byte_size); i++) {
831 int res = compare_heap_area_with_type(state, process_index, (char*)real_area1 + i * type->byte_size,
832 (char*)real_area2 + i * type->byte_size, snapshot1, snapshot2,
833 previous, type, -1, check_ignore, 0);
838 for (simgrid::mc::Member& member : type->members) {
839 // TODO, optimize this? (for the offset case)
840 void* real_member1 = simgrid::dwarf::resolve_member(real_area1, type, &member,
841 (simgrid::mc::AddressSpace*)snapshot1, process_index);
842 void* real_member2 = simgrid::dwarf::resolve_member(real_area2, type, &member,
843 (simgrid::mc::AddressSpace*)snapshot2, process_index);
844 int res = compare_heap_area_with_type(state, process_index, real_member1, real_member2, snapshot1,
845 snapshot2, previous, member.type, -1, check_ignore, 0);
852 case DW_TAG_union_type:
853 return compare_heap_area_without_type(state, process_index, real_area1, real_area2, snapshot1, snapshot2,
854 previous, type->byte_size, check_ignore);
860 xbt_die("Unreachable");
864 /** Infer the type of a part of the block from the type of the block
866 * TODO, handle DW_TAG_array_type as well as arrays of the object ((*p)[5], p[5])
868 * TODO, handle subfields ((*p).bar.foo, (*p)[5].bar…)
870 * @param type DWARF type ID of the root address
872 * @return DWARF type ID for given offset
874 static simgrid::mc::Type* get_offset_type(void *real_base_address, simgrid::mc::Type* type,
875 int offset, int area_size,
876 simgrid::mc::Snapshot* snapshot, int process_index)
879 // Beginning of the block, the infered variable type if the type of the block:
883 switch (type->type) {
885 case DW_TAG_structure_type:
886 case DW_TAG_class_type:
888 type = type->full_type;
889 if (area_size != -1 && type->byte_size != area_size) {
890 if (area_size > type->byte_size && area_size % type->byte_size == 0)
896 for (simgrid::mc::Member& member : type->members) {
897 if (member.has_offset_location()) {
898 // We have the offset, use it directly (shortcut):
899 if (member.offset() == offset)
902 void* real_member = simgrid::dwarf::resolve_member(real_base_address, type, &member, snapshot, process_index);
903 if ((char*)real_member - (char*)real_base_address == offset)
910 /* FIXME: other cases ? */
918 * @param area1 Process address for state 1
919 * @param area2 Process address for state 2
920 * @param snapshot1 Snapshot of state 1
921 * @param snapshot2 Snapshot of state 2
922 * @param previous Pairs of blocks already compared on the current path (or nullptr)
923 * @param type_id Type of variable
924 * @param pointer_level
925 * @return 0 (same), 1 (different), -1
928 int compare_heap_area(simgrid::mc::StateComparator& state, int process_index,
929 const void *area1, const void *area2,
930 simgrid::mc::Snapshot* snapshot1,
931 simgrid::mc::Snapshot* snapshot2,
932 HeapLocationPairs* previous,
933 simgrid::mc::Type* type, int pointer_level)
935 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
940 int check_ignore = 0;
948 simgrid::mc::Type* new_type1 = nullptr;
949 simgrid::mc::Type* new_type2 = nullptr;
951 bool match_pairs = false;
953 // This is the address of std_heap->heapinfo in the application process:
954 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
956 const malloc_info* heapinfos1 = snapshot1->read(remote((const malloc_info**)heapinfo_address), process_index);
957 const malloc_info* heapinfos2 = snapshot2->read(remote((const malloc_info**)heapinfo_address), process_index);
959 malloc_info heapinfo_temp1;
960 malloc_info heapinfo_temp2;
962 simgrid::mc::HeapLocationPairs current;
963 if (previous == nullptr) {
969 block1 = ((char*)area1 - (char*)state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
970 block2 = ((char*)area2 - (char*)state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
972 // If either block is a stack block:
973 if (is_block_stack((int) block1) && is_block_stack((int) block2)) {
974 previous->insert(simgrid::mc::makeHeapLocationPair(block1, -1, block2, -1));
976 state.match_equals(previous);
980 // If either block is not in the expected area of memory:
981 if (((char*)area1 < (char*)state.std_heap_copy.heapbase) || (block1 > (ssize_t)state.processStates[0].heapsize) ||
982 (block1 < 1) || ((char*)area2 < (char*)state.std_heap_copy.heapbase) ||
983 (block2 > (ssize_t)state.processStates[1].heapsize) || (block2 < 1)) {
987 // Process address of the block:
988 void* real_addr_block1 = (ADDR2UINT(block1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
989 void* real_addr_block2 = (ADDR2UINT(block2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
993 type = type->full_type;
995 // This assume that for "boring" types (volatile ...) byte_size is absent:
996 while (type->byte_size == 0 && type->subtype != nullptr)
997 type = type->subtype;
1000 if (type->type == DW_TAG_pointer_type ||
1001 (type->type == DW_TAG_base_type && not type->name.empty() && type->name == "char"))
1004 type_size = type->byte_size;
1008 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
1009 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
1011 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
1012 heap_region1, &heapinfo_temp1, &heapinfos1[block1], sizeof(malloc_info));
1013 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
1014 heap_region2, &heapinfo_temp2, &heapinfos2[block2], sizeof(malloc_info));
1016 if ((heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type==MMALLOC_TYPE_HEAPINFO)
1017 && (heapinfo2->type == MMALLOC_TYPE_FREE || heapinfo2->type ==MMALLOC_TYPE_HEAPINFO)) {
1020 state.match_equals(previous);
1024 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED && heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
1025 /* Complete block */
1027 // TODO, lookup variable type from block type as done for fragmented blocks
1029 if (state.equals_to1_(block1, 0).valid_ && state.equals_to2_(block2, 0).valid_ &&
1030 state.blocksEqual(block1, block2)) {
1032 state.match_equals(previous);
1036 if (type_size != -1 && type_size != (ssize_t)heapinfo1->busy_block.busy_size &&
1037 type_size != (ssize_t)heapinfo2->busy_block.busy_size &&
1038 (type->name.empty() || type->name == "struct s_smx_context")) {
1040 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_ && offset1 == offset2 &&
1102 state.fragmentsEqual(block1, frag1, block2, frag2)) {
1104 state.match_equals(previous);
1107 // Compare the size of both fragments:
1108 if (heapinfo1->busy_frag.frag_size[frag1] != heapinfo2->busy_frag.frag_size[frag2]) {
1109 if (type_size == -1) {
1111 state.match_equals(previous);
1117 // Size of the fragment_:
1118 size = heapinfo1->busy_frag.frag_size[frag1];
1120 // Remember (basic) type inference.
1121 // The current data structure only allows us to do this for the whole fragment_.
1122 if (type != nullptr && area1 == real_addr_frag1)
1123 state.types1_(block1, frag1) = type;
1124 if (type != nullptr && area2 == real_addr_frag2)
1125 state.types2_(block2, frag2) = type;
1127 // The type of the variable is already known:
1129 new_type1 = new_type2 = type;
1131 // Type inference from the block type.
1132 else if (state.types1_(block1, frag1) != nullptr || state.types2_(block2, frag2) != nullptr) {
1134 offset1 = (char*)area1 - (char*)real_addr_frag1;
1135 offset2 = (char*)area2 - (char*)real_addr_frag2;
1137 if (state.types1_(block1, frag1) != nullptr && state.types2_(block2, frag2) != nullptr) {
1139 get_offset_type(real_addr_frag1, state.types1_(block1, frag1), offset1, size, snapshot1, process_index);
1141 get_offset_type(real_addr_frag2, state.types2_(block2, frag2), offset1, size, snapshot2, process_index);
1142 } else if (state.types1_(block1, frag1) != nullptr) {
1144 get_offset_type(real_addr_frag1, state.types1_(block1, frag1), offset1, size, snapshot1, process_index);
1146 get_offset_type(real_addr_frag2, state.types1_(block1, frag1), offset2, size, snapshot2, process_index);
1147 } else if (state.types2_(block2, frag2) != nullptr) {
1149 get_offset_type(real_addr_frag1, state.types2_(block2, frag2), offset1, size, snapshot1, process_index);
1151 get_offset_type(real_addr_frag2, state.types2_(block2, frag2), offset2, size, snapshot2, process_index);
1154 state.match_equals(previous);
1158 if (new_type1 != nullptr && new_type2 != nullptr && new_type1 != new_type2) {
1161 while (type->byte_size == 0 && type->subtype != nullptr)
1162 type = type->subtype;
1163 new_size1 = type->byte_size;
1166 while (type->byte_size == 0 && type->subtype != nullptr)
1167 type = type->subtype;
1168 new_size2 = type->byte_size;
1172 state.match_equals(previous);
1177 if (new_size1 > 0 && new_size1 == new_size2) {
1182 if (offset1 == 0 && offset2 == 0 &&
1183 not previous->insert(simgrid::mc::makeHeapLocationPair(block1, frag1, block2, frag2)).second) {
1185 state.match_equals(previous);
1191 state.match_equals(previous);
1195 if ((heapinfo1->busy_frag.ignore[frag1] > 0) &&
1196 (heapinfo2->busy_frag.ignore[frag2] == heapinfo1->busy_frag.ignore[frag1]))
1197 check_ignore = heapinfo1->busy_frag.ignore[frag1];
1203 /* Start comparison */
1206 res_compare = compare_heap_area_with_type(state, process_index, area1, area2, snapshot1, snapshot2, previous, type,
1207 size, check_ignore, pointer_level);
1209 res_compare = compare_heap_area_without_type(state, process_index, area1, area2, snapshot1, snapshot2, previous,
1210 size, check_ignore);
1212 if (res_compare == 1)
1216 state.match_equals(previous);
1223 /************************** Snapshot comparison *******************************/
1224 /******************************************************************************/
1226 static int compare_areas_with_type(simgrid::mc::StateComparator& state,
1228 void* real_area1, simgrid::mc::Snapshot* snapshot1, mc_mem_region_t region1,
1229 void* real_area2, simgrid::mc::Snapshot* snapshot2, mc_mem_region_t region2,
1230 simgrid::mc::Type* type, int pointer_level)
1232 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
1234 simgrid::mc::Type* subtype;
1235 simgrid::mc::Type* subsubtype;
1241 switch (type->type) {
1242 case DW_TAG_unspecified_type:
1245 case DW_TAG_base_type:
1246 case DW_TAG_enumeration_type:
1247 case DW_TAG_union_type:
1248 return MC_snapshot_region_memcmp(real_area1, region1, real_area2, region2, type->byte_size) != 0;
1249 case DW_TAG_typedef:
1250 case DW_TAG_volatile_type:
1251 case DW_TAG_const_type:
1253 type = type->subtype;
1254 continue; // restart
1255 case DW_TAG_array_type:
1256 subtype = type->subtype;
1257 switch (subtype->type) {
1258 case DW_TAG_unspecified_type:
1261 case DW_TAG_base_type:
1262 case DW_TAG_enumeration_type:
1263 case DW_TAG_pointer_type:
1264 case DW_TAG_reference_type:
1265 case DW_TAG_rvalue_reference_type:
1266 case DW_TAG_structure_type:
1267 case DW_TAG_class_type:
1268 case DW_TAG_union_type:
1269 if (subtype->full_type)
1270 subtype = subtype->full_type;
1271 elm_size = subtype->byte_size;
1273 case DW_TAG_const_type:
1274 case DW_TAG_typedef:
1275 case DW_TAG_volatile_type:
1276 subsubtype = subtype->subtype;
1277 if (subsubtype->full_type)
1278 subsubtype = subsubtype->full_type;
1279 elm_size = subsubtype->byte_size;
1285 for (i = 0; i < type->element_count; i++) {
1286 size_t off = i * elm_size;
1287 res = compare_areas_with_type(state, process_index, (char*)real_area1 + off, snapshot1, region1,
1288 (char*)real_area2 + off, snapshot2, region2, type->subtype, pointer_level);
1293 case DW_TAG_pointer_type:
1294 case DW_TAG_reference_type:
1295 case DW_TAG_rvalue_reference_type: {
1296 void* addr_pointed1 = MC_region_read_pointer(region1, real_area1);
1297 void* addr_pointed2 = MC_region_read_pointer(region2, real_area2);
1299 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type)
1300 return (addr_pointed1 != addr_pointed2);
1301 if (addr_pointed1 == nullptr && addr_pointed2 == nullptr)
1303 if (addr_pointed1 == nullptr || addr_pointed2 == nullptr)
1305 if (not state.compared_pointers.insert(std::make_pair(addr_pointed1, addr_pointed2)).second)
1310 // Some cases are not handled here:
1311 // * the pointers lead to different areas (one to the heap, the other to the RW segment ...)
1312 // * a pointer leads to the read-only segment of the current object
1313 // * a pointer lead to a different ELF object
1315 if (addr_pointed1 > process->heap_address && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)) {
1316 if (not(addr_pointed2 > process->heap_address && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)))
1318 // The pointers are both in the heap:
1319 return simgrid::mc::compare_heap_area(state, process_index, addr_pointed1, addr_pointed2, snapshot1,
1320 snapshot2, nullptr, type->subtype, pointer_level);
1322 } else if (region1->contain(simgrid::mc::remote(addr_pointed1))) {
1323 // The pointers are both in the current object R/W segment:
1324 if (not region2->contain(simgrid::mc::remote(addr_pointed2)))
1326 if (not type->type_id)
1327 return (addr_pointed1 != addr_pointed2);
1329 return compare_areas_with_type(state, process_index, addr_pointed1, snapshot1, region1, addr_pointed2,
1330 snapshot2, region2, type->subtype, pointer_level);
1333 // TODO, We do not handle very well the case where
1334 // it belongs to a different (non-heap) region from the current one.
1336 return (addr_pointed1 != addr_pointed2);
1340 case DW_TAG_structure_type:
1341 case DW_TAG_class_type:
1342 for (simgrid::mc::Member& member : type->members) {
1343 void* member1 = simgrid::dwarf::resolve_member(real_area1, type, &member, snapshot1, process_index);
1344 void* member2 = simgrid::dwarf::resolve_member(real_area2, type, &member, snapshot2, process_index);
1345 mc_mem_region_t subregion1 = mc_get_region_hinted(member1, snapshot1, process_index, region1);
1346 mc_mem_region_t subregion2 = mc_get_region_hinted(member2, snapshot2, process_index, region2);
1347 res = compare_areas_with_type(state, process_index, member1, snapshot1, subregion1, member2, snapshot2,
1348 subregion2, member.type, pointer_level);
1353 case DW_TAG_subroutine_type:
1357 XBT_VERB("Unknown case: %d", type->type);
1365 static int compare_global_variables(
1366 simgrid::mc::StateComparator& state,
1367 simgrid::mc::ObjectInformation* object_info,
1369 mc_mem_region_t r1, mc_mem_region_t r2,
1370 simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2)
1372 xbt_assert(r1 && r2, "Missing region.");
1375 if (r1->storage_type() == simgrid::mc::StorageType::Privatized) {
1376 xbt_assert(process_index >= 0);
1377 if (r2->storage_type() != simgrid::mc::StorageType::Privatized)
1380 size_t process_count = MC_smpi_process_count();
1381 xbt_assert(process_count == r1->privatized_data().size()
1382 && process_count == r2->privatized_data().size());
1384 // Compare the global variables separately for each simulates process:
1385 for (size_t process_index = 0; process_index < process_count; process_index++) {
1386 if (compare_global_variables(state,
1387 object_info, process_index,
1388 &r1->privatized_data()[process_index],
1389 &r2->privatized_data()[process_index],
1390 snapshot1, snapshot2))
1396 xbt_assert(r1->storage_type() != simgrid::mc::StorageType::Privatized);
1398 xbt_assert(r2->storage_type() != simgrid::mc::StorageType::Privatized);
1400 std::vector<simgrid::mc::Variable>& variables = object_info->global_variables;
1402 for (simgrid::mc::Variable const& current_var : variables) {
1404 // If the variable is not in this object, skip it:
1405 // We do not expect to find a pointer to something which is not reachable
1406 // by the global variables.
1407 if ((char *) current_var.address < (char *) object_info->start_rw
1408 || (char *) current_var.address > (char *) object_info->end_rw)
1411 simgrid::mc::Type* bvariable_type = current_var.type;
1412 int res = compare_areas_with_type(state, process_index,
1413 (char *) current_var.address, snapshot1, r1,
1414 (char *) current_var.address, snapshot2, r2,
1417 XBT_VERB("Global variable %s (%p) is different between snapshots",
1418 current_var.name.c_str(),
1419 (char *) current_var.address);
1427 static int compare_local_variables(simgrid::mc::StateComparator& state,
1429 simgrid::mc::Snapshot* snapshot1,
1430 simgrid::mc::Snapshot* snapshot2,
1431 mc_snapshot_stack_t stack1,
1432 mc_snapshot_stack_t stack2)
1434 if (stack1->local_variables.size() != stack2->local_variables.size()) {
1435 XBT_VERB("Different number of local variables");
1439 unsigned int cursor = 0;
1440 local_variable_t current_var1;
1441 local_variable_t current_var2;
1442 while (cursor < stack1->local_variables.size()) {
1443 current_var1 = &stack1->local_variables[cursor];
1444 current_var2 = &stack1->local_variables[cursor];
1445 if (current_var1->name != current_var2->name
1446 || current_var1->subprogram != current_var2->subprogram
1447 || current_var1->ip != current_var2->ip) {
1448 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1450 ("Different name of variable (%s - %s) "
1451 "or frame (%s - %s) or ip (%lu - %lu)",
1452 current_var1->name.c_str(),
1453 current_var2->name.c_str(),
1454 current_var1->subprogram->name.c_str(),
1455 current_var2->subprogram->name.c_str(),
1456 current_var1->ip, current_var2->ip);
1459 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1461 simgrid::mc::Type* subtype = current_var1->type;
1462 int res = compare_areas_with_type(
1463 state, process_index, current_var1->address, snapshot1,
1464 mc_get_snapshot_region(current_var1->address, snapshot1, process_index), current_var2->address, snapshot2,
1465 mc_get_snapshot_region(current_var2->address, snapshot2, process_index), subtype, 0);
1468 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1469 XBT_VERB("Local variable %s (%p - %p) in frame %s "
1470 "is different between snapshots",
1471 current_var1->name.c_str(), current_var1->address, current_var2->address,
1472 current_var1->subprogram->name.c_str());
1483 static std::unique_ptr<simgrid::mc::StateComparator> state_comparator;
1485 int snapshot_compare(int num1, simgrid::mc::Snapshot* s1, int num2, simgrid::mc::Snapshot* s2)
1487 // TODO, make this a field of ModelChecker or something similar
1489 if (state_comparator == nullptr)
1490 state_comparator = std::unique_ptr<StateComparator>(new StateComparator());
1492 state_comparator->clear();
1494 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
1498 int hash_result = 0;
1500 hash_result = (s1->hash != s2->hash);
1502 XBT_VERB("(%d - %d) Different hash: 0x%" PRIx64 "--0x%" PRIx64, num1, num2, s1->hash, s2->hash);
1507 XBT_VERB("(%d - %d) Same hash: 0x%" PRIx64, num1, num2, s1->hash);
1510 /* Compare enabled processes */
1511 if (s1->enabled_processes != s2->enabled_processes) {
1512 XBT_VERB("(%d - %d) Different amount of enabled processes", num1, num2);
1516 /* Compare size of stacks */
1518 for (unsigned long i = 0; i < s1->stacks.size(); i++) {
1519 size_t size_used1 = s1->stack_sizes[i];
1520 size_t size_used2 = s2->stack_sizes[i];
1521 if (size_used1 != size_used2) {
1523 XBT_DEBUG("(%d - %d) Different size used in stacks: %zu - %zu", num1, num2, size_used1, size_used2);
1528 XBT_VERB("(%d - %d) Different size used in stacks: %zu - %zu", num1, num2, size_used1, size_used2);
1534 if (is_diff) // do not proceed if there is any stacks that don't match
1537 /* Init heap information used in heap comparison algorithm */
1538 xbt_mheap_t heap1 = (xbt_mheap_t)s1->read_bytes(
1539 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1540 remote(process->heap_address),
1541 simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1542 xbt_mheap_t heap2 = (xbt_mheap_t)s2->read_bytes(
1543 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1544 remote(process->heap_address),
1545 simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1546 int res_init = state_comparator->initHeapInformation(heap1, heap2, &s1->to_ignore, &s2->to_ignore);
1548 if (res_init == -1) {
1550 XBT_DEBUG("(%d - %d) Different heap information", num1, num2);
1554 XBT_VERB("(%d - %d) Different heap information", num1, num2);
1561 /* Stacks comparison */
1563 for (unsigned int cursor = 0; cursor < s1->stacks.size(); cursor++) {
1564 mc_snapshot_stack_t stack1 = &s1->stacks[cursor];
1565 mc_snapshot_stack_t stack2 = &s2->stacks[cursor];
1567 if (stack1->process_index != stack2->process_index) {
1569 XBT_DEBUG("(%d - %d) Stacks with different process index (%i vs %i)", num1, num2,
1570 stack1->process_index, stack2->process_index);
1572 else diff_local = compare_local_variables(*state_comparator,
1573 stack1->process_index, s1, s2, stack1, stack2);
1574 if (diff_local > 0) {
1576 XBT_DEBUG("(%d - %d) Different local variables between stacks %d", num1,
1582 XBT_VERB("(%d - %d) Different local variables between stacks %u", num1, num2, cursor + 1);
1590 size_t regions_count = s1->snapshot_regions.size();
1591 // TODO, raise a difference instead?
1592 xbt_assert(regions_count == s2->snapshot_regions.size());
1594 for (size_t k = 0; k != regions_count; ++k) {
1595 mc_mem_region_t region1 = s1->snapshot_regions[k].get();
1596 mc_mem_region_t region2 = s2->snapshot_regions[k].get();
1599 if (region1->region_type() != simgrid::mc::RegionType::Data)
1602 xbt_assert(region1->region_type() == region2->region_type());
1603 xbt_assert(region1->object_info() == region2->object_info());
1604 xbt_assert(region1->object_info());
1606 std::string const& name = region1->object_info()->file_name;
1608 /* Compare global variables */
1609 if (compare_global_variables(*state_comparator, region1->object_info(), simgrid::mc::ProcessIndexDisabled, region1,
1613 XBT_DEBUG("(%d - %d) Different global variables in %s",
1614 num1, num2, name.c_str());
1618 XBT_VERB("(%d - %d) Different global variables in %s",
1619 num1, num2, name.c_str());
1628 if (simgrid::mc::mmalloc_compare_heap(*state_comparator, s1, s2) > 0) {
1631 XBT_DEBUG("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
1636 XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
1643 if (errors || hash_result)
1644 XBT_VERB("(%d - %d) Difference found", num1, num2);
1646 XBT_VERB("(%d - %d) No difference found", num1, num2);
1649 #if defined(MC_DEBUG) && defined(MC_VERBOSE)
1651 // * false positive SHOULD be avoided.
1652 // * There MUST not be any false negative.
1654 XBT_VERB("(%d - %d) State equality hash test is %s %s", num1, num2,
1655 (hash_result != 0) == (errors != 0) ? "true" : "false", not hash_result ? "positive" : "negative");
1659 return errors > 0 || hash_result;