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));
122 /******************************* Core of MC *******************************/
123 /**************************************************************************/
127 mc_mode = MC_MODE_CLIENT;
129 MC_client_main_loop();
141 int MC_deadlock_check()
143 if (mc_mode == MC_MODE_SERVER) {
145 if ((res = mc_model_checker->process().send_message(MC_MESSAGE_DEADLOCK_CHECK)))
146 xbt_die("Could not check deadlock state");
147 s_mc_int_message_t message;
148 ssize_t s = mc_model_checker->process().receive_message(message);
150 xbt_die("Could not receive message");
151 else if (s != sizeof(message) || message.type != MC_MESSAGE_DEADLOCK_CHECK_REPLY) {
152 xbt_die("%s received unexpected message %s (%i, size=%i) "
153 "expected MC_MESSAGE_DEADLOCK_CHECK_REPLY (%i, size=%i)",
154 MC_mode_name(mc_mode),
155 MC_message_type_name(message.type), (int) message.type, (int) s,
156 (int) MC_MESSAGE_DEADLOCK_CHECK_REPLY, (int) sizeof(message)
160 return message.value;
163 int deadlock = FALSE;
164 smx_process_t process;
165 if (xbt_swag_size(simix_global->process_list)) {
167 xbt_swag_foreach(process, simix_global->process_list)
168 if (MC_process_is_enabled(process)) {
178 * \brief Re-executes from the state at position start all the transitions indicated by
179 * a given model-checker stack.
180 * \param stack The stack with the transitions to execute.
181 * \param start Start index to begin the re-execution.
183 void MC_replay(xbt_fifo_t stack)
185 int value, count = 1;
187 smx_simcall_t req = nullptr, saved_req = NULL;
188 xbt_fifo_item_t item, start_item;
191 XBT_DEBUG("**** Begin Replay ****");
193 /* Intermediate backtracking */
194 if(_sg_mc_checkpoint > 0 || _sg_mc_termination || _sg_mc_visited > 0) {
195 start_item = xbt_fifo_get_first_item(stack);
196 state = (mc_state_t)xbt_fifo_get_item_content(start_item);
197 if(state->system_state){
198 simgrid::mc::restore_snapshot(state->system_state);
199 if(_sg_mc_comms_determinism || _sg_mc_send_determinism)
200 MC_restore_communications_pattern(state);
206 /* Restore the initial state */
207 simgrid::mc::restore_snapshot(initial_global_state->snapshot);
208 /* At the moment of taking the snapshot the raw heap was set, so restoring
209 * it will set it back again, we have to unset it to continue */
211 start_item = xbt_fifo_get_last_item(stack);
213 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
214 // int n = xbt_dynar_length(incomplete_communications_pattern);
215 unsigned n = MC_smx_get_maxpid();
216 assert(n == xbt_dynar_length(incomplete_communications_pattern));
217 assert(n == xbt_dynar_length(initial_communications_pattern));
218 for (unsigned j=0; j < n ; j++) {
219 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
220 xbt_dynar_get_as(initial_communications_pattern, j, mc_list_comm_pattern_t)->index_comm = 0;
224 /* Traverse the stack from the state at position start and re-execute the transitions */
225 for (item = start_item;
226 item != xbt_fifo_get_first_item(stack);
227 item = xbt_fifo_get_prev_item(item)) {
229 state = (mc_state_t) xbt_fifo_get_item_content(item);
230 saved_req = MC_state_get_executed_request(state, &value);
233 /* because we got a copy of the executed request, we have to fetch the
234 real one, pointed by the request field of the issuer process */
236 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
237 req = &issuer->simcall;
239 /* Debug information */
240 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
241 req_str = MC_request_to_string(req, value, MC_REQUEST_SIMIX);
242 XBT_DEBUG("Replay: %s (%p)", req_str, state);
246 /* TODO : handle test and testany simcalls */
247 e_mc_call_type_t call = MC_CALL_TYPE_NONE;
248 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
249 call = MC_get_call_type(req);
251 MC_simcall_handle(req, value);
253 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
254 MC_handle_comm_pattern(call, req, value, nullptr, 1);
256 mc_model_checker->wait_for_requests();
261 /* Update statistics */
262 mc_stats->visited_states++;
263 mc_stats->executed_transitions++;
267 XBT_DEBUG("**** End Replay ****");
270 void MC_replay_liveness(xbt_fifo_t stack)
272 xbt_fifo_item_t item;
273 mc_pair_t pair = nullptr;
274 mc_state_t state = nullptr;
275 smx_simcall_t req = nullptr, saved_req = NULL;
276 int value, depth = 1;
279 XBT_DEBUG("**** Begin Replay ****");
281 /* Intermediate backtracking */
282 if(_sg_mc_checkpoint > 0) {
283 item = xbt_fifo_get_first_item(stack);
284 pair = (mc_pair_t) xbt_fifo_get_item_content(item);
285 if(pair->graph_state->system_state){
286 simgrid::mc::restore_snapshot(pair->graph_state->system_state);
291 /* Restore the initial state */
292 simgrid::mc::restore_snapshot(initial_global_state->snapshot);
294 /* Traverse the stack from the initial state and re-execute the transitions */
295 for (item = xbt_fifo_get_last_item(stack);
296 item != xbt_fifo_get_first_item(stack);
297 item = xbt_fifo_get_prev_item(item)) {
299 pair = (mc_pair_t) xbt_fifo_get_item_content(item);
301 state = (mc_state_t) pair->graph_state;
303 if (pair->exploration_started) {
305 saved_req = MC_state_get_executed_request(state, &value);
307 if (saved_req != nullptr) {
308 /* because we got a copy of the executed request, we have to fetch the
309 real one, pointed by the request field of the issuer process */
310 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
311 req = &issuer->simcall;
313 /* Debug information */
314 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
315 req_str = MC_request_to_string(req, value, MC_REQUEST_SIMIX);
316 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
322 MC_simcall_handle(req, value);
323 mc_model_checker->wait_for_requests();
326 /* Update statistics */
327 mc_stats->visited_pairs++;
328 mc_stats->executed_transitions++;
334 XBT_DEBUG("**** End Replay ****");
338 * \brief Dumps the contents of a model-checker's stack and shows the actual
340 * \param stack The stack to dump
342 void MC_dump_stack_safety(xbt_fifo_t stack)
344 MC_show_stack_safety(stack);
348 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != nullptr)
349 MC_state_delete(state, !state->in_visited_states ? 1 : 0);
353 void MC_show_stack_safety(xbt_fifo_t stack)
357 xbt_fifo_item_t item;
359 char *req_str = nullptr;
361 for (item = xbt_fifo_get_last_item(stack);
362 item; item = xbt_fifo_get_prev_item(item)) {
363 state = (mc_state_t)xbt_fifo_get_item_content(item);
364 req = MC_state_get_executed_request(state, &value);
366 req_str = MC_request_to_string(req, value, MC_REQUEST_EXECUTED);
367 XBT_INFO("%s", req_str);
373 void MC_show_deadlock(smx_simcall_t req)
375 /*char *req_str = nullptr; */
376 XBT_INFO("**************************");
377 XBT_INFO("*** DEAD-LOCK DETECTED ***");
378 XBT_INFO("**************************");
379 XBT_INFO("Locked request:");
380 /*req_str = MC_request_to_string(req);
381 XBT_INFO("%s", req_str);
382 xbt_free(req_str); */
383 XBT_INFO("Counter-example execution trace:");
384 MC_dump_stack_safety(mc_stack);
385 MC_print_statistics(mc_stats);
388 void MC_show_non_termination(void){
389 XBT_INFO("******************************************");
390 XBT_INFO("*** NON-PROGRESSIVE CYCLE DETECTED ***");
391 XBT_INFO("******************************************");
392 XBT_INFO("Counter-example execution trace:");
393 MC_dump_stack_safety(mc_stack);
394 MC_print_statistics(mc_stats);
398 void MC_show_stack_liveness(xbt_fifo_t stack)
402 xbt_fifo_item_t item;
404 char *req_str = nullptr;
406 for (item = xbt_fifo_get_last_item(stack);
407 item; item = xbt_fifo_get_prev_item(item)) {
408 pair = (mc_pair_t) xbt_fifo_get_item_content(item);
409 req = MC_state_get_executed_request(pair->graph_state, &value);
410 if (req && req->call != SIMCALL_NONE) {
411 req_str = MC_request_to_string(req, value, MC_REQUEST_EXECUTED);
412 XBT_INFO("%s", req_str);
419 void MC_dump_stack_liveness(xbt_fifo_t stack)
422 while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != nullptr)
423 MC_pair_delete(pair);
426 void MC_print_statistics(mc_stats_t stats)
428 if(_sg_mc_comms_determinism) {
429 if (!initial_global_state->recv_deterministic && initial_global_state->send_deterministic){
430 XBT_INFO("******************************************************");
431 XBT_INFO("**** Only-send-deterministic communication pattern ****");
432 XBT_INFO("******************************************************");
433 XBT_INFO("%s", initial_global_state->recv_diff);
434 }else if(!initial_global_state->send_deterministic && initial_global_state->recv_deterministic) {
435 XBT_INFO("******************************************************");
436 XBT_INFO("**** Only-recv-deterministic communication pattern ****");
437 XBT_INFO("******************************************************");
438 XBT_INFO("%s", initial_global_state->send_diff);
442 if (stats->expanded_pairs == 0) {
443 XBT_INFO("Expanded states = %lu", stats->expanded_states);
444 XBT_INFO("Visited states = %lu", stats->visited_states);
446 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
447 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
449 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
450 if ((_sg_mc_dot_output_file != nullptr) && (_sg_mc_dot_output_file[0] != '\0')) {
451 fprintf(dot_output, "}\n");
454 if (initial_global_state != nullptr && (_sg_mc_comms_determinism || _sg_mc_send_determinism)) {
455 XBT_INFO("Send-deterministic : %s", !initial_global_state->send_deterministic ? "No" : "Yes");
456 if (_sg_mc_comms_determinism)
457 XBT_INFO("Recv-deterministic : %s", !initial_global_state->recv_deterministic ? "No" : "Yes");
459 if (getenv("SIMGRID_MC_SYSTEM_STATISTICS")){
460 int ret=system("free");
461 if(ret!=0)XBT_WARN("system call did not return 0, but %d",ret);
465 void MC_automaton_load(const char *file)
467 if (_mc_property_automaton == nullptr)
468 _mc_property_automaton = xbt_automaton_new();
470 xbt_automaton_load(_mc_property_automaton, file);
473 // TODO, fix cross-process access (this function is not used)
474 static void MC_dump_stacks(FILE* file)
477 for (auto const& stack : mc_model_checker->process().stack_areas()) {
479 xbt_die("Fix cross-process access to the context");
480 unw_context_t * context = (unw_context_t *)stack.context;
481 fprintf(file, "Stack %i:\n", nstack);
486 unw_init_local (&c, context);
489 const char * name = !unw_get_proc_name(&c, buffer, 100, &off) ? buffer : "?";
490 #if defined(__x86_64__)
493 unw_get_reg(&c, UNW_X86_64_RIP, &rip);
494 unw_get_reg(&c, UNW_X86_64_RSP, &rsp);
495 fprintf(file, " %i: %s (RIP=0x%" PRIx64 " RSP=0x%" PRIx64 ")\n",
496 nframe, name, (std::uint64_t) rip, (std::uint64_t) rsp);
498 fprintf(file, " %i: %s\n", nframe, name);
501 } while(unw_step(&c));
508 double MC_process_clock_get(smx_process_t process)
511 if (process != nullptr)
512 return mc_time[process->pid];
519 void MC_process_clock_add(smx_process_t process, double amount)
521 mc_time[process->pid] += amount;
525 void MC_report_assertion_error(void)
527 XBT_INFO("**************************");
528 XBT_INFO("*** PROPERTY NOT VALID ***");
529 XBT_INFO("**************************");
530 XBT_INFO("Counter-example execution trace:");
531 MC_record_dump_path(mc_stack);
532 MC_dump_stack_safety(mc_stack);
533 MC_print_statistics(mc_stats);
536 void MC_report_crash(int status)
538 XBT_INFO("**************************");
539 XBT_INFO("** CRASH IN THE PROGRAM **");
540 XBT_INFO("**************************");
541 if (WIFSIGNALED(status))
542 XBT_INFO("From signal: %s", strsignal(WTERMSIG(status)));
543 else if (WIFEXITED(status))
544 XBT_INFO("From exit: %i", WEXITSTATUS(status));
545 if (WCOREDUMP(status))
546 XBT_INFO("A core dump was generated by the system.");
548 XBT_INFO("No core dump was generated by the system.");
549 XBT_INFO("Counter-example execution trace:");
550 MC_record_dump_path(mc_stack);
551 MC_dump_stack_safety(mc_stack);
552 MC_print_statistics(mc_stats);