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 {
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 const& 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::RemoteClient* process = &mc_model_checker->process();
321 /* Start comparison */
335 /* Check busy blocks */
338 malloc_info heapinfo_temp1;
339 malloc_info heapinfo_temp2;
340 malloc_info heapinfo_temp2b;
342 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
343 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
345 // This is the address of std_heap->heapinfo in the application process:
346 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
348 // This is in snapshot do not use them directly:
349 const malloc_info* heapinfos1 = snapshot1->read<malloc_info*>(
350 (std::uint64_t)heapinfo_address, simgrid::mc::ProcessIndexMissing);
351 const malloc_info* heapinfos2 = snapshot2->read<malloc_info*>(
352 (std::uint64_t)heapinfo_address, simgrid::mc::ProcessIndexMissing);
354 while (i1 < state.heaplimit) {
356 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(heap_region1, &heapinfo_temp1, &heapinfos1[i1], sizeof(malloc_info));
357 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2, &heapinfos2[i1], sizeof(malloc_info));
359 if (heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type == MMALLOC_TYPE_HEAPINFO) { /* Free block */
364 if (heapinfo1->type < 0) {
365 fprintf(stderr, "Unkown mmalloc block type.\n");
369 addr_block1 = ((void*)(((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase));
371 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED) { /* Large block */
373 if (is_stack(addr_block1)) {
374 for (k = 0; k < heapinfo1->busy_block.size; k++)
375 state.equals_to1_(i1 + k, 0) = HeapArea(i1, -1);
376 for (k = 0; k < heapinfo2->busy_block.size; k++)
377 state.equals_to2_(i1 + k, 0) = HeapArea(i1, -1);
378 i1 += heapinfo1->busy_block.size;
382 if (state.equals_to1_(i1, 0).valid) {
390 /* Try first to associate to same block in the other heap */
391 if (heapinfo2->type == heapinfo1->type
392 && state.equals_to2_(i1, 0).valid == 0) {
393 addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
394 int res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing, addr_block1, addr_block2,
395 snapshot1, snapshot2, nullptr, nullptr, 0);
396 if (res_compare != 1) {
397 for (k = 1; k < heapinfo2->busy_block.size; k++)
398 state.equals_to2_(i1 + k, 0) = HeapArea(i1, -1);
399 for (k = 1; k < heapinfo1->busy_block.size; k++)
400 state.equals_to1_(i1 + k, 0) = HeapArea(i1, -1);
402 i1 += heapinfo1->busy_block.size;
406 while (i2 < state.heaplimit && not equal) {
408 addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
415 const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2b, &heapinfos2[i2], sizeof(malloc_info));
417 if (heapinfo2b->type != MMALLOC_TYPE_UNFRAGMENTED) {
422 if (state.equals_to2_(i2, 0).valid) {
427 int res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing, addr_block1, addr_block2,
428 snapshot1, snapshot2, nullptr, nullptr, 0);
430 if (res_compare != 1) {
431 for (k = 1; k < heapinfo2b->busy_block.size; k++)
432 state.equals_to2_(i2 + k, 0) = HeapArea(i1, -1);
433 for (k = 1; k < heapinfo1->busy_block.size; k++)
434 state.equals_to1_(i1 + k, 0) = HeapArea(i2, -1);
436 i1 += heapinfo1->busy_block.size;
443 XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1, heapinfo1->busy_block.busy_size, addr_block1);
444 i1 = state.heaplimit + 1;
449 } else { /* Fragmented block */
451 for (j1 = 0; j1 < (size_t) (BLOCKSIZE >> heapinfo1->type); j1++) {
453 if (heapinfo1->busy_frag.frag_size[j1] == -1) /* Free fragment */
456 if (state.equals_to1_(i1, j1).valid)
459 addr_frag1 = (void*)((char*)addr_block1 + (j1 << heapinfo1->type));
464 /* Try first to associate to same fragment in the other heap */
465 if (heapinfo2->type == heapinfo1->type && not state.equals_to2_(i1, j1).valid) {
466 addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE +
467 (char *) state.std_heap_copy.heapbase;
469 (void *) ((char *) addr_block2 +
470 (j1 << heapinfo2->type));
471 int res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing, addr_frag1, addr_frag2,
472 snapshot1, snapshot2, nullptr, nullptr, 0);
473 if (res_compare != 1)
477 while (i2 < state.heaplimit && not equal) {
479 const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(
480 heap_region2, &heapinfo_temp2b, &heapinfos2[i2],
481 sizeof(malloc_info));
483 if (heapinfo2b->type == MMALLOC_TYPE_FREE || heapinfo2b->type == MMALLOC_TYPE_HEAPINFO) {
488 // We currently do not match fragments with unfragmented blocks (maybe we should).
489 if (heapinfo2b->type == MMALLOC_TYPE_UNFRAGMENTED) {
494 if (heapinfo2b->type < 0) {
495 fprintf(stderr, "Unknown mmalloc block type.\n");
499 for (j2 = 0; j2 < (size_t) (BLOCKSIZE >> heapinfo2b->type);
502 if (i2 == i1 && j2 == j1)
505 if (state.equals_to2_(i2, j2).valid)
508 addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
509 addr_frag2 = (void*)((char*)addr_block2 + (j2 << heapinfo2b->type));
511 int res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing, addr_frag1, addr_frag2,
512 snapshot2, snapshot2, nullptr, nullptr, 0);
513 if (res_compare != 1) {
523 XBT_DEBUG("Block %zu, fragment %zu not found (size_used = %zd, address = %p)\n", i1, j1,
524 heapinfo1->busy_frag.frag_size[j1], addr_frag1);
525 i1 = state.heaplimit + 1;
535 /* All blocks/fragments are equal to another block/fragment ? */
539 for(i = 1; i < state.heaplimit; i++) {
540 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
541 heap_region1, &heapinfo_temp1, &heapinfos1[i], sizeof(malloc_info));
543 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED && i1 == state.heaplimit && heapinfo1->busy_block.busy_size > 0 &&
544 not state.equals_to1_(i, 0).valid) {
545 XBT_DEBUG("Block %zu not found (size used = %zu)", i, heapinfo1->busy_block.busy_size);
549 if (heapinfo1->type <= 0)
551 for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo1->type); j++)
552 if (i1 == state.heaplimit && heapinfo1->busy_frag.frag_size[j] > 0 && not state.equals_to1_(i, j).valid) {
553 XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)", i, j, heapinfo1->busy_frag.frag_size[j]);
558 if (i1 == state.heaplimit)
559 XBT_DEBUG("Number of blocks/fragments not found in heap1: %d", nb_diff1);
561 for (i=1; i < state.heaplimit; i++) {
562 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
563 heap_region2, &heapinfo_temp2, &heapinfos2[i], sizeof(malloc_info));
564 if (heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED && i1 == state.heaplimit && heapinfo2->busy_block.busy_size > 0 &&
565 not state.equals_to2_(i, 0).valid) {
566 XBT_DEBUG("Block %zu not found (size used = %zu)", i,
567 heapinfo2->busy_block.busy_size);
571 if (heapinfo2->type <= 0)
574 for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo2->type); j++)
575 if (i1 == state.heaplimit && heapinfo2->busy_frag.frag_size[j] > 0 && not state.equals_to2_(i, j).valid) {
576 XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)",
577 i, j, heapinfo2->busy_frag.frag_size[j]);
583 if (i1 == state.heaplimit)
584 XBT_DEBUG("Number of blocks/fragments not found in heap2: %d", nb_diff2);
586 return nb_diff1 > 0 || nb_diff2 > 0;
592 * @param real_area1 Process address for state 1
593 * @param real_area2 Process address for state 2
594 * @param snapshot1 Snapshot of state 1
595 * @param snapshot2 Snapshot of state 2
598 * @param check_ignore
600 static int compare_heap_area_without_type(
601 simgrid::mc::StateComparator& state, int process_index,
602 const void *real_area1, const void *real_area2,
603 simgrid::mc::Snapshot* snapshot1,
604 simgrid::mc::Snapshot* snapshot2,
605 HeapLocationPairs* previous, int size,
608 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
609 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
610 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
612 for (int i = 0; i < size; ) {
614 if (check_ignore > 0) {
615 ssize_t ignore1 = heap_comparison_ignore_size(
616 state.processStates[0].to_ignore, (char *) real_area1 + i);
618 ssize_t ignore2 = heap_comparison_ignore_size(
619 state.processStates[1].to_ignore, (char *) real_area2 + i);
620 if (ignore2 == ignore1) {
633 if (MC_snapshot_region_memcmp(((char *) real_area1) + i, heap_region1, ((char *) real_area2) + i, heap_region2, 1) != 0) {
635 int pointer_align = (i / sizeof(void *)) * sizeof(void *);
636 const void* addr_pointed1 = snapshot1->read(
637 remote((void**)((char *) real_area1 + pointer_align)), process_index);
638 const void* addr_pointed2 = snapshot2->read(
639 remote((void**)((char *) real_area2 + pointer_align)), process_index);
641 if (process->in_maestro_stack(remote(addr_pointed1))
642 && process->in_maestro_stack(remote(addr_pointed2))) {
643 i = pointer_align + sizeof(void *);
647 if (addr_pointed1 > state.std_heap_copy.heapbase
648 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
649 && addr_pointed2 > state.std_heap_copy.heapbase
650 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)) {
651 // Both addreses are in the heap:
652 int res_compare = compare_heap_area(state ,process_index,
653 addr_pointed1, addr_pointed2,
654 snapshot1, snapshot2, previous, nullptr, 0);
655 if (res_compare == 1)
657 i = pointer_align + sizeof(void *);
673 * @param real_area1 Process address for state 1
674 * @param real_area2 Process address for state 2
675 * @param snapshot1 Snapshot of state 1
676 * @param snapshot2 Snapshot of state 2
679 * @param area_size either a byte_size or an elements_count (?)
680 * @param check_ignore
681 * @param pointer_level
682 * @return 0 (same), 1 (different), -1 (unknown)
684 static int compare_heap_area_with_type(
685 simgrid::mc::StateComparator& state, int process_index,
686 const void *real_area1, const void *real_area2,
687 simgrid::mc::Snapshot* snapshot1,
688 simgrid::mc::Snapshot* snapshot2,
689 HeapLocationPairs* previous, simgrid::mc::Type* type,
690 int area_size, int check_ignore,
695 // HACK: This should not happen but in pratice, there are some
696 // DW_TAG_typedef without an associated DW_AT_type:
697 //<1><538832>: Abbrev Number: 111 (DW_TAG_typedef)
698 // <538833> DW_AT_name : (indirect string, offset: 0x2292f3): gregset_t
699 // <538837> DW_AT_decl_file : 98
700 // <538838> DW_AT_decl_line : 37
704 if (is_stack(real_area1) && is_stack(real_area2))
707 if (check_ignore > 0) {
708 ssize_t ignore1 = heap_comparison_ignore_size(
709 state.processStates[0].to_ignore, real_area1);
711 && heap_comparison_ignore_size(
712 state.processStates[1].to_ignore, real_area2) == ignore1)
716 simgrid::mc::Type* subtype;
717 simgrid::mc::Type* subsubtype;
720 const void* addr_pointed1;
721 const void* addr_pointed2;
723 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
724 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
726 switch (type->type) {
727 case DW_TAG_unspecified_type:
730 case DW_TAG_base_type:
731 if (not type->name.empty() && type->name == "char") { /* String, hence random (arbitrary ?) size */
732 if (real_area1 == real_area2)
735 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, area_size) != 0;
737 if (area_size != -1 && type->byte_size != area_size)
740 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
744 case DW_TAG_enumeration_type:
745 if (area_size != -1 && type->byte_size != area_size)
747 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
750 case DW_TAG_const_type:
751 case DW_TAG_volatile_type:
753 type = type->subtype;
756 case DW_TAG_array_type:
757 subtype = type->subtype;
758 switch (subtype->type) {
759 case DW_TAG_unspecified_type:
762 case DW_TAG_base_type:
763 case DW_TAG_enumeration_type:
764 case DW_TAG_pointer_type:
765 case DW_TAG_reference_type:
766 case DW_TAG_rvalue_reference_type:
767 case DW_TAG_structure_type:
768 case DW_TAG_class_type:
769 case DW_TAG_union_type:
770 if (subtype->full_type)
771 subtype = subtype->full_type;
772 elm_size = subtype->byte_size;
774 // TODO, just remove the type indirection?
775 case DW_TAG_const_type:
777 case DW_TAG_volatile_type:
778 subsubtype = subtype->subtype;
779 if (subsubtype->full_type)
780 subsubtype = subsubtype->full_type;
781 elm_size = subsubtype->byte_size;
787 for (int i = 0; i < type->element_count; i++) {
788 // TODO, add support for variable stride (DW_AT_byte_stride)
790 compare_heap_area_with_type(state, process_index,
791 (char *) real_area1 + (i * elm_size),
792 (char *) real_area2 + (i * elm_size),
793 snapshot1, snapshot2, previous,
794 type->subtype, subtype->byte_size,
795 check_ignore, pointer_level);
801 case DW_TAG_reference_type:
802 case DW_TAG_rvalue_reference_type:
803 case DW_TAG_pointer_type:
804 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
805 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
806 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
807 return (addr_pointed1 != addr_pointed2);
810 if (pointer_level <= 1) {
811 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
812 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
813 if (addr_pointed1 > state.std_heap_copy.heapbase
814 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
815 && addr_pointed2 > state.std_heap_copy.heapbase
816 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
817 return compare_heap_area(state, process_index,
818 addr_pointed1, addr_pointed2, snapshot1,
819 snapshot2, previous, type->subtype,
822 return (addr_pointed1 != addr_pointed2);
824 for (size_t i = 0; i < (area_size / sizeof(void *)); i++) {
825 addr_pointed1 = snapshot1->read(
826 remote((void**)((char*) real_area1 + i * sizeof(void *))),
828 addr_pointed2 = snapshot2->read(
829 remote((void**)((char*) real_area2 + i * sizeof(void *))),
831 if (addr_pointed1 > state.std_heap_copy.heapbase
832 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
833 && addr_pointed2 > state.std_heap_copy.heapbase
834 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
836 compare_heap_area(state, process_index,
837 addr_pointed1, addr_pointed2, snapshot1,
838 snapshot2, previous, type->subtype,
841 res = (addr_pointed1 != addr_pointed2);
847 case DW_TAG_structure_type:
848 case DW_TAG_class_type:
850 type = type->full_type;
851 if (area_size != -1 && type->byte_size != area_size) {
852 if (area_size <= type->byte_size || area_size % type->byte_size != 0)
854 for (size_t i = 0; i < (size_t)(area_size / type->byte_size); i++) {
855 int res = compare_heap_area_with_type(state, process_index,
856 (char *) real_area1 + i * type->byte_size,
857 (char *) real_area2 + i * type->byte_size,
858 snapshot1, snapshot2, previous, type, -1,
864 for (simgrid::mc::Member& member : type->members) {
865 // TODO, optimize this? (for the offset case)
866 void *real_member1 = simgrid::dwarf::resolve_member(
867 real_area1, type, &member, (simgrid::mc::AddressSpace*) snapshot1, process_index);
868 void *real_member2 = simgrid::dwarf::resolve_member(
869 real_area2, type, &member, (simgrid::mc::AddressSpace*) snapshot2, process_index);
870 int res = compare_heap_area_with_type(
871 state, process_index, real_member1, real_member2,
872 snapshot1, snapshot2,
873 previous, member.type, -1,
881 case DW_TAG_union_type:
882 return compare_heap_area_without_type(state, process_index, real_area1, real_area2,
883 snapshot1, snapshot2, previous,
884 type->byte_size, check_ignore);
890 xbt_die("Unreachable");
893 /** Infer the type of a part of the block from the type of the block
895 * TODO, handle DW_TAG_array_type as well as arrays of the object ((*p)[5], p[5])
897 * TODO, handle subfields ((*p).bar.foo, (*p)[5].bar…)
899 * @param type DWARF type ID of the root address
901 * @return DWARF type ID for given offset
903 static simgrid::mc::Type* get_offset_type(void *real_base_address, simgrid::mc::Type* type,
904 int offset, int area_size,
905 simgrid::mc::Snapshot* snapshot, int process_index)
908 // Beginning of the block, the infered variable type if the type of the block:
912 switch (type->type) {
914 case DW_TAG_structure_type:
915 case DW_TAG_class_type:
917 type = type->full_type;
918 if (area_size != -1 && type->byte_size != area_size) {
919 if (area_size > type->byte_size && area_size % type->byte_size == 0)
925 for (simgrid::mc::Member& member : type->members) {
926 if (member.has_offset_location()) {
927 // We have the offset, use it directly (shortcut):
928 if (member.offset() == offset)
931 void* real_member = simgrid::dwarf::resolve_member(real_base_address, type, &member, snapshot, process_index);
932 if ((char*)real_member - (char*)real_base_address == offset)
939 /* FIXME: other cases ? */
947 * @param area1 Process address for state 1
948 * @param area2 Process address for state 2
949 * @param snapshot1 Snapshot of state 1
950 * @param snapshot2 Snapshot of state 2
951 * @param previous Pairs of blocks already compared on the current path (or nullptr)
952 * @param type_id Type of variable
953 * @param pointer_level
954 * @return 0 (same), 1 (different), -1
957 int compare_heap_area(simgrid::mc::StateComparator& state, int process_index,
958 const void *area1, const void *area2,
959 simgrid::mc::Snapshot* snapshot1,
960 simgrid::mc::Snapshot* snapshot2,
961 HeapLocationPairs* previous,
962 simgrid::mc::Type* type, int pointer_level)
964 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
969 int check_ignore = 0;
977 simgrid::mc::Type* new_type1 = nullptr;
978 simgrid::mc::Type* new_type2 = nullptr;
980 bool match_pairs = false;
982 // This is the address of std_heap->heapinfo in the application process:
983 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
985 const malloc_info* heapinfos1 = snapshot1->read(remote((const malloc_info**)heapinfo_address), process_index);
986 const malloc_info* heapinfos2 = snapshot2->read(remote((const malloc_info**)heapinfo_address), process_index);
988 malloc_info heapinfo_temp1, heapinfo_temp2;
990 simgrid::mc::HeapLocationPairs current;
991 if (previous == nullptr) {
997 block1 = ((char*)area1 - (char*)state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
998 block2 = ((char*)area2 - (char*)state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
1000 // If either block is a stack block:
1001 if (is_block_stack((int) block1) && is_block_stack((int) block2)) {
1002 previous->insert(simgrid::mc::makeHeapLocationPair(block1, -1, block2, -1));
1004 state.match_equals(previous);
1008 // If either block is not in the expected area of memory:
1009 if (((char*)area1 < (char*)state.std_heap_copy.heapbase) || (block1 > (ssize_t)state.processStates[0].heapsize) ||
1010 (block1 < 1) || ((char*)area2 < (char*)state.std_heap_copy.heapbase) ||
1011 (block2 > (ssize_t)state.processStates[1].heapsize) || (block2 < 1)) {
1015 // Process address of the block:
1016 void* real_addr_block1 = (ADDR2UINT(block1) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
1017 void* real_addr_block2 = (ADDR2UINT(block2) - 1) * BLOCKSIZE + (char*)state.std_heap_copy.heapbase;
1020 if (type->full_type)
1021 type = type->full_type;
1023 // This assume that for "boring" types (volatile ...) byte_size is absent:
1024 while (type->byte_size == 0 && type->subtype != nullptr)
1025 type = type->subtype;
1028 if (type->type == DW_TAG_pointer_type ||
1029 (type->type == DW_TAG_base_type && not type->name.empty() && type->name == "char"))
1032 type_size = type->byte_size;
1036 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
1037 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
1039 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
1040 heap_region1, &heapinfo_temp1, &heapinfos1[block1], sizeof(malloc_info));
1041 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
1042 heap_region2, &heapinfo_temp2, &heapinfos2[block2], sizeof(malloc_info));
1044 if ((heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type==MMALLOC_TYPE_HEAPINFO)
1045 && (heapinfo2->type == MMALLOC_TYPE_FREE || heapinfo2->type ==MMALLOC_TYPE_HEAPINFO)) {
1048 state.match_equals(previous);
1052 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED && heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
1053 /* Complete block */
1055 // TODO, lookup variable type from block type as done for fragmented blocks
1057 if (state.equals_to1_(block1, 0).valid && state.equals_to2_(block2, 0).valid && state.blocksEqual(block1, block2)) {
1059 state.match_equals(previous);
1063 if (type_size != -1) {
1064 if (type_size != (ssize_t) heapinfo1->busy_block.busy_size
1065 && type_size != (ssize_t) heapinfo2->busy_block.busy_size
1066 && (type->name.empty() || type->name == "struct s_smx_context")) {
1068 state.match_equals(previous);
1073 if (heapinfo1->busy_block.size != heapinfo2->busy_block.size)
1075 if (heapinfo1->busy_block.busy_size != heapinfo2->busy_block.busy_size)
1078 if (not previous->insert(simgrid::mc::makeHeapLocationPair(block1, -1, block2, -1)).second) {
1080 state.match_equals(previous);
1084 size = heapinfo1->busy_block.busy_size;
1086 // Remember (basic) type inference.
1087 // The current data structure only allows us to do this for the whole block.
1088 if (type != nullptr && area1 == real_addr_block1)
1089 state.types1_(block1, 0) = type;
1090 if (type != nullptr && area2 == real_addr_block2)
1091 state.types2_(block2, 0) = type;
1095 state.match_equals(previous);
1099 if (heapinfo1->busy_block.ignore > 0
1100 && heapinfo2->busy_block.ignore == heapinfo1->busy_block.ignore)
1101 check_ignore = heapinfo1->busy_block.ignore;
1103 } else if ((heapinfo1->type > 0) && (heapinfo2->type > 0)) { /* Fragmented block */
1106 ssize_t frag1 = ((uintptr_t)(ADDR2UINT(area1) % (BLOCKSIZE))) >> heapinfo1->type;
1107 ssize_t frag2 = ((uintptr_t)(ADDR2UINT(area2) % (BLOCKSIZE))) >> heapinfo2->type;
1109 // Process address of the fragment:
1110 void* real_addr_frag1 = (void*)((char*)real_addr_block1 + (frag1 << heapinfo1->type));
1111 void* real_addr_frag2 = (void*)((char*)real_addr_block2 + (frag2 << heapinfo2->type));
1113 // Check the size of the fragments against the size of the type:
1114 if (type_size != -1) {
1115 if (heapinfo1->busy_frag.frag_size[frag1] == -1 || heapinfo2->busy_frag.frag_size[frag2] == -1) {
1117 state.match_equals(previous);
1121 if (type_size != heapinfo1->busy_frag.frag_size[frag1]
1122 || type_size != heapinfo2->busy_frag.frag_size[frag2]) {
1124 state.match_equals(previous);
1129 // Check if the blocks are already matched together:
1130 if (state.equals_to1_(block1, frag1).valid && state.equals_to2_(block2, frag2).valid) {
1131 if (offset1==offset2 && state.fragmentsEqual(block1, frag1, block2, frag2)) {
1133 state.match_equals(previous);
1137 // Compare the size of both fragments:
1138 if (heapinfo1->busy_frag.frag_size[frag1] != heapinfo2->busy_frag.frag_size[frag2]) {
1139 if (type_size == -1) {
1141 state.match_equals(previous);
1147 // Size of the fragment:
1148 size = heapinfo1->busy_frag.frag_size[frag1];
1150 // Remember (basic) type inference.
1151 // The current data structure only allows us to do this for the whole fragment.
1152 if (type != nullptr && area1 == real_addr_frag1)
1153 state.types1_(block1, frag1) = type;
1154 if (type != nullptr && area2 == real_addr_frag2)
1155 state.types2_(block2, frag2) = type;
1157 // The type of the variable is already known:
1159 new_type1 = new_type2 = type;
1161 // Type inference from the block type.
1162 else if (state.types1_(block1, frag1) != nullptr || state.types2_(block2, frag2) != nullptr) {
1164 offset1 = (char*)area1 - (char*)real_addr_frag1;
1165 offset2 = (char*)area2 - (char*)real_addr_frag2;
1167 if (state.types1_(block1, frag1) != nullptr && state.types2_(block2, frag2) != nullptr) {
1169 get_offset_type(real_addr_frag1, state.types1_(block1, frag1), offset1, size, snapshot1, process_index);
1171 get_offset_type(real_addr_frag2, state.types2_(block2, frag2), offset1, size, snapshot2, process_index);
1172 } else if (state.types1_(block1, frag1) != nullptr) {
1174 get_offset_type(real_addr_frag1, state.types1_(block1, frag1), offset1, size, snapshot1, process_index);
1176 get_offset_type(real_addr_frag2, state.types1_(block1, frag1), offset2, size, snapshot2, process_index);
1177 } else if (state.types2_(block2, frag2) != nullptr) {
1179 get_offset_type(real_addr_frag1, state.types2_(block2, frag2), offset1, size, snapshot1, process_index);
1181 get_offset_type(real_addr_frag2, state.types2_(block2, frag2), offset2, size, snapshot2, process_index);
1184 state.match_equals(previous);
1188 if (new_type1 != nullptr && new_type2 != nullptr && new_type1 != new_type2) {
1191 while (type->byte_size == 0 && type->subtype != nullptr)
1192 type = type->subtype;
1193 new_size1 = type->byte_size;
1196 while (type->byte_size == 0 && type->subtype != nullptr)
1197 type = type->subtype;
1198 new_size2 = type->byte_size;
1202 state.match_equals(previous);
1207 if (new_size1 > 0 && new_size1 == new_size2) {
1212 if (offset1 == 0 && offset2 == 0 &&
1213 not previous->insert(simgrid::mc::makeHeapLocationPair(block1, frag1, block2, frag2)).second) {
1215 state.match_equals(previous);
1221 state.match_equals(previous);
1225 if ((heapinfo1->busy_frag.ignore[frag1] > 0) &&
1226 (heapinfo2->busy_frag.ignore[frag2] == heapinfo1->busy_frag.ignore[frag1]))
1227 check_ignore = heapinfo1->busy_frag.ignore[frag1];
1233 /* Start comparison */
1236 res_compare = compare_heap_area_with_type(state, process_index, area1, area2, snapshot1, snapshot2, previous, type,
1237 size, check_ignore, pointer_level);
1239 res_compare = compare_heap_area_without_type(state, process_index, area1, area2, snapshot1, snapshot2, previous,
1240 size, check_ignore);
1242 if (res_compare == 1)
1246 state.match_equals(previous);
1253 /************************** Snapshot comparison *******************************/
1254 /******************************************************************************/
1256 static int compare_areas_with_type(simgrid::mc::StateComparator& state,
1258 void* real_area1, simgrid::mc::Snapshot* snapshot1, mc_mem_region_t region1,
1259 void* real_area2, simgrid::mc::Snapshot* snapshot2, mc_mem_region_t region2,
1260 simgrid::mc::Type* type, int pointer_level)
1262 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
1264 simgrid::mc::Type* subtype;
1265 simgrid::mc::Type* subsubtype;
1271 switch (type->type) {
1272 case DW_TAG_unspecified_type:
1275 case DW_TAG_base_type:
1276 case DW_TAG_enumeration_type:
1277 case DW_TAG_union_type:
1278 return MC_snapshot_region_memcmp(real_area1, region1, real_area2, region2, type->byte_size) != 0;
1279 case DW_TAG_typedef:
1280 case DW_TAG_volatile_type:
1281 case DW_TAG_const_type:
1283 type = type->subtype;
1285 case DW_TAG_array_type:
1286 subtype = type->subtype;
1287 switch (subtype->type) {
1288 case DW_TAG_unspecified_type:
1291 case DW_TAG_base_type:
1292 case DW_TAG_enumeration_type:
1293 case DW_TAG_pointer_type:
1294 case DW_TAG_reference_type:
1295 case DW_TAG_rvalue_reference_type:
1296 case DW_TAG_structure_type:
1297 case DW_TAG_class_type:
1298 case DW_TAG_union_type:
1299 if (subtype->full_type)
1300 subtype = subtype->full_type;
1301 elm_size = subtype->byte_size;
1303 case DW_TAG_const_type:
1304 case DW_TAG_typedef:
1305 case DW_TAG_volatile_type:
1306 subsubtype = subtype->subtype;
1307 if (subsubtype->full_type)
1308 subsubtype = subsubtype->full_type;
1309 elm_size = subsubtype->byte_size;
1315 for (i = 0; i < type->element_count; i++) {
1316 size_t off = i * elm_size;
1317 res = compare_areas_with_type(state, process_index,
1318 (char*) real_area1 + off, snapshot1, region1,
1319 (char*) real_area2 + off, snapshot2, region2,
1320 type->subtype, pointer_level);
1325 case DW_TAG_pointer_type:
1326 case DW_TAG_reference_type:
1327 case DW_TAG_rvalue_reference_type:
1329 void* addr_pointed1 = MC_region_read_pointer(region1, real_area1);
1330 void* addr_pointed2 = MC_region_read_pointer(region2, real_area2);
1332 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type)
1333 return (addr_pointed1 != addr_pointed2);
1334 if (addr_pointed1 == nullptr && addr_pointed2 == nullptr)
1336 if (addr_pointed1 == nullptr || addr_pointed2 == nullptr)
1338 if (not state.compared_pointers.insert(std::make_pair(addr_pointed1, addr_pointed2)).second)
1343 // Some cases are not handled here:
1344 // * the pointers lead to different areas (one to the heap, the other to the RW segment ...);
1345 // * a pointer leads to the read-only segment of the current object;
1346 // * a pointer lead to a different ELF object.
1348 if (addr_pointed1 > process->heap_address
1349 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)) {
1350 if (not(addr_pointed2 > process->heap_address && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)))
1352 // The pointers are both in the heap:
1353 return simgrid::mc::compare_heap_area(state,
1354 process_index, addr_pointed1, addr_pointed2, snapshot1,
1355 snapshot2, nullptr, type->subtype, pointer_level);
1358 // The pointers are both in the current object R/W segment:
1359 else if (region1->contain(simgrid::mc::remote(addr_pointed1))) {
1360 if (not region2->contain(simgrid::mc::remote(addr_pointed2)))
1362 if (not type->type_id)
1363 return (addr_pointed1 != addr_pointed2);
1365 return compare_areas_with_type(state, process_index,
1366 addr_pointed1, snapshot1, region1,
1367 addr_pointed2, snapshot2, region2,
1368 type->subtype, pointer_level);
1371 // TODO, We do not handle very well the case where
1372 // it belongs to a different (non-heap) region from the current one.
1375 return (addr_pointed1 != addr_pointed2);
1379 case DW_TAG_structure_type:
1380 case DW_TAG_class_type:
1381 for (simgrid::mc::Member& member : type->members) {
1382 void *member1 = simgrid::dwarf::resolve_member(
1383 real_area1, type, &member, snapshot1, process_index);
1384 void *member2 = simgrid::dwarf::resolve_member(
1385 real_area2, type, &member, snapshot2, process_index);
1386 mc_mem_region_t subregion1 = mc_get_region_hinted(member1, snapshot1, process_index, region1);
1387 mc_mem_region_t subregion2 = mc_get_region_hinted(member2, snapshot2, process_index, region2);
1389 compare_areas_with_type(state, process_index,
1390 member1, snapshot1, subregion1,
1391 member2, snapshot2, subregion2,
1392 member.type, pointer_level);
1397 case DW_TAG_subroutine_type:
1401 XBT_VERB("Unknown case: %d", type->type);
1408 static int compare_global_variables(
1409 simgrid::mc::StateComparator& state,
1410 simgrid::mc::ObjectInformation* object_info,
1412 mc_mem_region_t r1, mc_mem_region_t r2,
1413 simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2)
1415 xbt_assert(r1 && r2, "Missing region.");
1418 if (r1->storage_type() == simgrid::mc::StorageType::Privatized) {
1419 xbt_assert(process_index >= 0);
1420 if (r2->storage_type() != simgrid::mc::StorageType::Privatized)
1423 size_t process_count = MC_smpi_process_count();
1424 xbt_assert(process_count == r1->privatized_data().size()
1425 && process_count == r2->privatized_data().size());
1427 // Compare the global variables separately for each simulates process:
1428 for (size_t process_index = 0; process_index < process_count; process_index++) {
1429 if (compare_global_variables(state,
1430 object_info, process_index,
1431 &r1->privatized_data()[process_index],
1432 &r2->privatized_data()[process_index],
1433 snapshot1, snapshot2))
1439 xbt_assert(r1->storage_type() != simgrid::mc::StorageType::Privatized);
1441 xbt_assert(r2->storage_type() != simgrid::mc::StorageType::Privatized);
1443 std::vector<simgrid::mc::Variable>& variables = object_info->global_variables;
1445 for (simgrid::mc::Variable const& current_var : variables) {
1447 // If the variable is not in this object, skip it:
1448 // We do not expect to find a pointer to something which is not reachable
1449 // by the global variables.
1450 if ((char *) current_var.address < (char *) object_info->start_rw
1451 || (char *) current_var.address > (char *) object_info->end_rw)
1454 simgrid::mc::Type* bvariable_type = current_var.type;
1455 int res = compare_areas_with_type(state, process_index,
1456 (char *) current_var.address, snapshot1, r1,
1457 (char *) current_var.address, snapshot2, r2,
1460 XBT_VERB("Global variable %s (%p) is different between snapshots",
1461 current_var.name.c_str(),
1462 (char *) current_var.address);
1470 static int compare_local_variables(simgrid::mc::StateComparator& state,
1472 simgrid::mc::Snapshot* snapshot1,
1473 simgrid::mc::Snapshot* snapshot2,
1474 mc_snapshot_stack_t stack1,
1475 mc_snapshot_stack_t stack2)
1477 if (stack1->local_variables.size() != stack2->local_variables.size()) {
1478 XBT_VERB("Different number of local variables");
1482 unsigned int cursor = 0;
1483 local_variable_t current_var1, current_var2;
1484 while (cursor < stack1->local_variables.size()) {
1485 current_var1 = &stack1->local_variables[cursor];
1486 current_var2 = &stack1->local_variables[cursor];
1487 if (current_var1->name != current_var2->name
1488 || current_var1->subprogram != current_var2->subprogram
1489 || current_var1->ip != current_var2->ip) {
1490 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1492 ("Different name of variable (%s - %s) "
1493 "or frame (%s - %s) or ip (%lu - %lu)",
1494 current_var1->name.c_str(),
1495 current_var2->name.c_str(),
1496 current_var1->subprogram->name.c_str(),
1497 current_var2->subprogram->name.c_str(),
1498 current_var1->ip, current_var2->ip);
1501 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1503 simgrid::mc::Type* subtype = current_var1->type;
1504 int res = compare_areas_with_type(
1505 state, process_index, current_var1->address, snapshot1,
1506 mc_get_snapshot_region(current_var1->address, snapshot1, process_index), current_var2->address, snapshot2,
1507 mc_get_snapshot_region(current_var2->address, snapshot2, process_index), subtype, 0);
1510 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1511 XBT_VERB("Local variable %s (%p - %p) in frame %s "
1512 "is different between snapshots",
1513 current_var1->name.c_str(), current_var1->address, current_var2->address,
1514 current_var1->subprogram->name.c_str());
1525 static std::unique_ptr<simgrid::mc::StateComparator> state_comparator;
1527 int snapshot_compare(int num1, simgrid::mc::Snapshot* s1, int num2, simgrid::mc::Snapshot* s2)
1529 // TODO, make this a field of ModelChecker or something similar
1531 if (state_comparator == nullptr)
1532 state_comparator = std::unique_ptr<StateComparator>(new StateComparator());
1534 state_comparator->clear();
1536 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
1540 int hash_result = 0;
1542 hash_result = (s1->hash != s2->hash);
1544 XBT_VERB("(%d - %d) Different hash: 0x%" PRIx64 "--0x%" PRIx64, num1, num2, s1->hash, s2->hash);
1549 XBT_VERB("(%d - %d) Same hash: 0x%" PRIx64, num1, num2, s1->hash);
1552 /* Compare enabled processes */
1553 if (s1->enabled_processes != s2->enabled_processes) {
1554 XBT_VERB("(%d - %d) Different amount of enabled processes", num1, num2);
1558 /* Compare size of stacks */
1560 for (unsigned long i = 0; i < s1->stacks.size(); i++) {
1561 size_t size_used1 = s1->stack_sizes[i];
1562 size_t size_used2 = s2->stack_sizes[i];
1563 if (size_used1 != size_used2) {
1565 XBT_DEBUG("(%d - %d) Different size used in stacks: %zu - %zu", num1, num2, size_used1, size_used2);
1570 XBT_VERB("(%d - %d) Different size used in stacks: %zu - %zu", num1, num2, size_used1, size_used2);
1576 if (is_diff) // do not proceed if there is any stacks that don't match
1579 /* Init heap information used in heap comparison algorithm */
1580 xbt_mheap_t heap1 = (xbt_mheap_t)s1->read_bytes(
1581 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1582 remote(process->heap_address),
1583 simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1584 xbt_mheap_t heap2 = (xbt_mheap_t)s2->read_bytes(
1585 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1586 remote(process->heap_address),
1587 simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1588 int res_init = state_comparator->initHeapInformation(heap1, heap2, &s1->to_ignore, &s2->to_ignore);
1590 if (res_init == -1) {
1592 XBT_DEBUG("(%d - %d) Different heap information", num1, num2);
1596 XBT_VERB("(%d - %d) Different heap information", num1, num2);
1603 /* Stacks comparison */
1605 for (unsigned int cursor = 0; cursor < s1->stacks.size(); cursor++) {
1606 mc_snapshot_stack_t stack1 = &s1->stacks[cursor];
1607 mc_snapshot_stack_t stack2 = &s2->stacks[cursor];
1609 if (stack1->process_index != stack2->process_index) {
1611 XBT_DEBUG("(%d - %d) Stacks with different process index (%i vs %i)", num1, num2,
1612 stack1->process_index, stack2->process_index);
1614 else diff_local = compare_local_variables(*state_comparator,
1615 stack1->process_index, s1, s2, stack1, stack2);
1616 if (diff_local > 0) {
1618 XBT_DEBUG("(%d - %d) Different local variables between stacks %d", num1,
1624 XBT_VERB("(%d - %d) Different local variables between stacks %u", num1, num2, cursor + 1);
1632 size_t regions_count = s1->snapshot_regions.size();
1633 // TODO, raise a difference instead?
1634 xbt_assert(regions_count == s2->snapshot_regions.size());
1636 for (size_t k = 0; k != regions_count; ++k) {
1637 mc_mem_region_t region1 = s1->snapshot_regions[k].get();
1638 mc_mem_region_t region2 = s2->snapshot_regions[k].get();
1641 if (region1->region_type() != simgrid::mc::RegionType::Data)
1644 xbt_assert(region1->region_type() == region2->region_type());
1645 xbt_assert(region1->object_info() == region2->object_info());
1646 xbt_assert(region1->object_info());
1648 std::string const& name = region1->object_info()->file_name;
1650 /* Compare global variables */
1651 if (compare_global_variables(*state_comparator, region1->object_info(), simgrid::mc::ProcessIndexDisabled, region1,
1655 XBT_DEBUG("(%d - %d) Different global variables in %s",
1656 num1, num2, name.c_str());
1660 XBT_VERB("(%d - %d) Different global variables in %s",
1661 num1, num2, name.c_str());
1670 if (simgrid::mc::mmalloc_compare_heap(*state_comparator, s1, s2) > 0) {
1673 XBT_DEBUG("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
1678 XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
1685 if (errors || hash_result)
1686 XBT_VERB("(%d - %d) Difference found", num1, num2);
1688 XBT_VERB("(%d - %d) No difference found", num1, num2);
1691 #if defined(MC_DEBUG) && defined(MC_VERBOSE)
1693 // * false positive SHOULD be avoided.
1694 // * There MUST not be any false negative.
1696 XBT_VERB("(%d - %d) State equality hash test is %s %s", num1, num2,
1697 (hash_result != 0) == (errors != 0) ? "true" : "false", not hash_result ? "positive" : "negative");
1701 return errors > 0 || hash_result;