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 class HeapArea : public HeapLocation {
48 explicit HeapArea(int block) : valid_(true) { block_ = block; }
49 HeapArea(int block, int fragment) : valid_(true)
56 class ProcessComparisonState {
58 std::vector<simgrid::mc::IgnoredHeapRegion>* to_ignore = nullptr;
59 std::vector<HeapArea> equals_to;
60 std::vector<simgrid::mc::Type*> types;
61 std::size_t heapsize = 0;
63 void initHeapInformation(xbt_mheap_t heap, std::vector<simgrid::mc::IgnoredHeapRegion>* i);
66 class StateComparator {
68 s_xbt_mheap_t std_heap_copy;
69 std::size_t heaplimit;
70 std::array<ProcessComparisonState, 2> processStates;
72 std::unordered_set<std::pair<const void*, const void*>, simgrid::xbt::hash<std::pair<const void*, const void*>>>
77 compared_pointers.clear();
80 int initHeapInformation(
81 xbt_mheap_t heap1, xbt_mheap_t heap2,
82 std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
83 std::vector<simgrid::mc::IgnoredHeapRegion>* i2);
85 template <int rank> HeapArea& equals_to_(std::size_t i, std::size_t j)
87 return processStates[rank - 1].equals_to[MAX_FRAGMENT_PER_BLOCK * i + j];
89 template <int rank> Type*& types_(std::size_t i, std::size_t j)
91 return processStates[rank - 1].types[MAX_FRAGMENT_PER_BLOCK * i + j];
94 template <int rank> HeapArea const& equals_to_(std::size_t i, std::size_t j) const
96 return processStates[rank - 1].equals_to[MAX_FRAGMENT_PER_BLOCK * i + j];
98 template <int rank> Type* const& types_(std::size_t i, std::size_t j) const
100 return processStates[rank - 1].types[MAX_FRAGMENT_PER_BLOCK * i + j];
103 /** Check whether two blocks are known to be matching
105 * @param b1 Block of state 1
106 * @param b2 Block of state 2
107 * @return if the blocks are known to be matching
109 bool blocksEqual(int b1, int b2) const
111 return this->equals_to_<1>(b1, 0).block_ == b2 && this->equals_to_<2>(b2, 0).block_ == b1;
114 /** Check whether two fragments are known to be matching
116 * @param b1 Block of state 1
117 * @param f1 Fragment of state 1
118 * @param b2 Block of state 2
119 * @param f2 Fragment of state 2
120 * @return if the fragments are known to be matching
122 int fragmentsEqual(int b1, int f1, int b2, int f2) const
124 return this->equals_to_<1>(b1, f1).block_ == b2 && this->equals_to_<1>(b1, f1).fragment_ == f2 &&
125 this->equals_to_<2>(b2, f2).block_ == b1 && this->equals_to_<2>(b2, f2).fragment_ == f1;
128 void match_equals(HeapLocationPairs* list);
134 /************************************************************************************/
136 static ssize_t heap_comparison_ignore_size(
137 std::vector<simgrid::mc::IgnoredHeapRegion>* ignore_list,
141 int end = ignore_list->size() - 1;
143 while (start <= end) {
144 unsigned int cursor = (start + end) / 2;
145 simgrid::mc::IgnoredHeapRegion const& region = (*ignore_list)[cursor];
146 if (region.address == address)
148 if (region.address < address)
150 if (region.address > address)
157 static bool is_on_heap(const void* address)
159 const xbt_mheap_t heap = mc_model_checker->process().get_heap();
160 return address >= heap->heapbase && address < heap->breakval;
163 static bool is_stack(const void *address)
165 for (auto const& stack : mc_model_checker->process().stack_areas())
166 if (address == stack.address)
171 // TODO, this should depend on the snapshot?
172 static bool is_block_stack(int block)
174 for (auto const& stack : mc_model_checker->process().stack_areas())
175 if (block == stack.block)
183 void StateComparator::match_equals(HeapLocationPairs* list)
185 for (auto const& pair : *list) {
186 if (pair[0].fragment_ != -1) {
187 this->equals_to_<1>(pair[0].block_, pair[0].fragment_) = simgrid::mc::HeapArea(pair[1].block_, pair[1].fragment_);
188 this->equals_to_<2>(pair[1].block_, pair[1].fragment_) = simgrid::mc::HeapArea(pair[0].block_, pair[0].fragment_);
190 this->equals_to_<1>(pair[0].block_, 0) = simgrid::mc::HeapArea(pair[1].block_, pair[1].fragment_);
191 this->equals_to_<2>(pair[1].block_, 0) = simgrid::mc::HeapArea(pair[0].block_, pair[0].fragment_);
196 void ProcessComparisonState::initHeapInformation(xbt_mheap_t heap,
197 std::vector<simgrid::mc::IgnoredHeapRegion>* i)
199 auto heaplimit = heap->heaplimit;
200 this->heapsize = heap->heapsize;
202 this->equals_to.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, HeapArea());
203 this->types.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, nullptr);
206 int StateComparator::initHeapInformation(xbt_mheap_t heap1, xbt_mheap_t heap2,
207 std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
208 std::vector<simgrid::mc::IgnoredHeapRegion>* i2)
210 if ((heap1->heaplimit != heap2->heaplimit) || (heap1->heapsize != heap2->heapsize))
212 this->heaplimit = heap1->heaplimit;
213 this->std_heap_copy = *mc_model_checker->process().get_heap();
214 this->processStates[0].initHeapInformation(heap1, i1);
215 this->processStates[1].initHeapInformation(heap2, i2);
219 // TODO, have a robust way to find it in O(1)
220 static inline Region* MC_get_heap_region(Snapshot* snapshot)
222 for (auto const& region : snapshot->snapshot_regions_)
223 if (region->region_type() == simgrid::mc::RegionType::Heap)
225 xbt_die("No heap region");
228 static bool heap_area_differ(StateComparator& state, const void* area1, const void* area2, Snapshot* snapshot1,
229 Snapshot* snapshot2, HeapLocationPairs* previous, Type* type, int pointer_level);
231 static bool mmalloc_heap_differ(simgrid::mc::StateComparator& state, simgrid::mc::Snapshot* snapshot1,
232 simgrid::mc::Snapshot* snapshot2)
234 const simgrid::mc::RemoteClient& process = mc_model_checker->process();
236 /* Check busy blocks */
239 malloc_info heapinfo_temp1;
240 malloc_info heapinfo_temp2;
241 malloc_info heapinfo_temp2b;
243 simgrid::mc::Region* heap_region1 = MC_get_heap_region(snapshot1);
244 simgrid::mc::Region* heap_region2 = MC_get_heap_region(snapshot2);
246 // This is the address of std_heap->heapinfo in the application process:
247 void* heapinfo_address = &((xbt_mheap_t)process.heap_address)->heapinfo;
249 // This is in snapshot do not use them directly:
250 const malloc_info* heapinfos1 =
251 snapshot1->read<malloc_info*>(RemotePtr<malloc_info*>((std::uint64_t)heapinfo_address));
252 const malloc_info* heapinfos2 =
253 snapshot2->read<malloc_info*>(RemotePtr<malloc_info*>((std::uint64_t)heapinfo_address));
255 while (i1 < state.heaplimit) {
257 const malloc_info* heapinfo1 =
258 (const malloc_info*)heap_region1->read(&heapinfo_temp1, &heapinfos1[i1], sizeof(malloc_info));
259 const malloc_info* heapinfo2 =
260 (const malloc_info*)heap_region2->read(&heapinfo_temp2, &heapinfos2[i1], sizeof(malloc_info));
262 if (heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type == MMALLOC_TYPE_HEAPINFO) { /* Free block */
267 if (heapinfo1->type < 0) {
268 fprintf(stderr, "Unkown mmalloc block type.\n");
272 void* addr_block1 = ((void*)(((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase));
274 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED) { /* Large block */
276 if (is_stack(addr_block1)) {
277 for (size_t k = 0; k < heapinfo1->busy_block.size; k++)
278 state.equals_to_<1>(i1 + k, 0) = HeapArea(i1, -1);
279 for (size_t k = 0; k < heapinfo2->busy_block.size; k++)
280 state.equals_to_<2>(i1 + k, 0) = HeapArea(i1, -1);
281 i1 += heapinfo1->busy_block.size;
285 if (state.equals_to_<1>(i1, 0).valid_) {
293 /* Try first to associate to same block in the other heap */
294 if (heapinfo2->type == heapinfo1->type && state.equals_to_<2>(i1, 0).valid_ == 0) {
295 void* addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
296 if (not heap_area_differ(state, addr_block1, addr_block2, snapshot1, snapshot2, nullptr, nullptr, 0)) {
297 for (size_t k = 1; k < heapinfo2->busy_block.size; k++)
298 state.equals_to_<2>(i1 + k, 0) = HeapArea(i1, -1);
299 for (size_t k = 1; k < heapinfo1->busy_block.size; k++)
300 state.equals_to_<1>(i1 + k, 0) = HeapArea(i1, -1);
302 i1 += heapinfo1->busy_block.size;
306 while (i2 < state.heaplimit && not equal) {
308 void* addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
315 const malloc_info* heapinfo2b =
316 (const malloc_info*)heap_region2->read(&heapinfo_temp2b, &heapinfos2[i2], sizeof(malloc_info));
318 if (heapinfo2b->type != MMALLOC_TYPE_UNFRAGMENTED) {
323 if (state.equals_to_<2>(i2, 0).valid_) {
328 if (not heap_area_differ(state, addr_block1, addr_block2, snapshot1, snapshot2, nullptr, nullptr, 0)) {
329 for (size_t k = 1; k < heapinfo2b->busy_block.size; k++)
330 state.equals_to_<2>(i2 + k, 0) = HeapArea(i1, -1);
331 for (size_t k = 1; k < heapinfo1->busy_block.size; k++)
332 state.equals_to_<1>(i1 + k, 0) = HeapArea(i2, -1);
334 i1 += heapinfo1->busy_block.size;
341 XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1, heapinfo1->busy_block.busy_size, addr_block1);
345 } else { /* Fragmented block */
347 for (size_t j1 = 0; j1 < (size_t)(BLOCKSIZE >> heapinfo1->type); j1++) {
349 if (heapinfo1->busy_frag.frag_size[j1] == -1) /* Free fragment_ */
352 if (state.equals_to_<1>(i1, j1).valid_)
355 void* addr_frag1 = (void*)((char*)addr_block1 + (j1 << heapinfo1->type));
360 /* Try first to associate to same fragment_ in the other heap */
361 if (heapinfo2->type == heapinfo1->type && not state.equals_to_<2>(i1, j1).valid_) {
362 void* addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
363 void* addr_frag2 = (void*)((char*)addr_block2 + (j1 << heapinfo2->type));
364 if (not heap_area_differ(state, addr_frag1, addr_frag2, snapshot1, snapshot2, nullptr, nullptr, 0))
368 while (i2 < state.heaplimit && not equal) {
370 const malloc_info* heapinfo2b =
371 (const malloc_info*)heap_region2->read(&heapinfo_temp2b, &heapinfos2[i2], sizeof(malloc_info));
373 if (heapinfo2b->type == MMALLOC_TYPE_FREE || heapinfo2b->type == MMALLOC_TYPE_HEAPINFO) {
378 // We currently do not match fragments with unfragmented blocks (maybe we should).
379 if (heapinfo2b->type == MMALLOC_TYPE_UNFRAGMENTED) {
384 if (heapinfo2b->type < 0) {
385 fprintf(stderr, "Unknown mmalloc block type.\n");
389 for (size_t j2 = 0; j2 < (size_t)(BLOCKSIZE >> heapinfo2b->type); j2++) {
391 if (i2 == i1 && j2 == j1)
394 if (state.equals_to_<2>(i2, j2).valid_)
397 void* addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
398 void* addr_frag2 = (void*)((char*)addr_block2 + (j2 << heapinfo2b->type));
400 if (not heap_area_differ(state, addr_frag1, addr_frag2, snapshot1, snapshot2, nullptr, nullptr, 0)) {
410 XBT_DEBUG("Block %zu, fragment_ %zu not found (size_used = %zd, address = %p)\n", i1, j1,
411 heapinfo1->busy_frag.frag_size[j1], addr_frag1);
420 /* All blocks/fragments are equal to another block/fragment_ ? */
421 for (size_t i = 1; i < state.heaplimit; i++) {
422 const malloc_info* heapinfo1 =
423 (const malloc_info*)heap_region1->read(&heapinfo_temp1, &heapinfos1[i], sizeof(malloc_info));
425 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED && i1 == state.heaplimit && heapinfo1->busy_block.busy_size > 0 &&
426 not state.equals_to_<1>(i, 0).valid_) {
427 XBT_DEBUG("Block %zu not found (size used = %zu)", i, heapinfo1->busy_block.busy_size);
431 if (heapinfo1->type <= 0)
433 for (size_t j = 0; j < (size_t)(BLOCKSIZE >> heapinfo1->type); j++)
434 if (i1 == state.heaplimit && heapinfo1->busy_frag.frag_size[j] > 0 && not state.equals_to_<1>(i, j).valid_) {
435 XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)", i, j, heapinfo1->busy_frag.frag_size[j]);
440 for (size_t i = 1; i < state.heaplimit; i++) {
441 const malloc_info* heapinfo2 =
442 (const malloc_info*)heap_region2->read(&heapinfo_temp2, &heapinfos2[i], sizeof(malloc_info));
443 if (heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED && i1 == state.heaplimit && heapinfo2->busy_block.busy_size > 0 &&
444 not state.equals_to_<2>(i, 0).valid_) {
445 XBT_DEBUG("Block %zu not found (size used = %zu)", i,
446 heapinfo2->busy_block.busy_size);
450 if (heapinfo2->type <= 0)
453 for (size_t j = 0; j < (size_t)(BLOCKSIZE >> heapinfo2->type); j++)
454 if (i1 == state.heaplimit && heapinfo2->busy_frag.frag_size[j] > 0 && not state.equals_to_<2>(i, j).valid_) {
455 XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)",
456 i, j, heapinfo2->busy_frag.frag_size[j]);
467 * @param real_area1 Process address for state 1
468 * @param real_area2 Process address for state 2
469 * @param snapshot1 Snapshot of state 1
470 * @param snapshot2 Snapshot of state 2
473 * @param check_ignore
474 * @return true when different, false otherwise (same or unknown)
476 static bool heap_area_differ_without_type(simgrid::mc::StateComparator& state, const void* real_area1,
477 const void* real_area2, simgrid::mc::Snapshot* snapshot1,
478 simgrid::mc::Snapshot* snapshot2, HeapLocationPairs* previous, int size,
481 const simgrid::mc::RemoteClient& process = mc_model_checker->process();
482 simgrid::mc::Region* heap_region1 = MC_get_heap_region(snapshot1);
483 simgrid::mc::Region* heap_region2 = MC_get_heap_region(snapshot2);
485 for (int i = 0; i < size; ) {
487 if (check_ignore > 0) {
488 ssize_t ignore1 = heap_comparison_ignore_size(state.processStates[0].to_ignore, (const char*)real_area1 + i);
490 ssize_t ignore2 = heap_comparison_ignore_size(state.processStates[1].to_ignore, (const char*)real_area2 + i);
491 if (ignore2 == ignore1) {
504 if (MC_snapshot_region_memcmp((const char*)real_area1 + i, heap_region1, (const char*)real_area2 + i, heap_region2,
507 int pointer_align = (i / sizeof(void *)) * sizeof(void *);
508 const void* addr_pointed1 = snapshot1->read(remote((void**)((const char*)real_area1 + pointer_align)));
509 const void* addr_pointed2 = snapshot2->read(remote((void**)((const char*)real_area2 + pointer_align)));
511 if (process.in_maestro_stack(remote(addr_pointed1)) && process.in_maestro_stack(remote(addr_pointed2))) {
512 i = pointer_align + sizeof(void *);
516 if (is_on_heap(addr_pointed1) && is_on_heap(addr_pointed2)) {
517 // Both addresses are in the heap:
518 if (heap_area_differ(state, addr_pointed1, addr_pointed2, snapshot1, snapshot2, previous, nullptr, 0))
520 i = pointer_align + sizeof(void *);
536 * @param real_area1 Process address for state 1
537 * @param real_area2 Process address for state 2
538 * @param snapshot1 Snapshot of state 1
539 * @param snapshot2 Snapshot of state 2
542 * @param area_size either a byte_size or an elements_count (?)
543 * @param check_ignore
544 * @param pointer_level
545 * @return true when different, false otherwise (same or unknown)
547 static bool heap_area_differ_with_type(simgrid::mc::StateComparator& state, const void* real_area1,
548 const void* real_area2, simgrid::mc::Snapshot* snapshot1,
549 simgrid::mc::Snapshot* snapshot2, HeapLocationPairs* previous,
550 simgrid::mc::Type* type, int area_size, int check_ignore, int pointer_level)
552 // HACK: This should not happen but in pratice, there are some
553 // DW_TAG_typedef without an associated DW_AT_type:
554 //<1><538832>: Abbrev Number: 111 (DW_TAG_typedef)
555 // <538833> DW_AT_name : (indirect string, offset: 0x2292f3): gregset_t
556 // <538837> DW_AT_decl_file : 98
557 // <538838> DW_AT_decl_line : 37
561 if (is_stack(real_area1) && is_stack(real_area2))
564 if (check_ignore > 0) {
565 ssize_t ignore1 = heap_comparison_ignore_size(state.processStates[0].to_ignore, real_area1);
566 if (ignore1 > 0 && heap_comparison_ignore_size(state.processStates[1].to_ignore, real_area2) == ignore1)
570 simgrid::mc::Type* subtype;
571 simgrid::mc::Type* subsubtype;
573 const void* addr_pointed1;
574 const void* addr_pointed2;
576 simgrid::mc::Region* heap_region1 = MC_get_heap_region(snapshot1);
577 simgrid::mc::Region* heap_region2 = MC_get_heap_region(snapshot2);
579 switch (type->type) {
580 case DW_TAG_unspecified_type:
583 case DW_TAG_base_type:
584 if (not type->name.empty() && type->name == "char") { /* String, hence random (arbitrary ?) size */
585 if (real_area1 == real_area2)
588 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, area_size) != 0;
590 if (area_size != -1 && type->byte_size != area_size)
593 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
596 case DW_TAG_enumeration_type:
597 if (area_size != -1 && type->byte_size != area_size)
599 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
602 case DW_TAG_const_type:
603 case DW_TAG_volatile_type:
604 return heap_area_differ_with_type(state, real_area1, real_area2, snapshot1, snapshot2, previous, type->subtype,
605 area_size, check_ignore, pointer_level);
607 case DW_TAG_array_type:
608 subtype = type->subtype;
609 switch (subtype->type) {
610 case DW_TAG_unspecified_type:
613 case DW_TAG_base_type:
614 case DW_TAG_enumeration_type:
615 case DW_TAG_pointer_type:
616 case DW_TAG_reference_type:
617 case DW_TAG_rvalue_reference_type:
618 case DW_TAG_structure_type:
619 case DW_TAG_class_type:
620 case DW_TAG_union_type:
621 if (subtype->full_type)
622 subtype = subtype->full_type;
623 elm_size = subtype->byte_size;
625 // TODO, just remove the type indirection?
626 case DW_TAG_const_type:
628 case DW_TAG_volatile_type:
629 subsubtype = subtype->subtype;
630 if (subsubtype->full_type)
631 subsubtype = subsubtype->full_type;
632 elm_size = subsubtype->byte_size;
637 for (int i = 0; i < type->element_count; i++) {
638 // TODO, add support for variable stride (DW_AT_byte_stride)
639 if (heap_area_differ_with_type(state, (const char*)real_area1 + (i * elm_size),
640 (const char*)real_area2 + (i * elm_size), snapshot1, snapshot2, previous,
641 type->subtype, subtype->byte_size, check_ignore, pointer_level))
646 case DW_TAG_reference_type:
647 case DW_TAG_rvalue_reference_type:
648 case DW_TAG_pointer_type:
649 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
650 addr_pointed1 = snapshot1->read(remote((void* const*)real_area1));
651 addr_pointed2 = snapshot2->read(remote((void* const*)real_area2));
652 return (addr_pointed1 != addr_pointed2);
655 if (pointer_level <= 1) {
656 addr_pointed1 = snapshot1->read(remote((void* const*)real_area1));
657 addr_pointed2 = snapshot2->read(remote((void* const*)real_area2));
658 if (is_on_heap(addr_pointed1) && is_on_heap(addr_pointed2))
659 return heap_area_differ(state, addr_pointed1, addr_pointed2, snapshot1, snapshot2, previous, type->subtype,
662 return (addr_pointed1 != addr_pointed2);
664 for (size_t i = 0; i < (area_size / sizeof(void*)); i++) {
665 addr_pointed1 = snapshot1->read(remote((void* const*)((const char*)real_area1 + i * sizeof(void*))));
666 addr_pointed2 = snapshot2->read(remote((void* const*)((const char*)real_area2 + i * sizeof(void*))));
667 bool differ = is_on_heap(addr_pointed1) && is_on_heap(addr_pointed2)
668 ? heap_area_differ(state, addr_pointed1, addr_pointed2, snapshot1, snapshot2, previous,
669 type->subtype, pointer_level)
670 : addr_pointed1 != addr_pointed2;
676 case DW_TAG_structure_type:
677 case DW_TAG_class_type:
679 type = type->full_type;
680 if (area_size != -1 && type->byte_size != area_size) {
681 if (area_size <= type->byte_size || area_size % type->byte_size != 0)
683 for (size_t i = 0; i < (size_t)(area_size / type->byte_size); i++) {
684 if (heap_area_differ_with_type(state, (const char*)real_area1 + i * type->byte_size,
685 (const char*)real_area2 + i * type->byte_size, snapshot1, snapshot2, previous,
686 type, -1, check_ignore, 0))
690 for (simgrid::mc::Member& member : type->members) {
691 // TODO, optimize this? (for the offset case)
693 simgrid::dwarf::resolve_member(real_area1, type, &member, (simgrid::mc::AddressSpace*)snapshot1);
695 simgrid::dwarf::resolve_member(real_area2, type, &member, (simgrid::mc::AddressSpace*)snapshot2);
696 if (heap_area_differ_with_type(state, real_member1, real_member2, snapshot1, snapshot2, previous,
697 member.type, -1, check_ignore, 0))
703 case DW_TAG_union_type:
704 return heap_area_differ_without_type(state, real_area1, real_area2, snapshot1, snapshot2, previous,
705 type->byte_size, check_ignore);
708 XBT_VERB("Unknown case: %d", type->type);
714 /** Infer the type of a part of the block from the type of the block
716 * TODO, handle DW_TAG_array_type as well as arrays of the object ((*p)[5], p[5])
718 * TODO, handle subfields ((*p).bar.foo, (*p)[5].bar…)
720 * @param type DWARF type ID of the root address
722 * @return DWARF type ID for given offset
724 static simgrid::mc::Type* get_offset_type(void* real_base_address, simgrid::mc::Type* type, int offset, int area_size,
725 simgrid::mc::Snapshot* snapshot)
728 // Beginning of the block, the infered variable type if the type of the block:
732 switch (type->type) {
734 case DW_TAG_structure_type:
735 case DW_TAG_class_type:
737 type = type->full_type;
738 if (area_size != -1 && type->byte_size != area_size) {
739 if (area_size > type->byte_size && area_size % type->byte_size == 0)
745 for (simgrid::mc::Member& member : type->members) {
746 if (member.has_offset_location()) {
747 // We have the offset, use it directly (shortcut):
748 if (member.offset() == offset)
751 void* real_member = simgrid::dwarf::resolve_member(real_base_address, type, &member, snapshot);
752 if ((char*)real_member - (char*)real_base_address == offset)
759 /* FIXME: other cases ? */
767 * @param area1 Process address for state 1
768 * @param area2 Process address for state 2
769 * @param snapshot1 Snapshot of state 1
770 * @param snapshot2 Snapshot of state 2
771 * @param previous Pairs of blocks already compared on the current path (or nullptr)
772 * @param type_id Type of variable
773 * @param pointer_level
774 * @return true when different, false otherwise (same or unknown)
776 static bool heap_area_differ(simgrid::mc::StateComparator& state, const void* area1, const void* area2,
777 simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2,
778 HeapLocationPairs* previous, simgrid::mc::Type* type, int pointer_level)
780 const simgrid::mc::RemoteClient& process = mc_model_checker->process();
785 int check_ignore = 0;
793 simgrid::mc::Type* new_type1 = nullptr;
794 simgrid::mc::Type* new_type2 = nullptr;
796 bool match_pairs = false;
798 // This is the address of std_heap->heapinfo in the application process:
799 void* heapinfo_address = &((xbt_mheap_t)process.heap_address)->heapinfo;
801 const malloc_info* heapinfos1 = snapshot1->read(remote((const malloc_info**)heapinfo_address));
802 const malloc_info* heapinfos2 = snapshot2->read(remote((const malloc_info**)heapinfo_address));
804 malloc_info heapinfo_temp1;
805 malloc_info heapinfo_temp2;
807 simgrid::mc::HeapLocationPairs current;
808 if (previous == nullptr) {
814 block1 = ((char*)area1 - (char*)state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
815 block2 = ((char*)area2 - (char*)state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
817 // If either block is a stack block:
818 if (is_block_stack((int) block1) && is_block_stack((int) block2)) {
819 previous->insert(HeapLocationPair{{HeapLocation(block1, -1), HeapLocation(block2, -1)}});
821 state.match_equals(previous);
825 // If either block is not in the expected area of memory:
826 if (((char*)area1 < (char*)state.std_heap_copy.heapbase) || (block1 > (ssize_t)state.processStates[0].heapsize) ||
827 (block1 < 1) || ((char*)area2 < (char*)state.std_heap_copy.heapbase) ||
828 (block2 > (ssize_t)state.processStates[1].heapsize) || (block2 < 1)) {
832 // Process address of the block:
833 void* real_addr_block1 = (ADDR2UINT(block1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
834 void* real_addr_block2 = (ADDR2UINT(block2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
838 type = type->full_type;
840 // This assume that for "boring" types (volatile ...) byte_size is absent:
841 while (type->byte_size == 0 && type->subtype != nullptr)
842 type = type->subtype;
845 if (type->type == DW_TAG_pointer_type ||
846 (type->type == DW_TAG_base_type && not type->name.empty() && type->name == "char"))
849 type_size = type->byte_size;
853 simgrid::mc::Region* heap_region1 = MC_get_heap_region(snapshot1);
854 simgrid::mc::Region* heap_region2 = MC_get_heap_region(snapshot2);
856 const malloc_info* heapinfo1 =
857 (const malloc_info*)heap_region1->read(&heapinfo_temp1, &heapinfos1[block1], sizeof(malloc_info));
858 const malloc_info* heapinfo2 =
859 (const malloc_info*)heap_region2->read(&heapinfo_temp2, &heapinfos2[block2], sizeof(malloc_info));
861 if ((heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type==MMALLOC_TYPE_HEAPINFO)
862 && (heapinfo2->type == MMALLOC_TYPE_FREE || heapinfo2->type ==MMALLOC_TYPE_HEAPINFO)) {
865 state.match_equals(previous);
869 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED && heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
872 // TODO, lookup variable type from block type as done for fragmented blocks
874 if (state.equals_to_<1>(block1, 0).valid_ && state.equals_to_<2>(block2, 0).valid_ &&
875 state.blocksEqual(block1, block2)) {
877 state.match_equals(previous);
881 if (type_size != -1 && type_size != (ssize_t)heapinfo1->busy_block.busy_size &&
882 type_size != (ssize_t)heapinfo2->busy_block.busy_size &&
883 (type->name.empty() || type->name == "struct s_smx_context")) {
885 state.match_equals(previous);
889 if (heapinfo1->busy_block.size != heapinfo2->busy_block.size ||
890 heapinfo1->busy_block.busy_size != heapinfo2->busy_block.busy_size)
893 if (not previous->insert(HeapLocationPair{{HeapLocation(block1, -1), HeapLocation(block2, -1)}}).second) {
895 state.match_equals(previous);
899 size = heapinfo1->busy_block.busy_size;
901 // Remember (basic) type inference.
902 // The current data structure only allows us to do this for the whole block.
903 if (type != nullptr && area1 == real_addr_block1)
904 state.types_<1>(block1, 0) = type;
905 if (type != nullptr && area2 == real_addr_block2)
906 state.types_<2>(block2, 0) = type;
910 state.match_equals(previous);
914 if (heapinfo1->busy_block.ignore > 0
915 && heapinfo2->busy_block.ignore == heapinfo1->busy_block.ignore)
916 check_ignore = heapinfo1->busy_block.ignore;
918 } else if ((heapinfo1->type > 0) && (heapinfo2->type > 0)) { /* Fragmented block */
921 ssize_t frag1 = ((uintptr_t)(ADDR2UINT(area1) % (BLOCKSIZE))) >> heapinfo1->type;
922 ssize_t frag2 = ((uintptr_t)(ADDR2UINT(area2) % (BLOCKSIZE))) >> heapinfo2->type;
924 // Process address of the fragment_:
925 void* real_addr_frag1 = (void*)((char*)real_addr_block1 + (frag1 << heapinfo1->type));
926 void* real_addr_frag2 = (void*)((char*)real_addr_block2 + (frag2 << heapinfo2->type));
928 // Check the size of the fragments against the size of the type:
929 if (type_size != -1) {
930 if (heapinfo1->busy_frag.frag_size[frag1] == -1 || heapinfo2->busy_frag.frag_size[frag2] == -1) {
932 state.match_equals(previous);
936 if (type_size != heapinfo1->busy_frag.frag_size[frag1]
937 || type_size != heapinfo2->busy_frag.frag_size[frag2]) {
939 state.match_equals(previous);
944 // Check if the blocks are already matched together:
945 if (state.equals_to_<1>(block1, frag1).valid_ && state.equals_to_<2>(block2, frag2).valid_ && offset1 == offset2 &&
946 state.fragmentsEqual(block1, frag1, block2, frag2)) {
948 state.match_equals(previous);
951 // Compare the size of both fragments:
952 if (heapinfo1->busy_frag.frag_size[frag1] != heapinfo2->busy_frag.frag_size[frag2]) {
953 if (type_size == -1) {
955 state.match_equals(previous);
961 // Size of the fragment_:
962 size = heapinfo1->busy_frag.frag_size[frag1];
964 // Remember (basic) type inference.
965 // The current data structure only allows us to do this for the whole fragment_.
966 if (type != nullptr && area1 == real_addr_frag1)
967 state.types_<1>(block1, frag1) = type;
968 if (type != nullptr && area2 == real_addr_frag2)
969 state.types_<2>(block2, frag2) = type;
971 // The type of the variable is already known:
973 new_type1 = new_type2 = type;
975 // Type inference from the block type.
976 else if (state.types_<1>(block1, frag1) != nullptr || state.types_<2>(block2, frag2) != nullptr) {
978 offset1 = (char*)area1 - (char*)real_addr_frag1;
979 offset2 = (char*)area2 - (char*)real_addr_frag2;
981 if (state.types_<1>(block1, frag1) != nullptr && state.types_<2>(block2, frag2) != nullptr) {
982 new_type1 = get_offset_type(real_addr_frag1, state.types_<1>(block1, frag1), offset1, size, snapshot1);
983 new_type2 = get_offset_type(real_addr_frag2, state.types_<2>(block2, frag2), offset1, size, snapshot2);
984 } else if (state.types_<1>(block1, frag1) != nullptr) {
985 new_type1 = get_offset_type(real_addr_frag1, state.types_<1>(block1, frag1), offset1, size, snapshot1);
986 new_type2 = get_offset_type(real_addr_frag2, state.types_<1>(block1, frag1), offset2, size, snapshot2);
987 } else if (state.types_<2>(block2, frag2) != nullptr) {
988 new_type1 = get_offset_type(real_addr_frag1, state.types_<2>(block2, frag2), offset1, size, snapshot1);
989 new_type2 = get_offset_type(real_addr_frag2, state.types_<2>(block2, frag2), offset2, size, snapshot2);
992 state.match_equals(previous);
996 if (new_type1 != nullptr && new_type2 != nullptr && new_type1 != new_type2) {
999 while (type->byte_size == 0 && type->subtype != nullptr)
1000 type = type->subtype;
1001 new_size1 = type->byte_size;
1004 while (type->byte_size == 0 && type->subtype != nullptr)
1005 type = type->subtype;
1006 new_size2 = type->byte_size;
1010 state.match_equals(previous);
1015 if (new_size1 > 0 && new_size1 == new_size2) {
1020 if (offset1 == 0 && offset2 == 0 &&
1021 not previous->insert(HeapLocationPair{{HeapLocation(block1, frag1), HeapLocation(block2, frag2)}}).second) {
1023 state.match_equals(previous);
1029 state.match_equals(previous);
1033 if ((heapinfo1->busy_frag.ignore[frag1] > 0) &&
1034 (heapinfo2->busy_frag.ignore[frag2] == heapinfo1->busy_frag.ignore[frag1]))
1035 check_ignore = heapinfo1->busy_frag.ignore[frag1];
1040 /* Start comparison */
1042 type ? heap_area_differ_with_type(state, area1, area2, snapshot1, snapshot2, previous, type, size, check_ignore,
1044 : heap_area_differ_without_type(state, area1, area2, snapshot1, snapshot2, previous, size, check_ignore);
1049 state.match_equals(previous);
1056 /************************** Snapshot comparison *******************************/
1057 /******************************************************************************/
1059 static bool areas_differ_with_type(simgrid::mc::StateComparator& state, const void* real_area1,
1060 simgrid::mc::Snapshot* snapshot1, simgrid::mc::Region* region1,
1061 const void* real_area2, simgrid::mc::Snapshot* snapshot2,
1062 simgrid::mc::Region* region2, simgrid::mc::Type* type, int pointer_level)
1064 simgrid::mc::Type* subtype;
1065 simgrid::mc::Type* subsubtype;
1069 xbt_assert(type != nullptr);
1070 switch (type->type) {
1071 case DW_TAG_unspecified_type:
1074 case DW_TAG_base_type:
1075 case DW_TAG_enumeration_type:
1076 case DW_TAG_union_type:
1077 return MC_snapshot_region_memcmp(real_area1, region1, real_area2, region2, type->byte_size) != 0;
1078 case DW_TAG_typedef:
1079 case DW_TAG_volatile_type:
1080 case DW_TAG_const_type:
1081 return areas_differ_with_type(state, real_area1, snapshot1, region1, real_area2, snapshot2, region2,
1082 type->subtype, pointer_level);
1083 case DW_TAG_array_type:
1084 subtype = type->subtype;
1085 switch (subtype->type) {
1086 case DW_TAG_unspecified_type:
1089 case DW_TAG_base_type:
1090 case DW_TAG_enumeration_type:
1091 case DW_TAG_pointer_type:
1092 case DW_TAG_reference_type:
1093 case DW_TAG_rvalue_reference_type:
1094 case DW_TAG_structure_type:
1095 case DW_TAG_class_type:
1096 case DW_TAG_union_type:
1097 if (subtype->full_type)
1098 subtype = subtype->full_type;
1099 elm_size = subtype->byte_size;
1101 case DW_TAG_const_type:
1102 case DW_TAG_typedef:
1103 case DW_TAG_volatile_type:
1104 subsubtype = subtype->subtype;
1105 if (subsubtype->full_type)
1106 subsubtype = subsubtype->full_type;
1107 elm_size = subsubtype->byte_size;
1112 for (i = 0; i < type->element_count; i++) {
1113 size_t off = i * elm_size;
1114 if (areas_differ_with_type(state, (char*)real_area1 + off, snapshot1, region1, (char*)real_area2 + off,
1115 snapshot2, region2, type->subtype, pointer_level))
1119 case DW_TAG_pointer_type:
1120 case DW_TAG_reference_type:
1121 case DW_TAG_rvalue_reference_type: {
1122 const void* addr_pointed1 = MC_region_read_pointer(region1, real_area1);
1123 const void* addr_pointed2 = MC_region_read_pointer(region2, real_area2);
1125 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type)
1126 return (addr_pointed1 != addr_pointed2);
1127 if (addr_pointed1 == nullptr && addr_pointed2 == nullptr)
1129 if (addr_pointed1 == nullptr || addr_pointed2 == nullptr)
1131 if (not state.compared_pointers.insert(std::make_pair(addr_pointed1, addr_pointed2)).second)
1136 // Some cases are not handled here:
1137 // * the pointers lead to different areas (one to the heap, the other to the RW segment ...)
1138 // * a pointer leads to the read-only segment of the current object
1139 // * a pointer lead to a different ELF object
1141 if (is_on_heap(addr_pointed1)) {
1142 if (not is_on_heap(addr_pointed2))
1144 // The pointers are both in the heap:
1145 return simgrid::mc::heap_area_differ(state, addr_pointed1, addr_pointed2, snapshot1, snapshot2, nullptr,
1146 type->subtype, pointer_level);
1148 } else if (region1->contain(simgrid::mc::remote(addr_pointed1))) {
1149 // The pointers are both in the current object R/W segment:
1150 if (not region2->contain(simgrid::mc::remote(addr_pointed2)))
1152 if (not type->type_id)
1153 return (addr_pointed1 != addr_pointed2);
1155 return areas_differ_with_type(state, addr_pointed1, snapshot1, region1, addr_pointed2, snapshot2, region2,
1156 type->subtype, pointer_level);
1159 // TODO, We do not handle very well the case where
1160 // it belongs to a different (non-heap) region from the current one.
1162 return (addr_pointed1 != addr_pointed2);
1165 case DW_TAG_structure_type:
1166 case DW_TAG_class_type:
1167 for (simgrid::mc::Member& member : type->members) {
1168 void* member1 = simgrid::dwarf::resolve_member(real_area1, type, &member, snapshot1);
1169 void* member2 = simgrid::dwarf::resolve_member(real_area2, type, &member, snapshot2);
1170 simgrid::mc::Region* subregion1 = snapshot1->get_region(member1, region1); // region1 is hinted
1171 simgrid::mc::Region* subregion2 = snapshot2->get_region(member2, region2); // region2 is hinted
1172 if (areas_differ_with_type(state, member1, snapshot1, subregion1, member2, snapshot2, subregion2, member.type,
1177 case DW_TAG_subroutine_type:
1180 XBT_VERB("Unknown case: %d", type->type);
1187 static bool global_variables_differ(simgrid::mc::StateComparator& state, simgrid::mc::ObjectInformation* object_info,
1188 simgrid::mc::Region* r1, simgrid::mc::Region* r2, simgrid::mc::Snapshot* snapshot1,
1189 simgrid::mc::Snapshot* snapshot2)
1191 xbt_assert(r1 && r2, "Missing region.");
1193 std::vector<simgrid::mc::Variable>& variables = object_info->global_variables;
1195 for (simgrid::mc::Variable const& current_var : variables) {
1197 // If the variable is not in this object, skip it:
1198 // We do not expect to find a pointer to something which is not reachable
1199 // by the global variables.
1200 if ((char *) current_var.address < (char *) object_info->start_rw
1201 || (char *) current_var.address > (char *) object_info->end_rw)
1204 simgrid::mc::Type* bvariable_type = current_var.type;
1205 if (areas_differ_with_type(state, (char*)current_var.address, snapshot1, r1, (char*)current_var.address, snapshot2,
1206 r2, bvariable_type, 0)) {
1207 XBT_VERB("Global variable %s (%p) is different between snapshots",
1208 current_var.name.c_str(),
1209 (char *) current_var.address);
1217 static bool local_variables_differ(simgrid::mc::StateComparator& state, simgrid::mc::Snapshot* snapshot1,
1218 simgrid::mc::Snapshot* snapshot2, mc_snapshot_stack_t stack1,
1219 mc_snapshot_stack_t stack2)
1221 if (stack1->local_variables.size() != stack2->local_variables.size()) {
1222 XBT_VERB("Different number of local variables");
1226 for (unsigned int cursor = 0; cursor < stack1->local_variables.size(); cursor++) {
1227 local_variable_t current_var1 = &stack1->local_variables[cursor];
1228 local_variable_t current_var2 = &stack2->local_variables[cursor];
1229 if (current_var1->name != current_var2->name || current_var1->subprogram != current_var2->subprogram ||
1230 current_var1->ip != current_var2->ip) {
1231 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1232 XBT_VERB("Different name of variable (%s - %s) "
1233 "or frame (%s - %s) or ip (%lu - %lu)",
1234 current_var1->name.c_str(), current_var2->name.c_str(), current_var1->subprogram->name.c_str(),
1235 current_var2->subprogram->name.c_str(), current_var1->ip, current_var2->ip);
1239 if (areas_differ_with_type(state, current_var1->address, snapshot1, snapshot1->get_region(current_var1->address),
1240 current_var2->address, snapshot2, snapshot2->get_region(current_var2->address),
1241 current_var1->type, 0)) {
1242 XBT_VERB("Local variable %s (%p - %p) in frame %s "
1243 "is different between snapshots",
1244 current_var1->name.c_str(), current_var1->address, current_var2->address,
1245 current_var1->subprogram->name.c_str());
1255 bool snapshot_equal(Snapshot* s1, Snapshot* s2)
1257 // TODO, make this a field of ModelChecker or something similar
1258 static StateComparator state_comparator;
1260 const RemoteClient& process = mc_model_checker->process();
1262 if (s1->hash_ != s2->hash_) {
1263 XBT_VERB("(%d - %d) Different hash: 0x%" PRIx64 "--0x%" PRIx64, s1->num_state_, s2->num_state_, s1->hash_,
1267 XBT_VERB("(%d - %d) Same hash: 0x%" PRIx64, s1->num_state_, s2->num_state_, s1->hash_);
1269 /* Compare enabled processes */
1270 if (s1->enabled_processes_ != s2->enabled_processes_) {
1271 XBT_VERB("(%d - %d) Different amount of enabled processes", s1->num_state_, s2->num_state_);
1275 /* Compare size of stacks */
1276 for (unsigned long i = 0; i < s1->stacks_.size(); i++) {
1277 size_t size_used1 = s1->stack_sizes_[i];
1278 size_t size_used2 = s2->stack_sizes_[i];
1279 if (size_used1 != size_used2) {
1280 XBT_VERB("(%d - %d) Different size used in stacks: %zu - %zu", s1->num_state_, s2->num_state_, size_used1,
1286 /* Init heap information used in heap comparison algorithm */
1288 static_cast<xbt_mheap_t>(s1->read_bytes(alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1289 remote(process.heap_address), simgrid::mc::ReadOptions::lazy()));
1291 static_cast<xbt_mheap_t>(s2->read_bytes(alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1292 remote(process.heap_address), simgrid::mc::ReadOptions::lazy()));
1293 if (state_comparator.initHeapInformation(heap1, heap2, &s1->to_ignore_, &s2->to_ignore_) == -1) {
1294 XBT_VERB("(%d - %d) Different heap information", s1->num_state_, s2->num_state_);
1298 /* Stacks comparison */
1299 for (unsigned int cursor = 0; cursor < s1->stacks_.size(); cursor++) {
1300 mc_snapshot_stack_t stack1 = &s1->stacks_[cursor];
1301 mc_snapshot_stack_t stack2 = &s2->stacks_[cursor];
1303 if (local_variables_differ(state_comparator, s1, s2, stack1, stack2)) {
1304 XBT_VERB("(%d - %d) Different local variables between stacks %u", s1->num_state_, s2->num_state_, cursor + 1);
1309 size_t regions_count = s1->snapshot_regions_.size();
1310 if (regions_count != s2->snapshot_regions_.size())
1313 for (size_t k = 0; k != regions_count; ++k) {
1314 Region* region1 = s1->snapshot_regions_[k].get();
1315 Region* region2 = s2->snapshot_regions_[k].get();
1318 if (region1->region_type() != RegionType::Data)
1321 xbt_assert(region1->region_type() == region2->region_type());
1322 xbt_assert(region1->object_info() == region2->object_info());
1323 xbt_assert(region1->object_info());
1325 /* Compare global variables */
1326 if (global_variables_differ(state_comparator, region1->object_info(), region1, region2, s1, s2)) {
1327 std::string const& name = region1->object_info()->file_name;
1328 XBT_VERB("(%d - %d) Different global variables in %s", s1->num_state_, s2->num_state_, name.c_str());
1334 if (mmalloc_heap_differ(state_comparator, s1, s2)) {
1335 XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", s1->num_state_, s2->num_state_);
1339 XBT_VERB("(%d - %d) No difference found", s1->num_state_, s2->num_state_);