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 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
237 int value, count = 1;
239 smx_simcall_t req = NULL, saved_req = NULL;
240 xbt_fifo_item_t item, start_item;
243 XBT_DEBUG("**** Begin Replay ****");
245 /* Intermediate backtracking */
246 if(_sg_mc_checkpoint > 0 || _sg_mc_termination || _sg_mc_visited > 0) {
247 start_item = xbt_fifo_get_first_item(stack);
248 state = (mc_state_t)xbt_fifo_get_item_content(start_item);
249 if(state->system_state){
250 MC_restore_snapshot(state->system_state);
251 if(_sg_mc_comms_determinism || _sg_mc_send_determinism)
252 MC_restore_communications_pattern(state);
253 mmalloc_set_current_heap(std_heap);
259 /* Restore the initial state */
260 MC_restore_snapshot(initial_global_state->snapshot);
261 /* At the moment of taking the snapshot the raw heap was set, so restoring
262 * it will set it back again, we have to unset it to continue */
263 mmalloc_set_current_heap(std_heap);
265 start_item = xbt_fifo_get_last_item(stack);
267 mmalloc_set_current_heap(mc_heap);
269 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
270 // int n = xbt_dynar_length(incomplete_communications_pattern);
271 unsigned n = MC_smx_get_maxpid();
272 assert(n == xbt_dynar_length(incomplete_communications_pattern));
273 assert(n == xbt_dynar_length(initial_communications_pattern));
274 for (unsigned j=0; j < n ; j++) {
275 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
276 xbt_dynar_get_as(initial_communications_pattern, j, mc_list_comm_pattern_t)->index_comm = 0;
280 mmalloc_set_current_heap(std_heap);
282 /* Traverse the stack from the state at position start and re-execute the transitions */
283 for (item = start_item;
284 item != xbt_fifo_get_first_item(stack);
285 item = xbt_fifo_get_prev_item(item)) {
287 state = (mc_state_t) xbt_fifo_get_item_content(item);
288 saved_req = MC_state_get_executed_request(state, &value);
291 /* because we got a copy of the executed request, we have to fetch the
292 real one, pointed by the request field of the issuer process */
294 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
295 req = &issuer->simcall;
297 /* Debug information */
298 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
299 req_str = MC_request_to_string(req, value, MC_REQUEST_SIMIX);
300 XBT_DEBUG("Replay: %s (%p)", req_str, state);
304 /* TODO : handle test and testany simcalls */
305 e_mc_call_type_t call = MC_CALL_TYPE_NONE;
306 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
307 call = MC_get_call_type(req);
309 MC_simcall_handle(req, value);
311 mmalloc_set_current_heap(mc_heap);
312 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
313 MC_handle_comm_pattern(call, req, value, NULL, 1);
314 mmalloc_set_current_heap(std_heap);
316 MC_wait_for_requests();
321 /* Update statistics */
322 mc_stats->visited_states++;
323 mc_stats->executed_transitions++;
327 XBT_DEBUG("**** End Replay ****");
328 mmalloc_set_current_heap(heap);
331 void MC_replay_liveness(xbt_fifo_t stack)
334 initial_global_state->raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
336 xbt_fifo_item_t item;
337 mc_pair_t pair = NULL;
338 mc_state_t state = NULL;
339 smx_simcall_t req = NULL, saved_req = NULL;
340 int value, depth = 1;
343 XBT_DEBUG("**** Begin Replay ****");
345 /* Intermediate backtracking */
346 if(_sg_mc_checkpoint > 0) {
347 item = xbt_fifo_get_first_item(stack);
348 pair = (mc_pair_t) xbt_fifo_get_item_content(item);
349 if(pair->graph_state->system_state){
350 MC_restore_snapshot(pair->graph_state->system_state);
351 mmalloc_set_current_heap(std_heap);
356 /* Restore the initial state */
357 MC_restore_snapshot(initial_global_state->snapshot);
359 /* At the moment of taking the snapshot the raw heap was set, so restoring
360 * it will set it back again, we have to unset it to continue */
361 if (!initial_global_state->raw_mem_set)
362 mmalloc_set_current_heap(std_heap);
364 /* Traverse the stack from the initial state and re-execute the transitions */
365 for (item = xbt_fifo_get_last_item(stack);
366 item != xbt_fifo_get_first_item(stack);
367 item = xbt_fifo_get_prev_item(item)) {
369 pair = (mc_pair_t) xbt_fifo_get_item_content(item);
371 state = (mc_state_t) pair->graph_state;
373 if (pair->exploration_started) {
375 saved_req = MC_state_get_executed_request(state, &value);
377 if (saved_req != NULL) {
378 /* because we got a copy of the executed request, we have to fetch the
379 real one, pointed by the request field of the issuer process */
380 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
381 req = &issuer->simcall;
383 /* Debug information */
384 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
385 req_str = MC_request_to_string(req, value, MC_REQUEST_SIMIX);
386 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
392 MC_simcall_handle(req, value);
393 MC_wait_for_requests();
396 /* Update statistics */
397 mc_stats->visited_pairs++;
398 mc_stats->executed_transitions++;
404 XBT_DEBUG("**** End Replay ****");
406 if (initial_global_state->raw_mem_set)
407 mmalloc_set_current_heap(mc_heap);
409 mmalloc_set_current_heap(std_heap);
414 * \brief Dumps the contents of a model-checker's stack and shows the actual
416 * \param stack The stack to dump
418 void MC_dump_stack_safety(xbt_fifo_t stack)
420 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
422 MC_show_stack_safety(stack);
426 mmalloc_set_current_heap(mc_heap);
427 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
428 MC_state_delete(state, !state->in_visited_states ? 1 : 0);
429 mmalloc_set_current_heap(heap);
433 void MC_show_stack_safety(xbt_fifo_t stack)
435 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
439 xbt_fifo_item_t item;
441 char *req_str = NULL;
443 for (item = xbt_fifo_get_last_item(stack);
444 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
445 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
446 req = MC_state_get_executed_request(state, &value);
448 req_str = MC_request_to_string(req, value, MC_REQUEST_EXECUTED);
449 XBT_INFO("%s", req_str);
454 mmalloc_set_current_heap(heap);
457 void MC_show_deadlock(smx_simcall_t req)
459 /*char *req_str = NULL; */
460 XBT_INFO("**************************");
461 XBT_INFO("*** DEAD-LOCK DETECTED ***");
462 XBT_INFO("**************************");
463 XBT_INFO("Locked request:");
464 /*req_str = MC_request_to_string(req);
465 XBT_INFO("%s", req_str);
466 xbt_free(req_str); */
467 XBT_INFO("Counter-example execution trace:");
468 MC_dump_stack_safety(mc_stack);
469 MC_print_statistics(mc_stats);
472 void MC_show_non_termination(void){
473 XBT_INFO("******************************************");
474 XBT_INFO("*** NON-PROGRESSIVE CYCLE DETECTED ***");
475 XBT_INFO("******************************************");
476 XBT_INFO("Counter-example execution trace:");
477 MC_dump_stack_safety(mc_stack);
478 MC_print_statistics(mc_stats);
482 void MC_show_stack_liveness(xbt_fifo_t stack)
486 xbt_fifo_item_t item;
488 char *req_str = NULL;
490 for (item = xbt_fifo_get_last_item(stack);
491 (item ? (pair = (mc_pair_t) (xbt_fifo_get_item_content(item))) : (NULL));
492 item = xbt_fifo_get_prev_item(item)) {
493 req = MC_state_get_executed_request(pair->graph_state, &value);
494 if (req && req->call != SIMCALL_NONE) {
495 req_str = MC_request_to_string(req, value, MC_REQUEST_EXECUTED);
496 XBT_INFO("%s", req_str);
503 void MC_dump_stack_liveness(xbt_fifo_t stack)
505 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
507 while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != NULL)
508 MC_pair_delete(pair);
509 mmalloc_set_current_heap(heap);
512 void MC_print_statistics(mc_stats_t stats)
514 if(_sg_mc_comms_determinism) {
515 if (!initial_global_state->recv_deterministic && initial_global_state->send_deterministic){
516 XBT_INFO("******************************************************");
517 XBT_INFO("**** Only-send-deterministic communication pattern ****");
518 XBT_INFO("******************************************************");
519 XBT_INFO("%s", initial_global_state->recv_diff);
520 }else if(!initial_global_state->send_deterministic && initial_global_state->recv_deterministic) {
521 XBT_INFO("******************************************************");
522 XBT_INFO("**** Only-recv-deterministic communication pattern ****");
523 XBT_INFO("******************************************************");
524 XBT_INFO("%s", initial_global_state->send_diff);
528 if (stats->expanded_pairs == 0) {
529 XBT_INFO("Expanded states = %lu", stats->expanded_states);
530 XBT_INFO("Visited states = %lu", stats->visited_states);
532 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
533 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
535 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
536 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
537 if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0')) {
538 fprintf(dot_output, "}\n");
541 if (initial_global_state != NULL && (_sg_mc_comms_determinism || _sg_mc_send_determinism)) {
542 XBT_INFO("Send-deterministic : %s", !initial_global_state->send_deterministic ? "No" : "Yes");
543 if (_sg_mc_comms_determinism)
544 XBT_INFO("Recv-deterministic : %s", !initial_global_state->recv_deterministic ? "No" : "Yes");
546 mmalloc_set_current_heap(heap);
549 void MC_automaton_load(const char *file)
551 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
553 if (_mc_property_automaton == NULL)
554 _mc_property_automaton = xbt_automaton_new();
556 xbt_automaton_load(_mc_property_automaton, file);
557 mmalloc_set_current_heap(heap);
560 static void register_symbol(xbt_automaton_propositional_symbol_t symbol)
562 if (mc_mode != MC_MODE_CLIENT)
564 s_mc_register_symbol_message_t message;
565 message.type = MC_MESSAGE_REGISTER_SYMBOL;
566 const char* name = xbt_automaton_propositional_symbol_get_name(symbol);
567 if (strlen(name) + 1 > sizeof(message.name))
568 xbt_die("Symbol is too long");
569 strncpy(message.name, name, sizeof(message.name));
570 message.callback = xbt_automaton_propositional_symbol_get_callback(symbol);
571 message.data = xbt_automaton_propositional_symbol_get_data(symbol);
572 MC_client_send_message(&message, sizeof(message));
575 void MC_automaton_new_propositional_symbol(const char *id, int(*fct)(void))
577 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
579 if (_mc_property_automaton == NULL)
580 _mc_property_automaton = xbt_automaton_new();
582 xbt_automaton_propositional_symbol_t symbol = xbt_automaton_propositional_symbol_new(_mc_property_automaton, id, fct);
583 register_symbol(symbol);
584 mmalloc_set_current_heap(heap);
587 void MC_automaton_new_propositional_symbol_pointer(const char *id, int* value)
589 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
590 if (_mc_property_automaton == NULL)
591 _mc_property_automaton = xbt_automaton_new();
592 xbt_automaton_propositional_symbol_t symbol = xbt_automaton_propositional_symbol_new_pointer(_mc_property_automaton, id, value);
593 register_symbol(symbol);
594 mmalloc_set_current_heap(heap);
597 void MC_automaton_new_propositional_symbol_callback(const char* id,
598 xbt_automaton_propositional_symbol_callback_type callback,
599 void* data, xbt_automaton_propositional_symbol_free_function_type free_function)
601 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
602 if (_mc_property_automaton == NULL)
603 _mc_property_automaton = xbt_automaton_new();
604 xbt_automaton_propositional_symbol_t symbol = xbt_automaton_propositional_symbol_new_callback(
605 _mc_property_automaton, id, callback, data, free_function);
606 register_symbol(symbol);
607 mmalloc_set_current_heap(heap);
610 // TODO, fix cross-process access (this function is not used)
611 void MC_dump_stacks(FILE* file)
613 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
616 stack_region_t current_stack;
618 xbt_dynar_foreach(stacks_areas, cursor, current_stack) {
619 unw_context_t * context = (unw_context_t *)current_stack->context;
620 fprintf(file, "Stack %i:\n", nstack);
625 unw_init_local (&c, context);
628 const char * name = !unw_get_proc_name(&c, buffer, 100, &off) ? buffer : "?";
629 #if defined(__x86_64__)
632 unw_get_reg(&c, UNW_X86_64_RIP, &rip);
633 unw_get_reg(&c, UNW_X86_64_RSP, &rsp);
634 fprintf(file, " %i: %s (RIP=0x%" PRIx64 " RSP=0x%" PRIx64 ")\n",
635 nframe, name, rip, rsp);
637 fprintf(file, " %i: %s\n", nframe, name);
640 } while(unw_step(&c));
644 mmalloc_set_current_heap(heap);
648 double MC_process_clock_get(smx_process_t process)
652 return mc_time[process->pid];
660 void MC_process_clock_add(smx_process_t process, double amount)
662 mc_time[process->pid] += amount;
666 void MC_report_assertion_error(void)
668 XBT_INFO("**************************");
669 XBT_INFO("*** PROPERTY NOT VALID ***");
670 XBT_INFO("**************************");
671 XBT_INFO("Counter-example execution trace:");
672 MC_record_dump_path(mc_stack);
673 MC_dump_stack_safety(mc_stack);
674 MC_print_statistics(mc_stats);
677 void MC_invalidate_cache(void)
679 if (mc_model_checker)
680 mc_model_checker->process().cache_flags = 0;