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);
568 //mmalloc_backtrace_block_display((void*)heapinfo1, i);
575 if (heapinfo1->type > 0) {
576 for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo1->type); j++) {
577 if (i1 == state->heaplimit) {
578 if (heapinfo1->busy_frag.frag_size[j] > 0) {
579 if (state->equals_to1_(i, j).valid == 0) {
580 if (XBT_LOG_ISENABLED(mc_diff, xbt_log_priority_debug)) {
581 // TODO, print fragment address
583 ("Block %zu, Fragment %zu not found (size used = %zd)",
585 heapinfo1->busy_frag.frag_size[j]);
586 //mmalloc_backtrace_fragment_display((void*)heapinfo1, i, j);
596 if (i1 == state->heaplimit)
597 XBT_DEBUG("Number of blocks/fragments not found in heap1 : %d", nb_diff1);
599 for (i=1; i <= state->heaplimit; i++) {
600 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
601 heap_region2, &heapinfo_temp2, &heapinfos2[i], sizeof(malloc_info));
602 if (heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
603 if (i1 == state->heaplimit) {
604 if (heapinfo2->busy_block.busy_size > 0) {
605 if (state->equals_to2_(i, 0).valid == 0) {
606 if (XBT_LOG_ISENABLED(mc_diff, xbt_log_priority_debug)) {
607 // TODO, print address of the block
608 XBT_DEBUG("Block %zu not found (size used = %zu)", i,
609 heapinfo2->busy_block.busy_size);
610 //mmalloc_backtrace_block_display((void*)heapinfo2, i);
617 if (heapinfo2->type > 0) {
618 for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo2->type); j++) {
619 if (i1 == state->heaplimit) {
620 if (heapinfo2->busy_frag.frag_size[j] > 0) {
621 if (state->equals_to2_(i, j).valid == 0) {
622 if (XBT_LOG_ISENABLED(mc_diff, xbt_log_priority_debug)) {
623 // TODO, print address of the block
625 ("Block %zu, Fragment %zu not found (size used = %zd)",
627 heapinfo2->busy_frag.frag_size[j]);
628 //mmalloc_backtrace_fragment_display((void*)heapinfo2, i, j);
638 if (i1 == state->heaplimit)
639 XBT_DEBUG("Number of blocks/fragments not found in heap2 : %d", nb_diff2);
641 return ((nb_diff1 > 0) || (nb_diff2 > 0));
647 * @param real_area1 Process address for state 1
648 * @param real_area2 Process address for state 2
649 * @param snapshot1 Snapshot of state 1
650 * @param snapshot2 Snapshot of state 2
653 * @param check_ignore
655 static int compare_heap_area_without_type(struct s_mc_diff *state, int process_index,
656 const void *real_area1, const void *real_area2,
657 simgrid::mc::Snapshot* snapshot1,
658 simgrid::mc::Snapshot* snapshot2,
659 xbt_dynar_t previous, int size,
662 simgrid::mc::Process* process = &mc_model_checker->process();
665 const void *addr_pointed1, *addr_pointed2;
666 int pointer_align, res_compare;
667 ssize_t ignore1, ignore2;
669 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
670 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
674 if (check_ignore > 0) {
676 heap_comparison_ignore_size(state->to_ignore1,
677 (char *) real_area1 + i)) != -1) {
679 heap_comparison_ignore_size(state->to_ignore2,
680 (char *) real_area2 + i)) == ignore1) {
693 if (MC_snapshot_region_memcmp(((char *) real_area1) + i, heap_region1, ((char *) real_area2) + i, heap_region2, 1) != 0) {
695 pointer_align = (i / sizeof(void *)) * sizeof(void *);
696 addr_pointed1 = snapshot1->read(
697 remote((void**)((char *) real_area1 + pointer_align)), process_index);
698 addr_pointed2 = snapshot2->read(
699 remote((void**)((char *) real_area2 + pointer_align)), process_index);
701 if (process->in_maestro_stack(remote(addr_pointed1))
702 && process->in_maestro_stack(remote(addr_pointed2))) {
703 i = pointer_align + sizeof(void *);
705 } else if (addr_pointed1 > state->std_heap_copy.heapbase
706 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
707 && addr_pointed2 > state->std_heap_copy.heapbase
708 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)) {
709 // Both addreses are in the heap:
711 compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
712 snapshot2, previous, nullptr, 0);
713 if (res_compare == 1)
715 i = pointer_align + sizeof(void *);
733 * @param real_area1 Process address for state 1
734 * @param real_area2 Process address for state 2
735 * @param snapshot1 Snapshot of state 1
736 * @param snapshot2 Snapshot of state 2
739 * @param area_size either a byte_size or an elements_count (?)
740 * @param check_ignore
741 * @param pointer_level
742 * @return 0 (same), 1 (different), -1 (unknown)
744 static int compare_heap_area_with_type(struct s_mc_diff *state, int process_index,
745 const void *real_area1, const void *real_area2,
746 simgrid::mc::Snapshot* snapshot1,
747 simgrid::mc::Snapshot* snapshot2,
748 xbt_dynar_t previous, simgrid::mc::Type* type,
749 int area_size, int check_ignore,
753 // HACK: This should not happen but in pratice, there is some
754 // DW_TAG_typedef without DW_AT_type. We should fix this somehow.
758 if (is_stack(real_area1) && is_stack(real_area2))
760 ssize_t ignore1, ignore2;
762 if ((check_ignore > 0)
763 && ((ignore1 = heap_comparison_ignore_size(state->to_ignore1, real_area1))
765 && ((ignore2 = heap_comparison_ignore_size(state->to_ignore2, real_area2))
769 simgrid::mc::Type *subtype, *subsubtype;
771 const void *addr_pointed1, *addr_pointed2;
773 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
774 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
776 switch (type->type) {
777 case DW_TAG_unspecified_type:
780 case DW_TAG_base_type:
781 if (!type->name.empty() && type->name == "char") { /* String, hence random (arbitrary ?) size */
782 if (real_area1 == real_area2)
785 return (MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, area_size) != 0);
787 if (area_size != -1 && type->byte_size != area_size)
790 return (MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0);
793 case DW_TAG_enumeration_type:
794 if (area_size != -1 && type->byte_size != area_size)
797 return (MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0);
800 case DW_TAG_const_type:
801 case DW_TAG_volatile_type:
803 type = type->subtype;
806 case DW_TAG_array_type:
807 subtype = type->subtype;
808 switch (subtype->type) {
809 case DW_TAG_unspecified_type:
812 case DW_TAG_base_type:
813 case DW_TAG_enumeration_type:
814 case DW_TAG_pointer_type:
815 case DW_TAG_reference_type:
816 case DW_TAG_rvalue_reference_type:
817 case DW_TAG_structure_type:
818 case DW_TAG_class_type:
819 case DW_TAG_union_type:
820 if (subtype->full_type)
821 subtype = subtype->full_type;
822 elm_size = subtype->byte_size;
824 // TODO, just remove the type indirection?
825 case DW_TAG_const_type:
827 case DW_TAG_volatile_type:
828 subsubtype = subtype->subtype;
829 if (subsubtype->full_type)
830 subsubtype = subsubtype->full_type;
831 elm_size = subsubtype->byte_size;
837 for (int i = 0; i < type->element_count; i++) {
838 // TODO, add support for variable stride (DW_AT_byte_stride)
840 compare_heap_area_with_type(state, process_index,
841 (char *) real_area1 + (i * elm_size),
842 (char *) real_area2 + (i * elm_size),
843 snapshot1, snapshot2, previous,
844 type->subtype, subtype->byte_size,
845 check_ignore, pointer_level);
850 case DW_TAG_reference_type:
851 case DW_TAG_rvalue_reference_type:
852 case DW_TAG_pointer_type:
853 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
854 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
855 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
856 return (addr_pointed1 != addr_pointed2);;
859 if (pointer_level > 1) { /* Array of pointers */
860 for (size_t i = 0; i < (area_size / sizeof(void *)); i++) {
861 addr_pointed1 = snapshot1->read(
862 remote((void**)((char*) real_area1 + i * sizeof(void *))),
864 addr_pointed2 = snapshot2->read(
865 remote((void**)((char*) real_area2 + i * sizeof(void *))),
867 if (addr_pointed1 > state->std_heap_copy.heapbase
868 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
869 && addr_pointed2 > state->std_heap_copy.heapbase
870 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
872 compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
873 snapshot2, previous, type->subtype,
876 res = (addr_pointed1 != addr_pointed2);
881 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
882 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
883 if (addr_pointed1 > state->std_heap_copy.heapbase
884 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
885 && addr_pointed2 > state->std_heap_copy.heapbase
886 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
887 return compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
888 snapshot2, previous, type->subtype,
891 return (addr_pointed1 != addr_pointed2);
895 case DW_TAG_structure_type:
896 case DW_TAG_class_type:
898 type = type->full_type;
899 if (area_size != -1 && type->byte_size != area_size) {
900 if (area_size > type->byte_size && area_size % type->byte_size == 0) {
901 for (size_t i = 0; i < (size_t)(area_size / type->byte_size); i++) {
903 compare_heap_area_with_type(state, process_index,
904 (char *) real_area1 + i * type->byte_size,
905 (char *) real_area2 + i * type->byte_size,
906 snapshot1, snapshot2, previous, type, -1,
914 for(simgrid::mc::Member& member : type->members) {
915 // TODO, optimize this? (for the offset case)
916 void *real_member1 = simgrid::dwarf::resolve_member(
917 real_area1, type, &member, (simgrid::mc::AddressSpace*) snapshot1, process_index);
918 void *real_member2 = simgrid::dwarf::resolve_member(
919 real_area2, type, &member, (simgrid::mc::AddressSpace*) snapshot2, process_index);
921 compare_heap_area_with_type(state, process_index, real_member1, real_member2,
922 snapshot1, snapshot2,
923 previous, member.type, -1,
930 case DW_TAG_union_type:
931 return compare_heap_area_without_type(state, process_index, real_area1, real_area2,
932 snapshot1, snapshot2, previous,
933 type->byte_size, check_ignore);
943 /** Infer the type of a part of the block from the type of the block
945 * TODO, handle DW_TAG_array_type as well as arrays of the object ((*p)[5], p[5])
947 * TODO, handle subfields ((*p).bar.foo, (*p)[5].bar…)
949 * @param type_id DWARF type ID of the root address
951 * @return DWARF type ID for given offset
953 static simgrid::mc::Type* get_offset_type(void *real_base_address, simgrid::mc::Type* type,
954 int offset, int area_size,
955 simgrid::mc::Snapshot* snapshot, int process_index)
958 // Beginning of the block, the infered variable type if the type of the block:
962 switch (type->type) {
963 case DW_TAG_structure_type:
964 case DW_TAG_class_type:
966 type = type->full_type;
968 if (area_size != -1 && type->byte_size != area_size) {
969 if (area_size > type->byte_size && area_size % type->byte_size == 0)
974 for(simgrid::mc::Member& member : type->members) {
976 if (member.has_offset_location()) {
977 // We have the offset, use it directly (shortcut):
978 if (member.offset() == offset)
981 void *real_member = simgrid::dwarf::resolve_member(
982 real_base_address, type, &member, snapshot, process_index);
983 if ((char*) real_member - (char *) real_base_address == offset)
992 /* FIXME : other cases ? */
1000 * @param area1 Process address for state 1
1001 * @param area2 Process address for state 2
1002 * @param snapshot1 Snapshot of state 1
1003 * @param snapshot2 Snapshot of state 2
1004 * @param previous Pairs of blocks already compared on the current path (or nullptr)
1005 * @param type_id Type of variable
1006 * @param pointer_level
1007 * @return 0 (same), 1 (different), -1
1009 int compare_heap_area(int process_index, const void *area1, const void *area2, simgrid::mc::Snapshot* snapshot1,
1010 simgrid::mc::Snapshot* snapshot2, xbt_dynar_t previous,
1011 simgrid::mc::Type* type, int pointer_level)
1013 simgrid::mc::Process* process = &mc_model_checker->process();
1015 struct s_mc_diff *state = mc_diff_info;
1018 ssize_t block1, frag1, block2, frag2;
1020 int check_ignore = 0;
1022 void *real_addr_block1, *real_addr_block2, *real_addr_frag1, *real_addr_frag2;
1024 int offset1 = 0, offset2 = 0;
1025 int new_size1 = -1, new_size2 = -1;
1026 simgrid::mc::Type *new_type1 = nullptr, *new_type2 = nullptr;
1028 int match_pairs = 0;
1030 // This is the address of std_heap->heapinfo in the application process:
1031 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
1033 const malloc_info* heapinfos1 = snapshot1->read(
1034 remote((const malloc_info**)heapinfo_address), process_index);
1035 const malloc_info* heapinfos2 = snapshot2->read(
1036 remote((const malloc_info**)heapinfo_address), process_index);
1038 malloc_info heapinfo_temp1, heapinfo_temp2;
1040 if (previous == nullptr) {
1042 xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
1045 // Get block number:
1048 (char *) state->std_heap_copy.heapbase) / BLOCKSIZE + 1;
1051 (char *) state->std_heap_copy.heapbase) / BLOCKSIZE + 1;
1053 // If either block is a stack block:
1054 if (is_block_stack((int) block1) && is_block_stack((int) block2)) {
1055 add_heap_area_pair(previous, block1, -1, block2, -1);
1057 match_equals(state, previous);
1058 xbt_dynar_free(&previous);
1062 // If either block is not in the expected area of memory:
1063 if (((char *) area1 < (char *) state->std_heap_copy.heapbase)
1064 || (block1 > (ssize_t) state->heapsize1) || (block1 < 1)
1065 || ((char *) area2 < (char *) state->std_heap_copy.heapbase)
1066 || (block2 > (ssize_t) state->heapsize2) || (block2 < 1)) {
1068 xbt_dynar_free(&previous);
1072 // Process address of the block:
1073 real_addr_block1 = (ADDR2UINT(block1) - 1) * BLOCKSIZE +
1074 (char *) state->std_heap_copy.heapbase;
1075 real_addr_block2 = (ADDR2UINT(block2) - 1) * BLOCKSIZE +
1076 (char *) state->std_heap_copy.heapbase;
1080 if (type->full_type)
1081 type = type->full_type;
1083 // This assume that for "boring" types (volatile ...) byte_size is absent:
1084 while (type->byte_size == 0 && type->subtype != nullptr)
1085 type = type->subtype;
1088 if ((type->type == DW_TAG_pointer_type)
1089 || ((type->type == DW_TAG_base_type) && !type->name.empty()
1090 && type->name == "char"))
1093 type_size = type->byte_size;
1097 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
1098 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
1100 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
1101 heap_region1, &heapinfo_temp1, &heapinfos1[block1], sizeof(malloc_info));
1102 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
1103 heap_region2, &heapinfo_temp2, &heapinfos2[block2], sizeof(malloc_info));
1105 if ((heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type==MMALLOC_TYPE_HEAPINFO)
1106 && (heapinfo2->type == MMALLOC_TYPE_FREE || heapinfo2->type ==MMALLOC_TYPE_HEAPINFO)) {
1110 match_equals(state, previous);
1111 xbt_dynar_free(&previous);
1115 } else if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED
1116 && heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
1117 /* Complete block */
1119 // TODO, lookup variable type from block type as done for fragmented blocks
1121 offset1 = (char *) area1 - (char *) real_addr_block1;
1122 offset2 = (char *) area2 - (char *) real_addr_block2;
1124 if (state->equals_to1_(block1, 0).valid
1125 && state->equals_to2_(block2, 0).valid) {
1126 if (equal_blocks(state, block1, block2)) {
1128 match_equals(state, previous);
1129 xbt_dynar_free(&previous);
1135 if (type_size != -1) {
1136 if (type_size != (ssize_t) heapinfo1->busy_block.busy_size
1137 && type_size != (ssize_t) heapinfo2->busy_block.busy_size
1138 && (type->name.empty() || type->name == "struct s_smx_context")) {
1140 match_equals(state, previous);
1141 xbt_dynar_free(&previous);
1147 if (heapinfo1->busy_block.size !=
1148 heapinfo2->busy_block.size) {
1150 xbt_dynar_free(&previous);
1154 if (heapinfo1->busy_block.busy_size !=
1155 heapinfo2->busy_block.busy_size) {
1157 xbt_dynar_free(&previous);
1161 if (!add_heap_area_pair(previous, block1, -1, block2, -1)) {
1163 match_equals(state, previous);
1164 xbt_dynar_free(&previous);
1169 size = heapinfo1->busy_block.busy_size;
1171 // Remember (basic) type inference.
1172 // The current data structure only allows us to do this for the whole block.
1173 if (type != nullptr && area1 == real_addr_block1)
1174 state->types1_(block1, 0) = type;
1175 if (type != nullptr && area2 == real_addr_block2)
1176 state->types2_(block2, 0) = type;
1180 match_equals(state, previous);
1181 xbt_dynar_free(&previous);
1189 if ((heapinfo1->busy_block.ignore > 0)
1190 && (heapinfo2->busy_block.ignore ==
1191 heapinfo1->busy_block.ignore))
1192 check_ignore = heapinfo1->busy_block.ignore;
1194 } else if ((heapinfo1->type > 0) && (heapinfo2->type > 0)) { /* Fragmented block */
1198 ((uintptr_t) (ADDR2UINT(area1) % (BLOCKSIZE))) >> heapinfo1->type;
1200 ((uintptr_t) (ADDR2UINT(area2) % (BLOCKSIZE))) >> heapinfo2->type;
1202 // Process address of the fragment:
1204 (void *) ((char *) real_addr_block1 +
1205 (frag1 << heapinfo1->type));
1207 (void *) ((char *) real_addr_block2 +
1208 (frag2 << heapinfo2->type));
1210 // Check the size of the fragments against the size of the type:
1211 if (type_size != -1) {
1212 if (heapinfo1->busy_frag.frag_size[frag1] == -1
1213 || heapinfo2->busy_frag.frag_size[frag2] == -1) {
1215 match_equals(state, previous);
1216 xbt_dynar_free(&previous);
1221 if (type_size != heapinfo1->busy_frag.frag_size[frag1]
1222 || type_size != heapinfo2->busy_frag.frag_size[frag2]) {
1224 match_equals(state, previous);
1225 xbt_dynar_free(&previous);
1231 // Check if the blocks are already matched together:
1232 if (state->equals_to1_(block1, frag1).valid
1233 && state->equals_to2_(block2, frag2).valid) {
1234 if (offset1==offset2 && equal_fragments(state, block1, frag1, block2, frag2)) {
1236 match_equals(state, previous);
1237 xbt_dynar_free(&previous);
1242 // Compare the size of both fragments:
1243 if (heapinfo1->busy_frag.frag_size[frag1] !=
1244 heapinfo2->busy_frag.frag_size[frag2]) {
1245 if (type_size == -1) {
1247 match_equals(state, previous);
1248 xbt_dynar_free(&previous);
1253 xbt_dynar_free(&previous);
1258 // Size of the fragment:
1259 size = heapinfo1->busy_frag.frag_size[frag1];
1261 // Remember (basic) type inference.
1262 // The current data structure only allows us to do this for the whole fragment.
1263 if (type != nullptr && area1 == real_addr_frag1)
1264 state->types1_(block1, frag1) = type;
1265 if (type != nullptr && area2 == real_addr_frag2)
1266 state->types2_(block2, frag2) = type;
1268 // The type of the variable is already known:
1273 // Type inference from the block type.
1274 else if (state->types1_(block1, frag1) != nullptr
1275 || state->types2_(block2, frag2) != nullptr) {
1277 offset1 = (char *) area1 - (char *) real_addr_frag1;
1278 offset2 = (char *) area2 - (char *) real_addr_frag2;
1280 if (state->types1_(block1, frag1) != nullptr
1281 && state->types2_(block2, frag2) != nullptr) {
1283 get_offset_type(real_addr_frag1, state->types1_(block1, frag1),
1284 offset1, size, snapshot1, process_index);
1286 get_offset_type(real_addr_frag2, state->types2_(block2, frag2),
1287 offset1, size, snapshot2, process_index);
1288 } else if (state->types1_(block1, frag1) != nullptr) {
1290 get_offset_type(real_addr_frag1, state->types1_(block1, frag1),
1291 offset1, size, snapshot1, process_index);
1293 get_offset_type(real_addr_frag2, state->types1_(block1, frag1),
1294 offset2, size, snapshot2, process_index);
1295 } else if (state->types2_(block2, frag2) != nullptr) {
1297 get_offset_type(real_addr_frag1, state->types2_(block2, frag2),
1298 offset1, size, snapshot1, process_index);
1300 get_offset_type(real_addr_frag2, state->types2_(block2, frag2),
1301 offset2, size, snapshot2, process_index);
1304 match_equals(state, previous);
1305 xbt_dynar_free(&previous);
1310 if (new_type1 != nullptr && new_type2 != nullptr && new_type1 != new_type2) {
1313 while (type->byte_size == 0 && type->subtype != nullptr)
1314 type = type->subtype;
1315 new_size1 = type->byte_size;
1318 while (type->byte_size == 0 && type->subtype != nullptr)
1319 type = type->subtype;
1320 new_size2 = type->byte_size;
1324 match_equals(state, previous);
1325 xbt_dynar_free(&previous);
1331 if (new_size1 > 0 && new_size1 == new_size2) {
1336 if (offset1 == 0 && offset2 == 0
1337 && !add_heap_area_pair(previous, block1, frag1, block2, frag2)) {
1339 match_equals(state, previous);
1340 xbt_dynar_free(&previous);
1347 match_equals(state, previous);
1348 xbt_dynar_free(&previous);
1353 if ((heapinfo1->busy_frag.ignore[frag1] > 0)
1354 && (heapinfo2->busy_frag.ignore[frag2] ==
1355 heapinfo1->busy_frag.ignore[frag1]))
1356 check_ignore = heapinfo1->busy_frag.ignore[frag1];
1361 xbt_dynar_free(&previous);
1367 /* Start comparison */
1370 compare_heap_area_with_type(state, process_index, area1, area2, snapshot1, snapshot2,
1371 previous, type, size, check_ignore,
1375 compare_heap_area_without_type(state, process_index, area1, area2, snapshot1, snapshot2,
1376 previous, size, check_ignore);
1378 if (res_compare == 1) {
1380 xbt_dynar_free(&previous);
1385 match_equals(state, previous);
1386 xbt_dynar_free(&previous);