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. */
9 #include "mc_private.h"
11 #include "xbt/mmalloc.h"
12 #include "xbt/mmalloc/mmprivate.h"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_compare, mc,
15 "Logging specific to mc_compare");
17 typedef struct s_pointers_pair {
20 } s_pointers_pair_t, *pointers_pair_t;
22 __thread xbt_dynar_t compared_pointers;
24 /************************** Free functions ****************************/
25 /********************************************************************/
27 static void stack_region_free(stack_region_t s)
30 xbt_free(s->process_name);
35 static void stack_region_free_voidp(void *s)
37 stack_region_free((stack_region_t) * (void **) s);
40 static void pointers_pair_free(pointers_pair_t p)
45 static void pointers_pair_free_voidp(void *p)
47 pointers_pair_free((pointers_pair_t) * (void **) p);
50 /************************** Snapshot comparison *******************************/
51 /******************************************************************************/
53 /** \brief Try to add a pair a compared pointers to the set of compared pointers
55 * \result !=0 if the pointers were added (they were not in the set),
56 * 0 otherwise (they were already in the set)
58 static int add_compared_pointers(void *p1, void *p2)
61 pointers_pair_t new_pair = xbt_new0(s_pointers_pair_t, 1);
65 if (xbt_dynar_is_empty(compared_pointers)) {
66 xbt_dynar_push(compared_pointers, &new_pair);
70 unsigned int cursor = 0;
72 int end = xbt_dynar_length(compared_pointers) - 1;
73 pointers_pair_t pair = NULL;
76 (pointers_pair_t *) xbt_dynar_get_ptr(compared_pointers, 0);
78 while (start <= end) {
79 cursor = (start + end) / 2;
83 } else if (pair->p1 > p1) {
85 } else if (pair->p2 < p2) {
87 } else if (pair->p2 > p2) {
90 pointers_pair_free(new_pair);
96 xbt_dynar_insert_at(compared_pointers, cursor + 1, &new_pair);
97 else if (pair->p1 > p1)
98 xbt_dynar_insert_at(compared_pointers, cursor, &new_pair);
99 else if (pair->p2 < p2)
100 xbt_dynar_insert_at(compared_pointers, cursor + 1, &new_pair);
101 else if (pair->p2 > p2)
102 xbt_dynar_insert_at(compared_pointers, cursor, &new_pair);
104 xbt_die("Unrecheable");
109 static int compare_areas_with_type(void *area1, void *area2,
110 mc_snapshot_t snapshot1,
111 mc_snapshot_t snapshot2, dw_type_t type,
112 int region_size, int region_type,
113 void *start_data, int pointer_level)
116 unsigned int cursor = 0;
117 dw_type_t member, subtype, subsubtype;
118 int elm_size, i, res;
119 void *addr_pointed1, *addr_pointed2;
121 switch (type->type) {
122 case DW_TAG_unspecified_type:
125 case DW_TAG_base_type:
126 case DW_TAG_enumeration_type:
127 case DW_TAG_union_type:
128 return (memcmp(area1, area2, type->byte_size) != 0);
131 case DW_TAG_volatile_type:
132 case DW_TAG_const_type:
133 return compare_areas_with_type(area1, area2, snapshot1, snapshot2,
134 type->subtype, region_size, region_type,
135 start_data, pointer_level);
137 case DW_TAG_array_type:
138 subtype = type->subtype;
139 switch (subtype->type) {
140 case DW_TAG_unspecified_type:
143 case DW_TAG_base_type:
144 case DW_TAG_enumeration_type:
145 case DW_TAG_pointer_type:
146 case DW_TAG_reference_type:
147 case DW_TAG_rvalue_reference_type:
148 case DW_TAG_structure_type:
149 case DW_TAG_class_type:
150 case DW_TAG_union_type:
151 if (subtype->full_type)
152 subtype = subtype->full_type;
153 elm_size = subtype->byte_size;
155 case DW_TAG_const_type:
157 case DW_TAG_volatile_type:
158 subsubtype = subtype->subtype;
159 if (subsubtype->full_type)
160 subsubtype = subsubtype->full_type;
161 elm_size = subsubtype->byte_size;
167 for (i = 0; i < type->element_count; i++) {
169 compare_areas_with_type((char *) area1 + (i * elm_size),
170 (char *) area2 + (i * elm_size), snapshot1,
171 snapshot2, type->subtype, region_size,
172 region_type, start_data, pointer_level);
177 case DW_TAG_pointer_type:
178 case DW_TAG_reference_type:
179 case DW_TAG_rvalue_reference_type:
181 addr_pointed1 = *((void **) (area1));
182 addr_pointed2 = *((void **) (area2));
184 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
185 return (addr_pointed1 != addr_pointed2);
188 if (addr_pointed1 == NULL && addr_pointed2 == NULL)
190 if (!add_compared_pointers(addr_pointed1, addr_pointed2))
195 // Some cases are not handled here:
196 // * the pointers lead to different areas (one to the heap, the other to the RW segment ...);
197 // * a pointer leads to the read-only segment of the current object;
198 // * a pointer lead to a different ELF object.
200 if (addr_pointed1 > std_heap
201 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)) {
203 (addr_pointed2 > std_heap
204 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)))
206 // The pointers are both in the heap:
207 return compare_heap_area(addr_pointed1, addr_pointed2, snapshot1,
208 snapshot2, NULL, type->subtype, pointer_level);
210 // The pointers are both in the current object R/W segment:
211 else if (addr_pointed1 > start_data
212 && (char *) addr_pointed1 <= (char *) start_data + region_size) {
214 (addr_pointed2 > start_data
215 && (char *) addr_pointed2 <= (char *) start_data + region_size))
217 if (type->dw_type_id == NULL)
218 return (addr_pointed1 != addr_pointed2);
220 void *translated_addr_pointer1 =
221 mc_translate_address((uintptr_t) addr_pointed1, snapshot1);
222 void *translated_addr_pointer2 =
223 mc_translate_address((uintptr_t) addr_pointed2, snapshot2);
224 return compare_areas_with_type(translated_addr_pointer1,
225 translated_addr_pointer2, snapshot1,
226 snapshot2, type->subtype, region_size,
227 region_type, start_data,
233 return (addr_pointed1 != addr_pointed2);
237 case DW_TAG_structure_type:
238 case DW_TAG_class_type:
239 xbt_dynar_foreach(type->members, cursor, member) {
241 mc_member_snapshot_resolve(area1, type, member, snapshot1);
243 mc_member_snapshot_resolve(area2, type, member, snapshot2);
245 compare_areas_with_type(member1, member2, snapshot1, snapshot2,
246 member->subtype, region_size, region_type,
247 start_data, pointer_level);
252 case DW_TAG_subroutine_type:
256 XBT_VERB("Unknown case : %d", type->type);
263 static int compare_global_variables(int region_type, mc_mem_region_t r1,
264 mc_mem_region_t r2, mc_snapshot_t snapshot1,
265 mc_snapshot_t snapshot2)
268 if (!compared_pointers) {
270 xbt_dynar_new(sizeof(pointers_pair_t), pointers_pair_free_voidp);
272 xbt_dynar_reset(compared_pointers);
275 xbt_dynar_t variables;
277 unsigned int cursor = 0;
278 dw_variable_t current_var;
281 void *start_data_binary = mc_binary_info->start_rw;
282 void *start_data_libsimgrid = mc_libsimgrid_info->start_rw;
284 mc_object_info_t object_info = NULL;
285 if (region_type == 2) {
286 object_info = mc_binary_info;
287 start_data = start_data_binary;
289 object_info = mc_libsimgrid_info;
290 start_data = start_data_libsimgrid;
292 variables = object_info->global_variables;
294 xbt_dynar_foreach(variables, cursor, current_var) {
296 // If the variable is not in this object, skip it:
297 // We do not expect to find a pointer to something which is not reachable
298 // by the global variables.
299 if ((char *) current_var->address < (char *) object_info->start_rw
300 || (char *) current_var->address > (char *) object_info->end_rw)
303 offset = (char *) current_var->address - (char *) object_info->start_rw;
305 dw_type_t bvariable_type = current_var->type;
307 compare_areas_with_type((char *) r1->data + offset,
308 (char *) r2->data + offset, snapshot1,
309 snapshot2, bvariable_type, r1->size,
310 region_type, start_data, 0);
312 XBT_VERB("Global variable %s (%p - %p) is different between snapshots",
313 current_var->name, (char *) r1->data + offset,
314 (char *) r2->data + offset);
315 xbt_dynar_free(&compared_pointers);
316 compared_pointers = NULL;
322 xbt_dynar_free(&compared_pointers);
323 compared_pointers = NULL;
329 static int compare_local_variables(mc_snapshot_t snapshot1,
330 mc_snapshot_t snapshot2,
331 mc_snapshot_stack_t stack1,
332 mc_snapshot_stack_t stack2, void *heap1,
335 void *start_data_binary = mc_binary_info->start_rw;
336 void *start_data_libsimgrid = mc_libsimgrid_info->start_rw;
338 if (!compared_pointers) {
340 xbt_dynar_new(sizeof(pointers_pair_t), pointers_pair_free_voidp);
342 xbt_dynar_reset(compared_pointers);
345 if (xbt_dynar_length(stack1->local_variables) !=
346 xbt_dynar_length(stack2->local_variables)) {
347 XBT_VERB("Different number of local variables");
348 xbt_dynar_free(&compared_pointers);
349 compared_pointers = NULL;
352 unsigned int cursor = 0;
353 local_variable_t current_var1, current_var2;
354 int offset1, offset2, res;
355 while (cursor < xbt_dynar_length(stack1->local_variables)) {
357 (local_variable_t) xbt_dynar_get_as(stack1->local_variables, cursor,
360 (local_variable_t) xbt_dynar_get_as(stack2->local_variables, cursor,
362 if (strcmp(current_var1->name, current_var2->name) != 0
363 || current_var1->subprogram != current_var1->subprogram
364 || current_var1->ip != current_var2->ip) {
365 xbt_dynar_free(&compared_pointers);
366 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
368 ("Different name of variable (%s - %s) or frame (%s - %s) or ip (%lu - %lu)",
369 current_var1->name, current_var2->name,
370 current_var1->subprogram->name, current_var2->subprogram->name,
371 current_var1->ip, current_var2->ip);
374 offset1 = (char *) current_var1->address - (char *) std_heap;
375 offset2 = (char *) current_var2->address - (char *) std_heap;
376 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
378 if (current_var1->region == 1) {
379 dw_type_t subtype = current_var1->type;
381 compare_areas_with_type((char *) heap1 + offset1,
382 (char *) heap2 + offset2, snapshot1,
383 snapshot2, subtype, 0, 1,
384 start_data_libsimgrid, 0);
386 dw_type_t subtype = current_var2->type;
388 compare_areas_with_type((char *) heap1 + offset1,
389 (char *) heap2 + offset2, snapshot1,
390 snapshot2, subtype, 0, 2, start_data_binary,
394 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
396 ("Local variable %s (%p - %p) in frame %s is different between snapshots",
397 current_var1->name, (char *) heap1 + offset1,
398 (char *) heap2 + offset2, current_var1->subprogram->name);
399 xbt_dynar_free(&compared_pointers);
400 compared_pointers = NULL;
405 xbt_dynar_free(&compared_pointers);
406 compared_pointers = NULL;
411 int snapshot_compare(void *state1, void *state2)
414 mc_snapshot_t s1, s2;
417 if (_sg_mc_property_file && _sg_mc_property_file[0] != '\0') { /* Liveness MC */
418 s1 = ((mc_visited_pair_t) state1)->graph_state->system_state;
419 s2 = ((mc_visited_pair_t) state2)->graph_state->system_state;
420 num1 = ((mc_visited_pair_t) state1)->num;
421 num2 = ((mc_visited_pair_t) state2)->num;
422 /* Firstly compare automaton state */
423 /*if(xbt_automaton_state_compare(((mc_pair_t)state1)->automaton_state, ((mc_pair_t)state2)->automaton_state) != 0)
425 if(xbt_automaton_propositional_symbols_compare_value(((mc_pair_t)state1)->atomic_propositions, ((mc_pair_t)state2)->atomic_propositions) != 0)
427 } else { /* Safety MC */
428 s1 = ((mc_visited_state_t) state1)->system_state;
429 s2 = ((mc_visited_state_t) state2)->system_state;
430 num1 = ((mc_visited_state_t) state1)->num;
431 num2 = ((mc_visited_state_t) state2)->num;
437 xbt_os_timer_t global_timer = xbt_os_timer_new();
438 xbt_os_timer_t timer = xbt_os_timer_new();
440 xbt_os_walltimer_start(global_timer);
443 xbt_os_walltimer_start(timer);
448 hash_result = (s1->hash != s2->hash);
450 XBT_VERB("(%d - %d) Different hash : 0x%" PRIx64 "--0x%" PRIx64, num1,
451 num2, s1->hash, s2->hash);
456 XBT_VERB("(%d - %d) Same hash : 0x%" PRIx64, num1, num2, s1->hash);
460 /* Compare enabled processes */
463 xbt_dynar_foreach(s1->enabled_processes, cursor, pid){
464 if(!xbt_dynar_member(s2->enabled_processes, &pid))
465 XBT_VERB("(%d - %d) Different enabled processes", num1, num2);
469 size_t size_used1, size_used2;
472 /* Compare size of stacks */
473 while (i < xbt_dynar_length(s1->stacks)) {
474 size_used1 = s1->stack_sizes[i];
475 size_used2 = s2->stack_sizes[i];
476 if (size_used1 != size_used2) {
479 xbt_os_walltimer_stop(timer);
480 mc_comp_times->stacks_sizes_comparison_time =
481 xbt_os_timer_elapsed(timer);
483 XBT_DEBUG("(%d - %d) Different size used in stacks : %zu - %zu", num1,
484 num2, size_used1, size_used2);
489 XBT_VERB("(%d - %d) Different size used in stacks : %zu - %zu", num1,
490 num2, size_used1, size_used2);
493 xbt_os_walltimer_stop(timer);
494 xbt_os_timer_free(timer);
495 xbt_os_walltimer_stop(global_timer);
496 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
497 xbt_os_timer_free(global_timer);
507 xbt_os_walltimer_stop(timer);
508 xbt_os_walltimer_start(timer);
511 /* Init heap information used in heap comparison algorithm */
513 init_heap_information((xbt_mheap_t) s1->regions[0]->data,
514 (xbt_mheap_t) s2->regions[0]->data, s1->to_ignore,
516 if (res_init == -1) {
518 XBT_DEBUG("(%d - %d) Different heap information", num1, num2);
522 XBT_VERB("(%d - %d) Different heap information", num1, num2);
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);
533 xbt_os_walltimer_start(timer);
536 /* Stacks comparison */
542 mc_snapshot_stack_t stack1, stack2;
544 while (cursor < xbt_dynar_length(s1->stacks)) {
546 (mc_snapshot_stack_t) xbt_dynar_get_as(s1->stacks, cursor,
547 mc_snapshot_stack_t);
549 (mc_snapshot_stack_t) xbt_dynar_get_as(s2->stacks, cursor,
550 mc_snapshot_stack_t);
552 compare_local_variables(s1, s2, stack1, stack2, s1->regions[0]->data,
553 s2->regions[0]->data);
554 if (diff_local > 0) {
557 xbt_os_walltimer_stop(timer);
558 mc_comp_times->stacks_comparison_time = xbt_os_timer_elapsed(timer);
560 XBT_DEBUG("(%d - %d) Different local variables between stacks %d", num1,
567 XBT_VERB("(%d - %d) Different local variables between stacks %d", num1,
571 reset_heap_information();
572 xbt_os_walltimer_stop(timer);
573 xbt_os_timer_free(timer);
574 xbt_os_walltimer_stop(global_timer);
575 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
576 xbt_os_timer_free(global_timer);
586 const char *names[3] = { "?", "libsimgrid", "binary" };
590 &mc_comp_times->libsimgrid_global_variables_comparison_time,
591 &mc_comp_times->binary_global_variables_comparison_time
596 for (k = 2; k != 0; --k) {
599 xbt_os_walltimer_stop(timer);
600 xbt_os_walltimer_start(timer);
603 /* Compare global variables */
605 compare_global_variables(k, s1->regions[k], s2->regions[k], s1, s2);
608 xbt_os_walltimer_stop(timer);
609 *times[k] = xbt_os_timer_elapsed(timer);
610 XBT_DEBUG("(%d - %d) Different global variables in %s", num1, num2,
615 XBT_VERB("(%d - %d) Different global variables in %s", num1, num2,
619 reset_heap_information();
620 xbt_os_walltimer_stop(timer);
621 xbt_os_timer_free(timer);
622 xbt_os_walltimer_stop(global_timer);
623 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
624 xbt_os_timer_free(global_timer);
632 xbt_os_walltimer_start(timer);
636 if (mmalloc_compare_heap(s1, s2, (xbt_mheap_t) s1->regions[0]->data,
637 (xbt_mheap_t) s2->regions[0]->data) > 0) {
640 xbt_os_walltimer_stop(timer);
641 mc_comp_times->heap_comparison_time = xbt_os_timer_elapsed(timer);
642 XBT_DEBUG("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
647 XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
650 reset_heap_information();
651 xbt_os_walltimer_stop(timer);
652 xbt_os_timer_free(timer);
653 xbt_os_walltimer_stop(global_timer);
654 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
655 xbt_os_timer_free(global_timer);
661 xbt_os_walltimer_stop(timer);
665 reset_heap_information();
667 xbt_os_walltimer_stop(timer);
668 xbt_os_timer_free(timer);
671 xbt_os_walltimer_stop(global_timer);
672 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
675 xbt_os_timer_free(global_timer);
678 print_comparison_times();
682 if (errors || hash_result)
683 XBT_VERB("(%d - %d) Difference found", num1, num2);
685 XBT_VERB("(%d - %d) No difference found", num1, num2);
688 #if defined(MC_DEBUG) && defined(MC_VERBOSE)
690 // * false positive SHOULD be avoided.
691 // * There MUST not be any false negative.
693 XBT_VERB("(%d - %d) State equality hash test is %s %s", num1, num2,
694 (hash_result != 0) == (errors != 0) ? "true" : "false",
695 !hash_result ? "positive" : "negative");
699 return errors > 0 || hash_result;
703 /***************************** Statistics *****************************/
704 /*******************************************************************/
706 void print_comparison_times()
708 XBT_DEBUG("*** Comparison times ***");
709 XBT_DEBUG("- Nb processes : %f", mc_comp_times->nb_processes_comparison_time);
710 XBT_DEBUG("- Nb bytes used : %f", mc_comp_times->bytes_used_comparison_time);
711 XBT_DEBUG("- Stacks sizes : %f", mc_comp_times->stacks_sizes_comparison_time);
712 XBT_DEBUG("- Binary global variables : %f",
713 mc_comp_times->binary_global_variables_comparison_time);
714 XBT_DEBUG("- Libsimgrid global variables : %f",
715 mc_comp_times->libsimgrid_global_variables_comparison_time);
716 XBT_DEBUG("- Heap : %f", mc_comp_times->heap_comparison_time);
717 XBT_DEBUG("- Stacks : %f", mc_comp_times->stacks_comparison_time);
720 /**************************** MC snapshot compare simcall **************************/
721 /***********************************************************************************/
723 int SIMIX_pre_mc_compare_snapshots(smx_simcall_t simcall,
724 mc_snapshot_t s1, mc_snapshot_t s2)
726 return snapshot_compare(s1, s2);
729 int MC_compare_snapshots(void *s1, void *s2)
732 return simcall_mc_compare_snapshots(s1, s2);