1 /* Copyright (c) 2008-2021. 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/Transition.hpp"
9 #include "src/mc/checker/Checker.hpp"
10 #include "src/mc/mc_config.hpp"
11 #include "src/mc/mc_exit.hpp"
12 #include "src/mc/mc_private.hpp"
13 #include "src/mc/remote/RemoteProcess.hpp"
14 #include "xbt/automaton.hpp"
15 #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()
43 checker_side_.start([](evutil_socket_t sig, short events, void* arg) {
44 auto mc = static_cast<simgrid::mc::ModelChecker*>(arg);
45 if (events == EV_READ) {
46 std::array<char, MC_MESSAGE_LENGTH> buffer;
47 ssize_t size = mc->checker_side_.get_channel().receive(buffer.data(), buffer.size(), false);
48 if (size == -1 && errno != EAGAIN)
49 throw simgrid::xbt::errno_error();
51 if (not mc->handle_message(buffer.data(), size))
52 mc->checker_side_.break_loop();
53 } else if (events == EV_SIGNAL) {
57 xbt_die("Unexpected event");
61 XBT_DEBUG("Waiting for the model-checked process");
64 // The model-checked process SIGSTOP itself to signal it's ready:
65 const pid_t pid = remote_process_->pid();
67 pid_t res = waitpid(pid, &status, WAITPID_CHECKED_FLAGS);
68 if (res < 0 || not WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP)
69 xbt_die("Could not wait model-checked process");
71 if (not _sg_mc_dot_output_file.get().empty())
77 ptrace(PTRACE_SETOPTIONS, pid, nullptr, PTRACE_O_TRACEEXIT);
78 ptrace(PTRACE_CONT, pid, 0, 0);
80 ptrace(PT_CONTINUE, pid, (caddr_t)1, 0);
82 # error "no ptrace equivalent coded for this platform"
86 static constexpr auto ignored_local_variables = {
87 std::make_pair("e", "*"),
88 std::make_pair("_log_ev", "*"),
90 /* Ignore local variable about time used for tracing */
91 std::make_pair("start_time", "*"),
94 void ModelChecker::setup_ignore()
96 const RemoteProcess& process = this->get_remote_process();
97 for (auto const& var : ignored_local_variables)
98 process.ignore_local_variable(var.first, var.second);
100 /* Static variable used for tracing */
101 process.ignore_global_variable("counter");
104 void ModelChecker::shutdown()
106 XBT_DEBUG("Shutting down model-checker");
108 RemoteProcess& process = get_remote_process();
109 if (process.running()) {
110 XBT_DEBUG("Killing process");
111 // TODO, terminate the model checker politely instead of exiting rudely
112 kill(process.pid(), SIGKILL);
117 void ModelChecker::resume()
119 int res = checker_side_.get_channel().send(MessageType::CONTINUE);
121 throw xbt::errno_error();
122 remote_process_->clear_cache();
125 static void MC_report_crash(int status)
127 XBT_INFO("**************************");
128 XBT_INFO("** CRASH IN THE PROGRAM **");
129 XBT_INFO("**************************");
130 if (WIFSIGNALED(status))
131 XBT_INFO("From signal: %s", strsignal(WTERMSIG(status)));
132 else if (WIFEXITED(status))
133 XBT_INFO("From exit: %i", WEXITSTATUS(status));
134 if (not xbt_log_no_loc)
135 XBT_INFO("%s core dump was generated by the system.", WCOREDUMP(status) ? "A" : "No");
136 XBT_INFO("Counter-example execution trace:");
137 for (auto const& s : mc_model_checker->getChecker()->get_textual_trace())
138 XBT_INFO(" %s", s.c_str());
140 session_singleton->log_state();
141 if (xbt_log_no_loc) {
142 XBT_INFO("Stack trace not displayed because you passed --log=no_loc");
144 XBT_INFO("Stack trace:");
145 mc_model_checker->get_remote_process().dump_stack();
149 bool ModelChecker::handle_message(const char* buffer, ssize_t size)
151 s_mc_message_t base_message;
152 xbt_assert(size >= (ssize_t)sizeof(base_message), "Broken message");
153 memcpy(&base_message, buffer, sizeof(base_message));
155 switch(base_message.type) {
156 case MessageType::INITIAL_ADDRESSES: {
157 s_mc_message_initial_addresses_t message;
158 xbt_assert(size == sizeof(message), "Broken message. Got %d bytes instead of %d.", (int)size, (int)sizeof(message));
159 memcpy(&message, buffer, sizeof(message));
161 get_remote_process().init(message.mmalloc_default_mdp, message.maxpid, message.actors, message.dead_actors);
165 case MessageType::IGNORE_HEAP: {
166 s_mc_message_ignore_heap_t message;
167 xbt_assert(size == sizeof(message), "Broken message");
168 memcpy(&message, buffer, sizeof(message));
170 IgnoredHeapRegion region;
171 region.block = message.block;
172 region.fragment = message.fragment;
173 region.address = message.address;
174 region.size = message.size;
175 get_remote_process().ignore_heap(region);
179 case MessageType::UNIGNORE_HEAP: {
180 s_mc_message_ignore_memory_t message;
181 xbt_assert(size == sizeof(message), "Broken message");
182 memcpy(&message, buffer, sizeof(message));
183 get_remote_process().unignore_heap((void*)(std::uintptr_t)message.addr, message.size);
187 case MessageType::IGNORE_MEMORY: {
188 s_mc_message_ignore_memory_t message;
189 xbt_assert(size == sizeof(message), "Broken message");
190 memcpy(&message, buffer, sizeof(message));
191 this->get_remote_process().ignore_region(message.addr, message.size);
195 case MessageType::STACK_REGION: {
196 s_mc_message_stack_region_t message;
197 xbt_assert(size == sizeof(message), "Broken message");
198 memcpy(&message, buffer, sizeof(message));
199 this->get_remote_process().stack_areas().push_back(message.stack_region);
202 case MessageType::REGISTER_SYMBOL: {
203 s_mc_message_register_symbol_t message;
204 xbt_assert(size == sizeof(message), "Broken message");
205 memcpy(&message, buffer, sizeof(message));
206 xbt_assert(not message.callback, "Support for client-side function proposition is not implemented.");
207 XBT_DEBUG("Received symbol: %s", message.name.data());
209 if (property_automaton == nullptr)
210 property_automaton = xbt_automaton_new();
212 const RemoteProcess* process = &this->get_remote_process();
213 RemotePtr<int> address = remote((int*)message.data);
214 xbt::add_proposition(property_automaton, message.name.data(),
215 [process, address]() { return process->read(address); });
220 case MessageType::WAITING:
223 case MessageType::ASSERTION_FAILED:
224 XBT_INFO("**************************");
225 XBT_INFO("*** PROPERTY NOT VALID ***");
226 XBT_INFO("**************************");
227 XBT_INFO("Counter-example execution trace:");
228 for (auto const& s : getChecker()->get_textual_trace())
229 XBT_INFO(" %s", s.c_str());
231 session_singleton->log_state();
233 this->exit(SIMGRID_MC_EXIT_SAFETY);
236 xbt_die("Unexpected message from model-checked application");
241 /** Terminate the model-checker application */
242 void ModelChecker::exit(int status)
248 void ModelChecker::handle_waitpid()
250 XBT_DEBUG("Check for wait event");
253 while ((pid = waitpid(-1, &status, WNOHANG)) != 0) {
255 if (errno == ECHILD) {
257 xbt_assert(not this->get_remote_process().running(), "Inconsistent state");
260 XBT_ERROR("Could not wait for pid");
261 throw simgrid::xbt::errno_error();
265 if (pid == this->get_remote_process().pid()) {
266 // From PTRACE_O_TRACEEXIT:
268 if (status>>8 == (SIGTRAP | (PTRACE_EVENT_EXIT<<8))) {
269 int ptrace_res = ptrace(PTRACE_GETEVENTMSG, remote_process_->pid(), 0, &status);
270 xbt_assert(ptrace_res != -1, "Could not get exit status");
271 if (WIFSIGNALED(status)) {
272 MC_report_crash(status);
273 this->get_remote_process().terminate();
274 this->exit(SIMGRID_MC_EXIT_PROGRAM_CRASH);
279 // We don't care about signals, just reinject them:
280 if (WIFSTOPPED(status)) {
281 XBT_DEBUG("Stopped with signal %i", (int) WSTOPSIG(status));
284 ptrace(PTRACE_CONT, remote_process_->pid(), 0, WSTOPSIG(status));
286 ptrace(PT_CONTINUE, remote_process_->pid(), (caddr_t)1, WSTOPSIG(status));
288 xbt_assert(errno == 0, "Could not PTRACE_CONT");
291 else if (WIFSIGNALED(status)) {
292 MC_report_crash(status);
293 this->get_remote_process().terminate();
294 this->exit(SIMGRID_MC_EXIT_PROGRAM_CRASH);
295 } else if (WIFEXITED(status)) {
296 XBT_DEBUG("Child process is over");
297 this->get_remote_process().terminate();
303 void ModelChecker::wait_for_requests()
306 if (this->get_remote_process().running())
307 checker_side_.dispatch();
310 void ModelChecker::handle_simcall(Transition const& transition)
312 s_mc_message_simcall_handle_t m;
313 memset(&m, 0, sizeof(m));
314 m.type = MessageType::SIMCALL_HANDLE;
315 m.pid_ = transition.pid_;
316 m.times_considered_ = transition.times_considered_;
317 checker_side_.get_channel().send(m);
318 this->remote_process_->clear_cache();
319 if (this->remote_process_->running())
320 checker_side_.dispatch();
322 bool ModelChecker::simcall_is_visible(int aid)
324 xbt_assert(mc_model_checker != nullptr, "This should be called from the checker side");
326 s_mc_message_simcall_is_visible_t m;
327 memset(&m, 0, sizeof(m));
328 m.type = MessageType::SIMCALL_IS_VISIBLE;
330 checker_side_.get_channel().send(m);
332 s_mc_message_simcall_is_visible_answer_t answer;
333 ssize_t s = checker_side_.get_channel().receive(answer);
334 xbt_assert(s != -1, "Could not receive message");
335 xbt_assert(s == sizeof(answer) && answer.type == MessageType::SIMCALL_IS_VISIBLE_ANSWER,
336 "Received unexpected message %s (%i, size=%i) "
337 "expected MessageType::SIMCALL_IS_VISIBLE_ANSWER (%i, size=%i)",
338 to_c_str(answer.type), (int)answer.type, (int)s, (int)MessageType::SIMCALL_IS_VISIBLE_ANSWER,
339 (int)sizeof(answer));
341 XBT_DEBUG("is_visible(%d) is returning %s", aid, answer.value ? "true" : "false");
343 this->remote_process_->clear_cache();
347 std::string ModelChecker::simcall_to_string(MessageType type, int aid, int times_considered)
349 xbt_assert(mc_model_checker != nullptr, "This should be called from the checker side");
351 s_mc_message_simcall_to_string_t m;
352 memset(&m, 0, sizeof(m));
355 m.time_considered = times_considered;
356 checker_side_.get_channel().send(m);
358 s_mc_message_simcall_to_string_answer_t answer;
359 ssize_t s = checker_side_.get_channel().receive(answer);
360 xbt_assert(s != -1, "Could not receive message");
361 xbt_assert(s == sizeof(answer) && answer.type == MessageType::SIMCALL_TO_STRING_ANSWER,
362 "Received unexpected message %s (%i, size=%i) "
363 "expected MessageType::SIMCALL_TO_STRING_ANSWER (%i, size=%i)",
364 to_c_str(answer.type), (int)answer.type, (int)s, (int)MessageType::SIMCALL_TO_STRING_ANSWER,
365 (int)sizeof(answer));
367 return std::string(answer.value);
370 std::string ModelChecker::simcall_to_string(int aid, int times_considered)
372 std::string answer = simcall_to_string(MessageType::SIMCALL_TO_STRING, aid, times_considered);
373 XBT_DEBUG("to_string(%d) is returning %s", aid, answer.c_str());
377 std::string ModelChecker::simcall_dot_label(int aid, int times_considered)
379 std::string answer = simcall_to_string(MessageType::SIMCALL_DOT_LABEL, aid, times_considered);
380 XBT_DEBUG("dot_label(%d) is returning %s", aid, answer.c_str());
384 void ModelChecker::finalize_app()
386 int res = checker_side_.get_channel().send(MessageType::FINALIZE);
387 xbt_assert(res == 0, "Could not ask the app to finalize on need");
389 s_mc_message_t answer;
390 ssize_t s = checker_side_.get_channel().receive(answer);
391 xbt_assert(s != -1, "Could not receive answer to FINALIZE");
394 bool ModelChecker::checkDeadlock()
396 int res = checker_side_.get_channel().send(MessageType::DEADLOCK_CHECK);
397 xbt_assert(res == 0, "Could not check deadlock state");
398 s_mc_message_int_t message;
399 ssize_t s = checker_side_.get_channel().receive(message);
400 xbt_assert(s != -1, "Could not receive message");
401 xbt_assert(s == sizeof(message) && message.type == MessageType::DEADLOCK_CHECK_REPLY,
402 "Received unexpected message %s (%i, size=%i) "
403 "expected MessageType::DEADLOCK_CHECK_REPLY (%i, size=%i)",
404 to_c_str(message.type), (int)message.type, (int)s, (int)MessageType::DEADLOCK_CHECK_REPLY,
405 (int)sizeof(message));
406 return message.value != 0;
410 } // namespace simgrid