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_object_info.h"
13 #include "mc_safety.h"
14 #include "mc_liveness.h"
15 #include "mc_private.h"
18 #include "smpi/private.h"
21 #include "xbt/mmalloc.h"
22 #include "xbt/mmalloc/mmprivate.h"
24 #include <xbt/probes.h>
26 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_compare, mc,
27 "Logging specific to mc_compare");
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 /************************** Free functions ****************************/
58 /********************************************************************/
60 static void stack_region_free(stack_region_t s)
63 xbt_free(s->process_name);
68 static void stack_region_free_voidp(void *s)
70 stack_region_free((stack_region_t) * (void **) s);
73 static void pointers_pair_free(pointers_pair_t p)
78 static void pointers_pair_free_voidp(void *p)
80 pointers_pair_free((pointers_pair_t) * (void **) p);
83 /************************** Snapshot comparison *******************************/
84 /******************************************************************************/
86 /** \brief Try to add a pair a compared pointers to the set of compared pointers
88 * \result !=0 if the pointers were added (they were not in the set),
89 * 0 otherwise (they were already in the set)
91 static int add_compared_pointers(mc_compare_state& state, void *p1, void *p2)
93 s_pointers_pair_t new_pair;
96 return state.compared_pointers.insert(new_pair).second ? 1 : 0;
99 static int compare_areas_with_type(struct mc_compare_state& state,
101 void* real_area1, mc_snapshot_t snapshot1, mc_mem_region_t region1,
102 void* real_area2, mc_snapshot_t snapshot2, mc_mem_region_t region2,
103 dw_type_t type, int pointer_level)
105 unsigned int cursor = 0;
106 dw_type_t member, subtype, subsubtype;
107 int elm_size, i, res;
110 switch (type->type) {
111 case DW_TAG_unspecified_type:
114 case DW_TAG_base_type:
115 case DW_TAG_enumeration_type:
116 case DW_TAG_union_type:
118 return mc_snapshot_region_memcmp(
119 real_area1, region1, real_area2, region2,
120 type->byte_size) != 0;
123 case DW_TAG_volatile_type:
124 case DW_TAG_const_type:
126 type = type->subtype;
128 case DW_TAG_array_type:
129 subtype = type->subtype;
130 switch (subtype->type) {
131 case DW_TAG_unspecified_type:
134 case DW_TAG_base_type:
135 case DW_TAG_enumeration_type:
136 case DW_TAG_pointer_type:
137 case DW_TAG_reference_type:
138 case DW_TAG_rvalue_reference_type:
139 case DW_TAG_structure_type:
140 case DW_TAG_class_type:
141 case DW_TAG_union_type:
142 if (subtype->full_type)
143 subtype = subtype->full_type;
144 elm_size = subtype->byte_size;
146 case DW_TAG_const_type:
148 case DW_TAG_volatile_type:
149 subsubtype = subtype->subtype;
150 if (subsubtype->full_type)
151 subsubtype = subsubtype->full_type;
152 elm_size = subsubtype->byte_size;
158 for (i = 0; i < type->element_count; i++) {
159 size_t off = i * elm_size;
160 res = compare_areas_with_type(state, process_index,
161 (char*) real_area1 + off, snapshot1, region1,
162 (char*) real_area2 + off, snapshot2, region2,
163 type->subtype, pointer_level);
168 case DW_TAG_pointer_type:
169 case DW_TAG_reference_type:
170 case DW_TAG_rvalue_reference_type:
172 void* addr_pointed1 = mc_snapshot_read_pointer_region(real_area1, region1);
173 void* addr_pointed2 = mc_snapshot_read_pointer_region(real_area2, region2);
175 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
176 return (addr_pointed1 != addr_pointed2);
179 if (addr_pointed1 == NULL && addr_pointed2 == NULL)
181 if (addr_pointed1 == NULL || addr_pointed2 == NULL)
183 if (!add_compared_pointers(state, addr_pointed1, addr_pointed2))
188 // Some cases are not handled here:
189 // * the pointers lead to different areas (one to the heap, the other to the RW segment ...);
190 // * a pointer leads to the read-only segment of the current object;
191 // * a pointer lead to a different ELF object.
193 if (addr_pointed1 > std_heap
194 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)) {
196 (addr_pointed2 > std_heap
197 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)))
199 // The pointers are both in the heap:
200 return compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
201 snapshot2, NULL, type->subtype, pointer_level);
204 // The pointers are both in the current object R/W segment:
205 else if (mc_region_contain(region1, addr_pointed1)) {
206 if (!mc_region_contain(region2, addr_pointed2))
208 if (type->dw_type_id == NULL)
209 return (addr_pointed1 != addr_pointed2);
211 return compare_areas_with_type(state, process_index,
212 addr_pointed1, snapshot1, region1,
213 addr_pointed2, snapshot2, region2,
214 type->subtype, pointer_level);
218 // TODO, We do not handle very well the case where
219 // it belongs to a different (non-heap) region from the current one.
222 return (addr_pointed1 != addr_pointed2);
227 case DW_TAG_structure_type:
228 case DW_TAG_class_type:
229 xbt_dynar_foreach(type->members, cursor, member) {
231 mc_member_resolve(real_area1, type, member, snapshot1, process_index);
233 mc_member_resolve(real_area2, type, member, snapshot2, process_index);
234 mc_mem_region_t subregion1 = mc_get_region_hinted(member1, snapshot1, process_index, region1);
235 mc_mem_region_t subregion2 = mc_get_region_hinted(member2, snapshot2, process_index, region2);
237 compare_areas_with_type(state, process_index,
238 member1, snapshot1, subregion1,
239 member2, snapshot2, subregion2,
240 member->subtype, pointer_level);
245 case DW_TAG_subroutine_type:
249 XBT_VERB("Unknown case : %d", type->type);
256 static int compare_global_variables(mc_object_info_t object_info,
259 mc_mem_region_t r2, mc_snapshot_t snapshot1,
260 mc_snapshot_t snapshot2)
262 xbt_assert(r1 && r2, "Missing region.");
265 if (r1->storage_type == MC_REGION_STORAGE_TYPE_PRIVATIZED) {
266 xbt_assert(process_index >= 0);
267 if (r2->storage_type != MC_REGION_STORAGE_TYPE_PRIVATIZED) {
271 size_t process_count = smpi_process_count();
272 xbt_assert(process_count == r1->privatized.regions_count
273 && process_count == r2->privatized.regions_count);
275 // Compare the global variables separately for each simulates process:
276 for (size_t process_index = 0; process_index < process_count; process_index++) {
277 int is_diff = compare_global_variables(object_info, process_index,
278 r1->privatized.regions[process_index], r2->privatized.regions[process_index],
279 snapshot1, snapshot2);
280 if (is_diff) return 1;
285 xbt_assert(r1->storage_type != MC_REGION_STORAGE_TYPE_PRIVATIZED);
287 xbt_assert(r2->storage_type != MC_REGION_STORAGE_TYPE_PRIVATIZED);
289 struct mc_compare_state state;
291 xbt_dynar_t variables;
293 unsigned int cursor = 0;
294 dw_variable_t current_var;
296 variables = object_info->global_variables;
298 xbt_dynar_foreach(variables, cursor, current_var) {
300 // If the variable is not in this object, skip it:
301 // We do not expect to find a pointer to something which is not reachable
302 // by the global variables.
303 if ((char *) current_var->address < (char *) object_info->start_rw
304 || (char *) current_var->address > (char *) object_info->end_rw)
307 dw_type_t bvariable_type = current_var->type;
309 compare_areas_with_type(state, process_index,
310 (char *) current_var->address, snapshot1, r1,
311 (char *) current_var->address, snapshot2, r2,
314 XBT_TRACE3(mc, global_diff, -1, -1, current_var->name);
315 XBT_VERB("Global variable %s (%p) is different between snapshots",
316 current_var->name, (char *) current_var->address);
326 static int compare_local_variables(int process_index,
327 mc_snapshot_t snapshot1,
328 mc_snapshot_t snapshot2,
329 mc_snapshot_stack_t stack1,
330 mc_snapshot_stack_t stack2)
332 struct mc_compare_state state;
334 if (xbt_dynar_length(stack1->local_variables) !=
335 xbt_dynar_length(stack2->local_variables)) {
336 XBT_VERB("Different number of local variables");
339 unsigned int cursor = 0;
340 local_variable_t current_var1, current_var2;
342 while (cursor < xbt_dynar_length(stack1->local_variables)) {
344 (local_variable_t) xbt_dynar_get_as(stack1->local_variables, cursor,
347 (local_variable_t) xbt_dynar_get_as(stack2->local_variables, cursor,
349 if (strcmp(current_var1->name, current_var2->name) != 0
350 || current_var1->subprogram != current_var1->subprogram
351 || current_var1->ip != current_var2->ip) {
352 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
354 ("Different name of variable (%s - %s) or frame (%s - %s) or ip (%lu - %lu)",
355 current_var1->name, current_var2->name,
356 current_var1->subprogram->name, current_var2->subprogram->name,
357 current_var1->ip, current_var2->ip);
360 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
362 dw_type_t subtype = current_var1->type;
364 compare_areas_with_type(state, process_index,
365 current_var1->address, snapshot1, mc_get_snapshot_region(current_var1->address, snapshot1, process_index),
366 current_var2->address, snapshot2, mc_get_snapshot_region(current_var2->address, snapshot2, process_index),
370 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
371 XBT_TRACE3(mc, local_diff, -1, -1, current_var1->name);
373 ("Local variable %s (%p - %p) in frame %s is different between snapshots",
374 current_var1->name, current_var1->address, current_var2->address,
375 current_var1->subprogram->name);
384 int snapshot_compare(void *state1, void *state2)
386 mc_snapshot_t s1, s2;
389 if (_sg_mc_liveness) { /* Liveness MC */
390 s1 = ((mc_visited_pair_t) state1)->graph_state->system_state;
391 s2 = ((mc_visited_pair_t) state2)->graph_state->system_state;
392 num1 = ((mc_visited_pair_t) state1)->num;
393 num2 = ((mc_visited_pair_t) state2)->num;
394 } else { /* Safety or comm determinism MC */
395 s1 = ((mc_visited_state_t) state1)->system_state;
396 s2 = ((mc_visited_state_t) state2)->system_state;
397 num1 = ((mc_visited_state_t) state1)->num;
398 num2 = ((mc_visited_state_t) state2)->num;
404 xbt_os_timer_t global_timer = xbt_os_timer_new();
405 xbt_os_timer_t timer = xbt_os_timer_new();
407 xbt_os_walltimer_start(global_timer);
410 xbt_os_walltimer_start(timer);
415 hash_result = (s1->hash != s2->hash);
417 XBT_TRACE2(mc, hash_diff, num1, num2);
418 XBT_VERB("(%d - %d) Different hash : 0x%" PRIx64 "--0x%" PRIx64, num1,
419 num2, s1->hash, s2->hash);
424 XBT_VERB("(%d - %d) Same hash : 0x%" PRIx64, num1, num2, s1->hash);
428 /* Compare enabled processes */
431 xbt_dynar_foreach(s1->enabled_processes, cursor, pid){
432 if(!xbt_dynar_member(s2->enabled_processes, &pid)) {
433 //XBT_TRACE3(mc, state_diff, num1, num2, "Different enabled processes");
434 XBT_VERB("(%d - %d) Different enabled processes", num1, num2);
440 size_t size_used1, size_used2;
443 /* Compare size of stacks */
444 while (i < xbt_dynar_length(s1->stacks)) {
445 size_used1 = s1->stack_sizes[i];
446 size_used2 = s2->stack_sizes[i];
447 if (size_used1 != size_used2) {
450 xbt_os_walltimer_stop(timer);
451 mc_comp_times->stacks_sizes_comparison_time =
452 xbt_os_timer_elapsed(timer);
454 XBT_DEBUG("(%d - %d) Different size used in stacks : %zu - %zu", num1,
455 num2, size_used1, size_used2);
460 XBT_VERB("(%d - %d) Different size used in stacks : %zu - %zu", num1,
461 num2, size_used1, size_used2);
463 XBT_TRACE3(mc, state_diff, num1, num2, "Different stack size");
465 xbt_os_walltimer_stop(timer);
466 xbt_os_timer_free(timer);
467 xbt_os_walltimer_stop(global_timer);
468 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
469 xbt_os_timer_free(global_timer);
479 xbt_os_walltimer_stop(timer);
480 xbt_os_walltimer_start(timer);
483 /* Init heap information used in heap comparison algorithm */
484 xbt_mheap_t heap1 = (xbt_mheap_t) mc_snapshot_read(std_heap, s1, MC_NO_PROCESS_INDEX,
485 alloca(sizeof(struct mdesc)), sizeof(struct mdesc));
486 xbt_mheap_t heap2 = (xbt_mheap_t) mc_snapshot_read(std_heap, s2, MC_NO_PROCESS_INDEX,
487 alloca(sizeof(struct mdesc)), sizeof(struct mdesc));
488 res_init = init_heap_information(heap1, heap2, s1->to_ignore, s2->to_ignore);
489 if (res_init == -1) {
491 XBT_DEBUG("(%d - %d) Different heap information", num1, num2);
495 XBT_TRACE3(mc, state_diff, num1, num2, "Different heap information");
496 XBT_VERB("(%d - %d) Different heap information", num1, num2);
499 xbt_os_walltimer_stop(global_timer);
500 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
501 xbt_os_timer_free(global_timer);
507 xbt_os_walltimer_start(timer);
510 /* Stacks comparison */
516 mc_snapshot_stack_t stack1, stack2;
517 while (cursor < xbt_dynar_length(s1->stacks)) {
519 (mc_snapshot_stack_t) xbt_dynar_get_as(s1->stacks, cursor,
520 mc_snapshot_stack_t);
522 (mc_snapshot_stack_t) xbt_dynar_get_as(s2->stacks, cursor,
523 mc_snapshot_stack_t);
525 if (stack1->process_index != stack2->process_index) {
527 XBT_DEBUG("(%d - %d) Stacks with different process index (%i vs %i)", num1, num2,
528 stack1->process_index, stack2->process_index);
531 compare_local_variables(stack1->process_index, s1, s2, stack1, stack2);
532 if (diff_local > 0) {
533 XBT_TRACE3(mc, state_diff, num1, num2, "Different local variables");
536 xbt_os_walltimer_stop(timer);
537 mc_comp_times->stacks_comparison_time = xbt_os_timer_elapsed(timer);
539 XBT_DEBUG("(%d - %d) Different local variables between stacks %d", num1,
546 XBT_VERB("(%d - %d) Different local variables between stacks %d", num1,
550 reset_heap_information();
551 xbt_os_walltimer_stop(timer);
552 xbt_os_timer_free(timer);
553 xbt_os_walltimer_stop(global_timer);
554 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
555 xbt_os_timer_free(global_timer);
565 const char *names[3] = { "?", "libsimgrid", "binary" };
569 &mc_comp_times->libsimgrid_global_variables_comparison_time,
570 &mc_comp_times->binary_global_variables_comparison_time
574 size_t regions_count = s1->snapshot_regions_count;
575 // TODO, raise a difference instead?
576 xbt_assert(regions_count == s2->snapshot_regions_count);
578 for (size_t k = 0; k != regions_count; ++k) {
579 mc_mem_region_t region1 = s1->snapshot_regions[k];
580 mc_mem_region_t region2 = s2->snapshot_regions[k];
583 if (region1->region_type != MC_REGION_TYPE_DATA)
586 xbt_assert(region1->region_type == region2->region_type);
587 xbt_assert(region1->object_info == region2->object_info);
589 xbt_assert(region1->object_info);
592 xbt_os_walltimer_stop(timer);
593 xbt_os_walltimer_start(timer);
596 /* Compare global variables */
598 compare_global_variables(region1->object_info, MC_NO_PROCESS_INDEX,
603 XBT_TRACE3(mc, state_diff, num1, num2, "Different global variables");
605 xbt_os_walltimer_stop(timer);
606 *times[k] = xbt_os_timer_elapsed(timer);
607 XBT_DEBUG("(%d - %d) Different global variables in %s", num1, num2,
612 XBT_VERB("(%d - %d) Different global variables in %s", num1, num2,
616 reset_heap_information();
617 xbt_os_walltimer_stop(timer);
618 xbt_os_timer_free(timer);
619 xbt_os_walltimer_stop(global_timer);
620 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
621 xbt_os_timer_free(global_timer);
629 xbt_os_walltimer_start(timer);
633 if (mmalloc_compare_heap(s1, s2) > 0) {
634 XBT_TRACE3(mc, state_diff, num1, num2, "Different heap");
637 xbt_os_walltimer_stop(timer);
638 mc_comp_times->heap_comparison_time = xbt_os_timer_elapsed(timer);
639 XBT_DEBUG("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
644 XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
647 reset_heap_information();
648 xbt_os_walltimer_stop(timer);
649 xbt_os_timer_free(timer);
650 xbt_os_walltimer_stop(global_timer);
651 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
652 xbt_os_timer_free(global_timer);
658 xbt_os_walltimer_stop(timer);
662 reset_heap_information();
664 xbt_os_walltimer_stop(timer);
665 xbt_os_timer_free(timer);
668 xbt_os_walltimer_stop(global_timer);
669 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
672 xbt_os_timer_free(global_timer);
675 print_comparison_times();
679 if (errors || hash_result)
680 XBT_VERB("(%d - %d) Difference found", num1, num2);
682 XBT_VERB("(%d - %d) No difference found", num1, num2);
685 #if defined(MC_DEBUG) && defined(MC_VERBOSE)
687 // * false positive SHOULD be avoided.
688 // * There MUST not be any false negative.
690 XBT_VERB("(%d - %d) State equality hash test is %s %s", num1, num2,
691 (hash_result != 0) == (errors != 0) ? "true" : "false",
692 !hash_result ? "positive" : "negative");
696 return errors > 0 || hash_result;
700 /***************************** Statistics *****************************/
701 /*******************************************************************/
703 void print_comparison_times()
705 XBT_DEBUG("*** Comparison times ***");
706 XBT_DEBUG("- Nb processes : %f", mc_comp_times->nb_processes_comparison_time);
707 XBT_DEBUG("- Nb bytes used : %f", mc_comp_times->bytes_used_comparison_time);
708 XBT_DEBUG("- Stacks sizes : %f", mc_comp_times->stacks_sizes_comparison_time);
709 XBT_DEBUG("- Binary global variables : %f",
710 mc_comp_times->binary_global_variables_comparison_time);
711 XBT_DEBUG("- Libsimgrid global variables : %f",
712 mc_comp_times->libsimgrid_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);
717 /**************************** MC snapshot compare simcall **************************/
718 /***********************************************************************************/
720 int simcall_HANDLER_mc_compare_snapshots(smx_simcall_t simcall,
721 mc_snapshot_t s1, mc_snapshot_t s2)
723 return snapshot_compare(s1, s2);
726 int MC_compare_snapshots(void *s1, void *s2)
729 return simcall_mc_compare_snapshots(s1, s2);