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_init_pid(getpid(), -1);
116 void MC_init_pid(pid_t pid, int socket)
118 /* Initialize the data structures that must be persistent across every
119 iteration of the model-checker (in RAW memory) */
121 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
122 mc_model_checker = new simgrid::mc::ModelChecker(pid, socket);
124 // It's not useful anymore:
125 if (0 && mc_mode == MC_MODE_SERVER) {
126 unsigned long maxpid;
127 MC_process_read_variable(&mc_model_checker->process(), "simix_process_maxpid",
128 &maxpid, sizeof(maxpid));
129 simix_process_maxpid = maxpid;
132 mmalloc_set_current_heap(std_heap);
133 mc_time = xbt_new0(double, MC_smx_get_maxpid());
134 mmalloc_set_current_heap(mc_heap);
136 mc_comp_times = xbt_new0(s_mc_comparison_times_t, 1);
138 /* Initialize statistics */
139 mc_stats = xbt_new0(s_mc_stats_t, 1);
140 mc_stats->state_size = 1;
142 if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0'))
143 MC_init_dot_output();
146 //parmap = xbt_parmap_mc_new(xbt_os_get_numcores(), XBT_PARMAP_DEFAULT);
148 mmalloc_set_current_heap(std_heap);
150 if (_sg_mc_visited > 0 || _sg_mc_liveness || _sg_mc_termination || mc_mode == MC_MODE_SERVER) {
151 /* Ignore some variables from xbt/ex.h used by exception e for stacks comparison */
152 MC_ignore_local_variable("e", "*");
153 MC_ignore_local_variable("__ex_cleanup", "*");
154 MC_ignore_local_variable("__ex_mctx_en", "*");
155 MC_ignore_local_variable("__ex_mctx_me", "*");
156 MC_ignore_local_variable("__xbt_ex_ctx_ptr", "*");
157 MC_ignore_local_variable("_log_ev", "*");
158 MC_ignore_local_variable("_throw_ctx", "*");
159 MC_ignore_local_variable("ctx", "*");
161 MC_ignore_local_variable("self", "simcall_BODY_mc_snapshot");
162 MC_ignore_local_variable("next_cont"
163 "ext", "smx_ctx_sysv_suspend_serial");
164 MC_ignore_local_variable("i", "smx_ctx_sysv_suspend_serial");
166 /* Ignore local variable about time used for tracing */
167 MC_ignore_local_variable("start_time", "*");
170 MCer_ignore_global_variable("mc_model_checker");
171 MCer_ignore_global_variable("initial_communications_pattern");
172 MCer_ignore_global_variable("incomplete_communications_pattern");
173 MCer_ignore_global_variable("nb_comm_pattern");
175 /* MC __thread variables: */
176 MCer_ignore_global_variable("mc_diff_info");
177 MCer_ignore_global_variable("mc_comp_times");
178 MCer_ignore_global_variable("mc_snapshot_comparison_time");
180 /* This MC state is used in MC replay as well: */
181 MCer_ignore_global_variable("mc_time");
183 /* Static variable used for tracing */
184 MCer_ignore_global_variable("counter");
187 MCer_ignore_global_variable("smx_total_comms");
189 if (mc_mode == MC_MODE_CLIENT) {
190 /* Those requests are handled on the client side and propagated by message
193 MC_ignore_heap(mc_time, MC_smx_get_maxpid() * sizeof(double));
195 smx_process_t process;
196 xbt_swag_foreach(process, simix_global->process_list) {
197 MC_ignore_heap(&(process->process_hookup), sizeof(process->process_hookup));
203 mmalloc_set_current_heap(heap);
207 /******************************* Core of MC *******************************/
208 /**************************************************************************/
212 mc_mode = MC_MODE_CLIENT;
214 MC_client_main_loop();
226 int MC_deadlock_check()
228 if (mc_mode == MC_MODE_SERVER) {
230 if ((res = MC_protocol_send_simple_message(mc_model_checker->process().socket,
231 MC_MESSAGE_DEADLOCK_CHECK)))
232 xbt_die("Could not check deadlock state");
233 s_mc_int_message_t message;
234 ssize_t s = MC_receive_message(mc_model_checker->process().socket, &message, sizeof(message), 0);
236 xbt_die("Could not receive message");
237 else if (s != sizeof(message) || message.type != MC_MESSAGE_DEADLOCK_CHECK_REPLY) {
238 xbt_die("%s received unexpected message %s (%i, size=%i) "
239 "expected MC_MESSAGE_DEADLOCK_CHECK_REPLY (%i, size=%i)",
240 MC_mode_name(mc_mode),
241 MC_message_type_name(message.type), (int) message.type, (int) s,
242 (int) MC_MESSAGE_DEADLOCK_CHECK_REPLY, (int) sizeof(message)
246 return message.value;
249 int deadlock = FALSE;
250 smx_process_t process;
251 if (xbt_swag_size(simix_global->process_list)) {
253 MC_EACH_SIMIX_PROCESS(process,
254 if (MC_process_is_enabled(process)) {
265 * \brief Re-executes from the state at position start all the transitions indicated by
266 * a given model-checker stack.
267 * \param stack The stack with the transitions to execute.
268 * \param start Start index to begin the re-execution.
270 void MC_replay(xbt_fifo_t stack)
272 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
274 int value, count = 1;
276 smx_simcall_t req = NULL, saved_req = NULL;
277 xbt_fifo_item_t item, start_item;
280 XBT_DEBUG("**** Begin Replay ****");
282 /* Intermediate backtracking */
283 if(_sg_mc_checkpoint > 0 || _sg_mc_termination || _sg_mc_visited > 0) {
284 start_item = xbt_fifo_get_first_item(stack);
285 state = (mc_state_t)xbt_fifo_get_item_content(start_item);
286 if(state->system_state){
287 MC_restore_snapshot(state->system_state);
288 if(_sg_mc_comms_determinism || _sg_mc_send_determinism)
289 MC_restore_communications_pattern(state);
290 mmalloc_set_current_heap(std_heap);
296 /* Restore the initial state */
297 MC_restore_snapshot(initial_global_state->snapshot);
298 /* At the moment of taking the snapshot the raw heap was set, so restoring
299 * it will set it back again, we have to unset it to continue */
300 mmalloc_set_current_heap(std_heap);
302 start_item = xbt_fifo_get_last_item(stack);
304 mmalloc_set_current_heap(mc_heap);
306 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
307 // int n = xbt_dynar_length(incomplete_communications_pattern);
308 unsigned n = MC_smx_get_maxpid();
309 assert(n == xbt_dynar_length(incomplete_communications_pattern));
310 assert(n == xbt_dynar_length(initial_communications_pattern));
311 for (unsigned j=0; j < n ; j++) {
312 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
313 xbt_dynar_get_as(initial_communications_pattern, j, mc_list_comm_pattern_t)->index_comm = 0;
317 mmalloc_set_current_heap(std_heap);
319 /* Traverse the stack from the state at position start and re-execute the transitions */
320 for (item = start_item;
321 item != xbt_fifo_get_first_item(stack);
322 item = xbt_fifo_get_prev_item(item)) {
324 state = (mc_state_t) xbt_fifo_get_item_content(item);
325 saved_req = MC_state_get_executed_request(state, &value);
328 /* because we got a copy of the executed request, we have to fetch the
329 real one, pointed by the request field of the issuer process */
331 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
332 req = &issuer->simcall;
334 /* Debug information */
335 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
336 req_str = MC_request_to_string(req, value, MC_REQUEST_SIMIX);
337 XBT_DEBUG("Replay: %s (%p)", req_str, state);
341 /* TODO : handle test and testany simcalls */
342 e_mc_call_type_t call = MC_CALL_TYPE_NONE;
343 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
344 call = MC_get_call_type(req);
346 MC_simcall_handle(req, value);
348 mmalloc_set_current_heap(mc_heap);
349 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
350 MC_handle_comm_pattern(call, req, value, NULL, 1);
351 mmalloc_set_current_heap(std_heap);
353 MC_wait_for_requests();
358 /* Update statistics */
359 mc_stats->visited_states++;
360 mc_stats->executed_transitions++;
364 XBT_DEBUG("**** End Replay ****");
365 mmalloc_set_current_heap(heap);
368 void MC_replay_liveness(xbt_fifo_t stack)
371 initial_global_state->raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
373 xbt_fifo_item_t item;
374 mc_pair_t pair = NULL;
375 mc_state_t state = NULL;
376 smx_simcall_t req = NULL, saved_req = NULL;
377 int value, depth = 1;
380 XBT_DEBUG("**** Begin Replay ****");
382 /* Intermediate backtracking */
383 if(_sg_mc_checkpoint > 0) {
384 item = xbt_fifo_get_first_item(stack);
385 pair = (mc_pair_t) xbt_fifo_get_item_content(item);
386 if(pair->graph_state->system_state){
387 MC_restore_snapshot(pair->graph_state->system_state);
388 mmalloc_set_current_heap(std_heap);
393 /* Restore the initial state */
394 MC_restore_snapshot(initial_global_state->snapshot);
396 /* At the moment of taking the snapshot the raw heap was set, so restoring
397 * it will set it back again, we have to unset it to continue */
398 if (!initial_global_state->raw_mem_set)
399 mmalloc_set_current_heap(std_heap);
401 /* Traverse the stack from the initial state and re-execute the transitions */
402 for (item = xbt_fifo_get_last_item(stack);
403 item != xbt_fifo_get_first_item(stack);
404 item = xbt_fifo_get_prev_item(item)) {
406 pair = (mc_pair_t) xbt_fifo_get_item_content(item);
408 state = (mc_state_t) pair->graph_state;
410 if (pair->exploration_started) {
412 saved_req = MC_state_get_executed_request(state, &value);
414 if (saved_req != NULL) {
415 /* because we got a copy of the executed request, we have to fetch the
416 real one, pointed by the request field of the issuer process */
417 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
418 req = &issuer->simcall;
420 /* Debug information */
421 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
422 req_str = MC_request_to_string(req, value, MC_REQUEST_SIMIX);
423 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
429 MC_simcall_handle(req, value);
430 MC_wait_for_requests();
433 /* Update statistics */
434 mc_stats->visited_pairs++;
435 mc_stats->executed_transitions++;
441 XBT_DEBUG("**** End Replay ****");
443 if (initial_global_state->raw_mem_set)
444 mmalloc_set_current_heap(mc_heap);
446 mmalloc_set_current_heap(std_heap);
451 * \brief Dumps the contents of a model-checker's stack and shows the actual
453 * \param stack The stack to dump
455 void MC_dump_stack_safety(xbt_fifo_t stack)
457 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
459 MC_show_stack_safety(stack);
463 mmalloc_set_current_heap(mc_heap);
464 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
465 MC_state_delete(state, !state->in_visited_states ? 1 : 0);
466 mmalloc_set_current_heap(heap);
470 void MC_show_stack_safety(xbt_fifo_t stack)
472 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
476 xbt_fifo_item_t item;
478 char *req_str = NULL;
480 for (item = xbt_fifo_get_last_item(stack);
481 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
482 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
483 req = MC_state_get_executed_request(state, &value);
485 req_str = MC_request_to_string(req, value, MC_REQUEST_EXECUTED);
486 XBT_INFO("%s", req_str);
491 mmalloc_set_current_heap(heap);
494 void MC_show_deadlock(smx_simcall_t req)
496 /*char *req_str = NULL; */
497 XBT_INFO("**************************");
498 XBT_INFO("*** DEAD-LOCK DETECTED ***");
499 XBT_INFO("**************************");
500 XBT_INFO("Locked request:");
501 /*req_str = MC_request_to_string(req);
502 XBT_INFO("%s", req_str);
503 xbt_free(req_str); */
504 XBT_INFO("Counter-example execution trace:");
505 MC_dump_stack_safety(mc_stack);
506 MC_print_statistics(mc_stats);
509 void MC_show_non_termination(void){
510 XBT_INFO("******************************************");
511 XBT_INFO("*** NON-PROGRESSIVE CYCLE DETECTED ***");
512 XBT_INFO("******************************************");
513 XBT_INFO("Counter-example execution trace:");
514 MC_dump_stack_safety(mc_stack);
515 MC_print_statistics(mc_stats);
519 void MC_show_stack_liveness(xbt_fifo_t stack)
523 xbt_fifo_item_t item;
525 char *req_str = NULL;
527 for (item = xbt_fifo_get_last_item(stack);
528 (item ? (pair = (mc_pair_t) (xbt_fifo_get_item_content(item))) : (NULL));
529 item = xbt_fifo_get_prev_item(item)) {
530 req = MC_state_get_executed_request(pair->graph_state, &value);
531 if (req && req->call != SIMCALL_NONE) {
532 req_str = MC_request_to_string(req, value, MC_REQUEST_EXECUTED);
533 XBT_INFO("%s", req_str);
540 void MC_dump_stack_liveness(xbt_fifo_t stack)
542 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
544 while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != NULL)
545 MC_pair_delete(pair);
546 mmalloc_set_current_heap(heap);
549 void MC_print_statistics(mc_stats_t stats)
551 if(_sg_mc_comms_determinism) {
552 if (!initial_global_state->recv_deterministic && initial_global_state->send_deterministic){
553 XBT_INFO("******************************************************");
554 XBT_INFO("**** Only-send-deterministic communication pattern ****");
555 XBT_INFO("******************************************************");
556 XBT_INFO("%s", initial_global_state->recv_diff);
557 }else if(!initial_global_state->send_deterministic && initial_global_state->recv_deterministic) {
558 XBT_INFO("******************************************************");
559 XBT_INFO("**** Only-recv-deterministic communication pattern ****");
560 XBT_INFO("******************************************************");
561 XBT_INFO("%s", initial_global_state->send_diff);
565 if (stats->expanded_pairs == 0) {
566 XBT_INFO("Expanded states = %lu", stats->expanded_states);
567 XBT_INFO("Visited states = %lu", stats->visited_states);
569 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
570 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
572 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
573 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
574 if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0')) {
575 fprintf(dot_output, "}\n");
578 if (initial_global_state != NULL && (_sg_mc_comms_determinism || _sg_mc_send_determinism)) {
579 XBT_INFO("Send-deterministic : %s", !initial_global_state->send_deterministic ? "No" : "Yes");
580 if (_sg_mc_comms_determinism)
581 XBT_INFO("Recv-deterministic : %s", !initial_global_state->recv_deterministic ? "No" : "Yes");
583 mmalloc_set_current_heap(heap);
586 void MC_automaton_load(const char *file)
588 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
590 if (_mc_property_automaton == NULL)
591 _mc_property_automaton = xbt_automaton_new();
593 xbt_automaton_load(_mc_property_automaton, file);
594 mmalloc_set_current_heap(heap);
597 static void register_symbol(xbt_automaton_propositional_symbol_t symbol)
599 if (mc_mode != MC_MODE_CLIENT)
601 s_mc_register_symbol_message_t message;
602 message.type = MC_MESSAGE_REGISTER_SYMBOL;
603 const char* name = xbt_automaton_propositional_symbol_get_name(symbol);
604 if (strlen(name) + 1 > sizeof(message.name))
605 xbt_die("Symbol is too long");
606 strncpy(message.name, name, sizeof(message.name));
607 message.callback = xbt_automaton_propositional_symbol_get_callback(symbol);
608 message.data = xbt_automaton_propositional_symbol_get_data(symbol);
609 MC_client_send_message(&message, sizeof(message));
612 void MC_automaton_new_propositional_symbol(const char *id, int(*fct)(void))
614 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
616 if (_mc_property_automaton == NULL)
617 _mc_property_automaton = xbt_automaton_new();
619 xbt_automaton_propositional_symbol_t symbol = xbt_automaton_propositional_symbol_new(_mc_property_automaton, id, fct);
620 register_symbol(symbol);
621 mmalloc_set_current_heap(heap);
624 void MC_automaton_new_propositional_symbol_pointer(const char *id, int* value)
626 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
627 if (_mc_property_automaton == NULL)
628 _mc_property_automaton = xbt_automaton_new();
629 xbt_automaton_propositional_symbol_t symbol = xbt_automaton_propositional_symbol_new_pointer(_mc_property_automaton, id, value);
630 register_symbol(symbol);
631 mmalloc_set_current_heap(heap);
634 void MC_automaton_new_propositional_symbol_callback(const char* id,
635 xbt_automaton_propositional_symbol_callback_type callback,
636 void* data, xbt_automaton_propositional_symbol_free_function_type free_function)
638 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
639 if (_mc_property_automaton == NULL)
640 _mc_property_automaton = xbt_automaton_new();
641 xbt_automaton_propositional_symbol_t symbol = xbt_automaton_propositional_symbol_new_callback(
642 _mc_property_automaton, id, callback, data, free_function);
643 register_symbol(symbol);
644 mmalloc_set_current_heap(heap);
647 // TODO, fix cross-process access (this function is not used)
648 void MC_dump_stacks(FILE* file)
650 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
653 stack_region_t current_stack;
655 xbt_dynar_foreach(stacks_areas, cursor, current_stack) {
656 unw_context_t * context = (unw_context_t *)current_stack->context;
657 fprintf(file, "Stack %i:\n", nstack);
662 unw_init_local (&c, context);
665 const char * name = !unw_get_proc_name(&c, buffer, 100, &off) ? buffer : "?";
666 #if defined(__x86_64__)
669 unw_get_reg(&c, UNW_X86_64_RIP, &rip);
670 unw_get_reg(&c, UNW_X86_64_RSP, &rsp);
671 fprintf(file, " %i: %s (RIP=0x%" PRIx64 " RSP=0x%" PRIx64 ")\n",
672 nframe, name, rip, rsp);
674 fprintf(file, " %i: %s\n", nframe, name);
677 } while(unw_step(&c));
681 mmalloc_set_current_heap(heap);
685 double MC_process_clock_get(smx_process_t process)
689 return mc_time[process->pid];
697 void MC_process_clock_add(smx_process_t process, double amount)
699 mc_time[process->pid] += amount;
703 void MC_report_assertion_error(void)
705 XBT_INFO("**************************");
706 XBT_INFO("*** PROPERTY NOT VALID ***");
707 XBT_INFO("**************************");
708 XBT_INFO("Counter-example execution trace:");
709 MC_record_dump_path(mc_stack);
710 MC_dump_stack_safety(mc_stack);
711 MC_print_statistics(mc_stats);
714 void MC_invalidate_cache(void)
716 if (mc_model_checker)
717 mc_model_checker->process().cache_flags = 0;