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 <libunwind.h>
32 #include <xbt/mmalloc.h>
33 #include "../xbt/mmalloc/mmprivate.h"
34 #include "mc_object_info.h"
35 #include "mc_comm_pattern.h"
36 #include "mc_request.h"
37 #include "mc_safety.h"
38 #include "mc_snapshot.h"
39 #include "mc_liveness.h"
40 #include "mc_private.h"
43 #include "mcer_ignore.h"
45 #include "mc_record.h"
46 #include "mc_protocol.h"
47 #include "mc_client.h"
51 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
52 "Logging specific to MC (global)");
56 double *mc_time = NULL;
59 int user_max_depth_reached = 0;
61 /* MC global data structures */
62 mc_state_t mc_current_state = NULL;
63 char mc_replay_mode = FALSE;
65 __thread mc_comparison_times_t mc_comp_times = NULL;
66 __thread double mc_snapshot_comparison_time;
67 mc_stats_t mc_stats = NULL;
68 mc_global_t initial_global_state = NULL;
69 xbt_fifo_t mc_stack = NULL;
72 xbt_automaton_t _mc_property_automaton = NULL;
75 FILE *dot_output = NULL;
76 const char *colors[13];
79 /******************************* Initialisation of MC *******************************/
80 /*********************************************************************************/
82 void MC_init_dot_output()
83 { /* FIXME : more colors */
88 colors[3] = "goldenrod";
91 colors[6] = "magenta";
92 colors[7] = "turquoise4";
94 colors[9] = "forestgreen";
95 colors[10] = "hotpink";
96 colors[11] = "lightblue";
99 dot_output = fopen(_sg_mc_dot_output_file, "w");
101 if (dot_output == NULL) {
102 perror("Error open dot output file");
107 "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");
114 mc_time = xbt_new0(double, simix_process_maxpid);
116 if (_sg_mc_visited > 0 || _sg_mc_liveness || _sg_mc_termination || mc_mode == MC_MODE_SERVER) {
117 /* Those requests are handled on the client side and propagated by message
120 MC_ignore_heap(mc_time, simix_process_maxpid * sizeof(double));
122 smx_process_t process;
123 xbt_swag_foreach(process, simix_global->process_list) {
124 MC_ignore_heap(&(process->process_hookup), sizeof(process->process_hookup));
131 /******************************* Core of MC *******************************/
132 /**************************************************************************/
136 mc_mode = MC_MODE_CLIENT;
138 MC_client_main_loop();
150 int MC_deadlock_check()
152 if (mc_mode == MC_MODE_SERVER) {
154 if ((res = mc_model_checker->process().send_message(MC_MESSAGE_DEADLOCK_CHECK)))
155 xbt_die("Could not check deadlock state");
156 s_mc_int_message_t message;
157 ssize_t s = mc_model_checker->process().receive_message(message);
159 xbt_die("Could not receive message");
160 else if (s != sizeof(message) || message.type != MC_MESSAGE_DEADLOCK_CHECK_REPLY) {
161 xbt_die("%s received unexpected message %s (%i, size=%i) "
162 "expected MC_MESSAGE_DEADLOCK_CHECK_REPLY (%i, size=%i)",
163 MC_mode_name(mc_mode),
164 MC_message_type_name(message.type), (int) message.type, (int) s,
165 (int) MC_MESSAGE_DEADLOCK_CHECK_REPLY, (int) sizeof(message)
169 return message.value;
172 int deadlock = FALSE;
173 smx_process_t process;
174 if (xbt_swag_size(simix_global->process_list)) {
176 xbt_swag_foreach(process, simix_global->process_list) {
177 if (MC_process_is_enabled(process)) {
188 * \brief Re-executes from the state at position start all the transitions indicated by
189 * a given model-checker stack.
190 * \param stack The stack with the transitions to execute.
191 * \param start Start index to begin the re-execution.
193 void MC_replay(xbt_fifo_t stack)
195 int value, count = 1;
197 smx_simcall_t req = NULL, saved_req = NULL;
198 xbt_fifo_item_t item, start_item;
201 XBT_DEBUG("**** Begin Replay ****");
203 /* Intermediate backtracking */
204 if(_sg_mc_checkpoint > 0 || _sg_mc_termination || _sg_mc_visited > 0) {
205 start_item = xbt_fifo_get_first_item(stack);
206 state = (mc_state_t)xbt_fifo_get_item_content(start_item);
207 if(state->system_state){
208 MC_restore_snapshot(state->system_state);
209 if(_sg_mc_comms_determinism || _sg_mc_send_determinism)
210 MC_restore_communications_pattern(state);
216 /* Restore the initial state */
217 MC_restore_snapshot(initial_global_state->snapshot);
218 /* At the moment of taking the snapshot the raw heap was set, so restoring
219 * it will set it back again, we have to unset it to continue */
221 start_item = xbt_fifo_get_last_item(stack);
223 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
224 // int n = xbt_dynar_length(incomplete_communications_pattern);
225 unsigned n = MC_smx_get_maxpid();
226 assert(n == xbt_dynar_length(incomplete_communications_pattern));
227 assert(n == xbt_dynar_length(initial_communications_pattern));
228 for (unsigned j=0; j < n ; j++) {
229 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
230 xbt_dynar_get_as(initial_communications_pattern, j, mc_list_comm_pattern_t)->index_comm = 0;
234 /* Traverse the stack from the state at position start and re-execute the transitions */
235 for (item = start_item;
236 item != xbt_fifo_get_first_item(stack);
237 item = xbt_fifo_get_prev_item(item)) {
239 state = (mc_state_t) xbt_fifo_get_item_content(item);
240 saved_req = MC_state_get_executed_request(state, &value);
243 /* because we got a copy of the executed request, we have to fetch the
244 real one, pointed by the request field of the issuer process */
246 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
247 req = &issuer->simcall;
249 /* Debug information */
250 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
251 req_str = MC_request_to_string(req, value, MC_REQUEST_SIMIX);
252 XBT_DEBUG("Replay: %s (%p)", req_str, state);
256 /* TODO : handle test and testany simcalls */
257 e_mc_call_type_t call = MC_CALL_TYPE_NONE;
258 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
259 call = MC_get_call_type(req);
261 MC_simcall_handle(req, value);
263 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
264 MC_handle_comm_pattern(call, req, value, NULL, 1);
266 mc_model_checker->wait_for_requests();
271 /* Update statistics */
272 mc_stats->visited_states++;
273 mc_stats->executed_transitions++;
277 XBT_DEBUG("**** End Replay ****");
280 void MC_replay_liveness(xbt_fifo_t stack)
282 xbt_fifo_item_t item;
283 mc_pair_t pair = NULL;
284 mc_state_t state = NULL;
285 smx_simcall_t req = NULL, saved_req = NULL;
286 int value, depth = 1;
289 XBT_DEBUG("**** Begin Replay ****");
291 /* Intermediate backtracking */
292 if(_sg_mc_checkpoint > 0) {
293 item = xbt_fifo_get_first_item(stack);
294 pair = (mc_pair_t) xbt_fifo_get_item_content(item);
295 if(pair->graph_state->system_state){
296 MC_restore_snapshot(pair->graph_state->system_state);
301 /* Restore the initial state */
302 MC_restore_snapshot(initial_global_state->snapshot);
304 /* Traverse the stack from the initial state and re-execute the transitions */
305 for (item = xbt_fifo_get_last_item(stack);
306 item != xbt_fifo_get_first_item(stack);
307 item = xbt_fifo_get_prev_item(item)) {
309 pair = (mc_pair_t) xbt_fifo_get_item_content(item);
311 state = (mc_state_t) pair->graph_state;
313 if (pair->exploration_started) {
315 saved_req = MC_state_get_executed_request(state, &value);
317 if (saved_req != NULL) {
318 /* because we got a copy of the executed request, we have to fetch the
319 real one, pointed by the request field of the issuer process */
320 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
321 req = &issuer->simcall;
323 /* Debug information */
324 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
325 req_str = MC_request_to_string(req, value, MC_REQUEST_SIMIX);
326 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
332 MC_simcall_handle(req, value);
333 mc_model_checker->wait_for_requests();
336 /* Update statistics */
337 mc_stats->visited_pairs++;
338 mc_stats->executed_transitions++;
344 XBT_DEBUG("**** End Replay ****");
348 * \brief Dumps the contents of a model-checker's stack and shows the actual
350 * \param stack The stack to dump
352 void MC_dump_stack_safety(xbt_fifo_t stack)
354 MC_show_stack_safety(stack);
358 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
359 MC_state_delete(state, !state->in_visited_states ? 1 : 0);
363 void MC_show_stack_safety(xbt_fifo_t stack)
367 xbt_fifo_item_t item;
369 char *req_str = NULL;
371 for (item = xbt_fifo_get_last_item(stack);
372 item; item = xbt_fifo_get_prev_item(item)) {
373 state = (mc_state_t)xbt_fifo_get_item_content(item);
374 req = MC_state_get_executed_request(state, &value);
376 req_str = MC_request_to_string(req, value, MC_REQUEST_EXECUTED);
377 XBT_INFO("%s", req_str);
383 void MC_show_deadlock(smx_simcall_t req)
385 /*char *req_str = NULL; */
386 XBT_INFO("**************************");
387 XBT_INFO("*** DEAD-LOCK DETECTED ***");
388 XBT_INFO("**************************");
389 XBT_INFO("Locked request:");
390 /*req_str = MC_request_to_string(req);
391 XBT_INFO("%s", req_str);
392 xbt_free(req_str); */
393 XBT_INFO("Counter-example execution trace:");
394 MC_dump_stack_safety(mc_stack);
395 MC_print_statistics(mc_stats);
398 void MC_show_non_termination(void){
399 XBT_INFO("******************************************");
400 XBT_INFO("*** NON-PROGRESSIVE CYCLE DETECTED ***");
401 XBT_INFO("******************************************");
402 XBT_INFO("Counter-example execution trace:");
403 MC_dump_stack_safety(mc_stack);
404 MC_print_statistics(mc_stats);
408 void MC_show_stack_liveness(xbt_fifo_t stack)
412 xbt_fifo_item_t item;
414 char *req_str = NULL;
416 for (item = xbt_fifo_get_last_item(stack);
417 item; item = xbt_fifo_get_prev_item(item)) {
418 pair = (mc_pair_t) xbt_fifo_get_item_content(item);
419 req = MC_state_get_executed_request(pair->graph_state, &value);
420 if (req && req->call != SIMCALL_NONE) {
421 req_str = MC_request_to_string(req, value, MC_REQUEST_EXECUTED);
422 XBT_INFO("%s", req_str);
429 void MC_dump_stack_liveness(xbt_fifo_t stack)
432 while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != NULL)
433 MC_pair_delete(pair);
436 void MC_print_statistics(mc_stats_t stats)
438 if(_sg_mc_comms_determinism) {
439 if (!initial_global_state->recv_deterministic && initial_global_state->send_deterministic){
440 XBT_INFO("******************************************************");
441 XBT_INFO("**** Only-send-deterministic communication pattern ****");
442 XBT_INFO("******************************************************");
443 XBT_INFO("%s", initial_global_state->recv_diff);
444 }else if(!initial_global_state->send_deterministic && initial_global_state->recv_deterministic) {
445 XBT_INFO("******************************************************");
446 XBT_INFO("**** Only-recv-deterministic communication pattern ****");
447 XBT_INFO("******************************************************");
448 XBT_INFO("%s", initial_global_state->send_diff);
452 if (stats->expanded_pairs == 0) {
453 XBT_INFO("Expanded states = %lu", stats->expanded_states);
454 XBT_INFO("Visited states = %lu", stats->visited_states);
456 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
457 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
459 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
460 if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0')) {
461 fprintf(dot_output, "}\n");
464 if (initial_global_state != NULL && (_sg_mc_comms_determinism || _sg_mc_send_determinism)) {
465 XBT_INFO("Send-deterministic : %s", !initial_global_state->send_deterministic ? "No" : "Yes");
466 if (_sg_mc_comms_determinism)
467 XBT_INFO("Recv-deterministic : %s", !initial_global_state->recv_deterministic ? "No" : "Yes");
469 if (getenv("SIMGRID_MC_SYSTEM_STATISTICS")){
470 int ret=system("free");
471 if(ret!=0)XBT_WARN("system call did not return 0, but %d",ret);
475 void MC_automaton_load(const char *file)
477 if (_mc_property_automaton == NULL)
478 _mc_property_automaton = xbt_automaton_new();
480 xbt_automaton_load(_mc_property_automaton, file);
483 // TODO, fix cross-process access (this function is not used)
484 void MC_dump_stacks(FILE* file)
487 stack_region_t current_stack;
489 xbt_dynar_foreach(stacks_areas, cursor, current_stack) {
490 unw_context_t * context = (unw_context_t *)current_stack->context;
491 fprintf(file, "Stack %i:\n", nstack);
496 unw_init_local (&c, context);
499 const char * name = !unw_get_proc_name(&c, buffer, 100, &off) ? buffer : "?";
500 #if defined(__x86_64__)
503 unw_get_reg(&c, UNW_X86_64_RIP, &rip);
504 unw_get_reg(&c, UNW_X86_64_RSP, &rsp);
505 fprintf(file, " %i: %s (RIP=0x%" PRIx64 " RSP=0x%" PRIx64 ")\n",
506 nframe, name, (std::uint64_t) rip, (std::uint64_t) rsp);
508 fprintf(file, " %i: %s\n", nframe, name);
511 } while(unw_step(&c));
518 double MC_process_clock_get(smx_process_t process)
522 return mc_time[process->pid];
530 void MC_process_clock_add(smx_process_t process, double amount)
532 mc_time[process->pid] += amount;
536 void MC_report_assertion_error(void)
538 XBT_INFO("**************************");
539 XBT_INFO("*** PROPERTY NOT VALID ***");
540 XBT_INFO("**************************");
541 XBT_INFO("Counter-example execution trace:");
542 MC_record_dump_path(mc_stack);
543 MC_dump_stack_safety(mc_stack);
544 MC_print_statistics(mc_stats);
547 void MC_report_crash(int status)
549 XBT_INFO("**************************");
550 XBT_INFO("** CRASH IN THE PROGRAM **");
551 XBT_INFO("**************************");
552 if (WIFSIGNALED(status))
553 XBT_INFO("From signal: %s", strsignal(WTERMSIG(status)));
554 else if (WIFEXITED(status))
555 XBT_INFO("From exit: %i", WEXITSTATUS(status));
556 if (WCOREDUMP(status))
557 XBT_INFO("A core dump was generated by the system.");
559 XBT_INFO("No core dump was generated by the system.");
560 XBT_INFO("Counter-example execution trace:");
561 MC_record_dump_path(mc_stack);
562 MC_dump_stack_safety(mc_stack);
563 MC_print_statistics(mc_stats);
566 void MC_invalidate_cache(void)
568 if (mc_model_checker)
569 mc_model_checker->process().cache_flags = 0;