1 /* Copyright (c) 2008-2016. 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"
26 #include "src/xbt/ex_interface.h"
29 #include "src/smpi/private.h"
32 #include "src/mc/mc_forward.hpp"
33 #include "src/mc/mc_safety.h"
34 #include "src/mc/mc_private.h"
35 #include "src/mc/mc_smx.h"
36 #include "src/mc/mc_dwarf.hpp"
37 #include "src/mc/Frame.hpp"
38 #include "src/mc/ObjectInformation.hpp"
39 #include "src/mc/Variable.hpp"
40 #include "src/mc/mc_private.h"
41 #include "src/mc/mc_snapshot.h"
42 #include "src/mc/mc_dwarf.hpp"
43 #include "src/mc/Type.hpp"
45 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_compare, xbt,
46 "Logging specific to mc_compare in mc");
52 typedef std::array<HeapLocation, 2> HeapLocationPair;
53 typedef std::set<HeapLocationPair> HeapLocationPairs;
55 struct ProcessComparisonState;
56 struct StateComparator;
58 static int compare_heap_area(
59 StateComparator& state,
60 int process_index, const void *area1, const void* area2,
61 Snapshot* snapshot1, Snapshot* snapshot2,
62 HeapLocationPairs* previous, Type* type, int pointer_level);
67 using simgrid::mc::remote;
69 /*********************************** Heap comparison ***********************************/
70 /***************************************************************************************/
80 HeapLocation(int block, int fragment = 0) : block(block), fragment(fragment) {}
82 bool operator==(HeapLocation const& that) const
84 return block == that.block && fragment == that.fragment;
86 bool operator<(HeapLocation const& that) const
88 return std::make_pair(block, fragment)
89 < std::make_pair(that.block, that.fragment);
94 HeapLocationPair makeHeapLocationPair(int block1, int fragment1, int block2, int fragment2)
96 return simgrid::mc::HeapLocationPair{{
97 simgrid::mc::HeapLocation(block1, fragment1),
98 simgrid::mc::HeapLocation(block2, fragment2)
102 struct HeapArea : public HeapLocation {
108 : valid(true), block(block) {}
109 HeapArea(int block, int fragment = 0)
110 : valid(true), block(block), fragment(fragment) {}
113 struct ProcessComparisonState {
114 std::vector<simgrid::mc::IgnoredHeapRegion>* to_ignore = nullptr;
115 std::vector<HeapArea> equals_to;
116 std::vector<simgrid::mc::Type*> types;
117 std::size_t heapsize = 0;
119 void initHeapInformation(xbt_mheap_t heap,
120 std::vector<simgrid::mc::IgnoredHeapRegion>* i);
125 /** A hash which works with more stuff
127 * It can hash pairs: the standard hash currently doesn't include this.
129 template<class X> struct hash : public std::hash<X> {};
131 template<class X, class Y>
132 struct hash<std::pair<X,Y>> {
133 std::size_t operator()(std::pair<X,Y>const& x) const
137 return h1(x.first) ^ h2(x.second);
144 struct StateComparator {
145 s_xbt_mheap_t std_heap_copy;
146 std::size_t heaplimit;
147 std::array<ProcessComparisonState, 2> processStates;
149 std::unordered_set<std::pair<void*, void*>, hash<std::pair<void*, void*>>> compared_pointers;
153 compared_pointers.clear();
156 int initHeapInformation(
157 xbt_mheap_t heap1, xbt_mheap_t heap2,
158 std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
159 std::vector<simgrid::mc::IgnoredHeapRegion>* i2);
161 HeapArea& equals_to1_(std::size_t i, std::size_t j)
163 return processStates[0].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
165 HeapArea& equals_to2_(std::size_t i, std::size_t j)
167 return processStates[1].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
169 Type*& types1_(std::size_t i, std::size_t j)
171 return processStates[0].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
173 Type*& types2_(std::size_t i, std::size_t j)
175 return processStates[1].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
178 HeapArea const& equals_to1_(std::size_t i, std::size_t j) const
180 return processStates[0].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
182 HeapArea const& equals_to2_(std::size_t i, std::size_t j) const
184 return processStates[1].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
186 Type* const& types1_(std::size_t i, std::size_t j) const
188 return processStates[0].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
190 Type* const& types2_(std::size_t i, std::size_t j) const
192 return processStates[1].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
195 /** Check whether two blocks are known to be matching
197 * @param b1 Block of state 1
198 * @param b2 Block of state 2
199 * @return if the blocks are known to be matching
201 bool blocksEqual(int b1, int b2) const
203 return this->equals_to1_(b1, 0).block == b2
204 && this->equals_to2_(b2, 0).block == b1;
207 /** Check whether two fragments are known to be matching
209 * @param b1 Block of state 1
210 * @param f1 Fragment of state 1
211 * @param b2 Block of state 2
212 * @param f2 Fragment of state 2
213 * @return if the fragments are known to be matching
215 int fragmentsEqual(int b1, int f1, int b2, int f2) const
217 return this->equals_to1_(b1, f1).block == b2
218 && this->equals_to1_(b1, f1).fragment == f2
219 && this->equals_to2_(b2, f2).block == b1
220 && this->equals_to2_(b2, f2).fragment == f1;
223 void match_equals(HeapLocationPairs* list);
229 /************************************************************************************/
231 static ssize_t heap_comparison_ignore_size(
232 std::vector<simgrid::mc::IgnoredHeapRegion>* ignore_list,
236 int end = ignore_list->size() - 1;
238 while (start <= end) {
239 unsigned int cursor = (start + end) / 2;
240 simgrid::mc::IgnoredHeapRegion const& region = (*ignore_list)[cursor];
241 if (region.address == address)
243 if (region.address < address)
245 if (region.address > address)
252 static bool is_stack(const void *address)
254 for (auto const& stack : mc_model_checker->process().stack_areas())
255 if (address == stack.address)
260 // TODO, this should depend on the snapshot?
261 static bool is_block_stack(int block)
263 for (auto const& stack : mc_model_checker->process().stack_areas())
264 if (block == stack.block)
272 void StateComparator::match_equals(HeapLocationPairs* list)
274 for (auto const& pair : *list) {
275 if (pair[0].fragment != -1) {
276 this->equals_to1_(pair[0].block, pair[0].fragment) =
277 simgrid::mc::HeapArea(pair[1].block, pair[1].fragment);
278 this->equals_to2_(pair[1].block, pair[1].fragment) =
279 simgrid::mc::HeapArea(pair[0].block, pair[0].fragment);
281 this->equals_to1_(pair[0].block, 0) =
282 simgrid::mc::HeapArea(pair[1].block, pair[1].fragment);
283 this->equals_to2_(pair[1].block, 0) =
284 simgrid::mc::HeapArea(pair[0].block, pair[0].fragment);
289 void ProcessComparisonState::initHeapInformation(xbt_mheap_t heap,
290 std::vector<simgrid::mc::IgnoredHeapRegion>* i)
292 auto heaplimit = ((struct mdesc *) heap)->heaplimit;
293 this->heapsize = ((struct mdesc *) heap)->heapsize;
295 this->equals_to.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, HeapArea());
296 this->types.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, nullptr);
299 int StateComparator::initHeapInformation(xbt_mheap_t heap1, xbt_mheap_t heap2,
300 std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
301 std::vector<simgrid::mc::IgnoredHeapRegion>* i2)
303 if ((((struct mdesc *) heap1)->heaplimit !=
304 ((struct mdesc *) heap2)->heaplimit)
306 ((((struct mdesc *) heap1)->heapsize !=
307 ((struct mdesc *) heap2)->heapsize)))
309 this->heaplimit = ((struct mdesc *) heap1)->heaplimit;
310 this->std_heap_copy = *mc_model_checker->process().get_heap();
311 this->processStates[0].initHeapInformation(heap1, i1);
312 this->processStates[1].initHeapInformation(heap2, i2);
316 // TODO, have a robust way to find it in O(1)
318 mc_mem_region_t MC_get_heap_region(simgrid::mc::Snapshot* snapshot)
320 for (auto& region : snapshot->snapshot_regions)
321 if (region->region_type() == simgrid::mc::RegionType::Heap)
323 xbt_die("No heap region");
327 int mmalloc_compare_heap(
328 simgrid::mc::StateComparator& state, simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2)
330 simgrid::mc::Process* process = &mc_model_checker->process();
332 /* Start comparison */
333 size_t i1, i2, j1, j2, k;
334 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
335 int nb_diff1 = 0, nb_diff2 = 0;
337 int equal, res_compare = 0;
339 /* Check busy blocks */
343 malloc_info heapinfo_temp1, heapinfo_temp2;
344 malloc_info heapinfo_temp2b;
346 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
347 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
349 // This is the address of std_heap->heapinfo in the application process:
350 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
352 // This is in snapshot do not use them directly:
353 const malloc_info* heapinfos1 = snapshot1->read<malloc_info*>(
354 (std::uint64_t)heapinfo_address, simgrid::mc::ProcessIndexMissing);
355 const malloc_info* heapinfos2 = snapshot2->read<malloc_info*>(
356 (std::uint64_t)heapinfo_address, simgrid::mc::ProcessIndexMissing);
358 while (i1 < state.heaplimit) {
360 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(heap_region1, &heapinfo_temp1, &heapinfos1[i1], sizeof(malloc_info));
361 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2, &heapinfos2[i1], sizeof(malloc_info));
363 if (heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type == MMALLOC_TYPE_HEAPINFO) { /* Free block */
368 if (heapinfo1->type < 0) {
369 fprintf(stderr, "Unkown mmalloc block type.\n");
374 ((void *) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE +
375 (char *) state.std_heap_copy.heapbase));
377 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED) { /* Large block */
379 if (is_stack(addr_block1)) {
380 for (k = 0; k < heapinfo1->busy_block.size; k++)
381 state.equals_to1_(i1 + k, 0) = HeapArea(i1, -1);
382 for (k = 0; k < heapinfo2->busy_block.size; k++)
383 state.equals_to2_(i1 + k, 0) = HeapArea(i1, -1);
384 i1 += heapinfo1->busy_block.size;
388 if (state.equals_to1_(i1, 0).valid) {
397 /* Try first to associate to same block in the other heap */
398 if (heapinfo2->type == heapinfo1->type
399 && state.equals_to2_(i1, 0).valid == 0) {
400 addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE +
401 (char *) state.std_heap_copy.heapbase;
402 res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing,
403 addr_block1, addr_block2, snapshot1, snapshot2,
404 nullptr, nullptr, 0);
405 if (res_compare != 1) {
406 for (k = 1; k < heapinfo2->busy_block.size; k++)
407 state.equals_to2_(i1 + k, 0) = HeapArea(i1, -1);
408 for (k = 1; k < heapinfo1->busy_block.size; k++)
409 state.equals_to1_(i1 + k, 0) = HeapArea(i1, -1);
411 i1 += heapinfo1->busy_block.size;
415 while (i2 < state.heaplimit && !equal) {
417 addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE +
418 (char *) state.std_heap_copy.heapbase;
425 const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2b, &heapinfos2[i2], sizeof(malloc_info));
427 if (heapinfo2b->type != MMALLOC_TYPE_UNFRAGMENTED) {
432 if (state.equals_to2_(i2, 0).valid) {
437 res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing,
438 addr_block1, addr_block2, snapshot1, snapshot2,
439 nullptr, nullptr, 0);
441 if (res_compare != 1) {
442 for (k = 1; k < heapinfo2b->busy_block.size; k++)
443 state.equals_to2_(i2 + k, 0) = HeapArea(i1, -1);
444 for (k = 1; k < heapinfo1->busy_block.size; k++)
445 state.equals_to1_(i1 + k, 0) = HeapArea(i2, -1);
447 i1 += heapinfo1->busy_block.size;
455 XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1,
456 heapinfo1->busy_block.busy_size, addr_block1);
457 i1 = state.heaplimit + 1;
462 } else { /* Fragmented block */
464 for (j1 = 0; j1 < (size_t) (BLOCKSIZE >> heapinfo1->type); j1++) {
466 if (heapinfo1->busy_frag.frag_size[j1] == -1) /* Free fragment */
469 if (state.equals_to1_(i1, j1).valid)
473 (void *) ((char *) addr_block1 + (j1 << heapinfo1->type));
478 /* Try first to associate to same fragment in the other heap */
479 if (heapinfo2->type == heapinfo1->type
480 && !state.equals_to2_(i1, j1).valid) {
481 addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE +
482 (char *) state.std_heap_copy.heapbase;
484 (void *) ((char *) addr_block2 +
485 (j1 << heapinfo2->type));
486 res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing,
487 addr_frag1, addr_frag2, snapshot1, snapshot2,
488 nullptr, nullptr, 0);
489 if (res_compare != 1)
495 while (i2 < state.heaplimit && !equal) {
497 const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(
498 heap_region2, &heapinfo_temp2b, &heapinfos2[i2],
499 sizeof(malloc_info));
501 if (heapinfo2b->type == MMALLOC_TYPE_FREE || heapinfo2b->type == MMALLOC_TYPE_HEAPINFO) {
506 // We currently do not match fragments with unfragmented blocks (maybe we should).
507 if (heapinfo2b->type == MMALLOC_TYPE_UNFRAGMENTED) {
512 if (heapinfo2b->type < 0) {
513 fprintf(stderr, "Unkown mmalloc block type.\n");
517 for (j2 = 0; j2 < (size_t) (BLOCKSIZE >> heapinfo2b->type);
520 if (i2 == i1 && j2 == j1)
523 if (state.equals_to2_(i2, j2).valid)
526 addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE +
527 (char *) state.std_heap_copy.heapbase;
529 (void *) ((char *) addr_block2 +
530 (j2 << heapinfo2b->type));
532 res_compare = compare_heap_area(
533 state, simgrid::mc::ProcessIndexMissing,
534 addr_frag1, addr_frag2, snapshot2, snapshot2,
535 nullptr, nullptr, 0);
536 if (res_compare != 1) {
549 ("Block %zu, fragment %zu not found (size_used = %zd, address = %p)\n",
550 i1, j1, heapinfo1->busy_frag.frag_size[j1],
552 i2 = state.heaplimit + 1;
553 i1 = state.heaplimit + 1;
566 /* All blocks/fragments are equal to another block/fragment ? */
569 for(i = 1; i < state.heaplimit; i++) {
570 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
571 heap_region1, &heapinfo_temp1, &heapinfos1[i], sizeof(malloc_info));
573 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED
574 && i1 == state.heaplimit
575 && heapinfo1->busy_block.busy_size > 0
576 && !state.equals_to1_(i, 0).valid) {
577 XBT_DEBUG("Block %zu not found (size used = %zu)", i,
578 heapinfo1->busy_block.busy_size);
582 if (heapinfo1->type <= 0)
584 for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo1->type); j++)
585 if (i1 == state.heaplimit
586 && heapinfo1->busy_frag.frag_size[j] > 0
587 && !state.equals_to1_(i, j).valid) {
588 XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)",
589 i, j, heapinfo1->busy_frag.frag_size[j]);
594 if (i1 == state.heaplimit)
595 XBT_DEBUG("Number of blocks/fragments not found in heap1: %d", nb_diff1);
597 for (i=1; i < state.heaplimit; i++) {
598 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
599 heap_region2, &heapinfo_temp2, &heapinfos2[i], sizeof(malloc_info));
600 if (heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED
601 && i1 == state.heaplimit
602 && heapinfo2->busy_block.busy_size > 0
603 && !state.equals_to2_(i, 0).valid) {
604 XBT_DEBUG("Block %zu not found (size used = %zu)", i,
605 heapinfo2->busy_block.busy_size);
609 if (heapinfo2->type <= 0)
612 for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo2->type); j++)
613 if (i1 == state.heaplimit
614 && heapinfo2->busy_frag.frag_size[j] > 0
615 && !state.equals_to2_(i, j).valid) {
616 XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)",
617 i, j, heapinfo2->busy_frag.frag_size[j]);
623 if (i1 == state.heaplimit)
624 XBT_DEBUG("Number of blocks/fragments not found in heap2: %d", nb_diff2);
626 return nb_diff1 > 0 || nb_diff2 > 0;
632 * @param real_area1 Process address for state 1
633 * @param real_area2 Process address for state 2
634 * @param snapshot1 Snapshot of state 1
635 * @param snapshot2 Snapshot of state 2
638 * @param check_ignore
640 static int compare_heap_area_without_type(
641 simgrid::mc::StateComparator& state, int process_index,
642 const void *real_area1, const void *real_area2,
643 simgrid::mc::Snapshot* snapshot1,
644 simgrid::mc::Snapshot* snapshot2,
645 HeapLocationPairs* previous, int size,
648 simgrid::mc::Process* process = &mc_model_checker->process();
649 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
650 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
652 for (int i = 0; i < size; ) {
654 if (check_ignore > 0) {
655 ssize_t ignore1 = heap_comparison_ignore_size(
656 state.processStates[0].to_ignore, (char *) real_area1 + i);
658 ssize_t ignore2 = heap_comparison_ignore_size(
659 state.processStates[1].to_ignore, (char *) real_area2 + i);
660 if (ignore2 == ignore1) {
673 if (MC_snapshot_region_memcmp(((char *) real_area1) + i, heap_region1, ((char *) real_area2) + i, heap_region2, 1) != 0) {
675 int pointer_align = (i / sizeof(void *)) * sizeof(void *);
676 const void* addr_pointed1 = snapshot1->read(
677 remote((void**)((char *) real_area1 + pointer_align)), process_index);
678 const void* addr_pointed2 = snapshot2->read(
679 remote((void**)((char *) real_area2 + pointer_align)), process_index);
681 if (process->in_maestro_stack(remote(addr_pointed1))
682 && process->in_maestro_stack(remote(addr_pointed2))) {
683 i = pointer_align + sizeof(void *);
687 if (addr_pointed1 > state.std_heap_copy.heapbase
688 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
689 && addr_pointed2 > state.std_heap_copy.heapbase
690 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)) {
691 // Both addreses are in the heap:
692 int res_compare = compare_heap_area(state ,process_index,
693 addr_pointed1, addr_pointed2,
694 snapshot1, snapshot2, previous, nullptr, 0);
695 if (res_compare == 1)
697 i = pointer_align + sizeof(void *);
713 * @param real_area1 Process address for state 1
714 * @param real_area2 Process address for state 2
715 * @param snapshot1 Snapshot of state 1
716 * @param snapshot2 Snapshot of state 2
719 * @param area_size either a byte_size or an elements_count (?)
720 * @param check_ignore
721 * @param pointer_level
722 * @return 0 (same), 1 (different), -1 (unknown)
724 static int compare_heap_area_with_type(
725 simgrid::mc::StateComparator& state, int process_index,
726 const void *real_area1, const void *real_area2,
727 simgrid::mc::Snapshot* snapshot1,
728 simgrid::mc::Snapshot* snapshot2,
729 HeapLocationPairs* previous, simgrid::mc::Type* type,
730 int area_size, int check_ignore,
735 // HACK: This should not happen but in pratice, there are some
736 // DW_TAG_typedef without an associated DW_AT_type:
737 //<1><538832>: Abbrev Number: 111 (DW_TAG_typedef)
738 // <538833> DW_AT_name : (indirect string, offset: 0x2292f3): gregset_t
739 // <538837> DW_AT_decl_file : 98
740 // <538838> DW_AT_decl_line : 37
744 if (is_stack(real_area1) && is_stack(real_area2))
747 if (check_ignore > 0) {
748 ssize_t ignore1 = heap_comparison_ignore_size(
749 state.processStates[0].to_ignore, real_area1);
751 && heap_comparison_ignore_size(
752 state.processStates[1].to_ignore, real_area2) == ignore1)
756 simgrid::mc::Type *subtype, *subsubtype;
758 const void *addr_pointed1, *addr_pointed2;
760 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
761 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
763 switch (type->type) {
764 case DW_TAG_unspecified_type:
767 case DW_TAG_base_type:
768 if (!type->name.empty() && type->name == "char") { /* String, hence random (arbitrary ?) size */
769 if (real_area1 == real_area2)
772 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, area_size) != 0;
774 if (area_size != -1 && type->byte_size != area_size)
777 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
781 case DW_TAG_enumeration_type:
782 if (area_size != -1 && type->byte_size != area_size)
784 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
787 case DW_TAG_const_type:
788 case DW_TAG_volatile_type:
790 type = type->subtype;
793 case DW_TAG_array_type:
794 subtype = type->subtype;
795 switch (subtype->type) {
796 case DW_TAG_unspecified_type:
799 case DW_TAG_base_type:
800 case DW_TAG_enumeration_type:
801 case DW_TAG_pointer_type:
802 case DW_TAG_reference_type:
803 case DW_TAG_rvalue_reference_type:
804 case DW_TAG_structure_type:
805 case DW_TAG_class_type:
806 case DW_TAG_union_type:
807 if (subtype->full_type)
808 subtype = subtype->full_type;
809 elm_size = subtype->byte_size;
811 // TODO, just remove the type indirection?
812 case DW_TAG_const_type:
814 case DW_TAG_volatile_type:
815 subsubtype = subtype->subtype;
816 if (subsubtype->full_type)
817 subsubtype = subsubtype->full_type;
818 elm_size = subsubtype->byte_size;
824 for (int i = 0; i < type->element_count; i++) {
825 // TODO, add support for variable stride (DW_AT_byte_stride)
827 compare_heap_area_with_type(state, process_index,
828 (char *) real_area1 + (i * elm_size),
829 (char *) real_area2 + (i * elm_size),
830 snapshot1, snapshot2, previous,
831 type->subtype, subtype->byte_size,
832 check_ignore, pointer_level);
838 case DW_TAG_reference_type:
839 case DW_TAG_rvalue_reference_type:
840 case DW_TAG_pointer_type:
841 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
842 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
843 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
844 return (addr_pointed1 != addr_pointed2);
847 if (pointer_level <= 1) {
848 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
849 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
850 if (addr_pointed1 > state.std_heap_copy.heapbase
851 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
852 && addr_pointed2 > state.std_heap_copy.heapbase
853 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
854 return compare_heap_area(state, process_index,
855 addr_pointed1, addr_pointed2, snapshot1,
856 snapshot2, previous, type->subtype,
859 return (addr_pointed1 != addr_pointed2);
861 for (size_t i = 0; i < (area_size / sizeof(void *)); i++) {
862 addr_pointed1 = snapshot1->read(
863 remote((void**)((char*) real_area1 + i * sizeof(void *))),
865 addr_pointed2 = snapshot2->read(
866 remote((void**)((char*) real_area2 + i * sizeof(void *))),
868 if (addr_pointed1 > state.std_heap_copy.heapbase
869 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
870 && addr_pointed2 > state.std_heap_copy.heapbase
871 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
873 compare_heap_area(state, process_index,
874 addr_pointed1, addr_pointed2, snapshot1,
875 snapshot2, previous, type->subtype,
878 res = (addr_pointed1 != addr_pointed2);
884 case DW_TAG_structure_type:
885 case DW_TAG_class_type:
887 type = type->full_type;
888 if (area_size != -1 && type->byte_size != area_size) {
889 if (area_size <= type->byte_size || area_size % type->byte_size != 0)
891 for (size_t i = 0; i < (size_t)(area_size / type->byte_size); i++) {
892 int res = compare_heap_area_with_type(state, process_index,
893 (char *) real_area1 + i * type->byte_size,
894 (char *) real_area2 + i * type->byte_size,
895 snapshot1, snapshot2, previous, type, -1,
901 for(simgrid::mc::Member& member : type->members) {
902 // TODO, optimize this? (for the offset case)
903 void *real_member1 = simgrid::dwarf::resolve_member(
904 real_area1, type, &member, (simgrid::mc::AddressSpace*) snapshot1, process_index);
905 void *real_member2 = simgrid::dwarf::resolve_member(
906 real_area2, type, &member, (simgrid::mc::AddressSpace*) snapshot2, process_index);
907 int res = compare_heap_area_with_type(
908 state, process_index, real_member1, real_member2,
909 snapshot1, snapshot2,
910 previous, member.type, -1,
918 case DW_TAG_union_type:
919 return compare_heap_area_without_type(state, process_index, real_area1, real_area2,
920 snapshot1, snapshot2, previous,
921 type->byte_size, check_ignore);
928 xbt_die("Unreachable");
931 /** Infer the type of a part of the block from the type of the block
933 * TODO, handle DW_TAG_array_type as well as arrays of the object ((*p)[5], p[5])
935 * TODO, handle subfields ((*p).bar.foo, (*p)[5].bar…)
937 * @param type_id DWARF type ID of the root address
939 * @return DWARF type ID for given offset
941 static simgrid::mc::Type* get_offset_type(void *real_base_address, simgrid::mc::Type* type,
942 int offset, int area_size,
943 simgrid::mc::Snapshot* snapshot, int process_index)
946 // Beginning of the block, the infered variable type if the type of the block:
950 switch (type->type) {
952 case DW_TAG_structure_type:
953 case DW_TAG_class_type:
955 type = type->full_type;
956 if (area_size != -1 && type->byte_size != area_size) {
957 if (area_size > type->byte_size && area_size % type->byte_size == 0)
963 for(simgrid::mc::Member& member : type->members) {
964 if (member.has_offset_location()) {
965 // We have the offset, use it directly (shortcut):
966 if (member.offset() == offset)
969 void *real_member = simgrid::dwarf::resolve_member(
970 real_base_address, type, &member, snapshot, process_index);
971 if ((char*) real_member - (char *) real_base_address == offset)
978 /* FIXME: other cases ? */
986 * @param area1 Process address for state 1
987 * @param area2 Process address for state 2
988 * @param snapshot1 Snapshot of state 1
989 * @param snapshot2 Snapshot of state 2
990 * @param previous Pairs of blocks already compared on the current path (or nullptr)
991 * @param type_id Type of variable
992 * @param pointer_level
993 * @return 0 (same), 1 (different), -1
996 int compare_heap_area(simgrid::mc::StateComparator& state, int process_index,
997 const void *area1, const void *area2,
998 simgrid::mc::Snapshot* snapshot1,
999 simgrid::mc::Snapshot* snapshot2,
1000 HeapLocationPairs* previous,
1001 simgrid::mc::Type* type, int pointer_level)
1003 simgrid::mc::Process* process = &mc_model_checker->process();
1006 ssize_t block1, frag1, block2, frag2;
1008 int check_ignore = 0;
1010 void *real_addr_block1, *real_addr_block2, *real_addr_frag1, *real_addr_frag2;
1012 int offset1 = 0, offset2 = 0;
1013 int new_size1 = -1, new_size2 = -1;
1014 simgrid::mc::Type *new_type1 = nullptr, *new_type2 = nullptr;
1016 bool match_pairs = false;
1018 // This is the address of std_heap->heapinfo in the application process:
1019 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
1021 const malloc_info* heapinfos1 = snapshot1->read(
1022 remote((const malloc_info**)heapinfo_address), process_index);
1023 const malloc_info* heapinfos2 = snapshot2->read(
1024 remote((const malloc_info**)heapinfo_address), process_index);
1026 malloc_info heapinfo_temp1, heapinfo_temp2;
1028 simgrid::mc::HeapLocationPairs current;
1029 if (previous == nullptr) {
1030 previous = ¤t;
1034 // Get block number:
1037 (char *) state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
1040 (char *) state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
1042 // If either block is a stack block:
1043 if (is_block_stack((int) block1) && is_block_stack((int) block2)) {
1044 previous->insert(simgrid::mc::makeHeapLocationPair(
1045 block1, -1, block2, -1));
1047 state.match_equals(previous);
1051 // If either block is not in the expected area of memory:
1052 if (((char *) area1 < (char *) state.std_heap_copy.heapbase)
1053 || (block1 > (ssize_t) state.processStates[0].heapsize) || (block1 < 1)
1054 || ((char *) area2 < (char *) state.std_heap_copy.heapbase)
1055 || (block2 > (ssize_t) state.processStates[1].heapsize) || (block2 < 1)) {
1059 // Process address of the block:
1060 real_addr_block1 = (ADDR2UINT(block1) - 1) * BLOCKSIZE +
1061 (char *) state.std_heap_copy.heapbase;
1062 real_addr_block2 = (ADDR2UINT(block2) - 1) * BLOCKSIZE +
1063 (char *) state.std_heap_copy.heapbase;
1067 if (type->full_type)
1068 type = type->full_type;
1070 // This assume that for "boring" types (volatile ...) byte_size is absent:
1071 while (type->byte_size == 0 && type->subtype != nullptr)
1072 type = type->subtype;
1075 if (type->type == DW_TAG_pointer_type
1076 || (type->type == DW_TAG_base_type && !type->name.empty()
1077 && type->name == "char"))
1080 type_size = type->byte_size;
1084 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
1085 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
1087 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
1088 heap_region1, &heapinfo_temp1, &heapinfos1[block1], sizeof(malloc_info));
1089 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
1090 heap_region2, &heapinfo_temp2, &heapinfos2[block2], sizeof(malloc_info));
1092 if ((heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type==MMALLOC_TYPE_HEAPINFO)
1093 && (heapinfo2->type == MMALLOC_TYPE_FREE || heapinfo2->type ==MMALLOC_TYPE_HEAPINFO)) {
1096 state.match_equals(previous);
1100 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED
1101 && heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
1102 /* Complete block */
1104 // TODO, lookup variable type from block type as done for fragmented blocks
1106 offset1 = (char *) area1 - (char *) real_addr_block1;
1107 offset2 = (char *) area2 - (char *) real_addr_block2;
1109 if (state.equals_to1_(block1, 0).valid
1110 && state.equals_to2_(block2, 0).valid
1111 && state.blocksEqual(block1, block2)) {
1113 state.match_equals(previous);
1117 if (type_size != -1) {
1118 if (type_size != (ssize_t) heapinfo1->busy_block.busy_size
1119 && type_size != (ssize_t) heapinfo2->busy_block.busy_size
1120 && (type->name.empty() || type->name == "struct s_smx_context")) {
1122 state.match_equals(previous);
1127 if (heapinfo1->busy_block.size != heapinfo2->busy_block.size)
1129 if (heapinfo1->busy_block.busy_size != heapinfo2->busy_block.busy_size)
1132 if (!previous->insert(simgrid::mc::makeHeapLocationPair(
1133 block1, -1, block2, -1)).second) {
1135 state.match_equals(previous);
1139 size = heapinfo1->busy_block.busy_size;
1141 // Remember (basic) type inference.
1142 // The current data structure only allows us to do this for the whole block.
1143 if (type != nullptr && area1 == real_addr_block1)
1144 state.types1_(block1, 0) = type;
1145 if (type != nullptr && area2 == real_addr_block2)
1146 state.types2_(block2, 0) = type;
1150 state.match_equals(previous);
1157 if (heapinfo1->busy_block.ignore > 0
1158 && heapinfo2->busy_block.ignore == heapinfo1->busy_block.ignore)
1159 check_ignore = heapinfo1->busy_block.ignore;
1161 } else if ((heapinfo1->type > 0) && (heapinfo2->type > 0)) { /* Fragmented block */
1165 ((uintptr_t) (ADDR2UINT(area1) % (BLOCKSIZE))) >> heapinfo1->type;
1167 ((uintptr_t) (ADDR2UINT(area2) % (BLOCKSIZE))) >> heapinfo2->type;
1169 // Process address of the fragment:
1171 (void *) ((char *) real_addr_block1 +
1172 (frag1 << heapinfo1->type));
1174 (void *) ((char *) real_addr_block2 +
1175 (frag2 << heapinfo2->type));
1177 // Check the size of the fragments against the size of the type:
1178 if (type_size != -1) {
1179 if (heapinfo1->busy_frag.frag_size[frag1] == -1
1180 || heapinfo2->busy_frag.frag_size[frag2] == -1) {
1182 state.match_equals(previous);
1186 if (type_size != heapinfo1->busy_frag.frag_size[frag1]
1187 || type_size != heapinfo2->busy_frag.frag_size[frag2]) {
1189 state.match_equals(previous);
1194 // Check if the blocks are already matched together:
1195 if (state.equals_to1_(block1, frag1).valid
1196 && state.equals_to2_(block2, frag2).valid) {
1197 if (offset1==offset2 && state.fragmentsEqual(block1, frag1, block2, frag2)) {
1199 state.match_equals(previous);
1203 // Compare the size of both fragments:
1204 if (heapinfo1->busy_frag.frag_size[frag1] !=
1205 heapinfo2->busy_frag.frag_size[frag2]) {
1206 if (type_size == -1) {
1208 state.match_equals(previous);
1214 // Size of the fragment:
1215 size = heapinfo1->busy_frag.frag_size[frag1];
1217 // Remember (basic) type inference.
1218 // The current data structure only allows us to do this for the whole fragment.
1219 if (type != nullptr && area1 == real_addr_frag1)
1220 state.types1_(block1, frag1) = type;
1221 if (type != nullptr && area2 == real_addr_frag2)
1222 state.types2_(block2, frag2) = type;
1224 // The type of the variable is already known:
1229 // Type inference from the block type.
1230 else if (state.types1_(block1, frag1) != nullptr
1231 || state.types2_(block2, frag2) != nullptr) {
1233 offset1 = (char *) area1 - (char *) real_addr_frag1;
1234 offset2 = (char *) area2 - (char *) real_addr_frag2;
1236 if (state.types1_(block1, frag1) != nullptr
1237 && state.types2_(block2, frag2) != nullptr) {
1239 get_offset_type(real_addr_frag1, state.types1_(block1, frag1),
1240 offset1, size, snapshot1, process_index);
1242 get_offset_type(real_addr_frag2, state.types2_(block2, frag2),
1243 offset1, size, snapshot2, process_index);
1244 } else if (state.types1_(block1, frag1) != nullptr) {
1246 get_offset_type(real_addr_frag1, state.types1_(block1, frag1),
1247 offset1, size, snapshot1, process_index);
1249 get_offset_type(real_addr_frag2, state.types1_(block1, frag1),
1250 offset2, size, snapshot2, process_index);
1251 } else if (state.types2_(block2, frag2) != nullptr) {
1253 get_offset_type(real_addr_frag1, state.types2_(block2, frag2),
1254 offset1, size, snapshot1, process_index);
1256 get_offset_type(real_addr_frag2, state.types2_(block2, frag2),
1257 offset2, size, snapshot2, process_index);
1260 state.match_equals(previous);
1264 if (new_type1 != nullptr && new_type2 != nullptr && new_type1 != new_type2) {
1267 while (type->byte_size == 0 && type->subtype != nullptr)
1268 type = type->subtype;
1269 new_size1 = type->byte_size;
1272 while (type->byte_size == 0 && type->subtype != nullptr)
1273 type = type->subtype;
1274 new_size2 = type->byte_size;
1278 state.match_equals(previous);
1283 if (new_size1 > 0 && new_size1 == new_size2) {
1288 if (offset1 == 0 && offset2 == 0
1289 && !previous->insert(simgrid::mc::makeHeapLocationPair(
1290 block1, frag1, block2, frag2)).second) {
1292 state.match_equals(previous);
1298 state.match_equals(previous);
1302 if ((heapinfo1->busy_frag.ignore[frag1] > 0)
1303 && (heapinfo2->busy_frag.ignore[frag2] ==
1304 heapinfo1->busy_frag.ignore[frag1]))
1305 check_ignore = heapinfo1->busy_frag.ignore[frag1];
1311 /* Start comparison */
1314 compare_heap_area_with_type(state, process_index, area1, area2, snapshot1, snapshot2,
1315 previous, type, size, check_ignore,
1319 compare_heap_area_without_type(state, process_index, area1, area2, snapshot1, snapshot2,
1320 previous, size, check_ignore);
1322 if (res_compare == 1)
1326 state.match_equals(previous);
1333 /************************** Snapshot comparison *******************************/
1334 /******************************************************************************/
1336 static int compare_areas_with_type(simgrid::mc::StateComparator& state,
1338 void* real_area1, simgrid::mc::Snapshot* snapshot1, mc_mem_region_t region1,
1339 void* real_area2, simgrid::mc::Snapshot* snapshot2, mc_mem_region_t region2,
1340 simgrid::mc::Type* type, int pointer_level)
1342 simgrid::mc::Process* process = &mc_model_checker->process();
1344 simgrid::mc::Type* subtype;
1345 simgrid::mc::Type* subsubtype;
1346 int elm_size, i, res;
1349 switch (type->type) {
1350 case DW_TAG_unspecified_type:
1353 case DW_TAG_base_type:
1354 case DW_TAG_enumeration_type:
1355 case DW_TAG_union_type:
1357 return MC_snapshot_region_memcmp(
1358 real_area1, region1, real_area2, region2,
1359 type->byte_size) != 0;
1361 case DW_TAG_typedef:
1362 case DW_TAG_volatile_type:
1363 case DW_TAG_const_type:
1365 type = type->subtype;
1367 case DW_TAG_array_type:
1368 subtype = type->subtype;
1369 switch (subtype->type) {
1370 case DW_TAG_unspecified_type:
1373 case DW_TAG_base_type:
1374 case DW_TAG_enumeration_type:
1375 case DW_TAG_pointer_type:
1376 case DW_TAG_reference_type:
1377 case DW_TAG_rvalue_reference_type:
1378 case DW_TAG_structure_type:
1379 case DW_TAG_class_type:
1380 case DW_TAG_union_type:
1381 if (subtype->full_type)
1382 subtype = subtype->full_type;
1383 elm_size = subtype->byte_size;
1385 case DW_TAG_const_type:
1386 case DW_TAG_typedef:
1387 case DW_TAG_volatile_type:
1388 subsubtype = subtype->subtype;
1389 if (subsubtype->full_type)
1390 subsubtype = subsubtype->full_type;
1391 elm_size = subsubtype->byte_size;
1397 for (i = 0; i < type->element_count; i++) {
1398 size_t off = i * elm_size;
1399 res = compare_areas_with_type(state, process_index,
1400 (char*) real_area1 + off, snapshot1, region1,
1401 (char*) real_area2 + off, snapshot2, region2,
1402 type->subtype, pointer_level);
1407 case DW_TAG_pointer_type:
1408 case DW_TAG_reference_type:
1409 case DW_TAG_rvalue_reference_type:
1411 void* addr_pointed1 = MC_region_read_pointer(region1, real_area1);
1412 void* addr_pointed2 = MC_region_read_pointer(region2, real_area2);
1414 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type)
1415 return (addr_pointed1 != addr_pointed2);
1416 if (addr_pointed1 == nullptr && addr_pointed2 == nullptr)
1418 if (addr_pointed1 == nullptr || addr_pointed2 == nullptr)
1420 if (!state.compared_pointers.insert(
1421 std::make_pair(addr_pointed1, addr_pointed2)).second)
1426 // Some cases are not handled here:
1427 // * the pointers lead to different areas (one to the heap, the other to the RW segment ...);
1428 // * a pointer leads to the read-only segment of the current object;
1429 // * a pointer lead to a different ELF object.
1431 if (addr_pointed1 > process->heap_address
1432 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)) {
1434 (addr_pointed2 > process->heap_address
1435 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)))
1437 // The pointers are both in the heap:
1438 return simgrid::mc::compare_heap_area(state,
1439 process_index, addr_pointed1, addr_pointed2, snapshot1,
1440 snapshot2, nullptr, type->subtype, pointer_level);
1443 // The pointers are both in the current object R/W segment:
1444 else if (region1->contain(simgrid::mc::remote(addr_pointed1))) {
1445 if (!region2->contain(simgrid::mc::remote(addr_pointed2)))
1448 return (addr_pointed1 != addr_pointed2);
1450 return compare_areas_with_type(state, process_index,
1451 addr_pointed1, snapshot1, region1,
1452 addr_pointed2, snapshot2, region2,
1453 type->subtype, pointer_level);
1456 // TODO, We do not handle very well the case where
1457 // it belongs to a different (non-heap) region from the current one.
1460 return (addr_pointed1 != addr_pointed2);
1464 case DW_TAG_structure_type:
1465 case DW_TAG_class_type:
1466 for(simgrid::mc::Member& member : type->members) {
1467 void *member1 = simgrid::dwarf::resolve_member(
1468 real_area1, type, &member, snapshot1, process_index);
1469 void *member2 = simgrid::dwarf::resolve_member(
1470 real_area2, type, &member, snapshot2, process_index);
1471 mc_mem_region_t subregion1 = mc_get_region_hinted(member1, snapshot1, process_index, region1);
1472 mc_mem_region_t subregion2 = mc_get_region_hinted(member2, snapshot2, process_index, region2);
1474 compare_areas_with_type(state, process_index,
1475 member1, snapshot1, subregion1,
1476 member2, snapshot2, subregion2,
1477 member.type, pointer_level);
1482 case DW_TAG_subroutine_type:
1486 XBT_VERB("Unknown case: %d", type->type);
1493 static int compare_global_variables(
1494 simgrid::mc::StateComparator& state,
1495 simgrid::mc::ObjectInformation* object_info,
1497 mc_mem_region_t r1, mc_mem_region_t r2,
1498 simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2)
1500 xbt_assert(r1 && r2, "Missing region.");
1503 if (r1->storage_type() == simgrid::mc::StorageType::Privatized) {
1504 xbt_assert(process_index >= 0);
1505 if (r2->storage_type() != simgrid::mc::StorageType::Privatized)
1508 size_t process_count = MC_smpi_process_count();
1509 xbt_assert(process_count == r1->privatized_data().size()
1510 && process_count == r2->privatized_data().size());
1512 // Compare the global variables separately for each simulates process:
1513 for (size_t process_index = 0; process_index < process_count; process_index++) {
1514 int is_diff = compare_global_variables(state,
1515 object_info, process_index,
1516 &r1->privatized_data()[process_index],
1517 &r2->privatized_data()[process_index],
1518 snapshot1, snapshot2);
1519 if (is_diff) return 1;
1524 xbt_assert(r1->storage_type() != simgrid::mc::StorageType::Privatized);
1526 xbt_assert(r2->storage_type() != simgrid::mc::StorageType::Privatized);
1528 std::vector<simgrid::mc::Variable>& variables = object_info->global_variables;
1530 for (simgrid::mc::Variable& current_var : variables) {
1532 // If the variable is not in this object, skip it:
1533 // We do not expect to find a pointer to something which is not reachable
1534 // by the global variables.
1535 if ((char *) current_var.address < (char *) object_info->start_rw
1536 || (char *) current_var.address > (char *) object_info->end_rw)
1539 simgrid::mc::Type* bvariable_type = current_var.type;
1540 int res = compare_areas_with_type(state, process_index,
1541 (char *) current_var.address, snapshot1, r1,
1542 (char *) current_var.address, snapshot2, r2,
1545 XBT_VERB("Global variable %s (%p) is different between snapshots",
1546 current_var.name.c_str(),
1547 (char *) current_var.address);
1557 static int compare_local_variables(simgrid::mc::StateComparator& state,
1559 simgrid::mc::Snapshot* snapshot1,
1560 simgrid::mc::Snapshot* snapshot2,
1561 mc_snapshot_stack_t stack1,
1562 mc_snapshot_stack_t stack2)
1564 if (stack1->local_variables.size() != stack2->local_variables.size()) {
1565 XBT_VERB("Different number of local variables");
1569 unsigned int cursor = 0;
1570 local_variable_t current_var1, current_var2;
1572 while (cursor < stack1->local_variables.size()) {
1573 current_var1 = &stack1->local_variables[cursor];
1574 current_var2 = &stack1->local_variables[cursor];
1575 if (current_var1->name != current_var2->name
1576 || current_var1->subprogram != current_var2->subprogram
1577 || current_var1->ip != current_var2->ip) {
1578 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1580 ("Different name of variable (%s - %s) "
1581 "or frame (%s - %s) or ip (%lu - %lu)",
1582 current_var1->name.c_str(),
1583 current_var2->name.c_str(),
1584 current_var1->subprogram->name.c_str(),
1585 current_var2->subprogram->name.c_str(),
1586 current_var1->ip, current_var2->ip);
1589 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1591 simgrid::mc::Type* subtype = current_var1->type;
1593 compare_areas_with_type(state, process_index,
1594 current_var1->address, snapshot1, mc_get_snapshot_region(current_var1->address, snapshot1, process_index),
1595 current_var2->address, snapshot2, mc_get_snapshot_region(current_var2->address, snapshot2, process_index),
1599 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1601 ("Local variable %s (%p - %p) in frame %s "
1602 "is different between snapshots",
1603 current_var1->name.c_str(),
1604 current_var1->address,
1605 current_var2->address,
1606 current_var1->subprogram->name.c_str());
1617 static std::unique_ptr<simgrid::mc::StateComparator> state_comparator;
1619 int snapshot_compare(int num1, simgrid::mc::Snapshot* s1, int num2, simgrid::mc::Snapshot* s2)
1621 // TODO, make this a field of ModelChecker or something similar
1623 if (state_comparator == nullptr)
1624 state_comparator = std::unique_ptr<StateComparator>(new StateComparator());
1626 state_comparator->clear();
1628 simgrid::mc::Process* process = &mc_model_checker->process();
1633 int hash_result = 0;
1635 hash_result = (s1->hash != s2->hash);
1637 XBT_VERB("(%d - %d) Different hash: 0x%" PRIx64 "--0x%" PRIx64, num1, num2, s1->hash, s2->hash);
1642 XBT_VERB("(%d - %d) Same hash: 0x%" PRIx64, num1, num2, s1->hash);
1645 /* Compare enabled processes */
1646 if (s1->enabled_processes != s2->enabled_processes) {
1647 XBT_VERB("(%d - %d) Different enabled processes", num1, num2);
1651 unsigned long i = 0;
1652 size_t size_used1, size_used2;
1655 /* Compare size of stacks */
1656 while (i < s1->stacks.size()) {
1657 size_used1 = s1->stack_sizes[i];
1658 size_used2 = s2->stack_sizes[i];
1659 if (size_used1 != size_used2) {
1661 XBT_DEBUG("(%d - %d) Different size used in stacks: %zu - %zu", num1, num2, size_used1, size_used2);
1666 XBT_VERB("(%d - %d) Different size used in stacks: %zu - %zu", num1, num2, size_used1, size_used2);
1674 /* Init heap information used in heap comparison algorithm */
1675 xbt_mheap_t heap1 = (xbt_mheap_t)s1->read_bytes(
1676 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1677 remote(process->heap_address),
1678 simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1679 xbt_mheap_t heap2 = (xbt_mheap_t)s2->read_bytes(
1680 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1681 remote(process->heap_address),
1682 simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1683 res_init = state_comparator->initHeapInformation(
1684 heap1, heap2, &s1->to_ignore, &s2->to_ignore);
1686 if (res_init == -1) {
1688 XBT_DEBUG("(%d - %d) Different heap information", num1, num2);
1692 XBT_VERB("(%d - %d) Different heap information", num1, num2);
1699 /* Stacks comparison */
1704 for (unsigned int cursor = 0; cursor < s1->stacks.size(); cursor++) {
1705 mc_snapshot_stack_t stack1 = &s1->stacks[cursor];
1706 mc_snapshot_stack_t stack2 = &s2->stacks[cursor];
1708 if (stack1->process_index != stack2->process_index) {
1710 XBT_DEBUG("(%d - %d) Stacks with different process index (%i vs %i)", num1, num2,
1711 stack1->process_index, stack2->process_index);
1713 else diff_local = compare_local_variables(*state_comparator,
1714 stack1->process_index, s1, s2, stack1, stack2);
1715 if (diff_local > 0) {
1717 XBT_DEBUG("(%d - %d) Different local variables between stacks %d", num1,
1724 XBT_VERB("(%d - %d) Different local variables between stacks %d", num1,
1733 size_t regions_count = s1->snapshot_regions.size();
1734 // TODO, raise a difference instead?
1735 xbt_assert(regions_count == s2->snapshot_regions.size());
1737 for (size_t k = 0; k != regions_count; ++k) {
1738 mc_mem_region_t region1 = s1->snapshot_regions[k].get();
1739 mc_mem_region_t region2 = s2->snapshot_regions[k].get();
1742 if (region1->region_type() != simgrid::mc::RegionType::Data)
1745 xbt_assert(region1->region_type() == region2->region_type());
1746 xbt_assert(region1->object_info() == region2->object_info());
1747 xbt_assert(region1->object_info());
1749 std::string const& name = region1->object_info()->file_name;
1751 /* Compare global variables */
1753 compare_global_variables(*state_comparator,
1754 region1->object_info(), simgrid::mc::ProcessIndexDisabled,
1755 region1, region2, s1, s2);
1759 XBT_DEBUG("(%d - %d) Different global variables in %s",
1760 num1, num2, name.c_str());
1764 XBT_VERB("(%d - %d) Different global variables in %s",
1765 num1, num2, name.c_str());
1774 if (simgrid::mc::mmalloc_compare_heap(*state_comparator, s1, s2) > 0) {
1777 XBT_DEBUG("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
1782 XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
1789 if (errors || hash_result)
1790 XBT_VERB("(%d - %d) Difference found", num1, num2);
1792 XBT_VERB("(%d - %d) No difference found", num1, num2);
1795 #if defined(MC_DEBUG) && defined(MC_VERBOSE)
1797 // * false positive SHOULD be avoided.
1798 // * There MUST not be any false negative.
1800 XBT_VERB("(%d - %d) State equality hash test is %s %s", num1, num2,
1801 (hash_result != 0) == (errors != 0) ? "true" : "false",
1802 !hash_result ? "positive" : "negative");
1806 return errors > 0 || hash_result;