1 /* Copyright (c) 2008-2019. 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/RemoteClient.hpp"
14 #include "xbt/automaton.hpp"
15 #include "xbt/system_error.hpp"
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_ModelChecker, mc, "ModelChecker");
21 ::simgrid::mc::ModelChecker* mc_model_checker = nullptr;
23 using simgrid::mc::remote;
26 # define WAITPID_CHECKED_FLAGS __WALL
28 # define WAITPID_CHECKED_FLAGS 0
34 ModelChecker::ModelChecker(std::unique_ptr<RemoteClient> process)
36 , socket_event_(nullptr)
37 , signal_event_(nullptr)
39 , process_(std::move(process))
44 ModelChecker::~ModelChecker() {
45 if (socket_event_ != nullptr)
46 event_free(socket_event_);
47 if (signal_event_ != nullptr)
48 event_free(signal_event_);
50 event_base_free(base_);
53 void ModelChecker::start()
55 base_ = event_base_new();
56 event_callback_fn event_callback = [](evutil_socket_t fd, short events, void *arg)
58 ((ModelChecker *)arg)->handle_events(fd, events);
60 socket_event_ = event_new(base_, process_->get_channel().get_socket(), EV_READ | EV_PERSIST, event_callback, this);
61 event_add(socket_event_, NULL);
62 signal_event_ = event_new(base_,
65 event_callback, this);
66 event_add(signal_event_, NULL);
68 XBT_DEBUG("Waiting for the model-checked process");
71 // The model-checked process SIGSTOP itself to signal it's ready:
72 const pid_t pid = process_->pid();
74 pid_t res = waitpid(pid, &status, WUNTRACED | WAITPID_CHECKED_FLAGS);
75 if (res < 0 || not WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP)
76 xbt_die("Could not wait model-checked process");
80 if (not _sg_mc_dot_output_file.get().empty())
85 if (kill(pid, SIGCONT) != 0)
86 throw simgrid::xbt::errno_error("Could not wake up the model-checked process");
89 static const std::pair<const char*, const char*> ignored_local_variables[] = {
90 std::pair<const char*, const char*>{ "e", "*" },
91 std::pair<const char*, const char*>{ "_log_ev", "*" },
93 /* Ignore local variable about time used for tracing */
94 std::pair<const char*, const char*>{ "start_time", "*" },
97 void ModelChecker::setup_ignore()
99 RemoteClient& process = this->process();
100 for (std::pair<const char*, const char*> const& var :
101 ignored_local_variables)
102 process.ignore_local_variable(var.first, var.second);
104 /* Static variable used for tracing */
105 process.ignore_global_variable("counter");
108 void ModelChecker::shutdown()
110 XBT_DEBUG("Shuting down model-checker");
112 simgrid::mc::RemoteClient* process = &this->process();
113 if (process->running()) {
114 XBT_DEBUG("Killing process");
115 kill(process->pid(), SIGKILL);
116 process->terminate();
120 void ModelChecker::resume(simgrid::mc::RemoteClient& process)
122 int res = process.get_channel().send(MC_MESSAGE_CONTINUE);
124 throw simgrid::xbt::errno_error();
125 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 (WCOREDUMP(status))
138 XBT_INFO("A core dump was generated by the system.");
140 XBT_INFO("No core dump was generated by the system.");
141 XBT_INFO("Counter-example execution trace:");
142 for (auto const& s : mc_model_checker->getChecker()->get_textual_trace())
143 XBT_INFO(" %s", s.c_str());
144 simgrid::mc::dumpRecordPath();
145 simgrid::mc::session->log_state();
146 XBT_INFO("Stack trace:");
147 mc_model_checker->process().dump_stack();
150 static void MC_report_assertion_error()
152 XBT_INFO("**************************");
153 XBT_INFO("*** PROPERTY NOT VALID ***");
154 XBT_INFO("**************************");
155 XBT_INFO("Counter-example execution trace:");
156 for (auto const& s : mc_model_checker->getChecker()->get_textual_trace())
157 XBT_INFO(" %s", s.c_str());
158 simgrid::mc::dumpRecordPath();
159 simgrid::mc::session->log_state();
162 bool ModelChecker::handle_message(char* buffer, ssize_t size)
164 s_mc_message_t base_message;
165 if (size < (ssize_t) sizeof(base_message))
166 xbt_die("Broken message");
167 memcpy(&base_message, buffer, sizeof(base_message));
169 switch(base_message.type) {
171 case MC_MESSAGE_IGNORE_HEAP:
173 s_mc_message_ignore_heap_t message;
174 if (size != sizeof(message))
175 xbt_die("Broken messsage");
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 process().ignore_heap(region);
187 case MC_MESSAGE_UNIGNORE_HEAP:
189 s_mc_message_ignore_memory_t message;
190 if (size != sizeof(message))
191 xbt_die("Broken messsage");
192 memcpy(&message, buffer, sizeof(message));
193 process().unignore_heap((void*)(std::uintptr_t)message.addr, message.size);
197 case MC_MESSAGE_IGNORE_MEMORY:
199 s_mc_message_ignore_memory_t message;
200 if (size != sizeof(message))
201 xbt_die("Broken messsage");
202 memcpy(&message, buffer, sizeof(message));
203 this->process().ignore_region(message.addr, message.size);
207 case MC_MESSAGE_STACK_REGION:
209 s_mc_message_stack_region_t message;
210 if (size != sizeof(message))
211 xbt_die("Broken messsage");
212 memcpy(&message, buffer, sizeof(message));
213 this->process().stack_areas().push_back(message.stack_region);
217 case MC_MESSAGE_REGISTER_SYMBOL:
219 s_mc_message_register_symbol_t message;
220 if (size != sizeof(message))
221 xbt_die("Broken message");
222 memcpy(&message, buffer, sizeof(message));
223 if (message.callback)
224 xbt_die("Support for client-side function proposition is not implemented.");
225 XBT_DEBUG("Received symbol: %s", message.name);
227 if (simgrid::mc::property_automaton == nullptr)
228 simgrid::mc::property_automaton = xbt_automaton_new();
230 simgrid::mc::RemoteClient* process = &this->process();
231 simgrid::mc::RemotePtr<int> address = simgrid::mc::remote((int*)message.data);
232 simgrid::xbt::add_proposition(simgrid::mc::property_automaton, message.name,
233 [process, address]() { return process->read(address); });
238 case MC_MESSAGE_WAITING:
241 case MC_MESSAGE_ASSERTION_FAILED:
242 MC_report_assertion_error();
243 this->exit(SIMGRID_MC_EXIT_SAFETY);
246 xbt_die("Unexpected message from model-checked application");
252 /** Terminate the model-checker application */
253 void ModelChecker::exit(int status)
255 // TODO, terminate the model checker politely instead of exiting rudely
256 if (process().running())
257 kill(process().pid(), SIGKILL);
261 void ModelChecker::handle_events(int fd, short events)
263 if (events == EV_READ) {
264 char buffer[MC_MESSAGE_LENGTH];
265 ssize_t size = process_->get_channel().receive(buffer, sizeof(buffer), false);
266 if (size == -1 && errno != EAGAIN)
267 throw simgrid::xbt::errno_error();
268 if (not handle_message(buffer, size)) {
269 event_base_loopbreak(base_);
272 else if (events == EV_SIGNAL) {
276 xbt_die("Unexpected event");
280 void ModelChecker::loop()
282 if (this->process().running())
283 event_base_dispatch(base_);
286 void ModelChecker::handle_waitpid()
288 XBT_DEBUG("Check for wait event");
291 while ((pid = waitpid(-1, &status, WNOHANG)) != 0) {
293 if (errno == ECHILD) {
295 if (this->process().running())
296 xbt_die("Inconsistent state");
300 XBT_ERROR("Could not wait for pid");
301 throw simgrid::xbt::errno_error();
305 if (pid == this->process().pid()) {
306 xbt_assert(WIFEXITED(status) || WIFSIGNALED(status));
307 XBT_DEBUG("Child process is over");
308 this->process().terminate();
313 void ModelChecker::on_signal(int signo)
315 if (signo == SIGCHLD)
316 this->handle_waitpid();
319 void ModelChecker::wait_for_requests()
321 this->resume(process());
322 if (this->process().running())
323 event_base_dispatch(base_);
326 void ModelChecker::handle_simcall(Transition const& transition)
328 s_mc_message_simcall_handle_t m;
329 memset(&m, 0, sizeof(m));
330 m.type = MC_MESSAGE_SIMCALL_HANDLE;
331 m.pid = transition.pid;
332 m.value = transition.argument;
333 this->process_->get_channel().send(m);
334 this->process_->clear_cache();
335 if (this->process_->running())
336 event_base_dispatch(base_);
339 bool ModelChecker::checkDeadlock()
342 if ((res = this->process().get_channel().send(MC_MESSAGE_DEADLOCK_CHECK)))
343 xbt_die("Could not check deadlock state");
344 s_mc_message_int_t message;
345 ssize_t s = mc_model_checker->process().get_channel().receive(message);
347 xbt_die("Could not receive message");
348 if (s != sizeof(message) || message.type != MC_MESSAGE_DEADLOCK_CHECK_REPLY)
349 xbt_die("Received unexpected message %s (%i, size=%i) "
350 "expected MC_MESSAGE_DEADLOCK_CHECK_REPLY (%i, size=%i)",
351 MC_message_type_name(message.type), (int) message.type, (int) s,
352 (int) MC_MESSAGE_DEADLOCK_CHECK_REPLY, (int) sizeof(message)
354 return message.value != 0;