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 xbt_assert(waitpid(pid, &status, WAITPID_CHECKED_FLAGS) == pid && WIFSTOPPED(status) && WSTOPSIG(status) == SIGSTOP,
68 "Could not wait model-checked process");
70 if (not _sg_mc_dot_output_file.get().empty())
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"
85 static constexpr auto ignored_local_variables = {
86 std::make_pair("e", "*"),
87 std::make_pair("_log_ev", "*"),
89 /* Ignore local variable about time used for tracing */
90 std::make_pair("start_time", "*"),
93 void ModelChecker::setup_ignore()
95 const RemoteProcess& process = this->get_remote_process();
96 for (auto const& var : ignored_local_variables)
97 process.ignore_local_variable(var.first, var.second);
99 /* Static variable used for tracing */
100 process.ignore_global_variable("counter");
103 void ModelChecker::shutdown()
105 XBT_DEBUG("Shutting down model-checker");
107 RemoteProcess& process = get_remote_process();
108 if (process.running()) {
109 XBT_DEBUG("Killing process");
111 kill(process.pid(), SIGKILL);
116 void ModelChecker::resume()
118 int res = checker_side_.get_channel().send(MessageType::CONTINUE);
120 throw xbt::errno_error();
121 remote_process_->clear_cache();
124 static void MC_report_crash(int status)
126 XBT_INFO("**************************");
127 XBT_INFO("** CRASH IN THE PROGRAM **");
128 XBT_INFO("**************************");
129 if (WIFSIGNALED(status))
130 XBT_INFO("From signal: %s", strsignal(WTERMSIG(status)));
131 else if (WIFEXITED(status))
132 XBT_INFO("From exit: %i", WEXITSTATUS(status));
133 if (not xbt_log_no_loc)
134 XBT_INFO("%s core dump was generated by the system.", WCOREDUMP(status) ? "A" : "No");
135 XBT_INFO("Counter-example execution trace:");
136 for (auto const& s : mc_model_checker->getChecker()->get_textual_trace())
137 XBT_INFO(" %s", s.c_str());
139 session_singleton->log_state();
140 if (xbt_log_no_loc) {
141 XBT_INFO("Stack trace not displayed because you passed --log=no_loc");
143 XBT_INFO("Stack trace:");
144 mc_model_checker->get_remote_process().dump_stack();
148 bool ModelChecker::handle_message(const char* buffer, ssize_t size)
150 s_mc_message_t base_message;
151 xbt_assert(size >= (ssize_t)sizeof(base_message), "Broken message");
152 memcpy(&base_message, buffer, sizeof(base_message));
154 switch(base_message.type) {
155 case MessageType::INITIAL_ADDRESSES: {
156 s_mc_message_initial_addresses_t message;
157 xbt_assert(size == sizeof(message), "Broken message. Got %d bytes instead of %d.", (int)size, (int)sizeof(message));
158 memcpy(&message, buffer, sizeof(message));
160 get_remote_process().init(message.mmalloc_default_mdp, message.maxpid, message.actors, message.dead_actors);
164 case MessageType::IGNORE_HEAP: {
165 s_mc_message_ignore_heap_t message;
166 xbt_assert(size == sizeof(message), "Broken message");
167 memcpy(&message, buffer, sizeof(message));
169 IgnoredHeapRegion region;
170 region.block = message.block;
171 region.fragment = message.fragment;
172 region.address = message.address;
173 region.size = message.size;
174 get_remote_process().ignore_heap(region);
178 case MessageType::UNIGNORE_HEAP: {
179 s_mc_message_ignore_memory_t message;
180 xbt_assert(size == sizeof(message), "Broken message");
181 memcpy(&message, buffer, sizeof(message));
182 get_remote_process().unignore_heap((void*)(std::uintptr_t)message.addr, message.size);
186 case MessageType::IGNORE_MEMORY: {
187 s_mc_message_ignore_memory_t message;
188 xbt_assert(size == sizeof(message), "Broken message");
189 memcpy(&message, buffer, sizeof(message));
190 this->get_remote_process().ignore_region(message.addr, message.size);
194 case MessageType::STACK_REGION: {
195 s_mc_message_stack_region_t message;
196 xbt_assert(size == sizeof(message), "Broken message");
197 memcpy(&message, buffer, sizeof(message));
198 this->get_remote_process().stack_areas().push_back(message.stack_region);
201 case MessageType::REGISTER_SYMBOL: {
202 s_mc_message_register_symbol_t message;
203 xbt_assert(size == sizeof(message), "Broken message");
204 memcpy(&message, buffer, sizeof(message));
205 xbt_assert(not message.callback, "Support for client-side function proposition is not implemented.");
206 XBT_DEBUG("Received symbol: %s", message.name.data());
208 if (property_automaton == nullptr)
209 property_automaton = xbt_automaton_new();
211 const RemoteProcess* process = &this->get_remote_process();
212 RemotePtr<int> address = remote((int*)message.data);
213 xbt::add_proposition(property_automaton, message.name.data(),
214 [process, address]() { return process->read(address); });
219 case MessageType::WAITING:
222 case MessageType::ASSERTION_FAILED:
223 XBT_INFO("**************************");
224 XBT_INFO("*** PROPERTY NOT VALID ***");
225 XBT_INFO("**************************");
226 XBT_INFO("Counter-example execution trace:");
227 for (auto const& s : getChecker()->get_textual_trace())
228 XBT_INFO(" %s", s.c_str());
230 session_singleton->log_state();
232 this->exit(SIMGRID_MC_EXIT_SAFETY);
235 xbt_die("Unexpected message from model-checked application");
240 /** Terminate the model-checker application */
241 void ModelChecker::exit(int status)
247 void ModelChecker::handle_waitpid()
249 XBT_DEBUG("Check for wait event");
252 while ((pid = waitpid(-1, &status, WNOHANG)) != 0) {
254 if (errno == ECHILD) {
256 xbt_assert(not this->get_remote_process().running(), "Inconsistent state");
259 XBT_ERROR("Could not wait for pid");
260 throw simgrid::xbt::errno_error();
264 if (pid == this->get_remote_process().pid()) {
265 // From PTRACE_O_TRACEEXIT:
267 if (status>>8 == (SIGTRAP | (PTRACE_EVENT_EXIT<<8))) {
268 xbt_assert(ptrace(PTRACE_GETEVENTMSG, remote_process_->pid(), 0, &status) != -1, "Could not get exit status");
269 if (WIFSIGNALED(status)) {
270 MC_report_crash(status);
271 this->get_remote_process().terminate();
272 this->exit(SIMGRID_MC_EXIT_PROGRAM_CRASH);
277 // We don't care about signals, just reinject them:
278 if (WIFSTOPPED(status)) {
279 XBT_DEBUG("Stopped with signal %i", (int) WSTOPSIG(status));
282 ptrace(PTRACE_CONT, remote_process_->pid(), 0, WSTOPSIG(status));
284 ptrace(PT_CONTINUE, remote_process_->pid(), (caddr_t)1, WSTOPSIG(status));
286 xbt_assert(errno == 0, "Could not PTRACE_CONT");
289 else if (WIFSIGNALED(status)) {
290 MC_report_crash(status);
291 this->get_remote_process().terminate();
292 this->exit(SIMGRID_MC_EXIT_PROGRAM_CRASH);
293 } else if (WIFEXITED(status)) {
294 XBT_DEBUG("Child process is over");
295 this->get_remote_process().terminate();
301 void ModelChecker::wait_for_requests()
304 if (this->get_remote_process().running())
305 checker_side_.dispatch();
308 void ModelChecker::handle_simcall(Transition const& transition)
310 s_mc_message_simcall_handle_t m;
311 memset(&m, 0, sizeof(m));
312 m.type = MessageType::SIMCALL_HANDLE;
313 m.aid_ = transition.aid_;
314 m.times_considered_ = transition.times_considered_;
315 checker_side_.get_channel().send(m);
316 this->remote_process_->clear_cache();
317 if (this->remote_process_->running())
318 checker_side_.dispatch();
320 bool ModelChecker::simcall_is_visible(int aid)
322 xbt_assert(mc_model_checker != nullptr, "This should be called from the checker side");
324 s_mc_message_simcall_is_visible_t m;
325 memset(&m, 0, sizeof(m));
326 m.type = MessageType::SIMCALL_IS_VISIBLE;
328 checker_side_.get_channel().send(m);
330 s_mc_message_simcall_is_visible_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_IS_VISIBLE_ANSWER,
334 "Received unexpected message %s (%i, size=%i) "
335 "expected MessageType::SIMCALL_IS_VISIBLE_ANSWER (%i, size=%i)",
336 to_c_str(answer.type), (int)answer.type, (int)s, (int)MessageType::SIMCALL_IS_VISIBLE_ANSWER,
337 (int)sizeof(answer));
339 XBT_DEBUG("is_visible(%d) is returning %s", aid, answer.value ? "true" : "false");
341 this->remote_process_->clear_cache();
345 std::string ModelChecker::simcall_to_string(MessageType type, int aid, int times_considered)
347 xbt_assert(mc_model_checker != nullptr, "This should be called from the checker side");
349 s_mc_message_simcall_to_string_t m;
350 memset(&m, 0, sizeof(m));
353 m.time_considered = times_considered;
354 checker_side_.get_channel().send(m);
356 s_mc_message_simcall_to_string_answer_t answer;
357 ssize_t s = checker_side_.get_channel().receive(answer);
358 xbt_assert(s != -1, "Could not receive message");
359 xbt_assert(s == sizeof(answer) && answer.type == MessageType::SIMCALL_TO_STRING_ANSWER,
360 "Received unexpected message %s (%i, size=%i) "
361 "expected MessageType::SIMCALL_TO_STRING_ANSWER (%i, size=%i)",
362 to_c_str(answer.type), (int)answer.type, (int)s, (int)MessageType::SIMCALL_TO_STRING_ANSWER,
363 (int)sizeof(answer));
365 return std::string(answer.value);
368 std::string ModelChecker::simcall_to_string(int aid, int times_considered)
370 std::string answer = simcall_to_string(MessageType::SIMCALL_TO_STRING, aid, times_considered);
371 XBT_DEBUG("to_string(%d) is returning %s", aid, answer.c_str());
375 std::string ModelChecker::simcall_dot_label(int aid, int times_considered)
377 std::string answer = simcall_to_string(MessageType::SIMCALL_DOT_LABEL, aid, times_considered);
378 XBT_DEBUG("dot_label(%d) is returning %s", aid, answer.c_str());
382 void ModelChecker::finalize_app(bool terminate_asap)
384 s_mc_message_int_t m;
385 memset(&m, 0, sizeof m);
386 m.type = MessageType::FINALIZE;
387 m.value = terminate_asap;
388 xbt_assert(checker_side_.get_channel().send(m) == 0, "Could not ask the app to finalize on need");
390 s_mc_message_t answer;
391 xbt_assert(checker_side_.get_channel().receive(answer) != -1, "Could not receive answer to FINALIZE");
394 bool ModelChecker::checkDeadlock()
396 xbt_assert(checker_side_.get_channel().send(MessageType::DEADLOCK_CHECK) == 0, "Could not check deadlock state");
397 s_mc_message_int_t message;
398 ssize_t s = checker_side_.get_channel().receive(message);
399 xbt_assert(s != -1, "Could not receive message");
400 xbt_assert(s == sizeof(message) && message.type == MessageType::DEADLOCK_CHECK_REPLY,
401 "Received unexpected message %s (%i, size=%i) "
402 "expected MessageType::DEADLOCK_CHECK_REPLY (%i, size=%i)",
403 to_c_str(message.type), (int)message.type, (int)s, (int)MessageType::DEADLOCK_CHECK_REPLY,
404 (int)sizeof(message));
405 return message.value != 0;
409 } // namespace simgrid