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_smx.hpp"
10 #include "src/mc/sosp/mc_snapshot.hpp"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_compare, xbt, "Logging specific to mc_compare in mc");
18 typedef std::array<HeapLocation, 2> HeapLocationPair;
19 typedef std::set<HeapLocationPair> HeapLocationPairs;
21 struct ProcessComparisonState;
22 struct StateComparator;
24 static int compare_heap_area(
25 StateComparator& state,
26 int process_index, const void *area1, const void* area2,
27 Snapshot* snapshot1, Snapshot* snapshot2,
28 HeapLocationPairs* previous, Type* type, int pointer_level);
33 using simgrid::mc::remote;
35 /*********************************** Heap comparison ***********************************/
36 /***************************************************************************************/
46 HeapLocation() = default;
47 HeapLocation(int block, int fragment = 0) : block_(block), fragment_(fragment) {}
49 bool operator==(HeapLocation const& that) const
51 return block_ == that.block_ && fragment_ == that.fragment_;
53 bool operator<(HeapLocation const& that) const
55 return std::make_pair(block_, fragment_) < std::make_pair(that.block_, that.fragment_);
60 HeapLocationPair makeHeapLocationPair(int block1, int fragment1, int block2, int fragment2)
62 return simgrid::mc::HeapLocationPair{{
63 simgrid::mc::HeapLocation(block1, fragment1),
64 simgrid::mc::HeapLocation(block2, fragment2)
68 class HeapArea : public HeapLocation {
72 explicit HeapArea(int block) : valid_(true) { block_ = block; }
73 HeapArea(int block, int fragment) : valid_(true)
80 class ProcessComparisonState {
82 std::vector<simgrid::mc::IgnoredHeapRegion>* to_ignore = nullptr;
83 std::vector<HeapArea> equals_to;
84 std::vector<simgrid::mc::Type*> types;
85 std::size_t heapsize = 0;
87 void initHeapInformation(xbt_mheap_t heap, std::vector<simgrid::mc::IgnoredHeapRegion>* i);
92 /** A hash which works with more stuff
94 * It can hash pairs: the standard hash currently doesn't include this.
96 template <class X> class hash : public std::hash<X> {
99 template <class X, class Y> class hash<std::pair<X, Y>> {
101 std::size_t operator()(std::pair<X,Y>const& x) const
105 return h1(x.first) ^ h2(x.second);
111 class StateComparator {
113 s_xbt_mheap_t std_heap_copy;
114 std::size_t heaplimit;
115 std::array<ProcessComparisonState, 2> processStates;
117 std::unordered_set<std::pair<void*, void*>, hash<std::pair<void*, void*>>> compared_pointers;
121 compared_pointers.clear();
124 int initHeapInformation(
125 xbt_mheap_t heap1, xbt_mheap_t heap2,
126 std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
127 std::vector<simgrid::mc::IgnoredHeapRegion>* i2);
129 HeapArea& equals_to1_(std::size_t i, std::size_t j)
131 return processStates[0].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
133 HeapArea& equals_to2_(std::size_t i, std::size_t j)
135 return processStates[1].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
137 Type*& types1_(std::size_t i, std::size_t j)
139 return processStates[0].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
141 Type*& types2_(std::size_t i, std::size_t j)
143 return processStates[1].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
146 HeapArea const& equals_to1_(std::size_t i, std::size_t j) const
148 return processStates[0].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
150 HeapArea const& equals_to2_(std::size_t i, std::size_t j) const
152 return processStates[1].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
154 Type* const& types1_(std::size_t i, std::size_t j) const
156 return processStates[0].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
158 Type* const& types2_(std::size_t i, std::size_t j) const
160 return processStates[1].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
163 /** Check whether two blocks are known to be matching
165 * @param b1 Block of state 1
166 * @param b2 Block of state 2
167 * @return if the blocks are known to be matching
169 bool blocksEqual(int b1, int b2) const
171 return this->equals_to1_(b1, 0).block_ == b2 && this->equals_to2_(b2, 0).block_ == b1;
174 /** Check whether two fragments are known to be matching
176 * @param b1 Block of state 1
177 * @param f1 Fragment of state 1
178 * @param b2 Block of state 2
179 * @param f2 Fragment of state 2
180 * @return if the fragments are known to be matching
182 int fragmentsEqual(int b1, int f1, int b2, int f2) const
184 return this->equals_to1_(b1, f1).block_ == b2 && this->equals_to1_(b1, f1).fragment_ == f2 &&
185 this->equals_to2_(b2, f2).block_ == b1 && this->equals_to2_(b2, f2).fragment_ == f1;
188 void match_equals(HeapLocationPairs* list);
194 /************************************************************************************/
196 static ssize_t heap_comparison_ignore_size(
197 std::vector<simgrid::mc::IgnoredHeapRegion>* ignore_list,
201 int end = ignore_list->size() - 1;
203 while (start <= end) {
204 unsigned int cursor = (start + end) / 2;
205 simgrid::mc::IgnoredHeapRegion const& region = (*ignore_list)[cursor];
206 if (region.address == address)
208 if (region.address < address)
210 if (region.address > address)
217 static bool is_stack(const void *address)
219 for (auto const& stack : mc_model_checker->process().stack_areas())
220 if (address == stack.address)
225 // TODO, this should depend on the snapshot?
226 static bool is_block_stack(int block)
228 for (auto const& stack : mc_model_checker->process().stack_areas())
229 if (block == stack.block)
237 void StateComparator::match_equals(HeapLocationPairs* list)
239 for (auto const& pair : *list) {
240 if (pair[0].fragment_ != -1) {
241 this->equals_to1_(pair[0].block_, pair[0].fragment_) = simgrid::mc::HeapArea(pair[1].block_, pair[1].fragment_);
242 this->equals_to2_(pair[1].block_, pair[1].fragment_) = simgrid::mc::HeapArea(pair[0].block_, pair[0].fragment_);
244 this->equals_to1_(pair[0].block_, 0) = simgrid::mc::HeapArea(pair[1].block_, pair[1].fragment_);
245 this->equals_to2_(pair[1].block_, 0) = simgrid::mc::HeapArea(pair[0].block_, pair[0].fragment_);
250 void ProcessComparisonState::initHeapInformation(xbt_mheap_t heap,
251 std::vector<simgrid::mc::IgnoredHeapRegion>* i)
253 auto heaplimit = heap->heaplimit;
254 this->heapsize = heap->heapsize;
256 this->equals_to.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, HeapArea());
257 this->types.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, nullptr);
260 int StateComparator::initHeapInformation(xbt_mheap_t heap1, xbt_mheap_t heap2,
261 std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
262 std::vector<simgrid::mc::IgnoredHeapRegion>* i2)
264 if ((heap1->heaplimit != heap2->heaplimit) || (heap1->heapsize != heap2->heapsize))
266 this->heaplimit = heap1->heaplimit;
267 this->std_heap_copy = *mc_model_checker->process().get_heap();
268 this->processStates[0].initHeapInformation(heap1, i1);
269 this->processStates[1].initHeapInformation(heap2, i2);
273 // TODO, have a robust way to find it in O(1)
274 static inline RegionSnapshot* MC_get_heap_region(Snapshot* snapshot)
276 for (auto const& region : snapshot->snapshot_regions_)
277 if (region->region_type() == simgrid::mc::RegionType::Heap)
279 xbt_die("No heap region");
283 int mmalloc_compare_heap(
284 simgrid::mc::StateComparator& state, simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2)
286 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
288 /* Start comparison */
293 /* Check busy blocks */
296 malloc_info heapinfo_temp1;
297 malloc_info heapinfo_temp2;
298 malloc_info heapinfo_temp2b;
300 simgrid::mc::RegionSnapshot* heap_region1 = MC_get_heap_region(snapshot1);
301 simgrid::mc::RegionSnapshot* heap_region2 = MC_get_heap_region(snapshot2);
303 // This is the address of std_heap->heapinfo in the application process:
304 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
306 // This is in snapshot do not use them directly:
307 const malloc_info* heapinfos1 = snapshot1->read<malloc_info*>(
308 RemotePtr<malloc_info*>((std::uint64_t)heapinfo_address), simgrid::mc::ProcessIndexMissing);
309 const malloc_info* heapinfos2 = snapshot2->read<malloc_info*>(
310 RemotePtr<malloc_info*>((std::uint64_t)heapinfo_address), simgrid::mc::ProcessIndexMissing);
312 while (i1 < state.heaplimit) {
314 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(heap_region1, &heapinfo_temp1, &heapinfos1[i1], sizeof(malloc_info));
315 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2, &heapinfos2[i1], sizeof(malloc_info));
317 if (heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type == MMALLOC_TYPE_HEAPINFO) { /* Free block */
322 if (heapinfo1->type < 0) {
323 fprintf(stderr, "Unkown mmalloc block type.\n");
327 void* addr_block1 = ((void*)(((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase));
329 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED) { /* Large block */
331 if (is_stack(addr_block1)) {
332 for (size_t k = 0; k < heapinfo1->busy_block.size; k++)
333 state.equals_to1_(i1 + k, 0) = HeapArea(i1, -1);
334 for (size_t k = 0; k < heapinfo2->busy_block.size; k++)
335 state.equals_to2_(i1 + k, 0) = HeapArea(i1, -1);
336 i1 += heapinfo1->busy_block.size;
340 if (state.equals_to1_(i1, 0).valid_) {
348 /* Try first to associate to same block in the other heap */
349 if (heapinfo2->type == heapinfo1->type && state.equals_to2_(i1, 0).valid_ == 0) {
350 void* addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
351 int res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing, addr_block1, addr_block2,
352 snapshot1, snapshot2, nullptr, nullptr, 0);
353 if (res_compare != 1) {
354 for (size_t k = 1; k < heapinfo2->busy_block.size; k++)
355 state.equals_to2_(i1 + 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(i1, -1);
359 i1 += heapinfo1->busy_block.size;
363 while (i2 < state.heaplimit && not equal) {
365 void* addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
372 const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2b, &heapinfos2[i2], sizeof(malloc_info));
374 if (heapinfo2b->type != MMALLOC_TYPE_UNFRAGMENTED) {
379 if (state.equals_to2_(i2, 0).valid_) {
384 int res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing, addr_block1, addr_block2,
385 snapshot1, snapshot2, nullptr, nullptr, 0);
387 if (res_compare != 1) {
388 for (size_t k = 1; k < heapinfo2b->busy_block.size; k++)
389 state.equals_to2_(i2 + k, 0) = HeapArea(i1, -1);
390 for (size_t k = 1; k < heapinfo1->busy_block.size; k++)
391 state.equals_to1_(i1 + k, 0) = HeapArea(i2, -1);
393 i1 += heapinfo1->busy_block.size;
400 XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1, heapinfo1->busy_block.busy_size, addr_block1);
401 i1 = state.heaplimit + 1;
405 } else { /* Fragmented block */
407 for (size_t j1 = 0; j1 < (size_t)(BLOCKSIZE >> heapinfo1->type); j1++) {
409 if (heapinfo1->busy_frag.frag_size[j1] == -1) /* Free fragment_ */
412 if (state.equals_to1_(i1, j1).valid_)
415 void* addr_frag1 = (void*)((char*)addr_block1 + (j1 << heapinfo1->type));
420 /* Try first to associate to same fragment_ in the other heap */
421 if (heapinfo2->type == heapinfo1->type && not state.equals_to2_(i1, j1).valid_) {
422 void* addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
423 void* addr_frag2 = (void*)((char*)addr_block2 + (j1 << heapinfo2->type));
424 int res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing, addr_frag1, addr_frag2,
425 snapshot1, snapshot2, nullptr, nullptr, 0);
426 if (res_compare != 1)
430 while (i2 < state.heaplimit && not equal) {
432 const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(
433 heap_region2, &heapinfo_temp2b, &heapinfos2[i2],
434 sizeof(malloc_info));
436 if (heapinfo2b->type == MMALLOC_TYPE_FREE || heapinfo2b->type == MMALLOC_TYPE_HEAPINFO) {
441 // We currently do not match fragments with unfragmented blocks (maybe we should).
442 if (heapinfo2b->type == MMALLOC_TYPE_UNFRAGMENTED) {
447 if (heapinfo2b->type < 0) {
448 fprintf(stderr, "Unknown mmalloc block type.\n");
452 for (size_t j2 = 0; j2 < (size_t)(BLOCKSIZE >> heapinfo2b->type); j2++) {
454 if (i2 == i1 && j2 == j1)
457 if (state.equals_to2_(i2, j2).valid_)
460 void* addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
461 void* addr_frag2 = (void*)((char*)addr_block2 + (j2 << heapinfo2b->type));
463 int res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing, addr_frag1, addr_frag2,
464 snapshot2, snapshot2, nullptr, nullptr, 0);
465 if (res_compare != 1) {
475 XBT_DEBUG("Block %zu, fragment_ %zu not found (size_used = %zd, address = %p)\n", i1, j1,
476 heapinfo1->busy_frag.frag_size[j1], addr_frag1);
477 i1 = state.heaplimit + 1;
487 /* All blocks/fragments are equal to another block/fragment_ ? */
488 for (size_t i = 1; i < state.heaplimit; i++) {
489 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
490 heap_region1, &heapinfo_temp1, &heapinfos1[i], sizeof(malloc_info));
492 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED && i1 == state.heaplimit && heapinfo1->busy_block.busy_size > 0 &&
493 not state.equals_to1_(i, 0).valid_) {
494 XBT_DEBUG("Block %zu not found (size used = %zu)", i, heapinfo1->busy_block.busy_size);
498 if (heapinfo1->type <= 0)
500 for (size_t j = 0; j < (size_t)(BLOCKSIZE >> heapinfo1->type); j++)
501 if (i1 == state.heaplimit && heapinfo1->busy_frag.frag_size[j] > 0 && not state.equals_to1_(i, j).valid_) {
502 XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)", i, j, heapinfo1->busy_frag.frag_size[j]);
507 if (i1 == state.heaplimit)
508 XBT_DEBUG("Number of blocks/fragments not found in heap1: %d", nb_diff1);
510 for (size_t i = 1; i < state.heaplimit; i++) {
511 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
512 heap_region2, &heapinfo_temp2, &heapinfos2[i], sizeof(malloc_info));
513 if (heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED && i1 == state.heaplimit && heapinfo2->busy_block.busy_size > 0 &&
514 not state.equals_to2_(i, 0).valid_) {
515 XBT_DEBUG("Block %zu not found (size used = %zu)", i,
516 heapinfo2->busy_block.busy_size);
520 if (heapinfo2->type <= 0)
523 for (size_t j = 0; j < (size_t)(BLOCKSIZE >> heapinfo2->type); j++)
524 if (i1 == state.heaplimit && heapinfo2->busy_frag.frag_size[j] > 0 && not state.equals_to2_(i, j).valid_) {
525 XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)",
526 i, j, heapinfo2->busy_frag.frag_size[j]);
532 if (i1 == state.heaplimit)
533 XBT_DEBUG("Number of blocks/fragments not found in heap2: %d", nb_diff2);
535 return nb_diff1 > 0 || nb_diff2 > 0;
541 * @param real_area1 Process address for state 1
542 * @param real_area2 Process address for state 2
543 * @param snapshot1 Snapshot of state 1
544 * @param snapshot2 Snapshot of state 2
547 * @param check_ignore
549 static int compare_heap_area_without_type(
550 simgrid::mc::StateComparator& state, int process_index,
551 const void *real_area1, const void *real_area2,
552 simgrid::mc::Snapshot* snapshot1,
553 simgrid::mc::Snapshot* snapshot2,
554 HeapLocationPairs* previous, int size,
557 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
558 simgrid::mc::RegionSnapshot* heap_region1 = MC_get_heap_region(snapshot1);
559 simgrid::mc::RegionSnapshot* heap_region2 = MC_get_heap_region(snapshot2);
561 for (int i = 0; i < size; ) {
563 if (check_ignore > 0) {
564 ssize_t ignore1 = heap_comparison_ignore_size(
565 state.processStates[0].to_ignore, (char *) real_area1 + i);
567 ssize_t ignore2 = heap_comparison_ignore_size(
568 state.processStates[1].to_ignore, (char *) real_area2 + i);
569 if (ignore2 == ignore1) {
582 if (MC_snapshot_region_memcmp(((char *) real_area1) + i, heap_region1, ((char *) real_area2) + i, heap_region2, 1) != 0) {
584 int pointer_align = (i / sizeof(void *)) * sizeof(void *);
585 const void* addr_pointed1 = snapshot1->read(
586 remote((void**)((char *) real_area1 + pointer_align)), process_index);
587 const void* addr_pointed2 = snapshot2->read(
588 remote((void**)((char *) real_area2 + pointer_align)), process_index);
590 if (process->in_maestro_stack(remote(addr_pointed1))
591 && process->in_maestro_stack(remote(addr_pointed2))) {
592 i = pointer_align + sizeof(void *);
596 if (addr_pointed1 > state.std_heap_copy.heapbase
597 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
598 && addr_pointed2 > state.std_heap_copy.heapbase
599 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)) {
600 // Both addreses are in the heap:
601 int res_compare = compare_heap_area(state ,process_index,
602 addr_pointed1, addr_pointed2,
603 snapshot1, snapshot2, previous, nullptr, 0);
604 if (res_compare == 1)
606 i = pointer_align + sizeof(void *);
622 * @param real_area1 Process address for state 1
623 * @param real_area2 Process address for state 2
624 * @param snapshot1 Snapshot of state 1
625 * @param snapshot2 Snapshot of state 2
628 * @param area_size either a byte_size or an elements_count (?)
629 * @param check_ignore
630 * @param pointer_level
631 * @return 0 (same), 1 (different), -1 (unknown)
633 static int compare_heap_area_with_type(
634 simgrid::mc::StateComparator& state, int process_index,
635 const void *real_area1, const void *real_area2,
636 simgrid::mc::Snapshot* snapshot1,
637 simgrid::mc::Snapshot* snapshot2,
638 HeapLocationPairs* previous, simgrid::mc::Type* type,
639 int area_size, int check_ignore,
644 // HACK: This should not happen but in pratice, there are some
645 // DW_TAG_typedef without an associated DW_AT_type:
646 //<1><538832>: Abbrev Number: 111 (DW_TAG_typedef)
647 // <538833> DW_AT_name : (indirect string, offset: 0x2292f3): gregset_t
648 // <538837> DW_AT_decl_file : 98
649 // <538838> DW_AT_decl_line : 37
653 if (is_stack(real_area1) && is_stack(real_area2))
656 if (check_ignore > 0) {
657 ssize_t ignore1 = heap_comparison_ignore_size(state.processStates[0].to_ignore, real_area1);
658 if (ignore1 > 0 && heap_comparison_ignore_size(state.processStates[1].to_ignore, real_area2) == ignore1)
662 simgrid::mc::Type* subtype;
663 simgrid::mc::Type* subsubtype;
665 const void* addr_pointed1;
666 const void* addr_pointed2;
668 simgrid::mc::RegionSnapshot* heap_region1 = MC_get_heap_region(snapshot1);
669 simgrid::mc::RegionSnapshot* heap_region2 = MC_get_heap_region(snapshot2);
671 switch (type->type) {
672 case DW_TAG_unspecified_type:
675 case DW_TAG_base_type:
676 if (not type->name.empty() && type->name == "char") { /* String, hence random (arbitrary ?) size */
677 if (real_area1 == real_area2)
680 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, area_size) != 0;
682 if (area_size != -1 && type->byte_size != area_size)
685 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
688 case DW_TAG_enumeration_type:
689 if (area_size != -1 && type->byte_size != area_size)
691 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
694 case DW_TAG_const_type:
695 case DW_TAG_volatile_type:
697 type = type->subtype;
700 case DW_TAG_array_type:
701 subtype = type->subtype;
702 switch (subtype->type) {
703 case DW_TAG_unspecified_type:
706 case DW_TAG_base_type:
707 case DW_TAG_enumeration_type:
708 case DW_TAG_pointer_type:
709 case DW_TAG_reference_type:
710 case DW_TAG_rvalue_reference_type:
711 case DW_TAG_structure_type:
712 case DW_TAG_class_type:
713 case DW_TAG_union_type:
714 if (subtype->full_type)
715 subtype = subtype->full_type;
716 elm_size = subtype->byte_size;
718 // TODO, just remove the type indirection?
719 case DW_TAG_const_type:
721 case DW_TAG_volatile_type:
722 subsubtype = subtype->subtype;
723 if (subsubtype->full_type)
724 subsubtype = subsubtype->full_type;
725 elm_size = subsubtype->byte_size;
730 for (int i = 0; i < type->element_count; i++) {
731 // TODO, add support for variable stride (DW_AT_byte_stride)
732 int res = compare_heap_area_with_type(state, process_index, (char*)real_area1 + (i * elm_size),
733 (char*)real_area2 + (i * elm_size), snapshot1, snapshot2, previous,
734 type->subtype, subtype->byte_size, check_ignore, pointer_level);
740 case DW_TAG_reference_type:
741 case DW_TAG_rvalue_reference_type:
742 case DW_TAG_pointer_type:
743 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
744 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
745 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
746 return (addr_pointed1 != addr_pointed2);
749 if (pointer_level <= 1) {
750 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
751 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
752 if (addr_pointed1 > state.std_heap_copy.heapbase && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1) &&
753 addr_pointed2 > state.std_heap_copy.heapbase && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
754 return compare_heap_area(state, process_index, addr_pointed1, addr_pointed2, snapshot1, snapshot2, previous,
755 type->subtype, pointer_level);
757 return (addr_pointed1 != addr_pointed2);
759 for (size_t i = 0; i < (area_size / sizeof(void*)); i++) {
760 addr_pointed1 = snapshot1->read(remote((void**)((char*)real_area1 + i * sizeof(void*))), process_index);
761 addr_pointed2 = snapshot2->read(remote((void**)((char*)real_area2 + i * sizeof(void*))), process_index);
763 if (addr_pointed1 > state.std_heap_copy.heapbase && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1) &&
764 addr_pointed2 > state.std_heap_copy.heapbase && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
765 res = compare_heap_area(state, process_index, addr_pointed1, addr_pointed2, snapshot1, snapshot2, previous,
766 type->subtype, pointer_level);
768 res = (addr_pointed1 != addr_pointed2);
774 case DW_TAG_structure_type:
775 case DW_TAG_class_type:
777 type = type->full_type;
778 if (area_size != -1 && type->byte_size != area_size) {
779 if (area_size <= type->byte_size || area_size % type->byte_size != 0)
781 for (size_t i = 0; i < (size_t)(area_size / type->byte_size); i++) {
782 int res = compare_heap_area_with_type(state, process_index, (char*)real_area1 + i * type->byte_size,
783 (char*)real_area2 + i * type->byte_size, snapshot1, snapshot2,
784 previous, type, -1, check_ignore, 0);
789 for (simgrid::mc::Member& member : type->members) {
790 // TODO, optimize this? (for the offset case)
791 void* real_member1 = simgrid::dwarf::resolve_member(real_area1, type, &member,
792 (simgrid::mc::AddressSpace*)snapshot1, process_index);
793 void* real_member2 = simgrid::dwarf::resolve_member(real_area2, type, &member,
794 (simgrid::mc::AddressSpace*)snapshot2, process_index);
795 int res = compare_heap_area_with_type(state, process_index, real_member1, real_member2, snapshot1,
796 snapshot2, previous, member.type, -1, check_ignore, 0);
803 case DW_TAG_union_type:
804 return compare_heap_area_without_type(state, process_index, real_area1, real_area2, snapshot1, snapshot2,
805 previous, type->byte_size, check_ignore);
811 xbt_die("Unreachable");
815 /** Infer the type of a part of the block from the type of the block
817 * TODO, handle DW_TAG_array_type as well as arrays of the object ((*p)[5], p[5])
819 * TODO, handle subfields ((*p).bar.foo, (*p)[5].bar…)
821 * @param type DWARF type ID of the root address
823 * @return DWARF type ID for given offset
825 static simgrid::mc::Type* get_offset_type(void *real_base_address, simgrid::mc::Type* type,
826 int offset, int area_size,
827 simgrid::mc::Snapshot* snapshot, int process_index)
830 // Beginning of the block, the infered variable type if the type of the block:
834 switch (type->type) {
836 case DW_TAG_structure_type:
837 case DW_TAG_class_type:
839 type = type->full_type;
840 if (area_size != -1 && type->byte_size != area_size) {
841 if (area_size > type->byte_size && area_size % type->byte_size == 0)
847 for (simgrid::mc::Member& member : type->members) {
848 if (member.has_offset_location()) {
849 // We have the offset, use it directly (shortcut):
850 if (member.offset() == offset)
853 void* real_member = simgrid::dwarf::resolve_member(real_base_address, type, &member, snapshot, process_index);
854 if ((char*)real_member - (char*)real_base_address == offset)
861 /* FIXME: other cases ? */
869 * @param area1 Process address for state 1
870 * @param area2 Process address for state 2
871 * @param snapshot1 Snapshot of state 1
872 * @param snapshot2 Snapshot of state 2
873 * @param previous Pairs of blocks already compared on the current path (or nullptr)
874 * @param type_id Type of variable
875 * @param pointer_level
876 * @return 0 (same), 1 (different), -1
879 int compare_heap_area(simgrid::mc::StateComparator& state, int process_index,
880 const void *area1, const void *area2,
881 simgrid::mc::Snapshot* snapshot1,
882 simgrid::mc::Snapshot* snapshot2,
883 HeapLocationPairs* previous,
884 simgrid::mc::Type* type, int pointer_level)
886 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
891 int check_ignore = 0;
899 simgrid::mc::Type* new_type1 = nullptr;
900 simgrid::mc::Type* new_type2 = nullptr;
902 bool match_pairs = false;
904 // This is the address of std_heap->heapinfo in the application process:
905 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
907 const malloc_info* heapinfos1 = snapshot1->read(remote((const malloc_info**)heapinfo_address), process_index);
908 const malloc_info* heapinfos2 = snapshot2->read(remote((const malloc_info**)heapinfo_address), process_index);
910 malloc_info heapinfo_temp1;
911 malloc_info heapinfo_temp2;
913 simgrid::mc::HeapLocationPairs current;
914 if (previous == nullptr) {
920 block1 = ((char*)area1 - (char*)state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
921 block2 = ((char*)area2 - (char*)state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
923 // If either block is a stack block:
924 if (is_block_stack((int) block1) && is_block_stack((int) block2)) {
925 previous->insert(simgrid::mc::makeHeapLocationPair(block1, -1, block2, -1));
927 state.match_equals(previous);
931 // If either block is not in the expected area of memory:
932 if (((char*)area1 < (char*)state.std_heap_copy.heapbase) || (block1 > (ssize_t)state.processStates[0].heapsize) ||
933 (block1 < 1) || ((char*)area2 < (char*)state.std_heap_copy.heapbase) ||
934 (block2 > (ssize_t)state.processStates[1].heapsize) || (block2 < 1)) {
938 // Process address of the block:
939 void* real_addr_block1 = (ADDR2UINT(block1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
940 void* real_addr_block2 = (ADDR2UINT(block2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
944 type = type->full_type;
946 // This assume that for "boring" types (volatile ...) byte_size is absent:
947 while (type->byte_size == 0 && type->subtype != nullptr)
948 type = type->subtype;
951 if (type->type == DW_TAG_pointer_type ||
952 (type->type == DW_TAG_base_type && not type->name.empty() && type->name == "char"))
955 type_size = type->byte_size;
959 simgrid::mc::RegionSnapshot* heap_region1 = MC_get_heap_region(snapshot1);
960 simgrid::mc::RegionSnapshot* heap_region2 = MC_get_heap_region(snapshot2);
962 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
963 heap_region1, &heapinfo_temp1, &heapinfos1[block1], sizeof(malloc_info));
964 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
965 heap_region2, &heapinfo_temp2, &heapinfos2[block2], sizeof(malloc_info));
967 if ((heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type==MMALLOC_TYPE_HEAPINFO)
968 && (heapinfo2->type == MMALLOC_TYPE_FREE || heapinfo2->type ==MMALLOC_TYPE_HEAPINFO)) {
971 state.match_equals(previous);
975 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED && heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
978 // TODO, lookup variable type from block type as done for fragmented blocks
980 if (state.equals_to1_(block1, 0).valid_ && state.equals_to2_(block2, 0).valid_ &&
981 state.blocksEqual(block1, block2)) {
983 state.match_equals(previous);
987 if (type_size != -1 && type_size != (ssize_t)heapinfo1->busy_block.busy_size &&
988 type_size != (ssize_t)heapinfo2->busy_block.busy_size &&
989 (type->name.empty() || type->name == "struct s_smx_context")) {
991 state.match_equals(previous);
995 if (heapinfo1->busy_block.size != heapinfo2->busy_block.size)
997 if (heapinfo1->busy_block.busy_size != heapinfo2->busy_block.busy_size)
1000 if (not previous->insert(simgrid::mc::makeHeapLocationPair(block1, -1, block2, -1)).second) {
1002 state.match_equals(previous);
1006 size = heapinfo1->busy_block.busy_size;
1008 // Remember (basic) type inference.
1009 // The current data structure only allows us to do this for the whole block.
1010 if (type != nullptr && area1 == real_addr_block1)
1011 state.types1_(block1, 0) = type;
1012 if (type != nullptr && area2 == real_addr_block2)
1013 state.types2_(block2, 0) = type;
1017 state.match_equals(previous);
1021 if (heapinfo1->busy_block.ignore > 0
1022 && heapinfo2->busy_block.ignore == heapinfo1->busy_block.ignore)
1023 check_ignore = heapinfo1->busy_block.ignore;
1025 } else if ((heapinfo1->type > 0) && (heapinfo2->type > 0)) { /* Fragmented block */
1028 ssize_t frag1 = ((uintptr_t)(ADDR2UINT(area1) % (BLOCKSIZE))) >> heapinfo1->type;
1029 ssize_t frag2 = ((uintptr_t)(ADDR2UINT(area2) % (BLOCKSIZE))) >> heapinfo2->type;
1031 // Process address of the fragment_:
1032 void* real_addr_frag1 = (void*)((char*)real_addr_block1 + (frag1 << heapinfo1->type));
1033 void* real_addr_frag2 = (void*)((char*)real_addr_block2 + (frag2 << heapinfo2->type));
1035 // Check the size of the fragments against the size of the type:
1036 if (type_size != -1) {
1037 if (heapinfo1->busy_frag.frag_size[frag1] == -1 || heapinfo2->busy_frag.frag_size[frag2] == -1) {
1039 state.match_equals(previous);
1043 if (type_size != heapinfo1->busy_frag.frag_size[frag1]
1044 || type_size != heapinfo2->busy_frag.frag_size[frag2]) {
1046 state.match_equals(previous);
1051 // Check if the blocks are already matched together:
1052 if (state.equals_to1_(block1, frag1).valid_ && state.equals_to2_(block2, frag2).valid_ && offset1 == offset2 &&
1053 state.fragmentsEqual(block1, frag1, block2, frag2)) {
1055 state.match_equals(previous);
1058 // Compare the size of both fragments:
1059 if (heapinfo1->busy_frag.frag_size[frag1] != heapinfo2->busy_frag.frag_size[frag2]) {
1060 if (type_size == -1) {
1062 state.match_equals(previous);
1068 // Size of the fragment_:
1069 size = heapinfo1->busy_frag.frag_size[frag1];
1071 // Remember (basic) type inference.
1072 // The current data structure only allows us to do this for the whole fragment_.
1073 if (type != nullptr && area1 == real_addr_frag1)
1074 state.types1_(block1, frag1) = type;
1075 if (type != nullptr && area2 == real_addr_frag2)
1076 state.types2_(block2, frag2) = type;
1078 // The type of the variable is already known:
1080 new_type1 = new_type2 = type;
1082 // Type inference from the block type.
1083 else if (state.types1_(block1, frag1) != nullptr || state.types2_(block2, frag2) != nullptr) {
1085 offset1 = (char*)area1 - (char*)real_addr_frag1;
1086 offset2 = (char*)area2 - (char*)real_addr_frag2;
1088 if (state.types1_(block1, frag1) != nullptr && state.types2_(block2, frag2) != nullptr) {
1090 get_offset_type(real_addr_frag1, state.types1_(block1, frag1), offset1, size, snapshot1, process_index);
1092 get_offset_type(real_addr_frag2, state.types2_(block2, frag2), offset1, size, snapshot2, process_index);
1093 } else if (state.types1_(block1, frag1) != nullptr) {
1095 get_offset_type(real_addr_frag1, state.types1_(block1, frag1), offset1, size, snapshot1, process_index);
1097 get_offset_type(real_addr_frag2, state.types1_(block1, frag1), offset2, size, snapshot2, process_index);
1098 } else if (state.types2_(block2, frag2) != nullptr) {
1100 get_offset_type(real_addr_frag1, state.types2_(block2, frag2), offset1, size, snapshot1, process_index);
1102 get_offset_type(real_addr_frag2, state.types2_(block2, frag2), offset2, size, snapshot2, process_index);
1105 state.match_equals(previous);
1109 if (new_type1 != nullptr && new_type2 != nullptr && new_type1 != new_type2) {
1112 while (type->byte_size == 0 && type->subtype != nullptr)
1113 type = type->subtype;
1114 new_size1 = type->byte_size;
1117 while (type->byte_size == 0 && type->subtype != nullptr)
1118 type = type->subtype;
1119 new_size2 = type->byte_size;
1123 state.match_equals(previous);
1128 if (new_size1 > 0 && new_size1 == new_size2) {
1133 if (offset1 == 0 && offset2 == 0 &&
1134 not previous->insert(simgrid::mc::makeHeapLocationPair(block1, frag1, block2, frag2)).second) {
1136 state.match_equals(previous);
1142 state.match_equals(previous);
1146 if ((heapinfo1->busy_frag.ignore[frag1] > 0) &&
1147 (heapinfo2->busy_frag.ignore[frag2] == heapinfo1->busy_frag.ignore[frag1]))
1148 check_ignore = heapinfo1->busy_frag.ignore[frag1];
1154 /* Start comparison */
1157 res_compare = compare_heap_area_with_type(state, process_index, area1, area2, snapshot1, snapshot2, previous, type,
1158 size, check_ignore, pointer_level);
1160 res_compare = compare_heap_area_without_type(state, process_index, area1, area2, snapshot1, snapshot2, previous,
1161 size, check_ignore);
1163 if (res_compare == 1)
1167 state.match_equals(previous);
1174 /************************** Snapshot comparison *******************************/
1175 /******************************************************************************/
1177 static int compare_areas_with_type(simgrid::mc::StateComparator& state, int process_index, void* real_area1,
1178 simgrid::mc::Snapshot* snapshot1, simgrid::mc::RegionSnapshot* region1,
1179 void* real_area2, simgrid::mc::Snapshot* snapshot2,
1180 simgrid::mc::RegionSnapshot* region2, simgrid::mc::Type* type, int pointer_level)
1182 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
1184 simgrid::mc::Type* subtype;
1185 simgrid::mc::Type* subsubtype;
1191 xbt_assert(type != nullptr);
1192 switch (type->type) {
1193 case DW_TAG_unspecified_type:
1196 case DW_TAG_base_type:
1197 case DW_TAG_enumeration_type:
1198 case DW_TAG_union_type:
1199 return MC_snapshot_region_memcmp(real_area1, region1, real_area2, region2, type->byte_size) != 0;
1200 case DW_TAG_typedef:
1201 case DW_TAG_volatile_type:
1202 case DW_TAG_const_type:
1204 type = type->subtype;
1205 continue; // restart
1206 case DW_TAG_array_type:
1207 subtype = type->subtype;
1208 switch (subtype->type) {
1209 case DW_TAG_unspecified_type:
1212 case DW_TAG_base_type:
1213 case DW_TAG_enumeration_type:
1214 case DW_TAG_pointer_type:
1215 case DW_TAG_reference_type:
1216 case DW_TAG_rvalue_reference_type:
1217 case DW_TAG_structure_type:
1218 case DW_TAG_class_type:
1219 case DW_TAG_union_type:
1220 if (subtype->full_type)
1221 subtype = subtype->full_type;
1222 elm_size = subtype->byte_size;
1224 case DW_TAG_const_type:
1225 case DW_TAG_typedef:
1226 case DW_TAG_volatile_type:
1227 subsubtype = subtype->subtype;
1228 if (subsubtype->full_type)
1229 subsubtype = subsubtype->full_type;
1230 elm_size = subsubtype->byte_size;
1235 for (i = 0; i < type->element_count; i++) {
1236 size_t off = i * elm_size;
1237 res = compare_areas_with_type(state, process_index, (char*)real_area1 + off, snapshot1, region1,
1238 (char*)real_area2 + off, snapshot2, region2, type->subtype, pointer_level);
1243 case DW_TAG_pointer_type:
1244 case DW_TAG_reference_type:
1245 case DW_TAG_rvalue_reference_type: {
1246 void* addr_pointed1 = MC_region_read_pointer(region1, real_area1);
1247 void* addr_pointed2 = MC_region_read_pointer(region2, real_area2);
1249 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type)
1250 return (addr_pointed1 != addr_pointed2);
1251 if (addr_pointed1 == nullptr && addr_pointed2 == nullptr)
1253 if (addr_pointed1 == nullptr || addr_pointed2 == nullptr)
1255 if (not state.compared_pointers.insert(std::make_pair(addr_pointed1, addr_pointed2)).second)
1260 // Some cases are not handled here:
1261 // * the pointers lead to different areas (one to the heap, the other to the RW segment ...)
1262 // * a pointer leads to the read-only segment of the current object
1263 // * a pointer lead to a different ELF object
1265 if (addr_pointed1 > process->heap_address && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)) {
1266 if (not(addr_pointed2 > process->heap_address && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)))
1268 // The pointers are both in the heap:
1269 return simgrid::mc::compare_heap_area(state, process_index, addr_pointed1, addr_pointed2, snapshot1,
1270 snapshot2, nullptr, type->subtype, pointer_level);
1272 } else if (region1->contain(simgrid::mc::remote(addr_pointed1))) {
1273 // The pointers are both in the current object R/W segment:
1274 if (not region2->contain(simgrid::mc::remote(addr_pointed2)))
1276 if (not type->type_id)
1277 return (addr_pointed1 != addr_pointed2);
1279 return compare_areas_with_type(state, process_index, addr_pointed1, snapshot1, region1, addr_pointed2,
1280 snapshot2, region2, type->subtype, pointer_level);
1283 // TODO, We do not handle very well the case where
1284 // it belongs to a different (non-heap) region from the current one.
1286 return (addr_pointed1 != addr_pointed2);
1289 case DW_TAG_structure_type:
1290 case DW_TAG_class_type:
1291 for (simgrid::mc::Member& member : type->members) {
1292 void* member1 = simgrid::dwarf::resolve_member(real_area1, type, &member, snapshot1, process_index);
1293 void* member2 = simgrid::dwarf::resolve_member(real_area2, type, &member, snapshot2, process_index);
1294 simgrid::mc::RegionSnapshot* subregion1 = snapshot1->get_region(member1, process_index, region1); // region1 is hinted
1295 simgrid::mc::RegionSnapshot* subregion2 = snapshot2->get_region(member2, process_index, region2); // region2 is hinted
1296 res = compare_areas_with_type(state, process_index, member1, snapshot1, subregion1, member2, snapshot2,
1297 subregion2, member.type, pointer_level);
1302 case DW_TAG_subroutine_type:
1305 XBT_VERB("Unknown case: %d", type->type);
1313 static int compare_global_variables(simgrid::mc::StateComparator& state, simgrid::mc::ObjectInformation* object_info,
1314 int process_index, simgrid::mc::RegionSnapshot* r1, simgrid::mc::RegionSnapshot* r2,
1315 simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2)
1317 xbt_assert(r1 && r2, "Missing region.");
1320 if (r1->storage_type() == simgrid::mc::StorageType::Privatized) {
1321 xbt_assert(process_index >= 0);
1322 if (r2->storage_type() != simgrid::mc::StorageType::Privatized)
1325 size_t process_count = MC_smpi_process_count();
1326 xbt_assert(process_count == r1->privatized_data().size()
1327 && process_count == r2->privatized_data().size());
1329 // Compare the global variables separately for each simulates process:
1330 for (size_t i = 0; i < process_count; i++) {
1331 if (compare_global_variables(state, object_info, i, &r1->privatized_data()[i], &r2->privatized_data()[i],
1332 snapshot1, snapshot2))
1338 xbt_assert(r1->storage_type() != simgrid::mc::StorageType::Privatized);
1340 xbt_assert(r2->storage_type() != simgrid::mc::StorageType::Privatized);
1342 std::vector<simgrid::mc::Variable>& variables = object_info->global_variables;
1344 for (simgrid::mc::Variable const& current_var : variables) {
1346 // If the variable is not in this object, skip it:
1347 // We do not expect to find a pointer to something which is not reachable
1348 // by the global variables.
1349 if ((char *) current_var.address < (char *) object_info->start_rw
1350 || (char *) current_var.address > (char *) object_info->end_rw)
1353 simgrid::mc::Type* bvariable_type = current_var.type;
1354 int res = compare_areas_with_type(state, process_index,
1355 (char *) current_var.address, snapshot1, r1,
1356 (char *) current_var.address, snapshot2, r2,
1359 XBT_VERB("Global variable %s (%p) is different between snapshots",
1360 current_var.name.c_str(),
1361 (char *) current_var.address);
1369 static int compare_local_variables(simgrid::mc::StateComparator& state,
1371 simgrid::mc::Snapshot* snapshot1,
1372 simgrid::mc::Snapshot* snapshot2,
1373 mc_snapshot_stack_t stack1,
1374 mc_snapshot_stack_t stack2)
1376 if (stack1->local_variables.size() != stack2->local_variables.size()) {
1377 XBT_VERB("Different number of local variables");
1381 unsigned int cursor = 0;
1382 local_variable_t current_var1;
1383 local_variable_t current_var2;
1384 while (cursor < stack1->local_variables.size()) {
1385 current_var1 = &stack1->local_variables[cursor];
1386 current_var2 = &stack1->local_variables[cursor];
1387 if (current_var1->name != current_var2->name
1388 || current_var1->subprogram != current_var2->subprogram
1389 || current_var1->ip != current_var2->ip) {
1390 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1392 ("Different name of variable (%s - %s) "
1393 "or frame (%s - %s) or ip (%lu - %lu)",
1394 current_var1->name.c_str(),
1395 current_var2->name.c_str(),
1396 current_var1->subprogram->name.c_str(),
1397 current_var2->subprogram->name.c_str(),
1398 current_var1->ip, current_var2->ip);
1401 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1403 simgrid::mc::Type* subtype = current_var1->type;
1405 compare_areas_with_type(state, process_index, current_var1->address, snapshot1,
1406 snapshot1->get_region(current_var1->address, process_index), current_var2->address,
1407 snapshot2, snapshot2->get_region(current_var2->address, process_index), subtype, 0);
1410 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1411 XBT_VERB("Local variable %s (%p - %p) in frame %s "
1412 "is different between snapshots",
1413 current_var1->name.c_str(), current_var1->address, current_var2->address,
1414 current_var1->subprogram->name.c_str());
1425 static std::unique_ptr<simgrid::mc::StateComparator> state_comparator;
1427 int snapshot_compare(Snapshot* s1, Snapshot* s2)
1429 // TODO, make this a field of ModelChecker or something similar
1430 if (state_comparator == nullptr)
1431 state_comparator.reset(new StateComparator());
1433 state_comparator->clear();
1435 RemoteClient* process = &mc_model_checker->process();
1439 int hash_result = 0;
1441 hash_result = (s1->hash_ != s2->hash_);
1443 XBT_VERB("(%d - %d) Different hash: 0x%" PRIx64 "--0x%" PRIx64, s1->num_state_, s2->num_state_, s1->hash_,
1449 XBT_VERB("(%d - %d) Same hash: 0x%" PRIx64, s1->num_state_, s2->num_state_, s1->hash_);
1452 /* Compare enabled processes */
1453 if (s1->enabled_processes_ != s2->enabled_processes_) {
1454 XBT_VERB("(%d - %d) Different amount of enabled processes", s1->num_state_, s2->num_state_);
1458 /* Compare size of stacks */
1460 for (unsigned long i = 0; i < s1->stacks_.size(); i++) {
1461 size_t size_used1 = s1->stack_sizes_[i];
1462 size_t size_used2 = s2->stack_sizes_[i];
1463 if (size_used1 != size_used2) {
1465 XBT_DEBUG("(%d - %d) Different size used in stacks: %zu - %zu", s1->num_state, s2->num_state, size_used1,
1471 XBT_VERB("(%d - %d) Different size used in stacks: %zu - %zu", s1->num_state_, s2->num_state_, size_used1,
1478 if (is_diff) // do not proceed if there is any stacks that don't match
1481 /* Init heap information used in heap comparison algorithm */
1482 xbt_mheap_t heap1 = (xbt_mheap_t)s1->read_bytes(
1483 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1484 remote(process->heap_address),
1485 simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1486 xbt_mheap_t heap2 = (xbt_mheap_t)s2->read_bytes(
1487 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1488 remote(process->heap_address),
1489 simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1490 int res_init = state_comparator->initHeapInformation(heap1, heap2, &s1->to_ignore_, &s2->to_ignore_);
1492 if (res_init == -1) {
1494 XBT_DEBUG("(%d - %d) Different heap information", num1, nus1->num_state, s2->num_statem2);
1498 XBT_VERB("(%d - %d) Different heap information", s1->num_state_, s2->num_state_);
1505 /* Stacks comparison */
1507 for (unsigned int cursor = 0; cursor < s1->stacks_.size(); cursor++) {
1508 mc_snapshot_stack_t stack1 = &s1->stacks_[cursor];
1509 mc_snapshot_stack_t stack2 = &s2->stacks_[cursor];
1511 if (stack1->process_index != stack2->process_index) {
1513 XBT_DEBUG("(%d - %d) Stacks with different process index (%i vs %i)", s1->num_state_, s2->num_state_,
1514 stack1->process_index, stack2->process_index);
1516 else diff_local = compare_local_variables(*state_comparator,
1517 stack1->process_index, s1, s2, stack1, stack2);
1518 if (diff_local > 0) {
1520 XBT_DEBUG("(%d - %d) Different local variables between stacks %d", num1,
1526 XBT_VERB("(%d - %d) Different local variables between stacks %u", s1->num_state_, s2->num_state_, cursor + 1);
1534 size_t regions_count = s1->snapshot_regions_.size();
1535 // TODO, raise a difference instead?
1536 xbt_assert(regions_count == s2->snapshot_regions_.size());
1538 for (size_t k = 0; k != regions_count; ++k) {
1539 RegionSnapshot* region1 = s1->snapshot_regions_[k].get();
1540 RegionSnapshot* region2 = s2->snapshot_regions_[k].get();
1543 if (region1->region_type() != RegionType::Data)
1546 xbt_assert(region1->region_type() == region2->region_type());
1547 xbt_assert(region1->object_info() == region2->object_info());
1548 xbt_assert(region1->object_info());
1550 /* Compare global variables */
1551 if (compare_global_variables(*state_comparator, region1->object_info(), simgrid::mc::ProcessIndexDisabled, region1,
1555 std::string const& name = region1->object_info()->file_name;
1556 XBT_DEBUG("(%d - %d) Different global variables in %s", s1->num_state, s2->num_state, name.c_str());
1560 std::string const& name = region1->object_info()->file_name;
1561 XBT_VERB("(%d - %d) Different global variables in %s", s1->num_state_, s2->num_state_, name.c_str());
1570 if (mmalloc_compare_heap(*state_comparator, s1, s2) > 0) {
1573 XBT_DEBUG("(%d - %d) Different heap (mmalloc_compare)", s1->num_state, s2->num_state);
1578 XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", s1->num_state_, s2->num_state_);
1585 if (errors || hash_result)
1586 XBT_VERB("(%d - %d) Difference found", s1->num_state_, s2->num_state_);
1588 XBT_VERB("(%d - %d) No difference found", s1->num_state_, s2->num_state_);
1591 #if defined(MC_DEBUG) && defined(MC_VERBOSE)
1593 // * false positive SHOULD be avoided.
1594 // * There MUST not be any false negative.
1596 XBT_VERB("(%d - %d) State equality hash test is %s %s", s1->num_state, s2->num_state,
1597 (hash_result != 0) == (errors != 0) ? "true" : "false", not hash_result ? "positive" : "negative");
1601 return errors > 0 || hash_result;