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/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"
19 #include <sys/ptrace.h>
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_ModelChecker, mc, "ModelChecker");
24 ::simgrid::mc::ModelChecker* mc_model_checker = nullptr;
26 using simgrid::mc::remote;
29 # define WAITPID_CHECKED_FLAGS __WALL
31 # define WAITPID_CHECKED_FLAGS 0
37 ModelChecker::ModelChecker(std::unique_ptr<RemoteProcess> remote_simulation, int sockfd)
38 : checker_side_(sockfd), remote_process_(std::move(remote_simulation))
42 void ModelChecker::start()
45 [](evutil_socket_t sig, short events, void* arg) {
46 auto mc = static_cast<simgrid::mc::ModelChecker*>(arg);
47 if (events == EV_READ) {
48 std::array<char, MC_MESSAGE_LENGTH> buffer;
49 ssize_t size = mc->checker_side_.get_channel().receive(buffer.data(), buffer.size(), false);
50 if (size == -1 && errno != EAGAIN)
51 throw simgrid::xbt::errno_error();
53 if (not mc->handle_message(buffer.data(), size))
54 mc->checker_side_.break_loop();
55 } else if (events == EV_SIGNAL) {
59 xbt_die("Unexpected event");
64 XBT_DEBUG("Waiting for the model-checked process");
67 // The model-checked process SIGSTOP itself to signal it's ready:
68 const pid_t pid = remote_process_->pid();
70 xbt_assert(waitpid(pid, &status, WAITPID_CHECKED_FLAGS) == pid && WIFSTOPPED(status) && WSTOPSIG(status) == SIGSTOP,
71 "Could not wait model-checked process");
73 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"
88 static constexpr auto ignored_local_variables = {
89 std::make_pair("e", "*"),
90 std::make_pair("_log_ev", "*"),
92 /* Ignore local variable about time used for tracing */
93 std::make_pair("start_time", "*"),
96 void ModelChecker::setup_ignore()
98 const RemoteProcess& process = this->get_remote_process();
99 for (auto const& var : ignored_local_variables)
100 process.ignore_local_variable(var.first, var.second);
102 /* Static variable used for tracing */
103 process.ignore_global_variable("counter");
106 void ModelChecker::shutdown()
108 XBT_DEBUG("Shutting down model-checker");
110 RemoteProcess& process = get_remote_process();
111 if (process.running()) {
112 XBT_DEBUG("Killing process");
114 kill(process.pid(), SIGKILL);
119 void ModelChecker::resume()
121 int res = checker_side_.get_channel().send(MessageType::CONTINUE);
123 throw xbt::errno_error();
124 remote_process_->clear_cache();
127 static void MC_report_crash(int status)
129 XBT_INFO("**************************");
130 XBT_INFO("** CRASH IN THE PROGRAM **");
131 XBT_INFO("**************************");
132 if (WIFSIGNALED(status))
133 XBT_INFO("From signal: %s", strsignal(WTERMSIG(status)));
134 else if (WIFEXITED(status))
135 XBT_INFO("From exit: %i", WEXITSTATUS(status));
136 if (not xbt_log_no_loc)
137 XBT_INFO("%s core dump was generated by the system.", WCOREDUMP(status) ? "A" : "No");
138 XBT_INFO("Counter-example execution trace:");
139 for (auto const& s : mc_model_checker->getChecker()->get_textual_trace())
140 XBT_INFO(" %s", s.c_str());
142 session_singleton->log_state();
143 if (xbt_log_no_loc) {
144 XBT_INFO("Stack trace not displayed because you passed --log=no_loc");
146 XBT_INFO("Stack trace:");
147 mc_model_checker->get_remote_process().dump_stack();
151 bool ModelChecker::handle_message(const char* buffer, ssize_t size)
153 s_mc_message_t base_message;
154 xbt_assert(size >= (ssize_t)sizeof(base_message), "Broken message");
155 memcpy(&base_message, buffer, sizeof(base_message));
157 switch(base_message.type) {
158 case MessageType::INITIAL_ADDRESSES: {
159 s_mc_message_initial_addresses_t message;
160 xbt_assert(size == sizeof(message), "Broken message. Got %d bytes instead of %d.", (int)size, (int)sizeof(message));
161 memcpy(&message, buffer, sizeof(message));
163 get_remote_process().init(message.mmalloc_default_mdp, message.maxpid, message.actors, message.dead_actors);
167 case MessageType::IGNORE_HEAP: {
168 s_mc_message_ignore_heap_t message;
169 xbt_assert(size == sizeof(message), "Broken message");
170 memcpy(&message, buffer, sizeof(message));
172 IgnoredHeapRegion region;
173 region.block = message.block;
174 region.fragment = message.fragment;
175 region.address = message.address;
176 region.size = message.size;
177 get_remote_process().ignore_heap(region);
181 case MessageType::UNIGNORE_HEAP: {
182 s_mc_message_ignore_memory_t message;
183 xbt_assert(size == sizeof(message), "Broken message");
184 memcpy(&message, buffer, sizeof(message));
185 get_remote_process().unignore_heap((void*)(std::uintptr_t)message.addr, message.size);
189 case MessageType::IGNORE_MEMORY: {
190 s_mc_message_ignore_memory_t message;
191 xbt_assert(size == sizeof(message), "Broken message");
192 memcpy(&message, buffer, sizeof(message));
193 this->get_remote_process().ignore_region(message.addr, message.size);
197 case MessageType::STACK_REGION: {
198 s_mc_message_stack_region_t message;
199 xbt_assert(size == sizeof(message), "Broken message");
200 memcpy(&message, buffer, sizeof(message));
201 this->get_remote_process().stack_areas().push_back(message.stack_region);
204 case MessageType::REGISTER_SYMBOL: {
205 s_mc_message_register_symbol_t message;
206 xbt_assert(size == sizeof(message), "Broken message");
207 memcpy(&message, buffer, sizeof(message));
208 xbt_assert(not message.callback, "Support for client-side function proposition is not implemented.");
209 XBT_DEBUG("Received symbol: %s", message.name.data());
211 if (property_automaton == nullptr)
212 property_automaton = xbt_automaton_new();
214 const RemoteProcess* process = &this->get_remote_process();
215 RemotePtr<int> address = remote((int*)message.data);
216 xbt::add_proposition(property_automaton, message.name.data(),
217 [process, address]() { return process->read(address); });
222 case MessageType::WAITING:
225 case MessageType::ASSERTION_FAILED:
226 XBT_INFO("**************************");
227 XBT_INFO("*** PROPERTY NOT VALID ***");
228 XBT_INFO("**************************");
229 XBT_INFO("Counter-example execution trace:");
230 for (auto const& s : getChecker()->get_textual_trace())
231 XBT_INFO(" %s", s.c_str());
233 session_singleton->log_state();
235 this->exit(SIMGRID_MC_EXIT_SAFETY);
238 xbt_die("Unexpected message from model-checked application");
243 /** Terminate the model-checker application */
244 void ModelChecker::exit(int status)
250 void ModelChecker::handle_waitpid()
252 XBT_DEBUG("Check for wait event");
255 while ((pid = waitpid(-1, &status, WNOHANG)) != 0) {
257 if (errno == ECHILD) {
259 xbt_assert(not this->get_remote_process().running(), "Inconsistent state");
262 XBT_ERROR("Could not wait for pid");
263 throw simgrid::xbt::errno_error();
267 if (pid == this->get_remote_process().pid()) {
268 // From PTRACE_O_TRACEEXIT:
270 if (status>>8 == (SIGTRAP | (PTRACE_EVENT_EXIT<<8))) {
271 xbt_assert(ptrace(PTRACE_GETEVENTMSG, remote_process_->pid(), 0, &status) != -1, "Could not get exit status");
272 if (WIFSIGNALED(status)) {
273 MC_report_crash(status);
274 this->get_remote_process().terminate();
275 this->exit(SIMGRID_MC_EXIT_PROGRAM_CRASH);
280 // We don't care about signals, just reinject them:
281 if (WIFSTOPPED(status)) {
282 XBT_DEBUG("Stopped with signal %i", (int) WSTOPSIG(status));
285 ptrace(PTRACE_CONT, remote_process_->pid(), 0, WSTOPSIG(status));
287 ptrace(PT_CONTINUE, remote_process_->pid(), (caddr_t)1, WSTOPSIG(status));
289 xbt_assert(errno == 0, "Could not PTRACE_CONT");
292 else if (WIFSIGNALED(status)) {
293 MC_report_crash(status);
294 this->get_remote_process().terminate();
295 this->exit(SIMGRID_MC_EXIT_PROGRAM_CRASH);
296 } else if (WIFEXITED(status)) {
297 XBT_DEBUG("Child process is over");
298 this->get_remote_process().terminate();
304 void ModelChecker::wait_for_requests()
307 if (this->get_remote_process().running())
308 checker_side_.dispatch();
311 void ModelChecker::handle_simcall(Transition const& transition)
313 s_mc_message_simcall_handle_t m;
314 memset(&m, 0, sizeof(m));
315 m.type = MessageType::SIMCALL_HANDLE;
316 m.aid_ = transition.aid_;
317 m.times_considered_ = transition.times_considered_;
318 checker_side_.get_channel().send(m);
319 this->remote_process_->clear_cache();
320 if (this->remote_process_->running())
321 checker_side_.dispatch();
323 bool ModelChecker::simcall_is_visible(aid_t aid)
325 xbt_assert(mc_model_checker != nullptr, "This should be called from the checker side");
327 s_mc_message_simcall_is_visible_t m;
328 memset(&m, 0, sizeof(m));
329 m.type = MessageType::SIMCALL_IS_VISIBLE;
331 checker_side_.get_channel().send(m);
333 s_mc_message_simcall_is_visible_answer_t answer;
334 ssize_t s = checker_side_.get_channel().receive(answer);
335 xbt_assert(s != -1, "Could not receive message");
336 xbt_assert(s == sizeof(answer) && answer.type == MessageType::SIMCALL_IS_VISIBLE_ANSWER,
337 "Received unexpected message %s (%i, size=%i) "
338 "expected MessageType::SIMCALL_IS_VISIBLE_ANSWER (%i, size=%i)",
339 to_c_str(answer.type), (int)answer.type, (int)s, (int)MessageType::SIMCALL_IS_VISIBLE_ANSWER,
340 (int)sizeof(answer));
342 XBT_DEBUG("is_visible(%ld) is returning %s", aid, answer.value ? "true" : "false");
344 this->remote_process_->clear_cache();
348 std::string ModelChecker::simcall_to_string(MessageType type, aid_t aid, int times_considered)
350 xbt_assert(mc_model_checker != nullptr, "This should be called from the checker side");
352 s_mc_message_simcall_to_string_t m;
353 memset(&m, 0, sizeof(m));
356 m.time_considered = times_considered;
357 checker_side_.get_channel().send(m);
359 s_mc_message_simcall_to_string_answer_t answer;
360 ssize_t s = checker_side_.get_channel().receive(answer);
361 xbt_assert(s != -1, "Could not receive message");
362 xbt_assert(s == sizeof(answer) && answer.type == MessageType::SIMCALL_TO_STRING_ANSWER,
363 "Received unexpected message %s (%i, size=%i) "
364 "expected MessageType::SIMCALL_TO_STRING_ANSWER (%i, size=%i)",
365 to_c_str(answer.type), (int)answer.type, (int)s, (int)MessageType::SIMCALL_TO_STRING_ANSWER,
366 (int)sizeof(answer));
368 return std::string(answer.value);
371 std::string ModelChecker::simcall_to_string(aid_t aid, int times_considered)
373 std::string answer = simcall_to_string(MessageType::SIMCALL_TO_STRING, aid, times_considered);
374 XBT_DEBUG("to_string(%ld) is returning %s", aid, answer.c_str());
378 std::string ModelChecker::simcall_dot_label(aid_t aid, int times_considered)
380 std::string answer = simcall_to_string(MessageType::SIMCALL_DOT_LABEL, aid, times_considered);
381 XBT_DEBUG("dot_label(%ld) is returning %s", aid, answer.c_str());
385 void ModelChecker::finalize_app(bool terminate_asap)
387 s_mc_message_int_t m;
388 memset(&m, 0, sizeof m);
389 m.type = MessageType::FINALIZE;
390 m.value = terminate_asap;
391 xbt_assert(checker_side_.get_channel().send(m) == 0, "Could not ask the app to finalize on need");
393 s_mc_message_t answer;
394 xbt_assert(checker_side_.get_channel().receive(answer) != -1, "Could not receive answer to FINALIZE");
397 bool ModelChecker::checkDeadlock()
399 xbt_assert(checker_side_.get_channel().send(MessageType::DEADLOCK_CHECK) == 0, "Could not check deadlock state");
400 s_mc_message_int_t message;
401 ssize_t s = checker_side_.get_channel().receive(message);
402 xbt_assert(s != -1, "Could not receive message");
403 xbt_assert(s == sizeof(message) && message.type == MessageType::DEADLOCK_CHECK_REPLY,
404 "Received unexpected message %s (%i, size=%i) "
405 "expected MessageType::DEADLOCK_CHECK_REPLY (%i, size=%i)",
406 to_c_str(message.type), (int)message.type, (int)s, (int)MessageType::DEADLOCK_CHECK_REPLY,
407 (int)sizeof(message));
408 return message.value != 0;
412 } // namespace simgrid