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* real_area1, mc_snapshot_t snapshot1, mc_mem_region_t region1,
110 void* real_area2, mc_snapshot_t snapshot2, mc_mem_region_t region2,
111 dw_type_t type, int pointer_level)
113 unsigned int cursor = 0;
114 dw_type_t member, subtype, subsubtype;
115 int elm_size, i, res;
118 switch (type->type) {
119 case DW_TAG_unspecified_type:
122 case DW_TAG_base_type:
123 case DW_TAG_enumeration_type:
124 case DW_TAG_union_type:
127 mc_snapshot_read_region(real_area1, region1, alloca(type->byte_size), type->byte_size);
129 mc_snapshot_read_region(real_area2, region1, alloca(type->byte_size), type->byte_size);
130 return (memcmp(data1, data2, type->byte_size) != 0);
134 case DW_TAG_volatile_type:
135 case DW_TAG_const_type:
137 type = type->subtype;
139 case DW_TAG_array_type:
140 subtype = type->subtype;
141 switch (subtype->type) {
142 case DW_TAG_unspecified_type:
145 case DW_TAG_base_type:
146 case DW_TAG_enumeration_type:
147 case DW_TAG_pointer_type:
148 case DW_TAG_reference_type:
149 case DW_TAG_rvalue_reference_type:
150 case DW_TAG_structure_type:
151 case DW_TAG_class_type:
152 case DW_TAG_union_type:
153 if (subtype->full_type)
154 subtype = subtype->full_type;
155 elm_size = subtype->byte_size;
157 case DW_TAG_const_type:
159 case DW_TAG_volatile_type:
160 subsubtype = subtype->subtype;
161 if (subsubtype->full_type)
162 subsubtype = subsubtype->full_type;
163 elm_size = subsubtype->byte_size;
169 for (i = 0; i < type->element_count; i++) {
170 size_t off = i * elm_size;
171 res = compare_areas_with_type(
172 (char*) real_area1 + off, snapshot1, region1,
173 (char*) real_area2 + off, snapshot2, region2,
174 type->subtype, pointer_level);
179 case DW_TAG_pointer_type:
180 case DW_TAG_reference_type:
181 case DW_TAG_rvalue_reference_type:
184 void* addr_pointed1 = *(void**) mc_snapshot_read_region(real_area1, region1, &temp, sizeof(void**));
185 void* addr_pointed2 = *(void**) mc_snapshot_read_region(real_area2, region2, &temp, sizeof(void**));
187 if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
188 return (addr_pointed1 != addr_pointed2);
191 if (addr_pointed1 == NULL && addr_pointed2 == NULL)
193 if (!add_compared_pointers(addr_pointed1, addr_pointed2))
198 // Some cases are not handled here:
199 // * the pointers lead to different areas (one to the heap, the other to the RW segment ...);
200 // * a pointer leads to the read-only segment of the current object;
201 // * a pointer lead to a different ELF object.
203 if (addr_pointed1 > std_heap
204 && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)) {
206 (addr_pointed2 > std_heap
207 && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)))
209 // The pointers are both in the heap:
210 return compare_heap_area(addr_pointed1, addr_pointed2, snapshot1,
211 snapshot2, NULL, type->subtype, pointer_level);
213 // The pointers are both in the current object R/W segment:
214 else if (addr_pointed1 > region1->start_addr
215 && (char *) addr_pointed1 <= (char *) region1->start_addr + region1->size) {
217 (addr_pointed2 > region2->start_addr
218 && (char *) addr_pointed2 <= (char *) region2->start_addr + region2->size))
220 if (type->dw_type_id == NULL)
221 return (addr_pointed1 != addr_pointed2);
223 return compare_areas_with_type(addr_pointed1, snapshot1, region1,
224 addr_pointed2, snapshot2, region2,
225 type->subtype, pointer_level);
230 return (addr_pointed1 != addr_pointed2);
235 case DW_TAG_structure_type:
236 case DW_TAG_class_type:
237 xbt_dynar_foreach(type->members, cursor, member) {
239 mc_member_resolve(real_area1, type, member, snapshot1);
241 mc_member_resolve(real_area2, type, member, snapshot2);
242 mc_mem_region_t subregion1 = mc_get_region_hinted(member1, snapshot1, region1);
243 mc_mem_region_t subregion2 = mc_get_region_hinted(member2, snapshot2, region2);
245 compare_areas_with_type(member1, snapshot1, subregion1,
246 member2, snapshot2, subregion2,
247 member->subtype, 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;
280 mc_object_info_t object_info = NULL;
281 if (region_type == 2) {
282 object_info = mc_binary_info;
284 object_info = mc_libsimgrid_info;
286 variables = object_info->global_variables;
288 xbt_dynar_foreach(variables, cursor, current_var) {
290 // If the variable is not in this object, skip it:
291 // We do not expect to find a pointer to something which is not reachable
292 // by the global variables.
293 if ((char *) current_var->address < (char *) object_info->start_rw
294 || (char *) current_var->address > (char *) object_info->end_rw)
297 dw_type_t bvariable_type = current_var->type;
299 compare_areas_with_type((char *) current_var->address, snapshot1, r1,
300 (char *) current_var->address, snapshot2, r2,
303 XBT_VERB("Global variable %s (%p) is different between snapshots",
304 current_var->name, (char *) current_var->address);
305 xbt_dynar_free(&compared_pointers);
306 compared_pointers = NULL;
312 xbt_dynar_free(&compared_pointers);
313 compared_pointers = NULL;
319 static int compare_local_variables(mc_snapshot_t snapshot1,
320 mc_snapshot_t snapshot2,
321 mc_snapshot_stack_t stack1,
322 mc_snapshot_stack_t stack2, void *heap1,
325 if (!compared_pointers) {
327 xbt_dynar_new(sizeof(pointers_pair_t), pointers_pair_free_voidp);
329 xbt_dynar_reset(compared_pointers);
332 if (xbt_dynar_length(stack1->local_variables) !=
333 xbt_dynar_length(stack2->local_variables)) {
334 XBT_VERB("Different number of local variables");
335 xbt_dynar_free(&compared_pointers);
336 compared_pointers = NULL;
339 unsigned int cursor = 0;
340 local_variable_t current_var1, current_var2;
341 int offset1, offset2, res;
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 xbt_dynar_free(&compared_pointers);
353 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
355 ("Different name of variable (%s - %s) or frame (%s - %s) or ip (%lu - %lu)",
356 current_var1->name, current_var2->name,
357 current_var1->subprogram->name, current_var2->subprogram->name,
358 current_var1->ip, current_var2->ip);
361 offset1 = (char *) current_var1->address - (char *) std_heap;
362 offset2 = (char *) current_var2->address - (char *) std_heap;
363 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
366 dw_type_t subtype = current_var1->type;
368 compare_areas_with_type(current_var1->address, snapshot1, mc_get_snapshot_region(current_var1->address, snapshot1),
369 current_var2->address, snapshot2, mc_get_snapshot_region(current_var2->address, snapshot2),
373 // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
375 ("Local variable %s (%p - %p) in frame %s is different between snapshots",
376 current_var1->name, (char *) heap1 + offset1,
377 (char *) heap2 + offset2, current_var1->subprogram->name);
378 xbt_dynar_free(&compared_pointers);
379 compared_pointers = NULL;
384 xbt_dynar_free(&compared_pointers);
385 compared_pointers = NULL;
390 int snapshot_compare(void *state1, void *state2)
393 mc_snapshot_t s1, s2;
396 if (_sg_mc_liveness) { /* Liveness MC */
397 s1 = ((mc_visited_pair_t) state1)->graph_state->system_state;
398 s2 = ((mc_visited_pair_t) state2)->graph_state->system_state;
399 num1 = ((mc_visited_pair_t) state1)->num;
400 num2 = ((mc_visited_pair_t) state2)->num;
401 } else { /* Safety or comm determinism MC */
402 s1 = ((mc_visited_state_t) state1)->system_state;
403 s2 = ((mc_visited_state_t) state2)->system_state;
404 num1 = ((mc_visited_state_t) state1)->num;
405 num2 = ((mc_visited_state_t) state2)->num;
411 xbt_os_timer_t global_timer = xbt_os_timer_new();
412 xbt_os_timer_t timer = xbt_os_timer_new();
414 xbt_os_walltimer_start(global_timer);
417 xbt_os_walltimer_start(timer);
422 hash_result = (s1->hash != s2->hash);
424 XBT_VERB("(%d - %d) Different hash : 0x%" PRIx64 "--0x%" PRIx64, num1,
425 num2, s1->hash, s2->hash);
430 XBT_VERB("(%d - %d) Same hash : 0x%" PRIx64, num1, num2, s1->hash);
434 /* Compare enabled processes */
437 xbt_dynar_foreach(s1->enabled_processes, cursor, pid){
438 if(!xbt_dynar_member(s2->enabled_processes, &pid))
439 XBT_VERB("(%d - %d) Different enabled processes", num1, num2);
443 size_t size_used1, size_used2;
446 /* Compare size of stacks */
447 while (i < xbt_dynar_length(s1->stacks)) {
448 size_used1 = s1->stack_sizes[i];
449 size_used2 = s2->stack_sizes[i];
450 if (size_used1 != size_used2) {
453 xbt_os_walltimer_stop(timer);
454 mc_comp_times->stacks_sizes_comparison_time =
455 xbt_os_timer_elapsed(timer);
457 XBT_DEBUG("(%d - %d) Different size used in stacks : %zu - %zu", num1,
458 num2, size_used1, size_used2);
463 XBT_VERB("(%d - %d) Different size used in stacks : %zu - %zu", num1,
464 num2, size_used1, size_used2);
467 xbt_os_walltimer_stop(timer);
468 xbt_os_timer_free(timer);
469 xbt_os_walltimer_stop(global_timer);
470 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
471 xbt_os_timer_free(global_timer);
481 xbt_os_walltimer_stop(timer);
482 xbt_os_walltimer_start(timer);
485 /* Init heap information used in heap comparison algorithm */
487 init_heap_information((xbt_mheap_t) s1->regions[0]->data,
488 (xbt_mheap_t) s2->regions[0]->data, s1->to_ignore,
490 if (res_init == -1) {
492 XBT_DEBUG("(%d - %d) Different heap information", num1, num2);
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;
518 while (cursor < xbt_dynar_length(s1->stacks)) {
520 (mc_snapshot_stack_t) xbt_dynar_get_as(s1->stacks, cursor,
521 mc_snapshot_stack_t);
523 (mc_snapshot_stack_t) xbt_dynar_get_as(s2->stacks, cursor,
524 mc_snapshot_stack_t);
526 compare_local_variables(s1, s2, stack1, stack2, s1->regions[0]->data,
527 s2->regions[0]->data);
528 if (diff_local > 0) {
531 xbt_os_walltimer_stop(timer);
532 mc_comp_times->stacks_comparison_time = xbt_os_timer_elapsed(timer);
534 XBT_DEBUG("(%d - %d) Different local variables between stacks %d", num1,
541 XBT_VERB("(%d - %d) Different local variables between stacks %d", num1,
545 reset_heap_information();
546 xbt_os_walltimer_stop(timer);
547 xbt_os_timer_free(timer);
548 xbt_os_walltimer_stop(global_timer);
549 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
550 xbt_os_timer_free(global_timer);
560 const char *names[3] = { "?", "libsimgrid", "binary" };
564 &mc_comp_times->libsimgrid_global_variables_comparison_time,
565 &mc_comp_times->binary_global_variables_comparison_time
570 for (k = 2; k != 0; --k) {
573 xbt_os_walltimer_stop(timer);
574 xbt_os_walltimer_start(timer);
577 /* Compare global variables */
579 compare_global_variables(k, s1->regions[k], s2->regions[k], s1, s2);
582 xbt_os_walltimer_stop(timer);
583 *times[k] = xbt_os_timer_elapsed(timer);
584 XBT_DEBUG("(%d - %d) Different global variables in %s", num1, num2,
589 XBT_VERB("(%d - %d) Different global variables in %s", num1, num2,
593 reset_heap_information();
594 xbt_os_walltimer_stop(timer);
595 xbt_os_timer_free(timer);
596 xbt_os_walltimer_stop(global_timer);
597 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
598 xbt_os_timer_free(global_timer);
606 xbt_os_walltimer_start(timer);
610 if (mmalloc_compare_heap(s1, s2) > 0) {
613 xbt_os_walltimer_stop(timer);
614 mc_comp_times->heap_comparison_time = xbt_os_timer_elapsed(timer);
615 XBT_DEBUG("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
620 XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
623 reset_heap_information();
624 xbt_os_walltimer_stop(timer);
625 xbt_os_timer_free(timer);
626 xbt_os_walltimer_stop(global_timer);
627 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
628 xbt_os_timer_free(global_timer);
634 xbt_os_walltimer_stop(timer);
638 reset_heap_information();
640 xbt_os_walltimer_stop(timer);
641 xbt_os_timer_free(timer);
644 xbt_os_walltimer_stop(global_timer);
645 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
648 xbt_os_timer_free(global_timer);
651 print_comparison_times();
655 if (errors || hash_result)
656 XBT_VERB("(%d - %d) Difference found", num1, num2);
658 XBT_VERB("(%d - %d) No difference found", num1, num2);
661 #if defined(MC_DEBUG) && defined(MC_VERBOSE)
663 // * false positive SHOULD be avoided.
664 // * There MUST not be any false negative.
666 XBT_VERB("(%d - %d) State equality hash test is %s %s", num1, num2,
667 (hash_result != 0) == (errors != 0) ? "true" : "false",
668 !hash_result ? "positive" : "negative");
672 return errors > 0 || hash_result;
676 /***************************** Statistics *****************************/
677 /*******************************************************************/
679 void print_comparison_times()
681 XBT_DEBUG("*** Comparison times ***");
682 XBT_DEBUG("- Nb processes : %f", mc_comp_times->nb_processes_comparison_time);
683 XBT_DEBUG("- Nb bytes used : %f", mc_comp_times->bytes_used_comparison_time);
684 XBT_DEBUG("- Stacks sizes : %f", mc_comp_times->stacks_sizes_comparison_time);
685 XBT_DEBUG("- Binary global variables : %f",
686 mc_comp_times->binary_global_variables_comparison_time);
687 XBT_DEBUG("- Libsimgrid global variables : %f",
688 mc_comp_times->libsimgrid_global_variables_comparison_time);
689 XBT_DEBUG("- Heap : %f", mc_comp_times->heap_comparison_time);
690 XBT_DEBUG("- Stacks : %f", mc_comp_times->stacks_comparison_time);
693 /**************************** MC snapshot compare simcall **************************/
694 /***********************************************************************************/
696 int SIMIX_pre_mc_compare_snapshots(smx_simcall_t simcall,
697 mc_snapshot_t s1, mc_snapshot_t s2)
699 return snapshot_compare(s1, s2);
702 int MC_compare_snapshots(void *s1, void *s2)
705 return simcall_mc_compare_snapshots(s1, s2);