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 "src/mc/mc_object_info.h"
14 #include "mc/datatypes.h"
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;
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_diff, xbt,
25 "Logging specific to mc_diff in mc");
27 /*********************************** Heap comparison ***********************************/
28 /***************************************************************************************/
30 typedef char *type_name;
32 struct XBT_PRIVATE s_mc_diff {
33 s_xbt_mheap_t std_heap_copy;
34 std::size_t heaplimit;
35 // Number of blocks in the heaps:
36 std::size_t heapsize1, heapsize2;
37 std::vector<simgrid::mc::IgnoredHeapRegion>* to_ignore1;
38 std::vector<simgrid::mc::IgnoredHeapRegion>* to_ignore2;
39 s_heap_area_t *equals_to1, *equals_to2;
40 simgrid::mc::Type **types1;
41 simgrid::mc::Type **types2;
42 std::size_t available;
45 #define equals_to1_(i,j) equals_to1[ MAX_FRAGMENT_PER_BLOCK*(i) + (j)]
46 #define equals_to2_(i,j) equals_to2[ MAX_FRAGMENT_PER_BLOCK*(i) + (j)]
47 #define types1_(i,j) types1[ MAX_FRAGMENT_PER_BLOCK*(i) + (j)]
48 #define types2_(i,j) types2[ MAX_FRAGMENT_PER_BLOCK*(i) + (j)]
50 static __thread struct s_mc_diff *mc_diff_info = nullptr;
52 /*********************************** Free functions ************************************/
54 static void heap_area_pair_free(heap_area_pair_t pair)
60 static void heap_area_pair_free_voidp(void *d)
62 heap_area_pair_free((heap_area_pair_t) * (void **) d);
65 static void heap_area_free(heap_area_t area)
71 /************************************************************************************/
73 static s_heap_area_t make_heap_area(int block, int fragment)
78 area.fragment = fragment;
83 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1,
84 int block2, int fragment2)
87 unsigned int cursor = 0;
88 heap_area_pair_t current_pair;
90 xbt_dynar_foreach(list, cursor, current_pair) {
91 if (current_pair->block1 == block1 && current_pair->block2 == block2
92 && current_pair->fragment1 == fragment1
93 && current_pair->fragment2 == fragment2)
100 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1,
101 int block2, int fragment2)
104 if (is_new_heap_area_pair(list, block1, fragment1, block2, fragment2)) {
105 heap_area_pair_t pair = NULL;
106 pair = xbt_new0(s_heap_area_pair_t, 1);
107 pair->block1 = block1;
108 pair->fragment1 = fragment1;
109 pair->block2 = block2;
110 pair->fragment2 = fragment2;
112 xbt_dynar_push(list, &pair);
120 static ssize_t heap_comparison_ignore_size(
121 std::vector<simgrid::mc::IgnoredHeapRegion>* ignore_list,
125 int end = ignore_list->size() - 1;
127 while (start <= end) {
128 unsigned int cursor = (start + end) / 2;
129 simgrid::mc::IgnoredHeapRegion const& region = (*ignore_list)[cursor];
130 if (region.address == address)
132 if (region.address < address)
134 if (region.address > address)
141 static bool is_stack(const void *address)
143 for (auto const& stack : mc_model_checker->process().stack_areas())
144 if (address == stack.address)
149 // TODO, this should depend on the snapshot?
150 static bool is_block_stack(int block)
152 for (auto const& stack : mc_model_checker->process().stack_areas())
153 if (block == stack.block)
158 static void match_equals(struct s_mc_diff *state, xbt_dynar_t list)
161 unsigned int cursor = 0;
162 heap_area_pair_t current_pair;
164 xbt_dynar_foreach(list, cursor, current_pair) {
166 if (current_pair->fragment1 != -1) {
168 state->equals_to1_(current_pair->block1, current_pair->fragment1) =
169 make_heap_area(current_pair->block2, current_pair->fragment2);
170 state->equals_to2_(current_pair->block2, current_pair->fragment2) =
171 make_heap_area(current_pair->block1, current_pair->fragment1);
175 state->equals_to1_(current_pair->block1, 0) =
176 make_heap_area(current_pair->block2, current_pair->fragment2);
177 state->equals_to2_(current_pair->block2, 0) =
178 make_heap_area(current_pair->block1, current_pair->fragment1);
185 /** Check whether two blocks are known to be matching
187 * @param state State used
188 * @param b1 Block of state 1
189 * @param b2 Block of state 2
190 * @return if the blocks are known to be matching
192 static int equal_blocks(struct s_mc_diff *state, int b1, int b2)
195 if (state->equals_to1_(b1, 0).block == b2
196 && state->equals_to2_(b2, 0).block == b1)
202 /** Check whether two fragments are known to be matching
204 * @param state State used
205 * @param b1 Block of state 1
206 * @param f1 Fragment of state 1
207 * @param b2 Block of state 2
208 * @param f2 Fragment of state 2
209 * @return if the fragments are known to be matching
211 static int equal_fragments(struct s_mc_diff *state, int b1, int f1, int b2,
215 if (state->equals_to1_(b1, f1).block == b2
216 && state->equals_to1_(b1, f1).fragment == f2
217 && state->equals_to2_(b2, f2).block == b1
218 && state->equals_to2_(b2, f2).fragment == f1)
226 int init_heap_information(xbt_mheap_t heap1, xbt_mheap_t heap2,
227 std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
228 std::vector<simgrid::mc::IgnoredHeapRegion>* i2)
230 if (mc_diff_info == NULL) {
231 mc_diff_info = xbt_new0(struct s_mc_diff, 1);
232 mc_diff_info->equals_to1 = NULL;
233 mc_diff_info->equals_to2 = NULL;
234 mc_diff_info->types1 = NULL;
235 mc_diff_info->types2 = NULL;
237 struct s_mc_diff *state = mc_diff_info;
239 if ((((struct mdesc *) heap1)->heaplimit !=
240 ((struct mdesc *) heap2)->heaplimit)
242 ((((struct mdesc *) heap1)->heapsize !=
243 ((struct mdesc *) heap2)->heapsize)))
246 state->heaplimit = ((struct mdesc *) heap1)->heaplimit;
248 state->std_heap_copy = *mc_model_checker->process().get_heap();
250 state->heapsize1 = heap1->heapsize;
251 state->heapsize2 = heap2->heapsize;
253 state->to_ignore1 = i1;
254 state->to_ignore2 = i2;
256 if (state->heaplimit > state->available) {
257 state->equals_to1 = (s_heap_area_t*)
258 realloc(state->equals_to1,
259 state->heaplimit * MAX_FRAGMENT_PER_BLOCK *
260 sizeof(s_heap_area_t));
261 state->types1 = (simgrid::mc::Type**)
262 realloc(state->types1,
263 state->heaplimit * MAX_FRAGMENT_PER_BLOCK *
264 sizeof(simgrid::mc::Type*));
265 state->equals_to2 = (s_heap_area_t*)
266 realloc(state->equals_to2,
267 state->heaplimit * MAX_FRAGMENT_PER_BLOCK *
268 sizeof(s_heap_area_t));
269 state->types2 = (simgrid::mc::Type**)
270 realloc(state->types2,
271 state->heaplimit * MAX_FRAGMENT_PER_BLOCK *
272 sizeof(simgrid::mc::Type*));
273 state->available = state->heaplimit;
276 memset(state->equals_to1, 0,
277 state->heaplimit * MAX_FRAGMENT_PER_BLOCK * sizeof(s_heap_area_t));
278 memset(state->equals_to2, 0,
279 state->heaplimit * MAX_FRAGMENT_PER_BLOCK * sizeof(s_heap_area_t));
280 memset(state->types1, 0,
281 state->heaplimit * MAX_FRAGMENT_PER_BLOCK * sizeof(type_name *));
282 memset(state->types2, 0,
283 state->heaplimit * MAX_FRAGMENT_PER_BLOCK * sizeof(type_name *));
291 void reset_heap_information()
296 // TODO, have a robust way to find it in O(1)
298 mc_mem_region_t MC_get_heap_region(mc_snapshot_t snapshot)
300 size_t n = snapshot->snapshot_regions.size();
301 for (size_t i=0; i!=n; ++i) {
302 mc_mem_region_t region = snapshot->snapshot_regions[i].get();
303 if (region->region_type() == simgrid::mc::RegionType::Heap)
306 xbt_die("No heap region");
309 int mmalloc_compare_heap(mc_snapshot_t snapshot1, mc_snapshot_t snapshot2)
311 simgrid::mc::Process* process = &mc_model_checker->process();
312 struct s_mc_diff *state = mc_diff_info;
314 /* Start comparison */
315 size_t i1, i2, j1, j2, k;
316 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
317 int nb_diff1 = 0, nb_diff2 = 0;
319 int equal, res_compare = 0;
321 /* Check busy blocks */
325 malloc_info heapinfo_temp1, heapinfo_temp2;
326 malloc_info heapinfo_temp2b;
328 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
329 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
331 // This is the address of std_heap->heapinfo in the application process:
332 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
334 // This is in snapshot do not use them directly:
335 const malloc_info* heapinfos1 = snapshot1->read<malloc_info*>(
336 (std::uint64_t)heapinfo_address, simgrid::mc::ProcessIndexMissing);
337 const malloc_info* heapinfos2 = snapshot2->read<malloc_info*>(
338 (std::uint64_t)heapinfo_address, simgrid::mc::ProcessIndexMissing);
340 while (i1 < state->heaplimit) {
342 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(heap_region1, &heapinfo_temp1, &heapinfos1[i1], sizeof(malloc_info));
343 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2, &heapinfos2[i1], sizeof(malloc_info));
345 if (heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type == MMALLOC_TYPE_HEAPINFO) { /* Free block */
350 if (heapinfo1->type < 0) {
351 fprintf(stderr, "Unkown mmalloc block type.\n");
356 ((void *) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE +
357 (char *) state->std_heap_copy.heapbase));
359 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED) { /* Large block */
361 if (is_stack(addr_block1)) {
362 for (k = 0; k < heapinfo1->busy_block.size; k++)
363 state->equals_to1_(i1 + k, 0) = make_heap_area(i1, -1);
364 for (k = 0; k < heapinfo2->busy_block.size; k++)
365 state->equals_to2_(i1 + k, 0) = make_heap_area(i1, -1);
366 i1 += heapinfo1->busy_block.size;
370 if (state->equals_to1_(i1, 0).valid) {
379 /* Try first to associate to same block in the other heap */
380 if (heapinfo2->type == heapinfo1->type) {
382 if (state->equals_to2_(i1, 0).valid == 0) {
384 addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE +
385 (char *) state->std_heap_copy.heapbase;
388 compare_heap_area(simgrid::mc::ProcessIndexMissing, addr_block1, addr_block2, snapshot1, snapshot2,
391 if (res_compare != 1) {
392 for (k = 1; k < heapinfo2->busy_block.size; k++)
393 state->equals_to2_(i1 + k, 0) = make_heap_area(i1, -1);
394 for (k = 1; k < heapinfo1->busy_block.size; k++)
395 state->equals_to1_(i1 + k, 0) = make_heap_area(i1, -1);
397 i1 += heapinfo1->busy_block.size;
404 while (i2 < state->heaplimit && !equal) {
406 addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE +
407 (char *) state->std_heap_copy.heapbase;
414 const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2b, &heapinfos2[i2], sizeof(malloc_info));
416 if (heapinfo2b->type != MMALLOC_TYPE_UNFRAGMENTED) {
421 if (state->equals_to2_(i2, 0).valid) {
427 compare_heap_area(simgrid::mc::ProcessIndexMissing, addr_block1, addr_block2, snapshot1, snapshot2,
430 if (res_compare != 1) {
431 for (k = 1; k < heapinfo2b->busy_block.size; k++)
432 state->equals_to2_(i2 + k, 0) = make_heap_area(i1, -1);
433 for (k = 1; k < heapinfo1->busy_block.size; k++)
434 state->equals_to1_(i1 + k, 0) = make_heap_area(i2, -1);
436 i1 += heapinfo1->busy_block.size;
444 XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1,
445 heapinfo1->busy_block.busy_size, addr_block1);
446 i1 = state->heaplimit + 1;
451 } else { /* Fragmented block */
453 for (j1 = 0; j1 < (size_t) (BLOCKSIZE >> heapinfo1->type); j1++) {
455 if (heapinfo1->busy_frag.frag_size[j1] == -1) /* Free fragment */
458 if (state->equals_to1_(i1, j1).valid)
462 (void *) ((char *) addr_block1 + (j1 << heapinfo1->type));
467 /* Try first to associate to same fragment in the other heap */
468 if (heapinfo2->type == heapinfo1->type) {
470 if (state->equals_to2_(i1, j1).valid == 0) {
472 addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE +
473 (char *) state->std_heap_copy.heapbase;
475 (void *) ((char *) addr_block2 +
476 (j1 << heapinfo2->type));
479 compare_heap_area(simgrid::mc::ProcessIndexMissing, addr_frag1, addr_frag2, snapshot1, snapshot2,
482 if (res_compare != 1)
489 while (i2 < state->heaplimit && !equal) {
491 const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(
492 heap_region2, &heapinfo_temp2b, &heapinfos2[i2],
493 sizeof(malloc_info));
495 if (heapinfo2b->type == MMALLOC_TYPE_FREE || heapinfo2b->type == MMALLOC_TYPE_HEAPINFO) {
500 // We currently do not match fragments with unfragmented blocks (maybe we should).
501 if (heapinfo2b->type == MMALLOC_TYPE_UNFRAGMENTED) {
506 if (heapinfo2b->type < 0) {
507 fprintf(stderr, "Unkown mmalloc block type.\n");
511 for (j2 = 0; j2 < (size_t) (BLOCKSIZE >> heapinfo2b->type);
514 if (i2 == i1 && j2 == j1)
517 if (state->equals_to2_(i2, j2).valid)
520 addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE +
521 (char *) state->std_heap_copy.heapbase;
523 (void *) ((char *) addr_block2 +
524 (j2 << heapinfo2b->type));
527 compare_heap_area(simgrid::mc::ProcessIndexMissing, addr_frag1, addr_frag2, snapshot2, snapshot2,
530 if (res_compare != 1) {
543 ("Block %zu, fragment %zu not found (size_used = %zd, address = %p)\n",
544 i1, j1, heapinfo1->busy_frag.frag_size[j1],
546 i2 = state->heaplimit + 1;
547 i1 = state->heaplimit + 1;
560 /* All blocks/fragments are equal to another block/fragment ? */
563 for(i = 1; i < state->heaplimit; i++) {
564 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
565 heap_region1, &heapinfo_temp1, &heapinfos1[i], sizeof(malloc_info));
566 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED) {
567 if (i1 == state->heaplimit) {
568 if (heapinfo1->busy_block.busy_size > 0) {
569 if (state->equals_to1_(i, 0).valid == 0) {
570 if (XBT_LOG_ISENABLED(mc_diff, xbt_log_priority_debug)) {
572 XBT_DEBUG("Block %zu not found (size used = %zu)", i,
573 heapinfo1->busy_block.busy_size);
574 //mmalloc_backtrace_block_display((void*)heapinfo1, i);
581 if (heapinfo1->type > 0) {
582 for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo1->type); j++) {
583 if (i1 == state->heaplimit) {
584 if (heapinfo1->busy_frag.frag_size[j] > 0) {
585 if (state->equals_to1_(i, j).valid == 0) {
586 if (XBT_LOG_ISENABLED(mc_diff, xbt_log_priority_debug)) {
587 // TODO, print fragment address
589 ("Block %zu, Fragment %zu not found (size used = %zd)",
591 heapinfo1->busy_frag.frag_size[j]);
592 //mmalloc_backtrace_fragment_display((void*)heapinfo1, i, j);
602 if (i1 == state->heaplimit)
603 XBT_DEBUG("Number of blocks/fragments not found in heap1 : %d", nb_diff1);
605 for (i=1; i < state->heaplimit; i++) {
606 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
607 heap_region2, &heapinfo_temp2, &heapinfos2[i], sizeof(malloc_info));
608 if (heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
609 if (i1 == state->heaplimit) {
610 if (heapinfo2->busy_block.busy_size > 0) {
611 if (state->equals_to2_(i, 0).valid == 0) {
612 if (XBT_LOG_ISENABLED(mc_diff, xbt_log_priority_debug)) {
613 // TODO, print address of the block
614 XBT_DEBUG("Block %zu not found (size used = %zu)", i,
615 heapinfo2->busy_block.busy_size);
616 //mmalloc_backtrace_block_display((void*)heapinfo2, i);
623 if (heapinfo2->type > 0) {
624 for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo2->type); j++) {
625 if (i1 == state->heaplimit) {
626 if (heapinfo2->busy_frag.frag_size[j] > 0) {
627 if (state->equals_to2_(i, j).valid == 0) {
628 if (XBT_LOG_ISENABLED(mc_diff, xbt_log_priority_debug)) {
629 // TODO, print address of the block
631 ("Block %zu, Fragment %zu not found (size used = %zd)",
633 heapinfo2->busy_frag.frag_size[j]);
634 //mmalloc_backtrace_fragment_display((void*)heapinfo2, i, j);
644 if (i1 == state->heaplimit)
645 XBT_DEBUG("Number of blocks/fragments not found in heap2 : %d", nb_diff2);
647 return ((nb_diff1 > 0) || (nb_diff2 > 0));
653 * @param real_area1 Process address for state 1
654 * @param real_area2 Process address for state 2
655 * @param snapshot1 Snapshot of state 1
656 * @param snapshot2 Snapshot of state 2
659 * @param check_ignore
661 static int compare_heap_area_without_type(struct s_mc_diff *state, int process_index,
662 const void *real_area1, const void *real_area2,
663 mc_snapshot_t snapshot1,
664 mc_snapshot_t snapshot2,
665 xbt_dynar_t previous, int size,
668 simgrid::mc::Process* process = &mc_model_checker->process();
671 const void *addr_pointed1, *addr_pointed2;
672 int pointer_align, res_compare;
673 ssize_t ignore1, ignore2;
675 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
676 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
680 if (check_ignore > 0) {
682 heap_comparison_ignore_size(state->to_ignore1,
683 (char *) real_area1 + i)) != -1) {
685 heap_comparison_ignore_size(state->to_ignore2,
686 (char *) real_area2 + i)) == ignore1) {
699 if (MC_snapshot_region_memcmp(((char *) real_area1) + i, heap_region1, ((char *) real_area2) + i, heap_region2, 1) != 0) {
701 pointer_align = (i / sizeof(void *)) * sizeof(void *);
702 addr_pointed1 = snapshot1->read(
703 remote((void**)((char *) real_area1 + pointer_align)), process_index);
704 addr_pointed2 = snapshot2->read(
705 remote((void**)((char *) real_area2 + pointer_align)), process_index);
707 if (process->in_maestro_stack(remote(addr_pointed1))
708 && process->in_maestro_stack(remote(addr_pointed2))) {
709 i = pointer_align + sizeof(void *);
711 } else if (addr_pointed1 > state->std_heap_copy.heapbase
712 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
713 && addr_pointed2 > state->std_heap_copy.heapbase
714 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)) {
715 // Both addreses are in the heap:
717 compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
718 snapshot2, previous, NULL, 0);
719 if (res_compare == 1) {
722 i = pointer_align + sizeof(void *);
741 * @param real_area1 Process address for state 1
742 * @param real_area2 Process address for state 2
743 * @param snapshot1 Snapshot of state 1
744 * @param snapshot2 Snapshot of state 2
747 * @param area_size either a byte_size or an elements_count (?)
748 * @param check_ignore
749 * @param pointer_level
750 * @return 0 (same), 1 (different), -1 (unknown)
752 static int compare_heap_area_with_type(struct s_mc_diff *state, int process_index,
753 const void *real_area1, const void *real_area2,
754 mc_snapshot_t snapshot1,
755 mc_snapshot_t snapshot2,
756 xbt_dynar_t previous, simgrid::mc::Type* type,
757 int area_size, int check_ignore,
761 if (is_stack(real_area1) && is_stack(real_area2))
764 ssize_t ignore1, ignore2;
766 if ((check_ignore > 0)
767 && ((ignore1 = heap_comparison_ignore_size(state->to_ignore1, real_area1))
769 && ((ignore2 = heap_comparison_ignore_size(state->to_ignore2, real_area2))
774 simgrid::mc::Type *subtype, *subsubtype;
776 const void *addr_pointed1, *addr_pointed2;
778 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
779 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
781 switch (type->type) {
782 case DW_TAG_unspecified_type:
785 case DW_TAG_base_type:
786 if (!type->name.empty() && type->name == "char") { /* String, hence random (arbitrary ?) size */
787 if (real_area1 == real_area2)
790 return (MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, area_size) != 0);
792 if (area_size != -1 && type->byte_size != area_size)
795 return (MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0);
799 case DW_TAG_enumeration_type:
800 if (area_size != -1 && type->byte_size != area_size)
803 return (MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0);
806 case DW_TAG_const_type:
807 case DW_TAG_volatile_type:
809 type = type->subtype;
812 case DW_TAG_array_type:
813 subtype = type->subtype;
814 switch (subtype->type) {
815 case DW_TAG_unspecified_type:
818 case DW_TAG_base_type:
819 case DW_TAG_enumeration_type:
820 case DW_TAG_pointer_type:
821 case DW_TAG_reference_type:
822 case DW_TAG_rvalue_reference_type:
823 case DW_TAG_structure_type:
824 case DW_TAG_class_type:
825 case DW_TAG_union_type:
826 if (subtype->full_type)
827 subtype = subtype->full_type;
828 elm_size = subtype->byte_size;
830 // TODO, just remove the type indirection?
831 case DW_TAG_const_type:
833 case DW_TAG_volatile_type:
834 subsubtype = subtype->subtype;
835 if (subsubtype->full_type)
836 subsubtype = subsubtype->full_type;
837 elm_size = subsubtype->byte_size;
843 for (int i = 0; i < type->element_count; i++) {
844 // TODO, add support for variable stride (DW_AT_byte_stride)
846 compare_heap_area_with_type(state, process_index,
847 (char *) real_area1 + (i * elm_size),
848 (char *) real_area2 + (i * elm_size),
849 snapshot1, snapshot2, previous,
850 type->subtype, subtype->byte_size,
851 check_ignore, pointer_level);
856 case DW_TAG_reference_type:
857 case DW_TAG_rvalue_reference_type:
858 case DW_TAG_pointer_type:
859 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
860 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
861 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
862 return (addr_pointed1 != addr_pointed2);;
865 if (pointer_level > 1) { /* Array of pointers */
866 for (size_t i = 0; i < (area_size / sizeof(void *)); i++) {
867 addr_pointed1 = snapshot1->read(
868 remote((void**)((char*) real_area1 + i * sizeof(void *))),
870 addr_pointed2 = snapshot2->read(
871 remote((void**)((char*) real_area2 + i * sizeof(void *))),
873 if (addr_pointed1 > state->std_heap_copy.heapbase
874 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
875 && addr_pointed2 > state->std_heap_copy.heapbase
876 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
878 compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
879 snapshot2, previous, type->subtype,
882 res = (addr_pointed1 != addr_pointed2);
887 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
888 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
889 if (addr_pointed1 > state->std_heap_copy.heapbase
890 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
891 && addr_pointed2 > state->std_heap_copy.heapbase
892 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
893 return compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
894 snapshot2, previous, type->subtype,
897 return (addr_pointed1 != addr_pointed2);
901 case DW_TAG_structure_type:
902 case DW_TAG_class_type:
904 type = type->full_type;
905 if (area_size != -1 && type->byte_size != area_size) {
906 if (area_size > type->byte_size && area_size % type->byte_size == 0) {
907 for (size_t i = 0; i < (size_t)(area_size / type->byte_size); i++) {
909 compare_heap_area_with_type(state, process_index,
910 (char *) real_area1 + i * type->byte_size,
911 (char *) real_area2 + i * type->byte_size,
912 snapshot1, snapshot2, previous, type, -1,
921 for(simgrid::mc::Member& member : type->members) {
922 // TODO, optimize this? (for the offset case)
923 void *real_member1 = simgrid::dwarf::resolve_member(
924 real_area1, type, &member, (simgrid::mc::AddressSpace*) snapshot1, process_index);
925 void *real_member2 = simgrid::dwarf::resolve_member(
926 real_area2, type, &member, (simgrid::mc::AddressSpace*) snapshot2, process_index);
928 compare_heap_area_with_type(state, process_index, real_member1, real_member2,
929 snapshot1, snapshot2,
930 previous, member.type, -1,
938 case DW_TAG_union_type:
939 return compare_heap_area_without_type(state, process_index, real_area1, real_area2,
940 snapshot1, snapshot2, previous,
941 type->byte_size, check_ignore);
951 /** Infer the type of a part of the block from the type of the block
953 * TODO, handle DW_TAG_array_type as well as arrays of the object ((*p)[5], p[5])
955 * TODO, handle subfields ((*p).bar.foo, (*p)[5].bar…)
957 * @param type_id DWARF type ID of the root address
959 * @return DWARF type ID for given offset
961 static simgrid::mc::Type* get_offset_type(void *real_base_address, simgrid::mc::Type* type,
962 int offset, int area_size,
963 mc_snapshot_t snapshot, int process_index)
966 // Beginning of the block, the infered variable type if the type of the block:
970 switch (type->type) {
971 case DW_TAG_structure_type:
972 case DW_TAG_class_type:
974 type = type->full_type;
976 if (area_size != -1 && type->byte_size != area_size) {
977 if (area_size > type->byte_size && area_size % type->byte_size == 0)
982 for(simgrid::mc::Member& member : type->members) {
984 if (member.has_offset_location()) {
985 // We have the offset, use it directly (shortcut):
986 if (member.offset() == offset)
989 void *real_member = simgrid::dwarf::resolve_member(
990 real_base_address, type, &member, snapshot, process_index);
991 if ((char*) real_member - (char *) real_base_address == offset)
1000 /* FIXME : other cases ? */
1008 * @param area1 Process address for state 1
1009 * @param area2 Process address for state 2
1010 * @param snapshot1 Snapshot of state 1
1011 * @param snapshot2 Snapshot of state 2
1012 * @param previous Pairs of blocks already compared on the current path (or NULL)
1013 * @param type_id Type of variable
1014 * @param pointer_level
1015 * @return 0 (same), 1 (different), -1
1017 int compare_heap_area(int process_index, const void *area1, const void *area2, mc_snapshot_t snapshot1,
1018 mc_snapshot_t snapshot2, xbt_dynar_t previous,
1019 simgrid::mc::Type* type, int pointer_level)
1021 simgrid::mc::Process* process = &mc_model_checker->process();
1023 struct s_mc_diff *state = mc_diff_info;
1026 ssize_t block1, frag1, block2, frag2;
1028 int check_ignore = 0;
1030 void *real_addr_block1, *real_addr_block2, *real_addr_frag1, *real_addr_frag2;
1032 int offset1 = 0, offset2 = 0;
1033 int new_size1 = -1, new_size2 = -1;
1034 simgrid::mc::Type *new_type1 = NULL, *new_type2 = NULL;
1036 int match_pairs = 0;
1038 // This is the address of std_heap->heapinfo in the application process:
1039 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
1041 const malloc_info* heapinfos1 = snapshot1->read(
1042 remote((const malloc_info**)heapinfo_address), process_index);
1043 const malloc_info* heapinfos2 = snapshot2->read(
1044 remote((const malloc_info**)heapinfo_address), process_index);
1046 malloc_info heapinfo_temp1, heapinfo_temp2;
1048 if (previous == NULL) {
1050 xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
1053 // Get block number:
1056 (char *) state->std_heap_copy.heapbase) / BLOCKSIZE + 1;
1059 (char *) state->std_heap_copy.heapbase) / BLOCKSIZE + 1;
1061 // If either block is a stack block:
1062 if (is_block_stack((int) block1) && is_block_stack((int) block2)) {
1063 add_heap_area_pair(previous, block1, -1, block2, -1);
1065 match_equals(state, previous);
1066 xbt_dynar_free(&previous);
1070 // If either block is not in the expected area of memory:
1071 if (((char *) area1 < (char *) state->std_heap_copy.heapbase)
1072 || (block1 > (ssize_t) state->heapsize1) || (block1 < 1)
1073 || ((char *) area2 < (char *) state->std_heap_copy.heapbase)
1074 || (block2 > (ssize_t) state->heapsize2) || (block2 < 1)) {
1076 xbt_dynar_free(&previous);
1081 // Process address of the block:
1082 real_addr_block1 = (ADDR2UINT(block1) - 1) * BLOCKSIZE +
1083 (char *) state->std_heap_copy.heapbase;
1084 real_addr_block2 = (ADDR2UINT(block2) - 1) * BLOCKSIZE +
1085 (char *) state->std_heap_copy.heapbase;
1089 if (type->full_type)
1090 type = type->full_type;
1092 // This assume that for "boring" types (volatile ...) byte_size is absent:
1093 while (type->byte_size == 0 && type->subtype != NULL)
1094 type = type->subtype;
1097 if ((type->type == DW_TAG_pointer_type)
1098 || ((type->type == DW_TAG_base_type) && !type->name.empty()
1099 && type->name == "char"))
1102 type_size = type->byte_size;
1106 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
1107 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
1109 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
1110 heap_region1, &heapinfo_temp1, &heapinfos1[block1], sizeof(malloc_info));
1111 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
1112 heap_region2, &heapinfo_temp2, &heapinfos2[block2], sizeof(malloc_info));
1114 if ((heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type==MMALLOC_TYPE_HEAPINFO)
1115 && (heapinfo2->type == MMALLOC_TYPE_FREE || heapinfo2->type ==MMALLOC_TYPE_HEAPINFO)) {
1119 match_equals(state, previous);
1120 xbt_dynar_free(&previous);
1124 } else if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED
1125 && heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
1126 /* Complete block */
1128 // TODO, lookup variable type from block type as done for fragmented blocks
1130 offset1 = (char *) area1 - (char *) real_addr_block1;
1131 offset2 = (char *) area2 - (char *) real_addr_block2;
1133 if (state->equals_to1_(block1, 0).valid
1134 && state->equals_to2_(block2, 0).valid) {
1135 if (equal_blocks(state, block1, block2)) {
1137 match_equals(state, previous);
1138 xbt_dynar_free(&previous);
1144 if (type_size != -1) {
1145 if (type_size != (ssize_t) heapinfo1->busy_block.busy_size
1146 && type_size != (ssize_t) heapinfo2->busy_block.busy_size
1147 && (type->name.empty() || type->name == "struct s_smx_context")) {
1149 match_equals(state, previous);
1150 xbt_dynar_free(&previous);
1156 if (heapinfo1->busy_block.size !=
1157 heapinfo2->busy_block.size) {
1159 xbt_dynar_free(&previous);
1164 if (heapinfo1->busy_block.busy_size !=
1165 heapinfo2->busy_block.busy_size) {
1167 xbt_dynar_free(&previous);
1172 if (!add_heap_area_pair(previous, block1, -1, block2, -1)) {
1174 match_equals(state, previous);
1175 xbt_dynar_free(&previous);
1180 size = heapinfo1->busy_block.busy_size;
1182 // Remember (basic) type inference.
1183 // The current data structure only allows us to do this for the whole block.
1184 if (type != NULL && area1 == real_addr_block1) {
1185 state->types1_(block1, 0) = type;
1187 if (type != NULL && area2 == real_addr_block2) {
1188 state->types2_(block2, 0) = type;
1193 match_equals(state, previous);
1194 xbt_dynar_free(&previous);
1202 if ((heapinfo1->busy_block.ignore > 0)
1203 && (heapinfo2->busy_block.ignore ==
1204 heapinfo1->busy_block.ignore))
1205 check_ignore = heapinfo1->busy_block.ignore;
1207 } else if ((heapinfo1->type > 0) && (heapinfo2->type > 0)) { /* Fragmented block */
1211 ((uintptr_t) (ADDR2UINT(area1) % (BLOCKSIZE))) >> heapinfo1->type;
1213 ((uintptr_t) (ADDR2UINT(area2) % (BLOCKSIZE))) >> heapinfo2->type;
1215 // Process address of the fragment:
1217 (void *) ((char *) real_addr_block1 +
1218 (frag1 << heapinfo1->type));
1220 (void *) ((char *) real_addr_block2 +
1221 (frag2 << heapinfo2->type));
1223 // Check the size of the fragments against the size of the type:
1224 if (type_size != -1) {
1225 if (heapinfo1->busy_frag.frag_size[frag1] == -1
1226 || heapinfo2->busy_frag.frag_size[frag2] == -1) {
1228 match_equals(state, previous);
1229 xbt_dynar_free(&previous);
1234 if (type_size != heapinfo1->busy_frag.frag_size[frag1]
1235 || type_size != heapinfo2->busy_frag.frag_size[frag2]) {
1237 match_equals(state, previous);
1238 xbt_dynar_free(&previous);
1244 // Check if the blocks are already matched together:
1245 if (state->equals_to1_(block1, frag1).valid
1246 && state->equals_to2_(block2, frag2).valid) {
1247 if (offset1==offset2 && equal_fragments(state, block1, frag1, block2, frag2)) {
1249 match_equals(state, previous);
1250 xbt_dynar_free(&previous);
1255 // Compare the size of both fragments:
1256 if (heapinfo1->busy_frag.frag_size[frag1] !=
1257 heapinfo2->busy_frag.frag_size[frag2]) {
1258 if (type_size == -1) {
1260 match_equals(state, previous);
1261 xbt_dynar_free(&previous);
1266 xbt_dynar_free(&previous);
1272 // Size of the fragment:
1273 size = heapinfo1->busy_frag.frag_size[frag1];
1275 // Remember (basic) type inference.
1276 // The current data structure only allows us to do this for the whole fragment.
1277 if (type != NULL && area1 == real_addr_frag1) {
1278 state->types1_(block1, frag1) = type;
1280 if (type != NULL && area2 == real_addr_frag2) {
1281 state->types2_(block2, frag2) = type;
1283 // The type of the variable is already known:
1288 // Type inference from the block type.
1289 else if (state->types1_(block1, frag1) != NULL
1290 || state->types2_(block2, frag2) != NULL) {
1292 offset1 = (char *) area1 - (char *) real_addr_frag1;
1293 offset2 = (char *) area2 - (char *) real_addr_frag2;
1295 if (state->types1_(block1, frag1) != NULL
1296 && state->types2_(block2, frag2) != NULL) {
1298 get_offset_type(real_addr_frag1, state->types1_(block1, frag1),
1299 offset1, size, snapshot1, process_index);
1301 get_offset_type(real_addr_frag2, state->types2_(block2, frag2),
1302 offset1, size, snapshot2, process_index);
1303 } else if (state->types1_(block1, frag1) != NULL) {
1305 get_offset_type(real_addr_frag1, state->types1_(block1, frag1),
1306 offset1, size, snapshot1, process_index);
1308 get_offset_type(real_addr_frag2, state->types1_(block1, frag1),
1309 offset2, size, snapshot2, process_index);
1310 } else if (state->types2_(block2, frag2) != NULL) {
1312 get_offset_type(real_addr_frag1, state->types2_(block2, frag2),
1313 offset1, size, snapshot1, process_index);
1315 get_offset_type(real_addr_frag2, state->types2_(block2, frag2),
1316 offset2, size, snapshot2, process_index);
1319 match_equals(state, previous);
1320 xbt_dynar_free(&previous);
1325 if (new_type1 != NULL && new_type2 != NULL && new_type1 != new_type2) {
1328 while (type->byte_size == 0 && type->subtype != NULL)
1329 type = type->subtype;
1330 new_size1 = type->byte_size;
1333 while (type->byte_size == 0 && type->subtype != NULL)
1334 type = type->subtype;
1335 new_size2 = type->byte_size;
1339 match_equals(state, previous);
1340 xbt_dynar_free(&previous);
1346 if (new_size1 > 0 && new_size1 == new_size2) {
1351 if (offset1 == 0 && offset2 == 0) {
1352 if (!add_heap_area_pair(previous, block1, frag1, block2, frag2)) {
1354 match_equals(state, previous);
1355 xbt_dynar_free(&previous);
1363 match_equals(state, previous);
1364 xbt_dynar_free(&previous);
1369 if ((heapinfo1->busy_frag.ignore[frag1] > 0)
1370 && (heapinfo2->busy_frag.ignore[frag2] ==
1371 heapinfo1->busy_frag.ignore[frag1]))
1372 check_ignore = heapinfo1->busy_frag.ignore[frag1];
1377 xbt_dynar_free(&previous);
1384 /* Start comparison */
1387 compare_heap_area_with_type(state, process_index, area1, area2, snapshot1, snapshot2,
1388 previous, type, size, check_ignore,
1392 compare_heap_area_without_type(state, process_index, area1, area2, snapshot1, snapshot2,
1393 previous, size, check_ignore);
1395 if (res_compare == 1) {
1397 xbt_dynar_free(&previous);
1402 match_equals(state, previous);
1403 xbt_dynar_free(&previous);
1409 /*********************************************** Miscellaneous ***************************************************/
1410 /****************************************************************************************************************/
1412 // Not used and broken code:
1416 static int get_pointed_area_size(void *area, int heap)
1419 struct s_mc_diff *state = mc_diff_info;
1422 malloc_info *heapinfo;
1425 heapinfo = state->heapinfo1;
1427 heapinfo = state->heapinfo2;
1431 (char *) state->std_heap_copy.heapbase) / BLOCKSIZE + 1;
1433 if (((char *) area < (char *) state->std_heap_copy.heapbase)
1434 || (block > state->heapsize1) || (block < 1))
1437 if (heapinfo[block].type == MMALLOC_TYPE_FREE || heapinfo[block].type == MMALLOC_TYPE_HEAPINFO) { /* Free block */
1439 } else if (heapinfo[block].type == MMALLOC_TYPE_UNFRAGMENTED) { /* Complete block */
1440 return (int) heapinfo[block].busy_block.busy_size;
1443 ((uintptr_t) (ADDR2UINT(area) % (BLOCKSIZE))) >> heapinfo[block].type;
1444 return (int) heapinfo[block].busy_frag.frag_size[frag];
1449 #define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
1453 int mmalloc_linear_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2)
1456 struct s_mc_diff *state = mc_diff_info;
1458 if (heap1 == NULL && heap1 == NULL) {
1459 XBT_DEBUG("Malloc descriptors null");
1463 if (heap1->heaplimit != heap2->heaplimit) {
1464 XBT_DEBUG("Different limit of valid info table indices");
1468 /* Heap information */
1469 state->heaplimit = ((struct mdesc *) heap1)->heaplimit;
1471 state->std_heap_copy = *mc_model_checker->process().get_heap();
1473 state->heapbase1 = (char *) heap1 + BLOCKSIZE;
1474 state->heapbase2 = (char *) heap2 + BLOCKSIZE;
1477 (malloc_info *) ((char *) heap1 +
1479 ((char *) heap1->heapinfo - (char *) state->s_heap)));
1481 (malloc_info *) ((char *) heap2 +
1483 ((char *) heap2->heapinfo - (char *) state->s_heap)));
1485 state->heapsize1 = heap1->heapsize;
1486 state->heapsize2 = heap2->heapsize;
1488 /* Start comparison */
1490 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
1494 /* Check busy blocks */
1498 while (i <= state->heaplimit) {
1501 ((void *) (((ADDR2UINT(i)) - 1) * BLOCKSIZE +
1502 (char *) state->heapbase1));
1504 ((void *) (((ADDR2UINT(i)) - 1) * BLOCKSIZE +
1505 (char *) state->heapbase2));
1507 if (state->heapinfo1[i].type != state->heapinfo2[i].type) {
1509 distance += BLOCKSIZE;
1510 XBT_DEBUG("Different type of blocks (%zu) : %d - %d -> distance = %d", i,
1511 state->heapinfo1[i].type, state->heapinfo2[i].type, distance);
1516 if (state->heapinfo1[i].type == MMALLOC_TYPE_FREE
1517 || state->heapinfo1[i].type == MMALLOC_TYPE_HAPINFO) { /* Free block */
1522 if (state->heapinfo1[i].type == MMALLOC_TYPE_UNFRAGMENTED) { /* Large block */
1524 if (state->heapinfo1[i].busy_block.size !=
1525 state->heapinfo2[i].busy_block.size) {
1527 BLOCKSIZE * max(state->heapinfo1[i].busy_block.size,
1528 state->heapinfo2[i].busy_block.size);
1529 i += max(state->heapinfo1[i].busy_block.size,
1530 state->heapinfo2[i].busy_block.size);
1532 ("Different larger of cluster at block %zu : %zu - %zu -> distance = %d",
1533 i, state->heapinfo1[i].busy_block.size,
1534 state->heapinfo2[i].busy_block.size, distance);
1538 /*if(heapinfo1[i].busy_block.busy_size != heapinfo2[i].busy_block.busy_size){
1539 distance += max(heapinfo1[i].busy_block.busy_size, heapinfo2[i].busy_block.busy_size);
1540 i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
1541 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);
1547 //while(k < (heapinfo1[i].busy_block.busy_size)){
1548 while (k < state->heapinfo1[i].busy_block.size * BLOCKSIZE) {
1549 if (memcmp((char *) addr_block1 + k, (char *) addr_block2 + k, 1) !=
1558 } else { /* Fragmented block */
1560 for (j = 0; j < (size_t) (BLOCKSIZE >> state->heapinfo1[i].type); j++) {
1563 (void *) ((char *) addr_block1 + (j << state->heapinfo1[i].type));
1565 (void *) ((char *) addr_block2 + (j << state->heapinfo2[i].type));
1567 if (state->heapinfo1[i].busy_frag.frag_size[j] == 0
1568 && state->heapinfo2[i].busy_frag.frag_size[j] == 0) {
1573 /*if(heapinfo1[i].busy_frag.frag_size[j] != heapinfo2[i].busy_frag.frag_size[j]){
1574 distance += max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j]);
1575 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);
1581 //while(k < max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j])){
1582 while (k < (BLOCKSIZE / (BLOCKSIZE >> state->heapinfo1[i].type))) {
1583 if (memcmp((char *) addr_frag1 + k, (char *) addr_frag2 + k, 1) !=