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
9 #include <boost/unordered_set.hpp>
11 #include <xbt/sysdep.h>
13 #include "internal_config.h"
14 #include "mc_object_info.h"
15 #include "mc_safety.h"
16 #include "mc_liveness.h"
17 #include "mc_private.h"
20 #include "mc/Frame.hpp"
21 #include "mc/ObjectInformation.hpp"
22 #include "mc/Variable.hpp"
25 #include "smpi/private.h"
28 #include "xbt/mmalloc.h"
29 #include "xbt/mmalloc/mmprivate.h"
31 #include <xbt/probes.h>
33 using simgrid::mc::remote;
35 typedef struct s_pointers_pair {
38 bool operator==(s_pointers_pair const& x) const {
39 return this->p1 == x.p1 && this->p2 == x.p2;
41 bool operator<(s_pointers_pair const& x) const {
42 return this->p1 < x.p1 || (this->p1 == x.p1 && this->p2 < x.p2);
44 } s_pointers_pair_t, *pointers_pair_t;
48 struct hash<s_pointers_pair> {
49 typedef uintptr_t result_type;
50 result_type operator()(s_pointers_pair const& x) const {
51 return (result_type) x.p1 ^
52 ((result_type) x.p2 << 8 | (result_type) x.p2 >> (8*sizeof(uintptr_t) - 8));
57 struct mc_compare_state {
58 boost::unordered_set<s_pointers_pair> compared_pointers;
63 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_compare, xbt,
64 "Logging specific to mc_compare in mc");
66 /************************** Free functions ****************************/
67 /********************************************************************/
69 static void stack_region_free(stack_region_t s)
76 static void stack_region_free_voidp(void *s)
78 stack_region_free((stack_region_t) * (void **) s);
81 static void pointers_pair_free(pointers_pair_t p)
86 static void pointers_pair_free_voidp(void *p)
88 pointers_pair_free((pointers_pair_t) * (void **) p);
91 /************************** Snapshot comparison *******************************/
92 /******************************************************************************/
94 /** \brief Try to add a pair a compared pointers to the set of compared pointers
96 * \result !=0 if the pointers were added (they were not in the set),
97 * 0 otherwise (they were already in the set)
99 static int add_compared_pointers(mc_compare_state& state, void *p1, void *p2)
101 s_pointers_pair_t new_pair;
104 return state.compared_pointers.insert(new_pair).second ? 1 : 0;
107 static int compare_areas_with_type(struct mc_compare_state& state,
109 void* real_area1, mc_snapshot_t snapshot1, mc_mem_region_t region1,
110 void* real_area2, mc_snapshot_t snapshot2, mc_mem_region_t region2,
111 simgrid::mc::Type* type, int pointer_level)
113 simgrid::mc::Process* process = &mc_model_checker->process();
115 simgrid::mc::Type* subtype;
116 simgrid::mc::Type* subsubtype;
117 int elm_size, i, res;
120 switch (type->type) {
121 case DW_TAG_unspecified_type:
124 case DW_TAG_base_type:
125 case DW_TAG_enumeration_type:
126 case DW_TAG_union_type:
128 return MC_snapshot_region_memcmp(
129 real_area1, region1, real_area2, region2,
130 type->byte_size) != 0;
133 case DW_TAG_volatile_type:
134 case DW_TAG_const_type:
136 type = type->subtype;
138 case DW_TAG_array_type:
139 subtype = type->subtype;
140 switch (subtype->type) {
141 case DW_TAG_unspecified_type:
144 case DW_TAG_base_type:
145 case DW_TAG_enumeration_type:
146 case DW_TAG_pointer_type:
147 case DW_TAG_reference_type:
148 case DW_TAG_rvalue_reference_type:
149 case DW_TAG_structure_type:
150 case DW_TAG_class_type:
151 case DW_TAG_union_type:
152 if (subtype->full_type)
153 subtype = subtype->full_type;
154 elm_size = subtype->byte_size;
156 case DW_TAG_const_type:
158 case DW_TAG_volatile_type:
159 subsubtype = subtype->subtype;
160 if (subsubtype->full_type)
161 subsubtype = subsubtype->full_type;
162 elm_size = subsubtype->byte_size;
168 for (i = 0; i < type->element_count; i++) {
169 size_t off = i * elm_size;
170 res = compare_areas_with_type(state, process_index,
171 (char*) real_area1 + off, snapshot1, region1,
172 (char*) real_area2 + off, snapshot2, region2,
173 type->subtype, pointer_level);
178 case DW_TAG_pointer_type:
179 case DW_TAG_reference_type:
180 case DW_TAG_rvalue_reference_type:
182 void* addr_pointed1 = MC_region_read_pointer(region1, real_area1);
183 void* addr_pointed2 = MC_region_read_pointer(region2, real_area2);
185 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
186 return (addr_pointed1 != addr_pointed2);
189 if (addr_pointed1 == NULL && addr_pointed2 == NULL)
191 if (addr_pointed1 == NULL || addr_pointed2 == NULL)
193 if (!add_compared_pointers(state, addr_pointed1, addr_pointed2))
198 // Some cases are not handled here:
199 // * the pointers lead to different areas (one to the heap, the other to the RW segment ...);
200 // * a pointer leads to the read-only segment of the current object;
201 // * a pointer lead to a different ELF object.
203 if (addr_pointed1 > process->heap_address
204 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)) {
206 (addr_pointed2 > process->heap_address
207 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)))
209 // The pointers are both in the heap:
210 return compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
211 snapshot2, NULL, type->subtype, pointer_level);
214 // The pointers are both in the current object R/W segment:
215 else if (region1->contain(simgrid::mc::remote(addr_pointed1))) {
216 if (!region2->contain(simgrid::mc::remote(addr_pointed2)))
219 return (addr_pointed1 != addr_pointed2);
221 return compare_areas_with_type(state, process_index,
222 addr_pointed1, snapshot1, region1,
223 addr_pointed2, snapshot2, region2,
224 type->subtype, pointer_level);
228 // TODO, We do not handle very well the case where
229 // it belongs to a different (non-heap) region from the current one.
232 return (addr_pointed1 != addr_pointed2);
237 case DW_TAG_structure_type:
238 case DW_TAG_class_type:
239 for(simgrid::mc::Type& member : type->members) {
241 mc_member_resolve(real_area1, type, &member, snapshot1, process_index);
243 mc_member_resolve(real_area2, type, &member, snapshot2, process_index);
244 mc_mem_region_t subregion1 = mc_get_region_hinted(member1, snapshot1, process_index, region1);
245 mc_mem_region_t subregion2 = mc_get_region_hinted(member2, snapshot2, process_index, region2);
247 compare_areas_with_type(state, process_index,
248 member1, snapshot1, subregion1,
249 member2, snapshot2, subregion2,
250 member.subtype, pointer_level);
255 case DW_TAG_subroutine_type:
259 XBT_VERB("Unknown case : %d", type->type);
266 static int compare_global_variables(simgrid::mc::ObjectInformation* object_info,
269 mc_mem_region_t r2, mc_snapshot_t snapshot1,
270 mc_snapshot_t snapshot2)
272 xbt_assert(r1 && r2, "Missing region.");
275 if (r1->storage_type() == simgrid::mc::StorageType::Privatized) {
276 xbt_assert(process_index >= 0);
277 if (r2->storage_type() != simgrid::mc::StorageType::Privatized) {
281 size_t process_count = MC_smpi_process_count();
282 xbt_assert(process_count == r1->privatized_data().size()
283 && process_count == r2->privatized_data().size());
285 // Compare the global variables separately for each simulates process:
286 for (size_t process_index = 0; process_index < process_count; process_index++) {
287 int is_diff = compare_global_variables(object_info, process_index,
288 &r1->privatized_data()[process_index],
289 &r2->privatized_data()[process_index],
290 snapshot1, snapshot2);
291 if (is_diff) return 1;
296 xbt_assert(r1->storage_type() != simgrid::mc::StorageType::Privatized);
298 xbt_assert(r2->storage_type() != simgrid::mc::StorageType::Privatized);
300 struct mc_compare_state state;
302 std::vector<simgrid::mc::Variable>& variables = object_info->global_variables;
304 for (simgrid::mc::Variable& current_var : variables) {
306 // If the variable is not in this object, skip it:
307 // We do not expect to find a pointer to something which is not reachable
308 // by the global variables.
309 if ((char *) current_var.address < (char *) object_info->start_rw
310 || (char *) current_var.address > (char *) object_info->end_rw)
313 simgrid::mc::Type* bvariable_type = current_var.type;
315 compare_areas_with_type(state, process_index,
316 (char *) current_var.address, snapshot1, r1,
317 (char *) current_var.address, snapshot2, r2,
320 XBT_TRACE3(mc, global_diff, -1, -1, current_var->name);
321 XBT_VERB("Global variable %s (%p) is different between snapshots",
322 current_var.name.c_str(),
323 (char *) current_var.address);
333 static int compare_local_variables(int process_index,
334 mc_snapshot_t snapshot1,
335 mc_snapshot_t snapshot2,
336 mc_snapshot_stack_t stack1,
337 mc_snapshot_stack_t stack2)
339 struct mc_compare_state state;
341 if (stack1->local_variables.size() != stack2->local_variables.size()) {
342 XBT_VERB("Different number of local variables");
345 unsigned int cursor = 0;
346 local_variable_t current_var1, current_var2;
348 while (cursor < stack1->local_variables.size()) {
349 current_var1 = &stack1->local_variables[cursor];
350 current_var2 = &stack1->local_variables[cursor];
351 if (current_var1->name != current_var2->name
352 || current_var1->subprogram != current_var1->subprogram
353 || current_var1->ip != current_var2->ip) {
354 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
356 ("Different name of variable (%s - %s) "
357 "or frame (%s - %s) or ip (%lu - %lu)",
358 current_var1->name.c_str(),
359 current_var2->name.c_str(),
360 current_var1->subprogram->name.c_str(),
361 current_var2->subprogram->name.c_str(),
362 current_var1->ip, current_var2->ip);
365 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
367 simgrid::mc::Type* subtype = current_var1->type;
369 compare_areas_with_type(state, process_index,
370 current_var1->address, snapshot1, mc_get_snapshot_region(current_var1->address, snapshot1, process_index),
371 current_var2->address, snapshot2, mc_get_snapshot_region(current_var2->address, snapshot2, process_index),
375 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
376 XBT_TRACE3(mc, local_diff, -1, -1, current_var1->name);
378 ("Local variable %s (%p - %p) in frame %s "
379 "is different between snapshots",
380 current_var1->name.c_str(),
381 current_var1->address,
382 current_var2->address,
383 current_var1->subprogram->name.c_str());
392 int snapshot_compare(void *state1, void *state2)
394 simgrid::mc::Process* process = &mc_model_checker->process();
396 mc_snapshot_t s1, s2;
399 if (_sg_mc_liveness) { /* Liveness MC */
400 s1 = ((mc_visited_pair_t) state1)->graph_state->system_state;
401 s2 = ((mc_visited_pair_t) state2)->graph_state->system_state;
402 num1 = ((mc_visited_pair_t) state1)->num;
403 num2 = ((mc_visited_pair_t) state2)->num;
404 }else if (_sg_mc_termination) { /* Non-progressive cycle MC */
405 s1 = ((mc_state_t) state1)->system_state;
406 s2 = ((mc_state_t) state2)->system_state;
407 num1 = ((mc_state_t) state1)->num;
408 num2 = ((mc_state_t) state2)->num;
409 } else { /* Safety or comm determinism MC */
410 s1 = ((mc_visited_state_t) state1)->system_state;
411 s2 = ((mc_visited_state_t) state2)->system_state;
412 num1 = ((mc_visited_state_t) state1)->num;
413 num2 = ((mc_visited_state_t) state2)->num;
419 xbt_os_timer_t global_timer = xbt_os_timer_new();
420 xbt_os_timer_t timer = xbt_os_timer_new();
422 xbt_os_walltimer_start(global_timer);
425 xbt_os_walltimer_start(timer);
430 hash_result = (s1->hash != s2->hash);
432 XBT_TRACE2(mc, hash_diff, num1, num2);
433 XBT_VERB("(%d - %d) Different hash : 0x%" PRIx64 "--0x%" PRIx64, num1,
434 num2, s1->hash, s2->hash);
439 XBT_VERB("(%d - %d) Same hash : 0x%" PRIx64, num1, num2, s1->hash);
443 /* Compare enabled processes */
444 if (s1->enabled_processes != s2->enabled_processes) {
445 //XBT_TRACE3(mc, state_diff, num1, num2, "Different enabled processes");
446 XBT_VERB("(%d - %d) Different enabled processes", num1, num2);
451 size_t size_used1, size_used2;
454 /* Compare size of stacks */
455 while (i < s1->stacks.size()) {
456 size_used1 = s1->stack_sizes[i];
457 size_used2 = s2->stack_sizes[i];
458 if (size_used1 != size_used2) {
461 xbt_os_walltimer_stop(timer);
462 mc_comp_times->stacks_sizes_comparison_time =
463 xbt_os_timer_elapsed(timer);
465 XBT_DEBUG("(%d - %d) Different size used in stacks : %zu - %zu", num1,
466 num2, size_used1, size_used2);
471 XBT_VERB("(%d - %d) Different size used in stacks : %zu - %zu", num1,
472 num2, size_used1, size_used2);
474 XBT_TRACE3(mc, state_diff, num1, num2, "Different stack size");
476 xbt_os_walltimer_stop(timer);
477 xbt_os_timer_free(timer);
478 xbt_os_walltimer_stop(global_timer);
479 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
480 xbt_os_timer_free(global_timer);
490 xbt_os_walltimer_stop(timer);
491 xbt_os_walltimer_start(timer);
494 /* Init heap information used in heap comparison algorithm */
495 xbt_mheap_t heap1 = (xbt_mheap_t)s1->read_bytes(
496 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
497 remote(process->heap_address),
498 simgrid::mc::ProcessIndexMissing, simgrid::mc::AddressSpace::Lazy);
499 xbt_mheap_t heap2 = (xbt_mheap_t)s2->read_bytes(
500 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
501 remote(process->heap_address),
502 simgrid::mc::ProcessIndexMissing, simgrid::mc::AddressSpace::Lazy);
503 res_init = init_heap_information(heap1, heap2, &s1->to_ignore, &s2->to_ignore);
504 if (res_init == -1) {
506 XBT_DEBUG("(%d - %d) Different heap information", num1, num2);
510 XBT_TRACE3(mc, state_diff, num1, num2, "Different heap information");
511 XBT_VERB("(%d - %d) Different heap information", num1, num2);
514 xbt_os_walltimer_stop(global_timer);
515 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
516 xbt_os_timer_free(global_timer);
522 xbt_os_walltimer_start(timer);
525 /* Stacks comparison */
531 mc_snapshot_stack_t stack1, stack2;
532 while (cursor < s1->stacks.size()) {
533 stack1 = &s1->stacks[cursor];
534 stack2 = &s1->stacks[cursor];
536 if (stack1->process_index != stack2->process_index) {
538 XBT_DEBUG("(%d - %d) Stacks with different process index (%i vs %i)", num1, num2,
539 stack1->process_index, stack2->process_index);
542 compare_local_variables(stack1->process_index, s1, s2, stack1, stack2);
543 if (diff_local > 0) {
544 XBT_TRACE3(mc, state_diff, num1, num2, "Different local variables");
547 xbt_os_walltimer_stop(timer);
548 mc_comp_times->stacks_comparison_time = xbt_os_timer_elapsed(timer);
550 XBT_DEBUG("(%d - %d) Different local variables between stacks %d", num1,
557 XBT_VERB("(%d - %d) Different local variables between stacks %d", num1,
561 reset_heap_information();
562 xbt_os_walltimer_stop(timer);
563 xbt_os_timer_free(timer);
564 xbt_os_walltimer_stop(global_timer);
565 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
566 xbt_os_timer_free(global_timer);
574 size_t regions_count = s1->snapshot_regions.size();
575 // TODO, raise a difference instead?
576 xbt_assert(regions_count == s2->snapshot_regions.size());
578 mc_comp_times->global_variables_comparison_time = 0;
580 for (size_t k = 0; k != regions_count; ++k) {
581 mc_mem_region_t region1 = s1->snapshot_regions[k].get();
582 mc_mem_region_t region2 = s2->snapshot_regions[k].get();
585 if (region1->region_type() != simgrid::mc::RegionType::Data)
588 xbt_assert(region1->region_type() == region2->region_type());
589 xbt_assert(region1->object_info() == region2->object_info());
590 xbt_assert(region1->object_info());
592 std::string const& name = region1->object_info()->file_name;
596 xbt_os_walltimer_stop(timer);
597 xbt_os_walltimer_start(timer);
600 /* Compare global variables */
602 compare_global_variables(region1->object_info( ), simgrid::mc::AddressSpace::Normal,
607 XBT_TRACE3(mc, state_diff, num1, num2, "Different global variables");
609 xbt_os_walltimer_stop(timer);
610 mc_comp_times->global_variables_comparison_time
611 += xbt_os_timer_elapsed(timer);
612 XBT_DEBUG("(%d - %d) Different global variables in %s",
613 num1, num2, name.c_str());
617 XBT_VERB("(%d - %d) Different global variables in %s",
618 num1, num2, name.c_str());
621 reset_heap_information();
622 xbt_os_walltimer_stop(timer);
623 xbt_os_timer_free(timer);
624 xbt_os_walltimer_stop(global_timer);
625 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
626 xbt_os_timer_free(global_timer);
634 xbt_os_walltimer_start(timer);
638 if (mmalloc_compare_heap(s1, s2) > 0) {
639 XBT_TRACE3(mc, state_diff, num1, num2, "Different heap");
642 xbt_os_walltimer_stop(timer);
643 mc_comp_times->heap_comparison_time = xbt_os_timer_elapsed(timer);
644 XBT_DEBUG("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
649 XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
652 reset_heap_information();
653 xbt_os_walltimer_stop(timer);
654 xbt_os_timer_free(timer);
655 xbt_os_walltimer_stop(global_timer);
656 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
657 xbt_os_timer_free(global_timer);
663 xbt_os_walltimer_stop(timer);
667 reset_heap_information();
669 xbt_os_walltimer_stop(timer);
670 xbt_os_timer_free(timer);
673 xbt_os_walltimer_stop(global_timer);
674 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
677 xbt_os_timer_free(global_timer);
680 print_comparison_times();
684 if (errors || hash_result)
685 XBT_VERB("(%d - %d) Difference found", num1, num2);
687 XBT_VERB("(%d - %d) No difference found", num1, num2);
690 #if defined(MC_DEBUG) && defined(MC_VERBOSE)
692 // * false positive SHOULD be avoided.
693 // * There MUST not be any false negative.
695 XBT_VERB("(%d - %d) State equality hash test is %s %s", num1, num2,
696 (hash_result != 0) == (errors != 0) ? "true" : "false",
697 !hash_result ? "positive" : "negative");
701 return errors > 0 || hash_result;
705 /***************************** Statistics *****************************/
706 /*******************************************************************/
708 void print_comparison_times()
710 XBT_DEBUG("*** Comparison times ***");
711 XBT_DEBUG("- Nb processes : %f", mc_comp_times->nb_processes_comparison_time);
712 XBT_DEBUG("- Nb bytes used : %f", mc_comp_times->bytes_used_comparison_time);
713 XBT_DEBUG("- Stacks sizes : %f", mc_comp_times->stacks_sizes_comparison_time);
714 XBT_DEBUG("- GLobal variables : %f", mc_comp_times->global_variables_comparison_time);
715 XBT_DEBUG("- Heap : %f", mc_comp_times->heap_comparison_time);
716 XBT_DEBUG("- Stacks : %f", mc_comp_times->stacks_comparison_time);