1 /* Copyright (c) 2008-2016. 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 /** \file mc_compare.cpp Memory snapshooting and comparison */
15 #include <unordered_set>
17 #include <xbt/sysdep.h>
18 #include <xbt/dynar.h>
19 #include <xbt/mmalloc.h>
22 #include <mc/datatypes.h>
24 #include "src/internal_config.h"
26 #include "src/xbt/mmalloc/mmprivate.h"
27 #include "src/xbt/ex_interface.h"
30 #include "src/smpi/private.h"
33 #include "src/mc/mc_forward.hpp"
34 #include "src/mc/mc_safety.h"
35 #include "src/mc/mc_private.h"
36 #include "src/mc/mc_smx.h"
37 #include "src/mc/mc_dwarf.hpp"
38 #include "src/mc/Frame.hpp"
39 #include "src/mc/ObjectInformation.hpp"
40 #include "src/mc/Variable.hpp"
41 #include "src/mc/mc_private.h"
42 #include "src/mc/mc_snapshot.h"
43 #include "src/mc/mc_dwarf.hpp"
44 #include "src/mc/Type.hpp"
46 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_compare, xbt,
47 "Logging specific to mc_compare in mc");
53 typedef std::array<HeapLocation, 2> HeapLocationPair;
54 typedef std::set<HeapLocationPair> HeapLocationPairs;
56 struct ProcessComparisonState;
57 struct StateComparator;
59 static int compare_heap_area(
60 StateComparator& state,
61 int process_index, const void *area1, const void* area2,
62 Snapshot* snapshot1, Snapshot* snapshot2,
63 HeapLocationPairs* previous, Type* type, int pointer_level);
68 using simgrid::mc::remote;
70 /*********************************** Heap comparison ***********************************/
71 /***************************************************************************************/
81 HeapLocation(int block, int fragment = 0) : block(block), fragment(fragment) {}
83 bool operator==(HeapLocation const& that) const
85 return block == that.block && fragment == that.fragment;
87 bool operator<(HeapLocation const& that) const
89 return std::make_pair(block, fragment)
90 < std::make_pair(that.block, that.fragment);
95 HeapLocationPair makeHeapLocationPair(int block1, int fragment1, int block2, int fragment2)
97 return simgrid::mc::HeapLocationPair({
98 simgrid::mc::HeapLocation(block1, fragment1),
99 simgrid::mc::HeapLocation(block2, fragment2)
103 struct HeapArea : public HeapLocation {
109 : valid(true), block(block) {}
110 HeapArea(int block, int fragment = 0)
111 : valid(true), block(block), fragment(fragment) {}
114 struct ProcessComparisonState {
115 std::vector<simgrid::mc::IgnoredHeapRegion>* to_ignore = nullptr;
116 std::vector<HeapArea> equals_to;
117 std::vector<simgrid::mc::Type*> types;
118 std::size_t heapsize = 0;
120 void initHeapInformation(xbt_mheap_t heap,
121 std::vector<simgrid::mc::IgnoredHeapRegion>* i);
126 /** A hash which works with more stuff
128 * It can hash pairs: the standard hash currently doesn't include this.
130 template<class X> struct hash : public std::hash<X> {};
132 template<class X, class Y>
133 struct hash<std::pair<X,Y>> {
134 std::size_t operator()(std::pair<X,Y>const& x) const
138 return h1(x.first) ^ h2(x.second);
145 struct StateComparator {
146 s_xbt_mheap_t std_heap_copy;
147 std::size_t heaplimit;
148 std::array<ProcessComparisonState, 2> processStates;
150 std::unordered_set<std::pair<void*, void*>, hash<std::pair<void*, void*>>> compared_pointers;
154 compared_pointers.clear();
157 int initHeapInformation(
158 xbt_mheap_t heap1, xbt_mheap_t heap2,
159 std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
160 std::vector<simgrid::mc::IgnoredHeapRegion>* i2);
162 HeapArea& equals_to1_(std::size_t i, std::size_t j)
164 return processStates[0].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
166 HeapArea& equals_to2_(std::size_t i, std::size_t j)
168 return processStates[1].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
170 Type*& types1_(std::size_t i, std::size_t j)
172 return processStates[0].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
174 Type*& types2_(std::size_t i, std::size_t j)
176 return processStates[1].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
179 HeapArea const& equals_to1_(std::size_t i, std::size_t j) const
181 return processStates[0].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
183 HeapArea const& equals_to2_(std::size_t i, std::size_t j) const
185 return processStates[1].equals_to[ MAX_FRAGMENT_PER_BLOCK * i + j];
187 Type* const& types1_(std::size_t i, std::size_t j) const
189 return processStates[0].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
191 Type* const& types2_(std::size_t i, std::size_t j) const
193 return processStates[1].types[ MAX_FRAGMENT_PER_BLOCK * i + j];
196 /** Check whether two blocks are known to be matching
198 * @param state State used
199 * @param b1 Block of state 1
200 * @param b2 Block of state 2
201 * @return if the blocks are known to be matching
203 bool blocksEqual(int b1, int b2) const
205 return this->equals_to1_(b1, 0).block == b2
206 && this->equals_to2_(b2, 0).block == b1;
209 /** Check whether two fragments are known to be matching
211 * @param state State used
212 * @param b1 Block of state 1
213 * @param f1 Fragment of state 1
214 * @param b2 Block of state 2
215 * @param f2 Fragment of state 2
216 * @return if the fragments are known to be matching
218 int fragmentsEqual(int b1, int f1, int b2, int f2) const
220 return this->equals_to1_(b1, f1).block == b2
221 && this->equals_to1_(b1, f1).fragment == f2
222 && this->equals_to2_(b2, f2).block == b1
223 && this->equals_to2_(b2, f2).fragment == f1;
226 void match_equals(HeapLocationPairs* list);
232 /************************************************************************************/
234 static ssize_t heap_comparison_ignore_size(
235 std::vector<simgrid::mc::IgnoredHeapRegion>* ignore_list,
239 int end = ignore_list->size() - 1;
241 while (start <= end) {
242 unsigned int cursor = (start + end) / 2;
243 simgrid::mc::IgnoredHeapRegion const& region = (*ignore_list)[cursor];
244 if (region.address == address)
246 if (region.address < address)
248 if (region.address > address)
255 static bool is_stack(const void *address)
257 for (auto const& stack : mc_model_checker->process().stack_areas())
258 if (address == stack.address)
263 // TODO, this should depend on the snapshot?
264 static bool is_block_stack(int block)
266 for (auto const& stack : mc_model_checker->process().stack_areas())
267 if (block == stack.block)
275 void StateComparator::match_equals(HeapLocationPairs* list)
277 for (auto const& pair : *list) {
278 if (pair[0].fragment != -1) {
279 this->equals_to1_(pair[0].block, pair[0].fragment) =
280 simgrid::mc::HeapArea(pair[1].block, pair[1].fragment);
281 this->equals_to2_(pair[1].block, pair[1].fragment) =
282 simgrid::mc::HeapArea(pair[0].block, pair[0].fragment);
284 this->equals_to1_(pair[0].block, 0) =
285 simgrid::mc::HeapArea(pair[1].block, pair[1].fragment);
286 this->equals_to2_(pair[1].block, 0) =
287 simgrid::mc::HeapArea(pair[0].block, pair[0].fragment);
292 void ProcessComparisonState::initHeapInformation(xbt_mheap_t heap,
293 std::vector<simgrid::mc::IgnoredHeapRegion>* i)
295 auto heaplimit = ((struct mdesc *) heap)->heaplimit;
296 this->heapsize = ((struct mdesc *) heap)->heapsize;
298 this->equals_to.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, HeapArea());
299 this->types.assign(heaplimit * MAX_FRAGMENT_PER_BLOCK, nullptr);
302 int StateComparator::initHeapInformation(xbt_mheap_t heap1, xbt_mheap_t heap2,
303 std::vector<simgrid::mc::IgnoredHeapRegion>* i1,
304 std::vector<simgrid::mc::IgnoredHeapRegion>* i2)
306 if ((((struct mdesc *) heap1)->heaplimit !=
307 ((struct mdesc *) heap2)->heaplimit)
309 ((((struct mdesc *) heap1)->heapsize !=
310 ((struct mdesc *) heap2)->heapsize)))
312 this->heaplimit = ((struct mdesc *) heap1)->heaplimit;
313 this->std_heap_copy = *mc_model_checker->process().get_heap();
314 this->processStates[0].initHeapInformation(heap1, i1);
315 this->processStates[1].initHeapInformation(heap2, i2);
319 // TODO, have a robust way to find it in O(1)
321 mc_mem_region_t MC_get_heap_region(simgrid::mc::Snapshot* snapshot)
323 for (auto& region : snapshot->snapshot_regions)
324 if (region->region_type() == simgrid::mc::RegionType::Heap)
326 xbt_die("No heap region");
330 int mmalloc_compare_heap(
331 simgrid::mc::StateComparator& state, simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2)
333 simgrid::mc::Process* process = &mc_model_checker->process();
335 /* Start comparison */
336 size_t i1, i2, j1, j2, k;
337 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
338 int nb_diff1 = 0, nb_diff2 = 0;
340 int equal, res_compare = 0;
342 /* Check busy blocks */
346 malloc_info heapinfo_temp1, heapinfo_temp2;
347 malloc_info heapinfo_temp2b;
349 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
350 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
352 // This is the address of std_heap->heapinfo in the application process:
353 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
355 // This is in snapshot do not use them directly:
356 const malloc_info* heapinfos1 = snapshot1->read<malloc_info*>(
357 (std::uint64_t)heapinfo_address, simgrid::mc::ProcessIndexMissing);
358 const malloc_info* heapinfos2 = snapshot2->read<malloc_info*>(
359 (std::uint64_t)heapinfo_address, simgrid::mc::ProcessIndexMissing);
361 while (i1 < state.heaplimit) {
363 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(heap_region1, &heapinfo_temp1, &heapinfos1[i1], sizeof(malloc_info));
364 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2, &heapinfos2[i1], sizeof(malloc_info));
366 if (heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type == MMALLOC_TYPE_HEAPINFO) { /* Free block */
371 if (heapinfo1->type < 0) {
372 fprintf(stderr, "Unkown mmalloc block type.\n");
377 ((void *) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE +
378 (char *) state.std_heap_copy.heapbase));
380 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED) { /* Large block */
382 if (is_stack(addr_block1)) {
383 for (k = 0; k < heapinfo1->busy_block.size; k++)
384 state.equals_to1_(i1 + k, 0) = HeapArea(i1, -1);
385 for (k = 0; k < heapinfo2->busy_block.size; k++)
386 state.equals_to2_(i1 + k, 0) = HeapArea(i1, -1);
387 i1 += heapinfo1->busy_block.size;
391 if (state.equals_to1_(i1, 0).valid) {
400 /* Try first to associate to same block in the other heap */
401 if (heapinfo2->type == heapinfo1->type
402 && state.equals_to2_(i1, 0).valid == 0) {
403 addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE +
404 (char *) state.std_heap_copy.heapbase;
405 res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing,
406 addr_block1, addr_block2, snapshot1, snapshot2,
407 nullptr, nullptr, 0);
408 if (res_compare != 1) {
409 for (k = 1; k < heapinfo2->busy_block.size; k++)
410 state.equals_to2_(i1 + k, 0) = HeapArea(i1, -1);
411 for (k = 1; k < heapinfo1->busy_block.size; k++)
412 state.equals_to1_(i1 + k, 0) = HeapArea(i1, -1);
414 i1 += heapinfo1->busy_block.size;
418 while (i2 < state.heaplimit && !equal) {
420 addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE +
421 (char *) state.std_heap_copy.heapbase;
428 const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(heap_region2, &heapinfo_temp2b, &heapinfos2[i2], sizeof(malloc_info));
430 if (heapinfo2b->type != MMALLOC_TYPE_UNFRAGMENTED) {
435 if (state.equals_to2_(i2, 0).valid) {
440 res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing,
441 addr_block1, addr_block2, snapshot1, snapshot2,
442 nullptr, nullptr, 0);
444 if (res_compare != 1) {
445 for (k = 1; k < heapinfo2b->busy_block.size; k++)
446 state.equals_to2_(i2 + k, 0) = HeapArea(i1, -1);
447 for (k = 1; k < heapinfo1->busy_block.size; k++)
448 state.equals_to1_(i1 + k, 0) = HeapArea(i2, -1);
450 i1 += heapinfo1->busy_block.size;
458 XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1,
459 heapinfo1->busy_block.busy_size, addr_block1);
460 i1 = state.heaplimit + 1;
465 } else { /* Fragmented block */
467 for (j1 = 0; j1 < (size_t) (BLOCKSIZE >> heapinfo1->type); j1++) {
469 if (heapinfo1->busy_frag.frag_size[j1] == -1) /* Free fragment */
472 if (state.equals_to1_(i1, j1).valid)
476 (void *) ((char *) addr_block1 + (j1 << heapinfo1->type));
481 /* Try first to associate to same fragment in the other heap */
482 if (heapinfo2->type == heapinfo1->type
483 && !state.equals_to2_(i1, j1).valid) {
484 addr_block2 = (ADDR2UINT(i1) - 1) * BLOCKSIZE +
485 (char *) state.std_heap_copy.heapbase;
487 (void *) ((char *) addr_block2 +
488 (j1 << heapinfo2->type));
489 res_compare = compare_heap_area(state, simgrid::mc::ProcessIndexMissing,
490 addr_frag1, addr_frag2, snapshot1, snapshot2,
491 nullptr, nullptr, 0);
492 if (res_compare != 1)
498 while (i2 < state.heaplimit && !equal) {
500 const malloc_info* heapinfo2b = (const malloc_info*) MC_region_read(
501 heap_region2, &heapinfo_temp2b, &heapinfos2[i2],
502 sizeof(malloc_info));
504 if (heapinfo2b->type == MMALLOC_TYPE_FREE || heapinfo2b->type == MMALLOC_TYPE_HEAPINFO) {
509 // We currently do not match fragments with unfragmented blocks (maybe we should).
510 if (heapinfo2b->type == MMALLOC_TYPE_UNFRAGMENTED) {
515 if (heapinfo2b->type < 0) {
516 fprintf(stderr, "Unkown mmalloc block type.\n");
520 for (j2 = 0; j2 < (size_t) (BLOCKSIZE >> heapinfo2b->type);
523 if (i2 == i1 && j2 == j1)
526 if (state.equals_to2_(i2, j2).valid)
529 addr_block2 = (ADDR2UINT(i2) - 1) * BLOCKSIZE +
530 (char *) state.std_heap_copy.heapbase;
532 (void *) ((char *) addr_block2 +
533 (j2 << heapinfo2b->type));
535 res_compare = compare_heap_area(
536 state, simgrid::mc::ProcessIndexMissing,
537 addr_frag1, addr_frag2, snapshot2, snapshot2,
538 nullptr, nullptr, 0);
539 if (res_compare != 1) {
552 ("Block %zu, fragment %zu not found (size_used = %zd, address = %p)\n",
553 i1, j1, heapinfo1->busy_frag.frag_size[j1],
555 i2 = state.heaplimit + 1;
556 i1 = state.heaplimit + 1;
569 /* All blocks/fragments are equal to another block/fragment ? */
572 for(i = 1; i < state.heaplimit; i++) {
573 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
574 heap_region1, &heapinfo_temp1, &heapinfos1[i], sizeof(malloc_info));
576 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED
577 && i1 == state.heaplimit
578 && heapinfo1->busy_block.busy_size > 0
579 && !state.equals_to1_(i, 0).valid) {
580 XBT_DEBUG("Block %zu not found (size used = %zu)", i,
581 heapinfo1->busy_block.busy_size);
585 if (heapinfo1->type <= 0)
587 for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo1->type); j++)
588 if (i1 == state.heaplimit
589 && heapinfo1->busy_frag.frag_size[j] > 0
590 && !state.equals_to1_(i, j).valid) {
591 XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)",
592 i, j, heapinfo1->busy_frag.frag_size[j]);
597 if (i1 == state.heaplimit)
598 XBT_DEBUG("Number of blocks/fragments not found in heap1 : %d", nb_diff1);
600 for (i=1; i < state.heaplimit; i++) {
601 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
602 heap_region2, &heapinfo_temp2, &heapinfos2[i], sizeof(malloc_info));
603 if (heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED
604 && i1 == state.heaplimit
605 && heapinfo2->busy_block.busy_size > 0
606 && !state.equals_to2_(i, 0).valid) {
607 XBT_DEBUG("Block %zu not found (size used = %zu)", i,
608 heapinfo2->busy_block.busy_size);
612 if (heapinfo2->type <= 0)
615 for (j = 0; j < (size_t) (BLOCKSIZE >> heapinfo2->type); j++)
616 if (i1 == state.heaplimit
617 && heapinfo2->busy_frag.frag_size[j] > 0
618 && !state.equals_to2_(i, j).valid) {
619 XBT_DEBUG("Block %zu, Fragment %zu not found (size used = %zd)",
620 i, j, heapinfo2->busy_frag.frag_size[j]);
626 if (i1 == state.heaplimit)
627 XBT_DEBUG("Number of blocks/fragments not found in heap2 : %d", nb_diff2);
629 return nb_diff1 > 0 || nb_diff2 > 0;
635 * @param real_area1 Process address for state 1
636 * @param real_area2 Process address for state 2
637 * @param snapshot1 Snapshot of state 1
638 * @param snapshot2 Snapshot of state 2
641 * @param check_ignore
643 static int compare_heap_area_without_type(
644 simgrid::mc::StateComparator& state, int process_index,
645 const void *real_area1, const void *real_area2,
646 simgrid::mc::Snapshot* snapshot1,
647 simgrid::mc::Snapshot* snapshot2,
648 HeapLocationPairs* previous, int size,
651 simgrid::mc::Process* process = &mc_model_checker->process();
652 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
653 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
655 for (int i = 0; i < size; ) {
657 if (check_ignore > 0) {
658 ssize_t ignore1 = heap_comparison_ignore_size(
659 state.processStates[0].to_ignore, (char *) real_area1 + i);
661 ssize_t ignore2 = heap_comparison_ignore_size(
662 state.processStates[1].to_ignore, (char *) real_area2 + i);
663 if (ignore2 == ignore1) {
676 if (MC_snapshot_region_memcmp(((char *) real_area1) + i, heap_region1, ((char *) real_area2) + i, heap_region2, 1) != 0) {
678 int pointer_align = (i / sizeof(void *)) * sizeof(void *);
679 const void* addr_pointed1 = snapshot1->read(
680 remote((void**)((char *) real_area1 + pointer_align)), process_index);
681 const void* addr_pointed2 = snapshot2->read(
682 remote((void**)((char *) real_area2 + pointer_align)), process_index);
684 if (process->in_maestro_stack(remote(addr_pointed1))
685 && process->in_maestro_stack(remote(addr_pointed2))) {
686 i = pointer_align + sizeof(void *);
690 if (addr_pointed1 > state.std_heap_copy.heapbase
691 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
692 && addr_pointed2 > state.std_heap_copy.heapbase
693 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)) {
694 // Both addreses are in the heap:
695 int res_compare = compare_heap_area(state ,process_index,
696 addr_pointed1, addr_pointed2,
697 snapshot1, snapshot2, previous, nullptr, 0);
698 if (res_compare == 1)
700 i = pointer_align + sizeof(void *);
716 * @param real_area1 Process address for state 1
717 * @param real_area2 Process address for state 2
718 * @param snapshot1 Snapshot of state 1
719 * @param snapshot2 Snapshot of state 2
722 * @param area_size either a byte_size or an elements_count (?)
723 * @param check_ignore
724 * @param pointer_level
725 * @return 0 (same), 1 (different), -1 (unknown)
727 static int compare_heap_area_with_type(
728 simgrid::mc::StateComparator& state, int process_index,
729 const void *real_area1, const void *real_area2,
730 simgrid::mc::Snapshot* snapshot1,
731 simgrid::mc::Snapshot* snapshot2,
732 HeapLocationPairs* previous, simgrid::mc::Type* type,
733 int area_size, int check_ignore,
738 // HACK: This should not happen but in pratice, there are some
739 // DW_TAG_typedef without an associated DW_AT_type:
740 //<1><538832>: Abbrev Number: 111 (DW_TAG_typedef)
741 // <538833> DW_AT_name : (indirect string, offset: 0x2292f3): gregset_t
742 // <538837> DW_AT_decl_file : 98
743 // <538838> DW_AT_decl_line : 37
747 if (is_stack(real_area1) && is_stack(real_area2))
750 if (check_ignore > 0) {
751 ssize_t ignore1 = heap_comparison_ignore_size(
752 state.processStates[0].to_ignore, real_area1);
754 && heap_comparison_ignore_size(
755 state.processStates[1].to_ignore, real_area2) == ignore1)
759 simgrid::mc::Type *subtype, *subsubtype;
761 const void *addr_pointed1, *addr_pointed2;
763 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
764 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
766 switch (type->type) {
767 case DW_TAG_unspecified_type:
770 case DW_TAG_base_type:
771 if (!type->name.empty() && type->name == "char") { /* String, hence random (arbitrary ?) size */
772 if (real_area1 == real_area2)
775 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, area_size) != 0;
777 if (area_size != -1 && type->byte_size != area_size)
780 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
784 case DW_TAG_enumeration_type:
785 if (area_size != -1 && type->byte_size != area_size)
787 return MC_snapshot_region_memcmp(real_area1, heap_region1, real_area2, heap_region2, type->byte_size) != 0;
790 case DW_TAG_const_type:
791 case DW_TAG_volatile_type:
793 type = type->subtype;
796 case DW_TAG_array_type:
797 subtype = type->subtype;
798 switch (subtype->type) {
799 case DW_TAG_unspecified_type:
802 case DW_TAG_base_type:
803 case DW_TAG_enumeration_type:
804 case DW_TAG_pointer_type:
805 case DW_TAG_reference_type:
806 case DW_TAG_rvalue_reference_type:
807 case DW_TAG_structure_type:
808 case DW_TAG_class_type:
809 case DW_TAG_union_type:
810 if (subtype->full_type)
811 subtype = subtype->full_type;
812 elm_size = subtype->byte_size;
814 // TODO, just remove the type indirection?
815 case DW_TAG_const_type:
817 case DW_TAG_volatile_type:
818 subsubtype = subtype->subtype;
819 if (subsubtype->full_type)
820 subsubtype = subsubtype->full_type;
821 elm_size = subsubtype->byte_size;
827 for (int i = 0; i < type->element_count; i++) {
828 // TODO, add support for variable stride (DW_AT_byte_stride)
830 compare_heap_area_with_type(state, process_index,
831 (char *) real_area1 + (i * elm_size),
832 (char *) real_area2 + (i * elm_size),
833 snapshot1, snapshot2, previous,
834 type->subtype, subtype->byte_size,
835 check_ignore, pointer_level);
841 case DW_TAG_reference_type:
842 case DW_TAG_rvalue_reference_type:
843 case DW_TAG_pointer_type:
844 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
845 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
846 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
847 return (addr_pointed1 != addr_pointed2);
850 if (pointer_level <= 1) {
851 addr_pointed1 = snapshot1->read(remote((void**)real_area1), process_index);
852 addr_pointed2 = snapshot2->read(remote((void**)real_area2), process_index);
853 if (addr_pointed1 > state.std_heap_copy.heapbase
854 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
855 && addr_pointed2 > state.std_heap_copy.heapbase
856 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
857 return compare_heap_area(state, process_index,
858 addr_pointed1, addr_pointed2, snapshot1,
859 snapshot2, previous, type->subtype,
862 return (addr_pointed1 != addr_pointed2);
864 for (size_t i = 0; i < (area_size / sizeof(void *)); i++) {
865 addr_pointed1 = snapshot1->read(
866 remote((void**)((char*) real_area1 + i * sizeof(void *))),
868 addr_pointed2 = snapshot2->read(
869 remote((void**)((char*) real_area2 + i * sizeof(void *))),
871 if (addr_pointed1 > state.std_heap_copy.heapbase
872 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)
873 && addr_pointed2 > state.std_heap_copy.heapbase
874 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2))
876 compare_heap_area(state, process_index,
877 addr_pointed1, addr_pointed2, snapshot1,
878 snapshot2, previous, type->subtype,
881 res = (addr_pointed1 != addr_pointed2);
887 case DW_TAG_structure_type:
888 case DW_TAG_class_type:
890 type = type->full_type;
891 if (area_size != -1 && type->byte_size != area_size) {
892 if (area_size <= type->byte_size || area_size % type->byte_size != 0)
894 for (size_t i = 0; i < (size_t)(area_size / type->byte_size); i++) {
895 int res = compare_heap_area_with_type(state, process_index,
896 (char *) real_area1 + i * type->byte_size,
897 (char *) real_area2 + i * type->byte_size,
898 snapshot1, snapshot2, previous, type, -1,
904 for(simgrid::mc::Member& member : type->members) {
905 // TODO, optimize this? (for the offset case)
906 void *real_member1 = simgrid::dwarf::resolve_member(
907 real_area1, type, &member, (simgrid::mc::AddressSpace*) snapshot1, process_index);
908 void *real_member2 = simgrid::dwarf::resolve_member(
909 real_area2, type, &member, (simgrid::mc::AddressSpace*) snapshot2, process_index);
910 int res = compare_heap_area_with_type(
911 state, process_index, real_member1, real_member2,
912 snapshot1, snapshot2,
913 previous, member.type, -1,
921 case DW_TAG_union_type:
922 return compare_heap_area_without_type(state, process_index, real_area1, real_area2,
923 snapshot1, snapshot2, previous,
924 type->byte_size, check_ignore);
931 xbt_die("Unreachable");
934 /** Infer the type of a part of the block from the type of the block
936 * TODO, handle DW_TAG_array_type as well as arrays of the object ((*p)[5], p[5])
938 * TODO, handle subfields ((*p).bar.foo, (*p)[5].bar…)
940 * @param type_id DWARF type ID of the root address
942 * @return DWARF type ID for given offset
944 static simgrid::mc::Type* get_offset_type(void *real_base_address, simgrid::mc::Type* type,
945 int offset, int area_size,
946 simgrid::mc::Snapshot* snapshot, int process_index)
949 // Beginning of the block, the infered variable type if the type of the block:
953 switch (type->type) {
955 case DW_TAG_structure_type:
956 case DW_TAG_class_type:
958 type = type->full_type;
959 if (area_size != -1 && type->byte_size != area_size) {
960 if (area_size > type->byte_size && area_size % type->byte_size == 0)
966 for(simgrid::mc::Member& member : type->members) {
967 if (member.has_offset_location()) {
968 // We have the offset, use it directly (shortcut):
969 if (member.offset() == offset)
972 void *real_member = simgrid::dwarf::resolve_member(
973 real_base_address, type, &member, snapshot, process_index);
974 if ((char*) real_member - (char *) real_base_address == offset)
981 /* FIXME : other cases ? */
989 * @param area1 Process address for state 1
990 * @param area2 Process address for state 2
991 * @param snapshot1 Snapshot of state 1
992 * @param snapshot2 Snapshot of state 2
993 * @param previous Pairs of blocks already compared on the current path (or nullptr)
994 * @param type_id Type of variable
995 * @param pointer_level
996 * @return 0 (same), 1 (different), -1
999 int compare_heap_area(simgrid::mc::StateComparator& state, int process_index,
1000 const void *area1, const void *area2,
1001 simgrid::mc::Snapshot* snapshot1,
1002 simgrid::mc::Snapshot* snapshot2,
1003 HeapLocationPairs* previous,
1004 simgrid::mc::Type* type, int pointer_level)
1006 simgrid::mc::Process* process = &mc_model_checker->process();
1009 ssize_t block1, frag1, block2, frag2;
1011 int check_ignore = 0;
1013 void *real_addr_block1, *real_addr_block2, *real_addr_frag1, *real_addr_frag2;
1015 int offset1 = 0, offset2 = 0;
1016 int new_size1 = -1, new_size2 = -1;
1017 simgrid::mc::Type *new_type1 = nullptr, *new_type2 = nullptr;
1019 bool match_pairs = false;
1021 // This is the address of std_heap->heapinfo in the application process:
1022 void* heapinfo_address = &((xbt_mheap_t) process->heap_address)->heapinfo;
1024 const malloc_info* heapinfos1 = snapshot1->read(
1025 remote((const malloc_info**)heapinfo_address), process_index);
1026 const malloc_info* heapinfos2 = snapshot2->read(
1027 remote((const malloc_info**)heapinfo_address), process_index);
1029 malloc_info heapinfo_temp1, heapinfo_temp2;
1031 simgrid::mc::HeapLocationPairs current;
1032 if (previous == nullptr) {
1033 previous = ¤t;
1037 // Get block number:
1040 (char *) state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
1043 (char *) state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
1045 // If either block is a stack block:
1046 if (is_block_stack((int) block1) && is_block_stack((int) block2)) {
1047 previous->insert(simgrid::mc::makeHeapLocationPair(
1048 block1, -1, block2, -1));
1050 state.match_equals(previous);
1054 // If either block is not in the expected area of memory:
1055 if (((char *) area1 < (char *) state.std_heap_copy.heapbase)
1056 || (block1 > (ssize_t) state.processStates[0].heapsize) || (block1 < 1)
1057 || ((char *) area2 < (char *) state.std_heap_copy.heapbase)
1058 || (block2 > (ssize_t) state.processStates[1].heapsize) || (block2 < 1)) {
1062 // Process address of the block:
1063 real_addr_block1 = (ADDR2UINT(block1) - 1) * BLOCKSIZE +
1064 (char *) state.std_heap_copy.heapbase;
1065 real_addr_block2 = (ADDR2UINT(block2) - 1) * BLOCKSIZE +
1066 (char *) state.std_heap_copy.heapbase;
1070 if (type->full_type)
1071 type = type->full_type;
1073 // This assume that for "boring" types (volatile ...) byte_size is absent:
1074 while (type->byte_size == 0 && type->subtype != nullptr)
1075 type = type->subtype;
1078 if (type->type == DW_TAG_pointer_type
1079 || (type->type == DW_TAG_base_type && !type->name.empty()
1080 && type->name == "char"))
1083 type_size = type->byte_size;
1087 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
1088 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
1090 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
1091 heap_region1, &heapinfo_temp1, &heapinfos1[block1], sizeof(malloc_info));
1092 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
1093 heap_region2, &heapinfo_temp2, &heapinfos2[block2], sizeof(malloc_info));
1095 if ((heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type==MMALLOC_TYPE_HEAPINFO)
1096 && (heapinfo2->type == MMALLOC_TYPE_FREE || heapinfo2->type ==MMALLOC_TYPE_HEAPINFO)) {
1099 state.match_equals(previous);
1103 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED
1104 && heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
1105 /* Complete block */
1107 // TODO, lookup variable type from block type as done for fragmented blocks
1109 offset1 = (char *) area1 - (char *) real_addr_block1;
1110 offset2 = (char *) area2 - (char *) real_addr_block2;
1112 if (state.equals_to1_(block1, 0).valid
1113 && state.equals_to2_(block2, 0).valid
1114 && state.blocksEqual(block1, block2)) {
1116 state.match_equals(previous);
1120 if (type_size != -1) {
1121 if (type_size != (ssize_t) heapinfo1->busy_block.busy_size
1122 && type_size != (ssize_t) heapinfo2->busy_block.busy_size
1123 && (type->name.empty() || type->name == "struct s_smx_context")) {
1125 state.match_equals(previous);
1130 if (heapinfo1->busy_block.size != heapinfo2->busy_block.size)
1132 if (heapinfo1->busy_block.busy_size != heapinfo2->busy_block.busy_size)
1135 if (!previous->insert(simgrid::mc::makeHeapLocationPair(
1136 block1, -1, block2, -1)).second) {
1138 state.match_equals(previous);
1142 size = heapinfo1->busy_block.busy_size;
1144 // Remember (basic) type inference.
1145 // The current data structure only allows us to do this for the whole block.
1146 if (type != nullptr && area1 == real_addr_block1)
1147 state.types1_(block1, 0) = type;
1148 if (type != nullptr && area2 == real_addr_block2)
1149 state.types2_(block2, 0) = type;
1153 state.match_equals(previous);
1160 if (heapinfo1->busy_block.ignore > 0
1161 && heapinfo2->busy_block.ignore == heapinfo1->busy_block.ignore)
1162 check_ignore = heapinfo1->busy_block.ignore;
1164 } else if ((heapinfo1->type > 0) && (heapinfo2->type > 0)) { /* Fragmented block */
1168 ((uintptr_t) (ADDR2UINT(area1) % (BLOCKSIZE))) >> heapinfo1->type;
1170 ((uintptr_t) (ADDR2UINT(area2) % (BLOCKSIZE))) >> heapinfo2->type;
1172 // Process address of the fragment:
1174 (void *) ((char *) real_addr_block1 +
1175 (frag1 << heapinfo1->type));
1177 (void *) ((char *) real_addr_block2 +
1178 (frag2 << heapinfo2->type));
1180 // Check the size of the fragments against the size of the type:
1181 if (type_size != -1) {
1182 if (heapinfo1->busy_frag.frag_size[frag1] == -1
1183 || heapinfo2->busy_frag.frag_size[frag2] == -1) {
1185 state.match_equals(previous);
1189 if (type_size != heapinfo1->busy_frag.frag_size[frag1]
1190 || type_size != heapinfo2->busy_frag.frag_size[frag2]) {
1192 state.match_equals(previous);
1197 // Check if the blocks are already matched together:
1198 if (state.equals_to1_(block1, frag1).valid
1199 && state.equals_to2_(block2, frag2).valid) {
1200 if (offset1==offset2 && state.fragmentsEqual(block1, frag1, block2, frag2)) {
1202 state.match_equals(previous);
1206 // Compare the size of both fragments:
1207 if (heapinfo1->busy_frag.frag_size[frag1] !=
1208 heapinfo2->busy_frag.frag_size[frag2]) {
1209 if (type_size == -1) {
1211 state.match_equals(previous);
1217 // Size of the fragment:
1218 size = heapinfo1->busy_frag.frag_size[frag1];
1220 // Remember (basic) type inference.
1221 // The current data structure only allows us to do this for the whole fragment.
1222 if (type != nullptr && area1 == real_addr_frag1)
1223 state.types1_(block1, frag1) = type;
1224 if (type != nullptr && area2 == real_addr_frag2)
1225 state.types2_(block2, frag2) = type;
1227 // The type of the variable is already known:
1232 // Type inference from the block type.
1233 else if (state.types1_(block1, frag1) != nullptr
1234 || state.types2_(block2, frag2) != nullptr) {
1236 offset1 = (char *) area1 - (char *) real_addr_frag1;
1237 offset2 = (char *) area2 - (char *) real_addr_frag2;
1239 if (state.types1_(block1, frag1) != nullptr
1240 && state.types2_(block2, frag2) != nullptr) {
1242 get_offset_type(real_addr_frag1, state.types1_(block1, frag1),
1243 offset1, size, snapshot1, process_index);
1245 get_offset_type(real_addr_frag2, state.types2_(block2, frag2),
1246 offset1, size, snapshot2, process_index);
1247 } else if (state.types1_(block1, frag1) != nullptr) {
1249 get_offset_type(real_addr_frag1, state.types1_(block1, frag1),
1250 offset1, size, snapshot1, process_index);
1252 get_offset_type(real_addr_frag2, state.types1_(block1, frag1),
1253 offset2, size, snapshot2, process_index);
1254 } else if (state.types2_(block2, frag2) != nullptr) {
1256 get_offset_type(real_addr_frag1, state.types2_(block2, frag2),
1257 offset1, size, snapshot1, process_index);
1259 get_offset_type(real_addr_frag2, state.types2_(block2, frag2),
1260 offset2, size, snapshot2, process_index);
1263 state.match_equals(previous);
1267 if (new_type1 != nullptr && new_type2 != nullptr && new_type1 != new_type2) {
1270 while (type->byte_size == 0 && type->subtype != nullptr)
1271 type = type->subtype;
1272 new_size1 = type->byte_size;
1275 while (type->byte_size == 0 && type->subtype != nullptr)
1276 type = type->subtype;
1277 new_size2 = type->byte_size;
1281 state.match_equals(previous);
1286 if (new_size1 > 0 && new_size1 == new_size2) {
1291 if (offset1 == 0 && offset2 == 0
1292 && !previous->insert(simgrid::mc::makeHeapLocationPair(
1293 block1, frag1, block2, frag2)).second) {
1295 state.match_equals(previous);
1301 state.match_equals(previous);
1305 if ((heapinfo1->busy_frag.ignore[frag1] > 0)
1306 && (heapinfo2->busy_frag.ignore[frag2] ==
1307 heapinfo1->busy_frag.ignore[frag1]))
1308 check_ignore = heapinfo1->busy_frag.ignore[frag1];
1314 /* Start comparison */
1317 compare_heap_area_with_type(state, process_index, area1, area2, snapshot1, snapshot2,
1318 previous, type, size, check_ignore,
1322 compare_heap_area_without_type(state, process_index, area1, area2, snapshot1, snapshot2,
1323 previous, size, check_ignore);
1325 if (res_compare == 1)
1329 state.match_equals(previous);
1336 /************************** Snapshot comparison *******************************/
1337 /******************************************************************************/
1339 static int compare_areas_with_type(simgrid::mc::StateComparator& state,
1341 void* real_area1, simgrid::mc::Snapshot* snapshot1, mc_mem_region_t region1,
1342 void* real_area2, simgrid::mc::Snapshot* snapshot2, mc_mem_region_t region2,
1343 simgrid::mc::Type* type, int pointer_level)
1345 simgrid::mc::Process* process = &mc_model_checker->process();
1347 simgrid::mc::Type* subtype;
1348 simgrid::mc::Type* subsubtype;
1349 int elm_size, i, res;
1352 switch (type->type) {
1353 case DW_TAG_unspecified_type:
1356 case DW_TAG_base_type:
1357 case DW_TAG_enumeration_type:
1358 case DW_TAG_union_type:
1360 return MC_snapshot_region_memcmp(
1361 real_area1, region1, real_area2, region2,
1362 type->byte_size) != 0;
1364 case DW_TAG_typedef:
1365 case DW_TAG_volatile_type:
1366 case DW_TAG_const_type:
1368 type = type->subtype;
1370 case DW_TAG_array_type:
1371 subtype = type->subtype;
1372 switch (subtype->type) {
1373 case DW_TAG_unspecified_type:
1376 case DW_TAG_base_type:
1377 case DW_TAG_enumeration_type:
1378 case DW_TAG_pointer_type:
1379 case DW_TAG_reference_type:
1380 case DW_TAG_rvalue_reference_type:
1381 case DW_TAG_structure_type:
1382 case DW_TAG_class_type:
1383 case DW_TAG_union_type:
1384 if (subtype->full_type)
1385 subtype = subtype->full_type;
1386 elm_size = subtype->byte_size;
1388 case DW_TAG_const_type:
1389 case DW_TAG_typedef:
1390 case DW_TAG_volatile_type:
1391 subsubtype = subtype->subtype;
1392 if (subsubtype->full_type)
1393 subsubtype = subsubtype->full_type;
1394 elm_size = subsubtype->byte_size;
1400 for (i = 0; i < type->element_count; i++) {
1401 size_t off = i * elm_size;
1402 res = compare_areas_with_type(state, process_index,
1403 (char*) real_area1 + off, snapshot1, region1,
1404 (char*) real_area2 + off, snapshot2, region2,
1405 type->subtype, pointer_level);
1410 case DW_TAG_pointer_type:
1411 case DW_TAG_reference_type:
1412 case DW_TAG_rvalue_reference_type:
1414 void* addr_pointed1 = MC_region_read_pointer(region1, real_area1);
1415 void* addr_pointed2 = MC_region_read_pointer(region2, real_area2);
1417 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type)
1418 return (addr_pointed1 != addr_pointed2);
1419 if (addr_pointed1 == nullptr && addr_pointed2 == nullptr)
1421 if (addr_pointed1 == nullptr || addr_pointed2 == nullptr)
1423 if (!state.compared_pointers.insert(
1424 std::make_pair(addr_pointed1, addr_pointed2)).second)
1429 // Some cases are not handled here:
1430 // * the pointers lead to different areas (one to the heap, the other to the RW segment ...);
1431 // * a pointer leads to the read-only segment of the current object;
1432 // * a pointer lead to a different ELF object.
1434 if (addr_pointed1 > process->heap_address
1435 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)) {
1437 (addr_pointed2 > process->heap_address
1438 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)))
1440 // The pointers are both in the heap:
1441 return simgrid::mc::compare_heap_area(state,
1442 process_index, addr_pointed1, addr_pointed2, snapshot1,
1443 snapshot2, nullptr, type->subtype, pointer_level);
1446 // The pointers are both in the current object R/W segment:
1447 else if (region1->contain(simgrid::mc::remote(addr_pointed1))) {
1448 if (!region2->contain(simgrid::mc::remote(addr_pointed2)))
1451 return (addr_pointed1 != addr_pointed2);
1453 return compare_areas_with_type(state, process_index,
1454 addr_pointed1, snapshot1, region1,
1455 addr_pointed2, snapshot2, region2,
1456 type->subtype, pointer_level);
1459 // TODO, We do not handle very well the case where
1460 // it belongs to a different (non-heap) region from the current one.
1463 return (addr_pointed1 != addr_pointed2);
1467 case DW_TAG_structure_type:
1468 case DW_TAG_class_type:
1469 for(simgrid::mc::Member& member : type->members) {
1470 void *member1 = simgrid::dwarf::resolve_member(
1471 real_area1, type, &member, snapshot1, process_index);
1472 void *member2 = simgrid::dwarf::resolve_member(
1473 real_area2, type, &member, snapshot2, process_index);
1474 mc_mem_region_t subregion1 = mc_get_region_hinted(member1, snapshot1, process_index, region1);
1475 mc_mem_region_t subregion2 = mc_get_region_hinted(member2, snapshot2, process_index, region2);
1477 compare_areas_with_type(state, process_index,
1478 member1, snapshot1, subregion1,
1479 member2, snapshot2, subregion2,
1480 member.type, pointer_level);
1485 case DW_TAG_subroutine_type:
1489 XBT_VERB("Unknown case : %d", type->type);
1496 static int compare_global_variables(
1497 simgrid::mc::StateComparator& state,
1498 simgrid::mc::ObjectInformation* object_info,
1500 mc_mem_region_t r1, mc_mem_region_t r2,
1501 simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2)
1503 xbt_assert(r1 && r2, "Missing region.");
1506 if (r1->storage_type() == simgrid::mc::StorageType::Privatized) {
1507 xbt_assert(process_index >= 0);
1508 if (r2->storage_type() != simgrid::mc::StorageType::Privatized)
1511 size_t process_count = MC_smpi_process_count();
1512 xbt_assert(process_count == r1->privatized_data().size()
1513 && process_count == r2->privatized_data().size());
1515 // Compare the global variables separately for each simulates process:
1516 for (size_t process_index = 0; process_index < process_count; process_index++) {
1517 int is_diff = compare_global_variables(state,
1518 object_info, process_index,
1519 &r1->privatized_data()[process_index],
1520 &r2->privatized_data()[process_index],
1521 snapshot1, snapshot2);
1522 if (is_diff) return 1;
1527 xbt_assert(r1->storage_type() != simgrid::mc::StorageType::Privatized);
1529 xbt_assert(r2->storage_type() != simgrid::mc::StorageType::Privatized);
1531 std::vector<simgrid::mc::Variable>& variables = object_info->global_variables;
1533 for (simgrid::mc::Variable& current_var : variables) {
1535 // If the variable is not in this object, skip it:
1536 // We do not expect to find a pointer to something which is not reachable
1537 // by the global variables.
1538 if ((char *) current_var.address < (char *) object_info->start_rw
1539 || (char *) current_var.address > (char *) object_info->end_rw)
1542 simgrid::mc::Type* bvariable_type = current_var.type;
1543 int res = compare_areas_with_type(state, process_index,
1544 (char *) current_var.address, snapshot1, r1,
1545 (char *) current_var.address, snapshot2, r2,
1548 XBT_VERB("Global variable %s (%p) is different between snapshots",
1549 current_var.name.c_str(),
1550 (char *) current_var.address);
1560 static int compare_local_variables(simgrid::mc::StateComparator& state,
1562 simgrid::mc::Snapshot* snapshot1,
1563 simgrid::mc::Snapshot* snapshot2,
1564 mc_snapshot_stack_t stack1,
1565 mc_snapshot_stack_t stack2)
1567 if (stack1->local_variables.size() != stack2->local_variables.size()) {
1568 XBT_VERB("Different number of local variables");
1572 unsigned int cursor = 0;
1573 local_variable_t current_var1, current_var2;
1575 while (cursor < stack1->local_variables.size()) {
1576 current_var1 = &stack1->local_variables[cursor];
1577 current_var2 = &stack1->local_variables[cursor];
1578 if (current_var1->name != current_var2->name
1579 || current_var1->subprogram != current_var2->subprogram
1580 || current_var1->ip != current_var2->ip) {
1581 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1583 ("Different name of variable (%s - %s) "
1584 "or frame (%s - %s) or ip (%lu - %lu)",
1585 current_var1->name.c_str(),
1586 current_var2->name.c_str(),
1587 current_var1->subprogram->name.c_str(),
1588 current_var2->subprogram->name.c_str(),
1589 current_var1->ip, current_var2->ip);
1592 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1594 simgrid::mc::Type* subtype = current_var1->type;
1596 compare_areas_with_type(state, process_index,
1597 current_var1->address, snapshot1, mc_get_snapshot_region(current_var1->address, snapshot1, process_index),
1598 current_var2->address, snapshot2, mc_get_snapshot_region(current_var2->address, snapshot2, process_index),
1602 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1604 ("Local variable %s (%p - %p) in frame %s "
1605 "is different between snapshots",
1606 current_var1->name.c_str(),
1607 current_var1->address,
1608 current_var2->address,
1609 current_var1->subprogram->name.c_str());
1620 static std::unique_ptr<simgrid::mc::StateComparator> state_comparator;
1622 int snapshot_compare(int num1, simgrid::mc::Snapshot* s1, int num2, simgrid::mc::Snapshot* s2)
1624 // TODO, make this a field of ModelChecker or something similar
1626 if (state_comparator == nullptr)
1627 state_comparator = std::unique_ptr<StateComparator>(new StateComparator());
1629 state_comparator->clear();
1631 simgrid::mc::Process* process = &mc_model_checker->process();
1636 int hash_result = 0;
1638 hash_result = (s1->hash != s2->hash);
1640 XBT_VERB("(%d - %d) Different hash : 0x%" PRIx64 "--0x%" PRIx64, num1,
1641 num2, s1->hash, s2->hash);
1646 XBT_VERB("(%d - %d) Same hash : 0x%" PRIx64, num1, num2, s1->hash);
1649 /* Compare enabled processes */
1650 if (s1->enabled_processes != s2->enabled_processes) {
1651 XBT_VERB("(%d - %d) Different enabled processes", num1, num2);
1655 unsigned long i = 0;
1656 size_t size_used1, size_used2;
1659 /* Compare size of stacks */
1660 while (i < s1->stacks.size()) {
1661 size_used1 = s1->stack_sizes[i];
1662 size_used2 = s2->stack_sizes[i];
1663 if (size_used1 != size_used2) {
1665 XBT_DEBUG("(%d - %d) Different size used in stacks : %zu - %zu", num1,
1666 num2, size_used1, size_used2);
1671 XBT_VERB("(%d - %d) Different size used in stacks : %zu - %zu", num1,
1672 num2, size_used1, size_used2);
1680 /* Init heap information used in heap comparison algorithm */
1681 xbt_mheap_t heap1 = (xbt_mheap_t)s1->read_bytes(
1682 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1683 remote(process->heap_address),
1684 simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1685 xbt_mheap_t heap2 = (xbt_mheap_t)s2->read_bytes(
1686 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1687 remote(process->heap_address),
1688 simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1689 res_init = state_comparator->initHeapInformation(
1690 heap1, heap2, &s1->to_ignore, &s2->to_ignore);
1692 if (res_init == -1) {
1694 XBT_DEBUG("(%d - %d) Different heap information", num1, num2);
1698 XBT_VERB("(%d - %d) Different heap information", num1, num2);
1705 /* Stacks comparison */
1706 unsigned cursor = 0;
1711 mc_snapshot_stack_t stack1, stack2;
1712 while (cursor < s1->stacks.size()) {
1713 stack1 = &s1->stacks[cursor];
1714 stack2 = &s2->stacks[cursor];
1716 if (stack1->process_index != stack2->process_index) {
1718 XBT_DEBUG("(%d - %d) Stacks with different process index (%i vs %i)", num1, num2,
1719 stack1->process_index, stack2->process_index);
1721 else diff_local = compare_local_variables(*state_comparator,
1722 stack1->process_index, s1, s2, stack1, stack2);
1723 if (diff_local > 0) {
1725 XBT_DEBUG("(%d - %d) Different local variables between stacks %d", num1,
1732 XBT_VERB("(%d - %d) Different local variables between stacks %d", num1,
1742 size_t regions_count = s1->snapshot_regions.size();
1743 // TODO, raise a difference instead?
1744 xbt_assert(regions_count == s2->snapshot_regions.size());
1746 for (size_t k = 0; k != regions_count; ++k) {
1747 mc_mem_region_t region1 = s1->snapshot_regions[k].get();
1748 mc_mem_region_t region2 = s2->snapshot_regions[k].get();
1751 if (region1->region_type() != simgrid::mc::RegionType::Data)
1754 xbt_assert(region1->region_type() == region2->region_type());
1755 xbt_assert(region1->object_info() == region2->object_info());
1756 xbt_assert(region1->object_info());
1758 std::string const& name = region1->object_info()->file_name;
1760 /* Compare global variables */
1762 compare_global_variables(*state_comparator,
1763 region1->object_info(), simgrid::mc::ProcessIndexDisabled,
1764 region1, region2, s1, s2);
1768 XBT_DEBUG("(%d - %d) Different global variables in %s",
1769 num1, num2, name.c_str());
1773 XBT_VERB("(%d - %d) Different global variables in %s",
1774 num1, num2, name.c_str());
1783 if (simgrid::mc::mmalloc_compare_heap(*state_comparator, s1, s2) > 0) {
1786 XBT_DEBUG("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
1791 XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
1799 if (errors || hash_result)
1800 XBT_VERB("(%d - %d) Difference found", num1, num2);
1802 XBT_VERB("(%d - %d) No difference found", num1, num2);
1805 #if defined(MC_DEBUG) && defined(MC_VERBOSE)
1807 // * false positive SHOULD be avoided.
1808 // * There MUST not be any false negative.
1810 XBT_VERB("(%d - %d) State equality hash test is %s %s", num1, num2,
1811 (hash_result != 0) == (errors != 0) ? "true" : "false",
1812 !hash_result ? "positive" : "negative");
1816 return errors > 0 || hash_result;