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 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_compare, mc,
22 "Logging specific to mc_compare");
24 typedef struct s_pointers_pair {
27 bool operator==(s_pointers_pair const& x) const {
28 return this->p1 == x.p1 && this->p2 == x.p2;
30 bool operator<(s_pointers_pair const& x) const {
31 return this->p1 < x.p1 || (this->p1 == x.p1 && this->p2 < x.p2);
33 } s_pointers_pair_t, *pointers_pair_t;
37 struct hash<s_pointers_pair> {
38 typedef uintptr_t result_type;
39 result_type operator()(s_pointers_pair const& x) const {
40 return (result_type) x.p1 ^
41 ((result_type) x.p2 << 8 | (result_type) x.p2 >> (8*sizeof(uintptr_t) - 8));
46 struct mc_compare_state {
47 boost::unordered_set<s_pointers_pair> compared_pointers;
52 /************************** Free functions ****************************/
53 /********************************************************************/
55 static void stack_region_free(stack_region_t s)
58 xbt_free(s->process_name);
63 static void stack_region_free_voidp(void *s)
65 stack_region_free((stack_region_t) * (void **) s);
68 static void pointers_pair_free(pointers_pair_t p)
73 static void pointers_pair_free_voidp(void *p)
75 pointers_pair_free((pointers_pair_t) * (void **) p);
78 /************************** Snapshot comparison *******************************/
79 /******************************************************************************/
81 /** \brief Try to add a pair a compared pointers to the set of compared pointers
83 * \result !=0 if the pointers were added (they were not in the set),
84 * 0 otherwise (they were already in the set)
86 static int add_compared_pointers(mc_compare_state& state, void *p1, void *p2)
88 s_pointers_pair_t new_pair;
91 return state.compared_pointers.insert(new_pair).second ? 1 : 0;
94 static int compare_areas_with_type(struct mc_compare_state& state,
96 void* real_area1, mc_snapshot_t snapshot1, mc_mem_region_t region1,
97 void* real_area2, mc_snapshot_t snapshot2, mc_mem_region_t region2,
98 dw_type_t type, int pointer_level)
100 unsigned int cursor = 0;
101 dw_type_t member, subtype, subsubtype;
102 int elm_size, i, res;
105 switch (type->type) {
106 case DW_TAG_unspecified_type:
109 case DW_TAG_base_type:
110 case DW_TAG_enumeration_type:
111 case DW_TAG_union_type:
113 return mc_snapshot_region_memcmp(
114 real_area1, region1, real_area2, region2,
115 type->byte_size) != 0;
118 case DW_TAG_volatile_type:
119 case DW_TAG_const_type:
121 type = type->subtype;
123 case DW_TAG_array_type:
124 subtype = type->subtype;
125 switch (subtype->type) {
126 case DW_TAG_unspecified_type:
129 case DW_TAG_base_type:
130 case DW_TAG_enumeration_type:
131 case DW_TAG_pointer_type:
132 case DW_TAG_reference_type:
133 case DW_TAG_rvalue_reference_type:
134 case DW_TAG_structure_type:
135 case DW_TAG_class_type:
136 case DW_TAG_union_type:
137 if (subtype->full_type)
138 subtype = subtype->full_type;
139 elm_size = subtype->byte_size;
141 case DW_TAG_const_type:
143 case DW_TAG_volatile_type:
144 subsubtype = subtype->subtype;
145 if (subsubtype->full_type)
146 subsubtype = subsubtype->full_type;
147 elm_size = subsubtype->byte_size;
153 for (i = 0; i < type->element_count; i++) {
154 size_t off = i * elm_size;
155 res = compare_areas_with_type(state, process_index,
156 (char*) real_area1 + off, snapshot1, region1,
157 (char*) real_area2 + off, snapshot2, region2,
158 type->subtype, pointer_level);
163 case DW_TAG_pointer_type:
164 case DW_TAG_reference_type:
165 case DW_TAG_rvalue_reference_type:
167 void* addr_pointed1 = mc_snapshot_read_pointer_region(real_area1, region1);
168 void* addr_pointed2 = mc_snapshot_read_pointer_region(real_area2, region2);
170 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
171 return (addr_pointed1 != addr_pointed2);
174 if (addr_pointed1 == NULL && addr_pointed2 == NULL)
176 if (addr_pointed1 == NULL || addr_pointed2 == NULL)
178 if (!add_compared_pointers(state, addr_pointed1, addr_pointed2))
183 // Some cases are not handled here:
184 // * the pointers lead to different areas (one to the heap, the other to the RW segment ...);
185 // * a pointer leads to the read-only segment of the current object;
186 // * a pointer lead to a different ELF object.
188 if (addr_pointed1 > std_heap
189 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)) {
191 (addr_pointed2 > std_heap
192 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)))
194 // The pointers are both in the heap:
195 return compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
196 snapshot2, NULL, type->subtype, pointer_level);
199 // The pointers are both in the current object R/W segment:
200 else if (mc_region_contain(region1, addr_pointed1)) {
201 if (!mc_region_contain(region2, addr_pointed2))
203 if (type->dw_type_id == NULL)
204 return (addr_pointed1 != addr_pointed2);
206 return compare_areas_with_type(state, process_index,
207 addr_pointed1, snapshot1, region1,
208 addr_pointed2, snapshot2, region2,
209 type->subtype, pointer_level);
213 // TODO, We do not handle very well the case where
214 // it belongs to a different (non-heap) region from the current one.
217 return (addr_pointed1 != addr_pointed2);
222 case DW_TAG_structure_type:
223 case DW_TAG_class_type:
224 xbt_dynar_foreach(type->members, cursor, member) {
226 mc_member_resolve(real_area1, type, member, snapshot1, process_index);
228 mc_member_resolve(real_area2, type, member, snapshot2, process_index);
229 mc_mem_region_t subregion1 = mc_get_region_hinted(member1, snapshot1, process_index, region1);
230 mc_mem_region_t subregion2 = mc_get_region_hinted(member2, snapshot2, process_index, region2);
232 compare_areas_with_type(state, process_index,
233 member1, snapshot1, subregion1,
234 member2, snapshot2, subregion2,
235 member->subtype, pointer_level);
240 case DW_TAG_subroutine_type:
244 XBT_VERB("Unknown case : %d", type->type);
251 static int compare_global_variables(mc_object_info_t object_info,
254 mc_mem_region_t r2, mc_snapshot_t snapshot1,
255 mc_snapshot_t snapshot2)
257 xbt_assert(r1 && r2, "Missing region.");
258 struct mc_compare_state state;
260 xbt_dynar_t variables;
262 unsigned int cursor = 0;
263 dw_variable_t current_var;
265 variables = object_info->global_variables;
267 xbt_dynar_foreach(variables, cursor, current_var) {
269 // If the variable is not in this object, skip it:
270 // We do not expect to find a pointer to something which is not reachable
271 // by the global variables.
272 if ((char *) current_var->address < (char *) object_info->start_rw
273 || (char *) current_var->address > (char *) object_info->end_rw)
276 dw_type_t bvariable_type = current_var->type;
278 compare_areas_with_type(state, process_index,
279 (char *) current_var->address, snapshot1, r1,
280 (char *) current_var->address, snapshot2, r2,
283 XBT_VERB("Global variable %s (%p) is different between snapshots",
284 current_var->name, (char *) current_var->address);
294 static int compare_local_variables(int process_index,
295 mc_snapshot_t snapshot1,
296 mc_snapshot_t snapshot2,
297 mc_snapshot_stack_t stack1,
298 mc_snapshot_stack_t stack2)
300 struct mc_compare_state state;
302 if (xbt_dynar_length(stack1->local_variables) !=
303 xbt_dynar_length(stack2->local_variables)) {
304 XBT_VERB("Different number of local variables");
307 unsigned int cursor = 0;
308 local_variable_t current_var1, current_var2;
310 while (cursor < xbt_dynar_length(stack1->local_variables)) {
312 (local_variable_t) xbt_dynar_get_as(stack1->local_variables, cursor,
315 (local_variable_t) xbt_dynar_get_as(stack2->local_variables, cursor,
317 if (strcmp(current_var1->name, current_var2->name) != 0
318 || current_var1->subprogram != current_var1->subprogram
319 || current_var1->ip != current_var2->ip) {
320 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
322 ("Different name of variable (%s - %s) or frame (%s - %s) or ip (%lu - %lu)",
323 current_var1->name, current_var2->name,
324 current_var1->subprogram->name, current_var2->subprogram->name,
325 current_var1->ip, current_var2->ip);
328 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
330 dw_type_t subtype = current_var1->type;
332 compare_areas_with_type(state, process_index,
333 current_var1->address, snapshot1, mc_get_snapshot_region(current_var1->address, snapshot1, process_index),
334 current_var2->address, snapshot2, mc_get_snapshot_region(current_var2->address, snapshot2, process_index),
338 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
340 ("Local variable %s (%p - %p) in frame %s is different between snapshots",
341 current_var1->name, current_var1->address, current_var2->address,
342 current_var1->subprogram->name);
351 int snapshot_compare(void *state1, void *state2)
354 mc_snapshot_t s1, s2;
357 if (_sg_mc_liveness) { /* Liveness MC */
358 s1 = ((mc_visited_pair_t) state1)->graph_state->system_state;
359 s2 = ((mc_visited_pair_t) state2)->graph_state->system_state;
360 num1 = ((mc_visited_pair_t) state1)->num;
361 num2 = ((mc_visited_pair_t) state2)->num;
362 } else { /* Safety or comm determinism MC */
363 s1 = ((mc_visited_state_t) state1)->system_state;
364 s2 = ((mc_visited_state_t) state2)->system_state;
365 num1 = ((mc_visited_state_t) state1)->num;
366 num2 = ((mc_visited_state_t) state2)->num;
372 xbt_os_timer_t global_timer = xbt_os_timer_new();
373 xbt_os_timer_t timer = xbt_os_timer_new();
375 xbt_os_walltimer_start(global_timer);
378 xbt_os_walltimer_start(timer);
383 hash_result = (s1->hash != s2->hash);
385 XBT_VERB("(%d - %d) Different hash : 0x%" PRIx64 "--0x%" PRIx64, num1,
386 num2, s1->hash, s2->hash);
391 XBT_VERB("(%d - %d) Same hash : 0x%" PRIx64, num1, num2, s1->hash);
395 /* Compare enabled processes */
398 xbt_dynar_foreach(s1->enabled_processes, cursor, pid){
399 if(!xbt_dynar_member(s2->enabled_processes, &pid))
400 XBT_VERB("(%d - %d) Different enabled processes", num1, num2);
404 size_t size_used1, size_used2;
407 /* Compare size of stacks */
408 while (i < xbt_dynar_length(s1->stacks)) {
409 size_used1 = s1->stack_sizes[i];
410 size_used2 = s2->stack_sizes[i];
411 if (size_used1 != size_used2) {
414 xbt_os_walltimer_stop(timer);
415 mc_comp_times->stacks_sizes_comparison_time =
416 xbt_os_timer_elapsed(timer);
418 XBT_DEBUG("(%d - %d) Different size used in stacks : %zu - %zu", num1,
419 num2, size_used1, size_used2);
424 XBT_VERB("(%d - %d) Different size used in stacks : %zu - %zu", num1,
425 num2, size_used1, size_used2);
428 xbt_os_walltimer_stop(timer);
429 xbt_os_timer_free(timer);
430 xbt_os_walltimer_stop(global_timer);
431 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
432 xbt_os_timer_free(global_timer);
442 xbt_os_walltimer_stop(timer);
443 xbt_os_walltimer_start(timer);
446 /* Init heap information used in heap comparison algorithm */
447 xbt_mheap_t heap1 = (xbt_mheap_t) mc_snapshot_read(std_heap, s1, MC_NO_PROCESS_INDEX,
448 alloca(sizeof(struct mdesc)), sizeof(struct mdesc));
449 xbt_mheap_t heap2 = (xbt_mheap_t) mc_snapshot_read(std_heap, s2, MC_NO_PROCESS_INDEX,
450 alloca(sizeof(struct mdesc)), sizeof(struct mdesc));
451 res_init = init_heap_information(heap1, heap2, s1->to_ignore, s2->to_ignore);
452 if (res_init == -1) {
454 XBT_DEBUG("(%d - %d) Different heap information", num1, num2);
458 XBT_VERB("(%d - %d) Different heap information", num1, num2);
461 xbt_os_walltimer_stop(global_timer);
462 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
463 xbt_os_timer_free(global_timer);
469 xbt_os_walltimer_start(timer);
472 /* Stacks comparison */
478 mc_snapshot_stack_t stack1, stack2;
479 while (cursor < xbt_dynar_length(s1->stacks)) {
481 (mc_snapshot_stack_t) xbt_dynar_get_as(s1->stacks, cursor,
482 mc_snapshot_stack_t);
484 (mc_snapshot_stack_t) xbt_dynar_get_as(s2->stacks, cursor,
485 mc_snapshot_stack_t);
487 if (stack1->process_index != stack2->process_index) {
489 XBT_DEBUG("(%d - %d) Stacks with different process index (%i vs %i)", num1, num2,
490 stack1->process_index, stack2->process_index);
493 compare_local_variables(stack1->process_index, s1, s2, stack1, stack2);
494 if (diff_local > 0) {
497 xbt_os_walltimer_stop(timer);
498 mc_comp_times->stacks_comparison_time = xbt_os_timer_elapsed(timer);
500 XBT_DEBUG("(%d - %d) Different local variables between stacks %d", num1,
507 XBT_VERB("(%d - %d) Different local variables between stacks %d", num1,
511 reset_heap_information();
512 xbt_os_walltimer_stop(timer);
513 xbt_os_timer_free(timer);
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);
526 const char *names[3] = { "?", "libsimgrid", "binary" };
530 &mc_comp_times->libsimgrid_global_variables_comparison_time,
531 &mc_comp_times->binary_global_variables_comparison_time
535 mc_object_info_t object_infos[] = { NULL, mc_libsimgrid_info, mc_binary_info };
538 for (k = 2; k != 0; --k) {
541 xbt_os_walltimer_stop(timer);
542 xbt_os_walltimer_start(timer);
545 /* Compare global variables */
547 if (object_infos[k] == mc_binary_info && smpi_privatize_global_variables) {
548 // Compare the global variables separately for each simulates process:
549 for (int process_index = 0; process_index < smpi_process_count(); process_index++) {
551 compare_global_variables(object_infos[k], process_index,
552 s1->privatization_regions[process_index], s2->privatization_regions[process_index], s1, s2);
559 compare_global_variables(object_infos[k], MC_NO_PROCESS_INDEX, s1->regions[k], s2->regions[k], s1, s2);
563 xbt_os_walltimer_stop(timer);
564 *times[k] = xbt_os_timer_elapsed(timer);
565 XBT_DEBUG("(%d - %d) Different global variables in %s", num1, num2,
570 XBT_VERB("(%d - %d) Different global variables in %s", num1, num2,
574 reset_heap_information();
575 xbt_os_walltimer_stop(timer);
576 xbt_os_timer_free(timer);
577 xbt_os_walltimer_stop(global_timer);
578 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
579 xbt_os_timer_free(global_timer);
587 xbt_os_walltimer_start(timer);
591 if (mmalloc_compare_heap(s1, s2) > 0) {
594 xbt_os_walltimer_stop(timer);
595 mc_comp_times->heap_comparison_time = xbt_os_timer_elapsed(timer);
596 XBT_DEBUG("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
601 XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
604 reset_heap_information();
605 xbt_os_walltimer_stop(timer);
606 xbt_os_timer_free(timer);
607 xbt_os_walltimer_stop(global_timer);
608 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
609 xbt_os_timer_free(global_timer);
615 xbt_os_walltimer_stop(timer);
619 reset_heap_information();
621 xbt_os_walltimer_stop(timer);
622 xbt_os_timer_free(timer);
625 xbt_os_walltimer_stop(global_timer);
626 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
629 xbt_os_timer_free(global_timer);
632 print_comparison_times();
636 if (errors || hash_result)
637 XBT_VERB("(%d - %d) Difference found", num1, num2);
639 XBT_VERB("(%d - %d) No difference found", num1, num2);
642 #if defined(MC_DEBUG) && defined(MC_VERBOSE)
644 // * false positive SHOULD be avoided.
645 // * There MUST not be any false negative.
647 XBT_VERB("(%d - %d) State equality hash test is %s %s", num1, num2,
648 (hash_result != 0) == (errors != 0) ? "true" : "false",
649 !hash_result ? "positive" : "negative");
653 return errors > 0 || hash_result;
657 /***************************** Statistics *****************************/
658 /*******************************************************************/
660 void print_comparison_times()
662 XBT_DEBUG("*** Comparison times ***");
663 XBT_DEBUG("- Nb processes : %f", mc_comp_times->nb_processes_comparison_time);
664 XBT_DEBUG("- Nb bytes used : %f", mc_comp_times->bytes_used_comparison_time);
665 XBT_DEBUG("- Stacks sizes : %f", mc_comp_times->stacks_sizes_comparison_time);
666 XBT_DEBUG("- Binary global variables : %f",
667 mc_comp_times->binary_global_variables_comparison_time);
668 XBT_DEBUG("- Libsimgrid global variables : %f",
669 mc_comp_times->libsimgrid_global_variables_comparison_time);
670 XBT_DEBUG("- Heap : %f", mc_comp_times->heap_comparison_time);
671 XBT_DEBUG("- Stacks : %f", mc_comp_times->stacks_comparison_time);
674 /**************************** MC snapshot compare simcall **************************/
675 /***********************************************************************************/
677 int SIMIX_pre_mc_compare_snapshots(smx_simcall_t simcall,
678 mc_snapshot_t s1, mc_snapshot_t s2)
680 return snapshot_compare(s1, s2);
683 int MC_compare_snapshots(void *s1, void *s2)
686 return simcall_mc_compare_snapshots(s1, s2);