1 /* Copyright (c) 2008-2019. 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. */
8 #include <sys/socket.h>
9 #include <sys/ptrace.h>
12 #include <system_error>
14 #include "xbt/automaton.h"
15 #include "xbt/automaton.hpp"
17 #include "xbt/system_error.hpp"
19 #include "simgrid/sg_config.hpp"
21 #include "src/mc/Transition.hpp"
22 #include "src/mc/checker/Checker.hpp"
23 #include "src/mc/mc_exit.hpp"
24 #include "src/mc/mc_private.hpp"
25 #include "src/mc/remote/RemoteClient.hpp"
27 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_ModelChecker, mc, "ModelChecker");
29 ::simgrid::mc::ModelChecker* mc_model_checker = nullptr;
31 using simgrid::mc::remote;
34 # define WAITPID_CHECKED_FLAGS __WALL
36 # define WAITPID_CHECKED_FLAGS 0
42 ModelChecker::ModelChecker(std::unique_ptr<RemoteClient> process)
44 , socket_event_(nullptr)
45 , signal_event_(nullptr)
47 , process_(std::move(process))
48 , parent_snapshot_(nullptr)
53 ModelChecker::~ModelChecker() {
54 if (socket_event_ != nullptr)
55 event_free(socket_event_);
56 if (signal_event_ != nullptr)
57 event_free(signal_event_);
59 event_base_free(base_);
62 void ModelChecker::start()
64 const pid_t pid = process_->pid();
66 base_ = event_base_new();
67 event_callback_fn event_callback = [](evutil_socket_t fd, short events, void *arg)
69 ((ModelChecker *)arg)->handle_events(fd, events);
71 socket_event_ = event_new(base_,
72 process_->getChannel().getSocket(),
74 event_callback, this);
75 event_add(socket_event_, NULL);
76 signal_event_ = event_new(base_,
79 event_callback, this);
80 event_add(signal_event_, NULL);
82 XBT_DEBUG("Waiting for the model-checked process");
85 // The model-checked process SIGSTOP itself to signal it's ready:
86 pid_t res = waitpid(pid, &status, WAITPID_CHECKED_FLAGS);
87 if (res < 0 || not WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP)
88 xbt_die("Could not wait model-checked process");
92 if (not _sg_mc_dot_output_file.get().empty())
98 ptrace(PTRACE_SETOPTIONS, pid, nullptr, PTRACE_O_TRACEEXIT);
99 ptrace(PTRACE_CONT, pid, 0, 0);
101 ptrace(PT_CONTINUE, pid, (caddr_t)1, 0);
103 # error "no ptrace equivalent coded for this platform"
107 static const std::pair<const char*, const char*> ignored_local_variables[] = {
108 std::pair<const char*, const char*>{ "e", "*" },
109 std::pair<const char*, const char*>{ "_log_ev", "*" },
111 /* Ignore local variable about time used for tracing */
112 std::pair<const char*, const char*>{ "start_time", "*" },
115 void ModelChecker::setup_ignore()
117 RemoteClient& process = this->process();
118 for (std::pair<const char*, const char*> const& var :
119 ignored_local_variables)
120 process.ignore_local_variable(var.first, var.second);
122 /* Static variable used for tracing */
123 process.ignore_global_variable("counter");
126 void ModelChecker::shutdown()
128 XBT_DEBUG("Shuting down model-checker");
130 simgrid::mc::RemoteClient* process = &this->process();
131 if (process->running()) {
132 XBT_DEBUG("Killing process");
133 kill(process->pid(), SIGKILL);
134 process->terminate();
138 void ModelChecker::resume(simgrid::mc::RemoteClient& process)
140 int res = process.getChannel().send(MC_MESSAGE_CONTINUE);
142 throw simgrid::xbt::errno_error();
143 process.clear_cache();
146 static void MC_report_crash(int status)
148 XBT_INFO("**************************");
149 XBT_INFO("** CRASH IN THE PROGRAM **");
150 XBT_INFO("**************************");
151 if (WIFSIGNALED(status))
152 XBT_INFO("From signal: %s", strsignal(WTERMSIG(status)));
153 else if (WIFEXITED(status))
154 XBT_INFO("From exit: %i", WEXITSTATUS(status));
155 if (WCOREDUMP(status))
156 XBT_INFO("A core dump was generated by the system.");
158 XBT_INFO("No core dump was generated by the system.");
159 XBT_INFO("Counter-example execution trace:");
160 for (auto const& s : mc_model_checker->getChecker()->getTextualTrace())
161 XBT_INFO(" %s", s.c_str());
162 simgrid::mc::dumpRecordPath();
163 simgrid::mc::session->logState();
164 XBT_INFO("Stack trace:");
165 mc_model_checker->process().dumpStack();
168 static void MC_report_assertion_error()
170 XBT_INFO("**************************");
171 XBT_INFO("*** PROPERTY NOT VALID ***");
172 XBT_INFO("**************************");
173 XBT_INFO("Counter-example execution trace:");
174 for (auto const& s : mc_model_checker->getChecker()->getTextualTrace())
175 XBT_INFO(" %s", s.c_str());
176 simgrid::mc::dumpRecordPath();
177 simgrid::mc::session->logState();
180 bool ModelChecker::handle_message(char* buffer, ssize_t size)
182 s_mc_message_t base_message;
183 if (size < (ssize_t) sizeof(base_message))
184 xbt_die("Broken message");
185 memcpy(&base_message, buffer, sizeof(base_message));
187 switch(base_message.type) {
189 case MC_MESSAGE_IGNORE_HEAP:
191 s_mc_message_ignore_heap_t message;
192 if (size != sizeof(message))
193 xbt_die("Broken messsage");
194 memcpy(&message, buffer, sizeof(message));
196 IgnoredHeapRegion region;
197 region.block = message.block;
198 region.fragment = message.fragment;
199 region.address = message.address;
200 region.size = message.size;
201 process().ignore_heap(region);
205 case MC_MESSAGE_UNIGNORE_HEAP:
207 s_mc_message_ignore_memory_t message;
208 if (size != sizeof(message))
209 xbt_die("Broken messsage");
210 memcpy(&message, buffer, sizeof(message));
211 process().unignore_heap((void*)(std::uintptr_t)message.addr, message.size);
215 case MC_MESSAGE_IGNORE_MEMORY:
217 s_mc_message_ignore_memory_t message;
218 if (size != sizeof(message))
219 xbt_die("Broken messsage");
220 memcpy(&message, buffer, sizeof(message));
221 this->process().ignore_region(message.addr, message.size);
225 case MC_MESSAGE_STACK_REGION:
227 s_mc_message_stack_region_t message;
228 if (size != sizeof(message))
229 xbt_die("Broken messsage");
230 memcpy(&message, buffer, sizeof(message));
231 this->process().stack_areas().push_back(message.stack_region);
235 case MC_MESSAGE_REGISTER_SYMBOL:
237 s_mc_message_register_symbol_t message;
238 if (size != sizeof(message))
239 xbt_die("Broken message");
240 memcpy(&message, buffer, sizeof(message));
241 if (message.callback)
242 xbt_die("Support for client-side function proposition is not implemented.");
243 XBT_DEBUG("Received symbol: %s", message.name);
245 if (simgrid::mc::property_automaton == nullptr)
246 simgrid::mc::property_automaton = xbt_automaton_new();
248 simgrid::mc::RemoteClient* process = &this->process();
249 simgrid::mc::RemotePtr<int> address = simgrid::mc::remote((int*)message.data);
250 simgrid::xbt::add_proposition(simgrid::mc::property_automaton, message.name,
251 [process, address]() { return process->read(address); });
256 case MC_MESSAGE_WAITING:
259 case MC_MESSAGE_ASSERTION_FAILED:
260 MC_report_assertion_error();
261 this->exit(SIMGRID_MC_EXIT_SAFETY);
264 xbt_die("Unexpected message from model-checked application");
270 /** Terminate the model-checker application */
271 void ModelChecker::exit(int status)
273 // TODO, terminate the model checker politely instead of exiting rudely
274 if (process().running())
275 kill(process().pid(), SIGKILL);
279 void ModelChecker::handle_events(int fd, short events)
281 if (events == EV_READ) {
282 char buffer[MC_MESSAGE_LENGTH];
283 ssize_t size = process_->getChannel().receive(buffer, sizeof(buffer), false);
284 if (size == -1 && errno != EAGAIN)
285 throw simgrid::xbt::errno_error();
286 if (not handle_message(buffer, size)) {
287 event_base_loopbreak(base_);
290 else if (events == EV_SIGNAL) {
294 xbt_die("Unexpected event");
298 void ModelChecker::loop()
300 if (this->process().running())
301 event_base_dispatch(base_);
304 void ModelChecker::handle_waitpid()
306 XBT_DEBUG("Check for wait event");
309 while ((pid = waitpid(-1, &status, WNOHANG)) != 0) {
311 if (errno == ECHILD) {
313 if (this->process().running())
314 xbt_die("Inconsistent state");
318 XBT_ERROR("Could not wait for pid");
319 throw simgrid::xbt::errno_error();
323 if (pid == this->process().pid()) {
325 // From PTRACE_O_TRACEEXIT:
327 if (status>>8 == (SIGTRAP | (PTRACE_EVENT_EXIT<<8))) {
328 if (ptrace(PTRACE_GETEVENTMSG, this->process().pid(), 0, &status) == -1)
329 xbt_die("Could not get exit status");
330 if (WIFSIGNALED(status)) {
331 MC_report_crash(status);
332 mc_model_checker->exit(SIMGRID_MC_EXIT_PROGRAM_CRASH);
337 // We don't care about signals, just reinject them:
338 if (WIFSTOPPED(status)) {
339 XBT_DEBUG("Stopped with signal %i", (int) WSTOPSIG(status));
342 ptrace(PTRACE_CONT, this->process().pid(), 0, WSTOPSIG(status));
344 ptrace(PT_CONTINUE, this->process().pid(), (caddr_t)1, WSTOPSIG(status));
347 xbt_die("Could not PTRACE_CONT");
350 else if (WIFEXITED(status) || WIFSIGNALED(status)) {
351 XBT_DEBUG("Child process is over");
352 this->process().terminate();
358 void ModelChecker::on_signal(int signo)
360 if (signo == SIGCHLD)
361 this->handle_waitpid();
364 void ModelChecker::wait_for_requests()
366 this->resume(process());
367 if (this->process().running())
368 event_base_dispatch(base_);
371 void ModelChecker::handle_simcall(Transition const& transition)
373 s_mc_message_simcall_handle_t m;
374 memset(&m, 0, sizeof(m));
375 m.type = MC_MESSAGE_SIMCALL_HANDLE;
376 m.pid = transition.pid;
377 m.value = transition.argument;
378 this->process_->getChannel().send(m);
379 this->process_->clear_cache();
380 if (this->process_->running())
381 event_base_dispatch(base_);
384 bool ModelChecker::checkDeadlock()
387 if ((res = this->process().getChannel().send(MC_MESSAGE_DEADLOCK_CHECK)))
388 xbt_die("Could not check deadlock state");
389 s_mc_message_int_t message;
390 ssize_t s = mc_model_checker->process().getChannel().receive(message);
392 xbt_die("Could not receive message");
393 if (s != sizeof(message) || message.type != MC_MESSAGE_DEADLOCK_CHECK_REPLY)
394 xbt_die("Received unexpected message %s (%i, size=%i) "
395 "expected MC_MESSAGE_DEADLOCK_CHECK_REPLY (%i, size=%i)",
396 MC_message_type_name(message.type), (int) message.type, (int) s,
397 (int) MC_MESSAGE_DEADLOCK_CHECK_REPLY, (int) sizeof(message)
399 return message.value != 0;