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 */
12 #include "xbt/mmalloc.h"
13 #include "mc/datatypes.h"
14 #include "src/mc/malloc.hpp"
15 #include "src/mc/mc_private.h"
16 #include "src/mc/mc_snapshot.h"
17 #include "src/mc/mc_dwarf.hpp"
18 #include "src/mc/Type.hpp"
20 using simgrid::mc::remote;
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_diff, xbt,
23 "Logging specific to mc_diff in mc");
25 /*********************************** Heap comparison ***********************************/
26 /***************************************************************************************/
28 struct XBT_PRIVATE s_mc_diff {
29 s_xbt_mheap_t std_heap_copy;
30 std::size_t heaplimit;
31 // Number of blocks in the heaps:
32 std::size_t heapsize1, heapsize2;
33 std::vector<simgrid::mc::IgnoredHeapRegion>* to_ignore1;
34 std::vector<simgrid::mc::IgnoredHeapRegion>* to_ignore2;
35 s_heap_area_t *equals_to1, *equals_to2;
36 simgrid::mc::Type **types1;
37 simgrid::mc::Type **types2;
38 std::size_t available;
41 #define equals_to1_(i,j) equals_to1[ MAX_FRAGMENT_PER_BLOCK*(i) + (j)]
42 #define equals_to2_(i,j) equals_to2[ MAX_FRAGMENT_PER_BLOCK*(i) + (j)]
43 #define types1_(i,j) types1[ MAX_FRAGMENT_PER_BLOCK*(i) + (j)]
44 #define types2_(i,j) types2[ MAX_FRAGMENT_PER_BLOCK*(i) + (j)]
46 static __thread struct s_mc_diff *mc_diff_info = nullptr;
48 /*********************************** Free functions ************************************/
50 static void heap_area_pair_free(heap_area_pair_t pair)
56 static void heap_area_pair_free_voidp(void *d)
58 heap_area_pair_free((heap_area_pair_t) * (void **) d);
61 static void heap_area_free(heap_area_t area)
67 /************************************************************************************/
69 static s_heap_area_t make_heap_area(int block, int fragment)
74 area.fragment = fragment;
79 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1,
80 int block2, int fragment2)
83 unsigned int cursor = 0;
84 heap_area_pair_t current_pair;
86 xbt_dynar_foreach(list, cursor, current_pair)
87 if (current_pair->block1 == block1 && current_pair->block2 == block2
88 && current_pair->fragment1 == fragment1
89 && current_pair->fragment2 == fragment2)
95 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1,
96 int block2, int fragment2)
99 if (is_new_heap_area_pair(list, block1, fragment1, block2, fragment2)) {
100 heap_area_pair_t pair = nullptr;
101 pair = xbt_new0(s_heap_area_pair_t, 1);
102 pair->block1 = block1;
103 pair->fragment1 = fragment1;
104 pair->block2 = block2;
105 pair->fragment2 = fragment2;
107 xbt_dynar_push(list, &pair);
115 static ssize_t heap_comparison_ignore_size(
116 std::vector<simgrid::mc::IgnoredHeapRegion>* ignore_list,
120 int end = ignore_list->size() - 1;
122 while (start <= end) {
123 unsigned int cursor = (start + end) / 2;
124 simgrid::mc::IgnoredHeapRegion const& region = (*ignore_list)[cursor];
125 if (region.address == address)
127 if (region.address < address)
129 if (region.address > address)
136 static bool is_stack(const void *address)
138 for (auto const& stack : mc_model_checker->process().stack_areas())
139 if (address == stack.address)
144 // TODO, this should depend on the snapshot?
145 static bool is_block_stack(int block)
147 for (auto const& stack : mc_model_checker->process().stack_areas())
148 if (block == stack.block)
153 static void match_equals(struct s_mc_diff *state, xbt_dynar_t list)
156 unsigned int cursor = 0;
157 heap_area_pair_t current_pair;
159 xbt_dynar_foreach(list, cursor, current_pair)
161 if (current_pair->fragment1 != -1) {
163 state->equals_to1_(current_pair->block1, current_pair->fragment1) =
164 make_heap_area(current_pair->block2, current_pair->fragment2);
165 state->equals_to2_(current_pair->block2, current_pair->fragment2) =
166 make_heap_area(current_pair->block1, current_pair->fragment1);
170 state->equals_to1_(current_pair->block1, 0) =
171 make_heap_area(current_pair->block2, current_pair->fragment2);
172 state->equals_to2_(current_pair->block2, 0) =
173 make_heap_area(current_pair->block1, current_pair->fragment1);
179 /** Check whether two blocks are known to be matching
181 * @param state State used
182 * @param b1 Block of state 1
183 * @param b2 Block of state 2
184 * @return if the blocks are known to be matching
186 static int equal_blocks(struct s_mc_diff *state, int b1, int b2)
189 if (state->equals_to1_(b1, 0).block == b2
190 && state->equals_to2_(b2, 0).block == b1)
196 /** Check whether two fragments are known to be matching
198 * @param state State used
199 * @param b1 Block of state 1
200 * @param f1 Fragment of state 1
201 * @param b2 Block of state 2
202 * @param f2 Fragment of state 2
203 * @return if the fragments are known to be matching
205 static int equal_fragments(struct s_mc_diff *state, int b1, int f1, int b2,
209 if (state->equals_to1_(b1, f1).block == b2
210 && state->equals_to1_(b1, f1).fragment == f2
211 && state->equals_to2_(b2, f2).block == b1
212 && state->equals_to2_(b2, f2).fragment == f1)
221 int init_heap_information(xbt_mheap_t heap1, xbt_mheap_t heap2,
222 std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
223 std::vector<simgrid::mc::IgnoredHeapRegion>* i2)
225 if (mc_diff_info == nullptr) {
226 mc_diff_info = xbt_new0(struct s_mc_diff, 1);
227 mc_diff_info->equals_to1 = nullptr;
228 mc_diff_info->equals_to2 = nullptr;
229 mc_diff_info->types1 = nullptr;
230 mc_diff_info->types2 = nullptr;
232 struct s_mc_diff *state = mc_diff_info;
234 if ((((struct mdesc *) heap1)->heaplimit !=
235 ((struct mdesc *) heap2)->heaplimit)
237 ((((struct mdesc *) heap1)->heapsize !=
238 ((struct mdesc *) heap2)->heapsize)))
241 state->heaplimit = ((struct mdesc *) heap1)->heaplimit;
243 state->std_heap_copy = *mc_model_checker->process().get_heap();
245 state->heapsize1 = heap1->heapsize;
246 state->heapsize2 = heap2->heapsize;
248 state->to_ignore1 = i1;
249 state->to_ignore2 = i2;
251 if (state->heaplimit > state->available) {
252 state->equals_to1 = (s_heap_area_t*)
253 realloc(state->equals_to1,
254 state->heaplimit * MAX_FRAGMENT_PER_BLOCK *
255 sizeof(s_heap_area_t));
256 state->types1 = (simgrid::mc::Type**)
257 realloc(state->types1,
258 state->heaplimit * MAX_FRAGMENT_PER_BLOCK *
259 sizeof(simgrid::mc::Type*));
260 state->equals_to2 = (s_heap_area_t*)
261 realloc(state->equals_to2,
262 state->heaplimit * MAX_FRAGMENT_PER_BLOCK *
263 sizeof(s_heap_area_t));
264 state->types2 = (simgrid::mc::Type**)
265 realloc(state->types2,
266 state->heaplimit * MAX_FRAGMENT_PER_BLOCK *
267 sizeof(simgrid::mc::Type*));
268 state->available = state->heaplimit;
271 memset(state->equals_to1, 0,
272 state->heaplimit * MAX_FRAGMENT_PER_BLOCK * sizeof(s_heap_area_t));
273 memset(state->equals_to2, 0,
274 state->heaplimit * MAX_FRAGMENT_PER_BLOCK * sizeof(s_heap_area_t));
275 memset(state->types1, 0,
276 state->heaplimit * MAX_FRAGMENT_PER_BLOCK * sizeof(char**));
277 memset(state->types2, 0,
278 state->heaplimit * MAX_FRAGMENT_PER_BLOCK * sizeof(char**));
284 void reset_heap_information()
289 // TODO, have a robust way to find it in O(1)
291 mc_mem_region_t MC_get_heap_region(mc_snapshot_t snapshot)
293 size_t n = snapshot->snapshot_regions.size();
294 for (size_t i=0; i!=n; ++i) {
295 mc_mem_region_t region = snapshot->snapshot_regions[i].get();
296 if (region->region_type() == simgrid::mc::RegionType::Heap)
299 xbt_die("No heap region");
302 int mmalloc_compare_heap(mc_snapshot_t snapshot1, mc_snapshot_t snapshot2)
304 simgrid::mc::Process* process = &mc_model_checker->process();
305 struct s_mc_diff *state = mc_diff_info;
307 /* Start comparison */
308 size_t i1, i2, j1, j2, k;
309 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
310 int nb_diff1 = 0, nb_diff2 = 0;
312 int equal, res_compare = 0;
314 /* Check busy blocks */
318 malloc_info heapinfo_temp1, heapinfo_temp2;
319 malloc_info heapinfo_temp2b;
321 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
322 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
324 // This is the address of std_heap->heapinfo in the application process:
325 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
327 // This is in snapshot do not use them directly:
328 const malloc_info* heapinfos1 = snapshot1->read<malloc_info*>(
329 (std::uint64_t)heapinfo_address, simgrid::mc::ProcessIndexMissing);
330 const malloc_info* heapinfos2 = snapshot2->read<malloc_info*>(
331 (std::uint64_t)heapinfo_address, simgrid::mc::ProcessIndexMissing);
333 while (i1 <= state->heaplimit) {
335 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(heap_region1, &heapinfo_temp1, &heapinfos1[i1], sizeof(malloc_info));
336 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2, &heapinfos2[i1], sizeof(malloc_info));
338 if (heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type == MMALLOC_TYPE_HEAPINFO) { /* Free block */
343 if (heapinfo1->type < 0) {
344 fprintf(stderr, "Unkown mmalloc block type.\n");
349 ((void *) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE +
350 (char *) state->std_heap_copy.heapbase));
352 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED) { /* Large block */
354 if (is_stack(addr_block1)) {
355 for (k = 0; k < heapinfo1->busy_block.size; k++)
356 state->equals_to1_(i1 + k, 0) = make_heap_area(i1, -1);
357 for (k = 0; k < heapinfo2->busy_block.size; k++)
358 state->equals_to2_(i1 + k, 0) = make_heap_area(i1, -1);
359 i1 += heapinfo1->busy_block.size;
363 if (state->equals_to1_(i1, 0).valid) {
372 /* Try first to associate to same block in the other heap */
373 if (heapinfo2->type == heapinfo1->type) {
375 if (state->equals_to2_(i1, 0).valid == 0) {
377 addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE +
378 (char *) state->std_heap_copy.heapbase;
381 compare_heap_area(simgrid::mc::ProcessIndexMissing, addr_block1, addr_block2, snapshot1, snapshot2,
384 if (res_compare != 1) {
385 for (k = 1; k < heapinfo2->busy_block.size; k++)
386 state->equals_to2_(i1 + k, 0) = make_heap_area(i1, -1);
387 for (k = 1; k < heapinfo1->busy_block.size; k++)
388 state->equals_to1_(i1 + k, 0) = make_heap_area(i1, -1);
390 i1 += heapinfo1->busy_block.size;
397 while (i2 <= state->heaplimit && !equal) {
399 addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE +
400 (char *) state->std_heap_copy.heapbase;
407 const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2b, &heapinfos2[i2], sizeof(malloc_info));
409 if (heapinfo2b->type != MMALLOC_TYPE_UNFRAGMENTED) {
414 if (state->equals_to2_(i2, 0).valid) {
420 compare_heap_area(simgrid::mc::ProcessIndexMissing, addr_block1, addr_block2, snapshot1, snapshot2,
423 if (res_compare != 1) {
424 for (k = 1; k < heapinfo2b->busy_block.size; k++)
425 state->equals_to2_(i2 + k, 0) = make_heap_area(i1, -1);
426 for (k = 1; k < heapinfo1->busy_block.size; k++)
427 state->equals_to1_(i1 + k, 0) = make_heap_area(i2, -1);
429 i1 += heapinfo1->busy_block.size;
437 XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1,
438 heapinfo1->busy_block.busy_size, addr_block1);
439 i1 = state->heaplimit + 1;
444 } else { /* Fragmented block */
446 for (j1 = 0; j1 < (size_t) (BLOCKSIZE >> heapinfo1->type); j1++) {
448 if (heapinfo1->busy_frag.frag_size[j1] == -1) /* Free fragment */
451 if (state->equals_to1_(i1, j1).valid)
455 (void *) ((char *) addr_block1 + (j1 << heapinfo1->type));
460 /* Try first to associate to same fragment in the other heap */
461 if (heapinfo2->type == heapinfo1->type) {
463 if (state->equals_to2_(i1, j1).valid == 0) {
465 addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE +
466 (char *) state->std_heap_copy.heapbase;
468 (void *) ((char *) addr_block2 +
469 (j1 << heapinfo2->type));
472 compare_heap_area(simgrid::mc::ProcessIndexMissing, addr_frag1, addr_frag2, snapshot1, snapshot2,
475 if (res_compare != 1)
482 while (i2 <= state->heaplimit && !equal) {
484 const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(
485 heap_region2, &heapinfo_temp2b, &heapinfos2[i2],
486 sizeof(malloc_info));
488 if (heapinfo2b->type == MMALLOC_TYPE_FREE || heapinfo2b->type == MMALLOC_TYPE_HEAPINFO) {
493 // We currently do not match fragments with unfragmented blocks (maybe we should).
494 if (heapinfo2b->type == MMALLOC_TYPE_UNFRAGMENTED) {
499 if (heapinfo2b->type < 0) {
500 fprintf(stderr, "Unkown mmalloc block type.\n");
504 for (j2 = 0; j2 < (size_t) (BLOCKSIZE >> heapinfo2b->type);
507 if (i2 == i1 && j2 == j1)
510 if (state->equals_to2_(i2, j2).valid)
513 addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE +
514 (char *) state->std_heap_copy.heapbase;
516 (void *) ((char *) addr_block2 +
517 (j2 << heapinfo2b->type));
520 compare_heap_area(simgrid::mc::ProcessIndexMissing, addr_frag1, addr_frag2, snapshot2, snapshot2,
523 if (res_compare != 1) {
536 ("Block %zu, fragment %zu not found (size_used = %zd, address = %p)\n",
537 i1, j1, heapinfo1->busy_frag.frag_size[j1],
539 i2 = state->heaplimit + 1;
540 i1 = state->heaplimit + 1;
553 /* All blocks/fragments are equal to another block/fragment ? */
556 for(i = 1; i <= state->heaplimit; i++) {
557 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
558 heap_region1, &heapinfo_temp1, &heapinfos1[i], sizeof(malloc_info));
559 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED) {
560 if (i1 == state->heaplimit) {
561 if (heapinfo1->busy_block.busy_size > 0) {
562 if (state->equals_to1_(i, 0).valid == 0) {
563 if (XBT_LOG_ISENABLED(mc_diff, xbt_log_priority_debug)) {
565 XBT_DEBUG("Block %zu not found (size used = %zu)", i,
566 heapinfo1->busy_block.busy_size);
567 //mmalloc_backtrace_block_display((void*)heapinfo1, i);
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]);
585 //mmalloc_backtrace_fragment_display((void*)heapinfo1, i, j);
595 if (i1 == state->heaplimit)
596 XBT_DEBUG("Number of blocks/fragments not found in heap1 : %d", nb_diff1);
598 for (i=1; i <= state->heaplimit; i++) {
599 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
600 heap_region2, &heapinfo_temp2, &heapinfos2[i], sizeof(malloc_info));
601 if (heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
602 if (i1 == state->heaplimit) {
603 if (heapinfo2->busy_block.busy_size > 0) {
604 if (state->equals_to2_(i, 0).valid == 0) {
605 if (XBT_LOG_ISENABLED(mc_diff, xbt_log_priority_debug)) {
606 // TODO, print address of the block
607 XBT_DEBUG("Block %zu not found (size used = %zu)", i,
608 heapinfo2->busy_block.busy_size);
609 //mmalloc_backtrace_block_display((void*)heapinfo2, i);
616 if (heapinfo2->type > 0) {
617 for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo2->type); j++) {
618 if (i1 == state->heaplimit) {
619 if (heapinfo2->busy_frag.frag_size[j] > 0) {
620 if (state->equals_to2_(i, j).valid == 0) {
621 if (XBT_LOG_ISENABLED(mc_diff, xbt_log_priority_debug)) {
622 // TODO, print address of the block
624 ("Block %zu, Fragment %zu not found (size used = %zd)",
626 heapinfo2->busy_frag.frag_size[j]);
627 //mmalloc_backtrace_fragment_display((void*)heapinfo2, i, j);
637 if (i1 == state->heaplimit)
638 XBT_DEBUG("Number of blocks/fragments not found in heap2 : %d", nb_diff2);
640 return ((nb_diff1 > 0) || (nb_diff2 > 0));
646 * @param real_area1 Process address for state 1
647 * @param real_area2 Process address for state 2
648 * @param snapshot1 Snapshot of state 1
649 * @param snapshot2 Snapshot of state 2
652 * @param check_ignore
654 static int compare_heap_area_without_type(struct s_mc_diff *state, int process_index,
655 const void *real_area1, const void *real_area2,
656 mc_snapshot_t snapshot1,
657 mc_snapshot_t snapshot2,
658 xbt_dynar_t previous, int size,
661 simgrid::mc::Process* process = &mc_model_checker->process();
664 const void *addr_pointed1, *addr_pointed2;
665 int pointer_align, res_compare;
666 ssize_t ignore1, ignore2;
668 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
669 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
673 if (check_ignore > 0) {
675 heap_comparison_ignore_size(state->to_ignore1,
676 (char *) real_area1 + i)) != -1) {
678 heap_comparison_ignore_size(state->to_ignore2,
679 (char *) real_area2 + i)) == ignore1) {
692 if (MC_snapshot_region_memcmp(((char *) real_area1) + i, heap_region1, ((char *) real_area2) + i, heap_region2, 1) != 0) {
694 pointer_align = (i / sizeof(void *)) * sizeof(void *);
695 addr_pointed1 = snapshot1->read(
696 remote((void**)((char *) real_area1 + pointer_align)), process_index);
697 addr_pointed2 = snapshot2->read(
698 remote((void**)((char *) real_area2 + pointer_align)), process_index);
700 if (process->in_maestro_stack(remote(addr_pointed1))
701 && process->in_maestro_stack(remote(addr_pointed2))) {
702 i = pointer_align + sizeof(void *);
704 } else if (addr_pointed1 > state->std_heap_copy.heapbase
705 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
706 && addr_pointed2 > state->std_heap_copy.heapbase
707 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)) {
708 // Both addreses are in the heap:
710 compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
711 snapshot2, previous, nullptr, 0);
712 if (res_compare == 1)
714 i = pointer_align + sizeof(void *);
732 * @param real_area1 Process address for state 1
733 * @param real_area2 Process address for state 2
734 * @param snapshot1 Snapshot of state 1
735 * @param snapshot2 Snapshot of state 2
738 * @param area_size either a byte_size or an elements_count (?)
739 * @param check_ignore
740 * @param pointer_level
741 * @return 0 (same), 1 (different), -1 (unknown)
743 static int compare_heap_area_with_type(struct s_mc_diff *state, int process_index,
744 const void *real_area1, const void *real_area2,
745 mc_snapshot_t snapshot1,
746 mc_snapshot_t snapshot2,
747 xbt_dynar_t previous, simgrid::mc::Type* type,
748 int area_size, int check_ignore,
752 // HACK: This should not happen but in pratice, there is some
753 // DW_TAG_typedef without DW_AT_type. We should fix this somehow.
757 if (is_stack(real_area1) && is_stack(real_area2))
759 ssize_t ignore1, ignore2;
761 if ((check_ignore > 0)
762 && ((ignore1 = heap_comparison_ignore_size(state->to_ignore1, real_area1))
764 && ((ignore2 = heap_comparison_ignore_size(state->to_ignore2, real_area2))
768 simgrid::mc::Type *subtype, *subsubtype;
770 const void *addr_pointed1, *addr_pointed2;
772 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
773 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
775 switch (type->type) {
776 case DW_TAG_unspecified_type:
779 case DW_TAG_base_type:
780 if (!type->name.empty() && type->name == "char") { /* String, hence random (arbitrary ?) size */
781 if (real_area1 == real_area2)
784 return (MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, area_size) != 0);
786 if (area_size != -1 && type->byte_size != area_size)
789 return (MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0);
792 case DW_TAG_enumeration_type:
793 if (area_size != -1 && type->byte_size != area_size)
796 return (MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0);
799 case DW_TAG_const_type:
800 case DW_TAG_volatile_type:
802 type = type->subtype;
805 case DW_TAG_array_type:
806 subtype = type->subtype;
807 switch (subtype->type) {
808 case DW_TAG_unspecified_type:
811 case DW_TAG_base_type:
812 case DW_TAG_enumeration_type:
813 case DW_TAG_pointer_type:
814 case DW_TAG_reference_type:
815 case DW_TAG_rvalue_reference_type:
816 case DW_TAG_structure_type:
817 case DW_TAG_class_type:
818 case DW_TAG_union_type:
819 if (subtype->full_type)
820 subtype = subtype->full_type;
821 elm_size = subtype->byte_size;
823 // TODO, just remove the type indirection?
824 case DW_TAG_const_type:
826 case DW_TAG_volatile_type:
827 subsubtype = subtype->subtype;
828 if (subsubtype->full_type)
829 subsubtype = subsubtype->full_type;
830 elm_size = subsubtype->byte_size;
836 for (int i = 0; i < type->element_count; i++) {
837 // TODO, add support for variable stride (DW_AT_byte_stride)
839 compare_heap_area_with_type(state, process_index,
840 (char *) real_area1 + (i * elm_size),
841 (char *) real_area2 + (i * elm_size),
842 snapshot1, snapshot2, previous,
843 type->subtype, subtype->byte_size,
844 check_ignore, pointer_level);
849 case DW_TAG_reference_type:
850 case DW_TAG_rvalue_reference_type:
851 case DW_TAG_pointer_type:
852 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
853 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
854 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
855 return (addr_pointed1 != addr_pointed2);;
858 if (pointer_level > 1) { /* Array of pointers */
859 for (size_t i = 0; i < (area_size / sizeof(void *)); i++) {
860 addr_pointed1 = snapshot1->read(
861 remote((void**)((char*) real_area1 + i * sizeof(void *))),
863 addr_pointed2 = snapshot2->read(
864 remote((void**)((char*) real_area2 + i * sizeof(void *))),
866 if (addr_pointed1 > state->std_heap_copy.heapbase
867 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
868 && addr_pointed2 > state->std_heap_copy.heapbase
869 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
871 compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
872 snapshot2, previous, type->subtype,
875 res = (addr_pointed1 != addr_pointed2);
880 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
881 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
882 if (addr_pointed1 > state->std_heap_copy.heapbase
883 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
884 && addr_pointed2 > state->std_heap_copy.heapbase
885 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
886 return compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
887 snapshot2, previous, type->subtype,
890 return (addr_pointed1 != addr_pointed2);
894 case DW_TAG_structure_type:
895 case DW_TAG_class_type:
897 type = type->full_type;
898 if (area_size != -1 && type->byte_size != area_size) {
899 if (area_size > type->byte_size && area_size % type->byte_size == 0) {
900 for (size_t i = 0; i < (size_t)(area_size / type->byte_size); i++) {
902 compare_heap_area_with_type(state, process_index,
903 (char *) real_area1 + i * type->byte_size,
904 (char *) real_area2 + i * type->byte_size,
905 snapshot1, snapshot2, previous, type, -1,
913 for(simgrid::mc::Member& member : type->members) {
914 // TODO, optimize this? (for the offset case)
915 void *real_member1 = simgrid::dwarf::resolve_member(
916 real_area1, type, &member, (simgrid::mc::AddressSpace*) snapshot1, process_index);
917 void *real_member2 = simgrid::dwarf::resolve_member(
918 real_area2, type, &member, (simgrid::mc::AddressSpace*) snapshot2, process_index);
920 compare_heap_area_with_type(state, process_index, real_member1, real_member2,
921 snapshot1, snapshot2,
922 previous, member.type, -1,
929 case DW_TAG_union_type:
930 return compare_heap_area_without_type(state, process_index, real_area1, real_area2,
931 snapshot1, snapshot2, previous,
932 type->byte_size, check_ignore);
942 /** Infer the type of a part of the block from the type of the block
944 * TODO, handle DW_TAG_array_type as well as arrays of the object ((*p)[5], p[5])
946 * TODO, handle subfields ((*p).bar.foo, (*p)[5].bar…)
948 * @param type_id DWARF type ID of the root address
950 * @return DWARF type ID for given offset
952 static simgrid::mc::Type* get_offset_type(void *real_base_address, simgrid::mc::Type* type,
953 int offset, int area_size,
954 mc_snapshot_t snapshot, int process_index)
957 // Beginning of the block, the infered variable type if the type of the block:
961 switch (type->type) {
962 case DW_TAG_structure_type:
963 case DW_TAG_class_type:
965 type = type->full_type;
967 if (area_size != -1 && type->byte_size != area_size) {
968 if (area_size > type->byte_size && area_size % type->byte_size == 0)
973 for(simgrid::mc::Member& member : type->members) {
975 if (member.has_offset_location()) {
976 // We have the offset, use it directly (shortcut):
977 if (member.offset() == offset)
980 void *real_member = simgrid::dwarf::resolve_member(
981 real_base_address, type, &member, snapshot, process_index);
982 if ((char*) real_member - (char *) real_base_address == offset)
991 /* FIXME : other cases ? */
999 * @param area1 Process address for state 1
1000 * @param area2 Process address for state 2
1001 * @param snapshot1 Snapshot of state 1
1002 * @param snapshot2 Snapshot of state 2
1003 * @param previous Pairs of blocks already compared on the current path (or nullptr)
1004 * @param type_id Type of variable
1005 * @param pointer_level
1006 * @return 0 (same), 1 (different), -1
1008 int compare_heap_area(int process_index, const void *area1, const void *area2, mc_snapshot_t snapshot1,
1009 mc_snapshot_t snapshot2, xbt_dynar_t previous,
1010 simgrid::mc::Type* type, int pointer_level)
1012 simgrid::mc::Process* process = &mc_model_checker->process();
1014 struct s_mc_diff *state = mc_diff_info;
1017 ssize_t block1, frag1, block2, frag2;
1019 int check_ignore = 0;
1021 void *real_addr_block1, *real_addr_block2, *real_addr_frag1, *real_addr_frag2;
1023 int offset1 = 0, offset2 = 0;
1024 int new_size1 = -1, new_size2 = -1;
1025 simgrid::mc::Type *new_type1 = nullptr, *new_type2 = NULL;
1027 int match_pairs = 0;
1029 // This is the address of std_heap->heapinfo in the application process:
1030 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
1032 const malloc_info* heapinfos1 = snapshot1->read(
1033 remote((const malloc_info**)heapinfo_address), process_index);
1034 const malloc_info* heapinfos2 = snapshot2->read(
1035 remote((const malloc_info**)heapinfo_address), process_index);
1037 malloc_info heapinfo_temp1, heapinfo_temp2;
1039 if (previous == nullptr) {
1041 xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
1044 // Get block number:
1047 (char *) state->std_heap_copy.heapbase) / BLOCKSIZE + 1;
1050 (char *) state->std_heap_copy.heapbase) / BLOCKSIZE + 1;
1052 // If either block is a stack block:
1053 if (is_block_stack((int) block1) && is_block_stack((int) block2)) {
1054 add_heap_area_pair(previous, block1, -1, block2, -1);
1056 match_equals(state, previous);
1057 xbt_dynar_free(&previous);
1061 // If either block is not in the expected area of memory:
1062 if (((char *) area1 < (char *) state->std_heap_copy.heapbase)
1063 || (block1 > (ssize_t) state->heapsize1) || (block1 < 1)
1064 || ((char *) area2 < (char *) state->std_heap_copy.heapbase)
1065 || (block2 > (ssize_t) state->heapsize2) || (block2 < 1)) {
1067 xbt_dynar_free(&previous);
1071 // Process address of the block:
1072 real_addr_block1 = (ADDR2UINT(block1) - 1) * BLOCKSIZE +
1073 (char *) state->std_heap_copy.heapbase;
1074 real_addr_block2 = (ADDR2UINT(block2) - 1) * BLOCKSIZE +
1075 (char *) state->std_heap_copy.heapbase;
1079 if (type->full_type)
1080 type = type->full_type;
1082 // This assume that for "boring" types (volatile ...) byte_size is absent:
1083 while (type->byte_size == 0 && type->subtype != nullptr)
1084 type = type->subtype;
1087 if ((type->type == DW_TAG_pointer_type)
1088 || ((type->type == DW_TAG_base_type) && !type->name.empty()
1089 && type->name == "char"))
1092 type_size = type->byte_size;
1096 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
1097 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
1099 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
1100 heap_region1, &heapinfo_temp1, &heapinfos1[block1], sizeof(malloc_info));
1101 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
1102 heap_region2, &heapinfo_temp2, &heapinfos2[block2], sizeof(malloc_info));
1104 if ((heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type==MMALLOC_TYPE_HEAPINFO)
1105 && (heapinfo2->type == MMALLOC_TYPE_FREE || heapinfo2->type ==MMALLOC_TYPE_HEAPINFO)) {
1109 match_equals(state, previous);
1110 xbt_dynar_free(&previous);
1114 } else if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED
1115 && heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
1116 /* Complete block */
1118 // TODO, lookup variable type from block type as done for fragmented blocks
1120 offset1 = (char *) area1 - (char *) real_addr_block1;
1121 offset2 = (char *) area2 - (char *) real_addr_block2;
1123 if (state->equals_to1_(block1, 0).valid
1124 && state->equals_to2_(block2, 0).valid) {
1125 if (equal_blocks(state, block1, block2)) {
1127 match_equals(state, previous);
1128 xbt_dynar_free(&previous);
1134 if (type_size != -1) {
1135 if (type_size != (ssize_t) heapinfo1->busy_block.busy_size
1136 && type_size != (ssize_t) heapinfo2->busy_block.busy_size
1137 && (type->name.empty() || type->name == "struct s_smx_context")) {
1139 match_equals(state, previous);
1140 xbt_dynar_free(&previous);
1146 if (heapinfo1->busy_block.size !=
1147 heapinfo2->busy_block.size) {
1149 xbt_dynar_free(&previous);
1153 if (heapinfo1->busy_block.busy_size !=
1154 heapinfo2->busy_block.busy_size) {
1156 xbt_dynar_free(&previous);
1160 if (!add_heap_area_pair(previous, block1, -1, block2, -1)) {
1162 match_equals(state, previous);
1163 xbt_dynar_free(&previous);
1168 size = heapinfo1->busy_block.busy_size;
1170 // Remember (basic) type inference.
1171 // The current data structure only allows us to do this for the whole block.
1172 if (type != nullptr && area1 == real_addr_block1)
1173 state->types1_(block1, 0) = type;
1174 if (type != nullptr && area2 == real_addr_block2)
1175 state->types2_(block2, 0) = type;
1179 match_equals(state, previous);
1180 xbt_dynar_free(&previous);
1188 if ((heapinfo1->busy_block.ignore > 0)
1189 && (heapinfo2->busy_block.ignore ==
1190 heapinfo1->busy_block.ignore))
1191 check_ignore = heapinfo1->busy_block.ignore;
1193 } else if ((heapinfo1->type > 0) && (heapinfo2->type > 0)) { /* Fragmented block */
1197 ((uintptr_t) (ADDR2UINT(area1) % (BLOCKSIZE))) >> heapinfo1->type;
1199 ((uintptr_t) (ADDR2UINT(area2) % (BLOCKSIZE))) >> heapinfo2->type;
1201 // Process address of the fragment:
1203 (void *) ((char *) real_addr_block1 +
1204 (frag1 << heapinfo1->type));
1206 (void *) ((char *) real_addr_block2 +
1207 (frag2 << heapinfo2->type));
1209 // Check the size of the fragments against the size of the type:
1210 if (type_size != -1) {
1211 if (heapinfo1->busy_frag.frag_size[frag1] == -1
1212 || heapinfo2->busy_frag.frag_size[frag2] == -1) {
1214 match_equals(state, previous);
1215 xbt_dynar_free(&previous);
1220 if (type_size != heapinfo1->busy_frag.frag_size[frag1]
1221 || type_size != heapinfo2->busy_frag.frag_size[frag2]) {
1223 match_equals(state, previous);
1224 xbt_dynar_free(&previous);
1230 // Check if the blocks are already matched together:
1231 if (state->equals_to1_(block1, frag1).valid
1232 && state->equals_to2_(block2, frag2).valid) {
1233 if (offset1==offset2 && equal_fragments(state, block1, frag1, block2, frag2)) {
1235 match_equals(state, previous);
1236 xbt_dynar_free(&previous);
1241 // Compare the size of both fragments:
1242 if (heapinfo1->busy_frag.frag_size[frag1] !=
1243 heapinfo2->busy_frag.frag_size[frag2]) {
1244 if (type_size == -1) {
1246 match_equals(state, previous);
1247 xbt_dynar_free(&previous);
1252 xbt_dynar_free(&previous);
1257 // Size of the fragment:
1258 size = heapinfo1->busy_frag.frag_size[frag1];
1260 // Remember (basic) type inference.
1261 // The current data structure only allows us to do this for the whole fragment.
1262 if (type != nullptr && area1 == real_addr_frag1)
1263 state->types1_(block1, frag1) = type;
1264 if (type != nullptr && area2 == real_addr_frag2)
1265 state->types2_(block2, frag2) = type;
1267 // The type of the variable is already known:
1272 // Type inference from the block type.
1273 else if (state->types1_(block1, frag1) != nullptr
1274 || state->types2_(block2, frag2) != nullptr) {
1276 offset1 = (char *) area1 - (char *) real_addr_frag1;
1277 offset2 = (char *) area2 - (char *) real_addr_frag2;
1279 if (state->types1_(block1, frag1) != nullptr
1280 && state->types2_(block2, frag2) != nullptr) {
1282 get_offset_type(real_addr_frag1, state->types1_(block1, frag1),
1283 offset1, size, snapshot1, process_index);
1285 get_offset_type(real_addr_frag2, state->types2_(block2, frag2),
1286 offset1, size, snapshot2, process_index);
1287 } else if (state->types1_(block1, frag1) != nullptr) {
1289 get_offset_type(real_addr_frag1, state->types1_(block1, frag1),
1290 offset1, size, snapshot1, process_index);
1292 get_offset_type(real_addr_frag2, state->types1_(block1, frag1),
1293 offset2, size, snapshot2, process_index);
1294 } else if (state->types2_(block2, frag2) != nullptr) {
1296 get_offset_type(real_addr_frag1, state->types2_(block2, frag2),
1297 offset1, size, snapshot1, process_index);
1299 get_offset_type(real_addr_frag2, state->types2_(block2, frag2),
1300 offset2, size, snapshot2, process_index);
1303 match_equals(state, previous);
1304 xbt_dynar_free(&previous);
1309 if (new_type1 != nullptr && new_type2 != NULL && new_type1 != new_type2) {
1312 while (type->byte_size == 0 && type->subtype != nullptr)
1313 type = type->subtype;
1314 new_size1 = type->byte_size;
1317 while (type->byte_size == 0 && type->subtype != nullptr)
1318 type = type->subtype;
1319 new_size2 = type->byte_size;
1323 match_equals(state, previous);
1324 xbt_dynar_free(&previous);
1330 if (new_size1 > 0 && new_size1 == new_size2) {
1335 if (offset1 == 0 && offset2 == 0
1336 && !add_heap_area_pair(previous, block1, frag1, block2, frag2)) {
1338 match_equals(state, previous);
1339 xbt_dynar_free(&previous);
1346 match_equals(state, previous);
1347 xbt_dynar_free(&previous);
1352 if ((heapinfo1->busy_frag.ignore[frag1] > 0)
1353 && (heapinfo2->busy_frag.ignore[frag2] ==
1354 heapinfo1->busy_frag.ignore[frag1]))
1355 check_ignore = heapinfo1->busy_frag.ignore[frag1];
1360 xbt_dynar_free(&previous);
1366 /* Start comparison */
1369 compare_heap_area_with_type(state, process_index, area1, area2, snapshot1, snapshot2,
1370 previous, type, size, check_ignore,
1374 compare_heap_area_without_type(state, process_index, area1, area2, snapshot1, snapshot2,
1375 previous, size, check_ignore);
1377 if (res_compare == 1) {
1379 xbt_dynar_free(&previous);
1384 match_equals(state, previous);
1385 xbt_dynar_free(&previous);
1391 /*********************************************** Miscellaneous ***************************************************/
1392 /****************************************************************************************************************/
1394 // Not used and broken code:
1398 static int get_pointed_area_size(void *area, int heap)
1401 struct s_mc_diff *state = mc_diff_info;
1404 malloc_info *heapinfo;
1407 heapinfo = state->heapinfo1;
1409 heapinfo = state->heapinfo2;
1413 (char *) state->std_heap_copy.heapbase) / BLOCKSIZE + 1;
1415 if (((char *) area < (char *) state->std_heap_copy.heapbase)
1416 || (block > state->heapsize1) || (block < 1))
1419 if (heapinfo[block].type == MMALLOC_TYPE_FREE || heapinfo[block].type == MMALLOC_TYPE_HEAPINFO) { /* Free block */
1421 else if (heapinfo[block].type == MMALLOC_TYPE_UNFRAGMENTED) /* Complete block */
1422 return (int) heapinfo[block].busy_block.busy_size;
1425 ((uintptr_t) (ADDR2UINT(area) % (BLOCKSIZE))) >> heapinfo[block].type;
1426 return (int) heapinfo[block].busy_frag.frag_size[frag];
1431 #define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
1435 int mmalloc_linear_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2)
1438 struct s_mc_diff *state = mc_diff_info;
1440 if (heap1 == nullptr && heap1 == NULL) {
1441 XBT_DEBUG("Malloc descriptors null");
1445 if (heap1->heaplimit != heap2->heaplimit) {
1446 XBT_DEBUG("Different limit of valid info table indices");
1450 /* Heap information */
1451 state->heaplimit = ((struct mdesc *) heap1)->heaplimit;
1453 state->std_heap_copy = *mc_model_checker->process().get_heap();
1455 state->heapbase1 = (char *) heap1 + BLOCKSIZE;
1456 state->heapbase2 = (char *) heap2 + BLOCKSIZE;
1459 (malloc_info *) ((char *) heap1 +
1461 ((char *) heap1->heapinfo - (char *) state->s_heap)));
1463 (malloc_info *) ((char *) heap2 +
1465 ((char *) heap2->heapinfo - (char *) state->s_heap)));
1467 state->heapsize1 = heap1->heapsize;
1468 state->heapsize2 = heap2->heapsize;
1470 /* Start comparison */
1472 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
1476 /* Check busy blocks */
1480 while (i <= state->heaplimit) {
1483 ((void *) (((ADDR2UINT(i)) - 1) * BLOCKSIZE +
1484 (char *) state->heapbase1));
1486 ((void *) (((ADDR2UINT(i)) - 1) * BLOCKSIZE +
1487 (char *) state->heapbase2));
1489 if (state->heapinfo1[i].type != state->heapinfo2[i].type) {
1491 distance += BLOCKSIZE;
1492 XBT_DEBUG("Different type of blocks (%zu) : %d - %d -> distance = %d", i,
1493 state->heapinfo1[i].type, state->heapinfo2[i].type, distance);
1498 if (state->heapinfo1[i].type == MMALLOC_TYPE_FREE
1499 || state->heapinfo1[i].type == MMALLOC_TYPE_HAPINFO) { /* Free block */
1504 if (state->heapinfo1[i].type == MMALLOC_TYPE_UNFRAGMENTED) { /* Large block */
1506 if (state->heapinfo1[i].busy_block.size !=
1507 state->heapinfo2[i].busy_block.size) {
1509 BLOCKSIZE * max(state->heapinfo1[i].busy_block.size,
1510 state->heapinfo2[i].busy_block.size);
1511 i += max(state->heapinfo1[i].busy_block.size,
1512 state->heapinfo2[i].busy_block.size);
1514 ("Different larger of cluster at block %zu : %zu - %zu -> distance = %d",
1515 i, state->heapinfo1[i].busy_block.size,
1516 state->heapinfo2[i].busy_block.size, distance);
1520 /*if(heapinfo1[i].busy_block.busy_size != heapinfo2[i].busy_block.busy_size){
1521 distance += max(heapinfo1[i].busy_block.busy_size, heapinfo2[i].busy_block.busy_size);
1522 i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
1523 XBT_DEBUG("Different size used oin large cluster at block %zu : %zu - %zu -> distance = %d", i, heapinfo1[i].busy_block.busy_size, heapinfo2[i].busy_block.busy_size, distance);
1529 //while(k < (heapinfo1[i].busy_block.busy_size)){
1530 while (k < state->heapinfo1[i].busy_block.size * BLOCKSIZE) {
1531 if (memcmp((char *) addr_block1 + k, (char *) addr_block2 + k, 1) !=
1540 } else { /* Fragmented block */
1542 for (j = 0; j < (size_t) (BLOCKSIZE >> state->heapinfo1[i].type); j++) {
1545 (void *) ((char *) addr_block1 + (j << state->heapinfo1[i].type));
1547 (void *) ((char *) addr_block2 + (j << state->heapinfo2[i].type));
1549 if (state->heapinfo1[i].busy_frag.frag_size[j] == 0
1550 && state->heapinfo2[i].busy_frag.frag_size[j] == 0) {
1555 /*if(heapinfo1[i].busy_frag.frag_size[j] != heapinfo2[i].busy_frag.frag_size[j]){
1556 distance += max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j]);
1557 XBT_DEBUG("Different size used in fragment %zu in block %zu : %d - %d -> distance = %d", j, i, heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j], distance);
1563 //while(k < max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j])){
1564 while (k < (BLOCKSIZE / (BLOCKSIZE >> state->heapinfo1[i].type))) {
1565 if (memcmp((char *) addr_frag1 + k, (char *) addr_frag2 + k, 1) !=