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/mc_liveness.h"
37 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_ModelChecker, mc, "ModelChecker");
41 ::simgrid::mc::ModelChecker* mc_model_checker = nullptr;
43 using simgrid::mc::remote;
45 // Hardcoded index for now:
46 #define SOCKET_FD_INDEX 0
47 #define SIGNAL_FD_INDEX 1
52 ModelChecker::ModelChecker(std::unique_ptr<Process> process) :
53 hostnames_(xbt_dict_new()),
55 process_(std::move(process)),
56 parent_snapshot_(nullptr)
61 ModelChecker::~ModelChecker()
63 xbt_dict_free(&this->hostnames_);
66 const char* ModelChecker::get_host_name(const char* hostname)
68 // Lookup the host name in the dictionary (or create it):
69 xbt_dictelm_t elt = xbt_dict_get_elm_or_null(this->hostnames_, hostname);
71 xbt_dict_set(this->hostnames_, hostname, nullptr, nullptr);
72 elt = xbt_dict_get_elm_or_null(this->hostnames_, hostname);
78 void ModelChecker::start()
80 const pid_t pid = process_->pid();
82 // Block SIGCHLD (this will be handled with accept/signalfd):
85 sigaddset(&set, SIGCHLD);
86 if (sigprocmask(SIG_BLOCK, &set, nullptr) == -1)
87 throw simgrid::xbt::errno_error(errno);
90 sigfillset(&full_set);
92 // Prepare data for poll:
94 struct pollfd* socket_pollfd = &fds_[SOCKET_FD_INDEX];
95 socket_pollfd->fd = process_->socket();;
96 socket_pollfd->events = POLLIN;
97 socket_pollfd->revents = 0;
99 int signal_fd = signalfd(-1, &set, 0);
101 throw simgrid::xbt::errno_error(errno);
103 struct pollfd* signalfd_pollfd = &fds_[SIGNAL_FD_INDEX];
104 signalfd_pollfd->fd = signal_fd;
105 signalfd_pollfd->events = POLLIN;
106 signalfd_pollfd->revents = 0;
108 XBT_DEBUG("Waiting for the model-checked process");
111 // The model-checked process SIGSTOP itself to signal it's ready:
112 pid_t res = waitpid(pid, &status, __WALL);
113 if (res < 0 || !WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP)
114 xbt_die("Could not wait model-checked process");
118 /* Initialize statistics */
119 mc_stats = xbt_new0(s_mc_stats_t, 1);
120 mc_stats->state_size = 1;
122 if ((_sg_mc_dot_output_file != nullptr) && (_sg_mc_dot_output_file[0] != '\0'))
123 MC_init_dot_output();
126 //parmap = xbt_parmap_mc_new(xbt_os_get_numcores(), XBT_PARMAP_DEFAULT);
130 ptrace(PTRACE_SETOPTIONS, pid, nullptr, PTRACE_O_TRACEEXIT);
131 ptrace(PTRACE_CONT, pid, 0, 0);
134 static const std::pair<const char*, const char*> ignored_local_variables[] = {
135 std::pair<const char*, const char*>{ "e", "*" },
136 std::pair<const char*, const char*>{ "__ex_cleanup", "*" },
137 std::pair<const char*, const char*>{ "__ex_mctx_en", "*" },
138 std::pair<const char*, const char*>{ "__ex_mctx_me", "*" },
139 std::pair<const char*, const char*>{ "__xbt_ex_ctx_ptr", "*" },
140 std::pair<const char*, const char*>{ "_log_ev", "*" },
141 std::pair<const char*, const char*>{ "_throw_ctx", "*" },
142 std::pair<const char*, const char*>{ "ctx", "*" },
144 std::pair<const char*, const char*>{ "self", "simcall_BODY_mc_snapshot" },
145 std::pair<const char*, const char*>{ "next_context", "smx_ctx_sysv_suspend_serial" },
146 std::pair<const char*, const char*>{ "i", "smx_ctx_sysv_suspend_serial" },
148 /* Ignore local variable about time used for tracing */
149 std::pair<const char*, const char*>{ "start_time", "*" },
152 void ModelChecker::setup_ignore()
154 Process& process = this->process();
155 for (std::pair<const char*, const char*> const& var :
156 ignored_local_variables)
157 process.ignore_local_variable(var.first, var.second);
159 /* Static variable used for tracing */
160 process.ignore_global_variable("counter");
163 process.ignore_global_variable("smx_total_comms");
166 void ModelChecker::shutdown()
168 XBT_DEBUG("Shuting down model-checker");
170 simgrid::mc::Process* process = &this->process();
171 if (process->running()) {
172 XBT_DEBUG("Killing process");
173 kill(process->pid(), SIGTERM);
174 process->terminate();
178 void ModelChecker::resume(simgrid::mc::Process& process)
180 int res = process.send_message(MC_MESSAGE_CONTINUE);
182 throw simgrid::xbt::errno_error(res);
183 process.cache_flags = (mc_process_cache_flags_t) 0;
187 void throw_socket_error(int fd)
190 socklen_t errlen = sizeof(error);
191 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *)&error, &errlen) == -1)
193 throw simgrid::xbt::errno_error(errno);
196 bool ModelChecker::handle_message(char* buffer, ssize_t size)
198 s_mc_message_t base_message;
199 if (size < (ssize_t) sizeof(base_message))
200 xbt_die("Broken message");
201 memcpy(&base_message, buffer, sizeof(base_message));
203 switch(base_message.type) {
205 case MC_MESSAGE_IGNORE_HEAP:
207 s_mc_ignore_heap_message_t message;
208 if (size != sizeof(message))
209 xbt_die("Broken messsage");
210 memcpy(&message, buffer, sizeof(message));
212 IgnoredHeapRegion region;
213 region.block = message.block;
214 region.fragment = message.fragment;
215 region.address = message.address;
216 region.size = message.size;
217 process().ignore_heap(region);
221 case MC_MESSAGE_UNIGNORE_HEAP:
223 s_mc_ignore_memory_message_t message;
224 if (size != sizeof(message))
225 xbt_die("Broken messsage");
226 memcpy(&message, buffer, sizeof(message));
227 process().unignore_heap(
228 (void *)(std::uintptr_t) message.addr, message.size);
232 case MC_MESSAGE_IGNORE_MEMORY:
234 s_mc_ignore_memory_message_t message;
235 if (size != sizeof(message))
236 xbt_die("Broken messsage");
237 memcpy(&message, buffer, sizeof(message));
238 this->process().ignore_region(message.addr, message.size);
242 case MC_MESSAGE_STACK_REGION:
244 s_mc_stack_region_message_t message;
245 if (size != sizeof(message))
246 xbt_die("Broken messsage");
247 memcpy(&message, buffer, sizeof(message));
248 this->process().stack_areas().push_back(message.stack_region);
252 case MC_MESSAGE_REGISTER_SYMBOL:
254 s_mc_register_symbol_message_t message;
255 if (size != sizeof(message))
256 xbt_die("Broken message");
257 memcpy(&message, buffer, sizeof(message));
258 if (message.callback)
259 xbt_die("Support for client-side function proposition is not implemented.");
260 XBT_DEBUG("Received symbol: %s", message.name);
262 if (_mc_property_automaton == nullptr)
263 _mc_property_automaton = xbt_automaton_new();
265 simgrid::mc::Process* process = &this->process();
266 simgrid::mc::remote_ptr<int> address
267 = simgrid::mc::remote((int*) message.data);
268 simgrid::xbt::add_proposition(_mc_property_automaton,
270 [process, address]() { return process->read(address); }
276 case MC_MESSAGE_WAITING:
279 case MC_MESSAGE_ASSERTION_FAILED:
280 MC_report_assertion_error();
281 this->exit(SIMGRID_MC_EXIT_SAFETY);
285 xbt_die("Unexpected message from model-checked application");
291 /** Terminate the model-checker aplication */
292 void ModelChecker::exit(int status)
294 // TODO, terminate the model checker politely instead of exiting rudel
295 if (process().running())
296 kill(process().pid(), SIGKILL);
300 bool ModelChecker::handle_events()
302 char buffer[MC_MESSAGE_LENGTH];
303 struct pollfd* socket_pollfd = &fds_[SOCKET_FD_INDEX];
304 struct pollfd* signalfd_pollfd = &fds_[SIGNAL_FD_INDEX];
306 while(poll(fds_, 2, -1) == -1) {
311 throw simgrid::xbt::errno_error(errno);
315 if (socket_pollfd->revents) {
316 if (socket_pollfd->revents & POLLIN) {
317 ssize_t size = MC_receive_message(socket_pollfd->fd, buffer, sizeof(buffer), MSG_DONTWAIT);
318 if (size == -1 && errno != EAGAIN)
319 throw simgrid::xbt::errno_error(errno);
320 return handle_message(buffer, size);
322 if (socket_pollfd->revents & POLLERR) {
323 throw_socket_error(socket_pollfd->fd);
325 if (socket_pollfd->revents & POLLHUP)
326 xbt_die("Socket hang up?");
329 if (signalfd_pollfd->revents) {
330 if (signalfd_pollfd->revents & POLLIN) {
331 this->handle_signals();
334 if (signalfd_pollfd->revents & POLLERR) {
335 throw_socket_error(signalfd_pollfd->fd);
337 if (signalfd_pollfd->revents & POLLHUP)
338 xbt_die("Signalfd hang up?");
344 void ModelChecker::loop()
346 while (this->process().running())
347 this->handle_events();
350 void ModelChecker::handle_signals()
352 struct signalfd_siginfo info;
353 struct pollfd* signalfd_pollfd = &fds_[SIGNAL_FD_INDEX];
355 ssize_t size = read(signalfd_pollfd->fd, &info, sizeof(info));
360 throw simgrid::xbt::errno_error(errno);
361 } else if (size != sizeof(info))
362 return throw std::runtime_error(
363 "Bad communication with model-checked application");
367 this->on_signal(&info);
370 void ModelChecker::handle_waitpid()
372 XBT_DEBUG("Check for wait event");
375 while ((pid = waitpid(-1, &status, WNOHANG)) != 0) {
377 if (errno == ECHILD) {
379 if (this->process().running())
380 xbt_die("Inconsistent state");
384 XBT_ERROR("Could not wait for pid");
385 throw simgrid::xbt::errno_error(errno);
389 if (pid == this->process().pid()) {
391 // From PTRACE_O_TRACEEXIT:
392 if (status>>8 == (SIGTRAP | (PTRACE_EVENT_EXIT<<8))) {
393 if (ptrace(PTRACE_GETEVENTMSG, this->process().pid(), 0, &status) == -1)
394 xbt_die("Could not get exit status");
395 if (WIFSIGNALED(status)) {
396 MC_report_crash(status);
397 mc_model_checker->exit(SIMGRID_MC_EXIT_PROGRAM_CRASH);
401 // We don't care about signals, just reinject them:
402 if (WIFSTOPPED(status)) {
403 XBT_DEBUG("Stopped with signal %i", (int) WSTOPSIG(status));
404 if (ptrace(PTRACE_CONT, this->process().pid(), 0, WSTOPSIG(status)) == -1)
405 xbt_die("Could not PTRACE_CONT");
408 else if (WIFEXITED(status) || WIFSIGNALED(status)) {
409 XBT_DEBUG("Child process is over");
410 this->process().terminate();
416 void ModelChecker::on_signal(const struct signalfd_siginfo* info)
418 switch(info->ssi_signo) {
420 this->handle_waitpid();
427 void ModelChecker::wait_client(simgrid::mc::Process& process)
429 this->resume(process);
430 while (this->process().running()) {
431 if (!this->handle_events())
436 void ModelChecker::simcall_handle(simgrid::mc::Process& process, unsigned long pid, int value)
438 s_mc_simcall_handle_message m;
439 memset(&m, 0, sizeof(m));
440 m.type = MC_MESSAGE_SIMCALL_HANDLE;
443 process.send_message(m);
444 process.cache_flags = (mc_process_cache_flags_t) 0;
445 while (process.running()) {
446 if (!this->handle_events())