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. */
22 #include "simgrid/sg_config.h"
23 #include "../surf/surf_private.h"
24 #include "../simix/smx_private.h"
26 #include "xbt/automaton.h"
28 #include "mc_record.h"
31 #include "src/mc/Server.hpp"
32 #include <libunwind.h>
33 #include <xbt/mmalloc.h>
34 #include "../xbt/mmalloc/mmprivate.h"
35 #include "mc_object_info.h"
36 #include "mc_comm_pattern.h"
37 #include "mc_request.h"
38 #include "mc_safety.h"
39 #include "mc_snapshot.h"
40 #include "mc_liveness.h"
41 #include "mc_private.h"
44 #include "mcer_ignore.h"
46 #include "mc_record.h"
47 #include "mc_protocol.h"
48 #include "mc_client.h"
52 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
53 "Logging specific to MC (global)");
57 double *mc_time = NULL;
60 int user_max_depth_reached = 0;
62 /* MC global data structures */
63 mc_state_t mc_current_state = NULL;
64 char mc_replay_mode = FALSE;
66 __thread mc_comparison_times_t mc_comp_times = NULL;
67 __thread double mc_snapshot_comparison_time;
68 mc_stats_t mc_stats = NULL;
69 mc_global_t initial_global_state = NULL;
70 xbt_fifo_t mc_stack = NULL;
73 xbt_automaton_t _mc_property_automaton = NULL;
76 FILE *dot_output = NULL;
77 const char *colors[13];
80 /******************************* Initialisation of MC *******************************/
81 /*********************************************************************************/
83 static void MC_init_dot_output()
84 { /* FIXME : more colors */
89 colors[3] = "goldenrod";
92 colors[6] = "magenta";
93 colors[7] = "turquoise4";
95 colors[9] = "forestgreen";
96 colors[10] = "hotpink";
97 colors[11] = "lightblue";
100 dot_output = fopen(_sg_mc_dot_output_file, "w");
102 if (dot_output == NULL) {
103 perror("Error open dot output file");
108 "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");
115 mc_time = xbt_new0(double, simix_process_maxpid);
117 if (_sg_mc_visited > 0 || _sg_mc_liveness || _sg_mc_termination || mc_mode == MC_MODE_SERVER) {
118 /* Those requests are handled on the client side and propagated by message
121 MC_ignore_heap(mc_time, simix_process_maxpid * sizeof(double));
123 smx_process_t process;
124 xbt_swag_foreach(process, simix_global->process_list) {
125 MC_ignore_heap(&(process->process_hookup), sizeof(process->process_hookup));
130 void MC_init_model_checker(pid_t pid, int socket)
132 mc_model_checker = new simgrid::mc::ModelChecker(pid, socket);
134 // TODO, avoid direct dependency on sg_cfg
135 mc_model_checker->process().privatized(sg_cfg_get_boolean("smpi/privatize_global_variables"));
137 mc_comp_times = xbt_new0(s_mc_comparison_times_t, 1);
139 /* Initialize statistics */
140 mc_stats = xbt_new0(s_mc_stats_t, 1);
141 mc_stats->state_size = 1;
143 if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0'))
144 MC_init_dot_output();
147 //parmap = xbt_parmap_mc_new(xbt_os_get_numcores(), XBT_PARMAP_DEFAULT);
149 /* Ignore some variables from xbt/ex.h used by exception e for stacks comparison */
150 MC_ignore_local_variable("e", "*");
151 MC_ignore_local_variable("__ex_cleanup", "*");
152 MC_ignore_local_variable("__ex_mctx_en", "*");
153 MC_ignore_local_variable("__ex_mctx_me", "*");
154 MC_ignore_local_variable("__xbt_ex_ctx_ptr", "*");
155 MC_ignore_local_variable("_log_ev", "*");
156 MC_ignore_local_variable("_throw_ctx", "*");
157 MC_ignore_local_variable("ctx", "*");
159 MC_ignore_local_variable("self", "simcall_BODY_mc_snapshot");
160 MC_ignore_local_variable("next_cont"
161 "ext", "smx_ctx_sysv_suspend_serial");
162 MC_ignore_local_variable("i", "smx_ctx_sysv_suspend_serial");
164 /* Ignore local variable about time used for tracing */
165 MC_ignore_local_variable("start_time", "*");
167 /* Static variable used for tracing */
168 MCer_ignore_global_variable("counter");
171 MCer_ignore_global_variable("smx_total_comms");
175 /******************************* Core of MC *******************************/
176 /**************************************************************************/
180 mc_mode = MC_MODE_CLIENT;
182 MC_client_main_loop();
194 int MC_deadlock_check()
196 if (mc_mode == MC_MODE_SERVER) {
198 if ((res = mc_model_checker->process().send_message(MC_MESSAGE_DEADLOCK_CHECK)))
199 xbt_die("Could not check deadlock state");
200 s_mc_int_message_t message;
201 ssize_t s = mc_model_checker->process().receive_message(message);
203 xbt_die("Could not receive message");
204 else if (s != sizeof(message) || message.type != MC_MESSAGE_DEADLOCK_CHECK_REPLY) {
205 xbt_die("%s received unexpected message %s (%i, size=%i) "
206 "expected MC_MESSAGE_DEADLOCK_CHECK_REPLY (%i, size=%i)",
207 MC_mode_name(mc_mode),
208 MC_message_type_name(message.type), (int) message.type, (int) s,
209 (int) MC_MESSAGE_DEADLOCK_CHECK_REPLY, (int) sizeof(message)
213 return message.value;
216 int deadlock = FALSE;
217 smx_process_t process;
218 if (xbt_swag_size(simix_global->process_list)) {
220 xbt_swag_foreach(process, simix_global->process_list) {
221 if (MC_process_is_enabled(process)) {
232 * \brief Re-executes from the state at position start all the transitions indicated by
233 * a given model-checker stack.
234 * \param stack The stack with the transitions to execute.
235 * \param start Start index to begin the re-execution.
237 void MC_replay(xbt_fifo_t stack)
239 int value, count = 1;
241 smx_simcall_t req = NULL, saved_req = NULL;
242 xbt_fifo_item_t item, start_item;
245 XBT_DEBUG("**** Begin Replay ****");
247 /* Intermediate backtracking */
248 if(_sg_mc_checkpoint > 0 || _sg_mc_termination || _sg_mc_visited > 0) {
249 start_item = xbt_fifo_get_first_item(stack);
250 state = (mc_state_t)xbt_fifo_get_item_content(start_item);
251 if(state->system_state){
252 MC_restore_snapshot(state->system_state);
253 if(_sg_mc_comms_determinism || _sg_mc_send_determinism)
254 MC_restore_communications_pattern(state);
260 /* Restore the initial state */
261 MC_restore_snapshot(initial_global_state->snapshot);
262 /* At the moment of taking the snapshot the raw heap was set, so restoring
263 * it will set it back again, we have to unset it to continue */
265 start_item = xbt_fifo_get_last_item(stack);
267 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
268 // int n = xbt_dynar_length(incomplete_communications_pattern);
269 unsigned n = MC_smx_get_maxpid();
270 assert(n == xbt_dynar_length(incomplete_communications_pattern));
271 assert(n == xbt_dynar_length(initial_communications_pattern));
272 for (unsigned j=0; j < n ; j++) {
273 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
274 xbt_dynar_get_as(initial_communications_pattern, j, mc_list_comm_pattern_t)->index_comm = 0;
278 /* Traverse the stack from the state at position start and re-execute the transitions */
279 for (item = start_item;
280 item != xbt_fifo_get_first_item(stack);
281 item = xbt_fifo_get_prev_item(item)) {
283 state = (mc_state_t) xbt_fifo_get_item_content(item);
284 saved_req = MC_state_get_executed_request(state, &value);
287 /* because we got a copy of the executed request, we have to fetch the
288 real one, pointed by the request field of the issuer process */
290 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
291 req = &issuer->simcall;
293 /* Debug information */
294 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
295 req_str = MC_request_to_string(req, value, MC_REQUEST_SIMIX);
296 XBT_DEBUG("Replay: %s (%p)", req_str, state);
300 /* TODO : handle test and testany simcalls */
301 e_mc_call_type_t call = MC_CALL_TYPE_NONE;
302 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
303 call = MC_get_call_type(req);
305 MC_simcall_handle(req, value);
307 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
308 MC_handle_comm_pattern(call, req, value, NULL, 1);
310 MC_wait_for_requests();
315 /* Update statistics */
316 mc_stats->visited_states++;
317 mc_stats->executed_transitions++;
321 XBT_DEBUG("**** End Replay ****");
324 void MC_replay_liveness(xbt_fifo_t stack)
326 xbt_fifo_item_t item;
327 mc_pair_t pair = NULL;
328 mc_state_t state = NULL;
329 smx_simcall_t req = NULL, saved_req = NULL;
330 int value, depth = 1;
333 XBT_DEBUG("**** Begin Replay ****");
335 /* Intermediate backtracking */
336 if(_sg_mc_checkpoint > 0) {
337 item = xbt_fifo_get_first_item(stack);
338 pair = (mc_pair_t) xbt_fifo_get_item_content(item);
339 if(pair->graph_state->system_state){
340 MC_restore_snapshot(pair->graph_state->system_state);
345 /* Restore the initial state */
346 MC_restore_snapshot(initial_global_state->snapshot);
348 /* Traverse the stack from the initial state and re-execute the transitions */
349 for (item = xbt_fifo_get_last_item(stack);
350 item != xbt_fifo_get_first_item(stack);
351 item = xbt_fifo_get_prev_item(item)) {
353 pair = (mc_pair_t) xbt_fifo_get_item_content(item);
355 state = (mc_state_t) pair->graph_state;
357 if (pair->exploration_started) {
359 saved_req = MC_state_get_executed_request(state, &value);
361 if (saved_req != NULL) {
362 /* because we got a copy of the executed request, we have to fetch the
363 real one, pointed by the request field of the issuer process */
364 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
365 req = &issuer->simcall;
367 /* Debug information */
368 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
369 req_str = MC_request_to_string(req, value, MC_REQUEST_SIMIX);
370 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
376 MC_simcall_handle(req, value);
377 MC_wait_for_requests();
380 /* Update statistics */
381 mc_stats->visited_pairs++;
382 mc_stats->executed_transitions++;
388 XBT_DEBUG("**** End Replay ****");
392 * \brief Dumps the contents of a model-checker's stack and shows the actual
394 * \param stack The stack to dump
396 void MC_dump_stack_safety(xbt_fifo_t stack)
398 MC_show_stack_safety(stack);
402 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
403 MC_state_delete(state, !state->in_visited_states ? 1 : 0);
407 void MC_show_stack_safety(xbt_fifo_t stack)
411 xbt_fifo_item_t item;
413 char *req_str = NULL;
415 for (item = xbt_fifo_get_last_item(stack);
416 item; item = xbt_fifo_get_prev_item(item)) {
417 state = (mc_state_t)xbt_fifo_get_item_content(item);
418 req = MC_state_get_executed_request(state, &value);
420 req_str = MC_request_to_string(req, value, MC_REQUEST_EXECUTED);
421 XBT_INFO("%s", req_str);
427 void MC_show_deadlock(smx_simcall_t req)
429 /*char *req_str = NULL; */
430 XBT_INFO("**************************");
431 XBT_INFO("*** DEAD-LOCK DETECTED ***");
432 XBT_INFO("**************************");
433 XBT_INFO("Locked request:");
434 /*req_str = MC_request_to_string(req);
435 XBT_INFO("%s", req_str);
436 xbt_free(req_str); */
437 XBT_INFO("Counter-example execution trace:");
438 MC_dump_stack_safety(mc_stack);
439 MC_print_statistics(mc_stats);
442 void MC_show_non_termination(void){
443 XBT_INFO("******************************************");
444 XBT_INFO("*** NON-PROGRESSIVE CYCLE DETECTED ***");
445 XBT_INFO("******************************************");
446 XBT_INFO("Counter-example execution trace:");
447 MC_dump_stack_safety(mc_stack);
448 MC_print_statistics(mc_stats);
452 void MC_show_stack_liveness(xbt_fifo_t stack)
456 xbt_fifo_item_t item;
458 char *req_str = NULL;
460 for (item = xbt_fifo_get_last_item(stack);
461 item; item = xbt_fifo_get_prev_item(item)) {
462 pair = (mc_pair_t) xbt_fifo_get_item_content(item);
463 req = MC_state_get_executed_request(pair->graph_state, &value);
464 if (req && req->call != SIMCALL_NONE) {
465 req_str = MC_request_to_string(req, value, MC_REQUEST_EXECUTED);
466 XBT_INFO("%s", req_str);
473 void MC_dump_stack_liveness(xbt_fifo_t stack)
476 while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != NULL)
477 MC_pair_delete(pair);
480 void MC_print_statistics(mc_stats_t stats)
482 if(_sg_mc_comms_determinism) {
483 if (!initial_global_state->recv_deterministic && initial_global_state->send_deterministic){
484 XBT_INFO("******************************************************");
485 XBT_INFO("**** Only-send-deterministic communication pattern ****");
486 XBT_INFO("******************************************************");
487 XBT_INFO("%s", initial_global_state->recv_diff);
488 }else if(!initial_global_state->send_deterministic && initial_global_state->recv_deterministic) {
489 XBT_INFO("******************************************************");
490 XBT_INFO("**** Only-recv-deterministic communication pattern ****");
491 XBT_INFO("******************************************************");
492 XBT_INFO("%s", initial_global_state->send_diff);
496 if (stats->expanded_pairs == 0) {
497 XBT_INFO("Expanded states = %lu", stats->expanded_states);
498 XBT_INFO("Visited states = %lu", stats->visited_states);
500 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
501 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
503 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
504 if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0')) {
505 fprintf(dot_output, "}\n");
508 if (initial_global_state != NULL && (_sg_mc_comms_determinism || _sg_mc_send_determinism)) {
509 XBT_INFO("Send-deterministic : %s", !initial_global_state->send_deterministic ? "No" : "Yes");
510 if (_sg_mc_comms_determinism)
511 XBT_INFO("Recv-deterministic : %s", !initial_global_state->recv_deterministic ? "No" : "Yes");
513 if (getenv("SIMGRID_MC_SYSTEM_STATISTICS")){
514 int ret=system("free");
515 if(ret!=0)XBT_WARN("system call did not return 0, but %d",ret);
519 void MC_automaton_load(const char *file)
521 if (_mc_property_automaton == NULL)
522 _mc_property_automaton = xbt_automaton_new();
524 xbt_automaton_load(_mc_property_automaton, file);
527 // TODO, fix cross-process access (this function is not used)
528 void MC_dump_stacks(FILE* file)
531 stack_region_t current_stack;
533 xbt_dynar_foreach(stacks_areas, cursor, current_stack) {
534 unw_context_t * context = (unw_context_t *)current_stack->context;
535 fprintf(file, "Stack %i:\n", nstack);
540 unw_init_local (&c, context);
543 const char * name = !unw_get_proc_name(&c, buffer, 100, &off) ? buffer : "?";
544 #if defined(__x86_64__)
547 unw_get_reg(&c, UNW_X86_64_RIP, &rip);
548 unw_get_reg(&c, UNW_X86_64_RSP, &rsp);
549 fprintf(file, " %i: %s (RIP=0x%" PRIx64 " RSP=0x%" PRIx64 ")\n",
550 nframe, name, (std::uint64_t) rip, (std::uint64_t) rsp);
552 fprintf(file, " %i: %s\n", nframe, name);
555 } while(unw_step(&c));
562 double MC_process_clock_get(smx_process_t process)
566 return mc_time[process->pid];
574 void MC_process_clock_add(smx_process_t process, double amount)
576 mc_time[process->pid] += amount;
580 void MC_report_assertion_error(void)
582 XBT_INFO("**************************");
583 XBT_INFO("*** PROPERTY NOT VALID ***");
584 XBT_INFO("**************************");
585 XBT_INFO("Counter-example execution trace:");
586 MC_record_dump_path(mc_stack);
587 MC_dump_stack_safety(mc_stack);
588 MC_print_statistics(mc_stats);
591 void MC_report_crash(int status)
593 XBT_INFO("**************************");
594 XBT_INFO("** CRASH IN THE PROGRAM **");
595 XBT_INFO("**************************");
596 if (WIFSIGNALED(status))
597 XBT_INFO("From signal: %s", strsignal(WTERMSIG(status)));
598 else if (WIFEXITED(status))
599 XBT_INFO("From exit: %i", WEXITSTATUS(status));
600 if (WCOREDUMP(status))
601 XBT_INFO("A core dump was generated by the system.");
603 XBT_INFO("No core dump was generated by the system.");
604 XBT_INFO("Counter-example execution trace:");
605 MC_record_dump_path(mc_stack);
606 MC_dump_stack_safety(mc_stack);
607 MC_print_statistics(mc_stats);
610 void MC_invalidate_cache(void)
612 if (mc_model_checker)
613 mc_model_checker->process().cache_flags = 0;