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. */
8 #include <boost/unordered_set.hpp>
10 #include "mc_private.h"
12 #include "xbt/mmalloc.h"
13 #include "xbt/mmalloc/mmprivate.h"
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_compare, mc,
16 "Logging specific to mc_compare");
18 typedef struct s_pointers_pair {
21 bool operator==(s_pointers_pair const& x) const {
22 return this->p1 == x.p1 && this->p2 == x.p2;
24 bool operator<(s_pointers_pair const& x) const {
25 return this->p1 < x.p1 || (this->p1 == x.p1 && this->p2 < x.p2);
27 } s_pointers_pair_t, *pointers_pair_t;
31 struct hash<s_pointers_pair> {
32 typedef uint64_t result_type;
33 result_type operator()(s_pointers_pair const& x) const {
34 return (result_type) x.p1 ^ (result_type) x.p2 << 8;
39 struct mc_compare_state {
40 boost::unordered_set<s_pointers_pair> compared_pointers;
45 /************************** Free functions ****************************/
46 /********************************************************************/
48 static void stack_region_free(stack_region_t s)
51 xbt_free(s->process_name);
56 static void stack_region_free_voidp(void *s)
58 stack_region_free((stack_region_t) * (void **) s);
61 static void pointers_pair_free(pointers_pair_t p)
66 static void pointers_pair_free_voidp(void *p)
68 pointers_pair_free((pointers_pair_t) * (void **) p);
71 /************************** Snapshot comparison *******************************/
72 /******************************************************************************/
74 /** \brief Try to add a pair a compared pointers to the set of compared pointers
76 * \result !=0 if the pointers were added (they were not in the set),
77 * 0 otherwise (they were already in the set)
79 static int add_compared_pointers(mc_compare_state& state, void *p1, void *p2)
81 s_pointers_pair_t new_pair;
84 return state.compared_pointers.insert(new_pair).second ? 1 : 0;
87 static int compare_areas_with_type(struct mc_compare_state& state,
88 void* real_area1, mc_snapshot_t snapshot1, mc_mem_region_t region1,
89 void* real_area2, mc_snapshot_t snapshot2, mc_mem_region_t region2,
90 dw_type_t type, int pointer_level)
92 unsigned int cursor = 0;
93 dw_type_t member, subtype, subsubtype;
98 case DW_TAG_unspecified_type:
101 case DW_TAG_base_type:
102 case DW_TAG_enumeration_type:
103 case DW_TAG_union_type:
106 mc_snapshot_read_region(real_area1, region1, alloca(type->byte_size), type->byte_size);
108 mc_snapshot_read_region(real_area2, region1, alloca(type->byte_size), type->byte_size);
109 return (memcmp(data1, data2, type->byte_size) != 0);
113 case DW_TAG_volatile_type:
114 case DW_TAG_const_type:
116 type = type->subtype;
118 case DW_TAG_array_type:
119 subtype = type->subtype;
120 switch (subtype->type) {
121 case DW_TAG_unspecified_type:
124 case DW_TAG_base_type:
125 case DW_TAG_enumeration_type:
126 case DW_TAG_pointer_type:
127 case DW_TAG_reference_type:
128 case DW_TAG_rvalue_reference_type:
129 case DW_TAG_structure_type:
130 case DW_TAG_class_type:
131 case DW_TAG_union_type:
132 if (subtype->full_type)
133 subtype = subtype->full_type;
134 elm_size = subtype->byte_size;
136 case DW_TAG_const_type:
138 case DW_TAG_volatile_type:
139 subsubtype = subtype->subtype;
140 if (subsubtype->full_type)
141 subsubtype = subsubtype->full_type;
142 elm_size = subsubtype->byte_size;
148 for (i = 0; i < type->element_count; i++) {
149 size_t off = i * elm_size;
150 res = compare_areas_with_type(state,
151 (char*) real_area1 + off, snapshot1, region1,
152 (char*) real_area2 + off, snapshot2, region2,
153 type->subtype, pointer_level);
158 case DW_TAG_pointer_type:
159 case DW_TAG_reference_type:
160 case DW_TAG_rvalue_reference_type:
163 void* addr_pointed1 = *(void**) mc_snapshot_read_region(real_area1, region1, &temp, sizeof(void**));
164 void* addr_pointed2 = *(void**) mc_snapshot_read_region(real_area2, region2, &temp, sizeof(void**));
166 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
167 return (addr_pointed1 != addr_pointed2);
170 if (addr_pointed1 == NULL && addr_pointed2 == NULL)
172 if (!add_compared_pointers(state, addr_pointed1, addr_pointed2))
177 // Some cases are not handled here:
178 // * the pointers lead to different areas (one to the heap, the other to the RW segment ...);
179 // * a pointer leads to the read-only segment of the current object;
180 // * a pointer lead to a different ELF object.
182 if (addr_pointed1 > std_heap
183 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)) {
185 (addr_pointed2 > std_heap
186 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)))
188 // The pointers are both in the heap:
189 return compare_heap_area(addr_pointed1, addr_pointed2, snapshot1,
190 snapshot2, NULL, type->subtype, pointer_level);
192 // The pointers are both in the current object R/W segment:
193 else if (addr_pointed1 > region1->start_addr
194 && (char *) addr_pointed1 <= (char *) region1->start_addr + region1->size) {
196 (addr_pointed2 > region2->start_addr
197 && (char *) addr_pointed2 <= (char *) region2->start_addr + region2->size))
199 if (type->dw_type_id == NULL)
200 return (addr_pointed1 != addr_pointed2);
202 return compare_areas_with_type(state,
203 addr_pointed1, snapshot1, region1,
204 addr_pointed2, snapshot2, region2,
205 type->subtype, pointer_level);
210 return (addr_pointed1 != addr_pointed2);
215 case DW_TAG_structure_type:
216 case DW_TAG_class_type:
217 xbt_dynar_foreach(type->members, cursor, member) {
219 mc_member_resolve(real_area1, type, member, snapshot1);
221 mc_member_resolve(real_area2, type, member, snapshot2);
222 mc_mem_region_t subregion1 = mc_get_region_hinted(member1, snapshot1, region1);
223 mc_mem_region_t subregion2 = mc_get_region_hinted(member2, snapshot2, region2);
225 compare_areas_with_type(state,
226 member1, snapshot1, subregion1,
227 member2, snapshot2, subregion2,
228 member->subtype, pointer_level);
233 case DW_TAG_subroutine_type:
237 XBT_VERB("Unknown case : %d", type->type);
244 static int compare_global_variables(int region_type, mc_mem_region_t r1,
245 mc_mem_region_t r2, mc_snapshot_t snapshot1,
246 mc_snapshot_t snapshot2)
248 struct mc_compare_state state;
250 xbt_dynar_t variables;
252 unsigned int cursor = 0;
253 dw_variable_t current_var;
255 mc_object_info_t object_info = NULL;
256 if (region_type == 2) {
257 object_info = mc_binary_info;
259 object_info = mc_libsimgrid_info;
261 variables = object_info->global_variables;
263 xbt_dynar_foreach(variables, cursor, current_var) {
265 // If the variable is not in this object, skip it:
266 // We do not expect to find a pointer to something which is not reachable
267 // by the global variables.
268 if ((char *) current_var->address < (char *) object_info->start_rw
269 || (char *) current_var->address > (char *) object_info->end_rw)
272 dw_type_t bvariable_type = current_var->type;
274 compare_areas_with_type(state,
275 (char *) current_var->address, snapshot1, r1,
276 (char *) current_var->address, snapshot2, r2,
279 XBT_VERB("Global variable %s (%p) is different between snapshots",
280 current_var->name, (char *) current_var->address);
290 static int compare_local_variables(mc_snapshot_t snapshot1,
291 mc_snapshot_t snapshot2,
292 mc_snapshot_stack_t stack1,
293 mc_snapshot_stack_t stack2)
295 struct mc_compare_state state;
297 if (xbt_dynar_length(stack1->local_variables) !=
298 xbt_dynar_length(stack2->local_variables)) {
299 XBT_VERB("Different number of local variables");
302 unsigned int cursor = 0;
303 local_variable_t current_var1, current_var2;
305 while (cursor < xbt_dynar_length(stack1->local_variables)) {
307 (local_variable_t) xbt_dynar_get_as(stack1->local_variables, cursor,
310 (local_variable_t) xbt_dynar_get_as(stack2->local_variables, cursor,
312 if (strcmp(current_var1->name, current_var2->name) != 0
313 || current_var1->subprogram != current_var1->subprogram
314 || current_var1->ip != current_var2->ip) {
315 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
317 ("Different name of variable (%s - %s) or frame (%s - %s) or ip (%lu - %lu)",
318 current_var1->name, current_var2->name,
319 current_var1->subprogram->name, current_var2->subprogram->name,
320 current_var1->ip, current_var2->ip);
323 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
325 dw_type_t subtype = current_var1->type;
327 compare_areas_with_type(state,
328 current_var1->address, snapshot1, mc_get_snapshot_region(current_var1->address, snapshot1),
329 current_var2->address, snapshot2, mc_get_snapshot_region(current_var2->address, snapshot2),
333 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
335 ("Local variable %s (%p - %p) in frame %s is different between snapshots",
336 current_var1->name, current_var1->address, current_var2->address,
337 current_var1->subprogram->name);
346 int snapshot_compare(void *state1, void *state2)
349 mc_snapshot_t s1, s2;
352 if (_sg_mc_liveness) { /* Liveness MC */
353 s1 = ((mc_visited_pair_t) state1)->graph_state->system_state;
354 s2 = ((mc_visited_pair_t) state2)->graph_state->system_state;
355 num1 = ((mc_visited_pair_t) state1)->num;
356 num2 = ((mc_visited_pair_t) state2)->num;
357 } else { /* Safety or comm determinism MC */
358 s1 = ((mc_visited_state_t) state1)->system_state;
359 s2 = ((mc_visited_state_t) state2)->system_state;
360 num1 = ((mc_visited_state_t) state1)->num;
361 num2 = ((mc_visited_state_t) state2)->num;
367 xbt_os_timer_t global_timer = xbt_os_timer_new();
368 xbt_os_timer_t timer = xbt_os_timer_new();
370 xbt_os_walltimer_start(global_timer);
373 xbt_os_walltimer_start(timer);
378 hash_result = (s1->hash != s2->hash);
380 XBT_VERB("(%d - %d) Different hash : 0x%" PRIx64 "--0x%" PRIx64, num1,
381 num2, s1->hash, s2->hash);
386 XBT_VERB("(%d - %d) Same hash : 0x%" PRIx64, num1, num2, s1->hash);
390 /* Compare enabled processes */
393 xbt_dynar_foreach(s1->enabled_processes, cursor, pid){
394 if(!xbt_dynar_member(s2->enabled_processes, &pid))
395 XBT_VERB("(%d - %d) Different enabled processes", num1, num2);
399 size_t size_used1, size_used2;
402 /* Compare size of stacks */
403 while (i < xbt_dynar_length(s1->stacks)) {
404 size_used1 = s1->stack_sizes[i];
405 size_used2 = s2->stack_sizes[i];
406 if (size_used1 != size_used2) {
409 xbt_os_walltimer_stop(timer);
410 mc_comp_times->stacks_sizes_comparison_time =
411 xbt_os_timer_elapsed(timer);
413 XBT_DEBUG("(%d - %d) Different size used in stacks : %zu - %zu", num1,
414 num2, size_used1, size_used2);
419 XBT_VERB("(%d - %d) Different size used in stacks : %zu - %zu", num1,
420 num2, size_used1, size_used2);
423 xbt_os_walltimer_stop(timer);
424 xbt_os_timer_free(timer);
425 xbt_os_walltimer_stop(global_timer);
426 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
427 xbt_os_timer_free(global_timer);
437 xbt_os_walltimer_stop(timer);
438 xbt_os_walltimer_start(timer);
441 /* Init heap information used in heap comparison algorithm */
442 xbt_mheap_t heap1 = (xbt_mheap_t) mc_snapshot_read(std_heap, s1,
443 alloca(sizeof(struct mdesc)), sizeof(struct mdesc));
444 xbt_mheap_t heap2 = (xbt_mheap_t) mc_snapshot_read(std_heap, s2,
445 alloca(sizeof(struct mdesc)), sizeof(struct mdesc));
446 res_init = init_heap_information(heap1, heap2, s1->to_ignore, s2->to_ignore);
447 if (res_init == -1) {
449 XBT_DEBUG("(%d - %d) Different heap information", num1, num2);
453 XBT_VERB("(%d - %d) Different heap information", num1, num2);
456 xbt_os_walltimer_stop(global_timer);
457 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
458 xbt_os_timer_free(global_timer);
464 xbt_os_walltimer_start(timer);
467 /* Stacks comparison */
473 mc_snapshot_stack_t stack1, stack2;
475 while (cursor < xbt_dynar_length(s1->stacks)) {
477 (mc_snapshot_stack_t) xbt_dynar_get_as(s1->stacks, cursor,
478 mc_snapshot_stack_t);
480 (mc_snapshot_stack_t) xbt_dynar_get_as(s2->stacks, cursor,
481 mc_snapshot_stack_t);
483 compare_local_variables(s1, s2, stack1, stack2);
484 if (diff_local > 0) {
487 xbt_os_walltimer_stop(timer);
488 mc_comp_times->stacks_comparison_time = xbt_os_timer_elapsed(timer);
490 XBT_DEBUG("(%d - %d) Different local variables between stacks %d", num1,
497 XBT_VERB("(%d - %d) Different local variables between stacks %d", num1,
501 reset_heap_information();
502 xbt_os_walltimer_stop(timer);
503 xbt_os_timer_free(timer);
504 xbt_os_walltimer_stop(global_timer);
505 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
506 xbt_os_timer_free(global_timer);
516 const char *names[3] = { "?", "libsimgrid", "binary" };
520 &mc_comp_times->libsimgrid_global_variables_comparison_time,
521 &mc_comp_times->binary_global_variables_comparison_time
526 for (k = 2; k != 0; --k) {
529 xbt_os_walltimer_stop(timer);
530 xbt_os_walltimer_start(timer);
533 /* Compare global variables */
535 compare_global_variables(k, s1->regions[k], s2->regions[k], s1, s2);
538 xbt_os_walltimer_stop(timer);
539 *times[k] = xbt_os_timer_elapsed(timer);
540 XBT_DEBUG("(%d - %d) Different global variables in %s", num1, num2,
545 XBT_VERB("(%d - %d) Different global variables in %s", num1, num2,
549 reset_heap_information();
550 xbt_os_walltimer_stop(timer);
551 xbt_os_timer_free(timer);
552 xbt_os_walltimer_stop(global_timer);
553 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
554 xbt_os_timer_free(global_timer);
562 xbt_os_walltimer_start(timer);
566 if (mmalloc_compare_heap(s1, s2) > 0) {
569 xbt_os_walltimer_stop(timer);
570 mc_comp_times->heap_comparison_time = xbt_os_timer_elapsed(timer);
571 XBT_DEBUG("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
576 XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
579 reset_heap_information();
580 xbt_os_walltimer_stop(timer);
581 xbt_os_timer_free(timer);
582 xbt_os_walltimer_stop(global_timer);
583 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
584 xbt_os_timer_free(global_timer);
590 xbt_os_walltimer_stop(timer);
594 reset_heap_information();
596 xbt_os_walltimer_stop(timer);
597 xbt_os_timer_free(timer);
600 xbt_os_walltimer_stop(global_timer);
601 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
604 xbt_os_timer_free(global_timer);
607 print_comparison_times();
611 if (errors || hash_result)
612 XBT_VERB("(%d - %d) Difference found", num1, num2);
614 XBT_VERB("(%d - %d) No difference found", num1, num2);
617 #if defined(MC_DEBUG) && defined(MC_VERBOSE)
619 // * false positive SHOULD be avoided.
620 // * There MUST not be any false negative.
622 XBT_VERB("(%d - %d) State equality hash test is %s %s", num1, num2,
623 (hash_result != 0) == (errors != 0) ? "true" : "false",
624 !hash_result ? "positive" : "negative");
628 return errors > 0 || hash_result;
632 /***************************** Statistics *****************************/
633 /*******************************************************************/
635 void print_comparison_times()
637 XBT_DEBUG("*** Comparison times ***");
638 XBT_DEBUG("- Nb processes : %f", mc_comp_times->nb_processes_comparison_time);
639 XBT_DEBUG("- Nb bytes used : %f", mc_comp_times->bytes_used_comparison_time);
640 XBT_DEBUG("- Stacks sizes : %f", mc_comp_times->stacks_sizes_comparison_time);
641 XBT_DEBUG("- Binary global variables : %f",
642 mc_comp_times->binary_global_variables_comparison_time);
643 XBT_DEBUG("- Libsimgrid global variables : %f",
644 mc_comp_times->libsimgrid_global_variables_comparison_time);
645 XBT_DEBUG("- Heap : %f", mc_comp_times->heap_comparison_time);
646 XBT_DEBUG("- Stacks : %f", mc_comp_times->stacks_comparison_time);
649 /**************************** MC snapshot compare simcall **************************/
650 /***********************************************************************************/
652 int SIMIX_pre_mc_compare_snapshots(smx_simcall_t simcall,
653 mc_snapshot_t s1, mc_snapshot_t s2)
655 return snapshot_compare(s1, s2);
658 int MC_compare_snapshots(void *s1, void *s2)
661 return simcall_mc_compare_snapshots(s1, s2);