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. */
19 #include "simgrid/sg_config.h"
20 #include "../surf/surf_private.h"
21 #include "../simix/smx_private.h"
23 #include "xbt/automaton.h"
25 #include "mc_record.h"
28 #include "mc_server.h"
29 #include <libunwind.h>
30 #include <xbt/mmalloc.h>
31 #include "../xbt/mmalloc/mmprivate.h"
32 #include "mc_object_info.h"
33 #include "mc_comm_pattern.h"
34 #include "mc_request.h"
35 #include "mc_safety.h"
36 #include "mc_memory_map.h"
37 #include "mc_snapshot.h"
38 #include "mc_liveness.h"
39 #include "mc_private.h"
43 #include "mc_record.h"
44 #include "mc_protocol.h"
45 #include "mc_client.h"
49 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
50 "Logging specific to MC (global)");
54 double *mc_time = NULL;
57 int user_max_depth_reached = 0;
59 /* MC global data structures */
60 mc_state_t mc_current_state = NULL;
61 char mc_replay_mode = FALSE;
63 __thread mc_comparison_times_t mc_comp_times = NULL;
64 __thread double mc_snapshot_comparison_time;
65 mc_stats_t mc_stats = NULL;
66 mc_global_t initial_global_state = NULL;
67 xbt_fifo_t mc_stack = NULL;
70 xbt_automaton_t _mc_property_automaton = NULL;
73 FILE *dot_output = NULL;
74 const char *colors[13];
77 /******************************* Initialisation of MC *******************************/
78 /*********************************************************************************/
80 static void MC_init_dot_output()
81 { /* FIXME : more colors */
86 colors[3] = "goldenrod";
89 colors[6] = "magenta";
90 colors[7] = "turquoise4";
92 colors[9] = "forestgreen";
93 colors[10] = "hotpink";
94 colors[11] = "lightblue";
97 dot_output = fopen(_sg_mc_dot_output_file, "w");
99 if (dot_output == NULL) {
100 perror("Error open dot output file");
105 "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");
109 static void MC_init_mode(void)
111 if (mc_mode == MC_MODE_NONE) {
112 if (getenv(MC_ENV_SOCKET_FD)) {
113 mc_mode = MC_MODE_CLIENT;
115 mc_mode = MC_MODE_STANDALONE;
123 MC_init_pid(getpid(), -1);
126 void MC_init_pid(pid_t pid, int socket)
128 /* Initialize the data structures that must be persistent across every
129 iteration of the model-checker (in RAW memory) */
131 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
132 mc_model_checker = MC_model_checker_new(pid, socket);
134 // It's not useful anymore:
135 if (0 && mc_mode == MC_MODE_SERVER) {
136 unsigned long maxpid;
137 MC_process_read_variable(&mc_model_checker->process, "simix_process_maxpid",
138 &maxpid, sizeof(maxpid));
139 simix_process_maxpid = maxpid;
142 mmalloc_set_current_heap(std_heap);
143 mc_time = xbt_new0(double, MC_smx_get_maxpid());
144 mmalloc_set_current_heap(mc_heap);
146 mc_comp_times = xbt_new0(s_mc_comparison_times_t, 1);
148 /* Initialize statistics */
149 mc_stats = xbt_new0(s_mc_stats_t, 1);
150 mc_stats->state_size = 1;
152 if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0'))
153 MC_init_dot_output();
156 //parmap = xbt_parmap_mc_new(xbt_os_get_numcores(), XBT_PARMAP_DEFAULT);
160 if (_sg_mc_visited > 0 || _sg_mc_liveness || _sg_mc_termination || mc_mode == MC_MODE_SERVER) {
161 /* Ignore some variables from xbt/ex.h used by exception e for stacks comparison */
162 MC_ignore_local_variable("e", "*");
163 MC_ignore_local_variable("__ex_cleanup", "*");
164 MC_ignore_local_variable("__ex_mctx_en", "*");
165 MC_ignore_local_variable("__ex_mctx_me", "*");
166 MC_ignore_local_variable("__xbt_ex_ctx_ptr", "*");
167 MC_ignore_local_variable("_log_ev", "*");
168 MC_ignore_local_variable("_throw_ctx", "*");
169 MC_ignore_local_variable("ctx", "*");
171 MC_ignore_local_variable("self", "simcall_BODY_mc_snapshot");
172 MC_ignore_local_variable("next_cont"
173 "ext", "smx_ctx_sysv_suspend_serial");
174 MC_ignore_local_variable("i", "smx_ctx_sysv_suspend_serial");
176 /* Ignore local variable about time used for tracing */
177 MC_ignore_local_variable("start_time", "*");
180 MC_ignore_global_variable("mc_model_checker");
181 MC_ignore_global_variable("initial_communications_pattern");
182 MC_ignore_global_variable("incomplete_communications_pattern");
183 MC_ignore_global_variable("nb_comm_pattern");
185 /* MC __thread variables: */
186 MC_ignore_global_variable("mc_diff_info");
187 MC_ignore_global_variable("mc_comp_times");
188 MC_ignore_global_variable("mc_snapshot_comparison_time");
190 /* This MC state is used in MC replay as well: */
191 MC_ignore_global_variable("mc_time");
193 /* Static variable used for tracing */
194 MC_ignore_global_variable("counter");
197 MC_ignore_global_variable("smx_total_comms");
199 if (mc_mode == MC_MODE_STANDALONE || mc_mode == MC_MODE_CLIENT) {
200 /* Those requests are handled on the client side and propagated by message
203 MC_ignore_heap(mc_time, MC_smx_get_maxpid() * sizeof(double));
205 smx_process_t process;
206 xbt_swag_foreach(process, simix_global->process_list) {
207 MC_ignore_heap(&(process->process_hookup), sizeof(process->process_hookup));
213 mmalloc_set_current_heap(heap);
217 /******************************* Core of MC *******************************/
218 /**************************************************************************/
220 void MC_do_the_modelcheck_for_real()
226 xbt_die("Unexpected mc mode");
230 MC_client_main_loop();
234 case MC_MODE_STANDALONE:
239 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
240 XBT_INFO("Check communication determinism");
241 mc_reduce_kind = e_mc_reduce_none;
242 MC_wait_for_requests();
243 MC_modelcheck_comm_determinism();
246 else if (!_sg_mc_property_file || _sg_mc_property_file[0] == '\0') {
247 if(_sg_mc_termination)
248 mc_reduce_kind = e_mc_reduce_none;
249 else if (mc_reduce_kind == e_mc_reduce_unset)
250 mc_reduce_kind = e_mc_reduce_dpor;
252 if (_sg_mc_termination)
253 XBT_INFO("Check non progressive cycles");
255 XBT_INFO("Check a safety property");
256 MC_wait_for_requests();
257 MC_modelcheck_safety();
261 if (mc_reduce_kind == e_mc_reduce_unset)
262 mc_reduce_kind = e_mc_reduce_none;
263 XBT_INFO("Check the liveness property %s", _sg_mc_property_file);
264 MC_automaton_load(_sg_mc_property_file);
265 MC_wait_for_requests();
266 MC_modelcheck_liveness();
281 int MC_deadlock_check()
283 if (mc_mode == MC_MODE_SERVER) {
285 if ((res = MC_protocol_send_simple_message(mc_model_checker->process.socket,
286 MC_MESSAGE_DEADLOCK_CHECK)))
287 xbt_die("Could not check deadlock state");
288 s_mc_int_message_t message;
289 ssize_t s = MC_receive_message(mc_model_checker->process.socket, &message, sizeof(message), 0);
291 xbt_die("Could not receive message");
292 else if (s != sizeof(message) || message.type != MC_MESSAGE_DEADLOCK_CHECK_REPLY) {
293 xbt_die("%s received unexpected message %s (%i, size=%i) "
294 "expected MC_MESSAGE_DEADLOCK_CHECK_REPLY (%i, size=%i)",
295 MC_mode_name(mc_mode),
296 MC_message_type_name(message.type), (int) message.type, (int) s,
297 (int) MC_MESSAGE_DEADLOCK_CHECK_REPLY, (int) sizeof(message)
301 return message.value;
304 int deadlock = FALSE;
305 smx_process_t process;
306 if (xbt_swag_size(simix_global->process_list)) {
308 MC_EACH_SIMIX_PROCESS(process,
309 if (MC_process_is_enabled(process)) {
320 * \brief Re-executes from the state at position start all the transitions indicated by
321 * a given model-checker stack.
322 * \param stack The stack with the transitions to execute.
323 * \param start Start index to begin the re-execution.
325 void MC_replay(xbt_fifo_t stack)
327 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
329 int value, count = 1;
331 smx_simcall_t req = NULL, saved_req = NULL;
332 xbt_fifo_item_t item, start_item;
335 XBT_DEBUG("**** Begin Replay ****");
337 /* Intermediate backtracking */
338 if(_sg_mc_checkpoint > 0 || _sg_mc_termination || _sg_mc_visited > 0) {
339 start_item = xbt_fifo_get_first_item(stack);
340 state = (mc_state_t)xbt_fifo_get_item_content(start_item);
341 if(state->system_state){
342 MC_restore_snapshot(state->system_state);
343 if(_sg_mc_comms_determinism || _sg_mc_send_determinism)
344 MC_restore_communications_pattern(state);
351 /* Restore the initial state */
352 MC_restore_snapshot(initial_global_state->snapshot);
353 /* At the moment of taking the snapshot the raw heap was set, so restoring
354 * it will set it back again, we have to unset it to continue */
357 start_item = xbt_fifo_get_last_item(stack);
361 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
362 // int n = xbt_dynar_length(incomplete_communications_pattern);
363 unsigned n = MC_smx_get_maxpid();
364 assert(n == xbt_dynar_length(incomplete_communications_pattern));
365 assert(n == xbt_dynar_length(initial_communications_pattern));
366 for (unsigned j=0; j < n ; j++) {
367 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
368 xbt_dynar_get_as(initial_communications_pattern, j, mc_list_comm_pattern_t)->index_comm = 0;
374 /* Traverse the stack from the state at position start and re-execute the transitions */
375 for (item = start_item;
376 item != xbt_fifo_get_first_item(stack);
377 item = xbt_fifo_get_prev_item(item)) {
379 state = (mc_state_t) xbt_fifo_get_item_content(item);
380 saved_req = MC_state_get_executed_request(state, &value);
383 /* because we got a copy of the executed request, we have to fetch the
384 real one, pointed by the request field of the issuer process */
386 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
387 req = &issuer->simcall;
389 /* Debug information */
390 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
391 req_str = MC_request_to_string(req, value, MC_REQUEST_SIMIX);
392 XBT_DEBUG("Replay: %s (%p)", req_str, state);
396 /* TODO : handle test and testany simcalls */
397 e_mc_call_type_t call = MC_CALL_TYPE_NONE;
398 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
399 call = MC_get_call_type(req);
401 MC_simcall_handle(req, value);
404 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
405 MC_handle_comm_pattern(call, req, value, NULL, 1);
408 MC_wait_for_requests();
413 /* Update statistics */
414 mc_stats->visited_states++;
415 mc_stats->executed_transitions++;
419 XBT_DEBUG("**** End Replay ****");
420 mmalloc_set_current_heap(heap);
423 void MC_replay_liveness(xbt_fifo_t stack)
426 initial_global_state->raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
428 xbt_fifo_item_t item;
429 mc_pair_t pair = NULL;
430 mc_state_t state = NULL;
431 smx_simcall_t req = NULL, saved_req = NULL;
432 int value, depth = 1;
435 XBT_DEBUG("**** Begin Replay ****");
437 /* Intermediate backtracking */
438 if(_sg_mc_checkpoint > 0) {
439 item = xbt_fifo_get_first_item(stack);
440 pair = (mc_pair_t) xbt_fifo_get_item_content(item);
441 if(pair->graph_state->system_state){
442 MC_restore_snapshot(pair->graph_state->system_state);
448 /* Restore the initial state */
449 MC_restore_snapshot(initial_global_state->snapshot);
451 /* At the moment of taking the snapshot the raw heap was set, so restoring
452 * it will set it back again, we have to unset it to continue */
453 if (!initial_global_state->raw_mem_set)
456 /* Traverse the stack from the initial state and re-execute the transitions */
457 for (item = xbt_fifo_get_last_item(stack);
458 item != xbt_fifo_get_first_item(stack);
459 item = xbt_fifo_get_prev_item(item)) {
461 pair = (mc_pair_t) xbt_fifo_get_item_content(item);
463 state = (mc_state_t) pair->graph_state;
465 if (pair->exploration_started) {
467 saved_req = MC_state_get_executed_request(state, &value);
469 if (saved_req != NULL) {
470 /* because we got a copy of the executed request, we have to fetch the
471 real one, pointed by the request field of the issuer process */
472 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
473 req = &issuer->simcall;
475 /* Debug information */
476 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
477 req_str = MC_request_to_string(req, value, MC_REQUEST_SIMIX);
478 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
484 MC_simcall_handle(req, value);
485 MC_wait_for_requests();
488 /* Update statistics */
489 mc_stats->visited_pairs++;
490 mc_stats->executed_transitions++;
496 XBT_DEBUG("**** End Replay ****");
498 if (initial_global_state->raw_mem_set)
506 * \brief Dumps the contents of a model-checker's stack and shows the actual
508 * \param stack The stack to dump
510 void MC_dump_stack_safety(xbt_fifo_t stack)
512 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
514 MC_show_stack_safety(stack);
519 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
520 MC_state_delete(state, !state->in_visited_states ? 1 : 0);
521 mmalloc_set_current_heap(heap);
525 void MC_show_stack_safety(xbt_fifo_t stack)
527 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
531 xbt_fifo_item_t item;
533 char *req_str = NULL;
535 for (item = xbt_fifo_get_last_item(stack);
536 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
537 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
538 req = MC_state_get_executed_request(state, &value);
540 req_str = MC_request_to_string(req, value, MC_REQUEST_EXECUTED);
541 XBT_INFO("%s", req_str);
546 mmalloc_set_current_heap(heap);
549 void MC_show_deadlock(smx_simcall_t req)
551 /*char *req_str = NULL; */
552 XBT_INFO("**************************");
553 XBT_INFO("*** DEAD-LOCK DETECTED ***");
554 XBT_INFO("**************************");
555 XBT_INFO("Locked request:");
556 /*req_str = MC_request_to_string(req);
557 XBT_INFO("%s", req_str);
558 xbt_free(req_str); */
559 XBT_INFO("Counter-example execution trace:");
560 MC_dump_stack_safety(mc_stack);
561 MC_print_statistics(mc_stats);
564 void MC_show_non_termination(void){
565 XBT_INFO("******************************************");
566 XBT_INFO("*** NON-PROGRESSIVE CYCLE DETECTED ***");
567 XBT_INFO("******************************************");
568 XBT_INFO("Counter-example execution trace:");
569 MC_dump_stack_safety(mc_stack);
570 MC_print_statistics(mc_stats);
574 void MC_show_stack_liveness(xbt_fifo_t stack)
578 xbt_fifo_item_t item;
580 char *req_str = NULL;
582 for (item = xbt_fifo_get_last_item(stack);
583 (item ? (pair = (mc_pair_t) (xbt_fifo_get_item_content(item))) : (NULL));
584 item = xbt_fifo_get_prev_item(item)) {
585 req = MC_state_get_executed_request(pair->graph_state, &value);
586 if (req && req->call != SIMCALL_NONE) {
587 req_str = MC_request_to_string(req, value, MC_REQUEST_EXECUTED);
588 XBT_INFO("%s", req_str);
595 void MC_dump_stack_liveness(xbt_fifo_t stack)
597 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
599 while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != NULL)
600 MC_pair_delete(pair);
601 mmalloc_set_current_heap(heap);
604 void MC_print_statistics(mc_stats_t stats)
606 if(_sg_mc_comms_determinism) {
607 if (!initial_global_state->recv_deterministic && initial_global_state->send_deterministic){
608 XBT_INFO("******************************************************");
609 XBT_INFO("**** Only-send-deterministic communication pattern ****");
610 XBT_INFO("******************************************************");
611 XBT_INFO("%s", initial_global_state->recv_diff);
612 }else if(!initial_global_state->send_deterministic && initial_global_state->recv_deterministic) {
613 XBT_INFO("******************************************************");
614 XBT_INFO("**** Only-recv-deterministic communication pattern ****");
615 XBT_INFO("******************************************************");
616 XBT_INFO("%s", initial_global_state->send_diff);
620 if (stats->expanded_pairs == 0) {
621 XBT_INFO("Expanded states = %lu", stats->expanded_states);
622 XBT_INFO("Visited states = %lu", stats->visited_states);
624 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
625 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
627 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
628 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
629 if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0')) {
630 fprintf(dot_output, "}\n");
633 if (initial_global_state != NULL && (_sg_mc_comms_determinism || _sg_mc_send_determinism)) {
634 XBT_INFO("Send-deterministic : %s", !initial_global_state->send_deterministic ? "No" : "Yes");
635 if (_sg_mc_comms_determinism)
636 XBT_INFO("Recv-deterministic : %s", !initial_global_state->recv_deterministic ? "No" : "Yes");
638 mmalloc_set_current_heap(heap);
641 void MC_automaton_load(const char *file)
643 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
645 if (_mc_property_automaton == NULL)
646 _mc_property_automaton = xbt_automaton_new();
648 xbt_automaton_load(_mc_property_automaton, file);
649 mmalloc_set_current_heap(heap);
652 static void register_symbol(xbt_automaton_propositional_symbol_t symbol)
654 if (mc_mode != MC_MODE_CLIENT)
656 s_mc_register_symbol_message_t message;
657 message.type = MC_MESSAGE_REGISTER_SYMBOL;
658 const char* name = xbt_automaton_propositional_symbol_get_name(symbol);
659 if (strlen(name) + 1 > sizeof(message.name))
660 xbt_die("Symbol is too long");
661 strncpy(message.name, name, sizeof(message.name));
662 message.callback = xbt_automaton_propositional_symbol_get_callback(symbol);
663 message.data = xbt_automaton_propositional_symbol_get_data(symbol);
664 MC_client_send_message(&message, sizeof(message));
667 void MC_automaton_new_propositional_symbol(const char *id, int(*fct)(void))
669 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
671 if (_mc_property_automaton == NULL)
672 _mc_property_automaton = xbt_automaton_new();
674 xbt_automaton_propositional_symbol_t symbol = xbt_automaton_propositional_symbol_new(_mc_property_automaton, id, fct);
675 register_symbol(symbol);
676 mmalloc_set_current_heap(heap);
679 void MC_automaton_new_propositional_symbol_pointer(const char *id, int* value)
681 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
682 if (_mc_property_automaton == NULL)
683 _mc_property_automaton = xbt_automaton_new();
684 xbt_automaton_propositional_symbol_t symbol = xbt_automaton_propositional_symbol_new_pointer(_mc_property_automaton, id, value);
685 register_symbol(symbol);
686 mmalloc_set_current_heap(heap);
689 void MC_automaton_new_propositional_symbol_callback(const char* id,
690 xbt_automaton_propositional_symbol_callback_type callback,
691 void* data, xbt_automaton_propositional_symbol_free_function_type free_function)
693 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
694 if (_mc_property_automaton == NULL)
695 _mc_property_automaton = xbt_automaton_new();
696 xbt_automaton_propositional_symbol_t symbol = xbt_automaton_propositional_symbol_new_callback(
697 _mc_property_automaton, id, callback, data, free_function);
698 register_symbol(symbol);
699 mmalloc_set_current_heap(heap);
702 // TODO, fix cross-process access (this function is not used)
703 void MC_dump_stacks(FILE* file)
705 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
708 stack_region_t current_stack;
710 xbt_dynar_foreach(stacks_areas, cursor, current_stack) {
711 unw_context_t * context = (unw_context_t *)current_stack->context;
712 fprintf(file, "Stack %i:\n", nstack);
717 unw_init_local (&c, context);
720 const char * name = !unw_get_proc_name(&c, buffer, 100, &off) ? buffer : "?";
721 #if defined(__x86_64__)
724 unw_get_reg(&c, UNW_X86_64_RIP, &rip);
725 unw_get_reg(&c, UNW_X86_64_RSP, &rsp);
726 fprintf(file, " %i: %s (RIP=0x%" PRIx64 " RSP=0x%" PRIx64 ")\n",
727 nframe, name, rip, rsp);
729 fprintf(file, " %i: %s\n", nframe, name);
732 } while(unw_step(&c));
736 mmalloc_set_current_heap(heap);
740 double MC_process_clock_get(smx_process_t process)
744 return mc_time[process->pid];
752 void MC_process_clock_add(smx_process_t process, double amount)
754 mc_time[process->pid] += amount;
758 void MC_report_assertion_error(void)
760 XBT_INFO("**************************");
761 XBT_INFO("*** PROPERTY NOT VALID ***");
762 XBT_INFO("**************************");
763 XBT_INFO("Counter-example execution trace:");
764 MC_record_dump_path(mc_stack);
765 MC_dump_stack_safety(mc_stack);
766 MC_print_statistics(mc_stats);