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. */
18 #include "simgrid/sg_config.h"
19 #include "../surf/surf_private.h"
20 #include "../simix/smx_private.h"
22 #include "xbt/automaton.h"
24 #include "mc_server.h"
27 #include <libunwind.h>
28 #include <xbt/mmalloc.h>
30 #include "../xbt/mmalloc/mmprivate.h"
31 #include "mc_object_info.h"
32 #include "mc_comm_pattern.h"
33 #include "mc_request.h"
34 #include "mc_safety.h"
35 #include "mc_memory_map.h"
36 #include "mc_snapshot.h"
37 #include "mc_liveness.h"
38 #include "mc_private.h"
42 #include "mc_record.h"
43 #include "mc_protocol.h"
44 #include "mc_client.h"
46 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
47 "Logging specific to MC (global)");
51 double *mc_time = NULL;
54 int user_max_depth_reached = 0;
56 /* MC global data structures */
57 mc_state_t mc_current_state = NULL;
58 char mc_replay_mode = FALSE;
60 __thread mc_comparison_times_t mc_comp_times = NULL;
61 __thread double mc_snapshot_comparison_time;
62 mc_stats_t mc_stats = NULL;
63 mc_global_t initial_global_state = NULL;
64 xbt_fifo_t mc_stack = NULL;
67 xbt_automaton_t _mc_property_automaton = NULL;
70 FILE *dot_output = NULL;
71 const char *colors[13];
74 /******************************* Initialisation of MC *******************************/
75 /*********************************************************************************/
77 static void MC_init_dot_output()
78 { /* FIXME : more colors */
83 colors[3] = "goldenrod";
86 colors[6] = "magenta";
87 colors[7] = "turquoise4";
89 colors[9] = "forestgreen";
90 colors[10] = "hotpink";
91 colors[11] = "lightblue";
94 dot_output = fopen(_sg_mc_dot_output_file, "w");
96 if (dot_output == NULL) {
97 perror("Error open dot output file");
102 "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");
108 MC_init_pid(getpid(), -1);
111 static void MC_init_mode(void)
113 if (mc_mode == MC_MODE_NONE) {
114 if (getenv(MC_ENV_SOCKET_FD)) {
115 mc_mode = MC_MODE_CLIENT;
117 mc_mode = MC_MODE_STANDALONE;
122 void MC_init_pid(pid_t pid, int socket)
124 /* Initialize the data structures that must be persistent across every
125 iteration of the model-checker (in RAW memory) */
127 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
129 mc_model_checker = MC_model_checker_new(pid, socket);
130 if (mc_mode == MC_MODE_SERVER) {
131 unsigned long maxpid;
132 MC_process_read_variable(&mc_model_checker->process, "simix_process_maxpid",
133 &maxpid, sizeof(maxpid));
134 simix_process_maxpid = maxpid;
137 mmalloc_set_current_heap(std_heap);
138 mc_time = xbt_new0(double, MC_smx_get_maxpid());
139 mmalloc_set_current_heap(mc_heap);
141 mc_comp_times = xbt_new0(s_mc_comparison_times_t, 1);
143 /* Initialize statistics */
144 mc_stats = xbt_new0(s_mc_stats_t, 1);
145 mc_stats->state_size = 1;
147 if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0'))
148 MC_init_dot_output();
151 //parmap = xbt_parmap_mc_new(xbt_os_get_numcores(), XBT_PARMAP_DEFAULT);
155 if (_sg_mc_visited > 0 || _sg_mc_liveness || _sg_mc_termination || mc_mode == MC_MODE_SERVER) {
156 /* Ignore some variables from xbt/ex.h used by exception e for stacks comparison */
157 MC_ignore_local_variable("e", "*");
158 MC_ignore_local_variable("__ex_cleanup", "*");
159 MC_ignore_local_variable("__ex_mctx_en", "*");
160 MC_ignore_local_variable("__ex_mctx_me", "*");
161 MC_ignore_local_variable("__xbt_ex_ctx_ptr", "*");
162 MC_ignore_local_variable("_log_ev", "*");
163 MC_ignore_local_variable("_throw_ctx", "*");
164 MC_ignore_local_variable("ctx", "*");
166 MC_ignore_local_variable("self", "simcall_BODY_mc_snapshot");
167 MC_ignore_local_variable("next_cont"
168 "ext", "smx_ctx_sysv_suspend_serial");
169 MC_ignore_local_variable("i", "smx_ctx_sysv_suspend_serial");
171 /* Ignore local variable about time used for tracing */
172 MC_ignore_local_variable("start_time", "*");
175 MC_ignore_global_variable("mc_model_checker");
176 MC_ignore_global_variable("initial_communications_pattern");
177 MC_ignore_global_variable("incomplete_communications_pattern");
178 MC_ignore_global_variable("nb_comm_pattern");
180 /* MC __thread variables: */
181 MC_ignore_global_variable("mc_diff_info");
182 MC_ignore_global_variable("mc_comp_times");
183 MC_ignore_global_variable("mc_snapshot_comparison_time");
185 /* This MC state is used in MC replay as well: */
186 MC_ignore_global_variable("mc_time");
188 /* Static variable used for tracing */
189 MC_ignore_global_variable("counter");
192 MC_ignore_global_variable("smx_total_comms");
194 if (mc_mode == MC_MODE_STANDALONE || mc_mode == MC_MODE_CLIENT) {
195 /* Those requests are handled on the client side and propagated by message
198 MC_ignore_heap(mc_time, MC_smx_get_maxpid() * sizeof(double));
200 smx_process_t process;
201 xbt_swag_foreach(process, simix_global->process_list) {
202 MC_ignore_heap(&(process->process_hookup), sizeof(process->process_hookup));
208 mmalloc_set_current_heap(heap);
211 /******************************* Core of MC *******************************/
212 /**************************************************************************/
214 void MC_do_the_modelcheck_for_real()
220 xbt_die("Unexpected mc mode");
224 MC_client_main_loop();
227 case MC_MODE_STANDALONE:
231 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
232 XBT_INFO("Check communication determinism");
233 mc_reduce_kind = e_mc_reduce_none;
234 MC_wait_for_requests();
235 MC_modelcheck_comm_determinism();
238 else if (!_sg_mc_property_file || _sg_mc_property_file[0] == '\0') {
239 if(_sg_mc_termination)
240 mc_reduce_kind = e_mc_reduce_none;
241 else if (mc_reduce_kind == e_mc_reduce_unset)
242 mc_reduce_kind = e_mc_reduce_dpor;
244 if (_sg_mc_termination)
245 XBT_INFO("Check non progressive cycles");
247 XBT_INFO("Check a safety property");
248 MC_wait_for_requests();
249 MC_modelcheck_safety();
253 if (mc_reduce_kind == e_mc_reduce_unset)
254 mc_reduce_kind = e_mc_reduce_none;
255 XBT_INFO("Check the liveness property %s", _sg_mc_property_file);
256 MC_automaton_load(_sg_mc_property_file);
257 MC_wait_for_requests();
258 MC_modelcheck_liveness();
272 int MC_deadlock_check()
274 if (mc_mode == MC_MODE_SERVER) {
276 if ((res = MC_protocol_send_simple_message(mc_model_checker->process.socket,
277 MC_MESSAGE_DEADLOCK_CHECK)))
278 xbt_die("Could not check deadlock state");
279 s_mc_int_message_t message;
280 ssize_t s = MC_receive_message(mc_model_checker->process.socket, &message, sizeof(message), 0);
282 xbt_die("Could not receive message");
283 else if (s != sizeof(message) || message.type != MC_MESSAGE_DEADLOCK_CHECK_REPLY) {
284 xbt_die("%s received unexpected message %s (%i, size=%i) "
285 "expected MC_MESSAGE_DEADLOCK_CHECK_REPLY (%i, size=%i)",
286 MC_mode_name(mc_mode),
287 MC_message_type_name(message.type), (int) message.type, (int) s,
288 (int) MC_MESSAGE_DEADLOCK_CHECK_REPLY, (int) sizeof(message)
292 return message.value;
295 int deadlock = FALSE;
296 smx_process_t process;
297 if (xbt_swag_size(simix_global->process_list)) {
299 MC_EACH_SIMIX_PROCESS(process,
300 if (MC_process_is_enabled(process)) {
309 void MC_handle_comm_pattern(e_mc_call_type_t call_type, smx_simcall_t req, int value, xbt_dynar_t pattern, int backtracking) {
312 case MC_CALL_TYPE_NONE:
314 case MC_CALL_TYPE_SEND:
315 case MC_CALL_TYPE_RECV:
316 MC_get_comm_pattern(pattern, req, call_type, backtracking);
318 case MC_CALL_TYPE_WAIT:
319 case MC_CALL_TYPE_WAITANY:
321 smx_synchro_t current_comm = NULL;
322 if (call_type == MC_CALL_TYPE_WAIT)
323 current_comm = simcall_comm_wait__get__comm(req);
325 current_comm = xbt_dynar_get_as(simcall_comm_waitany__get__comms(req), value, smx_synchro_t);
326 MC_complete_comm_pattern(pattern, current_comm, req->issuer->pid, backtracking);
330 xbt_die("Unexpected call type %i", (int)call_type);
336 * \brief Re-executes from the state at position start all the transitions indicated by
337 * a given model-checker stack.
338 * \param stack The stack with the transitions to execute.
339 * \param start Start index to begin the re-execution.
341 void MC_replay(xbt_fifo_t stack)
343 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
345 int value, count = 1, j;
347 smx_simcall_t req = NULL, saved_req = NULL;
348 xbt_fifo_item_t item, start_item;
351 XBT_DEBUG("**** Begin Replay ****");
353 /* Intermediate backtracking */
354 if(_sg_mc_checkpoint > 0 || _sg_mc_termination || _sg_mc_visited > 0) {
355 start_item = xbt_fifo_get_first_item(stack);
356 state = (mc_state_t)xbt_fifo_get_item_content(start_item);
357 if(state->system_state){
358 MC_restore_snapshot(state->system_state);
359 if(_sg_mc_comms_determinism || _sg_mc_send_determinism)
360 MC_restore_communications_pattern(state);
367 /* Restore the initial state */
368 MC_restore_snapshot(initial_global_state->snapshot);
369 /* At the moment of taking the snapshot the raw heap was set, so restoring
370 * it will set it back again, we have to unset it to continue */
373 start_item = xbt_fifo_get_last_item(stack);
377 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
378 for (j=0; j < simix_process_maxpid; j++) {
379 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
380 ((mc_list_comm_pattern_t)xbt_dynar_get_as(initial_communications_pattern, j, mc_list_comm_pattern_t))->index_comm = 0;
386 /* Traverse the stack from the state at position start and re-execute the transitions */
387 for (item = start_item;
388 item != xbt_fifo_get_first_item(stack);
389 item = xbt_fifo_get_prev_item(item)) {
391 state = (mc_state_t) xbt_fifo_get_item_content(item);
392 saved_req = MC_state_get_executed_request(state, &value);
395 /* because we got a copy of the executed request, we have to fetch the
396 real one, pointed by the request field of the issuer process */
398 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
399 req = &issuer->simcall;
401 /* Debug information */
402 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
403 req_str = MC_request_to_string(req, value, MC_REQUEST_SIMIX);
404 XBT_DEBUG("Replay: %s (%p)", req_str, state);
408 /* TODO : handle test and testany simcalls */
409 e_mc_call_type_t call = MC_CALL_TYPE_NONE;
410 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
411 call = MC_get_call_type(req);
413 MC_simcall_handle(req, value);
416 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
417 MC_handle_comm_pattern(call, req, value, NULL, 1);
420 MC_wait_for_requests();
425 /* Update statistics */
426 mc_stats->visited_states++;
427 mc_stats->executed_transitions++;
431 XBT_DEBUG("**** End Replay ****");
432 mmalloc_set_current_heap(heap);
435 void MC_replay_liveness(xbt_fifo_t stack)
438 initial_global_state->raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
440 xbt_fifo_item_t item;
441 mc_pair_t pair = NULL;
442 mc_state_t state = NULL;
443 smx_simcall_t req = NULL, saved_req = NULL;
444 int value, depth = 1;
447 XBT_DEBUG("**** Begin Replay ****");
449 /* Intermediate backtracking */
450 if(_sg_mc_checkpoint > 0) {
451 item = xbt_fifo_get_first_item(stack);
452 pair = (mc_pair_t) xbt_fifo_get_item_content(item);
453 if(pair->graph_state->system_state){
454 MC_restore_snapshot(pair->graph_state->system_state);
460 /* Restore the initial state */
461 MC_restore_snapshot(initial_global_state->snapshot);
463 /* At the moment of taking the snapshot the raw heap was set, so restoring
464 * it will set it back again, we have to unset it to continue */
465 if (!initial_global_state->raw_mem_set)
468 /* Traverse the stack from the initial state and re-execute the transitions */
469 for (item = xbt_fifo_get_last_item(stack);
470 item != xbt_fifo_get_first_item(stack);
471 item = xbt_fifo_get_prev_item(item)) {
473 pair = (mc_pair_t) xbt_fifo_get_item_content(item);
475 state = (mc_state_t) pair->graph_state;
477 if (pair->exploration_started) {
479 saved_req = MC_state_get_executed_request(state, &value);
481 if (saved_req != NULL) {
482 /* because we got a copy of the executed request, we have to fetch the
483 real one, pointed by the request field of the issuer process */
484 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
485 req = &issuer->simcall;
487 /* Debug information */
488 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
489 req_str = MC_request_to_string(req, value, MC_REQUEST_SIMIX);
490 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
496 MC_simcall_handle(req, value);
497 MC_wait_for_requests();
500 /* Update statistics */
501 mc_stats->visited_pairs++;
502 mc_stats->executed_transitions++;
508 XBT_DEBUG("**** End Replay ****");
510 if (initial_global_state->raw_mem_set)
518 * \brief Dumps the contents of a model-checker's stack and shows the actual
520 * \param stack The stack to dump
522 void MC_dump_stack_safety(xbt_fifo_t stack)
524 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
526 MC_show_stack_safety(stack);
531 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
532 MC_state_delete(state, !state->in_visited_states ? 1 : 0);
533 mmalloc_set_current_heap(heap);
537 void MC_show_stack_safety(xbt_fifo_t stack)
539 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
543 xbt_fifo_item_t item;
545 char *req_str = NULL;
547 for (item = xbt_fifo_get_last_item(stack);
548 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
549 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
550 req = MC_state_get_executed_request(state, &value);
552 req_str = MC_request_to_string(req, value, MC_REQUEST_EXECUTED);
553 XBT_INFO("%s", req_str);
558 mmalloc_set_current_heap(heap);
561 void MC_show_deadlock(smx_simcall_t req)
563 /*char *req_str = NULL; */
564 XBT_INFO("**************************");
565 XBT_INFO("*** DEAD-LOCK DETECTED ***");
566 XBT_INFO("**************************");
567 XBT_INFO("Locked request:");
568 /*req_str = MC_request_to_string(req);
569 XBT_INFO("%s", req_str);
570 xbt_free(req_str); */
571 XBT_INFO("Counter-example execution trace:");
572 MC_dump_stack_safety(mc_stack);
573 MC_print_statistics(mc_stats);
576 void MC_show_non_termination(void){
577 XBT_INFO("******************************************");
578 XBT_INFO("*** NON-PROGRESSIVE CYCLE DETECTED ***");
579 XBT_INFO("******************************************");
580 XBT_INFO("Counter-example execution trace:");
581 MC_dump_stack_safety(mc_stack);
582 MC_print_statistics(mc_stats);
586 void MC_show_stack_liveness(xbt_fifo_t stack)
590 xbt_fifo_item_t item;
592 char *req_str = NULL;
594 for (item = xbt_fifo_get_last_item(stack);
595 (item ? (pair = (mc_pair_t) (xbt_fifo_get_item_content(item))) : (NULL));
596 item = xbt_fifo_get_prev_item(item)) {
597 req = MC_state_get_executed_request(pair->graph_state, &value);
598 if (req && req->call != SIMCALL_NONE) {
599 req_str = MC_request_to_string(req, value, MC_REQUEST_EXECUTED);
600 XBT_INFO("%s", req_str);
607 void MC_dump_stack_liveness(xbt_fifo_t stack)
609 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
611 while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != NULL)
612 MC_pair_delete(pair);
613 mmalloc_set_current_heap(heap);
616 void MC_print_statistics(mc_stats_t stats)
618 if(_sg_mc_comms_determinism) {
619 if (!initial_global_state->recv_deterministic && initial_global_state->send_deterministic){
620 XBT_INFO("******************************************************");
621 XBT_INFO("**** Only-send-deterministic communication pattern ****");
622 XBT_INFO("******************************************************");
623 XBT_INFO("%s", initial_global_state->recv_diff);
624 }else if(!initial_global_state->send_deterministic && initial_global_state->recv_deterministic) {
625 XBT_INFO("******************************************************");
626 XBT_INFO("**** Only-recv-deterministic communication pattern ****");
627 XBT_INFO("******************************************************");
628 XBT_INFO("%s", initial_global_state->send_diff);
632 if (stats->expanded_pairs == 0) {
633 XBT_INFO("Expanded states = %lu", stats->expanded_states);
634 XBT_INFO("Visited states = %lu", stats->visited_states);
636 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
637 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
639 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
640 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
641 if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0')) {
642 fprintf(dot_output, "}\n");
645 if (initial_global_state != NULL && (_sg_mc_comms_determinism || _sg_mc_send_determinism)) {
646 XBT_INFO("Send-deterministic : %s", !initial_global_state->send_deterministic ? "No" : "Yes");
647 if (_sg_mc_comms_determinism)
648 XBT_INFO("Recv-deterministic : %s", !initial_global_state->recv_deterministic ? "No" : "Yes");
650 mmalloc_set_current_heap(heap);
653 void MC_automaton_load(const char *file)
655 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
657 if (_mc_property_automaton == NULL)
658 _mc_property_automaton = xbt_automaton_new();
660 xbt_automaton_load(_mc_property_automaton, file);
661 mmalloc_set_current_heap(heap);
664 static void register_symbol(xbt_automaton_propositional_symbol_t symbol)
666 if (mc_mode != MC_MODE_CLIENT)
668 s_mc_register_symbol_message_t message;
669 message.type = MC_MESSAGE_REGISTER_SYMBOL;
670 const char* name = xbt_automaton_propositional_symbol_get_name(symbol);
671 if (strlen(name) + 1 > sizeof(message.name))
672 xbt_die("Symbol is too long");
673 strncpy(message.name, name, sizeof(message.name));
674 message.callback = xbt_automaton_propositional_symbol_get_callback(symbol);
675 message.data = xbt_automaton_propositional_symbol_get_data(symbol);
676 MC_client_send_message(&message, sizeof(message));
679 void MC_automaton_new_propositional_symbol(const char *id, int(*fct)(void))
681 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
683 if (_mc_property_automaton == NULL)
684 _mc_property_automaton = xbt_automaton_new();
686 xbt_automaton_propositional_symbol_t symbol = xbt_automaton_propositional_symbol_new(_mc_property_automaton, id, fct);
687 register_symbol(symbol);
688 mmalloc_set_current_heap(heap);
691 void MC_automaton_new_propositional_symbol_pointer(const char *id, int* value)
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_pointer(_mc_property_automaton, id, value);
697 register_symbol(symbol);
698 mmalloc_set_current_heap(heap);
701 void MC_automaton_new_propositional_symbol_callback(const char* id,
702 xbt_automaton_propositional_symbol_callback_type callback,
703 void* data, xbt_automaton_propositional_symbol_free_function_type free_function)
705 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
706 if (_mc_property_automaton == NULL)
707 _mc_property_automaton = xbt_automaton_new();
708 xbt_automaton_propositional_symbol_t symbol = xbt_automaton_propositional_symbol_new_callback(
709 _mc_property_automaton, id, callback, data, free_function);
710 register_symbol(symbol);
711 mmalloc_set_current_heap(heap);
714 // TODO, fix cross-process access (this function is not used)
715 void MC_dump_stacks(FILE* file)
717 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
720 stack_region_t current_stack;
722 xbt_dynar_foreach(stacks_areas, cursor, current_stack) {
723 unw_context_t * context = (unw_context_t *)current_stack->context;
724 fprintf(file, "Stack %i:\n", nstack);
729 unw_init_local (&c, context);
732 const char * name = !unw_get_proc_name(&c, buffer, 100, &off) ? buffer : "?";
733 #if defined(__x86_64__)
736 unw_get_reg(&c, UNW_X86_64_RIP, &rip);
737 unw_get_reg(&c, UNW_X86_64_RSP, &rsp);
738 fprintf(file, " %i: %s (RIP=0x%" PRIx64 " RSP=0x%" PRIx64 ")\n",
739 nframe, name, rip, rsp);
741 fprintf(file, " %i: %s\n", nframe, name);
744 } while(unw_step(&c));
748 mmalloc_set_current_heap(heap);
752 double MC_process_clock_get(smx_process_t process)
756 return mc_time[process->pid];
764 void MC_process_clock_add(smx_process_t process, double amount)
766 mc_time[process->pid] += amount;
769 void MC_report_assertion_error(void)
771 XBT_INFO("**************************");
772 XBT_INFO("*** PROPERTY NOT VALID ***");
773 XBT_INFO("**************************");
774 XBT_INFO("Counter-example execution trace:");
775 MC_record_dump_path(mc_stack);
776 MC_dump_stack_safety(mc_stack);
777 MC_print_statistics(mc_stats);