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"
47 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
48 "Logging specific to MC (global)");
52 double *mc_time = NULL;
55 int user_max_depth_reached = 0;
57 /* MC global data structures */
58 mc_state_t mc_current_state = NULL;
59 char mc_replay_mode = FALSE;
61 __thread mc_comparison_times_t mc_comp_times = NULL;
62 __thread double mc_snapshot_comparison_time;
63 mc_stats_t mc_stats = NULL;
64 mc_global_t initial_global_state = NULL;
65 xbt_fifo_t mc_stack = NULL;
68 xbt_automaton_t _mc_property_automaton = NULL;
71 FILE *dot_output = NULL;
72 const char *colors[13];
75 /******************************* Initialisation of MC *******************************/
76 /*********************************************************************************/
78 static void MC_init_dot_output()
79 { /* FIXME : more colors */
84 colors[3] = "goldenrod";
87 colors[6] = "magenta";
88 colors[7] = "turquoise4";
90 colors[9] = "forestgreen";
91 colors[10] = "hotpink";
92 colors[11] = "lightblue";
95 dot_output = fopen(_sg_mc_dot_output_file, "w");
97 if (dot_output == NULL) {
98 perror("Error open dot output file");
103 "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 MC_init_pid(getpid(), -1);
112 static void MC_init_mode(void)
114 if (mc_mode == MC_MODE_NONE) {
115 if (getenv(MC_ENV_SOCKET_FD)) {
116 mc_mode = MC_MODE_CLIENT;
118 mc_mode = MC_MODE_STANDALONE;
123 void MC_init_pid(pid_t pid, int socket)
125 /* Initialize the data structures that must be persistent across every
126 iteration of the model-checker (in RAW memory) */
128 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
129 mc_model_checker = MC_model_checker_new(pid, socket);
131 // It's not useful anymore:
132 if (0 && mc_mode == MC_MODE_SERVER) {
133 unsigned long maxpid;
134 MC_process_read_variable(&mc_model_checker->process, "simix_process_maxpid",
135 &maxpid, sizeof(maxpid));
136 simix_process_maxpid = maxpid;
139 mmalloc_set_current_heap(std_heap);
140 mc_time = xbt_new0(double, MC_smx_get_maxpid());
141 mmalloc_set_current_heap(mc_heap);
143 mc_comp_times = xbt_new0(s_mc_comparison_times_t, 1);
145 /* Initialize statistics */
146 mc_stats = xbt_new0(s_mc_stats_t, 1);
147 mc_stats->state_size = 1;
149 if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0'))
150 MC_init_dot_output();
153 //parmap = xbt_parmap_mc_new(xbt_os_get_numcores(), XBT_PARMAP_DEFAULT);
157 if (_sg_mc_visited > 0 || _sg_mc_liveness || _sg_mc_termination || mc_mode == MC_MODE_SERVER) {
158 /* Ignore some variables from xbt/ex.h used by exception e for stacks comparison */
159 MC_ignore_local_variable("e", "*");
160 MC_ignore_local_variable("__ex_cleanup", "*");
161 MC_ignore_local_variable("__ex_mctx_en", "*");
162 MC_ignore_local_variable("__ex_mctx_me", "*");
163 MC_ignore_local_variable("__xbt_ex_ctx_ptr", "*");
164 MC_ignore_local_variable("_log_ev", "*");
165 MC_ignore_local_variable("_throw_ctx", "*");
166 MC_ignore_local_variable("ctx", "*");
168 MC_ignore_local_variable("self", "simcall_BODY_mc_snapshot");
169 MC_ignore_local_variable("next_cont"
170 "ext", "smx_ctx_sysv_suspend_serial");
171 MC_ignore_local_variable("i", "smx_ctx_sysv_suspend_serial");
173 /* Ignore local variable about time used for tracing */
174 MC_ignore_local_variable("start_time", "*");
177 MC_ignore_global_variable("mc_model_checker");
178 MC_ignore_global_variable("initial_communications_pattern");
179 MC_ignore_global_variable("incomplete_communications_pattern");
180 MC_ignore_global_variable("nb_comm_pattern");
182 /* MC __thread variables: */
183 MC_ignore_global_variable("mc_diff_info");
184 MC_ignore_global_variable("mc_comp_times");
185 MC_ignore_global_variable("mc_snapshot_comparison_time");
187 /* This MC state is used in MC replay as well: */
188 MC_ignore_global_variable("mc_time");
190 /* Static variable used for tracing */
191 MC_ignore_global_variable("counter");
194 MC_ignore_global_variable("smx_total_comms");
196 if (mc_mode == MC_MODE_STANDALONE || mc_mode == MC_MODE_CLIENT) {
197 /* Those requests are handled on the client side and propagated by message
200 MC_ignore_heap(mc_time, MC_smx_get_maxpid() * sizeof(double));
202 smx_process_t process;
203 xbt_swag_foreach(process, simix_global->process_list) {
204 MC_ignore_heap(&(process->process_hookup), sizeof(process->process_hookup));
210 mmalloc_set_current_heap(heap);
213 /******************************* Core of MC *******************************/
214 /**************************************************************************/
216 void MC_do_the_modelcheck_for_real()
222 xbt_die("Unexpected mc mode");
226 MC_client_main_loop();
230 case MC_MODE_STANDALONE:
235 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
236 XBT_INFO("Check communication determinism");
237 mc_reduce_kind = e_mc_reduce_none;
238 MC_wait_for_requests();
239 MC_modelcheck_comm_determinism();
242 else if (!_sg_mc_property_file || _sg_mc_property_file[0] == '\0') {
243 if(_sg_mc_termination)
244 mc_reduce_kind = e_mc_reduce_none;
245 else if (mc_reduce_kind == e_mc_reduce_unset)
246 mc_reduce_kind = e_mc_reduce_dpor;
248 if (_sg_mc_termination)
249 XBT_INFO("Check non progressive cycles");
251 XBT_INFO("Check a safety property");
252 MC_wait_for_requests();
253 MC_modelcheck_safety();
257 if (mc_reduce_kind == e_mc_reduce_unset)
258 mc_reduce_kind = e_mc_reduce_none;
259 XBT_INFO("Check the liveness property %s", _sg_mc_property_file);
260 MC_automaton_load(_sg_mc_property_file);
261 MC_wait_for_requests();
262 MC_modelcheck_liveness();
276 int MC_deadlock_check()
278 if (mc_mode == MC_MODE_SERVER) {
280 if ((res = MC_protocol_send_simple_message(mc_model_checker->process.socket,
281 MC_MESSAGE_DEADLOCK_CHECK)))
282 xbt_die("Could not check deadlock state");
283 s_mc_int_message_t message;
284 ssize_t s = MC_receive_message(mc_model_checker->process.socket, &message, sizeof(message), 0);
286 xbt_die("Could not receive message");
287 else if (s != sizeof(message) || message.type != MC_MESSAGE_DEADLOCK_CHECK_REPLY) {
288 xbt_die("%s received unexpected message %s (%i, size=%i) "
289 "expected MC_MESSAGE_DEADLOCK_CHECK_REPLY (%i, size=%i)",
290 MC_mode_name(mc_mode),
291 MC_message_type_name(message.type), (int) message.type, (int) s,
292 (int) MC_MESSAGE_DEADLOCK_CHECK_REPLY, (int) sizeof(message)
296 return message.value;
299 int deadlock = FALSE;
300 smx_process_t process;
301 if (xbt_swag_size(simix_global->process_list)) {
303 MC_EACH_SIMIX_PROCESS(process,
304 if (MC_process_is_enabled(process)) {
314 * \brief Re-executes from the state at position start all the transitions indicated by
315 * a given model-checker stack.
316 * \param stack The stack with the transitions to execute.
317 * \param start Start index to begin the re-execution.
319 void MC_replay(xbt_fifo_t stack)
321 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
323 int value, count = 1, j;
325 smx_simcall_t req = NULL, saved_req = NULL;
326 xbt_fifo_item_t item, start_item;
329 XBT_DEBUG("**** Begin Replay ****");
331 /* Intermediate backtracking */
332 if(_sg_mc_checkpoint > 0 || _sg_mc_termination || _sg_mc_visited > 0) {
333 start_item = xbt_fifo_get_first_item(stack);
334 state = (mc_state_t)xbt_fifo_get_item_content(start_item);
335 if(state->system_state){
336 MC_restore_snapshot(state->system_state);
337 if(_sg_mc_comms_determinism || _sg_mc_send_determinism)
338 MC_restore_communications_pattern(state);
345 /* Restore the initial state */
346 MC_restore_snapshot(initial_global_state->snapshot);
347 /* At the moment of taking the snapshot the raw heap was set, so restoring
348 * it will set it back again, we have to unset it to continue */
351 start_item = xbt_fifo_get_last_item(stack);
355 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
356 // int n = xbt_dynar_length(incomplete_communications_pattern);
357 int n = MC_smx_get_maxpid();
358 assert(n == xbt_dynar_length(incomplete_communications_pattern));
359 assert(n == xbt_dynar_length(initial_communications_pattern));
360 for (j=0; j < n ; j++) {
361 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
362 xbt_dynar_get_as(initial_communications_pattern, j, mc_list_comm_pattern_t)->index_comm = 0;
368 /* Traverse the stack from the state at position start and re-execute the transitions */
369 for (item = start_item;
370 item != xbt_fifo_get_first_item(stack);
371 item = xbt_fifo_get_prev_item(item)) {
373 state = (mc_state_t) xbt_fifo_get_item_content(item);
374 saved_req = MC_state_get_executed_request(state, &value);
377 /* because we got a copy of the executed request, we have to fetch the
378 real one, pointed by the request field of the issuer process */
380 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
381 req = &issuer->simcall;
383 /* Debug information */
384 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
385 req_str = MC_request_to_string(req, value, MC_REQUEST_SIMIX);
386 XBT_DEBUG("Replay: %s (%p)", req_str, state);
390 /* TODO : handle test and testany simcalls */
391 e_mc_call_type_t call = MC_CALL_TYPE_NONE;
392 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
393 call = MC_get_call_type(req);
395 MC_simcall_handle(req, value);
398 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
399 MC_handle_comm_pattern(call, req, value, NULL, 1);
402 MC_wait_for_requests();
407 /* Update statistics */
408 mc_stats->visited_states++;
409 mc_stats->executed_transitions++;
413 XBT_DEBUG("**** End Replay ****");
414 mmalloc_set_current_heap(heap);
417 void MC_replay_liveness(xbt_fifo_t stack)
420 initial_global_state->raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
422 xbt_fifo_item_t item;
423 mc_pair_t pair = NULL;
424 mc_state_t state = NULL;
425 smx_simcall_t req = NULL, saved_req = NULL;
426 int value, depth = 1;
429 XBT_DEBUG("**** Begin Replay ****");
431 /* Intermediate backtracking */
432 if(_sg_mc_checkpoint > 0) {
433 item = xbt_fifo_get_first_item(stack);
434 pair = (mc_pair_t) xbt_fifo_get_item_content(item);
435 if(pair->graph_state->system_state){
436 MC_restore_snapshot(pair->graph_state->system_state);
442 /* Restore the initial state */
443 MC_restore_snapshot(initial_global_state->snapshot);
445 /* At the moment of taking the snapshot the raw heap was set, so restoring
446 * it will set it back again, we have to unset it to continue */
447 if (!initial_global_state->raw_mem_set)
450 /* Traverse the stack from the initial state and re-execute the transitions */
451 for (item = xbt_fifo_get_last_item(stack);
452 item != xbt_fifo_get_first_item(stack);
453 item = xbt_fifo_get_prev_item(item)) {
455 pair = (mc_pair_t) xbt_fifo_get_item_content(item);
457 state = (mc_state_t) pair->graph_state;
459 if (pair->exploration_started) {
461 saved_req = MC_state_get_executed_request(state, &value);
463 if (saved_req != NULL) {
464 /* because we got a copy of the executed request, we have to fetch the
465 real one, pointed by the request field of the issuer process */
466 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
467 req = &issuer->simcall;
469 /* Debug information */
470 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
471 req_str = MC_request_to_string(req, value, MC_REQUEST_SIMIX);
472 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
478 MC_simcall_handle(req, value);
479 MC_wait_for_requests();
482 /* Update statistics */
483 mc_stats->visited_pairs++;
484 mc_stats->executed_transitions++;
490 XBT_DEBUG("**** End Replay ****");
492 if (initial_global_state->raw_mem_set)
500 * \brief Dumps the contents of a model-checker's stack and shows the actual
502 * \param stack The stack to dump
504 void MC_dump_stack_safety(xbt_fifo_t stack)
506 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
508 MC_show_stack_safety(stack);
513 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
514 MC_state_delete(state, !state->in_visited_states ? 1 : 0);
515 mmalloc_set_current_heap(heap);
519 void MC_show_stack_safety(xbt_fifo_t stack)
521 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
525 xbt_fifo_item_t item;
527 char *req_str = NULL;
529 for (item = xbt_fifo_get_last_item(stack);
530 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
531 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
532 req = MC_state_get_executed_request(state, &value);
534 req_str = MC_request_to_string(req, value, MC_REQUEST_EXECUTED);
535 XBT_INFO("%s", req_str);
540 mmalloc_set_current_heap(heap);
543 void MC_show_deadlock(smx_simcall_t req)
545 /*char *req_str = NULL; */
546 XBT_INFO("**************************");
547 XBT_INFO("*** DEAD-LOCK DETECTED ***");
548 XBT_INFO("**************************");
549 XBT_INFO("Locked request:");
550 /*req_str = MC_request_to_string(req);
551 XBT_INFO("%s", req_str);
552 xbt_free(req_str); */
553 XBT_INFO("Counter-example execution trace:");
554 MC_dump_stack_safety(mc_stack);
555 MC_print_statistics(mc_stats);
558 void MC_show_non_termination(void){
559 XBT_INFO("******************************************");
560 XBT_INFO("*** NON-PROGRESSIVE CYCLE DETECTED ***");
561 XBT_INFO("******************************************");
562 XBT_INFO("Counter-example execution trace:");
563 MC_dump_stack_safety(mc_stack);
564 MC_print_statistics(mc_stats);
568 void MC_show_stack_liveness(xbt_fifo_t stack)
572 xbt_fifo_item_t item;
574 char *req_str = NULL;
576 for (item = xbt_fifo_get_last_item(stack);
577 (item ? (pair = (mc_pair_t) (xbt_fifo_get_item_content(item))) : (NULL));
578 item = xbt_fifo_get_prev_item(item)) {
579 req = MC_state_get_executed_request(pair->graph_state, &value);
580 if (req && req->call != SIMCALL_NONE) {
581 req_str = MC_request_to_string(req, value, MC_REQUEST_EXECUTED);
582 XBT_INFO("%s", req_str);
589 void MC_dump_stack_liveness(xbt_fifo_t stack)
591 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
593 while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != NULL)
594 MC_pair_delete(pair);
595 mmalloc_set_current_heap(heap);
598 void MC_print_statistics(mc_stats_t stats)
600 if(_sg_mc_comms_determinism) {
601 if (!initial_global_state->recv_deterministic && initial_global_state->send_deterministic){
602 XBT_INFO("******************************************************");
603 XBT_INFO("**** Only-send-deterministic communication pattern ****");
604 XBT_INFO("******************************************************");
605 XBT_INFO("%s", initial_global_state->recv_diff);
606 }else if(!initial_global_state->send_deterministic && initial_global_state->recv_deterministic) {
607 XBT_INFO("******************************************************");
608 XBT_INFO("**** Only-recv-deterministic communication pattern ****");
609 XBT_INFO("******************************************************");
610 XBT_INFO("%s", initial_global_state->send_diff);
614 if (stats->expanded_pairs == 0) {
615 XBT_INFO("Expanded states = %lu", stats->expanded_states);
616 XBT_INFO("Visited states = %lu", stats->visited_states);
618 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
619 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
621 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
622 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
623 if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0')) {
624 fprintf(dot_output, "}\n");
627 if (initial_global_state != NULL && (_sg_mc_comms_determinism || _sg_mc_send_determinism)) {
628 XBT_INFO("Send-deterministic : %s", !initial_global_state->send_deterministic ? "No" : "Yes");
629 if (_sg_mc_comms_determinism)
630 XBT_INFO("Recv-deterministic : %s", !initial_global_state->recv_deterministic ? "No" : "Yes");
632 mmalloc_set_current_heap(heap);
635 void MC_automaton_load(const char *file)
637 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
639 if (_mc_property_automaton == NULL)
640 _mc_property_automaton = xbt_automaton_new();
642 xbt_automaton_load(_mc_property_automaton, file);
643 mmalloc_set_current_heap(heap);
646 static void register_symbol(xbt_automaton_propositional_symbol_t symbol)
648 if (mc_mode != MC_MODE_CLIENT)
650 s_mc_register_symbol_message_t message;
651 message.type = MC_MESSAGE_REGISTER_SYMBOL;
652 const char* name = xbt_automaton_propositional_symbol_get_name(symbol);
653 if (strlen(name) + 1 > sizeof(message.name))
654 xbt_die("Symbol is too long");
655 strncpy(message.name, name, sizeof(message.name));
656 message.callback = xbt_automaton_propositional_symbol_get_callback(symbol);
657 message.data = xbt_automaton_propositional_symbol_get_data(symbol);
658 MC_client_send_message(&message, sizeof(message));
661 void MC_automaton_new_propositional_symbol(const char *id, int(*fct)(void))
663 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
665 if (_mc_property_automaton == NULL)
666 _mc_property_automaton = xbt_automaton_new();
668 xbt_automaton_propositional_symbol_t symbol = xbt_automaton_propositional_symbol_new(_mc_property_automaton, id, fct);
669 register_symbol(symbol);
670 mmalloc_set_current_heap(heap);
673 void MC_automaton_new_propositional_symbol_pointer(const char *id, int* value)
675 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
676 if (_mc_property_automaton == NULL)
677 _mc_property_automaton = xbt_automaton_new();
678 xbt_automaton_propositional_symbol_t symbol = xbt_automaton_propositional_symbol_new_pointer(_mc_property_automaton, id, value);
679 register_symbol(symbol);
680 mmalloc_set_current_heap(heap);
683 void MC_automaton_new_propositional_symbol_callback(const char* id,
684 xbt_automaton_propositional_symbol_callback_type callback,
685 void* data, xbt_automaton_propositional_symbol_free_function_type free_function)
687 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
688 if (_mc_property_automaton == NULL)
689 _mc_property_automaton = xbt_automaton_new();
690 xbt_automaton_propositional_symbol_t symbol = xbt_automaton_propositional_symbol_new_callback(
691 _mc_property_automaton, id, callback, data, free_function);
692 register_symbol(symbol);
693 mmalloc_set_current_heap(heap);
696 // TODO, fix cross-process access (this function is not used)
697 void MC_dump_stacks(FILE* file)
699 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
702 stack_region_t current_stack;
704 xbt_dynar_foreach(stacks_areas, cursor, current_stack) {
705 unw_context_t * context = (unw_context_t *)current_stack->context;
706 fprintf(file, "Stack %i:\n", nstack);
711 unw_init_local (&c, context);
714 const char * name = !unw_get_proc_name(&c, buffer, 100, &off) ? buffer : "?";
715 #if defined(__x86_64__)
718 unw_get_reg(&c, UNW_X86_64_RIP, &rip);
719 unw_get_reg(&c, UNW_X86_64_RSP, &rsp);
720 fprintf(file, " %i: %s (RIP=0x%" PRIx64 " RSP=0x%" PRIx64 ")\n",
721 nframe, name, rip, rsp);
723 fprintf(file, " %i: %s\n", nframe, name);
726 } while(unw_step(&c));
730 mmalloc_set_current_heap(heap);
734 double MC_process_clock_get(smx_process_t process)
738 return mc_time[process->pid];
746 void MC_process_clock_add(smx_process_t process, double amount)
748 mc_time[process->pid] += amount;
752 void MC_report_assertion_error(void)
754 XBT_INFO("**************************");
755 XBT_INFO("*** PROPERTY NOT VALID ***");
756 XBT_INFO("**************************");
757 XBT_INFO("Counter-example execution trace:");
758 MC_record_dump_path(mc_stack);
759 MC_dump_stack_safety(mc_stack);
760 MC_print_statistics(mc_stats);