1 /* Copyright (c) 2012-2013. 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"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_compare, mc,
14 "Logging specific to mc_compare");
16 typedef struct s_pointers_pair{
19 }s_pointers_pair_t, *pointers_pair_t;
21 __thread xbt_dynar_t compared_pointers;
23 /************************** Free functions ****************************/
24 /********************************************************************/
26 static void stack_region_free(stack_region_t s){
28 xbt_free(s->process_name);
33 static void stack_region_free_voidp(void *s){
34 stack_region_free((stack_region_t) * (void **) s);
37 static void pointers_pair_free(pointers_pair_t p){
41 static void pointers_pair_free_voidp(void *p){
42 pointers_pair_free((pointers_pair_t) * (void **)p);
45 /************************** Snapshot comparison *******************************/
46 /******************************************************************************/
48 static int already_compared_pointers(void *p1, void *p2){
50 if(xbt_dynar_is_empty(compared_pointers))
53 unsigned int cursor = 0;
55 int end = xbt_dynar_length(compared_pointers) - 1;
59 cursor = (start + end) / 2;
60 pair = (pointers_pair_t)xbt_dynar_get_as(compared_pointers, cursor, pointers_pair_t);
64 else if(pair->p2 < p2)
68 }else if(pair->p1 < p1){
79 static void add_compared_pointers(void *p1, void *p2){
81 pointers_pair_t new_pair = xbt_new0(s_pointers_pair_t, 1);
85 if(xbt_dynar_is_empty(compared_pointers)){
86 xbt_dynar_push(compared_pointers, &new_pair);
90 unsigned int cursor = 0;
92 int end = xbt_dynar_length(compared_pointers) - 1;
93 pointers_pair_t pair = NULL;
96 cursor = (start + end) / 2;
97 pair = (pointers_pair_t)xbt_dynar_get_as(compared_pointers, cursor, pointers_pair_t);
100 pointers_pair_free(new_pair);
102 }else if(pair->p2 < p2)
106 }else if(pair->p1 < p1){
115 xbt_dynar_insert_at(compared_pointers, cursor + 1, &new_pair);
117 xbt_dynar_insert_at(compared_pointers, cursor, &new_pair);
120 xbt_dynar_insert_at(compared_pointers, cursor + 1, &new_pair);
122 xbt_dynar_insert_at(compared_pointers, cursor, &new_pair);
127 static int compare_areas_with_type(void *area1, void *area2, mc_object_info_t info, mc_object_info_t other_info, dw_type_t type, int region_size, int region_type, void *start_data, int pointer_level){
129 unsigned int cursor = 0;
130 dw_type_t member, subtype, subsubtype;
131 int elm_size, i, res, switch_types = 0;
132 void *addr_pointed1, *addr_pointed2;
135 case DW_TAG_base_type:
136 case DW_TAG_enumeration_type:
137 case DW_TAG_union_type:
138 return (memcmp(area1, area2, type->byte_size) != 0);
141 case DW_TAG_volatile_type:
142 case DW_TAG_const_type:
143 return compare_areas_with_type(area1, area2, info, other_info, type->subtype, region_size, region_type, start_data, pointer_level);
145 case DW_TAG_array_type:
146 subtype = type->subtype;
147 switch(subtype->type){
148 case DW_TAG_base_type:
149 case DW_TAG_enumeration_type:
150 case DW_TAG_pointer_type:
151 case DW_TAG_structure_type:
152 case DW_TAG_union_type:
153 if(subtype->byte_size == 0){ /*declaration of the type, need the complete description */
154 subtype = xbt_dict_get_or_null(other_info->types, get_type_description(other_info, subtype->name));
157 elm_size = subtype->byte_size;
159 case DW_TAG_const_type:
161 case DW_TAG_volatile_type:
162 subsubtype = subtype->subtype;
163 if(subsubtype->byte_size == 0){ /*declaration of the type, need the complete description */
164 subsubtype = xbt_dict_get_or_null(other_info->types, get_type_description(other_info, subsubtype->name));
167 elm_size = subsubtype->byte_size;
173 for(i=0; i<type->element_count; i++){
175 res = compare_areas_with_type((char *)area1 + (i*elm_size), (char *)area2 + (i*elm_size), other_info, info, type->subtype, region_size, region_type, start_data, pointer_level);
177 res = compare_areas_with_type((char *)area1 + (i*elm_size), (char *)area2 + (i*elm_size), info, other_info, type->subtype, region_size, region_type, start_data, pointer_level);
182 case DW_TAG_pointer_type:
183 if(type->dw_type_id && ((dw_type_t)xbt_dict_get_or_null(info->types, type->dw_type_id))->type == DW_TAG_subroutine_type){
184 addr_pointed1 = *((void **)(area1));
185 addr_pointed2 = *((void **)(area2));
186 return (addr_pointed1 != addr_pointed2);
188 addr_pointed1 = *((void **)(area1));
189 addr_pointed2 = *((void **)(area2));
191 if(addr_pointed1 == NULL && addr_pointed2 == NULL)
193 if(already_compared_pointers(addr_pointed1, addr_pointed2) != -1)
195 add_compared_pointers(addr_pointed1, addr_pointed2);
199 // Some cases are not handled here:
200 // * the pointers lead to different areas (one to the heap, the other to the RW segment ...);
201 // * a pointer leads to the read-only segment of the current object;
202 // * a pointer lead to a different ELF object.
204 // The pointers are both in the heap:
205 if(addr_pointed1 > std_heap && (char *)addr_pointed1 < (char*) std_heap + STD_HEAP_SIZE){
206 if(!(addr_pointed2 > std_heap && (char *)addr_pointed2 < (char*) std_heap + STD_HEAP_SIZE))
208 return compare_heap_area(addr_pointed1, addr_pointed2, NULL, info, other_info, type->dw_type_id, pointer_level);
211 // The pointers are both in the current object R/W segment:
212 else if(addr_pointed1 > start_data && (char*)addr_pointed1 <= (char *)start_data + region_size){
213 if(!(addr_pointed2 > start_data && (char*)addr_pointed2 <= (char *)start_data + region_size))
215 if(type->dw_type_id == NULL)
216 return (addr_pointed1 != addr_pointed2);
218 return compare_areas_with_type(addr_pointed1, addr_pointed2, info, other_info, type->subtype, region_size, region_type, start_data, pointer_level);
222 return (addr_pointed1 != addr_pointed2);
226 case DW_TAG_structure_type:
227 xbt_dynar_foreach(type->members, cursor, member){
228 XBT_DEBUG("Compare member %s", member->name);
229 res = compare_areas_with_type((char *)area1 + member->offset, (char *)area2 + member->offset, info, other_info, member->subtype, region_size, region_type, start_data, pointer_level);
234 case DW_TAG_subroutine_type:
238 XBT_VERB("Unknown case : %d", type->type);
245 static int compare_global_variables(int region_type, mc_mem_region_t r1, mc_mem_region_t r2){
247 if(!compared_pointers){
248 compared_pointers = xbt_dynar_new(sizeof(pointers_pair_t), pointers_pair_free_voidp);
249 MC_ignore_global_variable("compared_pointers");
251 xbt_dynar_reset(compared_pointers);
254 xbt_dynar_t variables;
256 unsigned int cursor = 0;
257 dw_variable_t current_var;
260 void* start_data_binary = mc_binary_info->start_rw;
261 void* start_data_libsimgrid = mc_libsimgrid_info->start_rw;
263 mc_object_info_t object_info = NULL;
264 mc_object_info_t other_object_info = NULL;
265 if(region_type == 2){
266 object_info = mc_binary_info;
267 other_object_info = mc_libsimgrid_info;
268 start_data = start_data_binary;
270 object_info = mc_libsimgrid_info;
271 other_object_info = mc_binary_info;
272 start_data = start_data_libsimgrid;
274 variables = object_info->global_variables;
276 xbt_dynar_foreach(variables, cursor, current_var){
278 // If the variable is not in this object, skip it:
279 // We do not expect to find a pointer to something which is not reachable
280 // by the global variables.
281 if((char*) current_var->address.address < (char*) object_info->start_rw
282 || (char*) current_var->address.address > (char*) object_info->end_rw)
285 offset = (char *)current_var->address.address - (char *)object_info->start_rw;
287 dw_type_t bvariable_type = xbt_dict_get_or_null(object_info->types, current_var->type_origin);
288 res = compare_areas_with_type((char *)r1->data + offset, (char *)r2->data + offset, object_info, other_object_info, bvariable_type, r1->size, region_type, start_data, 0);
290 XBT_VERB("Global variable %s (%p - %p) is different between snapshots", current_var->name, (char *)r1->data + offset, (char *)r2->data + offset);
291 xbt_dynar_free(&compared_pointers);
292 compared_pointers = NULL;
298 xbt_dynar_free(&compared_pointers);
299 compared_pointers = NULL;
305 static int compare_local_variables(mc_snapshot_stack_t stack1, mc_snapshot_stack_t stack2, void *heap1, void *heap2){
306 void* start_data_binary = mc_binary_info->start_rw;
307 void* start_data_libsimgrid = mc_libsimgrid_info->start_rw;
309 if(!compared_pointers){
310 compared_pointers = xbt_dynar_new(sizeof(pointers_pair_t), pointers_pair_free_voidp);
311 MC_ignore_global_variable("compared_pointers");
313 xbt_dynar_reset(compared_pointers);
316 if(xbt_dynar_length(stack1->local_variables) != xbt_dynar_length(stack2->local_variables)){
317 XBT_VERB("Different number of local variables");
318 xbt_dynar_free(&compared_pointers);
319 compared_pointers = NULL;
322 unsigned int cursor = 0;
323 local_variable_t current_var1, current_var2;
324 int offset1, offset2, res;
325 while(cursor < xbt_dynar_length(stack1->local_variables)){
326 current_var1 = (local_variable_t)xbt_dynar_get_as(stack1->local_variables, cursor, local_variable_t);
327 current_var2 = (local_variable_t)xbt_dynar_get_as(stack2->local_variables, cursor, local_variable_t);
328 if(strcmp(current_var1->name, current_var2->name) != 0 || strcmp(current_var1->frame, current_var2->frame) != 0 || current_var1->ip != current_var2->ip){
329 xbt_dynar_free(&compared_pointers);
330 XBT_VERB("Different name of variable (%s - %s) or frame (%s - %s) or ip (%lu - %lu)", current_var1->name, current_var2->name, current_var1->frame, current_var2->frame, current_var1->ip, current_var2->ip);
333 offset1 = (char *)current_var1->address - (char *)std_heap;
334 offset2 = (char *)current_var2->address - (char *)std_heap;
335 XBT_DEBUG("Compare local variable %s of frame %s", current_var1->name, current_var1->frame);
338 if(current_var1->region == 1) {
339 dw_type_t subtype = xbt_dict_get_or_null(mc_libsimgrid_info->types, current_var1->type);
340 res = compare_areas_with_type( (char *)heap1 + offset1, (char *)heap2 + offset2, mc_libsimgrid_info, mc_binary_info, subtype, 0, 1, start_data_libsimgrid, 0);
342 dw_type_t subtype = xbt_dict_get_or_null(mc_binary_info->types, current_var1->type);
343 res = compare_areas_with_type( (char *)heap1 + offset1, (char *)heap2 + offset2, mc_binary_info, mc_libsimgrid_info, subtype, 0, 2, start_data_binary, 0);
346 XBT_VERB("Local variable %s (%p - %p) in frame %s is different between snapshots", current_var1->name,(char *)heap1 + offset1, (char *)heap2 + offset2, current_var1->frame);
347 xbt_dynar_free(&compared_pointers);
348 compared_pointers = NULL;
353 xbt_dynar_free(&compared_pointers);
354 compared_pointers = NULL;
359 int snapshot_compare(void *state1, void *state2){
361 mc_snapshot_t s1, s2;
364 if(_sg_mc_property_file && _sg_mc_property_file[0] != '\0'){ /* Liveness MC */
365 s1 = ((mc_visited_pair_t)state1)->graph_state->system_state;
366 s2 = ((mc_visited_pair_t)state2)->graph_state->system_state;
367 num1 = ((mc_visited_pair_t)state1)->num;
368 num2 = ((mc_visited_pair_t)state2)->num;
369 /* Firstly compare automaton state */
370 /*if(xbt_automaton_state_compare(((mc_pair_t)state1)->automaton_state, ((mc_pair_t)state2)->automaton_state) != 0)
372 if(xbt_automaton_propositional_symbols_compare_value(((mc_pair_t)state1)->atomic_propositions, ((mc_pair_t)state2)->atomic_propositions) != 0)
374 }else{ /* Safety MC */
375 s1 = ((mc_visited_state_t)state1)->system_state;
376 s2 = ((mc_visited_state_t)state2)->system_state;
377 num1 = ((mc_visited_state_t)state1)->num;
378 num2 = ((mc_visited_state_t)state2)->num;
384 xbt_os_timer_t global_timer = xbt_os_timer_new();
385 xbt_os_timer_t timer = xbt_os_timer_new();
387 xbt_os_walltimer_start(global_timer);
390 xbt_os_walltimer_start(timer);
393 if(MC_USE_SNAPSHOT_HASH) {
394 if(s1->hash != s2->hash) {
395 XBT_VERB("(%d - %d) Different hash : 0x%" PRIx64 "--0x%" PRIx64, num1, num2, s1->hash, s2->hash);
398 XBT_VERB("(%d - %d) Same hash : 0x%" PRIx64, num1, num2, s1->hash);
403 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 = xbt_os_timer_elapsed(timer);
417 XBT_DEBUG("(%d - %d) Different size used in stacks : %zu - %zu", num1, num2, size_used1, size_used2);
422 XBT_VERB("(%d - %d) Different size used in stacks : %zu - %zu", num1, num2, size_used1, size_used2);
425 xbt_os_walltimer_stop(timer);
426 xbt_os_timer_free(timer);
427 xbt_os_walltimer_stop(global_timer);
428 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
429 xbt_os_timer_free(global_timer);
439 xbt_os_walltimer_stop(timer);
440 xbt_os_walltimer_start(timer);
443 /* Compare hash of global variables */
444 if(s1->hash_global != NULL && s2->hash_global != NULL){
445 if(strcmp(s1->hash_global, s2->hash_global) != 0){
447 xbt_os_walltimer_stop(timer);
448 mc_comp_times->hash_global_variables_comparison_time = xbt_os_timer_elapsed(timer);
449 XBT_DEBUG("Different hash of global variables : %s - %s", s1->hash_global, s2->hash_global);
453 XBT_VERB("Different hash of global variables : %s - %s", s1->hash_global, s2->hash_global);
456 xbt_os_walltimer_stop(timer);
457 xbt_os_timer_free(timer);
458 xbt_os_walltimer_stop(global_timer);
459 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
460 xbt_os_timer_free(global_timer);
468 xbt_os_walltimer_start(timer);
471 /* Compare hash of local variables */
472 if(s1->hash_local != NULL && s2->hash_local != NULL){
473 if(strcmp(s1->hash_local, s2->hash_local) != 0){
475 xbt_os_walltimer_stop(timer);
476 mc_comp_times->hash_local_variables_comparison_time = xbt_os_timer_elapsed(timer);
477 XBT_DEBUG("Different hash of local variables : %s - %s", s1->hash_local, s2->hash_local);
481 XBT_VERB("Different hash of local variables : %s - %s", s1->hash_local, s2->hash_local);
484 xbt_os_walltimer_stop(timer);
485 xbt_os_timer_free(timer);
486 xbt_os_walltimer_stop(global_timer);
487 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
488 xbt_os_timer_free(global_timer);
496 xbt_os_walltimer_start(timer);
499 /* Init heap information used in heap comparison algorithm */
500 res_init = init_heap_information((xbt_mheap_t)s1->regions[0]->data, (xbt_mheap_t)s2->regions[0]->data, s1->to_ignore, s2->to_ignore);
503 XBT_DEBUG("(%d - %d) Different heap information", num1, num2);
507 XBT_VERB("(%d - %d) Different heap information", num1, num2);
510 xbt_os_walltimer_stop(global_timer);
511 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
512 xbt_os_timer_free(global_timer);
519 xbt_os_walltimer_start(timer);
522 /* Stacks comparison */
523 unsigned int cursor = 0;
526 mc_snapshot_stack_t stack1, stack2;
528 while(cursor < xbt_dynar_length(s1->stacks)){
529 stack1 = (mc_snapshot_stack_t)xbt_dynar_get_as(s1->stacks, cursor, mc_snapshot_stack_t);
530 stack2 = (mc_snapshot_stack_t)xbt_dynar_get_as(s2->stacks, cursor, mc_snapshot_stack_t);
531 diff_local = compare_local_variables(stack1, stack2, s1->regions[0]->data, s2->regions[0]->data);
535 xbt_os_walltimer_stop(timer);
536 mc_comp_times->stacks_comparison_time = xbt_os_timer_elapsed(timer);
538 XBT_DEBUG("(%d - %d) Different local variables between stacks %d", num1, num2, cursor + 1);
544 XBT_VERB("(%d - %d) Different local variables between stacks %d", num1, num2, cursor + 1);
547 reset_heap_information();
548 xbt_os_walltimer_stop(timer);
549 xbt_os_timer_free(timer);
550 xbt_os_walltimer_stop(global_timer);
551 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
552 xbt_os_timer_free(global_timer);
562 const char* names[3] = { "?", "libsimgrid", "binary" };
566 &mc_comp_times->libsimgrid_global_variables_comparison_time,
567 &mc_comp_times->binary_global_variables_comparison_time
572 for(k=2; k!=0; --k) {
575 xbt_os_walltimer_stop(timer);
576 xbt_os_walltimer_start(timer);
579 /* Compare global variables */
580 is_diff = compare_global_variables(k, s1->regions[k], s2->regions[k]);
583 xbt_os_walltimer_stop(timer);
584 *times[k] = xbt_os_timer_elapsed(timer);
585 XBT_DEBUG("(%d - %d) Different global variables in %s", num1, num2, names[k]);
589 XBT_VERB("(%d - %d) Different global variables in %s", num1, num2, names[k]);
592 reset_heap_information();
593 xbt_os_walltimer_stop(timer);
594 xbt_os_timer_free(timer);
595 xbt_os_walltimer_stop(global_timer);
596 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
597 xbt_os_timer_free(global_timer);
605 xbt_os_walltimer_start(timer);
609 if(mmalloc_compare_heap((xbt_mheap_t)s1->regions[0]->data,
610 (xbt_mheap_t)s2->regions[0]->data,
612 mc_binary_info) > 0){
615 xbt_os_walltimer_stop(timer);
616 mc_comp_times->heap_comparison_time = xbt_os_timer_elapsed(timer);
617 XBT_DEBUG("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
622 XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
625 reset_heap_information();
626 xbt_os_walltimer_stop(timer);
627 xbt_os_timer_free(timer);
628 xbt_os_walltimer_stop(global_timer);
629 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
630 xbt_os_timer_free(global_timer);
636 xbt_os_walltimer_stop(timer);
640 reset_heap_information();
642 xbt_os_walltimer_stop(timer);
643 xbt_os_timer_free(timer);
646 xbt_os_walltimer_stop(global_timer);
647 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
650 xbt_os_timer_free(global_timer);
653 print_comparison_times();
658 XBT_VERB("(%d - %d) No difference found", num1, num2);
664 /***************************** Statistics *****************************/
665 /*******************************************************************/
667 void print_comparison_times(){
668 XBT_DEBUG("*** Comparison times ***");
669 XBT_DEBUG("- Nb processes : %f", mc_comp_times->nb_processes_comparison_time);
670 XBT_DEBUG("- Nb bytes used : %f", mc_comp_times->bytes_used_comparison_time);
671 XBT_DEBUG("- Stacks sizes : %f", mc_comp_times->stacks_sizes_comparison_time);
672 XBT_DEBUG("- Binary global variables : %f", mc_comp_times->binary_global_variables_comparison_time);
673 XBT_DEBUG("- Libsimgrid global variables : %f", mc_comp_times->libsimgrid_global_variables_comparison_time);
674 XBT_DEBUG("- Heap : %f", mc_comp_times->heap_comparison_time);
675 XBT_DEBUG("- Stacks : %f", mc_comp_times->stacks_comparison_time);
678 /**************************** MC snapshot compare simcall **************************/
679 /***********************************************************************************/
681 int SIMIX_pre_mc_compare_snapshots(smx_simcall_t simcall,
682 mc_snapshot_t s1, mc_snapshot_t s2){
683 return snapshot_compare(s1, s2);
686 int MC_compare_snapshots(void *s1, void *s2){
688 MC_ignore_local_variable("self", "simcall_BODY_mc_snapshot");
689 return simcall_mc_compare_snapshots(s1, s2);