1 /* Copyright (c) 2008-2022. 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/mc_config.hpp"
9 #include "src/mc/mc_exit.hpp"
10 #include "src/mc/mc_private.hpp"
11 #include "src/mc/remote/RemoteProcess.hpp"
12 #include "src/mc/transition/TransitionComm.hpp"
13 #include "xbt/automaton.hpp"
14 #include "xbt/system_error.hpp"
18 #include <sys/ptrace.h>
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_ModelChecker, mc, "ModelChecker");
23 ::simgrid::mc::ModelChecker* mc_model_checker = nullptr;
25 using simgrid::mc::remote;
28 # define WAITPID_CHECKED_FLAGS __WALL
30 # define WAITPID_CHECKED_FLAGS 0
36 ModelChecker::ModelChecker(std::unique_ptr<RemoteProcess> remote_simulation, int sockfd)
37 : checker_side_(sockfd), remote_process_(std::move(remote_simulation))
41 void ModelChecker::start()
44 [](evutil_socket_t sig, short events, void* arg) {
45 auto mc = static_cast<simgrid::mc::ModelChecker*>(arg);
46 if (events == EV_READ) {
47 std::array<char, MC_MESSAGE_LENGTH> buffer;
48 ssize_t size = mc->checker_side_.get_channel().receive(buffer.data(), buffer.size(), false);
49 if (size == -1 && errno != EAGAIN)
50 throw simgrid::xbt::errno_error();
52 if (not mc->handle_message(buffer.data(), size))
53 mc->checker_side_.break_loop();
54 } else if (events == EV_SIGNAL) {
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_->pid();
69 xbt_assert(waitpid(pid, &status, WAITPID_CHECKED_FLAGS) == pid && WIFSTOPPED(status) && WSTOPSIG(status) == SIGSTOP,
70 "Could not wait model-checked process");
72 if (not _sg_mc_dot_output_file.get().empty())
79 ptrace(PTRACE_SETOPTIONS, pid, nullptr, PTRACE_O_TRACEEXIT);
80 ptrace(PTRACE_CONT, pid, 0, 0);
82 ptrace(PT_CONTINUE, pid, (caddr_t)1, 0);
84 # error "no ptrace equivalent coded for this platform"
86 xbt_assert(errno == 0,
87 "Ptrace does not seem to be usable in your setup (errno: %d). "
88 "If you run from within a docker, adding `--cap-add SYS_PTRACE` to the docker line may help. "
89 "If it does not help, please report this bug.",
93 static constexpr auto ignored_local_variables = {
94 std::make_pair("e", "*"),
95 std::make_pair("_log_ev", "*"),
97 /* Ignore local variable about time used for tracing */
98 std::make_pair("start_time", "*"),
101 void ModelChecker::setup_ignore()
103 const RemoteProcess& process = this->get_remote_process();
104 for (auto const& [var, frame] : ignored_local_variables)
105 process.ignore_local_variable(var, frame);
107 /* Static variable used for tracing */
108 process.ignore_global_variable("counter");
111 void ModelChecker::shutdown()
113 XBT_DEBUG("Shutting down model-checker");
115 RemoteProcess& process = get_remote_process();
116 if (process.running()) {
117 XBT_DEBUG("Killing process");
119 kill(process.pid(), SIGKILL);
124 void ModelChecker::resume()
126 if (checker_side_.get_channel().send(MessageType::CONTINUE) != 0)
127 throw xbt::errno_error();
128 remote_process_->clear_cache();
131 static void MC_report_crash(int status)
133 XBT_INFO("**************************");
134 XBT_INFO("** CRASH IN THE PROGRAM **");
135 XBT_INFO("**************************");
136 if (WIFSIGNALED(status))
137 XBT_INFO("From signal: %s", strsignal(WTERMSIG(status)));
138 else if (WIFEXITED(status))
139 XBT_INFO("From exit: %i", WEXITSTATUS(status));
140 if (not xbt_log_no_loc)
141 XBT_INFO("%s core dump was generated by the system.", WCOREDUMP(status) ? "A" : "No");
142 if (mc_model_checker->get_exploration()) {
143 XBT_INFO("Counter-example execution trace:");
144 for (auto const& s : mc_model_checker->get_exploration()->get_textual_trace())
145 XBT_INFO(" %s", s.c_str());
146 XBT_INFO("Path = %s", mc_model_checker->get_exploration()->get_record_trace().to_string().c_str());
147 Api::get().get_session().log_state();
148 if (xbt_log_no_loc) {
149 XBT_INFO("Stack trace not displayed because you passed --log=no_loc");
151 XBT_INFO("Stack trace:");
152 mc_model_checker->get_remote_process().dump_stack();
157 bool ModelChecker::handle_message(const char* buffer, ssize_t size)
159 s_mc_message_t base_message;
160 xbt_assert(size >= (ssize_t)sizeof(base_message), "Broken message");
161 memcpy(&base_message, buffer, sizeof(base_message));
163 switch(base_message.type) {
164 case MessageType::INITIAL_ADDRESSES: {
165 s_mc_message_initial_addresses_t message;
166 xbt_assert(size == sizeof(message), "Broken message. Got %d bytes instead of %d.", (int)size, (int)sizeof(message));
167 memcpy(&message, buffer, sizeof(message));
169 get_remote_process().init(message.mmalloc_default_mdp, message.maxpid, message.actors);
173 case MessageType::IGNORE_HEAP: {
174 s_mc_message_ignore_heap_t message;
175 xbt_assert(size == sizeof(message), "Broken message");
176 memcpy(&message, buffer, sizeof(message));
178 IgnoredHeapRegion region;
179 region.block = message.block;
180 region.fragment = message.fragment;
181 region.address = message.address;
182 region.size = message.size;
183 get_remote_process().ignore_heap(region);
187 case MessageType::UNIGNORE_HEAP: {
188 s_mc_message_ignore_memory_t message;
189 xbt_assert(size == sizeof(message), "Broken message");
190 memcpy(&message, buffer, sizeof(message));
191 get_remote_process().unignore_heap((void*)(std::uintptr_t)message.addr, message.size);
195 case MessageType::IGNORE_MEMORY: {
196 s_mc_message_ignore_memory_t message;
197 xbt_assert(size == sizeof(message), "Broken message");
198 memcpy(&message, buffer, sizeof(message));
199 this->get_remote_process().ignore_region(message.addr, message.size);
203 case MessageType::STACK_REGION: {
204 s_mc_message_stack_region_t message;
205 xbt_assert(size == sizeof(message), "Broken message");
206 memcpy(&message, buffer, sizeof(message));
207 this->get_remote_process().stack_areas().push_back(message.stack_region);
210 case MessageType::REGISTER_SYMBOL: {
211 s_mc_message_register_symbol_t message;
212 xbt_assert(size == sizeof(message), "Broken message");
213 memcpy(&message, buffer, sizeof(message));
214 xbt_assert(not message.callback, "Support for client-side function proposition is not implemented.");
215 XBT_DEBUG("Received symbol: %s", message.name.data());
217 if (property_automaton == nullptr)
218 property_automaton = xbt_automaton_new();
220 const RemoteProcess* process = &this->get_remote_process();
221 RemotePtr<int> address = remote((int*)message.data);
222 xbt::add_proposition(property_automaton, message.name.data(),
223 [process, address]() { return process->read(address); });
228 case MessageType::WAITING:
231 case MessageType::ASSERTION_FAILED:
232 XBT_INFO("**************************");
233 XBT_INFO("*** PROPERTY NOT VALID ***");
234 XBT_INFO("**************************");
235 XBT_INFO("Counter-example execution trace:");
236 for (auto const& s : get_exploration()->get_textual_trace())
237 XBT_INFO(" %s", s.c_str());
238 XBT_INFO("Path = %s", get_exploration()->get_record_trace().to_string().c_str());
239 Api::get().get_session().log_state();
241 this->exit(SIMGRID_MC_EXIT_SAFETY);
244 xbt_die("Unexpected message from model-checked application");
249 /** Terminate the model-checker application */
250 void ModelChecker::exit(int status)
256 void ModelChecker::handle_waitpid()
258 XBT_DEBUG("Check for wait event");
261 while ((pid = waitpid(-1, &status, WNOHANG)) != 0) {
263 if (errno == ECHILD) {
265 xbt_assert(not this->get_remote_process().running(), "Inconsistent state");
268 XBT_ERROR("Could not wait for pid");
269 throw simgrid::xbt::errno_error();
273 if (pid == this->get_remote_process().pid()) {
274 // From PTRACE_O_TRACEEXIT:
276 if (status>>8 == (SIGTRAP | (PTRACE_EVENT_EXIT<<8))) {
277 xbt_assert(ptrace(PTRACE_GETEVENTMSG, remote_process_->pid(), 0, &status) != -1, "Could not get exit status");
278 if (WIFSIGNALED(status)) {
279 MC_report_crash(status);
280 this->get_remote_process().terminate();
281 this->exit(SIMGRID_MC_EXIT_PROGRAM_CRASH);
286 // We don't care about signals, just reinject them:
287 if (WIFSTOPPED(status)) {
288 XBT_DEBUG("Stopped with signal %i", (int) WSTOPSIG(status));
291 ptrace(PTRACE_CONT, remote_process_->pid(), 0, WSTOPSIG(status));
293 ptrace(PT_CONTINUE, remote_process_->pid(), (caddr_t)1, WSTOPSIG(status));
295 xbt_assert(errno == 0, "Could not PTRACE_CONT");
298 else if (WIFSIGNALED(status)) {
299 MC_report_crash(status);
300 this->get_remote_process().terminate();
301 this->exit(SIMGRID_MC_EXIT_PROGRAM_CRASH);
302 } else if (WIFEXITED(status)) {
303 XBT_DEBUG("Child process is over");
304 this->get_remote_process().terminate();
310 void ModelChecker::wait_for_requests()
313 if (this->get_remote_process().running())
314 checker_side_.dispatch();
317 Transition* ModelChecker::handle_simcall(aid_t aid, int times_considered, bool new_transition)
319 s_mc_message_simcall_execute_t m;
320 memset(&m, 0, sizeof(m));
321 m.type = MessageType::SIMCALL_EXECUTE;
323 m.times_considered_ = times_considered;
324 checker_side_.get_channel().send(m);
326 this->remote_process_->clear_cache();
327 if (this->remote_process_->running())
328 checker_side_.dispatch(); // The app may send messages while processing the transition
330 s_mc_message_simcall_execute_answer_t answer;
331 ssize_t s = checker_side_.get_channel().receive(answer);
332 xbt_assert(s != -1, "Could not receive message");
333 xbt_assert(s == sizeof(answer) && answer.type == MessageType::SIMCALL_EXECUTE_ANSWER,
334 "Received unexpected message %s (%i, size=%i) "
335 "expected MessageType::SIMCALL_EXECUTE_ANSWER (%i, size=%i)",
336 to_c_str(answer.type), (int)answer.type, (int)s, (int)MessageType::SIMCALL_EXECUTE_ANSWER,
337 (int)sizeof(answer));
339 if (new_transition) {
340 std::stringstream stream(answer.buffer.data());
341 return deserialize_transition(aid, times_considered, stream);
346 void ModelChecker::finalize_app(bool terminate_asap)
348 s_mc_message_int_t m;
349 memset(&m, 0, sizeof m);
350 m.type = MessageType::FINALIZE;
351 m.value = terminate_asap;
352 xbt_assert(checker_side_.get_channel().send(m) == 0, "Could not ask the app to finalize on need");
354 s_mc_message_t answer;
355 xbt_assert(checker_side_.get_channel().receive(answer) != -1, "Could not receive answer to FINALIZE");
359 } // namespace simgrid