1 /* Copyright (c) 2008-2020. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "src/mc/ModelChecker.hpp"
7 #include "src/mc/Session.hpp"
8 #include "src/mc/Transition.hpp"
9 #include "src/mc/checker/Checker.hpp"
10 #include "src/mc/mc_config.hpp"
11 #include "src/mc/mc_exit.hpp"
12 #include "src/mc/mc_private.hpp"
13 #include "src/mc/remote/RemoteClient.hpp"
14 #include "xbt/automaton.hpp"
15 #include "xbt/system_error.hpp"
17 #include <sys/ptrace.h>
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_ModelChecker, mc, "ModelChecker");
22 ::simgrid::mc::ModelChecker* mc_model_checker = nullptr;
24 using simgrid::mc::remote;
27 # define WAITPID_CHECKED_FLAGS __WALL
29 # define WAITPID_CHECKED_FLAGS 0
35 ModelChecker::ModelChecker(std::unique_ptr<RemoteClient> process) : process_(std::move(process)) {}
37 void ModelChecker::start()
39 event_loop_.start(process_->get_channel().get_socket(), [](evutil_socket_t sig, short events, void* arg) {
40 ((ModelChecker*)arg)->handle_events(sig, events);
43 XBT_DEBUG("Waiting for the model-checked process");
46 // The model-checked process SIGSTOP itself to signal it's ready:
47 const pid_t pid = process_->pid();
49 pid_t res = waitpid(pid, &status, WAITPID_CHECKED_FLAGS);
50 if (res < 0 || not WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP)
51 xbt_die("Could not wait model-checked process");
55 if (not _sg_mc_dot_output_file.get().empty())
61 ptrace(PTRACE_SETOPTIONS, pid, nullptr, PTRACE_O_TRACEEXIT);
62 ptrace(PTRACE_CONT, pid, 0, 0);
64 ptrace(PT_CONTINUE, pid, (caddr_t)1, 0);
66 # error "no ptrace equivalent coded for this platform"
70 static const std::pair<const char*, const char*> ignored_local_variables[] = {
71 std::pair<const char*, const char*>{ "e", "*" },
72 std::pair<const char*, const char*>{ "_log_ev", "*" },
74 /* Ignore local variable about time used for tracing */
75 std::pair<const char*, const char*>{ "start_time", "*" },
78 void ModelChecker::setup_ignore()
80 RemoteClient& process = this->process();
81 for (std::pair<const char*, const char*> const& var :
82 ignored_local_variables)
83 process.ignore_local_variable(var.first, var.second);
85 /* Static variable used for tracing */
86 process.ignore_global_variable("counter");
89 void ModelChecker::shutdown()
91 XBT_DEBUG("Shuting down model-checker");
93 RemoteClient* process = &this->process();
94 if (process->running()) {
95 XBT_DEBUG("Killing process");
96 kill(process->pid(), SIGKILL);
101 void ModelChecker::resume(RemoteClient& process)
103 int res = process.get_channel().send(MC_MESSAGE_CONTINUE);
105 throw xbt::errno_error();
106 process.clear_cache();
109 static void MC_report_crash(int status)
111 XBT_INFO("**************************");
112 XBT_INFO("** CRASH IN THE PROGRAM **");
113 XBT_INFO("**************************");
114 if (WIFSIGNALED(status))
115 XBT_INFO("From signal: %s", strsignal(WTERMSIG(status)));
116 else if (WIFEXITED(status))
117 XBT_INFO("From exit: %i", WEXITSTATUS(status));
118 if (not xbt_log_no_loc)
119 XBT_INFO("%s core dump was generated by the system.", WCOREDUMP(status) ? "A" : "No");
120 XBT_INFO("Counter-example execution trace:");
121 for (auto const& s : mc_model_checker->getChecker()->get_textual_trace())
122 XBT_INFO(" %s", s.c_str());
124 session->log_state();
125 if (xbt_log_no_loc) {
126 XBT_INFO("Stack trace not displayed because you passed --log=no_loc");
128 XBT_INFO("Stack trace:");
129 mc_model_checker->process().dump_stack();
133 static void MC_report_assertion_error()
135 XBT_INFO("**************************");
136 XBT_INFO("*** PROPERTY NOT VALID ***");
137 XBT_INFO("**************************");
138 XBT_INFO("Counter-example execution trace:");
139 for (auto const& s : mc_model_checker->getChecker()->get_textual_trace())
140 XBT_INFO(" %s", s.c_str());
142 session->log_state();
145 bool ModelChecker::handle_message(const char* buffer, ssize_t size)
147 s_mc_message_t base_message;
148 xbt_assert(size >= (ssize_t)sizeof(base_message), "Broken message");
149 memcpy(&base_message, buffer, sizeof(base_message));
151 switch(base_message.type) {
152 case MC_MESSAGE_IGNORE_HEAP:
154 s_mc_message_ignore_heap_t message;
155 xbt_assert(size == sizeof(message), "Broken messsage");
156 memcpy(&message, buffer, sizeof(message));
158 IgnoredHeapRegion region;
159 region.block = message.block;
160 region.fragment = message.fragment;
161 region.address = message.address;
162 region.size = message.size;
163 process().ignore_heap(region);
167 case MC_MESSAGE_UNIGNORE_HEAP:
169 s_mc_message_ignore_memory_t message;
170 xbt_assert(size == sizeof(message), "Broken messsage");
171 memcpy(&message, buffer, sizeof(message));
172 process().unignore_heap((void*)(std::uintptr_t)message.addr, message.size);
176 case MC_MESSAGE_IGNORE_MEMORY:
178 s_mc_message_ignore_memory_t message;
179 xbt_assert(size == sizeof(message), "Broken messsage");
180 memcpy(&message, buffer, sizeof(message));
181 this->process().ignore_region(message.addr, message.size);
185 case MC_MESSAGE_STACK_REGION:
187 s_mc_message_stack_region_t message;
188 xbt_assert(size == sizeof(message), "Broken messsage");
189 memcpy(&message, buffer, sizeof(message));
190 this->process().stack_areas().push_back(message.stack_region);
194 case MC_MESSAGE_REGISTER_SYMBOL:
196 s_mc_message_register_symbol_t message;
197 xbt_assert(size == sizeof(message), "Broken message");
198 memcpy(&message, buffer, sizeof(message));
199 xbt_assert(not message.callback, "Support for client-side function proposition is not implemented.");
200 XBT_DEBUG("Received symbol: %s", message.name);
202 if (property_automaton == nullptr)
203 property_automaton = xbt_automaton_new();
205 RemoteClient* process = &this->process();
206 RemotePtr<int> address = remote((int*)message.data);
207 xbt::add_proposition(property_automaton, message.name, [process, address]() { return process->read(address); });
212 case MC_MESSAGE_WAITING:
215 case MC_MESSAGE_ASSERTION_FAILED:
216 MC_report_assertion_error();
217 this->exit(SIMGRID_MC_EXIT_SAFETY);
220 xbt_die("Unexpected message from model-checked application");
225 /** Terminate the model-checker application */
226 void ModelChecker::exit(int status)
228 // TODO, terminate the model checker politely instead of exiting rudely
229 if (process().running())
230 kill(process().pid(), SIGKILL);
234 void ModelChecker::handle_events(int sig, short events)
236 if (events == EV_READ) {
237 char buffer[MC_MESSAGE_LENGTH];
238 ssize_t size = process_->get_channel().receive(buffer, sizeof(buffer), false);
239 if (size == -1 && errno != EAGAIN)
240 throw simgrid::xbt::errno_error();
242 if (not handle_message(buffer, size))
243 event_loop_.break_loop();
245 else if (events == EV_SIGNAL) {
247 this->handle_waitpid();
250 xbt_die("Unexpected event");
254 void ModelChecker::handle_waitpid()
256 XBT_DEBUG("Check for wait event");
259 while ((pid = waitpid(-1, &status, WNOHANG)) != 0) {
261 if (errno == ECHILD) {
263 xbt_assert(not this->process().running(), "Inconsistent state");
266 XBT_ERROR("Could not wait for pid");
267 throw simgrid::xbt::errno_error();
271 if (pid == this->process().pid()) {
272 // From PTRACE_O_TRACEEXIT:
274 if (status>>8 == (SIGTRAP | (PTRACE_EVENT_EXIT<<8))) {
275 xbt_assert(ptrace(PTRACE_GETEVENTMSG, this->process().pid(), 0, &status) != -1, "Could not get exit status");
276 if (WIFSIGNALED(status)) {
277 MC_report_crash(status);
278 mc_model_checker->exit(SIMGRID_MC_EXIT_PROGRAM_CRASH);
283 // We don't care about signals, just reinject them:
284 if (WIFSTOPPED(status)) {
285 XBT_DEBUG("Stopped with signal %i", (int) WSTOPSIG(status));
288 ptrace(PTRACE_CONT, this->process().pid(), 0, WSTOPSIG(status));
290 ptrace(PT_CONTINUE, this->process().pid(), (caddr_t)1, WSTOPSIG(status));
292 xbt_assert(errno == 0, "Could not PTRACE_CONT");
295 else if (WIFSIGNALED(status)) {
296 MC_report_crash(status);
297 mc_model_checker->exit(SIMGRID_MC_EXIT_PROGRAM_CRASH);
298 } else if (WIFEXITED(status)) {
299 XBT_DEBUG("Child process is over");
300 this->process().terminate();
306 void ModelChecker::wait_for_requests()
308 this->resume(process());
309 if (this->process().running())
310 event_loop_.dispatch();
313 void ModelChecker::handle_simcall(Transition const& transition)
315 s_mc_message_simcall_handle_t m;
316 memset(&m, 0, sizeof(m));
317 m.type = MC_MESSAGE_SIMCALL_HANDLE;
318 m.pid = transition.pid_;
319 m.value = transition.argument_;
320 this->process_->get_channel().send(m);
321 this->process_->clear_cache();
322 if (this->process_->running())
323 event_loop_.dispatch();
326 bool ModelChecker::checkDeadlock()
328 int res = this->process().get_channel().send(MC_MESSAGE_DEADLOCK_CHECK);
329 xbt_assert(res == 0, "Could not check deadlock state");
330 s_mc_message_int_t message;
331 ssize_t s = mc_model_checker->process().get_channel().receive(message);
332 xbt_assert(s != -1, "Could not receive message");
333 xbt_assert(s == sizeof(message) && message.type == MC_MESSAGE_DEADLOCK_CHECK_REPLY,
334 "Received unexpected message %s (%i, size=%i) "
335 "expected MC_MESSAGE_DEADLOCK_CHECK_REPLY (%i, size=%i)",
336 MC_message_type_name(message.type), (int)message.type, (int)s, (int)MC_MESSAGE_DEADLOCK_CHECK_REPLY,
337 (int)sizeof(message));
338 return message.value != 0;
342 } // namespace simgrid