1 /* Copyright (c) 2008-2017. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 /** \file compare.cpp Memory snapshooting and comparison */
14 #include <unordered_set>
16 #include "xbt/dynar.h"
17 #include "xbt/sysdep.h"
18 #include <xbt/mmalloc.h>
21 #include <mc/datatypes.h>
23 #include "src/internal_config.h"
25 #include "src/xbt/mmalloc/mmprivate.h"
28 #include "src/smpi/include/private.hpp"
31 #include "src/mc/Frame.hpp"
32 #include "src/mc/ObjectInformation.hpp"
33 #include "src/mc/Type.hpp"
34 #include "src/mc/Variable.hpp"
35 #include "src/mc/mc_dwarf.hpp"
36 #include "src/mc/mc_forward.hpp"
37 #include "src/mc/mc_private.hpp"
38 #include "src/mc/mc_smx.hpp"
39 #include "src/mc/mc_snapshot.hpp"
41 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_compare, xbt, "Logging specific to mc_compare in mc");
47 typedef std::array<HeapLocation, 2> HeapLocationPair;
48 typedef std::set<HeapLocationPair> HeapLocationPairs;
50 struct ProcessComparisonState;
51 struct StateComparator;
53 static int compare_heap_area(
54 StateComparator& state,
55 int process_index, const void *area1, const void* area2,
56 Snapshot* snapshot1, Snapshot* snapshot2,
57 HeapLocationPairs* previous, Type* type, int pointer_level);
62 using simgrid::mc::remote;
64 /*********************************** Heap comparison ***********************************/
65 /***************************************************************************************/
75 HeapLocation() = default;
76 HeapLocation(int block, int fragment = 0) : block_(block), fragment_(fragment) {}
78 bool operator==(HeapLocation const& that) const
80 return block_ == that.block_ && fragment_ == that.fragment_;
82 bool operator<(HeapLocation const& that) const
84 return std::make_pair(block_, fragment_) < std::make_pair(that.block_, that.fragment_);
89 HeapLocationPair makeHeapLocationPair(int block1, int fragment1, int block2, int fragment2)
91 return simgrid::mc::HeapLocationPair{{
92 simgrid::mc::HeapLocation(block1, fragment1),
93 simgrid::mc::HeapLocation(block2, fragment2)
97 class HeapArea : public HeapLocation {
100 HeapArea() = default;
101 explicit HeapArea(int block) : valid_(true) { block_ = block; }
102 HeapArea(int block, int fragment) : valid_(true)
105 fragment_ = fragment;
109 class ProcessComparisonState {
111 std::vector<simgrid::mc::IgnoredHeapRegion>* to_ignore = nullptr;
112 std::vector<HeapArea> equals_to;
113 std::vector<simgrid::mc::Type*> types;
114 std::size_t heapsize = 0;
116 void initHeapInformation(xbt_mheap_t heap, std::vector<simgrid::mc::IgnoredHeapRegion>* i);
121 /** A hash which works with more stuff
123 * It can hash pairs: the standard hash currently doesn't include this.
125 template <class X> class hash : public std::hash<X> {
128 template <class X, class Y> class hash<std::pair<X, Y>> {
130 std::size_t operator()(std::pair<X,Y>const& x) const
134 return h1(x.first) ^ h2(x.second);
140 class StateComparator {
142 s_xbt_mheap_t std_heap_copy;
143 std::size_t heaplimit;
144 std::array<ProcessComparisonState, 2> processStates;
146 std::unordered_set<std::pair<void*, void*>, hash<std::pair<void*, void*>>> compared_pointers;
150 compared_pointers.clear();
153 int initHeapInformation(
154 xbt_mheap_t heap1, xbt_mheap_t heap2,
155 std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
156 std::vector<simgrid::mc::IgnoredHeapRegion>* i2);
158 HeapArea& equals_to1_(std::size_t i, std::size_t j)
160 return processStates[0].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
162 HeapArea& equals_to2_(std::size_t i, std::size_t j)
164 return processStates[1].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
166 Type*& types1_(std::size_t i, std::size_t j)
168 return processStates[0].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
170 Type*& types2_(std::size_t i, std::size_t j)
172 return processStates[1].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
175 HeapArea const& equals_to1_(std::size_t i, std::size_t j) const
177 return processStates[0].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
179 HeapArea const& equals_to2_(std::size_t i, std::size_t j) const
181 return processStates[1].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
183 Type* const& types1_(std::size_t i, std::size_t j) const
185 return processStates[0].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
187 Type* const& types2_(std::size_t i, std::size_t j) const
189 return processStates[1].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
192 /** Check whether two blocks are known to be matching
194 * @param b1 Block of state 1
195 * @param b2 Block of state 2
196 * @return if the blocks are known to be matching
198 bool blocksEqual(int b1, int b2) const
200 return this->equals_to1_(b1, 0).block_ == b2 && this->equals_to2_(b2, 0).block_ == b1;
203 /** Check whether two fragments are known to be matching
205 * @param b1 Block of state 1
206 * @param f1 Fragment of state 1
207 * @param b2 Block of state 2
208 * @param f2 Fragment of state 2
209 * @return if the fragments are known to be matching
211 int fragmentsEqual(int b1, int f1, int b2, int f2) const
213 return this->equals_to1_(b1, f1).block_ == b2 && this->equals_to1_(b1, f1).fragment_ == f2 &&
214 this->equals_to2_(b2, f2).block_ == b1 && this->equals_to2_(b2, f2).fragment_ == f1;
217 void match_equals(HeapLocationPairs* list);
223 /************************************************************************************/
225 static ssize_t heap_comparison_ignore_size(
226 std::vector<simgrid::mc::IgnoredHeapRegion>* ignore_list,
230 int end = ignore_list->size() - 1;
232 while (start <= end) {
233 unsigned int cursor = (start + end) / 2;
234 simgrid::mc::IgnoredHeapRegion const& region = (*ignore_list)[cursor];
235 if (region.address == address)
237 if (region.address < address)
239 if (region.address > address)
246 static bool is_stack(const void *address)
248 for (auto const& stack : mc_model_checker->process().stack_areas())
249 if (address == stack.address)
254 // TODO, this should depend on the snapshot?
255 static bool is_block_stack(int block)
257 for (auto const& stack : mc_model_checker->process().stack_areas())
258 if (block == stack.block)
266 void StateComparator::match_equals(HeapLocationPairs* list)
268 for (auto const& pair : *list) {
269 if (pair[0].fragment_ != -1) {
270 this->equals_to1_(pair[0].block_, pair[0].fragment_) = simgrid::mc::HeapArea(pair[1].block_, pair[1].fragment_);
271 this->equals_to2_(pair[1].block_, pair[1].fragment_) = simgrid::mc::HeapArea(pair[0].block_, pair[0].fragment_);
273 this->equals_to1_(pair[0].block_, 0) = simgrid::mc::HeapArea(pair[1].block_, pair[1].fragment_);
274 this->equals_to2_(pair[1].block_, 0) = simgrid::mc::HeapArea(pair[0].block_, pair[0].fragment_);
279 void ProcessComparisonState::initHeapInformation(xbt_mheap_t heap,
280 std::vector<simgrid::mc::IgnoredHeapRegion>* i)
282 auto heaplimit = heap->heaplimit;
283 this->heapsize = heap->heapsize;
285 this->equals_to.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, HeapArea());
286 this->types.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, nullptr);
289 int StateComparator::initHeapInformation(xbt_mheap_t heap1, xbt_mheap_t heap2,
290 std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
291 std::vector<simgrid::mc::IgnoredHeapRegion>* i2)
293 if ((heap1->heaplimit != heap2->heaplimit) || (heap1->heapsize != heap2->heapsize))
295 this->heaplimit = heap1->heaplimit;
296 this->std_heap_copy = *mc_model_checker->process().get_heap();
297 this->processStates[0].initHeapInformation(heap1, i1);
298 this->processStates[1].initHeapInformation(heap2, i2);
302 // TODO, have a robust way to find it in O(1)
304 mc_mem_region_t MC_get_heap_region(simgrid::mc::Snapshot* snapshot)
306 for (auto const& region : snapshot->snapshot_regions)
307 if (region->region_type() == simgrid::mc::RegionType::Heap)
309 xbt_die("No heap region");
313 int mmalloc_compare_heap(
314 simgrid::mc::StateComparator& state, simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2)
316 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
318 /* Start comparison */
332 /* Check busy blocks */
335 malloc_info heapinfo_temp1;
336 malloc_info heapinfo_temp2;
337 malloc_info heapinfo_temp2b;
339 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
340 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
342 // This is the address of std_heap->heapinfo in the application process:
343 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
345 // This is in snapshot do not use them directly:
346 const malloc_info* heapinfos1 = snapshot1->read<malloc_info*>(
347 RemotePtr<malloc_info*>((std::uint64_t)heapinfo_address), simgrid::mc::ProcessIndexMissing);
348 const malloc_info* heapinfos2 = snapshot2->read<malloc_info*>(
349 RemotePtr<malloc_info*>((std::uint64_t)heapinfo_address), simgrid::mc::ProcessIndexMissing);
351 while (i1 < state.heaplimit) {
353 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(heap_region1, &heapinfo_temp1, &heapinfos1[i1], sizeof(malloc_info));
354 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2, &heapinfos2[i1], sizeof(malloc_info));
356 if (heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type == MMALLOC_TYPE_HEAPINFO) { /* Free block */
361 if (heapinfo1->type < 0) {
362 fprintf(stderr, "Unkown mmalloc block type.\n");
366 addr_block1 = ((void*)(((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase));
368 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED) { /* Large block */
370 if (is_stack(addr_block1)) {
371 for (k = 0; k < heapinfo1->busy_block.size; k++)
372 state.equals_to1_(i1 + k, 0) = HeapArea(i1, -1);
373 for (k = 0; k < heapinfo2->busy_block.size; k++)
374 state.equals_to2_(i1 + k, 0) = HeapArea(i1, -1);
375 i1 += heapinfo1->busy_block.size;
379 if (state.equals_to1_(i1, 0).valid_) {
387 /* Try first to associate to same block in the other heap */
388 if (heapinfo2->type == heapinfo1->type && state.equals_to2_(i1, 0).valid_ == 0) {
389 addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
390 int res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing, addr_block1, addr_block2,
391 snapshot1, snapshot2, nullptr, nullptr, 0);
392 if (res_compare != 1) {
393 for (k = 1; k < heapinfo2->busy_block.size; k++)
394 state.equals_to2_(i1 + k, 0) = HeapArea(i1, -1);
395 for (k = 1; k < heapinfo1->busy_block.size; k++)
396 state.equals_to1_(i1 + k, 0) = HeapArea(i1, -1);
398 i1 += heapinfo1->busy_block.size;
402 while (i2 < state.heaplimit && not equal) {
404 addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
411 const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2b, &heapinfos2[i2], sizeof(malloc_info));
413 if (heapinfo2b->type != MMALLOC_TYPE_UNFRAGMENTED) {
418 if (state.equals_to2_(i2, 0).valid_) {
423 int res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing, addr_block1, addr_block2,
424 snapshot1, snapshot2, nullptr, nullptr, 0);
426 if (res_compare != 1) {
427 for (k = 1; k < heapinfo2b->busy_block.size; k++)
428 state.equals_to2_(i2 + k, 0) = HeapArea(i1, -1);
429 for (k = 1; k < heapinfo1->busy_block.size; k++)
430 state.equals_to1_(i1 + k, 0) = HeapArea(i2, -1);
432 i1 += heapinfo1->busy_block.size;
439 XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1, heapinfo1->busy_block.busy_size, addr_block1);
440 i1 = state.heaplimit + 1;
444 } else { /* Fragmented block */
446 for (j1 = 0; j1 < (size_t) (BLOCKSIZE >> heapinfo1->type); j1++) {
448 if (heapinfo1->busy_frag.frag_size[j1] == -1) /* Free fragment_ */
451 if (state.equals_to1_(i1, j1).valid_)
454 addr_frag1 = (void*)((char*)addr_block1 + (j1 << heapinfo1->type));
459 /* Try first to associate to same fragment_ in the other heap */
460 if (heapinfo2->type == heapinfo1->type && not state.equals_to2_(i1, j1).valid_) {
461 addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE +
462 (char *) state.std_heap_copy.heapbase;
464 (void *) ((char *) addr_block2 +
465 (j1 << heapinfo2->type));
466 int res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing, addr_frag1, addr_frag2,
467 snapshot1, snapshot2, nullptr, nullptr, 0);
468 if (res_compare != 1)
472 while (i2 < state.heaplimit && not equal) {
474 const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(
475 heap_region2, &heapinfo_temp2b, &heapinfos2[i2],
476 sizeof(malloc_info));
478 if (heapinfo2b->type == MMALLOC_TYPE_FREE || heapinfo2b->type == MMALLOC_TYPE_HEAPINFO) {
483 // We currently do not match fragments with unfragmented blocks (maybe we should).
484 if (heapinfo2b->type == MMALLOC_TYPE_UNFRAGMENTED) {
489 if (heapinfo2b->type < 0) {
490 fprintf(stderr, "Unknown mmalloc block type.\n");
494 for (j2 = 0; j2 < (size_t) (BLOCKSIZE >> heapinfo2b->type);
497 if (i2 == i1 && j2 == j1)
500 if (state.equals_to2_(i2, j2).valid_)
503 addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
504 addr_frag2 = (void*)((char*)addr_block2 + (j2 << heapinfo2b->type));
506 int res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing, addr_frag1, addr_frag2,
507 snapshot2, snapshot2, nullptr, nullptr, 0);
508 if (res_compare != 1) {
518 XBT_DEBUG("Block %zu, fragment_ %zu not found (size_used = %zd, address = %p)\n", i1, j1,
519 heapinfo1->busy_frag.frag_size[j1], addr_frag1);
520 i1 = state.heaplimit + 1;
530 /* All blocks/fragments are equal to another block/fragment_ ? */
534 for(i = 1; i < state.heaplimit; i++) {
535 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
536 heap_region1, &heapinfo_temp1, &heapinfos1[i], sizeof(malloc_info));
538 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED && i1 == state.heaplimit && heapinfo1->busy_block.busy_size > 0 &&
539 not state.equals_to1_(i, 0).valid_) {
540 XBT_DEBUG("Block %zu not found (size used = %zu)", i, heapinfo1->busy_block.busy_size);
544 if (heapinfo1->type <= 0)
546 for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo1->type); j++)
547 if (i1 == state.heaplimit && heapinfo1->busy_frag.frag_size[j] > 0 && not state.equals_to1_(i, j).valid_) {
548 XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)", i, j, heapinfo1->busy_frag.frag_size[j]);
553 if (i1 == state.heaplimit)
554 XBT_DEBUG("Number of blocks/fragments not found in heap1: %d", nb_diff1);
556 for (i=1; i < state.heaplimit; i++) {
557 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
558 heap_region2, &heapinfo_temp2, &heapinfos2[i], sizeof(malloc_info));
559 if (heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED && i1 == state.heaplimit && heapinfo2->busy_block.busy_size > 0 &&
560 not state.equals_to2_(i, 0).valid_) {
561 XBT_DEBUG("Block %zu not found (size used = %zu)", i,
562 heapinfo2->busy_block.busy_size);
566 if (heapinfo2->type <= 0)
569 for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo2->type); j++)
570 if (i1 == state.heaplimit && heapinfo2->busy_frag.frag_size[j] > 0 && not state.equals_to2_(i, j).valid_) {
571 XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)",
572 i, j, heapinfo2->busy_frag.frag_size[j]);
578 if (i1 == state.heaplimit)
579 XBT_DEBUG("Number of blocks/fragments not found in heap2: %d", nb_diff2);
581 return nb_diff1 > 0 || nb_diff2 > 0;
587 * @param real_area1 Process address for state 1
588 * @param real_area2 Process address for state 2
589 * @param snapshot1 Snapshot of state 1
590 * @param snapshot2 Snapshot of state 2
593 * @param check_ignore
595 static int compare_heap_area_without_type(
596 simgrid::mc::StateComparator& state, int process_index,
597 const void *real_area1, const void *real_area2,
598 simgrid::mc::Snapshot* snapshot1,
599 simgrid::mc::Snapshot* snapshot2,
600 HeapLocationPairs* previous, int size,
603 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
604 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
605 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
607 for (int i = 0; i < size; ) {
609 if (check_ignore > 0) {
610 ssize_t ignore1 = heap_comparison_ignore_size(
611 state.processStates[0].to_ignore, (char *) real_area1 + i);
613 ssize_t ignore2 = heap_comparison_ignore_size(
614 state.processStates[1].to_ignore, (char *) real_area2 + i);
615 if (ignore2 == ignore1) {
628 if (MC_snapshot_region_memcmp(((char *) real_area1) + i, heap_region1, ((char *) real_area2) + i, heap_region2, 1) != 0) {
630 int pointer_align = (i / sizeof(void *)) * sizeof(void *);
631 const void* addr_pointed1 = snapshot1->read(
632 remote((void**)((char *) real_area1 + pointer_align)), process_index);
633 const void* addr_pointed2 = snapshot2->read(
634 remote((void**)((char *) real_area2 + pointer_align)), process_index);
636 if (process->in_maestro_stack(remote(addr_pointed1))
637 && process->in_maestro_stack(remote(addr_pointed2))) {
638 i = pointer_align + sizeof(void *);
642 if (addr_pointed1 > state.std_heap_copy.heapbase
643 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
644 && addr_pointed2 > state.std_heap_copy.heapbase
645 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)) {
646 // Both addreses are in the heap:
647 int res_compare = compare_heap_area(state ,process_index,
648 addr_pointed1, addr_pointed2,
649 snapshot1, snapshot2, previous, nullptr, 0);
650 if (res_compare == 1)
652 i = pointer_align + sizeof(void *);
668 * @param real_area1 Process address for state 1
669 * @param real_area2 Process address for state 2
670 * @param snapshot1 Snapshot of state 1
671 * @param snapshot2 Snapshot of state 2
674 * @param area_size either a byte_size or an elements_count (?)
675 * @param check_ignore
676 * @param pointer_level
677 * @return 0 (same), 1 (different), -1 (unknown)
679 static int compare_heap_area_with_type(
680 simgrid::mc::StateComparator& state, int process_index,
681 const void *real_area1, const void *real_area2,
682 simgrid::mc::Snapshot* snapshot1,
683 simgrid::mc::Snapshot* snapshot2,
684 HeapLocationPairs* previous, simgrid::mc::Type* type,
685 int area_size, int check_ignore,
690 // HACK: This should not happen but in pratice, there are some
691 // DW_TAG_typedef without an associated DW_AT_type:
692 //<1><538832>: Abbrev Number: 111 (DW_TAG_typedef)
693 // <538833> DW_AT_name : (indirect string, offset: 0x2292f3): gregset_t
694 // <538837> DW_AT_decl_file : 98
695 // <538838> DW_AT_decl_line : 37
699 if (is_stack(real_area1) && is_stack(real_area2))
702 if (check_ignore > 0) {
703 ssize_t ignore1 = heap_comparison_ignore_size(state.processStates[0].to_ignore, real_area1);
704 if (ignore1 > 0 && heap_comparison_ignore_size(state.processStates[1].to_ignore, real_area2) == ignore1)
708 simgrid::mc::Type* subtype;
709 simgrid::mc::Type* subsubtype;
712 const void* addr_pointed1;
713 const void* addr_pointed2;
715 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
716 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
718 switch (type->type) {
719 case DW_TAG_unspecified_type:
722 case DW_TAG_base_type:
723 if (not type->name.empty() && type->name == "char") { /* String, hence random (arbitrary ?) size */
724 if (real_area1 == real_area2)
727 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, area_size) != 0;
729 if (area_size != -1 && type->byte_size != area_size)
732 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
736 case DW_TAG_enumeration_type:
737 if (area_size != -1 && type->byte_size != area_size)
739 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
742 case DW_TAG_const_type:
743 case DW_TAG_volatile_type:
745 type = type->subtype;
748 case DW_TAG_array_type:
749 subtype = type->subtype;
750 switch (subtype->type) {
751 case DW_TAG_unspecified_type:
754 case DW_TAG_base_type:
755 case DW_TAG_enumeration_type:
756 case DW_TAG_pointer_type:
757 case DW_TAG_reference_type:
758 case DW_TAG_rvalue_reference_type:
759 case DW_TAG_structure_type:
760 case DW_TAG_class_type:
761 case DW_TAG_union_type:
762 if (subtype->full_type)
763 subtype = subtype->full_type;
764 elm_size = subtype->byte_size;
766 // TODO, just remove the type indirection?
767 case DW_TAG_const_type:
769 case DW_TAG_volatile_type:
770 subsubtype = subtype->subtype;
771 if (subsubtype->full_type)
772 subsubtype = subsubtype->full_type;
773 elm_size = subsubtype->byte_size;
779 for (int i = 0; i < type->element_count; i++) {
780 // TODO, add support for variable stride (DW_AT_byte_stride)
781 res = compare_heap_area_with_type(state, process_index, (char*)real_area1 + (i * elm_size),
782 (char*)real_area2 + (i * elm_size), snapshot1, snapshot2, previous,
783 type->subtype, subtype->byte_size, check_ignore, pointer_level);
789 case DW_TAG_reference_type:
790 case DW_TAG_rvalue_reference_type:
791 case DW_TAG_pointer_type:
792 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
793 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
794 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
795 return (addr_pointed1 != addr_pointed2);
798 if (pointer_level <= 1) {
799 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
800 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
801 if (addr_pointed1 > state.std_heap_copy.heapbase && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1) &&
802 addr_pointed2 > state.std_heap_copy.heapbase && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
803 return compare_heap_area(state, process_index, addr_pointed1, addr_pointed2, snapshot1, snapshot2, previous,
804 type->subtype, pointer_level);
806 return (addr_pointed1 != addr_pointed2);
808 for (size_t i = 0; i < (area_size / sizeof(void*)); i++) {
809 addr_pointed1 = snapshot1->read(remote((void**)((char*)real_area1 + i * sizeof(void*))), process_index);
810 addr_pointed2 = snapshot2->read(remote((void**)((char*)real_area2 + i * sizeof(void*))), process_index);
811 if (addr_pointed1 > state.std_heap_copy.heapbase && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1) &&
812 addr_pointed2 > state.std_heap_copy.heapbase && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
813 res = compare_heap_area(state, process_index, addr_pointed1, addr_pointed2, snapshot1, snapshot2, previous,
814 type->subtype, pointer_level);
816 res = (addr_pointed1 != addr_pointed2);
822 case DW_TAG_structure_type:
823 case DW_TAG_class_type:
825 type = type->full_type;
826 if (area_size != -1 && type->byte_size != area_size) {
827 if (area_size <= type->byte_size || area_size % type->byte_size != 0)
829 for (size_t i = 0; i < (size_t)(area_size / type->byte_size); i++) {
830 int res = compare_heap_area_with_type(state, process_index, (char*)real_area1 + i * type->byte_size,
831 (char*)real_area2 + i * type->byte_size, snapshot1, snapshot2,
832 previous, type, -1, check_ignore, 0);
837 for (simgrid::mc::Member& member : type->members) {
838 // TODO, optimize this? (for the offset case)
839 void* real_member1 = simgrid::dwarf::resolve_member(real_area1, type, &member,
840 (simgrid::mc::AddressSpace*)snapshot1, process_index);
841 void* real_member2 = simgrid::dwarf::resolve_member(real_area2, type, &member,
842 (simgrid::mc::AddressSpace*)snapshot2, process_index);
843 int res = compare_heap_area_with_type(state, process_index, real_member1, real_member2, snapshot1,
844 snapshot2, previous, member.type, -1, check_ignore, 0);
851 case DW_TAG_union_type:
852 return compare_heap_area_without_type(state, process_index, real_area1, real_area2, snapshot1, snapshot2,
853 previous, type->byte_size, check_ignore);
859 xbt_die("Unreachable");
863 /** Infer the type of a part of the block from the type of the block
865 * TODO, handle DW_TAG_array_type as well as arrays of the object ((*p)[5], p[5])
867 * TODO, handle subfields ((*p).bar.foo, (*p)[5].bar…)
869 * @param type DWARF type ID of the root address
871 * @return DWARF type ID for given offset
873 static simgrid::mc::Type* get_offset_type(void *real_base_address, simgrid::mc::Type* type,
874 int offset, int area_size,
875 simgrid::mc::Snapshot* snapshot, int process_index)
878 // Beginning of the block, the infered variable type if the type of the block:
882 switch (type->type) {
884 case DW_TAG_structure_type:
885 case DW_TAG_class_type:
887 type = type->full_type;
888 if (area_size != -1 && type->byte_size != area_size) {
889 if (area_size > type->byte_size && area_size % type->byte_size == 0)
895 for (simgrid::mc::Member& member : type->members) {
896 if (member.has_offset_location()) {
897 // We have the offset, use it directly (shortcut):
898 if (member.offset() == offset)
901 void* real_member = simgrid::dwarf::resolve_member(real_base_address, type, &member, snapshot, process_index);
902 if ((char*)real_member - (char*)real_base_address == offset)
909 /* FIXME: other cases ? */
917 * @param area1 Process address for state 1
918 * @param area2 Process address for state 2
919 * @param snapshot1 Snapshot of state 1
920 * @param snapshot2 Snapshot of state 2
921 * @param previous Pairs of blocks already compared on the current path (or nullptr)
922 * @param type_id Type of variable
923 * @param pointer_level
924 * @return 0 (same), 1 (different), -1
927 int compare_heap_area(simgrid::mc::StateComparator& state, int process_index,
928 const void *area1, const void *area2,
929 simgrid::mc::Snapshot* snapshot1,
930 simgrid::mc::Snapshot* snapshot2,
931 HeapLocationPairs* previous,
932 simgrid::mc::Type* type, int pointer_level)
934 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
939 int check_ignore = 0;
947 simgrid::mc::Type* new_type1 = nullptr;
948 simgrid::mc::Type* new_type2 = nullptr;
950 bool match_pairs = false;
952 // This is the address of std_heap->heapinfo in the application process:
953 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
955 const malloc_info* heapinfos1 = snapshot1->read(remote((const malloc_info**)heapinfo_address), process_index);
956 const malloc_info* heapinfos2 = snapshot2->read(remote((const malloc_info**)heapinfo_address), process_index);
958 malloc_info heapinfo_temp1;
959 malloc_info heapinfo_temp2;
961 simgrid::mc::HeapLocationPairs current;
962 if (previous == nullptr) {
968 block1 = ((char*)area1 - (char*)state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
969 block2 = ((char*)area2 - (char*)state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
971 // If either block is a stack block:
972 if (is_block_stack((int) block1) && is_block_stack((int) block2)) {
973 previous->insert(simgrid::mc::makeHeapLocationPair(block1, -1, block2, -1));
975 state.match_equals(previous);
979 // If either block is not in the expected area of memory:
980 if (((char*)area1 < (char*)state.std_heap_copy.heapbase) || (block1 > (ssize_t)state.processStates[0].heapsize) ||
981 (block1 < 1) || ((char*)area2 < (char*)state.std_heap_copy.heapbase) ||
982 (block2 > (ssize_t)state.processStates[1].heapsize) || (block2 < 1)) {
986 // Process address of the block:
987 void* real_addr_block1 = (ADDR2UINT(block1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
988 void* real_addr_block2 = (ADDR2UINT(block2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
992 type = type->full_type;
994 // This assume that for "boring" types (volatile ...) byte_size is absent:
995 while (type->byte_size == 0 && type->subtype != nullptr)
996 type = type->subtype;
999 if (type->type == DW_TAG_pointer_type ||
1000 (type->type == DW_TAG_base_type && not type->name.empty() && type->name == "char"))
1003 type_size = type->byte_size;
1007 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
1008 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
1010 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
1011 heap_region1, &heapinfo_temp1, &heapinfos1[block1], sizeof(malloc_info));
1012 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
1013 heap_region2, &heapinfo_temp2, &heapinfos2[block2], sizeof(malloc_info));
1015 if ((heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type==MMALLOC_TYPE_HEAPINFO)
1016 && (heapinfo2->type == MMALLOC_TYPE_FREE || heapinfo2->type ==MMALLOC_TYPE_HEAPINFO)) {
1019 state.match_equals(previous);
1023 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED && heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
1024 /* Complete block */
1026 // TODO, lookup variable type from block type as done for fragmented blocks
1028 if (state.equals_to1_(block1, 0).valid_ && state.equals_to2_(block2, 0).valid_ &&
1029 state.blocksEqual(block1, block2)) {
1031 state.match_equals(previous);
1035 if (type_size != -1 && type_size != (ssize_t)heapinfo1->busy_block.busy_size &&
1036 type_size != (ssize_t)heapinfo2->busy_block.busy_size &&
1037 (type->name.empty() || type->name == "struct s_smx_context")) {
1039 state.match_equals(previous);
1043 if (heapinfo1->busy_block.size != heapinfo2->busy_block.size)
1045 if (heapinfo1->busy_block.busy_size != heapinfo2->busy_block.busy_size)
1048 if (not previous->insert(simgrid::mc::makeHeapLocationPair(block1, -1, block2, -1)).second) {
1050 state.match_equals(previous);
1054 size = heapinfo1->busy_block.busy_size;
1056 // Remember (basic) type inference.
1057 // The current data structure only allows us to do this for the whole block.
1058 if (type != nullptr && area1 == real_addr_block1)
1059 state.types1_(block1, 0) = type;
1060 if (type != nullptr && area2 == real_addr_block2)
1061 state.types2_(block2, 0) = type;
1065 state.match_equals(previous);
1069 if (heapinfo1->busy_block.ignore > 0
1070 && heapinfo2->busy_block.ignore == heapinfo1->busy_block.ignore)
1071 check_ignore = heapinfo1->busy_block.ignore;
1073 } else if ((heapinfo1->type > 0) && (heapinfo2->type > 0)) { /* Fragmented block */
1076 ssize_t frag1 = ((uintptr_t)(ADDR2UINT(area1) % (BLOCKSIZE))) >> heapinfo1->type;
1077 ssize_t frag2 = ((uintptr_t)(ADDR2UINT(area2) % (BLOCKSIZE))) >> heapinfo2->type;
1079 // Process address of the fragment_:
1080 void* real_addr_frag1 = (void*)((char*)real_addr_block1 + (frag1 << heapinfo1->type));
1081 void* real_addr_frag2 = (void*)((char*)real_addr_block2 + (frag2 << heapinfo2->type));
1083 // Check the size of the fragments against the size of the type:
1084 if (type_size != -1) {
1085 if (heapinfo1->busy_frag.frag_size[frag1] == -1 || heapinfo2->busy_frag.frag_size[frag2] == -1) {
1087 state.match_equals(previous);
1091 if (type_size != heapinfo1->busy_frag.frag_size[frag1]
1092 || type_size != heapinfo2->busy_frag.frag_size[frag2]) {
1094 state.match_equals(previous);
1099 // Check if the blocks are already matched together:
1100 if (state.equals_to1_(block1, frag1).valid_ && state.equals_to2_(block2, frag2).valid_ && offset1 == offset2 &&
1101 state.fragmentsEqual(block1, frag1, block2, frag2)) {
1103 state.match_equals(previous);
1106 // Compare the size of both fragments:
1107 if (heapinfo1->busy_frag.frag_size[frag1] != heapinfo2->busy_frag.frag_size[frag2]) {
1108 if (type_size == -1) {
1110 state.match_equals(previous);
1116 // Size of the fragment_:
1117 size = heapinfo1->busy_frag.frag_size[frag1];
1119 // Remember (basic) type inference.
1120 // The current data structure only allows us to do this for the whole fragment_.
1121 if (type != nullptr && area1 == real_addr_frag1)
1122 state.types1_(block1, frag1) = type;
1123 if (type != nullptr && area2 == real_addr_frag2)
1124 state.types2_(block2, frag2) = type;
1126 // The type of the variable is already known:
1128 new_type1 = new_type2 = type;
1130 // Type inference from the block type.
1131 else if (state.types1_(block1, frag1) != nullptr || state.types2_(block2, frag2) != nullptr) {
1133 offset1 = (char*)area1 - (char*)real_addr_frag1;
1134 offset2 = (char*)area2 - (char*)real_addr_frag2;
1136 if (state.types1_(block1, frag1) != nullptr && state.types2_(block2, frag2) != nullptr) {
1138 get_offset_type(real_addr_frag1, state.types1_(block1, frag1), offset1, size, snapshot1, process_index);
1140 get_offset_type(real_addr_frag2, state.types2_(block2, frag2), offset1, size, snapshot2, process_index);
1141 } else if (state.types1_(block1, frag1) != nullptr) {
1143 get_offset_type(real_addr_frag1, state.types1_(block1, frag1), offset1, size, snapshot1, process_index);
1145 get_offset_type(real_addr_frag2, state.types1_(block1, frag1), offset2, size, snapshot2, process_index);
1146 } else if (state.types2_(block2, frag2) != nullptr) {
1148 get_offset_type(real_addr_frag1, state.types2_(block2, frag2), offset1, size, snapshot1, process_index);
1150 get_offset_type(real_addr_frag2, state.types2_(block2, frag2), offset2, size, snapshot2, process_index);
1153 state.match_equals(previous);
1157 if (new_type1 != nullptr && new_type2 != nullptr && new_type1 != new_type2) {
1160 while (type->byte_size == 0 && type->subtype != nullptr)
1161 type = type->subtype;
1162 new_size1 = type->byte_size;
1165 while (type->byte_size == 0 && type->subtype != nullptr)
1166 type = type->subtype;
1167 new_size2 = type->byte_size;
1171 state.match_equals(previous);
1176 if (new_size1 > 0 && new_size1 == new_size2) {
1181 if (offset1 == 0 && offset2 == 0 &&
1182 not previous->insert(simgrid::mc::makeHeapLocationPair(block1, frag1, block2, frag2)).second) {
1184 state.match_equals(previous);
1190 state.match_equals(previous);
1194 if ((heapinfo1->busy_frag.ignore[frag1] > 0) &&
1195 (heapinfo2->busy_frag.ignore[frag2] == heapinfo1->busy_frag.ignore[frag1]))
1196 check_ignore = heapinfo1->busy_frag.ignore[frag1];
1202 /* Start comparison */
1205 res_compare = compare_heap_area_with_type(state, process_index, area1, area2, snapshot1, snapshot2, previous, type,
1206 size, check_ignore, pointer_level);
1208 res_compare = compare_heap_area_without_type(state, process_index, area1, area2, snapshot1, snapshot2, previous,
1209 size, check_ignore);
1211 if (res_compare == 1)
1215 state.match_equals(previous);
1222 /************************** Snapshot comparison *******************************/
1223 /******************************************************************************/
1225 static int compare_areas_with_type(simgrid::mc::StateComparator& state,
1227 void* real_area1, simgrid::mc::Snapshot* snapshot1, mc_mem_region_t region1,
1228 void* real_area2, simgrid::mc::Snapshot* snapshot2, mc_mem_region_t region2,
1229 simgrid::mc::Type* type, int pointer_level)
1231 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
1233 simgrid::mc::Type* subtype;
1234 simgrid::mc::Type* subsubtype;
1240 switch (type->type) {
1241 case DW_TAG_unspecified_type:
1244 case DW_TAG_base_type:
1245 case DW_TAG_enumeration_type:
1246 case DW_TAG_union_type:
1247 return MC_snapshot_region_memcmp(real_area1, region1, real_area2, region2, type->byte_size) != 0;
1248 case DW_TAG_typedef:
1249 case DW_TAG_volatile_type:
1250 case DW_TAG_const_type:
1252 type = type->subtype;
1253 continue; // restart
1254 case DW_TAG_array_type:
1255 subtype = type->subtype;
1256 switch (subtype->type) {
1257 case DW_TAG_unspecified_type:
1260 case DW_TAG_base_type:
1261 case DW_TAG_enumeration_type:
1262 case DW_TAG_pointer_type:
1263 case DW_TAG_reference_type:
1264 case DW_TAG_rvalue_reference_type:
1265 case DW_TAG_structure_type:
1266 case DW_TAG_class_type:
1267 case DW_TAG_union_type:
1268 if (subtype->full_type)
1269 subtype = subtype->full_type;
1270 elm_size = subtype->byte_size;
1272 case DW_TAG_const_type:
1273 case DW_TAG_typedef:
1274 case DW_TAG_volatile_type:
1275 subsubtype = subtype->subtype;
1276 if (subsubtype->full_type)
1277 subsubtype = subsubtype->full_type;
1278 elm_size = subsubtype->byte_size;
1284 for (i = 0; i < type->element_count; i++) {
1285 size_t off = i * elm_size;
1286 res = compare_areas_with_type(state, process_index, (char*)real_area1 + off, snapshot1, region1,
1287 (char*)real_area2 + off, snapshot2, region2, type->subtype, pointer_level);
1292 case DW_TAG_pointer_type:
1293 case DW_TAG_reference_type:
1294 case DW_TAG_rvalue_reference_type: {
1295 void* addr_pointed1 = MC_region_read_pointer(region1, real_area1);
1296 void* addr_pointed2 = MC_region_read_pointer(region2, real_area2);
1298 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type)
1299 return (addr_pointed1 != addr_pointed2);
1300 if (addr_pointed1 == nullptr && addr_pointed2 == nullptr)
1302 if (addr_pointed1 == nullptr || addr_pointed2 == nullptr)
1304 if (not state.compared_pointers.insert(std::make_pair(addr_pointed1, addr_pointed2)).second)
1309 // Some cases are not handled here:
1310 // * the pointers lead to different areas (one to the heap, the other to the RW segment ...)
1311 // * a pointer leads to the read-only segment of the current object
1312 // * a pointer lead to a different ELF object
1314 if (addr_pointed1 > process->heap_address && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)) {
1315 if (not(addr_pointed2 > process->heap_address && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)))
1317 // The pointers are both in the heap:
1318 return simgrid::mc::compare_heap_area(state, process_index, addr_pointed1, addr_pointed2, snapshot1,
1319 snapshot2, nullptr, type->subtype, pointer_level);
1321 } else if (region1->contain(simgrid::mc::remote(addr_pointed1))) {
1322 // The pointers are both in the current object R/W segment:
1323 if (not region2->contain(simgrid::mc::remote(addr_pointed2)))
1325 if (not type->type_id)
1326 return (addr_pointed1 != addr_pointed2);
1328 return compare_areas_with_type(state, process_index, addr_pointed1, snapshot1, region1, addr_pointed2,
1329 snapshot2, region2, type->subtype, pointer_level);
1332 // TODO, We do not handle very well the case where
1333 // it belongs to a different (non-heap) region from the current one.
1335 return (addr_pointed1 != addr_pointed2);
1339 case DW_TAG_structure_type:
1340 case DW_TAG_class_type:
1341 for (simgrid::mc::Member& member : type->members) {
1342 void* member1 = simgrid::dwarf::resolve_member(real_area1, type, &member, snapshot1, process_index);
1343 void* member2 = simgrid::dwarf::resolve_member(real_area2, type, &member, snapshot2, process_index);
1344 mc_mem_region_t subregion1 = mc_get_region_hinted(member1, snapshot1, process_index, region1);
1345 mc_mem_region_t subregion2 = mc_get_region_hinted(member2, snapshot2, process_index, region2);
1346 res = compare_areas_with_type(state, process_index, member1, snapshot1, subregion1, member2, snapshot2,
1347 subregion2, member.type, pointer_level);
1352 case DW_TAG_subroutine_type:
1356 XBT_VERB("Unknown case: %d", type->type);
1364 static int compare_global_variables(
1365 simgrid::mc::StateComparator& state,
1366 simgrid::mc::ObjectInformation* object_info,
1368 mc_mem_region_t r1, mc_mem_region_t r2,
1369 simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2)
1371 xbt_assert(r1 && r2, "Missing region.");
1374 if (r1->storage_type() == simgrid::mc::StorageType::Privatized) {
1375 xbt_assert(process_index >= 0);
1376 if (r2->storage_type() != simgrid::mc::StorageType::Privatized)
1379 size_t process_count = MC_smpi_process_count();
1380 xbt_assert(process_count == r1->privatized_data().size()
1381 && process_count == r2->privatized_data().size());
1383 // Compare the global variables separately for each simulates process:
1384 for (size_t process_index = 0; process_index < process_count; process_index++) {
1385 if (compare_global_variables(state,
1386 object_info, process_index,
1387 &r1->privatized_data()[process_index],
1388 &r2->privatized_data()[process_index],
1389 snapshot1, snapshot2))
1395 xbt_assert(r1->storage_type() != simgrid::mc::StorageType::Privatized);
1397 xbt_assert(r2->storage_type() != simgrid::mc::StorageType::Privatized);
1399 std::vector<simgrid::mc::Variable>& variables = object_info->global_variables;
1401 for (simgrid::mc::Variable const& current_var : variables) {
1403 // If the variable is not in this object, skip it:
1404 // We do not expect to find a pointer to something which is not reachable
1405 // by the global variables.
1406 if ((char *) current_var.address < (char *) object_info->start_rw
1407 || (char *) current_var.address > (char *) object_info->end_rw)
1410 simgrid::mc::Type* bvariable_type = current_var.type;
1411 int res = compare_areas_with_type(state, process_index,
1412 (char *) current_var.address, snapshot1, r1,
1413 (char *) current_var.address, snapshot2, r2,
1416 XBT_VERB("Global variable %s (%p) is different between snapshots",
1417 current_var.name.c_str(),
1418 (char *) current_var.address);
1426 static int compare_local_variables(simgrid::mc::StateComparator& state,
1428 simgrid::mc::Snapshot* snapshot1,
1429 simgrid::mc::Snapshot* snapshot2,
1430 mc_snapshot_stack_t stack1,
1431 mc_snapshot_stack_t stack2)
1433 if (stack1->local_variables.size() != stack2->local_variables.size()) {
1434 XBT_VERB("Different number of local variables");
1438 unsigned int cursor = 0;
1439 local_variable_t current_var1;
1440 local_variable_t current_var2;
1441 while (cursor < stack1->local_variables.size()) {
1442 current_var1 = &stack1->local_variables[cursor];
1443 current_var2 = &stack1->local_variables[cursor];
1444 if (current_var1->name != current_var2->name
1445 || current_var1->subprogram != current_var2->subprogram
1446 || current_var1->ip != current_var2->ip) {
1447 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1449 ("Different name of variable (%s - %s) "
1450 "or frame (%s - %s) or ip (%lu - %lu)",
1451 current_var1->name.c_str(),
1452 current_var2->name.c_str(),
1453 current_var1->subprogram->name.c_str(),
1454 current_var2->subprogram->name.c_str(),
1455 current_var1->ip, current_var2->ip);
1458 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1460 simgrid::mc::Type* subtype = current_var1->type;
1461 int res = compare_areas_with_type(
1462 state, process_index, current_var1->address, snapshot1,
1463 mc_get_snapshot_region(current_var1->address, snapshot1, process_index), current_var2->address, snapshot2,
1464 mc_get_snapshot_region(current_var2->address, snapshot2, process_index), subtype, 0);
1467 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1468 XBT_VERB("Local variable %s (%p - %p) in frame %s "
1469 "is different between snapshots",
1470 current_var1->name.c_str(), current_var1->address, current_var2->address,
1471 current_var1->subprogram->name.c_str());
1482 static std::unique_ptr<simgrid::mc::StateComparator> state_comparator;
1484 int snapshot_compare(int num1, simgrid::mc::Snapshot* s1, int num2, simgrid::mc::Snapshot* s2)
1486 // TODO, make this a field of ModelChecker or something similar
1488 if (state_comparator == nullptr)
1489 state_comparator = std::unique_ptr<StateComparator>(new StateComparator());
1491 state_comparator->clear();
1493 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
1497 int hash_result = 0;
1499 hash_result = (s1->hash != s2->hash);
1501 XBT_VERB("(%d - %d) Different hash: 0x%" PRIx64 "--0x%" PRIx64, num1, num2, s1->hash, s2->hash);
1506 XBT_VERB("(%d - %d) Same hash: 0x%" PRIx64, num1, num2, s1->hash);
1509 /* Compare enabled processes */
1510 if (s1->enabled_processes != s2->enabled_processes) {
1511 XBT_VERB("(%d - %d) Different amount of enabled processes", num1, num2);
1515 /* Compare size of stacks */
1517 for (unsigned long i = 0; i < s1->stacks.size(); i++) {
1518 size_t size_used1 = s1->stack_sizes[i];
1519 size_t size_used2 = s2->stack_sizes[i];
1520 if (size_used1 != size_used2) {
1522 XBT_DEBUG("(%d - %d) Different size used in stacks: %zu - %zu", num1, num2, size_used1, size_used2);
1527 XBT_VERB("(%d - %d) Different size used in stacks: %zu - %zu", num1, num2, size_used1, size_used2);
1533 if (is_diff) // do not proceed if there is any stacks that don't match
1536 /* Init heap information used in heap comparison algorithm */
1537 xbt_mheap_t heap1 = (xbt_mheap_t)s1->read_bytes(
1538 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1539 remote(process->heap_address),
1540 simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1541 xbt_mheap_t heap2 = (xbt_mheap_t)s2->read_bytes(
1542 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1543 remote(process->heap_address),
1544 simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1545 int res_init = state_comparator->initHeapInformation(heap1, heap2, &s1->to_ignore, &s2->to_ignore);
1547 if (res_init == -1) {
1549 XBT_DEBUG("(%d - %d) Different heap information", num1, num2);
1553 XBT_VERB("(%d - %d) Different heap information", num1, num2);
1560 /* Stacks comparison */
1562 for (unsigned int cursor = 0; cursor < s1->stacks.size(); cursor++) {
1563 mc_snapshot_stack_t stack1 = &s1->stacks[cursor];
1564 mc_snapshot_stack_t stack2 = &s2->stacks[cursor];
1566 if (stack1->process_index != stack2->process_index) {
1568 XBT_DEBUG("(%d - %d) Stacks with different process index (%i vs %i)", num1, num2,
1569 stack1->process_index, stack2->process_index);
1571 else diff_local = compare_local_variables(*state_comparator,
1572 stack1->process_index, s1, s2, stack1, stack2);
1573 if (diff_local > 0) {
1575 XBT_DEBUG("(%d - %d) Different local variables between stacks %d", num1,
1581 XBT_VERB("(%d - %d) Different local variables between stacks %u", num1, num2, cursor + 1);
1589 size_t regions_count = s1->snapshot_regions.size();
1590 // TODO, raise a difference instead?
1591 xbt_assert(regions_count == s2->snapshot_regions.size());
1593 for (size_t k = 0; k != regions_count; ++k) {
1594 mc_mem_region_t region1 = s1->snapshot_regions[k].get();
1595 mc_mem_region_t region2 = s2->snapshot_regions[k].get();
1598 if (region1->region_type() != simgrid::mc::RegionType::Data)
1601 xbt_assert(region1->region_type() == region2->region_type());
1602 xbt_assert(region1->object_info() == region2->object_info());
1603 xbt_assert(region1->object_info());
1605 std::string const& name = region1->object_info()->file_name;
1607 /* Compare global variables */
1608 if (compare_global_variables(*state_comparator, region1->object_info(), simgrid::mc::ProcessIndexDisabled, region1,
1612 XBT_DEBUG("(%d - %d) Different global variables in %s",
1613 num1, num2, name.c_str());
1617 XBT_VERB("(%d - %d) Different global variables in %s",
1618 num1, num2, name.c_str());
1627 if (simgrid::mc::mmalloc_compare_heap(*state_comparator, s1, s2) > 0) {
1630 XBT_DEBUG("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
1635 XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
1642 if (errors || hash_result)
1643 XBT_VERB("(%d - %d) Difference found", num1, num2);
1645 XBT_VERB("(%d - %d) No difference found", num1, num2);
1648 #if defined(MC_DEBUG) && defined(MC_VERBOSE)
1650 // * false positive SHOULD be avoided.
1651 // * There MUST not be any false negative.
1653 XBT_VERB("(%d - %d) State equality hash test is %s %s", num1, num2,
1654 (hash_result != 0) == (errors != 0) ? "true" : "false", not hash_result ? "positive" : "negative");
1658 return errors > 0 || hash_result;