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/Session.hpp"
8 #include "src/mc/api/Transition.hpp"
9 #include "src/mc/api/TransitionComm.hpp"
10 #include "src/mc/checker/Checker.hpp"
11 #include "src/mc/mc_config.hpp"
12 #include "src/mc/mc_exit.hpp"
13 #include "src/mc/mc_private.hpp"
14 #include "src/mc/remote/RemoteProcess.hpp"
15 #include "xbt/automaton.hpp"
16 #include "xbt/system_error.hpp"
20 #include <sys/ptrace.h>
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_ModelChecker, mc, "ModelChecker");
25 ::simgrid::mc::ModelChecker* mc_model_checker = nullptr;
27 using simgrid::mc::remote;
30 # define WAITPID_CHECKED_FLAGS __WALL
32 # define WAITPID_CHECKED_FLAGS 0
38 ModelChecker::ModelChecker(std::unique_ptr<RemoteProcess> remote_simulation, int sockfd)
39 : checker_side_(sockfd), remote_process_(std::move(remote_simulation))
43 void ModelChecker::start()
46 [](evutil_socket_t sig, short events, void* arg) {
47 auto mc = static_cast<simgrid::mc::ModelChecker*>(arg);
48 if (events == EV_READ) {
49 std::array<char, MC_MESSAGE_LENGTH> buffer;
50 ssize_t size = mc->checker_side_.get_channel().receive(buffer.data(), buffer.size(), false);
51 if (size == -1 && errno != EAGAIN)
52 throw simgrid::xbt::errno_error();
54 if (not mc->handle_message(buffer.data(), size))
55 mc->checker_side_.break_loop();
56 } else if (events == EV_SIGNAL) {
60 xbt_die("Unexpected event");
65 XBT_DEBUG("Waiting for the model-checked process");
68 // The model-checked process SIGSTOP itself to signal it's ready:
69 const pid_t pid = remote_process_->pid();
71 xbt_assert(waitpid(pid, &status, WAITPID_CHECKED_FLAGS) == pid && WIFSTOPPED(status) && WSTOPSIG(status) == SIGSTOP,
72 "Could not wait model-checked process");
74 if (not _sg_mc_dot_output_file.get().empty())
80 ptrace(PTRACE_SETOPTIONS, pid, nullptr, PTRACE_O_TRACEEXIT);
81 ptrace(PTRACE_CONT, pid, 0, 0);
83 ptrace(PT_CONTINUE, pid, (caddr_t)1, 0);
85 # error "no ptrace equivalent coded for this platform"
89 static constexpr auto ignored_local_variables = {
90 std::make_pair("e", "*"),
91 std::make_pair("_log_ev", "*"),
93 /* Ignore local variable about time used for tracing */
94 std::make_pair("start_time", "*"),
97 void ModelChecker::setup_ignore()
99 const RemoteProcess& process = this->get_remote_process();
100 for (auto const& var : ignored_local_variables)
101 process.ignore_local_variable(var.first, var.second);
103 /* Static variable used for tracing */
104 process.ignore_global_variable("counter");
107 void ModelChecker::shutdown()
109 XBT_DEBUG("Shutting down model-checker");
111 RemoteProcess& process = get_remote_process();
112 if (process.running()) {
113 XBT_DEBUG("Killing process");
115 kill(process.pid(), SIGKILL);
120 void ModelChecker::resume()
122 int res = checker_side_.get_channel().send(MessageType::CONTINUE);
124 throw xbt::errno_error();
125 remote_process_->clear_cache();
128 static void MC_report_crash(int status)
130 XBT_INFO("**************************");
131 XBT_INFO("** CRASH IN THE PROGRAM **");
132 XBT_INFO("**************************");
133 if (WIFSIGNALED(status))
134 XBT_INFO("From signal: %s", strsignal(WTERMSIG(status)));
135 else if (WIFEXITED(status))
136 XBT_INFO("From exit: %i", WEXITSTATUS(status));
137 if (not xbt_log_no_loc)
138 XBT_INFO("%s core dump was generated by the system.", WCOREDUMP(status) ? "A" : "No");
139 XBT_INFO("Counter-example execution trace:");
140 for (auto const& s : mc_model_checker->getChecker()->get_textual_trace())
141 XBT_INFO(" %s", s.c_str());
142 XBT_INFO("Path = %s", mc_model_checker->getChecker()->get_record_trace().to_string().c_str());
143 session_singleton->log_state();
144 if (xbt_log_no_loc) {
145 XBT_INFO("Stack trace not displayed because you passed --log=no_loc");
147 XBT_INFO("Stack trace:");
148 mc_model_checker->get_remote_process().dump_stack();
152 bool ModelChecker::handle_message(const char* buffer, ssize_t size)
154 s_mc_message_t base_message;
155 xbt_assert(size >= (ssize_t)sizeof(base_message), "Broken message");
156 memcpy(&base_message, buffer, sizeof(base_message));
158 switch(base_message.type) {
159 case MessageType::INITIAL_ADDRESSES: {
160 s_mc_message_initial_addresses_t message;
161 xbt_assert(size == sizeof(message), "Broken message. Got %d bytes instead of %d.", (int)size, (int)sizeof(message));
162 memcpy(&message, buffer, sizeof(message));
164 get_remote_process().init(message.mmalloc_default_mdp, message.maxpid, message.actors, message.dead_actors);
168 case MessageType::IGNORE_HEAP: {
169 s_mc_message_ignore_heap_t message;
170 xbt_assert(size == sizeof(message), "Broken message");
171 memcpy(&message, buffer, sizeof(message));
173 IgnoredHeapRegion region;
174 region.block = message.block;
175 region.fragment = message.fragment;
176 region.address = message.address;
177 region.size = message.size;
178 get_remote_process().ignore_heap(region);
182 case MessageType::UNIGNORE_HEAP: {
183 s_mc_message_ignore_memory_t message;
184 xbt_assert(size == sizeof(message), "Broken message");
185 memcpy(&message, buffer, sizeof(message));
186 get_remote_process().unignore_heap((void*)(std::uintptr_t)message.addr, message.size);
190 case MessageType::IGNORE_MEMORY: {
191 s_mc_message_ignore_memory_t message;
192 xbt_assert(size == sizeof(message), "Broken message");
193 memcpy(&message, buffer, sizeof(message));
194 this->get_remote_process().ignore_region(message.addr, message.size);
198 case MessageType::STACK_REGION: {
199 s_mc_message_stack_region_t message;
200 xbt_assert(size == sizeof(message), "Broken message");
201 memcpy(&message, buffer, sizeof(message));
202 this->get_remote_process().stack_areas().push_back(message.stack_region);
205 case MessageType::REGISTER_SYMBOL: {
206 s_mc_message_register_symbol_t message;
207 xbt_assert(size == sizeof(message), "Broken message");
208 memcpy(&message, buffer, sizeof(message));
209 xbt_assert(not message.callback, "Support for client-side function proposition is not implemented.");
210 XBT_DEBUG("Received symbol: %s", message.name.data());
212 if (property_automaton == nullptr)
213 property_automaton = xbt_automaton_new();
215 const RemoteProcess* process = &this->get_remote_process();
216 RemotePtr<int> address = remote((int*)message.data);
217 xbt::add_proposition(property_automaton, message.name.data(),
218 [process, address]() { return process->read(address); });
223 case MessageType::WAITING:
226 case MessageType::ASSERTION_FAILED:
227 XBT_INFO("**************************");
228 XBT_INFO("*** PROPERTY NOT VALID ***");
229 XBT_INFO("**************************");
230 XBT_INFO("Counter-example execution trace:");
231 for (auto const& s : getChecker()->get_textual_trace())
232 XBT_INFO(" %s", s.c_str());
233 XBT_INFO("Path = %s", getChecker()->get_record_trace().to_string().c_str());
234 session_singleton->log_state();
236 this->exit(SIMGRID_MC_EXIT_SAFETY);
239 xbt_die("Unexpected message from model-checked application");
244 /** Terminate the model-checker application */
245 void ModelChecker::exit(int status)
251 void ModelChecker::handle_waitpid()
253 XBT_DEBUG("Check for wait event");
256 while ((pid = waitpid(-1, &status, WNOHANG)) != 0) {
258 if (errno == ECHILD) {
260 xbt_assert(not this->get_remote_process().running(), "Inconsistent state");
263 XBT_ERROR("Could not wait for pid");
264 throw simgrid::xbt::errno_error();
268 if (pid == this->get_remote_process().pid()) {
269 // From PTRACE_O_TRACEEXIT:
271 if (status>>8 == (SIGTRAP | (PTRACE_EVENT_EXIT<<8))) {
272 xbt_assert(ptrace(PTRACE_GETEVENTMSG, remote_process_->pid(), 0, &status) != -1, "Could not get exit status");
273 if (WIFSIGNALED(status)) {
274 MC_report_crash(status);
275 this->get_remote_process().terminate();
276 this->exit(SIMGRID_MC_EXIT_PROGRAM_CRASH);
281 // We don't care about signals, just reinject them:
282 if (WIFSTOPPED(status)) {
283 XBT_DEBUG("Stopped with signal %i", (int) WSTOPSIG(status));
286 ptrace(PTRACE_CONT, remote_process_->pid(), 0, WSTOPSIG(status));
288 ptrace(PT_CONTINUE, remote_process_->pid(), (caddr_t)1, WSTOPSIG(status));
290 xbt_assert(errno == 0, "Could not PTRACE_CONT");
293 else if (WIFSIGNALED(status)) {
294 MC_report_crash(status);
295 this->get_remote_process().terminate();
296 this->exit(SIMGRID_MC_EXIT_PROGRAM_CRASH);
297 } else if (WIFEXITED(status)) {
298 XBT_DEBUG("Child process is over");
299 this->get_remote_process().terminate();
305 void ModelChecker::wait_for_requests()
308 if (this->get_remote_process().running())
309 checker_side_.dispatch();
312 Transition* ModelChecker::handle_simcall(aid_t aid, int times_considered, bool new_transition)
314 s_mc_message_simcall_execute_t m;
315 memset(&m, 0, sizeof(m));
316 m.type = MessageType::SIMCALL_EXECUTE;
318 m.times_considered_ = times_considered;
319 checker_side_.get_channel().send(m);
321 s_mc_message_simcall_execute_answer_t answer;
322 ssize_t s = checker_side_.get_channel().receive(answer);
323 xbt_assert(s != -1, "Could not receive message");
324 xbt_assert(s == sizeof(answer) && answer.type == MessageType::SIMCALL_EXECUTE_ANSWER,
325 "Received unexpected message %s (%i, size=%i) "
326 "expected MessageType::SIMCALL_EXECUTE_ANSWER (%i, size=%i)",
327 to_c_str(answer.type), (int)answer.type, (int)s, (int)MessageType::SIMCALL_EXECUTE_ANSWER,
328 (int)sizeof(answer));
330 this->remote_process_->clear_cache();
331 if (this->remote_process_->running())
332 checker_side_.dispatch(); // The app may send messages while processing the transition
334 if (new_transition) {
335 std::stringstream stream(answer.buffer.data());
336 return deserialize_transition(aid, times_considered, stream);
340 bool ModelChecker::simcall_is_visible(aid_t aid)
342 xbt_assert(mc_model_checker != nullptr, "This should be called from the checker side");
344 s_mc_message_simcall_is_visible_t m;
345 memset(&m, 0, sizeof(m));
346 m.type = MessageType::SIMCALL_IS_VISIBLE;
348 checker_side_.get_channel().send(m);
350 s_mc_message_simcall_is_visible_answer_t answer;
351 ssize_t s = checker_side_.get_channel().receive(answer);
352 xbt_assert(s != -1, "Could not receive message");
353 xbt_assert(s == sizeof(answer) && answer.type == MessageType::SIMCALL_IS_VISIBLE_ANSWER,
354 "Received unexpected message %s (%i, size=%i) "
355 "expected MessageType::SIMCALL_IS_VISIBLE_ANSWER (%i, size=%i)",
356 to_c_str(answer.type), (int)answer.type, (int)s, (int)MessageType::SIMCALL_IS_VISIBLE_ANSWER,
357 (int)sizeof(answer));
359 XBT_DEBUG("is_visible(%ld) is returning %s", aid, answer.value ? "true" : "false");
361 this->remote_process_->clear_cache();
365 void ModelChecker::finalize_app(bool terminate_asap)
367 s_mc_message_int_t m;
368 memset(&m, 0, sizeof m);
369 m.type = MessageType::FINALIZE;
370 m.value = terminate_asap;
371 xbt_assert(checker_side_.get_channel().send(m) == 0, "Could not ask the app to finalize on need");
373 s_mc_message_t answer;
374 xbt_assert(checker_side_.get_channel().receive(answer) != -1, "Could not receive answer to FINALIZE");
377 bool ModelChecker::checkDeadlock()
379 xbt_assert(checker_side_.get_channel().send(MessageType::DEADLOCK_CHECK) == 0, "Could not check deadlock state");
380 s_mc_message_int_t message;
381 ssize_t s = checker_side_.get_channel().receive(message);
382 xbt_assert(s != -1, "Could not receive message");
383 xbt_assert(s == sizeof(message) && message.type == MessageType::DEADLOCK_CHECK_REPLY,
384 "Received unexpected message %s (%i, size=%i) "
385 "expected MessageType::DEADLOCK_CHECK_REPLY (%i, size=%i)",
386 to_c_str(message.type), (int)message.type, (int)s, (int)MessageType::DEADLOCK_CHECK_REPLY,
387 (int)sizeof(message));
388 return message.value != 0;
392 } // namespace simgrid