1 /* Copyright (c) 2012-2015. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #define __STDC_FORMAT_MACROS
11 #include <unordered_set>
13 #include <xbt/sysdep.h>
15 #include "src/internal_config.h"
16 #include "src/mc/mc_forward.hpp"
17 #include "src/mc/mc_safety.h"
18 #include "src/mc/LivenessChecker.hpp"
19 #include "src/mc/mc_private.h"
20 #include "src/mc/mc_smx.h"
21 #include "src/mc/mc_dwarf.hpp"
22 #include "src/mc/malloc.hpp"
23 #include "src/mc/Frame.hpp"
24 #include "src/mc/ObjectInformation.hpp"
25 #include "src/mc/Variable.hpp"
28 #include "src/smpi/private.h"
31 #include "xbt/mmalloc.h"
32 #include "src/xbt/mmalloc/mmprivate.h"
34 using simgrid::mc::remote;
36 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_compare, xbt,
37 "Logging specific to mc_compare in mc");
42 /** A hash which works with more stuff
44 * It can hash pairs: the standard hash currently doesn't include this.
46 template<class X> struct hash : public std::hash<X> {};
48 template<class X, class Y>
49 struct hash<std::pair<X,Y>> {
50 std::size_t operator()(std::pair<X,Y>const& x) const
54 return h1(x.first) ^ h2(x.second);
58 struct ComparisonState {
59 std::unordered_set<std::pair<void*, void*>, hash<std::pair<void*, void*>>> compared_pointers;
65 using simgrid::mc::ComparisonState;
67 /************************** Snapshot comparison *******************************/
68 /******************************************************************************/
70 static int compare_areas_with_type(ComparisonState& state,
72 void* real_area1, simgrid::mc::Snapshot* snapshot1, mc_mem_region_t region1,
73 void* real_area2, simgrid::mc::Snapshot* snapshot2, mc_mem_region_t region2,
74 simgrid::mc::Type* type, int pointer_level)
76 simgrid::mc::Process* process = &mc_model_checker->process();
78 simgrid::mc::Type* subtype;
79 simgrid::mc::Type* subsubtype;
84 case DW_TAG_unspecified_type:
87 case DW_TAG_base_type:
88 case DW_TAG_enumeration_type:
89 case DW_TAG_union_type:
91 return MC_snapshot_region_memcmp(
92 real_area1, region1, real_area2, region2,
93 type->byte_size) != 0;
96 case DW_TAG_volatile_type:
97 case DW_TAG_const_type:
101 case DW_TAG_array_type:
102 subtype = type->subtype;
103 switch (subtype->type) {
104 case DW_TAG_unspecified_type:
107 case DW_TAG_base_type:
108 case DW_TAG_enumeration_type:
109 case DW_TAG_pointer_type:
110 case DW_TAG_reference_type:
111 case DW_TAG_rvalue_reference_type:
112 case DW_TAG_structure_type:
113 case DW_TAG_class_type:
114 case DW_TAG_union_type:
115 if (subtype->full_type)
116 subtype = subtype->full_type;
117 elm_size = subtype->byte_size;
119 case DW_TAG_const_type:
121 case DW_TAG_volatile_type:
122 subsubtype = subtype->subtype;
123 if (subsubtype->full_type)
124 subsubtype = subsubtype->full_type;
125 elm_size = subsubtype->byte_size;
131 for (i = 0; i < type->element_count; i++) {
132 size_t off = i * elm_size;
133 res = compare_areas_with_type(state, process_index,
134 (char*) real_area1 + off, snapshot1, region1,
135 (char*) real_area2 + off, snapshot2, region2,
136 type->subtype, pointer_level);
141 case DW_TAG_pointer_type:
142 case DW_TAG_reference_type:
143 case DW_TAG_rvalue_reference_type:
145 void* addr_pointed1 = MC_region_read_pointer(region1, real_area1);
146 void* addr_pointed2 = MC_region_read_pointer(region2, real_area2);
148 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type)
149 return (addr_pointed1 != addr_pointed2);
150 if (addr_pointed1 == nullptr && addr_pointed2 == NULL)
152 if (addr_pointed1 == nullptr || addr_pointed2 == NULL)
154 if (!state.compared_pointers.insert(
155 std::make_pair(addr_pointed1, addr_pointed2)).second)
160 // Some cases are not handled here:
161 // * the pointers lead to different areas (one to the heap, the other to the RW segment ...);
162 // * a pointer leads to the read-only segment of the current object;
163 // * a pointer lead to a different ELF object.
165 if (addr_pointed1 > process->heap_address
166 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)) {
168 (addr_pointed2 > process->heap_address
169 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)))
171 // The pointers are both in the heap:
172 return simgrid::mc::compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
173 snapshot2, nullptr, type->subtype, pointer_level);
176 // The pointers are both in the current object R/W segment:
177 else if (region1->contain(simgrid::mc::remote(addr_pointed1))) {
178 if (!region2->contain(simgrid::mc::remote(addr_pointed2)))
181 return (addr_pointed1 != addr_pointed2);
183 return compare_areas_with_type(state, process_index,
184 addr_pointed1, snapshot1, region1,
185 addr_pointed2, snapshot2, region2,
186 type->subtype, pointer_level);
189 // TODO, We do not handle very well the case where
190 // it belongs to a different (non-heap) region from the current one.
193 return (addr_pointed1 != addr_pointed2);
197 case DW_TAG_structure_type:
198 case DW_TAG_class_type:
199 for(simgrid::mc::Member& member : type->members) {
200 void *member1 = simgrid::dwarf::resolve_member(
201 real_area1, type, &member, snapshot1, process_index);
202 void *member2 = simgrid::dwarf::resolve_member(
203 real_area2, type, &member, snapshot2, process_index);
204 mc_mem_region_t subregion1 = mc_get_region_hinted(member1, snapshot1, process_index, region1);
205 mc_mem_region_t subregion2 = mc_get_region_hinted(member2, snapshot2, process_index, region2);
207 compare_areas_with_type(state, process_index,
208 member1, snapshot1, subregion1,
209 member2, snapshot2, subregion2,
210 member.type, pointer_level);
215 case DW_TAG_subroutine_type:
219 XBT_VERB("Unknown case : %d", type->type);
226 static int compare_global_variables(simgrid::mc::ObjectInformation* object_info,
229 mc_mem_region_t r2, simgrid::mc::Snapshot* snapshot1,
230 simgrid::mc::Snapshot* snapshot2)
232 xbt_assert(r1 && r2, "Missing region.");
235 if (r1->storage_type() == simgrid::mc::StorageType::Privatized) {
236 xbt_assert(process_index >= 0);
237 if (r2->storage_type() != simgrid::mc::StorageType::Privatized)
240 size_t process_count = MC_smpi_process_count();
241 xbt_assert(process_count == r1->privatized_data().size()
242 && process_count == r2->privatized_data().size());
244 // Compare the global variables separately for each simulates process:
245 for (size_t process_index = 0; process_index < process_count; process_index++) {
246 int is_diff = compare_global_variables(object_info, process_index,
247 &r1->privatized_data()[process_index],
248 &r2->privatized_data()[process_index],
249 snapshot1, snapshot2);
250 if (is_diff) return 1;
255 xbt_assert(r1->storage_type() != simgrid::mc::StorageType::Privatized);
257 xbt_assert(r2->storage_type() != simgrid::mc::StorageType::Privatized);
259 ComparisonState state;
261 std::vector<simgrid::mc::Variable>& variables = object_info->global_variables;
263 for (simgrid::mc::Variable& current_var : variables) {
265 // If the variable is not in this object, skip it:
266 // We do not expect to find a pointer to something which is not reachable
267 // by the global variables.
268 if ((char *) current_var.address < (char *) object_info->start_rw
269 || (char *) current_var.address > (char *) object_info->end_rw)
272 simgrid::mc::Type* bvariable_type = current_var.type;
274 compare_areas_with_type(state, process_index,
275 (char *) current_var.address, snapshot1, r1,
276 (char *) current_var.address, snapshot2, r2,
279 XBT_VERB("Global variable %s (%p) is different between snapshots",
280 current_var.name.c_str(),
281 (char *) current_var.address);
291 static int compare_local_variables(int process_index,
292 simgrid::mc::Snapshot* snapshot1,
293 simgrid::mc::Snapshot* snapshot2,
294 mc_snapshot_stack_t stack1,
295 mc_snapshot_stack_t stack2)
297 ComparisonState state;
299 if (stack1->local_variables.size() != stack2->local_variables.size()) {
300 XBT_VERB("Different number of local variables");
304 unsigned int cursor = 0;
305 local_variable_t current_var1, current_var2;
307 while (cursor < stack1->local_variables.size()) {
308 current_var1 = &stack1->local_variables[cursor];
309 current_var2 = &stack1->local_variables[cursor];
310 if (current_var1->name != current_var2->name
311 || current_var1->subprogram != current_var2->subprogram
312 || current_var1->ip != current_var2->ip) {
313 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
315 ("Different name of variable (%s - %s) "
316 "or frame (%s - %s) or ip (%lu - %lu)",
317 current_var1->name.c_str(),
318 current_var2->name.c_str(),
319 current_var1->subprogram->name.c_str(),
320 current_var2->subprogram->name.c_str(),
321 current_var1->ip, current_var2->ip);
324 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
326 simgrid::mc::Type* subtype = current_var1->type;
328 compare_areas_with_type(state, process_index,
329 current_var1->address, snapshot1, mc_get_snapshot_region(current_var1->address, snapshot1, process_index),
330 current_var2->address, snapshot2, mc_get_snapshot_region(current_var2->address, snapshot2, process_index),
334 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
336 ("Local variable %s (%p - %p) in frame %s "
337 "is different between snapshots",
338 current_var1->name.c_str(),
339 current_var1->address,
340 current_var2->address,
341 current_var1->subprogram->name.c_str());
352 int snapshot_compare(int num1, simgrid::mc::Snapshot* s1, int num2, simgrid::mc::Snapshot* s2)
354 simgrid::mc::Process* process = &mc_model_checker->process();
361 hash_result = (s1->hash != s2->hash);
363 XBT_VERB("(%d - %d) Different hash : 0x%" PRIx64 "--0x%" PRIx64, num1,
364 num2, s1->hash, s2->hash);
369 XBT_VERB("(%d - %d) Same hash : 0x%" PRIx64, num1, num2, s1->hash);
372 /* Compare enabled processes */
373 if (s1->enabled_processes != s2->enabled_processes) {
374 XBT_VERB("(%d - %d) Different enabled processes", num1, num2);
379 size_t size_used1, size_used2;
382 /* Compare size of stacks */
383 while (i < s1->stacks.size()) {
384 size_used1 = s1->stack_sizes[i];
385 size_used2 = s2->stack_sizes[i];
386 if (size_used1 != size_used2) {
388 XBT_DEBUG("(%d - %d) Different size used in stacks : %zu - %zu", num1,
389 num2, size_used1, size_used2);
394 XBT_VERB("(%d - %d) Different size used in stacks : %zu - %zu", num1,
395 num2, size_used1, size_used2);
403 /* Init heap information used in heap comparison algorithm */
404 xbt_mheap_t heap1 = (xbt_mheap_t)s1->read_bytes(
405 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
406 remote(process->heap_address),
407 simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
408 xbt_mheap_t heap2 = (xbt_mheap_t)s2->read_bytes(
409 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
410 remote(process->heap_address),
411 simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
412 res_init = simgrid::mc::init_heap_information(heap1, heap2, &s1->to_ignore, &s2->to_ignore);
413 if (res_init == -1) {
415 XBT_DEBUG("(%d - %d) Different heap information", num1, num2);
419 XBT_VERB("(%d - %d) Different heap information", num1, num2);
426 /* Stacks comparison */
432 mc_snapshot_stack_t stack1, stack2;
433 while (cursor < s1->stacks.size()) {
434 stack1 = &s1->stacks[cursor];
435 stack2 = &s2->stacks[cursor];
437 if (stack1->process_index != stack2->process_index) {
439 XBT_DEBUG("(%d - %d) Stacks with different process index (%i vs %i)", num1, num2,
440 stack1->process_index, stack2->process_index);
443 compare_local_variables(stack1->process_index, s1, s2, stack1, stack2);
444 if (diff_local > 0) {
446 XBT_DEBUG("(%d - %d) Different local variables between stacks %d", num1,
453 XBT_VERB("(%d - %d) Different local variables between stacks %d", num1,
457 simgrid::mc::reset_heap_information();
465 size_t regions_count = s1->snapshot_regions.size();
466 // TODO, raise a difference instead?
467 xbt_assert(regions_count == s2->snapshot_regions.size());
469 for (size_t k = 0; k != regions_count; ++k) {
470 mc_mem_region_t region1 = s1->snapshot_regions[k].get();
471 mc_mem_region_t region2 = s2->snapshot_regions[k].get();
474 if (region1->region_type() != simgrid::mc::RegionType::Data)
477 xbt_assert(region1->region_type() == region2->region_type());
478 xbt_assert(region1->object_info() == region2->object_info());
479 xbt_assert(region1->object_info());
481 std::string const& name = region1->object_info()->file_name;
483 /* Compare global variables */
485 compare_global_variables(region1->object_info(),
486 simgrid::mc::ProcessIndexDisabled,
492 XBT_DEBUG("(%d - %d) Different global variables in %s",
493 num1, num2, name.c_str());
497 XBT_VERB("(%d - %d) Different global variables in %s",
498 num1, num2, name.c_str());
507 if (simgrid::mc::mmalloc_compare_heap(s1, s2) > 0) {
510 XBT_DEBUG("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
515 XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
522 simgrid::mc::reset_heap_information();
525 if (errors || hash_result)
526 XBT_VERB("(%d - %d) Difference found", num1, num2);
528 XBT_VERB("(%d - %d) No difference found", num1, num2);
531 #if defined(MC_DEBUG) && defined(MC_VERBOSE)
533 // * false positive SHOULD be avoided.
534 // * There MUST not be any false negative.
536 XBT_VERB("(%d - %d) State equality hash test is %s %s", num1, num2,
537 (hash_result != 0) == (errors != 0) ? "true" : "false",
538 !hash_result ? "positive" : "negative");
542 return errors > 0 || hash_result;
545 int snapshot_compare(mc_state_t state1, mc_state_t state2)
547 simgrid::mc::Snapshot* s1 = state1->system_state;
548 simgrid::mc::Snapshot* s2 = state2->system_state;
549 int num1 = state1->num;
550 int num2 = state2->num;
551 return snapshot_compare(num1, s1, num2, s2);
554 int snapshot_compare(simgrid::mc::VisitedState* state1, simgrid::mc::VisitedState* state2)
556 simgrid::mc::Snapshot* s1 = state1->system_state;
557 simgrid::mc::Snapshot* s2 = state2->system_state;
558 int num1 = state1->num;
559 int num2 = state2->num;
560 return snapshot_compare(num1, s1, num2, s2);