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, std::vector<simgrid::mc::IgnoredHeapRegion>* i);
122 /** A hash which works with more stuff
124 * It can hash pairs: the standard hash currently doesn't include this.
126 template<class X> struct hash : public std::hash<X> {};
128 template<class X, class Y>
129 struct 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);
141 struct 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
201 && this->equals_to2_(b2, 0).block == b1;
204 /** Check whether two fragments are known to be matching
206 * @param b1 Block of state 1
207 * @param f1 Fragment of state 1
208 * @param b2 Block of state 2
209 * @param f2 Fragment of state 2
210 * @return if the fragments are known to be matching
212 int fragmentsEqual(int b1, int f1, int b2, int f2) const
214 return this->equals_to1_(b1, f1).block == b2
215 && this->equals_to1_(b1, f1).fragment == f2
216 && this->equals_to2_(b2, f2).block == b1
217 && this->equals_to2_(b2, f2).fragment == f1;
220 void match_equals(HeapLocationPairs* list);
226 /************************************************************************************/
228 static ssize_t heap_comparison_ignore_size(
229 std::vector<simgrid::mc::IgnoredHeapRegion>* ignore_list,
233 int end = ignore_list->size() - 1;
235 while (start <= end) {
236 unsigned int cursor = (start + end) / 2;
237 simgrid::mc::IgnoredHeapRegion const& region = (*ignore_list)[cursor];
238 if (region.address == address)
240 if (region.address < address)
242 if (region.address > address)
249 static bool is_stack(const void *address)
251 for (auto const& stack : mc_model_checker->process().stack_areas())
252 if (address == stack.address)
257 // TODO, this should depend on the snapshot?
258 static bool is_block_stack(int block)
260 for (auto const& stack : mc_model_checker->process().stack_areas())
261 if (block == stack.block)
269 void StateComparator::match_equals(HeapLocationPairs* list)
271 for (auto const& pair : *list) {
272 if (pair[0].fragment != -1) {
273 this->equals_to1_(pair[0].block, pair[0].fragment) = simgrid::mc::HeapArea(pair[1].block, pair[1].fragment);
274 this->equals_to2_(pair[1].block, pair[1].fragment) = simgrid::mc::HeapArea(pair[0].block, pair[0].fragment);
276 this->equals_to1_(pair[0].block, 0) = simgrid::mc::HeapArea(pair[1].block, pair[1].fragment);
277 this->equals_to2_(pair[1].block, 0) = simgrid::mc::HeapArea(pair[0].block, pair[0].fragment);
282 void ProcessComparisonState::initHeapInformation(xbt_mheap_t heap,
283 std::vector<simgrid::mc::IgnoredHeapRegion>* i)
285 auto heaplimit = heap->heaplimit;
286 this->heapsize = heap->heapsize;
288 this->equals_to.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, HeapArea());
289 this->types.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, nullptr);
292 int StateComparator::initHeapInformation(xbt_mheap_t heap1, xbt_mheap_t heap2,
293 std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
294 std::vector<simgrid::mc::IgnoredHeapRegion>* i2)
296 if ((heap1->heaplimit != heap2->heaplimit) || (heap1->heapsize != heap2->heapsize))
298 this->heaplimit = heap1->heaplimit;
299 this->std_heap_copy = *mc_model_checker->process().get_heap();
300 this->processStates[0].initHeapInformation(heap1, i1);
301 this->processStates[1].initHeapInformation(heap2, i2);
305 // TODO, have a robust way to find it in O(1)
307 mc_mem_region_t MC_get_heap_region(simgrid::mc::Snapshot* snapshot)
309 for (auto& region : snapshot->snapshot_regions)
310 if (region->region_type() == simgrid::mc::RegionType::Heap)
312 xbt_die("No heap region");
316 int mmalloc_compare_heap(
317 simgrid::mc::StateComparator& state, simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2)
319 simgrid::mc::Process* process = &mc_model_checker->process();
321 /* Start comparison */
336 /* Check busy blocks */
339 malloc_info heapinfo_temp1;
340 malloc_info heapinfo_temp2;
341 malloc_info heapinfo_temp2b;
343 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
344 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
346 // This is the address of std_heap->heapinfo in the application process:
347 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
349 // This is in snapshot do not use them directly:
350 const malloc_info* heapinfos1 = snapshot1->read<malloc_info*>(
351 (std::uint64_t)heapinfo_address, simgrid::mc::ProcessIndexMissing);
352 const malloc_info* heapinfos2 = snapshot2->read<malloc_info*>(
353 (std::uint64_t)heapinfo_address, simgrid::mc::ProcessIndexMissing);
355 while (i1 < state.heaplimit) {
357 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(heap_region1, &heapinfo_temp1, &heapinfos1[i1], sizeof(malloc_info));
358 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2, &heapinfos2[i1], sizeof(malloc_info));
360 if (heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type == MMALLOC_TYPE_HEAPINFO) { /* Free block */
365 if (heapinfo1->type < 0) {
366 fprintf(stderr, "Unkown mmalloc block type.\n");
370 addr_block1 = ((void*)(((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase));
372 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED) { /* Large block */
374 if (is_stack(addr_block1)) {
375 for (k = 0; k < heapinfo1->busy_block.size; k++)
376 state.equals_to1_(i1 + k, 0) = HeapArea(i1, -1);
377 for (k = 0; k < heapinfo2->busy_block.size; k++)
378 state.equals_to2_(i1 + k, 0) = HeapArea(i1, -1);
379 i1 += heapinfo1->busy_block.size;
383 if (state.equals_to1_(i1, 0).valid) {
392 /* Try first to associate to same block in the other heap */
393 if (heapinfo2->type == heapinfo1->type
394 && state.equals_to2_(i1, 0).valid == 0) {
395 addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
396 res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing, addr_block1, addr_block2, snapshot1,
397 snapshot2, nullptr, nullptr, 0);
398 if (res_compare != 1) {
399 for (k = 1; k < heapinfo2->busy_block.size; k++)
400 state.equals_to2_(i1 + k, 0) = HeapArea(i1, -1);
401 for (k = 1; k < heapinfo1->busy_block.size; k++)
402 state.equals_to1_(i1 + k, 0) = HeapArea(i1, -1);
404 i1 += heapinfo1->busy_block.size;
408 while (i2 < state.heaplimit && !equal) {
410 addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
417 const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2b, &heapinfos2[i2], sizeof(malloc_info));
419 if (heapinfo2b->type != MMALLOC_TYPE_UNFRAGMENTED) {
424 if (state.equals_to2_(i2, 0).valid) {
429 res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing,
430 addr_block1, addr_block2, snapshot1, snapshot2,
431 nullptr, nullptr, 0);
433 if (res_compare != 1) {
434 for (k = 1; k < heapinfo2b->busy_block.size; k++)
435 state.equals_to2_(i2 + k, 0) = HeapArea(i1, -1);
436 for (k = 1; k < heapinfo1->busy_block.size; k++)
437 state.equals_to1_(i1 + k, 0) = HeapArea(i2, -1);
439 i1 += heapinfo1->busy_block.size;
446 XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1, heapinfo1->busy_block.busy_size, addr_block1);
447 i1 = state.heaplimit + 1;
452 } else { /* Fragmented block */
454 for (j1 = 0; j1 < (size_t) (BLOCKSIZE >> heapinfo1->type); j1++) {
456 if (heapinfo1->busy_frag.frag_size[j1] == -1) /* Free fragment */
459 if (state.equals_to1_(i1, j1).valid)
462 addr_frag1 = (void*)((char*)addr_block1 + (j1 << heapinfo1->type));
467 /* Try first to associate to same fragment in the other heap */
468 if (heapinfo2->type == heapinfo1->type
469 && !state.equals_to2_(i1, j1).valid) {
470 addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE +
471 (char *) state.std_heap_copy.heapbase;
473 (void *) ((char *) addr_block2 +
474 (j1 << heapinfo2->type));
475 res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing, addr_frag1, addr_frag2, snapshot1,
476 snapshot2, nullptr, nullptr, 0);
477 if (res_compare != 1)
481 while (i2 < state.heaplimit && !equal) {
483 const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(
484 heap_region2, &heapinfo_temp2b, &heapinfos2[i2],
485 sizeof(malloc_info));
487 if (heapinfo2b->type == MMALLOC_TYPE_FREE || heapinfo2b->type == MMALLOC_TYPE_HEAPINFO) {
492 // We currently do not match fragments with unfragmented blocks (maybe we should).
493 if (heapinfo2b->type == MMALLOC_TYPE_UNFRAGMENTED) {
498 if (heapinfo2b->type < 0) {
499 fprintf(stderr, "Unknown mmalloc block type.\n");
503 for (j2 = 0; j2 < (size_t) (BLOCKSIZE >> heapinfo2b->type);
506 if (i2 == i1 && j2 == j1)
509 if (state.equals_to2_(i2, j2).valid)
512 addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
513 addr_frag2 = (void*)((char*)addr_block2 + (j2 << heapinfo2b->type));
515 res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing, addr_frag1, addr_frag2, snapshot2,
516 snapshot2, nullptr, nullptr, 0);
517 if (res_compare != 1) {
527 XBT_DEBUG("Block %zu, fragment %zu not found (size_used = %zd, address = %p)\n", i1, j1,
528 heapinfo1->busy_frag.frag_size[j1], addr_frag1);
529 i2 = state.heaplimit + 1;
530 i1 = state.heaplimit + 1;
540 /* All blocks/fragments are equal to another block/fragment ? */
544 for(i = 1; i < state.heaplimit; i++) {
545 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
546 heap_region1, &heapinfo_temp1, &heapinfos1[i], sizeof(malloc_info));
548 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED && i1 == state.heaplimit && heapinfo1->busy_block.busy_size > 0 &&
549 !state.equals_to1_(i, 0).valid) {
550 XBT_DEBUG("Block %zu not found (size used = %zu)", i, heapinfo1->busy_block.busy_size);
554 if (heapinfo1->type <= 0)
556 for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo1->type); j++)
557 if (i1 == state.heaplimit && heapinfo1->busy_frag.frag_size[j] > 0 && !state.equals_to1_(i, j).valid) {
558 XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)", i, j, heapinfo1->busy_frag.frag_size[j]);
563 if (i1 == state.heaplimit)
564 XBT_DEBUG("Number of blocks/fragments not found in heap1: %d", nb_diff1);
566 for (i=1; i < state.heaplimit; i++) {
567 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
568 heap_region2, &heapinfo_temp2, &heapinfos2[i], sizeof(malloc_info));
569 if (heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED
570 && i1 == state.heaplimit
571 && heapinfo2->busy_block.busy_size > 0
572 && !state.equals_to2_(i, 0).valid) {
573 XBT_DEBUG("Block %zu not found (size used = %zu)", i,
574 heapinfo2->busy_block.busy_size);
578 if (heapinfo2->type <= 0)
581 for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo2->type); j++)
582 if (i1 == state.heaplimit
583 && heapinfo2->busy_frag.frag_size[j] > 0
584 && !state.equals_to2_(i, j).valid) {
585 XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)",
586 i, j, heapinfo2->busy_frag.frag_size[j]);
592 if (i1 == state.heaplimit)
593 XBT_DEBUG("Number of blocks/fragments not found in heap2: %d", nb_diff2);
595 return nb_diff1 > 0 || nb_diff2 > 0;
601 * @param real_area1 Process address for state 1
602 * @param real_area2 Process address for state 2
603 * @param snapshot1 Snapshot of state 1
604 * @param snapshot2 Snapshot of state 2
607 * @param check_ignore
609 static int compare_heap_area_without_type(
610 simgrid::mc::StateComparator& state, int process_index,
611 const void *real_area1, const void *real_area2,
612 simgrid::mc::Snapshot* snapshot1,
613 simgrid::mc::Snapshot* snapshot2,
614 HeapLocationPairs* previous, int size,
617 simgrid::mc::Process* process = &mc_model_checker->process();
618 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
619 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
621 for (int i = 0; i < size; ) {
623 if (check_ignore > 0) {
624 ssize_t ignore1 = heap_comparison_ignore_size(
625 state.processStates[0].to_ignore, (char *) real_area1 + i);
627 ssize_t ignore2 = heap_comparison_ignore_size(
628 state.processStates[1].to_ignore, (char *) real_area2 + i);
629 if (ignore2 == ignore1) {
642 if (MC_snapshot_region_memcmp(((char *) real_area1) + i, heap_region1, ((char *) real_area2) + i, heap_region2, 1) != 0) {
644 int pointer_align = (i / sizeof(void *)) * sizeof(void *);
645 const void* addr_pointed1 = snapshot1->read(
646 remote((void**)((char *) real_area1 + pointer_align)), process_index);
647 const void* addr_pointed2 = snapshot2->read(
648 remote((void**)((char *) real_area2 + pointer_align)), process_index);
650 if (process->in_maestro_stack(remote(addr_pointed1))
651 && process->in_maestro_stack(remote(addr_pointed2))) {
652 i = pointer_align + sizeof(void *);
656 if (addr_pointed1 > state.std_heap_copy.heapbase
657 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
658 && addr_pointed2 > state.std_heap_copy.heapbase
659 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)) {
660 // Both addreses are in the heap:
661 int res_compare = compare_heap_area(state ,process_index,
662 addr_pointed1, addr_pointed2,
663 snapshot1, snapshot2, previous, nullptr, 0);
664 if (res_compare == 1)
666 i = pointer_align + sizeof(void *);
682 * @param real_area1 Process address for state 1
683 * @param real_area2 Process address for state 2
684 * @param snapshot1 Snapshot of state 1
685 * @param snapshot2 Snapshot of state 2
688 * @param area_size either a byte_size or an elements_count (?)
689 * @param check_ignore
690 * @param pointer_level
691 * @return 0 (same), 1 (different), -1 (unknown)
693 static int compare_heap_area_with_type(
694 simgrid::mc::StateComparator& state, int process_index,
695 const void *real_area1, const void *real_area2,
696 simgrid::mc::Snapshot* snapshot1,
697 simgrid::mc::Snapshot* snapshot2,
698 HeapLocationPairs* previous, simgrid::mc::Type* type,
699 int area_size, int check_ignore,
704 // HACK: This should not happen but in pratice, there are some
705 // DW_TAG_typedef without an associated DW_AT_type:
706 //<1><538832>: Abbrev Number: 111 (DW_TAG_typedef)
707 // <538833> DW_AT_name : (indirect string, offset: 0x2292f3): gregset_t
708 // <538837> DW_AT_decl_file : 98
709 // <538838> DW_AT_decl_line : 37
713 if (is_stack(real_area1) && is_stack(real_area2))
716 if (check_ignore > 0) {
717 ssize_t ignore1 = heap_comparison_ignore_size(
718 state.processStates[0].to_ignore, real_area1);
720 && heap_comparison_ignore_size(
721 state.processStates[1].to_ignore, real_area2) == ignore1)
725 simgrid::mc::Type* subtype;
726 simgrid::mc::Type* subsubtype;
729 const void* addr_pointed1;
730 const void* addr_pointed2;
732 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
733 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
735 switch (type->type) {
736 case DW_TAG_unspecified_type:
739 case DW_TAG_base_type:
740 if (!type->name.empty() && type->name == "char") { /* String, hence random (arbitrary ?) size */
741 if (real_area1 == real_area2)
744 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, area_size) != 0;
746 if (area_size != -1 && type->byte_size != area_size)
749 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
753 case DW_TAG_enumeration_type:
754 if (area_size != -1 && type->byte_size != area_size)
756 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
759 case DW_TAG_const_type:
760 case DW_TAG_volatile_type:
762 type = type->subtype;
765 case DW_TAG_array_type:
766 subtype = type->subtype;
767 switch (subtype->type) {
768 case DW_TAG_unspecified_type:
771 case DW_TAG_base_type:
772 case DW_TAG_enumeration_type:
773 case DW_TAG_pointer_type:
774 case DW_TAG_reference_type:
775 case DW_TAG_rvalue_reference_type:
776 case DW_TAG_structure_type:
777 case DW_TAG_class_type:
778 case DW_TAG_union_type:
779 if (subtype->full_type)
780 subtype = subtype->full_type;
781 elm_size = subtype->byte_size;
783 // TODO, just remove the type indirection?
784 case DW_TAG_const_type:
786 case DW_TAG_volatile_type:
787 subsubtype = subtype->subtype;
788 if (subsubtype->full_type)
789 subsubtype = subsubtype->full_type;
790 elm_size = subsubtype->byte_size;
796 for (int i = 0; i < type->element_count; i++) {
797 // TODO, add support for variable stride (DW_AT_byte_stride)
799 compare_heap_area_with_type(state, process_index,
800 (char *) real_area1 + (i * elm_size),
801 (char *) real_area2 + (i * elm_size),
802 snapshot1, snapshot2, previous,
803 type->subtype, subtype->byte_size,
804 check_ignore, pointer_level);
810 case DW_TAG_reference_type:
811 case DW_TAG_rvalue_reference_type:
812 case DW_TAG_pointer_type:
813 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
814 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
815 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
816 return (addr_pointed1 != addr_pointed2);
819 if (pointer_level <= 1) {
820 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
821 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
822 if (addr_pointed1 > state.std_heap_copy.heapbase
823 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
824 && addr_pointed2 > state.std_heap_copy.heapbase
825 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
826 return compare_heap_area(state, process_index,
827 addr_pointed1, addr_pointed2, snapshot1,
828 snapshot2, previous, type->subtype,
831 return (addr_pointed1 != addr_pointed2);
833 for (size_t i = 0; i < (area_size / sizeof(void *)); i++) {
834 addr_pointed1 = snapshot1->read(
835 remote((void**)((char*) real_area1 + i * sizeof(void *))),
837 addr_pointed2 = snapshot2->read(
838 remote((void**)((char*) real_area2 + i * sizeof(void *))),
840 if (addr_pointed1 > state.std_heap_copy.heapbase
841 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
842 && addr_pointed2 > state.std_heap_copy.heapbase
843 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
845 compare_heap_area(state, process_index,
846 addr_pointed1, addr_pointed2, snapshot1,
847 snapshot2, previous, type->subtype,
850 res = (addr_pointed1 != addr_pointed2);
856 case DW_TAG_structure_type:
857 case DW_TAG_class_type:
859 type = type->full_type;
860 if (area_size != -1 && type->byte_size != area_size) {
861 if (area_size <= type->byte_size || area_size % type->byte_size != 0)
863 for (size_t i = 0; i < (size_t)(area_size / type->byte_size); i++) {
864 int res = compare_heap_area_with_type(state, process_index,
865 (char *) real_area1 + i * type->byte_size,
866 (char *) real_area2 + i * type->byte_size,
867 snapshot1, snapshot2, previous, type, -1,
873 for(simgrid::mc::Member& member : type->members) {
874 // TODO, optimize this? (for the offset case)
875 void *real_member1 = simgrid::dwarf::resolve_member(
876 real_area1, type, &member, (simgrid::mc::AddressSpace*) snapshot1, process_index);
877 void *real_member2 = simgrid::dwarf::resolve_member(
878 real_area2, type, &member, (simgrid::mc::AddressSpace*) snapshot2, process_index);
879 int res = compare_heap_area_with_type(
880 state, process_index, real_member1, real_member2,
881 snapshot1, snapshot2,
882 previous, member.type, -1,
890 case DW_TAG_union_type:
891 return compare_heap_area_without_type(state, process_index, real_area1, real_area2,
892 snapshot1, snapshot2, previous,
893 type->byte_size, check_ignore);
899 xbt_die("Unreachable");
902 /** Infer the type of a part of the block from the type of the block
904 * TODO, handle DW_TAG_array_type as well as arrays of the object ((*p)[5], p[5])
906 * TODO, handle subfields ((*p).bar.foo, (*p)[5].bar…)
908 * @param type DWARF type ID of the root address
910 * @return DWARF type ID for given offset
912 static simgrid::mc::Type* get_offset_type(void *real_base_address, simgrid::mc::Type* type,
913 int offset, int area_size,
914 simgrid::mc::Snapshot* snapshot, int process_index)
917 // Beginning of the block, the infered variable type if the type of the block:
921 switch (type->type) {
923 case DW_TAG_structure_type:
924 case DW_TAG_class_type:
926 type = type->full_type;
927 if (area_size != -1 && type->byte_size != area_size) {
928 if (area_size > type->byte_size && area_size % type->byte_size == 0)
934 for(simgrid::mc::Member& member : type->members) {
935 if (member.has_offset_location()) {
936 // We have the offset, use it directly (shortcut):
937 if (member.offset() == offset)
940 void* real_member = simgrid::dwarf::resolve_member(real_base_address, type, &member, snapshot, process_index);
941 if ((char*)real_member - (char*)real_base_address == offset)
948 /* FIXME: other cases ? */
956 * @param area1 Process address for state 1
957 * @param area2 Process address for state 2
958 * @param snapshot1 Snapshot of state 1
959 * @param snapshot2 Snapshot of state 2
960 * @param previous Pairs of blocks already compared on the current path (or nullptr)
961 * @param type_id Type of variable
962 * @param pointer_level
963 * @return 0 (same), 1 (different), -1
966 int compare_heap_area(simgrid::mc::StateComparator& state, int process_index,
967 const void *area1, const void *area2,
968 simgrid::mc::Snapshot* snapshot1,
969 simgrid::mc::Snapshot* snapshot2,
970 HeapLocationPairs* previous,
971 simgrid::mc::Type* type, int pointer_level)
973 simgrid::mc::Process* process = &mc_model_checker->process();
981 int check_ignore = 0;
983 void* real_addr_block1;
984 void* real_addr_block2;
985 void* real_addr_frag1;
986 void* real_addr_frag2;
993 simgrid::mc::Type* new_type1 = nullptr;
994 simgrid::mc::Type* new_type2 = nullptr;
996 bool match_pairs = false;
998 // This is the address of std_heap->heapinfo in the application process:
999 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
1001 const malloc_info* heapinfos1 = snapshot1->read(remote((const malloc_info**)heapinfo_address), process_index);
1002 const malloc_info* heapinfos2 = snapshot2->read(remote((const malloc_info**)heapinfo_address), process_index);
1004 malloc_info heapinfo_temp1, heapinfo_temp2;
1006 simgrid::mc::HeapLocationPairs current;
1007 if (previous == nullptr) {
1008 previous = ¤t;
1012 // Get block number:
1013 block1 = ((char*)area1 - (char*)state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
1014 block2 = ((char*)area2 - (char*)state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
1016 // If either block is a stack block:
1017 if (is_block_stack((int) block1) && is_block_stack((int) block2)) {
1018 previous->insert(simgrid::mc::makeHeapLocationPair(block1, -1, block2, -1));
1020 state.match_equals(previous);
1024 // If either block is not in the expected area of memory:
1025 if (((char*)area1 < (char*)state.std_heap_copy.heapbase) || (block1 > (ssize_t)state.processStates[0].heapsize) ||
1026 (block1 < 1) || ((char*)area2 < (char*)state.std_heap_copy.heapbase) ||
1027 (block2 > (ssize_t)state.processStates[1].heapsize) || (block2 < 1)) {
1031 // Process address of the block:
1032 real_addr_block1 = (ADDR2UINT(block1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
1033 real_addr_block2 = (ADDR2UINT(block2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
1036 if (type->full_type)
1037 type = type->full_type;
1039 // This assume that for "boring" types (volatile ...) byte_size is absent:
1040 while (type->byte_size == 0 && type->subtype != nullptr)
1041 type = type->subtype;
1044 if (type->type == DW_TAG_pointer_type ||
1045 (type->type == DW_TAG_base_type && !type->name.empty() && type->name == "char"))
1048 type_size = type->byte_size;
1052 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
1053 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
1055 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
1056 heap_region1, &heapinfo_temp1, &heapinfos1[block1], sizeof(malloc_info));
1057 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
1058 heap_region2, &heapinfo_temp2, &heapinfos2[block2], sizeof(malloc_info));
1060 if ((heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type==MMALLOC_TYPE_HEAPINFO)
1061 && (heapinfo2->type == MMALLOC_TYPE_FREE || heapinfo2->type ==MMALLOC_TYPE_HEAPINFO)) {
1064 state.match_equals(previous);
1068 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED && heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
1069 /* Complete block */
1071 // TODO, lookup variable type from block type as done for fragmented blocks
1073 offset1 = (char*)area1 - (char*)real_addr_block1;
1074 offset2 = (char*)area2 - (char*)real_addr_block2;
1076 if (state.equals_to1_(block1, 0).valid && state.equals_to2_(block2, 0).valid && state.blocksEqual(block1, block2)) {
1078 state.match_equals(previous);
1082 if (type_size != -1) {
1083 if (type_size != (ssize_t) heapinfo1->busy_block.busy_size
1084 && type_size != (ssize_t) heapinfo2->busy_block.busy_size
1085 && (type->name.empty() || type->name == "struct s_smx_context")) {
1087 state.match_equals(previous);
1092 if (heapinfo1->busy_block.size != heapinfo2->busy_block.size)
1094 if (heapinfo1->busy_block.busy_size != heapinfo2->busy_block.busy_size)
1097 if (!previous->insert(simgrid::mc::makeHeapLocationPair(block1, -1, block2, -1)).second) {
1099 state.match_equals(previous);
1103 size = heapinfo1->busy_block.busy_size;
1105 // Remember (basic) type inference.
1106 // The current data structure only allows us to do this for the whole block.
1107 if (type != nullptr && area1 == real_addr_block1)
1108 state.types1_(block1, 0) = type;
1109 if (type != nullptr && area2 == real_addr_block2)
1110 state.types2_(block2, 0) = type;
1114 state.match_equals(previous);
1121 if (heapinfo1->busy_block.ignore > 0
1122 && heapinfo2->busy_block.ignore == heapinfo1->busy_block.ignore)
1123 check_ignore = heapinfo1->busy_block.ignore;
1125 } else if ((heapinfo1->type > 0) && (heapinfo2->type > 0)) { /* Fragmented block */
1128 frag1 = ((uintptr_t)(ADDR2UINT(area1) % (BLOCKSIZE))) >> heapinfo1->type;
1129 frag2 = ((uintptr_t)(ADDR2UINT(area2) % (BLOCKSIZE))) >> heapinfo2->type;
1131 // Process address of the fragment:
1132 real_addr_frag1 = (void*)((char*)real_addr_block1 + (frag1 << heapinfo1->type));
1133 real_addr_frag2 = (void*)((char*)real_addr_block2 + (frag2 << heapinfo2->type));
1135 // Check the size of the fragments against the size of the type:
1136 if (type_size != -1) {
1137 if (heapinfo1->busy_frag.frag_size[frag1] == -1 || heapinfo2->busy_frag.frag_size[frag2] == -1) {
1139 state.match_equals(previous);
1143 if (type_size != heapinfo1->busy_frag.frag_size[frag1]
1144 || type_size != heapinfo2->busy_frag.frag_size[frag2]) {
1146 state.match_equals(previous);
1151 // Check if the blocks are already matched together:
1152 if (state.equals_to1_(block1, frag1).valid && state.equals_to2_(block2, frag2).valid) {
1153 if (offset1==offset2 && state.fragmentsEqual(block1, frag1, block2, frag2)) {
1155 state.match_equals(previous);
1159 // Compare the size of both fragments:
1160 if (heapinfo1->busy_frag.frag_size[frag1] != heapinfo2->busy_frag.frag_size[frag2]) {
1161 if (type_size == -1) {
1163 state.match_equals(previous);
1169 // Size of the fragment:
1170 size = heapinfo1->busy_frag.frag_size[frag1];
1172 // Remember (basic) type inference.
1173 // The current data structure only allows us to do this for the whole fragment.
1174 if (type != nullptr && area1 == real_addr_frag1)
1175 state.types1_(block1, frag1) = type;
1176 if (type != nullptr && area2 == real_addr_frag2)
1177 state.types2_(block2, frag2) = type;
1179 // The type of the variable is already known:
1184 // Type inference from the block type.
1185 else if (state.types1_(block1, frag1) != nullptr || state.types2_(block2, frag2) != nullptr) {
1187 offset1 = (char*)area1 - (char*)real_addr_frag1;
1188 offset2 = (char*)area2 - (char*)real_addr_frag2;
1190 if (state.types1_(block1, frag1) != nullptr && state.types2_(block2, frag2) != nullptr) {
1192 get_offset_type(real_addr_frag1, state.types1_(block1, frag1), offset1, size, snapshot1, process_index);
1194 get_offset_type(real_addr_frag2, state.types2_(block2, frag2), offset1, size, snapshot2, process_index);
1195 } else if (state.types1_(block1, frag1) != nullptr) {
1197 get_offset_type(real_addr_frag1, state.types1_(block1, frag1), offset1, size, snapshot1, process_index);
1199 get_offset_type(real_addr_frag2, state.types1_(block1, frag1), offset2, size, snapshot2, process_index);
1200 } else if (state.types2_(block2, frag2) != nullptr) {
1202 get_offset_type(real_addr_frag1, state.types2_(block2, frag2), offset1, size, snapshot1, process_index);
1204 get_offset_type(real_addr_frag2, state.types2_(block2, frag2), offset2, size, snapshot2, process_index);
1207 state.match_equals(previous);
1211 if (new_type1 != nullptr && new_type2 != nullptr && new_type1 != new_type2) {
1214 while (type->byte_size == 0 && type->subtype != nullptr)
1215 type = type->subtype;
1216 new_size1 = type->byte_size;
1219 while (type->byte_size == 0 && type->subtype != nullptr)
1220 type = type->subtype;
1221 new_size2 = type->byte_size;
1225 state.match_equals(previous);
1230 if (new_size1 > 0 && new_size1 == new_size2) {
1235 if (offset1 == 0 && offset2 == 0 &&
1236 !previous->insert(simgrid::mc::makeHeapLocationPair(block1, frag1, block2, frag2)).second) {
1238 state.match_equals(previous);
1244 state.match_equals(previous);
1248 if ((heapinfo1->busy_frag.ignore[frag1] > 0) &&
1249 (heapinfo2->busy_frag.ignore[frag2] == heapinfo1->busy_frag.ignore[frag1]))
1250 check_ignore = heapinfo1->busy_frag.ignore[frag1];
1256 /* Start comparison */
1258 res_compare = compare_heap_area_with_type(state, process_index, area1, area2, snapshot1, snapshot2, previous, type,
1259 size, check_ignore, pointer_level);
1261 res_compare = compare_heap_area_without_type(state, process_index, area1, area2, snapshot1, snapshot2, previous,
1262 size, check_ignore);
1264 if (res_compare == 1)
1268 state.match_equals(previous);
1275 /************************** Snapshot comparison *******************************/
1276 /******************************************************************************/
1278 static int compare_areas_with_type(simgrid::mc::StateComparator& state,
1280 void* real_area1, simgrid::mc::Snapshot* snapshot1, mc_mem_region_t region1,
1281 void* real_area2, simgrid::mc::Snapshot* snapshot2, mc_mem_region_t region2,
1282 simgrid::mc::Type* type, int pointer_level)
1284 simgrid::mc::Process* process = &mc_model_checker->process();
1286 simgrid::mc::Type* subtype;
1287 simgrid::mc::Type* subsubtype;
1293 switch (type->type) {
1294 case DW_TAG_unspecified_type:
1297 case DW_TAG_base_type:
1298 case DW_TAG_enumeration_type:
1299 case DW_TAG_union_type:
1300 return MC_snapshot_region_memcmp(real_area1, region1, real_area2, region2, type->byte_size) != 0;
1301 case DW_TAG_typedef:
1302 case DW_TAG_volatile_type:
1303 case DW_TAG_const_type:
1305 type = type->subtype;
1307 case DW_TAG_array_type:
1308 subtype = type->subtype;
1309 switch (subtype->type) {
1310 case DW_TAG_unspecified_type:
1313 case DW_TAG_base_type:
1314 case DW_TAG_enumeration_type:
1315 case DW_TAG_pointer_type:
1316 case DW_TAG_reference_type:
1317 case DW_TAG_rvalue_reference_type:
1318 case DW_TAG_structure_type:
1319 case DW_TAG_class_type:
1320 case DW_TAG_union_type:
1321 if (subtype->full_type)
1322 subtype = subtype->full_type;
1323 elm_size = subtype->byte_size;
1325 case DW_TAG_const_type:
1326 case DW_TAG_typedef:
1327 case DW_TAG_volatile_type:
1328 subsubtype = subtype->subtype;
1329 if (subsubtype->full_type)
1330 subsubtype = subsubtype->full_type;
1331 elm_size = subsubtype->byte_size;
1337 for (i = 0; i < type->element_count; i++) {
1338 size_t off = i * elm_size;
1339 res = compare_areas_with_type(state, process_index,
1340 (char*) real_area1 + off, snapshot1, region1,
1341 (char*) real_area2 + off, snapshot2, region2,
1342 type->subtype, pointer_level);
1347 case DW_TAG_pointer_type:
1348 case DW_TAG_reference_type:
1349 case DW_TAG_rvalue_reference_type:
1351 void* addr_pointed1 = MC_region_read_pointer(region1, real_area1);
1352 void* addr_pointed2 = MC_region_read_pointer(region2, real_area2);
1354 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type)
1355 return (addr_pointed1 != addr_pointed2);
1356 if (addr_pointed1 == nullptr && addr_pointed2 == nullptr)
1358 if (addr_pointed1 == nullptr || addr_pointed2 == nullptr)
1360 if (!state.compared_pointers.insert(
1361 std::make_pair(addr_pointed1, addr_pointed2)).second)
1366 // Some cases are not handled here:
1367 // * the pointers lead to different areas (one to the heap, the other to the RW segment ...);
1368 // * a pointer leads to the read-only segment of the current object;
1369 // * a pointer lead to a different ELF object.
1371 if (addr_pointed1 > process->heap_address
1372 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)) {
1374 (addr_pointed2 > process->heap_address
1375 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)))
1377 // The pointers are both in the heap:
1378 return simgrid::mc::compare_heap_area(state,
1379 process_index, addr_pointed1, addr_pointed2, snapshot1,
1380 snapshot2, nullptr, type->subtype, pointer_level);
1383 // The pointers are both in the current object R/W segment:
1384 else if (region1->contain(simgrid::mc::remote(addr_pointed1))) {
1385 if (!region2->contain(simgrid::mc::remote(addr_pointed2)))
1388 return (addr_pointed1 != addr_pointed2);
1390 return compare_areas_with_type(state, process_index,
1391 addr_pointed1, snapshot1, region1,
1392 addr_pointed2, snapshot2, region2,
1393 type->subtype, pointer_level);
1396 // TODO, We do not handle very well the case where
1397 // it belongs to a different (non-heap) region from the current one.
1400 return (addr_pointed1 != addr_pointed2);
1404 case DW_TAG_structure_type:
1405 case DW_TAG_class_type:
1406 for(simgrid::mc::Member& member : type->members) {
1407 void *member1 = simgrid::dwarf::resolve_member(
1408 real_area1, type, &member, snapshot1, process_index);
1409 void *member2 = simgrid::dwarf::resolve_member(
1410 real_area2, type, &member, snapshot2, process_index);
1411 mc_mem_region_t subregion1 = mc_get_region_hinted(member1, snapshot1, process_index, region1);
1412 mc_mem_region_t subregion2 = mc_get_region_hinted(member2, snapshot2, process_index, region2);
1414 compare_areas_with_type(state, process_index,
1415 member1, snapshot1, subregion1,
1416 member2, snapshot2, subregion2,
1417 member.type, pointer_level);
1422 case DW_TAG_subroutine_type:
1426 XBT_VERB("Unknown case: %d", type->type);
1433 static int compare_global_variables(
1434 simgrid::mc::StateComparator& state,
1435 simgrid::mc::ObjectInformation* object_info,
1437 mc_mem_region_t r1, mc_mem_region_t r2,
1438 simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2)
1440 xbt_assert(r1 && r2, "Missing region.");
1443 if (r1->storage_type() == simgrid::mc::StorageType::Privatized) {
1444 xbt_assert(process_index >= 0);
1445 if (r2->storage_type() != simgrid::mc::StorageType::Privatized)
1448 size_t process_count = MC_smpi_process_count();
1449 xbt_assert(process_count == r1->privatized_data().size()
1450 && process_count == r2->privatized_data().size());
1452 // Compare the global variables separately for each simulates process:
1453 for (size_t process_index = 0; process_index < process_count; process_index++) {
1454 if (compare_global_variables(state,
1455 object_info, process_index,
1456 &r1->privatized_data()[process_index],
1457 &r2->privatized_data()[process_index],
1458 snapshot1, snapshot2))
1464 xbt_assert(r1->storage_type() != simgrid::mc::StorageType::Privatized);
1466 xbt_assert(r2->storage_type() != simgrid::mc::StorageType::Privatized);
1468 std::vector<simgrid::mc::Variable>& variables = object_info->global_variables;
1470 for (simgrid::mc::Variable& current_var : variables) {
1472 // If the variable is not in this object, skip it:
1473 // We do not expect to find a pointer to something which is not reachable
1474 // by the global variables.
1475 if ((char *) current_var.address < (char *) object_info->start_rw
1476 || (char *) current_var.address > (char *) object_info->end_rw)
1479 simgrid::mc::Type* bvariable_type = current_var.type;
1480 int res = compare_areas_with_type(state, process_index,
1481 (char *) current_var.address, snapshot1, r1,
1482 (char *) current_var.address, snapshot2, r2,
1485 XBT_VERB("Global variable %s (%p) is different between snapshots",
1486 current_var.name.c_str(),
1487 (char *) current_var.address);
1497 static int compare_local_variables(simgrid::mc::StateComparator& state,
1499 simgrid::mc::Snapshot* snapshot1,
1500 simgrid::mc::Snapshot* snapshot2,
1501 mc_snapshot_stack_t stack1,
1502 mc_snapshot_stack_t stack2)
1504 if (stack1->local_variables.size() != stack2->local_variables.size()) {
1505 XBT_VERB("Different number of local variables");
1509 unsigned int cursor = 0;
1510 local_variable_t current_var1, current_var2;
1512 while (cursor < stack1->local_variables.size()) {
1513 current_var1 = &stack1->local_variables[cursor];
1514 current_var2 = &stack1->local_variables[cursor];
1515 if (current_var1->name != current_var2->name
1516 || current_var1->subprogram != current_var2->subprogram
1517 || current_var1->ip != current_var2->ip) {
1518 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1520 ("Different name of variable (%s - %s) "
1521 "or frame (%s - %s) or ip (%lu - %lu)",
1522 current_var1->name.c_str(),
1523 current_var2->name.c_str(),
1524 current_var1->subprogram->name.c_str(),
1525 current_var2->subprogram->name.c_str(),
1526 current_var1->ip, current_var2->ip);
1529 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1531 simgrid::mc::Type* subtype = current_var1->type;
1533 compare_areas_with_type(state, process_index,
1534 current_var1->address, snapshot1, mc_get_snapshot_region(current_var1->address, snapshot1, process_index),
1535 current_var2->address, snapshot2, mc_get_snapshot_region(current_var2->address, snapshot2, process_index),
1539 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1541 ("Local variable %s (%p - %p) in frame %s "
1542 "is different between snapshots",
1543 current_var1->name.c_str(),
1544 current_var1->address,
1545 current_var2->address,
1546 current_var1->subprogram->name.c_str());
1557 static std::unique_ptr<simgrid::mc::StateComparator> state_comparator;
1559 int snapshot_compare(int num1, simgrid::mc::Snapshot* s1, int num2, simgrid::mc::Snapshot* s2)
1561 // TODO, make this a field of ModelChecker or something similar
1563 if (state_comparator == nullptr)
1564 state_comparator = std::unique_ptr<StateComparator>(new StateComparator());
1566 state_comparator->clear();
1568 simgrid::mc::Process* process = &mc_model_checker->process();
1572 int hash_result = 0;
1574 hash_result = (s1->hash != s2->hash);
1576 XBT_VERB("(%d - %d) Different hash: 0x%" PRIx64 "--0x%" PRIx64, num1, num2, s1->hash, s2->hash);
1581 XBT_VERB("(%d - %d) Same hash: 0x%" PRIx64, num1, num2, s1->hash);
1584 /* Compare enabled processes */
1585 if (s1->enabled_processes != s2->enabled_processes) {
1586 XBT_VERB("(%d - %d) Different amount of enabled processes", num1, num2);
1590 /* Compare size of stacks */
1592 for (unsigned long i = 0; i < s1->stacks.size(); i++) {
1593 size_t size_used1 = s1->stack_sizes[i];
1594 size_t size_used2 = s2->stack_sizes[i];
1595 if (size_used1 != size_used2) {
1597 XBT_DEBUG("(%d - %d) Different size used in stacks: %zu - %zu", num1, num2, size_used1, size_used2);
1602 XBT_VERB("(%d - %d) Different size used in stacks: %zu - %zu", num1, num2, size_used1, size_used2);
1608 if (is_diff) // do not proceed if there is any stacks that don't match
1611 /* Init heap information used in heap comparison algorithm */
1612 xbt_mheap_t heap1 = (xbt_mheap_t)s1->read_bytes(
1613 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1614 remote(process->heap_address),
1615 simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1616 xbt_mheap_t heap2 = (xbt_mheap_t)s2->read_bytes(
1617 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1618 remote(process->heap_address),
1619 simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1620 int res_init = state_comparator->initHeapInformation(heap1, heap2, &s1->to_ignore, &s2->to_ignore);
1622 if (res_init == -1) {
1624 XBT_DEBUG("(%d - %d) Different heap information", num1, num2);
1628 XBT_VERB("(%d - %d) Different heap information", num1, num2);
1635 /* Stacks comparison */
1637 for (unsigned int cursor = 0; cursor < s1->stacks.size(); cursor++) {
1638 mc_snapshot_stack_t stack1 = &s1->stacks[cursor];
1639 mc_snapshot_stack_t stack2 = &s2->stacks[cursor];
1641 if (stack1->process_index != stack2->process_index) {
1643 XBT_DEBUG("(%d - %d) Stacks with different process index (%i vs %i)", num1, num2,
1644 stack1->process_index, stack2->process_index);
1646 else diff_local = compare_local_variables(*state_comparator,
1647 stack1->process_index, s1, s2, stack1, stack2);
1648 if (diff_local > 0) {
1650 XBT_DEBUG("(%d - %d) Different local variables between stacks %d", num1,
1657 XBT_VERB("(%d - %d) Different local variables between stacks %d", num1,
1666 size_t regions_count = s1->snapshot_regions.size();
1667 // TODO, raise a difference instead?
1668 xbt_assert(regions_count == s2->snapshot_regions.size());
1670 for (size_t k = 0; k != regions_count; ++k) {
1671 mc_mem_region_t region1 = s1->snapshot_regions[k].get();
1672 mc_mem_region_t region2 = s2->snapshot_regions[k].get();
1675 if (region1->region_type() != simgrid::mc::RegionType::Data)
1678 xbt_assert(region1->region_type() == region2->region_type());
1679 xbt_assert(region1->object_info() == region2->object_info());
1680 xbt_assert(region1->object_info());
1682 std::string const& name = region1->object_info()->file_name;
1684 /* Compare global variables */
1685 if (compare_global_variables(*state_comparator, region1->object_info(), simgrid::mc::ProcessIndexDisabled, region1,
1689 XBT_DEBUG("(%d - %d) Different global variables in %s",
1690 num1, num2, name.c_str());
1694 XBT_VERB("(%d - %d) Different global variables in %s",
1695 num1, num2, name.c_str());
1704 if (simgrid::mc::mmalloc_compare_heap(*state_comparator, s1, s2) > 0) {
1707 XBT_DEBUG("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
1712 XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
1719 if (errors || hash_result)
1720 XBT_VERB("(%d - %d) Difference found", num1, num2);
1722 XBT_VERB("(%d - %d) No difference found", num1, num2);
1725 #if defined(MC_DEBUG) && defined(MC_VERBOSE)
1727 // * false positive SHOULD be avoided.
1728 // * There MUST not be any false negative.
1730 XBT_VERB("(%d - %d) State equality hash test is %s %s", num1, num2,
1731 (hash_result != 0) == (errors != 0) ? "true" : "false",
1732 !hash_result ? "positive" : "negative");
1736 return errors > 0 || hash_result;