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/RemoteSimulation.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<RemoteSimulation> remote_simulation, int sockfd)
36 : checker_side_(sockfd), remote_simulation_(std::move(remote_simulation))
40 void ModelChecker::start()
42 checker_side_.start([](evutil_socket_t sig, short events, void* arg) {
43 auto mc = static_cast<simgrid::mc::ModelChecker*>(arg);
44 if (events == EV_READ) {
45 char buffer[MC_MESSAGE_LENGTH];
46 ssize_t size = mc->checker_side_.get_channel().receive(buffer, sizeof(buffer), false);
47 if (size == -1 && errno != EAGAIN)
48 throw simgrid::xbt::errno_error();
50 if (not mc->handle_message(buffer, size))
51 mc->checker_side_.break_loop();
52 } else if (events == EV_SIGNAL) {
56 xbt_die("Unexpected event");
60 XBT_DEBUG("Waiting for the model-checked process");
63 // The model-checked process SIGSTOP itself to signal it's ready:
64 const pid_t pid = remote_simulation_->pid();
66 pid_t res = waitpid(pid, &status, WAITPID_CHECKED_FLAGS);
67 if (res < 0 || not WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP)
68 xbt_die("Could not wait model-checked process");
70 remote_simulation_->init();
72 if (not _sg_mc_dot_output_file.get().empty())
78 ptrace(PTRACE_SETOPTIONS, pid, nullptr, PTRACE_O_TRACEEXIT);
79 ptrace(PTRACE_CONT, pid, 0, 0);
81 ptrace(PT_CONTINUE, pid, (caddr_t)1, 0);
83 # error "no ptrace equivalent coded for this platform"
87 static const std::pair<const char*, const char*> ignored_local_variables[] = {
88 std::pair<const char*, const char*>{ "e", "*" },
89 std::pair<const char*, const char*>{ "_log_ev", "*" },
91 /* Ignore local variable about time used for tracing */
92 std::pair<const char*, const char*>{ "start_time", "*" },
95 void ModelChecker::setup_ignore()
97 const RemoteSimulation& process = this->get_remote_simulation();
98 for (std::pair<const char*, const char*> const& var :
99 ignored_local_variables)
100 process.ignore_local_variable(var.first, var.second);
102 /* Static variable used for tracing */
103 process.ignore_global_variable("counter");
106 void ModelChecker::shutdown()
108 XBT_DEBUG("Shutting down model-checker");
110 RemoteSimulation* process = &this->get_remote_simulation();
111 if (process->running()) {
112 XBT_DEBUG("Killing process");
113 kill(process->pid(), SIGKILL);
114 process->terminate();
118 void ModelChecker::resume(RemoteSimulation& process)
120 int res = checker_side_.get_channel().send(MC_MESSAGE_CONTINUE);
122 throw xbt::errno_error();
123 process.clear_cache();
126 static void MC_report_crash(int status)
128 XBT_INFO("**************************");
129 XBT_INFO("** CRASH IN THE PROGRAM **");
130 XBT_INFO("**************************");
131 if (WIFSIGNALED(status))
132 XBT_INFO("From signal: %s", strsignal(WTERMSIG(status)));
133 else if (WIFEXITED(status))
134 XBT_INFO("From exit: %i", WEXITSTATUS(status));
135 if (not xbt_log_no_loc)
136 XBT_INFO("%s core dump was generated by the system.", WCOREDUMP(status) ? "A" : "No");
137 XBT_INFO("Counter-example execution trace:");
138 for (auto const& s : mc_model_checker->getChecker()->get_textual_trace())
139 XBT_INFO(" %s", s.c_str());
141 session->log_state();
142 if (xbt_log_no_loc) {
143 XBT_INFO("Stack trace not displayed because you passed --log=no_loc");
145 XBT_INFO("Stack trace:");
146 mc_model_checker->get_remote_simulation().dump_stack();
150 static void MC_report_assertion_error()
152 XBT_INFO("**************************");
153 XBT_INFO("*** PROPERTY NOT VALID ***");
154 XBT_INFO("**************************");
155 XBT_INFO("Counter-example execution trace:");
156 for (auto const& s : mc_model_checker->getChecker()->get_textual_trace())
157 XBT_INFO(" %s", s.c_str());
159 session->log_state();
162 bool ModelChecker::handle_message(const char* buffer, ssize_t size)
164 s_mc_message_t base_message;
165 xbt_assert(size >= (ssize_t)sizeof(base_message), "Broken message");
166 memcpy(&base_message, buffer, sizeof(base_message));
168 switch(base_message.type) {
169 case MC_MESSAGE_IGNORE_HEAP:
171 s_mc_message_ignore_heap_t message;
172 xbt_assert(size == sizeof(message), "Broken message");
173 memcpy(&message, buffer, sizeof(message));
175 IgnoredHeapRegion region;
176 region.block = message.block;
177 region.fragment = message.fragment;
178 region.address = message.address;
179 region.size = message.size;
180 get_remote_simulation().ignore_heap(region);
184 case MC_MESSAGE_UNIGNORE_HEAP:
186 s_mc_message_ignore_memory_t message;
187 xbt_assert(size == sizeof(message), "Broken message");
188 memcpy(&message, buffer, sizeof(message));
189 get_remote_simulation().unignore_heap((void*)(std::uintptr_t)message.addr, message.size);
193 case MC_MESSAGE_IGNORE_MEMORY:
195 s_mc_message_ignore_memory_t message;
196 xbt_assert(size == sizeof(message), "Broken message");
197 memcpy(&message, buffer, sizeof(message));
198 this->get_remote_simulation().ignore_region(message.addr, message.size);
202 case MC_MESSAGE_STACK_REGION:
204 s_mc_message_stack_region_t message;
205 xbt_assert(size == sizeof(message), "Broken message");
206 memcpy(&message, buffer, sizeof(message));
207 this->get_remote_simulation().stack_areas().push_back(message.stack_region);
211 case MC_MESSAGE_REGISTER_SYMBOL:
213 s_mc_message_register_symbol_t message;
214 xbt_assert(size == sizeof(message), "Broken message");
215 memcpy(&message, buffer, sizeof(message));
216 xbt_assert(not message.callback, "Support for client-side function proposition is not implemented.");
217 XBT_DEBUG("Received symbol: %s", message.name);
219 if (property_automaton == nullptr)
220 property_automaton = xbt_automaton_new();
222 RemoteSimulation* process = &this->get_remote_simulation();
223 RemotePtr<int> address = remote((int*)message.data);
224 xbt::add_proposition(property_automaton, message.name, [process, address]() { return process->read(address); });
229 case MC_MESSAGE_WAITING:
232 case MC_MESSAGE_ASSERTION_FAILED:
233 MC_report_assertion_error();
234 this->exit(SIMGRID_MC_EXIT_SAFETY);
237 xbt_die("Unexpected message from model-checked application");
242 /** Terminate the model-checker application */
243 void ModelChecker::exit(int status)
245 // TODO, terminate the model checker politely instead of exiting rudely
246 if (get_remote_simulation().running())
247 kill(get_remote_simulation().pid(), SIGKILL);
251 void ModelChecker::handle_waitpid()
253 XBT_DEBUG("Check for wait event");
256 while ((pid = waitpid(-1, &status, WNOHANG)) != 0) {
258 if (errno == ECHILD) {
260 xbt_assert(not this->get_remote_simulation().running(), "Inconsistent state");
263 XBT_ERROR("Could not wait for pid");
264 throw simgrid::xbt::errno_error();
268 if (pid == this->get_remote_simulation().pid()) {
269 // From PTRACE_O_TRACEEXIT:
271 if (status>>8 == (SIGTRAP | (PTRACE_EVENT_EXIT<<8))) {
272 xbt_assert(ptrace(PTRACE_GETEVENTMSG, remote_simulation_->pid(), 0, &status) != -1,
273 "Could not get exit status");
274 if (WIFSIGNALED(status)) {
275 MC_report_crash(status);
276 mc_model_checker->exit(SIMGRID_MC_EXIT_PROGRAM_CRASH);
281 // We don't care about signals, just reinject them:
282 if (WIFSTOPPED(status)) {
283 XBT_DEBUG("Stopped with signal %i", (int) WSTOPSIG(status));
286 ptrace(PTRACE_CONT, remote_simulation_->pid(), 0, WSTOPSIG(status));
288 ptrace(PT_CONTINUE, remote_simulation_->pid(), (caddr_t)1, WSTOPSIG(status));
290 xbt_assert(errno == 0, "Could not PTRACE_CONT");
293 else if (WIFSIGNALED(status)) {
294 MC_report_crash(status);
295 mc_model_checker->exit(SIMGRID_MC_EXIT_PROGRAM_CRASH);
296 } else if (WIFEXITED(status)) {
297 XBT_DEBUG("Child process is over");
298 this->get_remote_simulation().terminate();
304 void ModelChecker::wait_for_requests()
306 this->resume(get_remote_simulation());
307 if (this->get_remote_simulation().running())
308 checker_side_.dispatch();
311 void ModelChecker::handle_simcall(Transition const& transition)
313 s_mc_message_simcall_handle_t m;
314 memset(&m, 0, sizeof(m));
315 m.type = MC_MESSAGE_SIMCALL_HANDLE;
316 m.pid = transition.pid_;
317 m.value = transition.argument_;
318 checker_side_.get_channel().send(m);
319 this->remote_simulation_->clear_cache();
320 if (this->remote_simulation_->running())
321 checker_side_.dispatch();
324 bool ModelChecker::checkDeadlock()
326 int res = checker_side_.get_channel().send(MC_MESSAGE_DEADLOCK_CHECK);
327 xbt_assert(res == 0, "Could not check deadlock state");
328 s_mc_message_int_t message;
329 ssize_t s = checker_side_.get_channel().receive(message);
330 xbt_assert(s != -1, "Could not receive message");
331 xbt_assert(s == sizeof(message) && message.type == MC_MESSAGE_DEADLOCK_CHECK_REPLY,
332 "Received unexpected message %s (%i, size=%i) "
333 "expected MC_MESSAGE_DEADLOCK_CHECK_REPLY (%i, size=%i)",
334 MC_message_type_name(message.type), (int)message.type, (int)s, (int)MC_MESSAGE_DEADLOCK_CHECK_REPLY,
335 (int)sizeof(message));
336 return message.value != 0;
340 } // namespace simgrid