1 /* Copyright (c) 2008-2017. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 /** \file compare.cpp Memory snapshooting and comparison */
14 #include <unordered_set>
16 #include <xbt/sysdep.h>
17 #include <xbt/dynar.h>
18 #include <xbt/mmalloc.h>
21 #include <mc/datatypes.h>
23 #include "src/internal_config.h"
25 #include "src/xbt/mmalloc/mmprivate.h"
28 #include "src/smpi/private.h"
31 #include "src/mc/mc_forward.hpp"
32 #include "src/mc/mc_safety.h"
33 #include "src/mc/mc_private.h"
34 #include "src/mc/mc_smx.h"
35 #include "src/mc/mc_dwarf.hpp"
36 #include "src/mc/Frame.hpp"
37 #include "src/mc/ObjectInformation.hpp"
38 #include "src/mc/Variable.hpp"
39 #include "src/mc/mc_private.h"
40 #include "src/mc/mc_snapshot.h"
41 #include "src/mc/mc_dwarf.hpp"
42 #include "src/mc/Type.hpp"
44 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_compare, xbt,
45 "Logging specific to mc_compare in mc");
51 typedef std::array<HeapLocation, 2> HeapLocationPair;
52 typedef std::set<HeapLocationPair> HeapLocationPairs;
54 struct ProcessComparisonState;
55 struct StateComparator;
57 static int compare_heap_area(
58 StateComparator& state,
59 int process_index, const void *area1, const void* area2,
60 Snapshot* snapshot1, Snapshot* snapshot2,
61 HeapLocationPairs* previous, Type* type, int pointer_level);
66 using simgrid::mc::remote;
68 /*********************************** Heap comparison ***********************************/
69 /***************************************************************************************/
79 HeapLocation(int block, int fragment = 0) : block(block), fragment(fragment) {}
81 bool operator==(HeapLocation const& that) const
83 return block == that.block && fragment == that.fragment;
85 bool operator<(HeapLocation const& that) const
87 return std::make_pair(block, fragment)
88 < std::make_pair(that.block, that.fragment);
93 HeapLocationPair makeHeapLocationPair(int block1, int fragment1, int block2, int fragment2)
95 return simgrid::mc::HeapLocationPair{{
96 simgrid::mc::HeapLocation(block1, fragment1),
97 simgrid::mc::HeapLocation(block2, fragment2)
101 struct HeapArea : public HeapLocation {
107 : valid(true), block(block) {}
108 HeapArea(int block, int fragment = 0)
109 : valid(true), block(block), fragment(fragment) {}
112 struct ProcessComparisonState {
113 std::vector<simgrid::mc::IgnoredHeapRegion>* to_ignore = nullptr;
114 std::vector<HeapArea> equals_to;
115 std::vector<simgrid::mc::Type*> types;
116 std::size_t heapsize = 0;
118 void initHeapInformation(xbt_mheap_t heap,
119 std::vector<simgrid::mc::IgnoredHeapRegion>* i);
124 /** A hash which works with more stuff
126 * It can hash pairs: the standard hash currently doesn't include this.
128 template<class X> struct hash : public std::hash<X> {};
130 template<class X, class Y>
131 struct hash<std::pair<X,Y>> {
132 std::size_t operator()(std::pair<X,Y>const& x) const
136 return h1(x.first) ^ h2(x.second);
143 struct StateComparator {
144 s_xbt_mheap_t std_heap_copy;
145 std::size_t heaplimit;
146 std::array<ProcessComparisonState, 2> processStates;
148 std::unordered_set<std::pair<void*, void*>, hash<std::pair<void*, void*>>> compared_pointers;
152 compared_pointers.clear();
155 int initHeapInformation(
156 xbt_mheap_t heap1, xbt_mheap_t heap2,
157 std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
158 std::vector<simgrid::mc::IgnoredHeapRegion>* i2);
160 HeapArea& equals_to1_(std::size_t i, std::size_t j)
162 return processStates[0].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
164 HeapArea& equals_to2_(std::size_t i, std::size_t j)
166 return processStates[1].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
168 Type*& types1_(std::size_t i, std::size_t j)
170 return processStates[0].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
172 Type*& types2_(std::size_t i, std::size_t j)
174 return processStates[1].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
177 HeapArea const& equals_to1_(std::size_t i, std::size_t j) const
179 return processStates[0].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
181 HeapArea const& equals_to2_(std::size_t i, std::size_t j) const
183 return processStates[1].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
185 Type* const& types1_(std::size_t i, std::size_t j) const
187 return processStates[0].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
189 Type* const& types2_(std::size_t i, std::size_t j) const
191 return processStates[1].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
194 /** Check whether two blocks are known to be matching
196 * @param b1 Block of state 1
197 * @param b2 Block of state 2
198 * @return if the blocks are known to be matching
200 bool blocksEqual(int b1, int b2) const
202 return this->equals_to1_(b1, 0).block == b2
203 && this->equals_to2_(b2, 0).block == b1;
206 /** Check whether two fragments are known to be matching
208 * @param b1 Block of state 1
209 * @param f1 Fragment of state 1
210 * @param b2 Block of state 2
211 * @param f2 Fragment of state 2
212 * @return if the fragments are known to be matching
214 int fragmentsEqual(int b1, int f1, int b2, int f2) const
216 return this->equals_to1_(b1, f1).block == b2
217 && this->equals_to1_(b1, f1).fragment == f2
218 && this->equals_to2_(b2, f2).block == b1
219 && this->equals_to2_(b2, f2).fragment == f1;
222 void match_equals(HeapLocationPairs* list);
228 /************************************************************************************/
230 static ssize_t heap_comparison_ignore_size(
231 std::vector<simgrid::mc::IgnoredHeapRegion>* ignore_list,
235 int end = ignore_list->size() - 1;
237 while (start <= end) {
238 unsigned int cursor = (start + end) / 2;
239 simgrid::mc::IgnoredHeapRegion const& region = (*ignore_list)[cursor];
240 if (region.address == address)
242 if (region.address < address)
244 if (region.address > address)
251 static bool is_stack(const void *address)
253 for (auto const& stack : mc_model_checker->process().stack_areas())
254 if (address == stack.address)
259 // TODO, this should depend on the snapshot?
260 static bool is_block_stack(int block)
262 for (auto const& stack : mc_model_checker->process().stack_areas())
263 if (block == stack.block)
271 void StateComparator::match_equals(HeapLocationPairs* list)
273 for (auto const& pair : *list) {
274 if (pair[0].fragment != -1) {
275 this->equals_to1_(pair[0].block, pair[0].fragment) =
276 simgrid::mc::HeapArea(pair[1].block, pair[1].fragment);
277 this->equals_to2_(pair[1].block, pair[1].fragment) =
278 simgrid::mc::HeapArea(pair[0].block, pair[0].fragment);
280 this->equals_to1_(pair[0].block, 0) =
281 simgrid::mc::HeapArea(pair[1].block, pair[1].fragment);
282 this->equals_to2_(pair[1].block, 0) =
283 simgrid::mc::HeapArea(pair[0].block, pair[0].fragment);
288 void ProcessComparisonState::initHeapInformation(xbt_mheap_t heap,
289 std::vector<simgrid::mc::IgnoredHeapRegion>* i)
291 auto heaplimit = heap->heaplimit;
292 this->heapsize = heap->heapsize;
294 this->equals_to.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, HeapArea());
295 this->types.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, nullptr);
298 int StateComparator::initHeapInformation(xbt_mheap_t heap1, xbt_mheap_t heap2,
299 std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
300 std::vector<simgrid::mc::IgnoredHeapRegion>* i2)
302 if ((heap1->heaplimit != heap2->heaplimit) || (heap1->heapsize != heap2->heapsize))
304 this->heaplimit = heap1->heaplimit;
305 this->std_heap_copy = *mc_model_checker->process().get_heap();
306 this->processStates[0].initHeapInformation(heap1, i1);
307 this->processStates[1].initHeapInformation(heap2, i2);
311 // TODO, have a robust way to find it in O(1)
313 mc_mem_region_t MC_get_heap_region(simgrid::mc::Snapshot* snapshot)
315 for (auto& region : snapshot->snapshot_regions)
316 if (region->region_type() == simgrid::mc::RegionType::Heap)
318 xbt_die("No heap region");
322 int mmalloc_compare_heap(
323 simgrid::mc::StateComparator& state, simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2)
325 simgrid::mc::Process* process = &mc_model_checker->process();
327 /* Start comparison */
328 size_t i1, i2, j1, j2, k;
329 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
330 int nb_diff1 = 0, nb_diff2 = 0;
332 int equal, res_compare = 0;
334 /* Check busy blocks */
337 malloc_info heapinfo_temp1, 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");
368 ((void *) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE +
369 (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) {
391 /* Try first to associate to same block in the other heap */
392 if (heapinfo2->type == heapinfo1->type
393 && state.equals_to2_(i1, 0).valid == 0) {
394 addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE +
395 (char *) state.std_heap_copy.heapbase;
396 res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing,
397 addr_block1, addr_block2, snapshot1, snapshot2,
398 nullptr, nullptr, 0);
399 if (res_compare != 1) {
400 for (k = 1; k < heapinfo2->busy_block.size; k++)
401 state.equals_to2_(i1 + k, 0) = HeapArea(i1, -1);
402 for (k = 1; k < heapinfo1->busy_block.size; k++)
403 state.equals_to1_(i1 + k, 0) = HeapArea(i1, -1);
405 i1 += heapinfo1->busy_block.size;
409 while (i2 < state.heaplimit && !equal) {
411 addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE +
412 (char *) state.std_heap_copy.heapbase;
419 const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2b, &heapinfos2[i2], sizeof(malloc_info));
421 if (heapinfo2b->type != MMALLOC_TYPE_UNFRAGMENTED) {
426 if (state.equals_to2_(i2, 0).valid) {
431 res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing,
432 addr_block1, addr_block2, snapshot1, snapshot2,
433 nullptr, nullptr, 0);
435 if (res_compare != 1) {
436 for (k = 1; k < heapinfo2b->busy_block.size; k++)
437 state.equals_to2_(i2 + k, 0) = HeapArea(i1, -1);
438 for (k = 1; k < heapinfo1->busy_block.size; k++)
439 state.equals_to1_(i1 + k, 0) = HeapArea(i2, -1);
441 i1 += heapinfo1->busy_block.size;
448 XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1,
449 heapinfo1->busy_block.busy_size, addr_block1);
450 i1 = state.heaplimit + 1;
455 } else { /* Fragmented block */
457 for (j1 = 0; j1 < (size_t) (BLOCKSIZE >> heapinfo1->type); j1++) {
459 if (heapinfo1->busy_frag.frag_size[j1] == -1) /* Free fragment */
462 if (state.equals_to1_(i1, j1).valid)
466 (void *) ((char *) addr_block1 + (j1 << heapinfo1->type));
471 /* Try first to associate to same fragment in the other heap */
472 if (heapinfo2->type == heapinfo1->type
473 && !state.equals_to2_(i1, j1).valid) {
474 addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE +
475 (char *) state.std_heap_copy.heapbase;
477 (void *) ((char *) addr_block2 +
478 (j1 << heapinfo2->type));
479 res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing,
480 addr_frag1, addr_frag2, snapshot1, snapshot2,
481 nullptr, nullptr, 0);
482 if (res_compare != 1)
488 while (i2 < state.heaplimit && !equal) {
490 const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(
491 heap_region2, &heapinfo_temp2b, &heapinfos2[i2],
492 sizeof(malloc_info));
494 if (heapinfo2b->type == MMALLOC_TYPE_FREE || heapinfo2b->type == MMALLOC_TYPE_HEAPINFO) {
499 // We currently do not match fragments with unfragmented blocks (maybe we should).
500 if (heapinfo2b->type == MMALLOC_TYPE_UNFRAGMENTED) {
505 if (heapinfo2b->type < 0) {
506 fprintf(stderr, "Unknown mmalloc block type.\n");
510 for (j2 = 0; j2 < (size_t) (BLOCKSIZE >> heapinfo2b->type);
513 if (i2 == i1 && j2 == j1)
516 if (state.equals_to2_(i2, j2).valid)
519 addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE +
520 (char *) state.std_heap_copy.heapbase;
522 (void *) ((char *) addr_block2 +
523 (j2 << heapinfo2b->type));
525 res_compare = compare_heap_area(
526 state, simgrid::mc::ProcessIndexMissing,
527 addr_frag1, addr_frag2, snapshot2, snapshot2,
528 nullptr, nullptr, 0);
529 if (res_compare != 1) {
540 ("Block %zu, fragment %zu not found (size_used = %zd, address = %p)\n",
541 i1, j1, heapinfo1->busy_frag.frag_size[j1],
543 i2 = state.heaplimit + 1;
544 i1 = state.heaplimit + 1;
554 /* All blocks/fragments are equal to another block/fragment ? */
557 for(i = 1; i < state.heaplimit; i++) {
558 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
559 heap_region1, &heapinfo_temp1, &heapinfos1[i], sizeof(malloc_info));
561 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED
562 && i1 == state.heaplimit
563 && heapinfo1->busy_block.busy_size > 0
564 && !state.equals_to1_(i, 0).valid) {
565 XBT_DEBUG("Block %zu not found (size used = %zu)", i,
566 heapinfo1->busy_block.busy_size);
570 if (heapinfo1->type <= 0)
572 for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo1->type); j++)
573 if (i1 == state.heaplimit
574 && heapinfo1->busy_frag.frag_size[j] > 0
575 && !state.equals_to1_(i, j).valid) {
576 XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)",
577 i, j, heapinfo1->busy_frag.frag_size[j]);
582 if (i1 == state.heaplimit)
583 XBT_DEBUG("Number of blocks/fragments not found in heap1: %d", nb_diff1);
585 for (i=1; i < state.heaplimit; i++) {
586 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
587 heap_region2, &heapinfo_temp2, &heapinfos2[i], sizeof(malloc_info));
588 if (heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED
589 && i1 == state.heaplimit
590 && heapinfo2->busy_block.busy_size > 0
591 && !state.equals_to2_(i, 0).valid) {
592 XBT_DEBUG("Block %zu not found (size used = %zu)", i,
593 heapinfo2->busy_block.busy_size);
597 if (heapinfo2->type <= 0)
600 for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo2->type); j++)
601 if (i1 == state.heaplimit
602 && heapinfo2->busy_frag.frag_size[j] > 0
603 && !state.equals_to2_(i, j).valid) {
604 XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)",
605 i, j, heapinfo2->busy_frag.frag_size[j]);
611 if (i1 == state.heaplimit)
612 XBT_DEBUG("Number of blocks/fragments not found in heap2: %d", nb_diff2);
614 return nb_diff1 > 0 || nb_diff2 > 0;
620 * @param real_area1 Process address for state 1
621 * @param real_area2 Process address for state 2
622 * @param snapshot1 Snapshot of state 1
623 * @param snapshot2 Snapshot of state 2
626 * @param check_ignore
628 static int compare_heap_area_without_type(
629 simgrid::mc::StateComparator& state, int process_index,
630 const void *real_area1, const void *real_area2,
631 simgrid::mc::Snapshot* snapshot1,
632 simgrid::mc::Snapshot* snapshot2,
633 HeapLocationPairs* previous, int size,
636 simgrid::mc::Process* process = &mc_model_checker->process();
637 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
638 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
640 for (int i = 0; i < size; ) {
642 if (check_ignore > 0) {
643 ssize_t ignore1 = heap_comparison_ignore_size(
644 state.processStates[0].to_ignore, (char *) real_area1 + i);
646 ssize_t ignore2 = heap_comparison_ignore_size(
647 state.processStates[1].to_ignore, (char *) real_area2 + i);
648 if (ignore2 == ignore1) {
661 if (MC_snapshot_region_memcmp(((char *) real_area1) + i, heap_region1, ((char *) real_area2) + i, heap_region2, 1) != 0) {
663 int pointer_align = (i / sizeof(void *)) * sizeof(void *);
664 const void* addr_pointed1 = snapshot1->read(
665 remote((void**)((char *) real_area1 + pointer_align)), process_index);
666 const void* addr_pointed2 = snapshot2->read(
667 remote((void**)((char *) real_area2 + pointer_align)), process_index);
669 if (process->in_maestro_stack(remote(addr_pointed1))
670 && process->in_maestro_stack(remote(addr_pointed2))) {
671 i = pointer_align + sizeof(void *);
675 if (addr_pointed1 > state.std_heap_copy.heapbase
676 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
677 && addr_pointed2 > state.std_heap_copy.heapbase
678 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)) {
679 // Both addreses are in the heap:
680 int res_compare = compare_heap_area(state ,process_index,
681 addr_pointed1, addr_pointed2,
682 snapshot1, snapshot2, previous, nullptr, 0);
683 if (res_compare == 1)
685 i = pointer_align + sizeof(void *);
701 * @param real_area1 Process address for state 1
702 * @param real_area2 Process address for state 2
703 * @param snapshot1 Snapshot of state 1
704 * @param snapshot2 Snapshot of state 2
707 * @param area_size either a byte_size or an elements_count (?)
708 * @param check_ignore
709 * @param pointer_level
710 * @return 0 (same), 1 (different), -1 (unknown)
712 static int compare_heap_area_with_type(
713 simgrid::mc::StateComparator& state, int process_index,
714 const void *real_area1, const void *real_area2,
715 simgrid::mc::Snapshot* snapshot1,
716 simgrid::mc::Snapshot* snapshot2,
717 HeapLocationPairs* previous, simgrid::mc::Type* type,
718 int area_size, int check_ignore,
723 // HACK: This should not happen but in pratice, there are some
724 // DW_TAG_typedef without an associated DW_AT_type:
725 //<1><538832>: Abbrev Number: 111 (DW_TAG_typedef)
726 // <538833> DW_AT_name : (indirect string, offset: 0x2292f3): gregset_t
727 // <538837> DW_AT_decl_file : 98
728 // <538838> DW_AT_decl_line : 37
732 if (is_stack(real_area1) && is_stack(real_area2))
735 if (check_ignore > 0) {
736 ssize_t ignore1 = heap_comparison_ignore_size(
737 state.processStates[0].to_ignore, real_area1);
739 && heap_comparison_ignore_size(
740 state.processStates[1].to_ignore, real_area2) == ignore1)
744 simgrid::mc::Type *subtype, *subsubtype;
746 const void *addr_pointed1, *addr_pointed2;
748 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
749 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
751 switch (type->type) {
752 case DW_TAG_unspecified_type:
755 case DW_TAG_base_type:
756 if (!type->name.empty() && type->name == "char") { /* String, hence random (arbitrary ?) size */
757 if (real_area1 == real_area2)
760 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, area_size) != 0;
762 if (area_size != -1 && type->byte_size != area_size)
765 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
769 case DW_TAG_enumeration_type:
770 if (area_size != -1 && type->byte_size != area_size)
772 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
775 case DW_TAG_const_type:
776 case DW_TAG_volatile_type:
778 type = type->subtype;
781 case DW_TAG_array_type:
782 subtype = type->subtype;
783 switch (subtype->type) {
784 case DW_TAG_unspecified_type:
787 case DW_TAG_base_type:
788 case DW_TAG_enumeration_type:
789 case DW_TAG_pointer_type:
790 case DW_TAG_reference_type:
791 case DW_TAG_rvalue_reference_type:
792 case DW_TAG_structure_type:
793 case DW_TAG_class_type:
794 case DW_TAG_union_type:
795 if (subtype->full_type)
796 subtype = subtype->full_type;
797 elm_size = subtype->byte_size;
799 // TODO, just remove the type indirection?
800 case DW_TAG_const_type:
802 case DW_TAG_volatile_type:
803 subsubtype = subtype->subtype;
804 if (subsubtype->full_type)
805 subsubtype = subsubtype->full_type;
806 elm_size = subsubtype->byte_size;
812 for (int i = 0; i < type->element_count; i++) {
813 // TODO, add support for variable stride (DW_AT_byte_stride)
815 compare_heap_area_with_type(state, process_index,
816 (char *) real_area1 + (i * elm_size),
817 (char *) real_area2 + (i * elm_size),
818 snapshot1, snapshot2, previous,
819 type->subtype, subtype->byte_size,
820 check_ignore, pointer_level);
826 case DW_TAG_reference_type:
827 case DW_TAG_rvalue_reference_type:
828 case DW_TAG_pointer_type:
829 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
830 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
831 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
832 return (addr_pointed1 != addr_pointed2);
835 if (pointer_level <= 1) {
836 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
837 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
838 if (addr_pointed1 > state.std_heap_copy.heapbase
839 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
840 && addr_pointed2 > state.std_heap_copy.heapbase
841 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
842 return compare_heap_area(state, process_index,
843 addr_pointed1, addr_pointed2, snapshot1,
844 snapshot2, previous, type->subtype,
847 return (addr_pointed1 != addr_pointed2);
849 for (size_t i = 0; i < (area_size / sizeof(void *)); i++) {
850 addr_pointed1 = snapshot1->read(
851 remote((void**)((char*) real_area1 + i * sizeof(void *))),
853 addr_pointed2 = snapshot2->read(
854 remote((void**)((char*) real_area2 + i * sizeof(void *))),
856 if (addr_pointed1 > state.std_heap_copy.heapbase
857 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
858 && addr_pointed2 > state.std_heap_copy.heapbase
859 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
861 compare_heap_area(state, process_index,
862 addr_pointed1, addr_pointed2, snapshot1,
863 snapshot2, previous, type->subtype,
866 res = (addr_pointed1 != addr_pointed2);
872 case DW_TAG_structure_type:
873 case DW_TAG_class_type:
875 type = type->full_type;
876 if (area_size != -1 && type->byte_size != area_size) {
877 if (area_size <= type->byte_size || area_size % type->byte_size != 0)
879 for (size_t i = 0; i < (size_t)(area_size / type->byte_size); i++) {
880 int res = compare_heap_area_with_type(state, process_index,
881 (char *) real_area1 + i * type->byte_size,
882 (char *) real_area2 + i * type->byte_size,
883 snapshot1, snapshot2, previous, type, -1,
889 for(simgrid::mc::Member& member : type->members) {
890 // TODO, optimize this? (for the offset case)
891 void *real_member1 = simgrid::dwarf::resolve_member(
892 real_area1, type, &member, (simgrid::mc::AddressSpace*) snapshot1, process_index);
893 void *real_member2 = simgrid::dwarf::resolve_member(
894 real_area2, type, &member, (simgrid::mc::AddressSpace*) snapshot2, process_index);
895 int res = compare_heap_area_with_type(
896 state, process_index, real_member1, real_member2,
897 snapshot1, snapshot2,
898 previous, member.type, -1,
906 case DW_TAG_union_type:
907 return compare_heap_area_without_type(state, process_index, real_area1, real_area2,
908 snapshot1, snapshot2, previous,
909 type->byte_size, check_ignore);
915 xbt_die("Unreachable");
918 /** Infer the type of a part of the block from the type of the block
920 * TODO, handle DW_TAG_array_type as well as arrays of the object ((*p)[5], p[5])
922 * TODO, handle subfields ((*p).bar.foo, (*p)[5].bar…)
924 * @param type DWARF type ID of the root address
926 * @return DWARF type ID for given offset
928 static simgrid::mc::Type* get_offset_type(void *real_base_address, simgrid::mc::Type* type,
929 int offset, int area_size,
930 simgrid::mc::Snapshot* snapshot, int process_index)
933 // Beginning of the block, the infered variable type if the type of the block:
937 switch (type->type) {
939 case DW_TAG_structure_type:
940 case DW_TAG_class_type:
942 type = type->full_type;
943 if (area_size != -1 && type->byte_size != area_size) {
944 if (area_size > type->byte_size && area_size % type->byte_size == 0)
950 for(simgrid::mc::Member& member : type->members) {
951 if (member.has_offset_location()) {
952 // We have the offset, use it directly (shortcut):
953 if (member.offset() == offset)
956 void *real_member = simgrid::dwarf::resolve_member(
957 real_base_address, type, &member, snapshot, process_index);
958 if ((char*) real_member - (char *) real_base_address == offset)
965 /* FIXME: other cases ? */
973 * @param area1 Process address for state 1
974 * @param area2 Process address for state 2
975 * @param snapshot1 Snapshot of state 1
976 * @param snapshot2 Snapshot of state 2
977 * @param previous Pairs of blocks already compared on the current path (or nullptr)
978 * @param type_id Type of variable
979 * @param pointer_level
980 * @return 0 (same), 1 (different), -1
983 int compare_heap_area(simgrid::mc::StateComparator& state, int process_index,
984 const void *area1, const void *area2,
985 simgrid::mc::Snapshot* snapshot1,
986 simgrid::mc::Snapshot* snapshot2,
987 HeapLocationPairs* previous,
988 simgrid::mc::Type* type, int pointer_level)
990 simgrid::mc::Process* process = &mc_model_checker->process();
993 ssize_t block1, frag1, block2, frag2;
995 int check_ignore = 0;
997 void *real_addr_block1, *real_addr_block2, *real_addr_frag1, *real_addr_frag2;
999 int offset1 = 0, offset2 = 0;
1000 int new_size1 = -1, new_size2 = -1;
1001 simgrid::mc::Type *new_type1 = nullptr, *new_type2 = nullptr;
1003 bool match_pairs = false;
1005 // This is the address of std_heap->heapinfo in the application process:
1006 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
1008 const malloc_info* heapinfos1 = snapshot1->read(
1009 remote((const malloc_info**)heapinfo_address), process_index);
1010 const malloc_info* heapinfos2 = snapshot2->read(
1011 remote((const malloc_info**)heapinfo_address), process_index);
1013 malloc_info heapinfo_temp1, heapinfo_temp2;
1015 simgrid::mc::HeapLocationPairs current;
1016 if (previous == nullptr) {
1017 previous = ¤t;
1021 // Get block number:
1024 (char *) state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
1027 (char *) state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
1029 // If either block is a stack block:
1030 if (is_block_stack((int) block1) && is_block_stack((int) block2)) {
1031 previous->insert(simgrid::mc::makeHeapLocationPair(
1032 block1, -1, block2, -1));
1034 state.match_equals(previous);
1038 // If either block is not in the expected area of memory:
1039 if (((char *) area1 < (char *) state.std_heap_copy.heapbase)
1040 || (block1 > (ssize_t) state.processStates[0].heapsize) || (block1 < 1)
1041 || ((char *) area2 < (char *) state.std_heap_copy.heapbase)
1042 || (block2 > (ssize_t) state.processStates[1].heapsize) || (block2 < 1)) {
1046 // Process address of the block:
1047 real_addr_block1 = (ADDR2UINT(block1) - 1) * BLOCKSIZE +
1048 (char *) state.std_heap_copy.heapbase;
1049 real_addr_block2 = (ADDR2UINT(block2) - 1) * BLOCKSIZE +
1050 (char *) state.std_heap_copy.heapbase;
1054 if (type->full_type)
1055 type = type->full_type;
1057 // This assume that for "boring" types (volatile ...) byte_size is absent:
1058 while (type->byte_size == 0 && type->subtype != nullptr)
1059 type = type->subtype;
1062 if (type->type == DW_TAG_pointer_type
1063 || (type->type == DW_TAG_base_type && !type->name.empty()
1064 && type->name == "char"))
1067 type_size = type->byte_size;
1071 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
1072 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
1074 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
1075 heap_region1, &heapinfo_temp1, &heapinfos1[block1], sizeof(malloc_info));
1076 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
1077 heap_region2, &heapinfo_temp2, &heapinfos2[block2], sizeof(malloc_info));
1079 if ((heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type==MMALLOC_TYPE_HEAPINFO)
1080 && (heapinfo2->type == MMALLOC_TYPE_FREE || heapinfo2->type ==MMALLOC_TYPE_HEAPINFO)) {
1083 state.match_equals(previous);
1087 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED
1088 && heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
1089 /* Complete block */
1091 // TODO, lookup variable type from block type as done for fragmented blocks
1093 offset1 = (char *) area1 - (char *) real_addr_block1;
1094 offset2 = (char *) area2 - (char *) real_addr_block2;
1096 if (state.equals_to1_(block1, 0).valid
1097 && state.equals_to2_(block2, 0).valid
1098 && state.blocksEqual(block1, block2)) {
1100 state.match_equals(previous);
1104 if (type_size != -1) {
1105 if (type_size != (ssize_t) heapinfo1->busy_block.busy_size
1106 && type_size != (ssize_t) heapinfo2->busy_block.busy_size
1107 && (type->name.empty() || type->name == "struct s_smx_context")) {
1109 state.match_equals(previous);
1114 if (heapinfo1->busy_block.size != heapinfo2->busy_block.size)
1116 if (heapinfo1->busy_block.busy_size != heapinfo2->busy_block.busy_size)
1119 if (!previous->insert(simgrid::mc::makeHeapLocationPair(
1120 block1, -1, block2, -1)).second) {
1122 state.match_equals(previous);
1126 size = heapinfo1->busy_block.busy_size;
1128 // Remember (basic) type inference.
1129 // The current data structure only allows us to do this for the whole block.
1130 if (type != nullptr && area1 == real_addr_block1)
1131 state.types1_(block1, 0) = type;
1132 if (type != nullptr && area2 == real_addr_block2)
1133 state.types2_(block2, 0) = type;
1137 state.match_equals(previous);
1144 if (heapinfo1->busy_block.ignore > 0
1145 && heapinfo2->busy_block.ignore == heapinfo1->busy_block.ignore)
1146 check_ignore = heapinfo1->busy_block.ignore;
1148 } else if ((heapinfo1->type > 0) && (heapinfo2->type > 0)) { /* Fragmented block */
1152 ((uintptr_t) (ADDR2UINT(area1) % (BLOCKSIZE))) >> heapinfo1->type;
1154 ((uintptr_t) (ADDR2UINT(area2) % (BLOCKSIZE))) >> heapinfo2->type;
1156 // Process address of the fragment:
1158 (void *) ((char *) real_addr_block1 +
1159 (frag1 << heapinfo1->type));
1161 (void *) ((char *) real_addr_block2 +
1162 (frag2 << heapinfo2->type));
1164 // Check the size of the fragments against the size of the type:
1165 if (type_size != -1) {
1166 if (heapinfo1->busy_frag.frag_size[frag1] == -1
1167 || heapinfo2->busy_frag.frag_size[frag2] == -1) {
1169 state.match_equals(previous);
1173 if (type_size != heapinfo1->busy_frag.frag_size[frag1]
1174 || type_size != heapinfo2->busy_frag.frag_size[frag2]) {
1176 state.match_equals(previous);
1181 // Check if the blocks are already matched together:
1182 if (state.equals_to1_(block1, frag1).valid
1183 && state.equals_to2_(block2, frag2).valid) {
1184 if (offset1==offset2 && state.fragmentsEqual(block1, frag1, block2, frag2)) {
1186 state.match_equals(previous);
1190 // Compare the size of both fragments:
1191 if (heapinfo1->busy_frag.frag_size[frag1] !=
1192 heapinfo2->busy_frag.frag_size[frag2]) {
1193 if (type_size == -1) {
1195 state.match_equals(previous);
1201 // Size of the fragment:
1202 size = heapinfo1->busy_frag.frag_size[frag1];
1204 // Remember (basic) type inference.
1205 // The current data structure only allows us to do this for the whole fragment.
1206 if (type != nullptr && area1 == real_addr_frag1)
1207 state.types1_(block1, frag1) = type;
1208 if (type != nullptr && area2 == real_addr_frag2)
1209 state.types2_(block2, frag2) = type;
1211 // The type of the variable is already known:
1216 // Type inference from the block type.
1217 else if (state.types1_(block1, frag1) != nullptr
1218 || state.types2_(block2, frag2) != nullptr) {
1220 offset1 = (char *) area1 - (char *) real_addr_frag1;
1221 offset2 = (char *) area2 - (char *) real_addr_frag2;
1223 if (state.types1_(block1, frag1) != nullptr
1224 && state.types2_(block2, frag2) != nullptr) {
1226 get_offset_type(real_addr_frag1, state.types1_(block1, frag1),
1227 offset1, size, snapshot1, process_index);
1229 get_offset_type(real_addr_frag2, state.types2_(block2, frag2),
1230 offset1, size, snapshot2, process_index);
1231 } else if (state.types1_(block1, frag1) != nullptr) {
1233 get_offset_type(real_addr_frag1, state.types1_(block1, frag1),
1234 offset1, size, snapshot1, process_index);
1236 get_offset_type(real_addr_frag2, state.types1_(block1, frag1),
1237 offset2, size, snapshot2, process_index);
1238 } else if (state.types2_(block2, frag2) != nullptr) {
1240 get_offset_type(real_addr_frag1, state.types2_(block2, frag2),
1241 offset1, size, snapshot1, process_index);
1243 get_offset_type(real_addr_frag2, state.types2_(block2, frag2),
1244 offset2, size, snapshot2, process_index);
1247 state.match_equals(previous);
1251 if (new_type1 != nullptr && new_type2 != nullptr && new_type1 != new_type2) {
1254 while (type->byte_size == 0 && type->subtype != nullptr)
1255 type = type->subtype;
1256 new_size1 = type->byte_size;
1259 while (type->byte_size == 0 && type->subtype != nullptr)
1260 type = type->subtype;
1261 new_size2 = type->byte_size;
1265 state.match_equals(previous);
1270 if (new_size1 > 0 && new_size1 == new_size2) {
1275 if (offset1 == 0 && offset2 == 0
1276 && !previous->insert(simgrid::mc::makeHeapLocationPair(
1277 block1, frag1, block2, frag2)).second) {
1279 state.match_equals(previous);
1285 state.match_equals(previous);
1289 if ((heapinfo1->busy_frag.ignore[frag1] > 0)
1290 && (heapinfo2->busy_frag.ignore[frag2] ==
1291 heapinfo1->busy_frag.ignore[frag1]))
1292 check_ignore = heapinfo1->busy_frag.ignore[frag1];
1298 /* Start comparison */
1301 compare_heap_area_with_type(state, process_index, area1, area2, snapshot1, snapshot2,
1302 previous, type, size, check_ignore,
1306 compare_heap_area_without_type(state, process_index, area1, area2, snapshot1, snapshot2,
1307 previous, size, check_ignore);
1309 if (res_compare == 1)
1313 state.match_equals(previous);
1320 /************************** Snapshot comparison *******************************/
1321 /******************************************************************************/
1323 static int compare_areas_with_type(simgrid::mc::StateComparator& state,
1325 void* real_area1, simgrid::mc::Snapshot* snapshot1, mc_mem_region_t region1,
1326 void* real_area2, simgrid::mc::Snapshot* snapshot2, mc_mem_region_t region2,
1327 simgrid::mc::Type* type, int pointer_level)
1329 simgrid::mc::Process* process = &mc_model_checker->process();
1331 simgrid::mc::Type* subtype;
1332 simgrid::mc::Type* subsubtype;
1333 int elm_size, i, res;
1336 switch (type->type) {
1337 case DW_TAG_unspecified_type:
1340 case DW_TAG_base_type:
1341 case DW_TAG_enumeration_type:
1342 case DW_TAG_union_type:
1344 return MC_snapshot_region_memcmp(
1345 real_area1, region1, real_area2, region2,
1346 type->byte_size) != 0;
1348 case DW_TAG_typedef:
1349 case DW_TAG_volatile_type:
1350 case DW_TAG_const_type:
1352 type = type->subtype;
1354 case DW_TAG_array_type:
1355 subtype = type->subtype;
1356 switch (subtype->type) {
1357 case DW_TAG_unspecified_type:
1360 case DW_TAG_base_type:
1361 case DW_TAG_enumeration_type:
1362 case DW_TAG_pointer_type:
1363 case DW_TAG_reference_type:
1364 case DW_TAG_rvalue_reference_type:
1365 case DW_TAG_structure_type:
1366 case DW_TAG_class_type:
1367 case DW_TAG_union_type:
1368 if (subtype->full_type)
1369 subtype = subtype->full_type;
1370 elm_size = subtype->byte_size;
1372 case DW_TAG_const_type:
1373 case DW_TAG_typedef:
1374 case DW_TAG_volatile_type:
1375 subsubtype = subtype->subtype;
1376 if (subsubtype->full_type)
1377 subsubtype = subsubtype->full_type;
1378 elm_size = subsubtype->byte_size;
1384 for (i = 0; i < type->element_count; i++) {
1385 size_t off = i * elm_size;
1386 res = compare_areas_with_type(state, process_index,
1387 (char*) real_area1 + off, snapshot1, region1,
1388 (char*) real_area2 + off, snapshot2, region2,
1389 type->subtype, pointer_level);
1394 case DW_TAG_pointer_type:
1395 case DW_TAG_reference_type:
1396 case DW_TAG_rvalue_reference_type:
1398 void* addr_pointed1 = MC_region_read_pointer(region1, real_area1);
1399 void* addr_pointed2 = MC_region_read_pointer(region2, real_area2);
1401 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type)
1402 return (addr_pointed1 != addr_pointed2);
1403 if (addr_pointed1 == nullptr && addr_pointed2 == nullptr)
1405 if (addr_pointed1 == nullptr || addr_pointed2 == nullptr)
1407 if (!state.compared_pointers.insert(
1408 std::make_pair(addr_pointed1, addr_pointed2)).second)
1413 // Some cases are not handled here:
1414 // * the pointers lead to different areas (one to the heap, the other to the RW segment ...);
1415 // * a pointer leads to the read-only segment of the current object;
1416 // * a pointer lead to a different ELF object.
1418 if (addr_pointed1 > process->heap_address
1419 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)) {
1421 (addr_pointed2 > process->heap_address
1422 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)))
1424 // The pointers are both in the heap:
1425 return simgrid::mc::compare_heap_area(state,
1426 process_index, addr_pointed1, addr_pointed2, snapshot1,
1427 snapshot2, nullptr, type->subtype, pointer_level);
1430 // The pointers are both in the current object R/W segment:
1431 else if (region1->contain(simgrid::mc::remote(addr_pointed1))) {
1432 if (!region2->contain(simgrid::mc::remote(addr_pointed2)))
1435 return (addr_pointed1 != addr_pointed2);
1437 return compare_areas_with_type(state, process_index,
1438 addr_pointed1, snapshot1, region1,
1439 addr_pointed2, snapshot2, region2,
1440 type->subtype, pointer_level);
1443 // TODO, We do not handle very well the case where
1444 // it belongs to a different (non-heap) region from the current one.
1447 return (addr_pointed1 != addr_pointed2);
1451 case DW_TAG_structure_type:
1452 case DW_TAG_class_type:
1453 for(simgrid::mc::Member& member : type->members) {
1454 void *member1 = simgrid::dwarf::resolve_member(
1455 real_area1, type, &member, snapshot1, process_index);
1456 void *member2 = simgrid::dwarf::resolve_member(
1457 real_area2, type, &member, snapshot2, process_index);
1458 mc_mem_region_t subregion1 = mc_get_region_hinted(member1, snapshot1, process_index, region1);
1459 mc_mem_region_t subregion2 = mc_get_region_hinted(member2, snapshot2, process_index, region2);
1461 compare_areas_with_type(state, process_index,
1462 member1, snapshot1, subregion1,
1463 member2, snapshot2, subregion2,
1464 member.type, pointer_level);
1469 case DW_TAG_subroutine_type:
1473 XBT_VERB("Unknown case: %d", type->type);
1480 static int compare_global_variables(
1481 simgrid::mc::StateComparator& state,
1482 simgrid::mc::ObjectInformation* object_info,
1484 mc_mem_region_t r1, mc_mem_region_t r2,
1485 simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2)
1487 xbt_assert(r1 && r2, "Missing region.");
1490 if (r1->storage_type() == simgrid::mc::StorageType::Privatized) {
1491 xbt_assert(process_index >= 0);
1492 if (r2->storage_type() != simgrid::mc::StorageType::Privatized)
1495 size_t process_count = MC_smpi_process_count();
1496 xbt_assert(process_count == r1->privatized_data().size()
1497 && process_count == r2->privatized_data().size());
1499 // Compare the global variables separately for each simulates process:
1500 for (size_t process_index = 0; process_index < process_count; process_index++) {
1501 if (compare_global_variables(state,
1502 object_info, process_index,
1503 &r1->privatized_data()[process_index],
1504 &r2->privatized_data()[process_index],
1505 snapshot1, snapshot2))
1511 xbt_assert(r1->storage_type() != simgrid::mc::StorageType::Privatized);
1513 xbt_assert(r2->storage_type() != simgrid::mc::StorageType::Privatized);
1515 std::vector<simgrid::mc::Variable>& variables = object_info->global_variables;
1517 for (simgrid::mc::Variable& current_var : variables) {
1519 // If the variable is not in this object, skip it:
1520 // We do not expect to find a pointer to something which is not reachable
1521 // by the global variables.
1522 if ((char *) current_var.address < (char *) object_info->start_rw
1523 || (char *) current_var.address > (char *) object_info->end_rw)
1526 simgrid::mc::Type* bvariable_type = current_var.type;
1527 int res = compare_areas_with_type(state, process_index,
1528 (char *) current_var.address, snapshot1, r1,
1529 (char *) current_var.address, snapshot2, r2,
1532 XBT_VERB("Global variable %s (%p) is different between snapshots",
1533 current_var.name.c_str(),
1534 (char *) current_var.address);
1544 static int compare_local_variables(simgrid::mc::StateComparator& state,
1546 simgrid::mc::Snapshot* snapshot1,
1547 simgrid::mc::Snapshot* snapshot2,
1548 mc_snapshot_stack_t stack1,
1549 mc_snapshot_stack_t stack2)
1551 if (stack1->local_variables.size() != stack2->local_variables.size()) {
1552 XBT_VERB("Different number of local variables");
1556 unsigned int cursor = 0;
1557 local_variable_t current_var1, current_var2;
1559 while (cursor < stack1->local_variables.size()) {
1560 current_var1 = &stack1->local_variables[cursor];
1561 current_var2 = &stack1->local_variables[cursor];
1562 if (current_var1->name != current_var2->name
1563 || current_var1->subprogram != current_var2->subprogram
1564 || current_var1->ip != current_var2->ip) {
1565 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1567 ("Different name of variable (%s - %s) "
1568 "or frame (%s - %s) or ip (%lu - %lu)",
1569 current_var1->name.c_str(),
1570 current_var2->name.c_str(),
1571 current_var1->subprogram->name.c_str(),
1572 current_var2->subprogram->name.c_str(),
1573 current_var1->ip, current_var2->ip);
1576 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1578 simgrid::mc::Type* subtype = current_var1->type;
1580 compare_areas_with_type(state, process_index,
1581 current_var1->address, snapshot1, mc_get_snapshot_region(current_var1->address, snapshot1, process_index),
1582 current_var2->address, snapshot2, mc_get_snapshot_region(current_var2->address, snapshot2, process_index),
1586 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1588 ("Local variable %s (%p - %p) in frame %s "
1589 "is different between snapshots",
1590 current_var1->name.c_str(),
1591 current_var1->address,
1592 current_var2->address,
1593 current_var1->subprogram->name.c_str());
1604 static std::unique_ptr<simgrid::mc::StateComparator> state_comparator;
1606 int snapshot_compare(int num1, simgrid::mc::Snapshot* s1, int num2, simgrid::mc::Snapshot* s2)
1608 // TODO, make this a field of ModelChecker or something similar
1610 if (state_comparator == nullptr)
1611 state_comparator = std::unique_ptr<StateComparator>(new StateComparator());
1613 state_comparator->clear();
1615 simgrid::mc::Process* process = &mc_model_checker->process();
1619 int hash_result = 0;
1621 hash_result = (s1->hash != s2->hash);
1623 XBT_VERB("(%d - %d) Different hash: 0x%" PRIx64 "--0x%" PRIx64, num1, num2, s1->hash, s2->hash);
1628 XBT_VERB("(%d - %d) Same hash: 0x%" PRIx64, num1, num2, s1->hash);
1631 /* Compare enabled processes */
1632 if (s1->enabled_processes != s2->enabled_processes) {
1633 XBT_VERB("(%d - %d) Different amount of enabled processes", num1, num2);
1637 /* Compare size of stacks */
1639 for (unsigned long i = 0; i < s1->stacks.size(); i++) {
1640 size_t size_used1 = s1->stack_sizes[i];
1641 size_t size_used2 = s2->stack_sizes[i];
1642 if (size_used1 != size_used2) {
1644 XBT_DEBUG("(%d - %d) Different size used in stacks: %zu - %zu", num1, num2, size_used1, size_used2);
1649 XBT_VERB("(%d - %d) Different size used in stacks: %zu - %zu", num1, num2, size_used1, size_used2);
1655 if (is_diff) // do not proceed if there is any stacks that don't match
1658 /* Init heap information used in heap comparison algorithm */
1659 xbt_mheap_t heap1 = (xbt_mheap_t)s1->read_bytes(
1660 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1661 remote(process->heap_address),
1662 simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1663 xbt_mheap_t heap2 = (xbt_mheap_t)s2->read_bytes(
1664 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1665 remote(process->heap_address),
1666 simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1667 int res_init = state_comparator->initHeapInformation(heap1, heap2, &s1->to_ignore, &s2->to_ignore);
1669 if (res_init == -1) {
1671 XBT_DEBUG("(%d - %d) Different heap information", num1, num2);
1675 XBT_VERB("(%d - %d) Different heap information", num1, num2);
1682 /* Stacks comparison */
1684 for (unsigned int cursor = 0; cursor < s1->stacks.size(); cursor++) {
1685 mc_snapshot_stack_t stack1 = &s1->stacks[cursor];
1686 mc_snapshot_stack_t stack2 = &s2->stacks[cursor];
1688 if (stack1->process_index != stack2->process_index) {
1690 XBT_DEBUG("(%d - %d) Stacks with different process index (%i vs %i)", num1, num2,
1691 stack1->process_index, stack2->process_index);
1693 else diff_local = compare_local_variables(*state_comparator,
1694 stack1->process_index, s1, s2, stack1, stack2);
1695 if (diff_local > 0) {
1697 XBT_DEBUG("(%d - %d) Different local variables between stacks %d", num1,
1704 XBT_VERB("(%d - %d) Different local variables between stacks %d", num1,
1713 size_t regions_count = s1->snapshot_regions.size();
1714 // TODO, raise a difference instead?
1715 xbt_assert(regions_count == s2->snapshot_regions.size());
1717 for (size_t k = 0; k != regions_count; ++k) {
1718 mc_mem_region_t region1 = s1->snapshot_regions[k].get();
1719 mc_mem_region_t region2 = s2->snapshot_regions[k].get();
1722 if (region1->region_type() != simgrid::mc::RegionType::Data)
1725 xbt_assert(region1->region_type() == region2->region_type());
1726 xbt_assert(region1->object_info() == region2->object_info());
1727 xbt_assert(region1->object_info());
1729 std::string const& name = region1->object_info()->file_name;
1731 /* Compare global variables */
1732 if (compare_global_variables(*state_comparator, region1->object_info(), simgrid::mc::ProcessIndexDisabled, region1,
1736 XBT_DEBUG("(%d - %d) Different global variables in %s",
1737 num1, num2, name.c_str());
1741 XBT_VERB("(%d - %d) Different global variables in %s",
1742 num1, num2, name.c_str());
1751 if (simgrid::mc::mmalloc_compare_heap(*state_comparator, s1, s2) > 0) {
1754 XBT_DEBUG("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
1759 XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
1766 if (errors || hash_result)
1767 XBT_VERB("(%d - %d) Difference found", num1, num2);
1769 XBT_VERB("(%d - %d) No difference found", num1, num2);
1772 #if defined(MC_DEBUG) && defined(MC_VERBOSE)
1774 // * false positive SHOULD be avoided.
1775 // * There MUST not be any false negative.
1777 XBT_VERB("(%d - %d) State equality hash test is %s %s", num1, num2,
1778 (hash_result != 0) == (errors != 0) ? "true" : "false",
1779 !hash_result ? "positive" : "negative");
1783 return errors > 0 || hash_result;