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. */
10 #include <sys/socket.h>
11 #include <sys/ptrace.h>
14 #include <system_error>
16 #include "xbt/automaton.h"
17 #include "xbt/automaton.hpp"
19 #include "xbt/system_error.hpp"
21 #include "simgrid/sg_config.hpp"
23 #include "src/mc/ModelChecker.hpp"
24 #include "src/mc/Transition.hpp"
25 #include "src/mc/checker/Checker.hpp"
26 #include "src/mc/mc_exit.hpp"
27 #include "src/mc/mc_private.hpp"
28 #include "src/mc/mc_record.hpp"
29 #include "src/mc/remote/RemoteClient.hpp"
30 #include "src/mc/remote/mc_protocol.h"
31 #include "src/mc/sosp/PageStore.hpp"
33 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_ModelChecker, mc, "ModelChecker");
35 ::simgrid::mc::ModelChecker* mc_model_checker = nullptr;
37 using simgrid::mc::remote;
40 # define WAITPID_CHECKED_FLAGS __WALL
42 # define WAITPID_CHECKED_FLAGS 0
48 ModelChecker::ModelChecker(std::unique_ptr<RemoteClient> process)
50 , socket_event_(nullptr)
51 , signal_event_(nullptr)
53 , process_(std::move(process))
54 , parent_snapshot_(nullptr)
59 ModelChecker::~ModelChecker() {
60 if (socket_event_ != nullptr)
61 event_free(socket_event_);
62 if (signal_event_ != nullptr)
63 event_free(signal_event_);
65 event_base_free(base_);
68 void ModelChecker::start()
70 const pid_t pid = process_->pid();
72 base_ = event_base_new();
73 event_callback_fn event_callback = [](evutil_socket_t fd, short events, void *arg)
75 ((ModelChecker *)arg)->handle_events(fd, events);
77 socket_event_ = event_new(base_,
78 process_->getChannel().getSocket(),
80 event_callback, this);
81 event_add(socket_event_, NULL);
82 signal_event_ = event_new(base_,
85 event_callback, this);
86 event_add(signal_event_, NULL);
88 XBT_DEBUG("Waiting for the model-checked process");
91 // The model-checked process SIGSTOP itself to signal it's ready:
92 pid_t res = waitpid(pid, &status, WAITPID_CHECKED_FLAGS);
93 if (res < 0 || not WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP)
94 xbt_die("Could not wait model-checked process");
98 if (not _sg_mc_dot_output_file.get().empty())
104 ptrace(PTRACE_SETOPTIONS, pid, nullptr, PTRACE_O_TRACEEXIT);
105 ptrace(PTRACE_CONT, pid, 0, 0);
107 ptrace(PT_CONTINUE, pid, (caddr_t)1, 0);
109 # error "no ptrace equivalent coded for this platform"
113 static const std::pair<const char*, const char*> ignored_local_variables[] = {
114 std::pair<const char*, const char*>{ "e", "*" },
115 std::pair<const char*, const char*>{ "_log_ev", "*" },
117 /* Ignore local variable about time used for tracing */
118 std::pair<const char*, const char*>{ "start_time", "*" },
121 void ModelChecker::setup_ignore()
123 RemoteClient& process = this->process();
124 for (std::pair<const char*, const char*> const& var :
125 ignored_local_variables)
126 process.ignore_local_variable(var.first, var.second);
128 /* Static variable used for tracing */
129 process.ignore_global_variable("counter");
132 void ModelChecker::shutdown()
134 XBT_DEBUG("Shuting down model-checker");
136 simgrid::mc::RemoteClient* process = &this->process();
137 if (process->running()) {
138 XBT_DEBUG("Killing process");
139 kill(process->pid(), SIGKILL);
140 process->terminate();
144 void ModelChecker::resume(simgrid::mc::RemoteClient& process)
146 int res = process.getChannel().send(MC_MESSAGE_CONTINUE);
148 throw simgrid::xbt::errno_error();
149 process.clear_cache();
152 static void MC_report_crash(int status)
154 XBT_INFO("**************************");
155 XBT_INFO("** CRASH IN THE PROGRAM **");
156 XBT_INFO("**************************");
157 if (WIFSIGNALED(status))
158 XBT_INFO("From signal: %s", strsignal(WTERMSIG(status)));
159 else if (WIFEXITED(status))
160 XBT_INFO("From exit: %i", WEXITSTATUS(status));
161 if (WCOREDUMP(status))
162 XBT_INFO("A core dump was generated by the system.");
164 XBT_INFO("No core dump was generated by the system.");
165 XBT_INFO("Counter-example execution trace:");
166 for (auto const& s : mc_model_checker->getChecker()->getTextualTrace())
167 XBT_INFO(" %s", s.c_str());
168 simgrid::mc::dumpRecordPath();
169 simgrid::mc::session->logState();
170 XBT_INFO("Stack trace:");
171 mc_model_checker->process().dumpStack();
174 static void MC_report_assertion_error()
176 XBT_INFO("**************************");
177 XBT_INFO("*** PROPERTY NOT VALID ***");
178 XBT_INFO("**************************");
179 XBT_INFO("Counter-example execution trace:");
180 for (auto const& s : mc_model_checker->getChecker()->getTextualTrace())
181 XBT_INFO(" %s", s.c_str());
182 simgrid::mc::dumpRecordPath();
183 simgrid::mc::session->logState();
186 bool ModelChecker::handle_message(char* buffer, ssize_t size)
188 s_mc_message_t base_message;
189 if (size < (ssize_t) sizeof(base_message))
190 xbt_die("Broken message");
191 memcpy(&base_message, buffer, sizeof(base_message));
193 switch(base_message.type) {
195 case MC_MESSAGE_IGNORE_HEAP:
197 s_mc_message_ignore_heap_t message;
198 if (size != sizeof(message))
199 xbt_die("Broken messsage");
200 memcpy(&message, buffer, sizeof(message));
202 IgnoredHeapRegion region;
203 region.block = message.block;
204 region.fragment = message.fragment;
205 region.address = message.address;
206 region.size = message.size;
207 process().ignore_heap(region);
211 case MC_MESSAGE_UNIGNORE_HEAP:
213 s_mc_message_ignore_memory_t message;
214 if (size != sizeof(message))
215 xbt_die("Broken messsage");
216 memcpy(&message, buffer, sizeof(message));
217 process().unignore_heap((void*)(std::uintptr_t)message.addr, message.size);
221 case MC_MESSAGE_IGNORE_MEMORY:
223 s_mc_message_ignore_memory_t message;
224 if (size != sizeof(message))
225 xbt_die("Broken messsage");
226 memcpy(&message, buffer, sizeof(message));
227 this->process().ignore_region(message.addr, message.size);
231 case MC_MESSAGE_STACK_REGION:
233 s_mc_message_stack_region_t message;
234 if (size != sizeof(message))
235 xbt_die("Broken messsage");
236 memcpy(&message, buffer, sizeof(message));
237 this->process().stack_areas().push_back(message.stack_region);
241 case MC_MESSAGE_REGISTER_SYMBOL:
243 s_mc_message_register_symbol_t message;
244 if (size != sizeof(message))
245 xbt_die("Broken message");
246 memcpy(&message, buffer, sizeof(message));
247 if (message.callback)
248 xbt_die("Support for client-side function proposition is not implemented.");
249 XBT_DEBUG("Received symbol: %s", message.name);
251 if (simgrid::mc::property_automaton == nullptr)
252 simgrid::mc::property_automaton = xbt_automaton_new();
254 simgrid::mc::RemoteClient* process = &this->process();
255 simgrid::mc::RemotePtr<int> address = simgrid::mc::remote((int*)message.data);
256 simgrid::xbt::add_proposition(simgrid::mc::property_automaton, message.name,
257 [process, address]() { return process->read(address); });
262 case MC_MESSAGE_WAITING:
265 case MC_MESSAGE_ASSERTION_FAILED:
266 MC_report_assertion_error();
267 this->exit(SIMGRID_MC_EXIT_SAFETY);
270 xbt_die("Unexpected message from model-checked application");
276 /** Terminate the model-checker application */
277 void ModelChecker::exit(int status)
279 // TODO, terminate the model checker politely instead of exiting rudely
280 if (process().running())
281 kill(process().pid(), SIGKILL);
285 void ModelChecker::handle_events(int fd, short events)
287 if (events == EV_READ) {
288 char buffer[MC_MESSAGE_LENGTH];
289 ssize_t size = process_->getChannel().receive(buffer, sizeof(buffer), false);
290 if (size == -1 && errno != EAGAIN)
291 throw simgrid::xbt::errno_error();
292 if (not handle_message(buffer, size)) {
293 event_base_loopbreak(base_);
296 else if (events == EV_SIGNAL) {
300 xbt_die("Unexpected event");
304 void ModelChecker::loop()
306 if (this->process().running())
307 event_base_dispatch(base_);
310 void ModelChecker::handle_waitpid()
312 XBT_DEBUG("Check for wait event");
315 while ((pid = waitpid(-1, &status, WNOHANG)) != 0) {
317 if (errno == ECHILD) {
319 if (this->process().running())
320 xbt_die("Inconsistent state");
324 XBT_ERROR("Could not wait for pid");
325 throw simgrid::xbt::errno_error();
329 if (pid == this->process().pid()) {
331 // From PTRACE_O_TRACEEXIT:
333 if (status>>8 == (SIGTRAP | (PTRACE_EVENT_EXIT<<8))) {
334 if (ptrace(PTRACE_GETEVENTMSG, this->process().pid(), 0, &status) == -1)
335 xbt_die("Could not get exit status");
336 if (WIFSIGNALED(status)) {
337 MC_report_crash(status);
338 mc_model_checker->exit(SIMGRID_MC_EXIT_PROGRAM_CRASH);
343 // We don't care about signals, just reinject them:
344 if (WIFSTOPPED(status)) {
345 XBT_DEBUG("Stopped with signal %i", (int) WSTOPSIG(status));
348 ptrace(PTRACE_CONT, this->process().pid(), 0, WSTOPSIG(status));
350 ptrace(PT_CONTINUE, this->process().pid(), (caddr_t)1, WSTOPSIG(status));
353 xbt_die("Could not PTRACE_CONT");
356 else if (WIFEXITED(status) || WIFSIGNALED(status)) {
357 XBT_DEBUG("Child process is over");
358 this->process().terminate();
364 void ModelChecker::on_signal(int signo)
366 if (signo == SIGCHLD)
367 this->handle_waitpid();
370 void ModelChecker::wait_for_requests()
372 this->resume(process());
373 if (this->process().running())
374 event_base_dispatch(base_);
377 void ModelChecker::handle_simcall(Transition const& transition)
379 s_mc_message_simcall_handle_t m;
380 memset(&m, 0, sizeof(m));
381 m.type = MC_MESSAGE_SIMCALL_HANDLE;
382 m.pid = transition.pid;
383 m.value = transition.argument;
384 this->process_->getChannel().send(m);
385 this->process_->clear_cache();
386 if (this->process_->running())
387 event_base_dispatch(base_);
390 bool ModelChecker::checkDeadlock()
393 if ((res = this->process().getChannel().send(MC_MESSAGE_DEADLOCK_CHECK)))
394 xbt_die("Could not check deadlock state");
395 s_mc_message_int_t message;
396 ssize_t s = mc_model_checker->process().getChannel().receive(message);
398 xbt_die("Could not receive message");
399 if (s != sizeof(message) || message.type != MC_MESSAGE_DEADLOCK_CHECK_REPLY)
400 xbt_die("Received unexpected message %s (%i, size=%i) "
401 "expected MC_MESSAGE_DEADLOCK_CHECK_REPLY (%i, size=%i)",
402 MC_message_type_name(message.type), (int) message.type, (int) s,
403 (int) MC_MESSAGE_DEADLOCK_CHECK_REPLY, (int) sizeof(message)
405 return message.value != 0;