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 constexpr auto ignored_local_variables = {
88 std::make_pair("e", "*"),
89 std::make_pair("_log_ev", "*"),
91 /* Ignore local variable about time used for tracing */
92 std::make_pair("start_time", "*"),
95 void ModelChecker::setup_ignore()
97 const RemoteSimulation& process = this->get_remote_simulation();
98 for (auto const& var : ignored_local_variables)
99 process.ignore_local_variable(var.first, var.second);
101 /* Static variable used for tracing */
102 process.ignore_global_variable("counter");
105 void ModelChecker::shutdown()
107 XBT_DEBUG("Shutting down model-checker");
109 RemoteSimulation* process = &this->get_remote_simulation();
110 if (process->running()) {
111 XBT_DEBUG("Killing process");
112 kill(process->pid(), SIGKILL);
113 process->terminate();
117 void ModelChecker::resume(RemoteSimulation& process)
119 int res = checker_side_.get_channel().send(MC_MESSAGE_CONTINUE);
121 throw xbt::errno_error();
122 process.clear_cache();
125 static void MC_report_crash(int status)
127 XBT_INFO("**************************");
128 XBT_INFO("** CRASH IN THE PROGRAM **");
129 XBT_INFO("**************************");
130 if (WIFSIGNALED(status))
131 XBT_INFO("From signal: %s", strsignal(WTERMSIG(status)));
132 else if (WIFEXITED(status))
133 XBT_INFO("From exit: %i", WEXITSTATUS(status));
134 if (not xbt_log_no_loc)
135 XBT_INFO("%s core dump was generated by the system.", WCOREDUMP(status) ? "A" : "No");
136 XBT_INFO("Counter-example execution trace:");
137 for (auto const& s : mc_model_checker->getChecker()->get_textual_trace())
138 XBT_INFO(" %s", s.c_str());
140 session->log_state();
141 if (xbt_log_no_loc) {
142 XBT_INFO("Stack trace not displayed because you passed --log=no_loc");
144 XBT_INFO("Stack trace:");
145 mc_model_checker->get_remote_simulation().dump_stack();
149 static void MC_report_assertion_error()
151 XBT_INFO("**************************");
152 XBT_INFO("*** PROPERTY NOT VALID ***");
153 XBT_INFO("**************************");
154 XBT_INFO("Counter-example execution trace:");
155 for (auto const& s : mc_model_checker->getChecker()->get_textual_trace())
156 XBT_INFO(" %s", s.c_str());
158 session->log_state();
161 bool ModelChecker::handle_message(const char* buffer, ssize_t size)
163 s_mc_message_t base_message;
164 xbt_assert(size >= (ssize_t)sizeof(base_message), "Broken message");
165 memcpy(&base_message, buffer, sizeof(base_message));
167 switch(base_message.type) {
168 case MC_MESSAGE_IGNORE_HEAP:
170 s_mc_message_ignore_heap_t message;
171 xbt_assert(size == sizeof(message), "Broken message");
172 memcpy(&message, buffer, sizeof(message));
174 IgnoredHeapRegion region;
175 region.block = message.block;
176 region.fragment = message.fragment;
177 region.address = message.address;
178 region.size = message.size;
179 get_remote_simulation().ignore_heap(region);
183 case MC_MESSAGE_UNIGNORE_HEAP:
185 s_mc_message_ignore_memory_t message;
186 xbt_assert(size == sizeof(message), "Broken message");
187 memcpy(&message, buffer, sizeof(message));
188 get_remote_simulation().unignore_heap((void*)(std::uintptr_t)message.addr, message.size);
192 case MC_MESSAGE_IGNORE_MEMORY:
194 s_mc_message_ignore_memory_t message;
195 xbt_assert(size == sizeof(message), "Broken message");
196 memcpy(&message, buffer, sizeof(message));
197 this->get_remote_simulation().ignore_region(message.addr, message.size);
201 case MC_MESSAGE_STACK_REGION:
203 s_mc_message_stack_region_t message;
204 xbt_assert(size == sizeof(message), "Broken message");
205 memcpy(&message, buffer, sizeof(message));
206 this->get_remote_simulation().stack_areas().push_back(message.stack_region);
210 case MC_MESSAGE_REGISTER_SYMBOL:
212 s_mc_message_register_symbol_t message;
213 xbt_assert(size == sizeof(message), "Broken message");
214 memcpy(&message, buffer, sizeof(message));
215 xbt_assert(not message.callback, "Support for client-side function proposition is not implemented.");
216 XBT_DEBUG("Received symbol: %s", message.name);
218 if (property_automaton == nullptr)
219 property_automaton = xbt_automaton_new();
221 const RemoteSimulation* process = &this->get_remote_simulation();
222 RemotePtr<int> address = remote((int*)message.data);
223 xbt::add_proposition(property_automaton, message.name, [process, address]() { return process->read(address); });
228 case MC_MESSAGE_WAITING:
231 case MC_MESSAGE_ASSERTION_FAILED:
232 MC_report_assertion_error();
233 this->exit(SIMGRID_MC_EXIT_SAFETY);
236 xbt_die("Unexpected message from model-checked application");
241 /** Terminate the model-checker application */
242 void ModelChecker::exit(int status)
244 // TODO, terminate the model checker politely instead of exiting rudely
245 if (get_remote_simulation().running())
246 kill(get_remote_simulation().pid(), SIGKILL);
250 void ModelChecker::handle_waitpid()
252 XBT_DEBUG("Check for wait event");
255 while ((pid = waitpid(-1, &status, WNOHANG)) != 0) {
257 if (errno == ECHILD) {
259 xbt_assert(not this->get_remote_simulation().running(), "Inconsistent state");
262 XBT_ERROR("Could not wait for pid");
263 throw simgrid::xbt::errno_error();
267 if (pid == this->get_remote_simulation().pid()) {
268 // From PTRACE_O_TRACEEXIT:
270 if (status>>8 == (SIGTRAP | (PTRACE_EVENT_EXIT<<8))) {
271 xbt_assert(ptrace(PTRACE_GETEVENTMSG, remote_simulation_->pid(), 0, &status) != -1,
272 "Could not get exit status");
273 if (WIFSIGNALED(status)) {
274 MC_report_crash(status);
275 mc_model_checker->exit(SIMGRID_MC_EXIT_PROGRAM_CRASH);
280 // We don't care about signals, just reinject them:
281 if (WIFSTOPPED(status)) {
282 XBT_DEBUG("Stopped with signal %i", (int) WSTOPSIG(status));
285 ptrace(PTRACE_CONT, remote_simulation_->pid(), 0, WSTOPSIG(status));
287 ptrace(PT_CONTINUE, remote_simulation_->pid(), (caddr_t)1, WSTOPSIG(status));
289 xbt_assert(errno == 0, "Could not PTRACE_CONT");
292 else if (WIFSIGNALED(status)) {
293 MC_report_crash(status);
294 mc_model_checker->exit(SIMGRID_MC_EXIT_PROGRAM_CRASH);
295 } else if (WIFEXITED(status)) {
296 XBT_DEBUG("Child process is over");
297 this->get_remote_simulation().terminate();
303 void ModelChecker::wait_for_requests()
305 this->resume(get_remote_simulation());
306 if (this->get_remote_simulation().running())
307 checker_side_.dispatch();
310 void ModelChecker::handle_simcall(Transition const& transition)
312 s_mc_message_simcall_handle_t m;
313 memset(&m, 0, sizeof(m));
314 m.type = MC_MESSAGE_SIMCALL_HANDLE;
315 m.pid = transition.pid_;
316 m.value = transition.argument_;
317 checker_side_.get_channel().send(m);
318 this->remote_simulation_->clear_cache();
319 if (this->remote_simulation_->running())
320 checker_side_.dispatch();
323 bool ModelChecker::checkDeadlock()
325 int res = checker_side_.get_channel().send(MC_MESSAGE_DEADLOCK_CHECK);
326 xbt_assert(res == 0, "Could not check deadlock state");
327 s_mc_message_int_t message;
328 ssize_t s = checker_side_.get_channel().receive(message);
329 xbt_assert(s != -1, "Could not receive message");
330 xbt_assert(s == sizeof(message) && message.type == MC_MESSAGE_DEADLOCK_CHECK_REPLY,
331 "Received unexpected message %s (%i, size=%i) "
332 "expected MC_MESSAGE_DEADLOCK_CHECK_REPLY (%i, size=%i)",
333 MC_message_type_name(message.type), (int)message.type, (int)s, (int)MC_MESSAGE_DEADLOCK_CHECK_REPLY,
334 (int)sizeof(message));
335 return message.value != 0;
339 } // namespace simgrid