1 /* Copyright (c) 2008-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. */
14 #include "simgrid/sg_config.h"
15 #include "../surf/surf_private.h"
16 #include "../simix/smx_private.h"
17 #include "../xbt/mmalloc/mmprivate.h"
19 #include "mc_private.h"
20 #include "xbt/automaton.h"
23 XBT_LOG_NEW_CATEGORY(mc, "All MC categories");
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
25 "Logging specific to MC (global)");
27 /* Configuration support */
28 e_mc_reduce_t mc_reduce_kind=e_mc_reduce_unset;
30 int _sg_do_model_check = 0;
31 int _sg_mc_checkpoint=0;
32 char* _sg_mc_property_file=NULL;
35 int _sg_mc_max_depth=1000;
37 char *_sg_mc_dot_output_file = NULL;
38 int _sg_mc_comms_determinism=0;
39 int _sg_mc_send_determinism=0;
41 int user_max_depth_reached = 0;
43 void _mc_cfg_cb_reduce(const char *name, int pos) {
44 if (_sg_cfg_init_status && !_sg_do_model_check) {
45 xbt_die("You are specifying a reduction strategy after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
47 char *val= xbt_cfg_get_string(_sg_cfg_set, name);
48 if (!strcasecmp(val,"none")) {
49 mc_reduce_kind = e_mc_reduce_none;
50 } else if (!strcasecmp(val,"dpor")) {
51 mc_reduce_kind = e_mc_reduce_dpor;
53 xbt_die("configuration option %s can only take 'none' or 'dpor' as a value",name);
57 void _mc_cfg_cb_checkpoint(const char *name, int pos) {
58 if (_sg_cfg_init_status && !_sg_do_model_check) {
59 xbt_die("You are specifying a checkpointing value after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
61 _sg_mc_checkpoint = xbt_cfg_get_int(_sg_cfg_set, name);
63 void _mc_cfg_cb_property(const char *name, int pos) {
64 if (_sg_cfg_init_status && !_sg_do_model_check) {
65 xbt_die("You are specifying a property after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
67 _sg_mc_property_file= xbt_cfg_get_string(_sg_cfg_set, name);
70 void _mc_cfg_cb_timeout(const char *name, int pos) {
71 if (_sg_cfg_init_status && !_sg_do_model_check) {
72 xbt_die("You are specifying a value to enable/disable timeout for wait requests after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
74 _sg_mc_timeout= xbt_cfg_get_boolean(_sg_cfg_set, name);
77 void _mc_cfg_cb_hash(const char *name, int pos) {
78 if (_sg_cfg_init_status && !_sg_do_model_check) {
79 xbt_die("You are specifying a value to enable/disable the use of global hash to speedup state comparaison, but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
81 _sg_mc_hash= xbt_cfg_get_boolean(_sg_cfg_set, name);
84 void _mc_cfg_cb_max_depth(const char *name, int pos) {
85 if (_sg_cfg_init_status && !_sg_do_model_check) {
86 xbt_die("You are specifying a max depth value after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
88 _sg_mc_max_depth= xbt_cfg_get_int(_sg_cfg_set, name);
91 void _mc_cfg_cb_visited(const char *name, int pos) {
92 if (_sg_cfg_init_status && !_sg_do_model_check) {
93 xbt_die("You are specifying a number of stored visited states after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
95 _sg_mc_visited= xbt_cfg_get_int(_sg_cfg_set, name);
98 void _mc_cfg_cb_dot_output(const char *name, int pos) {
99 if (_sg_cfg_init_status && !_sg_do_model_check) {
100 xbt_die("You are specifying a file name for a dot output of graph state after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
102 _sg_mc_dot_output_file= xbt_cfg_get_string(_sg_cfg_set, name);
105 void _mc_cfg_cb_comms_determinism(const char *name, int pos) {
106 if (_sg_cfg_init_status && !_sg_do_model_check) {
107 xbt_die("You are specifying a value to enable/disable the detection of determinism in the communications schemes after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
109 _sg_mc_comms_determinism= xbt_cfg_get_boolean(_sg_cfg_set, name);
112 void _mc_cfg_cb_send_determinism(const char *name, int pos) {
113 if (_sg_cfg_init_status && !_sg_do_model_check) {
114 xbt_die("You are specifying a value to enable/disable the detection of send-determinism in the communications schemes after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
116 _sg_mc_send_determinism= xbt_cfg_get_boolean(_sg_cfg_set, name);
119 /* MC global data structures */
120 mc_state_t mc_current_state = NULL;
121 char mc_replay_mode = FALSE;
122 double *mc_time = NULL;
123 __thread mc_comparison_times_t mc_comp_times = NULL;
124 __thread double mc_snapshot_comparison_time;
125 mc_stats_t mc_stats = NULL;
128 xbt_fifo_t mc_stack_safety = NULL;
129 mc_global_t initial_state_safety = NULL;
132 xbt_fifo_t mc_stack_liveness = NULL;
133 mc_global_t initial_state_liveness = NULL;
136 xbt_automaton_t _mc_property_automaton = NULL;
139 mc_object_info_t mc_libsimgrid_info = NULL;
140 mc_object_info_t mc_binary_info = NULL;
142 mc_object_info_t mc_object_infos[2] = { NULL, NULL };
143 size_t mc_object_infos_size = 2;
145 /* Ignore mechanism */
146 extern xbt_dynar_t mc_heap_comparison_ignore;
147 extern xbt_dynar_t stacks_areas;
150 FILE *dot_output = NULL;
151 const char* colors[13];
154 /******************************* DWARF Information *******************************/
155 /**********************************************************************************/
157 /************************** Free functions *************************/
159 void mc_frame_free(dw_frame_t frame){
160 xbt_free(frame->name);
161 mc_dwarf_location_list_clear(&(frame->frame_base));
162 xbt_dynar_free(&(frame->variables));
163 xbt_dynar_free(&(frame->scopes));
167 void dw_type_free(dw_type_t t){
169 xbt_free(t->dw_type_id);
170 xbt_dynar_free(&(t->members));
171 mc_dwarf_expression_clear(&t->location);
175 void dw_variable_free(dw_variable_t v){
178 xbt_free(v->type_origin);
180 if(v->locations.locations)
181 mc_dwarf_location_list_clear(&v->locations);
186 void dw_variable_free_voidp(void *t){
187 dw_variable_free((dw_variable_t) * (void **) t);
194 mc_object_info_t MC_new_object_info(void) {
195 mc_object_info_t res = xbt_new0(s_mc_object_info_t, 1);
196 res->subprograms = xbt_dict_new_homogeneous((void (*)(void*))mc_frame_free);
197 res->global_variables = xbt_dynar_new(sizeof(dw_variable_t), dw_variable_free_voidp);
198 res->types = xbt_dict_new_homogeneous((void (*)(void*))dw_type_free);
199 res->full_types_by_name = xbt_dict_new_homogeneous(NULL);
203 void MC_free_object_info(mc_object_info_t* info) {
204 xbt_free(&(*info)->file_name);
205 xbt_dict_free(&(*info)->subprograms);
206 xbt_dynar_free(&(*info)->global_variables);
207 xbt_dict_free(&(*info)->types);
208 xbt_dict_free(&(*info)->full_types_by_name);
210 xbt_dynar_free(&(*info)->functions_index);
216 void* MC_object_base_address(mc_object_info_t info) {
217 if(info->flags & MC_OBJECT_INFO_EXECUTABLE)
219 void* result = info->start_exec;
220 if(info->start_rw!=NULL && result > (void*) info->start_rw) result = info->start_rw;
221 if(info->start_ro!=NULL && result > (void*) info->start_ro) result = info->start_ro;
225 // ***** Functions index
227 static int MC_compare_frame_index_items(mc_function_index_item_t a, mc_function_index_item_t b) {
228 if(a->low_pc < b->low_pc)
230 else if(a->low_pc == b->low_pc)
236 static void MC_make_functions_index(mc_object_info_t info) {
237 xbt_dynar_t index = xbt_dynar_new(sizeof(s_mc_function_index_item_t), NULL);
239 // Populate the array:
240 dw_frame_t frame = NULL;
241 xbt_dict_cursor_t cursor;
243 xbt_dict_foreach(info->subprograms, cursor, key, frame) {
244 if(frame->low_pc==NULL)
246 s_mc_function_index_item_t entry;
247 entry.low_pc = frame->low_pc;
248 entry.high_pc = frame->high_pc;
249 entry.function = frame;
250 xbt_dynar_push(index, &entry);
253 mc_function_index_item_t base = (mc_function_index_item_t) xbt_dynar_get_ptr(index, 0);
255 // Sort the array by low_pc:
257 xbt_dynar_length(index),
258 sizeof(s_mc_function_index_item_t),
259 (int (*)(const void *, const void *))MC_compare_frame_index_items);
261 info->functions_index = index;
264 mc_object_info_t MC_ip_find_object_info(void* ip) {
266 for(i=0; i!=mc_object_infos_size; ++i) {
267 if(ip >= (void*)mc_object_infos[i]->start_exec && ip <= (void*)mc_object_infos[i]->end_exec) {
268 return mc_object_infos[i];
274 static dw_frame_t MC_find_function_by_ip_and_object(void* ip, mc_object_info_t info) {
275 xbt_dynar_t dynar = info->functions_index;
276 mc_function_index_item_t base = (mc_function_index_item_t) xbt_dynar_get_ptr(dynar, 0);
278 int j = xbt_dynar_length(dynar) - 1;
280 int k = i + ((j-i)/2);
281 if(ip < base[k].low_pc) {
283 } else if(ip >= base[k].high_pc) {
286 return base[k].function;
292 dw_frame_t MC_find_function_by_ip(void* ip) {
293 mc_object_info_t info = MC_ip_find_object_info(ip);
297 return MC_find_function_by_ip_and_object(ip, info);
300 static void MC_post_process_variables(mc_object_info_t info) {
302 dw_variable_t variable = NULL;
303 xbt_dynar_foreach(info->global_variables, cursor, variable) {
304 if(variable->type_origin) {
305 variable->type = xbt_dict_get_or_null(info->types, variable->type_origin);
310 static void mc_post_process_scope(mc_object_info_t info, dw_frame_t scope) {
312 if(scope->tag == DW_TAG_inlined_subroutine) {
314 // Attach correct namespaced name in inlined subroutine:
315 char* key = bprintf("%" PRIx64, (uint64_t) scope->abstract_origin_id);
316 dw_frame_t abstract_origin = xbt_dict_get_or_null(info->subprograms, key);
317 xbt_assert(abstract_origin, "Could not lookup abstract origin %s", key);
319 scope->name = xbt_strdup(abstract_origin->name);
325 dw_variable_t variable = NULL;
326 xbt_dynar_foreach(scope->variables, cursor, variable) {
327 if(variable->type_origin) {
328 variable->type = xbt_dict_get_or_null(info->types, variable->type_origin);
332 // Recursive post-processing of nested-scopes:
333 dw_frame_t nested_scope = NULL;
334 xbt_dynar_foreach(scope->scopes, cursor, nested_scope)
335 mc_post_process_scope(info, nested_scope);
339 static void MC_post_process_functions(mc_object_info_t info) {
340 xbt_dict_cursor_t cursor;
342 dw_frame_t subprogram = NULL;
343 xbt_dict_foreach(info->subprograms, cursor, key, subprogram) {
344 mc_post_process_scope(info, subprogram);
348 /** \brief Finds informations about a given shared object/executable */
349 mc_object_info_t MC_find_object_info(memory_map_t maps, char* name, int executable) {
350 mc_object_info_t result = MC_new_object_info();
352 result->flags |= MC_OBJECT_INFO_EXECUTABLE;
353 result->file_name = xbt_strdup(name);
354 MC_find_object_address(maps, result);
355 MC_dwarf_get_variables(result);
356 MC_post_process_types(result);
357 MC_post_process_variables(result);
358 MC_post_process_functions(result);
359 MC_make_functions_index(result);
363 /*************************************************************************/
365 static int MC_dwarf_get_variable_index(xbt_dynar_t variables, char* var, void *address){
367 if(xbt_dynar_is_empty(variables))
370 unsigned int cursor = 0;
372 int end = xbt_dynar_length(variables) - 1;
373 dw_variable_t var_test = NULL;
376 cursor = (start + end) / 2;
377 var_test = (dw_variable_t)xbt_dynar_get_as(variables, cursor, dw_variable_t);
378 if(strcmp(var_test->name, var) < 0){
380 }else if(strcmp(var_test->name, var) > 0){
383 if(address){ /* global variable */
384 if(var_test->address == address)
386 if(var_test->address > address)
390 }else{ /* local variable */
396 if(strcmp(var_test->name, var) == 0){
397 if(address && var_test->address < address)
401 }else if(strcmp(var_test->name, var) < 0)
408 void MC_dwarf_register_global_variable(mc_object_info_t info, dw_variable_t variable) {
409 int index = MC_dwarf_get_variable_index(info->global_variables, variable->name, variable->address);
411 xbt_dynar_insert_at(info->global_variables, index, &variable);
415 void MC_dwarf_register_non_global_variable(mc_object_info_t info, dw_frame_t frame, dw_variable_t variable) {
416 xbt_assert(frame, "Frame is NULL");
417 int index = MC_dwarf_get_variable_index(frame->variables, variable->name, NULL);
419 xbt_dynar_insert_at(frame->variables, index, &variable);
423 void MC_dwarf_register_variable(mc_object_info_t info, dw_frame_t frame, dw_variable_t variable) {
425 MC_dwarf_register_global_variable(info, variable);
427 xbt_die("No frame for this local variable");
429 MC_dwarf_register_non_global_variable(info, frame, variable);
433 /******************************* Ignore mechanism *******************************/
434 /*********************************************************************************/
436 xbt_dynar_t mc_checkpoint_ignore;
438 typedef struct s_mc_stack_ignore_variable{
441 }s_mc_stack_ignore_variable_t, *mc_stack_ignore_variable_t;
443 /**************************** Free functions ******************************/
445 static void stack_ignore_variable_free(mc_stack_ignore_variable_t v){
446 xbt_free(v->var_name);
451 static void stack_ignore_variable_free_voidp(void *v){
452 stack_ignore_variable_free((mc_stack_ignore_variable_t) * (void **) v);
455 void heap_ignore_region_free(mc_heap_ignore_region_t r){
459 void heap_ignore_region_free_voidp(void *r){
460 heap_ignore_region_free((mc_heap_ignore_region_t) * (void **) r);
463 static void checkpoint_ignore_region_free(mc_checkpoint_ignore_region_t r){
467 static void checkpoint_ignore_region_free_voidp(void *r){
468 checkpoint_ignore_region_free((mc_checkpoint_ignore_region_t) * (void **) r);
471 /***********************************************************************/
473 void MC_ignore_heap(void *address, size_t size){
475 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
479 mc_heap_ignore_region_t region = NULL;
480 region = xbt_new0(s_mc_heap_ignore_region_t, 1);
481 region->address = address;
484 region->block = ((char*)address - (char*)((xbt_mheap_t)std_heap)->heapbase) / BLOCKSIZE + 1;
486 if(((xbt_mheap_t)std_heap)->heapinfo[region->block].type == 0){
487 region->fragment = -1;
488 ((xbt_mheap_t)std_heap)->heapinfo[region->block].busy_block.ignore++;
490 region->fragment = ((uintptr_t) (ADDR2UINT (address) % (BLOCKSIZE))) >> ((xbt_mheap_t)std_heap)->heapinfo[region->block].type;
491 ((xbt_mheap_t)std_heap)->heapinfo[region->block].busy_frag.ignore[region->fragment]++;
494 if(mc_heap_comparison_ignore == NULL){
495 mc_heap_comparison_ignore = xbt_dynar_new(sizeof(mc_heap_ignore_region_t), heap_ignore_region_free_voidp);
496 xbt_dynar_push(mc_heap_comparison_ignore, ®ion);
502 unsigned int cursor = 0;
503 mc_heap_ignore_region_t current_region = NULL;
505 int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
508 cursor = (start + end) / 2;
509 current_region = (mc_heap_ignore_region_t)xbt_dynar_get_as(mc_heap_comparison_ignore, cursor, mc_heap_ignore_region_t);
510 if(current_region->address == address){
511 heap_ignore_region_free(region);
515 }else if(current_region->address < address){
522 if(current_region->address < address)
523 xbt_dynar_insert_at(mc_heap_comparison_ignore, cursor + 1, ®ion);
525 xbt_dynar_insert_at(mc_heap_comparison_ignore, cursor, ®ion);
531 void MC_remove_ignore_heap(void *address, size_t size){
533 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
537 unsigned int cursor = 0;
539 int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
540 mc_heap_ignore_region_t region;
541 int ignore_found = 0;
544 cursor = (start + end) / 2;
545 region = (mc_heap_ignore_region_t)xbt_dynar_get_as(mc_heap_comparison_ignore, cursor, mc_heap_ignore_region_t);
546 if(region->address == address){
549 }else if(region->address < address){
552 if((char * )region->address <= ((char *)address + size)){
561 if(ignore_found == 1){
562 xbt_dynar_remove_at(mc_heap_comparison_ignore, cursor, NULL);
563 MC_remove_ignore_heap(address, size);
571 void MC_ignore_global_variable(const char *name){
573 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
577 xbt_assert(mc_libsimgrid_info, "MC subsystem not initialized");
579 unsigned int cursor = 0;
580 dw_variable_t current_var;
582 int end = xbt_dynar_length(mc_libsimgrid_info->global_variables) - 1;
585 cursor = (start + end) /2;
586 current_var = (dw_variable_t)xbt_dynar_get_as(mc_libsimgrid_info->global_variables, cursor, dw_variable_t);
587 if(strcmp(current_var->name, name) == 0){
588 xbt_dynar_remove_at(mc_libsimgrid_info->global_variables, cursor, NULL);
590 end = xbt_dynar_length(mc_libsimgrid_info->global_variables) - 1;
591 }else if(strcmp(current_var->name, name) < 0){
602 /** \brief Ignore a local variable in a scope
604 * Ignore all instances of variables with a given name in
605 * any (possibly inlined) subprogram with a given namespaced
608 * \param var_name Name of the local variable (or parameter to ignore)
609 * \param subprogram_name Name of the subprogram fo ignore (NULL for any)
610 * \param subprogram (possibly inlined) Subprogram of the scope
611 * \param scope Current scope
613 static void mc_ignore_local_variable_in_scope(
614 const char *var_name, const char *subprogram_name,
615 dw_frame_t subprogram, dw_frame_t scope) {
616 // Processing of direct variables:
618 // If the current subprogram matche the given name:
619 if(subprogram_name==NULL || strcmp(subprogram_name, subprogram->name)==0) {
621 // Try to find the variable and remove it:
623 int end = xbt_dynar_length(scope->variables) - 1;
625 // Dichotomic search:
627 int cursor = (start + end) / 2;
628 dw_variable_t current_var = (dw_variable_t)xbt_dynar_get_as(scope->variables, cursor, dw_variable_t);
630 int compare = strcmp(current_var->name, var_name);
632 // Variable found, remove it:
633 xbt_dynar_remove_at(scope->variables, cursor, NULL);
637 end = xbt_dynar_length(scope->variables) - 1;
638 }else if(compare < 0){
647 // And recursive processing in nested scopes:
649 dw_frame_t nested_scope = NULL;
650 xbt_dynar_foreach(scope->scopes, cursor, nested_scope) {
651 // The new scope may be an inlined subroutine, in this case we want to use its
652 // namespaced name in recursive calls:
653 dw_frame_t nested_subprogram = nested_scope->tag == DW_TAG_inlined_subroutine ? nested_scope : subprogram;
655 mc_ignore_local_variable_in_scope(var_name, subprogram_name, nested_subprogram, nested_scope);
659 static void MC_ignore_local_variable_in_object(const char *var_name, const char *subprogram_name, mc_object_info_t info) {
660 xbt_dict_cursor_t cursor2;
663 xbt_dict_foreach(info->subprograms, cursor2, key, frame) {
664 mc_ignore_local_variable_in_scope(var_name, subprogram_name, frame, frame);
668 void MC_ignore_local_variable(const char *var_name, const char *frame_name){
670 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
672 if(strcmp(frame_name, "*") == 0)
677 MC_ignore_local_variable_in_object(var_name, frame_name, mc_libsimgrid_info);
679 MC_ignore_local_variable_in_object(var_name, frame_name, mc_binary_info);
686 void MC_new_stack_area(void *stack, char *name, void* context, size_t size){
688 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
692 if(stacks_areas == NULL)
693 stacks_areas = xbt_dynar_new(sizeof(stack_region_t), NULL);
695 stack_region_t region = NULL;
696 region = xbt_new0(s_stack_region_t, 1);
697 region->address = stack;
698 region->process_name = strdup(name);
699 region->context = context;
701 region->block = ((char*)stack - (char*)((xbt_mheap_t)std_heap)->heapbase) / BLOCKSIZE + 1;
702 xbt_dynar_push(stacks_areas, ®ion);
708 void MC_ignore(void *addr, size_t size){
710 int raw_mem_set= (mmalloc_get_current_heap() == mc_heap);
714 if(mc_checkpoint_ignore == NULL)
715 mc_checkpoint_ignore = xbt_dynar_new(sizeof(mc_checkpoint_ignore_region_t), checkpoint_ignore_region_free_voidp);
717 mc_checkpoint_ignore_region_t region = xbt_new0(s_mc_checkpoint_ignore_region_t, 1);
721 if(xbt_dynar_is_empty(mc_checkpoint_ignore)){
722 xbt_dynar_push(mc_checkpoint_ignore, ®ion);
725 unsigned int cursor = 0;
727 int end = xbt_dynar_length(mc_checkpoint_ignore) -1;
728 mc_checkpoint_ignore_region_t current_region = NULL;
731 cursor = (start + end) / 2;
732 current_region = (mc_checkpoint_ignore_region_t)xbt_dynar_get_as(mc_checkpoint_ignore, cursor, mc_checkpoint_ignore_region_t);
733 if(current_region->addr == addr){
734 if(current_region->size == size){
735 checkpoint_ignore_region_free(region);
739 }else if(current_region->size < size){
744 }else if(current_region->addr < addr){
751 if(current_region->addr == addr){
752 if(current_region->size < size){
753 xbt_dynar_insert_at(mc_checkpoint_ignore, cursor + 1, ®ion);
755 xbt_dynar_insert_at(mc_checkpoint_ignore, cursor, ®ion);
757 }else if(current_region->addr < addr){
758 xbt_dynar_insert_at(mc_checkpoint_ignore, cursor + 1, ®ion);
760 xbt_dynar_insert_at(mc_checkpoint_ignore, cursor, ®ion);
768 /******************************* Initialisation of MC *******************************/
769 /*********************************************************************************/
771 static void MC_post_process_object_info(mc_object_info_t info) {
772 xbt_dict_cursor_t cursor = NULL;
774 dw_type_t type = NULL;
775 xbt_dict_foreach(info->types, cursor, key, type){
777 // Resolve full_type:
778 if(type->name && type->byte_size == 0) {
779 for(size_t i=0; i!=mc_object_infos_size; ++i) {
780 dw_type_t same_type = xbt_dict_get_or_null(mc_object_infos[i]->full_types_by_name, type->name);
781 if(same_type && same_type->name && same_type->byte_size) {
782 type->full_type = same_type;
791 static void MC_init_debug_info(void) {
792 XBT_INFO("Get debug information ...");
794 memory_map_t maps = MC_get_memory_map();
796 /* Get local variables for state equality detection */
797 mc_binary_info = MC_find_object_info(maps, xbt_binary_name, 1);
798 mc_object_infos[0] = mc_binary_info;
800 mc_libsimgrid_info = MC_find_object_info(maps, libsimgrid_path, 0);
801 mc_object_infos[1] = mc_libsimgrid_info;
803 // Use information of the other objects:
804 MC_post_process_object_info(mc_binary_info);
805 MC_post_process_object_info(mc_libsimgrid_info);
807 MC_free_memory_map(maps);
808 XBT_INFO("Get debug information done !");
813 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
817 /* Initialize the data structures that must be persistent across every
818 iteration of the model-checker (in RAW memory) */
822 MC_init_memory_map_info();
823 MC_init_debug_info();
826 parmap = xbt_parmap_mc_new(xbt_os_get_numcores(), XBT_PARMAP_DEFAULT);
830 /* Ignore some variables from xbt/ex.h used by exception e for stacks comparison */
831 MC_ignore_local_variable("e", "*");
832 MC_ignore_local_variable("__ex_cleanup", "*");
833 MC_ignore_local_variable("__ex_mctx_en", "*");
834 MC_ignore_local_variable("__ex_mctx_me", "*");
835 MC_ignore_local_variable("__xbt_ex_ctx_ptr", "*");
836 MC_ignore_local_variable("_log_ev", "*");
837 MC_ignore_local_variable("_throw_ctx", "*");
838 MC_ignore_local_variable("ctx", "*");
840 MC_ignore_local_variable("self", "simcall_BODY_mc_snapshot");
841 MC_ignore_local_variable("next_context", "smx_ctx_sysv_suspend_serial");
842 MC_ignore_local_variable("i", "smx_ctx_sysv_suspend_serial");
844 /* Ignore local variable about time used for tracing */
845 MC_ignore_local_variable("start_time", "*");
847 MC_ignore_global_variable("compared_pointers");
848 MC_ignore_global_variable("mc_comp_times");
849 MC_ignore_global_variable("mc_snapshot_comparison_time");
850 MC_ignore_global_variable("mc_time");
851 MC_ignore_global_variable("smpi_current_rank");
852 MC_ignore_global_variable("counter"); /* Static variable used for tracing */
853 MC_ignore_global_variable("maestro_stack_start");
854 MC_ignore_global_variable("maestro_stack_end");
855 MC_ignore_global_variable("smx_total_comms");
857 MC_ignore_heap(&(simix_global->process_to_run), sizeof(simix_global->process_to_run));
858 MC_ignore_heap(&(simix_global->process_that_ran), sizeof(simix_global->process_that_ran));
859 MC_ignore_heap(simix_global->process_to_run, sizeof(*(simix_global->process_to_run)));
860 MC_ignore_heap(simix_global->process_that_ran, sizeof(*(simix_global->process_that_ran)));
862 smx_process_t process;
863 xbt_swag_foreach(process, simix_global->process_list){
864 MC_ignore_heap(&(process->process_hookup), sizeof(process->process_hookup));
872 static void MC_init_dot_output(){ /* FIXME : more colors */
876 colors[2] = "green3";
877 colors[3] = "goldenrod";
879 colors[5] = "purple";
880 colors[6] = "magenta";
881 colors[7] = "turquoise4";
882 colors[8] = "gray25";
883 colors[9] = "forestgreen";
884 colors[10] = "hotpink";
885 colors[11] = "lightblue";
888 dot_output = fopen(_sg_mc_dot_output_file, "w");
890 if(dot_output == NULL){
891 perror("Error open dot output file");
895 fprintf(dot_output, "digraph graphname{\n fixedsize=true; rankdir=TB; ranksep=.25; edge [fontsize=12]; node [fontsize=10, shape=circle,width=.5 ]; graph [resolution=20, fontsize=10];\n");
899 /******************************* Core of MC *******************************/
900 /**************************************************************************/
902 void MC_do_the_modelcheck_for_real() {
905 mc_comp_times = xbt_new0(s_mc_comparison_times_t, 1);
908 if (!_sg_mc_property_file || _sg_mc_property_file[0]=='\0') {
909 if (mc_reduce_kind==e_mc_reduce_unset)
910 mc_reduce_kind=e_mc_reduce_dpor;
912 XBT_INFO("Check a safety property");
913 MC_modelcheck_safety();
917 if (mc_reduce_kind==e_mc_reduce_unset)
918 mc_reduce_kind=e_mc_reduce_none;
920 XBT_INFO("Check the liveness property %s",_sg_mc_property_file);
921 MC_automaton_load(_sg_mc_property_file);
922 MC_modelcheck_liveness();
926 void MC_modelcheck_safety(void)
928 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
930 /* Check if MC is already initialized */
931 if (initial_state_safety)
934 mc_time = xbt_new0(double, simix_process_maxpid);
936 /* mc_time refers to clock for each process -> ignore it for heap comparison */
937 MC_ignore_heap(mc_time, simix_process_maxpid * sizeof(double));
939 /* Initialize the data structures that must be persistent across every
940 iteration of the model-checker (in RAW memory) */
944 /* Initialize statistics */
945 mc_stats = xbt_new0(s_mc_stats_t, 1);
946 mc_stats->state_size = 1;
948 /* Create exploration stack */
949 mc_stack_safety = xbt_fifo_new();
951 if((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0]!='\0'))
952 MC_init_dot_output();
956 if(_sg_mc_visited > 0){
960 MC_init_memory_map_info();
961 MC_init_debug_info();
968 /* Save the initial state */
969 initial_state_safety = xbt_new0(s_mc_global_t, 1);
970 initial_state_safety->snapshot = MC_take_snapshot(0);
971 initial_state_safety->initial_communications_pattern_done = 0;
972 initial_state_safety->comm_deterministic = 1;
973 initial_state_safety->send_deterministic = 1;
985 void MC_modelcheck_liveness(){
987 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
991 mc_time = xbt_new0(double, simix_process_maxpid);
993 /* mc_time refers to clock for each process -> ignore it for heap comparison */
994 MC_ignore_heap(mc_time, simix_process_maxpid * sizeof(double));
998 /* Initialize statistics */
999 mc_stats = xbt_new0(s_mc_stats_t, 1);
1000 mc_stats->state_size = 1;
1002 /* Create exploration stack */
1003 mc_stack_liveness = xbt_fifo_new();
1005 /* Create the initial state */
1006 initial_state_liveness = xbt_new0(s_mc_global_t, 1);
1008 if((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0]!='\0'))
1009 MC_init_dot_output();
1016 MC_print_statistics(mc_stats);
1033 int SIMIX_pre_mc_random(smx_simcall_t simcall, int min, int max){
1035 return simcall->mc_value;
1039 int MC_random(int min, int max)
1041 /*FIXME: return mc_current_state->executed_transition->random.value;*/
1042 return simcall_mc_random(min, max);
1046 * \brief Schedules all the process that are ready to run
1048 void MC_wait_for_requests(void)
1050 smx_process_t process;
1054 while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
1055 SIMIX_process_runall();
1056 xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
1057 req = &process->simcall;
1058 if (req->call != SIMCALL_NONE && !MC_request_is_visible(req))
1059 SIMIX_simcall_pre(req, 0);
1064 int MC_deadlock_check()
1066 int deadlock = FALSE;
1067 smx_process_t process;
1068 if(xbt_swag_size(simix_global->process_list)){
1070 xbt_swag_foreach(process, simix_global->process_list){
1071 if(process->simcall.call != SIMCALL_NONE
1072 && MC_request_is_enabled(&process->simcall)){
1082 * \brief Re-executes from the state at position start all the transitions indicated by
1083 * a given model-checker stack.
1084 * \param stack The stack with the transitions to execute.
1085 * \param start Start index to begin the re-execution.
1087 void MC_replay(xbt_fifo_t stack, int start)
1089 int raw_mem = (mmalloc_get_current_heap() == mc_heap);
1091 int value, i = 1, count = 1;
1093 smx_simcall_t req = NULL, saved_req = NULL;
1094 xbt_fifo_item_t item, start_item;
1096 smx_process_t process = NULL;
1097 int comm_pattern = 0;
1099 XBT_DEBUG("**** Begin Replay ****");
1102 /* Restore the initial state */
1103 MC_restore_snapshot(initial_state_safety->snapshot);
1104 /* At the moment of taking the snapshot the raw heap was set, so restoring
1105 * it will set it back again, we have to unset it to continue */
1109 start_item = xbt_fifo_get_last_item(stack);
1112 start_item = xbt_fifo_get_prev_item(start_item);
1119 if(mc_reduce_kind == e_mc_reduce_dpor){
1120 xbt_dict_reset(first_enabled_state);
1121 xbt_swag_foreach(process, simix_global->process_list){
1122 if(MC_process_is_enabled(process)){
1123 char *key = bprintf("%lu", process->pid);
1124 char *data = bprintf("%d", count);
1125 xbt_dict_set(first_enabled_state, key, data, NULL);
1131 if(_sg_mc_comms_determinism || _sg_mc_send_determinism){
1132 xbt_dynar_reset(communications_pattern);
1133 xbt_dynar_reset(incomplete_communications_pattern);
1139 /* Traverse the stack from the state at position start and re-execute the transitions */
1140 for (item = start_item;
1141 item != xbt_fifo_get_first_item(stack);
1142 item = xbt_fifo_get_prev_item(item)) {
1144 state = (mc_state_t) xbt_fifo_get_item_content(item);
1145 saved_req = MC_state_get_executed_request(state, &value);
1147 if(mc_reduce_kind == e_mc_reduce_dpor){
1149 char *key = bprintf("%lu", saved_req->issuer->pid);
1150 xbt_dict_remove(first_enabled_state, key);
1156 /* because we got a copy of the executed request, we have to fetch the
1157 real one, pointed by the request field of the issuer process */
1158 req = &saved_req->issuer->simcall;
1160 /* Debug information */
1161 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
1162 req_str = MC_request_to_string(req, value);
1163 XBT_DEBUG("Replay: %s (%p)", req_str, state);
1168 if(_sg_mc_comms_determinism || _sg_mc_send_determinism){
1169 if(req->call == SIMCALL_COMM_ISEND)
1171 else if(req->call == SIMCALL_COMM_IRECV)
1175 SIMIX_simcall_pre(req, value);
1177 if(_sg_mc_comms_determinism || _sg_mc_send_determinism){
1179 if(comm_pattern != 0){
1180 get_comm_pattern(communications_pattern, req, comm_pattern);
1186 MC_wait_for_requests();
1190 if(mc_reduce_kind == e_mc_reduce_dpor){
1192 /* Insert in dict all enabled processes */
1193 xbt_swag_foreach(process, simix_global->process_list){
1194 if(MC_process_is_enabled(process) /*&& !MC_state_process_is_done(state, process)*/){
1195 char *key = bprintf("%lu", process->pid);
1196 if(xbt_dict_get_or_null(first_enabled_state, key) == NULL){
1197 char *data = bprintf("%d", count);
1198 xbt_dict_set(first_enabled_state, key, data, NULL);
1206 /* Update statistics */
1207 mc_stats->visited_states++;
1208 mc_stats->executed_transitions++;
1212 XBT_DEBUG("**** End Replay ****");
1222 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
1225 initial_state_liveness->raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
1229 smx_simcall_t req = NULL, saved_req = NULL;
1230 xbt_fifo_item_t item;
1235 XBT_DEBUG("**** Begin Replay ****");
1237 /* Restore the initial state */
1238 MC_restore_snapshot(initial_state_liveness->snapshot);
1240 /* At the moment of taking the snapshot the raw heap was set, so restoring
1241 * it will set it back again, we have to unset it to continue */
1242 if(!initial_state_liveness->raw_mem_set)
1247 item = xbt_fifo_get_last_item(stack);
1249 while(depth <= xbt_fifo_size(stack)){
1251 pair = (mc_pair_t) xbt_fifo_get_item_content(item);
1252 state = (mc_state_t) pair->graph_state;
1254 if(pair->requests > 0){
1256 saved_req = MC_state_get_executed_request(state, &value);
1257 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
1259 if(saved_req != NULL){
1260 /* because we got a copy of the executed request, we have to fetch the
1261 real one, pointed by the request field of the issuer process */
1262 req = &saved_req->issuer->simcall;
1263 //XBT_DEBUG("Req->call %u", req->call);
1265 /* Debug information */
1266 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
1267 req_str = MC_request_to_string(req, value);
1268 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
1274 SIMIX_simcall_pre(req, value);
1275 MC_wait_for_requests();
1280 /* Update statistics */
1281 mc_stats->visited_pairs++;
1282 mc_stats->executed_transitions++;
1284 item = xbt_fifo_get_prev_item(item);
1289 /* Traverse the stack from the initial state and re-execute the transitions */
1290 for (item = xbt_fifo_get_last_item(stack);
1291 item != xbt_fifo_get_first_item(stack);
1292 item = xbt_fifo_get_prev_item(item)) {
1294 pair = (mc_pair_t) xbt_fifo_get_item_content(item);
1295 state = (mc_state_t) pair->graph_state;
1297 if(pair->requests > 0){
1299 saved_req = MC_state_get_executed_request(state, &value);
1300 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
1302 if(saved_req != NULL){
1303 /* because we got a copy of the executed request, we have to fetch the
1304 real one, pointed by the request field of the issuer process */
1305 req = &saved_req->issuer->simcall;
1306 //XBT_DEBUG("Req->call %u", req->call);
1308 /* Debug information */
1309 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
1310 req_str = MC_request_to_string(req, value);
1311 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
1317 SIMIX_simcall_pre(req, value);
1318 MC_wait_for_requests();
1323 /* Update statistics */
1324 mc_stats->visited_pairs++;
1325 mc_stats->executed_transitions++;
1329 XBT_DEBUG("**** End Replay ****");
1331 if(initial_state_liveness->raw_mem_set)
1339 * \brief Dumps the contents of a model-checker's stack and shows the actual
1341 * \param stack The stack to dump
1343 void MC_dump_stack_safety(xbt_fifo_t stack)
1346 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
1348 MC_show_stack_safety(stack);
1350 if(!_sg_mc_checkpoint){
1355 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
1356 MC_state_delete(state);
1369 void MC_show_stack_safety(xbt_fifo_t stack)
1372 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
1378 xbt_fifo_item_t item;
1380 char *req_str = NULL;
1382 for (item = xbt_fifo_get_last_item(stack);
1383 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
1384 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
1385 req = MC_state_get_executed_request(state, &value);
1387 req_str = MC_request_to_string(req, value);
1388 XBT_INFO("%s", req_str);
1397 void MC_show_deadlock(smx_simcall_t req)
1399 /*char *req_str = NULL;*/
1400 XBT_INFO("**************************");
1401 XBT_INFO("*** DEAD-LOCK DETECTED ***");
1402 XBT_INFO("**************************");
1403 XBT_INFO("Locked request:");
1404 /*req_str = MC_request_to_string(req);
1405 XBT_INFO("%s", req_str);
1406 xbt_free(req_str);*/
1407 XBT_INFO("Counter-example execution trace:");
1408 MC_dump_stack_safety(mc_stack_safety);
1409 MC_print_statistics(mc_stats);
1413 void MC_show_stack_liveness(xbt_fifo_t stack){
1416 xbt_fifo_item_t item;
1418 char *req_str = NULL;
1420 for (item = xbt_fifo_get_last_item(stack);
1421 (item ? (pair = (mc_pair_t) (xbt_fifo_get_item_content(item)))
1422 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
1423 req = MC_state_get_executed_request(pair->graph_state, &value);
1425 if(pair->requests>0){
1426 req_str = MC_request_to_string(req, value);
1427 XBT_INFO("%s", req_str);
1430 XBT_INFO("End of system requests but evolution in Büchi automaton");
1436 void MC_dump_stack_liveness(xbt_fifo_t stack){
1438 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
1443 while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != NULL)
1444 MC_pair_delete(pair);
1453 void MC_print_statistics(mc_stats_t stats)
1455 if(stats->expanded_pairs == 0){
1456 XBT_INFO("Expanded states = %lu", stats->expanded_states);
1457 XBT_INFO("Visited states = %lu", stats->visited_states);
1459 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
1460 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
1462 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
1464 if((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0]!='\0')){
1465 fprintf(dot_output, "}\n");
1468 if(initial_state_safety != NULL){
1469 if(_sg_mc_comms_determinism)
1470 XBT_INFO("Communication-deterministic : %s", !initial_state_safety->comm_deterministic ? "No" : "Yes");
1471 if (_sg_mc_send_determinism)
1472 XBT_INFO("Send-deterministic : %s", !initial_state_safety->send_deterministic ? "No" : "Yes");
1477 void MC_assert(int prop)
1479 if (MC_is_active() && !prop){
1480 XBT_INFO("**************************");
1481 XBT_INFO("*** PROPERTY NOT VALID ***");
1482 XBT_INFO("**************************");
1483 XBT_INFO("Counter-example execution trace:");
1484 MC_dump_stack_safety(mc_stack_safety);
1485 MC_print_statistics(mc_stats);
1491 user_max_depth_reached = 1;
1494 void MC_process_clock_add(smx_process_t process, double amount)
1496 mc_time[process->pid] += amount;
1499 double MC_process_clock_get(smx_process_t process)
1503 return mc_time[process->pid];
1511 void MC_automaton_load(const char *file){
1513 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
1517 if (_mc_property_automaton == NULL)
1518 _mc_property_automaton = xbt_automaton_new();
1520 xbt_automaton_load(_mc_property_automaton,file);
1529 void MC_automaton_new_propositional_symbol(const char* id, void* fct) {
1531 int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
1535 if (_mc_property_automaton == NULL)
1536 _mc_property_automaton = xbt_automaton_new();
1538 xbt_automaton_propositional_symbol_new(_mc_property_automaton,id,fct);