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"
42 #include "mcer_ignore.h"
44 #include "mc_record.h"
45 #include "mc_protocol.h"
46 #include "mc_client.h"
50 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
51 "Logging specific to MC (global)");
55 double *mc_time = NULL;
58 int user_max_depth_reached = 0;
60 /* MC global data structures */
61 mc_state_t mc_current_state = NULL;
62 char mc_replay_mode = FALSE;
64 __thread mc_comparison_times_t mc_comp_times = NULL;
65 __thread double mc_snapshot_comparison_time;
66 mc_stats_t mc_stats = NULL;
67 mc_global_t initial_global_state = NULL;
68 xbt_fifo_t mc_stack = NULL;
71 xbt_automaton_t _mc_property_automaton = NULL;
74 FILE *dot_output = NULL;
75 const char *colors[13];
78 /******************************* Initialisation of MC *******************************/
79 /*********************************************************************************/
81 static void MC_init_dot_output()
82 { /* FIXME : more colors */
87 colors[3] = "goldenrod";
90 colors[6] = "magenta";
91 colors[7] = "turquoise4";
93 colors[9] = "forestgreen";
94 colors[10] = "hotpink";
95 colors[11] = "lightblue";
98 dot_output = fopen(_sg_mc_dot_output_file, "w");
100 if (dot_output == NULL) {
101 perror("Error open dot output file");
106 "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");
113 mc_time = xbt_new0(double, simix_process_maxpid);
115 if (_sg_mc_visited > 0 || _sg_mc_liveness || _sg_mc_termination || mc_mode == MC_MODE_SERVER) {
116 /* Those requests are handled on the client side and propagated by message
119 MC_ignore_heap(mc_time, simix_process_maxpid * sizeof(double));
121 smx_process_t process;
122 xbt_swag_foreach(process, simix_global->process_list) {
123 MC_ignore_heap(&(process->process_hookup), sizeof(process->process_hookup));
128 void MC_init_model_checker(pid_t pid, int socket)
130 mc_model_checker = new simgrid::mc::ModelChecker(pid, socket);
132 mc_comp_times = xbt_new0(s_mc_comparison_times_t, 1);
134 /* Initialize statistics */
135 mc_stats = xbt_new0(s_mc_stats_t, 1);
136 mc_stats->state_size = 1;
138 if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0'))
139 MC_init_dot_output();
142 //parmap = xbt_parmap_mc_new(xbt_os_get_numcores(), XBT_PARMAP_DEFAULT);
144 /* Ignore some variables from xbt/ex.h used by exception e for stacks comparison */
145 MC_ignore_local_variable("e", "*");
146 MC_ignore_local_variable("__ex_cleanup", "*");
147 MC_ignore_local_variable("__ex_mctx_en", "*");
148 MC_ignore_local_variable("__ex_mctx_me", "*");
149 MC_ignore_local_variable("__xbt_ex_ctx_ptr", "*");
150 MC_ignore_local_variable("_log_ev", "*");
151 MC_ignore_local_variable("_throw_ctx", "*");
152 MC_ignore_local_variable("ctx", "*");
154 MC_ignore_local_variable("self", "simcall_BODY_mc_snapshot");
155 MC_ignore_local_variable("next_cont"
156 "ext", "smx_ctx_sysv_suspend_serial");
157 MC_ignore_local_variable("i", "smx_ctx_sysv_suspend_serial");
159 /* Ignore local variable about time used for tracing */
160 MC_ignore_local_variable("start_time", "*");
162 /* Static variable used for tracing */
163 MCer_ignore_global_variable("counter");
166 MCer_ignore_global_variable("smx_total_comms");
170 /******************************* Core of MC *******************************/
171 /**************************************************************************/
175 mc_mode = MC_MODE_CLIENT;
177 MC_client_main_loop();
189 int MC_deadlock_check()
191 if (mc_mode == MC_MODE_SERVER) {
193 if ((res = MC_protocol_send_simple_message(mc_model_checker->process().socket,
194 MC_MESSAGE_DEADLOCK_CHECK)))
195 xbt_die("Could not check deadlock state");
196 s_mc_int_message_t message;
197 ssize_t s = MC_receive_message(mc_model_checker->process().socket, &message, sizeof(message), 0);
199 xbt_die("Could not receive message");
200 else if (s != sizeof(message) || message.type != MC_MESSAGE_DEADLOCK_CHECK_REPLY) {
201 xbt_die("%s received unexpected message %s (%i, size=%i) "
202 "expected MC_MESSAGE_DEADLOCK_CHECK_REPLY (%i, size=%i)",
203 MC_mode_name(mc_mode),
204 MC_message_type_name(message.type), (int) message.type, (int) s,
205 (int) MC_MESSAGE_DEADLOCK_CHECK_REPLY, (int) sizeof(message)
209 return message.value;
212 int deadlock = FALSE;
213 smx_process_t process;
214 if (xbt_swag_size(simix_global->process_list)) {
216 xbt_swag_foreach(process, simix_global->process_list) {
217 if (MC_process_is_enabled(process)) {
228 * \brief Re-executes from the state at position start all the transitions indicated by
229 * a given model-checker stack.
230 * \param stack The stack with the transitions to execute.
231 * \param start Start index to begin the re-execution.
233 void MC_replay(xbt_fifo_t stack)
235 int value, count = 1;
237 smx_simcall_t req = NULL, saved_req = NULL;
238 xbt_fifo_item_t item, start_item;
241 XBT_DEBUG("**** Begin Replay ****");
243 /* Intermediate backtracking */
244 if(_sg_mc_checkpoint > 0 || _sg_mc_termination || _sg_mc_visited > 0) {
245 start_item = xbt_fifo_get_first_item(stack);
246 state = (mc_state_t)xbt_fifo_get_item_content(start_item);
247 if(state->system_state){
248 MC_restore_snapshot(state->system_state);
249 if(_sg_mc_comms_determinism || _sg_mc_send_determinism)
250 MC_restore_communications_pattern(state);
256 /* Restore the initial state */
257 MC_restore_snapshot(initial_global_state->snapshot);
258 /* At the moment of taking the snapshot the raw heap was set, so restoring
259 * it will set it back again, we have to unset it to continue */
261 start_item = xbt_fifo_get_last_item(stack);
263 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
264 // int n = xbt_dynar_length(incomplete_communications_pattern);
265 unsigned n = MC_smx_get_maxpid();
266 assert(n == xbt_dynar_length(incomplete_communications_pattern));
267 assert(n == xbt_dynar_length(initial_communications_pattern));
268 for (unsigned j=0; j < n ; j++) {
269 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
270 xbt_dynar_get_as(initial_communications_pattern, j, mc_list_comm_pattern_t)->index_comm = 0;
274 /* Traverse the stack from the state at position start and re-execute the transitions */
275 for (item = start_item;
276 item != xbt_fifo_get_first_item(stack);
277 item = xbt_fifo_get_prev_item(item)) {
279 state = (mc_state_t) xbt_fifo_get_item_content(item);
280 saved_req = MC_state_get_executed_request(state, &value);
283 /* because we got a copy of the executed request, we have to fetch the
284 real one, pointed by the request field of the issuer process */
286 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
287 req = &issuer->simcall;
289 /* Debug information */
290 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
291 req_str = MC_request_to_string(req, value, MC_REQUEST_SIMIX);
292 XBT_DEBUG("Replay: %s (%p)", req_str, state);
296 /* TODO : handle test and testany simcalls */
297 e_mc_call_type_t call = MC_CALL_TYPE_NONE;
298 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
299 call = MC_get_call_type(req);
301 MC_simcall_handle(req, value);
303 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
304 MC_handle_comm_pattern(call, req, value, NULL, 1);
306 MC_wait_for_requests();
311 /* Update statistics */
312 mc_stats->visited_states++;
313 mc_stats->executed_transitions++;
317 XBT_DEBUG("**** End Replay ****");
320 void MC_replay_liveness(xbt_fifo_t stack)
322 xbt_fifo_item_t item;
323 mc_pair_t pair = NULL;
324 mc_state_t state = NULL;
325 smx_simcall_t req = NULL, saved_req = NULL;
326 int value, depth = 1;
329 XBT_DEBUG("**** Begin Replay ****");
331 /* Intermediate backtracking */
332 if(_sg_mc_checkpoint > 0) {
333 item = xbt_fifo_get_first_item(stack);
334 pair = (mc_pair_t) xbt_fifo_get_item_content(item);
335 if(pair->graph_state->system_state){
336 MC_restore_snapshot(pair->graph_state->system_state);
341 /* Restore the initial state */
342 MC_restore_snapshot(initial_global_state->snapshot);
344 /* Traverse the stack from the initial state and re-execute the transitions */
345 for (item = xbt_fifo_get_last_item(stack);
346 item != xbt_fifo_get_first_item(stack);
347 item = xbt_fifo_get_prev_item(item)) {
349 pair = (mc_pair_t) xbt_fifo_get_item_content(item);
351 state = (mc_state_t) pair->graph_state;
353 if (pair->exploration_started) {
355 saved_req = MC_state_get_executed_request(state, &value);
357 if (saved_req != NULL) {
358 /* because we got a copy of the executed request, we have to fetch the
359 real one, pointed by the request field of the issuer process */
360 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
361 req = &issuer->simcall;
363 /* Debug information */
364 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
365 req_str = MC_request_to_string(req, value, MC_REQUEST_SIMIX);
366 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
372 MC_simcall_handle(req, value);
373 MC_wait_for_requests();
376 /* Update statistics */
377 mc_stats->visited_pairs++;
378 mc_stats->executed_transitions++;
384 XBT_DEBUG("**** End Replay ****");
388 * \brief Dumps the contents of a model-checker's stack and shows the actual
390 * \param stack The stack to dump
392 void MC_dump_stack_safety(xbt_fifo_t stack)
394 MC_show_stack_safety(stack);
398 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
399 MC_state_delete(state, !state->in_visited_states ? 1 : 0);
403 void MC_show_stack_safety(xbt_fifo_t stack)
407 xbt_fifo_item_t item;
409 char *req_str = NULL;
411 for (item = xbt_fifo_get_last_item(stack);
412 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
413 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
414 req = MC_state_get_executed_request(state, &value);
416 req_str = MC_request_to_string(req, value, MC_REQUEST_EXECUTED);
417 XBT_INFO("%s", req_str);
423 void MC_show_deadlock(smx_simcall_t req)
425 /*char *req_str = NULL; */
426 XBT_INFO("**************************");
427 XBT_INFO("*** DEAD-LOCK DETECTED ***");
428 XBT_INFO("**************************");
429 XBT_INFO("Locked request:");
430 /*req_str = MC_request_to_string(req);
431 XBT_INFO("%s", req_str);
432 xbt_free(req_str); */
433 XBT_INFO("Counter-example execution trace:");
434 MC_dump_stack_safety(mc_stack);
435 MC_print_statistics(mc_stats);
438 void MC_show_non_termination(void){
439 XBT_INFO("******************************************");
440 XBT_INFO("*** NON-PROGRESSIVE CYCLE DETECTED ***");
441 XBT_INFO("******************************************");
442 XBT_INFO("Counter-example execution trace:");
443 MC_dump_stack_safety(mc_stack);
444 MC_print_statistics(mc_stats);
448 void MC_show_stack_liveness(xbt_fifo_t stack)
452 xbt_fifo_item_t item;
454 char *req_str = NULL;
456 for (item = xbt_fifo_get_last_item(stack);
457 (item ? (pair = (mc_pair_t) (xbt_fifo_get_item_content(item))) : (NULL));
458 item = xbt_fifo_get_prev_item(item)) {
459 req = MC_state_get_executed_request(pair->graph_state, &value);
460 if (req && req->call != SIMCALL_NONE) {
461 req_str = MC_request_to_string(req, value, MC_REQUEST_EXECUTED);
462 XBT_INFO("%s", req_str);
469 void MC_dump_stack_liveness(xbt_fifo_t stack)
472 while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != NULL)
473 MC_pair_delete(pair);
476 void MC_print_statistics(mc_stats_t stats)
478 if(_sg_mc_comms_determinism) {
479 if (!initial_global_state->recv_deterministic && initial_global_state->send_deterministic){
480 XBT_INFO("******************************************************");
481 XBT_INFO("**** Only-send-deterministic communication pattern ****");
482 XBT_INFO("******************************************************");
483 XBT_INFO("%s", initial_global_state->recv_diff);
484 }else if(!initial_global_state->send_deterministic && initial_global_state->recv_deterministic) {
485 XBT_INFO("******************************************************");
486 XBT_INFO("**** Only-recv-deterministic communication pattern ****");
487 XBT_INFO("******************************************************");
488 XBT_INFO("%s", initial_global_state->send_diff);
492 if (stats->expanded_pairs == 0) {
493 XBT_INFO("Expanded states = %lu", stats->expanded_states);
494 XBT_INFO("Visited states = %lu", stats->visited_states);
496 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
497 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
499 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
500 if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0')) {
501 fprintf(dot_output, "}\n");
504 if (initial_global_state != NULL && (_sg_mc_comms_determinism || _sg_mc_send_determinism)) {
505 XBT_INFO("Send-deterministic : %s", !initial_global_state->send_deterministic ? "No" : "Yes");
506 if (_sg_mc_comms_determinism)
507 XBT_INFO("Recv-deterministic : %s", !initial_global_state->recv_deterministic ? "No" : "Yes");
511 void MC_automaton_load(const char *file)
513 if (_mc_property_automaton == NULL)
514 _mc_property_automaton = xbt_automaton_new();
516 xbt_automaton_load(_mc_property_automaton, file);
519 static void register_symbol(xbt_automaton_propositional_symbol_t symbol)
521 if (mc_mode != MC_MODE_CLIENT)
523 s_mc_register_symbol_message_t message;
524 message.type = MC_MESSAGE_REGISTER_SYMBOL;
525 const char* name = xbt_automaton_propositional_symbol_get_name(symbol);
526 if (strlen(name) + 1 > sizeof(message.name))
527 xbt_die("Symbol is too long");
528 strncpy(message.name, name, sizeof(message.name));
529 message.callback = xbt_automaton_propositional_symbol_get_callback(symbol);
530 message.data = xbt_automaton_propositional_symbol_get_data(symbol);
531 MC_client_send_message(&message, sizeof(message));
534 void MC_automaton_new_propositional_symbol(const char *id, int(*fct)(void))
536 if (_mc_property_automaton == NULL)
537 _mc_property_automaton = xbt_automaton_new();
539 xbt_automaton_propositional_symbol_t symbol = xbt_automaton_propositional_symbol_new(_mc_property_automaton, id, fct);
540 register_symbol(symbol);
543 void MC_automaton_new_propositional_symbol_pointer(const char *id, int* value)
545 if (_mc_property_automaton == NULL)
546 _mc_property_automaton = xbt_automaton_new();
547 xbt_automaton_propositional_symbol_t symbol = xbt_automaton_propositional_symbol_new_pointer(_mc_property_automaton, id, value);
548 register_symbol(symbol);
551 void MC_automaton_new_propositional_symbol_callback(const char* id,
552 xbt_automaton_propositional_symbol_callback_type callback,
553 void* data, xbt_automaton_propositional_symbol_free_function_type free_function)
555 if (_mc_property_automaton == NULL)
556 _mc_property_automaton = xbt_automaton_new();
557 xbt_automaton_propositional_symbol_t symbol = xbt_automaton_propositional_symbol_new_callback(
558 _mc_property_automaton, id, callback, data, free_function);
559 register_symbol(symbol);
562 // TODO, fix cross-process access (this function is not used)
563 void MC_dump_stacks(FILE* file)
566 stack_region_t current_stack;
568 xbt_dynar_foreach(stacks_areas, cursor, current_stack) {
569 unw_context_t * context = (unw_context_t *)current_stack->context;
570 fprintf(file, "Stack %i:\n", nstack);
575 unw_init_local (&c, context);
578 const char * name = !unw_get_proc_name(&c, buffer, 100, &off) ? buffer : "?";
579 #if defined(__x86_64__)
582 unw_get_reg(&c, UNW_X86_64_RIP, &rip);
583 unw_get_reg(&c, UNW_X86_64_RSP, &rsp);
584 fprintf(file, " %i: %s (RIP=0x%" PRIx64 " RSP=0x%" PRIx64 ")\n",
585 nframe, name, rip, rsp);
587 fprintf(file, " %i: %s\n", nframe, name);
590 } while(unw_step(&c));
597 double MC_process_clock_get(smx_process_t process)
601 return mc_time[process->pid];
609 void MC_process_clock_add(smx_process_t process, double amount)
611 mc_time[process->pid] += amount;
615 void MC_report_assertion_error(void)
617 XBT_INFO("**************************");
618 XBT_INFO("*** PROPERTY NOT VALID ***");
619 XBT_INFO("**************************");
620 XBT_INFO("Counter-example execution trace:");
621 MC_record_dump_path(mc_stack);
622 MC_dump_stack_safety(mc_stack);
623 MC_print_statistics(mc_stats);
626 void MC_invalidate_cache(void)
628 if (mc_model_checker)
629 mc_model_checker->process().cache_flags = 0;