1 /* Copyright (c) 2015-2017. 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"
15 #include <simgrid/modelchecker.h>
16 #include <simgrid/sg_config.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/Checker.hpp"
23 #include "src/smpi/include/private.hpp"
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_Session, mc, "Model-checker session");
30 static void setup_child_environment(int socket)
33 // Make sure we do not outlive our parent:
36 if (sigprocmask(SIG_SETMASK, &mask, nullptr) < 0)
37 throw simgrid::xbt::errno_error("Could not unblock signals");
38 if (prctl(PR_SET_PDEATHSIG, SIGHUP) != 0)
39 throw simgrid::xbt::errno_error("Could not PR_SET_PDEATHSIG");
44 // Remove CLOEXEC in order to pass the socket to the exec-ed program:
45 int fdflags = fcntl(socket, F_GETFD, 0);
46 if (fdflags == -1 || fcntl(socket, F_SETFD, fdflags & ~FD_CLOEXEC) == -1)
47 throw simgrid::xbt::errno_error("Could not remove CLOEXEC for socket");
50 setenv(MC_ENV_VARIABLE, "1", 1);
52 // Disable lazy relocation in the model-checked process.
53 // We don't want the model-checked process to modify its .got.plt during
55 setenv("LC_BIND_NOW", "1", 1);
58 res = std::snprintf(buffer, sizeof(buffer), "%i", socket);
59 if ((size_t) res >= sizeof(buffer) || res == -1)
61 setenv(MC_ENV_SOCKET_FD, buffer, 1);
64 /** Execute some code in a forked process */
71 throw simgrid::xbt::errno_error("Could not fork model-checked process");
81 // The callback should catch exceptions:
86 Session::Session(pid_t pid, int socket)
88 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 != SMPI_PRIVATIZE_NONE);
91 modelChecker_ = std::unique_ptr<ModelChecker>(
92 new simgrid::mc::ModelChecker(std::move(process)));
93 xbt_assert(mc_model_checker == nullptr);
94 mc_model_checker = modelChecker_.get();
95 mc_model_checker->start();
103 void Session::initialize()
105 xbt_assert(initialSnapshot_ == nullptr);
106 mc_model_checker->wait_for_requests();
107 initialSnapshot_ = simgrid::mc::take_snapshot(0);
110 void Session::execute(Transition const& transition)
112 modelChecker_->handle_simcall(transition);
113 modelChecker_->wait_for_requests();
116 void Session::restoreInitialState()
118 simgrid::mc::restore_snapshot(this->initialSnapshot_);
121 void Session::logState()
123 mc_model_checker->getChecker()->logState();
125 if ((_sg_mc_dot_output_file != nullptr) && (_sg_mc_dot_output_file[0] != '\0')) {
126 fprintf(dot_output, "}\n");
129 if (getenv("SIMGRID_MC_SYSTEM_STATISTICS")){
130 int ret=system("free");
131 if(ret!=0)XBT_WARN("system call did not return 0, but %d",ret);
136 Session* Session::fork(std::function<void()> code)
138 // Create a AF_LOCAL socketpair used for exchanging messages
139 // between the model-checker process (ourselves) and the model-checked
143 res = socketpair(AF_LOCAL, SOCK_DGRAM | SOCK_CLOEXEC, 0, sockets);
145 throw simgrid::xbt::errno_error("Could not create socketpair");
147 pid_t pid = do_fork([sockets, &code] {
149 setup_child_environment(sockets[0]);
151 xbt_die("The model-checked process failed to exec()");
154 // Parent (model-checker):
157 return new Session(pid, sockets[1]);
161 Session* Session::spawnv(const char *path, char *const argv[])
163 return Session::fork([path, argv] {
169 Session* Session::spawnvp(const char *file, char *const argv[])
171 return Session::fork([file, argv] {
176 void Session::close()
178 initialSnapshot_ = nullptr;
180 modelChecker_->shutdown();
181 modelChecker_ = nullptr;
182 mc_model_checker = nullptr;
186 simgrid::mc::Session* session;