1 /* Copyright (c) 2008-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/ModelChecker.hpp"
7 #include "src/mc/explo/Exploration.hpp"
8 #include "src/mc/explo/LivenessChecker.hpp"
9 #include "src/mc/mc_config.hpp"
10 #include "src/mc/mc_exit.hpp"
11 #include "src/mc/mc_private.hpp"
12 #include "src/mc/sosp/RemoteProcessMemory.hpp"
13 #include "src/mc/transition/TransitionComm.hpp"
14 #include "xbt/automaton.hpp"
15 #include "xbt/system_error.hpp"
19 #include <sys/ptrace.h>
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_ModelChecker, mc, "ModelChecker");
24 ::simgrid::mc::ModelChecker* mc_model_checker = nullptr;
27 # define WAITPID_CHECKED_FLAGS __WALL
29 # define WAITPID_CHECKED_FLAGS 0
32 namespace simgrid::mc {
34 ModelChecker::ModelChecker(std::unique_ptr<RemoteProcessMemory> remote_memory, int sockfd)
35 : checker_side_(sockfd), remote_process_memory_(std::move(remote_memory))
39 void ModelChecker::start()
42 [](evutil_socket_t sig, short events, void* arg) {
43 auto mc = static_cast<simgrid::mc::ModelChecker*>(arg);
44 if (events == EV_READ) {
45 std::array<char, MC_MESSAGE_LENGTH> buffer;
46 ssize_t size = mc->checker_side_.get_channel().receive(buffer.data(), buffer.size(), false);
47 if (size == -1 && errno != EAGAIN)
48 throw simgrid::xbt::errno_error();
50 if (not mc->handle_message(buffer.data(), size))
51 mc->checker_side_.break_loop();
52 } else if (events == EV_SIGNAL) {
56 xbt_die("Unexpected signal: %d", sig);
58 xbt_die("Unexpected event");
63 XBT_DEBUG("Waiting for the model-checked process");
66 // The model-checked process SIGSTOP itself to signal it's ready:
67 const pid_t pid = remote_process_memory_->pid();
69 xbt_assert(waitpid(pid, &status, WAITPID_CHECKED_FLAGS) == pid && WIFSTOPPED(status) && WSTOPSIG(status) == SIGSTOP,
70 "Could not wait model-checked process");
74 ptrace(PTRACE_SETOPTIONS, pid, nullptr, PTRACE_O_TRACEEXIT);
75 ptrace(PTRACE_CONT, pid, 0, 0);
77 ptrace(PT_CONTINUE, pid, (caddr_t)1, 0);
79 # error "no ptrace equivalent coded for this platform"
81 xbt_assert(errno == 0,
82 "Ptrace does not seem to be usable in your setup (errno: %d). "
83 "If you run from within a docker, adding `--cap-add SYS_PTRACE` to the docker line may help. "
84 "If it does not help, please report this bug.",
88 bool ModelChecker::handle_message(const char* buffer, ssize_t size)
90 s_mc_message_t base_message;
91 xbt_assert(size >= (ssize_t)sizeof(base_message), "Broken message");
92 memcpy(&base_message, buffer, sizeof(base_message));
94 switch(base_message.type) {
95 case MessageType::INITIAL_ADDRESSES: {
96 s_mc_message_initial_addresses_t message;
97 xbt_assert(size == sizeof(message), "Broken message. Got %d bytes instead of %d.", (int)size, (int)sizeof(message));
98 memcpy(&message, buffer, sizeof(message));
100 get_remote_process_memory().init(message.mmalloc_default_mdp);
104 case MessageType::IGNORE_HEAP: {
105 s_mc_message_ignore_heap_t message;
106 xbt_assert(size == sizeof(message), "Broken message");
107 memcpy(&message, buffer, sizeof(message));
109 IgnoredHeapRegion region;
110 region.block = message.block;
111 region.fragment = message.fragment;
112 region.address = message.address;
113 region.size = message.size;
114 get_remote_process_memory().ignore_heap(region);
118 case MessageType::UNIGNORE_HEAP: {
119 s_mc_message_ignore_memory_t message;
120 xbt_assert(size == sizeof(message), "Broken message");
121 memcpy(&message, buffer, sizeof(message));
122 get_remote_process_memory().unignore_heap((void*)(std::uintptr_t)message.addr, message.size);
126 case MessageType::IGNORE_MEMORY: {
127 s_mc_message_ignore_memory_t message;
128 xbt_assert(size == sizeof(message), "Broken message");
129 memcpy(&message, buffer, sizeof(message));
130 this->get_remote_process_memory().ignore_region(message.addr, message.size);
134 case MessageType::STACK_REGION: {
135 s_mc_message_stack_region_t message;
136 xbt_assert(size == sizeof(message), "Broken message");
137 memcpy(&message, buffer, sizeof(message));
138 this->get_remote_process_memory().stack_areas().push_back(message.stack_region);
141 case MessageType::REGISTER_SYMBOL: {
142 s_mc_message_register_symbol_t message;
143 xbt_assert(size == sizeof(message), "Broken message");
144 memcpy(&message, buffer, sizeof(message));
145 xbt_assert(not message.callback, "Support for client-side function proposition is not implemented.");
146 XBT_DEBUG("Received symbol: %s", message.name.data());
148 LivenessChecker::automaton_register_symbol(get_remote_process_memory(), message.name.data(),
149 remote((int*)message.data));
153 case MessageType::WAITING:
156 case MessageType::ASSERTION_FAILED:
157 exploration_->report_assertion_failure();
161 xbt_die("Unexpected message from model-checked application");
166 void ModelChecker::handle_waitpid()
168 XBT_DEBUG("Check for wait event");
171 while ((pid = waitpid(-1, &status, WNOHANG)) != 0) {
173 if (errno == ECHILD) {
175 xbt_assert(not this->get_remote_process_memory().running(), "Inconsistent state");
178 XBT_ERROR("Could not wait for pid");
179 throw simgrid::xbt::errno_error();
183 if (pid == this->get_remote_process_memory().pid()) {
184 // From PTRACE_O_TRACEEXIT:
186 if (status>>8 == (SIGTRAP | (PTRACE_EVENT_EXIT<<8))) {
187 unsigned long eventmsg;
188 xbt_assert(ptrace(PTRACE_GETEVENTMSG, get_remote_process_memory().pid(), 0, &eventmsg) != -1,
189 "Could not get exit status");
190 status = static_cast<int>(eventmsg);
191 if (WIFSIGNALED(status))
192 exploration_->report_crash(status);
196 // We don't care about non-lethal signals, just reinject them:
197 if (WIFSTOPPED(status)) {
198 XBT_DEBUG("Stopped with signal %i", (int) WSTOPSIG(status));
201 ptrace(PTRACE_CONT, get_remote_process_memory().pid(), 0, WSTOPSIG(status));
203 ptrace(PT_CONTINUE, get_remote_process_memory().pid(), (caddr_t)1, WSTOPSIG(status));
205 xbt_assert(errno == 0, "Could not PTRACE_CONT");
208 else if (WIFSIGNALED(status)) {
209 exploration_->report_crash(status);
210 } else if (WIFEXITED(status)) {
211 XBT_DEBUG("Child process is over");
212 this->get_remote_process_memory().terminate();
218 Transition* ModelChecker::handle_simcall(aid_t aid, int times_considered, bool new_transition)
220 s_mc_message_simcall_execute_t m = {};
221 m.type = MessageType::SIMCALL_EXECUTE;
223 m.times_considered_ = times_considered;
224 checker_side_.get_channel().send(m);
226 this->remote_process_memory_->clear_cache();
227 if (this->remote_process_memory_->running())
228 checker_side_.dispatch(); // The app may send messages while processing the transition
230 s_mc_message_simcall_execute_answer_t answer;
231 ssize_t s = checker_side_.get_channel().receive(answer);
232 xbt_assert(s != -1, "Could not receive message");
233 xbt_assert(s == sizeof answer, "Broken message (size=%zd; expected %zu)", s, sizeof answer);
234 xbt_assert(answer.type == MessageType::SIMCALL_EXECUTE_ANSWER,
235 "Received unexpected message %s (%i); expected MessageType::SIMCALL_EXECUTE_ANSWER (%i)",
236 to_c_str(answer.type), (int)answer.type, (int)MessageType::SIMCALL_EXECUTE_ANSWER);
238 if (new_transition) {
239 std::stringstream stream(answer.buffer.data());
240 return deserialize_transition(aid, times_considered, stream);
245 } // namespace simgrid::mc