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/sysdep.h>
17 #include <xbt/dynar.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/private.h"
31 #include "src/mc/mc_forward.hpp"
32 #include "src/mc/mc_safety.h"
33 #include "src/mc/mc_private.h"
34 #include "src/mc/mc_smx.h"
35 #include "src/mc/mc_dwarf.hpp"
36 #include "src/mc/Frame.hpp"
37 #include "src/mc/ObjectInformation.hpp"
38 #include "src/mc/Variable.hpp"
39 #include "src/mc/mc_private.h"
40 #include "src/mc/mc_snapshot.h"
41 #include "src/mc/mc_dwarf.hpp"
42 #include "src/mc/Type.hpp"
44 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_compare, xbt, "Logging specific to mc_compare in mc");
50 typedef std::array<HeapLocation, 2> HeapLocationPair;
51 typedef std::set<HeapLocationPair> HeapLocationPairs;
53 struct ProcessComparisonState;
54 struct StateComparator;
56 static int compare_heap_area(
57 StateComparator& state,
58 int process_index, const void *area1, const void* area2,
59 Snapshot* snapshot1, Snapshot* snapshot2,
60 HeapLocationPairs* previous, Type* type, int pointer_level);
65 using simgrid::mc::remote;
67 /*********************************** Heap comparison ***********************************/
68 /***************************************************************************************/
78 HeapLocation(int block, int fragment = 0) : block(block), fragment(fragment) {}
80 bool operator==(HeapLocation const& that) const
82 return block == that.block && fragment == that.fragment;
84 bool operator<(HeapLocation const& that) const
86 return std::make_pair(block, fragment)
87 < std::make_pair(that.block, that.fragment);
92 HeapLocationPair makeHeapLocationPair(int block1, int fragment1, int block2, int fragment2)
94 return simgrid::mc::HeapLocationPair{{
95 simgrid::mc::HeapLocation(block1, fragment1),
96 simgrid::mc::HeapLocation(block2, fragment2)
100 struct HeapArea : public HeapLocation {
106 : valid(true), block(block) {}
107 HeapArea(int block, int fragment = 0)
108 : valid(true), block(block), fragment(fragment) {}
111 struct ProcessComparisonState {
112 std::vector<simgrid::mc::IgnoredHeapRegion>* to_ignore = nullptr;
113 std::vector<HeapArea> equals_to;
114 std::vector<simgrid::mc::Type*> types;
115 std::size_t heapsize = 0;
117 void initHeapInformation(xbt_mheap_t heap,
118 std::vector<simgrid::mc::IgnoredHeapRegion>* i);
123 /** A hash which works with more stuff
125 * It can hash pairs: the standard hash currently doesn't include this.
127 template<class X> struct hash : public std::hash<X> {};
129 template<class X, class Y>
130 struct hash<std::pair<X,Y>> {
131 std::size_t operator()(std::pair<X,Y>const& x) const
135 return h1(x.first) ^ h2(x.second);
142 struct StateComparator {
143 s_xbt_mheap_t std_heap_copy;
144 std::size_t heaplimit;
145 std::array<ProcessComparisonState, 2> processStates;
147 std::unordered_set<std::pair<void*, void*>, hash<std::pair<void*, void*>>> compared_pointers;
151 compared_pointers.clear();
154 int initHeapInformation(
155 xbt_mheap_t heap1, xbt_mheap_t heap2,
156 std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
157 std::vector<simgrid::mc::IgnoredHeapRegion>* i2);
159 HeapArea& equals_to1_(std::size_t i, std::size_t j)
161 return processStates[0].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
163 HeapArea& equals_to2_(std::size_t i, std::size_t j)
165 return processStates[1].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
167 Type*& types1_(std::size_t i, std::size_t j)
169 return processStates[0].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
171 Type*& types2_(std::size_t i, std::size_t j)
173 return processStates[1].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
176 HeapArea const& equals_to1_(std::size_t i, std::size_t j) const
178 return processStates[0].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
180 HeapArea const& equals_to2_(std::size_t i, std::size_t j) const
182 return processStates[1].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
184 Type* const& types1_(std::size_t i, std::size_t j) const
186 return processStates[0].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
188 Type* const& types2_(std::size_t i, std::size_t j) const
190 return processStates[1].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
193 /** Check whether two blocks are known to be matching
195 * @param b1 Block of state 1
196 * @param b2 Block of state 2
197 * @return if the blocks are known to be matching
199 bool blocksEqual(int b1, int b2) const
201 return this->equals_to1_(b1, 0).block == b2
202 && this->equals_to2_(b2, 0).block == b1;
205 /** Check whether two fragments are known to be matching
207 * @param b1 Block of state 1
208 * @param f1 Fragment of state 1
209 * @param b2 Block of state 2
210 * @param f2 Fragment of state 2
211 * @return if the fragments are known to be matching
213 int fragmentsEqual(int b1, int f1, int b2, int f2) const
215 return this->equals_to1_(b1, f1).block == b2
216 && this->equals_to1_(b1, f1).fragment == f2
217 && this->equals_to2_(b2, f2).block == b1
218 && this->equals_to2_(b2, f2).fragment == f1;
221 void match_equals(HeapLocationPairs* list);
227 /************************************************************************************/
229 static ssize_t heap_comparison_ignore_size(
230 std::vector<simgrid::mc::IgnoredHeapRegion>* ignore_list,
234 int end = ignore_list->size() - 1;
236 while (start <= end) {
237 unsigned int cursor = (start + end) / 2;
238 simgrid::mc::IgnoredHeapRegion const& region = (*ignore_list)[cursor];
239 if (region.address == address)
241 if (region.address < address)
243 if (region.address > address)
250 static bool is_stack(const void *address)
252 for (auto const& stack : mc_model_checker->process().stack_areas())
253 if (address == stack.address)
258 // TODO, this should depend on the snapshot?
259 static bool is_block_stack(int block)
261 for (auto const& stack : mc_model_checker->process().stack_areas())
262 if (block == stack.block)
270 void StateComparator::match_equals(HeapLocationPairs* list)
272 for (auto const& pair : *list) {
273 if (pair[0].fragment != -1) {
274 this->equals_to1_(pair[0].block, pair[0].fragment) = simgrid::mc::HeapArea(pair[1].block, pair[1].fragment);
275 this->equals_to2_(pair[1].block, pair[1].fragment) = simgrid::mc::HeapArea(pair[0].block, pair[0].fragment);
277 this->equals_to1_(pair[0].block, 0) = simgrid::mc::HeapArea(pair[1].block, pair[1].fragment);
278 this->equals_to2_(pair[1].block, 0) = simgrid::mc::HeapArea(pair[0].block, pair[0].fragment);
283 void ProcessComparisonState::initHeapInformation(xbt_mheap_t heap,
284 std::vector<simgrid::mc::IgnoredHeapRegion>* i)
286 auto heaplimit = heap->heaplimit;
287 this->heapsize = heap->heapsize;
289 this->equals_to.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, HeapArea());
290 this->types.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, nullptr);
293 int StateComparator::initHeapInformation(xbt_mheap_t heap1, xbt_mheap_t heap2,
294 std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
295 std::vector<simgrid::mc::IgnoredHeapRegion>* i2)
297 if ((heap1->heaplimit != heap2->heaplimit) || (heap1->heapsize != heap2->heapsize))
299 this->heaplimit = heap1->heaplimit;
300 this->std_heap_copy = *mc_model_checker->process().get_heap();
301 this->processStates[0].initHeapInformation(heap1, i1);
302 this->processStates[1].initHeapInformation(heap2, i2);
306 // TODO, have a robust way to find it in O(1)
308 mc_mem_region_t MC_get_heap_region(simgrid::mc::Snapshot* snapshot)
310 for (auto& region : snapshot->snapshot_regions)
311 if (region->region_type() == simgrid::mc::RegionType::Heap)
313 xbt_die("No heap region");
317 int mmalloc_compare_heap(
318 simgrid::mc::StateComparator& state, simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2)
320 simgrid::mc::Process* process = &mc_model_checker->process();
322 /* 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 (std::uint64_t)heapinfo_address, simgrid::mc::ProcessIndexMissing);
348 const malloc_info* heapinfos2 = snapshot2->read<malloc_info*>(
349 (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 void* 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) {
388 /* Try first to associate to same block in the other heap */
389 if (heapinfo2->type == heapinfo1->type
390 && state.equals_to2_(i1, 0).valid == 0) {
391 addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
392 res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing, addr_block1, addr_block2, snapshot1,
393 snapshot2, nullptr, nullptr, 0);
394 if (res_compare != 1) {
395 for (k = 1; k < heapinfo2->busy_block.size; k++)
396 state.equals_to2_(i1 + k, 0) = HeapArea(i1, -1);
397 for (k = 1; k < heapinfo1->busy_block.size; k++)
398 state.equals_to1_(i1 + k, 0) = HeapArea(i1, -1);
400 i1 += heapinfo1->busy_block.size;
404 while (i2 < state.heaplimit && !equal) {
406 addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
413 const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2b, &heapinfos2[i2], sizeof(malloc_info));
415 if (heapinfo2b->type != MMALLOC_TYPE_UNFRAGMENTED) {
420 if (state.equals_to2_(i2, 0).valid) {
425 res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing,
426 addr_block1, addr_block2, snapshot1, snapshot2,
427 nullptr, nullptr, 0);
429 if (res_compare != 1) {
430 for (k = 1; k < heapinfo2b->busy_block.size; k++)
431 state.equals_to2_(i2 + k, 0) = HeapArea(i1, -1);
432 for (k = 1; k < heapinfo1->busy_block.size; k++)
433 state.equals_to1_(i1 + k, 0) = HeapArea(i2, -1);
435 i1 += heapinfo1->busy_block.size;
442 XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1, heapinfo1->busy_block.busy_size, addr_block1);
443 i1 = state.heaplimit + 1;
448 } else { /* Fragmented block */
450 for (j1 = 0; j1 < (size_t) (BLOCKSIZE >> heapinfo1->type); j1++) {
452 if (heapinfo1->busy_frag.frag_size[j1] == -1) /* Free fragment */
455 if (state.equals_to1_(i1, j1).valid)
458 addr_frag1 = (void*)((char*)addr_block1 + (j1 << heapinfo1->type));
463 /* Try first to associate to same fragment in the other heap */
464 if (heapinfo2->type == heapinfo1->type
465 && !state.equals_to2_(i1, j1).valid) {
466 addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE +
467 (char *) state.std_heap_copy.heapbase;
469 (void *) ((char *) addr_block2 +
470 (j1 << heapinfo2->type));
471 res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing, addr_frag1, addr_frag2, snapshot1,
472 snapshot2, nullptr, nullptr, 0);
473 if (res_compare != 1)
477 while (i2 < state.heaplimit && !equal) {
479 const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(
480 heap_region2, &heapinfo_temp2b, &heapinfos2[i2],
481 sizeof(malloc_info));
483 if (heapinfo2b->type == MMALLOC_TYPE_FREE || heapinfo2b->type == MMALLOC_TYPE_HEAPINFO) {
488 // We currently do not match fragments with unfragmented blocks (maybe we should).
489 if (heapinfo2b->type == MMALLOC_TYPE_UNFRAGMENTED) {
494 if (heapinfo2b->type < 0) {
495 fprintf(stderr, "Unknown mmalloc block type.\n");
499 for (j2 = 0; j2 < (size_t) (BLOCKSIZE >> heapinfo2b->type);
502 if (i2 == i1 && j2 == j1)
505 if (state.equals_to2_(i2, j2).valid)
508 addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
509 addr_frag2 = (void*)((char*)addr_block2 + (j2 << heapinfo2b->type));
511 res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing, addr_frag1, addr_frag2, snapshot2,
512 snapshot2, nullptr, nullptr, 0);
513 if (res_compare != 1) {
523 XBT_DEBUG("Block %zu, fragment %zu not found (size_used = %zd, address = %p)\n", i1, j1,
524 heapinfo1->busy_frag.frag_size[j1], addr_frag1);
525 i2 = state.heaplimit + 1;
526 i1 = state.heaplimit + 1;
536 /* All blocks/fragments are equal to another block/fragment ? */
540 for(i = 1; i < state.heaplimit; i++) {
541 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
542 heap_region1, &heapinfo_temp1, &heapinfos1[i], sizeof(malloc_info));
544 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED && i1 == state.heaplimit && heapinfo1->busy_block.busy_size > 0 &&
545 !state.equals_to1_(i, 0).valid) {
546 XBT_DEBUG("Block %zu not found (size used = %zu)", i, heapinfo1->busy_block.busy_size);
550 if (heapinfo1->type <= 0)
552 for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo1->type); j++)
553 if (i1 == state.heaplimit && heapinfo1->busy_frag.frag_size[j] > 0 && !state.equals_to1_(i, j).valid) {
554 XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)", i, j, heapinfo1->busy_frag.frag_size[j]);
559 if (i1 == state.heaplimit)
560 XBT_DEBUG("Number of blocks/fragments not found in heap1: %d", nb_diff1);
562 for (i=1; i < state.heaplimit; i++) {
563 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
564 heap_region2, &heapinfo_temp2, &heapinfos2[i], sizeof(malloc_info));
565 if (heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED
566 && i1 == state.heaplimit
567 && heapinfo2->busy_block.busy_size > 0
568 && !state.equals_to2_(i, 0).valid) {
569 XBT_DEBUG("Block %zu not found (size used = %zu)", i,
570 heapinfo2->busy_block.busy_size);
574 if (heapinfo2->type <= 0)
577 for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo2->type); j++)
578 if (i1 == state.heaplimit
579 && heapinfo2->busy_frag.frag_size[j] > 0
580 && !state.equals_to2_(i, j).valid) {
581 XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)",
582 i, j, heapinfo2->busy_frag.frag_size[j]);
588 if (i1 == state.heaplimit)
589 XBT_DEBUG("Number of blocks/fragments not found in heap2: %d", nb_diff2);
591 return nb_diff1 > 0 || nb_diff2 > 0;
597 * @param real_area1 Process address for state 1
598 * @param real_area2 Process address for state 2
599 * @param snapshot1 Snapshot of state 1
600 * @param snapshot2 Snapshot of state 2
603 * @param check_ignore
605 static int compare_heap_area_without_type(
606 simgrid::mc::StateComparator& state, int process_index,
607 const void *real_area1, const void *real_area2,
608 simgrid::mc::Snapshot* snapshot1,
609 simgrid::mc::Snapshot* snapshot2,
610 HeapLocationPairs* previous, int size,
613 simgrid::mc::Process* process = &mc_model_checker->process();
614 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
615 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
617 for (int i = 0; i < size; ) {
619 if (check_ignore > 0) {
620 ssize_t ignore1 = heap_comparison_ignore_size(
621 state.processStates[0].to_ignore, (char *) real_area1 + i);
623 ssize_t ignore2 = heap_comparison_ignore_size(
624 state.processStates[1].to_ignore, (char *) real_area2 + i);
625 if (ignore2 == ignore1) {
638 if (MC_snapshot_region_memcmp(((char *) real_area1) + i, heap_region1, ((char *) real_area2) + i, heap_region2, 1) != 0) {
640 int pointer_align = (i / sizeof(void *)) * sizeof(void *);
641 const void* addr_pointed1 = snapshot1->read(
642 remote((void**)((char *) real_area1 + pointer_align)), process_index);
643 const void* addr_pointed2 = snapshot2->read(
644 remote((void**)((char *) real_area2 + pointer_align)), process_index);
646 if (process->in_maestro_stack(remote(addr_pointed1))
647 && process->in_maestro_stack(remote(addr_pointed2))) {
648 i = pointer_align + sizeof(void *);
652 if (addr_pointed1 > state.std_heap_copy.heapbase
653 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
654 && addr_pointed2 > state.std_heap_copy.heapbase
655 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)) {
656 // Both addreses are in the heap:
657 int res_compare = compare_heap_area(state ,process_index,
658 addr_pointed1, addr_pointed2,
659 snapshot1, snapshot2, previous, nullptr, 0);
660 if (res_compare == 1)
662 i = pointer_align + sizeof(void *);
678 * @param real_area1 Process address for state 1
679 * @param real_area2 Process address for state 2
680 * @param snapshot1 Snapshot of state 1
681 * @param snapshot2 Snapshot of state 2
684 * @param area_size either a byte_size or an elements_count (?)
685 * @param check_ignore
686 * @param pointer_level
687 * @return 0 (same), 1 (different), -1 (unknown)
689 static int compare_heap_area_with_type(
690 simgrid::mc::StateComparator& state, int process_index,
691 const void *real_area1, const void *real_area2,
692 simgrid::mc::Snapshot* snapshot1,
693 simgrid::mc::Snapshot* snapshot2,
694 HeapLocationPairs* previous, simgrid::mc::Type* type,
695 int area_size, int check_ignore,
700 // HACK: This should not happen but in pratice, there are some
701 // DW_TAG_typedef without an associated DW_AT_type:
702 //<1><538832>: Abbrev Number: 111 (DW_TAG_typedef)
703 // <538833> DW_AT_name : (indirect string, offset: 0x2292f3): gregset_t
704 // <538837> DW_AT_decl_file : 98
705 // <538838> DW_AT_decl_line : 37
709 if (is_stack(real_area1) && is_stack(real_area2))
712 if (check_ignore > 0) {
713 ssize_t ignore1 = heap_comparison_ignore_size(
714 state.processStates[0].to_ignore, real_area1);
716 && heap_comparison_ignore_size(
717 state.processStates[1].to_ignore, real_area2) == ignore1)
721 simgrid::mc::Type* subtype;
722 simgrid::mc::Type* subsubtype;
725 const void* addr_pointed1;
726 const void* addr_pointed2;
728 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
729 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
731 switch (type->type) {
732 case DW_TAG_unspecified_type:
735 case DW_TAG_base_type:
736 if (!type->name.empty() && type->name == "char") { /* String, hence random (arbitrary ?) size */
737 if (real_area1 == real_area2)
740 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, area_size) != 0;
742 if (area_size != -1 && type->byte_size != area_size)
745 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
749 case DW_TAG_enumeration_type:
750 if (area_size != -1 && type->byte_size != area_size)
752 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
755 case DW_TAG_const_type:
756 case DW_TAG_volatile_type:
758 type = type->subtype;
761 case DW_TAG_array_type:
762 subtype = type->subtype;
763 switch (subtype->type) {
764 case DW_TAG_unspecified_type:
767 case DW_TAG_base_type:
768 case DW_TAG_enumeration_type:
769 case DW_TAG_pointer_type:
770 case DW_TAG_reference_type:
771 case DW_TAG_rvalue_reference_type:
772 case DW_TAG_structure_type:
773 case DW_TAG_class_type:
774 case DW_TAG_union_type:
775 if (subtype->full_type)
776 subtype = subtype->full_type;
777 elm_size = subtype->byte_size;
779 // TODO, just remove the type indirection?
780 case DW_TAG_const_type:
782 case DW_TAG_volatile_type:
783 subsubtype = subtype->subtype;
784 if (subsubtype->full_type)
785 subsubtype = subsubtype->full_type;
786 elm_size = subsubtype->byte_size;
792 for (int i = 0; i < type->element_count; i++) {
793 // TODO, add support for variable stride (DW_AT_byte_stride)
795 compare_heap_area_with_type(state, process_index,
796 (char *) real_area1 + (i * elm_size),
797 (char *) real_area2 + (i * elm_size),
798 snapshot1, snapshot2, previous,
799 type->subtype, subtype->byte_size,
800 check_ignore, pointer_level);
806 case DW_TAG_reference_type:
807 case DW_TAG_rvalue_reference_type:
808 case DW_TAG_pointer_type:
809 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
810 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
811 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
812 return (addr_pointed1 != addr_pointed2);
815 if (pointer_level <= 1) {
816 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
817 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
818 if (addr_pointed1 > state.std_heap_copy.heapbase
819 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
820 && addr_pointed2 > state.std_heap_copy.heapbase
821 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
822 return compare_heap_area(state, process_index,
823 addr_pointed1, addr_pointed2, snapshot1,
824 snapshot2, previous, type->subtype,
827 return (addr_pointed1 != addr_pointed2);
829 for (size_t i = 0; i < (area_size / sizeof(void *)); i++) {
830 addr_pointed1 = snapshot1->read(
831 remote((void**)((char*) real_area1 + i * sizeof(void *))),
833 addr_pointed2 = snapshot2->read(
834 remote((void**)((char*) real_area2 + i * sizeof(void *))),
836 if (addr_pointed1 > state.std_heap_copy.heapbase
837 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
838 && addr_pointed2 > state.std_heap_copy.heapbase
839 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
841 compare_heap_area(state, process_index,
842 addr_pointed1, addr_pointed2, snapshot1,
843 snapshot2, previous, type->subtype,
846 res = (addr_pointed1 != addr_pointed2);
852 case DW_TAG_structure_type:
853 case DW_TAG_class_type:
855 type = type->full_type;
856 if (area_size != -1 && type->byte_size != area_size) {
857 if (area_size <= type->byte_size || area_size % type->byte_size != 0)
859 for (size_t i = 0; i < (size_t)(area_size / type->byte_size); i++) {
860 int res = compare_heap_area_with_type(state, process_index,
861 (char *) real_area1 + i * type->byte_size,
862 (char *) real_area2 + i * type->byte_size,
863 snapshot1, snapshot2, previous, type, -1,
869 for(simgrid::mc::Member& member : type->members) {
870 // TODO, optimize this? (for the offset case)
871 void *real_member1 = simgrid::dwarf::resolve_member(
872 real_area1, type, &member, (simgrid::mc::AddressSpace*) snapshot1, process_index);
873 void *real_member2 = simgrid::dwarf::resolve_member(
874 real_area2, type, &member, (simgrid::mc::AddressSpace*) snapshot2, process_index);
875 int res = compare_heap_area_with_type(
876 state, process_index, real_member1, real_member2,
877 snapshot1, snapshot2,
878 previous, member.type, -1,
886 case DW_TAG_union_type:
887 return compare_heap_area_without_type(state, process_index, real_area1, real_area2,
888 snapshot1, snapshot2, previous,
889 type->byte_size, check_ignore);
895 xbt_die("Unreachable");
898 /** Infer the type of a part of the block from the type of the block
900 * TODO, handle DW_TAG_array_type as well as arrays of the object ((*p)[5], p[5])
902 * TODO, handle subfields ((*p).bar.foo, (*p)[5].bar…)
904 * @param type DWARF type ID of the root address
906 * @return DWARF type ID for given offset
908 static simgrid::mc::Type* get_offset_type(void *real_base_address, simgrid::mc::Type* type,
909 int offset, int area_size,
910 simgrid::mc::Snapshot* snapshot, int process_index)
913 // Beginning of the block, the infered variable type if the type of the block:
917 switch (type->type) {
919 case DW_TAG_structure_type:
920 case DW_TAG_class_type:
922 type = type->full_type;
923 if (area_size != -1 && type->byte_size != area_size) {
924 if (area_size > type->byte_size && area_size % type->byte_size == 0)
930 for(simgrid::mc::Member& member : type->members) {
931 if (member.has_offset_location()) {
932 // We have the offset, use it directly (shortcut):
933 if (member.offset() == offset)
936 void* real_member = simgrid::dwarf::resolve_member(real_base_address, type, &member, snapshot, process_index);
937 if ((char*)real_member - (char*)real_base_address == offset)
944 /* FIXME: other cases ? */
952 * @param area1 Process address for state 1
953 * @param area2 Process address for state 2
954 * @param snapshot1 Snapshot of state 1
955 * @param snapshot2 Snapshot of state 2
956 * @param previous Pairs of blocks already compared on the current path (or nullptr)
957 * @param type_id Type of variable
958 * @param pointer_level
959 * @return 0 (same), 1 (different), -1
962 int compare_heap_area(simgrid::mc::StateComparator& state, int process_index,
963 const void *area1, const void *area2,
964 simgrid::mc::Snapshot* snapshot1,
965 simgrid::mc::Snapshot* snapshot2,
966 HeapLocationPairs* previous,
967 simgrid::mc::Type* type, int pointer_level)
969 simgrid::mc::Process* process = &mc_model_checker->process();
977 int check_ignore = 0;
979 void* real_addr_block1;
980 void* real_addr_block2;
981 void* real_addr_frag1;
982 void* real_addr_frag2;
989 simgrid::mc::Type* new_type1 = nullptr;
990 simgrid::mc::Type* new_type2 = nullptr;
992 bool match_pairs = false;
994 // This is the address of std_heap->heapinfo in the application process:
995 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
997 const malloc_info* heapinfos1 = snapshot1->read(remote((const malloc_info**)heapinfo_address), process_index);
998 const malloc_info* heapinfos2 = snapshot2->read(remote((const malloc_info**)heapinfo_address), process_index);
1000 malloc_info heapinfo_temp1, heapinfo_temp2;
1002 simgrid::mc::HeapLocationPairs current;
1003 if (previous == nullptr) {
1004 previous = ¤t;
1008 // Get block number:
1009 block1 = ((char*)area1 - (char*)state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
1010 block2 = ((char*)area2 - (char*)state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
1012 // If either block is a stack block:
1013 if (is_block_stack((int) block1) && is_block_stack((int) block2)) {
1014 previous->insert(simgrid::mc::makeHeapLocationPair(block1, -1, block2, -1));
1016 state.match_equals(previous);
1020 // If either block is not in the expected area of memory:
1021 if (((char*)area1 < (char*)state.std_heap_copy.heapbase) || (block1 > (ssize_t)state.processStates[0].heapsize) ||
1022 (block1 < 1) || ((char*)area2 < (char*)state.std_heap_copy.heapbase) ||
1023 (block2 > (ssize_t)state.processStates[1].heapsize) || (block2 < 1)) {
1027 // Process address of the block:
1028 real_addr_block1 = (ADDR2UINT(block1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
1029 real_addr_block2 = (ADDR2UINT(block2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
1032 if (type->full_type)
1033 type = type->full_type;
1035 // This assume that for "boring" types (volatile ...) byte_size is absent:
1036 while (type->byte_size == 0 && type->subtype != nullptr)
1037 type = type->subtype;
1040 if (type->type == DW_TAG_pointer_type ||
1041 (type->type == DW_TAG_base_type && !type->name.empty() && type->name == "char"))
1044 type_size = type->byte_size;
1048 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
1049 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
1051 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
1052 heap_region1, &heapinfo_temp1, &heapinfos1[block1], sizeof(malloc_info));
1053 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
1054 heap_region2, &heapinfo_temp2, &heapinfos2[block2], sizeof(malloc_info));
1056 if ((heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type==MMALLOC_TYPE_HEAPINFO)
1057 && (heapinfo2->type == MMALLOC_TYPE_FREE || heapinfo2->type ==MMALLOC_TYPE_HEAPINFO)) {
1060 state.match_equals(previous);
1064 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED && heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
1065 /* Complete block */
1067 // TODO, lookup variable type from block type as done for fragmented blocks
1069 offset1 = (char*)area1 - (char*)real_addr_block1;
1070 offset2 = (char*)area2 - (char*)real_addr_block2;
1072 if (state.equals_to1_(block1, 0).valid && state.equals_to2_(block2, 0).valid && state.blocksEqual(block1, block2)) {
1074 state.match_equals(previous);
1078 if (type_size != -1) {
1079 if (type_size != (ssize_t) heapinfo1->busy_block.busy_size
1080 && type_size != (ssize_t) heapinfo2->busy_block.busy_size
1081 && (type->name.empty() || type->name == "struct s_smx_context")) {
1083 state.match_equals(previous);
1088 if (heapinfo1->busy_block.size != heapinfo2->busy_block.size)
1090 if (heapinfo1->busy_block.busy_size != heapinfo2->busy_block.busy_size)
1093 if (!previous->insert(simgrid::mc::makeHeapLocationPair(block1, -1, block2, -1)).second) {
1095 state.match_equals(previous);
1099 size = heapinfo1->busy_block.busy_size;
1101 // Remember (basic) type inference.
1102 // The current data structure only allows us to do this for the whole block.
1103 if (type != nullptr && area1 == real_addr_block1)
1104 state.types1_(block1, 0) = type;
1105 if (type != nullptr && area2 == real_addr_block2)
1106 state.types2_(block2, 0) = type;
1110 state.match_equals(previous);
1117 if (heapinfo1->busy_block.ignore > 0
1118 && heapinfo2->busy_block.ignore == heapinfo1->busy_block.ignore)
1119 check_ignore = heapinfo1->busy_block.ignore;
1121 } else if ((heapinfo1->type > 0) && (heapinfo2->type > 0)) { /* Fragmented block */
1124 frag1 = ((uintptr_t)(ADDR2UINT(area1) % (BLOCKSIZE))) >> heapinfo1->type;
1125 frag2 = ((uintptr_t)(ADDR2UINT(area2) % (BLOCKSIZE))) >> heapinfo2->type;
1127 // Process address of the fragment:
1128 real_addr_frag1 = (void*)((char*)real_addr_block1 + (frag1 << heapinfo1->type));
1129 real_addr_frag2 = (void*)((char*)real_addr_block2 + (frag2 << heapinfo2->type));
1131 // Check the size of the fragments against the size of the type:
1132 if (type_size != -1) {
1133 if (heapinfo1->busy_frag.frag_size[frag1] == -1 || heapinfo2->busy_frag.frag_size[frag2] == -1) {
1135 state.match_equals(previous);
1139 if (type_size != heapinfo1->busy_frag.frag_size[frag1]
1140 || type_size != heapinfo2->busy_frag.frag_size[frag2]) {
1142 state.match_equals(previous);
1147 // Check if the blocks are already matched together:
1148 if (state.equals_to1_(block1, frag1).valid && state.equals_to2_(block2, frag2).valid) {
1149 if (offset1==offset2 && state.fragmentsEqual(block1, frag1, block2, frag2)) {
1151 state.match_equals(previous);
1155 // Compare the size of both fragments:
1156 if (heapinfo1->busy_frag.frag_size[frag1] != heapinfo2->busy_frag.frag_size[frag2]) {
1157 if (type_size == -1) {
1159 state.match_equals(previous);
1165 // Size of the fragment:
1166 size = heapinfo1->busy_frag.frag_size[frag1];
1168 // Remember (basic) type inference.
1169 // The current data structure only allows us to do this for the whole fragment.
1170 if (type != nullptr && area1 == real_addr_frag1)
1171 state.types1_(block1, frag1) = type;
1172 if (type != nullptr && area2 == real_addr_frag2)
1173 state.types2_(block2, frag2) = type;
1175 // The type of the variable is already known:
1180 // Type inference from the block type.
1181 else if (state.types1_(block1, frag1) != nullptr || state.types2_(block2, frag2) != nullptr) {
1183 offset1 = (char*)area1 - (char*)real_addr_frag1;
1184 offset2 = (char*)area2 - (char*)real_addr_frag2;
1186 if (state.types1_(block1, frag1) != nullptr && state.types2_(block2, frag2) != nullptr) {
1188 get_offset_type(real_addr_frag1, state.types1_(block1, frag1), offset1, size, snapshot1, process_index);
1190 get_offset_type(real_addr_frag2, state.types2_(block2, frag2), offset1, size, snapshot2, process_index);
1191 } else if (state.types1_(block1, frag1) != nullptr) {
1193 get_offset_type(real_addr_frag1, state.types1_(block1, frag1), offset1, size, snapshot1, process_index);
1195 get_offset_type(real_addr_frag2, state.types1_(block1, frag1), offset2, size, snapshot2, process_index);
1196 } else if (state.types2_(block2, frag2) != nullptr) {
1198 get_offset_type(real_addr_frag1, state.types2_(block2, frag2), offset1, size, snapshot1, process_index);
1200 get_offset_type(real_addr_frag2, state.types2_(block2, frag2), offset2, size, snapshot2, process_index);
1203 state.match_equals(previous);
1207 if (new_type1 != nullptr && new_type2 != nullptr && new_type1 != new_type2) {
1210 while (type->byte_size == 0 && type->subtype != nullptr)
1211 type = type->subtype;
1212 new_size1 = type->byte_size;
1215 while (type->byte_size == 0 && type->subtype != nullptr)
1216 type = type->subtype;
1217 new_size2 = type->byte_size;
1221 state.match_equals(previous);
1226 if (new_size1 > 0 && new_size1 == new_size2) {
1231 if (offset1 == 0 && offset2 == 0 &&
1232 !previous->insert(simgrid::mc::makeHeapLocationPair(block1, frag1, block2, frag2)).second) {
1234 state.match_equals(previous);
1240 state.match_equals(previous);
1244 if ((heapinfo1->busy_frag.ignore[frag1] > 0) &&
1245 (heapinfo2->busy_frag.ignore[frag2] == heapinfo1->busy_frag.ignore[frag1]))
1246 check_ignore = heapinfo1->busy_frag.ignore[frag1];
1252 /* Start comparison */
1254 res_compare = compare_heap_area_with_type(state, process_index, area1, area2, snapshot1, snapshot2, previous, type,
1255 size, check_ignore, pointer_level);
1257 res_compare = compare_heap_area_without_type(state, process_index, area1, area2, snapshot1, snapshot2, previous,
1258 size, check_ignore);
1260 if (res_compare == 1)
1264 state.match_equals(previous);
1271 /************************** Snapshot comparison *******************************/
1272 /******************************************************************************/
1274 static int compare_areas_with_type(simgrid::mc::StateComparator& state,
1276 void* real_area1, simgrid::mc::Snapshot* snapshot1, mc_mem_region_t region1,
1277 void* real_area2, simgrid::mc::Snapshot* snapshot2, mc_mem_region_t region2,
1278 simgrid::mc::Type* type, int pointer_level)
1280 simgrid::mc::Process* process = &mc_model_checker->process();
1282 simgrid::mc::Type* subtype;
1283 simgrid::mc::Type* subsubtype;
1289 switch (type->type) {
1290 case DW_TAG_unspecified_type:
1293 case DW_TAG_base_type:
1294 case DW_TAG_enumeration_type:
1295 case DW_TAG_union_type:
1296 return MC_snapshot_region_memcmp(real_area1, region1, real_area2, region2, type->byte_size) != 0;
1297 case DW_TAG_typedef:
1298 case DW_TAG_volatile_type:
1299 case DW_TAG_const_type:
1301 type = type->subtype;
1303 case DW_TAG_array_type:
1304 subtype = type->subtype;
1305 switch (subtype->type) {
1306 case DW_TAG_unspecified_type:
1309 case DW_TAG_base_type:
1310 case DW_TAG_enumeration_type:
1311 case DW_TAG_pointer_type:
1312 case DW_TAG_reference_type:
1313 case DW_TAG_rvalue_reference_type:
1314 case DW_TAG_structure_type:
1315 case DW_TAG_class_type:
1316 case DW_TAG_union_type:
1317 if (subtype->full_type)
1318 subtype = subtype->full_type;
1319 elm_size = subtype->byte_size;
1321 case DW_TAG_const_type:
1322 case DW_TAG_typedef:
1323 case DW_TAG_volatile_type:
1324 subsubtype = subtype->subtype;
1325 if (subsubtype->full_type)
1326 subsubtype = subsubtype->full_type;
1327 elm_size = subsubtype->byte_size;
1333 for (i = 0; i < type->element_count; i++) {
1334 size_t off = i * elm_size;
1335 res = compare_areas_with_type(state, process_index,
1336 (char*) real_area1 + off, snapshot1, region1,
1337 (char*) real_area2 + off, snapshot2, region2,
1338 type->subtype, pointer_level);
1343 case DW_TAG_pointer_type:
1344 case DW_TAG_reference_type:
1345 case DW_TAG_rvalue_reference_type:
1347 void* addr_pointed1 = MC_region_read_pointer(region1, real_area1);
1348 void* addr_pointed2 = MC_region_read_pointer(region2, real_area2);
1350 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type)
1351 return (addr_pointed1 != addr_pointed2);
1352 if (addr_pointed1 == nullptr && addr_pointed2 == nullptr)
1354 if (addr_pointed1 == nullptr || addr_pointed2 == nullptr)
1356 if (!state.compared_pointers.insert(
1357 std::make_pair(addr_pointed1, addr_pointed2)).second)
1362 // Some cases are not handled here:
1363 // * the pointers lead to different areas (one to the heap, the other to the RW segment ...);
1364 // * a pointer leads to the read-only segment of the current object;
1365 // * a pointer lead to a different ELF object.
1367 if (addr_pointed1 > process->heap_address
1368 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)) {
1370 (addr_pointed2 > process->heap_address
1371 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)))
1373 // The pointers are both in the heap:
1374 return simgrid::mc::compare_heap_area(state,
1375 process_index, addr_pointed1, addr_pointed2, snapshot1,
1376 snapshot2, nullptr, type->subtype, pointer_level);
1379 // The pointers are both in the current object R/W segment:
1380 else if (region1->contain(simgrid::mc::remote(addr_pointed1))) {
1381 if (!region2->contain(simgrid::mc::remote(addr_pointed2)))
1384 return (addr_pointed1 != addr_pointed2);
1386 return compare_areas_with_type(state, process_index,
1387 addr_pointed1, snapshot1, region1,
1388 addr_pointed2, snapshot2, region2,
1389 type->subtype, pointer_level);
1392 // TODO, We do not handle very well the case where
1393 // it belongs to a different (non-heap) region from the current one.
1396 return (addr_pointed1 != addr_pointed2);
1400 case DW_TAG_structure_type:
1401 case DW_TAG_class_type:
1402 for(simgrid::mc::Member& member : type->members) {
1403 void *member1 = simgrid::dwarf::resolve_member(
1404 real_area1, type, &member, snapshot1, process_index);
1405 void *member2 = simgrid::dwarf::resolve_member(
1406 real_area2, type, &member, snapshot2, process_index);
1407 mc_mem_region_t subregion1 = mc_get_region_hinted(member1, snapshot1, process_index, region1);
1408 mc_mem_region_t subregion2 = mc_get_region_hinted(member2, snapshot2, process_index, region2);
1410 compare_areas_with_type(state, process_index,
1411 member1, snapshot1, subregion1,
1412 member2, snapshot2, subregion2,
1413 member.type, pointer_level);
1418 case DW_TAG_subroutine_type:
1422 XBT_VERB("Unknown case: %d", type->type);
1429 static int compare_global_variables(
1430 simgrid::mc::StateComparator& state,
1431 simgrid::mc::ObjectInformation* object_info,
1433 mc_mem_region_t r1, mc_mem_region_t r2,
1434 simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2)
1436 xbt_assert(r1 && r2, "Missing region.");
1439 if (r1->storage_type() == simgrid::mc::StorageType::Privatized) {
1440 xbt_assert(process_index >= 0);
1441 if (r2->storage_type() != simgrid::mc::StorageType::Privatized)
1444 size_t process_count = MC_smpi_process_count();
1445 xbt_assert(process_count == r1->privatized_data().size()
1446 && process_count == r2->privatized_data().size());
1448 // Compare the global variables separately for each simulates process:
1449 for (size_t process_index = 0; process_index < process_count; process_index++) {
1450 if (compare_global_variables(state,
1451 object_info, process_index,
1452 &r1->privatized_data()[process_index],
1453 &r2->privatized_data()[process_index],
1454 snapshot1, snapshot2))
1460 xbt_assert(r1->storage_type() != simgrid::mc::StorageType::Privatized);
1462 xbt_assert(r2->storage_type() != simgrid::mc::StorageType::Privatized);
1464 std::vector<simgrid::mc::Variable>& variables = object_info->global_variables;
1466 for (simgrid::mc::Variable& current_var : variables) {
1468 // If the variable is not in this object, skip it:
1469 // We do not expect to find a pointer to something which is not reachable
1470 // by the global variables.
1471 if ((char *) current_var.address < (char *) object_info->start_rw
1472 || (char *) current_var.address > (char *) object_info->end_rw)
1475 simgrid::mc::Type* bvariable_type = current_var.type;
1476 int res = compare_areas_with_type(state, process_index,
1477 (char *) current_var.address, snapshot1, r1,
1478 (char *) current_var.address, snapshot2, r2,
1481 XBT_VERB("Global variable %s (%p) is different between snapshots",
1482 current_var.name.c_str(),
1483 (char *) current_var.address);
1493 static int compare_local_variables(simgrid::mc::StateComparator& state,
1495 simgrid::mc::Snapshot* snapshot1,
1496 simgrid::mc::Snapshot* snapshot2,
1497 mc_snapshot_stack_t stack1,
1498 mc_snapshot_stack_t stack2)
1500 if (stack1->local_variables.size() != stack2->local_variables.size()) {
1501 XBT_VERB("Different number of local variables");
1505 unsigned int cursor = 0;
1506 local_variable_t current_var1, current_var2;
1508 while (cursor < stack1->local_variables.size()) {
1509 current_var1 = &stack1->local_variables[cursor];
1510 current_var2 = &stack1->local_variables[cursor];
1511 if (current_var1->name != current_var2->name
1512 || current_var1->subprogram != current_var2->subprogram
1513 || current_var1->ip != current_var2->ip) {
1514 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1516 ("Different name of variable (%s - %s) "
1517 "or frame (%s - %s) or ip (%lu - %lu)",
1518 current_var1->name.c_str(),
1519 current_var2->name.c_str(),
1520 current_var1->subprogram->name.c_str(),
1521 current_var2->subprogram->name.c_str(),
1522 current_var1->ip, current_var2->ip);
1525 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1527 simgrid::mc::Type* subtype = current_var1->type;
1529 compare_areas_with_type(state, process_index,
1530 current_var1->address, snapshot1, mc_get_snapshot_region(current_var1->address, snapshot1, process_index),
1531 current_var2->address, snapshot2, mc_get_snapshot_region(current_var2->address, snapshot2, process_index),
1535 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1537 ("Local variable %s (%p - %p) in frame %s "
1538 "is different between snapshots",
1539 current_var1->name.c_str(),
1540 current_var1->address,
1541 current_var2->address,
1542 current_var1->subprogram->name.c_str());
1553 static std::unique_ptr<simgrid::mc::StateComparator> state_comparator;
1555 int snapshot_compare(int num1, simgrid::mc::Snapshot* s1, int num2, simgrid::mc::Snapshot* s2)
1557 // TODO, make this a field of ModelChecker or something similar
1559 if (state_comparator == nullptr)
1560 state_comparator = std::unique_ptr<StateComparator>(new StateComparator());
1562 state_comparator->clear();
1564 simgrid::mc::Process* process = &mc_model_checker->process();
1568 int hash_result = 0;
1570 hash_result = (s1->hash != s2->hash);
1572 XBT_VERB("(%d - %d) Different hash: 0x%" PRIx64 "--0x%" PRIx64, num1, num2, s1->hash, s2->hash);
1577 XBT_VERB("(%d - %d) Same hash: 0x%" PRIx64, num1, num2, s1->hash);
1580 /* Compare enabled processes */
1581 if (s1->enabled_processes != s2->enabled_processes) {
1582 XBT_VERB("(%d - %d) Different amount of enabled processes", num1, num2);
1586 /* Compare size of stacks */
1588 for (unsigned long i = 0; i < s1->stacks.size(); i++) {
1589 size_t size_used1 = s1->stack_sizes[i];
1590 size_t size_used2 = s2->stack_sizes[i];
1591 if (size_used1 != size_used2) {
1593 XBT_DEBUG("(%d - %d) Different size used in stacks: %zu - %zu", num1, num2, size_used1, size_used2);
1598 XBT_VERB("(%d - %d) Different size used in stacks: %zu - %zu", num1, num2, size_used1, size_used2);
1604 if (is_diff) // do not proceed if there is any stacks that don't match
1607 /* Init heap information used in heap comparison algorithm */
1608 xbt_mheap_t heap1 = (xbt_mheap_t)s1->read_bytes(
1609 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1610 remote(process->heap_address),
1611 simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1612 xbt_mheap_t heap2 = (xbt_mheap_t)s2->read_bytes(
1613 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1614 remote(process->heap_address),
1615 simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1616 int res_init = state_comparator->initHeapInformation(heap1, heap2, &s1->to_ignore, &s2->to_ignore);
1618 if (res_init == -1) {
1620 XBT_DEBUG("(%d - %d) Different heap information", num1, num2);
1624 XBT_VERB("(%d - %d) Different heap information", num1, num2);
1631 /* Stacks comparison */
1633 for (unsigned int cursor = 0; cursor < s1->stacks.size(); cursor++) {
1634 mc_snapshot_stack_t stack1 = &s1->stacks[cursor];
1635 mc_snapshot_stack_t stack2 = &s2->stacks[cursor];
1637 if (stack1->process_index != stack2->process_index) {
1639 XBT_DEBUG("(%d - %d) Stacks with different process index (%i vs %i)", num1, num2,
1640 stack1->process_index, stack2->process_index);
1642 else diff_local = compare_local_variables(*state_comparator,
1643 stack1->process_index, s1, s2, stack1, stack2);
1644 if (diff_local > 0) {
1646 XBT_DEBUG("(%d - %d) Different local variables between stacks %d", num1,
1653 XBT_VERB("(%d - %d) Different local variables between stacks %d", num1,
1662 size_t regions_count = s1->snapshot_regions.size();
1663 // TODO, raise a difference instead?
1664 xbt_assert(regions_count == s2->snapshot_regions.size());
1666 for (size_t k = 0; k != regions_count; ++k) {
1667 mc_mem_region_t region1 = s1->snapshot_regions[k].get();
1668 mc_mem_region_t region2 = s2->snapshot_regions[k].get();
1671 if (region1->region_type() != simgrid::mc::RegionType::Data)
1674 xbt_assert(region1->region_type() == region2->region_type());
1675 xbt_assert(region1->object_info() == region2->object_info());
1676 xbt_assert(region1->object_info());
1678 std::string const& name = region1->object_info()->file_name;
1680 /* Compare global variables */
1681 if (compare_global_variables(*state_comparator, region1->object_info(), simgrid::mc::ProcessIndexDisabled, region1,
1685 XBT_DEBUG("(%d - %d) Different global variables in %s",
1686 num1, num2, name.c_str());
1690 XBT_VERB("(%d - %d) Different global variables in %s",
1691 num1, num2, name.c_str());
1700 if (simgrid::mc::mmalloc_compare_heap(*state_comparator, s1, s2) > 0) {
1703 XBT_DEBUG("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
1708 XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
1715 if (errors || hash_result)
1716 XBT_VERB("(%d - %d) Difference found", num1, num2);
1718 XBT_VERB("(%d - %d) No difference found", num1, num2);
1721 #if defined(MC_DEBUG) && defined(MC_VERBOSE)
1723 // * false positive SHOULD be avoided.
1724 // * There MUST not be any false negative.
1726 XBT_VERB("(%d - %d) State equality hash test is %s %s", num1, num2,
1727 (hash_result != 0) == (errors != 0) ? "true" : "false",
1728 !hash_result ? "positive" : "negative");
1732 return errors > 0 || hash_result;