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. */
7 #include "mc_private.h"
9 #include "xbt/mmalloc.h"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_compare, mc,
12 "Logging specific to mc_compare");
14 typedef struct s_pointers_pair{
17 }s_pointers_pair_t, *pointers_pair_t;
19 __thread xbt_dynar_t compared_pointers;
21 /************************** Free functions ****************************/
22 /********************************************************************/
24 static void stack_region_free(stack_region_t s){
26 xbt_free(s->process_name);
31 static void stack_region_free_voidp(void *s){
32 stack_region_free((stack_region_t) * (void **) s);
35 static void pointers_pair_free(pointers_pair_t p){
39 static void pointers_pair_free_voidp(void *p){
40 pointers_pair_free((pointers_pair_t) * (void **)p);
43 /************************** Snapshot comparison *******************************/
44 /******************************************************************************/
46 static int already_compared_pointers(void *p1, void *p2){
48 if(xbt_dynar_is_empty(compared_pointers))
51 unsigned int cursor = 0;
53 int end = xbt_dynar_length(compared_pointers) - 1;
57 cursor = (start + end) / 2;
58 pair = (pointers_pair_t)xbt_dynar_get_as(compared_pointers, cursor, pointers_pair_t);
62 else if(pair->p2 < p2)
66 }else if(pair->p1 < p1){
77 static void add_compared_pointers(void *p1, void *p2){
79 pointers_pair_t new_pair = xbt_new0(s_pointers_pair_t, 1);
83 if(xbt_dynar_is_empty(compared_pointers)){
84 xbt_dynar_push(compared_pointers, &new_pair);
88 unsigned int cursor = 0;
90 int end = xbt_dynar_length(compared_pointers) - 1;
91 pointers_pair_t pair = NULL;
94 cursor = (start + end) / 2;
95 pair = (pointers_pair_t)xbt_dynar_get_as(compared_pointers, cursor, pointers_pair_t);
98 pointers_pair_free(new_pair);
100 }else if(pair->p2 < p2)
104 }else if(pair->p1 < p1){
113 xbt_dynar_insert_at(compared_pointers, cursor + 1, &new_pair);
115 xbt_dynar_insert_at(compared_pointers, cursor, &new_pair);
118 xbt_dynar_insert_at(compared_pointers, cursor + 1, &new_pair);
120 xbt_dynar_insert_at(compared_pointers, cursor, &new_pair);
125 static int compare_areas_with_type(void *area1, void *area2, xbt_dict_t types, xbt_dict_t other_types, char *type_id, int region_size, int region_type, void *start_data, int pointer_level){
127 dw_type_t type = xbt_dict_get_or_null(types, type_id);;
128 unsigned int cursor = 0;
129 dw_type_t member, subtype, subsubtype;
130 int elm_size, i, res, switch_types = 0;
131 void *addr_pointed1, *addr_pointed2;
134 case DW_TAG_base_type:
135 case DW_TAG_enumeration_type:
136 case DW_TAG_union_type:
137 return (memcmp(area1, area2, type->byte_size) != 0);
140 case DW_TAG_volatile_type:
141 case DW_TAG_const_type:
142 return compare_areas_with_type(area1, area2, types, other_types, type->dw_type_id, region_size, region_type, start_data, pointer_level);
144 case DW_TAG_array_type:
145 subtype = xbt_dict_get_or_null(types, type->dw_type_id);
146 switch(subtype->type){
147 case DW_TAG_base_type:
148 case DW_TAG_enumeration_type:
149 case DW_TAG_pointer_type:
150 case DW_TAG_structure_type:
151 case DW_TAG_union_type:
152 if(subtype->byte_size == 0){ /*declaration of the type, need the complete description */
153 subtype = xbt_dict_get_or_null(types, get_type_description(types, subtype->name));
155 subtype = xbt_dict_get_or_null(other_types, get_type_description(other_types, subtype->name));
159 elm_size = subtype->byte_size;
161 case DW_TAG_const_type:
163 case DW_TAG_volatile_type:
164 subsubtype = xbt_dict_get_or_null(types, subtype->dw_type_id);
165 if(subsubtype->byte_size == 0){ /*declaration of the type, need the complete description */
166 subsubtype = xbt_dict_get_or_null(types, get_type_description(types, subsubtype->name));
167 if(subsubtype == NULL){
168 subsubtype = xbt_dict_get_or_null(other_types, get_type_description(other_types, subsubtype->name));
172 elm_size = subsubtype->byte_size;
178 for(i=0; i<type->element_count; i++){
180 res = compare_areas_with_type((char *)area1 + (i*elm_size), (char *)area2 + (i*elm_size), other_types, types, type->dw_type_id, region_size, region_type, start_data, pointer_level);
182 res = compare_areas_with_type((char *)area1 + (i*elm_size), (char *)area2 + (i*elm_size), types, other_types, type->dw_type_id, region_size, region_type, start_data, pointer_level);
187 case DW_TAG_pointer_type:
188 if(type->dw_type_id && ((dw_type_t)xbt_dict_get_or_null(types, type->dw_type_id))->type == DW_TAG_subroutine_type){
189 addr_pointed1 = *((void **)(area1));
190 addr_pointed2 = *((void **)(area2));
191 return (addr_pointed1 != addr_pointed2);
193 addr_pointed1 = *((void **)(area1));
194 addr_pointed2 = *((void **)(area2));
196 if(addr_pointed1 == NULL && addr_pointed2 == NULL)
198 if(already_compared_pointers(addr_pointed1, addr_pointed2) != -1)
200 add_compared_pointers(addr_pointed1, addr_pointed2);
204 // Some cases are not handled here:
205 // * the pointers lead to different areas (one to the heap, the other to the RW segment ...);
206 // * a pointer leads to the read-only segment of the current object;
207 // * a pointer lead to a different ELF object.
209 // The pointers are both in the heap:
210 if(addr_pointed1 > std_heap && (char *)addr_pointed1 < (char*) std_heap + STD_HEAP_SIZE){
211 if(!(addr_pointed2 > std_heap && (char *)addr_pointed2 < (char*) std_heap + STD_HEAP_SIZE))
213 return compare_heap_area(addr_pointed1, addr_pointed2, NULL, types, other_types, type->dw_type_id, pointer_level);
216 // The pointers are both in the current object R/W segment:
217 else if(addr_pointed1 > start_data && (char*)addr_pointed1 <= (char *)start_data + region_size){
218 if(!(addr_pointed2 > start_data && (char*)addr_pointed2 <= (char *)start_data + region_size))
220 if(type->dw_type_id == NULL)
221 return (addr_pointed1 != addr_pointed2);
223 return compare_areas_with_type(addr_pointed1, addr_pointed2, types, other_types, type->dw_type_id, region_size, region_type, start_data, pointer_level);
227 return (addr_pointed1 != addr_pointed2);
231 case DW_TAG_structure_type:
232 xbt_dynar_foreach(type->members, cursor, member){
233 res = compare_areas_with_type((char *)area1 + member->offset, (char *)area2 + member->offset, types, other_types, member->dw_type_id, region_size, region_type, start_data, pointer_level);
238 case DW_TAG_subroutine_type:
242 XBT_VERB("Unknown case : %d", type->type);
249 static int compare_global_variables(int region_type, mc_mem_region_t r1, mc_mem_region_t r2){
251 if(!compared_pointers){
252 compared_pointers = xbt_dynar_new(sizeof(pointers_pair_t), pointers_pair_free_voidp);
253 MC_ignore_global_variable("compared_pointers");
255 xbt_dynar_reset(compared_pointers);
258 xbt_dynar_t variables;
259 xbt_dict_t types, other_types;
261 unsigned int cursor = 0;
262 dw_variable_t current_var;
265 void* start_data_binary = mc_binary_info->start_rw;
266 void* start_data_libsimgrid = mc_libsimgrid_info->start_rw;
268 mc_object_info_t object_info = NULL;
269 mc_object_info_t other_object_info = NULL;
270 if(region_type == 2){
271 object_info = mc_binary_info;
272 other_object_info = mc_libsimgrid_info;
273 start_data = start_data_binary;
275 object_info = mc_libsimgrid_info;
276 other_object_info = mc_binary_info;
277 start_data = start_data_libsimgrid;
279 variables = object_info->global_variables;
280 types = object_info->types;
281 other_types = other_object_info->types;
283 xbt_dynar_foreach(variables, cursor, current_var){
285 // If the variable is not in this object, skip it:
286 // We do not expect to find a pointer to something which is not reachable
287 // by the global variables.
288 if((char*) current_var->address < (char*) object_info->start_rw
289 || (char*) current_var->address > (char*) object_info->end_rw)
292 offset = (char *)current_var->address - (char *)object_info->start_rw;
294 res = compare_areas_with_type((char *)r1->data + offset, (char *)r2->data + offset, types, other_types, current_var->type_origin, r1->size, region_type, start_data, 0);
296 XBT_VERB("Global variable %s (%p - %p) is different between snapshots", current_var->name, (char *)r1->data + offset, (char *)r2->data + offset);
297 xbt_dynar_free(&compared_pointers);
298 compared_pointers = NULL;
304 xbt_dynar_free(&compared_pointers);
305 compared_pointers = NULL;
311 static int compare_local_variables(mc_snapshot_stack_t stack1, mc_snapshot_stack_t stack2, void *heap1, void *heap2){
312 void* start_data_binary = mc_binary_info->start_rw;
313 void* start_data_libsimgrid = mc_libsimgrid_info->start_rw;
315 if(!compared_pointers){
316 compared_pointers = xbt_dynar_new(sizeof(pointers_pair_t), pointers_pair_free_voidp);
317 MC_ignore_global_variable("compared_pointers");
319 xbt_dynar_reset(compared_pointers);
322 if(xbt_dynar_length(stack1->local_variables) != xbt_dynar_length(stack2->local_variables)){
323 XBT_VERB("Different number of local variables");
324 xbt_dynar_free(&compared_pointers);
325 compared_pointers = NULL;
328 unsigned int cursor = 0;
329 local_variable_t current_var1, current_var2;
330 int offset1, offset2, res;
331 while(cursor < xbt_dynar_length(stack1->local_variables)){
332 current_var1 = (local_variable_t)xbt_dynar_get_as(stack1->local_variables, cursor, local_variable_t);
333 current_var2 = (local_variable_t)xbt_dynar_get_as(stack2->local_variables, cursor, local_variable_t);
334 if(strcmp(current_var1->name, current_var2->name) != 0 || strcmp(current_var1->frame, current_var2->frame) != 0 || current_var1->ip != current_var2->ip){
335 xbt_dynar_free(&compared_pointers);
336 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);
339 offset1 = (char *)current_var1->address - (char *)std_heap;
340 offset2 = (char *)current_var2->address - (char *)std_heap;
341 if(current_var1->region == 1)
342 res = compare_areas_with_type( (char *)heap1 + offset1, (char *)heap2 + offset2, mc_libsimgrid_info->types, mc_binary_info->types, current_var1->type, 0, 1, start_data_libsimgrid, 0);
344 res = compare_areas_with_type( (char *)heap1 + offset1, (char *)heap2 + offset2, mc_binary_info->types, mc_libsimgrid_info->types, current_var1->type, 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 /* Compare size of stacks */
395 size_t size_used1, size_used2;
397 while(i < xbt_dynar_length(s1->stacks)){
398 size_used1 = s1->stack_sizes[i];
399 size_used2 = s2->stack_sizes[i];
400 if(size_used1 != size_used2){
403 xbt_os_walltimer_stop(timer);
404 mc_comp_times->stacks_sizes_comparison_time = xbt_os_timer_elapsed(timer);
406 XBT_DEBUG("(%d - %d) Different size used in stacks : %zu - %zu", num1, num2, size_used1, size_used2);
411 XBT_VERB("(%d - %d) Different size used in stacks : %zu - %zu", num1, num2, size_used1, size_used2);
414 xbt_os_walltimer_stop(timer);
415 xbt_os_timer_free(timer);
416 xbt_os_walltimer_stop(global_timer);
417 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
418 xbt_os_timer_free(global_timer);
428 xbt_os_walltimer_stop(timer);
429 xbt_os_walltimer_start(timer);
432 /* Compare hash of global variables */
433 if(s1->hash_global != NULL && s2->hash_global != NULL){
434 if(strcmp(s1->hash_global, s2->hash_global) != 0){
436 xbt_os_walltimer_stop(timer);
437 mc_comp_times->hash_global_variables_comparison_time = xbt_os_timer_elapsed(timer);
438 XBT_DEBUG("Different hash of global variables : %s - %s", s1->hash_global, s2->hash_global);
442 XBT_VERB("Different hash of global variables : %s - %s", s1->hash_global, s2->hash_global);
445 xbt_os_walltimer_stop(timer);
446 xbt_os_timer_free(timer);
447 xbt_os_walltimer_stop(global_timer);
448 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
449 xbt_os_timer_free(global_timer);
457 xbt_os_walltimer_start(timer);
460 /* Compare hash of local variables */
461 if(s1->hash_local != NULL && s2->hash_local != NULL){
462 if(strcmp(s1->hash_local, s2->hash_local) != 0){
464 xbt_os_walltimer_stop(timer);
465 mc_comp_times->hash_local_variables_comparison_time = xbt_os_timer_elapsed(timer);
466 XBT_DEBUG("Different hash of local variables : %s - %s", s1->hash_local, s2->hash_local);
470 XBT_VERB("Different hash of local variables : %s - %s", s1->hash_local, s2->hash_local);
473 xbt_os_walltimer_stop(timer);
474 xbt_os_timer_free(timer);
475 xbt_os_walltimer_stop(global_timer);
476 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
477 xbt_os_timer_free(global_timer);
485 xbt_os_walltimer_start(timer);
488 /* Init heap information used in heap comparison algorithm */
489 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);
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);
508 xbt_os_walltimer_start(timer);
511 /* Stacks comparison */
512 unsigned int cursor = 0;
515 mc_snapshot_stack_t stack1, stack2;
517 while(cursor < xbt_dynar_length(s1->stacks)){
518 stack1 = (mc_snapshot_stack_t)xbt_dynar_get_as(s1->stacks, cursor, mc_snapshot_stack_t);
519 stack2 = (mc_snapshot_stack_t)xbt_dynar_get_as(s2->stacks, cursor, mc_snapshot_stack_t);
520 diff_local = compare_local_variables(stack1, stack2, s1->regions[0]->data, s2->regions[0]->data);
524 xbt_os_walltimer_stop(timer);
525 mc_comp_times->stacks_comparison_time = xbt_os_timer_elapsed(timer);
527 XBT_DEBUG("(%d - %d) Different local variables between stacks %d", num1, num2, cursor + 1);
533 XBT_VERB("(%d - %d) Different local variables between stacks %d", num1, num2, cursor + 1);
536 reset_heap_information();
537 xbt_os_walltimer_stop(timer);
538 xbt_os_timer_free(timer);
539 xbt_os_walltimer_stop(global_timer);
540 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
541 xbt_os_timer_free(global_timer);
551 const char* names[3] = { "?", "libsimgrid", "binary" };
555 &mc_comp_times->libsimgrid_global_variables_comparison_time,
556 &mc_comp_times->binary_global_variables_comparison_time
561 for(k=2; k!=0; --k) {
564 xbt_os_walltimer_stop(timer);
565 xbt_os_walltimer_start(timer);
568 /* Compare global variables */
569 is_diff = compare_global_variables(k, s1->regions[k], s2->regions[k]);
572 xbt_os_walltimer_stop(timer);
573 *times[k] = xbt_os_timer_elapsed(timer);
574 XBT_DEBUG("(%d - %d) Different global variables in %s", num1, num2, names[k]);
578 XBT_VERB("(%d - %d) Different global variables in %s", num1, num2, names[k]);
581 reset_heap_information();
582 xbt_os_walltimer_stop(timer);
583 xbt_os_timer_free(timer);
584 xbt_os_walltimer_stop(global_timer);
585 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
586 xbt_os_timer_free(global_timer);
594 xbt_os_walltimer_start(timer);
598 if(mmalloc_compare_heap((xbt_mheap_t)s1->regions[0]->data,
599 (xbt_mheap_t)s2->regions[0]->data,
600 mc_libsimgrid_info->types,
601 mc_binary_info->types) > 0){
604 xbt_os_walltimer_stop(timer);
605 mc_comp_times->heap_comparison_time = xbt_os_timer_elapsed(timer);
606 XBT_DEBUG("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
611 XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
614 reset_heap_information();
615 xbt_os_walltimer_stop(timer);
616 xbt_os_timer_free(timer);
617 xbt_os_walltimer_stop(global_timer);
618 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
619 xbt_os_timer_free(global_timer);
625 xbt_os_walltimer_stop(timer);
629 reset_heap_information();
631 xbt_os_walltimer_stop(timer);
632 xbt_os_timer_free(timer);
635 xbt_os_walltimer_stop(global_timer);
636 mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
639 xbt_os_timer_free(global_timer);
642 print_comparison_times();
649 /***************************** Statistics *****************************/
650 /*******************************************************************/
652 void print_comparison_times(){
653 XBT_DEBUG("*** Comparison times ***");
654 XBT_DEBUG("- Nb processes : %f", mc_comp_times->nb_processes_comparison_time);
655 XBT_DEBUG("- Nb bytes used : %f", mc_comp_times->bytes_used_comparison_time);
656 XBT_DEBUG("- Stacks sizes : %f", mc_comp_times->stacks_sizes_comparison_time);
657 XBT_DEBUG("- Binary global variables : %f", mc_comp_times->binary_global_variables_comparison_time);
658 XBT_DEBUG("- Libsimgrid global variables : %f", mc_comp_times->libsimgrid_global_variables_comparison_time);
659 XBT_DEBUG("- Heap : %f", mc_comp_times->heap_comparison_time);
660 XBT_DEBUG("- Stacks : %f", mc_comp_times->stacks_comparison_time);
663 /**************************** MC snapshot compare simcall **************************/
664 /***********************************************************************************/
666 int SIMIX_pre_mc_compare_snapshots(smx_simcall_t simcall,
667 mc_snapshot_t s1, mc_snapshot_t s2){
668 return snapshot_compare(s1, s2);
671 int MC_compare_snapshots(void *s1, void *s2){
673 MC_ignore_local_variable("self", "simcall_BODY_mc_snapshot");
674 return simcall_mc_compare_snapshots(s1, s2);