1 /* Copyright (c) 2015-2016. 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. */
13 #include <xbt/system_error.hpp>
14 #include <simgrid/sg_config.h>
15 #include <simgrid/modelchecker.h>
18 #include "src/mc/Session.hpp"
19 #include "src/mc/mc_state.h"
20 #include "src/mc/mc_private.h"
21 #include "src/mc/Checker.hpp"
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_Session, mc, "Model-checker session");
28 static void setup_child_environment(int socket)
31 // Make sure we do not outlive our parent:
34 if (sigprocmask(SIG_SETMASK, &mask, nullptr) < 0)
35 throw simgrid::xbt::errno_error("Could not unblock signals");
36 if (prctl(PR_SET_PDEATHSIG, SIGHUP) != 0)
37 throw simgrid::xbt::errno_error("Could not PR_SET_PDEATHSIG");
42 // Remove CLOEXEC in order to pass the socket to the exec-ed program:
43 int fdflags = fcntl(socket, F_GETFD, 0);
44 if (fdflags == -1 || fcntl(socket, F_SETFD, fdflags & ~FD_CLOEXEC) == -1)
45 throw simgrid::xbt::errno_error("Could not remove CLOEXEC for socket");
48 setenv(MC_ENV_VARIABLE, "1", 1);
50 // Disable lazy relocation in the model-checked process.
51 // We don't want the model-checked process to modify its .got.plt during
53 setenv("LC_BIND_NOW", "1", 1);
56 res = std::snprintf(buffer, sizeof(buffer), "%i", socket);
57 if ((size_t) res >= sizeof(buffer) || res == -1)
59 setenv(MC_ENV_SOCKET_FD, buffer, 1);
62 /** Execute some code in a forked process */
69 throw simgrid::xbt::errno_error("Could not fork model-checked process");
79 // The callback should catch exceptions:
84 Session::Session(pid_t pid, int socket)
86 std::unique_ptr<simgrid::mc::Process> process(new simgrid::mc::Process(pid, socket));
87 // TODO, automatic detection of the config from the process
89 xbt_cfg_get_boolean("smpi/privatize-global-variables"));
90 modelChecker_ = std::unique_ptr<ModelChecker>(
91 new simgrid::mc::ModelChecker(std::move(process)));
92 xbt_assert(mc_model_checker == nullptr);
93 mc_model_checker = modelChecker_.get();
94 mc_model_checker->start();
102 void Session::initialize()
104 xbt_assert(initialSnapshot_ == nullptr);
105 mc_model_checker->wait_for_requests();
106 initialSnapshot_ = simgrid::mc::take_snapshot(0);
109 void Session::execute(Transition const& transition)
111 modelChecker_->handle_simcall(transition);
112 modelChecker_->wait_for_requests();
115 void Session::restoreInitialState()
117 simgrid::mc::restore_snapshot(this->initialSnapshot_);
120 void Session::logState()
122 mc_model_checker->getChecker()->logState();
124 if ((_sg_mc_dot_output_file != nullptr) && (_sg_mc_dot_output_file[0] != '\0')) {
125 fprintf(dot_output, "}\n");
128 if (getenv("SIMGRID_MC_SYSTEM_STATISTICS")){
129 int ret=system("free");
130 if(ret!=0)XBT_WARN("system call did not return 0, but %d",ret);
135 Session* Session::fork(std::function<void(void)> code)
137 // Create a AF_LOCAL socketpair used for exchanging messages
138 // bewteen the model-checker process (ourselves) and the model-checked
142 res = socketpair(AF_LOCAL, SOCK_DGRAM | SOCK_CLOEXEC, 0, sockets);
144 throw simgrid::xbt::errno_error("Could not create socketpair");
146 pid_t pid = do_fork([&] {
148 setup_child_environment(sockets[0]);
150 xbt_die("The model-checked process failed to exec()");
153 // Parent (model-checker):
156 return new Session(pid, sockets[1]);
160 Session* Session::spawnv(const char *path, char *const argv[])
162 return Session::fork([&] {
168 Session* Session::spawnvp(const char *path, char *const argv[])
170 return Session::fork([&] {
175 void Session::close()
177 initialSnapshot_ = nullptr;
179 modelChecker_->shutdown();
180 modelChecker_ = nullptr;
181 mc_model_checker = nullptr;
185 simgrid::mc::Session* session;