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 state State used
198 * @param b1 Block of state 1
199 * @param b2 Block of state 2
200 * @return if the blocks are known to be matching
202 bool blocksEqual(int b1, int b2) const
204 return this->equals_to1_(b1, 0).block == b2
205 && this->equals_to2_(b2, 0).block == b1;
208 /** Check whether two fragments are known to be matching
210 * @param state State used
211 * @param b1 Block of state 1
212 * @param f1 Fragment of state 1
213 * @param b2 Block of state 2
214 * @param f2 Fragment of state 2
215 * @return if the fragments are known to be matching
217 int fragmentsEqual(int b1, int f1, int b2, int f2) const
219 return this->equals_to1_(b1, f1).block == b2
220 && this->equals_to1_(b1, f1).fragment == f2
221 && this->equals_to2_(b2, f2).block == b1
222 && this->equals_to2_(b2, f2).fragment == f1;
225 void match_equals(HeapLocationPairs* list);
231 /************************************************************************************/
233 static ssize_t heap_comparison_ignore_size(
234 std::vector<simgrid::mc::IgnoredHeapRegion>* ignore_list,
238 int end = ignore_list->size() - 1;
240 while (start <= end) {
241 unsigned int cursor = (start + end) / 2;
242 simgrid::mc::IgnoredHeapRegion const& region = (*ignore_list)[cursor];
243 if (region.address == address)
245 if (region.address < address)
247 if (region.address > address)
254 static bool is_stack(const void *address)
256 for (auto const& stack : mc_model_checker->process().stack_areas())
257 if (address == stack.address)
262 // TODO, this should depend on the snapshot?
263 static bool is_block_stack(int block)
265 for (auto const& stack : mc_model_checker->process().stack_areas())
266 if (block == stack.block)
274 void StateComparator::match_equals(HeapLocationPairs* list)
276 for (auto const& pair : *list) {
277 if (pair[0].fragment != -1) {
278 this->equals_to1_(pair[0].block, pair[0].fragment) =
279 simgrid::mc::HeapArea(pair[1].block, pair[1].fragment);
280 this->equals_to2_(pair[1].block, pair[1].fragment) =
281 simgrid::mc::HeapArea(pair[0].block, pair[0].fragment);
283 this->equals_to1_(pair[0].block, 0) =
284 simgrid::mc::HeapArea(pair[1].block, pair[1].fragment);
285 this->equals_to2_(pair[1].block, 0) =
286 simgrid::mc::HeapArea(pair[0].block, pair[0].fragment);
291 void ProcessComparisonState::initHeapInformation(xbt_mheap_t heap,
292 std::vector<simgrid::mc::IgnoredHeapRegion>* i)
294 auto heaplimit = ((struct mdesc *) heap)->heaplimit;
295 this->heapsize = ((struct mdesc *) heap)->heapsize;
297 this->equals_to.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, HeapArea());
298 this->types.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, nullptr);
301 int StateComparator::initHeapInformation(xbt_mheap_t heap1, xbt_mheap_t heap2,
302 std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
303 std::vector<simgrid::mc::IgnoredHeapRegion>* i2)
305 if ((((struct mdesc *) heap1)->heaplimit !=
306 ((struct mdesc *) heap2)->heaplimit)
308 ((((struct mdesc *) heap1)->heapsize !=
309 ((struct mdesc *) heap2)->heapsize)))
311 this->heaplimit = ((struct mdesc *) heap1)->heaplimit;
312 this->std_heap_copy = *mc_model_checker->process().get_heap();
313 this->processStates[0].initHeapInformation(heap1, i1);
314 this->processStates[1].initHeapInformation(heap2, i2);
318 // TODO, have a robust way to find it in O(1)
320 mc_mem_region_t MC_get_heap_region(simgrid::mc::Snapshot* snapshot)
322 for (auto& region : snapshot->snapshot_regions)
323 if (region->region_type() == simgrid::mc::RegionType::Heap)
325 xbt_die("No heap region");
329 int mmalloc_compare_heap(
330 simgrid::mc::StateComparator& state, simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2)
332 simgrid::mc::Process* process = &mc_model_checker->process();
334 /* Start comparison */
335 size_t i1, i2, j1, j2, k;
336 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
337 int nb_diff1 = 0, nb_diff2 = 0;
339 int equal, res_compare = 0;
341 /* Check busy blocks */
345 malloc_info heapinfo_temp1, heapinfo_temp2;
346 malloc_info heapinfo_temp2b;
348 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
349 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
351 // This is the address of std_heap->heapinfo in the application process:
352 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
354 // This is in snapshot do not use them directly:
355 const malloc_info* heapinfos1 = snapshot1->read<malloc_info*>(
356 (std::uint64_t)heapinfo_address, simgrid::mc::ProcessIndexMissing);
357 const malloc_info* heapinfos2 = snapshot2->read<malloc_info*>(
358 (std::uint64_t)heapinfo_address, simgrid::mc::ProcessIndexMissing);
360 while (i1 < state.heaplimit) {
362 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(heap_region1, &heapinfo_temp1, &heapinfos1[i1], sizeof(malloc_info));
363 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2, &heapinfos2[i1], sizeof(malloc_info));
365 if (heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type == MMALLOC_TYPE_HEAPINFO) { /* Free block */
370 if (heapinfo1->type < 0) {
371 fprintf(stderr, "Unkown mmalloc block type.\n");
376 ((void *) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE +
377 (char *) state.std_heap_copy.heapbase));
379 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED) { /* Large block */
381 if (is_stack(addr_block1)) {
382 for (k = 0; k < heapinfo1->busy_block.size; k++)
383 state.equals_to1_(i1 + k, 0) = HeapArea(i1, -1);
384 for (k = 0; k < heapinfo2->busy_block.size; k++)
385 state.equals_to2_(i1 + k, 0) = HeapArea(i1, -1);
386 i1 += heapinfo1->busy_block.size;
390 if (state.equals_to1_(i1, 0).valid) {
399 /* Try first to associate to same block in the other heap */
400 if (heapinfo2->type == heapinfo1->type
401 && state.equals_to2_(i1, 0).valid == 0) {
402 addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE +
403 (char *) state.std_heap_copy.heapbase;
404 res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing,
405 addr_block1, addr_block2, snapshot1, snapshot2,
406 nullptr, nullptr, 0);
407 if (res_compare != 1) {
408 for (k = 1; k < heapinfo2->busy_block.size; k++)
409 state.equals_to2_(i1 + k, 0) = HeapArea(i1, -1);
410 for (k = 1; k < heapinfo1->busy_block.size; k++)
411 state.equals_to1_(i1 + k, 0) = HeapArea(i1, -1);
413 i1 += heapinfo1->busy_block.size;
417 while (i2 < state.heaplimit && !equal) {
419 addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE +
420 (char *) state.std_heap_copy.heapbase;
427 const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2b, &heapinfos2[i2], sizeof(malloc_info));
429 if (heapinfo2b->type != MMALLOC_TYPE_UNFRAGMENTED) {
434 if (state.equals_to2_(i2, 0).valid) {
439 res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing,
440 addr_block1, addr_block2, snapshot1, snapshot2,
441 nullptr, nullptr, 0);
443 if (res_compare != 1) {
444 for (k = 1; k < heapinfo2b->busy_block.size; k++)
445 state.equals_to2_(i2 + k, 0) = HeapArea(i1, -1);
446 for (k = 1; k < heapinfo1->busy_block.size; k++)
447 state.equals_to1_(i1 + k, 0) = HeapArea(i2, -1);
449 i1 += heapinfo1->busy_block.size;
457 XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1,
458 heapinfo1->busy_block.busy_size, addr_block1);
459 i1 = state.heaplimit + 1;
464 } else { /* Fragmented block */
466 for (j1 = 0; j1 < (size_t) (BLOCKSIZE >> heapinfo1->type); j1++) {
468 if (heapinfo1->busy_frag.frag_size[j1] == -1) /* Free fragment */
471 if (state.equals_to1_(i1, j1).valid)
475 (void *) ((char *) addr_block1 + (j1 << heapinfo1->type));
480 /* Try first to associate to same fragment in the other heap */
481 if (heapinfo2->type == heapinfo1->type
482 && !state.equals_to2_(i1, j1).valid) {
483 addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE +
484 (char *) state.std_heap_copy.heapbase;
486 (void *) ((char *) addr_block2 +
487 (j1 << heapinfo2->type));
488 res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing,
489 addr_frag1, addr_frag2, snapshot1, snapshot2,
490 nullptr, nullptr, 0);
491 if (res_compare != 1)
497 while (i2 < state.heaplimit && !equal) {
499 const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(
500 heap_region2, &heapinfo_temp2b, &heapinfos2[i2],
501 sizeof(malloc_info));
503 if (heapinfo2b->type == MMALLOC_TYPE_FREE || heapinfo2b->type == MMALLOC_TYPE_HEAPINFO) {
508 // We currently do not match fragments with unfragmented blocks (maybe we should).
509 if (heapinfo2b->type == MMALLOC_TYPE_UNFRAGMENTED) {
514 if (heapinfo2b->type < 0) {
515 fprintf(stderr, "Unkown mmalloc block type.\n");
519 for (j2 = 0; j2 < (size_t) (BLOCKSIZE >> heapinfo2b->type);
522 if (i2 == i1 && j2 == j1)
525 if (state.equals_to2_(i2, j2).valid)
528 addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE +
529 (char *) state.std_heap_copy.heapbase;
531 (void *) ((char *) addr_block2 +
532 (j2 << heapinfo2b->type));
534 res_compare = compare_heap_area(
535 state, simgrid::mc::ProcessIndexMissing,
536 addr_frag1, addr_frag2, snapshot2, snapshot2,
537 nullptr, nullptr, 0);
538 if (res_compare != 1) {
551 ("Block %zu, fragment %zu not found (size_used = %zd, address = %p)\n",
552 i1, j1, heapinfo1->busy_frag.frag_size[j1],
554 i2 = state.heaplimit + 1;
555 i1 = state.heaplimit + 1;
568 /* All blocks/fragments are equal to another block/fragment ? */
571 for(i = 1; i < state.heaplimit; i++) {
572 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
573 heap_region1, &heapinfo_temp1, &heapinfos1[i], sizeof(malloc_info));
575 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED
576 && i1 == state.heaplimit
577 && heapinfo1->busy_block.busy_size > 0
578 && !state.equals_to1_(i, 0).valid) {
579 XBT_DEBUG("Block %zu not found (size used = %zu)", i,
580 heapinfo1->busy_block.busy_size);
584 if (heapinfo1->type <= 0)
586 for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo1->type); j++)
587 if (i1 == state.heaplimit
588 && heapinfo1->busy_frag.frag_size[j] > 0
589 && !state.equals_to1_(i, j).valid) {
590 XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)",
591 i, j, heapinfo1->busy_frag.frag_size[j]);
596 if (i1 == state.heaplimit)
597 XBT_DEBUG("Number of blocks/fragments not found in heap1 : %d", nb_diff1);
599 for (i=1; i < state.heaplimit; i++) {
600 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
601 heap_region2, &heapinfo_temp2, &heapinfos2[i], sizeof(malloc_info));
602 if (heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED
603 && i1 == state.heaplimit
604 && heapinfo2->busy_block.busy_size > 0
605 && !state.equals_to2_(i, 0).valid) {
606 XBT_DEBUG("Block %zu not found (size used = %zu)", i,
607 heapinfo2->busy_block.busy_size);
611 if (heapinfo2->type <= 0)
614 for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo2->type); j++)
615 if (i1 == state.heaplimit
616 && heapinfo2->busy_frag.frag_size[j] > 0
617 && !state.equals_to2_(i, j).valid) {
618 XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)",
619 i, j, heapinfo2->busy_frag.frag_size[j]);
625 if (i1 == state.heaplimit)
626 XBT_DEBUG("Number of blocks/fragments not found in heap2 : %d", nb_diff2);
628 return nb_diff1 > 0 || nb_diff2 > 0;
634 * @param real_area1 Process address for state 1
635 * @param real_area2 Process address for state 2
636 * @param snapshot1 Snapshot of state 1
637 * @param snapshot2 Snapshot of state 2
640 * @param check_ignore
642 static int compare_heap_area_without_type(
643 simgrid::mc::StateComparator& state, int process_index,
644 const void *real_area1, const void *real_area2,
645 simgrid::mc::Snapshot* snapshot1,
646 simgrid::mc::Snapshot* snapshot2,
647 HeapLocationPairs* previous, int size,
650 simgrid::mc::Process* process = &mc_model_checker->process();
651 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
652 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
654 for (int i = 0; i < size; ) {
656 if (check_ignore > 0) {
657 ssize_t ignore1 = heap_comparison_ignore_size(
658 state.processStates[0].to_ignore, (char *) real_area1 + i);
660 ssize_t ignore2 = heap_comparison_ignore_size(
661 state.processStates[1].to_ignore, (char *) real_area2 + i);
662 if (ignore2 == ignore1) {
675 if (MC_snapshot_region_memcmp(((char *) real_area1) + i, heap_region1, ((char *) real_area2) + i, heap_region2, 1) != 0) {
677 int pointer_align = (i / sizeof(void *)) * sizeof(void *);
678 const void* addr_pointed1 = snapshot1->read(
679 remote((void**)((char *) real_area1 + pointer_align)), process_index);
680 const void* addr_pointed2 = snapshot2->read(
681 remote((void**)((char *) real_area2 + pointer_align)), process_index);
683 if (process->in_maestro_stack(remote(addr_pointed1))
684 && process->in_maestro_stack(remote(addr_pointed2))) {
685 i = pointer_align + sizeof(void *);
689 if (addr_pointed1 > state.std_heap_copy.heapbase
690 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
691 && addr_pointed2 > state.std_heap_copy.heapbase
692 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)) {
693 // Both addreses are in the heap:
694 int res_compare = compare_heap_area(state ,process_index,
695 addr_pointed1, addr_pointed2,
696 snapshot1, snapshot2, previous, nullptr, 0);
697 if (res_compare == 1)
699 i = pointer_align + sizeof(void *);
715 * @param real_area1 Process address for state 1
716 * @param real_area2 Process address for state 2
717 * @param snapshot1 Snapshot of state 1
718 * @param snapshot2 Snapshot of state 2
721 * @param area_size either a byte_size or an elements_count (?)
722 * @param check_ignore
723 * @param pointer_level
724 * @return 0 (same), 1 (different), -1 (unknown)
726 static int compare_heap_area_with_type(
727 simgrid::mc::StateComparator& state, int process_index,
728 const void *real_area1, const void *real_area2,
729 simgrid::mc::Snapshot* snapshot1,
730 simgrid::mc::Snapshot* snapshot2,
731 HeapLocationPairs* previous, simgrid::mc::Type* type,
732 int area_size, int check_ignore,
737 // HACK: This should not happen but in pratice, there are some
738 // DW_TAG_typedef without an associated DW_AT_type:
739 //<1><538832>: Abbrev Number: 111 (DW_TAG_typedef)
740 // <538833> DW_AT_name : (indirect string, offset: 0x2292f3): gregset_t
741 // <538837> DW_AT_decl_file : 98
742 // <538838> DW_AT_decl_line : 37
746 if (is_stack(real_area1) && is_stack(real_area2))
749 if (check_ignore > 0) {
750 ssize_t ignore1 = heap_comparison_ignore_size(
751 state.processStates[0].to_ignore, real_area1);
753 && heap_comparison_ignore_size(
754 state.processStates[1].to_ignore, real_area2) == ignore1)
758 simgrid::mc::Type *subtype, *subsubtype;
760 const void *addr_pointed1, *addr_pointed2;
762 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
763 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
765 switch (type->type) {
766 case DW_TAG_unspecified_type:
769 case DW_TAG_base_type:
770 if (!type->name.empty() && type->name == "char") { /* String, hence random (arbitrary ?) size */
771 if (real_area1 == real_area2)
774 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, area_size) != 0;
776 if (area_size != -1 && type->byte_size != area_size)
779 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
783 case DW_TAG_enumeration_type:
784 if (area_size != -1 && type->byte_size != area_size)
786 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
789 case DW_TAG_const_type:
790 case DW_TAG_volatile_type:
792 type = type->subtype;
795 case DW_TAG_array_type:
796 subtype = type->subtype;
797 switch (subtype->type) {
798 case DW_TAG_unspecified_type:
801 case DW_TAG_base_type:
802 case DW_TAG_enumeration_type:
803 case DW_TAG_pointer_type:
804 case DW_TAG_reference_type:
805 case DW_TAG_rvalue_reference_type:
806 case DW_TAG_structure_type:
807 case DW_TAG_class_type:
808 case DW_TAG_union_type:
809 if (subtype->full_type)
810 subtype = subtype->full_type;
811 elm_size = subtype->byte_size;
813 // TODO, just remove the type indirection?
814 case DW_TAG_const_type:
816 case DW_TAG_volatile_type:
817 subsubtype = subtype->subtype;
818 if (subsubtype->full_type)
819 subsubtype = subsubtype->full_type;
820 elm_size = subsubtype->byte_size;
826 for (int i = 0; i < type->element_count; i++) {
827 // TODO, add support for variable stride (DW_AT_byte_stride)
829 compare_heap_area_with_type(state, process_index,
830 (char *) real_area1 + (i * elm_size),
831 (char *) real_area2 + (i * elm_size),
832 snapshot1, snapshot2, previous,
833 type->subtype, subtype->byte_size,
834 check_ignore, pointer_level);
840 case DW_TAG_reference_type:
841 case DW_TAG_rvalue_reference_type:
842 case DW_TAG_pointer_type:
843 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
844 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
845 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
846 return (addr_pointed1 != addr_pointed2);
849 if (pointer_level <= 1) {
850 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
851 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
852 if (addr_pointed1 > state.std_heap_copy.heapbase
853 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
854 && addr_pointed2 > state.std_heap_copy.heapbase
855 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
856 return compare_heap_area(state, process_index,
857 addr_pointed1, addr_pointed2, snapshot1,
858 snapshot2, previous, type->subtype,
861 return (addr_pointed1 != addr_pointed2);
863 for (size_t i = 0; i < (area_size / sizeof(void *)); i++) {
864 addr_pointed1 = snapshot1->read(
865 remote((void**)((char*) real_area1 + i * sizeof(void *))),
867 addr_pointed2 = snapshot2->read(
868 remote((void**)((char*) real_area2 + i * sizeof(void *))),
870 if (addr_pointed1 > state.std_heap_copy.heapbase
871 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
872 && addr_pointed2 > state.std_heap_copy.heapbase
873 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
875 compare_heap_area(state, process_index,
876 addr_pointed1, addr_pointed2, snapshot1,
877 snapshot2, previous, type->subtype,
880 res = (addr_pointed1 != addr_pointed2);
886 case DW_TAG_structure_type:
887 case DW_TAG_class_type:
889 type = type->full_type;
890 if (area_size != -1 && type->byte_size != area_size) {
891 if (area_size <= type->byte_size || area_size % type->byte_size != 0)
893 for (size_t i = 0; i < (size_t)(area_size / type->byte_size); i++) {
894 int res = compare_heap_area_with_type(state, process_index,
895 (char *) real_area1 + i * type->byte_size,
896 (char *) real_area2 + i * type->byte_size,
897 snapshot1, snapshot2, previous, type, -1,
903 for(simgrid::mc::Member& member : type->members) {
904 // TODO, optimize this? (for the offset case)
905 void *real_member1 = simgrid::dwarf::resolve_member(
906 real_area1, type, &member, (simgrid::mc::AddressSpace*) snapshot1, process_index);
907 void *real_member2 = simgrid::dwarf::resolve_member(
908 real_area2, type, &member, (simgrid::mc::AddressSpace*) snapshot2, process_index);
909 int res = compare_heap_area_with_type(
910 state, process_index, real_member1, real_member2,
911 snapshot1, snapshot2,
912 previous, member.type, -1,
920 case DW_TAG_union_type:
921 return compare_heap_area_without_type(state, process_index, real_area1, real_area2,
922 snapshot1, snapshot2, previous,
923 type->byte_size, check_ignore);
930 xbt_die("Unreachable");
933 /** Infer the type of a part of the block from the type of the block
935 * TODO, handle DW_TAG_array_type as well as arrays of the object ((*p)[5], p[5])
937 * TODO, handle subfields ((*p).bar.foo, (*p)[5].bar…)
939 * @param type_id DWARF type ID of the root address
941 * @return DWARF type ID for given offset
943 static simgrid::mc::Type* get_offset_type(void *real_base_address, simgrid::mc::Type* type,
944 int offset, int area_size,
945 simgrid::mc::Snapshot* snapshot, int process_index)
948 // Beginning of the block, the infered variable type if the type of the block:
952 switch (type->type) {
954 case DW_TAG_structure_type:
955 case DW_TAG_class_type:
957 type = type->full_type;
958 if (area_size != -1 && type->byte_size != area_size) {
959 if (area_size > type->byte_size && area_size % type->byte_size == 0)
965 for(simgrid::mc::Member& member : type->members) {
966 if (member.has_offset_location()) {
967 // We have the offset, use it directly (shortcut):
968 if (member.offset() == offset)
971 void *real_member = simgrid::dwarf::resolve_member(
972 real_base_address, type, &member, snapshot, process_index);
973 if ((char*) real_member - (char *) real_base_address == offset)
980 /* FIXME : other cases ? */
988 * @param area1 Process address for state 1
989 * @param area2 Process address for state 2
990 * @param snapshot1 Snapshot of state 1
991 * @param snapshot2 Snapshot of state 2
992 * @param previous Pairs of blocks already compared on the current path (or nullptr)
993 * @param type_id Type of variable
994 * @param pointer_level
995 * @return 0 (same), 1 (different), -1
998 int compare_heap_area(simgrid::mc::StateComparator& state, int process_index,
999 const void *area1, const void *area2,
1000 simgrid::mc::Snapshot* snapshot1,
1001 simgrid::mc::Snapshot* snapshot2,
1002 HeapLocationPairs* previous,
1003 simgrid::mc::Type* type, int pointer_level)
1005 simgrid::mc::Process* process = &mc_model_checker->process();
1008 ssize_t block1, frag1, block2, frag2;
1010 int check_ignore = 0;
1012 void *real_addr_block1, *real_addr_block2, *real_addr_frag1, *real_addr_frag2;
1014 int offset1 = 0, offset2 = 0;
1015 int new_size1 = -1, new_size2 = -1;
1016 simgrid::mc::Type *new_type1 = nullptr, *new_type2 = nullptr;
1018 bool match_pairs = false;
1020 // This is the address of std_heap->heapinfo in the application process:
1021 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
1023 const malloc_info* heapinfos1 = snapshot1->read(
1024 remote((const malloc_info**)heapinfo_address), process_index);
1025 const malloc_info* heapinfos2 = snapshot2->read(
1026 remote((const malloc_info**)heapinfo_address), process_index);
1028 malloc_info heapinfo_temp1, heapinfo_temp2;
1030 simgrid::mc::HeapLocationPairs current;
1031 if (previous == nullptr) {
1032 previous = ¤t;
1036 // Get block number:
1039 (char *) state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
1042 (char *) state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
1044 // If either block is a stack block:
1045 if (is_block_stack((int) block1) && is_block_stack((int) block2)) {
1046 previous->insert(simgrid::mc::makeHeapLocationPair(
1047 block1, -1, block2, -1));
1049 state.match_equals(previous);
1053 // If either block is not in the expected area of memory:
1054 if (((char *) area1 < (char *) state.std_heap_copy.heapbase)
1055 || (block1 > (ssize_t) state.processStates[0].heapsize) || (block1 < 1)
1056 || ((char *) area2 < (char *) state.std_heap_copy.heapbase)
1057 || (block2 > (ssize_t) state.processStates[1].heapsize) || (block2 < 1)) {
1061 // Process address of the block:
1062 real_addr_block1 = (ADDR2UINT(block1) - 1) * BLOCKSIZE +
1063 (char *) state.std_heap_copy.heapbase;
1064 real_addr_block2 = (ADDR2UINT(block2) - 1) * BLOCKSIZE +
1065 (char *) state.std_heap_copy.heapbase;
1069 if (type->full_type)
1070 type = type->full_type;
1072 // This assume that for "boring" types (volatile ...) byte_size is absent:
1073 while (type->byte_size == 0 && type->subtype != nullptr)
1074 type = type->subtype;
1077 if (type->type == DW_TAG_pointer_type
1078 || (type->type == DW_TAG_base_type && !type->name.empty()
1079 && type->name == "char"))
1082 type_size = type->byte_size;
1086 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
1087 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
1089 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
1090 heap_region1, &heapinfo_temp1, &heapinfos1[block1], sizeof(malloc_info));
1091 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
1092 heap_region2, &heapinfo_temp2, &heapinfos2[block2], sizeof(malloc_info));
1094 if ((heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type==MMALLOC_TYPE_HEAPINFO)
1095 && (heapinfo2->type == MMALLOC_TYPE_FREE || heapinfo2->type ==MMALLOC_TYPE_HEAPINFO)) {
1098 state.match_equals(previous);
1102 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED
1103 && heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
1104 /* Complete block */
1106 // TODO, lookup variable type from block type as done for fragmented blocks
1108 offset1 = (char *) area1 - (char *) real_addr_block1;
1109 offset2 = (char *) area2 - (char *) real_addr_block2;
1111 if (state.equals_to1_(block1, 0).valid
1112 && state.equals_to2_(block2, 0).valid
1113 && state.blocksEqual(block1, block2)) {
1115 state.match_equals(previous);
1119 if (type_size != -1) {
1120 if (type_size != (ssize_t) heapinfo1->busy_block.busy_size
1121 && type_size != (ssize_t) heapinfo2->busy_block.busy_size
1122 && (type->name.empty() || type->name == "struct s_smx_context")) {
1124 state.match_equals(previous);
1129 if (heapinfo1->busy_block.size != heapinfo2->busy_block.size)
1131 if (heapinfo1->busy_block.busy_size != heapinfo2->busy_block.busy_size)
1134 if (!previous->insert(simgrid::mc::makeHeapLocationPair(
1135 block1, -1, block2, -1)).second) {
1137 state.match_equals(previous);
1141 size = heapinfo1->busy_block.busy_size;
1143 // Remember (basic) type inference.
1144 // The current data structure only allows us to do this for the whole block.
1145 if (type != nullptr && area1 == real_addr_block1)
1146 state.types1_(block1, 0) = type;
1147 if (type != nullptr && area2 == real_addr_block2)
1148 state.types2_(block2, 0) = type;
1152 state.match_equals(previous);
1159 if (heapinfo1->busy_block.ignore > 0
1160 && heapinfo2->busy_block.ignore == heapinfo1->busy_block.ignore)
1161 check_ignore = heapinfo1->busy_block.ignore;
1163 } else if ((heapinfo1->type > 0) && (heapinfo2->type > 0)) { /* Fragmented block */
1167 ((uintptr_t) (ADDR2UINT(area1) % (BLOCKSIZE))) >> heapinfo1->type;
1169 ((uintptr_t) (ADDR2UINT(area2) % (BLOCKSIZE))) >> heapinfo2->type;
1171 // Process address of the fragment:
1173 (void *) ((char *) real_addr_block1 +
1174 (frag1 << heapinfo1->type));
1176 (void *) ((char *) real_addr_block2 +
1177 (frag2 << heapinfo2->type));
1179 // Check the size of the fragments against the size of the type:
1180 if (type_size != -1) {
1181 if (heapinfo1->busy_frag.frag_size[frag1] == -1
1182 || heapinfo2->busy_frag.frag_size[frag2] == -1) {
1184 state.match_equals(previous);
1188 if (type_size != heapinfo1->busy_frag.frag_size[frag1]
1189 || type_size != heapinfo2->busy_frag.frag_size[frag2]) {
1191 state.match_equals(previous);
1196 // Check if the blocks are already matched together:
1197 if (state.equals_to1_(block1, frag1).valid
1198 && state.equals_to2_(block2, frag2).valid) {
1199 if (offset1==offset2 && state.fragmentsEqual(block1, frag1, block2, frag2)) {
1201 state.match_equals(previous);
1205 // Compare the size of both fragments:
1206 if (heapinfo1->busy_frag.frag_size[frag1] !=
1207 heapinfo2->busy_frag.frag_size[frag2]) {
1208 if (type_size == -1) {
1210 state.match_equals(previous);
1216 // Size of the fragment:
1217 size = heapinfo1->busy_frag.frag_size[frag1];
1219 // Remember (basic) type inference.
1220 // The current data structure only allows us to do this for the whole fragment.
1221 if (type != nullptr && area1 == real_addr_frag1)
1222 state.types1_(block1, frag1) = type;
1223 if (type != nullptr && area2 == real_addr_frag2)
1224 state.types2_(block2, frag2) = type;
1226 // The type of the variable is already known:
1231 // Type inference from the block type.
1232 else if (state.types1_(block1, frag1) != nullptr
1233 || state.types2_(block2, frag2) != nullptr) {
1235 offset1 = (char *) area1 - (char *) real_addr_frag1;
1236 offset2 = (char *) area2 - (char *) real_addr_frag2;
1238 if (state.types1_(block1, frag1) != nullptr
1239 && state.types2_(block2, frag2) != nullptr) {
1241 get_offset_type(real_addr_frag1, state.types1_(block1, frag1),
1242 offset1, size, snapshot1, process_index);
1244 get_offset_type(real_addr_frag2, state.types2_(block2, frag2),
1245 offset1, size, snapshot2, process_index);
1246 } else if (state.types1_(block1, frag1) != nullptr) {
1248 get_offset_type(real_addr_frag1, state.types1_(block1, frag1),
1249 offset1, size, snapshot1, process_index);
1251 get_offset_type(real_addr_frag2, state.types1_(block1, frag1),
1252 offset2, size, snapshot2, process_index);
1253 } else if (state.types2_(block2, frag2) != nullptr) {
1255 get_offset_type(real_addr_frag1, state.types2_(block2, frag2),
1256 offset1, size, snapshot1, process_index);
1258 get_offset_type(real_addr_frag2, state.types2_(block2, frag2),
1259 offset2, size, snapshot2, process_index);
1262 state.match_equals(previous);
1266 if (new_type1 != nullptr && new_type2 != nullptr && new_type1 != new_type2) {
1269 while (type->byte_size == 0 && type->subtype != nullptr)
1270 type = type->subtype;
1271 new_size1 = type->byte_size;
1274 while (type->byte_size == 0 && type->subtype != nullptr)
1275 type = type->subtype;
1276 new_size2 = type->byte_size;
1280 state.match_equals(previous);
1285 if (new_size1 > 0 && new_size1 == new_size2) {
1290 if (offset1 == 0 && offset2 == 0
1291 && !previous->insert(simgrid::mc::makeHeapLocationPair(
1292 block1, frag1, block2, frag2)).second) {
1294 state.match_equals(previous);
1300 state.match_equals(previous);
1304 if ((heapinfo1->busy_frag.ignore[frag1] > 0)
1305 && (heapinfo2->busy_frag.ignore[frag2] ==
1306 heapinfo1->busy_frag.ignore[frag1]))
1307 check_ignore = heapinfo1->busy_frag.ignore[frag1];
1313 /* Start comparison */
1316 compare_heap_area_with_type(state, process_index, area1, area2, snapshot1, snapshot2,
1317 previous, type, size, check_ignore,
1321 compare_heap_area_without_type(state, process_index, area1, area2, snapshot1, snapshot2,
1322 previous, size, check_ignore);
1324 if (res_compare == 1)
1328 state.match_equals(previous);
1335 /************************** Snapshot comparison *******************************/
1336 /******************************************************************************/
1338 static int compare_areas_with_type(simgrid::mc::StateComparator& state,
1340 void* real_area1, simgrid::mc::Snapshot* snapshot1, mc_mem_region_t region1,
1341 void* real_area2, simgrid::mc::Snapshot* snapshot2, mc_mem_region_t region2,
1342 simgrid::mc::Type* type, int pointer_level)
1344 simgrid::mc::Process* process = &mc_model_checker->process();
1346 simgrid::mc::Type* subtype;
1347 simgrid::mc::Type* subsubtype;
1348 int elm_size, i, res;
1351 switch (type->type) {
1352 case DW_TAG_unspecified_type:
1355 case DW_TAG_base_type:
1356 case DW_TAG_enumeration_type:
1357 case DW_TAG_union_type:
1359 return MC_snapshot_region_memcmp(
1360 real_area1, region1, real_area2, region2,
1361 type->byte_size) != 0;
1363 case DW_TAG_typedef:
1364 case DW_TAG_volatile_type:
1365 case DW_TAG_const_type:
1367 type = type->subtype;
1369 case DW_TAG_array_type:
1370 subtype = type->subtype;
1371 switch (subtype->type) {
1372 case DW_TAG_unspecified_type:
1375 case DW_TAG_base_type:
1376 case DW_TAG_enumeration_type:
1377 case DW_TAG_pointer_type:
1378 case DW_TAG_reference_type:
1379 case DW_TAG_rvalue_reference_type:
1380 case DW_TAG_structure_type:
1381 case DW_TAG_class_type:
1382 case DW_TAG_union_type:
1383 if (subtype->full_type)
1384 subtype = subtype->full_type;
1385 elm_size = subtype->byte_size;
1387 case DW_TAG_const_type:
1388 case DW_TAG_typedef:
1389 case DW_TAG_volatile_type:
1390 subsubtype = subtype->subtype;
1391 if (subsubtype->full_type)
1392 subsubtype = subsubtype->full_type;
1393 elm_size = subsubtype->byte_size;
1399 for (i = 0; i < type->element_count; i++) {
1400 size_t off = i * elm_size;
1401 res = compare_areas_with_type(state, process_index,
1402 (char*) real_area1 + off, snapshot1, region1,
1403 (char*) real_area2 + off, snapshot2, region2,
1404 type->subtype, pointer_level);
1409 case DW_TAG_pointer_type:
1410 case DW_TAG_reference_type:
1411 case DW_TAG_rvalue_reference_type:
1413 void* addr_pointed1 = MC_region_read_pointer(region1, real_area1);
1414 void* addr_pointed2 = MC_region_read_pointer(region2, real_area2);
1416 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type)
1417 return (addr_pointed1 != addr_pointed2);
1418 if (addr_pointed1 == nullptr && addr_pointed2 == nullptr)
1420 if (addr_pointed1 == nullptr || addr_pointed2 == nullptr)
1422 if (!state.compared_pointers.insert(
1423 std::make_pair(addr_pointed1, addr_pointed2)).second)
1428 // Some cases are not handled here:
1429 // * the pointers lead to different areas (one to the heap, the other to the RW segment ...);
1430 // * a pointer leads to the read-only segment of the current object;
1431 // * a pointer lead to a different ELF object.
1433 if (addr_pointed1 > process->heap_address
1434 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)) {
1436 (addr_pointed2 > process->heap_address
1437 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)))
1439 // The pointers are both in the heap:
1440 return simgrid::mc::compare_heap_area(state,
1441 process_index, addr_pointed1, addr_pointed2, snapshot1,
1442 snapshot2, nullptr, type->subtype, pointer_level);
1445 // The pointers are both in the current object R/W segment:
1446 else if (region1->contain(simgrid::mc::remote(addr_pointed1))) {
1447 if (!region2->contain(simgrid::mc::remote(addr_pointed2)))
1450 return (addr_pointed1 != addr_pointed2);
1452 return compare_areas_with_type(state, process_index,
1453 addr_pointed1, snapshot1, region1,
1454 addr_pointed2, snapshot2, region2,
1455 type->subtype, pointer_level);
1458 // TODO, We do not handle very well the case where
1459 // it belongs to a different (non-heap) region from the current one.
1462 return (addr_pointed1 != addr_pointed2);
1466 case DW_TAG_structure_type:
1467 case DW_TAG_class_type:
1468 for(simgrid::mc::Member& member : type->members) {
1469 void *member1 = simgrid::dwarf::resolve_member(
1470 real_area1, type, &member, snapshot1, process_index);
1471 void *member2 = simgrid::dwarf::resolve_member(
1472 real_area2, type, &member, snapshot2, process_index);
1473 mc_mem_region_t subregion1 = mc_get_region_hinted(member1, snapshot1, process_index, region1);
1474 mc_mem_region_t subregion2 = mc_get_region_hinted(member2, snapshot2, process_index, region2);
1476 compare_areas_with_type(state, process_index,
1477 member1, snapshot1, subregion1,
1478 member2, snapshot2, subregion2,
1479 member.type, pointer_level);
1484 case DW_TAG_subroutine_type:
1488 XBT_VERB("Unknown case : %d", type->type);
1495 static int compare_global_variables(
1496 simgrid::mc::StateComparator& state,
1497 simgrid::mc::ObjectInformation* object_info,
1499 mc_mem_region_t r1, mc_mem_region_t r2,
1500 simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2)
1502 xbt_assert(r1 && r2, "Missing region.");
1505 if (r1->storage_type() == simgrid::mc::StorageType::Privatized) {
1506 xbt_assert(process_index >= 0);
1507 if (r2->storage_type() != simgrid::mc::StorageType::Privatized)
1510 size_t process_count = MC_smpi_process_count();
1511 xbt_assert(process_count == r1->privatized_data().size()
1512 && process_count == r2->privatized_data().size());
1514 // Compare the global variables separately for each simulates process:
1515 for (size_t process_index = 0; process_index < process_count; process_index++) {
1516 int is_diff = compare_global_variables(state,
1517 object_info, process_index,
1518 &r1->privatized_data()[process_index],
1519 &r2->privatized_data()[process_index],
1520 snapshot1, snapshot2);
1521 if (is_diff) return 1;
1526 xbt_assert(r1->storage_type() != simgrid::mc::StorageType::Privatized);
1528 xbt_assert(r2->storage_type() != simgrid::mc::StorageType::Privatized);
1530 std::vector<simgrid::mc::Variable>& variables = object_info->global_variables;
1532 for (simgrid::mc::Variable& current_var : variables) {
1534 // If the variable is not in this object, skip it:
1535 // We do not expect to find a pointer to something which is not reachable
1536 // by the global variables.
1537 if ((char *) current_var.address < (char *) object_info->start_rw
1538 || (char *) current_var.address > (char *) object_info->end_rw)
1541 simgrid::mc::Type* bvariable_type = current_var.type;
1542 int res = compare_areas_with_type(state, process_index,
1543 (char *) current_var.address, snapshot1, r1,
1544 (char *) current_var.address, snapshot2, r2,
1547 XBT_VERB("Global variable %s (%p) is different between snapshots",
1548 current_var.name.c_str(),
1549 (char *) current_var.address);
1559 static int compare_local_variables(simgrid::mc::StateComparator& state,
1561 simgrid::mc::Snapshot* snapshot1,
1562 simgrid::mc::Snapshot* snapshot2,
1563 mc_snapshot_stack_t stack1,
1564 mc_snapshot_stack_t stack2)
1566 if (stack1->local_variables.size() != stack2->local_variables.size()) {
1567 XBT_VERB("Different number of local variables");
1571 unsigned int cursor = 0;
1572 local_variable_t current_var1, current_var2;
1574 while (cursor < stack1->local_variables.size()) {
1575 current_var1 = &stack1->local_variables[cursor];
1576 current_var2 = &stack1->local_variables[cursor];
1577 if (current_var1->name != current_var2->name
1578 || current_var1->subprogram != current_var2->subprogram
1579 || current_var1->ip != current_var2->ip) {
1580 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1582 ("Different name of variable (%s - %s) "
1583 "or frame (%s - %s) or ip (%lu - %lu)",
1584 current_var1->name.c_str(),
1585 current_var2->name.c_str(),
1586 current_var1->subprogram->name.c_str(),
1587 current_var2->subprogram->name.c_str(),
1588 current_var1->ip, current_var2->ip);
1591 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1593 simgrid::mc::Type* subtype = current_var1->type;
1595 compare_areas_with_type(state, process_index,
1596 current_var1->address, snapshot1, mc_get_snapshot_region(current_var1->address, snapshot1, process_index),
1597 current_var2->address, snapshot2, mc_get_snapshot_region(current_var2->address, snapshot2, process_index),
1601 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1603 ("Local variable %s (%p - %p) in frame %s "
1604 "is different between snapshots",
1605 current_var1->name.c_str(),
1606 current_var1->address,
1607 current_var2->address,
1608 current_var1->subprogram->name.c_str());
1619 static std::unique_ptr<simgrid::mc::StateComparator> state_comparator;
1621 int snapshot_compare(int num1, simgrid::mc::Snapshot* s1, int num2, simgrid::mc::Snapshot* s2)
1623 // TODO, make this a field of ModelChecker or something similar
1625 if (state_comparator == nullptr)
1626 state_comparator = std::unique_ptr<StateComparator>(new StateComparator());
1628 state_comparator->clear();
1630 simgrid::mc::Process* process = &mc_model_checker->process();
1635 int hash_result = 0;
1637 hash_result = (s1->hash != s2->hash);
1639 XBT_VERB("(%d - %d) Different hash : 0x%" PRIx64 "--0x%" PRIx64, num1,
1640 num2, s1->hash, s2->hash);
1645 XBT_VERB("(%d - %d) Same hash : 0x%" PRIx64, num1, num2, s1->hash);
1648 /* Compare enabled processes */
1649 if (s1->enabled_processes != s2->enabled_processes) {
1650 XBT_VERB("(%d - %d) Different enabled processes", num1, num2);
1654 unsigned long i = 0;
1655 size_t size_used1, size_used2;
1658 /* Compare size of stacks */
1659 while (i < s1->stacks.size()) {
1660 size_used1 = s1->stack_sizes[i];
1661 size_used2 = s2->stack_sizes[i];
1662 if (size_used1 != size_used2) {
1664 XBT_DEBUG("(%d - %d) Different size used in stacks : %zu - %zu", num1,
1665 num2, size_used1, size_used2);
1670 XBT_VERB("(%d - %d) Different size used in stacks : %zu - %zu", num1,
1671 num2, size_used1, size_used2);
1679 /* Init heap information used in heap comparison algorithm */
1680 xbt_mheap_t heap1 = (xbt_mheap_t)s1->read_bytes(
1681 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1682 remote(process->heap_address),
1683 simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1684 xbt_mheap_t heap2 = (xbt_mheap_t)s2->read_bytes(
1685 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1686 remote(process->heap_address),
1687 simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1688 res_init = state_comparator->initHeapInformation(
1689 heap1, heap2, &s1->to_ignore, &s2->to_ignore);
1691 if (res_init == -1) {
1693 XBT_DEBUG("(%d - %d) Different heap information", num1, num2);
1697 XBT_VERB("(%d - %d) Different heap information", num1, num2);
1704 /* Stacks comparison */
1705 unsigned cursor = 0;
1710 mc_snapshot_stack_t stack1, stack2;
1711 while (cursor < s1->stacks.size()) {
1712 stack1 = &s1->stacks[cursor];
1713 stack2 = &s2->stacks[cursor];
1715 if (stack1->process_index != stack2->process_index) {
1717 XBT_DEBUG("(%d - %d) Stacks with different process index (%i vs %i)", num1, num2,
1718 stack1->process_index, stack2->process_index);
1720 else diff_local = compare_local_variables(*state_comparator,
1721 stack1->process_index, s1, s2, stack1, stack2);
1722 if (diff_local > 0) {
1724 XBT_DEBUG("(%d - %d) Different local variables between stacks %d", num1,
1731 XBT_VERB("(%d - %d) Different local variables between stacks %d", num1,
1741 size_t regions_count = s1->snapshot_regions.size();
1742 // TODO, raise a difference instead?
1743 xbt_assert(regions_count == s2->snapshot_regions.size());
1745 for (size_t k = 0; k != regions_count; ++k) {
1746 mc_mem_region_t region1 = s1->snapshot_regions[k].get();
1747 mc_mem_region_t region2 = s2->snapshot_regions[k].get();
1750 if (region1->region_type() != simgrid::mc::RegionType::Data)
1753 xbt_assert(region1->region_type() == region2->region_type());
1754 xbt_assert(region1->object_info() == region2->object_info());
1755 xbt_assert(region1->object_info());
1757 std::string const& name = region1->object_info()->file_name;
1759 /* Compare global variables */
1761 compare_global_variables(*state_comparator,
1762 region1->object_info(), simgrid::mc::ProcessIndexDisabled,
1763 region1, region2, s1, s2);
1767 XBT_DEBUG("(%d - %d) Different global variables in %s",
1768 num1, num2, name.c_str());
1772 XBT_VERB("(%d - %d) Different global variables in %s",
1773 num1, num2, name.c_str());
1782 if (simgrid::mc::mmalloc_compare_heap(*state_comparator, s1, s2) > 0) {
1785 XBT_DEBUG("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
1790 XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
1798 if (errors || hash_result)
1799 XBT_VERB("(%d - %d) Difference found", num1, num2);
1801 XBT_VERB("(%d - %d) No difference found", num1, num2);
1804 #if defined(MC_DEBUG) && defined(MC_VERBOSE)
1806 // * false positive SHOULD be avoided.
1807 // * There MUST not be any false negative.
1809 XBT_VERB("(%d - %d) State equality hash test is %s %s", num1, num2,
1810 (hash_result != 0) == (errors != 0) ? "true" : "false",
1811 !hash_result ? "positive" : "negative");
1815 return errors > 0 || hash_result;