1 /* Copyright (c) 2008-2017. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 /** \file compare.cpp Memory snapshooting and comparison */
14 #include <unordered_set>
16 #include "xbt/dynar.h"
17 #include "xbt/sysdep.h"
18 #include <xbt/mmalloc.h>
21 #include <mc/datatypes.h>
23 #include "src/internal_config.h"
25 #include "src/xbt/mmalloc/mmprivate.h"
28 #include "src/smpi/include/private.h"
29 #include "src/smpi/include/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 {
105 explicit HeapArea(int block) : valid(true), block(block) {}
106 HeapArea(int block, int fragment) : valid(true), block(block), fragment(fragment) {}
109 struct ProcessComparisonState {
110 std::vector<simgrid::mc::IgnoredHeapRegion>* to_ignore = nullptr;
111 std::vector<HeapArea> equals_to;
112 std::vector<simgrid::mc::Type*> types;
113 std::size_t heapsize = 0;
115 void initHeapInformation(xbt_mheap_t heap, std::vector<simgrid::mc::IgnoredHeapRegion>* i);
120 /** A hash which works with more stuff
122 * It can hash pairs: the standard hash currently doesn't include this.
124 template<class X> struct hash : public std::hash<X> {};
126 template<class X, class Y>
127 struct hash<std::pair<X,Y>> {
128 std::size_t operator()(std::pair<X,Y>const& x) const
132 return h1(x.first) ^ h2(x.second);
139 struct StateComparator {
140 s_xbt_mheap_t std_heap_copy;
141 std::size_t heaplimit;
142 std::array<ProcessComparisonState, 2> processStates;
144 std::unordered_set<std::pair<void*, void*>, hash<std::pair<void*, void*>>> compared_pointers;
148 compared_pointers.clear();
151 int initHeapInformation(
152 xbt_mheap_t heap1, xbt_mheap_t heap2,
153 std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
154 std::vector<simgrid::mc::IgnoredHeapRegion>* i2);
156 HeapArea& equals_to1_(std::size_t i, std::size_t j)
158 return processStates[0].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
160 HeapArea& equals_to2_(std::size_t i, std::size_t j)
162 return processStates[1].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
164 Type*& types1_(std::size_t i, std::size_t j)
166 return processStates[0].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
168 Type*& types2_(std::size_t i, std::size_t j)
170 return processStates[1].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
173 HeapArea const& equals_to1_(std::size_t i, std::size_t j) const
175 return processStates[0].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
177 HeapArea const& equals_to2_(std::size_t i, std::size_t j) const
179 return processStates[1].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
181 Type* const& types1_(std::size_t i, std::size_t j) const
183 return processStates[0].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
185 Type* const& types2_(std::size_t i, std::size_t j) const
187 return processStates[1].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
190 /** Check whether two blocks are known to be matching
192 * @param b1 Block of state 1
193 * @param b2 Block of state 2
194 * @return if the blocks are known to be matching
196 bool blocksEqual(int b1, int b2) const
198 return this->equals_to1_(b1, 0).block == b2
199 && this->equals_to2_(b2, 0).block == b1;
202 /** Check whether two fragments are known to be matching
204 * @param b1 Block of state 1
205 * @param f1 Fragment of state 1
206 * @param b2 Block of state 2
207 * @param f2 Fragment of state 2
208 * @return if the fragments are known to be matching
210 int fragmentsEqual(int b1, int f1, int b2, int f2) const
212 return this->equals_to1_(b1, f1).block == b2
213 && this->equals_to1_(b1, f1).fragment == f2
214 && this->equals_to2_(b2, f2).block == b1
215 && this->equals_to2_(b2, f2).fragment == f1;
218 void match_equals(HeapLocationPairs* list);
224 /************************************************************************************/
226 static ssize_t heap_comparison_ignore_size(
227 std::vector<simgrid::mc::IgnoredHeapRegion>* ignore_list,
231 int end = ignore_list->size() - 1;
233 while (start <= end) {
234 unsigned int cursor = (start + end) / 2;
235 simgrid::mc::IgnoredHeapRegion const& region = (*ignore_list)[cursor];
236 if (region.address == address)
238 if (region.address < address)
240 if (region.address > address)
247 static bool is_stack(const void *address)
249 for (auto const& stack : mc_model_checker->process().stack_areas())
250 if (address == stack.address)
255 // TODO, this should depend on the snapshot?
256 static bool is_block_stack(int block)
258 for (auto const& stack : mc_model_checker->process().stack_areas())
259 if (block == stack.block)
267 void StateComparator::match_equals(HeapLocationPairs* list)
269 for (auto const& pair : *list) {
270 if (pair[0].fragment != -1) {
271 this->equals_to1_(pair[0].block, pair[0].fragment) = simgrid::mc::HeapArea(pair[1].block, pair[1].fragment);
272 this->equals_to2_(pair[1].block, pair[1].fragment) = simgrid::mc::HeapArea(pair[0].block, pair[0].fragment);
274 this->equals_to1_(pair[0].block, 0) = simgrid::mc::HeapArea(pair[1].block, pair[1].fragment);
275 this->equals_to2_(pair[1].block, 0) = simgrid::mc::HeapArea(pair[0].block, pair[0].fragment);
280 void ProcessComparisonState::initHeapInformation(xbt_mheap_t heap,
281 std::vector<simgrid::mc::IgnoredHeapRegion>* i)
283 auto heaplimit = heap->heaplimit;
284 this->heapsize = heap->heapsize;
286 this->equals_to.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, HeapArea());
287 this->types.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, nullptr);
290 int StateComparator::initHeapInformation(xbt_mheap_t heap1, xbt_mheap_t heap2,
291 std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
292 std::vector<simgrid::mc::IgnoredHeapRegion>* i2)
294 if ((heap1->heaplimit != heap2->heaplimit) || (heap1->heapsize != heap2->heapsize))
296 this->heaplimit = heap1->heaplimit;
297 this->std_heap_copy = *mc_model_checker->process().get_heap();
298 this->processStates[0].initHeapInformation(heap1, i1);
299 this->processStates[1].initHeapInformation(heap2, i2);
303 // TODO, have a robust way to find it in O(1)
305 mc_mem_region_t MC_get_heap_region(simgrid::mc::Snapshot* snapshot)
307 for (auto const& region : snapshot->snapshot_regions)
308 if (region->region_type() == simgrid::mc::RegionType::Heap)
310 xbt_die("No heap region");
314 int mmalloc_compare_heap(
315 simgrid::mc::StateComparator& state, simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2)
317 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
319 /* Start comparison */
333 /* Check busy blocks */
336 malloc_info heapinfo_temp1;
337 malloc_info heapinfo_temp2;
338 malloc_info heapinfo_temp2b;
340 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
341 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
343 // This is the address of std_heap->heapinfo in the application process:
344 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
346 // This is in snapshot do not use them directly:
347 const malloc_info* heapinfos1 = snapshot1->read<malloc_info*>(
348 (std::uint64_t)heapinfo_address, simgrid::mc::ProcessIndexMissing);
349 const malloc_info* heapinfos2 = snapshot2->read<malloc_info*>(
350 (std::uint64_t)heapinfo_address, simgrid::mc::ProcessIndexMissing);
352 while (i1 < state.heaplimit) {
354 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(heap_region1, &heapinfo_temp1, &heapinfos1[i1], sizeof(malloc_info));
355 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2, &heapinfos2[i1], sizeof(malloc_info));
357 if (heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type == MMALLOC_TYPE_HEAPINFO) { /* Free block */
362 if (heapinfo1->type < 0) {
363 fprintf(stderr, "Unkown mmalloc block type.\n");
367 addr_block1 = ((void*)(((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase));
369 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED) { /* Large block */
371 if (is_stack(addr_block1)) {
372 for (k = 0; k < heapinfo1->busy_block.size; k++)
373 state.equals_to1_(i1 + k, 0) = HeapArea(i1, -1);
374 for (k = 0; k < heapinfo2->busy_block.size; k++)
375 state.equals_to2_(i1 + k, 0) = HeapArea(i1, -1);
376 i1 += heapinfo1->busy_block.size;
380 if (state.equals_to1_(i1, 0).valid) {
388 /* Try first to associate to same block in the other heap */
389 if (heapinfo2->type == heapinfo1->type
390 && state.equals_to2_(i1, 0).valid == 0) {
391 addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
392 int res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing, addr_block1, addr_block2,
393 snapshot1, snapshot2, nullptr, nullptr, 0);
394 if (res_compare != 1) {
395 for (k = 1; k < heapinfo2->busy_block.size; k++)
396 state.equals_to2_(i1 + k, 0) = HeapArea(i1, -1);
397 for (k = 1; k < heapinfo1->busy_block.size; k++)
398 state.equals_to1_(i1 + k, 0) = HeapArea(i1, -1);
400 i1 += heapinfo1->busy_block.size;
404 while (i2 < state.heaplimit && not equal) {
406 addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
413 const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2b, &heapinfos2[i2], sizeof(malloc_info));
415 if (heapinfo2b->type != MMALLOC_TYPE_UNFRAGMENTED) {
420 if (state.equals_to2_(i2, 0).valid) {
425 int res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing, addr_block1, addr_block2,
426 snapshot1, snapshot2, nullptr, nullptr, 0);
428 if (res_compare != 1) {
429 for (k = 1; k < heapinfo2b->busy_block.size; k++)
430 state.equals_to2_(i2 + k, 0) = HeapArea(i1, -1);
431 for (k = 1; k < heapinfo1->busy_block.size; k++)
432 state.equals_to1_(i1 + k, 0) = HeapArea(i2, -1);
434 i1 += heapinfo1->busy_block.size;
441 XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1, heapinfo1->busy_block.busy_size, addr_block1);
442 i1 = state.heaplimit + 1;
447 } else { /* Fragmented block */
449 for (j1 = 0; j1 < (size_t) (BLOCKSIZE >> heapinfo1->type); j1++) {
451 if (heapinfo1->busy_frag.frag_size[j1] == -1) /* Free fragment */
454 if (state.equals_to1_(i1, j1).valid)
457 addr_frag1 = (void*)((char*)addr_block1 + (j1 << heapinfo1->type));
462 /* Try first to associate to same fragment in the other heap */
463 if (heapinfo2->type == heapinfo1->type && not state.equals_to2_(i1, j1).valid) {
464 addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE +
465 (char *) state.std_heap_copy.heapbase;
467 (void *) ((char *) addr_block2 +
468 (j1 << heapinfo2->type));
469 int res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing, addr_frag1, addr_frag2,
470 snapshot1, snapshot2, nullptr, nullptr, 0);
471 if (res_compare != 1)
475 while (i2 < state.heaplimit && not equal) {
477 const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(
478 heap_region2, &heapinfo_temp2b, &heapinfos2[i2],
479 sizeof(malloc_info));
481 if (heapinfo2b->type == MMALLOC_TYPE_FREE || heapinfo2b->type == MMALLOC_TYPE_HEAPINFO) {
486 // We currently do not match fragments with unfragmented blocks (maybe we should).
487 if (heapinfo2b->type == MMALLOC_TYPE_UNFRAGMENTED) {
492 if (heapinfo2b->type < 0) {
493 fprintf(stderr, "Unknown mmalloc block type.\n");
497 for (j2 = 0; j2 < (size_t) (BLOCKSIZE >> heapinfo2b->type);
500 if (i2 == i1 && j2 == j1)
503 if (state.equals_to2_(i2, j2).valid)
506 addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
507 addr_frag2 = (void*)((char*)addr_block2 + (j2 << heapinfo2b->type));
509 int res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing, addr_frag1, addr_frag2,
510 snapshot2, snapshot2, nullptr, nullptr, 0);
511 if (res_compare != 1) {
521 XBT_DEBUG("Block %zu, fragment %zu not found (size_used = %zd, address = %p)\n", i1, j1,
522 heapinfo1->busy_frag.frag_size[j1], addr_frag1);
523 i1 = state.heaplimit + 1;
533 /* All blocks/fragments are equal to another block/fragment ? */
537 for(i = 1; i < state.heaplimit; i++) {
538 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
539 heap_region1, &heapinfo_temp1, &heapinfos1[i], sizeof(malloc_info));
541 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED && i1 == state.heaplimit && heapinfo1->busy_block.busy_size > 0 &&
542 not state.equals_to1_(i, 0).valid) {
543 XBT_DEBUG("Block %zu not found (size used = %zu)", i, heapinfo1->busy_block.busy_size);
547 if (heapinfo1->type <= 0)
549 for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo1->type); j++)
550 if (i1 == state.heaplimit && heapinfo1->busy_frag.frag_size[j] > 0 && not state.equals_to1_(i, j).valid) {
551 XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)", i, j, heapinfo1->busy_frag.frag_size[j]);
556 if (i1 == state.heaplimit)
557 XBT_DEBUG("Number of blocks/fragments not found in heap1: %d", nb_diff1);
559 for (i=1; i < state.heaplimit; i++) {
560 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
561 heap_region2, &heapinfo_temp2, &heapinfos2[i], sizeof(malloc_info));
562 if (heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED && i1 == state.heaplimit && heapinfo2->busy_block.busy_size > 0 &&
563 not state.equals_to2_(i, 0).valid) {
564 XBT_DEBUG("Block %zu not found (size used = %zu)", i,
565 heapinfo2->busy_block.busy_size);
569 if (heapinfo2->type <= 0)
572 for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo2->type); j++)
573 if (i1 == state.heaplimit && heapinfo2->busy_frag.frag_size[j] > 0 && not state.equals_to2_(i, j).valid) {
574 XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)",
575 i, j, heapinfo2->busy_frag.frag_size[j]);
581 if (i1 == state.heaplimit)
582 XBT_DEBUG("Number of blocks/fragments not found in heap2: %d", nb_diff2);
584 return nb_diff1 > 0 || nb_diff2 > 0;
590 * @param real_area1 Process address for state 1
591 * @param real_area2 Process address for state 2
592 * @param snapshot1 Snapshot of state 1
593 * @param snapshot2 Snapshot of state 2
596 * @param check_ignore
598 static int compare_heap_area_without_type(
599 simgrid::mc::StateComparator& state, int process_index,
600 const void *real_area1, const void *real_area2,
601 simgrid::mc::Snapshot* snapshot1,
602 simgrid::mc::Snapshot* snapshot2,
603 HeapLocationPairs* previous, int size,
606 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
607 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
608 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
610 for (int i = 0; i < size; ) {
612 if (check_ignore > 0) {
613 ssize_t ignore1 = heap_comparison_ignore_size(
614 state.processStates[0].to_ignore, (char *) real_area1 + i);
616 ssize_t ignore2 = heap_comparison_ignore_size(
617 state.processStates[1].to_ignore, (char *) real_area2 + i);
618 if (ignore2 == ignore1) {
631 if (MC_snapshot_region_memcmp(((char *) real_area1) + i, heap_region1, ((char *) real_area2) + i, heap_region2, 1) != 0) {
633 int pointer_align = (i / sizeof(void *)) * sizeof(void *);
634 const void* addr_pointed1 = snapshot1->read(
635 remote((void**)((char *) real_area1 + pointer_align)), process_index);
636 const void* addr_pointed2 = snapshot2->read(
637 remote((void**)((char *) real_area2 + pointer_align)), process_index);
639 if (process->in_maestro_stack(remote(addr_pointed1))
640 && process->in_maestro_stack(remote(addr_pointed2))) {
641 i = pointer_align + sizeof(void *);
645 if (addr_pointed1 > state.std_heap_copy.heapbase
646 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
647 && addr_pointed2 > state.std_heap_copy.heapbase
648 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)) {
649 // Both addreses are in the heap:
650 int res_compare = compare_heap_area(state ,process_index,
651 addr_pointed1, addr_pointed2,
652 snapshot1, snapshot2, previous, nullptr, 0);
653 if (res_compare == 1)
655 i = pointer_align + sizeof(void *);
671 * @param real_area1 Process address for state 1
672 * @param real_area2 Process address for state 2
673 * @param snapshot1 Snapshot of state 1
674 * @param snapshot2 Snapshot of state 2
677 * @param area_size either a byte_size or an elements_count (?)
678 * @param check_ignore
679 * @param pointer_level
680 * @return 0 (same), 1 (different), -1 (unknown)
682 static int compare_heap_area_with_type(
683 simgrid::mc::StateComparator& state, int process_index,
684 const void *real_area1, const void *real_area2,
685 simgrid::mc::Snapshot* snapshot1,
686 simgrid::mc::Snapshot* snapshot2,
687 HeapLocationPairs* previous, simgrid::mc::Type* type,
688 int area_size, int check_ignore,
693 // HACK: This should not happen but in pratice, there are some
694 // DW_TAG_typedef without an associated DW_AT_type:
695 //<1><538832>: Abbrev Number: 111 (DW_TAG_typedef)
696 // <538833> DW_AT_name : (indirect string, offset: 0x2292f3): gregset_t
697 // <538837> DW_AT_decl_file : 98
698 // <538838> DW_AT_decl_line : 37
702 if (is_stack(real_area1) && is_stack(real_area2))
705 if (check_ignore > 0) {
706 ssize_t ignore1 = heap_comparison_ignore_size(
707 state.processStates[0].to_ignore, real_area1);
709 && heap_comparison_ignore_size(
710 state.processStates[1].to_ignore, real_area2) == ignore1)
714 simgrid::mc::Type* subtype;
715 simgrid::mc::Type* subsubtype;
718 const void* addr_pointed1;
719 const void* addr_pointed2;
721 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
722 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
724 switch (type->type) {
725 case DW_TAG_unspecified_type:
728 case DW_TAG_base_type:
729 if (not type->name.empty() && type->name == "char") { /* String, hence random (arbitrary ?) size */
730 if (real_area1 == real_area2)
733 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, area_size) != 0;
735 if (area_size != -1 && type->byte_size != area_size)
738 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
742 case DW_TAG_enumeration_type:
743 if (area_size != -1 && type->byte_size != area_size)
745 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
748 case DW_TAG_const_type:
749 case DW_TAG_volatile_type:
751 type = type->subtype;
754 case DW_TAG_array_type:
755 subtype = type->subtype;
756 switch (subtype->type) {
757 case DW_TAG_unspecified_type:
760 case DW_TAG_base_type:
761 case DW_TAG_enumeration_type:
762 case DW_TAG_pointer_type:
763 case DW_TAG_reference_type:
764 case DW_TAG_rvalue_reference_type:
765 case DW_TAG_structure_type:
766 case DW_TAG_class_type:
767 case DW_TAG_union_type:
768 if (subtype->full_type)
769 subtype = subtype->full_type;
770 elm_size = subtype->byte_size;
772 // TODO, just remove the type indirection?
773 case DW_TAG_const_type:
775 case DW_TAG_volatile_type:
776 subsubtype = subtype->subtype;
777 if (subsubtype->full_type)
778 subsubtype = subsubtype->full_type;
779 elm_size = subsubtype->byte_size;
785 for (int i = 0; i < type->element_count; i++) {
786 // TODO, add support for variable stride (DW_AT_byte_stride)
788 compare_heap_area_with_type(state, process_index,
789 (char *) real_area1 + (i * elm_size),
790 (char *) real_area2 + (i * elm_size),
791 snapshot1, snapshot2, previous,
792 type->subtype, subtype->byte_size,
793 check_ignore, pointer_level);
799 case DW_TAG_reference_type:
800 case DW_TAG_rvalue_reference_type:
801 case DW_TAG_pointer_type:
802 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
803 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
804 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
805 return (addr_pointed1 != addr_pointed2);
808 if (pointer_level <= 1) {
809 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
810 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
811 if (addr_pointed1 > state.std_heap_copy.heapbase
812 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
813 && addr_pointed2 > state.std_heap_copy.heapbase
814 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
815 return compare_heap_area(state, process_index,
816 addr_pointed1, addr_pointed2, snapshot1,
817 snapshot2, previous, type->subtype,
820 return (addr_pointed1 != addr_pointed2);
822 for (size_t i = 0; i < (area_size / sizeof(void *)); i++) {
823 addr_pointed1 = snapshot1->read(
824 remote((void**)((char*) real_area1 + i * sizeof(void *))),
826 addr_pointed2 = snapshot2->read(
827 remote((void**)((char*) real_area2 + i * sizeof(void *))),
829 if (addr_pointed1 > state.std_heap_copy.heapbase
830 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
831 && addr_pointed2 > state.std_heap_copy.heapbase
832 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
834 compare_heap_area(state, process_index,
835 addr_pointed1, addr_pointed2, snapshot1,
836 snapshot2, previous, type->subtype,
839 res = (addr_pointed1 != addr_pointed2);
845 case DW_TAG_structure_type:
846 case DW_TAG_class_type:
848 type = type->full_type;
849 if (area_size != -1 && type->byte_size != area_size) {
850 if (area_size <= type->byte_size || area_size % type->byte_size != 0)
852 for (size_t i = 0; i < (size_t)(area_size / type->byte_size); i++) {
853 int res = compare_heap_area_with_type(state, process_index,
854 (char *) real_area1 + i * type->byte_size,
855 (char *) real_area2 + i * type->byte_size,
856 snapshot1, snapshot2, previous, type, -1,
862 for (simgrid::mc::Member& member : type->members) {
863 // TODO, optimize this? (for the offset case)
864 void *real_member1 = simgrid::dwarf::resolve_member(
865 real_area1, type, &member, (simgrid::mc::AddressSpace*) snapshot1, process_index);
866 void *real_member2 = simgrid::dwarf::resolve_member(
867 real_area2, type, &member, (simgrid::mc::AddressSpace*) snapshot2, process_index);
868 int res = compare_heap_area_with_type(
869 state, process_index, real_member1, real_member2,
870 snapshot1, snapshot2,
871 previous, member.type, -1,
879 case DW_TAG_union_type:
880 return compare_heap_area_without_type(state, process_index, real_area1, real_area2,
881 snapshot1, snapshot2, previous,
882 type->byte_size, check_ignore);
888 xbt_die("Unreachable");
891 /** Infer the type of a part of the block from the type of the block
893 * TODO, handle DW_TAG_array_type as well as arrays of the object ((*p)[5], p[5])
895 * TODO, handle subfields ((*p).bar.foo, (*p)[5].bar…)
897 * @param type DWARF type ID of the root address
899 * @return DWARF type ID for given offset
901 static simgrid::mc::Type* get_offset_type(void *real_base_address, simgrid::mc::Type* type,
902 int offset, int area_size,
903 simgrid::mc::Snapshot* snapshot, int process_index)
906 // Beginning of the block, the infered variable type if the type of the block:
910 switch (type->type) {
912 case DW_TAG_structure_type:
913 case DW_TAG_class_type:
915 type = type->full_type;
916 if (area_size != -1 && type->byte_size != area_size) {
917 if (area_size > type->byte_size && area_size % type->byte_size == 0)
923 for (simgrid::mc::Member& member : type->members) {
924 if (member.has_offset_location()) {
925 // We have the offset, use it directly (shortcut):
926 if (member.offset() == offset)
929 void* real_member = simgrid::dwarf::resolve_member(real_base_address, type, &member, snapshot, process_index);
930 if ((char*)real_member - (char*)real_base_address == offset)
937 /* FIXME: other cases ? */
945 * @param area1 Process address for state 1
946 * @param area2 Process address for state 2
947 * @param snapshot1 Snapshot of state 1
948 * @param snapshot2 Snapshot of state 2
949 * @param previous Pairs of blocks already compared on the current path (or nullptr)
950 * @param type_id Type of variable
951 * @param pointer_level
952 * @return 0 (same), 1 (different), -1
955 int compare_heap_area(simgrid::mc::StateComparator& state, int process_index,
956 const void *area1, const void *area2,
957 simgrid::mc::Snapshot* snapshot1,
958 simgrid::mc::Snapshot* snapshot2,
959 HeapLocationPairs* previous,
960 simgrid::mc::Type* type, int pointer_level)
962 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
967 int check_ignore = 0;
975 simgrid::mc::Type* new_type1 = nullptr;
976 simgrid::mc::Type* new_type2 = nullptr;
978 bool match_pairs = false;
980 // This is the address of std_heap->heapinfo in the application process:
981 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
983 const malloc_info* heapinfos1 = snapshot1->read(remote((const malloc_info**)heapinfo_address), process_index);
984 const malloc_info* heapinfos2 = snapshot2->read(remote((const malloc_info**)heapinfo_address), process_index);
986 malloc_info heapinfo_temp1, heapinfo_temp2;
988 simgrid::mc::HeapLocationPairs current;
989 if (previous == nullptr) {
995 block1 = ((char*)area1 - (char*)state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
996 block2 = ((char*)area2 - (char*)state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
998 // If either block is a stack block:
999 if (is_block_stack((int) block1) && is_block_stack((int) block2)) {
1000 previous->insert(simgrid::mc::makeHeapLocationPair(block1, -1, block2, -1));
1002 state.match_equals(previous);
1006 // If either block is not in the expected area of memory:
1007 if (((char*)area1 < (char*)state.std_heap_copy.heapbase) || (block1 > (ssize_t)state.processStates[0].heapsize) ||
1008 (block1 < 1) || ((char*)area2 < (char*)state.std_heap_copy.heapbase) ||
1009 (block2 > (ssize_t)state.processStates[1].heapsize) || (block2 < 1)) {
1013 // Process address of the block:
1014 void* real_addr_block1 = (ADDR2UINT(block1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
1015 void* real_addr_block2 = (ADDR2UINT(block2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
1018 if (type->full_type)
1019 type = type->full_type;
1021 // This assume that for "boring" types (volatile ...) byte_size is absent:
1022 while (type->byte_size == 0 && type->subtype != nullptr)
1023 type = type->subtype;
1026 if (type->type == DW_TAG_pointer_type ||
1027 (type->type == DW_TAG_base_type && not type->name.empty() && type->name == "char"))
1030 type_size = type->byte_size;
1034 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
1035 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
1037 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
1038 heap_region1, &heapinfo_temp1, &heapinfos1[block1], sizeof(malloc_info));
1039 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
1040 heap_region2, &heapinfo_temp2, &heapinfos2[block2], sizeof(malloc_info));
1042 if ((heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type==MMALLOC_TYPE_HEAPINFO)
1043 && (heapinfo2->type == MMALLOC_TYPE_FREE || heapinfo2->type ==MMALLOC_TYPE_HEAPINFO)) {
1046 state.match_equals(previous);
1050 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED && heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
1051 /* Complete block */
1053 // TODO, lookup variable type from block type as done for fragmented blocks
1055 if (state.equals_to1_(block1, 0).valid && state.equals_to2_(block2, 0).valid && state.blocksEqual(block1, block2)) {
1057 state.match_equals(previous);
1061 if (type_size != -1) {
1062 if (type_size != (ssize_t) heapinfo1->busy_block.busy_size
1063 && type_size != (ssize_t) heapinfo2->busy_block.busy_size
1064 && (type->name.empty() || type->name == "struct s_smx_context")) {
1066 state.match_equals(previous);
1071 if (heapinfo1->busy_block.size != heapinfo2->busy_block.size)
1073 if (heapinfo1->busy_block.busy_size != heapinfo2->busy_block.busy_size)
1076 if (not previous->insert(simgrid::mc::makeHeapLocationPair(block1, -1, block2, -1)).second) {
1078 state.match_equals(previous);
1082 size = heapinfo1->busy_block.busy_size;
1084 // Remember (basic) type inference.
1085 // The current data structure only allows us to do this for the whole block.
1086 if (type != nullptr && area1 == real_addr_block1)
1087 state.types1_(block1, 0) = type;
1088 if (type != nullptr && area2 == real_addr_block2)
1089 state.types2_(block2, 0) = type;
1093 state.match_equals(previous);
1097 if (heapinfo1->busy_block.ignore > 0
1098 && heapinfo2->busy_block.ignore == heapinfo1->busy_block.ignore)
1099 check_ignore = heapinfo1->busy_block.ignore;
1101 } else if ((heapinfo1->type > 0) && (heapinfo2->type > 0)) { /* Fragmented block */
1104 ssize_t frag1 = ((uintptr_t)(ADDR2UINT(area1) % (BLOCKSIZE))) >> heapinfo1->type;
1105 ssize_t frag2 = ((uintptr_t)(ADDR2UINT(area2) % (BLOCKSIZE))) >> heapinfo2->type;
1107 // Process address of the fragment:
1108 void* real_addr_frag1 = (void*)((char*)real_addr_block1 + (frag1 << heapinfo1->type));
1109 void* real_addr_frag2 = (void*)((char*)real_addr_block2 + (frag2 << heapinfo2->type));
1111 // Check the size of the fragments against the size of the type:
1112 if (type_size != -1) {
1113 if (heapinfo1->busy_frag.frag_size[frag1] == -1 || heapinfo2->busy_frag.frag_size[frag2] == -1) {
1115 state.match_equals(previous);
1119 if (type_size != heapinfo1->busy_frag.frag_size[frag1]
1120 || type_size != heapinfo2->busy_frag.frag_size[frag2]) {
1122 state.match_equals(previous);
1127 // Check if the blocks are already matched together:
1128 if (state.equals_to1_(block1, frag1).valid && state.equals_to2_(block2, frag2).valid) {
1129 if (offset1==offset2 && state.fragmentsEqual(block1, frag1, block2, frag2)) {
1131 state.match_equals(previous);
1135 // Compare the size of both fragments:
1136 if (heapinfo1->busy_frag.frag_size[frag1] != heapinfo2->busy_frag.frag_size[frag2]) {
1137 if (type_size == -1) {
1139 state.match_equals(previous);
1145 // Size of the fragment:
1146 size = heapinfo1->busy_frag.frag_size[frag1];
1148 // Remember (basic) type inference.
1149 // The current data structure only allows us to do this for the whole fragment.
1150 if (type != nullptr && area1 == real_addr_frag1)
1151 state.types1_(block1, frag1) = type;
1152 if (type != nullptr && area2 == real_addr_frag2)
1153 state.types2_(block2, frag2) = type;
1155 // The type of the variable is already known:
1157 new_type1 = new_type2 = type;
1159 // Type inference from the block type.
1160 else if (state.types1_(block1, frag1) != nullptr || state.types2_(block2, frag2) != nullptr) {
1162 offset1 = (char*)area1 - (char*)real_addr_frag1;
1163 offset2 = (char*)area2 - (char*)real_addr_frag2;
1165 if (state.types1_(block1, frag1) != nullptr && state.types2_(block2, frag2) != nullptr) {
1167 get_offset_type(real_addr_frag1, state.types1_(block1, frag1), offset1, size, snapshot1, process_index);
1169 get_offset_type(real_addr_frag2, state.types2_(block2, frag2), offset1, size, snapshot2, process_index);
1170 } else if (state.types1_(block1, frag1) != nullptr) {
1172 get_offset_type(real_addr_frag1, state.types1_(block1, frag1), offset1, size, snapshot1, process_index);
1174 get_offset_type(real_addr_frag2, state.types1_(block1, frag1), offset2, size, snapshot2, process_index);
1175 } else if (state.types2_(block2, frag2) != nullptr) {
1177 get_offset_type(real_addr_frag1, state.types2_(block2, frag2), offset1, size, snapshot1, process_index);
1179 get_offset_type(real_addr_frag2, state.types2_(block2, frag2), offset2, size, snapshot2, process_index);
1182 state.match_equals(previous);
1186 if (new_type1 != nullptr && new_type2 != nullptr && new_type1 != new_type2) {
1189 while (type->byte_size == 0 && type->subtype != nullptr)
1190 type = type->subtype;
1191 new_size1 = type->byte_size;
1194 while (type->byte_size == 0 && type->subtype != nullptr)
1195 type = type->subtype;
1196 new_size2 = type->byte_size;
1200 state.match_equals(previous);
1205 if (new_size1 > 0 && new_size1 == new_size2) {
1210 if (offset1 == 0 && offset2 == 0 &&
1211 not previous->insert(simgrid::mc::makeHeapLocationPair(block1, frag1, block2, frag2)).second) {
1213 state.match_equals(previous);
1219 state.match_equals(previous);
1223 if ((heapinfo1->busy_frag.ignore[frag1] > 0) &&
1224 (heapinfo2->busy_frag.ignore[frag2] == heapinfo1->busy_frag.ignore[frag1]))
1225 check_ignore = heapinfo1->busy_frag.ignore[frag1];
1231 /* Start comparison */
1234 res_compare = compare_heap_area_with_type(state, process_index, area1, area2, snapshot1, snapshot2, previous, type,
1235 size, check_ignore, pointer_level);
1237 res_compare = compare_heap_area_without_type(state, process_index, area1, area2, snapshot1, snapshot2, previous,
1238 size, check_ignore);
1240 if (res_compare == 1)
1244 state.match_equals(previous);
1251 /************************** Snapshot comparison *******************************/
1252 /******************************************************************************/
1254 static int compare_areas_with_type(simgrid::mc::StateComparator& state,
1256 void* real_area1, simgrid::mc::Snapshot* snapshot1, mc_mem_region_t region1,
1257 void* real_area2, simgrid::mc::Snapshot* snapshot2, mc_mem_region_t region2,
1258 simgrid::mc::Type* type, int pointer_level)
1260 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
1262 simgrid::mc::Type* subtype;
1263 simgrid::mc::Type* subsubtype;
1269 switch (type->type) {
1270 case DW_TAG_unspecified_type:
1273 case DW_TAG_base_type:
1274 case DW_TAG_enumeration_type:
1275 case DW_TAG_union_type:
1276 return MC_snapshot_region_memcmp(real_area1, region1, real_area2, region2, type->byte_size) != 0;
1277 case DW_TAG_typedef:
1278 case DW_TAG_volatile_type:
1279 case DW_TAG_const_type:
1281 type = type->subtype;
1283 case DW_TAG_array_type:
1284 subtype = type->subtype;
1285 switch (subtype->type) {
1286 case DW_TAG_unspecified_type:
1289 case DW_TAG_base_type:
1290 case DW_TAG_enumeration_type:
1291 case DW_TAG_pointer_type:
1292 case DW_TAG_reference_type:
1293 case DW_TAG_rvalue_reference_type:
1294 case DW_TAG_structure_type:
1295 case DW_TAG_class_type:
1296 case DW_TAG_union_type:
1297 if (subtype->full_type)
1298 subtype = subtype->full_type;
1299 elm_size = subtype->byte_size;
1301 case DW_TAG_const_type:
1302 case DW_TAG_typedef:
1303 case DW_TAG_volatile_type:
1304 subsubtype = subtype->subtype;
1305 if (subsubtype->full_type)
1306 subsubtype = subsubtype->full_type;
1307 elm_size = subsubtype->byte_size;
1313 for (i = 0; i < type->element_count; i++) {
1314 size_t off = i * elm_size;
1315 res = compare_areas_with_type(state, process_index,
1316 (char*) real_area1 + off, snapshot1, region1,
1317 (char*) real_area2 + off, snapshot2, region2,
1318 type->subtype, pointer_level);
1323 case DW_TAG_pointer_type:
1324 case DW_TAG_reference_type:
1325 case DW_TAG_rvalue_reference_type:
1327 void* addr_pointed1 = MC_region_read_pointer(region1, real_area1);
1328 void* addr_pointed2 = MC_region_read_pointer(region2, real_area2);
1330 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type)
1331 return (addr_pointed1 != addr_pointed2);
1332 if (addr_pointed1 == nullptr && addr_pointed2 == nullptr)
1334 if (addr_pointed1 == nullptr || addr_pointed2 == nullptr)
1336 if (not state.compared_pointers.insert(std::make_pair(addr_pointed1, addr_pointed2)).second)
1341 // Some cases are not handled here:
1342 // * the pointers lead to different areas (one to the heap, the other to the RW segment ...);
1343 // * a pointer leads to the read-only segment of the current object;
1344 // * a pointer lead to a different ELF object.
1346 if (addr_pointed1 > process->heap_address
1347 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)) {
1348 if (not(addr_pointed2 > process->heap_address && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)))
1350 // The pointers are both in the heap:
1351 return simgrid::mc::compare_heap_area(state,
1352 process_index, addr_pointed1, addr_pointed2, snapshot1,
1353 snapshot2, nullptr, type->subtype, pointer_level);
1356 // The pointers are both in the current object R/W segment:
1357 else if (region1->contain(simgrid::mc::remote(addr_pointed1))) {
1358 if (not region2->contain(simgrid::mc::remote(addr_pointed2)))
1360 if (not type->type_id)
1361 return (addr_pointed1 != addr_pointed2);
1363 return compare_areas_with_type(state, process_index,
1364 addr_pointed1, snapshot1, region1,
1365 addr_pointed2, snapshot2, region2,
1366 type->subtype, pointer_level);
1369 // TODO, We do not handle very well the case where
1370 // it belongs to a different (non-heap) region from the current one.
1373 return (addr_pointed1 != addr_pointed2);
1377 case DW_TAG_structure_type:
1378 case DW_TAG_class_type:
1379 for (simgrid::mc::Member& member : type->members) {
1380 void *member1 = simgrid::dwarf::resolve_member(
1381 real_area1, type, &member, snapshot1, process_index);
1382 void *member2 = simgrid::dwarf::resolve_member(
1383 real_area2, type, &member, snapshot2, process_index);
1384 mc_mem_region_t subregion1 = mc_get_region_hinted(member1, snapshot1, process_index, region1);
1385 mc_mem_region_t subregion2 = mc_get_region_hinted(member2, snapshot2, process_index, region2);
1387 compare_areas_with_type(state, process_index,
1388 member1, snapshot1, subregion1,
1389 member2, snapshot2, subregion2,
1390 member.type, pointer_level);
1395 case DW_TAG_subroutine_type:
1399 XBT_VERB("Unknown case: %d", type->type);
1406 static int compare_global_variables(
1407 simgrid::mc::StateComparator& state,
1408 simgrid::mc::ObjectInformation* object_info,
1410 mc_mem_region_t r1, mc_mem_region_t r2,
1411 simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2)
1413 xbt_assert(r1 && r2, "Missing region.");
1416 if (r1->storage_type() == simgrid::mc::StorageType::Privatized) {
1417 xbt_assert(process_index >= 0);
1418 if (r2->storage_type() != simgrid::mc::StorageType::Privatized)
1421 size_t process_count = MC_smpi_process_count();
1422 xbt_assert(process_count == r1->privatized_data().size()
1423 && process_count == r2->privatized_data().size());
1425 // Compare the global variables separately for each simulates process:
1426 for (size_t process_index = 0; process_index < process_count; process_index++) {
1427 if (compare_global_variables(state,
1428 object_info, process_index,
1429 &r1->privatized_data()[process_index],
1430 &r2->privatized_data()[process_index],
1431 snapshot1, snapshot2))
1437 xbt_assert(r1->storage_type() != simgrid::mc::StorageType::Privatized);
1439 xbt_assert(r2->storage_type() != simgrid::mc::StorageType::Privatized);
1441 std::vector<simgrid::mc::Variable>& variables = object_info->global_variables;
1443 for (simgrid::mc::Variable const& current_var : variables) {
1445 // If the variable is not in this object, skip it:
1446 // We do not expect to find a pointer to something which is not reachable
1447 // by the global variables.
1448 if ((char *) current_var.address < (char *) object_info->start_rw
1449 || (char *) current_var.address > (char *) object_info->end_rw)
1452 simgrid::mc::Type* bvariable_type = current_var.type;
1453 int res = compare_areas_with_type(state, process_index,
1454 (char *) current_var.address, snapshot1, r1,
1455 (char *) current_var.address, snapshot2, r2,
1458 XBT_VERB("Global variable %s (%p) is different between snapshots",
1459 current_var.name.c_str(),
1460 (char *) current_var.address);
1468 static int compare_local_variables(simgrid::mc::StateComparator& state,
1470 simgrid::mc::Snapshot* snapshot1,
1471 simgrid::mc::Snapshot* snapshot2,
1472 mc_snapshot_stack_t stack1,
1473 mc_snapshot_stack_t stack2)
1475 if (stack1->local_variables.size() != stack2->local_variables.size()) {
1476 XBT_VERB("Different number of local variables");
1480 unsigned int cursor = 0;
1481 local_variable_t current_var1, current_var2;
1482 while (cursor < stack1->local_variables.size()) {
1483 current_var1 = &stack1->local_variables[cursor];
1484 current_var2 = &stack1->local_variables[cursor];
1485 if (current_var1->name != current_var2->name
1486 || current_var1->subprogram != current_var2->subprogram
1487 || current_var1->ip != current_var2->ip) {
1488 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1490 ("Different name of variable (%s - %s) "
1491 "or frame (%s - %s) or ip (%lu - %lu)",
1492 current_var1->name.c_str(),
1493 current_var2->name.c_str(),
1494 current_var1->subprogram->name.c_str(),
1495 current_var2->subprogram->name.c_str(),
1496 current_var1->ip, current_var2->ip);
1499 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1501 simgrid::mc::Type* subtype = current_var1->type;
1502 int res = compare_areas_with_type(
1503 state, process_index, current_var1->address, snapshot1,
1504 mc_get_snapshot_region(current_var1->address, snapshot1, process_index), current_var2->address, snapshot2,
1505 mc_get_snapshot_region(current_var2->address, snapshot2, process_index), subtype, 0);
1508 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1509 XBT_VERB("Local variable %s (%p - %p) in frame %s "
1510 "is different between snapshots",
1511 current_var1->name.c_str(), current_var1->address, current_var2->address,
1512 current_var1->subprogram->name.c_str());
1523 static std::unique_ptr<simgrid::mc::StateComparator> state_comparator;
1525 int snapshot_compare(int num1, simgrid::mc::Snapshot* s1, int num2, simgrid::mc::Snapshot* s2)
1527 // TODO, make this a field of ModelChecker or something similar
1529 if (state_comparator == nullptr)
1530 state_comparator = std::unique_ptr<StateComparator>(new StateComparator());
1532 state_comparator->clear();
1534 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
1538 int hash_result = 0;
1540 hash_result = (s1->hash != s2->hash);
1542 XBT_VERB("(%d - %d) Different hash: 0x%" PRIx64 "--0x%" PRIx64, num1, num2, s1->hash, s2->hash);
1547 XBT_VERB("(%d - %d) Same hash: 0x%" PRIx64, num1, num2, s1->hash);
1550 /* Compare enabled processes */
1551 if (s1->enabled_processes != s2->enabled_processes) {
1552 XBT_VERB("(%d - %d) Different amount of enabled processes", num1, num2);
1556 /* Compare size of stacks */
1558 for (unsigned long i = 0; i < s1->stacks.size(); i++) {
1559 size_t size_used1 = s1->stack_sizes[i];
1560 size_t size_used2 = s2->stack_sizes[i];
1561 if (size_used1 != size_used2) {
1563 XBT_DEBUG("(%d - %d) Different size used in stacks: %zu - %zu", num1, num2, size_used1, size_used2);
1568 XBT_VERB("(%d - %d) Different size used in stacks: %zu - %zu", num1, num2, size_used1, size_used2);
1574 if (is_diff) // do not proceed if there is any stacks that don't match
1577 /* Init heap information used in heap comparison algorithm */
1578 xbt_mheap_t heap1 = (xbt_mheap_t)s1->read_bytes(
1579 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1580 remote(process->heap_address),
1581 simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1582 xbt_mheap_t heap2 = (xbt_mheap_t)s2->read_bytes(
1583 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1584 remote(process->heap_address),
1585 simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1586 int res_init = state_comparator->initHeapInformation(heap1, heap2, &s1->to_ignore, &s2->to_ignore);
1588 if (res_init == -1) {
1590 XBT_DEBUG("(%d - %d) Different heap information", num1, num2);
1594 XBT_VERB("(%d - %d) Different heap information", num1, num2);
1601 /* Stacks comparison */
1603 for (unsigned int cursor = 0; cursor < s1->stacks.size(); cursor++) {
1604 mc_snapshot_stack_t stack1 = &s1->stacks[cursor];
1605 mc_snapshot_stack_t stack2 = &s2->stacks[cursor];
1607 if (stack1->process_index != stack2->process_index) {
1609 XBT_DEBUG("(%d - %d) Stacks with different process index (%i vs %i)", num1, num2,
1610 stack1->process_index, stack2->process_index);
1612 else diff_local = compare_local_variables(*state_comparator,
1613 stack1->process_index, s1, s2, stack1, stack2);
1614 if (diff_local > 0) {
1616 XBT_DEBUG("(%d - %d) Different local variables between stacks %d", num1,
1622 XBT_VERB("(%d - %d) Different local variables between stacks %u", num1, num2, cursor + 1);
1630 size_t regions_count = s1->snapshot_regions.size();
1631 // TODO, raise a difference instead?
1632 xbt_assert(regions_count == s2->snapshot_regions.size());
1634 for (size_t k = 0; k != regions_count; ++k) {
1635 mc_mem_region_t region1 = s1->snapshot_regions[k].get();
1636 mc_mem_region_t region2 = s2->snapshot_regions[k].get();
1639 if (region1->region_type() != simgrid::mc::RegionType::Data)
1642 xbt_assert(region1->region_type() == region2->region_type());
1643 xbt_assert(region1->object_info() == region2->object_info());
1644 xbt_assert(region1->object_info());
1646 std::string const& name = region1->object_info()->file_name;
1648 /* Compare global variables */
1649 if (compare_global_variables(*state_comparator, region1->object_info(), simgrid::mc::ProcessIndexDisabled, region1,
1653 XBT_DEBUG("(%d - %d) Different global variables in %s",
1654 num1, num2, name.c_str());
1658 XBT_VERB("(%d - %d) Different global variables in %s",
1659 num1, num2, name.c_str());
1668 if (simgrid::mc::mmalloc_compare_heap(*state_comparator, s1, s2) > 0) {
1671 XBT_DEBUG("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
1676 XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
1683 if (errors || hash_result)
1684 XBT_VERB("(%d - %d) Difference found", num1, num2);
1686 XBT_VERB("(%d - %d) No difference found", num1, num2);
1689 #if defined(MC_DEBUG) && defined(MC_VERBOSE)
1691 // * false positive SHOULD be avoided.
1692 // * There MUST not be any false negative.
1694 XBT_VERB("(%d - %d) State equality hash test is %s %s", num1, num2,
1695 (hash_result != 0) == (errors != 0) ? "true" : "false", not hash_result ? "positive" : "negative");
1699 return errors > 0 || hash_result;