1 /* Copyright (c) 2015-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/Session.hpp"
7 #include "src/mc/checker/Checker.hpp"
8 #include "src/mc/mc_config.hpp"
9 #include "src/mc/mc_private.hpp"
10 #include "src/mc/mc_state.hpp"
12 #include "xbt/system_error.hpp"
16 #include <sys/prctl.h>
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_Session, mc, "Model-checker session");
24 static void setup_child_environment(int socket)
27 // Make sure we do not outlive our parent:
30 if (sigprocmask(SIG_SETMASK, &mask, nullptr) < 0)
31 throw simgrid::xbt::errno_error("Could not unblock signals");
32 if (prctl(PR_SET_PDEATHSIG, SIGHUP) != 0)
33 throw simgrid::xbt::errno_error("Could not PR_SET_PDEATHSIG");
38 // Remove CLOEXEC in order to pass the socket to the exec-ed program:
39 int fdflags = fcntl(socket, F_GETFD, 0);
40 if (fdflags == -1 || fcntl(socket, F_SETFD, fdflags & ~FD_CLOEXEC) == -1)
41 throw simgrid::xbt::errno_error("Could not remove CLOEXEC for socket");
44 setenv(MC_ENV_VARIABLE, "1", 1);
46 // Disable lazy relocation in the model-checked process.
47 // We don't want the model-checked process to modify its .got.plt during
49 setenv("LC_BIND_NOW", "1", 1);
52 res = std::snprintf(buffer, sizeof(buffer), "%i", socket);
53 if ((size_t) res >= sizeof(buffer) || res == -1)
55 setenv(MC_ENV_SOCKET_FD, buffer, 1);
58 /** Execute some code in a forked process */
65 throw simgrid::xbt::errno_error("Could not fork model-checked process");
75 // The callback should catch exceptions:
80 Session::Session(pid_t pid, int socket)
82 std::unique_ptr<simgrid::mc::RemoteClient> process(new simgrid::mc::RemoteClient(pid, socket));
85 xbt_assert(smpi_privatize_global_variables != SmpiPrivStrategies::MMAP,
86 "Please use the dlopen privatization schema when model-checking SMPI code");
89 model_checker_.reset(new simgrid::mc::ModelChecker(std::move(process)));
90 xbt_assert(mc_model_checker == nullptr);
91 mc_model_checker = model_checker_.get();
92 mc_model_checker->start();
100 void Session::initialize()
102 xbt_assert(initial_snapshot_ == nullptr);
103 mc_model_checker->wait_for_requests();
104 initial_snapshot_ = simgrid::mc::take_snapshot(0);
107 void Session::execute(Transition const& transition)
109 model_checker_->handle_simcall(transition);
110 model_checker_->wait_for_requests();
113 void Session::restore_initial_state()
115 this->initial_snapshot_->restore(&mc_model_checker->process());
118 void Session::log_state()
120 mc_model_checker->getChecker()->log_state();
122 if (not _sg_mc_dot_output_file.get().empty()) {
123 fprintf(dot_output, "}\n");
126 if (getenv("SIMGRID_MC_SYSTEM_STATISTICS")){
127 int ret=system("free");
128 if(ret!=0)XBT_WARN("system call did not return 0, but %d",ret);
133 Session* Session::fork(const std::function<void()>& code)
135 // Create a AF_LOCAL socketpair used for exchanging messages
136 // between the model-checker process (ourselves) and the model-checked
140 res = socketpair(AF_LOCAL, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sockets);
142 throw simgrid::xbt::errno_error("Could not create socketpair");
144 pid_t pid = do_fork([sockets, &code] {
146 setup_child_environment(sockets[0]);
148 xbt_die("The model-checked process failed to exec()");
151 // Parent (model-checker):
154 return new Session(pid, sockets[1]);
158 Session* Session::spawnv(const char *path, char *const argv[])
160 return Session::fork([path, argv] {
166 Session* Session::spawnvp(const char *file, char *const argv[])
168 return Session::fork([file, argv] {
173 void Session::close()
175 initial_snapshot_ = nullptr;
176 if (model_checker_) {
177 model_checker_->shutdown();
178 model_checker_ = nullptr;
179 mc_model_checker = nullptr;
183 simgrid::mc::Session* session;