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. */
12 #include "xbt/system_error.hpp"
14 #include <simgrid/modelchecker.h>
15 #include <simgrid/sg_config.hpp>
17 #include "src/mc/Session.hpp"
18 #include "src/mc/checker/Checker.hpp"
19 #include "src/mc/mc_private.hpp"
20 #include "src/mc/mc_state.hpp"
22 #include "src/smpi/include/private.hpp"
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_Session, mc, "Model-checker session");
29 static void setup_child_environment(int socket)
32 // Make sure we do not outlive our parent:
35 if (sigprocmask(SIG_SETMASK, &mask, nullptr) < 0)
36 throw simgrid::xbt::errno_error("Could not unblock signals");
37 if (prctl(PR_SET_PDEATHSIG, SIGHUP) != 0)
38 throw simgrid::xbt::errno_error("Could not PR_SET_PDEATHSIG");
43 // Remove CLOEXEC in order to pass the socket to the exec-ed program:
44 int fdflags = fcntl(socket, F_GETFD, 0);
45 if (fdflags == -1 || fcntl(socket, F_SETFD, fdflags & ~FD_CLOEXEC) == -1)
46 throw simgrid::xbt::errno_error("Could not remove CLOEXEC for socket");
49 setenv(MC_ENV_VARIABLE, "1", 1);
51 // Disable lazy relocation in the model-checked process.
52 // We don't want the model-checked process to modify its .got.plt during
54 setenv("LC_BIND_NOW", "1", 1);
57 res = std::snprintf(buffer, sizeof(buffer), "%i", socket);
58 if ((size_t) res >= sizeof(buffer) || res == -1)
60 setenv(MC_ENV_SOCKET_FD, buffer, 1);
63 /** Execute some code in a forked process */
70 throw simgrid::xbt::errno_error("Could not fork model-checked process");
80 // The callback should catch exceptions:
85 Session::Session(pid_t pid, int socket)
87 std::unique_ptr<simgrid::mc::RemoteClient> process(new simgrid::mc::RemoteClient(pid, socket));
89 // TODO, automatic detection of the config from the process
90 process->privatized(smpi_privatize_global_variables != SmpiPrivStrategies::NONE);
92 process->privatized(false);
94 modelChecker_.reset(new simgrid::mc::ModelChecker(std::move(process)));
95 xbt_assert(mc_model_checker == nullptr);
96 mc_model_checker = modelChecker_.get();
97 mc_model_checker->start();
105 void Session::initialize()
107 xbt_assert(initialSnapshot_ == nullptr);
108 mc_model_checker->wait_for_requests();
109 initialSnapshot_ = simgrid::mc::take_snapshot(0);
112 void Session::execute(Transition const& transition)
114 modelChecker_->handle_simcall(transition);
115 modelChecker_->wait_for_requests();
118 void Session::restoreInitialState()
120 simgrid::mc::restore_snapshot(this->initialSnapshot_);
123 void Session::logState()
125 mc_model_checker->getChecker()->logState();
127 if (not _sg_mc_dot_output_file.get().empty()) {
128 fprintf(dot_output, "}\n");
131 if (getenv("SIMGRID_MC_SYSTEM_STATISTICS")){
132 int ret=system("free");
133 if(ret!=0)XBT_WARN("system call did not return 0, but %d",ret);
138 Session* Session::fork(const std::function<void()>& code)
140 // Create a AF_LOCAL socketpair used for exchanging messages
141 // between the model-checker process (ourselves) and the model-checked
145 res = socketpair(AF_LOCAL, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sockets);
147 throw simgrid::xbt::errno_error("Could not create socketpair");
149 pid_t pid = do_fork([sockets, &code] {
151 setup_child_environment(sockets[0]);
153 xbt_die("The model-checked process failed to exec()");
156 // Parent (model-checker):
159 return new Session(pid, sockets[1]);
163 Session* Session::spawnv(const char *path, char *const argv[])
165 return Session::fork([path, argv] {
171 Session* Session::spawnvp(const char *file, char *const argv[])
173 return Session::fork([file, argv] {
178 void Session::close()
180 initialSnapshot_ = nullptr;
182 modelChecker_->shutdown();
183 modelChecker_ = nullptr;
184 mc_model_checker = nullptr;
188 simgrid::mc::Session* session;