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"
43 #include "mc_record.h"
44 #include "mc_protocol.h"
45 #include "mc_client.h"
49 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
50 "Logging specific to MC (global)");
54 double *mc_time = NULL;
57 int user_max_depth_reached = 0;
59 /* MC global data structures */
60 mc_state_t mc_current_state = NULL;
61 char mc_replay_mode = FALSE;
63 __thread mc_comparison_times_t mc_comp_times = NULL;
64 __thread double mc_snapshot_comparison_time;
65 mc_stats_t mc_stats = NULL;
66 mc_global_t initial_global_state = NULL;
67 xbt_fifo_t mc_stack = NULL;
70 xbt_automaton_t _mc_property_automaton = NULL;
73 FILE *dot_output = NULL;
74 const char *colors[13];
77 /******************************* Initialisation of MC *******************************/
78 /*********************************************************************************/
80 static void MC_init_dot_output()
81 { /* FIXME : more colors */
86 colors[3] = "goldenrod";
89 colors[6] = "magenta";
90 colors[7] = "turquoise4";
92 colors[9] = "forestgreen";
93 colors[10] = "hotpink";
94 colors[11] = "lightblue";
97 dot_output = fopen(_sg_mc_dot_output_file, "w");
99 if (dot_output == NULL) {
100 perror("Error open dot output file");
105 "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");
112 MC_init_pid(getpid(), -1);
115 void MC_init_pid(pid_t pid, int socket)
117 /* Initialize the data structures that must be persistent across every
118 iteration of the model-checker (in RAW memory) */
120 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
121 mc_model_checker = MC_model_checker_new(pid, socket);
123 // It's not useful anymore:
124 if (0 && mc_mode == MC_MODE_SERVER) {
125 unsigned long maxpid;
126 MC_process_read_variable(&mc_model_checker->process, "simix_process_maxpid",
127 &maxpid, sizeof(maxpid));
128 simix_process_maxpid = maxpid;
131 mmalloc_set_current_heap(std_heap);
132 mc_time = xbt_new0(double, MC_smx_get_maxpid());
133 mmalloc_set_current_heap(mc_heap);
135 mc_comp_times = xbt_new0(s_mc_comparison_times_t, 1);
137 /* Initialize statistics */
138 mc_stats = xbt_new0(s_mc_stats_t, 1);
139 mc_stats->state_size = 1;
141 if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0'))
142 MC_init_dot_output();
145 //parmap = xbt_parmap_mc_new(xbt_os_get_numcores(), XBT_PARMAP_DEFAULT);
149 if (_sg_mc_visited > 0 || _sg_mc_liveness || _sg_mc_termination || mc_mode == MC_MODE_SERVER) {
150 /* Ignore some variables from xbt/ex.h used by exception e for stacks comparison */
151 MC_ignore_local_variable("e", "*");
152 MC_ignore_local_variable("__ex_cleanup", "*");
153 MC_ignore_local_variable("__ex_mctx_en", "*");
154 MC_ignore_local_variable("__ex_mctx_me", "*");
155 MC_ignore_local_variable("__xbt_ex_ctx_ptr", "*");
156 MC_ignore_local_variable("_log_ev", "*");
157 MC_ignore_local_variable("_throw_ctx", "*");
158 MC_ignore_local_variable("ctx", "*");
160 MC_ignore_local_variable("self", "simcall_BODY_mc_snapshot");
161 MC_ignore_local_variable("next_cont"
162 "ext", "smx_ctx_sysv_suspend_serial");
163 MC_ignore_local_variable("i", "smx_ctx_sysv_suspend_serial");
165 /* Ignore local variable about time used for tracing */
166 MC_ignore_local_variable("start_time", "*");
169 MC_ignore_global_variable("mc_model_checker");
170 MC_ignore_global_variable("initial_communications_pattern");
171 MC_ignore_global_variable("incomplete_communications_pattern");
172 MC_ignore_global_variable("nb_comm_pattern");
174 /* MC __thread variables: */
175 MC_ignore_global_variable("mc_diff_info");
176 MC_ignore_global_variable("mc_comp_times");
177 MC_ignore_global_variable("mc_snapshot_comparison_time");
179 /* This MC state is used in MC replay as well: */
180 MC_ignore_global_variable("mc_time");
182 /* Static variable used for tracing */
183 MC_ignore_global_variable("counter");
186 MC_ignore_global_variable("smx_total_comms");
188 if (mc_mode == MC_MODE_CLIENT) {
189 /* Those requests are handled on the client side and propagated by message
192 MC_ignore_heap(mc_time, MC_smx_get_maxpid() * sizeof(double));
194 smx_process_t process;
195 xbt_swag_foreach(process, simix_global->process_list) {
196 MC_ignore_heap(&(process->process_hookup), sizeof(process->process_hookup));
202 mmalloc_set_current_heap(heap);
206 /******************************* Core of MC *******************************/
207 /**************************************************************************/
211 mc_mode = MC_MODE_CLIENT;
213 MC_client_main_loop();
225 int MC_deadlock_check()
227 if (mc_mode == MC_MODE_SERVER) {
229 if ((res = MC_protocol_send_simple_message(mc_model_checker->process.socket,
230 MC_MESSAGE_DEADLOCK_CHECK)))
231 xbt_die("Could not check deadlock state");
232 s_mc_int_message_t message;
233 ssize_t s = MC_receive_message(mc_model_checker->process.socket, &message, sizeof(message), 0);
235 xbt_die("Could not receive message");
236 else if (s != sizeof(message) || message.type != MC_MESSAGE_DEADLOCK_CHECK_REPLY) {
237 xbt_die("%s received unexpected message %s (%i, size=%i) "
238 "expected MC_MESSAGE_DEADLOCK_CHECK_REPLY (%i, size=%i)",
239 MC_mode_name(mc_mode),
240 MC_message_type_name(message.type), (int) message.type, (int) s,
241 (int) MC_MESSAGE_DEADLOCK_CHECK_REPLY, (int) sizeof(message)
245 return message.value;
248 int deadlock = FALSE;
249 smx_process_t process;
250 if (xbt_swag_size(simix_global->process_list)) {
252 MC_EACH_SIMIX_PROCESS(process,
253 if (MC_process_is_enabled(process)) {
264 * \brief Re-executes from the state at position start all the transitions indicated by
265 * a given model-checker stack.
266 * \param stack The stack with the transitions to execute.
267 * \param start Start index to begin the re-execution.
269 void MC_replay(xbt_fifo_t stack)
271 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
273 int value, count = 1;
275 smx_simcall_t req = NULL, saved_req = NULL;
276 xbt_fifo_item_t item, start_item;
279 XBT_DEBUG("**** Begin Replay ****");
281 /* Intermediate backtracking */
282 if(_sg_mc_checkpoint > 0 || _sg_mc_termination || _sg_mc_visited > 0) {
283 start_item = xbt_fifo_get_first_item(stack);
284 state = (mc_state_t)xbt_fifo_get_item_content(start_item);
285 if(state->system_state){
286 MC_restore_snapshot(state->system_state);
287 if(_sg_mc_comms_determinism || _sg_mc_send_determinism)
288 MC_restore_communications_pattern(state);
295 /* Restore the initial state */
296 MC_restore_snapshot(initial_global_state->snapshot);
297 /* At the moment of taking the snapshot the raw heap was set, so restoring
298 * it will set it back again, we have to unset it to continue */
301 start_item = xbt_fifo_get_last_item(stack);
305 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
306 // int n = xbt_dynar_length(incomplete_communications_pattern);
307 unsigned n = MC_smx_get_maxpid();
308 assert(n == xbt_dynar_length(incomplete_communications_pattern));
309 assert(n == xbt_dynar_length(initial_communications_pattern));
310 for (unsigned j=0; j < n ; j++) {
311 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
312 xbt_dynar_get_as(initial_communications_pattern, j, mc_list_comm_pattern_t)->index_comm = 0;
318 /* Traverse the stack from the state at position start and re-execute the transitions */
319 for (item = start_item;
320 item != xbt_fifo_get_first_item(stack);
321 item = xbt_fifo_get_prev_item(item)) {
323 state = (mc_state_t) xbt_fifo_get_item_content(item);
324 saved_req = MC_state_get_executed_request(state, &value);
327 /* because we got a copy of the executed request, we have to fetch the
328 real one, pointed by the request field of the issuer process */
330 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
331 req = &issuer->simcall;
333 /* Debug information */
334 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
335 req_str = MC_request_to_string(req, value, MC_REQUEST_SIMIX);
336 XBT_DEBUG("Replay: %s (%p)", req_str, state);
340 /* TODO : handle test and testany simcalls */
341 e_mc_call_type_t call = MC_CALL_TYPE_NONE;
342 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
343 call = MC_get_call_type(req);
345 MC_simcall_handle(req, value);
348 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
349 MC_handle_comm_pattern(call, req, value, NULL, 1);
352 MC_wait_for_requests();
357 /* Update statistics */
358 mc_stats->visited_states++;
359 mc_stats->executed_transitions++;
363 XBT_DEBUG("**** End Replay ****");
364 mmalloc_set_current_heap(heap);
367 void MC_replay_liveness(xbt_fifo_t stack)
370 initial_global_state->raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
372 xbt_fifo_item_t item;
373 mc_pair_t pair = NULL;
374 mc_state_t state = NULL;
375 smx_simcall_t req = NULL, saved_req = NULL;
376 int value, depth = 1;
379 XBT_DEBUG("**** Begin Replay ****");
381 /* Intermediate backtracking */
382 if(_sg_mc_checkpoint > 0) {
383 item = xbt_fifo_get_first_item(stack);
384 pair = (mc_pair_t) xbt_fifo_get_item_content(item);
385 if(pair->graph_state->system_state){
386 MC_restore_snapshot(pair->graph_state->system_state);
392 /* Restore the initial state */
393 MC_restore_snapshot(initial_global_state->snapshot);
395 /* At the moment of taking the snapshot the raw heap was set, so restoring
396 * it will set it back again, we have to unset it to continue */
397 if (!initial_global_state->raw_mem_set)
400 /* Traverse the stack from the initial state and re-execute the transitions */
401 for (item = xbt_fifo_get_last_item(stack);
402 item != xbt_fifo_get_first_item(stack);
403 item = xbt_fifo_get_prev_item(item)) {
405 pair = (mc_pair_t) xbt_fifo_get_item_content(item);
407 state = (mc_state_t) pair->graph_state;
409 if (pair->exploration_started) {
411 saved_req = MC_state_get_executed_request(state, &value);
413 if (saved_req != NULL) {
414 /* because we got a copy of the executed request, we have to fetch the
415 real one, pointed by the request field of the issuer process */
416 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
417 req = &issuer->simcall;
419 /* Debug information */
420 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
421 req_str = MC_request_to_string(req, value, MC_REQUEST_SIMIX);
422 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
428 MC_simcall_handle(req, value);
429 MC_wait_for_requests();
432 /* Update statistics */
433 mc_stats->visited_pairs++;
434 mc_stats->executed_transitions++;
440 XBT_DEBUG("**** End Replay ****");
442 if (initial_global_state->raw_mem_set)
450 * \brief Dumps the contents of a model-checker's stack and shows the actual
452 * \param stack The stack to dump
454 void MC_dump_stack_safety(xbt_fifo_t stack)
456 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
458 MC_show_stack_safety(stack);
463 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
464 MC_state_delete(state, !state->in_visited_states ? 1 : 0);
465 mmalloc_set_current_heap(heap);
469 void MC_show_stack_safety(xbt_fifo_t stack)
471 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
475 xbt_fifo_item_t item;
477 char *req_str = NULL;
479 for (item = xbt_fifo_get_last_item(stack);
480 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
481 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
482 req = MC_state_get_executed_request(state, &value);
484 req_str = MC_request_to_string(req, value, MC_REQUEST_EXECUTED);
485 XBT_INFO("%s", req_str);
490 mmalloc_set_current_heap(heap);
493 void MC_show_deadlock(smx_simcall_t req)
495 /*char *req_str = NULL; */
496 XBT_INFO("**************************");
497 XBT_INFO("*** DEAD-LOCK DETECTED ***");
498 XBT_INFO("**************************");
499 XBT_INFO("Locked request:");
500 /*req_str = MC_request_to_string(req);
501 XBT_INFO("%s", req_str);
502 xbt_free(req_str); */
503 XBT_INFO("Counter-example execution trace:");
504 MC_dump_stack_safety(mc_stack);
505 MC_print_statistics(mc_stats);
508 void MC_show_non_termination(void){
509 XBT_INFO("******************************************");
510 XBT_INFO("*** NON-PROGRESSIVE CYCLE DETECTED ***");
511 XBT_INFO("******************************************");
512 XBT_INFO("Counter-example execution trace:");
513 MC_dump_stack_safety(mc_stack);
514 MC_print_statistics(mc_stats);
518 void MC_show_stack_liveness(xbt_fifo_t stack)
522 xbt_fifo_item_t item;
524 char *req_str = NULL;
526 for (item = xbt_fifo_get_last_item(stack);
527 (item ? (pair = (mc_pair_t) (xbt_fifo_get_item_content(item))) : (NULL));
528 item = xbt_fifo_get_prev_item(item)) {
529 req = MC_state_get_executed_request(pair->graph_state, &value);
530 if (req && req->call != SIMCALL_NONE) {
531 req_str = MC_request_to_string(req, value, MC_REQUEST_EXECUTED);
532 XBT_INFO("%s", req_str);
539 void MC_dump_stack_liveness(xbt_fifo_t stack)
541 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
543 while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != NULL)
544 MC_pair_delete(pair);
545 mmalloc_set_current_heap(heap);
548 void MC_print_statistics(mc_stats_t stats)
550 if(_sg_mc_comms_determinism) {
551 if (!initial_global_state->recv_deterministic && initial_global_state->send_deterministic){
552 XBT_INFO("******************************************************");
553 XBT_INFO("**** Only-send-deterministic communication pattern ****");
554 XBT_INFO("******************************************************");
555 XBT_INFO("%s", initial_global_state->recv_diff);
556 }else if(!initial_global_state->send_deterministic && initial_global_state->recv_deterministic) {
557 XBT_INFO("******************************************************");
558 XBT_INFO("**** Only-recv-deterministic communication pattern ****");
559 XBT_INFO("******************************************************");
560 XBT_INFO("%s", initial_global_state->send_diff);
564 if (stats->expanded_pairs == 0) {
565 XBT_INFO("Expanded states = %lu", stats->expanded_states);
566 XBT_INFO("Visited states = %lu", stats->visited_states);
568 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
569 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
571 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
572 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
573 if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0')) {
574 fprintf(dot_output, "}\n");
577 if (initial_global_state != NULL && (_sg_mc_comms_determinism || _sg_mc_send_determinism)) {
578 XBT_INFO("Send-deterministic : %s", !initial_global_state->send_deterministic ? "No" : "Yes");
579 if (_sg_mc_comms_determinism)
580 XBT_INFO("Recv-deterministic : %s", !initial_global_state->recv_deterministic ? "No" : "Yes");
582 mmalloc_set_current_heap(heap);
585 void MC_automaton_load(const char *file)
587 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
589 if (_mc_property_automaton == NULL)
590 _mc_property_automaton = xbt_automaton_new();
592 xbt_automaton_load(_mc_property_automaton, file);
593 mmalloc_set_current_heap(heap);
596 static void register_symbol(xbt_automaton_propositional_symbol_t symbol)
598 if (mc_mode != MC_MODE_CLIENT)
600 s_mc_register_symbol_message_t message;
601 message.type = MC_MESSAGE_REGISTER_SYMBOL;
602 const char* name = xbt_automaton_propositional_symbol_get_name(symbol);
603 if (strlen(name) + 1 > sizeof(message.name))
604 xbt_die("Symbol is too long");
605 strncpy(message.name, name, sizeof(message.name));
606 message.callback = xbt_automaton_propositional_symbol_get_callback(symbol);
607 message.data = xbt_automaton_propositional_symbol_get_data(symbol);
608 MC_client_send_message(&message, sizeof(message));
611 void MC_automaton_new_propositional_symbol(const char *id, int(*fct)(void))
613 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
615 if (_mc_property_automaton == NULL)
616 _mc_property_automaton = xbt_automaton_new();
618 xbt_automaton_propositional_symbol_t symbol = xbt_automaton_propositional_symbol_new(_mc_property_automaton, id, fct);
619 register_symbol(symbol);
620 mmalloc_set_current_heap(heap);
623 void MC_automaton_new_propositional_symbol_pointer(const char *id, int* value)
625 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
626 if (_mc_property_automaton == NULL)
627 _mc_property_automaton = xbt_automaton_new();
628 xbt_automaton_propositional_symbol_t symbol = xbt_automaton_propositional_symbol_new_pointer(_mc_property_automaton, id, value);
629 register_symbol(symbol);
630 mmalloc_set_current_heap(heap);
633 void MC_automaton_new_propositional_symbol_callback(const char* id,
634 xbt_automaton_propositional_symbol_callback_type callback,
635 void* data, xbt_automaton_propositional_symbol_free_function_type free_function)
637 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
638 if (_mc_property_automaton == NULL)
639 _mc_property_automaton = xbt_automaton_new();
640 xbt_automaton_propositional_symbol_t symbol = xbt_automaton_propositional_symbol_new_callback(
641 _mc_property_automaton, id, callback, data, free_function);
642 register_symbol(symbol);
643 mmalloc_set_current_heap(heap);
646 // TODO, fix cross-process access (this function is not used)
647 void MC_dump_stacks(FILE* file)
649 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
652 stack_region_t current_stack;
654 xbt_dynar_foreach(stacks_areas, cursor, current_stack) {
655 unw_context_t * context = (unw_context_t *)current_stack->context;
656 fprintf(file, "Stack %i:\n", nstack);
661 unw_init_local (&c, context);
664 const char * name = !unw_get_proc_name(&c, buffer, 100, &off) ? buffer : "?";
665 #if defined(__x86_64__)
668 unw_get_reg(&c, UNW_X86_64_RIP, &rip);
669 unw_get_reg(&c, UNW_X86_64_RSP, &rsp);
670 fprintf(file, " %i: %s (RIP=0x%" PRIx64 " RSP=0x%" PRIx64 ")\n",
671 nframe, name, rip, rsp);
673 fprintf(file, " %i: %s\n", nframe, name);
676 } while(unw_step(&c));
680 mmalloc_set_current_heap(heap);
684 double MC_process_clock_get(smx_process_t process)
688 return mc_time[process->pid];
696 void MC_process_clock_add(smx_process_t process, double amount)
698 mc_time[process->pid] += amount;
702 void MC_report_assertion_error(void)
704 XBT_INFO("**************************");
705 XBT_INFO("*** PROPERTY NOT VALID ***");
706 XBT_INFO("**************************");
707 XBT_INFO("Counter-example execution trace:");
708 MC_record_dump_path(mc_stack);
709 MC_dump_stack_safety(mc_stack);
710 MC_print_statistics(mc_stats);