1 /* Copyright (c) 2012-2014. 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"
21 #include "smpi/private.h"
24 #include "xbt/mmalloc.h"
25 #include "xbt/mmalloc/mmprivate.h"
27 #include <xbt/probes.h>
29 using simgrid::mc::remote;
31 typedef struct s_pointers_pair {
34 bool operator==(s_pointers_pair const& x) const {
35 return this->p1 == x.p1 && this->p2 == x.p2;
37 bool operator<(s_pointers_pair const& x) const {
38 return this->p1 < x.p1 || (this->p1 == x.p1 && this->p2 < x.p2);
40 } s_pointers_pair_t, *pointers_pair_t;
44 struct hash<s_pointers_pair> {
45 typedef uintptr_t result_type;
46 result_type operator()(s_pointers_pair const& x) const {
47 return (result_type) x.p1 ^
48 ((result_type) x.p2 << 8 | (result_type) x.p2 >> (8*sizeof(uintptr_t) - 8));
53 struct mc_compare_state {
54 boost::unordered_set<s_pointers_pair> compared_pointers;
59 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_compare, xbt,
60 "Logging specific to mc_compare in mc");
62 /************************** Free functions ****************************/
63 /********************************************************************/
65 static void stack_region_free(stack_region_t s)
72 static void stack_region_free_voidp(void *s)
74 stack_region_free((stack_region_t) * (void **) s);
77 static void pointers_pair_free(pointers_pair_t p)
82 static void pointers_pair_free_voidp(void *p)
84 pointers_pair_free((pointers_pair_t) * (void **) p);
87 /************************** Snapshot comparison *******************************/
88 /******************************************************************************/
90 /** \brief Try to add a pair a compared pointers to the set of compared pointers
92 * \result !=0 if the pointers were added (they were not in the set),
93 * 0 otherwise (they were already in the set)
95 static int add_compared_pointers(mc_compare_state& state, void *p1, void *p2)
97 s_pointers_pair_t new_pair;
100 return state.compared_pointers.insert(new_pair).second ? 1 : 0;
103 static int compare_areas_with_type(struct mc_compare_state& state,
105 void* real_area1, mc_snapshot_t snapshot1, mc_mem_region_t region1,
106 void* real_area2, mc_snapshot_t snapshot2, mc_mem_region_t region2,
107 dw_type_t type, int pointer_level)
109 mc_process_t process = &mc_model_checker->process();
111 unsigned int cursor = 0;
112 dw_type_t member, subtype, subsubtype;
113 int elm_size, i, res;
116 switch (type->type) {
117 case DW_TAG_unspecified_type:
120 case DW_TAG_base_type:
121 case DW_TAG_enumeration_type:
122 case DW_TAG_union_type:
124 return MC_snapshot_region_memcmp(
125 real_area1, region1, real_area2, region2,
126 type->byte_size) != 0;
129 case DW_TAG_volatile_type:
130 case DW_TAG_const_type:
132 type = type->subtype;
134 case DW_TAG_array_type:
135 subtype = type->subtype;
136 switch (subtype->type) {
137 case DW_TAG_unspecified_type:
140 case DW_TAG_base_type:
141 case DW_TAG_enumeration_type:
142 case DW_TAG_pointer_type:
143 case DW_TAG_reference_type:
144 case DW_TAG_rvalue_reference_type:
145 case DW_TAG_structure_type:
146 case DW_TAG_class_type:
147 case DW_TAG_union_type:
148 if (subtype->full_type)
149 subtype = subtype->full_type;
150 elm_size = subtype->byte_size;
152 case DW_TAG_const_type:
154 case DW_TAG_volatile_type:
155 subsubtype = subtype->subtype;
156 if (subsubtype->full_type)
157 subsubtype = subsubtype->full_type;
158 elm_size = subsubtype->byte_size;
164 for (i = 0; i < type->element_count; i++) {
165 size_t off = i * elm_size;
166 res = compare_areas_with_type(state, process_index,
167 (char*) real_area1 + off, snapshot1, region1,
168 (char*) real_area2 + off, snapshot2, region2,
169 type->subtype, pointer_level);
174 case DW_TAG_pointer_type:
175 case DW_TAG_reference_type:
176 case DW_TAG_rvalue_reference_type:
178 void* addr_pointed1 = MC_region_read_pointer(region1, real_area1);
179 void* addr_pointed2 = MC_region_read_pointer(region2, real_area2);
181 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
182 return (addr_pointed1 != addr_pointed2);
185 if (addr_pointed1 == NULL && addr_pointed2 == NULL)
187 if (addr_pointed1 == NULL || addr_pointed2 == NULL)
189 if (!add_compared_pointers(state, addr_pointed1, addr_pointed2))
194 // Some cases are not handled here:
195 // * the pointers lead to different areas (one to the heap, the other to the RW segment ...);
196 // * a pointer leads to the read-only segment of the current object;
197 // * a pointer lead to a different ELF object.
199 if (addr_pointed1 > process->heap_address
200 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)) {
202 (addr_pointed2 > process->heap_address
203 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)))
205 // The pointers are both in the heap:
206 return compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
207 snapshot2, NULL, type->subtype, pointer_level);
210 // The pointers are both in the current object R/W segment:
211 else if (region1->contain(simgrid::mc::remote(addr_pointed1))) {
212 if (!region2->contain(simgrid::mc::remote(addr_pointed2)))
214 if (type->dw_type_id == NULL)
215 return (addr_pointed1 != addr_pointed2);
217 return compare_areas_with_type(state, process_index,
218 addr_pointed1, snapshot1, region1,
219 addr_pointed2, snapshot2, region2,
220 type->subtype, pointer_level);
224 // TODO, We do not handle very well the case where
225 // it belongs to a different (non-heap) region from the current one.
228 return (addr_pointed1 != addr_pointed2);
233 case DW_TAG_structure_type:
234 case DW_TAG_class_type:
235 xbt_dynar_foreach(type->members, cursor, member) {
237 mc_member_resolve(real_area1, type, member, snapshot1, process_index);
239 mc_member_resolve(real_area2, type, member, snapshot2, process_index);
240 mc_mem_region_t subregion1 = mc_get_region_hinted(member1, snapshot1, process_index, region1);
241 mc_mem_region_t subregion2 = mc_get_region_hinted(member2, snapshot2, process_index, region2);
243 compare_areas_with_type(state, process_index,
244 member1, snapshot1, subregion1,
245 member2, snapshot2, subregion2,
246 member->subtype, pointer_level);
251 case DW_TAG_subroutine_type:
255 XBT_VERB("Unknown case : %d", type->type);
262 static int compare_global_variables(mc_object_info_t object_info,
265 mc_mem_region_t r2, mc_snapshot_t snapshot1,
266 mc_snapshot_t snapshot2)
268 xbt_assert(r1 && r2, "Missing region.");
271 if (r1->storage_type() == simgrid::mc::StorageType::Privatized) {
272 xbt_assert(process_index >= 0);
273 if (r2->storage_type() != simgrid::mc::StorageType::Privatized) {
277 size_t process_count = MC_smpi_process_count();
278 xbt_assert(process_count == r1->privatized_data().size()
279 && process_count == r2->privatized_data().size());
281 // Compare the global variables separately for each simulates process:
282 for (size_t process_index = 0; process_index < process_count; process_index++) {
283 int is_diff = compare_global_variables(object_info, process_index,
284 &r1->privatized_data()[process_index],
285 &r2->privatized_data()[process_index],
286 snapshot1, snapshot2);
287 if (is_diff) return 1;
292 xbt_assert(r1->storage_type() != simgrid::mc::StorageType::Privatized);
294 xbt_assert(r2->storage_type() != simgrid::mc::StorageType::Privatized);
296 struct mc_compare_state state;
298 xbt_dynar_t variables;
300 unsigned int cursor = 0;
301 dw_variable_t current_var;
303 variables = object_info->global_variables;
305 xbt_dynar_foreach(variables, cursor, current_var) {
307 // If the variable is not in this object, skip it:
308 // We do not expect to find a pointer to something which is not reachable
309 // by the global variables.
310 if ((char *) current_var->address < (char *) object_info->start_rw
311 || (char *) current_var->address > (char *) object_info->end_rw)
314 dw_type_t bvariable_type = current_var->type;
316 compare_areas_with_type(state, process_index,
317 (char *) current_var->address, snapshot1, r1,
318 (char *) current_var->address, snapshot2, r2,
321 XBT_TRACE3(mc, global_diff, -1, -1, current_var->name);
322 XBT_VERB("Global variable %s (%p) is different between snapshots",
323 current_var->name, (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) or frame (%s - %s) or ip (%lu - %lu)",
357 current_var1->name.c_str(), current_var2->name.c_str(),
358 current_var1->subprogram->name, current_var2->subprogram->name,
359 current_var1->ip, current_var2->ip);
362 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
364 dw_type_t subtype = current_var1->type;
366 compare_areas_with_type(state, process_index,
367 current_var1->address, snapshot1, mc_get_snapshot_region(current_var1->address, snapshot1, process_index),
368 current_var2->address, snapshot2, mc_get_snapshot_region(current_var2->address, snapshot2, process_index),
372 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
373 XBT_TRACE3(mc, local_diff, -1, -1, current_var1->name);
375 ("Local variable %s (%p - %p) in frame %s is different between snapshots",
376 current_var1->name.c_str(), current_var1->address, current_var2->address,
377 current_var1->subprogram->name);
386 int snapshot_compare(void *state1, void *state2)
388 mc_process_t process = &mc_model_checker->process();
390 mc_snapshot_t s1, s2;
393 if (_sg_mc_liveness) { /* Liveness MC */
394 s1 = ((mc_visited_pair_t) state1)->graph_state->system_state;
395 s2 = ((mc_visited_pair_t) state2)->graph_state->system_state;
396 num1 = ((mc_visited_pair_t) state1)->num;
397 num2 = ((mc_visited_pair_t) state2)->num;
398 }else if (_sg_mc_termination) { /* Non-progressive cycle MC */
399 s1 = ((mc_state_t) state1)->system_state;
400 s2 = ((mc_state_t) state2)->system_state;
401 num1 = ((mc_state_t) state1)->num;
402 num2 = ((mc_state_t) state2)->num;
403 } else { /* Safety or comm determinism MC */
404 s1 = ((mc_visited_state_t) state1)->system_state;
405 s2 = ((mc_visited_state_t) state2)->system_state;
406 num1 = ((mc_visited_state_t) state1)->num;
407 num2 = ((mc_visited_state_t) state2)->num;
413 xbt_os_timer_t global_timer = xbt_os_timer_new();
414 xbt_os_timer_t timer = xbt_os_timer_new();
416 xbt_os_walltimer_start(global_timer);
419 xbt_os_walltimer_start(timer);
424 hash_result = (s1->hash != s2->hash);
426 XBT_TRACE2(mc, hash_diff, num1, num2);
427 XBT_VERB("(%d - %d) Different hash : 0x%" PRIx64 "--0x%" PRIx64, num1,
428 num2, s1->hash, s2->hash);
433 XBT_VERB("(%d - %d) Same hash : 0x%" PRIx64, num1, num2, s1->hash);
437 /* Compare enabled processes */
438 if (s1->enabled_processes != s2->enabled_processes) {
439 //XBT_TRACE3(mc, state_diff, num1, num2, "Different enabled processes");
440 XBT_VERB("(%d - %d) Different enabled processes", num1, num2);
445 size_t size_used1, size_used2;
448 /* Compare size of stacks */
449 while (i < xbt_dynar_length(s1->stacks)) {
450 size_used1 = s1->stack_sizes[i];
451 size_used2 = s2->stack_sizes[i];
452 if (size_used1 != size_used2) {
455 xbt_os_walltimer_stop(timer);
456 mc_comp_times->stacks_sizes_comparison_time =
457 xbt_os_timer_elapsed(timer);
459 XBT_DEBUG("(%d - %d) Different size used in stacks : %zu - %zu", num1,
460 num2, size_used1, size_used2);
465 XBT_VERB("(%d - %d) Different size used in stacks : %zu - %zu", num1,
466 num2, size_used1, size_used2);
468 XBT_TRACE3(mc, state_diff, num1, num2, "Different stack size");
470 xbt_os_walltimer_stop(timer);
471 xbt_os_timer_free(timer);
472 xbt_os_walltimer_stop(global_timer);
473 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
474 xbt_os_timer_free(global_timer);
484 xbt_os_walltimer_stop(timer);
485 xbt_os_walltimer_start(timer);
488 /* Init heap information used in heap comparison algorithm */
489 xbt_mheap_t heap1 = (xbt_mheap_t)s1->read_bytes(
490 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
491 remote(process->heap_address),
492 simgrid::mc::ProcessIndexMissing, simgrid::mc::AddressSpace::Lazy);
493 xbt_mheap_t heap2 = (xbt_mheap_t)s2->read_bytes(
494 alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
495 remote(process->heap_address),
496 simgrid::mc::ProcessIndexMissing, simgrid::mc::AddressSpace::Lazy);
497 res_init = init_heap_information(heap1, heap2, &s1->to_ignore, &s2->to_ignore);
498 if (res_init == -1) {
500 XBT_DEBUG("(%d - %d) Different heap information", num1, num2);
504 XBT_TRACE3(mc, state_diff, num1, num2, "Different heap information");
505 XBT_VERB("(%d - %d) Different heap information", num1, num2);
508 xbt_os_walltimer_stop(global_timer);
509 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
510 xbt_os_timer_free(global_timer);
516 xbt_os_walltimer_start(timer);
519 /* Stacks comparison */
525 mc_snapshot_stack_t stack1, stack2;
526 while (cursor < xbt_dynar_length(s1->stacks)) {
528 (mc_snapshot_stack_t) xbt_dynar_get_as(s1->stacks, cursor,
529 mc_snapshot_stack_t);
531 (mc_snapshot_stack_t) xbt_dynar_get_as(s2->stacks, cursor,
532 mc_snapshot_stack_t);
534 if (stack1->process_index != stack2->process_index) {
536 XBT_DEBUG("(%d - %d) Stacks with different process index (%i vs %i)", num1, num2,
537 stack1->process_index, stack2->process_index);
540 compare_local_variables(stack1->process_index, s1, s2, stack1, stack2);
541 if (diff_local > 0) {
542 XBT_TRACE3(mc, state_diff, num1, num2, "Different local variables");
545 xbt_os_walltimer_stop(timer);
546 mc_comp_times->stacks_comparison_time = xbt_os_timer_elapsed(timer);
548 XBT_DEBUG("(%d - %d) Different local variables between stacks %d", num1,
555 XBT_VERB("(%d - %d) Different local variables between stacks %d", num1,
559 reset_heap_information();
560 xbt_os_walltimer_stop(timer);
561 xbt_os_timer_free(timer);
562 xbt_os_walltimer_stop(global_timer);
563 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
564 xbt_os_timer_free(global_timer);
572 size_t regions_count = s1->snapshot_regions.size();
573 // TODO, raise a difference instead?
574 xbt_assert(regions_count == s2->snapshot_regions.size());
576 mc_comp_times->global_variables_comparison_time = 0;
578 for (size_t k = 0; k != regions_count; ++k) {
579 mc_mem_region_t region1 = s1->snapshot_regions[k].get();
580 mc_mem_region_t region2 = s2->snapshot_regions[k].get();
583 if (region1->region_type() != simgrid::mc::RegionType::Data)
586 xbt_assert(region1->region_type() == region2->region_type());
587 xbt_assert(region1->object_info() == region2->object_info());
588 xbt_assert(region1->object_info());
590 const char* name = region1->object_info()->file_name;
594 xbt_os_walltimer_stop(timer);
595 xbt_os_walltimer_start(timer);
598 /* Compare global variables */
600 compare_global_variables(region1->object_info( ), simgrid::mc::AddressSpace::Normal,
605 XBT_TRACE3(mc, state_diff, num1, num2, "Different global variables");
607 xbt_os_walltimer_stop(timer);
608 mc_comp_times->global_variables_comparison_time
609 += xbt_os_timer_elapsed(timer);
610 XBT_DEBUG("(%d - %d) Different global variables in %s", num1, num2,
615 XBT_VERB("(%d - %d) Different global variables in %s", num1, num2,
619 reset_heap_information();
620 xbt_os_walltimer_stop(timer);
621 xbt_os_timer_free(timer);
622 xbt_os_walltimer_stop(global_timer);
623 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
624 xbt_os_timer_free(global_timer);
632 xbt_os_walltimer_start(timer);
636 if (mmalloc_compare_heap(s1, s2) > 0) {
637 XBT_TRACE3(mc, state_diff, num1, num2, "Different heap");
640 xbt_os_walltimer_stop(timer);
641 mc_comp_times->heap_comparison_time = xbt_os_timer_elapsed(timer);
642 XBT_DEBUG("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
647 XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
650 reset_heap_information();
651 xbt_os_walltimer_stop(timer);
652 xbt_os_timer_free(timer);
653 xbt_os_walltimer_stop(global_timer);
654 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
655 xbt_os_timer_free(global_timer);
661 xbt_os_walltimer_stop(timer);
665 reset_heap_information();
667 xbt_os_walltimer_stop(timer);
668 xbt_os_timer_free(timer);
671 xbt_os_walltimer_stop(global_timer);
672 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
675 xbt_os_timer_free(global_timer);
678 print_comparison_times();
682 if (errors || hash_result)
683 XBT_VERB("(%d - %d) Difference found", num1, num2);
685 XBT_VERB("(%d - %d) No difference found", num1, num2);
688 #if defined(MC_DEBUG) && defined(MC_VERBOSE)
690 // * false positive SHOULD be avoided.
691 // * There MUST not be any false negative.
693 XBT_VERB("(%d - %d) State equality hash test is %s %s", num1, num2,
694 (hash_result != 0) == (errors != 0) ? "true" : "false",
695 !hash_result ? "positive" : "negative");
699 return errors > 0 || hash_result;
703 /***************************** Statistics *****************************/
704 /*******************************************************************/
706 void print_comparison_times()
708 XBT_DEBUG("*** Comparison times ***");
709 XBT_DEBUG("- Nb processes : %f", mc_comp_times->nb_processes_comparison_time);
710 XBT_DEBUG("- Nb bytes used : %f", mc_comp_times->bytes_used_comparison_time);
711 XBT_DEBUG("- Stacks sizes : %f", mc_comp_times->stacks_sizes_comparison_time);
712 XBT_DEBUG("- GLobal variables : %f", mc_comp_times->global_variables_comparison_time);
713 XBT_DEBUG("- Heap : %f", mc_comp_times->heap_comparison_time);
714 XBT_DEBUG("- Stacks : %f", mc_comp_times->stacks_comparison_time);