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);
916 xbt_die("Unreachable");
919 /** Infer the type of a part of the block from the type of the block
921 * TODO, handle DW_TAG_array_type as well as arrays of the object ((*p)[5], p[5])
923 * TODO, handle subfields ((*p).bar.foo, (*p)[5].bar…)
925 * @param type DWARF type ID of the root address
927 * @return DWARF type ID for given offset
929 static simgrid::mc::Type* get_offset_type(void *real_base_address, simgrid::mc::Type* type,
930 int offset, int area_size,
931 simgrid::mc::Snapshot* snapshot, int process_index)
934 // Beginning of the block, the infered variable type if the type of the block:
938 switch (type->type) {
940 case DW_TAG_structure_type:
941 case DW_TAG_class_type:
943 type = type->full_type;
944 if (area_size != -1 && type->byte_size != area_size) {
945 if (area_size > type->byte_size && area_size % type->byte_size == 0)
951 for(simgrid::mc::Member& member : type->members) {
952 if (member.has_offset_location()) {
953 // We have the offset, use it directly (shortcut):
954 if (member.offset() == offset)
957 void *real_member = simgrid::dwarf::resolve_member(
958 real_base_address, type, &member, snapshot, process_index);
959 if ((char*) real_member - (char *) real_base_address == offset)
966 /* FIXME: other cases ? */
974 * @param area1 Process address for state 1
975 * @param area2 Process address for state 2
976 * @param snapshot1 Snapshot of state 1
977 * @param snapshot2 Snapshot of state 2
978 * @param previous Pairs of blocks already compared on the current path (or nullptr)
979 * @param type_id Type of variable
980 * @param pointer_level
981 * @return 0 (same), 1 (different), -1
984 int compare_heap_area(simgrid::mc::StateComparator& state, int process_index,
985 const void *area1, const void *area2,
986 simgrid::mc::Snapshot* snapshot1,
987 simgrid::mc::Snapshot* snapshot2,
988 HeapLocationPairs* previous,
989 simgrid::mc::Type* type, int pointer_level)
991 simgrid::mc::Process* process = &mc_model_checker->process();
994 ssize_t block1, frag1, block2, frag2;
996 int check_ignore = 0;
998 void *real_addr_block1, *real_addr_block2, *real_addr_frag1, *real_addr_frag2;
1000 int offset1 = 0, offset2 = 0;
1001 int new_size1 = -1, new_size2 = -1;
1002 simgrid::mc::Type *new_type1 = nullptr, *new_type2 = nullptr;
1004 bool match_pairs = false;
1006 // This is the address of std_heap->heapinfo in the application process:
1007 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
1009 const malloc_info* heapinfos1 = snapshot1->read(
1010 remote((const malloc_info**)heapinfo_address), process_index);
1011 const malloc_info* heapinfos2 = snapshot2->read(
1012 remote((const malloc_info**)heapinfo_address), process_index);
1014 malloc_info heapinfo_temp1, heapinfo_temp2;
1016 simgrid::mc::HeapLocationPairs current;
1017 if (previous == nullptr) {
1018 previous = ¤t;
1022 // Get block number:
1025 (char *) state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
1028 (char *) state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
1030 // If either block is a stack block:
1031 if (is_block_stack((int) block1) && is_block_stack((int) block2)) {
1032 previous->insert(simgrid::mc::makeHeapLocationPair(
1033 block1, -1, block2, -1));
1035 state.match_equals(previous);
1039 // If either block is not in the expected area of memory:
1040 if (((char *) area1 < (char *) state.std_heap_copy.heapbase)
1041 || (block1 > (ssize_t) state.processStates[0].heapsize) || (block1 < 1)
1042 || ((char *) area2 < (char *) state.std_heap_copy.heapbase)
1043 || (block2 > (ssize_t) state.processStates[1].heapsize) || (block2 < 1)) {
1047 // Process address of the block:
1048 real_addr_block1 = (ADDR2UINT(block1) - 1) * BLOCKSIZE +
1049 (char *) state.std_heap_copy.heapbase;
1050 real_addr_block2 = (ADDR2UINT(block2) - 1) * BLOCKSIZE +
1051 (char *) state.std_heap_copy.heapbase;
1055 if (type->full_type)
1056 type = type->full_type;
1058 // This assume that for "boring" types (volatile ...) byte_size is absent:
1059 while (type->byte_size == 0 && type->subtype != nullptr)
1060 type = type->subtype;
1063 if (type->type == DW_TAG_pointer_type
1064 || (type->type == DW_TAG_base_type && !type->name.empty()
1065 && type->name == "char"))
1068 type_size = type->byte_size;
1072 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
1073 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
1075 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
1076 heap_region1, &heapinfo_temp1, &heapinfos1[block1], sizeof(malloc_info));
1077 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
1078 heap_region2, &heapinfo_temp2, &heapinfos2[block2], sizeof(malloc_info));
1080 if ((heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type==MMALLOC_TYPE_HEAPINFO)
1081 && (heapinfo2->type == MMALLOC_TYPE_FREE || heapinfo2->type ==MMALLOC_TYPE_HEAPINFO)) {
1084 state.match_equals(previous);
1088 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED
1089 && heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
1090 /* Complete block */
1092 // TODO, lookup variable type from block type as done for fragmented blocks
1094 offset1 = (char *) area1 - (char *) real_addr_block1;
1095 offset2 = (char *) area2 - (char *) real_addr_block2;
1097 if (state.equals_to1_(block1, 0).valid
1098 && state.equals_to2_(block2, 0).valid
1099 && state.blocksEqual(block1, block2)) {
1101 state.match_equals(previous);
1105 if (type_size != -1) {
1106 if (type_size != (ssize_t) heapinfo1->busy_block.busy_size
1107 && type_size != (ssize_t) heapinfo2->busy_block.busy_size
1108 && (type->name.empty() || type->name == "struct s_smx_context")) {
1110 state.match_equals(previous);
1115 if (heapinfo1->busy_block.size != heapinfo2->busy_block.size)
1117 if (heapinfo1->busy_block.busy_size != heapinfo2->busy_block.busy_size)
1120 if (!previous->insert(simgrid::mc::makeHeapLocationPair(
1121 block1, -1, block2, -1)).second) {
1123 state.match_equals(previous);
1127 size = heapinfo1->busy_block.busy_size;
1129 // Remember (basic) type inference.
1130 // The current data structure only allows us to do this for the whole block.
1131 if (type != nullptr && area1 == real_addr_block1)
1132 state.types1_(block1, 0) = type;
1133 if (type != nullptr && area2 == real_addr_block2)
1134 state.types2_(block2, 0) = type;
1138 state.match_equals(previous);
1145 if (heapinfo1->busy_block.ignore > 0
1146 && heapinfo2->busy_block.ignore == heapinfo1->busy_block.ignore)
1147 check_ignore = heapinfo1->busy_block.ignore;
1149 } else if ((heapinfo1->type > 0) && (heapinfo2->type > 0)) { /* Fragmented block */
1153 ((uintptr_t) (ADDR2UINT(area1) % (BLOCKSIZE))) >> heapinfo1->type;
1155 ((uintptr_t) (ADDR2UINT(area2) % (BLOCKSIZE))) >> heapinfo2->type;
1157 // Process address of the fragment:
1159 (void *) ((char *) real_addr_block1 +
1160 (frag1 << heapinfo1->type));
1162 (void *) ((char *) real_addr_block2 +
1163 (frag2 << heapinfo2->type));
1165 // Check the size of the fragments against the size of the type:
1166 if (type_size != -1) {
1167 if (heapinfo1->busy_frag.frag_size[frag1] == -1
1168 || heapinfo2->busy_frag.frag_size[frag2] == -1) {
1170 state.match_equals(previous);
1174 if (type_size != heapinfo1->busy_frag.frag_size[frag1]
1175 || type_size != heapinfo2->busy_frag.frag_size[frag2]) {
1177 state.match_equals(previous);
1182 // Check if the blocks are already matched together:
1183 if (state.equals_to1_(block1, frag1).valid
1184 && state.equals_to2_(block2, frag2).valid) {
1185 if (offset1==offset2 && state.fragmentsEqual(block1, frag1, block2, frag2)) {
1187 state.match_equals(previous);
1191 // Compare the size of both fragments:
1192 if (heapinfo1->busy_frag.frag_size[frag1] !=
1193 heapinfo2->busy_frag.frag_size[frag2]) {
1194 if (type_size == -1) {
1196 state.match_equals(previous);
1202 // Size of the fragment:
1203 size = heapinfo1->busy_frag.frag_size[frag1];
1205 // Remember (basic) type inference.
1206 // The current data structure only allows us to do this for the whole fragment.
1207 if (type != nullptr && area1 == real_addr_frag1)
1208 state.types1_(block1, frag1) = type;
1209 if (type != nullptr && area2 == real_addr_frag2)
1210 state.types2_(block2, frag2) = type;
1212 // The type of the variable is already known:
1217 // Type inference from the block type.
1218 else if (state.types1_(block1, frag1) != nullptr
1219 || state.types2_(block2, frag2) != nullptr) {
1221 offset1 = (char *) area1 - (char *) real_addr_frag1;
1222 offset2 = (char *) area2 - (char *) real_addr_frag2;
1224 if (state.types1_(block1, frag1) != nullptr
1225 && state.types2_(block2, frag2) != nullptr) {
1227 get_offset_type(real_addr_frag1, state.types1_(block1, frag1),
1228 offset1, size, snapshot1, process_index);
1230 get_offset_type(real_addr_frag2, state.types2_(block2, frag2),
1231 offset1, size, snapshot2, process_index);
1232 } else if (state.types1_(block1, frag1) != nullptr) {
1234 get_offset_type(real_addr_frag1, state.types1_(block1, frag1),
1235 offset1, size, snapshot1, process_index);
1237 get_offset_type(real_addr_frag2, state.types1_(block1, frag1),
1238 offset2, size, snapshot2, process_index);
1239 } else if (state.types2_(block2, frag2) != nullptr) {
1241 get_offset_type(real_addr_frag1, state.types2_(block2, frag2),
1242 offset1, size, snapshot1, process_index);
1244 get_offset_type(real_addr_frag2, state.types2_(block2, frag2),
1245 offset2, size, snapshot2, process_index);
1248 state.match_equals(previous);
1252 if (new_type1 != nullptr && new_type2 != nullptr && new_type1 != new_type2) {
1255 while (type->byte_size == 0 && type->subtype != nullptr)
1256 type = type->subtype;
1257 new_size1 = type->byte_size;
1260 while (type->byte_size == 0 && type->subtype != nullptr)
1261 type = type->subtype;
1262 new_size2 = type->byte_size;
1266 state.match_equals(previous);
1271 if (new_size1 > 0 && new_size1 == new_size2) {
1276 if (offset1 == 0 && offset2 == 0
1277 && !previous->insert(simgrid::mc::makeHeapLocationPair(
1278 block1, frag1, block2, frag2)).second) {
1280 state.match_equals(previous);
1286 state.match_equals(previous);
1290 if ((heapinfo1->busy_frag.ignore[frag1] > 0)
1291 && (heapinfo2->busy_frag.ignore[frag2] ==
1292 heapinfo1->busy_frag.ignore[frag1]))
1293 check_ignore = heapinfo1->busy_frag.ignore[frag1];
1299 /* Start comparison */
1302 compare_heap_area_with_type(state, process_index, area1, area2, snapshot1, snapshot2,
1303 previous, type, size, check_ignore,
1307 compare_heap_area_without_type(state, process_index, area1, area2, snapshot1, snapshot2,
1308 previous, size, check_ignore);
1310 if (res_compare == 1)
1314 state.match_equals(previous);
1321 /************************** Snapshot comparison *******************************/
1322 /******************************************************************************/
1324 static int compare_areas_with_type(simgrid::mc::StateComparator& state,
1326 void* real_area1, simgrid::mc::Snapshot* snapshot1, mc_mem_region_t region1,
1327 void* real_area2, simgrid::mc::Snapshot* snapshot2, mc_mem_region_t region2,
1328 simgrid::mc::Type* type, int pointer_level)
1330 simgrid::mc::Process* process = &mc_model_checker->process();
1332 simgrid::mc::Type* subtype;
1333 simgrid::mc::Type* subsubtype;
1334 int elm_size, i, res;
1337 switch (type->type) {
1338 case DW_TAG_unspecified_type:
1341 case DW_TAG_base_type:
1342 case DW_TAG_enumeration_type:
1343 case DW_TAG_union_type:
1345 return MC_snapshot_region_memcmp(
1346 real_area1, region1, real_area2, region2,
1347 type->byte_size) != 0;
1349 case DW_TAG_typedef:
1350 case DW_TAG_volatile_type:
1351 case DW_TAG_const_type:
1353 type = type->subtype;
1355 case DW_TAG_array_type:
1356 subtype = type->subtype;
1357 switch (subtype->type) {
1358 case DW_TAG_unspecified_type:
1361 case DW_TAG_base_type:
1362 case DW_TAG_enumeration_type:
1363 case DW_TAG_pointer_type:
1364 case DW_TAG_reference_type:
1365 case DW_TAG_rvalue_reference_type:
1366 case DW_TAG_structure_type:
1367 case DW_TAG_class_type:
1368 case DW_TAG_union_type:
1369 if (subtype->full_type)
1370 subtype = subtype->full_type;
1371 elm_size = subtype->byte_size;
1373 case DW_TAG_const_type:
1374 case DW_TAG_typedef:
1375 case DW_TAG_volatile_type:
1376 subsubtype = subtype->subtype;
1377 if (subsubtype->full_type)
1378 subsubtype = subsubtype->full_type;
1379 elm_size = subsubtype->byte_size;
1385 for (i = 0; i < type->element_count; i++) {
1386 size_t off = i * elm_size;
1387 res = compare_areas_with_type(state, process_index,
1388 (char*) real_area1 + off, snapshot1, region1,
1389 (char*) real_area2 + off, snapshot2, region2,
1390 type->subtype, pointer_level);
1395 case DW_TAG_pointer_type:
1396 case DW_TAG_reference_type:
1397 case DW_TAG_rvalue_reference_type:
1399 void* addr_pointed1 = MC_region_read_pointer(region1, real_area1);
1400 void* addr_pointed2 = MC_region_read_pointer(region2, real_area2);
1402 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type)
1403 return (addr_pointed1 != addr_pointed2);
1404 if (addr_pointed1 == nullptr && addr_pointed2 == nullptr)
1406 if (addr_pointed1 == nullptr || addr_pointed2 == nullptr)
1408 if (!state.compared_pointers.insert(
1409 std::make_pair(addr_pointed1, addr_pointed2)).second)
1414 // Some cases are not handled here:
1415 // * the pointers lead to different areas (one to the heap, the other to the RW segment ...);
1416 // * a pointer leads to the read-only segment of the current object;
1417 // * a pointer lead to a different ELF object.
1419 if (addr_pointed1 > process->heap_address
1420 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)) {
1422 (addr_pointed2 > process->heap_address
1423 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)))
1425 // The pointers are both in the heap:
1426 return simgrid::mc::compare_heap_area(state,
1427 process_index, addr_pointed1, addr_pointed2, snapshot1,
1428 snapshot2, nullptr, type->subtype, pointer_level);
1431 // The pointers are both in the current object R/W segment:
1432 else if (region1->contain(simgrid::mc::remote(addr_pointed1))) {
1433 if (!region2->contain(simgrid::mc::remote(addr_pointed2)))
1436 return (addr_pointed1 != addr_pointed2);
1438 return compare_areas_with_type(state, process_index,
1439 addr_pointed1, snapshot1, region1,
1440 addr_pointed2, snapshot2, region2,
1441 type->subtype, pointer_level);
1444 // TODO, We do not handle very well the case where
1445 // it belongs to a different (non-heap) region from the current one.
1448 return (addr_pointed1 != addr_pointed2);
1452 case DW_TAG_structure_type:
1453 case DW_TAG_class_type:
1454 for(simgrid::mc::Member& member : type->members) {
1455 void *member1 = simgrid::dwarf::resolve_member(
1456 real_area1, type, &member, snapshot1, process_index);
1457 void *member2 = simgrid::dwarf::resolve_member(
1458 real_area2, type, &member, snapshot2, process_index);
1459 mc_mem_region_t subregion1 = mc_get_region_hinted(member1, snapshot1, process_index, region1);
1460 mc_mem_region_t subregion2 = mc_get_region_hinted(member2, snapshot2, process_index, region2);
1462 compare_areas_with_type(state, process_index,
1463 member1, snapshot1, subregion1,
1464 member2, snapshot2, subregion2,
1465 member.type, pointer_level);
1470 case DW_TAG_subroutine_type:
1474 XBT_VERB("Unknown case: %d", type->type);
1481 static int compare_global_variables(
1482 simgrid::mc::StateComparator& state,
1483 simgrid::mc::ObjectInformation* object_info,
1485 mc_mem_region_t r1, mc_mem_region_t r2,
1486 simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2)
1488 xbt_assert(r1 && r2, "Missing region.");
1491 if (r1->storage_type() == simgrid::mc::StorageType::Privatized) {
1492 xbt_assert(process_index >= 0);
1493 if (r2->storage_type() != simgrid::mc::StorageType::Privatized)
1496 size_t process_count = MC_smpi_process_count();
1497 xbt_assert(process_count == r1->privatized_data().size()
1498 && process_count == r2->privatized_data().size());
1500 // Compare the global variables separately for each simulates process:
1501 for (size_t process_index = 0; process_index < process_count; process_index++) {
1502 if (compare_global_variables(state,
1503 object_info, process_index,
1504 &r1->privatized_data()[process_index],
1505 &r2->privatized_data()[process_index],
1506 snapshot1, snapshot2))
1512 xbt_assert(r1->storage_type() != simgrid::mc::StorageType::Privatized);
1514 xbt_assert(r2->storage_type() != simgrid::mc::StorageType::Privatized);
1516 std::vector<simgrid::mc::Variable>& variables = object_info->global_variables;
1518 for (simgrid::mc::Variable& current_var : variables) {
1520 // If the variable is not in this object, skip it:
1521 // We do not expect to find a pointer to something which is not reachable
1522 // by the global variables.
1523 if ((char *) current_var.address < (char *) object_info->start_rw
1524 || (char *) current_var.address > (char *) object_info->end_rw)
1527 simgrid::mc::Type* bvariable_type = current_var.type;
1528 int res = compare_areas_with_type(state, process_index,
1529 (char *) current_var.address, snapshot1, r1,
1530 (char *) current_var.address, snapshot2, r2,
1533 XBT_VERB("Global variable %s (%p) is different between snapshots",
1534 current_var.name.c_str(),
1535 (char *) current_var.address);
1545 static int compare_local_variables(simgrid::mc::StateComparator& state,
1547 simgrid::mc::Snapshot* snapshot1,
1548 simgrid::mc::Snapshot* snapshot2,
1549 mc_snapshot_stack_t stack1,
1550 mc_snapshot_stack_t stack2)
1552 if (stack1->local_variables.size() != stack2->local_variables.size()) {
1553 XBT_VERB("Different number of local variables");
1557 unsigned int cursor = 0;
1558 local_variable_t current_var1, current_var2;
1560 while (cursor < stack1->local_variables.size()) {
1561 current_var1 = &stack1->local_variables[cursor];
1562 current_var2 = &stack1->local_variables[cursor];
1563 if (current_var1->name != current_var2->name
1564 || current_var1->subprogram != current_var2->subprogram
1565 || current_var1->ip != current_var2->ip) {
1566 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1568 ("Different name of variable (%s - %s) "
1569 "or frame (%s - %s) or ip (%lu - %lu)",
1570 current_var1->name.c_str(),
1571 current_var2->name.c_str(),
1572 current_var1->subprogram->name.c_str(),
1573 current_var2->subprogram->name.c_str(),
1574 current_var1->ip, current_var2->ip);
1577 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1579 simgrid::mc::Type* subtype = current_var1->type;
1581 compare_areas_with_type(state, process_index,
1582 current_var1->address, snapshot1, mc_get_snapshot_region(current_var1->address, snapshot1, process_index),
1583 current_var2->address, snapshot2, mc_get_snapshot_region(current_var2->address, snapshot2, process_index),
1587 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1589 ("Local variable %s (%p - %p) in frame %s "
1590 "is different between snapshots",
1591 current_var1->name.c_str(),
1592 current_var1->address,
1593 current_var2->address,
1594 current_var1->subprogram->name.c_str());
1605 static std::unique_ptr<simgrid::mc::StateComparator> state_comparator;
1607 int snapshot_compare(int num1, simgrid::mc::Snapshot* s1, int num2, simgrid::mc::Snapshot* s2)
1609 // TODO, make this a field of ModelChecker or something similar
1611 if (state_comparator == nullptr)
1612 state_comparator = std::unique_ptr<StateComparator>(new StateComparator());
1614 state_comparator->clear();
1616 simgrid::mc::Process* process = &mc_model_checker->process();
1620 int hash_result = 0;
1622 hash_result = (s1->hash != s2->hash);
1624 XBT_VERB("(%d - %d) Different hash: 0x%" PRIx64 "--0x%" PRIx64, num1, num2, s1->hash, s2->hash);
1629 XBT_VERB("(%d - %d) Same hash: 0x%" PRIx64, num1, num2, s1->hash);
1632 /* Compare enabled processes */
1633 if (s1->enabled_processes != s2->enabled_processes) {
1634 XBT_VERB("(%d - %d) Different amount of enabled processes", num1, num2);
1638 /* Compare size of stacks */
1640 for (unsigned long i = 0; i < s1->stacks.size(); i++) {
1641 size_t size_used1 = s1->stack_sizes[i];
1642 size_t size_used2 = s2->stack_sizes[i];
1643 if (size_used1 != size_used2) {
1645 XBT_DEBUG("(%d - %d) Different size used in stacks: %zu - %zu", num1, num2, size_used1, size_used2);
1650 XBT_VERB("(%d - %d) Different size used in stacks: %zu - %zu", num1, num2, size_used1, size_used2);
1656 if (is_diff) // do not proceed if there is any stacks that don't match
1659 /* Init heap information used in heap comparison algorithm */
1660 xbt_mheap_t heap1 = (xbt_mheap_t)s1->read_bytes(
1661 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1662 remote(process->heap_address),
1663 simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1664 xbt_mheap_t heap2 = (xbt_mheap_t)s2->read_bytes(
1665 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1666 remote(process->heap_address),
1667 simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1668 int res_init = state_comparator->initHeapInformation(heap1, heap2, &s1->to_ignore, &s2->to_ignore);
1670 if (res_init == -1) {
1672 XBT_DEBUG("(%d - %d) Different heap information", num1, num2);
1676 XBT_VERB("(%d - %d) Different heap information", num1, num2);
1683 /* Stacks comparison */
1685 for (unsigned int cursor = 0; cursor < s1->stacks.size(); cursor++) {
1686 mc_snapshot_stack_t stack1 = &s1->stacks[cursor];
1687 mc_snapshot_stack_t stack2 = &s2->stacks[cursor];
1689 if (stack1->process_index != stack2->process_index) {
1691 XBT_DEBUG("(%d - %d) Stacks with different process index (%i vs %i)", num1, num2,
1692 stack1->process_index, stack2->process_index);
1694 else diff_local = compare_local_variables(*state_comparator,
1695 stack1->process_index, s1, s2, stack1, stack2);
1696 if (diff_local > 0) {
1698 XBT_DEBUG("(%d - %d) Different local variables between stacks %d", num1,
1705 XBT_VERB("(%d - %d) Different local variables between stacks %d", num1,
1714 size_t regions_count = s1->snapshot_regions.size();
1715 // TODO, raise a difference instead?
1716 xbt_assert(regions_count == s2->snapshot_regions.size());
1718 for (size_t k = 0; k != regions_count; ++k) {
1719 mc_mem_region_t region1 = s1->snapshot_regions[k].get();
1720 mc_mem_region_t region2 = s2->snapshot_regions[k].get();
1723 if (region1->region_type() != simgrid::mc::RegionType::Data)
1726 xbt_assert(region1->region_type() == region2->region_type());
1727 xbt_assert(region1->object_info() == region2->object_info());
1728 xbt_assert(region1->object_info());
1730 std::string const& name = region1->object_info()->file_name;
1732 /* Compare global variables */
1733 if (compare_global_variables(*state_comparator, region1->object_info(), simgrid::mc::ProcessIndexDisabled, region1,
1737 XBT_DEBUG("(%d - %d) Different global variables in %s",
1738 num1, num2, name.c_str());
1742 XBT_VERB("(%d - %d) Different global variables in %s",
1743 num1, num2, name.c_str());
1752 if (simgrid::mc::mmalloc_compare_heap(*state_comparator, s1, s2) > 0) {
1755 XBT_DEBUG("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
1760 XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
1767 if (errors || hash_result)
1768 XBT_VERB("(%d - %d) Difference found", num1, num2);
1770 XBT_VERB("(%d - %d) No difference found", num1, num2);
1773 #if defined(MC_DEBUG) && defined(MC_VERBOSE)
1775 // * false positive SHOULD be avoided.
1776 // * There MUST not be any false negative.
1778 XBT_VERB("(%d - %d) State equality hash test is %s %s", num1, num2,
1779 (hash_result != 0) == (errors != 0) ? "true" : "false",
1780 !hash_result ? "positive" : "negative");
1784 return errors > 0 || hash_result;