1 /* Copyright (c) 2008-2015. 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. */
23 #include <xbt/automaton.h>
25 #include "src/simix/smx_process_private.h"
28 #include <libunwind.h>
29 #include "src/mc/mc_comm_pattern.h"
30 #include "src/mc/mc_request.h"
31 #include "src/mc/mc_safety.h"
32 #include "src/mc/mc_snapshot.h"
33 #include "src/mc/mc_liveness.h"
34 #include "src/mc/mc_private.h"
35 #include "src/mc/mc_unw.h"
36 #include "src/mc/mc_smx.h"
39 #include "src/mc/mc_record.h"
40 #include "src/mc/mc_protocol.h"
41 #include "src/mc/mc_client.h"
45 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
46 "Logging specific to MC (global)");
50 double *mc_time = nullptr;
53 int user_max_depth_reached = 0;
55 /* MC global data structures */
56 mc_state_t mc_current_state = nullptr;
57 char mc_replay_mode = FALSE;
59 mc_stats_t mc_stats = nullptr;
60 mc_global_t initial_global_state = nullptr;
61 xbt_fifo_t mc_stack = nullptr;
64 xbt_automaton_t _mc_property_automaton = nullptr;
67 FILE *dot_output = nullptr;
68 const char *colors[13];
71 /******************************* Initialisation of MC *******************************/
72 /*********************************************************************************/
74 void MC_init_dot_output()
75 { /* FIXME : more colors */
80 colors[3] = "goldenrod";
83 colors[6] = "magenta";
84 colors[7] = "turquoise4";
86 colors[9] = "forestgreen";
87 colors[10] = "hotpink";
88 colors[11] = "lightblue";
91 dot_output = fopen(_sg_mc_dot_output_file, "w");
93 if (dot_output == nullptr) {
94 perror("Error open dot output file");
99 "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");
106 mc_time = xbt_new0(double, simix_process_maxpid);
108 if (_sg_mc_visited > 0 || _sg_mc_liveness || _sg_mc_termination || mc_mode == MC_MODE_SERVER) {
109 /* Those requests are handled on the client side and propagated by message
112 MC_ignore_heap(mc_time, simix_process_maxpid * sizeof(double));
114 smx_process_t process;
115 xbt_swag_foreach(process, simix_global->process_list) {
116 MC_ignore_heap(&(process->process_hookup), sizeof(process->process_hookup));
123 /******************************* Core of MC *******************************/
124 /**************************************************************************/
128 mc_mode = MC_MODE_CLIENT;
130 MC_client_main_loop();
142 int MC_deadlock_check()
144 if (mc_mode == MC_MODE_SERVER) {
146 if ((res = mc_model_checker->process().send_message(MC_MESSAGE_DEADLOCK_CHECK)))
147 xbt_die("Could not check deadlock state");
148 s_mc_int_message_t message;
149 ssize_t s = mc_model_checker->process().receive_message(message);
151 xbt_die("Could not receive message");
152 else if (s != sizeof(message) || message.type != MC_MESSAGE_DEADLOCK_CHECK_REPLY) {
153 xbt_die("%s received unexpected message %s (%i, size=%i) "
154 "expected MC_MESSAGE_DEADLOCK_CHECK_REPLY (%i, size=%i)",
155 MC_mode_name(mc_mode),
156 MC_message_type_name(message.type), (int) message.type, (int) s,
157 (int) MC_MESSAGE_DEADLOCK_CHECK_REPLY, (int) sizeof(message)
161 return message.value;
164 int deadlock = FALSE;
165 smx_process_t process;
166 if (xbt_swag_size(simix_global->process_list)) {
168 xbt_swag_foreach(process, simix_global->process_list) {
169 if (MC_process_is_enabled(process)) {
180 * \brief Re-executes from the state at position start all the transitions indicated by
181 * a given model-checker stack.
182 * \param stack The stack with the transitions to execute.
183 * \param start Start index to begin the re-execution.
185 void MC_replay(xbt_fifo_t stack)
187 int value, count = 1;
189 smx_simcall_t req = nullptr, saved_req = NULL;
190 xbt_fifo_item_t item, start_item;
193 XBT_DEBUG("**** Begin Replay ****");
195 /* Intermediate backtracking */
196 if(_sg_mc_checkpoint > 0 || _sg_mc_termination || _sg_mc_visited > 0) {
197 start_item = xbt_fifo_get_first_item(stack);
198 state = (mc_state_t)xbt_fifo_get_item_content(start_item);
199 if(state->system_state){
200 simgrid::mc::restore_snapshot(state->system_state);
201 if(_sg_mc_comms_determinism || _sg_mc_send_determinism)
202 MC_restore_communications_pattern(state);
208 /* Restore the initial state */
209 simgrid::mc::restore_snapshot(initial_global_state->snapshot);
210 /* At the moment of taking the snapshot the raw heap was set, so restoring
211 * it will set it back again, we have to unset it to continue */
213 start_item = xbt_fifo_get_last_item(stack);
215 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
216 // int n = xbt_dynar_length(incomplete_communications_pattern);
217 unsigned n = MC_smx_get_maxpid();
218 assert(n == xbt_dynar_length(incomplete_communications_pattern));
219 assert(n == xbt_dynar_length(initial_communications_pattern));
220 for (unsigned j=0; j < n ; j++) {
221 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
222 xbt_dynar_get_as(initial_communications_pattern, j, mc_list_comm_pattern_t)->index_comm = 0;
226 /* Traverse the stack from the state at position start and re-execute the transitions */
227 for (item = start_item;
228 item != xbt_fifo_get_first_item(stack);
229 item = xbt_fifo_get_prev_item(item)) {
231 state = (mc_state_t) xbt_fifo_get_item_content(item);
232 saved_req = MC_state_get_executed_request(state, &value);
235 /* because we got a copy of the executed request, we have to fetch the
236 real one, pointed by the request field of the issuer process */
238 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
239 req = &issuer->simcall;
241 /* Debug information */
242 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
243 req_str = MC_request_to_string(req, value, MC_REQUEST_SIMIX);
244 XBT_DEBUG("Replay: %s (%p)", req_str, state);
248 /* TODO : handle test and testany simcalls */
249 e_mc_call_type_t call = MC_CALL_TYPE_NONE;
250 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
251 call = MC_get_call_type(req);
253 MC_simcall_handle(req, value);
255 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
256 MC_handle_comm_pattern(call, req, value, nullptr, 1);
258 mc_model_checker->wait_for_requests();
263 /* Update statistics */
264 mc_stats->visited_states++;
265 mc_stats->executed_transitions++;
269 XBT_DEBUG("**** End Replay ****");
272 void MC_replay_liveness(xbt_fifo_t stack)
274 xbt_fifo_item_t item;
275 mc_pair_t pair = nullptr;
276 mc_state_t state = nullptr;
277 smx_simcall_t req = nullptr, saved_req = NULL;
278 int value, depth = 1;
281 XBT_DEBUG("**** Begin Replay ****");
283 /* Intermediate backtracking */
284 if(_sg_mc_checkpoint > 0) {
285 item = xbt_fifo_get_first_item(stack);
286 pair = (mc_pair_t) xbt_fifo_get_item_content(item);
287 if(pair->graph_state->system_state){
288 simgrid::mc::restore_snapshot(pair->graph_state->system_state);
293 /* Restore the initial state */
294 simgrid::mc::restore_snapshot(initial_global_state->snapshot);
296 /* Traverse the stack from the initial state and re-execute the transitions */
297 for (item = xbt_fifo_get_last_item(stack);
298 item != xbt_fifo_get_first_item(stack);
299 item = xbt_fifo_get_prev_item(item)) {
301 pair = (mc_pair_t) xbt_fifo_get_item_content(item);
303 state = (mc_state_t) pair->graph_state;
305 if (pair->exploration_started) {
307 saved_req = MC_state_get_executed_request(state, &value);
309 if (saved_req != nullptr) {
310 /* because we got a copy of the executed request, we have to fetch the
311 real one, pointed by the request field of the issuer process */
312 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
313 req = &issuer->simcall;
315 /* Debug information */
316 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
317 req_str = MC_request_to_string(req, value, MC_REQUEST_SIMIX);
318 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
324 MC_simcall_handle(req, value);
325 mc_model_checker->wait_for_requests();
328 /* Update statistics */
329 mc_stats->visited_pairs++;
330 mc_stats->executed_transitions++;
336 XBT_DEBUG("**** End Replay ****");
340 * \brief Dumps the contents of a model-checker's stack and shows the actual
342 * \param stack The stack to dump
344 void MC_dump_stack_safety(xbt_fifo_t stack)
346 MC_show_stack_safety(stack);
350 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != nullptr)
351 MC_state_delete(state, !state->in_visited_states ? 1 : 0);
355 void MC_show_stack_safety(xbt_fifo_t stack)
359 xbt_fifo_item_t item;
361 char *req_str = nullptr;
363 for (item = xbt_fifo_get_last_item(stack);
364 item; item = xbt_fifo_get_prev_item(item)) {
365 state = (mc_state_t)xbt_fifo_get_item_content(item);
366 req = MC_state_get_executed_request(state, &value);
368 req_str = MC_request_to_string(req, value, MC_REQUEST_EXECUTED);
369 XBT_INFO("%s", req_str);
375 void MC_show_deadlock(smx_simcall_t req)
377 /*char *req_str = nullptr; */
378 XBT_INFO("**************************");
379 XBT_INFO("*** DEAD-LOCK DETECTED ***");
380 XBT_INFO("**************************");
381 XBT_INFO("Locked request:");
382 /*req_str = MC_request_to_string(req);
383 XBT_INFO("%s", req_str);
384 xbt_free(req_str); */
385 XBT_INFO("Counter-example execution trace:");
386 MC_dump_stack_safety(mc_stack);
387 MC_print_statistics(mc_stats);
390 void MC_show_non_termination(void){
391 XBT_INFO("******************************************");
392 XBT_INFO("*** NON-PROGRESSIVE CYCLE DETECTED ***");
393 XBT_INFO("******************************************");
394 XBT_INFO("Counter-example execution trace:");
395 MC_dump_stack_safety(mc_stack);
396 MC_print_statistics(mc_stats);
400 void MC_show_stack_liveness(xbt_fifo_t stack)
404 xbt_fifo_item_t item;
406 char *req_str = nullptr;
408 for (item = xbt_fifo_get_last_item(stack);
409 item; item = xbt_fifo_get_prev_item(item)) {
410 pair = (mc_pair_t) xbt_fifo_get_item_content(item);
411 req = MC_state_get_executed_request(pair->graph_state, &value);
412 if (req && req->call != SIMCALL_NONE) {
413 req_str = MC_request_to_string(req, value, MC_REQUEST_EXECUTED);
414 XBT_INFO("%s", req_str);
421 void MC_dump_stack_liveness(xbt_fifo_t stack)
424 while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != nullptr)
425 MC_pair_delete(pair);
428 void MC_print_statistics(mc_stats_t stats)
430 if(_sg_mc_comms_determinism) {
431 if (!initial_global_state->recv_deterministic && initial_global_state->send_deterministic){
432 XBT_INFO("******************************************************");
433 XBT_INFO("**** Only-send-deterministic communication pattern ****");
434 XBT_INFO("******************************************************");
435 XBT_INFO("%s", initial_global_state->recv_diff);
436 }else if(!initial_global_state->send_deterministic && initial_global_state->recv_deterministic) {
437 XBT_INFO("******************************************************");
438 XBT_INFO("**** Only-recv-deterministic communication pattern ****");
439 XBT_INFO("******************************************************");
440 XBT_INFO("%s", initial_global_state->send_diff);
444 if (stats->expanded_pairs == 0) {
445 XBT_INFO("Expanded states = %lu", stats->expanded_states);
446 XBT_INFO("Visited states = %lu", stats->visited_states);
448 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
449 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
451 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
452 if ((_sg_mc_dot_output_file != nullptr) && (_sg_mc_dot_output_file[0] != '\0')) {
453 fprintf(dot_output, "}\n");
456 if (initial_global_state != nullptr && (_sg_mc_comms_determinism || _sg_mc_send_determinism)) {
457 XBT_INFO("Send-deterministic : %s", !initial_global_state->send_deterministic ? "No" : "Yes");
458 if (_sg_mc_comms_determinism)
459 XBT_INFO("Recv-deterministic : %s", !initial_global_state->recv_deterministic ? "No" : "Yes");
461 if (getenv("SIMGRID_MC_SYSTEM_STATISTICS")){
462 int ret=system("free");
463 if(ret!=0)XBT_WARN("system call did not return 0, but %d",ret);
467 void MC_automaton_load(const char *file)
469 if (_mc_property_automaton == nullptr)
470 _mc_property_automaton = xbt_automaton_new();
472 xbt_automaton_load(_mc_property_automaton, file);
475 // TODO, fix cross-process access (this function is not used)
476 static void MC_dump_stacks(FILE* file)
479 for (auto const& stack : mc_model_checker->process().stack_areas()) {
481 xbt_die("Fix cross-process access to the context");
482 unw_context_t * context = (unw_context_t *)stack.context;
483 fprintf(file, "Stack %i:\n", nstack);
488 unw_init_local (&c, context);
491 const char * name = !unw_get_proc_name(&c, buffer, 100, &off) ? buffer : "?";
492 #if defined(__x86_64__)
495 unw_get_reg(&c, UNW_X86_64_RIP, &rip);
496 unw_get_reg(&c, UNW_X86_64_RSP, &rsp);
497 fprintf(file, " %i: %s (RIP=0x%" PRIx64 " RSP=0x%" PRIx64 ")\n",
498 nframe, name, (std::uint64_t) rip, (std::uint64_t) rsp);
500 fprintf(file, " %i: %s\n", nframe, name);
503 } while(unw_step(&c));
510 double MC_process_clock_get(smx_process_t process)
513 if (process != nullptr)
514 return mc_time[process->pid];
522 void MC_process_clock_add(smx_process_t process, double amount)
524 mc_time[process->pid] += amount;
528 void MC_report_assertion_error(void)
530 XBT_INFO("**************************");
531 XBT_INFO("*** PROPERTY NOT VALID ***");
532 XBT_INFO("**************************");
533 XBT_INFO("Counter-example execution trace:");
534 MC_record_dump_path(mc_stack);
535 MC_dump_stack_safety(mc_stack);
536 MC_print_statistics(mc_stats);
539 void MC_report_crash(int status)
541 XBT_INFO("**************************");
542 XBT_INFO("** CRASH IN THE PROGRAM **");
543 XBT_INFO("**************************");
544 if (WIFSIGNALED(status))
545 XBT_INFO("From signal: %s", strsignal(WTERMSIG(status)));
546 else if (WIFEXITED(status))
547 XBT_INFO("From exit: %i", WEXITSTATUS(status));
548 if (WCOREDUMP(status))
549 XBT_INFO("A core dump was generated by the system.");
551 XBT_INFO("No core dump was generated by the system.");
552 XBT_INFO("Counter-example execution trace:");
553 MC_record_dump_path(mc_stack);
554 MC_dump_stack_safety(mc_stack);
555 MC_print_statistics(mc_stats);