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 */
9 #include "src/xbt/ex_interface.h" /* internals of backtrace setup */
11 #include "xbt/mmalloc.h"
12 #include "mc/datatypes.h"
13 #include "src/mc/malloc.hpp"
14 #include "src/mc/mc_private.h"
15 #include "src/mc/mc_snapshot.h"
16 #include "src/mc/mc_dwarf.hpp"
17 #include "src/mc/Type.hpp"
19 #include <xbt/dynar.h>
21 using simgrid::mc::remote;
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_diff, xbt,
24 "Logging specific to mc_diff in mc");
26 /*********************************** Heap comparison ***********************************/
27 /***************************************************************************************/
29 struct XBT_PRIVATE s_mc_diff {
30 s_xbt_mheap_t std_heap_copy;
31 std::size_t heaplimit;
32 // Number of blocks in the heaps:
33 std::size_t heapsize1, heapsize2;
34 std::vector<simgrid::mc::IgnoredHeapRegion>* to_ignore1;
35 std::vector<simgrid::mc::IgnoredHeapRegion>* to_ignore2;
36 s_heap_area_t *equals_to1, *equals_to2;
37 simgrid::mc::Type **types1;
38 simgrid::mc::Type **types2;
39 std::size_t available;
42 #define equals_to1_(i,j) equals_to1[ MAX_FRAGMENT_PER_BLOCK*(i) + (j)]
43 #define equals_to2_(i,j) equals_to2[ MAX_FRAGMENT_PER_BLOCK*(i) + (j)]
44 #define types1_(i,j) types1[ MAX_FRAGMENT_PER_BLOCK*(i) + (j)]
45 #define types2_(i,j) types2[ MAX_FRAGMENT_PER_BLOCK*(i) + (j)]
47 static __thread struct s_mc_diff *mc_diff_info = nullptr;
49 /*********************************** Free functions ************************************/
51 static void heap_area_pair_free(heap_area_pair_t pair)
57 static void heap_area_pair_free_voidp(void *d)
59 heap_area_pair_free((heap_area_pair_t) * (void **) d);
62 static void heap_area_free(heap_area_t area)
68 /************************************************************************************/
70 static s_heap_area_t make_heap_area(int block, int fragment)
75 area.fragment = fragment;
80 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1,
81 int block2, int fragment2)
84 unsigned int cursor = 0;
85 heap_area_pair_t current_pair;
87 xbt_dynar_foreach(list, cursor, current_pair)
88 if (current_pair->block1 == block1 && current_pair->block2 == block2
89 && current_pair->fragment1 == fragment1
90 && current_pair->fragment2 == fragment2)
96 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1,
97 int block2, int fragment2)
100 if (is_new_heap_area_pair(list, block1, fragment1, block2, fragment2)) {
101 heap_area_pair_t pair = nullptr;
102 pair = xbt_new0(s_heap_area_pair_t, 1);
103 pair->block1 = block1;
104 pair->fragment1 = fragment1;
105 pair->block2 = block2;
106 pair->fragment2 = fragment2;
108 xbt_dynar_push(list, &pair);
116 static ssize_t heap_comparison_ignore_size(
117 std::vector<simgrid::mc::IgnoredHeapRegion>* ignore_list,
121 int end = ignore_list->size() - 1;
123 while (start <= end) {
124 unsigned int cursor = (start + end) / 2;
125 simgrid::mc::IgnoredHeapRegion const& region = (*ignore_list)[cursor];
126 if (region.address == address)
128 if (region.address < address)
130 if (region.address > address)
137 static bool is_stack(const void *address)
139 for (auto const& stack : mc_model_checker->process().stack_areas())
140 if (address == stack.address)
145 // TODO, this should depend on the snapshot?
146 static bool is_block_stack(int block)
148 for (auto const& stack : mc_model_checker->process().stack_areas())
149 if (block == stack.block)
154 static void match_equals(struct s_mc_diff *state, xbt_dynar_t list)
157 unsigned int cursor = 0;
158 heap_area_pair_t current_pair;
160 xbt_dynar_foreach(list, cursor, current_pair)
162 if (current_pair->fragment1 != -1) {
164 state->equals_to1_(current_pair->block1, current_pair->fragment1) =
165 make_heap_area(current_pair->block2, current_pair->fragment2);
166 state->equals_to2_(current_pair->block2, current_pair->fragment2) =
167 make_heap_area(current_pair->block1, current_pair->fragment1);
171 state->equals_to1_(current_pair->block1, 0) =
172 make_heap_area(current_pair->block2, current_pair->fragment2);
173 state->equals_to2_(current_pair->block2, 0) =
174 make_heap_area(current_pair->block1, current_pair->fragment1);
180 /** Check whether two blocks are known to be matching
182 * @param state State used
183 * @param b1 Block of state 1
184 * @param b2 Block of state 2
185 * @return if the blocks are known to be matching
187 static int equal_blocks(struct s_mc_diff *state, int b1, int b2)
190 if (state->equals_to1_(b1, 0).block == b2
191 && state->equals_to2_(b2, 0).block == b1)
197 /** Check whether two fragments are known to be matching
199 * @param state State used
200 * @param b1 Block of state 1
201 * @param f1 Fragment of state 1
202 * @param b2 Block of state 2
203 * @param f2 Fragment of state 2
204 * @return if the fragments are known to be matching
206 static int equal_fragments(struct s_mc_diff *state, int b1, int f1, int b2,
210 if (state->equals_to1_(b1, f1).block == b2
211 && state->equals_to1_(b1, f1).fragment == f2
212 && state->equals_to2_(b2, f2).block == b1
213 && state->equals_to2_(b2, f2).fragment == f1)
222 int init_heap_information(xbt_mheap_t heap1, xbt_mheap_t heap2,
223 std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
224 std::vector<simgrid::mc::IgnoredHeapRegion>* i2)
226 if (mc_diff_info == nullptr) {
227 mc_diff_info = xbt_new0(struct s_mc_diff, 1);
228 mc_diff_info->equals_to1 = nullptr;
229 mc_diff_info->equals_to2 = nullptr;
230 mc_diff_info->types1 = nullptr;
231 mc_diff_info->types2 = nullptr;
233 struct s_mc_diff *state = mc_diff_info;
235 if ((((struct mdesc *) heap1)->heaplimit !=
236 ((struct mdesc *) heap2)->heaplimit)
238 ((((struct mdesc *) heap1)->heapsize !=
239 ((struct mdesc *) heap2)->heapsize)))
242 state->heaplimit = ((struct mdesc *) heap1)->heaplimit;
244 state->std_heap_copy = *mc_model_checker->process().get_heap();
246 state->heapsize1 = heap1->heapsize;
247 state->heapsize2 = heap2->heapsize;
249 state->to_ignore1 = i1;
250 state->to_ignore2 = i2;
252 if (state->heaplimit > state->available) {
253 state->equals_to1 = (s_heap_area_t*)
254 realloc(state->equals_to1,
255 state->heaplimit * MAX_FRAGMENT_PER_BLOCK *
256 sizeof(s_heap_area_t));
257 state->types1 = (simgrid::mc::Type**)
258 realloc(state->types1,
259 state->heaplimit * MAX_FRAGMENT_PER_BLOCK *
260 sizeof(simgrid::mc::Type*));
261 state->equals_to2 = (s_heap_area_t*)
262 realloc(state->equals_to2,
263 state->heaplimit * MAX_FRAGMENT_PER_BLOCK *
264 sizeof(s_heap_area_t));
265 state->types2 = (simgrid::mc::Type**)
266 realloc(state->types2,
267 state->heaplimit * MAX_FRAGMENT_PER_BLOCK *
268 sizeof(simgrid::mc::Type*));
269 state->available = state->heaplimit;
272 memset(state->equals_to1, 0,
273 state->heaplimit * MAX_FRAGMENT_PER_BLOCK * sizeof(s_heap_area_t));
274 memset(state->equals_to2, 0,
275 state->heaplimit * MAX_FRAGMENT_PER_BLOCK * sizeof(s_heap_area_t));
276 memset(state->types1, 0,
277 state->heaplimit * MAX_FRAGMENT_PER_BLOCK * sizeof(char**));
278 memset(state->types2, 0,
279 state->heaplimit * MAX_FRAGMENT_PER_BLOCK * sizeof(char**));
285 void reset_heap_information()
290 // TODO, have a robust way to find it in O(1)
292 mc_mem_region_t MC_get_heap_region(simgrid::mc::Snapshot* snapshot)
294 size_t n = snapshot->snapshot_regions.size();
295 for (size_t i=0; i!=n; ++i) {
296 mc_mem_region_t region = snapshot->snapshot_regions[i].get();
297 if (region->region_type() == simgrid::mc::RegionType::Heap)
300 xbt_die("No heap region");
303 int mmalloc_compare_heap(simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2)
305 simgrid::mc::Process* process = &mc_model_checker->process();
306 struct s_mc_diff *state = mc_diff_info;
308 /* Start comparison */
309 size_t i1, i2, j1, j2, k;
310 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
311 int nb_diff1 = 0, nb_diff2 = 0;
313 int equal, res_compare = 0;
315 /* Check busy blocks */
319 malloc_info heapinfo_temp1, heapinfo_temp2;
320 malloc_info heapinfo_temp2b;
322 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
323 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
325 // This is the address of std_heap->heapinfo in the application process:
326 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
328 // This is in snapshot do not use them directly:
329 const malloc_info* heapinfos1 = snapshot1->read<malloc_info*>(
330 (std::uint64_t)heapinfo_address, simgrid::mc::ProcessIndexMissing);
331 const malloc_info* heapinfos2 = snapshot2->read<malloc_info*>(
332 (std::uint64_t)heapinfo_address, simgrid::mc::ProcessIndexMissing);
334 while (i1 < state->heaplimit) {
336 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(heap_region1, &heapinfo_temp1, &heapinfos1[i1], sizeof(malloc_info));
337 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2, &heapinfos2[i1], sizeof(malloc_info));
339 if (heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type == MMALLOC_TYPE_HEAPINFO) { /* Free block */
344 if (heapinfo1->type < 0) {
345 fprintf(stderr, "Unkown mmalloc block type.\n");
350 ((void *) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE +
351 (char *) state->std_heap_copy.heapbase));
353 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED) { /* Large block */
355 if (is_stack(addr_block1)) {
356 for (k = 0; k < heapinfo1->busy_block.size; k++)
357 state->equals_to1_(i1 + k, 0) = make_heap_area(i1, -1);
358 for (k = 0; k < heapinfo2->busy_block.size; k++)
359 state->equals_to2_(i1 + k, 0) = make_heap_area(i1, -1);
360 i1 += heapinfo1->busy_block.size;
364 if (state->equals_to1_(i1, 0).valid) {
373 /* Try first to associate to same block in the other heap */
374 if (heapinfo2->type == heapinfo1->type) {
376 if (state->equals_to2_(i1, 0).valid == 0) {
378 addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE +
379 (char *) state->std_heap_copy.heapbase;
382 compare_heap_area(simgrid::mc::ProcessIndexMissing, addr_block1, addr_block2, snapshot1, snapshot2,
383 nullptr, nullptr, 0);
385 if (res_compare != 1) {
386 for (k = 1; k < heapinfo2->busy_block.size; k++)
387 state->equals_to2_(i1 + k, 0) = make_heap_area(i1, -1);
388 for (k = 1; k < heapinfo1->busy_block.size; k++)
389 state->equals_to1_(i1 + k, 0) = make_heap_area(i1, -1);
391 i1 += heapinfo1->busy_block.size;
398 while (i2 < state->heaplimit && !equal) {
400 addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE +
401 (char *) state->std_heap_copy.heapbase;
408 const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2b, &heapinfos2[i2], sizeof(malloc_info));
410 if (heapinfo2b->type != MMALLOC_TYPE_UNFRAGMENTED) {
415 if (state->equals_to2_(i2, 0).valid) {
421 compare_heap_area(simgrid::mc::ProcessIndexMissing, addr_block1, addr_block2, snapshot1, snapshot2,
422 nullptr, nullptr, 0);
424 if (res_compare != 1) {
425 for (k = 1; k < heapinfo2b->busy_block.size; k++)
426 state->equals_to2_(i2 + k, 0) = make_heap_area(i1, -1);
427 for (k = 1; k < heapinfo1->busy_block.size; k++)
428 state->equals_to1_(i1 + k, 0) = make_heap_area(i2, -1);
430 i1 += heapinfo1->busy_block.size;
438 XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1,
439 heapinfo1->busy_block.busy_size, addr_block1);
440 i1 = state->heaplimit + 1;
445 } else { /* Fragmented block */
447 for (j1 = 0; j1 < (size_t) (BLOCKSIZE >> heapinfo1->type); j1++) {
449 if (heapinfo1->busy_frag.frag_size[j1] == -1) /* Free fragment */
452 if (state->equals_to1_(i1, j1).valid)
456 (void *) ((char *) addr_block1 + (j1 << heapinfo1->type));
461 /* Try first to associate to same fragment in the other heap */
462 if (heapinfo2->type == heapinfo1->type) {
464 if (state->equals_to2_(i1, j1).valid == 0) {
466 addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE +
467 (char *) state->std_heap_copy.heapbase;
469 (void *) ((char *) addr_block2 +
470 (j1 << heapinfo2->type));
473 compare_heap_area(simgrid::mc::ProcessIndexMissing, addr_frag1, addr_frag2, snapshot1, snapshot2,
474 nullptr, nullptr, 0);
476 if (res_compare != 1)
483 while (i2 < state->heaplimit && !equal) {
485 const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(
486 heap_region2, &heapinfo_temp2b, &heapinfos2[i2],
487 sizeof(malloc_info));
489 if (heapinfo2b->type == MMALLOC_TYPE_FREE || heapinfo2b->type == MMALLOC_TYPE_HEAPINFO) {
494 // We currently do not match fragments with unfragmented blocks (maybe we should).
495 if (heapinfo2b->type == MMALLOC_TYPE_UNFRAGMENTED) {
500 if (heapinfo2b->type < 0) {
501 fprintf(stderr, "Unkown mmalloc block type.\n");
505 for (j2 = 0; j2 < (size_t) (BLOCKSIZE >> heapinfo2b->type);
508 if (i2 == i1 && j2 == j1)
511 if (state->equals_to2_(i2, j2).valid)
514 addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE +
515 (char *) state->std_heap_copy.heapbase;
517 (void *) ((char *) addr_block2 +
518 (j2 << heapinfo2b->type));
521 compare_heap_area(simgrid::mc::ProcessIndexMissing, addr_frag1, addr_frag2, snapshot2, snapshot2,
522 nullptr, nullptr, 0);
524 if (res_compare != 1) {
537 ("Block %zu, fragment %zu not found (size_used = %zd, address = %p)\n",
538 i1, j1, heapinfo1->busy_frag.frag_size[j1],
540 i2 = state->heaplimit + 1;
541 i1 = state->heaplimit + 1;
554 /* All blocks/fragments are equal to another block/fragment ? */
557 for(i = 1; i < state->heaplimit; i++) {
558 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
559 heap_region1, &heapinfo_temp1, &heapinfos1[i], sizeof(malloc_info));
560 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED) {
561 if (i1 == state->heaplimit) {
562 if (heapinfo1->busy_block.busy_size > 0) {
563 if (state->equals_to1_(i, 0).valid == 0) {
564 if (XBT_LOG_ISENABLED(mc_diff, xbt_log_priority_debug)) {
566 XBT_DEBUG("Block %zu not found (size used = %zu)", i,
567 heapinfo1->busy_block.busy_size);
574 if (heapinfo1->type > 0) {
575 for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo1->type); j++) {
576 if (i1 == state->heaplimit) {
577 if (heapinfo1->busy_frag.frag_size[j] > 0) {
578 if (state->equals_to1_(i, j).valid == 0) {
579 if (XBT_LOG_ISENABLED(mc_diff, xbt_log_priority_debug)) {
580 // TODO, print fragment address
582 ("Block %zu, Fragment %zu not found (size used = %zd)",
584 heapinfo1->busy_frag.frag_size[j]);
594 if (i1 == state->heaplimit)
595 XBT_DEBUG("Number of blocks/fragments not found in heap1 : %d", nb_diff1);
597 for (i=1; i < state->heaplimit; i++) {
598 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
599 heap_region2, &heapinfo_temp2, &heapinfos2[i], sizeof(malloc_info));
600 if (heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
601 if (i1 == state->heaplimit) {
602 if (heapinfo2->busy_block.busy_size > 0) {
603 if (state->equals_to2_(i, 0).valid == 0) {
604 if (XBT_LOG_ISENABLED(mc_diff, xbt_log_priority_debug)) {
605 // TODO, print address of the block
606 XBT_DEBUG("Block %zu not found (size used = %zu)", i,
607 heapinfo2->busy_block.busy_size);
614 if (heapinfo2->type > 0) {
615 for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo2->type); j++) {
616 if (i1 == state->heaplimit) {
617 if (heapinfo2->busy_frag.frag_size[j] > 0) {
618 if (state->equals_to2_(i, j).valid == 0) {
619 if (XBT_LOG_ISENABLED(mc_diff, xbt_log_priority_debug)) {
620 // TODO, print address of the block
622 ("Block %zu, Fragment %zu not found (size used = %zd)",
624 heapinfo2->busy_frag.frag_size[j]);
634 if (i1 == state->heaplimit)
635 XBT_DEBUG("Number of blocks/fragments not found in heap2 : %d", nb_diff2);
637 return ((nb_diff1 > 0) || (nb_diff2 > 0));
643 * @param real_area1 Process address for state 1
644 * @param real_area2 Process address for state 2
645 * @param snapshot1 Snapshot of state 1
646 * @param snapshot2 Snapshot of state 2
649 * @param check_ignore
651 static int compare_heap_area_without_type(struct s_mc_diff *state, int process_index,
652 const void *real_area1, const void *real_area2,
653 simgrid::mc::Snapshot* snapshot1,
654 simgrid::mc::Snapshot* snapshot2,
655 xbt_dynar_t previous, int size,
658 simgrid::mc::Process* process = &mc_model_checker->process();
661 const void *addr_pointed1, *addr_pointed2;
662 int pointer_align, res_compare;
663 ssize_t ignore1, ignore2;
665 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
666 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
670 if (check_ignore > 0) {
672 heap_comparison_ignore_size(state->to_ignore1,
673 (char *) real_area1 + i)) != -1) {
675 heap_comparison_ignore_size(state->to_ignore2,
676 (char *) real_area2 + i)) == ignore1) {
689 if (MC_snapshot_region_memcmp(((char *) real_area1) + i, heap_region1, ((char *) real_area2) + i, heap_region2, 1) != 0) {
691 pointer_align = (i / sizeof(void *)) * sizeof(void *);
692 addr_pointed1 = snapshot1->read(
693 remote((void**)((char *) real_area1 + pointer_align)), process_index);
694 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 *);
701 } else if (addr_pointed1 > state->std_heap_copy.heapbase
702 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
703 && addr_pointed2 > state->std_heap_copy.heapbase
704 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)) {
705 // Both addreses are in the heap:
707 compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
708 snapshot2, previous, nullptr, 0);
709 if (res_compare == 1)
711 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(struct s_mc_diff *state, int process_index,
741 const void *real_area1, const void *real_area2,
742 simgrid::mc::Snapshot* snapshot1,
743 simgrid::mc::Snapshot* snapshot2,
744 xbt_dynar_t previous, simgrid::mc::Type* type,
745 int area_size, int check_ignore,
750 // HACK: This should not happen but in pratice, there are some
751 // DW_TAG_typedef without an associated DW_AT_type:
752 //<1><538832>: Abbrev Number: 111 (DW_TAG_typedef)
753 // <538833> DW_AT_name : (indirect string, offset: 0x2292f3): gregset_t
754 // <538837> DW_AT_decl_file : 98
755 // <538838> DW_AT_decl_line : 37
759 if (is_stack(real_area1) && is_stack(real_area2))
761 ssize_t ignore1, ignore2;
763 if ((check_ignore > 0)
764 && ((ignore1 = heap_comparison_ignore_size(state->to_ignore1, real_area1))
766 && ((ignore2 = heap_comparison_ignore_size(state->to_ignore2, real_area2))
770 simgrid::mc::Type *subtype, *subsubtype;
772 const void *addr_pointed1, *addr_pointed2;
774 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
775 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
777 switch (type->type) {
778 case DW_TAG_unspecified_type:
781 case DW_TAG_base_type:
782 if (!type->name.empty() && type->name == "char") { /* String, hence random (arbitrary ?) size */
783 if (real_area1 == real_area2)
786 return (MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, area_size) != 0);
788 if (area_size != -1 && type->byte_size != area_size)
791 return (MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0);
794 case DW_TAG_enumeration_type:
795 if (area_size != -1 && type->byte_size != area_size)
798 return (MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0);
801 case DW_TAG_const_type:
802 case DW_TAG_volatile_type:
804 type = type->subtype;
807 case DW_TAG_array_type:
808 subtype = type->subtype;
809 switch (subtype->type) {
810 case DW_TAG_unspecified_type:
813 case DW_TAG_base_type:
814 case DW_TAG_enumeration_type:
815 case DW_TAG_pointer_type:
816 case DW_TAG_reference_type:
817 case DW_TAG_rvalue_reference_type:
818 case DW_TAG_structure_type:
819 case DW_TAG_class_type:
820 case DW_TAG_union_type:
821 if (subtype->full_type)
822 subtype = subtype->full_type;
823 elm_size = subtype->byte_size;
825 // TODO, just remove the type indirection?
826 case DW_TAG_const_type:
828 case DW_TAG_volatile_type:
829 subsubtype = subtype->subtype;
830 if (subsubtype->full_type)
831 subsubtype = subsubtype->full_type;
832 elm_size = subsubtype->byte_size;
838 for (int i = 0; i < type->element_count; i++) {
839 // TODO, add support for variable stride (DW_AT_byte_stride)
841 compare_heap_area_with_type(state, process_index,
842 (char *) real_area1 + (i * elm_size),
843 (char *) real_area2 + (i * elm_size),
844 snapshot1, snapshot2, previous,
845 type->subtype, subtype->byte_size,
846 check_ignore, pointer_level);
851 case DW_TAG_reference_type:
852 case DW_TAG_rvalue_reference_type:
853 case DW_TAG_pointer_type:
854 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
855 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
856 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
857 return (addr_pointed1 != addr_pointed2);;
860 if (pointer_level > 1) { /* Array of pointers */
861 for (size_t i = 0; i < (area_size / sizeof(void *)); i++) {
862 addr_pointed1 = snapshot1->read(
863 remote((void**)((char*) real_area1 + i * sizeof(void *))),
865 addr_pointed2 = snapshot2->read(
866 remote((void**)((char*) real_area2 + i * sizeof(void *))),
868 if (addr_pointed1 > state->std_heap_copy.heapbase
869 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
870 && addr_pointed2 > state->std_heap_copy.heapbase
871 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
873 compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
874 snapshot2, previous, type->subtype,
877 res = (addr_pointed1 != addr_pointed2);
882 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
883 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
884 if (addr_pointed1 > state->std_heap_copy.heapbase
885 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
886 && addr_pointed2 > state->std_heap_copy.heapbase
887 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
888 return compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
889 snapshot2, previous, type->subtype,
892 return (addr_pointed1 != addr_pointed2);
896 case DW_TAG_structure_type:
897 case DW_TAG_class_type:
899 type = type->full_type;
900 if (area_size != -1 && type->byte_size != area_size) {
901 if (area_size > type->byte_size && area_size % type->byte_size == 0) {
902 for (size_t i = 0; i < (size_t)(area_size / type->byte_size); i++) {
904 compare_heap_area_with_type(state, process_index,
905 (char *) real_area1 + i * type->byte_size,
906 (char *) real_area2 + i * type->byte_size,
907 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);
922 compare_heap_area_with_type(state, process_index, real_member1, real_member2,
923 snapshot1, snapshot2,
924 previous, member.type, -1,
931 case DW_TAG_union_type:
932 return compare_heap_area_without_type(state, process_index, real_area1, real_area2,
933 snapshot1, snapshot2, previous,
934 type->byte_size, check_ignore);
944 /** Infer the type of a part of the block from the type of the block
946 * TODO, handle DW_TAG_array_type as well as arrays of the object ((*p)[5], p[5])
948 * TODO, handle subfields ((*p).bar.foo, (*p)[5].bar…)
950 * @param type_id DWARF type ID of the root address
952 * @return DWARF type ID for given offset
954 static simgrid::mc::Type* get_offset_type(void *real_base_address, simgrid::mc::Type* type,
955 int offset, int area_size,
956 simgrid::mc::Snapshot* snapshot, int process_index)
959 // Beginning of the block, the infered variable type if the type of the block:
963 switch (type->type) {
964 case DW_TAG_structure_type:
965 case DW_TAG_class_type:
967 type = type->full_type;
969 if (area_size != -1 && type->byte_size != area_size) {
970 if (area_size > type->byte_size && area_size % type->byte_size == 0)
975 for(simgrid::mc::Member& member : type->members) {
977 if (member.has_offset_location()) {
978 // We have the offset, use it directly (shortcut):
979 if (member.offset() == offset)
982 void *real_member = simgrid::dwarf::resolve_member(
983 real_base_address, type, &member, snapshot, process_index);
984 if ((char*) real_member - (char *) real_base_address == offset)
993 /* FIXME : other cases ? */
1001 * @param area1 Process address for state 1
1002 * @param area2 Process address for state 2
1003 * @param snapshot1 Snapshot of state 1
1004 * @param snapshot2 Snapshot of state 2
1005 * @param previous Pairs of blocks already compared on the current path (or nullptr)
1006 * @param type_id Type of variable
1007 * @param pointer_level
1008 * @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 struct s_mc_diff *state = mc_diff_info;
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 match_equals(state, previous);
1059 xbt_dynar_free(&previous);
1063 // If either block is not in the expected area of memory:
1064 if (((char *) area1 < (char *) state->std_heap_copy.heapbase)
1065 || (block1 > (ssize_t) state->heapsize1) || (block1 < 1)
1066 || ((char *) area2 < (char *) state->std_heap_copy.heapbase)
1067 || (block2 > (ssize_t) state->heapsize2) || (block2 < 1)) {
1069 xbt_dynar_free(&previous);
1073 // Process address of the block:
1074 real_addr_block1 = (ADDR2UINT(block1) - 1) * BLOCKSIZE +
1075 (char *) state->std_heap_copy.heapbase;
1076 real_addr_block2 = (ADDR2UINT(block2) - 1) * BLOCKSIZE +
1077 (char *) state->std_heap_copy.heapbase;
1081 if (type->full_type)
1082 type = type->full_type;
1084 // This assume that for "boring" types (volatile ...) byte_size is absent:
1085 while (type->byte_size == 0 && type->subtype != nullptr)
1086 type = type->subtype;
1089 if ((type->type == DW_TAG_pointer_type)
1090 || ((type->type == DW_TAG_base_type) && !type->name.empty()
1091 && type->name == "char"))
1094 type_size = type->byte_size;
1098 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
1099 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
1101 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
1102 heap_region1, &heapinfo_temp1, &heapinfos1[block1], sizeof(malloc_info));
1103 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
1104 heap_region2, &heapinfo_temp2, &heapinfos2[block2], sizeof(malloc_info));
1106 if ((heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type==MMALLOC_TYPE_HEAPINFO)
1107 && (heapinfo2->type == MMALLOC_TYPE_FREE || heapinfo2->type ==MMALLOC_TYPE_HEAPINFO)) {
1111 match_equals(state, previous);
1112 xbt_dynar_free(&previous);
1116 } else if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED
1117 && heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
1118 /* Complete block */
1120 // TODO, lookup variable type from block type as done for fragmented blocks
1122 offset1 = (char *) area1 - (char *) real_addr_block1;
1123 offset2 = (char *) area2 - (char *) real_addr_block2;
1125 if (state->equals_to1_(block1, 0).valid
1126 && state->equals_to2_(block2, 0).valid) {
1127 if (equal_blocks(state, block1, block2)) {
1129 match_equals(state, previous);
1130 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 match_equals(state, previous);
1142 xbt_dynar_free(&previous);
1148 if (heapinfo1->busy_block.size !=
1149 heapinfo2->busy_block.size) {
1151 xbt_dynar_free(&previous);
1155 if (heapinfo1->busy_block.busy_size !=
1156 heapinfo2->busy_block.busy_size) {
1158 xbt_dynar_free(&previous);
1162 if (!add_heap_area_pair(previous, block1, -1, block2, -1)) {
1164 match_equals(state, previous);
1165 xbt_dynar_free(&previous);
1170 size = heapinfo1->busy_block.busy_size;
1172 // Remember (basic) type inference.
1173 // The current data structure only allows us to do this for the whole block.
1174 if (type != nullptr && area1 == real_addr_block1)
1175 state->types1_(block1, 0) = type;
1176 if (type != nullptr && area2 == real_addr_block2)
1177 state->types2_(block2, 0) = type;
1181 match_equals(state, previous);
1182 xbt_dynar_free(&previous);
1190 if ((heapinfo1->busy_block.ignore > 0)
1191 && (heapinfo2->busy_block.ignore ==
1192 heapinfo1->busy_block.ignore))
1193 check_ignore = heapinfo1->busy_block.ignore;
1195 } else if ((heapinfo1->type > 0) && (heapinfo2->type > 0)) { /* Fragmented block */
1199 ((uintptr_t) (ADDR2UINT(area1) % (BLOCKSIZE))) >> heapinfo1->type;
1201 ((uintptr_t) (ADDR2UINT(area2) % (BLOCKSIZE))) >> heapinfo2->type;
1203 // Process address of the fragment:
1205 (void *) ((char *) real_addr_block1 +
1206 (frag1 << heapinfo1->type));
1208 (void *) ((char *) real_addr_block2 +
1209 (frag2 << heapinfo2->type));
1211 // Check the size of the fragments against the size of the type:
1212 if (type_size != -1) {
1213 if (heapinfo1->busy_frag.frag_size[frag1] == -1
1214 || heapinfo2->busy_frag.frag_size[frag2] == -1) {
1216 match_equals(state, previous);
1217 xbt_dynar_free(&previous);
1222 if (type_size != heapinfo1->busy_frag.frag_size[frag1]
1223 || type_size != heapinfo2->busy_frag.frag_size[frag2]) {
1225 match_equals(state, previous);
1226 xbt_dynar_free(&previous);
1232 // Check if the blocks are already matched together:
1233 if (state->equals_to1_(block1, frag1).valid
1234 && state->equals_to2_(block2, frag2).valid) {
1235 if (offset1==offset2 && equal_fragments(state, block1, frag1, block2, frag2)) {
1237 match_equals(state, previous);
1238 xbt_dynar_free(&previous);
1243 // Compare the size of both fragments:
1244 if (heapinfo1->busy_frag.frag_size[frag1] !=
1245 heapinfo2->busy_frag.frag_size[frag2]) {
1246 if (type_size == -1) {
1248 match_equals(state, previous);
1249 xbt_dynar_free(&previous);
1254 xbt_dynar_free(&previous);
1259 // Size of the fragment:
1260 size = heapinfo1->busy_frag.frag_size[frag1];
1262 // Remember (basic) type inference.
1263 // The current data structure only allows us to do this for the whole fragment.
1264 if (type != nullptr && area1 == real_addr_frag1)
1265 state->types1_(block1, frag1) = type;
1266 if (type != nullptr && area2 == real_addr_frag2)
1267 state->types2_(block2, frag2) = type;
1269 // The type of the variable is already known:
1274 // Type inference from the block type.
1275 else if (state->types1_(block1, frag1) != nullptr
1276 || state->types2_(block2, frag2) != nullptr) {
1278 offset1 = (char *) area1 - (char *) real_addr_frag1;
1279 offset2 = (char *) area2 - (char *) real_addr_frag2;
1281 if (state->types1_(block1, frag1) != nullptr
1282 && state->types2_(block2, frag2) != nullptr) {
1284 get_offset_type(real_addr_frag1, state->types1_(block1, frag1),
1285 offset1, size, snapshot1, process_index);
1287 get_offset_type(real_addr_frag2, state->types2_(block2, frag2),
1288 offset1, size, snapshot2, process_index);
1289 } else if (state->types1_(block1, frag1) != nullptr) {
1291 get_offset_type(real_addr_frag1, state->types1_(block1, frag1),
1292 offset1, size, snapshot1, process_index);
1294 get_offset_type(real_addr_frag2, state->types1_(block1, frag1),
1295 offset2, size, snapshot2, process_index);
1296 } else if (state->types2_(block2, frag2) != nullptr) {
1298 get_offset_type(real_addr_frag1, state->types2_(block2, frag2),
1299 offset1, size, snapshot1, process_index);
1301 get_offset_type(real_addr_frag2, state->types2_(block2, frag2),
1302 offset2, size, snapshot2, process_index);
1305 match_equals(state, previous);
1306 xbt_dynar_free(&previous);
1311 if (new_type1 != nullptr && new_type2 != nullptr && new_type1 != new_type2) {
1314 while (type->byte_size == 0 && type->subtype != nullptr)
1315 type = type->subtype;
1316 new_size1 = type->byte_size;
1319 while (type->byte_size == 0 && type->subtype != nullptr)
1320 type = type->subtype;
1321 new_size2 = type->byte_size;
1325 match_equals(state, previous);
1326 xbt_dynar_free(&previous);
1332 if (new_size1 > 0 && new_size1 == new_size2) {
1337 if (offset1 == 0 && offset2 == 0
1338 && !add_heap_area_pair(previous, block1, frag1, block2, frag2)) {
1340 match_equals(state, previous);
1341 xbt_dynar_free(&previous);
1348 match_equals(state, previous);
1349 xbt_dynar_free(&previous);
1354 if ((heapinfo1->busy_frag.ignore[frag1] > 0)
1355 && (heapinfo2->busy_frag.ignore[frag2] ==
1356 heapinfo1->busy_frag.ignore[frag1]))
1357 check_ignore = heapinfo1->busy_frag.ignore[frag1];
1362 xbt_dynar_free(&previous);
1368 /* Start comparison */
1371 compare_heap_area_with_type(state, process_index, area1, area2, snapshot1, snapshot2,
1372 previous, type, size, check_ignore,
1376 compare_heap_area_without_type(state, process_index, area1, area2, snapshot1, snapshot2,
1377 previous, size, check_ignore);
1379 if (res_compare == 1) {
1381 xbt_dynar_free(&previous);
1386 match_equals(state, previous);
1387 xbt_dynar_free(&previous);