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 */
8 #include "src/mc/mc_config.hpp"
9 #include "src/mc/mc_private.hpp"
10 #include "src/mc/mc_smx.hpp"
11 #include "src/mc/sosp/Snapshot.hpp"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_compare, xbt, "Logging specific to mc_compare in mc");
15 using simgrid::mc::remote;
20 /*********************************** Heap comparison ***********************************/
21 /***************************************************************************************/
28 HeapLocation() = default;
29 HeapLocation(int block, int fragment = 0) : block_(block), fragment_(fragment) {}
31 bool operator==(HeapLocation const& that) const
33 return block_ == that.block_ && fragment_ == that.fragment_;
35 bool operator<(HeapLocation const& that) const
37 return std::make_pair(block_, fragment_) < std::make_pair(that.block_, that.fragment_);
41 typedef std::array<HeapLocation, 2> HeapLocationPair;
42 typedef std::set<HeapLocationPair> HeapLocationPairs;
44 struct ProcessComparisonState;
45 struct StateComparator;
48 HeapLocationPair makeHeapLocationPair(int block1, int fragment1, int block2, int fragment2)
50 return HeapLocationPair{{HeapLocation(block1, fragment1), HeapLocation(block2, fragment2)}};
53 class HeapArea : public HeapLocation {
57 explicit HeapArea(int block) : valid_(true) { block_ = block; }
58 HeapArea(int block, int fragment) : valid_(true)
65 class ProcessComparisonState {
67 std::vector<simgrid::mc::IgnoredHeapRegion>* to_ignore = nullptr;
68 std::vector<HeapArea> equals_to;
69 std::vector<simgrid::mc::Type*> types;
70 std::size_t heapsize = 0;
72 void initHeapInformation(xbt_mheap_t heap, std::vector<simgrid::mc::IgnoredHeapRegion>* i);
75 static bool heap_area_differ(StateComparator& state, const void* area1, const void* area2, Snapshot* snapshot1,
76 Snapshot* snapshot2, HeapLocationPairs* previous, Type* type, int pointer_level);
78 class StateComparator {
80 s_xbt_mheap_t std_heap_copy;
81 std::size_t heaplimit;
82 std::array<ProcessComparisonState, 2> processStates;
84 std::unordered_set<std::pair<const void*, const void*>, simgrid::xbt::hash<std::pair<const void*, const void*>>>
89 compared_pointers.clear();
92 int initHeapInformation(
93 xbt_mheap_t heap1, xbt_mheap_t heap2,
94 std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
95 std::vector<simgrid::mc::IgnoredHeapRegion>* i2);
97 HeapArea& equals_to1_(std::size_t i, std::size_t j)
99 return processStates[0].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
101 HeapArea& equals_to2_(std::size_t i, std::size_t j)
103 return processStates[1].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
105 Type*& types1_(std::size_t i, std::size_t j)
107 return processStates[0].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
109 Type*& types2_(std::size_t i, std::size_t j)
111 return processStates[1].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
114 HeapArea const& equals_to1_(std::size_t i, std::size_t j) const
116 return processStates[0].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
118 HeapArea const& equals_to2_(std::size_t i, std::size_t j) const
120 return processStates[1].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
122 Type* const& types1_(std::size_t i, std::size_t j) const
124 return processStates[0].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
126 Type* const& types2_(std::size_t i, std::size_t j) const
128 return processStates[1].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
131 /** Check whether two blocks are known to be matching
133 * @param b1 Block of state 1
134 * @param b2 Block of state 2
135 * @return if the blocks are known to be matching
137 bool blocksEqual(int b1, int b2) const
139 return this->equals_to1_(b1, 0).block_ == b2 && this->equals_to2_(b2, 0).block_ == b1;
142 /** Check whether two fragments are known to be matching
144 * @param b1 Block of state 1
145 * @param f1 Fragment of state 1
146 * @param b2 Block of state 2
147 * @param f2 Fragment of state 2
148 * @return if the fragments are known to be matching
150 int fragmentsEqual(int b1, int f1, int b2, int f2) const
152 return this->equals_to1_(b1, f1).block_ == b2 && this->equals_to1_(b1, f1).fragment_ == f2 &&
153 this->equals_to2_(b2, f2).block_ == b1 && this->equals_to2_(b2, f2).fragment_ == f1;
156 void match_equals(HeapLocationPairs* list);
162 /************************************************************************************/
164 static ssize_t heap_comparison_ignore_size(
165 std::vector<simgrid::mc::IgnoredHeapRegion>* ignore_list,
169 int end = ignore_list->size() - 1;
171 while (start <= end) {
172 unsigned int cursor = (start + end) / 2;
173 simgrid::mc::IgnoredHeapRegion const& region = (*ignore_list)[cursor];
174 if (region.address == address)
176 if (region.address < address)
178 if (region.address > address)
185 static bool is_on_heap(const void* address)
187 const xbt_mheap_t heap = mc_model_checker->process().get_heap();
188 return address >= heap->heapbase && address < heap->breakval;
191 static bool is_stack(const void *address)
193 for (auto const& stack : mc_model_checker->process().stack_areas())
194 if (address == stack.address)
199 // TODO, this should depend on the snapshot?
200 static bool is_block_stack(int block)
202 for (auto const& stack : mc_model_checker->process().stack_areas())
203 if (block == stack.block)
211 void StateComparator::match_equals(HeapLocationPairs* list)
213 for (auto const& pair : *list) {
214 if (pair[0].fragment_ != -1) {
215 this->equals_to1_(pair[0].block_, pair[0].fragment_) = simgrid::mc::HeapArea(pair[1].block_, pair[1].fragment_);
216 this->equals_to2_(pair[1].block_, pair[1].fragment_) = simgrid::mc::HeapArea(pair[0].block_, pair[0].fragment_);
218 this->equals_to1_(pair[0].block_, 0) = simgrid::mc::HeapArea(pair[1].block_, pair[1].fragment_);
219 this->equals_to2_(pair[1].block_, 0) = simgrid::mc::HeapArea(pair[0].block_, pair[0].fragment_);
224 void ProcessComparisonState::initHeapInformation(xbt_mheap_t heap,
225 std::vector<simgrid::mc::IgnoredHeapRegion>* i)
227 auto heaplimit = heap->heaplimit;
228 this->heapsize = heap->heapsize;
230 this->equals_to.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, HeapArea());
231 this->types.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, nullptr);
234 int StateComparator::initHeapInformation(xbt_mheap_t heap1, xbt_mheap_t heap2,
235 std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
236 std::vector<simgrid::mc::IgnoredHeapRegion>* i2)
238 if ((heap1->heaplimit != heap2->heaplimit) || (heap1->heapsize != heap2->heapsize))
240 this->heaplimit = heap1->heaplimit;
241 this->std_heap_copy = *mc_model_checker->process().get_heap();
242 this->processStates[0].initHeapInformation(heap1, i1);
243 this->processStates[1].initHeapInformation(heap2, i2);
247 // TODO, have a robust way to find it in O(1)
248 static inline Region* MC_get_heap_region(Snapshot* snapshot)
250 for (auto const& region : snapshot->snapshot_regions_)
251 if (region->region_type() == simgrid::mc::RegionType::Heap)
253 xbt_die("No heap region");
256 static bool mmalloc_heap_differ(simgrid::mc::StateComparator& state, simgrid::mc::Snapshot* snapshot1,
257 simgrid::mc::Snapshot* snapshot2)
259 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
261 /* Check busy blocks */
264 malloc_info heapinfo_temp1;
265 malloc_info heapinfo_temp2;
266 malloc_info heapinfo_temp2b;
268 simgrid::mc::Region* heap_region1 = MC_get_heap_region(snapshot1);
269 simgrid::mc::Region* heap_region2 = MC_get_heap_region(snapshot2);
271 // This is the address of std_heap->heapinfo in the application process:
272 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
274 // This is in snapshot do not use them directly:
275 const malloc_info* heapinfos1 =
276 snapshot1->read<malloc_info*>(RemotePtr<malloc_info*>((std::uint64_t)heapinfo_address));
277 const malloc_info* heapinfos2 =
278 snapshot2->read<malloc_info*>(RemotePtr<malloc_info*>((std::uint64_t)heapinfo_address));
280 while (i1 < state.heaplimit) {
282 const malloc_info* heapinfo1 =
283 (const malloc_info*)heap_region1->read(&heapinfo_temp1, &heapinfos1[i1], sizeof(malloc_info));
284 const malloc_info* heapinfo2 =
285 (const malloc_info*)heap_region2->read(&heapinfo_temp2, &heapinfos2[i1], sizeof(malloc_info));
287 if (heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type == MMALLOC_TYPE_HEAPINFO) { /* Free block */
292 if (heapinfo1->type < 0) {
293 fprintf(stderr, "Unkown mmalloc block type.\n");
297 void* addr_block1 = ((void*)(((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase));
299 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED) { /* Large block */
301 if (is_stack(addr_block1)) {
302 for (size_t k = 0; k < heapinfo1->busy_block.size; k++)
303 state.equals_to1_(i1 + k, 0) = HeapArea(i1, -1);
304 for (size_t k = 0; k < heapinfo2->busy_block.size; k++)
305 state.equals_to2_(i1 + k, 0) = HeapArea(i1, -1);
306 i1 += heapinfo1->busy_block.size;
310 if (state.equals_to1_(i1, 0).valid_) {
318 /* Try first to associate to same block in the other heap */
319 if (heapinfo2->type == heapinfo1->type && state.equals_to2_(i1, 0).valid_ == 0) {
320 void* addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
321 if (not heap_area_differ(state, addr_block1, addr_block2, snapshot1, snapshot2, nullptr, nullptr, 0)) {
322 for (size_t k = 1; k < heapinfo2->busy_block.size; k++)
323 state.equals_to2_(i1 + k, 0) = HeapArea(i1, -1);
324 for (size_t k = 1; k < heapinfo1->busy_block.size; k++)
325 state.equals_to1_(i1 + k, 0) = HeapArea(i1, -1);
327 i1 += heapinfo1->busy_block.size;
331 while (i2 < state.heaplimit && not equal) {
333 void* addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
340 const malloc_info* heapinfo2b =
341 (const malloc_info*)heap_region2->read(&heapinfo_temp2b, &heapinfos2[i2], sizeof(malloc_info));
343 if (heapinfo2b->type != MMALLOC_TYPE_UNFRAGMENTED) {
348 if (state.equals_to2_(i2, 0).valid_) {
353 if (not heap_area_differ(state, addr_block1, addr_block2, snapshot1, snapshot2, nullptr, nullptr, 0)) {
354 for (size_t k = 1; k < heapinfo2b->busy_block.size; k++)
355 state.equals_to2_(i2 + k, 0) = HeapArea(i1, -1);
356 for (size_t k = 1; k < heapinfo1->busy_block.size; k++)
357 state.equals_to1_(i1 + k, 0) = HeapArea(i2, -1);
359 i1 += heapinfo1->busy_block.size;
366 XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1, heapinfo1->busy_block.busy_size, addr_block1);
370 } else { /* Fragmented block */
372 for (size_t j1 = 0; j1 < (size_t)(BLOCKSIZE >> heapinfo1->type); j1++) {
374 if (heapinfo1->busy_frag.frag_size[j1] == -1) /* Free fragment_ */
377 if (state.equals_to1_(i1, j1).valid_)
380 void* addr_frag1 = (void*)((char*)addr_block1 + (j1 << heapinfo1->type));
385 /* Try first to associate to same fragment_ in the other heap */
386 if (heapinfo2->type == heapinfo1->type && not state.equals_to2_(i1, j1).valid_) {
387 void* addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
388 void* addr_frag2 = (void*)((char*)addr_block2 + (j1 << heapinfo2->type));
389 if (not heap_area_differ(state, addr_frag1, addr_frag2, snapshot1, snapshot2, nullptr, nullptr, 0))
393 while (i2 < state.heaplimit && not equal) {
395 const malloc_info* heapinfo2b =
396 (const malloc_info*)heap_region2->read(&heapinfo_temp2b, &heapinfos2[i2], sizeof(malloc_info));
398 if (heapinfo2b->type == MMALLOC_TYPE_FREE || heapinfo2b->type == MMALLOC_TYPE_HEAPINFO) {
403 // We currently do not match fragments with unfragmented blocks (maybe we should).
404 if (heapinfo2b->type == MMALLOC_TYPE_UNFRAGMENTED) {
409 if (heapinfo2b->type < 0) {
410 fprintf(stderr, "Unknown mmalloc block type.\n");
414 for (size_t j2 = 0; j2 < (size_t)(BLOCKSIZE >> heapinfo2b->type); j2++) {
416 if (i2 == i1 && j2 == j1)
419 if (state.equals_to2_(i2, j2).valid_)
422 void* addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
423 void* addr_frag2 = (void*)((char*)addr_block2 + (j2 << heapinfo2b->type));
425 if (not heap_area_differ(state, addr_frag1, addr_frag2, snapshot1, snapshot2, nullptr, nullptr, 0)) {
435 XBT_DEBUG("Block %zu, fragment_ %zu not found (size_used = %zd, address = %p)\n", i1, j1,
436 heapinfo1->busy_frag.frag_size[j1], addr_frag1);
445 /* All blocks/fragments are equal to another block/fragment_ ? */
446 for (size_t i = 1; i < state.heaplimit; i++) {
447 const malloc_info* heapinfo1 =
448 (const malloc_info*)heap_region1->read(&heapinfo_temp1, &heapinfos1[i], sizeof(malloc_info));
450 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED && i1 == state.heaplimit && heapinfo1->busy_block.busy_size > 0 &&
451 not state.equals_to1_(i, 0).valid_) {
452 XBT_DEBUG("Block %zu not found (size used = %zu)", i, heapinfo1->busy_block.busy_size);
456 if (heapinfo1->type <= 0)
458 for (size_t j = 0; j < (size_t)(BLOCKSIZE >> heapinfo1->type); j++)
459 if (i1 == state.heaplimit && heapinfo1->busy_frag.frag_size[j] > 0 && not state.equals_to1_(i, j).valid_) {
460 XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)", i, j, heapinfo1->busy_frag.frag_size[j]);
465 for (size_t i = 1; i < state.heaplimit; i++) {
466 const malloc_info* heapinfo2 =
467 (const malloc_info*)heap_region2->read(&heapinfo_temp2, &heapinfos2[i], sizeof(malloc_info));
468 if (heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED && i1 == state.heaplimit && heapinfo2->busy_block.busy_size > 0 &&
469 not state.equals_to2_(i, 0).valid_) {
470 XBT_DEBUG("Block %zu not found (size used = %zu)", i,
471 heapinfo2->busy_block.busy_size);
475 if (heapinfo2->type <= 0)
478 for (size_t j = 0; j < (size_t)(BLOCKSIZE >> heapinfo2->type); j++)
479 if (i1 == state.heaplimit && heapinfo2->busy_frag.frag_size[j] > 0 && not state.equals_to2_(i, j).valid_) {
480 XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)",
481 i, j, heapinfo2->busy_frag.frag_size[j]);
492 * @param real_area1 Process address for state 1
493 * @param real_area2 Process address for state 2
494 * @param snapshot1 Snapshot of state 1
495 * @param snapshot2 Snapshot of state 2
498 * @param check_ignore
499 * @return true when different, false otherwise (same or unknown)
501 static bool heap_area_differ_without_type(simgrid::mc::StateComparator& state, const void* real_area1,
502 const void* real_area2, simgrid::mc::Snapshot* snapshot1,
503 simgrid::mc::Snapshot* snapshot2, HeapLocationPairs* previous, int size,
506 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
507 simgrid::mc::Region* heap_region1 = MC_get_heap_region(snapshot1);
508 simgrid::mc::Region* heap_region2 = MC_get_heap_region(snapshot2);
510 for (int i = 0; i < size; ) {
512 if (check_ignore > 0) {
513 ssize_t ignore1 = heap_comparison_ignore_size(state.processStates[0].to_ignore, (const char*)real_area1 + i);
515 ssize_t ignore2 = heap_comparison_ignore_size(state.processStates[1].to_ignore, (const char*)real_area2 + i);
516 if (ignore2 == ignore1) {
529 if (MC_snapshot_region_memcmp((const char*)real_area1 + i, heap_region1, (const char*)real_area2 + i, heap_region2,
532 int pointer_align = (i / sizeof(void *)) * sizeof(void *);
533 const void* addr_pointed1 = snapshot1->read(remote((void**)((const char*)real_area1 + pointer_align)));
534 const void* addr_pointed2 = snapshot2->read(remote((void**)((const char*)real_area2 + pointer_align)));
536 if (process->in_maestro_stack(remote(addr_pointed1))
537 && process->in_maestro_stack(remote(addr_pointed2))) {
538 i = pointer_align + sizeof(void *);
542 if (is_on_heap(addr_pointed1) && is_on_heap(addr_pointed2)) {
543 // Both addresses are in the heap:
544 if (heap_area_differ(state, addr_pointed1, addr_pointed2, snapshot1, snapshot2, previous, nullptr, 0))
546 i = pointer_align + sizeof(void *);
562 * @param real_area1 Process address for state 1
563 * @param real_area2 Process address for state 2
564 * @param snapshot1 Snapshot of state 1
565 * @param snapshot2 Snapshot of state 2
568 * @param area_size either a byte_size or an elements_count (?)
569 * @param check_ignore
570 * @param pointer_level
571 * @return true when different, false otherwise (same or unknown)
573 static bool heap_area_differ_with_type(simgrid::mc::StateComparator& state, const void* real_area1,
574 const void* real_area2, simgrid::mc::Snapshot* snapshot1,
575 simgrid::mc::Snapshot* snapshot2, HeapLocationPairs* previous,
576 simgrid::mc::Type* type, int area_size, int check_ignore, int pointer_level)
578 // HACK: This should not happen but in pratice, there are some
579 // DW_TAG_typedef without an associated DW_AT_type:
580 //<1><538832>: Abbrev Number: 111 (DW_TAG_typedef)
581 // <538833> DW_AT_name : (indirect string, offset: 0x2292f3): gregset_t
582 // <538837> DW_AT_decl_file : 98
583 // <538838> DW_AT_decl_line : 37
587 if (is_stack(real_area1) && is_stack(real_area2))
590 if (check_ignore > 0) {
591 ssize_t ignore1 = heap_comparison_ignore_size(state.processStates[0].to_ignore, real_area1);
592 if (ignore1 > 0 && heap_comparison_ignore_size(state.processStates[1].to_ignore, real_area2) == ignore1)
596 simgrid::mc::Type* subtype;
597 simgrid::mc::Type* subsubtype;
599 const void* addr_pointed1;
600 const void* addr_pointed2;
602 simgrid::mc::Region* heap_region1 = MC_get_heap_region(snapshot1);
603 simgrid::mc::Region* heap_region2 = MC_get_heap_region(snapshot2);
605 switch (type->type) {
606 case DW_TAG_unspecified_type:
609 case DW_TAG_base_type:
610 if (not type->name.empty() && type->name == "char") { /* String, hence random (arbitrary ?) size */
611 if (real_area1 == real_area2)
614 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, area_size) != 0;
616 if (area_size != -1 && type->byte_size != area_size)
619 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
622 case DW_TAG_enumeration_type:
623 if (area_size != -1 && type->byte_size != area_size)
625 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
628 case DW_TAG_const_type:
629 case DW_TAG_volatile_type:
630 return heap_area_differ_with_type(state, real_area1, real_area2, snapshot1, snapshot2, previous, type->subtype,
631 area_size, check_ignore, pointer_level);
633 case DW_TAG_array_type:
634 subtype = type->subtype;
635 switch (subtype->type) {
636 case DW_TAG_unspecified_type:
639 case DW_TAG_base_type:
640 case DW_TAG_enumeration_type:
641 case DW_TAG_pointer_type:
642 case DW_TAG_reference_type:
643 case DW_TAG_rvalue_reference_type:
644 case DW_TAG_structure_type:
645 case DW_TAG_class_type:
646 case DW_TAG_union_type:
647 if (subtype->full_type)
648 subtype = subtype->full_type;
649 elm_size = subtype->byte_size;
651 // TODO, just remove the type indirection?
652 case DW_TAG_const_type:
654 case DW_TAG_volatile_type:
655 subsubtype = subtype->subtype;
656 if (subsubtype->full_type)
657 subsubtype = subsubtype->full_type;
658 elm_size = subsubtype->byte_size;
663 for (int i = 0; i < type->element_count; i++) {
664 // TODO, add support for variable stride (DW_AT_byte_stride)
665 if (heap_area_differ_with_type(state, (const char*)real_area1 + (i * elm_size),
666 (const char*)real_area2 + (i * elm_size), snapshot1, snapshot2, previous,
667 type->subtype, subtype->byte_size, check_ignore, pointer_level))
672 case DW_TAG_reference_type:
673 case DW_TAG_rvalue_reference_type:
674 case DW_TAG_pointer_type:
675 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
676 addr_pointed1 = snapshot1->read(remote((void* const*)real_area1));
677 addr_pointed2 = snapshot2->read(remote((void* const*)real_area2));
678 return (addr_pointed1 != addr_pointed2);
681 if (pointer_level <= 1) {
682 addr_pointed1 = snapshot1->read(remote((void* const*)real_area1));
683 addr_pointed2 = snapshot2->read(remote((void* const*)real_area2));
684 if (is_on_heap(addr_pointed1) && is_on_heap(addr_pointed2))
685 return heap_area_differ(state, addr_pointed1, addr_pointed2, snapshot1, snapshot2, previous, type->subtype,
688 return (addr_pointed1 != addr_pointed2);
690 for (size_t i = 0; i < (area_size / sizeof(void*)); i++) {
691 addr_pointed1 = snapshot1->read(remote((void* const*)((const char*)real_area1 + i * sizeof(void*))));
692 addr_pointed2 = snapshot2->read(remote((void* const*)((const char*)real_area2 + i * sizeof(void*))));
693 bool differ = is_on_heap(addr_pointed1) && is_on_heap(addr_pointed2)
694 ? heap_area_differ(state, addr_pointed1, addr_pointed2, snapshot1, snapshot2, previous,
695 type->subtype, pointer_level)
696 : addr_pointed1 != addr_pointed2;
702 case DW_TAG_structure_type:
703 case DW_TAG_class_type:
705 type = type->full_type;
706 if (area_size != -1 && type->byte_size != area_size) {
707 if (area_size <= type->byte_size || area_size % type->byte_size != 0)
709 for (size_t i = 0; i < (size_t)(area_size / type->byte_size); i++) {
710 if (heap_area_differ_with_type(state, (const char*)real_area1 + i * type->byte_size,
711 (const char*)real_area2 + i * type->byte_size, snapshot1, snapshot2, previous,
712 type, -1, check_ignore, 0))
716 for (simgrid::mc::Member& member : type->members) {
717 // TODO, optimize this? (for the offset case)
719 simgrid::dwarf::resolve_member(real_area1, type, &member, (simgrid::mc::AddressSpace*)snapshot1);
721 simgrid::dwarf::resolve_member(real_area2, type, &member, (simgrid::mc::AddressSpace*)snapshot2);
722 if (heap_area_differ_with_type(state, real_member1, real_member2, snapshot1, snapshot2, previous,
723 member.type, -1, check_ignore, 0))
729 case DW_TAG_union_type:
730 return heap_area_differ_without_type(state, real_area1, real_area2, snapshot1, snapshot2, previous,
731 type->byte_size, check_ignore);
734 XBT_VERB("Unknown case: %d", type->type);
740 /** Infer the type of a part of the block from the type of the block
742 * TODO, handle DW_TAG_array_type as well as arrays of the object ((*p)[5], p[5])
744 * TODO, handle subfields ((*p).bar.foo, (*p)[5].bar…)
746 * @param type DWARF type ID of the root address
748 * @return DWARF type ID for given offset
750 static simgrid::mc::Type* get_offset_type(void* real_base_address, simgrid::mc::Type* type, int offset, int area_size,
751 simgrid::mc::Snapshot* snapshot)
754 // Beginning of the block, the infered variable type if the type of the block:
758 switch (type->type) {
760 case DW_TAG_structure_type:
761 case DW_TAG_class_type:
763 type = type->full_type;
764 if (area_size != -1 && type->byte_size != area_size) {
765 if (area_size > type->byte_size && area_size % type->byte_size == 0)
771 for (simgrid::mc::Member& member : type->members) {
772 if (member.has_offset_location()) {
773 // We have the offset, use it directly (shortcut):
774 if (member.offset() == offset)
777 void* real_member = simgrid::dwarf::resolve_member(real_base_address, type, &member, snapshot);
778 if ((char*)real_member - (char*)real_base_address == offset)
785 /* FIXME: other cases ? */
793 * @param area1 Process address for state 1
794 * @param area2 Process address for state 2
795 * @param snapshot1 Snapshot of state 1
796 * @param snapshot2 Snapshot of state 2
797 * @param previous Pairs of blocks already compared on the current path (or nullptr)
798 * @param type_id Type of variable
799 * @param pointer_level
800 * @return true when different, false otherwise (same or unknown)
802 static bool heap_area_differ(simgrid::mc::StateComparator& state, const void* area1, const void* area2,
803 simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2,
804 HeapLocationPairs* previous, simgrid::mc::Type* type, int pointer_level)
806 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
811 int check_ignore = 0;
819 simgrid::mc::Type* new_type1 = nullptr;
820 simgrid::mc::Type* new_type2 = nullptr;
822 bool match_pairs = false;
824 // This is the address of std_heap->heapinfo in the application process:
825 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
827 const malloc_info* heapinfos1 = snapshot1->read(remote((const malloc_info**)heapinfo_address));
828 const malloc_info* heapinfos2 = snapshot2->read(remote((const malloc_info**)heapinfo_address));
830 malloc_info heapinfo_temp1;
831 malloc_info heapinfo_temp2;
833 simgrid::mc::HeapLocationPairs current;
834 if (previous == nullptr) {
840 block1 = ((char*)area1 - (char*)state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
841 block2 = ((char*)area2 - (char*)state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
843 // If either block is a stack block:
844 if (is_block_stack((int) block1) && is_block_stack((int) block2)) {
845 previous->insert(simgrid::mc::makeHeapLocationPair(block1, -1, block2, -1));
847 state.match_equals(previous);
851 // If either block is not in the expected area of memory:
852 if (((char*)area1 < (char*)state.std_heap_copy.heapbase) || (block1 > (ssize_t)state.processStates[0].heapsize) ||
853 (block1 < 1) || ((char*)area2 < (char*)state.std_heap_copy.heapbase) ||
854 (block2 > (ssize_t)state.processStates[1].heapsize) || (block2 < 1)) {
858 // Process address of the block:
859 void* real_addr_block1 = (ADDR2UINT(block1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
860 void* real_addr_block2 = (ADDR2UINT(block2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
864 type = type->full_type;
866 // This assume that for "boring" types (volatile ...) byte_size is absent:
867 while (type->byte_size == 0 && type->subtype != nullptr)
868 type = type->subtype;
871 if (type->type == DW_TAG_pointer_type ||
872 (type->type == DW_TAG_base_type && not type->name.empty() && type->name == "char"))
875 type_size = type->byte_size;
879 simgrid::mc::Region* heap_region1 = MC_get_heap_region(snapshot1);
880 simgrid::mc::Region* heap_region2 = MC_get_heap_region(snapshot2);
882 const malloc_info* heapinfo1 =
883 (const malloc_info*)heap_region1->read(&heapinfo_temp1, &heapinfos1[block1], sizeof(malloc_info));
884 const malloc_info* heapinfo2 =
885 (const malloc_info*)heap_region2->read(&heapinfo_temp2, &heapinfos2[block2], sizeof(malloc_info));
887 if ((heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type==MMALLOC_TYPE_HEAPINFO)
888 && (heapinfo2->type == MMALLOC_TYPE_FREE || heapinfo2->type ==MMALLOC_TYPE_HEAPINFO)) {
891 state.match_equals(previous);
895 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED && heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
898 // TODO, lookup variable type from block type as done for fragmented blocks
900 if (state.equals_to1_(block1, 0).valid_ && state.equals_to2_(block2, 0).valid_ &&
901 state.blocksEqual(block1, block2)) {
903 state.match_equals(previous);
907 if (type_size != -1 && type_size != (ssize_t)heapinfo1->busy_block.busy_size &&
908 type_size != (ssize_t)heapinfo2->busy_block.busy_size &&
909 (type->name.empty() || type->name == "struct s_smx_context")) {
911 state.match_equals(previous);
915 if (heapinfo1->busy_block.size != heapinfo2->busy_block.size ||
916 heapinfo1->busy_block.busy_size != heapinfo2->busy_block.busy_size)
919 if (not previous->insert(simgrid::mc::makeHeapLocationPair(block1, -1, block2, -1)).second) {
921 state.match_equals(previous);
925 size = heapinfo1->busy_block.busy_size;
927 // Remember (basic) type inference.
928 // The current data structure only allows us to do this for the whole block.
929 if (type != nullptr && area1 == real_addr_block1)
930 state.types1_(block1, 0) = type;
931 if (type != nullptr && area2 == real_addr_block2)
932 state.types2_(block2, 0) = type;
936 state.match_equals(previous);
940 if (heapinfo1->busy_block.ignore > 0
941 && heapinfo2->busy_block.ignore == heapinfo1->busy_block.ignore)
942 check_ignore = heapinfo1->busy_block.ignore;
944 } else if ((heapinfo1->type > 0) && (heapinfo2->type > 0)) { /* Fragmented block */
947 ssize_t frag1 = ((uintptr_t)(ADDR2UINT(area1) % (BLOCKSIZE))) >> heapinfo1->type;
948 ssize_t frag2 = ((uintptr_t)(ADDR2UINT(area2) % (BLOCKSIZE))) >> heapinfo2->type;
950 // Process address of the fragment_:
951 void* real_addr_frag1 = (void*)((char*)real_addr_block1 + (frag1 << heapinfo1->type));
952 void* real_addr_frag2 = (void*)((char*)real_addr_block2 + (frag2 << heapinfo2->type));
954 // Check the size of the fragments against the size of the type:
955 if (type_size != -1) {
956 if (heapinfo1->busy_frag.frag_size[frag1] == -1 || heapinfo2->busy_frag.frag_size[frag2] == -1) {
958 state.match_equals(previous);
962 if (type_size != heapinfo1->busy_frag.frag_size[frag1]
963 || type_size != heapinfo2->busy_frag.frag_size[frag2]) {
965 state.match_equals(previous);
970 // Check if the blocks are already matched together:
971 if (state.equals_to1_(block1, frag1).valid_ && state.equals_to2_(block2, frag2).valid_ && offset1 == offset2 &&
972 state.fragmentsEqual(block1, frag1, block2, frag2)) {
974 state.match_equals(previous);
977 // Compare the size of both fragments:
978 if (heapinfo1->busy_frag.frag_size[frag1] != heapinfo2->busy_frag.frag_size[frag2]) {
979 if (type_size == -1) {
981 state.match_equals(previous);
987 // Size of the fragment_:
988 size = heapinfo1->busy_frag.frag_size[frag1];
990 // Remember (basic) type inference.
991 // The current data structure only allows us to do this for the whole fragment_.
992 if (type != nullptr && area1 == real_addr_frag1)
993 state.types1_(block1, frag1) = type;
994 if (type != nullptr && area2 == real_addr_frag2)
995 state.types2_(block2, frag2) = type;
997 // The type of the variable is already known:
999 new_type1 = new_type2 = type;
1001 // Type inference from the block type.
1002 else if (state.types1_(block1, frag1) != nullptr || state.types2_(block2, frag2) != nullptr) {
1004 offset1 = (char*)area1 - (char*)real_addr_frag1;
1005 offset2 = (char*)area2 - (char*)real_addr_frag2;
1007 if (state.types1_(block1, frag1) != nullptr && state.types2_(block2, frag2) != nullptr) {
1008 new_type1 = get_offset_type(real_addr_frag1, state.types1_(block1, frag1), offset1, size, snapshot1);
1009 new_type2 = get_offset_type(real_addr_frag2, state.types2_(block2, frag2), offset1, size, snapshot2);
1010 } else if (state.types1_(block1, frag1) != nullptr) {
1011 new_type1 = get_offset_type(real_addr_frag1, state.types1_(block1, frag1), offset1, size, snapshot1);
1012 new_type2 = get_offset_type(real_addr_frag2, state.types1_(block1, frag1), offset2, size, snapshot2);
1013 } else if (state.types2_(block2, frag2) != nullptr) {
1014 new_type1 = get_offset_type(real_addr_frag1, state.types2_(block2, frag2), offset1, size, snapshot1);
1015 new_type2 = get_offset_type(real_addr_frag2, state.types2_(block2, frag2), offset2, size, snapshot2);
1018 state.match_equals(previous);
1022 if (new_type1 != nullptr && new_type2 != nullptr && new_type1 != new_type2) {
1025 while (type->byte_size == 0 && type->subtype != nullptr)
1026 type = type->subtype;
1027 new_size1 = type->byte_size;
1030 while (type->byte_size == 0 && type->subtype != nullptr)
1031 type = type->subtype;
1032 new_size2 = type->byte_size;
1036 state.match_equals(previous);
1041 if (new_size1 > 0 && new_size1 == new_size2) {
1046 if (offset1 == 0 && offset2 == 0 &&
1047 not previous->insert(simgrid::mc::makeHeapLocationPair(block1, frag1, block2, frag2)).second) {
1049 state.match_equals(previous);
1055 state.match_equals(previous);
1059 if ((heapinfo1->busy_frag.ignore[frag1] > 0) &&
1060 (heapinfo2->busy_frag.ignore[frag2] == heapinfo1->busy_frag.ignore[frag1]))
1061 check_ignore = heapinfo1->busy_frag.ignore[frag1];
1066 /* Start comparison */
1068 type ? heap_area_differ_with_type(state, area1, area2, snapshot1, snapshot2, previous, type, size, check_ignore,
1070 : heap_area_differ_without_type(state, area1, area2, snapshot1, snapshot2, previous, size, check_ignore);
1075 state.match_equals(previous);
1082 /************************** Snapshot comparison *******************************/
1083 /******************************************************************************/
1085 static bool areas_differ_with_type(simgrid::mc::StateComparator& state, const void* real_area1,
1086 simgrid::mc::Snapshot* snapshot1, simgrid::mc::Region* region1,
1087 const void* real_area2, simgrid::mc::Snapshot* snapshot2,
1088 simgrid::mc::Region* region2, simgrid::mc::Type* type, int pointer_level)
1090 simgrid::mc::Type* subtype;
1091 simgrid::mc::Type* subsubtype;
1095 xbt_assert(type != nullptr);
1096 switch (type->type) {
1097 case DW_TAG_unspecified_type:
1100 case DW_TAG_base_type:
1101 case DW_TAG_enumeration_type:
1102 case DW_TAG_union_type:
1103 return MC_snapshot_region_memcmp(real_area1, region1, real_area2, region2, type->byte_size) != 0;
1104 case DW_TAG_typedef:
1105 case DW_TAG_volatile_type:
1106 case DW_TAG_const_type:
1107 return areas_differ_with_type(state, real_area1, snapshot1, region1, real_area2, snapshot2, region2,
1108 type->subtype, pointer_level);
1109 case DW_TAG_array_type:
1110 subtype = type->subtype;
1111 switch (subtype->type) {
1112 case DW_TAG_unspecified_type:
1115 case DW_TAG_base_type:
1116 case DW_TAG_enumeration_type:
1117 case DW_TAG_pointer_type:
1118 case DW_TAG_reference_type:
1119 case DW_TAG_rvalue_reference_type:
1120 case DW_TAG_structure_type:
1121 case DW_TAG_class_type:
1122 case DW_TAG_union_type:
1123 if (subtype->full_type)
1124 subtype = subtype->full_type;
1125 elm_size = subtype->byte_size;
1127 case DW_TAG_const_type:
1128 case DW_TAG_typedef:
1129 case DW_TAG_volatile_type:
1130 subsubtype = subtype->subtype;
1131 if (subsubtype->full_type)
1132 subsubtype = subsubtype->full_type;
1133 elm_size = subsubtype->byte_size;
1138 for (i = 0; i < type->element_count; i++) {
1139 size_t off = i * elm_size;
1140 if (areas_differ_with_type(state, (char*)real_area1 + off, snapshot1, region1, (char*)real_area2 + off,
1141 snapshot2, region2, type->subtype, pointer_level))
1145 case DW_TAG_pointer_type:
1146 case DW_TAG_reference_type:
1147 case DW_TAG_rvalue_reference_type: {
1148 const void* addr_pointed1 = MC_region_read_pointer(region1, real_area1);
1149 const void* addr_pointed2 = MC_region_read_pointer(region2, real_area2);
1151 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type)
1152 return (addr_pointed1 != addr_pointed2);
1153 if (addr_pointed1 == nullptr && addr_pointed2 == nullptr)
1155 if (addr_pointed1 == nullptr || addr_pointed2 == nullptr)
1157 if (not state.compared_pointers.insert(std::make_pair(addr_pointed1, addr_pointed2)).second)
1162 // Some cases are not handled here:
1163 // * the pointers lead to different areas (one to the heap, the other to the RW segment ...)
1164 // * a pointer leads to the read-only segment of the current object
1165 // * a pointer lead to a different ELF object
1167 if (is_on_heap(addr_pointed1)) {
1168 if (not is_on_heap(addr_pointed2))
1170 // The pointers are both in the heap:
1171 return simgrid::mc::heap_area_differ(state, addr_pointed1, addr_pointed2, snapshot1, snapshot2, nullptr,
1172 type->subtype, pointer_level);
1174 } else if (region1->contain(simgrid::mc::remote(addr_pointed1))) {
1175 // The pointers are both in the current object R/W segment:
1176 if (not region2->contain(simgrid::mc::remote(addr_pointed2)))
1178 if (not type->type_id)
1179 return (addr_pointed1 != addr_pointed2);
1181 return areas_differ_with_type(state, addr_pointed1, snapshot1, region1, addr_pointed2, snapshot2, region2,
1182 type->subtype, pointer_level);
1185 // TODO, We do not handle very well the case where
1186 // it belongs to a different (non-heap) region from the current one.
1188 return (addr_pointed1 != addr_pointed2);
1191 case DW_TAG_structure_type:
1192 case DW_TAG_class_type:
1193 for (simgrid::mc::Member& member : type->members) {
1194 void* member1 = simgrid::dwarf::resolve_member(real_area1, type, &member, snapshot1);
1195 void* member2 = simgrid::dwarf::resolve_member(real_area2, type, &member, snapshot2);
1196 simgrid::mc::Region* subregion1 = snapshot1->get_region(member1, region1); // region1 is hinted
1197 simgrid::mc::Region* subregion2 = snapshot2->get_region(member2, region2); // region2 is hinted
1198 if (areas_differ_with_type(state, member1, snapshot1, subregion1, member2, snapshot2, subregion2, member.type,
1203 case DW_TAG_subroutine_type:
1206 XBT_VERB("Unknown case: %d", type->type);
1213 static bool global_variables_differ(simgrid::mc::StateComparator& state, simgrid::mc::ObjectInformation* object_info,
1214 simgrid::mc::Region* r1, simgrid::mc::Region* r2, simgrid::mc::Snapshot* snapshot1,
1215 simgrid::mc::Snapshot* snapshot2)
1217 xbt_assert(r1 && r2, "Missing region.");
1219 std::vector<simgrid::mc::Variable>& variables = object_info->global_variables;
1221 for (simgrid::mc::Variable const& current_var : variables) {
1223 // If the variable is not in this object, skip it:
1224 // We do not expect to find a pointer to something which is not reachable
1225 // by the global variables.
1226 if ((char *) current_var.address < (char *) object_info->start_rw
1227 || (char *) current_var.address > (char *) object_info->end_rw)
1230 simgrid::mc::Type* bvariable_type = current_var.type;
1231 if (areas_differ_with_type(state, (char*)current_var.address, snapshot1, r1, (char*)current_var.address, snapshot2,
1232 r2, bvariable_type, 0)) {
1233 XBT_VERB("Global variable %s (%p) is different between snapshots",
1234 current_var.name.c_str(),
1235 (char *) current_var.address);
1243 static bool local_variables_differ(simgrid::mc::StateComparator& state, simgrid::mc::Snapshot* snapshot1,
1244 simgrid::mc::Snapshot* snapshot2, mc_snapshot_stack_t stack1,
1245 mc_snapshot_stack_t stack2)
1247 if (stack1->local_variables.size() != stack2->local_variables.size()) {
1248 XBT_VERB("Different number of local variables");
1252 for (unsigned int cursor = 0; cursor < stack1->local_variables.size(); cursor++) {
1253 local_variable_t current_var1 = &stack1->local_variables[cursor];
1254 local_variable_t current_var2 = &stack2->local_variables[cursor];
1255 if (current_var1->name != current_var2->name || current_var1->subprogram != current_var2->subprogram ||
1256 current_var1->ip != current_var2->ip) {
1257 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1258 XBT_VERB("Different name of variable (%s - %s) "
1259 "or frame (%s - %s) or ip (%lu - %lu)",
1260 current_var1->name.c_str(), current_var2->name.c_str(), current_var1->subprogram->name.c_str(),
1261 current_var2->subprogram->name.c_str(), current_var1->ip, current_var2->ip);
1265 if (areas_differ_with_type(state, current_var1->address, snapshot1, snapshot1->get_region(current_var1->address),
1266 current_var2->address, snapshot2, snapshot2->get_region(current_var2->address),
1267 current_var1->type, 0)) {
1268 XBT_VERB("Local variable %s (%p - %p) in frame %s "
1269 "is different between snapshots",
1270 current_var1->name.c_str(), current_var1->address, current_var2->address,
1271 current_var1->subprogram->name.c_str());
1281 static std::unique_ptr<simgrid::mc::StateComparator> state_comparator;
1283 bool snapshot_equal(Snapshot* s1, Snapshot* s2)
1285 // TODO, make this a field of ModelChecker or something similar
1286 if (state_comparator == nullptr)
1287 state_comparator.reset(new StateComparator());
1289 state_comparator->clear();
1291 RemoteClient* process = &mc_model_checker->process();
1293 if (s1->hash_ != s2->hash_) {
1294 XBT_VERB("(%d - %d) Different hash: 0x%" PRIx64 "--0x%" PRIx64, s1->num_state_, s2->num_state_, s1->hash_,
1298 XBT_VERB("(%d - %d) Same hash: 0x%" PRIx64, s1->num_state_, s2->num_state_, s1->hash_);
1300 /* Compare enabled processes */
1301 if (s1->enabled_processes_ != s2->enabled_processes_) {
1302 XBT_VERB("(%d - %d) Different amount of enabled processes", s1->num_state_, s2->num_state_);
1306 /* Compare size of stacks */
1307 for (unsigned long i = 0; i < s1->stacks_.size(); i++) {
1308 size_t size_used1 = s1->stack_sizes_[i];
1309 size_t size_used2 = s2->stack_sizes_[i];
1310 if (size_used1 != size_used2) {
1311 XBT_VERB("(%d - %d) Different size used in stacks: %zu - %zu", s1->num_state_, s2->num_state_, size_used1,
1317 /* Init heap information used in heap comparison algorithm */
1318 xbt_mheap_t heap1 = (xbt_mheap_t)s1->read_bytes(alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1319 remote(process->heap_address), simgrid::mc::ReadOptions::lazy());
1320 xbt_mheap_t heap2 = (xbt_mheap_t)s2->read_bytes(alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1321 remote(process->heap_address), simgrid::mc::ReadOptions::lazy());
1322 if (state_comparator->initHeapInformation(heap1, heap2, &s1->to_ignore_, &s2->to_ignore_) == -1) {
1323 XBT_VERB("(%d - %d) Different heap information", s1->num_state_, s2->num_state_);
1327 /* Stacks comparison */
1328 for (unsigned int cursor = 0; cursor < s1->stacks_.size(); cursor++) {
1329 mc_snapshot_stack_t stack1 = &s1->stacks_[cursor];
1330 mc_snapshot_stack_t stack2 = &s2->stacks_[cursor];
1332 if (local_variables_differ(*state_comparator, s1, s2, stack1, stack2)) {
1333 XBT_VERB("(%d - %d) Different local variables between stacks %u", s1->num_state_, s2->num_state_, cursor + 1);
1338 size_t regions_count = s1->snapshot_regions_.size();
1339 if (regions_count != s2->snapshot_regions_.size())
1342 for (size_t k = 0; k != regions_count; ++k) {
1343 Region* region1 = s1->snapshot_regions_[k].get();
1344 Region* region2 = s2->snapshot_regions_[k].get();
1347 if (region1->region_type() != RegionType::Data)
1350 xbt_assert(region1->region_type() == region2->region_type());
1351 xbt_assert(region1->object_info() == region2->object_info());
1352 xbt_assert(region1->object_info());
1354 /* Compare global variables */
1355 if (global_variables_differ(*state_comparator, region1->object_info(), region1, region2, s1, s2)) {
1356 std::string const& name = region1->object_info()->file_name;
1357 XBT_VERB("(%d - %d) Different global variables in %s", s1->num_state_, s2->num_state_, name.c_str());
1363 if (mmalloc_heap_differ(*state_comparator, s1, s2)) {
1364 XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", s1->num_state_, s2->num_state_);
1368 XBT_VERB("(%d - %d) No difference found", s1->num_state_, s2->num_state_);