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. */
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)
37 , socket_event_(nullptr)
38 , signal_event_(nullptr)
40 , process_(std::move(process))
45 ModelChecker::~ModelChecker() {
46 if (socket_event_ != nullptr)
47 event_free(socket_event_);
48 if (signal_event_ != nullptr)
49 event_free(signal_event_);
51 event_base_free(base_);
54 void ModelChecker::start()
56 base_ = event_base_new();
57 event_callback_fn event_callback = [](evutil_socket_t fd, short events, void *arg)
59 ((ModelChecker *)arg)->handle_events(fd, events);
61 socket_event_ = event_new(base_, process_->get_channel().get_socket(), EV_READ | EV_PERSIST, event_callback, this);
62 event_add(socket_event_, NULL);
63 signal_event_ = event_new(base_,
66 event_callback, this);
67 event_add(signal_event_, NULL);
69 XBT_DEBUG("Waiting for the model-checked process");
72 // The model-checked process SIGSTOP itself to signal it's ready:
73 const pid_t pid = process_->pid();
75 pid_t res = waitpid(pid, &status, WAITPID_CHECKED_FLAGS);
76 if (res < 0 || not WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP)
77 xbt_die("Could not wait model-checked process");
81 if (not _sg_mc_dot_output_file.get().empty())
87 ptrace(PTRACE_SETOPTIONS, pid, nullptr, PTRACE_O_TRACEEXIT);
88 ptrace(PTRACE_CONT, pid, 0, 0);
90 ptrace(PT_CONTINUE, pid, (caddr_t)1, 0);
92 # error "no ptrace equivalent coded for this platform"
96 static const std::pair<const char*, const char*> ignored_local_variables[] = {
97 std::pair<const char*, const char*>{ "e", "*" },
98 std::pair<const char*, const char*>{ "_log_ev", "*" },
100 /* Ignore local variable about time used for tracing */
101 std::pair<const char*, const char*>{ "start_time", "*" },
104 void ModelChecker::setup_ignore()
106 RemoteClient& process = this->process();
107 for (std::pair<const char*, const char*> const& var :
108 ignored_local_variables)
109 process.ignore_local_variable(var.first, var.second);
111 /* Static variable used for tracing */
112 process.ignore_global_variable("counter");
115 void ModelChecker::shutdown()
117 XBT_DEBUG("Shuting down model-checker");
119 simgrid::mc::RemoteClient* process = &this->process();
120 if (process->running()) {
121 XBT_DEBUG("Killing process");
122 kill(process->pid(), SIGKILL);
123 process->terminate();
127 void ModelChecker::resume(simgrid::mc::RemoteClient& process)
129 int res = process.get_channel().send(MC_MESSAGE_CONTINUE);
131 throw simgrid::xbt::errno_error();
132 process.clear_cache();
135 static void MC_report_crash(int status)
137 XBT_INFO("**************************");
138 XBT_INFO("** CRASH IN THE PROGRAM **");
139 XBT_INFO("**************************");
140 if (WIFSIGNALED(status))
141 XBT_INFO("From signal: %s", strsignal(WTERMSIG(status)));
142 else if (WIFEXITED(status))
143 XBT_INFO("From exit: %i", WEXITSTATUS(status));
144 if (not xbt_log_no_loc)
145 XBT_INFO("%s core dump was generated by the system.", WCOREDUMP(status) ? "A" : "No");
146 XBT_INFO("Counter-example execution trace:");
147 for (auto const& s : mc_model_checker->getChecker()->get_textual_trace())
148 XBT_INFO(" %s", s.c_str());
149 simgrid::mc::dumpRecordPath();
150 simgrid::mc::session->log_state();
151 if (xbt_log_no_loc) {
152 XBT_INFO("Stack trace not displayed because you passed --log=no_loc");
154 XBT_INFO("Stack trace:");
155 mc_model_checker->process().dump_stack();
159 static void MC_report_assertion_error()
161 XBT_INFO("**************************");
162 XBT_INFO("*** PROPERTY NOT VALID ***");
163 XBT_INFO("**************************");
164 XBT_INFO("Counter-example execution trace:");
165 for (auto const& s : mc_model_checker->getChecker()->get_textual_trace())
166 XBT_INFO(" %s", s.c_str());
167 simgrid::mc::dumpRecordPath();
168 simgrid::mc::session->log_state();
171 bool ModelChecker::handle_message(char* buffer, ssize_t size)
173 s_mc_message_t base_message;
174 xbt_assert(size >= (ssize_t)sizeof(base_message), "Broken message");
175 memcpy(&base_message, buffer, sizeof(base_message));
177 switch(base_message.type) {
179 case MC_MESSAGE_IGNORE_HEAP:
181 s_mc_message_ignore_heap_t message;
182 xbt_assert(size == sizeof(message), "Broken messsage");
183 memcpy(&message, buffer, sizeof(message));
185 IgnoredHeapRegion region;
186 region.block = message.block;
187 region.fragment = message.fragment;
188 region.address = message.address;
189 region.size = message.size;
190 process().ignore_heap(region);
194 case MC_MESSAGE_UNIGNORE_HEAP:
196 s_mc_message_ignore_memory_t message;
197 xbt_assert(size == sizeof(message), "Broken messsage");
198 memcpy(&message, buffer, sizeof(message));
199 process().unignore_heap((void*)(std::uintptr_t)message.addr, message.size);
203 case MC_MESSAGE_IGNORE_MEMORY:
205 s_mc_message_ignore_memory_t message;
206 xbt_assert(size == sizeof(message), "Broken messsage");
207 memcpy(&message, buffer, sizeof(message));
208 this->process().ignore_region(message.addr, message.size);
212 case MC_MESSAGE_STACK_REGION:
214 s_mc_message_stack_region_t message;
215 xbt_assert(size == sizeof(message), "Broken messsage");
216 memcpy(&message, buffer, sizeof(message));
217 this->process().stack_areas().push_back(message.stack_region);
221 case MC_MESSAGE_REGISTER_SYMBOL:
223 s_mc_message_register_symbol_t message;
224 xbt_assert(size == sizeof(message), "Broken message");
225 memcpy(&message, buffer, sizeof(message));
226 xbt_assert(not message.callback, "Support for client-side function proposition is not implemented.");
227 XBT_DEBUG("Received symbol: %s", message.name);
229 if (simgrid::mc::property_automaton == nullptr)
230 simgrid::mc::property_automaton = xbt_automaton_new();
232 simgrid::mc::RemoteClient* process = &this->process();
233 simgrid::mc::RemotePtr<int> address = simgrid::mc::remote((int*)message.data);
234 simgrid::xbt::add_proposition(simgrid::mc::property_automaton, message.name,
235 [process, address]() { return process->read(address); });
240 case MC_MESSAGE_WAITING:
243 case MC_MESSAGE_ASSERTION_FAILED:
244 MC_report_assertion_error();
245 this->exit(SIMGRID_MC_EXIT_SAFETY);
248 xbt_die("Unexpected message from model-checked application");
254 /** Terminate the model-checker application */
255 void ModelChecker::exit(int status)
257 // TODO, terminate the model checker politely instead of exiting rudely
258 if (process().running())
259 kill(process().pid(), SIGKILL);
263 void ModelChecker::handle_events(int fd, short events)
265 if (events == EV_READ) {
266 char buffer[MC_MESSAGE_LENGTH];
267 ssize_t size = process_->get_channel().receive(buffer, sizeof(buffer), false);
268 if (size == -1 && errno != EAGAIN)
269 throw simgrid::xbt::errno_error();
270 if (not handle_message(buffer, size)) {
271 event_base_loopbreak(base_);
274 else if (events == EV_SIGNAL) {
278 xbt_die("Unexpected event");
282 void ModelChecker::loop()
284 if (this->process().running())
285 event_base_dispatch(base_);
288 void ModelChecker::handle_waitpid()
290 XBT_DEBUG("Check for wait event");
293 while ((pid = waitpid(-1, &status, WNOHANG)) != 0) {
295 if (errno == ECHILD) {
297 xbt_assert(not this->process().running(), "Inconsistent state");
300 XBT_ERROR("Could not wait for pid");
301 throw simgrid::xbt::errno_error();
305 if (pid == this->process().pid()) {
307 // From PTRACE_O_TRACEEXIT:
309 if (status>>8 == (SIGTRAP | (PTRACE_EVENT_EXIT<<8))) {
310 xbt_assert(ptrace(PTRACE_GETEVENTMSG, this->process().pid(), 0, &status) != -1, "Could not get exit status");
311 if (WIFSIGNALED(status)) {
312 MC_report_crash(status);
313 mc_model_checker->exit(SIMGRID_MC_EXIT_PROGRAM_CRASH);
318 // We don't care about signals, just reinject them:
319 if (WIFSTOPPED(status)) {
320 XBT_DEBUG("Stopped with signal %i", (int) WSTOPSIG(status));
323 ptrace(PTRACE_CONT, this->process().pid(), 0, WSTOPSIG(status));
325 ptrace(PT_CONTINUE, this->process().pid(), (caddr_t)1, WSTOPSIG(status));
327 xbt_assert(errno == 0, "Could not PTRACE_CONT");
330 else if (WIFSIGNALED(status)) {
331 MC_report_crash(status);
332 mc_model_checker->exit(SIMGRID_MC_EXIT_PROGRAM_CRASH);
333 } else if (WIFEXITED(status)) {
334 XBT_DEBUG("Child process is over");
335 this->process().terminate();
341 void ModelChecker::on_signal(int signo)
343 if (signo == SIGCHLD)
344 this->handle_waitpid();
347 void ModelChecker::wait_for_requests()
349 this->resume(process());
350 if (this->process().running())
351 event_base_dispatch(base_);
354 void ModelChecker::handle_simcall(Transition const& transition)
356 s_mc_message_simcall_handle_t m;
357 memset(&m, 0, sizeof(m));
358 m.type = MC_MESSAGE_SIMCALL_HANDLE;
359 m.pid = transition.pid_;
360 m.value = transition.argument_;
361 this->process_->get_channel().send(m);
362 this->process_->clear_cache();
363 if (this->process_->running())
364 event_base_dispatch(base_);
367 bool ModelChecker::checkDeadlock()
369 int res = this->process().get_channel().send(MC_MESSAGE_DEADLOCK_CHECK);
370 xbt_assert(res == 0, "Could not check deadlock state");
371 s_mc_message_int_t message;
372 ssize_t s = mc_model_checker->process().get_channel().receive(message);
373 xbt_assert(s != -1, "Could not receive message");
374 xbt_assert(s == sizeof(message) && message.type == MC_MESSAGE_DEADLOCK_CHECK_REPLY,
375 "Received unexpected message %s (%i, size=%i) "
376 "expected MC_MESSAGE_DEADLOCK_CHECK_REPLY (%i, size=%i)",
377 MC_message_type_name(message.type), (int)message.type, (int)s, (int)MC_MESSAGE_DEADLOCK_CHECK_REPLY,
378 (int)sizeof(message));
379 return message.value != 0;