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. */
10 #include <unordered_set>
12 #include <xbt/sysdep.h>
14 #include "src/internal_config.h"
15 #include "src/mc/mc_forward.hpp"
16 #include "src/mc/mc_safety.h"
17 #include "src/mc/mc_private.h"
18 #include "src/mc/mc_smx.h"
19 #include "src/mc/mc_dwarf.hpp"
20 #include "src/mc/malloc.hpp"
21 #include "src/mc/Frame.hpp"
22 #include "src/mc/ObjectInformation.hpp"
23 #include "src/mc/Variable.hpp"
26 #include "src/smpi/private.h"
29 #include "xbt/mmalloc.h"
30 #include "src/xbt/mmalloc/mmprivate.h"
32 using simgrid::mc::remote;
34 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_compare, xbt,
35 "Logging specific to mc_compare in mc");
40 /** A hash which works with more stuff
42 * It can hash pairs: the standard hash currently doesn't include this.
44 template<class X> struct hash : public std::hash<X> {};
46 template<class X, class Y>
47 struct hash<std::pair<X,Y>> {
48 std::size_t operator()(std::pair<X,Y>const& x) const
52 return h1(x.first) ^ h2(x.second);
56 struct ComparisonState {
57 std::unordered_set<std::pair<void*, void*>, hash<std::pair<void*, void*>>> compared_pointers;
63 using simgrid::mc::ComparisonState;
65 /************************** Snapshot comparison *******************************/
66 /******************************************************************************/
68 static int compare_areas_with_type(ComparisonState& state,
70 void* real_area1, simgrid::mc::Snapshot* snapshot1, mc_mem_region_t region1,
71 void* real_area2, simgrid::mc::Snapshot* snapshot2, mc_mem_region_t region2,
72 simgrid::mc::Type* type, int pointer_level)
74 simgrid::mc::Process* process = &mc_model_checker->process();
76 simgrid::mc::Type* subtype;
77 simgrid::mc::Type* subsubtype;
82 case DW_TAG_unspecified_type:
85 case DW_TAG_base_type:
86 case DW_TAG_enumeration_type:
87 case DW_TAG_union_type:
89 return MC_snapshot_region_memcmp(
90 real_area1, region1, real_area2, region2,
91 type->byte_size) != 0;
94 case DW_TAG_volatile_type:
95 case DW_TAG_const_type:
99 case DW_TAG_array_type:
100 subtype = type->subtype;
101 switch (subtype->type) {
102 case DW_TAG_unspecified_type:
105 case DW_TAG_base_type:
106 case DW_TAG_enumeration_type:
107 case DW_TAG_pointer_type:
108 case DW_TAG_reference_type:
109 case DW_TAG_rvalue_reference_type:
110 case DW_TAG_structure_type:
111 case DW_TAG_class_type:
112 case DW_TAG_union_type:
113 if (subtype->full_type)
114 subtype = subtype->full_type;
115 elm_size = subtype->byte_size;
117 case DW_TAG_const_type:
119 case DW_TAG_volatile_type:
120 subsubtype = subtype->subtype;
121 if (subsubtype->full_type)
122 subsubtype = subsubtype->full_type;
123 elm_size = subsubtype->byte_size;
129 for (i = 0; i < type->element_count; i++) {
130 size_t off = i * elm_size;
131 res = compare_areas_with_type(state, process_index,
132 (char*) real_area1 + off, snapshot1, region1,
133 (char*) real_area2 + off, snapshot2, region2,
134 type->subtype, pointer_level);
139 case DW_TAG_pointer_type:
140 case DW_TAG_reference_type:
141 case DW_TAG_rvalue_reference_type:
143 void* addr_pointed1 = MC_region_read_pointer(region1, real_area1);
144 void* addr_pointed2 = MC_region_read_pointer(region2, real_area2);
146 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type)
147 return (addr_pointed1 != addr_pointed2);
148 if (addr_pointed1 == nullptr && addr_pointed2 == nullptr)
150 if (addr_pointed1 == nullptr || addr_pointed2 == nullptr)
152 if (!state.compared_pointers.insert(
153 std::make_pair(addr_pointed1, addr_pointed2)).second)
158 // Some cases are not handled here:
159 // * the pointers lead to different areas (one to the heap, the other to the RW segment ...);
160 // * a pointer leads to the read-only segment of the current object;
161 // * a pointer lead to a different ELF object.
163 if (addr_pointed1 > process->heap_address
164 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)) {
166 (addr_pointed2 > process->heap_address
167 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)))
169 // The pointers are both in the heap:
170 return simgrid::mc::compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
171 snapshot2, nullptr, type->subtype, pointer_level);
174 // The pointers are both in the current object R/W segment:
175 else if (region1->contain(simgrid::mc::remote(addr_pointed1))) {
176 if (!region2->contain(simgrid::mc::remote(addr_pointed2)))
179 return (addr_pointed1 != addr_pointed2);
181 return compare_areas_with_type(state, process_index,
182 addr_pointed1, snapshot1, region1,
183 addr_pointed2, snapshot2, region2,
184 type->subtype, pointer_level);
187 // TODO, We do not handle very well the case where
188 // it belongs to a different (non-heap) region from the current one.
191 return (addr_pointed1 != addr_pointed2);
195 case DW_TAG_structure_type:
196 case DW_TAG_class_type:
197 for(simgrid::mc::Member& member : type->members) {
198 void *member1 = simgrid::dwarf::resolve_member(
199 real_area1, type, &member, snapshot1, process_index);
200 void *member2 = simgrid::dwarf::resolve_member(
201 real_area2, type, &member, snapshot2, process_index);
202 mc_mem_region_t subregion1 = mc_get_region_hinted(member1, snapshot1, process_index, region1);
203 mc_mem_region_t subregion2 = mc_get_region_hinted(member2, snapshot2, process_index, region2);
205 compare_areas_with_type(state, process_index,
206 member1, snapshot1, subregion1,
207 member2, snapshot2, subregion2,
208 member.type, pointer_level);
213 case DW_TAG_subroutine_type:
217 XBT_VERB("Unknown case : %d", type->type);
224 static int compare_global_variables(simgrid::mc::ObjectInformation* object_info,
227 mc_mem_region_t r2, simgrid::mc::Snapshot* snapshot1,
228 simgrid::mc::Snapshot* snapshot2)
230 xbt_assert(r1 && r2, "Missing region.");
233 if (r1->storage_type() == simgrid::mc::StorageType::Privatized) {
234 xbt_assert(process_index >= 0);
235 if (r2->storage_type() != simgrid::mc::StorageType::Privatized)
238 size_t process_count = MC_smpi_process_count();
239 xbt_assert(process_count == r1->privatized_data().size()
240 && process_count == r2->privatized_data().size());
242 // Compare the global variables separately for each simulates process:
243 for (size_t process_index = 0; process_index < process_count; process_index++) {
244 int is_diff = compare_global_variables(object_info, process_index,
245 &r1->privatized_data()[process_index],
246 &r2->privatized_data()[process_index],
247 snapshot1, snapshot2);
248 if (is_diff) return 1;
253 xbt_assert(r1->storage_type() != simgrid::mc::StorageType::Privatized);
255 xbt_assert(r2->storage_type() != simgrid::mc::StorageType::Privatized);
257 ComparisonState state;
259 std::vector<simgrid::mc::Variable>& variables = object_info->global_variables;
261 for (simgrid::mc::Variable& current_var : variables) {
263 // If the variable is not in this object, skip it:
264 // We do not expect to find a pointer to something which is not reachable
265 // by the global variables.
266 if ((char *) current_var.address < (char *) object_info->start_rw
267 || (char *) current_var.address > (char *) object_info->end_rw)
270 simgrid::mc::Type* bvariable_type = current_var.type;
272 compare_areas_with_type(state, process_index,
273 (char *) current_var.address, snapshot1, r1,
274 (char *) current_var.address, snapshot2, r2,
277 XBT_VERB("Global variable %s (%p) is different between snapshots",
278 current_var.name.c_str(),
279 (char *) current_var.address);
289 static int compare_local_variables(int process_index,
290 simgrid::mc::Snapshot* snapshot1,
291 simgrid::mc::Snapshot* snapshot2,
292 mc_snapshot_stack_t stack1,
293 mc_snapshot_stack_t stack2)
295 ComparisonState state;
297 if (stack1->local_variables.size() != stack2->local_variables.size()) {
298 XBT_VERB("Different number of local variables");
302 unsigned int cursor = 0;
303 local_variable_t current_var1, current_var2;
305 while (cursor < stack1->local_variables.size()) {
306 current_var1 = &stack1->local_variables[cursor];
307 current_var2 = &stack1->local_variables[cursor];
308 if (current_var1->name != current_var2->name
309 || current_var1->subprogram != current_var2->subprogram
310 || current_var1->ip != current_var2->ip) {
311 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
313 ("Different name of variable (%s - %s) "
314 "or frame (%s - %s) or ip (%lu - %lu)",
315 current_var1->name.c_str(),
316 current_var2->name.c_str(),
317 current_var1->subprogram->name.c_str(),
318 current_var2->subprogram->name.c_str(),
319 current_var1->ip, current_var2->ip);
322 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
324 simgrid::mc::Type* subtype = current_var1->type;
326 compare_areas_with_type(state, process_index,
327 current_var1->address, snapshot1, mc_get_snapshot_region(current_var1->address, snapshot1, process_index),
328 current_var2->address, snapshot2, mc_get_snapshot_region(current_var2->address, snapshot2, process_index),
332 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
334 ("Local variable %s (%p - %p) in frame %s "
335 "is different between snapshots",
336 current_var1->name.c_str(),
337 current_var1->address,
338 current_var2->address,
339 current_var1->subprogram->name.c_str());
350 int snapshot_compare(int num1, simgrid::mc::Snapshot* s1, int num2, simgrid::mc::Snapshot* s2)
352 simgrid::mc::Process* process = &mc_model_checker->process();
359 hash_result = (s1->hash != s2->hash);
361 XBT_VERB("(%d - %d) Different hash : 0x%" PRIx64 "--0x%" PRIx64, num1,
362 num2, s1->hash, s2->hash);
367 XBT_VERB("(%d - %d) Same hash : 0x%" PRIx64, num1, num2, s1->hash);
370 /* Compare enabled processes */
371 if (s1->enabled_processes != s2->enabled_processes) {
372 XBT_VERB("(%d - %d) Different enabled processes", num1, num2);
377 size_t size_used1, size_used2;
380 /* Compare size of stacks */
381 while (i < s1->stacks.size()) {
382 size_used1 = s1->stack_sizes[i];
383 size_used2 = s2->stack_sizes[i];
384 if (size_used1 != size_used2) {
386 XBT_DEBUG("(%d - %d) Different size used in stacks : %zu - %zu", num1,
387 num2, size_used1, size_used2);
392 XBT_VERB("(%d - %d) Different size used in stacks : %zu - %zu", num1,
393 num2, size_used1, size_used2);
401 /* Init heap information used in heap comparison algorithm */
402 xbt_mheap_t heap1 = (xbt_mheap_t)s1->read_bytes(
403 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
404 remote(process->heap_address),
405 simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
406 xbt_mheap_t heap2 = (xbt_mheap_t)s2->read_bytes(
407 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
408 remote(process->heap_address),
409 simgrid::mc::ProcessIndexMissing, simgrid::mc::ReadOptions::lazy());
410 res_init = simgrid::mc::init_heap_information(heap1, heap2, &s1->to_ignore, &s2->to_ignore);
411 if (res_init == -1) {
413 XBT_DEBUG("(%d - %d) Different heap information", num1, num2);
417 XBT_VERB("(%d - %d) Different heap information", num1, num2);
424 /* Stacks comparison */
430 mc_snapshot_stack_t stack1, stack2;
431 while (cursor < s1->stacks.size()) {
432 stack1 = &s1->stacks[cursor];
433 stack2 = &s2->stacks[cursor];
435 if (stack1->process_index != stack2->process_index) {
437 XBT_DEBUG("(%d - %d) Stacks with different process index (%i vs %i)", num1, num2,
438 stack1->process_index, stack2->process_index);
441 compare_local_variables(stack1->process_index, s1, s2, stack1, stack2);
442 if (diff_local > 0) {
444 XBT_DEBUG("(%d - %d) Different local variables between stacks %d", num1,
451 XBT_VERB("(%d - %d) Different local variables between stacks %d", num1,
455 simgrid::mc::reset_heap_information();
463 size_t regions_count = s1->snapshot_regions.size();
464 // TODO, raise a difference instead?
465 xbt_assert(regions_count == s2->snapshot_regions.size());
467 for (size_t k = 0; k != regions_count; ++k) {
468 mc_mem_region_t region1 = s1->snapshot_regions[k].get();
469 mc_mem_region_t region2 = s2->snapshot_regions[k].get();
472 if (region1->region_type() != simgrid::mc::RegionType::Data)
475 xbt_assert(region1->region_type() == region2->region_type());
476 xbt_assert(region1->object_info() == region2->object_info());
477 xbt_assert(region1->object_info());
479 std::string const& name = region1->object_info()->file_name;
481 /* Compare global variables */
483 compare_global_variables(region1->object_info(),
484 simgrid::mc::ProcessIndexDisabled,
490 XBT_DEBUG("(%d - %d) Different global variables in %s",
491 num1, num2, name.c_str());
495 XBT_VERB("(%d - %d) Different global variables in %s",
496 num1, num2, name.c_str());
505 if (simgrid::mc::mmalloc_compare_heap(s1, s2) > 0) {
508 XBT_DEBUG("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
513 XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
520 simgrid::mc::reset_heap_information();
523 if (errors || hash_result)
524 XBT_VERB("(%d - %d) Difference found", num1, num2);
526 XBT_VERB("(%d - %d) No difference found", num1, num2);
529 #if defined(MC_DEBUG) && defined(MC_VERBOSE)
531 // * false positive SHOULD be avoided.
532 // * There MUST not be any false negative.
534 XBT_VERB("(%d - %d) State equality hash test is %s %s", num1, num2,
535 (hash_result != 0) == (errors != 0) ? "true" : "false",
536 !hash_result ? "positive" : "negative");
540 return errors > 0 || hash_result;