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/mc_snapshot.hpp"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_compare, xbt, "Logging specific to mc_compare in mc");
19 typedef std::array<HeapLocation, 2> HeapLocationPair;
20 typedef std::set<HeapLocationPair> HeapLocationPairs;
22 struct ProcessComparisonState;
23 struct StateComparator;
25 static int compare_heap_area(
26 StateComparator& state,
27 int process_index, const void *area1, const void* area2,
28 Snapshot* snapshot1, Snapshot* snapshot2,
29 HeapLocationPairs* previous, Type* type, int pointer_level);
34 using simgrid::mc::remote;
36 /*********************************** Heap comparison ***********************************/
37 /***************************************************************************************/
47 HeapLocation() = default;
48 HeapLocation(int block, int fragment = 0) : block_(block), fragment_(fragment) {}
50 bool operator==(HeapLocation const& that) const
52 return block_ == that.block_ && fragment_ == that.fragment_;
54 bool operator<(HeapLocation const& that) const
56 return std::make_pair(block_, fragment_) < std::make_pair(that.block_, that.fragment_);
61 HeapLocationPair makeHeapLocationPair(int block1, int fragment1, int block2, int fragment2)
63 return simgrid::mc::HeapLocationPair{{
64 simgrid::mc::HeapLocation(block1, fragment1),
65 simgrid::mc::HeapLocation(block2, fragment2)
69 class HeapArea : public HeapLocation {
73 explicit HeapArea(int block) : valid_(true) { block_ = block; }
74 HeapArea(int block, int fragment) : valid_(true)
81 class ProcessComparisonState {
83 std::vector<simgrid::mc::IgnoredHeapRegion>* to_ignore = nullptr;
84 std::vector<HeapArea> equals_to;
85 std::vector<simgrid::mc::Type*> types;
86 std::size_t heapsize = 0;
88 void initHeapInformation(xbt_mheap_t heap, std::vector<simgrid::mc::IgnoredHeapRegion>* i);
93 /** A hash which works with more stuff
95 * It can hash pairs: the standard hash currently doesn't include this.
97 template <class X> class hash : public std::hash<X> {
100 template <class X, class Y> class hash<std::pair<X, Y>> {
102 std::size_t operator()(std::pair<X,Y>const& x) const
106 return h1(x.first) ^ h2(x.second);
112 class StateComparator {
114 s_xbt_mheap_t std_heap_copy;
115 std::size_t heaplimit;
116 std::array<ProcessComparisonState, 2> processStates;
118 std::unordered_set<std::pair<void*, void*>, hash<std::pair<void*, void*>>> compared_pointers;
122 compared_pointers.clear();
125 int initHeapInformation(
126 xbt_mheap_t heap1, xbt_mheap_t heap2,
127 std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
128 std::vector<simgrid::mc::IgnoredHeapRegion>* i2);
130 HeapArea& equals_to1_(std::size_t i, std::size_t j)
132 return processStates[0].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
134 HeapArea& equals_to2_(std::size_t i, std::size_t j)
136 return processStates[1].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
138 Type*& types1_(std::size_t i, std::size_t j)
140 return processStates[0].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
142 Type*& types2_(std::size_t i, std::size_t j)
144 return processStates[1].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
147 HeapArea const& equals_to1_(std::size_t i, std::size_t j) const
149 return processStates[0].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
151 HeapArea const& equals_to2_(std::size_t i, std::size_t j) const
153 return processStates[1].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
155 Type* const& types1_(std::size_t i, std::size_t j) const
157 return processStates[0].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
159 Type* const& types2_(std::size_t i, std::size_t j) const
161 return processStates[1].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
164 /** Check whether two blocks are known to be matching
166 * @param b1 Block of state 1
167 * @param b2 Block of state 2
168 * @return if the blocks are known to be matching
170 bool blocksEqual(int b1, int b2) const
172 return this->equals_to1_(b1, 0).block_ == b2 && this->equals_to2_(b2, 0).block_ == b1;
175 /** Check whether two fragments are known to be matching
177 * @param b1 Block of state 1
178 * @param f1 Fragment of state 1
179 * @param b2 Block of state 2
180 * @param f2 Fragment of state 2
181 * @return if the fragments are known to be matching
183 int fragmentsEqual(int b1, int f1, int b2, int f2) const
185 return this->equals_to1_(b1, f1).block_ == b2 && this->equals_to1_(b1, f1).fragment_ == f2 &&
186 this->equals_to2_(b2, f2).block_ == b1 && this->equals_to2_(b2, f2).fragment_ == f1;
189 void match_equals(HeapLocationPairs* list);
195 /************************************************************************************/
197 static ssize_t heap_comparison_ignore_size(
198 std::vector<simgrid::mc::IgnoredHeapRegion>* ignore_list,
202 int end = ignore_list->size() - 1;
204 while (start <= end) {
205 unsigned int cursor = (start + end) / 2;
206 simgrid::mc::IgnoredHeapRegion const& region = (*ignore_list)[cursor];
207 if (region.address == address)
209 if (region.address < address)
211 if (region.address > address)
218 static bool is_stack(const void *address)
220 for (auto const& stack : mc_model_checker->process().stack_areas())
221 if (address == stack.address)
226 // TODO, this should depend on the snapshot?
227 static bool is_block_stack(int block)
229 for (auto const& stack : mc_model_checker->process().stack_areas())
230 if (block == stack.block)
238 void StateComparator::match_equals(HeapLocationPairs* list)
240 for (auto const& pair : *list) {
241 if (pair[0].fragment_ != -1) {
242 this->equals_to1_(pair[0].block_, pair[0].fragment_) = simgrid::mc::HeapArea(pair[1].block_, pair[1].fragment_);
243 this->equals_to2_(pair[1].block_, pair[1].fragment_) = simgrid::mc::HeapArea(pair[0].block_, pair[0].fragment_);
245 this->equals_to1_(pair[0].block_, 0) = simgrid::mc::HeapArea(pair[1].block_, pair[1].fragment_);
246 this->equals_to2_(pair[1].block_, 0) = simgrid::mc::HeapArea(pair[0].block_, pair[0].fragment_);
251 void ProcessComparisonState::initHeapInformation(xbt_mheap_t heap,
252 std::vector<simgrid::mc::IgnoredHeapRegion>* i)
254 auto heaplimit = heap->heaplimit;
255 this->heapsize = heap->heapsize;
257 this->equals_to.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, HeapArea());
258 this->types.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, nullptr);
261 int StateComparator::initHeapInformation(xbt_mheap_t heap1, xbt_mheap_t heap2,
262 std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
263 std::vector<simgrid::mc::IgnoredHeapRegion>* i2)
265 if ((heap1->heaplimit != heap2->heaplimit) || (heap1->heapsize != heap2->heapsize))
267 this->heaplimit = heap1->heaplimit;
268 this->std_heap_copy = *mc_model_checker->process().get_heap();
269 this->processStates[0].initHeapInformation(heap1, i1);
270 this->processStates[1].initHeapInformation(heap2, i2);
274 // TODO, have a robust way to find it in O(1)
275 static inline RegionSnapshot* MC_get_heap_region(Snapshot* snapshot)
277 for (auto const& region : snapshot->snapshot_regions_)
278 if (region->region_type() == simgrid::mc::RegionType::Heap)
280 xbt_die("No heap region");
284 int mmalloc_compare_heap(
285 simgrid::mc::StateComparator& state, simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2)
287 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
289 /* Start comparison */
294 /* Check busy blocks */
297 malloc_info heapinfo_temp1;
298 malloc_info heapinfo_temp2;
299 malloc_info heapinfo_temp2b;
301 simgrid::mc::RegionSnapshot* heap_region1 = MC_get_heap_region(snapshot1);
302 simgrid::mc::RegionSnapshot* heap_region2 = MC_get_heap_region(snapshot2);
304 // This is the address of std_heap->heapinfo in the application process:
305 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
307 // This is in snapshot do not use them directly:
308 const malloc_info* heapinfos1 = snapshot1->read<malloc_info*>(
309 RemotePtr<malloc_info*>((std::uint64_t)heapinfo_address), simgrid::mc::ProcessIndexMissing);
310 const malloc_info* heapinfos2 = snapshot2->read<malloc_info*>(
311 RemotePtr<malloc_info*>((std::uint64_t)heapinfo_address), simgrid::mc::ProcessIndexMissing);
313 while (i1 < state.heaplimit) {
315 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(heap_region1, &heapinfo_temp1, &heapinfos1[i1], sizeof(malloc_info));
316 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2, &heapinfos2[i1], sizeof(malloc_info));
318 if (heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type == MMALLOC_TYPE_HEAPINFO) { /* Free block */
323 if (heapinfo1->type < 0) {
324 fprintf(stderr, "Unkown mmalloc block type.\n");
328 void* addr_block1 = ((void*)(((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase));
330 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED) { /* Large block */
332 if (is_stack(addr_block1)) {
333 for (size_t k = 0; k < heapinfo1->busy_block.size; k++)
334 state.equals_to1_(i1 + k, 0) = HeapArea(i1, -1);
335 for (size_t k = 0; k < heapinfo2->busy_block.size; k++)
336 state.equals_to2_(i1 + k, 0) = HeapArea(i1, -1);
337 i1 += heapinfo1->busy_block.size;
341 if (state.equals_to1_(i1, 0).valid_) {
349 /* Try first to associate to same block in the other heap */
350 if (heapinfo2->type == heapinfo1->type && state.equals_to2_(i1, 0).valid_ == 0) {
351 void* addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
352 int res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing, addr_block1, addr_block2,
353 snapshot1, snapshot2, nullptr, nullptr, 0);
354 if (res_compare != 1) {
355 for (size_t k = 1; k < heapinfo2->busy_block.size; k++)
356 state.equals_to2_(i1 + k, 0) = HeapArea(i1, -1);
357 for (size_t k = 1; k < heapinfo1->busy_block.size; k++)
358 state.equals_to1_(i1 + k, 0) = HeapArea(i1, -1);
360 i1 += heapinfo1->busy_block.size;
364 while (i2 < state.heaplimit && not equal) {
366 void* addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
373 const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2b, &heapinfos2[i2], sizeof(malloc_info));
375 if (heapinfo2b->type != MMALLOC_TYPE_UNFRAGMENTED) {
380 if (state.equals_to2_(i2, 0).valid_) {
385 int res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing, addr_block1, addr_block2,
386 snapshot1, snapshot2, nullptr, nullptr, 0);
388 if (res_compare != 1) {
389 for (size_t k = 1; k < heapinfo2b->busy_block.size; k++)
390 state.equals_to2_(i2 + k, 0) = HeapArea(i1, -1);
391 for (size_t k = 1; k < heapinfo1->busy_block.size; k++)
392 state.equals_to1_(i1 + k, 0) = HeapArea(i2, -1);
394 i1 += heapinfo1->busy_block.size;
401 XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1, heapinfo1->busy_block.busy_size, addr_block1);
402 i1 = state.heaplimit + 1;
406 } else { /* Fragmented block */
408 for (size_t j1 = 0; j1 < (size_t)(BLOCKSIZE >> heapinfo1->type); j1++) {
410 if (heapinfo1->busy_frag.frag_size[j1] == -1) /* Free fragment_ */
413 if (state.equals_to1_(i1, j1).valid_)
416 void* addr_frag1 = (void*)((char*)addr_block1 + (j1 << heapinfo1->type));
421 /* Try first to associate to same fragment_ in the other heap */
422 if (heapinfo2->type == heapinfo1->type && not state.equals_to2_(i1, j1).valid_) {
423 void* addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
424 void* addr_frag2 = (void*)((char*)addr_block2 + (j1 << heapinfo2->type));
425 int res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing, addr_frag1, addr_frag2,
426 snapshot1, snapshot2, nullptr, nullptr, 0);
427 if (res_compare != 1)
431 while (i2 < state.heaplimit && not equal) {
433 const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(
434 heap_region2, &heapinfo_temp2b, &heapinfos2[i2],
435 sizeof(malloc_info));
437 if (heapinfo2b->type == MMALLOC_TYPE_FREE || heapinfo2b->type == MMALLOC_TYPE_HEAPINFO) {
442 // We currently do not match fragments with unfragmented blocks (maybe we should).
443 if (heapinfo2b->type == MMALLOC_TYPE_UNFRAGMENTED) {
448 if (heapinfo2b->type < 0) {
449 fprintf(stderr, "Unknown mmalloc block type.\n");
453 for (size_t j2 = 0; j2 < (size_t)(BLOCKSIZE >> heapinfo2b->type); j2++) {
455 if (i2 == i1 && j2 == j1)
458 if (state.equals_to2_(i2, j2).valid_)
461 void* addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
462 void* addr_frag2 = (void*)((char*)addr_block2 + (j2 << heapinfo2b->type));
464 int res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing, addr_frag1, addr_frag2,
465 snapshot2, snapshot2, nullptr, nullptr, 0);
466 if (res_compare != 1) {
476 XBT_DEBUG("Block %zu, fragment_ %zu not found (size_used = %zd, address = %p)\n", i1, j1,
477 heapinfo1->busy_frag.frag_size[j1], addr_frag1);
478 i1 = state.heaplimit + 1;
488 /* All blocks/fragments are equal to another block/fragment_ ? */
489 for (size_t i = 1; i < state.heaplimit; i++) {
490 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
491 heap_region1, &heapinfo_temp1, &heapinfos1[i], sizeof(malloc_info));
493 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED && i1 == state.heaplimit && heapinfo1->busy_block.busy_size > 0 &&
494 not state.equals_to1_(i, 0).valid_) {
495 XBT_DEBUG("Block %zu not found (size used = %zu)", i, heapinfo1->busy_block.busy_size);
499 if (heapinfo1->type <= 0)
501 for (size_t j = 0; j < (size_t)(BLOCKSIZE >> heapinfo1->type); j++)
502 if (i1 == state.heaplimit && heapinfo1->busy_frag.frag_size[j] > 0 && not state.equals_to1_(i, j).valid_) {
503 XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)", i, j, heapinfo1->busy_frag.frag_size[j]);
508 if (i1 == state.heaplimit)
509 XBT_DEBUG("Number of blocks/fragments not found in heap1: %d", nb_diff1);
511 for (size_t i = 1; i < state.heaplimit; i++) {
512 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
513 heap_region2, &heapinfo_temp2, &heapinfos2[i], sizeof(malloc_info));
514 if (heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED && i1 == state.heaplimit && heapinfo2->busy_block.busy_size > 0 &&
515 not state.equals_to2_(i, 0).valid_) {
516 XBT_DEBUG("Block %zu not found (size used = %zu)", i,
517 heapinfo2->busy_block.busy_size);
521 if (heapinfo2->type <= 0)
524 for (size_t j = 0; j < (size_t)(BLOCKSIZE >> heapinfo2->type); j++)
525 if (i1 == state.heaplimit && heapinfo2->busy_frag.frag_size[j] > 0 && not state.equals_to2_(i, j).valid_) {
526 XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)",
527 i, j, heapinfo2->busy_frag.frag_size[j]);
533 if (i1 == state.heaplimit)
534 XBT_DEBUG("Number of blocks/fragments not found in heap2: %d", nb_diff2);
536 return nb_diff1 > 0 || nb_diff2 > 0;
542 * @param real_area1 Process address for state 1
543 * @param real_area2 Process address for state 2
544 * @param snapshot1 Snapshot of state 1
545 * @param snapshot2 Snapshot of state 2
548 * @param check_ignore
550 static int compare_heap_area_without_type(
551 simgrid::mc::StateComparator& state, int process_index,
552 const void *real_area1, const void *real_area2,
553 simgrid::mc::Snapshot* snapshot1,
554 simgrid::mc::Snapshot* snapshot2,
555 HeapLocationPairs* previous, int size,
558 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
559 simgrid::mc::RegionSnapshot* heap_region1 = MC_get_heap_region(snapshot1);
560 simgrid::mc::RegionSnapshot* heap_region2 = MC_get_heap_region(snapshot2);
562 for (int i = 0; i < size; ) {
564 if (check_ignore > 0) {
565 ssize_t ignore1 = heap_comparison_ignore_size(
566 state.processStates[0].to_ignore, (char *) real_area1 + i);
568 ssize_t ignore2 = heap_comparison_ignore_size(
569 state.processStates[1].to_ignore, (char *) real_area2 + i);
570 if (ignore2 == ignore1) {
583 if (MC_snapshot_region_memcmp(((char *) real_area1) + i, heap_region1, ((char *) real_area2) + i, heap_region2, 1) != 0) {
585 int pointer_align = (i / sizeof(void *)) * sizeof(void *);
586 const void* addr_pointed1 = snapshot1->read(
587 remote((void**)((char *) real_area1 + pointer_align)), process_index);
588 const void* addr_pointed2 = snapshot2->read(
589 remote((void**)((char *) real_area2 + pointer_align)), process_index);
591 if (process->in_maestro_stack(remote(addr_pointed1))
592 && process->in_maestro_stack(remote(addr_pointed2))) {
593 i = pointer_align + sizeof(void *);
597 if (addr_pointed1 > state.std_heap_copy.heapbase
598 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
599 && addr_pointed2 > state.std_heap_copy.heapbase
600 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)) {
601 // Both addreses are in the heap:
602 int res_compare = compare_heap_area(state ,process_index,
603 addr_pointed1, addr_pointed2,
604 snapshot1, snapshot2, previous, nullptr, 0);
605 if (res_compare == 1)
607 i = pointer_align + sizeof(void *);
623 * @param real_area1 Process address for state 1
624 * @param real_area2 Process address for state 2
625 * @param snapshot1 Snapshot of state 1
626 * @param snapshot2 Snapshot of state 2
629 * @param area_size either a byte_size or an elements_count (?)
630 * @param check_ignore
631 * @param pointer_level
632 * @return 0 (same), 1 (different), -1 (unknown)
634 static int compare_heap_area_with_type(
635 simgrid::mc::StateComparator& state, int process_index,
636 const void *real_area1, const void *real_area2,
637 simgrid::mc::Snapshot* snapshot1,
638 simgrid::mc::Snapshot* snapshot2,
639 HeapLocationPairs* previous, simgrid::mc::Type* type,
640 int area_size, int check_ignore,
645 // HACK: This should not happen but in pratice, there are some
646 // DW_TAG_typedef without an associated DW_AT_type:
647 //<1><538832>: Abbrev Number: 111 (DW_TAG_typedef)
648 // <538833> DW_AT_name : (indirect string, offset: 0x2292f3): gregset_t
649 // <538837> DW_AT_decl_file : 98
650 // <538838> DW_AT_decl_line : 37
654 if (is_stack(real_area1) && is_stack(real_area2))
657 if (check_ignore > 0) {
658 ssize_t ignore1 = heap_comparison_ignore_size(state.processStates[0].to_ignore, real_area1);
659 if (ignore1 > 0 && heap_comparison_ignore_size(state.processStates[1].to_ignore, real_area2) == ignore1)
663 simgrid::mc::Type* subtype;
664 simgrid::mc::Type* subsubtype;
666 const void* addr_pointed1;
667 const void* addr_pointed2;
669 simgrid::mc::RegionSnapshot* heap_region1 = MC_get_heap_region(snapshot1);
670 simgrid::mc::RegionSnapshot* heap_region2 = MC_get_heap_region(snapshot2);
672 switch (type->type) {
673 case DW_TAG_unspecified_type:
676 case DW_TAG_base_type:
677 if (not type->name.empty() && type->name == "char") { /* String, hence random (arbitrary ?) size */
678 if (real_area1 == real_area2)
681 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, area_size) != 0;
683 if (area_size != -1 && type->byte_size != area_size)
686 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
689 case DW_TAG_enumeration_type:
690 if (area_size != -1 && type->byte_size != area_size)
692 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
695 case DW_TAG_const_type:
696 case DW_TAG_volatile_type:
698 type = type->subtype;
701 case DW_TAG_array_type:
702 subtype = type->subtype;
703 switch (subtype->type) {
704 case DW_TAG_unspecified_type:
707 case DW_TAG_base_type:
708 case DW_TAG_enumeration_type:
709 case DW_TAG_pointer_type:
710 case DW_TAG_reference_type:
711 case DW_TAG_rvalue_reference_type:
712 case DW_TAG_structure_type:
713 case DW_TAG_class_type:
714 case DW_TAG_union_type:
715 if (subtype->full_type)
716 subtype = subtype->full_type;
717 elm_size = subtype->byte_size;
719 // TODO, just remove the type indirection?
720 case DW_TAG_const_type:
722 case DW_TAG_volatile_type:
723 subsubtype = subtype->subtype;
724 if (subsubtype->full_type)
725 subsubtype = subsubtype->full_type;
726 elm_size = subsubtype->byte_size;
731 for (int i = 0; i < type->element_count; i++) {
732 // TODO, add support for variable stride (DW_AT_byte_stride)
733 int res = compare_heap_area_with_type(state, process_index, (char*)real_area1 + (i * elm_size),
734 (char*)real_area2 + (i * elm_size), snapshot1, snapshot2, previous,
735 type->subtype, subtype->byte_size, check_ignore, pointer_level);
741 case DW_TAG_reference_type:
742 case DW_TAG_rvalue_reference_type:
743 case DW_TAG_pointer_type:
744 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
745 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
746 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
747 return (addr_pointed1 != addr_pointed2);
750 if (pointer_level <= 1) {
751 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
752 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
753 if (addr_pointed1 > state.std_heap_copy.heapbase && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1) &&
754 addr_pointed2 > state.std_heap_copy.heapbase && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
755 return compare_heap_area(state, process_index, addr_pointed1, addr_pointed2, snapshot1, snapshot2, previous,
756 type->subtype, pointer_level);
758 return (addr_pointed1 != addr_pointed2);
760 for (size_t i = 0; i < (area_size / sizeof(void*)); i++) {
761 addr_pointed1 = snapshot1->read(remote((void**)((char*)real_area1 + i * sizeof(void*))), process_index);
762 addr_pointed2 = snapshot2->read(remote((void**)((char*)real_area2 + i * sizeof(void*))), process_index);
764 if (addr_pointed1 > state.std_heap_copy.heapbase && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1) &&
765 addr_pointed2 > state.std_heap_copy.heapbase && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
766 res = compare_heap_area(state, process_index, addr_pointed1, addr_pointed2, snapshot1, snapshot2, previous,
767 type->subtype, pointer_level);
769 res = (addr_pointed1 != addr_pointed2);
775 case DW_TAG_structure_type:
776 case DW_TAG_class_type:
778 type = type->full_type;
779 if (area_size != -1 && type->byte_size != area_size) {
780 if (area_size <= type->byte_size || area_size % type->byte_size != 0)
782 for (size_t i = 0; i < (size_t)(area_size / type->byte_size); i++) {
783 int res = compare_heap_area_with_type(state, process_index, (char*)real_area1 + i * type->byte_size,
784 (char*)real_area2 + i * type->byte_size, snapshot1, snapshot2,
785 previous, type, -1, check_ignore, 0);
790 for (simgrid::mc::Member& member : type->members) {
791 // TODO, optimize this? (for the offset case)
792 void* real_member1 = simgrid::dwarf::resolve_member(real_area1, type, &member,
793 (simgrid::mc::AddressSpace*)snapshot1, process_index);
794 void* real_member2 = simgrid::dwarf::resolve_member(real_area2, type, &member,
795 (simgrid::mc::AddressSpace*)snapshot2, process_index);
796 int res = compare_heap_area_with_type(state, process_index, real_member1, real_member2, snapshot1,
797 snapshot2, previous, member.type, -1, check_ignore, 0);
804 case DW_TAG_union_type:
805 return compare_heap_area_without_type(state, process_index, real_area1, real_area2, snapshot1, snapshot2,
806 previous, type->byte_size, check_ignore);
812 xbt_die("Unreachable");
816 /** Infer the type of a part of the block from the type of the block
818 * TODO, handle DW_TAG_array_type as well as arrays of the object ((*p)[5], p[5])
820 * TODO, handle subfields ((*p).bar.foo, (*p)[5].bar…)
822 * @param type DWARF type ID of the root address
824 * @return DWARF type ID for given offset
826 static simgrid::mc::Type* get_offset_type(void *real_base_address, simgrid::mc::Type* type,
827 int offset, int area_size,
828 simgrid::mc::Snapshot* snapshot, int process_index)
831 // Beginning of the block, the infered variable type if the type of the block:
835 switch (type->type) {
837 case DW_TAG_structure_type:
838 case DW_TAG_class_type:
840 type = type->full_type;
841 if (area_size != -1 && type->byte_size != area_size) {
842 if (area_size > type->byte_size && area_size % type->byte_size == 0)
848 for (simgrid::mc::Member& member : type->members) {
849 if (member.has_offset_location()) {
850 // We have the offset, use it directly (shortcut):
851 if (member.offset() == offset)
854 void* real_member = simgrid::dwarf::resolve_member(real_base_address, type, &member, snapshot, process_index);
855 if ((char*)real_member - (char*)real_base_address == offset)
862 /* FIXME: other cases ? */
870 * @param area1 Process address for state 1
871 * @param area2 Process address for state 2
872 * @param snapshot1 Snapshot of state 1
873 * @param snapshot2 Snapshot of state 2
874 * @param previous Pairs of blocks already compared on the current path (or nullptr)
875 * @param type_id Type of variable
876 * @param pointer_level
877 * @return 0 (same), 1 (different), -1
880 int compare_heap_area(simgrid::mc::StateComparator& state, int process_index,
881 const void *area1, const void *area2,
882 simgrid::mc::Snapshot* snapshot1,
883 simgrid::mc::Snapshot* snapshot2,
884 HeapLocationPairs* previous,
885 simgrid::mc::Type* type, int pointer_level)
887 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
892 int check_ignore = 0;
900 simgrid::mc::Type* new_type1 = nullptr;
901 simgrid::mc::Type* new_type2 = nullptr;
903 bool match_pairs = false;
905 // This is the address of std_heap->heapinfo in the application process:
906 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
908 const malloc_info* heapinfos1 = snapshot1->read(remote((const malloc_info**)heapinfo_address), process_index);
909 const malloc_info* heapinfos2 = snapshot2->read(remote((const malloc_info**)heapinfo_address), process_index);
911 malloc_info heapinfo_temp1;
912 malloc_info heapinfo_temp2;
914 simgrid::mc::HeapLocationPairs current;
915 if (previous == nullptr) {
921 block1 = ((char*)area1 - (char*)state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
922 block2 = ((char*)area2 - (char*)state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
924 // If either block is a stack block:
925 if (is_block_stack((int) block1) && is_block_stack((int) block2)) {
926 previous->insert(simgrid::mc::makeHeapLocationPair(block1, -1, block2, -1));
928 state.match_equals(previous);
932 // If either block is not in the expected area of memory:
933 if (((char*)area1 < (char*)state.std_heap_copy.heapbase) || (block1 > (ssize_t)state.processStates[0].heapsize) ||
934 (block1 < 1) || ((char*)area2 < (char*)state.std_heap_copy.heapbase) ||
935 (block2 > (ssize_t)state.processStates[1].heapsize) || (block2 < 1)) {
939 // Process address of the block:
940 void* real_addr_block1 = (ADDR2UINT(block1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
941 void* real_addr_block2 = (ADDR2UINT(block2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
945 type = type->full_type;
947 // This assume that for "boring" types (volatile ...) byte_size is absent:
948 while (type->byte_size == 0 && type->subtype != nullptr)
949 type = type->subtype;
952 if (type->type == DW_TAG_pointer_type ||
953 (type->type == DW_TAG_base_type && not type->name.empty() && type->name == "char"))
956 type_size = type->byte_size;
960 simgrid::mc::RegionSnapshot* heap_region1 = MC_get_heap_region(snapshot1);
961 simgrid::mc::RegionSnapshot* heap_region2 = MC_get_heap_region(snapshot2);
963 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
964 heap_region1, &heapinfo_temp1, &heapinfos1[block1], sizeof(malloc_info));
965 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
966 heap_region2, &heapinfo_temp2, &heapinfos2[block2], sizeof(malloc_info));
968 if ((heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type==MMALLOC_TYPE_HEAPINFO)
969 && (heapinfo2->type == MMALLOC_TYPE_FREE || heapinfo2->type ==MMALLOC_TYPE_HEAPINFO)) {
972 state.match_equals(previous);
976 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED && heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
979 // TODO, lookup variable type from block type as done for fragmented blocks
981 if (state.equals_to1_(block1, 0).valid_ && state.equals_to2_(block2, 0).valid_ &&
982 state.blocksEqual(block1, block2)) {
984 state.match_equals(previous);
988 if (type_size != -1 && type_size != (ssize_t)heapinfo1->busy_block.busy_size &&
989 type_size != (ssize_t)heapinfo2->busy_block.busy_size &&
990 (type->name.empty() || type->name == "struct s_smx_context")) {
992 state.match_equals(previous);
996 if (heapinfo1->busy_block.size != heapinfo2->busy_block.size)
998 if (heapinfo1->busy_block.busy_size != heapinfo2->busy_block.busy_size)
1001 if (not previous->insert(simgrid::mc::makeHeapLocationPair(block1, -1, block2, -1)).second) {
1003 state.match_equals(previous);
1007 size = heapinfo1->busy_block.busy_size;
1009 // Remember (basic) type inference.
1010 // The current data structure only allows us to do this for the whole block.
1011 if (type != nullptr && area1 == real_addr_block1)
1012 state.types1_(block1, 0) = type;
1013 if (type != nullptr && area2 == real_addr_block2)
1014 state.types2_(block2, 0) = type;
1018 state.match_equals(previous);
1022 if (heapinfo1->busy_block.ignore > 0
1023 && heapinfo2->busy_block.ignore == heapinfo1->busy_block.ignore)
1024 check_ignore = heapinfo1->busy_block.ignore;
1026 } else if ((heapinfo1->type > 0) && (heapinfo2->type > 0)) { /* Fragmented block */
1029 ssize_t frag1 = ((uintptr_t)(ADDR2UINT(area1) % (BLOCKSIZE))) >> heapinfo1->type;
1030 ssize_t frag2 = ((uintptr_t)(ADDR2UINT(area2) % (BLOCKSIZE))) >> heapinfo2->type;
1032 // Process address of the fragment_:
1033 void* real_addr_frag1 = (void*)((char*)real_addr_block1 + (frag1 << heapinfo1->type));
1034 void* real_addr_frag2 = (void*)((char*)real_addr_block2 + (frag2 << heapinfo2->type));
1036 // Check the size of the fragments against the size of the type:
1037 if (type_size != -1) {
1038 if (heapinfo1->busy_frag.frag_size[frag1] == -1 || heapinfo2->busy_frag.frag_size[frag2] == -1) {
1040 state.match_equals(previous);
1044 if (type_size != heapinfo1->busy_frag.frag_size[frag1]
1045 || type_size != heapinfo2->busy_frag.frag_size[frag2]) {
1047 state.match_equals(previous);
1052 // Check if the blocks are already matched together:
1053 if (state.equals_to1_(block1, frag1).valid_ && state.equals_to2_(block2, frag2).valid_ && offset1 == offset2 &&
1054 state.fragmentsEqual(block1, frag1, block2, frag2)) {
1056 state.match_equals(previous);
1059 // Compare the size of both fragments:
1060 if (heapinfo1->busy_frag.frag_size[frag1] != heapinfo2->busy_frag.frag_size[frag2]) {
1061 if (type_size == -1) {
1063 state.match_equals(previous);
1069 // Size of the fragment_:
1070 size = heapinfo1->busy_frag.frag_size[frag1];
1072 // Remember (basic) type inference.
1073 // The current data structure only allows us to do this for the whole fragment_.
1074 if (type != nullptr && area1 == real_addr_frag1)
1075 state.types1_(block1, frag1) = type;
1076 if (type != nullptr && area2 == real_addr_frag2)
1077 state.types2_(block2, frag2) = type;
1079 // The type of the variable is already known:
1081 new_type1 = new_type2 = type;
1083 // Type inference from the block type.
1084 else if (state.types1_(block1, frag1) != nullptr || state.types2_(block2, frag2) != nullptr) {
1086 offset1 = (char*)area1 - (char*)real_addr_frag1;
1087 offset2 = (char*)area2 - (char*)real_addr_frag2;
1089 if (state.types1_(block1, frag1) != nullptr && state.types2_(block2, frag2) != nullptr) {
1091 get_offset_type(real_addr_frag1, state.types1_(block1, frag1), offset1, size, snapshot1, process_index);
1093 get_offset_type(real_addr_frag2, state.types2_(block2, frag2), offset1, size, snapshot2, process_index);
1094 } else if (state.types1_(block1, frag1) != nullptr) {
1096 get_offset_type(real_addr_frag1, state.types1_(block1, frag1), offset1, size, snapshot1, process_index);
1098 get_offset_type(real_addr_frag2, state.types1_(block1, frag1), offset2, size, snapshot2, process_index);
1099 } else if (state.types2_(block2, frag2) != nullptr) {
1101 get_offset_type(real_addr_frag1, state.types2_(block2, frag2), offset1, size, snapshot1, process_index);
1103 get_offset_type(real_addr_frag2, state.types2_(block2, frag2), offset2, size, snapshot2, process_index);
1106 state.match_equals(previous);
1110 if (new_type1 != nullptr && new_type2 != nullptr && new_type1 != new_type2) {
1113 while (type->byte_size == 0 && type->subtype != nullptr)
1114 type = type->subtype;
1115 new_size1 = type->byte_size;
1118 while (type->byte_size == 0 && type->subtype != nullptr)
1119 type = type->subtype;
1120 new_size2 = type->byte_size;
1124 state.match_equals(previous);
1129 if (new_size1 > 0 && new_size1 == new_size2) {
1134 if (offset1 == 0 && offset2 == 0 &&
1135 not previous->insert(simgrid::mc::makeHeapLocationPair(block1, frag1, block2, frag2)).second) {
1137 state.match_equals(previous);
1143 state.match_equals(previous);
1147 if ((heapinfo1->busy_frag.ignore[frag1] > 0) &&
1148 (heapinfo2->busy_frag.ignore[frag2] == heapinfo1->busy_frag.ignore[frag1]))
1149 check_ignore = heapinfo1->busy_frag.ignore[frag1];
1155 /* Start comparison */
1158 res_compare = compare_heap_area_with_type(state, process_index, area1, area2, snapshot1, snapshot2, previous, type,
1159 size, check_ignore, pointer_level);
1161 res_compare = compare_heap_area_without_type(state, process_index, area1, area2, snapshot1, snapshot2, previous,
1162 size, check_ignore);
1164 if (res_compare == 1)
1168 state.match_equals(previous);
1175 /************************** Snapshot comparison *******************************/
1176 /******************************************************************************/
1178 static int compare_areas_with_type(simgrid::mc::StateComparator& state, int process_index, void* real_area1,
1179 simgrid::mc::Snapshot* snapshot1, simgrid::mc::RegionSnapshot* region1,
1180 void* real_area2, simgrid::mc::Snapshot* snapshot2,
1181 simgrid::mc::RegionSnapshot* region2, simgrid::mc::Type* type, int pointer_level)
1183 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
1185 simgrid::mc::Type* subtype;
1186 simgrid::mc::Type* subsubtype;
1192 xbt_assert(type != nullptr);
1193 switch (type->type) {
1194 case DW_TAG_unspecified_type:
1197 case DW_TAG_base_type:
1198 case DW_TAG_enumeration_type:
1199 case DW_TAG_union_type:
1200 return MC_snapshot_region_memcmp(real_area1, region1, real_area2, region2, type->byte_size) != 0;
1201 case DW_TAG_typedef:
1202 case DW_TAG_volatile_type:
1203 case DW_TAG_const_type:
1205 type = type->subtype;
1206 continue; // restart
1207 case DW_TAG_array_type:
1208 subtype = type->subtype;
1209 switch (subtype->type) {
1210 case DW_TAG_unspecified_type:
1213 case DW_TAG_base_type:
1214 case DW_TAG_enumeration_type:
1215 case DW_TAG_pointer_type:
1216 case DW_TAG_reference_type:
1217 case DW_TAG_rvalue_reference_type:
1218 case DW_TAG_structure_type:
1219 case DW_TAG_class_type:
1220 case DW_TAG_union_type:
1221 if (subtype->full_type)
1222 subtype = subtype->full_type;
1223 elm_size = subtype->byte_size;
1225 case DW_TAG_const_type:
1226 case DW_TAG_typedef:
1227 case DW_TAG_volatile_type:
1228 subsubtype = subtype->subtype;
1229 if (subsubtype->full_type)
1230 subsubtype = subsubtype->full_type;
1231 elm_size = subsubtype->byte_size;
1236 for (i = 0; i < type->element_count; i++) {
1237 size_t off = i * elm_size;
1238 res = compare_areas_with_type(state, process_index, (char*)real_area1 + off, snapshot1, region1,
1239 (char*)real_area2 + off, snapshot2, region2, type->subtype, pointer_level);
1244 case DW_TAG_pointer_type:
1245 case DW_TAG_reference_type:
1246 case DW_TAG_rvalue_reference_type: {
1247 void* addr_pointed1 = MC_region_read_pointer(region1, real_area1);
1248 void* addr_pointed2 = MC_region_read_pointer(region2, real_area2);
1250 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type)
1251 return (addr_pointed1 != addr_pointed2);
1252 if (addr_pointed1 == nullptr && addr_pointed2 == nullptr)
1254 if (addr_pointed1 == nullptr || addr_pointed2 == nullptr)
1256 if (not state.compared_pointers.insert(std::make_pair(addr_pointed1, addr_pointed2)).second)
1261 // Some cases are not handled here:
1262 // * the pointers lead to different areas (one to the heap, the other to the RW segment ...)
1263 // * a pointer leads to the read-only segment of the current object
1264 // * a pointer lead to a different ELF object
1266 if (addr_pointed1 > process->heap_address && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)) {
1267 if (not(addr_pointed2 > process->heap_address && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)))
1269 // The pointers are both in the heap:
1270 return simgrid::mc::compare_heap_area(state, process_index, addr_pointed1, addr_pointed2, snapshot1,
1271 snapshot2, nullptr, type->subtype, pointer_level);
1273 } else if (region1->contain(simgrid::mc::remote(addr_pointed1))) {
1274 // The pointers are both in the current object R/W segment:
1275 if (not region2->contain(simgrid::mc::remote(addr_pointed2)))
1277 if (not type->type_id)
1278 return (addr_pointed1 != addr_pointed2);
1280 return compare_areas_with_type(state, process_index, addr_pointed1, snapshot1, region1, addr_pointed2,
1281 snapshot2, region2, type->subtype, pointer_level);
1284 // TODO, We do not handle very well the case where
1285 // it belongs to a different (non-heap) region from the current one.
1287 return (addr_pointed1 != addr_pointed2);
1290 case DW_TAG_structure_type:
1291 case DW_TAG_class_type:
1292 for (simgrid::mc::Member& member : type->members) {
1293 void* member1 = simgrid::dwarf::resolve_member(real_area1, type, &member, snapshot1, process_index);
1294 void* member2 = simgrid::dwarf::resolve_member(real_area2, type, &member, snapshot2, process_index);
1295 simgrid::mc::RegionSnapshot* subregion1 = snapshot1->get_region(member1, process_index, region1); // region1 is hinted
1296 simgrid::mc::RegionSnapshot* subregion2 = snapshot2->get_region(member2, process_index, region2); // region2 is hinted
1297 res = compare_areas_with_type(state, process_index, member1, snapshot1, subregion1, member2, snapshot2,
1298 subregion2, member.type, pointer_level);
1303 case DW_TAG_subroutine_type:
1306 XBT_VERB("Unknown case: %d", type->type);
1314 static int compare_global_variables(simgrid::mc::StateComparator& state, simgrid::mc::ObjectInformation* object_info,
1315 int process_index, simgrid::mc::RegionSnapshot* r1, simgrid::mc::RegionSnapshot* r2,
1316 simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2)
1318 xbt_assert(r1 && r2, "Missing region.");
1321 if (r1->storage_type() == simgrid::mc::StorageType::Privatized) {
1322 xbt_assert(process_index >= 0);
1323 if (r2->storage_type() != simgrid::mc::StorageType::Privatized)
1326 size_t process_count = MC_smpi_process_count();
1327 xbt_assert(process_count == r1->privatized_data().size()
1328 && process_count == r2->privatized_data().size());
1330 // Compare the global variables separately for each simulates process:
1331 for (size_t i = 0; i < process_count; i++) {
1332 if (compare_global_variables(state, object_info, i, r1->privatized_data()[i].get(),
1333 r2->privatized_data()[i].get(), snapshot1, snapshot2))
1339 xbt_assert(r1->storage_type() != simgrid::mc::StorageType::Privatized);
1341 xbt_assert(r2->storage_type() != simgrid::mc::StorageType::Privatized);
1343 std::vector<simgrid::mc::Variable>& variables = object_info->global_variables;
1345 for (simgrid::mc::Variable const& current_var : variables) {
1347 // If the variable is not in this object, skip it:
1348 // We do not expect to find a pointer to something which is not reachable
1349 // by the global variables.
1350 if ((char *) current_var.address < (char *) object_info->start_rw
1351 || (char *) current_var.address > (char *) object_info->end_rw)
1354 simgrid::mc::Type* bvariable_type = current_var.type;
1355 int res = compare_areas_with_type(state, process_index,
1356 (char *) current_var.address, snapshot1, r1,
1357 (char *) current_var.address, snapshot2, r2,
1360 XBT_VERB("Global variable %s (%p) is different between snapshots",
1361 current_var.name.c_str(),
1362 (char *) current_var.address);
1370 static int compare_local_variables(simgrid::mc::StateComparator& state,
1372 simgrid::mc::Snapshot* snapshot1,
1373 simgrid::mc::Snapshot* snapshot2,
1374 mc_snapshot_stack_t stack1,
1375 mc_snapshot_stack_t stack2)
1377 if (stack1->local_variables.size() != stack2->local_variables.size()) {
1378 XBT_VERB("Different number of local variables");
1382 unsigned int cursor = 0;
1383 local_variable_t current_var1;
1384 local_variable_t current_var2;
1385 while (cursor < stack1->local_variables.size()) {
1386 current_var1 = &stack1->local_variables[cursor];
1387 current_var2 = &stack1->local_variables[cursor];
1388 if (current_var1->name != current_var2->name
1389 || current_var1->subprogram != current_var2->subprogram
1390 || current_var1->ip != current_var2->ip) {
1391 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1393 ("Different name of variable (%s - %s) "
1394 "or frame (%s - %s) or ip (%lu - %lu)",
1395 current_var1->name.c_str(),
1396 current_var2->name.c_str(),
1397 current_var1->subprogram->name.c_str(),
1398 current_var2->subprogram->name.c_str(),
1399 current_var1->ip, current_var2->ip);
1402 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1404 simgrid::mc::Type* subtype = current_var1->type;
1406 compare_areas_with_type(state, process_index, current_var1->address, snapshot1,
1407 snapshot1->get_region(current_var1->address, process_index), current_var2->address,
1408 snapshot2, snapshot2->get_region(current_var2->address, process_index), subtype, 0);
1411 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1412 XBT_VERB("Local variable %s (%p - %p) in frame %s "
1413 "is different between snapshots",
1414 current_var1->name.c_str(), current_var1->address, current_var2->address,
1415 current_var1->subprogram->name.c_str());
1426 static std::unique_ptr<simgrid::mc::StateComparator> state_comparator;
1428 int snapshot_compare(Snapshot* s1, Snapshot* s2)
1430 // TODO, make this a field of ModelChecker or something similar
1431 if (state_comparator == nullptr)
1432 state_comparator.reset(new StateComparator());
1434 state_comparator->clear();
1436 RemoteClient* process = &mc_model_checker->process();
1440 int hash_result = 0;
1442 hash_result = (s1->hash_ != s2->hash_);
1444 XBT_VERB("(%d - %d) Different hash: 0x%" PRIx64 "--0x%" PRIx64, s1->num_state_, s2->num_state_, s1->hash_,
1450 XBT_VERB("(%d - %d) Same hash: 0x%" PRIx64, s1->num_state_, s2->num_state_, s1->hash_);
1453 /* Compare enabled processes */
1454 if (s1->enabled_processes_ != s2->enabled_processes_) {
1455 XBT_VERB("(%d - %d) Different amount of enabled processes", s1->num_state_, s2->num_state_);
1459 /* Compare size of stacks */
1461 for (unsigned long i = 0; i < s1->stacks_.size(); i++) {
1462 size_t size_used1 = s1->stack_sizes_[i];
1463 size_t size_used2 = s2->stack_sizes_[i];
1464 if (size_used1 != size_used2) {
1466 XBT_DEBUG("(%d - %d) Different size used in stacks: %zu - %zu", s1->num_state, s2->num_state, size_used1,
1472 XBT_VERB("(%d - %d) Different size used in stacks: %zu - %zu", s1->num_state_, s2->num_state_, size_used1,
1479 if (is_diff) // do not proceed if there is any stacks that don't match
1482 /* Init heap information used in heap comparison algorithm */
1483 xbt_mheap_t heap1 = (xbt_mheap_t)s1->read_bytes(
1484 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1485 remote(process->heap_address),
1486 simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1487 xbt_mheap_t heap2 = (xbt_mheap_t)s2->read_bytes(
1488 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1489 remote(process->heap_address),
1490 simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1491 int res_init = state_comparator->initHeapInformation(heap1, heap2, &s1->to_ignore_, &s2->to_ignore_);
1493 if (res_init == -1) {
1495 XBT_DEBUG("(%d - %d) Different heap information", num1, nus1->num_state, s2->num_statem2);
1499 XBT_VERB("(%d - %d) Different heap information", s1->num_state_, s2->num_state_);
1506 /* Stacks comparison */
1508 for (unsigned int cursor = 0; cursor < s1->stacks_.size(); cursor++) {
1509 mc_snapshot_stack_t stack1 = &s1->stacks_[cursor];
1510 mc_snapshot_stack_t stack2 = &s2->stacks_[cursor];
1512 if (stack1->process_index != stack2->process_index) {
1514 XBT_DEBUG("(%d - %d) Stacks with different process index (%i vs %i)", s1->num_state_, s2->num_state_,
1515 stack1->process_index, stack2->process_index);
1517 else diff_local = compare_local_variables(*state_comparator,
1518 stack1->process_index, s1, s2, stack1, stack2);
1519 if (diff_local > 0) {
1521 XBT_DEBUG("(%d - %d) Different local variables between stacks %d", num1,
1527 XBT_VERB("(%d - %d) Different local variables between stacks %u", s1->num_state_, s2->num_state_, cursor + 1);
1535 size_t regions_count = s1->snapshot_regions_.size();
1536 // TODO, raise a difference instead?
1537 xbt_assert(regions_count == s2->snapshot_regions_.size());
1539 for (size_t k = 0; k != regions_count; ++k) {
1540 RegionSnapshot* region1 = s1->snapshot_regions_[k].get();
1541 RegionSnapshot* region2 = s2->snapshot_regions_[k].get();
1544 if (region1->region_type() != RegionType::Data)
1547 xbt_assert(region1->region_type() == region2->region_type());
1548 xbt_assert(region1->object_info() == region2->object_info());
1549 xbt_assert(region1->object_info());
1551 /* Compare global variables */
1552 if (compare_global_variables(*state_comparator, region1->object_info(), simgrid::mc::ProcessIndexDisabled, region1,
1556 std::string const& name = region1->object_info()->file_name;
1557 XBT_DEBUG("(%d - %d) Different global variables in %s", s1->num_state, s2->num_state, name.c_str());
1561 std::string const& name = region1->object_info()->file_name;
1562 XBT_VERB("(%d - %d) Different global variables in %s", s1->num_state_, s2->num_state_, name.c_str());
1571 if (mmalloc_compare_heap(*state_comparator, s1, s2) > 0) {
1574 XBT_DEBUG("(%d - %d) Different heap (mmalloc_compare)", s1->num_state, s2->num_state);
1579 XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", s1->num_state_, s2->num_state_);
1586 if (errors || hash_result)
1587 XBT_VERB("(%d - %d) Difference found", s1->num_state_, s2->num_state_);
1589 XBT_VERB("(%d - %d) No difference found", s1->num_state_, s2->num_state_);
1592 #if defined(MC_DEBUG) && defined(MC_VERBOSE)
1594 // * false positive SHOULD be avoided.
1595 // * There MUST not be any false negative.
1597 XBT_VERB("(%d - %d) State equality hash test is %s %s", s1->num_state, s2->num_state,
1598 (hash_result != 0) == (errors != 0) ? "true" : "false", not hash_result ? "positive" : "negative");
1602 return errors > 0 || hash_result;