1 /* Copyright (c) 2008-2015. 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 /* mc_diff - Memory snapshooting and comparison */
13 #include "src/xbt/ex_interface.h" /* internals of backtrace setup */
15 #include "xbt/mmalloc.h"
16 #include "mc/datatypes.h"
17 #include "src/mc/malloc.hpp"
18 #include "src/mc/mc_private.h"
19 #include "src/mc/mc_snapshot.h"
20 #include "src/mc/mc_dwarf.hpp"
21 #include "src/mc/Type.hpp"
23 #include <xbt/dynar.h>
25 using simgrid::mc::remote;
27 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_diff, xbt,
28 "Logging specific to mc_diff in mc");
30 /*********************************** Heap comparison ***********************************/
31 /***************************************************************************************/
36 struct ProcessComparisonState {
37 std::vector<simgrid::mc::IgnoredHeapRegion>* to_ignore = nullptr;
38 std::vector<s_heap_area_t> equals_to;
39 std::vector<simgrid::mc::Type*> types;
40 std::size_t heapsize = 0;
42 void initHeapInformation(xbt_mheap_t heap,
43 std::vector<simgrid::mc::IgnoredHeapRegion>* i);
46 struct StateComparator {
47 s_xbt_mheap_t std_heap_copy;
48 std::size_t heaplimit;
49 std::array<ProcessComparisonState, 2> processStates;
51 int initHeapInformation(
52 xbt_mheap_t heap1, xbt_mheap_t heap2,
53 std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
54 std::vector<simgrid::mc::IgnoredHeapRegion>* i2);
56 s_heap_area_t& equals_to1_(std::size_t i, std::size_t j)
58 return processStates[0].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
60 s_heap_area_t& equals_to2_(std::size_t i, std::size_t j)
62 return processStates[1].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
64 Type*& types1_(std::size_t i, std::size_t j)
66 return processStates[0].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
68 Type*& types2_(std::size_t i, std::size_t j)
70 return processStates[1].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
73 s_heap_area_t const& equals_to1_(std::size_t i, std::size_t j) const
75 return processStates[0].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
77 s_heap_area_t const& equals_to2_(std::size_t i, std::size_t j) const
79 return processStates[1].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
81 Type* const& types1_(std::size_t i, std::size_t j) const
83 return processStates[0].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
85 Type* const& types2_(std::size_t i, std::size_t j) const
87 return processStates[1].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
90 /** Check whether two blocks are known to be matching
92 * @param state State used
93 * @param b1 Block of state 1
94 * @param b2 Block of state 2
95 * @return if the blocks are known to be matching
97 bool blocksEqual(int b1, int b2) const
99 return this->equals_to1_(b1, 0).block == b2
100 && this->equals_to2_(b2, 0).block == b1;
103 /** Check whether two fragments are known to be matching
105 * @param state State used
106 * @param b1 Block of state 1
107 * @param f1 Fragment of state 1
108 * @param b2 Block of state 2
109 * @param f2 Fragment of state 2
110 * @return if the fragments are known to be matching
112 int fragmentsEqual(int b1, int f1, int b2, int f2) const
114 return this->equals_to1_(b1, f1).block == b2
115 && this->equals_to1_(b1, f1).fragment == f2
116 && this->equals_to2_(b2, f2).block == b1
117 && this->equals_to2_(b2, f2).fragment == f1;
120 void match_equals(xbt_dynar_t list);
126 // TODO, make this a field of ModelChecker or something similar
127 static std::unique_ptr<simgrid::mc::StateComparator> mc_diff_info;
129 /*********************************** Free functions ************************************/
131 static void heap_area_pair_free(heap_area_pair_t pair)
137 static void heap_area_pair_free_voidp(void *d)
139 heap_area_pair_free((heap_area_pair_t) * (void **) d);
142 static void heap_area_free(heap_area_t area)
148 /************************************************************************************/
150 static s_heap_area_t make_heap_area(int block, int fragment)
155 area.fragment = fragment;
160 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1,
161 int block2, int fragment2)
164 unsigned int cursor = 0;
165 heap_area_pair_t current_pair;
167 xbt_dynar_foreach(list, cursor, current_pair)
168 if (current_pair->block1 == block1 && current_pair->block2 == block2
169 && current_pair->fragment1 == fragment1
170 && current_pair->fragment2 == fragment2)
176 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1,
177 int block2, int fragment2)
180 if (!is_new_heap_area_pair(list, block1, fragment1, block2, fragment2))
183 heap_area_pair_t pair = nullptr;
184 pair = xbt_new0(s_heap_area_pair_t, 1);
185 pair->block1 = block1;
186 pair->fragment1 = fragment1;
187 pair->block2 = block2;
188 pair->fragment2 = fragment2;
189 xbt_dynar_push(list, &pair);
193 static ssize_t heap_comparison_ignore_size(
194 std::vector<simgrid::mc::IgnoredHeapRegion>* ignore_list,
198 int end = ignore_list->size() - 1;
200 while (start <= end) {
201 unsigned int cursor = (start + end) / 2;
202 simgrid::mc::IgnoredHeapRegion const& region = (*ignore_list)[cursor];
203 if (region.address == address)
205 if (region.address < address)
207 if (region.address > address)
214 static bool is_stack(const void *address)
216 for (auto const& stack : mc_model_checker->process().stack_areas())
217 if (address == stack.address)
222 // TODO, this should depend on the snapshot?
223 static bool is_block_stack(int block)
225 for (auto const& stack : mc_model_checker->process().stack_areas())
226 if (block == stack.block)
234 void StateComparator::match_equals(xbt_dynar_t list)
236 unsigned int cursor = 0;
237 heap_area_pair_t current_pair;
239 xbt_dynar_foreach(list, cursor, current_pair) {
240 if (current_pair->fragment1 != -1) {
241 this->equals_to1_(current_pair->block1, current_pair->fragment1) =
242 make_heap_area(current_pair->block2, current_pair->fragment2);
243 this->equals_to2_(current_pair->block2, current_pair->fragment2) =
244 make_heap_area(current_pair->block1, current_pair->fragment1);
246 this->equals_to1_(current_pair->block1, 0) =
247 make_heap_area(current_pair->block2, current_pair->fragment2);
248 this->equals_to2_(current_pair->block2, 0) =
249 make_heap_area(current_pair->block1, current_pair->fragment1);
254 int init_heap_information(xbt_mheap_t heap1, xbt_mheap_t heap2,
255 std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
256 std::vector<simgrid::mc::IgnoredHeapRegion>* i2)
258 if (mc_diff_info == nullptr)
259 mc_diff_info = std::unique_ptr<StateComparator>(new StateComparator());
260 return mc_diff_info->initHeapInformation(heap1, heap2, i1, i2);
263 void ProcessComparisonState::initHeapInformation(xbt_mheap_t heap,
264 std::vector<simgrid::mc::IgnoredHeapRegion>* i)
266 auto heaplimit = ((struct mdesc *) heap)->heaplimit;
267 this->heapsize = ((struct mdesc *) heap)->heapsize;
269 this->equals_to.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, s_heap_area {0, 0, 0});
270 this->types.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, nullptr);
273 int StateComparator::initHeapInformation(xbt_mheap_t heap1, xbt_mheap_t heap2,
274 std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
275 std::vector<simgrid::mc::IgnoredHeapRegion>* i2)
277 if ((((struct mdesc *) heap1)->heaplimit !=
278 ((struct mdesc *) heap2)->heaplimit)
280 ((((struct mdesc *) heap1)->heapsize !=
281 ((struct mdesc *) heap2)->heapsize)))
283 this->heaplimit = ((struct mdesc *) heap1)->heaplimit;
284 this->std_heap_copy = *mc_model_checker->process().get_heap();
285 this->processStates[0].initHeapInformation(heap1, i1);
286 this->processStates[1].initHeapInformation(heap2, i2);
290 void reset_heap_information()
295 // TODO, have a robust way to find it in O(1)
297 mc_mem_region_t MC_get_heap_region(simgrid::mc::Snapshot* snapshot)
299 for (auto& region : snapshot->snapshot_regions)
300 if (region->region_type() == simgrid::mc::RegionType::Heap)
302 xbt_die("No heap region");
305 int mmalloc_compare_heap(simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2)
307 simgrid::mc::Process* process = &mc_model_checker->process();
308 simgrid::mc::StateComparator *state = mc_diff_info.get();
310 /* Start comparison */
311 size_t i1, i2, j1, j2, k;
312 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
313 int nb_diff1 = 0, nb_diff2 = 0;
315 int equal, res_compare = 0;
317 /* Check busy blocks */
321 malloc_info heapinfo_temp1, heapinfo_temp2;
322 malloc_info heapinfo_temp2b;
324 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
325 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
327 // This is the address of std_heap->heapinfo in the application process:
328 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
330 // This is in snapshot do not use them directly:
331 const malloc_info* heapinfos1 = snapshot1->read<malloc_info*>(
332 (std::uint64_t)heapinfo_address, simgrid::mc::ProcessIndexMissing);
333 const malloc_info* heapinfos2 = snapshot2->read<malloc_info*>(
334 (std::uint64_t)heapinfo_address, simgrid::mc::ProcessIndexMissing);
336 while (i1 < state->heaplimit) {
338 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(heap_region1, &heapinfo_temp1, &heapinfos1[i1], sizeof(malloc_info));
339 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2, &heapinfos2[i1], sizeof(malloc_info));
341 if (heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type == MMALLOC_TYPE_HEAPINFO) { /* Free block */
346 if (heapinfo1->type < 0) {
347 fprintf(stderr, "Unkown mmalloc block type.\n");
352 ((void *) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE +
353 (char *) state->std_heap_copy.heapbase));
355 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED) { /* Large block */
357 if (is_stack(addr_block1)) {
358 for (k = 0; k < heapinfo1->busy_block.size; k++)
359 state->equals_to1_(i1 + k, 0) = make_heap_area(i1, -1);
360 for (k = 0; k < heapinfo2->busy_block.size; k++)
361 state->equals_to2_(i1 + k, 0) = make_heap_area(i1, -1);
362 i1 += heapinfo1->busy_block.size;
366 if (state->equals_to1_(i1, 0).valid) {
375 /* Try first to associate to same block in the other heap */
376 if (heapinfo2->type == heapinfo1->type
377 && state->equals_to2_(i1, 0).valid == 0) {
378 addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE +
379 (char *) state->std_heap_copy.heapbase;
381 compare_heap_area(simgrid::mc::ProcessIndexMissing, addr_block1, addr_block2, snapshot1, snapshot2,
382 nullptr, nullptr, 0);
383 if (res_compare != 1) {
384 for (k = 1; k < heapinfo2->busy_block.size; k++)
385 state->equals_to2_(i1 + k, 0) = make_heap_area(i1, -1);
386 for (k = 1; k < heapinfo1->busy_block.size; k++)
387 state->equals_to1_(i1 + k, 0) = make_heap_area(i1, -1);
389 i1 += heapinfo1->busy_block.size;
393 while (i2 < state->heaplimit && !equal) {
395 addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE +
396 (char *) state->std_heap_copy.heapbase;
403 const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2b, &heapinfos2[i2], sizeof(malloc_info));
405 if (heapinfo2b->type != MMALLOC_TYPE_UNFRAGMENTED) {
410 if (state->equals_to2_(i2, 0).valid) {
416 compare_heap_area(simgrid::mc::ProcessIndexMissing, addr_block1, addr_block2, snapshot1, snapshot2,
417 nullptr, nullptr, 0);
419 if (res_compare != 1) {
420 for (k = 1; k < heapinfo2b->busy_block.size; k++)
421 state->equals_to2_(i2 + k, 0) = make_heap_area(i1, -1);
422 for (k = 1; k < heapinfo1->busy_block.size; k++)
423 state->equals_to1_(i1 + k, 0) = make_heap_area(i2, -1);
425 i1 += heapinfo1->busy_block.size;
433 XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1,
434 heapinfo1->busy_block.busy_size, addr_block1);
435 i1 = state->heaplimit + 1;
440 } else { /* Fragmented block */
442 for (j1 = 0; j1 < (size_t) (BLOCKSIZE >> heapinfo1->type); j1++) {
444 if (heapinfo1->busy_frag.frag_size[j1] == -1) /* Free fragment */
447 if (state->equals_to1_(i1, j1).valid)
451 (void *) ((char *) addr_block1 + (j1 << heapinfo1->type));
456 /* Try first to associate to same fragment in the other heap */
457 if (heapinfo2->type == heapinfo1->type
458 && state->equals_to2_(i1, j1).valid == 0) {
459 addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE +
460 (char *) state->std_heap_copy.heapbase;
462 (void *) ((char *) addr_block2 +
463 (j1 << heapinfo2->type));
465 compare_heap_area(simgrid::mc::ProcessIndexMissing, addr_frag1, addr_frag2, snapshot1, snapshot2,
466 nullptr, nullptr, 0);
467 if (res_compare != 1)
473 while (i2 < state->heaplimit && !equal) {
475 const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(
476 heap_region2, &heapinfo_temp2b, &heapinfos2[i2],
477 sizeof(malloc_info));
479 if (heapinfo2b->type == MMALLOC_TYPE_FREE || heapinfo2b->type == MMALLOC_TYPE_HEAPINFO) {
484 // We currently do not match fragments with unfragmented blocks (maybe we should).
485 if (heapinfo2b->type == MMALLOC_TYPE_UNFRAGMENTED) {
490 if (heapinfo2b->type < 0) {
491 fprintf(stderr, "Unkown mmalloc block type.\n");
495 for (j2 = 0; j2 < (size_t) (BLOCKSIZE >> heapinfo2b->type);
498 if (i2 == i1 && j2 == j1)
501 if (state->equals_to2_(i2, j2).valid)
504 addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE +
505 (char *) state->std_heap_copy.heapbase;
507 (void *) ((char *) addr_block2 +
508 (j2 << heapinfo2b->type));
511 compare_heap_area(simgrid::mc::ProcessIndexMissing, addr_frag1, addr_frag2, snapshot2, snapshot2,
512 nullptr, nullptr, 0);
514 if (res_compare != 1) {
527 ("Block %zu, fragment %zu not found (size_used = %zd, address = %p)\n",
528 i1, j1, heapinfo1->busy_frag.frag_size[j1],
530 i2 = state->heaplimit + 1;
531 i1 = state->heaplimit + 1;
544 /* All blocks/fragments are equal to another block/fragment ? */
547 for(i = 1; i < state->heaplimit; i++) {
548 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
549 heap_region1, &heapinfo_temp1, &heapinfos1[i], sizeof(malloc_info));
551 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED
552 && i1 == state->heaplimit
553 && heapinfo1->busy_block.busy_size > 0
554 && state->equals_to1_(i, 0).valid == 0) {
555 XBT_DEBUG("Block %zu not found (size used = %zu)", i,
556 heapinfo1->busy_block.busy_size);
560 if (heapinfo1->type <= 0)
562 for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo1->type); j++)
563 if (i1 == state->heaplimit
564 && heapinfo1->busy_frag.frag_size[j] > 0
565 && state->equals_to1_(i, j).valid == 0) {
566 XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)",
567 i, j, heapinfo1->busy_frag.frag_size[j]);
572 if (i1 == state->heaplimit)
573 XBT_DEBUG("Number of blocks/fragments not found in heap1 : %d", nb_diff1);
575 for (i=1; i < state->heaplimit; i++) {
576 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
577 heap_region2, &heapinfo_temp2, &heapinfos2[i], sizeof(malloc_info));
578 if (heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED
579 && i1 == state->heaplimit
580 && heapinfo2->busy_block.busy_size > 0
581 && state->equals_to2_(i, 0).valid == 0) {
582 XBT_DEBUG("Block %zu not found (size used = %zu)", i,
583 heapinfo2->busy_block.busy_size);
587 if (heapinfo2->type <= 0)
590 for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo2->type); j++)
591 if (i1 == state->heaplimit
592 && heapinfo2->busy_frag.frag_size[j] > 0
593 && state->equals_to2_(i, j).valid == 0) {
594 XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)",
595 i, j, heapinfo2->busy_frag.frag_size[j]);
601 if (i1 == state->heaplimit)
602 XBT_DEBUG("Number of blocks/fragments not found in heap2 : %d", nb_diff2);
604 return nb_diff1 > 0 || nb_diff2 > 0;
610 * @param real_area1 Process address for state 1
611 * @param real_area2 Process address for state 2
612 * @param snapshot1 Snapshot of state 1
613 * @param snapshot2 Snapshot of state 2
616 * @param check_ignore
618 static int compare_heap_area_without_type(
619 simgrid::mc::StateComparator *state, int process_index,
620 const void *real_area1, const void *real_area2,
621 simgrid::mc::Snapshot* snapshot1,
622 simgrid::mc::Snapshot* snapshot2,
623 xbt_dynar_t previous, int size,
626 simgrid::mc::Process* process = &mc_model_checker->process();
627 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
628 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
630 for (int i = 0; i < size; ) {
632 if (check_ignore > 0) {
633 ssize_t ignore1 = heap_comparison_ignore_size(
634 state->processStates[0].to_ignore, (char *) real_area1 + i);
636 ssize_t ignore2 = heap_comparison_ignore_size(
637 state->processStates[1].to_ignore, (char *) real_area2 + i);
638 if (ignore2 == ignore1) {
651 if (MC_snapshot_region_memcmp(((char *) real_area1) + i, heap_region1, ((char *) real_area2) + i, heap_region2, 1) != 0) {
653 int pointer_align = (i / sizeof(void *)) * sizeof(void *);
654 const void* addr_pointed1 = snapshot1->read(
655 remote((void**)((char *) real_area1 + pointer_align)), process_index);
656 const void* addr_pointed2 = snapshot2->read(
657 remote((void**)((char *) real_area2 + pointer_align)), process_index);
659 if (process->in_maestro_stack(remote(addr_pointed1))
660 && process->in_maestro_stack(remote(addr_pointed2))) {
661 i = pointer_align + sizeof(void *);
665 if (addr_pointed1 > state->std_heap_copy.heapbase
666 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
667 && addr_pointed2 > state->std_heap_copy.heapbase
668 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)) {
669 // Both addreses are in the heap:
671 compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
672 snapshot2, previous, nullptr, 0);
673 if (res_compare == 1)
675 i = pointer_align + sizeof(void *);
691 * @param real_area1 Process address for state 1
692 * @param real_area2 Process address for state 2
693 * @param snapshot1 Snapshot of state 1
694 * @param snapshot2 Snapshot of state 2
697 * @param area_size either a byte_size or an elements_count (?)
698 * @param check_ignore
699 * @param pointer_level
700 * @return 0 (same), 1 (different), -1 (unknown)
702 static int compare_heap_area_with_type(
703 simgrid::mc::StateComparator *state, int process_index,
704 const void *real_area1, const void *real_area2,
705 simgrid::mc::Snapshot* snapshot1,
706 simgrid::mc::Snapshot* snapshot2,
707 xbt_dynar_t previous, simgrid::mc::Type* type,
708 int area_size, int check_ignore,
713 // HACK: This should not happen but in pratice, there are some
714 // DW_TAG_typedef without an associated DW_AT_type:
715 //<1><538832>: Abbrev Number: 111 (DW_TAG_typedef)
716 // <538833> DW_AT_name : (indirect string, offset: 0x2292f3): gregset_t
717 // <538837> DW_AT_decl_file : 98
718 // <538838> DW_AT_decl_line : 37
722 if (is_stack(real_area1) && is_stack(real_area2))
725 if (check_ignore > 0) {
726 ssize_t ignore1 = heap_comparison_ignore_size(
727 state->processStates[0].to_ignore, real_area1);
729 && heap_comparison_ignore_size(
730 state->processStates[1].to_ignore, real_area2) == ignore1)
734 simgrid::mc::Type *subtype, *subsubtype;
736 const void *addr_pointed1, *addr_pointed2;
738 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
739 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
741 switch (type->type) {
742 case DW_TAG_unspecified_type:
745 case DW_TAG_base_type:
746 if (!type->name.empty() && type->name == "char") { /* String, hence random (arbitrary ?) size */
747 if (real_area1 == real_area2)
750 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, area_size) != 0;
752 if (area_size != -1 && type->byte_size != area_size)
755 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
759 case DW_TAG_enumeration_type:
760 if (area_size != -1 && type->byte_size != area_size)
762 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
765 case DW_TAG_const_type:
766 case DW_TAG_volatile_type:
768 type = type->subtype;
771 case DW_TAG_array_type:
772 subtype = type->subtype;
773 switch (subtype->type) {
774 case DW_TAG_unspecified_type:
777 case DW_TAG_base_type:
778 case DW_TAG_enumeration_type:
779 case DW_TAG_pointer_type:
780 case DW_TAG_reference_type:
781 case DW_TAG_rvalue_reference_type:
782 case DW_TAG_structure_type:
783 case DW_TAG_class_type:
784 case DW_TAG_union_type:
785 if (subtype->full_type)
786 subtype = subtype->full_type;
787 elm_size = subtype->byte_size;
789 // TODO, just remove the type indirection?
790 case DW_TAG_const_type:
792 case DW_TAG_volatile_type:
793 subsubtype = subtype->subtype;
794 if (subsubtype->full_type)
795 subsubtype = subsubtype->full_type;
796 elm_size = subsubtype->byte_size;
802 for (int i = 0; i < type->element_count; i++) {
803 // TODO, add support for variable stride (DW_AT_byte_stride)
805 compare_heap_area_with_type(state, process_index,
806 (char *) real_area1 + (i * elm_size),
807 (char *) real_area2 + (i * elm_size),
808 snapshot1, snapshot2, previous,
809 type->subtype, subtype->byte_size,
810 check_ignore, pointer_level);
816 case DW_TAG_reference_type:
817 case DW_TAG_rvalue_reference_type:
818 case DW_TAG_pointer_type:
819 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
820 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
821 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
822 return (addr_pointed1 != addr_pointed2);
825 if (pointer_level <= 1) {
826 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
827 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
828 if (addr_pointed1 > state->std_heap_copy.heapbase
829 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
830 && addr_pointed2 > state->std_heap_copy.heapbase
831 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
832 return compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
833 snapshot2, previous, type->subtype,
836 return (addr_pointed1 != addr_pointed2);
838 for (size_t i = 0; i < (area_size / sizeof(void *)); i++) {
839 addr_pointed1 = snapshot1->read(
840 remote((void**)((char*) real_area1 + i * sizeof(void *))),
842 addr_pointed2 = snapshot2->read(
843 remote((void**)((char*) real_area2 + i * sizeof(void *))),
845 if (addr_pointed1 > state->std_heap_copy.heapbase
846 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
847 && addr_pointed2 > state->std_heap_copy.heapbase
848 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
850 compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
851 snapshot2, previous, type->subtype,
854 res = (addr_pointed1 != addr_pointed2);
860 case DW_TAG_structure_type:
861 case DW_TAG_class_type:
863 type = type->full_type;
864 if (area_size != -1 && type->byte_size != area_size) {
865 if (area_size <= type->byte_size || area_size % type->byte_size != 0)
867 for (size_t i = 0; i < (size_t)(area_size / type->byte_size); i++) {
868 int res = compare_heap_area_with_type(state, process_index,
869 (char *) real_area1 + i * type->byte_size,
870 (char *) real_area2 + i * type->byte_size,
871 snapshot1, snapshot2, previous, type, -1,
877 for(simgrid::mc::Member& member : type->members) {
878 // TODO, optimize this? (for the offset case)
879 void *real_member1 = simgrid::dwarf::resolve_member(
880 real_area1, type, &member, (simgrid::mc::AddressSpace*) snapshot1, process_index);
881 void *real_member2 = simgrid::dwarf::resolve_member(
882 real_area2, type, &member, (simgrid::mc::AddressSpace*) snapshot2, process_index);
883 int res = compare_heap_area_with_type(
884 state, process_index, real_member1, real_member2,
885 snapshot1, snapshot2,
886 previous, member.type, -1,
894 case DW_TAG_union_type:
895 return compare_heap_area_without_type(state, process_index, real_area1, real_area2,
896 snapshot1, snapshot2, previous,
897 type->byte_size, check_ignore);
904 xbt_die("Unreachable");
907 /** Infer the type of a part of the block from the type of the block
909 * TODO, handle DW_TAG_array_type as well as arrays of the object ((*p)[5], p[5])
911 * TODO, handle subfields ((*p).bar.foo, (*p)[5].bar…)
913 * @param type_id DWARF type ID of the root address
915 * @return DWARF type ID for given offset
917 static simgrid::mc::Type* get_offset_type(void *real_base_address, simgrid::mc::Type* type,
918 int offset, int area_size,
919 simgrid::mc::Snapshot* snapshot, int process_index)
922 // Beginning of the block, the infered variable type if the type of the block:
926 switch (type->type) {
928 case DW_TAG_structure_type:
929 case DW_TAG_class_type:
931 type = type->full_type;
932 if (area_size != -1 && type->byte_size != area_size) {
933 if (area_size > type->byte_size && area_size % type->byte_size == 0)
939 for(simgrid::mc::Member& member : type->members) {
940 if (member.has_offset_location()) {
941 // We have the offset, use it directly (shortcut):
942 if (member.offset() == offset)
945 void *real_member = simgrid::dwarf::resolve_member(
946 real_base_address, type, &member, snapshot, process_index);
947 if ((char*) real_member - (char *) real_base_address == offset)
954 /* FIXME : other cases ? */
962 * @param area1 Process address for state 1
963 * @param area2 Process address for state 2
964 * @param snapshot1 Snapshot of state 1
965 * @param snapshot2 Snapshot of state 2
966 * @param previous Pairs of blocks already compared on the current path (or nullptr)
967 * @param type_id Type of variable
968 * @param pointer_level
969 * @return 0 (same), 1 (different), -1
971 int compare_heap_area(int process_index, const void *area1, const void *area2, simgrid::mc::Snapshot* snapshot1,
972 simgrid::mc::Snapshot* snapshot2, xbt_dynar_t previous,
973 simgrid::mc::Type* type, int pointer_level)
975 simgrid::mc::Process* process = &mc_model_checker->process();
977 simgrid::mc::StateComparator *state = mc_diff_info.get();
980 ssize_t block1, frag1, block2, frag2;
982 int check_ignore = 0;
984 void *real_addr_block1, *real_addr_block2, *real_addr_frag1, *real_addr_frag2;
986 int offset1 = 0, offset2 = 0;
987 int new_size1 = -1, new_size2 = -1;
988 simgrid::mc::Type *new_type1 = nullptr, *new_type2 = nullptr;
992 // This is the address of std_heap->heapinfo in the application process:
993 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
995 const malloc_info* heapinfos1 = snapshot1->read(
996 remote((const malloc_info**)heapinfo_address), process_index);
997 const malloc_info* heapinfos2 = snapshot2->read(
998 remote((const malloc_info**)heapinfo_address), process_index);
1000 malloc_info heapinfo_temp1, heapinfo_temp2;
1002 if (previous == nullptr) {
1004 xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
1007 // Get block number:
1010 (char *) state->std_heap_copy.heapbase) / BLOCKSIZE + 1;
1013 (char *) state->std_heap_copy.heapbase) / BLOCKSIZE + 1;
1015 // If either block is a stack block:
1016 if (is_block_stack((int) block1) && is_block_stack((int) block2)) {
1017 add_heap_area_pair(previous, block1, -1, block2, -1);
1019 state->match_equals(previous);
1020 xbt_dynar_free(&previous);
1025 // If either block is not in the expected area of memory:
1026 if (((char *) area1 < (char *) state->std_heap_copy.heapbase)
1027 || (block1 > (ssize_t) state->processStates[0].heapsize) || (block1 < 1)
1028 || ((char *) area2 < (char *) state->std_heap_copy.heapbase)
1029 || (block2 > (ssize_t) state->processStates[1].heapsize) || (block2 < 1)) {
1031 xbt_dynar_free(&previous);
1035 // Process address of the block:
1036 real_addr_block1 = (ADDR2UINT(block1) - 1) * BLOCKSIZE +
1037 (char *) state->std_heap_copy.heapbase;
1038 real_addr_block2 = (ADDR2UINT(block2) - 1) * BLOCKSIZE +
1039 (char *) state->std_heap_copy.heapbase;
1043 if (type->full_type)
1044 type = type->full_type;
1046 // This assume that for "boring" types (volatile ...) byte_size is absent:
1047 while (type->byte_size == 0 && type->subtype != nullptr)
1048 type = type->subtype;
1051 if (type->type == DW_TAG_pointer_type
1052 || (type->type == DW_TAG_base_type && !type->name.empty()
1053 && type->name == "char"))
1056 type_size = type->byte_size;
1060 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
1061 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
1063 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
1064 heap_region1, &heapinfo_temp1, &heapinfos1[block1], sizeof(malloc_info));
1065 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
1066 heap_region2, &heapinfo_temp2, &heapinfos2[block2], sizeof(malloc_info));
1068 if ((heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type==MMALLOC_TYPE_HEAPINFO)
1069 && (heapinfo2->type == MMALLOC_TYPE_FREE || heapinfo2->type ==MMALLOC_TYPE_HEAPINFO)) {
1072 state->match_equals(previous);
1073 xbt_dynar_free(&previous);
1078 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED
1079 && heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
1080 /* Complete block */
1082 // TODO, lookup variable type from block type as done for fragmented blocks
1084 offset1 = (char *) area1 - (char *) real_addr_block1;
1085 offset2 = (char *) area2 - (char *) real_addr_block2;
1087 if (state->equals_to1_(block1, 0).valid
1088 && state->equals_to2_(block2, 0).valid
1089 && state->blocksEqual(block1, block2)) {
1091 state->match_equals(previous);
1092 xbt_dynar_free(&previous);
1097 if (type_size != -1) {
1098 if (type_size != (ssize_t) heapinfo1->busy_block.busy_size
1099 && type_size != (ssize_t) heapinfo2->busy_block.busy_size
1100 && (type->name.empty() || type->name == "struct s_smx_context")) {
1102 state->match_equals(previous);
1103 xbt_dynar_free(&previous);
1109 if (heapinfo1->busy_block.size != heapinfo2->busy_block.size) {
1111 xbt_dynar_free(&previous);
1115 if (heapinfo1->busy_block.busy_size != heapinfo2->busy_block.busy_size) {
1117 xbt_dynar_free(&previous);
1121 if (!add_heap_area_pair(previous, block1, -1, block2, -1)) {
1123 state->match_equals(previous);
1124 xbt_dynar_free(&previous);
1129 size = heapinfo1->busy_block.busy_size;
1131 // Remember (basic) type inference.
1132 // The current data structure only allows us to do this for the whole block.
1133 if (type != nullptr && area1 == real_addr_block1)
1134 state->types1_(block1, 0) = type;
1135 if (type != nullptr && area2 == real_addr_block2)
1136 state->types2_(block2, 0) = type;
1140 state->match_equals(previous);
1141 xbt_dynar_free(&previous);
1149 if (heapinfo1->busy_block.ignore > 0
1150 && heapinfo2->busy_block.ignore == heapinfo1->busy_block.ignore)
1151 check_ignore = heapinfo1->busy_block.ignore;
1153 } else if ((heapinfo1->type > 0) && (heapinfo2->type > 0)) { /* Fragmented block */
1157 ((uintptr_t) (ADDR2UINT(area1) % (BLOCKSIZE))) >> heapinfo1->type;
1159 ((uintptr_t) (ADDR2UINT(area2) % (BLOCKSIZE))) >> heapinfo2->type;
1161 // Process address of the fragment:
1163 (void *) ((char *) real_addr_block1 +
1164 (frag1 << heapinfo1->type));
1166 (void *) ((char *) real_addr_block2 +
1167 (frag2 << heapinfo2->type));
1169 // Check the size of the fragments against the size of the type:
1170 if (type_size != -1) {
1171 if (heapinfo1->busy_frag.frag_size[frag1] == -1
1172 || heapinfo2->busy_frag.frag_size[frag2] == -1) {
1174 state->match_equals(previous);
1175 xbt_dynar_free(&previous);
1180 if (type_size != heapinfo1->busy_frag.frag_size[frag1]
1181 || type_size != heapinfo2->busy_frag.frag_size[frag2]) {
1183 state->match_equals(previous);
1184 xbt_dynar_free(&previous);
1190 // Check if the blocks are already matched together:
1191 if (state->equals_to1_(block1, frag1).valid
1192 && state->equals_to2_(block2, frag2).valid) {
1193 if (offset1==offset2 && state->fragmentsEqual(block1, frag1, block2, frag2)) {
1195 state->match_equals(previous);
1196 xbt_dynar_free(&previous);
1201 // Compare the size of both fragments:
1202 if (heapinfo1->busy_frag.frag_size[frag1] !=
1203 heapinfo2->busy_frag.frag_size[frag2]) {
1204 if (type_size == -1) {
1206 state->match_equals(previous);
1207 xbt_dynar_free(&previous);
1212 xbt_dynar_free(&previous);
1217 // Size of the fragment:
1218 size = heapinfo1->busy_frag.frag_size[frag1];
1220 // Remember (basic) type inference.
1221 // The current data structure only allows us to do this for the whole fragment.
1222 if (type != nullptr && area1 == real_addr_frag1)
1223 state->types1_(block1, frag1) = type;
1224 if (type != nullptr && area2 == real_addr_frag2)
1225 state->types2_(block2, frag2) = type;
1227 // The type of the variable is already known:
1232 // Type inference from the block type.
1233 else if (state->types1_(block1, frag1) != nullptr
1234 || state->types2_(block2, frag2) != nullptr) {
1236 offset1 = (char *) area1 - (char *) real_addr_frag1;
1237 offset2 = (char *) area2 - (char *) real_addr_frag2;
1239 if (state->types1_(block1, frag1) != nullptr
1240 && state->types2_(block2, frag2) != nullptr) {
1242 get_offset_type(real_addr_frag1, state->types1_(block1, frag1),
1243 offset1, size, snapshot1, process_index);
1245 get_offset_type(real_addr_frag2, state->types2_(block2, frag2),
1246 offset1, size, snapshot2, process_index);
1247 } else if (state->types1_(block1, frag1) != nullptr) {
1249 get_offset_type(real_addr_frag1, state->types1_(block1, frag1),
1250 offset1, size, snapshot1, process_index);
1252 get_offset_type(real_addr_frag2, state->types1_(block1, frag1),
1253 offset2, size, snapshot2, process_index);
1254 } else if (state->types2_(block2, frag2) != nullptr) {
1256 get_offset_type(real_addr_frag1, state->types2_(block2, frag2),
1257 offset1, size, snapshot1, process_index);
1259 get_offset_type(real_addr_frag2, state->types2_(block2, frag2),
1260 offset2, size, snapshot2, process_index);
1263 state->match_equals(previous);
1264 xbt_dynar_free(&previous);
1269 if (new_type1 != nullptr && new_type2 != nullptr && new_type1 != new_type2) {
1272 while (type->byte_size == 0 && type->subtype != nullptr)
1273 type = type->subtype;
1274 new_size1 = type->byte_size;
1277 while (type->byte_size == 0 && type->subtype != nullptr)
1278 type = type->subtype;
1279 new_size2 = type->byte_size;
1283 state->match_equals(previous);
1284 xbt_dynar_free(&previous);
1290 if (new_size1 > 0 && new_size1 == new_size2) {
1295 if (offset1 == 0 && offset2 == 0
1296 && !add_heap_area_pair(previous, block1, frag1, block2, frag2)) {
1298 state->match_equals(previous);
1299 xbt_dynar_free(&previous);
1306 state->match_equals(previous);
1307 xbt_dynar_free(&previous);
1312 if ((heapinfo1->busy_frag.ignore[frag1] > 0)
1313 && (heapinfo2->busy_frag.ignore[frag2] ==
1314 heapinfo1->busy_frag.ignore[frag1]))
1315 check_ignore = heapinfo1->busy_frag.ignore[frag1];
1319 xbt_dynar_free(&previous);
1324 /* Start comparison */
1327 compare_heap_area_with_type(state, process_index, area1, area2, snapshot1, snapshot2,
1328 previous, type, size, check_ignore,
1332 compare_heap_area_without_type(state, process_index, area1, area2, snapshot1, snapshot2,
1333 previous, size, check_ignore);
1335 if (res_compare == 1) {
1337 xbt_dynar_free(&previous);
1342 state->match_equals(previous);
1343 xbt_dynar_free(&previous);