1 /* Copyright (c) 2008-2015. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include <sys/types.h>
12 #include <sys/socket.h>
13 #include <sys/signalfd.h>
14 #include <sys/ptrace.h>
17 #include <system_error>
20 #include <xbt/automaton.h>
21 #include <xbt/automaton.hpp>
22 #include <xbt/system_error.hpp>
24 #include "simgrid/sg_config.h"
26 #include "src/mc/ModelChecker.hpp"
27 #include "src/mc/PageStore.hpp"
28 #include "src/mc/ModelChecker.hpp"
29 #include "src/mc/mc_protocol.h"
30 #include "src/mc/mc_private.h"
31 #include "src/mc/mc_ignore.h"
32 #include "src/mc/mc_exit.h"
33 #include "src/mc/LivenessChecker.hpp"
35 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_ModelChecker, mc, "ModelChecker");
37 ::simgrid::mc::ModelChecker* mc_model_checker = nullptr;
39 using simgrid::mc::remote;
41 // Hardcoded index for now:
42 #define SOCKET_FD_INDEX 0
43 #define SIGNAL_FD_INDEX 1
48 ModelChecker::ModelChecker(std::unique_ptr<Process> process) :
49 hostnames_(xbt_dict_new()),
51 process_(std::move(process)),
52 parent_snapshot_(nullptr)
57 ModelChecker::~ModelChecker()
59 xbt_dict_free(&this->hostnames_);
62 const char* ModelChecker::get_host_name(const char* hostname)
64 // Lookup the host name in the dictionary (or create it):
65 xbt_dictelm_t elt = xbt_dict_get_elm_or_null(this->hostnames_, hostname);
67 xbt_dict_set(this->hostnames_, hostname, nullptr, nullptr);
68 elt = xbt_dict_get_elm_or_null(this->hostnames_, hostname);
74 void ModelChecker::start()
76 const pid_t pid = process_->pid();
78 // Block SIGCHLD (this will be handled with accept/signalfd):
81 sigaddset(&set, SIGCHLD);
82 if (sigprocmask(SIG_BLOCK, &set, nullptr) == -1)
83 throw simgrid::xbt::errno_error(errno);
86 sigfillset(&full_set);
88 // Prepare data for poll:
90 struct pollfd* socket_pollfd = &fds_[SOCKET_FD_INDEX];
91 socket_pollfd->fd = process_->getChannel().getSocket();
92 socket_pollfd->events = POLLIN;
93 socket_pollfd->revents = 0;
95 int signal_fd = signalfd(-1, &set, 0);
97 throw simgrid::xbt::errno_error(errno);
99 struct pollfd* signalfd_pollfd = &fds_[SIGNAL_FD_INDEX];
100 signalfd_pollfd->fd = signal_fd;
101 signalfd_pollfd->events = POLLIN;
102 signalfd_pollfd->revents = 0;
104 XBT_DEBUG("Waiting for the model-checked process");
107 // The model-checked process SIGSTOP itself to signal it's ready:
108 pid_t res = waitpid(pid, &status, __WALL);
109 if (res < 0 || !WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP)
110 xbt_die("Could not wait model-checked process");
114 /* Initialize statistics */
115 mc_stats = xbt_new0(s_mc_stats_t, 1);
116 mc_stats->state_size = 1;
118 if ((_sg_mc_dot_output_file != nullptr) && (_sg_mc_dot_output_file[0] != '\0'))
119 MC_init_dot_output();
123 ptrace(PTRACE_SETOPTIONS, pid, nullptr, PTRACE_O_TRACEEXIT);
124 ptrace(PTRACE_CONT, pid, 0, 0);
127 static const std::pair<const char*, const char*> ignored_local_variables[] = {
128 std::pair<const char*, const char*>{ "e", "*" },
129 std::pair<const char*, const char*>{ "__ex_cleanup", "*" },
130 std::pair<const char*, const char*>{ "__ex_mctx_en", "*" },
131 std::pair<const char*, const char*>{ "__ex_mctx_me", "*" },
132 std::pair<const char*, const char*>{ "__xbt_ex_ctx_ptr", "*" },
133 std::pair<const char*, const char*>{ "_log_ev", "*" },
134 std::pair<const char*, const char*>{ "_throw_ctx", "*" },
135 std::pair<const char*, const char*>{ "ctx", "*" },
137 std::pair<const char*, const char*>{ "self", "simcall_BODY_mc_snapshot" },
138 std::pair<const char*, const char*>{ "next_context", "smx_ctx_sysv_suspend_serial" },
139 std::pair<const char*, const char*>{ "i", "smx_ctx_sysv_suspend_serial" },
141 /* Ignore local variable about time used for tracing */
142 std::pair<const char*, const char*>{ "start_time", "*" },
145 void ModelChecker::setup_ignore()
147 Process& process = this->process();
148 for (std::pair<const char*, const char*> const& var :
149 ignored_local_variables)
150 process.ignore_local_variable(var.first, var.second);
152 /* Static variable used for tracing */
153 process.ignore_global_variable("counter");
156 process.ignore_global_variable("smx_total_comms");
159 void ModelChecker::shutdown()
161 XBT_DEBUG("Shuting down model-checker");
163 simgrid::mc::Process* process = &this->process();
164 if (process->running()) {
165 XBT_DEBUG("Killing process");
166 kill(process->pid(), SIGTERM);
167 process->terminate();
171 void ModelChecker::resume(simgrid::mc::Process& process)
173 int res = process.getChannel().send(MC_MESSAGE_CONTINUE);
175 throw simgrid::xbt::errno_error(res);
176 process.clear_cache();
180 void throw_socket_error(int fd)
183 socklen_t errlen = sizeof(error);
184 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *)&error, &errlen) == -1)
186 throw simgrid::xbt::errno_error(errno);
189 bool ModelChecker::handle_message(char* buffer, ssize_t size)
191 s_mc_message_t base_message;
192 if (size < (ssize_t) sizeof(base_message))
193 xbt_die("Broken message");
194 memcpy(&base_message, buffer, sizeof(base_message));
196 switch(base_message.type) {
198 case MC_MESSAGE_IGNORE_HEAP:
200 s_mc_ignore_heap_message_t message;
201 if (size != sizeof(message))
202 xbt_die("Broken messsage");
203 memcpy(&message, buffer, sizeof(message));
205 IgnoredHeapRegion region;
206 region.block = message.block;
207 region.fragment = message.fragment;
208 region.address = message.address;
209 region.size = message.size;
210 process().ignore_heap(region);
214 case MC_MESSAGE_UNIGNORE_HEAP:
216 s_mc_ignore_memory_message_t message;
217 if (size != sizeof(message))
218 xbt_die("Broken messsage");
219 memcpy(&message, buffer, sizeof(message));
220 process().unignore_heap(
221 (void *)(std::uintptr_t) message.addr, message.size);
225 case MC_MESSAGE_IGNORE_MEMORY:
227 s_mc_ignore_memory_message_t message;
228 if (size != sizeof(message))
229 xbt_die("Broken messsage");
230 memcpy(&message, buffer, sizeof(message));
231 this->process().ignore_region(message.addr, message.size);
235 case MC_MESSAGE_STACK_REGION:
237 s_mc_stack_region_message_t message;
238 if (size != sizeof(message))
239 xbt_die("Broken messsage");
240 memcpy(&message, buffer, sizeof(message));
241 this->process().stack_areas().push_back(message.stack_region);
245 case MC_MESSAGE_REGISTER_SYMBOL:
247 s_mc_register_symbol_message_t message;
248 if (size != sizeof(message))
249 xbt_die("Broken message");
250 memcpy(&message, buffer, sizeof(message));
251 if (message.callback)
252 xbt_die("Support for client-side function proposition is not implemented.");
253 XBT_DEBUG("Received symbol: %s", message.name);
255 if (simgrid::mc::property_automaton == nullptr)
256 simgrid::mc::property_automaton = xbt_automaton_new();
258 simgrid::mc::Process* process = &this->process();
259 simgrid::mc::RemotePtr<int> address
260 = simgrid::mc::remote((int*) message.data);
261 simgrid::xbt::add_proposition(simgrid::mc::property_automaton,
263 [process, address]() { return process->read(address); }
269 case MC_MESSAGE_WAITING:
272 case MC_MESSAGE_ASSERTION_FAILED:
273 MC_report_assertion_error();
274 this->exit(SIMGRID_MC_EXIT_SAFETY);
278 xbt_die("Unexpected message from model-checked application");
284 /** Terminate the model-checker aplication */
285 void ModelChecker::exit(int status)
287 // TODO, terminate the model checker politely instead of exiting rudel
288 if (process().running())
289 kill(process().pid(), SIGKILL);
293 bool ModelChecker::handle_events()
295 char buffer[MC_MESSAGE_LENGTH];
296 struct pollfd* socket_pollfd = &fds_[SOCKET_FD_INDEX];
297 struct pollfd* signalfd_pollfd = &fds_[SIGNAL_FD_INDEX];
299 while(poll(fds_, 2, -1) == -1) {
304 throw simgrid::xbt::errno_error(errno);
308 if (socket_pollfd->revents) {
309 if (socket_pollfd->revents & POLLIN) {
310 ssize_t size = process_->getChannel().receive(buffer, sizeof(buffer), false);
311 if (size == -1 && errno != EAGAIN)
312 throw simgrid::xbt::errno_error(errno);
313 return handle_message(buffer, size);
315 if (socket_pollfd->revents & POLLERR)
316 throw_socket_error(socket_pollfd->fd);
317 if (socket_pollfd->revents & POLLHUP)
318 xbt_die("Socket hang up?");
321 if (signalfd_pollfd->revents) {
322 if (signalfd_pollfd->revents & POLLIN) {
323 this->handle_signals();
326 if (signalfd_pollfd->revents & POLLERR)
327 throw_socket_error(signalfd_pollfd->fd);
328 if (signalfd_pollfd->revents & POLLHUP)
329 xbt_die("Signalfd hang up?");
335 void ModelChecker::loop()
337 while (this->process().running())
338 this->handle_events();
341 void ModelChecker::handle_signals()
343 struct signalfd_siginfo info;
344 struct pollfd* signalfd_pollfd = &fds_[SIGNAL_FD_INDEX];
346 ssize_t size = read(signalfd_pollfd->fd, &info, sizeof(info));
351 throw simgrid::xbt::errno_error(errno);
352 } else if (size != sizeof(info))
353 return throw std::runtime_error(
354 "Bad communication with model-checked application");
358 this->on_signal(&info);
361 void ModelChecker::handle_waitpid()
363 XBT_DEBUG("Check for wait event");
366 while ((pid = waitpid(-1, &status, WNOHANG)) != 0) {
368 if (errno == ECHILD) {
370 if (this->process().running())
371 xbt_die("Inconsistent state");
375 XBT_ERROR("Could not wait for pid");
376 throw simgrid::xbt::errno_error(errno);
380 if (pid == this->process().pid()) {
382 // From PTRACE_O_TRACEEXIT:
383 if (status>>8 == (SIGTRAP | (PTRACE_EVENT_EXIT<<8))) {
384 if (ptrace(PTRACE_GETEVENTMSG, this->process().pid(), 0, &status) == -1)
385 xbt_die("Could not get exit status");
386 if (WIFSIGNALED(status)) {
387 MC_report_crash(status);
388 mc_model_checker->exit(SIMGRID_MC_EXIT_PROGRAM_CRASH);
392 // We don't care about signals, just reinject them:
393 if (WIFSTOPPED(status)) {
394 XBT_DEBUG("Stopped with signal %i", (int) WSTOPSIG(status));
395 if (ptrace(PTRACE_CONT, this->process().pid(), 0, WSTOPSIG(status)) == -1)
396 xbt_die("Could not PTRACE_CONT");
399 else if (WIFEXITED(status) || WIFSIGNALED(status)) {
400 XBT_DEBUG("Child process is over");
401 this->process().terminate();
407 void ModelChecker::on_signal(const struct signalfd_siginfo* info)
409 switch(info->ssi_signo) {
411 this->handle_waitpid();
418 void ModelChecker::wait_client(simgrid::mc::Process& process)
420 this->resume(process);
421 while (this->process().running())
422 if (!this->handle_events())
426 void ModelChecker::simcall_handle(simgrid::mc::Process& process, unsigned long pid, int value)
428 s_mc_simcall_handle_message m;
429 memset(&m, 0, sizeof(m));
430 m.type = MC_MESSAGE_SIMCALL_HANDLE;
433 process.getChannel().send(m);
434 process.clear_cache();
435 while (process.running())
436 if (!this->handle_events())
440 bool ModelChecker::checkDeadlock()
443 if ((res = this->process().getChannel().send(MC_MESSAGE_DEADLOCK_CHECK)))
444 xbt_die("Could not check deadlock state");
445 s_mc_int_message_t message;
446 ssize_t s = mc_model_checker->process().getChannel().receive(message);
448 xbt_die("Could not receive message");
449 if (s != sizeof(message) || message.type != MC_MESSAGE_DEADLOCK_CHECK_REPLY)
450 xbt_die("%s received unexpected message %s (%i, size=%i) "
451 "expected MC_MESSAGE_DEADLOCK_CHECK_REPLY (%i, size=%i)",
452 MC_mode_name(mc_mode),
453 MC_message_type_name(message.type), (int) message.type, (int) s,
454 (int) MC_MESSAGE_DEADLOCK_CHECK_REPLY, (int) sizeof(message)
456 return message.value != 0;