1 /* Copyright (c) 2008-2016. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 /** \file mc_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");
51 struct ProcessComparisonState;
52 struct StateComparator;
54 static int compare_heap_area(
55 int process_index, const void *area1, const void* area2,
56 Snapshot* snapshot1, Snapshot* snapshot2,
57 xbt_dynar_t previous, Type* type, int pointer_level);
59 static void reset_heap_information(void);
64 using simgrid::mc::remote;
66 /*********************************** Heap comparison ***********************************/
67 /***************************************************************************************/
72 struct ProcessComparisonState {
73 std::vector<simgrid::mc::IgnoredHeapRegion>* to_ignore = nullptr;
74 std::vector<s_heap_area_t> equals_to;
75 std::vector<simgrid::mc::Type*> types;
76 std::size_t heapsize = 0;
78 void initHeapInformation(xbt_mheap_t heap,
79 std::vector<simgrid::mc::IgnoredHeapRegion>* i);
82 struct StateComparator {
83 s_xbt_mheap_t std_heap_copy;
84 std::size_t heaplimit;
85 std::array<ProcessComparisonState, 2> processStates;
87 int initHeapInformation(
88 xbt_mheap_t heap1, xbt_mheap_t heap2,
89 std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
90 std::vector<simgrid::mc::IgnoredHeapRegion>* i2);
92 s_heap_area_t& equals_to1_(std::size_t i, std::size_t j)
94 return processStates[0].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
96 s_heap_area_t& equals_to2_(std::size_t i, std::size_t j)
98 return processStates[1].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
100 Type*& types1_(std::size_t i, std::size_t j)
102 return processStates[0].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
104 Type*& types2_(std::size_t i, std::size_t j)
106 return processStates[1].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
109 s_heap_area_t const& equals_to1_(std::size_t i, std::size_t j) const
111 return processStates[0].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
113 s_heap_area_t const& equals_to2_(std::size_t i, std::size_t j) const
115 return processStates[1].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
117 Type* const& types1_(std::size_t i, std::size_t j) const
119 return processStates[0].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
121 Type* const& types2_(std::size_t i, std::size_t j) const
123 return processStates[1].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
126 /** Check whether two blocks are known to be matching
128 * @param state State used
129 * @param b1 Block of state 1
130 * @param b2 Block of state 2
131 * @return if the blocks are known to be matching
133 bool blocksEqual(int b1, int b2) const
135 return this->equals_to1_(b1, 0).block == b2
136 && this->equals_to2_(b2, 0).block == b1;
139 /** Check whether two fragments are known to be matching
141 * @param state State used
142 * @param b1 Block of state 1
143 * @param f1 Fragment of state 1
144 * @param b2 Block of state 2
145 * @param f2 Fragment of state 2
146 * @return if the fragments are known to be matching
148 int fragmentsEqual(int b1, int f1, int b2, int f2) const
150 return this->equals_to1_(b1, f1).block == b2
151 && this->equals_to1_(b1, f1).fragment == f2
152 && this->equals_to2_(b2, f2).block == b1
153 && this->equals_to2_(b2, f2).fragment == f1;
156 void match_equals(xbt_dynar_t list);
162 // TODO, make this a field of ModelChecker or something similar
163 static std::unique_ptr<simgrid::mc::StateComparator> mc_diff_info;
165 /*********************************** Free functions ************************************/
167 static void heap_area_pair_free(heap_area_pair_t pair)
173 static void heap_area_pair_free_voidp(void *d)
175 heap_area_pair_free((heap_area_pair_t) * (void **) d);
178 static void heap_area_free(heap_area_t area)
184 /************************************************************************************/
186 static s_heap_area_t make_heap_area(int block, int fragment)
191 area.fragment = fragment;
195 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1,
196 int block2, int fragment2)
199 unsigned int cursor = 0;
200 heap_area_pair_t current_pair;
202 xbt_dynar_foreach(list, cursor, current_pair)
203 if (current_pair->block1 == block1 && current_pair->block2 == block2
204 && current_pair->fragment1 == fragment1
205 && current_pair->fragment2 == fragment2)
211 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1,
212 int block2, int fragment2)
215 if (!is_new_heap_area_pair(list, block1, fragment1, block2, fragment2))
218 heap_area_pair_t pair = nullptr;
219 pair = xbt_new0(s_heap_area_pair_t, 1);
220 pair->block1 = block1;
221 pair->fragment1 = fragment1;
222 pair->block2 = block2;
223 pair->fragment2 = fragment2;
224 xbt_dynar_push(list, &pair);
228 static ssize_t heap_comparison_ignore_size(
229 std::vector<simgrid::mc::IgnoredHeapRegion>* ignore_list,
233 int end = ignore_list->size() - 1;
235 while (start <= end) {
236 unsigned int cursor = (start + end) / 2;
237 simgrid::mc::IgnoredHeapRegion const& region = (*ignore_list)[cursor];
238 if (region.address == address)
240 if (region.address < address)
242 if (region.address > address)
249 static bool is_stack(const void *address)
251 for (auto const& stack : mc_model_checker->process().stack_areas())
252 if (address == stack.address)
257 // TODO, this should depend on the snapshot?
258 static bool is_block_stack(int block)
260 for (auto const& stack : mc_model_checker->process().stack_areas())
261 if (block == stack.block)
269 void StateComparator::match_equals(xbt_dynar_t list)
271 unsigned int cursor = 0;
272 heap_area_pair_t current_pair;
274 xbt_dynar_foreach(list, cursor, current_pair) {
275 if (current_pair->fragment1 != -1) {
276 this->equals_to1_(current_pair->block1, current_pair->fragment1) =
277 make_heap_area(current_pair->block2, current_pair->fragment2);
278 this->equals_to2_(current_pair->block2, current_pair->fragment2) =
279 make_heap_area(current_pair->block1, current_pair->fragment1);
281 this->equals_to1_(current_pair->block1, 0) =
282 make_heap_area(current_pair->block2, current_pair->fragment2);
283 this->equals_to2_(current_pair->block2, 0) =
284 make_heap_area(current_pair->block1, current_pair->fragment1);
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, s_heap_area {0, 0, 0});
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);
317 int init_heap_information(xbt_mheap_t heap1, xbt_mheap_t heap2,
318 std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
319 std::vector<simgrid::mc::IgnoredHeapRegion>* i2)
321 if (mc_diff_info == nullptr)
322 mc_diff_info = std::unique_ptr<StateComparator>(new StateComparator());
323 return mc_diff_info->initHeapInformation(heap1, heap2, i1, i2);
327 void reset_heap_information()
332 // TODO, have a robust way to find it in O(1)
334 mc_mem_region_t MC_get_heap_region(simgrid::mc::Snapshot* snapshot)
336 for (auto& region : snapshot->snapshot_regions)
337 if (region->region_type() == simgrid::mc::RegionType::Heap)
339 xbt_die("No heap region");
343 int mmalloc_compare_heap(simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2)
345 simgrid::mc::Process* process = &mc_model_checker->process();
346 simgrid::mc::StateComparator *state = mc_diff_info.get();
348 /* Start comparison */
349 size_t i1, i2, j1, j2, k;
350 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
351 int nb_diff1 = 0, nb_diff2 = 0;
353 int equal, res_compare = 0;
355 /* Check busy blocks */
359 malloc_info heapinfo_temp1, heapinfo_temp2;
360 malloc_info heapinfo_temp2b;
362 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
363 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
365 // This is the address of std_heap->heapinfo in the application process:
366 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
368 // This is in snapshot do not use them directly:
369 const malloc_info* heapinfos1 = snapshot1->read<malloc_info*>(
370 (std::uint64_t)heapinfo_address, simgrid::mc::ProcessIndexMissing);
371 const malloc_info* heapinfos2 = snapshot2->read<malloc_info*>(
372 (std::uint64_t)heapinfo_address, simgrid::mc::ProcessIndexMissing);
374 while (i1 < state->heaplimit) {
376 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(heap_region1, &heapinfo_temp1, &heapinfos1[i1], sizeof(malloc_info));
377 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2, &heapinfos2[i1], sizeof(malloc_info));
379 if (heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type == MMALLOC_TYPE_HEAPINFO) { /* Free block */
384 if (heapinfo1->type < 0) {
385 fprintf(stderr, "Unkown mmalloc block type.\n");
390 ((void *) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE +
391 (char *) state->std_heap_copy.heapbase));
393 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED) { /* Large block */
395 if (is_stack(addr_block1)) {
396 for (k = 0; k < heapinfo1->busy_block.size; k++)
397 state->equals_to1_(i1 + k, 0) = make_heap_area(i1, -1);
398 for (k = 0; k < heapinfo2->busy_block.size; k++)
399 state->equals_to2_(i1 + k, 0) = make_heap_area(i1, -1);
400 i1 += heapinfo1->busy_block.size;
404 if (state->equals_to1_(i1, 0).valid) {
413 /* Try first to associate to same block in the other heap */
414 if (heapinfo2->type == heapinfo1->type
415 && state->equals_to2_(i1, 0).valid == 0) {
416 addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE +
417 (char *) state->std_heap_copy.heapbase;
419 compare_heap_area(simgrid::mc::ProcessIndexMissing, addr_block1, addr_block2, snapshot1, snapshot2,
420 nullptr, nullptr, 0);
421 if (res_compare != 1) {
422 for (k = 1; k < heapinfo2->busy_block.size; k++)
423 state->equals_to2_(i1 + k, 0) = make_heap_area(i1, -1);
424 for (k = 1; k < heapinfo1->busy_block.size; k++)
425 state->equals_to1_(i1 + k, 0) = make_heap_area(i1, -1);
427 i1 += heapinfo1->busy_block.size;
431 while (i2 < state->heaplimit && !equal) {
433 addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE +
434 (char *) state->std_heap_copy.heapbase;
441 const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2b, &heapinfos2[i2], sizeof(malloc_info));
443 if (heapinfo2b->type != MMALLOC_TYPE_UNFRAGMENTED) {
448 if (state->equals_to2_(i2, 0).valid) {
454 compare_heap_area(simgrid::mc::ProcessIndexMissing, addr_block1, addr_block2, snapshot1, snapshot2,
455 nullptr, nullptr, 0);
457 if (res_compare != 1) {
458 for (k = 1; k < heapinfo2b->busy_block.size; k++)
459 state->equals_to2_(i2 + k, 0) = make_heap_area(i1, -1);
460 for (k = 1; k < heapinfo1->busy_block.size; k++)
461 state->equals_to1_(i1 + k, 0) = make_heap_area(i2, -1);
463 i1 += heapinfo1->busy_block.size;
471 XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1,
472 heapinfo1->busy_block.busy_size, addr_block1);
473 i1 = state->heaplimit + 1;
478 } else { /* Fragmented block */
480 for (j1 = 0; j1 < (size_t) (BLOCKSIZE >> heapinfo1->type); j1++) {
482 if (heapinfo1->busy_frag.frag_size[j1] == -1) /* Free fragment */
485 if (state->equals_to1_(i1, j1).valid)
489 (void *) ((char *) addr_block1 + (j1 << heapinfo1->type));
494 /* Try first to associate to same fragment in the other heap */
495 if (heapinfo2->type == heapinfo1->type
496 && state->equals_to2_(i1, j1).valid == 0) {
497 addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE +
498 (char *) state->std_heap_copy.heapbase;
500 (void *) ((char *) addr_block2 +
501 (j1 << heapinfo2->type));
503 compare_heap_area(simgrid::mc::ProcessIndexMissing, addr_frag1, addr_frag2, snapshot1, snapshot2,
504 nullptr, nullptr, 0);
505 if (res_compare != 1)
511 while (i2 < state->heaplimit && !equal) {
513 const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(
514 heap_region2, &heapinfo_temp2b, &heapinfos2[i2],
515 sizeof(malloc_info));
517 if (heapinfo2b->type == MMALLOC_TYPE_FREE || heapinfo2b->type == MMALLOC_TYPE_HEAPINFO) {
522 // We currently do not match fragments with unfragmented blocks (maybe we should).
523 if (heapinfo2b->type == MMALLOC_TYPE_UNFRAGMENTED) {
528 if (heapinfo2b->type < 0) {
529 fprintf(stderr, "Unkown mmalloc block type.\n");
533 for (j2 = 0; j2 < (size_t) (BLOCKSIZE >> heapinfo2b->type);
536 if (i2 == i1 && j2 == j1)
539 if (state->equals_to2_(i2, j2).valid)
542 addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE +
543 (char *) state->std_heap_copy.heapbase;
545 (void *) ((char *) addr_block2 +
546 (j2 << heapinfo2b->type));
549 compare_heap_area(simgrid::mc::ProcessIndexMissing, addr_frag1, addr_frag2, snapshot2, snapshot2,
550 nullptr, nullptr, 0);
552 if (res_compare != 1) {
565 ("Block %zu, fragment %zu not found (size_used = %zd, address = %p)\n",
566 i1, j1, heapinfo1->busy_frag.frag_size[j1],
568 i2 = state->heaplimit + 1;
569 i1 = state->heaplimit + 1;
582 /* All blocks/fragments are equal to another block/fragment ? */
585 for(i = 1; i < state->heaplimit; i++) {
586 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
587 heap_region1, &heapinfo_temp1, &heapinfos1[i], sizeof(malloc_info));
589 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED
590 && i1 == state->heaplimit
591 && heapinfo1->busy_block.busy_size > 0
592 && state->equals_to1_(i, 0).valid == 0) {
593 XBT_DEBUG("Block %zu not found (size used = %zu)", i,
594 heapinfo1->busy_block.busy_size);
598 if (heapinfo1->type <= 0)
600 for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo1->type); j++)
601 if (i1 == state->heaplimit
602 && heapinfo1->busy_frag.frag_size[j] > 0
603 && state->equals_to1_(i, j).valid == 0) {
604 XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)",
605 i, j, heapinfo1->busy_frag.frag_size[j]);
610 if (i1 == state->heaplimit)
611 XBT_DEBUG("Number of blocks/fragments not found in heap1 : %d", nb_diff1);
613 for (i=1; i < state->heaplimit; i++) {
614 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
615 heap_region2, &heapinfo_temp2, &heapinfos2[i], sizeof(malloc_info));
616 if (heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED
617 && i1 == state->heaplimit
618 && heapinfo2->busy_block.busy_size > 0
619 && state->equals_to2_(i, 0).valid == 0) {
620 XBT_DEBUG("Block %zu not found (size used = %zu)", i,
621 heapinfo2->busy_block.busy_size);
625 if (heapinfo2->type <= 0)
628 for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo2->type); j++)
629 if (i1 == state->heaplimit
630 && heapinfo2->busy_frag.frag_size[j] > 0
631 && state->equals_to2_(i, j).valid == 0) {
632 XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)",
633 i, j, heapinfo2->busy_frag.frag_size[j]);
639 if (i1 == state->heaplimit)
640 XBT_DEBUG("Number of blocks/fragments not found in heap2 : %d", nb_diff2);
642 return nb_diff1 > 0 || nb_diff2 > 0;
648 * @param real_area1 Process address for state 1
649 * @param real_area2 Process address for state 2
650 * @param snapshot1 Snapshot of state 1
651 * @param snapshot2 Snapshot of state 2
654 * @param check_ignore
656 static int compare_heap_area_without_type(
657 simgrid::mc::StateComparator *state, int process_index,
658 const void *real_area1, const void *real_area2,
659 simgrid::mc::Snapshot* snapshot1,
660 simgrid::mc::Snapshot* snapshot2,
661 xbt_dynar_t previous, int size,
664 simgrid::mc::Process* process = &mc_model_checker->process();
665 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
666 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
668 for (int i = 0; i < size; ) {
670 if (check_ignore > 0) {
671 ssize_t ignore1 = heap_comparison_ignore_size(
672 state->processStates[0].to_ignore, (char *) real_area1 + i);
674 ssize_t ignore2 = heap_comparison_ignore_size(
675 state->processStates[1].to_ignore, (char *) real_area2 + i);
676 if (ignore2 == ignore1) {
689 if (MC_snapshot_region_memcmp(((char *) real_area1) + i, heap_region1, ((char *) real_area2) + i, heap_region2, 1) != 0) {
691 int pointer_align = (i / sizeof(void *)) * sizeof(void *);
692 const void* addr_pointed1 = snapshot1->read(
693 remote((void**)((char *) real_area1 + pointer_align)), process_index);
694 const void* addr_pointed2 = snapshot2->read(
695 remote((void**)((char *) real_area2 + pointer_align)), process_index);
697 if (process->in_maestro_stack(remote(addr_pointed1))
698 && process->in_maestro_stack(remote(addr_pointed2))) {
699 i = pointer_align + sizeof(void *);
703 if (addr_pointed1 > state->std_heap_copy.heapbase
704 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
705 && addr_pointed2 > state->std_heap_copy.heapbase
706 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)) {
707 // Both addreses are in the heap:
709 compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
710 snapshot2, previous, nullptr, 0);
711 if (res_compare == 1)
713 i = pointer_align + sizeof(void *);
729 * @param real_area1 Process address for state 1
730 * @param real_area2 Process address for state 2
731 * @param snapshot1 Snapshot of state 1
732 * @param snapshot2 Snapshot of state 2
735 * @param area_size either a byte_size or an elements_count (?)
736 * @param check_ignore
737 * @param pointer_level
738 * @return 0 (same), 1 (different), -1 (unknown)
740 static int compare_heap_area_with_type(
741 simgrid::mc::StateComparator *state, int process_index,
742 const void *real_area1, const void *real_area2,
743 simgrid::mc::Snapshot* snapshot1,
744 simgrid::mc::Snapshot* snapshot2,
745 xbt_dynar_t previous, simgrid::mc::Type* type,
746 int area_size, int check_ignore,
751 // HACK: This should not happen but in pratice, there are some
752 // DW_TAG_typedef without an associated DW_AT_type:
753 //<1><538832>: Abbrev Number: 111 (DW_TAG_typedef)
754 // <538833> DW_AT_name : (indirect string, offset: 0x2292f3): gregset_t
755 // <538837> DW_AT_decl_file : 98
756 // <538838> DW_AT_decl_line : 37
760 if (is_stack(real_area1) && is_stack(real_area2))
763 if (check_ignore > 0) {
764 ssize_t ignore1 = heap_comparison_ignore_size(
765 state->processStates[0].to_ignore, real_area1);
767 && heap_comparison_ignore_size(
768 state->processStates[1].to_ignore, real_area2) == ignore1)
772 simgrid::mc::Type *subtype, *subsubtype;
774 const void *addr_pointed1, *addr_pointed2;
776 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
777 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
779 switch (type->type) {
780 case DW_TAG_unspecified_type:
783 case DW_TAG_base_type:
784 if (!type->name.empty() && type->name == "char") { /* String, hence random (arbitrary ?) size */
785 if (real_area1 == real_area2)
788 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, area_size) != 0;
790 if (area_size != -1 && type->byte_size != area_size)
793 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
797 case DW_TAG_enumeration_type:
798 if (area_size != -1 && type->byte_size != area_size)
800 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
803 case DW_TAG_const_type:
804 case DW_TAG_volatile_type:
806 type = type->subtype;
809 case DW_TAG_array_type:
810 subtype = type->subtype;
811 switch (subtype->type) {
812 case DW_TAG_unspecified_type:
815 case DW_TAG_base_type:
816 case DW_TAG_enumeration_type:
817 case DW_TAG_pointer_type:
818 case DW_TAG_reference_type:
819 case DW_TAG_rvalue_reference_type:
820 case DW_TAG_structure_type:
821 case DW_TAG_class_type:
822 case DW_TAG_union_type:
823 if (subtype->full_type)
824 subtype = subtype->full_type;
825 elm_size = subtype->byte_size;
827 // TODO, just remove the type indirection?
828 case DW_TAG_const_type:
830 case DW_TAG_volatile_type:
831 subsubtype = subtype->subtype;
832 if (subsubtype->full_type)
833 subsubtype = subsubtype->full_type;
834 elm_size = subsubtype->byte_size;
840 for (int i = 0; i < type->element_count; i++) {
841 // TODO, add support for variable stride (DW_AT_byte_stride)
843 compare_heap_area_with_type(state, process_index,
844 (char *) real_area1 + (i * elm_size),
845 (char *) real_area2 + (i * elm_size),
846 snapshot1, snapshot2, previous,
847 type->subtype, subtype->byte_size,
848 check_ignore, pointer_level);
854 case DW_TAG_reference_type:
855 case DW_TAG_rvalue_reference_type:
856 case DW_TAG_pointer_type:
857 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
858 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
859 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
860 return (addr_pointed1 != addr_pointed2);
863 if (pointer_level <= 1) {
864 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
865 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
866 if (addr_pointed1 > state->std_heap_copy.heapbase
867 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
868 && addr_pointed2 > state->std_heap_copy.heapbase
869 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
870 return compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
871 snapshot2, previous, type->subtype,
874 return (addr_pointed1 != addr_pointed2);
876 for (size_t i = 0; i < (area_size / sizeof(void *)); i++) {
877 addr_pointed1 = snapshot1->read(
878 remote((void**)((char*) real_area1 + i * sizeof(void *))),
880 addr_pointed2 = snapshot2->read(
881 remote((void**)((char*) real_area2 + i * sizeof(void *))),
883 if (addr_pointed1 > state->std_heap_copy.heapbase
884 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
885 && addr_pointed2 > state->std_heap_copy.heapbase
886 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
888 compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
889 snapshot2, previous, type->subtype,
892 res = (addr_pointed1 != addr_pointed2);
898 case DW_TAG_structure_type:
899 case DW_TAG_class_type:
901 type = type->full_type;
902 if (area_size != -1 && type->byte_size != area_size) {
903 if (area_size <= type->byte_size || area_size % type->byte_size != 0)
905 for (size_t i = 0; i < (size_t)(area_size / type->byte_size); i++) {
906 int res = compare_heap_area_with_type(state, process_index,
907 (char *) real_area1 + i * type->byte_size,
908 (char *) real_area2 + i * type->byte_size,
909 snapshot1, snapshot2, previous, type, -1,
915 for(simgrid::mc::Member& member : type->members) {
916 // TODO, optimize this? (for the offset case)
917 void *real_member1 = simgrid::dwarf::resolve_member(
918 real_area1, type, &member, (simgrid::mc::AddressSpace*) snapshot1, process_index);
919 void *real_member2 = simgrid::dwarf::resolve_member(
920 real_area2, type, &member, (simgrid::mc::AddressSpace*) snapshot2, process_index);
921 int res = compare_heap_area_with_type(
922 state, process_index, real_member1, real_member2,
923 snapshot1, snapshot2,
924 previous, member.type, -1,
932 case DW_TAG_union_type:
933 return compare_heap_area_without_type(state, process_index, real_area1, real_area2,
934 snapshot1, snapshot2, previous,
935 type->byte_size, check_ignore);
942 xbt_die("Unreachable");
945 /** Infer the type of a part of the block from the type of the block
947 * TODO, handle DW_TAG_array_type as well as arrays of the object ((*p)[5], p[5])
949 * TODO, handle subfields ((*p).bar.foo, (*p)[5].bar…)
951 * @param type_id DWARF type ID of the root address
953 * @return DWARF type ID for given offset
955 static simgrid::mc::Type* get_offset_type(void *real_base_address, simgrid::mc::Type* type,
956 int offset, int area_size,
957 simgrid::mc::Snapshot* snapshot, int process_index)
960 // Beginning of the block, the infered variable type if the type of the block:
964 switch (type->type) {
966 case DW_TAG_structure_type:
967 case DW_TAG_class_type:
969 type = type->full_type;
970 if (area_size != -1 && type->byte_size != area_size) {
971 if (area_size > type->byte_size && area_size % type->byte_size == 0)
977 for(simgrid::mc::Member& member : type->members) {
978 if (member.has_offset_location()) {
979 // We have the offset, use it directly (shortcut):
980 if (member.offset() == offset)
983 void *real_member = simgrid::dwarf::resolve_member(
984 real_base_address, type, &member, snapshot, process_index);
985 if ((char*) real_member - (char *) real_base_address == offset)
992 /* FIXME : other cases ? */
1000 * @param area1 Process address for state 1
1001 * @param area2 Process address for state 2
1002 * @param snapshot1 Snapshot of state 1
1003 * @param snapshot2 Snapshot of state 2
1004 * @param previous Pairs of blocks already compared on the current path (or nullptr)
1005 * @param type_id Type of variable
1006 * @param pointer_level
1007 * @return 0 (same), 1 (different), -1
1010 int compare_heap_area(int process_index, const void *area1, const void *area2, simgrid::mc::Snapshot* snapshot1,
1011 simgrid::mc::Snapshot* snapshot2, xbt_dynar_t previous,
1012 simgrid::mc::Type* type, int pointer_level)
1014 simgrid::mc::Process* process = &mc_model_checker->process();
1016 simgrid::mc::StateComparator *state = mc_diff_info.get();
1019 ssize_t block1, frag1, block2, frag2;
1021 int check_ignore = 0;
1023 void *real_addr_block1, *real_addr_block2, *real_addr_frag1, *real_addr_frag2;
1025 int offset1 = 0, offset2 = 0;
1026 int new_size1 = -1, new_size2 = -1;
1027 simgrid::mc::Type *new_type1 = nullptr, *new_type2 = nullptr;
1029 int match_pairs = 0;
1031 // This is the address of std_heap->heapinfo in the application process:
1032 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
1034 const malloc_info* heapinfos1 = snapshot1->read(
1035 remote((const malloc_info**)heapinfo_address), process_index);
1036 const malloc_info* heapinfos2 = snapshot2->read(
1037 remote((const malloc_info**)heapinfo_address), process_index);
1039 malloc_info heapinfo_temp1, heapinfo_temp2;
1041 if (previous == nullptr) {
1043 xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
1046 // Get block number:
1049 (char *) state->std_heap_copy.heapbase) / BLOCKSIZE + 1;
1052 (char *) state->std_heap_copy.heapbase) / BLOCKSIZE + 1;
1054 // If either block is a stack block:
1055 if (is_block_stack((int) block1) && is_block_stack((int) block2)) {
1056 add_heap_area_pair(previous, block1, -1, block2, -1);
1058 state->match_equals(previous);
1059 xbt_dynar_free(&previous);
1064 // If either block is not in the expected area of memory:
1065 if (((char *) area1 < (char *) state->std_heap_copy.heapbase)
1066 || (block1 > (ssize_t) state->processStates[0].heapsize) || (block1 < 1)
1067 || ((char *) area2 < (char *) state->std_heap_copy.heapbase)
1068 || (block2 > (ssize_t) state->processStates[1].heapsize) || (block2 < 1)) {
1070 xbt_dynar_free(&previous);
1074 // Process address of the block:
1075 real_addr_block1 = (ADDR2UINT(block1) - 1) * BLOCKSIZE +
1076 (char *) state->std_heap_copy.heapbase;
1077 real_addr_block2 = (ADDR2UINT(block2) - 1) * BLOCKSIZE +
1078 (char *) state->std_heap_copy.heapbase;
1082 if (type->full_type)
1083 type = type->full_type;
1085 // This assume that for "boring" types (volatile ...) byte_size is absent:
1086 while (type->byte_size == 0 && type->subtype != nullptr)
1087 type = type->subtype;
1090 if (type->type == DW_TAG_pointer_type
1091 || (type->type == DW_TAG_base_type && !type->name.empty()
1092 && type->name == "char"))
1095 type_size = type->byte_size;
1099 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
1100 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
1102 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
1103 heap_region1, &heapinfo_temp1, &heapinfos1[block1], sizeof(malloc_info));
1104 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
1105 heap_region2, &heapinfo_temp2, &heapinfos2[block2], sizeof(malloc_info));
1107 if ((heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type==MMALLOC_TYPE_HEAPINFO)
1108 && (heapinfo2->type == MMALLOC_TYPE_FREE || heapinfo2->type ==MMALLOC_TYPE_HEAPINFO)) {
1111 state->match_equals(previous);
1112 xbt_dynar_free(&previous);
1117 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED
1118 && heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
1119 /* Complete block */
1121 // TODO, lookup variable type from block type as done for fragmented blocks
1123 offset1 = (char *) area1 - (char *) real_addr_block1;
1124 offset2 = (char *) area2 - (char *) real_addr_block2;
1126 if (state->equals_to1_(block1, 0).valid
1127 && state->equals_to2_(block2, 0).valid
1128 && state->blocksEqual(block1, block2)) {
1130 state->match_equals(previous);
1131 xbt_dynar_free(&previous);
1136 if (type_size != -1) {
1137 if (type_size != (ssize_t) heapinfo1->busy_block.busy_size
1138 && type_size != (ssize_t) heapinfo2->busy_block.busy_size
1139 && (type->name.empty() || type->name == "struct s_smx_context")) {
1141 state->match_equals(previous);
1142 xbt_dynar_free(&previous);
1148 if (heapinfo1->busy_block.size != heapinfo2->busy_block.size) {
1150 xbt_dynar_free(&previous);
1154 if (heapinfo1->busy_block.busy_size != heapinfo2->busy_block.busy_size) {
1156 xbt_dynar_free(&previous);
1160 if (!add_heap_area_pair(previous, block1, -1, block2, -1)) {
1162 state->match_equals(previous);
1163 xbt_dynar_free(&previous);
1168 size = heapinfo1->busy_block.busy_size;
1170 // Remember (basic) type inference.
1171 // The current data structure only allows us to do this for the whole block.
1172 if (type != nullptr && area1 == real_addr_block1)
1173 state->types1_(block1, 0) = type;
1174 if (type != nullptr && area2 == real_addr_block2)
1175 state->types2_(block2, 0) = type;
1179 state->match_equals(previous);
1180 xbt_dynar_free(&previous);
1188 if (heapinfo1->busy_block.ignore > 0
1189 && heapinfo2->busy_block.ignore == heapinfo1->busy_block.ignore)
1190 check_ignore = heapinfo1->busy_block.ignore;
1192 } else if ((heapinfo1->type > 0) && (heapinfo2->type > 0)) { /* Fragmented block */
1196 ((uintptr_t) (ADDR2UINT(area1) % (BLOCKSIZE))) >> heapinfo1->type;
1198 ((uintptr_t) (ADDR2UINT(area2) % (BLOCKSIZE))) >> heapinfo2->type;
1200 // Process address of the fragment:
1202 (void *) ((char *) real_addr_block1 +
1203 (frag1 << heapinfo1->type));
1205 (void *) ((char *) real_addr_block2 +
1206 (frag2 << heapinfo2->type));
1208 // Check the size of the fragments against the size of the type:
1209 if (type_size != -1) {
1210 if (heapinfo1->busy_frag.frag_size[frag1] == -1
1211 || heapinfo2->busy_frag.frag_size[frag2] == -1) {
1213 state->match_equals(previous);
1214 xbt_dynar_free(&previous);
1219 if (type_size != heapinfo1->busy_frag.frag_size[frag1]
1220 || type_size != heapinfo2->busy_frag.frag_size[frag2]) {
1222 state->match_equals(previous);
1223 xbt_dynar_free(&previous);
1229 // Check if the blocks are already matched together:
1230 if (state->equals_to1_(block1, frag1).valid
1231 && state->equals_to2_(block2, frag2).valid) {
1232 if (offset1==offset2 && state->fragmentsEqual(block1, frag1, block2, frag2)) {
1234 state->match_equals(previous);
1235 xbt_dynar_free(&previous);
1240 // Compare the size of both fragments:
1241 if (heapinfo1->busy_frag.frag_size[frag1] !=
1242 heapinfo2->busy_frag.frag_size[frag2]) {
1243 if (type_size == -1) {
1245 state->match_equals(previous);
1246 xbt_dynar_free(&previous);
1251 xbt_dynar_free(&previous);
1256 // Size of the fragment:
1257 size = heapinfo1->busy_frag.frag_size[frag1];
1259 // Remember (basic) type inference.
1260 // The current data structure only allows us to do this for the whole fragment.
1261 if (type != nullptr && area1 == real_addr_frag1)
1262 state->types1_(block1, frag1) = type;
1263 if (type != nullptr && area2 == real_addr_frag2)
1264 state->types2_(block2, frag2) = type;
1266 // The type of the variable is already known:
1271 // Type inference from the block type.
1272 else if (state->types1_(block1, frag1) != nullptr
1273 || state->types2_(block2, frag2) != nullptr) {
1275 offset1 = (char *) area1 - (char *) real_addr_frag1;
1276 offset2 = (char *) area2 - (char *) real_addr_frag2;
1278 if (state->types1_(block1, frag1) != nullptr
1279 && state->types2_(block2, frag2) != nullptr) {
1281 get_offset_type(real_addr_frag1, state->types1_(block1, frag1),
1282 offset1, size, snapshot1, process_index);
1284 get_offset_type(real_addr_frag2, state->types2_(block2, frag2),
1285 offset1, size, snapshot2, process_index);
1286 } else if (state->types1_(block1, frag1) != nullptr) {
1288 get_offset_type(real_addr_frag1, state->types1_(block1, frag1),
1289 offset1, size, snapshot1, process_index);
1291 get_offset_type(real_addr_frag2, state->types1_(block1, frag1),
1292 offset2, size, snapshot2, process_index);
1293 } else if (state->types2_(block2, frag2) != nullptr) {
1295 get_offset_type(real_addr_frag1, state->types2_(block2, frag2),
1296 offset1, size, snapshot1, process_index);
1298 get_offset_type(real_addr_frag2, state->types2_(block2, frag2),
1299 offset2, size, snapshot2, process_index);
1302 state->match_equals(previous);
1303 xbt_dynar_free(&previous);
1308 if (new_type1 != nullptr && new_type2 != nullptr && new_type1 != new_type2) {
1311 while (type->byte_size == 0 && type->subtype != nullptr)
1312 type = type->subtype;
1313 new_size1 = type->byte_size;
1316 while (type->byte_size == 0 && type->subtype != nullptr)
1317 type = type->subtype;
1318 new_size2 = type->byte_size;
1322 state->match_equals(previous);
1323 xbt_dynar_free(&previous);
1329 if (new_size1 > 0 && new_size1 == new_size2) {
1334 if (offset1 == 0 && offset2 == 0
1335 && !add_heap_area_pair(previous, block1, frag1, block2, frag2)) {
1337 state->match_equals(previous);
1338 xbt_dynar_free(&previous);
1345 state->match_equals(previous);
1346 xbt_dynar_free(&previous);
1351 if ((heapinfo1->busy_frag.ignore[frag1] > 0)
1352 && (heapinfo2->busy_frag.ignore[frag2] ==
1353 heapinfo1->busy_frag.ignore[frag1]))
1354 check_ignore = heapinfo1->busy_frag.ignore[frag1];
1358 xbt_dynar_free(&previous);
1363 /* Start comparison */
1366 compare_heap_area_with_type(state, process_index, area1, area2, snapshot1, snapshot2,
1367 previous, type, size, check_ignore,
1371 compare_heap_area_without_type(state, process_index, area1, area2, snapshot1, snapshot2,
1372 previous, size, check_ignore);
1374 if (res_compare == 1) {
1376 xbt_dynar_free(&previous);
1381 state->match_equals(previous);
1382 xbt_dynar_free(&previous);
1394 /** A hash which works with more stuff
1396 * It can hash pairs: the standard hash currently doesn't include this.
1398 template<class X> struct hash : public std::hash<X> {};
1400 template<class X, class Y>
1401 struct hash<std::pair<X,Y>> {
1402 std::size_t operator()(std::pair<X,Y>const& x) const
1406 return h1(x.first) ^ h2(x.second);
1410 struct ComparisonState {
1411 std::unordered_set<std::pair<void*, void*>, hash<std::pair<void*, void*>>> compared_pointers;
1417 using simgrid::mc::ComparisonState;
1419 /************************** Snapshot comparison *******************************/
1420 /******************************************************************************/
1422 static int compare_areas_with_type(ComparisonState& state,
1424 void* real_area1, simgrid::mc::Snapshot* snapshot1, mc_mem_region_t region1,
1425 void* real_area2, simgrid::mc::Snapshot* snapshot2, mc_mem_region_t region2,
1426 simgrid::mc::Type* type, int pointer_level)
1428 simgrid::mc::Process* process = &mc_model_checker->process();
1430 simgrid::mc::Type* subtype;
1431 simgrid::mc::Type* subsubtype;
1432 int elm_size, i, res;
1435 switch (type->type) {
1436 case DW_TAG_unspecified_type:
1439 case DW_TAG_base_type:
1440 case DW_TAG_enumeration_type:
1441 case DW_TAG_union_type:
1443 return MC_snapshot_region_memcmp(
1444 real_area1, region1, real_area2, region2,
1445 type->byte_size) != 0;
1447 case DW_TAG_typedef:
1448 case DW_TAG_volatile_type:
1449 case DW_TAG_const_type:
1451 type = type->subtype;
1453 case DW_TAG_array_type:
1454 subtype = type->subtype;
1455 switch (subtype->type) {
1456 case DW_TAG_unspecified_type:
1459 case DW_TAG_base_type:
1460 case DW_TAG_enumeration_type:
1461 case DW_TAG_pointer_type:
1462 case DW_TAG_reference_type:
1463 case DW_TAG_rvalue_reference_type:
1464 case DW_TAG_structure_type:
1465 case DW_TAG_class_type:
1466 case DW_TAG_union_type:
1467 if (subtype->full_type)
1468 subtype = subtype->full_type;
1469 elm_size = subtype->byte_size;
1471 case DW_TAG_const_type:
1472 case DW_TAG_typedef:
1473 case DW_TAG_volatile_type:
1474 subsubtype = subtype->subtype;
1475 if (subsubtype->full_type)
1476 subsubtype = subsubtype->full_type;
1477 elm_size = subsubtype->byte_size;
1483 for (i = 0; i < type->element_count; i++) {
1484 size_t off = i * elm_size;
1485 res = compare_areas_with_type(state, process_index,
1486 (char*) real_area1 + off, snapshot1, region1,
1487 (char*) real_area2 + off, snapshot2, region2,
1488 type->subtype, pointer_level);
1493 case DW_TAG_pointer_type:
1494 case DW_TAG_reference_type:
1495 case DW_TAG_rvalue_reference_type:
1497 void* addr_pointed1 = MC_region_read_pointer(region1, real_area1);
1498 void* addr_pointed2 = MC_region_read_pointer(region2, real_area2);
1500 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type)
1501 return (addr_pointed1 != addr_pointed2);
1502 if (addr_pointed1 == nullptr && addr_pointed2 == nullptr)
1504 if (addr_pointed1 == nullptr || addr_pointed2 == nullptr)
1506 if (!state.compared_pointers.insert(
1507 std::make_pair(addr_pointed1, addr_pointed2)).second)
1512 // Some cases are not handled here:
1513 // * the pointers lead to different areas (one to the heap, the other to the RW segment ...);
1514 // * a pointer leads to the read-only segment of the current object;
1515 // * a pointer lead to a different ELF object.
1517 if (addr_pointed1 > process->heap_address
1518 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)) {
1520 (addr_pointed2 > process->heap_address
1521 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)))
1523 // The pointers are both in the heap:
1524 return simgrid::mc::compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
1525 snapshot2, nullptr, type->subtype, pointer_level);
1528 // The pointers are both in the current object R/W segment:
1529 else if (region1->contain(simgrid::mc::remote(addr_pointed1))) {
1530 if (!region2->contain(simgrid::mc::remote(addr_pointed2)))
1533 return (addr_pointed1 != addr_pointed2);
1535 return compare_areas_with_type(state, process_index,
1536 addr_pointed1, snapshot1, region1,
1537 addr_pointed2, snapshot2, region2,
1538 type->subtype, pointer_level);
1541 // TODO, We do not handle very well the case where
1542 // it belongs to a different (non-heap) region from the current one.
1545 return (addr_pointed1 != addr_pointed2);
1549 case DW_TAG_structure_type:
1550 case DW_TAG_class_type:
1551 for(simgrid::mc::Member& member : type->members) {
1552 void *member1 = simgrid::dwarf::resolve_member(
1553 real_area1, type, &member, snapshot1, process_index);
1554 void *member2 = simgrid::dwarf::resolve_member(
1555 real_area2, type, &member, snapshot2, process_index);
1556 mc_mem_region_t subregion1 = mc_get_region_hinted(member1, snapshot1, process_index, region1);
1557 mc_mem_region_t subregion2 = mc_get_region_hinted(member2, snapshot2, process_index, region2);
1559 compare_areas_with_type(state, process_index,
1560 member1, snapshot1, subregion1,
1561 member2, snapshot2, subregion2,
1562 member.type, pointer_level);
1567 case DW_TAG_subroutine_type:
1571 XBT_VERB("Unknown case : %d", type->type);
1578 static int compare_global_variables(simgrid::mc::ObjectInformation* object_info,
1581 mc_mem_region_t r2, simgrid::mc::Snapshot* snapshot1,
1582 simgrid::mc::Snapshot* snapshot2)
1584 xbt_assert(r1 && r2, "Missing region.");
1587 if (r1->storage_type() == simgrid::mc::StorageType::Privatized) {
1588 xbt_assert(process_index >= 0);
1589 if (r2->storage_type() != simgrid::mc::StorageType::Privatized)
1592 size_t process_count = MC_smpi_process_count();
1593 xbt_assert(process_count == r1->privatized_data().size()
1594 && process_count == r2->privatized_data().size());
1596 // Compare the global variables separately for each simulates process:
1597 for (size_t process_index = 0; process_index < process_count; process_index++) {
1598 int is_diff = compare_global_variables(object_info, process_index,
1599 &r1->privatized_data()[process_index],
1600 &r2->privatized_data()[process_index],
1601 snapshot1, snapshot2);
1602 if (is_diff) return 1;
1607 xbt_assert(r1->storage_type() != simgrid::mc::StorageType::Privatized);
1609 xbt_assert(r2->storage_type() != simgrid::mc::StorageType::Privatized);
1611 ComparisonState state;
1613 std::vector<simgrid::mc::Variable>& variables = object_info->global_variables;
1615 for (simgrid::mc::Variable& current_var : variables) {
1617 // If the variable is not in this object, skip it:
1618 // We do not expect to find a pointer to something which is not reachable
1619 // by the global variables.
1620 if ((char *) current_var.address < (char *) object_info->start_rw
1621 || (char *) current_var.address > (char *) object_info->end_rw)
1624 simgrid::mc::Type* bvariable_type = current_var.type;
1626 compare_areas_with_type(state, process_index,
1627 (char *) current_var.address, snapshot1, r1,
1628 (char *) current_var.address, snapshot2, r2,
1631 XBT_VERB("Global variable %s (%p) is different between snapshots",
1632 current_var.name.c_str(),
1633 (char *) current_var.address);
1643 static int compare_local_variables(int process_index,
1644 simgrid::mc::Snapshot* snapshot1,
1645 simgrid::mc::Snapshot* snapshot2,
1646 mc_snapshot_stack_t stack1,
1647 mc_snapshot_stack_t stack2)
1649 ComparisonState state;
1651 if (stack1->local_variables.size() != stack2->local_variables.size()) {
1652 XBT_VERB("Different number of local variables");
1656 unsigned int cursor = 0;
1657 local_variable_t current_var1, current_var2;
1659 while (cursor < stack1->local_variables.size()) {
1660 current_var1 = &stack1->local_variables[cursor];
1661 current_var2 = &stack1->local_variables[cursor];
1662 if (current_var1->name != current_var2->name
1663 || current_var1->subprogram != current_var2->subprogram
1664 || current_var1->ip != current_var2->ip) {
1665 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1667 ("Different name of variable (%s - %s) "
1668 "or frame (%s - %s) or ip (%lu - %lu)",
1669 current_var1->name.c_str(),
1670 current_var2->name.c_str(),
1671 current_var1->subprogram->name.c_str(),
1672 current_var2->subprogram->name.c_str(),
1673 current_var1->ip, current_var2->ip);
1676 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1678 simgrid::mc::Type* subtype = current_var1->type;
1680 compare_areas_with_type(state, process_index,
1681 current_var1->address, snapshot1, mc_get_snapshot_region(current_var1->address, snapshot1, process_index),
1682 current_var2->address, snapshot2, mc_get_snapshot_region(current_var2->address, snapshot2, process_index),
1686 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1688 ("Local variable %s (%p - %p) in frame %s "
1689 "is different between snapshots",
1690 current_var1->name.c_str(),
1691 current_var1->address,
1692 current_var2->address,
1693 current_var1->subprogram->name.c_str());
1704 int snapshot_compare(int num1, simgrid::mc::Snapshot* s1, int num2, simgrid::mc::Snapshot* s2)
1706 simgrid::mc::Process* process = &mc_model_checker->process();
1711 int hash_result = 0;
1713 hash_result = (s1->hash != s2->hash);
1715 XBT_VERB("(%d - %d) Different hash : 0x%" PRIx64 "--0x%" PRIx64, num1,
1716 num2, s1->hash, s2->hash);
1721 XBT_VERB("(%d - %d) Same hash : 0x%" PRIx64, num1, num2, s1->hash);
1724 /* Compare enabled processes */
1725 if (s1->enabled_processes != s2->enabled_processes) {
1726 XBT_VERB("(%d - %d) Different enabled processes", num1, num2);
1730 unsigned long i = 0;
1731 size_t size_used1, size_used2;
1734 /* Compare size of stacks */
1735 while (i < s1->stacks.size()) {
1736 size_used1 = s1->stack_sizes[i];
1737 size_used2 = s2->stack_sizes[i];
1738 if (size_used1 != size_used2) {
1740 XBT_DEBUG("(%d - %d) Different size used in stacks : %zu - %zu", num1,
1741 num2, size_used1, size_used2);
1746 XBT_VERB("(%d - %d) Different size used in stacks : %zu - %zu", num1,
1747 num2, size_used1, size_used2);
1755 /* Init heap information used in heap comparison algorithm */
1756 xbt_mheap_t heap1 = (xbt_mheap_t)s1->read_bytes(
1757 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1758 remote(process->heap_address),
1759 simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1760 xbt_mheap_t heap2 = (xbt_mheap_t)s2->read_bytes(
1761 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1762 remote(process->heap_address),
1763 simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1764 res_init = simgrid::mc::init_heap_information(heap1, heap2, &s1->to_ignore, &s2->to_ignore);
1765 if (res_init == -1) {
1767 XBT_DEBUG("(%d - %d) Different heap information", num1, num2);
1771 XBT_VERB("(%d - %d) Different heap information", num1, num2);
1778 /* Stacks comparison */
1779 unsigned cursor = 0;
1784 mc_snapshot_stack_t stack1, stack2;
1785 while (cursor < s1->stacks.size()) {
1786 stack1 = &s1->stacks[cursor];
1787 stack2 = &s2->stacks[cursor];
1789 if (stack1->process_index != stack2->process_index) {
1791 XBT_DEBUG("(%d - %d) Stacks with different process index (%i vs %i)", num1, num2,
1792 stack1->process_index, stack2->process_index);
1795 compare_local_variables(stack1->process_index, s1, s2, stack1, stack2);
1796 if (diff_local > 0) {
1798 XBT_DEBUG("(%d - %d) Different local variables between stacks %d", num1,
1805 XBT_VERB("(%d - %d) Different local variables between stacks %d", num1,
1809 simgrid::mc::reset_heap_information();
1817 size_t regions_count = s1->snapshot_regions.size();
1818 // TODO, raise a difference instead?
1819 xbt_assert(regions_count == s2->snapshot_regions.size());
1821 for (size_t k = 0; k != regions_count; ++k) {
1822 mc_mem_region_t region1 = s1->snapshot_regions[k].get();
1823 mc_mem_region_t region2 = s2->snapshot_regions[k].get();
1826 if (region1->region_type() != simgrid::mc::RegionType::Data)
1829 xbt_assert(region1->region_type() == region2->region_type());
1830 xbt_assert(region1->object_info() == region2->object_info());
1831 xbt_assert(region1->object_info());
1833 std::string const& name = region1->object_info()->file_name;
1835 /* Compare global variables */
1837 compare_global_variables(region1->object_info(),
1838 simgrid::mc::ProcessIndexDisabled,
1844 XBT_DEBUG("(%d - %d) Different global variables in %s",
1845 num1, num2, name.c_str());
1849 XBT_VERB("(%d - %d) Different global variables in %s",
1850 num1, num2, name.c_str());
1859 if (simgrid::mc::mmalloc_compare_heap(s1, s2) > 0) {
1862 XBT_DEBUG("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
1867 XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
1874 simgrid::mc::reset_heap_information();
1877 if (errors || hash_result)
1878 XBT_VERB("(%d - %d) Difference found", num1, num2);
1880 XBT_VERB("(%d - %d) No difference found", num1, num2);
1883 #if defined(MC_DEBUG) && defined(MC_VERBOSE)
1885 // * false positive SHOULD be avoided.
1886 // * There MUST not be any false negative.
1888 XBT_VERB("(%d - %d) State equality hash test is %s %s", num1, num2,
1889 (hash_result != 0) == (errors != 0) ? "true" : "false",
1890 !hash_result ? "positive" : "negative");
1894 return errors > 0 || hash_result;