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 typedef struct s_pointers_pair {
32 bool operator==(s_pointers_pair const& x) const {
33 return this->p1 == x.p1 && this->p2 == x.p2;
35 bool operator<(s_pointers_pair const& x) const {
36 return this->p1 < x.p1 || (this->p1 == x.p1 && this->p2 < x.p2);
38 } s_pointers_pair_t, *pointers_pair_t;
42 struct hash<s_pointers_pair> {
43 typedef uintptr_t result_type;
44 result_type operator()(s_pointers_pair const& x) const {
45 return (result_type) x.p1 ^
46 ((result_type) x.p2 << 8 | (result_type) x.p2 >> (8*sizeof(uintptr_t) - 8));
51 struct mc_compare_state {
52 boost::unordered_set<s_pointers_pair> compared_pointers;
57 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_compare, xbt,
58 "Logging specific to mc_compare in mc");
60 /************************** Free functions ****************************/
61 /********************************************************************/
63 static void stack_region_free(stack_region_t s)
70 static void stack_region_free_voidp(void *s)
72 stack_region_free((stack_region_t) * (void **) s);
75 static void pointers_pair_free(pointers_pair_t p)
80 static void pointers_pair_free_voidp(void *p)
82 pointers_pair_free((pointers_pair_t) * (void **) p);
85 /************************** Snapshot comparison *******************************/
86 /******************************************************************************/
88 /** \brief Try to add a pair a compared pointers to the set of compared pointers
90 * \result !=0 if the pointers were added (they were not in the set),
91 * 0 otherwise (they were already in the set)
93 static int add_compared_pointers(mc_compare_state& state, void *p1, void *p2)
95 s_pointers_pair_t new_pair;
98 return state.compared_pointers.insert(new_pair).second ? 1 : 0;
101 static int compare_areas_with_type(struct mc_compare_state& state,
103 void* real_area1, mc_snapshot_t snapshot1, mc_mem_region_t region1,
104 void* real_area2, mc_snapshot_t snapshot2, mc_mem_region_t region2,
105 dw_type_t type, int pointer_level)
107 mc_process_t process = &mc_model_checker->process();
109 unsigned int cursor = 0;
110 dw_type_t member, subtype, subsubtype;
111 int elm_size, i, res;
114 switch (type->type) {
115 case DW_TAG_unspecified_type:
118 case DW_TAG_base_type:
119 case DW_TAG_enumeration_type:
120 case DW_TAG_union_type:
122 return MC_snapshot_region_memcmp(
123 real_area1, region1, real_area2, region2,
124 type->byte_size) != 0;
127 case DW_TAG_volatile_type:
128 case DW_TAG_const_type:
130 type = type->subtype;
132 case DW_TAG_array_type:
133 subtype = type->subtype;
134 switch (subtype->type) {
135 case DW_TAG_unspecified_type:
138 case DW_TAG_base_type:
139 case DW_TAG_enumeration_type:
140 case DW_TAG_pointer_type:
141 case DW_TAG_reference_type:
142 case DW_TAG_rvalue_reference_type:
143 case DW_TAG_structure_type:
144 case DW_TAG_class_type:
145 case DW_TAG_union_type:
146 if (subtype->full_type)
147 subtype = subtype->full_type;
148 elm_size = subtype->byte_size;
150 case DW_TAG_const_type:
152 case DW_TAG_volatile_type:
153 subsubtype = subtype->subtype;
154 if (subsubtype->full_type)
155 subsubtype = subsubtype->full_type;
156 elm_size = subsubtype->byte_size;
162 for (i = 0; i < type->element_count; i++) {
163 size_t off = i * elm_size;
164 res = compare_areas_with_type(state, process_index,
165 (char*) real_area1 + off, snapshot1, region1,
166 (char*) real_area2 + off, snapshot2, region2,
167 type->subtype, pointer_level);
172 case DW_TAG_pointer_type:
173 case DW_TAG_reference_type:
174 case DW_TAG_rvalue_reference_type:
176 void* addr_pointed1 = MC_region_read_pointer(region1, real_area1);
177 void* addr_pointed2 = MC_region_read_pointer(region2, real_area2);
179 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
180 return (addr_pointed1 != addr_pointed2);
183 if (addr_pointed1 == NULL && addr_pointed2 == NULL)
185 if (addr_pointed1 == NULL || addr_pointed2 == NULL)
187 if (!add_compared_pointers(state, addr_pointed1, addr_pointed2))
192 // Some cases are not handled here:
193 // * the pointers lead to different areas (one to the heap, the other to the RW segment ...);
194 // * a pointer leads to the read-only segment of the current object;
195 // * a pointer lead to a different ELF object.
197 if (addr_pointed1 > process->heap_address
198 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)) {
200 (addr_pointed2 > process->heap_address
201 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)))
203 // The pointers are both in the heap:
204 return compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
205 snapshot2, NULL, type->subtype, pointer_level);
208 // The pointers are both in the current object R/W segment:
209 else if (mc_region_contain(region1, addr_pointed1)) {
210 if (!mc_region_contain(region2, addr_pointed2))
212 if (type->dw_type_id == NULL)
213 return (addr_pointed1 != addr_pointed2);
215 return compare_areas_with_type(state, process_index,
216 addr_pointed1, snapshot1, region1,
217 addr_pointed2, snapshot2, region2,
218 type->subtype, pointer_level);
222 // TODO, We do not handle very well the case where
223 // it belongs to a different (non-heap) region from the current one.
226 return (addr_pointed1 != addr_pointed2);
231 case DW_TAG_structure_type:
232 case DW_TAG_class_type:
233 xbt_dynar_foreach(type->members, cursor, member) {
235 mc_member_resolve(real_area1, type, member, snapshot1, process_index);
237 mc_member_resolve(real_area2, type, member, snapshot2, process_index);
238 mc_mem_region_t subregion1 = mc_get_region_hinted(member1, snapshot1, process_index, region1);
239 mc_mem_region_t subregion2 = mc_get_region_hinted(member2, snapshot2, process_index, region2);
241 compare_areas_with_type(state, process_index,
242 member1, snapshot1, subregion1,
243 member2, snapshot2, subregion2,
244 member->subtype, pointer_level);
249 case DW_TAG_subroutine_type:
253 XBT_VERB("Unknown case : %d", type->type);
260 static int compare_global_variables(mc_object_info_t object_info,
263 mc_mem_region_t r2, mc_snapshot_t snapshot1,
264 mc_snapshot_t snapshot2)
266 xbt_assert(r1 && r2, "Missing region.");
269 if (r1->storage_type == MC_REGION_STORAGE_TYPE_PRIVATIZED) {
270 xbt_assert(process_index >= 0);
271 if (r2->storage_type != MC_REGION_STORAGE_TYPE_PRIVATIZED) {
275 size_t process_count = MC_smpi_process_count();
276 xbt_assert(process_count == r1->privatized.regions_count
277 && process_count == r2->privatized.regions_count);
279 // Compare the global variables separately for each simulates process:
280 for (size_t process_index = 0; process_index < process_count; process_index++) {
281 int is_diff = compare_global_variables(object_info, process_index,
282 r1->privatized.regions[process_index], r2->privatized.regions[process_index],
283 snapshot1, snapshot2);
284 if (is_diff) return 1;
289 xbt_assert(r1->storage_type != MC_REGION_STORAGE_TYPE_PRIVATIZED);
291 xbt_assert(r2->storage_type != MC_REGION_STORAGE_TYPE_PRIVATIZED);
293 struct mc_compare_state state;
295 xbt_dynar_t variables;
297 unsigned int cursor = 0;
298 dw_variable_t current_var;
300 variables = object_info->global_variables;
302 xbt_dynar_foreach(variables, cursor, current_var) {
304 // If the variable is not in this object, skip it:
305 // We do not expect to find a pointer to something which is not reachable
306 // by the global variables.
307 if ((char *) current_var->address < (char *) object_info->start_rw
308 || (char *) current_var->address > (char *) object_info->end_rw)
311 dw_type_t bvariable_type = current_var->type;
313 compare_areas_with_type(state, process_index,
314 (char *) current_var->address, snapshot1, r1,
315 (char *) current_var->address, snapshot2, r2,
318 XBT_TRACE3(mc, global_diff, -1, -1, current_var->name);
319 XBT_VERB("Global variable %s (%p) is different between snapshots",
320 current_var->name, (char *) current_var->address);
330 static int compare_local_variables(int process_index,
331 mc_snapshot_t snapshot1,
332 mc_snapshot_t snapshot2,
333 mc_snapshot_stack_t stack1,
334 mc_snapshot_stack_t stack2)
336 struct mc_compare_state state;
338 if (xbt_dynar_length(stack1->local_variables) !=
339 xbt_dynar_length(stack2->local_variables)) {
340 XBT_VERB("Different number of local variables");
343 unsigned int cursor = 0;
344 local_variable_t current_var1, current_var2;
346 while (cursor < xbt_dynar_length(stack1->local_variables)) {
348 (local_variable_t) xbt_dynar_get_as(stack1->local_variables, cursor,
351 (local_variable_t) xbt_dynar_get_as(stack2->local_variables, cursor,
353 if (strcmp(current_var1->name, current_var2->name) != 0
354 || current_var1->subprogram != current_var1->subprogram
355 || current_var1->ip != current_var2->ip) {
356 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
358 ("Different name of variable (%s - %s) or frame (%s - %s) or ip (%lu - %lu)",
359 current_var1->name, current_var2->name,
360 current_var1->subprogram->name, current_var2->subprogram->name,
361 current_var1->ip, current_var2->ip);
364 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
366 dw_type_t subtype = current_var1->type;
368 compare_areas_with_type(state, process_index,
369 current_var1->address, snapshot1, mc_get_snapshot_region(current_var1->address, snapshot1, process_index),
370 current_var2->address, snapshot2, mc_get_snapshot_region(current_var2->address, snapshot2, process_index),
374 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
375 XBT_TRACE3(mc, local_diff, -1, -1, current_var1->name);
377 ("Local variable %s (%p - %p) in frame %s is different between snapshots",
378 current_var1->name, current_var1->address, current_var2->address,
379 current_var1->subprogram->name);
388 int snapshot_compare(void *state1, void *state2)
390 mc_process_t process = &mc_model_checker->process();
392 mc_snapshot_t s1, s2;
395 if (_sg_mc_liveness) { /* Liveness MC */
396 s1 = ((mc_visited_pair_t) state1)->graph_state->system_state;
397 s2 = ((mc_visited_pair_t) state2)->graph_state->system_state;
398 num1 = ((mc_visited_pair_t) state1)->num;
399 num2 = ((mc_visited_pair_t) state2)->num;
400 }else if (_sg_mc_termination) { /* Non-progressive cycle MC */
401 s1 = ((mc_state_t) state1)->system_state;
402 s2 = ((mc_state_t) state2)->system_state;
403 num1 = ((mc_state_t) state1)->num;
404 num2 = ((mc_state_t) state2)->num;
405 } else { /* Safety or comm determinism MC */
406 s1 = ((mc_visited_state_t) state1)->system_state;
407 s2 = ((mc_visited_state_t) state2)->system_state;
408 num1 = ((mc_visited_state_t) state1)->num;
409 num2 = ((mc_visited_state_t) state2)->num;
415 xbt_os_timer_t global_timer = xbt_os_timer_new();
416 xbt_os_timer_t timer = xbt_os_timer_new();
418 xbt_os_walltimer_start(global_timer);
421 xbt_os_walltimer_start(timer);
426 hash_result = (s1->hash != s2->hash);
428 XBT_TRACE2(mc, hash_diff, num1, num2);
429 XBT_VERB("(%d - %d) Different hash : 0x%" PRIx64 "--0x%" PRIx64, num1,
430 num2, s1->hash, s2->hash);
435 XBT_VERB("(%d - %d) Same hash : 0x%" PRIx64, num1, num2, s1->hash);
439 /* Compare enabled processes */
442 xbt_dynar_foreach(s1->enabled_processes, cursor, pid){
443 if(!xbt_dynar_member(s2->enabled_processes, &pid)) {
444 //XBT_TRACE3(mc, state_diff, num1, num2, "Different enabled processes");
445 XBT_VERB("(%d - %d) Different enabled processes", num1, num2);
451 size_t size_used1, size_used2;
454 /* Compare size of stacks */
455 while (i < xbt_dynar_length(s1->stacks)) {
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 (std::uint64_t)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 (std::uint64_t)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 < xbt_dynar_length(s1->stacks)) {
534 (mc_snapshot_stack_t) xbt_dynar_get_as(s1->stacks, cursor,
535 mc_snapshot_stack_t);
537 (mc_snapshot_stack_t) xbt_dynar_get_as(s2->stacks, cursor,
538 mc_snapshot_stack_t);
540 if (stack1->process_index != stack2->process_index) {
542 XBT_DEBUG("(%d - %d) Stacks with different process index (%i vs %i)", num1, num2,
543 stack1->process_index, stack2->process_index);
546 compare_local_variables(stack1->process_index, s1, s2, stack1, stack2);
547 if (diff_local > 0) {
548 XBT_TRACE3(mc, state_diff, num1, num2, "Different local variables");
551 xbt_os_walltimer_stop(timer);
552 mc_comp_times->stacks_comparison_time = xbt_os_timer_elapsed(timer);
554 XBT_DEBUG("(%d - %d) Different local variables between stacks %d", num1,
561 XBT_VERB("(%d - %d) Different local variables between stacks %d", num1,
565 reset_heap_information();
566 xbt_os_walltimer_stop(timer);
567 xbt_os_timer_free(timer);
568 xbt_os_walltimer_stop(global_timer);
569 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
570 xbt_os_timer_free(global_timer);
578 size_t regions_count = s1->snapshot_regions_count;
579 // TODO, raise a difference instead?
580 xbt_assert(regions_count == s2->snapshot_regions_count);
582 mc_comp_times->global_variables_comparison_time = 0;
584 for (size_t k = 0; k != regions_count; ++k) {
585 mc_mem_region_t region1 = s1->snapshot_regions[k];
586 mc_mem_region_t region2 = s2->snapshot_regions[k];
589 if (region1->region_type != MC_REGION_TYPE_DATA)
592 xbt_assert(region1->region_type == region2->region_type);
593 xbt_assert(region1->object_info == region2->object_info);
595 xbt_assert(region1->object_info);
597 const char* name = region1->object_info->file_name;
601 xbt_os_walltimer_stop(timer);
602 xbt_os_walltimer_start(timer);
605 /* Compare global variables */
607 compare_global_variables(region1->object_info, simgrid::mc::AddressSpace::Normal,
612 XBT_TRACE3(mc, state_diff, num1, num2, "Different global variables");
614 xbt_os_walltimer_stop(timer);
615 mc_comp_times->global_variables_comparison_time
616 += xbt_os_timer_elapsed(timer);
617 XBT_DEBUG("(%d - %d) Different global variables in %s", num1, num2,
622 XBT_VERB("(%d - %d) Different global variables in %s", num1, num2,
626 reset_heap_information();
627 xbt_os_walltimer_stop(timer);
628 xbt_os_timer_free(timer);
629 xbt_os_walltimer_stop(global_timer);
630 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
631 xbt_os_timer_free(global_timer);
639 xbt_os_walltimer_start(timer);
643 if (mmalloc_compare_heap(s1, s2) > 0) {
644 XBT_TRACE3(mc, state_diff, num1, num2, "Different heap");
647 xbt_os_walltimer_stop(timer);
648 mc_comp_times->heap_comparison_time = xbt_os_timer_elapsed(timer);
649 XBT_DEBUG("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
654 XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
657 reset_heap_information();
658 xbt_os_walltimer_stop(timer);
659 xbt_os_timer_free(timer);
660 xbt_os_walltimer_stop(global_timer);
661 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
662 xbt_os_timer_free(global_timer);
668 xbt_os_walltimer_stop(timer);
672 reset_heap_information();
674 xbt_os_walltimer_stop(timer);
675 xbt_os_timer_free(timer);
678 xbt_os_walltimer_stop(global_timer);
679 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
682 xbt_os_timer_free(global_timer);
685 print_comparison_times();
689 if (errors || hash_result)
690 XBT_VERB("(%d - %d) Difference found", num1, num2);
692 XBT_VERB("(%d - %d) No difference found", num1, num2);
695 #if defined(MC_DEBUG) && defined(MC_VERBOSE)
697 // * false positive SHOULD be avoided.
698 // * There MUST not be any false negative.
700 XBT_VERB("(%d - %d) State equality hash test is %s %s", num1, num2,
701 (hash_result != 0) == (errors != 0) ? "true" : "false",
702 !hash_result ? "positive" : "negative");
706 return errors > 0 || hash_result;
710 /***************************** Statistics *****************************/
711 /*******************************************************************/
713 void print_comparison_times()
715 XBT_DEBUG("*** Comparison times ***");
716 XBT_DEBUG("- Nb processes : %f", mc_comp_times->nb_processes_comparison_time);
717 XBT_DEBUG("- Nb bytes used : %f", mc_comp_times->bytes_used_comparison_time);
718 XBT_DEBUG("- Stacks sizes : %f", mc_comp_times->stacks_sizes_comparison_time);
719 XBT_DEBUG("- GLobal variables : %f", mc_comp_times->global_variables_comparison_time);
720 XBT_DEBUG("- Heap : %f", mc_comp_times->heap_comparison_time);
721 XBT_DEBUG("- Stacks : %f", mc_comp_times->stacks_comparison_time);