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,
749 // HACK: This should not happen but in pratice, there is some
750 // DW_TAG_typedef without DW_AT_type. We should fix this somehow.
754 if (is_stack(real_area1) && is_stack(real_area2))
756 ssize_t ignore1, ignore2;
758 if ((check_ignore > 0)
759 && ((ignore1 = heap_comparison_ignore_size(state->to_ignore1, real_area1))
761 && ((ignore2 = heap_comparison_ignore_size(state->to_ignore2, real_area2))
765 simgrid::mc::Type *subtype, *subsubtype;
767 const void *addr_pointed1, *addr_pointed2;
769 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
770 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
772 switch (type->type) {
773 case DW_TAG_unspecified_type:
776 case DW_TAG_base_type:
777 if (!type->name.empty() && type->name == "char") { /* String, hence random (arbitrary ?) size */
778 if (real_area1 == real_area2)
781 return (MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, area_size) != 0);
783 if (area_size != -1 && type->byte_size != area_size)
786 return (MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0);
789 case DW_TAG_enumeration_type:
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);
796 case DW_TAG_const_type:
797 case DW_TAG_volatile_type:
799 type = type->subtype;
802 case DW_TAG_array_type:
803 subtype = type->subtype;
804 switch (subtype->type) {
805 case DW_TAG_unspecified_type:
808 case DW_TAG_base_type:
809 case DW_TAG_enumeration_type:
810 case DW_TAG_pointer_type:
811 case DW_TAG_reference_type:
812 case DW_TAG_rvalue_reference_type:
813 case DW_TAG_structure_type:
814 case DW_TAG_class_type:
815 case DW_TAG_union_type:
816 if (subtype->full_type)
817 subtype = subtype->full_type;
818 elm_size = subtype->byte_size;
820 // TODO, just remove the type indirection?
821 case DW_TAG_const_type:
823 case DW_TAG_volatile_type:
824 subsubtype = subtype->subtype;
825 if (subsubtype->full_type)
826 subsubtype = subsubtype->full_type;
827 elm_size = subsubtype->byte_size;
833 for (int i = 0; i < type->element_count; i++) {
834 // TODO, add support for variable stride (DW_AT_byte_stride)
836 compare_heap_area_with_type(state, process_index,
837 (char *) real_area1 + (i * elm_size),
838 (char *) real_area2 + (i * elm_size),
839 snapshot1, snapshot2, previous,
840 type->subtype, subtype->byte_size,
841 check_ignore, pointer_level);
846 case DW_TAG_reference_type:
847 case DW_TAG_rvalue_reference_type:
848 case DW_TAG_pointer_type:
849 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
850 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
851 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
852 return (addr_pointed1 != addr_pointed2);;
855 if (pointer_level > 1) { /* Array of pointers */
856 for (size_t i = 0; i < (area_size / sizeof(void *)); i++) {
857 addr_pointed1 = snapshot1->read(
858 remote((void**)((char*) real_area1 + i * sizeof(void *))),
860 addr_pointed2 = snapshot2->read(
861 remote((void**)((char*) real_area2 + i * sizeof(void *))),
863 if (addr_pointed1 > state->std_heap_copy.heapbase
864 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
865 && addr_pointed2 > state->std_heap_copy.heapbase
866 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
868 compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
869 snapshot2, previous, type->subtype,
872 res = (addr_pointed1 != addr_pointed2);
877 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
878 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
879 if (addr_pointed1 > state->std_heap_copy.heapbase
880 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
881 && addr_pointed2 > state->std_heap_copy.heapbase
882 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
883 return compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
884 snapshot2, previous, type->subtype,
887 return (addr_pointed1 != addr_pointed2);
891 case DW_TAG_structure_type:
892 case DW_TAG_class_type:
894 type = type->full_type;
895 if (area_size != -1 && type->byte_size != area_size) {
896 if (area_size > type->byte_size && area_size % type->byte_size == 0) {
897 for (size_t i = 0; i < (size_t)(area_size / type->byte_size); i++) {
899 compare_heap_area_with_type(state, process_index,
900 (char *) real_area1 + i * type->byte_size,
901 (char *) real_area2 + i * type->byte_size,
902 snapshot1, snapshot2, previous, type, -1,
910 for(simgrid::mc::Member& member : type->members) {
911 // TODO, optimize this? (for the offset case)
912 void *real_member1 = simgrid::dwarf::resolve_member(
913 real_area1, type, &member, (simgrid::mc::AddressSpace*) snapshot1, process_index);
914 void *real_member2 = simgrid::dwarf::resolve_member(
915 real_area2, type, &member, (simgrid::mc::AddressSpace*) snapshot2, process_index);
917 compare_heap_area_with_type(state, process_index, real_member1, real_member2,
918 snapshot1, snapshot2,
919 previous, member.type, -1,
926 case DW_TAG_union_type:
927 return compare_heap_area_without_type(state, process_index, real_area1, real_area2,
928 snapshot1, snapshot2, previous,
929 type->byte_size, check_ignore);
939 /** Infer the type of a part of the block from the type of the block
941 * TODO, handle DW_TAG_array_type as well as arrays of the object ((*p)[5], p[5])
943 * TODO, handle subfields ((*p).bar.foo, (*p)[5].bar…)
945 * @param type_id DWARF type ID of the root address
947 * @return DWARF type ID for given offset
949 static simgrid::mc::Type* get_offset_type(void *real_base_address, simgrid::mc::Type* type,
950 int offset, int area_size,
951 simgrid::mc::Snapshot* snapshot, int process_index)
954 // Beginning of the block, the infered variable type if the type of the block:
958 switch (type->type) {
959 case DW_TAG_structure_type:
960 case DW_TAG_class_type:
962 type = type->full_type;
964 if (area_size != -1 && type->byte_size != area_size) {
965 if (area_size > type->byte_size && area_size % type->byte_size == 0)
970 for(simgrid::mc::Member& member : type->members) {
972 if (member.has_offset_location()) {
973 // We have the offset, use it directly (shortcut):
974 if (member.offset() == offset)
977 void *real_member = simgrid::dwarf::resolve_member(
978 real_base_address, type, &member, snapshot, process_index);
979 if ((char*) real_member - (char *) real_base_address == offset)
988 /* FIXME : other cases ? */
996 * @param area1 Process address for state 1
997 * @param area2 Process address for state 2
998 * @param snapshot1 Snapshot of state 1
999 * @param snapshot2 Snapshot of state 2
1000 * @param previous Pairs of blocks already compared on the current path (or nullptr)
1001 * @param type_id Type of variable
1002 * @param pointer_level
1003 * @return 0 (same), 1 (different), -1
1005 int compare_heap_area(int process_index, const void *area1, const void *area2, simgrid::mc::Snapshot* snapshot1,
1006 simgrid::mc::Snapshot* snapshot2, xbt_dynar_t previous,
1007 simgrid::mc::Type* type, int pointer_level)
1009 simgrid::mc::Process* process = &mc_model_checker->process();
1011 struct s_mc_diff *state = mc_diff_info;
1014 ssize_t block1, frag1, block2, frag2;
1016 int check_ignore = 0;
1018 void *real_addr_block1, *real_addr_block2, *real_addr_frag1, *real_addr_frag2;
1020 int offset1 = 0, offset2 = 0;
1021 int new_size1 = -1, new_size2 = -1;
1022 simgrid::mc::Type *new_type1 = nullptr, *new_type2 = nullptr;
1024 int match_pairs = 0;
1026 // This is the address of std_heap->heapinfo in the application process:
1027 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
1029 const malloc_info* heapinfos1 = snapshot1->read(
1030 remote((const malloc_info**)heapinfo_address), process_index);
1031 const malloc_info* heapinfos2 = snapshot2->read(
1032 remote((const malloc_info**)heapinfo_address), process_index);
1034 malloc_info heapinfo_temp1, heapinfo_temp2;
1036 if (previous == nullptr) {
1038 xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
1041 // Get block number:
1044 (char *) state->std_heap_copy.heapbase) / BLOCKSIZE + 1;
1047 (char *) state->std_heap_copy.heapbase) / BLOCKSIZE + 1;
1049 // If either block is a stack block:
1050 if (is_block_stack((int) block1) && is_block_stack((int) block2)) {
1051 add_heap_area_pair(previous, block1, -1, block2, -1);
1053 match_equals(state, previous);
1054 xbt_dynar_free(&previous);
1058 // If either block is not in the expected area of memory:
1059 if (((char *) area1 < (char *) state->std_heap_copy.heapbase)
1060 || (block1 > (ssize_t) state->heapsize1) || (block1 < 1)
1061 || ((char *) area2 < (char *) state->std_heap_copy.heapbase)
1062 || (block2 > (ssize_t) state->heapsize2) || (block2 < 1)) {
1064 xbt_dynar_free(&previous);
1068 // Process address of the block:
1069 real_addr_block1 = (ADDR2UINT(block1) - 1) * BLOCKSIZE +
1070 (char *) state->std_heap_copy.heapbase;
1071 real_addr_block2 = (ADDR2UINT(block2) - 1) * BLOCKSIZE +
1072 (char *) state->std_heap_copy.heapbase;
1076 if (type->full_type)
1077 type = type->full_type;
1079 // This assume that for "boring" types (volatile ...) byte_size is absent:
1080 while (type->byte_size == 0 && type->subtype != nullptr)
1081 type = type->subtype;
1084 if ((type->type == DW_TAG_pointer_type)
1085 || ((type->type == DW_TAG_base_type) && !type->name.empty()
1086 && type->name == "char"))
1089 type_size = type->byte_size;
1093 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
1094 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
1096 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
1097 heap_region1, &heapinfo_temp1, &heapinfos1[block1], sizeof(malloc_info));
1098 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
1099 heap_region2, &heapinfo_temp2, &heapinfos2[block2], sizeof(malloc_info));
1101 if ((heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type==MMALLOC_TYPE_HEAPINFO)
1102 && (heapinfo2->type == MMALLOC_TYPE_FREE || heapinfo2->type ==MMALLOC_TYPE_HEAPINFO)) {
1106 match_equals(state, previous);
1107 xbt_dynar_free(&previous);
1111 } else if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED
1112 && heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
1113 /* Complete block */
1115 // TODO, lookup variable type from block type as done for fragmented blocks
1117 offset1 = (char *) area1 - (char *) real_addr_block1;
1118 offset2 = (char *) area2 - (char *) real_addr_block2;
1120 if (state->equals_to1_(block1, 0).valid
1121 && state->equals_to2_(block2, 0).valid) {
1122 if (equal_blocks(state, block1, block2)) {
1124 match_equals(state, previous);
1125 xbt_dynar_free(&previous);
1131 if (type_size != -1) {
1132 if (type_size != (ssize_t) heapinfo1->busy_block.busy_size
1133 && type_size != (ssize_t) heapinfo2->busy_block.busy_size
1134 && (type->name.empty() || type->name == "struct s_smx_context")) {
1136 match_equals(state, previous);
1137 xbt_dynar_free(&previous);
1143 if (heapinfo1->busy_block.size !=
1144 heapinfo2->busy_block.size) {
1146 xbt_dynar_free(&previous);
1150 if (heapinfo1->busy_block.busy_size !=
1151 heapinfo2->busy_block.busy_size) {
1153 xbt_dynar_free(&previous);
1157 if (!add_heap_area_pair(previous, block1, -1, block2, -1)) {
1159 match_equals(state, previous);
1160 xbt_dynar_free(&previous);
1165 size = heapinfo1->busy_block.busy_size;
1167 // Remember (basic) type inference.
1168 // The current data structure only allows us to do this for the whole block.
1169 if (type != nullptr && area1 == real_addr_block1)
1170 state->types1_(block1, 0) = type;
1171 if (type != nullptr && area2 == real_addr_block2)
1172 state->types2_(block2, 0) = type;
1176 match_equals(state, previous);
1177 xbt_dynar_free(&previous);
1185 if ((heapinfo1->busy_block.ignore > 0)
1186 && (heapinfo2->busy_block.ignore ==
1187 heapinfo1->busy_block.ignore))
1188 check_ignore = heapinfo1->busy_block.ignore;
1190 } else if ((heapinfo1->type > 0) && (heapinfo2->type > 0)) { /* Fragmented block */
1194 ((uintptr_t) (ADDR2UINT(area1) % (BLOCKSIZE))) >> heapinfo1->type;
1196 ((uintptr_t) (ADDR2UINT(area2) % (BLOCKSIZE))) >> heapinfo2->type;
1198 // Process address of the fragment:
1200 (void *) ((char *) real_addr_block1 +
1201 (frag1 << heapinfo1->type));
1203 (void *) ((char *) real_addr_block2 +
1204 (frag2 << heapinfo2->type));
1206 // Check the size of the fragments against the size of the type:
1207 if (type_size != -1) {
1208 if (heapinfo1->busy_frag.frag_size[frag1] == -1
1209 || heapinfo2->busy_frag.frag_size[frag2] == -1) {
1211 match_equals(state, previous);
1212 xbt_dynar_free(&previous);
1217 if (type_size != heapinfo1->busy_frag.frag_size[frag1]
1218 || type_size != heapinfo2->busy_frag.frag_size[frag2]) {
1220 match_equals(state, previous);
1221 xbt_dynar_free(&previous);
1227 // Check if the blocks are already matched together:
1228 if (state->equals_to1_(block1, frag1).valid
1229 && state->equals_to2_(block2, frag2).valid) {
1230 if (offset1==offset2 && equal_fragments(state, block1, frag1, block2, frag2)) {
1232 match_equals(state, previous);
1233 xbt_dynar_free(&previous);
1238 // Compare the size of both fragments:
1239 if (heapinfo1->busy_frag.frag_size[frag1] !=
1240 heapinfo2->busy_frag.frag_size[frag2]) {
1241 if (type_size == -1) {
1243 match_equals(state, previous);
1244 xbt_dynar_free(&previous);
1249 xbt_dynar_free(&previous);
1254 // Size of the fragment:
1255 size = heapinfo1->busy_frag.frag_size[frag1];
1257 // Remember (basic) type inference.
1258 // The current data structure only allows us to do this for the whole fragment.
1259 if (type != nullptr && area1 == real_addr_frag1)
1260 state->types1_(block1, frag1) = type;
1261 if (type != nullptr && area2 == real_addr_frag2)
1262 state->types2_(block2, frag2) = type;
1264 // The type of the variable is already known:
1269 // Type inference from the block type.
1270 else if (state->types1_(block1, frag1) != nullptr
1271 || state->types2_(block2, frag2) != nullptr) {
1273 offset1 = (char *) area1 - (char *) real_addr_frag1;
1274 offset2 = (char *) area2 - (char *) real_addr_frag2;
1276 if (state->types1_(block1, frag1) != nullptr
1277 && state->types2_(block2, frag2) != nullptr) {
1279 get_offset_type(real_addr_frag1, state->types1_(block1, frag1),
1280 offset1, size, snapshot1, process_index);
1282 get_offset_type(real_addr_frag2, state->types2_(block2, frag2),
1283 offset1, size, snapshot2, process_index);
1284 } else if (state->types1_(block1, frag1) != nullptr) {
1286 get_offset_type(real_addr_frag1, state->types1_(block1, frag1),
1287 offset1, size, snapshot1, process_index);
1289 get_offset_type(real_addr_frag2, state->types1_(block1, frag1),
1290 offset2, size, snapshot2, process_index);
1291 } else if (state->types2_(block2, frag2) != nullptr) {
1293 get_offset_type(real_addr_frag1, state->types2_(block2, frag2),
1294 offset1, size, snapshot1, process_index);
1296 get_offset_type(real_addr_frag2, state->types2_(block2, frag2),
1297 offset2, size, snapshot2, process_index);
1300 match_equals(state, previous);
1301 xbt_dynar_free(&previous);
1306 if (new_type1 != nullptr && new_type2 != nullptr && new_type1 != new_type2) {
1309 while (type->byte_size == 0 && type->subtype != nullptr)
1310 type = type->subtype;
1311 new_size1 = type->byte_size;
1314 while (type->byte_size == 0 && type->subtype != nullptr)
1315 type = type->subtype;
1316 new_size2 = type->byte_size;
1320 match_equals(state, previous);
1321 xbt_dynar_free(&previous);
1327 if (new_size1 > 0 && new_size1 == new_size2) {
1332 if (offset1 == 0 && offset2 == 0
1333 && !add_heap_area_pair(previous, block1, frag1, block2, frag2)) {
1335 match_equals(state, previous);
1336 xbt_dynar_free(&previous);
1343 match_equals(state, previous);
1344 xbt_dynar_free(&previous);
1349 if ((heapinfo1->busy_frag.ignore[frag1] > 0)
1350 && (heapinfo2->busy_frag.ignore[frag2] ==
1351 heapinfo1->busy_frag.ignore[frag1]))
1352 check_ignore = heapinfo1->busy_frag.ignore[frag1];
1357 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 match_equals(state, previous);
1382 xbt_dynar_free(&previous);