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");
76 ptrace(PTRACE_SETOPTIONS, pid, nullptr, PTRACE_O_TRACEEXIT);
77 ptrace(PTRACE_CONT, pid, 0, 0);
79 ptrace(PT_CONTINUE, pid, (caddr_t)1, 0);
81 # error "no ptrace equivalent coded for this platform"
83 xbt_assert(errno == 0,
84 "Ptrace does not seem to be usable in your setup (errno: %d). "
85 "If you run from within a docker, adding `--cap-add SYS_PTRACE` to the docker line may help. "
86 "If it does not help, please report this bug.",
90 static constexpr auto ignored_local_variables = {
91 std::make_pair("e", "*"),
92 std::make_pair("_log_ev", "*"),
94 /* Ignore local variable about time used for tracing */
95 std::make_pair("start_time", "*"),
98 void ModelChecker::setup_ignore()
100 const RemoteProcessMemory& process = this->get_remote_process_memory();
101 for (auto const& [var, frame] : ignored_local_variables)
102 process.ignore_local_variable(var, frame);
104 /* Static variable used for tracing */
105 process.ignore_global_variable("counter");
108 void ModelChecker::resume()
110 if (checker_side_.get_channel().send(MessageType::CONTINUE) != 0)
111 throw xbt::errno_error();
112 remote_process_memory_->clear_cache();
115 static void MC_report_crash(Exploration* explorer, int status)
117 XBT_INFO("**************************");
118 XBT_INFO("** CRASH IN THE PROGRAM **");
119 XBT_INFO("**************************");
120 if (WIFSIGNALED(status))
121 XBT_INFO("From signal: %s", strsignal(WTERMSIG(status)));
122 else if (WIFEXITED(status))
123 XBT_INFO("From exit: %i", WEXITSTATUS(status));
124 if (not xbt_log_no_loc)
125 XBT_INFO("%s core dump was generated by the system.", WCOREDUMP(status) ? "A" : "No");
127 XBT_INFO("Counter-example execution trace:");
128 for (auto const& s : explorer->get_textual_trace())
129 XBT_INFO(" %s", s.c_str());
130 XBT_INFO("You can debug the problem (and see the whole details) by rerunning out of simgrid-mc with "
131 "--cfg=model-check/replay:'%s'",
132 explorer->get_record_trace().to_string().c_str());
133 explorer->log_state();
134 if (xbt_log_no_loc) {
135 XBT_INFO("Stack trace not displayed because you passed --log=no_loc");
137 XBT_INFO("Stack trace:");
138 explorer->get_remote_app().get_remote_process_memory().dump_stack();
143 bool ModelChecker::handle_message(const char* buffer, ssize_t size)
145 s_mc_message_t base_message;
146 xbt_assert(size >= (ssize_t)sizeof(base_message), "Broken message");
147 memcpy(&base_message, buffer, sizeof(base_message));
149 switch(base_message.type) {
150 case MessageType::INITIAL_ADDRESSES: {
151 s_mc_message_initial_addresses_t message;
152 xbt_assert(size == sizeof(message), "Broken message. Got %d bytes instead of %d.", (int)size, (int)sizeof(message));
153 memcpy(&message, buffer, sizeof(message));
155 get_remote_process_memory().init(message.mmalloc_default_mdp);
159 case MessageType::IGNORE_HEAP: {
160 s_mc_message_ignore_heap_t message;
161 xbt_assert(size == sizeof(message), "Broken message");
162 memcpy(&message, buffer, sizeof(message));
164 IgnoredHeapRegion region;
165 region.block = message.block;
166 region.fragment = message.fragment;
167 region.address = message.address;
168 region.size = message.size;
169 get_remote_process_memory().ignore_heap(region);
173 case MessageType::UNIGNORE_HEAP: {
174 s_mc_message_ignore_memory_t message;
175 xbt_assert(size == sizeof(message), "Broken message");
176 memcpy(&message, buffer, sizeof(message));
177 get_remote_process_memory().unignore_heap((void*)(std::uintptr_t)message.addr, message.size);
181 case MessageType::IGNORE_MEMORY: {
182 s_mc_message_ignore_memory_t message;
183 xbt_assert(size == sizeof(message), "Broken message");
184 memcpy(&message, buffer, sizeof(message));
185 this->get_remote_process_memory().ignore_region(message.addr, message.size);
189 case MessageType::STACK_REGION: {
190 s_mc_message_stack_region_t message;
191 xbt_assert(size == sizeof(message), "Broken message");
192 memcpy(&message, buffer, sizeof(message));
193 this->get_remote_process_memory().stack_areas().push_back(message.stack_region);
196 case MessageType::REGISTER_SYMBOL: {
197 s_mc_message_register_symbol_t message;
198 xbt_assert(size == sizeof(message), "Broken message");
199 memcpy(&message, buffer, sizeof(message));
200 xbt_assert(not message.callback, "Support for client-side function proposition is not implemented.");
201 XBT_DEBUG("Received symbol: %s", message.name.data());
203 LivenessChecker::automaton_register_symbol(get_remote_process_memory(), message.name.data(),
204 remote((int*)message.data));
208 case MessageType::WAITING:
211 case MessageType::ASSERTION_FAILED:
212 XBT_INFO("**************************");
213 XBT_INFO("*** PROPERTY NOT VALID ***");
214 XBT_INFO("**************************");
215 XBT_INFO("Counter-example execution trace:");
216 for (auto const& s : get_exploration()->get_textual_trace())
217 XBT_INFO(" %s", s.c_str());
218 XBT_INFO("You can debug the problem (and see the whole details) by rerunning out of simgrid-mc with "
219 "--cfg=model-check/replay:'%s'",
220 get_exploration()->get_record_trace().to_string().c_str());
221 exploration_->log_state();
223 exploration_->system_exit(SIMGRID_MC_EXIT_SAFETY);
226 xbt_die("Unexpected message from model-checked application");
231 void ModelChecker::handle_waitpid()
233 XBT_DEBUG("Check for wait event");
236 while ((pid = waitpid(-1, &status, WNOHANG)) != 0) {
238 if (errno == ECHILD) {
240 xbt_assert(not this->get_remote_process_memory().running(), "Inconsistent state");
243 XBT_ERROR("Could not wait for pid");
244 throw simgrid::xbt::errno_error();
248 if (pid == this->get_remote_process_memory().pid()) {
249 // From PTRACE_O_TRACEEXIT:
251 if (status>>8 == (SIGTRAP | (PTRACE_EVENT_EXIT<<8))) {
252 unsigned long eventmsg;
253 xbt_assert(ptrace(PTRACE_GETEVENTMSG, get_remote_process_memory().pid(), 0, &eventmsg) != -1,
254 "Could not get exit status");
255 status = static_cast<int>(eventmsg);
256 if (WIFSIGNALED(status)) {
257 MC_report_crash(exploration_, status);
258 this->get_remote_process_memory().terminate();
259 exploration_->system_exit(SIMGRID_MC_EXIT_PROGRAM_CRASH);
264 // We don't care about non-lethal signals, just reinject them:
265 if (WIFSTOPPED(status)) {
266 XBT_DEBUG("Stopped with signal %i", (int) WSTOPSIG(status));
269 ptrace(PTRACE_CONT, get_remote_process_memory().pid(), 0, WSTOPSIG(status));
271 ptrace(PT_CONTINUE, get_remote_process_memory().pid(), (caddr_t)1, WSTOPSIG(status));
273 xbt_assert(errno == 0, "Could not PTRACE_CONT");
276 else if (WIFSIGNALED(status)) {
277 MC_report_crash(exploration_, status);
278 this->get_remote_process_memory().terminate();
279 exploration_->system_exit(SIMGRID_MC_EXIT_PROGRAM_CRASH);
280 } else if (WIFEXITED(status)) {
281 XBT_DEBUG("Child process is over");
282 this->get_remote_process_memory().terminate();
288 void ModelChecker::wait_for_requests()
291 if (this->get_remote_process_memory().running())
292 checker_side_.dispatch();
295 Transition* ModelChecker::handle_simcall(aid_t aid, int times_considered, bool new_transition)
297 s_mc_message_simcall_execute_t m = {};
298 m.type = MessageType::SIMCALL_EXECUTE;
300 m.times_considered_ = times_considered;
301 checker_side_.get_channel().send(m);
303 this->remote_process_memory_->clear_cache();
304 if (this->remote_process_memory_->running())
305 checker_side_.dispatch(); // The app may send messages while processing the transition
307 s_mc_message_simcall_execute_answer_t answer;
308 ssize_t s = checker_side_.get_channel().receive(answer);
309 xbt_assert(s != -1, "Could not receive message");
310 xbt_assert(s == sizeof answer, "Broken message (size=%zd; expected %zu)", s, sizeof answer);
311 xbt_assert(answer.type == MessageType::SIMCALL_EXECUTE_ANSWER,
312 "Received unexpected message %s (%i); expected MessageType::SIMCALL_EXECUTE_ANSWER (%i)",
313 to_c_str(answer.type), (int)answer.type, (int)MessageType::SIMCALL_EXECUTE_ANSWER);
315 if (new_transition) {
316 std::stringstream stream(answer.buffer.data());
317 return deserialize_transition(aid, times_considered, stream);
322 } // namespace simgrid::mc