1 /* Copyright (c) 2008-2017. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 /** \file compare.cpp Memory snapshooting and comparison */
14 #include <unordered_set>
16 #include "xbt/dynar.h"
17 #include "xbt/sysdep.h"
18 #include <xbt/mmalloc.h>
21 #include <mc/datatypes.h>
23 #include "src/internal_config.h"
25 #include "src/xbt/mmalloc/mmprivate.h"
28 #include "src/smpi/private.h"
29 #include "src/smpi/private.hpp"
32 #include "src/mc/mc_forward.hpp"
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 && not 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 && not state.equals_to2_(i1, j1).valid) {
469 addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE +
470 (char *) state.std_heap_copy.heapbase;
472 (void *) ((char *) addr_block2 +
473 (j1 << heapinfo2->type));
474 res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing, addr_frag1, addr_frag2, snapshot1,
475 snapshot2, nullptr, nullptr, 0);
476 if (res_compare != 1)
480 while (i2 < state.heaplimit && not equal) {
482 const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(
483 heap_region2, &heapinfo_temp2b, &heapinfos2[i2],
484 sizeof(malloc_info));
486 if (heapinfo2b->type == MMALLOC_TYPE_FREE || heapinfo2b->type == MMALLOC_TYPE_HEAPINFO) {
491 // We currently do not match fragments with unfragmented blocks (maybe we should).
492 if (heapinfo2b->type == MMALLOC_TYPE_UNFRAGMENTED) {
497 if (heapinfo2b->type < 0) {
498 fprintf(stderr, "Unknown mmalloc block type.\n");
502 for (j2 = 0; j2 < (size_t) (BLOCKSIZE >> heapinfo2b->type);
505 if (i2 == i1 && j2 == j1)
508 if (state.equals_to2_(i2, j2).valid)
511 addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
512 addr_frag2 = (void*)((char*)addr_block2 + (j2 << heapinfo2b->type));
514 res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing, addr_frag1, addr_frag2, snapshot2,
515 snapshot2, nullptr, nullptr, 0);
516 if (res_compare != 1) {
526 XBT_DEBUG("Block %zu, fragment %zu not found (size_used = %zd, address = %p)\n", i1, j1,
527 heapinfo1->busy_frag.frag_size[j1], addr_frag1);
528 i2 = state.heaplimit + 1;
529 i1 = state.heaplimit + 1;
539 /* All blocks/fragments are equal to another block/fragment ? */
543 for(i = 1; i < state.heaplimit; i++) {
544 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
545 heap_region1, &heapinfo_temp1, &heapinfos1[i], sizeof(malloc_info));
547 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED && i1 == state.heaplimit && heapinfo1->busy_block.busy_size > 0 &&
548 not state.equals_to1_(i, 0).valid) {
549 XBT_DEBUG("Block %zu not found (size used = %zu)", i, heapinfo1->busy_block.busy_size);
553 if (heapinfo1->type <= 0)
555 for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo1->type); j++)
556 if (i1 == state.heaplimit && heapinfo1->busy_frag.frag_size[j] > 0 && not state.equals_to1_(i, j).valid) {
557 XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)", i, j, heapinfo1->busy_frag.frag_size[j]);
562 if (i1 == state.heaplimit)
563 XBT_DEBUG("Number of blocks/fragments not found in heap1: %d", nb_diff1);
565 for (i=1; i < state.heaplimit; i++) {
566 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
567 heap_region2, &heapinfo_temp2, &heapinfos2[i], sizeof(malloc_info));
568 if (heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED && i1 == state.heaplimit && heapinfo2->busy_block.busy_size > 0 &&
569 not state.equals_to2_(i, 0).valid) {
570 XBT_DEBUG("Block %zu not found (size used = %zu)", i,
571 heapinfo2->busy_block.busy_size);
575 if (heapinfo2->type <= 0)
578 for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo2->type); j++)
579 if (i1 == state.heaplimit && heapinfo2->busy_frag.frag_size[j] > 0 && not state.equals_to2_(i, j).valid) {
580 XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)",
581 i, j, heapinfo2->busy_frag.frag_size[j]);
587 if (i1 == state.heaplimit)
588 XBT_DEBUG("Number of blocks/fragments not found in heap2: %d", nb_diff2);
590 return nb_diff1 > 0 || nb_diff2 > 0;
596 * @param real_area1 Process address for state 1
597 * @param real_area2 Process address for state 2
598 * @param snapshot1 Snapshot of state 1
599 * @param snapshot2 Snapshot of state 2
602 * @param check_ignore
604 static int compare_heap_area_without_type(
605 simgrid::mc::StateComparator& state, int process_index,
606 const void *real_area1, const void *real_area2,
607 simgrid::mc::Snapshot* snapshot1,
608 simgrid::mc::Snapshot* snapshot2,
609 HeapLocationPairs* previous, int size,
612 simgrid::mc::Process* process = &mc_model_checker->process();
613 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
614 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
616 for (int i = 0; i < size; ) {
618 if (check_ignore > 0) {
619 ssize_t ignore1 = heap_comparison_ignore_size(
620 state.processStates[0].to_ignore, (char *) real_area1 + i);
622 ssize_t ignore2 = heap_comparison_ignore_size(
623 state.processStates[1].to_ignore, (char *) real_area2 + i);
624 if (ignore2 == ignore1) {
637 if (MC_snapshot_region_memcmp(((char *) real_area1) + i, heap_region1, ((char *) real_area2) + i, heap_region2, 1) != 0) {
639 int pointer_align = (i / sizeof(void *)) * sizeof(void *);
640 const void* addr_pointed1 = snapshot1->read(
641 remote((void**)((char *) real_area1 + pointer_align)), process_index);
642 const void* addr_pointed2 = snapshot2->read(
643 remote((void**)((char *) real_area2 + pointer_align)), process_index);
645 if (process->in_maestro_stack(remote(addr_pointed1))
646 && process->in_maestro_stack(remote(addr_pointed2))) {
647 i = pointer_align + sizeof(void *);
651 if (addr_pointed1 > state.std_heap_copy.heapbase
652 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
653 && addr_pointed2 > state.std_heap_copy.heapbase
654 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)) {
655 // Both addreses are in the heap:
656 int res_compare = compare_heap_area(state ,process_index,
657 addr_pointed1, addr_pointed2,
658 snapshot1, snapshot2, previous, nullptr, 0);
659 if (res_compare == 1)
661 i = pointer_align + sizeof(void *);
677 * @param real_area1 Process address for state 1
678 * @param real_area2 Process address for state 2
679 * @param snapshot1 Snapshot of state 1
680 * @param snapshot2 Snapshot of state 2
683 * @param area_size either a byte_size or an elements_count (?)
684 * @param check_ignore
685 * @param pointer_level
686 * @return 0 (same), 1 (different), -1 (unknown)
688 static int compare_heap_area_with_type(
689 simgrid::mc::StateComparator& state, int process_index,
690 const void *real_area1, const void *real_area2,
691 simgrid::mc::Snapshot* snapshot1,
692 simgrid::mc::Snapshot* snapshot2,
693 HeapLocationPairs* previous, simgrid::mc::Type* type,
694 int area_size, int check_ignore,
699 // HACK: This should not happen but in pratice, there are some
700 // DW_TAG_typedef without an associated DW_AT_type:
701 //<1><538832>: Abbrev Number: 111 (DW_TAG_typedef)
702 // <538833> DW_AT_name : (indirect string, offset: 0x2292f3): gregset_t
703 // <538837> DW_AT_decl_file : 98
704 // <538838> DW_AT_decl_line : 37
708 if (is_stack(real_area1) && is_stack(real_area2))
711 if (check_ignore > 0) {
712 ssize_t ignore1 = heap_comparison_ignore_size(
713 state.processStates[0].to_ignore, real_area1);
715 && heap_comparison_ignore_size(
716 state.processStates[1].to_ignore, real_area2) == ignore1)
720 simgrid::mc::Type* subtype;
721 simgrid::mc::Type* subsubtype;
724 const void* addr_pointed1;
725 const void* addr_pointed2;
727 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
728 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
730 switch (type->type) {
731 case DW_TAG_unspecified_type:
734 case DW_TAG_base_type:
735 if (not type->name.empty() && type->name == "char") { /* String, hence random (arbitrary ?) size */
736 if (real_area1 == real_area2)
739 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, area_size) != 0;
741 if (area_size != -1 && type->byte_size != area_size)
744 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
748 case DW_TAG_enumeration_type:
749 if (area_size != -1 && type->byte_size != area_size)
751 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
754 case DW_TAG_const_type:
755 case DW_TAG_volatile_type:
757 type = type->subtype;
760 case DW_TAG_array_type:
761 subtype = type->subtype;
762 switch (subtype->type) {
763 case DW_TAG_unspecified_type:
766 case DW_TAG_base_type:
767 case DW_TAG_enumeration_type:
768 case DW_TAG_pointer_type:
769 case DW_TAG_reference_type:
770 case DW_TAG_rvalue_reference_type:
771 case DW_TAG_structure_type:
772 case DW_TAG_class_type:
773 case DW_TAG_union_type:
774 if (subtype->full_type)
775 subtype = subtype->full_type;
776 elm_size = subtype->byte_size;
778 // TODO, just remove the type indirection?
779 case DW_TAG_const_type:
781 case DW_TAG_volatile_type:
782 subsubtype = subtype->subtype;
783 if (subsubtype->full_type)
784 subsubtype = subsubtype->full_type;
785 elm_size = subsubtype->byte_size;
791 for (int i = 0; i < type->element_count; i++) {
792 // TODO, add support for variable stride (DW_AT_byte_stride)
794 compare_heap_area_with_type(state, process_index,
795 (char *) real_area1 + (i * elm_size),
796 (char *) real_area2 + (i * elm_size),
797 snapshot1, snapshot2, previous,
798 type->subtype, subtype->byte_size,
799 check_ignore, pointer_level);
805 case DW_TAG_reference_type:
806 case DW_TAG_rvalue_reference_type:
807 case DW_TAG_pointer_type:
808 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
809 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
810 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
811 return (addr_pointed1 != addr_pointed2);
814 if (pointer_level <= 1) {
815 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
816 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
817 if (addr_pointed1 > state.std_heap_copy.heapbase
818 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
819 && addr_pointed2 > state.std_heap_copy.heapbase
820 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
821 return compare_heap_area(state, process_index,
822 addr_pointed1, addr_pointed2, snapshot1,
823 snapshot2, previous, type->subtype,
826 return (addr_pointed1 != addr_pointed2);
828 for (size_t i = 0; i < (area_size / sizeof(void *)); i++) {
829 addr_pointed1 = snapshot1->read(
830 remote((void**)((char*) real_area1 + i * sizeof(void *))),
832 addr_pointed2 = snapshot2->read(
833 remote((void**)((char*) real_area2 + i * sizeof(void *))),
835 if (addr_pointed1 > state.std_heap_copy.heapbase
836 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
837 && addr_pointed2 > state.std_heap_copy.heapbase
838 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
840 compare_heap_area(state, process_index,
841 addr_pointed1, addr_pointed2, snapshot1,
842 snapshot2, previous, type->subtype,
845 res = (addr_pointed1 != addr_pointed2);
851 case DW_TAG_structure_type:
852 case DW_TAG_class_type:
854 type = type->full_type;
855 if (area_size != -1 && type->byte_size != area_size) {
856 if (area_size <= type->byte_size || area_size % type->byte_size != 0)
858 for (size_t i = 0; i < (size_t)(area_size / type->byte_size); i++) {
859 int res = compare_heap_area_with_type(state, process_index,
860 (char *) real_area1 + i * type->byte_size,
861 (char *) real_area2 + i * type->byte_size,
862 snapshot1, snapshot2, previous, type, -1,
868 for(simgrid::mc::Member& member : type->members) {
869 // TODO, optimize this? (for the offset case)
870 void *real_member1 = simgrid::dwarf::resolve_member(
871 real_area1, type, &member, (simgrid::mc::AddressSpace*) snapshot1, process_index);
872 void *real_member2 = simgrid::dwarf::resolve_member(
873 real_area2, type, &member, (simgrid::mc::AddressSpace*) snapshot2, process_index);
874 int res = compare_heap_area_with_type(
875 state, process_index, real_member1, real_member2,
876 snapshot1, snapshot2,
877 previous, member.type, -1,
885 case DW_TAG_union_type:
886 return compare_heap_area_without_type(state, process_index, real_area1, real_area2,
887 snapshot1, snapshot2, previous,
888 type->byte_size, check_ignore);
894 xbt_die("Unreachable");
897 /** Infer the type of a part of the block from the type of the block
899 * TODO, handle DW_TAG_array_type as well as arrays of the object ((*p)[5], p[5])
901 * TODO, handle subfields ((*p).bar.foo, (*p)[5].bar…)
903 * @param type DWARF type ID of the root address
905 * @return DWARF type ID for given offset
907 static simgrid::mc::Type* get_offset_type(void *real_base_address, simgrid::mc::Type* type,
908 int offset, int area_size,
909 simgrid::mc::Snapshot* snapshot, int process_index)
912 // Beginning of the block, the infered variable type if the type of the block:
916 switch (type->type) {
918 case DW_TAG_structure_type:
919 case DW_TAG_class_type:
921 type = type->full_type;
922 if (area_size != -1 && type->byte_size != area_size) {
923 if (area_size > type->byte_size && area_size % type->byte_size == 0)
929 for(simgrid::mc::Member& member : type->members) {
930 if (member.has_offset_location()) {
931 // We have the offset, use it directly (shortcut):
932 if (member.offset() == offset)
935 void* real_member = simgrid::dwarf::resolve_member(real_base_address, type, &member, snapshot, process_index);
936 if ((char*)real_member - (char*)real_base_address == offset)
943 /* FIXME: other cases ? */
951 * @param area1 Process address for state 1
952 * @param area2 Process address for state 2
953 * @param snapshot1 Snapshot of state 1
954 * @param snapshot2 Snapshot of state 2
955 * @param previous Pairs of blocks already compared on the current path (or nullptr)
956 * @param type_id Type of variable
957 * @param pointer_level
958 * @return 0 (same), 1 (different), -1
961 int compare_heap_area(simgrid::mc::StateComparator& state, int process_index,
962 const void *area1, const void *area2,
963 simgrid::mc::Snapshot* snapshot1,
964 simgrid::mc::Snapshot* snapshot2,
965 HeapLocationPairs* previous,
966 simgrid::mc::Type* type, int pointer_level)
968 simgrid::mc::Process* process = &mc_model_checker->process();
976 int check_ignore = 0;
978 void* real_addr_block1;
979 void* real_addr_block2;
980 void* real_addr_frag1;
981 void* real_addr_frag2;
988 simgrid::mc::Type* new_type1 = nullptr;
989 simgrid::mc::Type* new_type2 = nullptr;
991 bool match_pairs = false;
993 // This is the address of std_heap->heapinfo in the application process:
994 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
996 const malloc_info* heapinfos1 = snapshot1->read(remote((const malloc_info**)heapinfo_address), process_index);
997 const malloc_info* heapinfos2 = snapshot2->read(remote((const malloc_info**)heapinfo_address), process_index);
999 malloc_info heapinfo_temp1, heapinfo_temp2;
1001 simgrid::mc::HeapLocationPairs current;
1002 if (previous == nullptr) {
1003 previous = ¤t;
1007 // Get block number:
1008 block1 = ((char*)area1 - (char*)state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
1009 block2 = ((char*)area2 - (char*)state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
1011 // If either block is a stack block:
1012 if (is_block_stack((int) block1) && is_block_stack((int) block2)) {
1013 previous->insert(simgrid::mc::makeHeapLocationPair(block1, -1, block2, -1));
1015 state.match_equals(previous);
1019 // If either block is not in the expected area of memory:
1020 if (((char*)area1 < (char*)state.std_heap_copy.heapbase) || (block1 > (ssize_t)state.processStates[0].heapsize) ||
1021 (block1 < 1) || ((char*)area2 < (char*)state.std_heap_copy.heapbase) ||
1022 (block2 > (ssize_t)state.processStates[1].heapsize) || (block2 < 1)) {
1026 // Process address of the block:
1027 real_addr_block1 = (ADDR2UINT(block1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
1028 real_addr_block2 = (ADDR2UINT(block2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
1031 if (type->full_type)
1032 type = type->full_type;
1034 // This assume that for "boring" types (volatile ...) byte_size is absent:
1035 while (type->byte_size == 0 && type->subtype != nullptr)
1036 type = type->subtype;
1039 if (type->type == DW_TAG_pointer_type ||
1040 (type->type == DW_TAG_base_type && not type->name.empty() && type->name == "char"))
1043 type_size = type->byte_size;
1047 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
1048 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
1050 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
1051 heap_region1, &heapinfo_temp1, &heapinfos1[block1], sizeof(malloc_info));
1052 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
1053 heap_region2, &heapinfo_temp2, &heapinfos2[block2], sizeof(malloc_info));
1055 if ((heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type==MMALLOC_TYPE_HEAPINFO)
1056 && (heapinfo2->type == MMALLOC_TYPE_FREE || heapinfo2->type ==MMALLOC_TYPE_HEAPINFO)) {
1059 state.match_equals(previous);
1063 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED && heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
1064 /* Complete block */
1066 // TODO, lookup variable type from block type as done for fragmented blocks
1068 offset1 = (char*)area1 - (char*)real_addr_block1;
1069 offset2 = (char*)area2 - (char*)real_addr_block2;
1071 if (state.equals_to1_(block1, 0).valid && state.equals_to2_(block2, 0).valid && state.blocksEqual(block1, block2)) {
1073 state.match_equals(previous);
1077 if (type_size != -1) {
1078 if (type_size != (ssize_t) heapinfo1->busy_block.busy_size
1079 && type_size != (ssize_t) heapinfo2->busy_block.busy_size
1080 && (type->name.empty() || type->name == "struct s_smx_context")) {
1082 state.match_equals(previous);
1087 if (heapinfo1->busy_block.size != heapinfo2->busy_block.size)
1089 if (heapinfo1->busy_block.busy_size != heapinfo2->busy_block.busy_size)
1092 if (not previous->insert(simgrid::mc::makeHeapLocationPair(block1, -1, block2, -1)).second) {
1094 state.match_equals(previous);
1098 size = heapinfo1->busy_block.busy_size;
1100 // Remember (basic) type inference.
1101 // The current data structure only allows us to do this for the whole block.
1102 if (type != nullptr && area1 == real_addr_block1)
1103 state.types1_(block1, 0) = type;
1104 if (type != nullptr && area2 == real_addr_block2)
1105 state.types2_(block2, 0) = type;
1109 state.match_equals(previous);
1116 if (heapinfo1->busy_block.ignore > 0
1117 && heapinfo2->busy_block.ignore == heapinfo1->busy_block.ignore)
1118 check_ignore = heapinfo1->busy_block.ignore;
1120 } else if ((heapinfo1->type > 0) && (heapinfo2->type > 0)) { /* Fragmented block */
1123 frag1 = ((uintptr_t)(ADDR2UINT(area1) % (BLOCKSIZE))) >> heapinfo1->type;
1124 frag2 = ((uintptr_t)(ADDR2UINT(area2) % (BLOCKSIZE))) >> heapinfo2->type;
1126 // Process address of the fragment:
1127 real_addr_frag1 = (void*)((char*)real_addr_block1 + (frag1 << heapinfo1->type));
1128 real_addr_frag2 = (void*)((char*)real_addr_block2 + (frag2 << heapinfo2->type));
1130 // Check the size of the fragments against the size of the type:
1131 if (type_size != -1) {
1132 if (heapinfo1->busy_frag.frag_size[frag1] == -1 || heapinfo2->busy_frag.frag_size[frag2] == -1) {
1134 state.match_equals(previous);
1138 if (type_size != heapinfo1->busy_frag.frag_size[frag1]
1139 || type_size != heapinfo2->busy_frag.frag_size[frag2]) {
1141 state.match_equals(previous);
1146 // Check if the blocks are already matched together:
1147 if (state.equals_to1_(block1, frag1).valid && state.equals_to2_(block2, frag2).valid) {
1148 if (offset1==offset2 && state.fragmentsEqual(block1, frag1, block2, frag2)) {
1150 state.match_equals(previous);
1154 // Compare the size of both fragments:
1155 if (heapinfo1->busy_frag.frag_size[frag1] != heapinfo2->busy_frag.frag_size[frag2]) {
1156 if (type_size == -1) {
1158 state.match_equals(previous);
1164 // Size of the fragment:
1165 size = heapinfo1->busy_frag.frag_size[frag1];
1167 // Remember (basic) type inference.
1168 // The current data structure only allows us to do this for the whole fragment.
1169 if (type != nullptr && area1 == real_addr_frag1)
1170 state.types1_(block1, frag1) = type;
1171 if (type != nullptr && area2 == real_addr_frag2)
1172 state.types2_(block2, frag2) = type;
1174 // The type of the variable is already known:
1179 // Type inference from the block type.
1180 else if (state.types1_(block1, frag1) != nullptr || state.types2_(block2, frag2) != nullptr) {
1182 offset1 = (char*)area1 - (char*)real_addr_frag1;
1183 offset2 = (char*)area2 - (char*)real_addr_frag2;
1185 if (state.types1_(block1, frag1) != nullptr && state.types2_(block2, frag2) != nullptr) {
1187 get_offset_type(real_addr_frag1, state.types1_(block1, frag1), offset1, size, snapshot1, process_index);
1189 get_offset_type(real_addr_frag2, state.types2_(block2, frag2), offset1, size, snapshot2, process_index);
1190 } else if (state.types1_(block1, frag1) != 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.types1_(block1, frag1), offset2, size, snapshot2, process_index);
1195 } else if (state.types2_(block2, frag2) != nullptr) {
1197 get_offset_type(real_addr_frag1, state.types2_(block2, frag2), offset1, size, snapshot1, process_index);
1199 get_offset_type(real_addr_frag2, state.types2_(block2, frag2), offset2, size, snapshot2, process_index);
1202 state.match_equals(previous);
1206 if (new_type1 != nullptr && new_type2 != nullptr && new_type1 != new_type2) {
1209 while (type->byte_size == 0 && type->subtype != nullptr)
1210 type = type->subtype;
1211 new_size1 = type->byte_size;
1214 while (type->byte_size == 0 && type->subtype != nullptr)
1215 type = type->subtype;
1216 new_size2 = type->byte_size;
1220 state.match_equals(previous);
1225 if (new_size1 > 0 && new_size1 == new_size2) {
1230 if (offset1 == 0 && offset2 == 0 &&
1231 not previous->insert(simgrid::mc::makeHeapLocationPair(block1, frag1, block2, frag2)).second) {
1233 state.match_equals(previous);
1239 state.match_equals(previous);
1243 if ((heapinfo1->busy_frag.ignore[frag1] > 0) &&
1244 (heapinfo2->busy_frag.ignore[frag2] == heapinfo1->busy_frag.ignore[frag1]))
1245 check_ignore = heapinfo1->busy_frag.ignore[frag1];
1251 /* Start comparison */
1253 res_compare = compare_heap_area_with_type(state, process_index, area1, area2, snapshot1, snapshot2, previous, type,
1254 size, check_ignore, pointer_level);
1256 res_compare = compare_heap_area_without_type(state, process_index, area1, area2, snapshot1, snapshot2, previous,
1257 size, check_ignore);
1259 if (res_compare == 1)
1263 state.match_equals(previous);
1270 /************************** Snapshot comparison *******************************/
1271 /******************************************************************************/
1273 static int compare_areas_with_type(simgrid::mc::StateComparator& state,
1275 void* real_area1, simgrid::mc::Snapshot* snapshot1, mc_mem_region_t region1,
1276 void* real_area2, simgrid::mc::Snapshot* snapshot2, mc_mem_region_t region2,
1277 simgrid::mc::Type* type, int pointer_level)
1279 simgrid::mc::Process* process = &mc_model_checker->process();
1281 simgrid::mc::Type* subtype;
1282 simgrid::mc::Type* subsubtype;
1288 switch (type->type) {
1289 case DW_TAG_unspecified_type:
1292 case DW_TAG_base_type:
1293 case DW_TAG_enumeration_type:
1294 case DW_TAG_union_type:
1295 return MC_snapshot_region_memcmp(real_area1, region1, real_area2, region2, type->byte_size) != 0;
1296 case DW_TAG_typedef:
1297 case DW_TAG_volatile_type:
1298 case DW_TAG_const_type:
1300 type = type->subtype;
1302 case DW_TAG_array_type:
1303 subtype = type->subtype;
1304 switch (subtype->type) {
1305 case DW_TAG_unspecified_type:
1308 case DW_TAG_base_type:
1309 case DW_TAG_enumeration_type:
1310 case DW_TAG_pointer_type:
1311 case DW_TAG_reference_type:
1312 case DW_TAG_rvalue_reference_type:
1313 case DW_TAG_structure_type:
1314 case DW_TAG_class_type:
1315 case DW_TAG_union_type:
1316 if (subtype->full_type)
1317 subtype = subtype->full_type;
1318 elm_size = subtype->byte_size;
1320 case DW_TAG_const_type:
1321 case DW_TAG_typedef:
1322 case DW_TAG_volatile_type:
1323 subsubtype = subtype->subtype;
1324 if (subsubtype->full_type)
1325 subsubtype = subsubtype->full_type;
1326 elm_size = subsubtype->byte_size;
1332 for (i = 0; i < type->element_count; i++) {
1333 size_t off = i * elm_size;
1334 res = compare_areas_with_type(state, process_index,
1335 (char*) real_area1 + off, snapshot1, region1,
1336 (char*) real_area2 + off, snapshot2, region2,
1337 type->subtype, pointer_level);
1342 case DW_TAG_pointer_type:
1343 case DW_TAG_reference_type:
1344 case DW_TAG_rvalue_reference_type:
1346 void* addr_pointed1 = MC_region_read_pointer(region1, real_area1);
1347 void* addr_pointed2 = MC_region_read_pointer(region2, real_area2);
1349 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type)
1350 return (addr_pointed1 != addr_pointed2);
1351 if (addr_pointed1 == nullptr && addr_pointed2 == nullptr)
1353 if (addr_pointed1 == nullptr || addr_pointed2 == nullptr)
1355 if (not state.compared_pointers.insert(std::make_pair(addr_pointed1, addr_pointed2)).second)
1360 // Some cases are not handled here:
1361 // * the pointers lead to different areas (one to the heap, the other to the RW segment ...);
1362 // * a pointer leads to the read-only segment of the current object;
1363 // * a pointer lead to a different ELF object.
1365 if (addr_pointed1 > process->heap_address
1366 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)) {
1367 if (not(addr_pointed2 > process->heap_address && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)))
1369 // The pointers are both in the heap:
1370 return simgrid::mc::compare_heap_area(state,
1371 process_index, addr_pointed1, addr_pointed2, snapshot1,
1372 snapshot2, nullptr, type->subtype, pointer_level);
1375 // The pointers are both in the current object R/W segment:
1376 else if (region1->contain(simgrid::mc::remote(addr_pointed1))) {
1377 if (not region2->contain(simgrid::mc::remote(addr_pointed2)))
1379 if (not type->type_id)
1380 return (addr_pointed1 != addr_pointed2);
1382 return compare_areas_with_type(state, process_index,
1383 addr_pointed1, snapshot1, region1,
1384 addr_pointed2, snapshot2, region2,
1385 type->subtype, pointer_level);
1388 // TODO, We do not handle very well the case where
1389 // it belongs to a different (non-heap) region from the current one.
1392 return (addr_pointed1 != addr_pointed2);
1396 case DW_TAG_structure_type:
1397 case DW_TAG_class_type:
1398 for(simgrid::mc::Member& member : type->members) {
1399 void *member1 = simgrid::dwarf::resolve_member(
1400 real_area1, type, &member, snapshot1, process_index);
1401 void *member2 = simgrid::dwarf::resolve_member(
1402 real_area2, type, &member, snapshot2, process_index);
1403 mc_mem_region_t subregion1 = mc_get_region_hinted(member1, snapshot1, process_index, region1);
1404 mc_mem_region_t subregion2 = mc_get_region_hinted(member2, snapshot2, process_index, region2);
1406 compare_areas_with_type(state, process_index,
1407 member1, snapshot1, subregion1,
1408 member2, snapshot2, subregion2,
1409 member.type, pointer_level);
1414 case DW_TAG_subroutine_type:
1418 XBT_VERB("Unknown case: %d", type->type);
1425 static int compare_global_variables(
1426 simgrid::mc::StateComparator& state,
1427 simgrid::mc::ObjectInformation* object_info,
1429 mc_mem_region_t r1, mc_mem_region_t r2,
1430 simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2)
1432 xbt_assert(r1 && r2, "Missing region.");
1435 if (r1->storage_type() == simgrid::mc::StorageType::Privatized) {
1436 xbt_assert(process_index >= 0);
1437 if (r2->storage_type() != simgrid::mc::StorageType::Privatized)
1440 size_t process_count = MC_smpi_process_count();
1441 xbt_assert(process_count == r1->privatized_data().size()
1442 && process_count == r2->privatized_data().size());
1444 // Compare the global variables separately for each simulates process:
1445 for (size_t process_index = 0; process_index < process_count; process_index++) {
1446 if (compare_global_variables(state,
1447 object_info, process_index,
1448 &r1->privatized_data()[process_index],
1449 &r2->privatized_data()[process_index],
1450 snapshot1, snapshot2))
1456 xbt_assert(r1->storage_type() != simgrid::mc::StorageType::Privatized);
1458 xbt_assert(r2->storage_type() != simgrid::mc::StorageType::Privatized);
1460 std::vector<simgrid::mc::Variable>& variables = object_info->global_variables;
1462 for (simgrid::mc::Variable& current_var : variables) {
1464 // If the variable is not in this object, skip it:
1465 // We do not expect to find a pointer to something which is not reachable
1466 // by the global variables.
1467 if ((char *) current_var.address < (char *) object_info->start_rw
1468 || (char *) current_var.address > (char *) object_info->end_rw)
1471 simgrid::mc::Type* bvariable_type = current_var.type;
1472 int res = compare_areas_with_type(state, process_index,
1473 (char *) current_var.address, snapshot1, r1,
1474 (char *) current_var.address, snapshot2, r2,
1477 XBT_VERB("Global variable %s (%p) is different between snapshots",
1478 current_var.name.c_str(),
1479 (char *) current_var.address);
1489 static int compare_local_variables(simgrid::mc::StateComparator& state,
1491 simgrid::mc::Snapshot* snapshot1,
1492 simgrid::mc::Snapshot* snapshot2,
1493 mc_snapshot_stack_t stack1,
1494 mc_snapshot_stack_t stack2)
1496 if (stack1->local_variables.size() != stack2->local_variables.size()) {
1497 XBT_VERB("Different number of local variables");
1501 unsigned int cursor = 0;
1502 local_variable_t current_var1, current_var2;
1504 while (cursor < stack1->local_variables.size()) {
1505 current_var1 = &stack1->local_variables[cursor];
1506 current_var2 = &stack1->local_variables[cursor];
1507 if (current_var1->name != current_var2->name
1508 || current_var1->subprogram != current_var2->subprogram
1509 || current_var1->ip != current_var2->ip) {
1510 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1512 ("Different name of variable (%s - %s) "
1513 "or frame (%s - %s) or ip (%lu - %lu)",
1514 current_var1->name.c_str(),
1515 current_var2->name.c_str(),
1516 current_var1->subprogram->name.c_str(),
1517 current_var2->subprogram->name.c_str(),
1518 current_var1->ip, current_var2->ip);
1521 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1523 simgrid::mc::Type* subtype = current_var1->type;
1525 compare_areas_with_type(state, process_index,
1526 current_var1->address, snapshot1, mc_get_snapshot_region(current_var1->address, snapshot1, process_index),
1527 current_var2->address, snapshot2, mc_get_snapshot_region(current_var2->address, snapshot2, process_index),
1531 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1533 ("Local variable %s (%p - %p) in frame %s "
1534 "is different between snapshots",
1535 current_var1->name.c_str(),
1536 current_var1->address,
1537 current_var2->address,
1538 current_var1->subprogram->name.c_str());
1549 static std::unique_ptr<simgrid::mc::StateComparator> state_comparator;
1551 int snapshot_compare(int num1, simgrid::mc::Snapshot* s1, int num2, simgrid::mc::Snapshot* s2)
1553 // TODO, make this a field of ModelChecker or something similar
1555 if (state_comparator == nullptr)
1556 state_comparator = std::unique_ptr<StateComparator>(new StateComparator());
1558 state_comparator->clear();
1560 simgrid::mc::Process* process = &mc_model_checker->process();
1564 int hash_result = 0;
1566 hash_result = (s1->hash != s2->hash);
1568 XBT_VERB("(%d - %d) Different hash: 0x%" PRIx64 "--0x%" PRIx64, num1, num2, s1->hash, s2->hash);
1573 XBT_VERB("(%d - %d) Same hash: 0x%" PRIx64, num1, num2, s1->hash);
1576 /* Compare enabled processes */
1577 if (s1->enabled_processes != s2->enabled_processes) {
1578 XBT_VERB("(%d - %d) Different amount of enabled processes", num1, num2);
1582 /* Compare size of stacks */
1584 for (unsigned long i = 0; i < s1->stacks.size(); i++) {
1585 size_t size_used1 = s1->stack_sizes[i];
1586 size_t size_used2 = s2->stack_sizes[i];
1587 if (size_used1 != size_used2) {
1589 XBT_DEBUG("(%d - %d) Different size used in stacks: %zu - %zu", num1, num2, size_used1, size_used2);
1594 XBT_VERB("(%d - %d) Different size used in stacks: %zu - %zu", num1, num2, size_used1, size_used2);
1600 if (is_diff) // do not proceed if there is any stacks that don't match
1603 /* Init heap information used in heap comparison algorithm */
1604 xbt_mheap_t heap1 = (xbt_mheap_t)s1->read_bytes(
1605 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1606 remote(process->heap_address),
1607 simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1608 xbt_mheap_t heap2 = (xbt_mheap_t)s2->read_bytes(
1609 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1610 remote(process->heap_address),
1611 simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1612 int res_init = state_comparator->initHeapInformation(heap1, heap2, &s1->to_ignore, &s2->to_ignore);
1614 if (res_init == -1) {
1616 XBT_DEBUG("(%d - %d) Different heap information", num1, num2);
1620 XBT_VERB("(%d - %d) Different heap information", num1, num2);
1627 /* Stacks comparison */
1629 for (unsigned int cursor = 0; cursor < s1->stacks.size(); cursor++) {
1630 mc_snapshot_stack_t stack1 = &s1->stacks[cursor];
1631 mc_snapshot_stack_t stack2 = &s2->stacks[cursor];
1633 if (stack1->process_index != stack2->process_index) {
1635 XBT_DEBUG("(%d - %d) Stacks with different process index (%i vs %i)", num1, num2,
1636 stack1->process_index, stack2->process_index);
1638 else diff_local = compare_local_variables(*state_comparator,
1639 stack1->process_index, s1, s2, stack1, stack2);
1640 if (diff_local > 0) {
1642 XBT_DEBUG("(%d - %d) Different local variables between stacks %d", num1,
1649 XBT_VERB("(%d - %d) Different local variables between stacks %d", num1,
1658 size_t regions_count = s1->snapshot_regions.size();
1659 // TODO, raise a difference instead?
1660 xbt_assert(regions_count == s2->snapshot_regions.size());
1662 for (size_t k = 0; k != regions_count; ++k) {
1663 mc_mem_region_t region1 = s1->snapshot_regions[k].get();
1664 mc_mem_region_t region2 = s2->snapshot_regions[k].get();
1667 if (region1->region_type() != simgrid::mc::RegionType::Data)
1670 xbt_assert(region1->region_type() == region2->region_type());
1671 xbt_assert(region1->object_info() == region2->object_info());
1672 xbt_assert(region1->object_info());
1674 std::string const& name = region1->object_info()->file_name;
1676 /* Compare global variables */
1677 if (compare_global_variables(*state_comparator, region1->object_info(), simgrid::mc::ProcessIndexDisabled, region1,
1681 XBT_DEBUG("(%d - %d) Different global variables in %s",
1682 num1, num2, name.c_str());
1686 XBT_VERB("(%d - %d) Different global variables in %s",
1687 num1, num2, name.c_str());
1696 if (simgrid::mc::mmalloc_compare_heap(*state_comparator, s1, s2) > 0) {
1699 XBT_DEBUG("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
1704 XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
1711 if (errors || hash_result)
1712 XBT_VERB("(%d - %d) Difference found", num1, num2);
1714 XBT_VERB("(%d - %d) No difference found", num1, num2);
1717 #if defined(MC_DEBUG) && defined(MC_VERBOSE)
1719 // * false positive SHOULD be avoided.
1720 // * There MUST not be any false negative.
1722 XBT_VERB("(%d - %d) State equality hash test is %s %s", num1, num2,
1723 (hash_result != 0) == (errors != 0) ? "true" : "false", not hash_result ? "positive" : "negative");
1727 return errors > 0 || hash_result;