1 /* Copyright (c) 2007-2023. 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/remote/CheckerSide.hpp"
7 #include "src/mc/explo/Exploration.hpp"
8 #include "src/mc/explo/LivenessChecker.hpp"
9 #include "src/mc/sosp/RemoteProcessMemory.hpp"
10 #include "xbt/system_error.hpp"
13 #include <sys/personality.h>
14 #include <sys/prctl.h>
17 #include <boost/tokenizer.hpp>
20 #include <sys/ptrace.h>
24 #define WAITPID_CHECKED_FLAGS __WALL
26 #define WAITPID_CHECKED_FLAGS 0
29 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_checkerside, mc, "MC communication with the application");
31 static simgrid::config::Flag<std::string> _sg_mc_setenv{
32 "model-check/setenv", "Extra environment variables to pass to the child process (ex: 'AZE=aze;QWE=qwe').", "",
33 [](std::string_view value) {
34 xbt_assert(value.empty() || value.find('=', 0) != std::string_view::npos,
35 "The 'model-check/setenv' parameter must be like 'AZE=aze', but it does not contain an equal sign.");
38 namespace simgrid::mc {
40 XBT_ATTRIB_NORETURN static void run_child_process(int socket, const std::vector<char*>& args)
42 /* On startup, simix_global_init() calls simgrid::mc::Client::initialize(), which checks whether the MC_ENV_SOCKET_FD
43 * env variable is set. If so, MC mode is assumed, and the client is setup from its side
47 // Make sure we do not outlive our parent
50 xbt_assert(sigprocmask(SIG_SETMASK, &mask, nullptr) >= 0, "Could not unblock signals");
51 xbt_assert(prctl(PR_SET_PDEATHSIG, SIGHUP) == 0, "Could not PR_SET_PDEATHSIG");
53 // Make sure that the application process layout is not randomized, so that the info we gather is stable over re-execs
54 if (personality(ADDR_NO_RANDOMIZE) == -1) {
55 XBT_ERROR("Could not set the NO_RANDOMIZE personality");
56 throw xbt::errno_error();
60 // Remove CLOEXEC to pass the socket to the application
61 int fdflags = fcntl(socket, F_GETFD, 0);
62 xbt_assert(fdflags != -1 && fcntl(socket, F_SETFD, fdflags & ~FD_CLOEXEC) != -1,
63 "Could not remove CLOEXEC for socket");
65 setenv(MC_ENV_SOCKET_FD, std::to_string(socket).c_str(), 1);
67 /* Setup the tokenizer that parses the cfg:model-check/setenv parameter */
68 using Tokenizer = boost::tokenizer<boost::char_separator<char>>;
69 boost::char_separator<char> semicol_sep(";");
70 boost::char_separator<char> equal_sep("=");
71 Tokenizer token_vars(_sg_mc_setenv.get(), semicol_sep); /* Iterate over all FOO=foo parts */
72 for (const auto& token : token_vars) {
73 std::vector<std::string> kv;
74 Tokenizer token_kv(token, equal_sep);
75 for (const auto& t : token_kv) /* Iterate over 'FOO' and then 'foo' in that 'FOO=foo' */
77 xbt_assert(kv.size() == 2, "Parse error on 'model-check/setenv' value %s. Does it contain an equal sign?",
79 XBT_INFO("setenv '%s'='%s'", kv[0].c_str(), kv[1].c_str());
80 setenv(kv[0].c_str(), kv[1].c_str(), 1);
83 /* And now, exec the child process */
85 while (args[i] != nullptr && args[i][0] == '-')
88 xbt_assert(args[i] != nullptr,
89 "Unable to find a binary to exec on the command line. Did you only pass config flags?");
91 execvp(args[i], args.data() + i);
92 XBT_CRITICAL("The model-checked process failed to exec(%s): %s.\n"
93 " Make sure that your binary exists on disk and is executable.",
94 args[i], strerror(errno));
95 if (strchr(args[i], '=') != nullptr)
96 XBT_CRITICAL("If you want to pass environment variables to the application, please use --cfg=model-check/setenv:%s",
99 xbt_die("Aborting now.");
102 static void wait_application_process(pid_t pid)
104 XBT_DEBUG("Waiting for the model-checked process");
107 // The model-checked process SIGSTOP itself to signal it's ready:
108 xbt_assert(waitpid(pid, &status, WAITPID_CHECKED_FLAGS) == pid && WIFSTOPPED(status) && WSTOPSIG(status) == SIGSTOP,
109 "Could not wait model-checked process");
113 ptrace(PTRACE_SETOPTIONS, pid, nullptr, PTRACE_O_TRACEEXIT);
114 ptrace(PTRACE_CONT, pid, 0, 0);
116 ptrace(PT_CONTINUE, pid, (caddr_t)1, 0);
118 #error "no ptrace equivalent coded for this platform"
120 xbt_assert(errno == 0,
121 "Ptrace does not seem to be usable in your setup (errno: %d). "
122 "If you run from within a docker, adding `--cap-add SYS_PTRACE` to the docker line may help. "
123 "If it does not help, please report this bug.",
127 void CheckerSide::setup_events()
129 if (base_ != nullptr)
130 event_base_free(base_.get());
131 auto* base = event_base_new();
134 socket_event_ = event_new(
135 base, get_channel().get_socket(), EV_READ | EV_PERSIST,
136 [](evutil_socket_t sig, short events, void* arg) {
137 auto checker = static_cast<simgrid::mc::CheckerSide*>(arg);
138 if (events == EV_READ) {
139 std::array<char, MC_MESSAGE_LENGTH> buffer;
140 ssize_t size = recv(checker->get_channel().get_socket(), buffer.data(), buffer.size(), MSG_DONTWAIT);
142 XBT_ERROR("Channel::receive failure: %s", strerror(errno));
144 throw simgrid::xbt::errno_error();
147 if (not checker->handle_message(buffer.data(), size))
148 checker->break_loop();
150 xbt_die("Unexpected event");
154 event_add(socket_event_, nullptr);
156 signal_event_ = event_new(
157 base, SIGCHLD, EV_SIGNAL | EV_PERSIST,
158 [](evutil_socket_t sig, short events, void* arg) {
159 auto checker = static_cast<simgrid::mc::CheckerSide*>(arg);
160 if (events == EV_SIGNAL) {
162 checker->handle_waitpid();
164 xbt_die("Unexpected signal: %d", sig);
166 xbt_die("Unexpected event");
170 event_add(signal_event_, nullptr);
173 CheckerSide::CheckerSide(const std::vector<char*>& args, bool need_memory_introspection) : running_(true)
175 // Create an AF_LOCAL socketpair used for exchanging messages between the model-checker process (ancestor)
176 // and the application process (child)
178 xbt_assert(socketpair(AF_LOCAL, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sockets) != -1, "Could not create socketpair");
181 xbt_assert(pid_ >= 0, "Could not fork model-checked process");
183 if (pid_ == 0) { // Child
185 run_child_process(sockets[0], args);
189 // Parent (model-checker):
191 channel_.reset_socket(sockets[1]);
194 wait_application_process(pid_);
198 // Request the initial memory on need
199 if (need_memory_introspection) {
200 channel_.send(MessageType::INITIAL_ADDRESSES);
201 s_mc_message_initial_addresses_reply_t answer;
202 ssize_t answer_size = channel_.receive(answer);
203 xbt_assert(answer_size != -1, "Could not receive message");
204 xbt_assert(answer.type == MessageType::INITIAL_ADDRESSES_REPLY,
205 "The received message is not the INITIAL_ADDRESS_REPLY I was expecting but of type %s",
206 to_c_str(answer.type));
207 xbt_assert(answer_size == sizeof answer, "Broken message (size=%zd; expected %zu)", answer_size, sizeof answer);
209 /* We now have enough info to create the memory address space */
210 remote_memory_ = std::make_unique<simgrid::mc::RemoteProcessMemory>(pid_, answer.mmalloc_default_mdp);
214 CheckerSide::~CheckerSide()
216 event_del(socket_event_);
217 event_free(socket_event_);
218 event_del(signal_event_);
219 event_free(signal_event_);
222 void CheckerSide::dispatch_events() const
224 event_base_dispatch(base_.get());
227 void CheckerSide::break_loop() const
229 event_base_loopbreak(base_.get());
232 bool CheckerSide::handle_message(const char* buffer, ssize_t size)
234 s_mc_message_t base_message;
235 xbt_assert(size >= (ssize_t)sizeof(base_message), "Broken message");
236 memcpy(&base_message, buffer, sizeof(base_message));
238 switch (base_message.type) {
239 case MessageType::IGNORE_HEAP: {
240 if (remote_memory_ != nullptr) {
241 s_mc_message_ignore_heap_t message;
242 xbt_assert(size == sizeof(message), "Broken message");
243 memcpy(&message, buffer, sizeof(message));
245 IgnoredHeapRegion region;
246 region.block = message.block;
247 region.fragment = message.fragment;
248 region.address = message.address;
249 region.size = message.size;
250 get_remote_memory()->ignore_heap(region);
252 XBT_INFO("Ignoring a IGNORE_HEAP message because we don't need to introspect memory.");
257 case MessageType::UNIGNORE_HEAP: {
258 if (remote_memory_ != nullptr) {
259 s_mc_message_ignore_memory_t message;
260 xbt_assert(size == sizeof(message), "Broken message");
261 memcpy(&message, buffer, sizeof(message));
262 get_remote_memory()->unignore_heap((void*)(std::uintptr_t)message.addr, message.size);
264 XBT_INFO("Ignoring an UNIGNORE_HEAP message because we don't need to introspect memory.");
269 case MessageType::IGNORE_MEMORY: {
270 if (remote_memory_ != nullptr) {
271 s_mc_message_ignore_memory_t message;
272 xbt_assert(size == sizeof(message), "Broken message");
273 memcpy(&message, buffer, sizeof(message));
274 get_remote_memory()->ignore_region(message.addr, message.size);
276 XBT_INFO("Ignoring an IGNORE_MEMORY message because we don't need to introspect memory.");
281 case MessageType::STACK_REGION: {
282 if (remote_memory_ != nullptr) {
283 s_mc_message_stack_region_t message;
284 xbt_assert(size == sizeof(message), "Broken message");
285 memcpy(&message, buffer, sizeof(message));
286 get_remote_memory()->stack_areas().push_back(message.stack_region);
288 XBT_INFO("Ignoring an STACK_REGION message because we don't need to introspect memory.");
293 case MessageType::REGISTER_SYMBOL: {
294 s_mc_message_register_symbol_t message;
295 xbt_assert(size == sizeof(message), "Broken message");
296 memcpy(&message, buffer, sizeof(message));
297 xbt_assert(not message.callback, "Support for client-side function proposition is not implemented.");
298 XBT_DEBUG("Received symbol: %s", message.name.data());
300 LivenessChecker::automaton_register_symbol(*get_remote_memory(), message.name.data(), remote((int*)message.data));
304 case MessageType::WAITING:
307 case MessageType::ASSERTION_FAILED:
308 Exploration::get_instance()->report_assertion_failure();
312 xbt_die("Unexpected message from model-checked application");
317 void CheckerSide::wait_for_requests()
319 /* Resume the application */
320 if (get_channel().send(MessageType::CONTINUE) != 0)
321 throw xbt::errno_error();
322 clear_memory_cache();
328 void CheckerSide::clear_memory_cache()
331 remote_memory_->clear_cache();
334 void CheckerSide::handle_waitpid()
336 XBT_DEBUG("Check for wait event");
339 while ((pid = waitpid(-1, &status, WNOHANG)) != 0) {
341 if (errno == ECHILD) { // No more children:
342 xbt_assert(not this->running(), "Inconsistent state");
345 XBT_ERROR("Could not wait for pid");
346 throw simgrid::xbt::errno_error();
350 if (pid == get_pid()) {
351 // From PTRACE_O_TRACEEXIT:
353 if (status >> 8 == (SIGTRAP | (PTRACE_EVENT_EXIT << 8))) {
354 unsigned long eventmsg;
355 xbt_assert(ptrace(PTRACE_GETEVENTMSG, pid, 0, &eventmsg) != -1, "Could not get exit status");
356 status = static_cast<int>(eventmsg);
357 if (WIFSIGNALED(status)) {
359 Exploration::get_instance()->report_crash(status);
364 // We don't care about non-lethal signals, just reinject them:
365 if (WIFSTOPPED(status)) {
366 XBT_DEBUG("Stopped with signal %i", (int)WSTOPSIG(status));
369 ptrace(PTRACE_CONT, pid, 0, WSTOPSIG(status));
371 ptrace(PT_CONTINUE, pid, (caddr_t)1, WSTOPSIG(status));
373 xbt_assert(errno == 0, "Could not PTRACE_CONT");
376 else if (WIFSIGNALED(status)) {
378 Exploration::get_instance()->report_crash(status);
379 } else if (WIFEXITED(status)) {
380 XBT_DEBUG("Child process is over");
387 } // namespace simgrid::mc