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. */
17 #include "simgrid/sg_config.h"
18 #include "../surf/surf_private.h"
19 #include "../simix/smx_private.h"
21 #include "xbt/automaton.h"
25 #include <libunwind.h>
26 #include <xbt/mmalloc.h>
28 #include "../xbt/mmalloc/mmprivate.h"
29 #include "mc_object_info.h"
30 #include "mc_comm_pattern.h"
31 #include "mc_request.h"
32 #include "mc_safety.h"
33 #include "mc_memory_map.h"
34 #include "mc_snapshot.h"
35 #include "mc_liveness.h"
36 #include "mc_private.h"
40 #include "mc_record.h"
41 #include "mc_protocol.h"
42 #include "mc_client.h"
44 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
45 "Logging specific to MC (global)");
49 double *mc_time = NULL;
52 int user_max_depth_reached = 0;
54 /* MC global data structures */
55 mc_state_t mc_current_state = NULL;
56 char mc_replay_mode = FALSE;
58 __thread mc_comparison_times_t mc_comp_times = NULL;
59 __thread double mc_snapshot_comparison_time;
60 mc_stats_t mc_stats = NULL;
61 mc_global_t initial_global_state = NULL;
62 xbt_fifo_t mc_stack = NULL;
65 xbt_automaton_t _mc_property_automaton = NULL;
68 FILE *dot_output = NULL;
69 const char *colors[13];
72 /******************************* Initialisation of MC *******************************/
73 /*********************************************************************************/
75 static void MC_init_dot_output()
76 { /* FIXME : more colors */
81 colors[3] = "goldenrod";
84 colors[6] = "magenta";
85 colors[7] = "turquoise4";
87 colors[9] = "forestgreen";
88 colors[10] = "hotpink";
89 colors[11] = "lightblue";
92 dot_output = fopen(_sg_mc_dot_output_file, "w");
94 if (dot_output == NULL) {
95 perror("Error open dot output file");
100 "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");
106 MC_init_pid(getpid(), -1);
109 void MC_init_pid(pid_t pid, int socket)
111 if (mc_mode == MC_MODE_NONE) {
112 if (getenv(MC_ENV_SOCKET_FD)) {
113 mc_mode = MC_MODE_CLIENT;
115 mc_mode = MC_MODE_STANDALONE;
119 /* Initialize the data structures that must be persistent across every
120 iteration of the model-checker (in RAW memory) */
122 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
124 mc_model_checker = MC_model_checker_new(pid, socket);
125 if (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);
150 if (_sg_mc_visited > 0 || _sg_mc_liveness || _sg_mc_termination) {
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 MC_ignore_global_variable("mc_model_checker");
171 MC_ignore_global_variable("initial_communications_pattern");
172 MC_ignore_global_variable("incomplete_communications_pattern");
173 MC_ignore_global_variable("nb_comm_pattern");
175 /* MC __thread variables: */
176 MC_ignore_global_variable("mc_diff_info");
177 MC_ignore_global_variable("mc_comp_times");
178 MC_ignore_global_variable("mc_snapshot_comparison_time");
180 /* This MC state is used in MC replay as well: */
181 MC_ignore_global_variable("mc_time");
183 /* Static variable used for tracing */
184 MC_ignore_global_variable("counter");
187 MC_ignore_global_variable("smx_total_comms");
189 if (mc_mode == MC_MODE_STANDALONE || 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);
205 if (mc_mode == MC_MODE_CLIENT) {
206 // This will move somehwere else:
207 MC_client_handle_messages();
212 /******************************* Core of MC *******************************/
213 /**************************************************************************/
215 static void MC_modelcheck_comm_determinism_init(void)
219 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
221 /* Create exploration stack */
222 mc_stack = xbt_fifo_new();
226 MC_pre_modelcheck_comm_determinism();
229 initial_global_state = xbt_new0(s_mc_global_t, 1);
230 initial_global_state->snapshot = MC_take_snapshot(0);
231 initial_global_state->initial_communications_pattern_done = 0;
232 initial_global_state->recv_deterministic = 1;
233 initial_global_state->send_deterministic = 1;
234 initial_global_state->recv_diff = NULL;
235 initial_global_state->send_diff = NULL;
239 MC_modelcheck_comm_determinism();
241 mmalloc_set_current_heap(heap);
244 static void MC_modelcheck_safety_init(void)
250 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
252 /* Create exploration stack */
253 mc_stack = xbt_fifo_new();
257 MC_pre_modelcheck_safety();
260 /* Save the initial state */
261 initial_global_state = xbt_new0(s_mc_global_t, 1);
262 initial_global_state->snapshot = MC_take_snapshot(0);
265 MC_modelcheck_safety();
267 mmalloc_set_current_heap(heap);
273 static void MC_modelcheck_liveness_init()
279 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
281 /* Create exploration stack */
282 mc_stack = xbt_fifo_new();
284 /* Create the initial state */
285 initial_global_state = xbt_new0(s_mc_global_t, 1);
289 MC_pre_modelcheck_liveness();
292 MC_print_statistics(mc_stats);
295 mmalloc_set_current_heap(heap);
299 void MC_do_the_modelcheck_for_real()
302 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
303 XBT_INFO("Check communication determinism");
304 mc_reduce_kind = e_mc_reduce_none;
305 MC_modelcheck_comm_determinism_init();
306 } else if (!_sg_mc_property_file || _sg_mc_property_file[0] == '\0') {
307 if(_sg_mc_termination){
308 XBT_INFO("Check non progressive cycles");
309 mc_reduce_kind = e_mc_reduce_none;
311 XBT_INFO("Check a safety property");
313 MC_modelcheck_safety_init();
315 if (mc_reduce_kind == e_mc_reduce_unset)
316 mc_reduce_kind = e_mc_reduce_none;
317 XBT_INFO("Check the liveness property %s", _sg_mc_property_file);
318 MC_automaton_load(_sg_mc_property_file);
319 MC_modelcheck_liveness_init();
332 int MC_deadlock_check()
334 if (mc_mode == MC_MODE_SERVER) {
336 if ((res = MC_protocol_send_simple_message(mc_model_checker->process.socket,
337 MC_MESSAGE_DEADLOCK_CHECK)))
338 xbt_die("Could not check deadlock state");
339 s_mc_int_message_t message;
340 ssize_t s = MC_receive_message(mc_model_checker->process.socket, &message, sizeof(message));
342 xbt_die("Could not receive message");
343 else if (s != sizeof(message) || message.type != MC_MESSAGE_DEADLOCK_CHECK_REPLY) {
344 xbt_die("Unexpected message, expected MC_MESSAGE_DEADLOCK_CHECK_REPLY %i %i vs %i %i",
345 (int) s, (int) message.type, (int) sizeof(message), (int) MC_MESSAGE_DEADLOCK_CHECK_REPLY
349 return message.value;
352 int deadlock = FALSE;
353 smx_process_t process;
354 if (xbt_swag_size(simix_global->process_list)) {
356 MC_EACH_SIMIX_PROCESS(process,
357 if (MC_process_is_enabled(process)) {
366 void MC_handle_comm_pattern(e_mc_call_type_t call_type, smx_simcall_t req, int value, xbt_dynar_t pattern, int backtracking) {
369 case MC_CALL_TYPE_NONE:
371 case MC_CALL_TYPE_SEND:
372 case MC_CALL_TYPE_RECV:
373 MC_get_comm_pattern(pattern, req, call_type, backtracking);
375 case MC_CALL_TYPE_WAIT:
376 case MC_CALL_TYPE_WAITANY:
378 smx_synchro_t current_comm = NULL;
379 if (call_type == MC_CALL_TYPE_WAIT)
380 current_comm = simcall_comm_wait__get__comm(req);
382 current_comm = xbt_dynar_get_as(simcall_comm_waitany__get__comms(req), value, smx_synchro_t);
383 MC_complete_comm_pattern(pattern, current_comm, req->issuer->pid, backtracking);
387 xbt_die("Unexpected call type %i", (int)call_type);
393 * \brief Re-executes from the state at position start all the transitions indicated by
394 * a given model-checker stack.
395 * \param stack The stack with the transitions to execute.
396 * \param start Start index to begin the re-execution.
398 void MC_replay(xbt_fifo_t stack)
400 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
402 int value, count = 1, j;
404 smx_simcall_t req = NULL, saved_req = NULL;
405 xbt_fifo_item_t item, start_item;
408 XBT_DEBUG("**** Begin Replay ****");
410 /* Intermediate backtracking */
411 if(_sg_mc_checkpoint > 0 || _sg_mc_termination || _sg_mc_visited > 0) {
412 start_item = xbt_fifo_get_first_item(stack);
413 state = (mc_state_t)xbt_fifo_get_item_content(start_item);
414 if(state->system_state){
415 MC_restore_snapshot(state->system_state);
416 if(_sg_mc_comms_determinism || _sg_mc_send_determinism)
417 MC_restore_communications_pattern(state);
424 /* Restore the initial state */
425 MC_restore_snapshot(initial_global_state->snapshot);
426 /* At the moment of taking the snapshot the raw heap was set, so restoring
427 * it will set it back again, we have to unset it to continue */
430 start_item = xbt_fifo_get_last_item(stack);
434 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
435 for (j=0; j < simix_process_maxpid; j++) {
436 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
437 ((mc_list_comm_pattern_t)xbt_dynar_get_as(initial_communications_pattern, j, mc_list_comm_pattern_t))->index_comm = 0;
443 /* Traverse the stack from the state at position start and re-execute the transitions */
444 for (item = start_item;
445 item != xbt_fifo_get_first_item(stack);
446 item = xbt_fifo_get_prev_item(item)) {
448 state = (mc_state_t) xbt_fifo_get_item_content(item);
449 saved_req = MC_state_get_executed_request(state, &value);
452 /* because we got a copy of the executed request, we have to fetch the
453 real one, pointed by the request field of the issuer process */
455 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
456 req = &issuer->simcall;
458 /* Debug information */
459 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
460 req_str = MC_request_to_string(req, value);
461 XBT_DEBUG("Replay: %s (%p)", req_str, state);
465 /* TODO : handle test and testany simcalls */
466 e_mc_call_type_t call = MC_CALL_TYPE_NONE;
467 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
468 call = MC_get_call_type(req);
470 MC_simcall_handle(req, value);
473 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
474 MC_handle_comm_pattern(call, req, value, NULL, 1);
477 MC_wait_for_requests();
482 /* Update statistics */
483 mc_stats->visited_states++;
484 mc_stats->executed_transitions++;
488 XBT_DEBUG("**** End Replay ****");
489 mmalloc_set_current_heap(heap);
492 void MC_replay_liveness(xbt_fifo_t stack)
495 initial_global_state->raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
497 xbt_fifo_item_t item;
498 mc_pair_t pair = NULL;
499 mc_state_t state = NULL;
500 smx_simcall_t req = NULL, saved_req = NULL;
501 int value, depth = 1;
504 XBT_DEBUG("**** Begin Replay ****");
506 /* Intermediate backtracking */
507 if(_sg_mc_checkpoint > 0) {
508 item = xbt_fifo_get_first_item(stack);
509 pair = (mc_pair_t) xbt_fifo_get_item_content(item);
510 if(pair->graph_state->system_state){
511 MC_restore_snapshot(pair->graph_state->system_state);
517 /* Restore the initial state */
518 MC_restore_snapshot(initial_global_state->snapshot);
520 /* At the moment of taking the snapshot the raw heap was set, so restoring
521 * it will set it back again, we have to unset it to continue */
522 if (!initial_global_state->raw_mem_set)
525 /* Traverse the stack from the initial state and re-execute the transitions */
526 for (item = xbt_fifo_get_last_item(stack);
527 item != xbt_fifo_get_first_item(stack);
528 item = xbt_fifo_get_prev_item(item)) {
530 pair = (mc_pair_t) xbt_fifo_get_item_content(item);
532 state = (mc_state_t) pair->graph_state;
534 if (pair->exploration_started) {
536 saved_req = MC_state_get_executed_request(state, &value);
538 if (saved_req != NULL) {
539 /* because we got a copy of the executed request, we have to fetch the
540 real one, pointed by the request field of the issuer process */
541 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
542 req = &issuer->simcall;
544 /* Debug information */
545 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
546 req_str = MC_request_to_string(req, value);
547 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
553 MC_simcall_handle(req, value);
554 MC_wait_for_requests();
557 /* Update statistics */
558 mc_stats->visited_pairs++;
559 mc_stats->executed_transitions++;
565 XBT_DEBUG("**** End Replay ****");
567 if (initial_global_state->raw_mem_set)
575 * \brief Dumps the contents of a model-checker's stack and shows the actual
577 * \param stack The stack to dump
579 void MC_dump_stack_safety(xbt_fifo_t stack)
581 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
583 MC_show_stack_safety(stack);
588 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
589 MC_state_delete(state, !state->in_visited_states ? 1 : 0);
590 mmalloc_set_current_heap(heap);
594 void MC_show_stack_safety(xbt_fifo_t stack)
596 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
600 xbt_fifo_item_t item;
602 char *req_str = NULL;
604 for (item = xbt_fifo_get_last_item(stack);
605 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
606 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
607 req = MC_state_get_executed_request(state, &value);
609 req_str = MC_request_to_string(req, value);
610 XBT_INFO("%s", req_str);
615 mmalloc_set_current_heap(heap);
618 void MC_show_deadlock(smx_simcall_t req)
620 /*char *req_str = NULL; */
621 XBT_INFO("**************************");
622 XBT_INFO("*** DEAD-LOCK DETECTED ***");
623 XBT_INFO("**************************");
624 XBT_INFO("Locked request:");
625 /*req_str = MC_request_to_string(req);
626 XBT_INFO("%s", req_str);
627 xbt_free(req_str); */
628 XBT_INFO("Counter-example execution trace:");
629 MC_dump_stack_safety(mc_stack);
630 MC_print_statistics(mc_stats);
633 void MC_show_non_termination(void){
634 XBT_INFO("******************************************");
635 XBT_INFO("*** NON-PROGRESSIVE CYCLE DETECTED ***");
636 XBT_INFO("******************************************");
637 XBT_INFO("Counter-example execution trace:");
638 MC_dump_stack_safety(mc_stack);
639 MC_print_statistics(mc_stats);
643 void MC_show_stack_liveness(xbt_fifo_t stack)
647 xbt_fifo_item_t item;
649 char *req_str = NULL;
651 for (item = xbt_fifo_get_last_item(stack);
652 (item ? (pair = (mc_pair_t) (xbt_fifo_get_item_content(item))) : (NULL));
653 item = xbt_fifo_get_prev_item(item)) {
654 req = MC_state_get_executed_request(pair->graph_state, &value);
655 if (req && req->call != SIMCALL_NONE) {
656 req_str = MC_request_to_string(req, value);
657 XBT_INFO("%s", req_str);
664 void MC_dump_stack_liveness(xbt_fifo_t stack)
666 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
668 while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != NULL)
669 MC_pair_delete(pair);
670 mmalloc_set_current_heap(heap);
673 void MC_print_statistics(mc_stats_t stats)
675 if(_sg_mc_comms_determinism) {
676 if (!initial_global_state->recv_deterministic && initial_global_state->send_deterministic){
677 XBT_INFO("******************************************************");
678 XBT_INFO("**** Only-send-deterministic communication pattern ****");
679 XBT_INFO("******************************************************");
680 XBT_INFO("%s", initial_global_state->recv_diff);
681 }else if(!initial_global_state->send_deterministic && initial_global_state->recv_deterministic) {
682 XBT_INFO("******************************************************");
683 XBT_INFO("**** Only-recv-deterministic communication pattern ****");
684 XBT_INFO("******************************************************");
685 XBT_INFO("%s", initial_global_state->send_diff);
689 if (stats->expanded_pairs == 0) {
690 XBT_INFO("Expanded states = %lu", stats->expanded_states);
691 XBT_INFO("Visited states = %lu", stats->visited_states);
693 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
694 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
696 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
697 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
698 if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0')) {
699 fprintf(dot_output, "}\n");
702 if (initial_global_state != NULL && (_sg_mc_comms_determinism || _sg_mc_send_determinism)) {
703 XBT_INFO("Send-deterministic : %s", !initial_global_state->send_deterministic ? "No" : "Yes");
704 if (_sg_mc_comms_determinism)
705 XBT_INFO("Recv-deterministic : %s", !initial_global_state->recv_deterministic ? "No" : "Yes");
707 mmalloc_set_current_heap(heap);
710 void MC_automaton_load(const char *file)
712 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
714 if (_mc_property_automaton == NULL)
715 _mc_property_automaton = xbt_automaton_new();
717 xbt_automaton_load(_mc_property_automaton, file);
718 mmalloc_set_current_heap(heap);
721 static void register_symbol(xbt_automaton_propositional_symbol_t symbol)
723 if (mc_mode != MC_MODE_CLIENT)
725 s_mc_register_symbol_message_t message;
726 message.type = MC_MESSAGE_REGISTER_SYMBOL;
727 const char* name = xbt_automaton_propositional_symbol_get_name(symbol);
728 if (strlen(name) + 1 > sizeof(message.name))
729 xbt_die("Symbol is too long");
730 strncpy(message.name, name, sizeof(message.name));
731 message.callback = xbt_automaton_propositional_symbol_get_callback(symbol);
732 message.data = xbt_automaton_propositional_symbol_get_data(symbol);
733 MC_client_send_message(&message, sizeof(message));
736 void MC_automaton_new_propositional_symbol(const char *id, int(*fct)(void))
738 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
740 if (_mc_property_automaton == NULL)
741 _mc_property_automaton = xbt_automaton_new();
743 xbt_automaton_propositional_symbol_t symbol = xbt_automaton_propositional_symbol_new(_mc_property_automaton, id, fct);
744 register_symbol(symbol);
745 mmalloc_set_current_heap(heap);
748 void MC_automaton_new_propositional_symbol_pointer(const char *id, int* value)
750 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
751 if (_mc_property_automaton == NULL)
752 _mc_property_automaton = xbt_automaton_new();
753 xbt_automaton_propositional_symbol_t symbol = xbt_automaton_propositional_symbol_new_pointer(_mc_property_automaton, id, value);
754 register_symbol(symbol);
755 mmalloc_set_current_heap(heap);
758 void MC_automaton_new_propositional_symbol_callback(const char* id,
759 xbt_automaton_propositional_symbol_callback_type callback,
760 void* data, xbt_automaton_propositional_symbol_free_function_type free_function)
762 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
763 if (_mc_property_automaton == NULL)
764 _mc_property_automaton = xbt_automaton_new();
765 xbt_automaton_propositional_symbol_t symbol = xbt_automaton_propositional_symbol_new_callback(
766 _mc_property_automaton, id, callback, data, free_function);
767 register_symbol(symbol);
768 mmalloc_set_current_heap(heap);
771 void MC_dump_stacks(FILE* file)
773 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
776 stack_region_t current_stack;
778 xbt_dynar_foreach(stacks_areas, cursor, current_stack) {
779 unw_context_t * context = (unw_context_t *)current_stack->context;
780 fprintf(file, "Stack %i:\n", nstack);
785 unw_init_local (&c, context);
788 const char * name = !unw_get_proc_name(&c, buffer, 100, &off) ? buffer : "?";
789 fprintf(file, " %i: %s\n", nframe, name);
791 } while(unw_step(&c));
795 mmalloc_set_current_heap(heap);
799 double MC_process_clock_get(smx_process_t process)
803 return mc_time[process->pid];
811 void MC_process_clock_add(smx_process_t process, double amount)
813 mc_time[process->pid] += amount;