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 int match_pairs = 0;
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 if (previous == nullptr) {
1032 previous = new HeapLocationPairs();
1036 // Get block number:
1039 (char *) state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
1042 (char *) state.std_heap_copy.heapbase) / BLOCKSIZE + 1;
1044 // If either block is a stack block:
1045 if (is_block_stack((int) block1) && is_block_stack((int) block2)) {
1046 previous->insert(simgrid::mc::makeHeapLocationPair(
1047 block1, -1, block2, -1));
1049 state.match_equals(previous);
1055 // If either block is not in the expected area of memory:
1056 if (((char *) area1 < (char *) state.std_heap_copy.heapbase)
1057 || (block1 > (ssize_t) state.processStates[0].heapsize) || (block1 < 1)
1058 || ((char *) area2 < (char *) state.std_heap_copy.heapbase)
1059 || (block2 > (ssize_t) state.processStates[1].heapsize) || (block2 < 1)) {
1065 // Process address of the block:
1066 real_addr_block1 = (ADDR2UINT(block1) - 1) * BLOCKSIZE +
1067 (char *) state.std_heap_copy.heapbase;
1068 real_addr_block2 = (ADDR2UINT(block2) - 1) * BLOCKSIZE +
1069 (char *) state.std_heap_copy.heapbase;
1073 if (type->full_type)
1074 type = type->full_type;
1076 // This assume that for "boring" types (volatile ...) byte_size is absent:
1077 while (type->byte_size == 0 && type->subtype != nullptr)
1078 type = type->subtype;
1081 if (type->type == DW_TAG_pointer_type
1082 || (type->type == DW_TAG_base_type && !type->name.empty()
1083 && type->name == "char"))
1086 type_size = type->byte_size;
1090 mc_mem_region_t heap_region1 = MC_get_heap_region(snapshot1);
1091 mc_mem_region_t heap_region2 = MC_get_heap_region(snapshot2);
1093 const malloc_info* heapinfo1 = (const malloc_info*) MC_region_read(
1094 heap_region1, &heapinfo_temp1, &heapinfos1[block1], sizeof(malloc_info));
1095 const malloc_info* heapinfo2 = (const malloc_info*) MC_region_read(
1096 heap_region2, &heapinfo_temp2, &heapinfos2[block2], sizeof(malloc_info));
1098 if ((heapinfo1->type == MMALLOC_TYPE_FREE || heapinfo1->type==MMALLOC_TYPE_HEAPINFO)
1099 && (heapinfo2->type == MMALLOC_TYPE_FREE || heapinfo2->type ==MMALLOC_TYPE_HEAPINFO)) {
1102 state.match_equals(previous);
1108 if (heapinfo1->type == MMALLOC_TYPE_UNFRAGMENTED
1109 && heapinfo2->type == MMALLOC_TYPE_UNFRAGMENTED) {
1110 /* Complete block */
1112 // TODO, lookup variable type from block type as done for fragmented blocks
1114 offset1 = (char *) area1 - (char *) real_addr_block1;
1115 offset2 = (char *) area2 - (char *) real_addr_block2;
1117 if (state.equals_to1_(block1, 0).valid
1118 && state.equals_to2_(block2, 0).valid
1119 && state.blocksEqual(block1, block2)) {
1121 state.match_equals(previous);
1127 if (type_size != -1) {
1128 if (type_size != (ssize_t) heapinfo1->busy_block.busy_size
1129 && type_size != (ssize_t) heapinfo2->busy_block.busy_size
1130 && (type->name.empty() || type->name == "struct s_smx_context")) {
1132 state.match_equals(previous);
1139 if (heapinfo1->busy_block.size != heapinfo2->busy_block.size) {
1145 if (heapinfo1->busy_block.busy_size != heapinfo2->busy_block.busy_size) {
1151 if (!previous->insert(simgrid::mc::makeHeapLocationPair(
1152 block1, -1, block2, -1)).second) {
1154 state.match_equals(previous);
1160 size = heapinfo1->busy_block.busy_size;
1162 // Remember (basic) type inference.
1163 // The current data structure only allows us to do this for the whole block.
1164 if (type != nullptr && area1 == real_addr_block1)
1165 state.types1_(block1, 0) = type;
1166 if (type != nullptr && area2 == real_addr_block2)
1167 state.types2_(block2, 0) = type;
1171 state.match_equals(previous);
1180 if (heapinfo1->busy_block.ignore > 0
1181 && heapinfo2->busy_block.ignore == heapinfo1->busy_block.ignore)
1182 check_ignore = heapinfo1->busy_block.ignore;
1184 } else if ((heapinfo1->type > 0) && (heapinfo2->type > 0)) { /* Fragmented block */
1188 ((uintptr_t) (ADDR2UINT(area1) % (BLOCKSIZE))) >> heapinfo1->type;
1190 ((uintptr_t) (ADDR2UINT(area2) % (BLOCKSIZE))) >> heapinfo2->type;
1192 // Process address of the fragment:
1194 (void *) ((char *) real_addr_block1 +
1195 (frag1 << heapinfo1->type));
1197 (void *) ((char *) real_addr_block2 +
1198 (frag2 << heapinfo2->type));
1200 // Check the size of the fragments against the size of the type:
1201 if (type_size != -1) {
1202 if (heapinfo1->busy_frag.frag_size[frag1] == -1
1203 || heapinfo2->busy_frag.frag_size[frag2] == -1) {
1205 state.match_equals(previous);
1211 if (type_size != heapinfo1->busy_frag.frag_size[frag1]
1212 || type_size != heapinfo2->busy_frag.frag_size[frag2]) {
1214 state.match_equals(previous);
1221 // Check if the blocks are already matched together:
1222 if (state.equals_to1_(block1, frag1).valid
1223 && state.equals_to2_(block2, frag2).valid) {
1224 if (offset1==offset2 && state.fragmentsEqual(block1, frag1, block2, frag2)) {
1226 state.match_equals(previous);
1232 // Compare the size of both fragments:
1233 if (heapinfo1->busy_frag.frag_size[frag1] !=
1234 heapinfo2->busy_frag.frag_size[frag2]) {
1235 if (type_size == -1) {
1237 state.match_equals(previous);
1248 // Size of the fragment:
1249 size = heapinfo1->busy_frag.frag_size[frag1];
1251 // Remember (basic) type inference.
1252 // The current data structure only allows us to do this for the whole fragment.
1253 if (type != nullptr && area1 == real_addr_frag1)
1254 state.types1_(block1, frag1) = type;
1255 if (type != nullptr && area2 == real_addr_frag2)
1256 state.types2_(block2, frag2) = type;
1258 // The type of the variable is already known:
1263 // Type inference from the block type.
1264 else if (state.types1_(block1, frag1) != nullptr
1265 || state.types2_(block2, frag2) != nullptr) {
1267 offset1 = (char *) area1 - (char *) real_addr_frag1;
1268 offset2 = (char *) area2 - (char *) real_addr_frag2;
1270 if (state.types1_(block1, frag1) != nullptr
1271 && state.types2_(block2, frag2) != nullptr) {
1273 get_offset_type(real_addr_frag1, state.types1_(block1, frag1),
1274 offset1, size, snapshot1, process_index);
1276 get_offset_type(real_addr_frag2, state.types2_(block2, frag2),
1277 offset1, size, snapshot2, process_index);
1278 } else if (state.types1_(block1, frag1) != nullptr) {
1280 get_offset_type(real_addr_frag1, state.types1_(block1, frag1),
1281 offset1, size, snapshot1, process_index);
1283 get_offset_type(real_addr_frag2, state.types1_(block1, frag1),
1284 offset2, size, snapshot2, process_index);
1285 } else if (state.types2_(block2, frag2) != nullptr) {
1287 get_offset_type(real_addr_frag1, state.types2_(block2, frag2),
1288 offset1, size, snapshot1, process_index);
1290 get_offset_type(real_addr_frag2, state.types2_(block2, frag2),
1291 offset2, size, snapshot2, process_index);
1294 state.match_equals(previous);
1300 if (new_type1 != nullptr && new_type2 != nullptr && new_type1 != new_type2) {
1303 while (type->byte_size == 0 && type->subtype != nullptr)
1304 type = type->subtype;
1305 new_size1 = type->byte_size;
1308 while (type->byte_size == 0 && type->subtype != nullptr)
1309 type = type->subtype;
1310 new_size2 = type->byte_size;
1314 state.match_equals(previous);
1321 if (new_size1 > 0 && new_size1 == new_size2) {
1326 if (offset1 == 0 && offset2 == 0
1327 && !previous->insert(simgrid::mc::makeHeapLocationPair(
1328 block1, frag1, block2, frag2)).second) {
1330 state.match_equals(previous);
1338 state.match_equals(previous);
1344 if ((heapinfo1->busy_frag.ignore[frag1] > 0)
1345 && (heapinfo2->busy_frag.ignore[frag2] ==
1346 heapinfo1->busy_frag.ignore[frag1]))
1347 check_ignore = heapinfo1->busy_frag.ignore[frag1];
1356 /* Start comparison */
1359 compare_heap_area_with_type(state, process_index, area1, area2, snapshot1, snapshot2,
1360 previous, type, size, check_ignore,
1364 compare_heap_area_without_type(state, process_index, area1, area2, snapshot1, snapshot2,
1365 previous, size, check_ignore);
1367 if (res_compare == 1) {
1374 state.match_equals(previous);
1384 /************************** Snapshot comparison *******************************/
1385 /******************************************************************************/
1387 static int compare_areas_with_type(simgrid::mc::StateComparator& state,
1389 void* real_area1, simgrid::mc::Snapshot* snapshot1, mc_mem_region_t region1,
1390 void* real_area2, simgrid::mc::Snapshot* snapshot2, mc_mem_region_t region2,
1391 simgrid::mc::Type* type, int pointer_level)
1393 simgrid::mc::Process* process = &mc_model_checker->process();
1395 simgrid::mc::Type* subtype;
1396 simgrid::mc::Type* subsubtype;
1397 int elm_size, i, res;
1400 switch (type->type) {
1401 case DW_TAG_unspecified_type:
1404 case DW_TAG_base_type:
1405 case DW_TAG_enumeration_type:
1406 case DW_TAG_union_type:
1408 return MC_snapshot_region_memcmp(
1409 real_area1, region1, real_area2, region2,
1410 type->byte_size) != 0;
1412 case DW_TAG_typedef:
1413 case DW_TAG_volatile_type:
1414 case DW_TAG_const_type:
1416 type = type->subtype;
1418 case DW_TAG_array_type:
1419 subtype = type->subtype;
1420 switch (subtype->type) {
1421 case DW_TAG_unspecified_type:
1424 case DW_TAG_base_type:
1425 case DW_TAG_enumeration_type:
1426 case DW_TAG_pointer_type:
1427 case DW_TAG_reference_type:
1428 case DW_TAG_rvalue_reference_type:
1429 case DW_TAG_structure_type:
1430 case DW_TAG_class_type:
1431 case DW_TAG_union_type:
1432 if (subtype->full_type)
1433 subtype = subtype->full_type;
1434 elm_size = subtype->byte_size;
1436 case DW_TAG_const_type:
1437 case DW_TAG_typedef:
1438 case DW_TAG_volatile_type:
1439 subsubtype = subtype->subtype;
1440 if (subsubtype->full_type)
1441 subsubtype = subsubtype->full_type;
1442 elm_size = subsubtype->byte_size;
1448 for (i = 0; i < type->element_count; i++) {
1449 size_t off = i * elm_size;
1450 res = compare_areas_with_type(state, process_index,
1451 (char*) real_area1 + off, snapshot1, region1,
1452 (char*) real_area2 + off, snapshot2, region2,
1453 type->subtype, pointer_level);
1458 case DW_TAG_pointer_type:
1459 case DW_TAG_reference_type:
1460 case DW_TAG_rvalue_reference_type:
1462 void* addr_pointed1 = MC_region_read_pointer(region1, real_area1);
1463 void* addr_pointed2 = MC_region_read_pointer(region2, real_area2);
1465 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type)
1466 return (addr_pointed1 != addr_pointed2);
1467 if (addr_pointed1 == nullptr && addr_pointed2 == nullptr)
1469 if (addr_pointed1 == nullptr || addr_pointed2 == nullptr)
1471 if (!state.compared_pointers.insert(
1472 std::make_pair(addr_pointed1, addr_pointed2)).second)
1477 // Some cases are not handled here:
1478 // * the pointers lead to different areas (one to the heap, the other to the RW segment ...);
1479 // * a pointer leads to the read-only segment of the current object;
1480 // * a pointer lead to a different ELF object.
1482 if (addr_pointed1 > process->heap_address
1483 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)) {
1485 (addr_pointed2 > process->heap_address
1486 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)))
1488 // The pointers are both in the heap:
1489 return simgrid::mc::compare_heap_area(state,
1490 process_index, addr_pointed1, addr_pointed2, snapshot1,
1491 snapshot2, nullptr, type->subtype, pointer_level);
1494 // The pointers are both in the current object R/W segment:
1495 else if (region1->contain(simgrid::mc::remote(addr_pointed1))) {
1496 if (!region2->contain(simgrid::mc::remote(addr_pointed2)))
1499 return (addr_pointed1 != addr_pointed2);
1501 return compare_areas_with_type(state, process_index,
1502 addr_pointed1, snapshot1, region1,
1503 addr_pointed2, snapshot2, region2,
1504 type->subtype, pointer_level);
1507 // TODO, We do not handle very well the case where
1508 // it belongs to a different (non-heap) region from the current one.
1511 return (addr_pointed1 != addr_pointed2);
1515 case DW_TAG_structure_type:
1516 case DW_TAG_class_type:
1517 for(simgrid::mc::Member& member : type->members) {
1518 void *member1 = simgrid::dwarf::resolve_member(
1519 real_area1, type, &member, snapshot1, process_index);
1520 void *member2 = simgrid::dwarf::resolve_member(
1521 real_area2, type, &member, snapshot2, process_index);
1522 mc_mem_region_t subregion1 = mc_get_region_hinted(member1, snapshot1, process_index, region1);
1523 mc_mem_region_t subregion2 = mc_get_region_hinted(member2, snapshot2, process_index, region2);
1525 compare_areas_with_type(state, process_index,
1526 member1, snapshot1, subregion1,
1527 member2, snapshot2, subregion2,
1528 member.type, pointer_level);
1533 case DW_TAG_subroutine_type:
1537 XBT_VERB("Unknown case : %d", type->type);
1544 static int compare_global_variables(
1545 simgrid::mc::StateComparator& state,
1546 simgrid::mc::ObjectInformation* object_info,
1548 mc_mem_region_t r1, mc_mem_region_t r2,
1549 simgrid::mc::Snapshot* snapshot1, simgrid::mc::Snapshot* snapshot2)
1551 xbt_assert(r1 && r2, "Missing region.");
1554 if (r1->storage_type() == simgrid::mc::StorageType::Privatized) {
1555 xbt_assert(process_index >= 0);
1556 if (r2->storage_type() != simgrid::mc::StorageType::Privatized)
1559 size_t process_count = MC_smpi_process_count();
1560 xbt_assert(process_count == r1->privatized_data().size()
1561 && process_count == r2->privatized_data().size());
1563 // Compare the global variables separately for each simulates process:
1564 for (size_t process_index = 0; process_index < process_count; process_index++) {
1565 int is_diff = compare_global_variables(state,
1566 object_info, process_index,
1567 &r1->privatized_data()[process_index],
1568 &r2->privatized_data()[process_index],
1569 snapshot1, snapshot2);
1570 if (is_diff) return 1;
1575 xbt_assert(r1->storage_type() != simgrid::mc::StorageType::Privatized);
1577 xbt_assert(r2->storage_type() != simgrid::mc::StorageType::Privatized);
1579 std::vector<simgrid::mc::Variable>& variables = object_info->global_variables;
1581 for (simgrid::mc::Variable& current_var : variables) {
1583 // If the variable is not in this object, skip it:
1584 // We do not expect to find a pointer to something which is not reachable
1585 // by the global variables.
1586 if ((char *) current_var.address < (char *) object_info->start_rw
1587 || (char *) current_var.address > (char *) object_info->end_rw)
1590 simgrid::mc::Type* bvariable_type = current_var.type;
1591 int res = compare_areas_with_type(state, process_index,
1592 (char *) current_var.address, snapshot1, r1,
1593 (char *) current_var.address, snapshot2, r2,
1596 XBT_VERB("Global variable %s (%p) is different between snapshots",
1597 current_var.name.c_str(),
1598 (char *) current_var.address);
1608 static int compare_local_variables(simgrid::mc::StateComparator& state,
1610 simgrid::mc::Snapshot* snapshot1,
1611 simgrid::mc::Snapshot* snapshot2,
1612 mc_snapshot_stack_t stack1,
1613 mc_snapshot_stack_t stack2)
1615 if (stack1->local_variables.size() != stack2->local_variables.size()) {
1616 XBT_VERB("Different number of local variables");
1620 unsigned int cursor = 0;
1621 local_variable_t current_var1, current_var2;
1623 while (cursor < stack1->local_variables.size()) {
1624 current_var1 = &stack1->local_variables[cursor];
1625 current_var2 = &stack1->local_variables[cursor];
1626 if (current_var1->name != current_var2->name
1627 || current_var1->subprogram != current_var2->subprogram
1628 || current_var1->ip != current_var2->ip) {
1629 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1631 ("Different name of variable (%s - %s) "
1632 "or frame (%s - %s) or ip (%lu - %lu)",
1633 current_var1->name.c_str(),
1634 current_var2->name.c_str(),
1635 current_var1->subprogram->name.c_str(),
1636 current_var2->subprogram->name.c_str(),
1637 current_var1->ip, current_var2->ip);
1640 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1642 simgrid::mc::Type* subtype = current_var1->type;
1644 compare_areas_with_type(state, process_index,
1645 current_var1->address, snapshot1, mc_get_snapshot_region(current_var1->address, snapshot1, process_index),
1646 current_var2->address, snapshot2, mc_get_snapshot_region(current_var2->address, snapshot2, process_index),
1650 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
1652 ("Local variable %s (%p - %p) in frame %s "
1653 "is different between snapshots",
1654 current_var1->name.c_str(),
1655 current_var1->address,
1656 current_var2->address,
1657 current_var1->subprogram->name.c_str());
1668 static std::unique_ptr<simgrid::mc::StateComparator> state_comparator;
1670 int snapshot_compare(int num1, simgrid::mc::Snapshot* s1, int num2, simgrid::mc::Snapshot* s2)
1672 // TODO, make this a field of ModelChecker or something similar
1674 if (state_comparator == nullptr)
1675 state_comparator = std::unique_ptr<StateComparator>(new StateComparator());
1677 state_comparator->clear();
1679 simgrid::mc::Process* process = &mc_model_checker->process();
1684 int hash_result = 0;
1686 hash_result = (s1->hash != s2->hash);
1688 XBT_VERB("(%d - %d) Different hash : 0x%" PRIx64 "--0x%" PRIx64, num1,
1689 num2, s1->hash, s2->hash);
1694 XBT_VERB("(%d - %d) Same hash : 0x%" PRIx64, num1, num2, s1->hash);
1697 /* Compare enabled processes */
1698 if (s1->enabled_processes != s2->enabled_processes) {
1699 XBT_VERB("(%d - %d) Different enabled processes", num1, num2);
1703 unsigned long i = 0;
1704 size_t size_used1, size_used2;
1707 /* Compare size of stacks */
1708 while (i < s1->stacks.size()) {
1709 size_used1 = s1->stack_sizes[i];
1710 size_used2 = s2->stack_sizes[i];
1711 if (size_used1 != size_used2) {
1713 XBT_DEBUG("(%d - %d) Different size used in stacks : %zu - %zu", num1,
1714 num2, size_used1, size_used2);
1719 XBT_VERB("(%d - %d) Different size used in stacks : %zu - %zu", num1,
1720 num2, size_used1, size_used2);
1728 /* Init heap information used in heap comparison algorithm */
1729 xbt_mheap_t heap1 = (xbt_mheap_t)s1->read_bytes(
1730 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1731 remote(process->heap_address),
1732 simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1733 xbt_mheap_t heap2 = (xbt_mheap_t)s2->read_bytes(
1734 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
1735 remote(process->heap_address),
1736 simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
1737 res_init = state_comparator->initHeapInformation(
1738 heap1, heap2, &s1->to_ignore, &s2->to_ignore);
1740 if (res_init == -1) {
1742 XBT_DEBUG("(%d - %d) Different heap information", num1, num2);
1746 XBT_VERB("(%d - %d) Different heap information", num1, num2);
1753 /* Stacks comparison */
1754 unsigned cursor = 0;
1759 mc_snapshot_stack_t stack1, stack2;
1760 while (cursor < s1->stacks.size()) {
1761 stack1 = &s1->stacks[cursor];
1762 stack2 = &s2->stacks[cursor];
1764 if (stack1->process_index != stack2->process_index) {
1766 XBT_DEBUG("(%d - %d) Stacks with different process index (%i vs %i)", num1, num2,
1767 stack1->process_index, stack2->process_index);
1769 else diff_local = compare_local_variables(*state_comparator,
1770 stack1->process_index, s1, s2, stack1, stack2);
1771 if (diff_local > 0) {
1773 XBT_DEBUG("(%d - %d) Different local variables between stacks %d", num1,
1780 XBT_VERB("(%d - %d) Different local variables between stacks %d", num1,
1790 size_t regions_count = s1->snapshot_regions.size();
1791 // TODO, raise a difference instead?
1792 xbt_assert(regions_count == s2->snapshot_regions.size());
1794 for (size_t k = 0; k != regions_count; ++k) {
1795 mc_mem_region_t region1 = s1->snapshot_regions[k].get();
1796 mc_mem_region_t region2 = s2->snapshot_regions[k].get();
1799 if (region1->region_type() != simgrid::mc::RegionType::Data)
1802 xbt_assert(region1->region_type() == region2->region_type());
1803 xbt_assert(region1->object_info() == region2->object_info());
1804 xbt_assert(region1->object_info());
1806 std::string const& name = region1->object_info()->file_name;
1808 /* Compare global variables */
1810 compare_global_variables(*state_comparator,
1811 region1->object_info(), simgrid::mc::ProcessIndexDisabled,
1812 region1, region2, s1, s2);
1816 XBT_DEBUG("(%d - %d) Different global variables in %s",
1817 num1, num2, name.c_str());
1821 XBT_VERB("(%d - %d) Different global variables in %s",
1822 num1, num2, name.c_str());
1831 if (simgrid::mc::mmalloc_compare_heap(*state_comparator, s1, s2) > 0) {
1834 XBT_DEBUG("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
1839 XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
1847 if (errors || hash_result)
1848 XBT_VERB("(%d - %d) Difference found", num1, num2);
1850 XBT_VERB("(%d - %d) No difference found", num1, num2);
1853 #if defined(MC_DEBUG) && defined(MC_VERBOSE)
1855 // * false positive SHOULD be avoided.
1856 // * There MUST not be any false negative.
1858 XBT_VERB("(%d - %d) State equality hash test is %s %s", num1, num2,
1859 (hash_result != 0) == (errors != 0) ? "true" : "false",
1860 !hash_result ? "positive" : "negative");
1864 return errors > 0 || hash_result;