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 "internal_config.h"
12 #include "mc_private.h"
15 #include "smpi/private.h"
18 #include "xbt/mmalloc.h"
19 #include "xbt/mmalloc/mmprivate.h"
21 #include <xbt/probes.h>
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_compare, mc,
24 "Logging specific to mc_compare");
26 typedef struct s_pointers_pair {
29 bool operator==(s_pointers_pair const& x) const {
30 return this->p1 == x.p1 && this->p2 == x.p2;
32 bool operator<(s_pointers_pair const& x) const {
33 return this->p1 < x.p1 || (this->p1 == x.p1 && this->p2 < x.p2);
35 } s_pointers_pair_t, *pointers_pair_t;
39 struct hash<s_pointers_pair> {
40 typedef uintptr_t result_type;
41 result_type operator()(s_pointers_pair const& x) const {
42 return (result_type) x.p1 ^
43 ((result_type) x.p2 << 8 | (result_type) x.p2 >> (8*sizeof(uintptr_t) - 8));
48 struct mc_compare_state {
49 boost::unordered_set<s_pointers_pair> compared_pointers;
54 /************************** Free functions ****************************/
55 /********************************************************************/
57 static void stack_region_free(stack_region_t s)
60 xbt_free(s->process_name);
65 static void stack_region_free_voidp(void *s)
67 stack_region_free((stack_region_t) * (void **) s);
70 static void pointers_pair_free(pointers_pair_t p)
75 static void pointers_pair_free_voidp(void *p)
77 pointers_pair_free((pointers_pair_t) * (void **) p);
80 /************************** Snapshot comparison *******************************/
81 /******************************************************************************/
83 /** \brief Try to add a pair a compared pointers to the set of compared pointers
85 * \result !=0 if the pointers were added (they were not in the set),
86 * 0 otherwise (they were already in the set)
88 static int add_compared_pointers(mc_compare_state& state, void *p1, void *p2)
90 s_pointers_pair_t new_pair;
93 return state.compared_pointers.insert(new_pair).second ? 1 : 0;
96 static int compare_areas_with_type(struct mc_compare_state& state,
98 void* real_area1, mc_snapshot_t snapshot1, mc_mem_region_t region1,
99 void* real_area2, mc_snapshot_t snapshot2, mc_mem_region_t region2,
100 dw_type_t type, int pointer_level)
102 unsigned int cursor = 0;
103 dw_type_t member, subtype, subsubtype;
104 int elm_size, i, res;
107 switch (type->type) {
108 case DW_TAG_unspecified_type:
111 case DW_TAG_base_type:
112 case DW_TAG_enumeration_type:
113 case DW_TAG_union_type:
115 return mc_snapshot_region_memcmp(
116 real_area1, region1, real_area2, region2,
117 type->byte_size) != 0;
120 case DW_TAG_volatile_type:
121 case DW_TAG_const_type:
123 type = type->subtype;
125 case DW_TAG_array_type:
126 subtype = type->subtype;
127 switch (subtype->type) {
128 case DW_TAG_unspecified_type:
131 case DW_TAG_base_type:
132 case DW_TAG_enumeration_type:
133 case DW_TAG_pointer_type:
134 case DW_TAG_reference_type:
135 case DW_TAG_rvalue_reference_type:
136 case DW_TAG_structure_type:
137 case DW_TAG_class_type:
138 case DW_TAG_union_type:
139 if (subtype->full_type)
140 subtype = subtype->full_type;
141 elm_size = subtype->byte_size;
143 case DW_TAG_const_type:
145 case DW_TAG_volatile_type:
146 subsubtype = subtype->subtype;
147 if (subsubtype->full_type)
148 subsubtype = subsubtype->full_type;
149 elm_size = subsubtype->byte_size;
155 for (i = 0; i < type->element_count; i++) {
156 size_t off = i * elm_size;
157 res = compare_areas_with_type(state, process_index,
158 (char*) real_area1 + off, snapshot1, region1,
159 (char*) real_area2 + off, snapshot2, region2,
160 type->subtype, pointer_level);
165 case DW_TAG_pointer_type:
166 case DW_TAG_reference_type:
167 case DW_TAG_rvalue_reference_type:
169 void* addr_pointed1 = mc_snapshot_read_pointer_region(real_area1, region1);
170 void* addr_pointed2 = mc_snapshot_read_pointer_region(real_area2, region2);
172 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
173 return (addr_pointed1 != addr_pointed2);
176 if (addr_pointed1 == NULL && addr_pointed2 == NULL)
178 if (addr_pointed1 == NULL || addr_pointed2 == NULL)
180 if (!add_compared_pointers(state, addr_pointed1, addr_pointed2))
185 // Some cases are not handled here:
186 // * the pointers lead to different areas (one to the heap, the other to the RW segment ...);
187 // * a pointer leads to the read-only segment of the current object;
188 // * a pointer lead to a different ELF object.
190 if (addr_pointed1 > std_heap
191 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)) {
193 (addr_pointed2 > std_heap
194 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)))
196 // The pointers are both in the heap:
197 return compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
198 snapshot2, NULL, type->subtype, pointer_level);
201 // The pointers are both in the current object R/W segment:
202 else if (mc_region_contain(region1, addr_pointed1)) {
203 if (!mc_region_contain(region2, addr_pointed2))
205 if (type->dw_type_id == NULL)
206 return (addr_pointed1 != addr_pointed2);
208 return compare_areas_with_type(state, process_index,
209 addr_pointed1, snapshot1, region1,
210 addr_pointed2, snapshot2, region2,
211 type->subtype, pointer_level);
215 // TODO, We do not handle very well the case where
216 // it belongs to a different (non-heap) region from the current one.
219 return (addr_pointed1 != addr_pointed2);
224 case DW_TAG_structure_type:
225 case DW_TAG_class_type:
226 xbt_dynar_foreach(type->members, cursor, member) {
228 mc_member_resolve(real_area1, type, member, snapshot1, process_index);
230 mc_member_resolve(real_area2, type, member, snapshot2, process_index);
231 mc_mem_region_t subregion1 = mc_get_region_hinted(member1, snapshot1, process_index, region1);
232 mc_mem_region_t subregion2 = mc_get_region_hinted(member2, snapshot2, process_index, region2);
234 compare_areas_with_type(state, process_index,
235 member1, snapshot1, subregion1,
236 member2, snapshot2, subregion2,
237 member->subtype, pointer_level);
242 case DW_TAG_subroutine_type:
246 XBT_VERB("Unknown case : %d", type->type);
253 static int compare_global_variables(mc_object_info_t object_info,
256 mc_mem_region_t r2, mc_snapshot_t snapshot1,
257 mc_snapshot_t snapshot2)
259 xbt_assert(r1 && r2, "Missing region.");
260 struct mc_compare_state state;
262 xbt_dynar_t variables;
264 unsigned int cursor = 0;
265 dw_variable_t current_var;
267 variables = object_info->global_variables;
269 xbt_dynar_foreach(variables, cursor, current_var) {
271 // If the variable is not in this object, skip it:
272 // We do not expect to find a pointer to something which is not reachable
273 // by the global variables.
274 if ((char *) current_var->address < (char *) object_info->start_rw
275 || (char *) current_var->address > (char *) object_info->end_rw)
278 dw_type_t bvariable_type = current_var->type;
280 compare_areas_with_type(state, process_index,
281 (char *) current_var->address, snapshot1, r1,
282 (char *) current_var->address, snapshot2, r2,
285 XBT_TRACE3(mc, global_diff, -1, -1, current_var->name);
286 XBT_VERB("Global variable %s (%p) is different between snapshots",
287 current_var->name, (char *) current_var->address);
297 static int compare_local_variables(int process_index,
298 mc_snapshot_t snapshot1,
299 mc_snapshot_t snapshot2,
300 mc_snapshot_stack_t stack1,
301 mc_snapshot_stack_t stack2)
303 struct mc_compare_state state;
305 if (xbt_dynar_length(stack1->local_variables) !=
306 xbt_dynar_length(stack2->local_variables)) {
307 XBT_VERB("Different number of local variables");
310 unsigned int cursor = 0;
311 local_variable_t current_var1, current_var2;
313 while (cursor < xbt_dynar_length(stack1->local_variables)) {
315 (local_variable_t) xbt_dynar_get_as(stack1->local_variables, cursor,
318 (local_variable_t) xbt_dynar_get_as(stack2->local_variables, cursor,
320 if (strcmp(current_var1->name, current_var2->name) != 0
321 || current_var1->subprogram != current_var1->subprogram
322 || current_var1->ip != current_var2->ip) {
323 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
325 ("Different name of variable (%s - %s) or frame (%s - %s) or ip (%lu - %lu)",
326 current_var1->name, current_var2->name,
327 current_var1->subprogram->name, current_var2->subprogram->name,
328 current_var1->ip, current_var2->ip);
331 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
333 dw_type_t subtype = current_var1->type;
335 compare_areas_with_type(state, process_index,
336 current_var1->address, snapshot1, mc_get_snapshot_region(current_var1->address, snapshot1, process_index),
337 current_var2->address, snapshot2, mc_get_snapshot_region(current_var2->address, snapshot2, process_index),
341 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
342 XBT_TRACE3(mc, local_diff, -1, -1, current_var1->name);
344 ("Local variable %s (%p - %p) in frame %s is different between snapshots",
345 current_var1->name, current_var1->address, current_var2->address,
346 current_var1->subprogram->name);
355 int snapshot_compare(void *state1, void *state2)
358 mc_snapshot_t s1, s2;
361 if (_sg_mc_liveness) { /* Liveness MC */
362 s1 = ((mc_visited_pair_t) state1)->graph_state->system_state;
363 s2 = ((mc_visited_pair_t) state2)->graph_state->system_state;
364 num1 = ((mc_visited_pair_t) state1)->num;
365 num2 = ((mc_visited_pair_t) state2)->num;
366 } else { /* Safety or comm determinism MC */
367 s1 = ((mc_visited_state_t) state1)->system_state;
368 s2 = ((mc_visited_state_t) state2)->system_state;
369 num1 = ((mc_visited_state_t) state1)->num;
370 num2 = ((mc_visited_state_t) state2)->num;
376 xbt_os_timer_t global_timer = xbt_os_timer_new();
377 xbt_os_timer_t timer = xbt_os_timer_new();
379 xbt_os_walltimer_start(global_timer);
382 xbt_os_walltimer_start(timer);
387 hash_result = (s1->hash != s2->hash);
389 XBT_TRACE2(mc, hash_diff, num1, num2);
390 XBT_VERB("(%d - %d) Different hash : 0x%" PRIx64 "--0x%" PRIx64, num1,
391 num2, s1->hash, s2->hash);
396 XBT_VERB("(%d - %d) Same hash : 0x%" PRIx64, num1, num2, s1->hash);
400 /* Compare enabled processes */
403 xbt_dynar_foreach(s1->enabled_processes, cursor, pid){
404 if(!xbt_dynar_member(s2->enabled_processes, &pid)) {
405 //XBT_TRACE3(mc, state_diff, num1, num2, "Different enabled processes");
406 XBT_VERB("(%d - %d) Different enabled processes", num1, num2);
412 size_t size_used1, size_used2;
415 /* Compare size of stacks */
416 while (i < xbt_dynar_length(s1->stacks)) {
417 size_used1 = s1->stack_sizes[i];
418 size_used2 = s2->stack_sizes[i];
419 if (size_used1 != size_used2) {
422 xbt_os_walltimer_stop(timer);
423 mc_comp_times->stacks_sizes_comparison_time =
424 xbt_os_timer_elapsed(timer);
426 XBT_DEBUG("(%d - %d) Different size used in stacks : %zu - %zu", num1,
427 num2, size_used1, size_used2);
432 XBT_VERB("(%d - %d) Different size used in stacks : %zu - %zu", num1,
433 num2, size_used1, size_used2);
435 XBT_TRACE3(mc, state_diff, num1, num2, "Different stack size");
437 xbt_os_walltimer_stop(timer);
438 xbt_os_timer_free(timer);
439 xbt_os_walltimer_stop(global_timer);
440 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
441 xbt_os_timer_free(global_timer);
451 xbt_os_walltimer_stop(timer);
452 xbt_os_walltimer_start(timer);
455 /* Init heap information used in heap comparison algorithm */
456 xbt_mheap_t heap1 = (xbt_mheap_t) mc_snapshot_read(std_heap, s1, MC_NO_PROCESS_INDEX,
457 alloca(sizeof(struct mdesc)), sizeof(struct mdesc));
458 xbt_mheap_t heap2 = (xbt_mheap_t) mc_snapshot_read(std_heap, s2, MC_NO_PROCESS_INDEX,
459 alloca(sizeof(struct mdesc)), sizeof(struct mdesc));
460 res_init = init_heap_information(heap1, heap2, s1->to_ignore, s2->to_ignore);
461 if (res_init == -1) {
463 XBT_DEBUG("(%d - %d) Different heap information", num1, num2);
467 XBT_TRACE3(mc, state_diff, num1, num2, "Different heap information");
468 XBT_VERB("(%d - %d) Different heap information", num1, num2);
471 xbt_os_walltimer_stop(global_timer);
472 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
473 xbt_os_timer_free(global_timer);
479 xbt_os_walltimer_start(timer);
482 /* Stacks comparison */
488 mc_snapshot_stack_t stack1, stack2;
489 while (cursor < xbt_dynar_length(s1->stacks)) {
491 (mc_snapshot_stack_t) xbt_dynar_get_as(s1->stacks, cursor,
492 mc_snapshot_stack_t);
494 (mc_snapshot_stack_t) xbt_dynar_get_as(s2->stacks, cursor,
495 mc_snapshot_stack_t);
497 if (stack1->process_index != stack2->process_index) {
499 XBT_DEBUG("(%d - %d) Stacks with different process index (%i vs %i)", num1, num2,
500 stack1->process_index, stack2->process_index);
503 compare_local_variables(stack1->process_index, s1, s2, stack1, stack2);
504 if (diff_local > 0) {
505 XBT_TRACE3(mc, state_diff, num1, num2, "Different local variables");
508 xbt_os_walltimer_stop(timer);
509 mc_comp_times->stacks_comparison_time = xbt_os_timer_elapsed(timer);
511 XBT_DEBUG("(%d - %d) Different local variables between stacks %d", num1,
518 XBT_VERB("(%d - %d) Different local variables between stacks %d", num1,
522 reset_heap_information();
523 xbt_os_walltimer_stop(timer);
524 xbt_os_timer_free(timer);
525 xbt_os_walltimer_stop(global_timer);
526 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
527 xbt_os_timer_free(global_timer);
537 const char *names[3] = { "?", "libsimgrid", "binary" };
541 &mc_comp_times->libsimgrid_global_variables_comparison_time,
542 &mc_comp_times->binary_global_variables_comparison_time
546 mc_object_info_t object_infos[] = { NULL, mc_libsimgrid_info, mc_binary_info };
549 for (k = 2; k != 0; --k) {
552 xbt_os_walltimer_stop(timer);
553 xbt_os_walltimer_start(timer);
556 /* Compare global variables */
558 if (object_infos[k] == mc_binary_info && smpi_privatize_global_variables) {
559 // Compare the global variables separately for each simulates process:
560 for (int process_index = 0; process_index < smpi_process_count(); process_index++) {
562 compare_global_variables(object_infos[k], process_index,
563 s1->privatization_regions[process_index], s2->privatization_regions[process_index], s1, s2);
570 compare_global_variables(object_infos[k], MC_NO_PROCESS_INDEX, s1->regions[k], s2->regions[k], s1, s2);
573 XBT_TRACE3(mc, state_diff, num1, num2, "Different global variables");
575 xbt_os_walltimer_stop(timer);
576 *times[k] = xbt_os_timer_elapsed(timer);
577 XBT_DEBUG("(%d - %d) Different global variables in %s", num1, num2,
582 XBT_VERB("(%d - %d) Different global variables in %s", num1, num2,
586 reset_heap_information();
587 xbt_os_walltimer_stop(timer);
588 xbt_os_timer_free(timer);
589 xbt_os_walltimer_stop(global_timer);
590 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
591 xbt_os_timer_free(global_timer);
599 xbt_os_walltimer_start(timer);
603 if (mmalloc_compare_heap(s1, s2) > 0) {
604 XBT_TRACE3(mc, state_diff, num1, num2, "Different heap");
607 xbt_os_walltimer_stop(timer);
608 mc_comp_times->heap_comparison_time = xbt_os_timer_elapsed(timer);
609 XBT_DEBUG("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
614 XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
617 reset_heap_information();
618 xbt_os_walltimer_stop(timer);
619 xbt_os_timer_free(timer);
620 xbt_os_walltimer_stop(global_timer);
621 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
622 xbt_os_timer_free(global_timer);
628 xbt_os_walltimer_stop(timer);
632 reset_heap_information();
634 xbt_os_walltimer_stop(timer);
635 xbt_os_timer_free(timer);
638 xbt_os_walltimer_stop(global_timer);
639 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
642 xbt_os_timer_free(global_timer);
645 print_comparison_times();
649 if (errors || hash_result)
650 XBT_VERB("(%d - %d) Difference found", num1, num2);
652 XBT_VERB("(%d - %d) No difference found", num1, num2);
655 #if defined(MC_DEBUG) && defined(MC_VERBOSE)
657 // * false positive SHOULD be avoided.
658 // * There MUST not be any false negative.
660 XBT_VERB("(%d - %d) State equality hash test is %s %s", num1, num2,
661 (hash_result != 0) == (errors != 0) ? "true" : "false",
662 !hash_result ? "positive" : "negative");
666 return errors > 0 || hash_result;
670 /***************************** Statistics *****************************/
671 /*******************************************************************/
673 void print_comparison_times()
675 XBT_DEBUG("*** Comparison times ***");
676 XBT_DEBUG("- Nb processes : %f", mc_comp_times->nb_processes_comparison_time);
677 XBT_DEBUG("- Nb bytes used : %f", mc_comp_times->bytes_used_comparison_time);
678 XBT_DEBUG("- Stacks sizes : %f", mc_comp_times->stacks_sizes_comparison_time);
679 XBT_DEBUG("- Binary global variables : %f",
680 mc_comp_times->binary_global_variables_comparison_time);
681 XBT_DEBUG("- Libsimgrid global variables : %f",
682 mc_comp_times->libsimgrid_global_variables_comparison_time);
683 XBT_DEBUG("- Heap : %f", mc_comp_times->heap_comparison_time);
684 XBT_DEBUG("- Stacks : %f", mc_comp_times->stacks_comparison_time);
687 /**************************** MC snapshot compare simcall **************************/
688 /***********************************************************************************/
690 int simcall_HANDLER_mc_compare_snapshots(smx_simcall_t simcall,
691 mc_snapshot_t s1, mc_snapshot_t s2)
693 return snapshot_compare(s1, s2);
696 int MC_compare_snapshots(void *s1, void *s2)
699 return simcall_mc_compare_snapshots(s1, s2);