Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
20fafe1b80e550c51495b86863c08c05ed7d5804
[simgrid.git] / src / mc / Session.cpp
1 /* Copyright (c) 2015-2019. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include <csignal>
8 #include <fcntl.h>
9
10 #include <functional>
11
12 #include "xbt/log.h"
13 #include "xbt/system_error.hpp"
14 #include <mc/mc.h>
15 #include <simgrid/modelchecker.h>
16 #include <simgrid/sg_config.hpp>
17
18 #include "src/mc/Session.hpp"
19 #include "src/mc/checker/Checker.hpp"
20 #include "src/mc/mc_private.hpp"
21 #include "src/mc/mc_state.hpp"
22
23 #include "src/smpi/include/private.hpp"
24
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_Session, mc, "Model-checker session");
26
27 namespace simgrid {
28 namespace mc {
29
30 static void setup_child_environment(int socket)
31 {
32 #ifdef __linux__
33   // Make sure we do not outlive our parent:
34   sigset_t mask;
35   sigemptyset (&mask);
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");
40 #endif
41
42   int res;
43
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");
48
49   // Set environment:
50   setenv(MC_ENV_VARIABLE, "1", 1);
51
52   // Disable lazy relocation in the model-checked process.
53   // We don't want the model-checked process to modify its .got.plt during
54   // snapshot.
55   setenv("LC_BIND_NOW", "1", 1);
56
57   char buffer[64];
58   res = std::snprintf(buffer, sizeof(buffer), "%i", socket);
59   if ((size_t) res >= sizeof(buffer) || res == -1)
60     std::abort();
61   setenv(MC_ENV_SOCKET_FD, buffer, 1);
62 }
63
64 /** Execute some code in a forked process */
65 template<class F>
66 static inline
67 pid_t do_fork(F code)
68 {
69   pid_t pid = fork();
70   if (pid < 0)
71     throw simgrid::xbt::errno_error("Could not fork model-checked process");
72   if (pid != 0)
73     return pid;
74
75   // Child-process:
76   try {
77     code();
78     _exit(EXIT_SUCCESS);
79   }
80   catch(...) {
81     // The callback should catch exceptions:
82     std::terminate();
83   }
84 }
85
86 Session::Session(pid_t pid, int socket)
87 {
88   std::unique_ptr<simgrid::mc::RemoteClient> process(new simgrid::mc::RemoteClient(pid, socket));
89 #if HAVE_SMPI
90   // TODO, automatic detection of the config from the process
91   process->privatized(smpi_privatize_global_variables != SmpiPrivStrategies::NONE);
92 #else
93   process->privatized(false);
94 #endif
95   modelChecker_ = std::unique_ptr<ModelChecker>(
96     new simgrid::mc::ModelChecker(std::move(process)));
97   xbt_assert(mc_model_checker == nullptr);
98   mc_model_checker = modelChecker_.get();
99   mc_model_checker->start();
100 }
101
102 Session::~Session()
103 {
104   this->close();
105 }
106
107 void Session::initialize()
108 {
109   xbt_assert(initialSnapshot_ == nullptr);
110   mc_model_checker->wait_for_requests();
111   initialSnapshot_ = simgrid::mc::take_snapshot(0);
112 }
113
114 void Session::execute(Transition const& transition)
115 {
116   modelChecker_->handle_simcall(transition);
117   modelChecker_->wait_for_requests();
118 }
119
120 void Session::restoreInitialState()
121 {
122   simgrid::mc::restore_snapshot(this->initialSnapshot_);
123 }
124
125 void Session::logState()
126 {
127   mc_model_checker->getChecker()->logState();
128
129   if (not _sg_mc_dot_output_file.get().empty()) {
130     fprintf(dot_output, "}\n");
131     fclose(dot_output);
132   }
133   if (getenv("SIMGRID_MC_SYSTEM_STATISTICS")){
134     int ret=system("free");
135     if(ret!=0)XBT_WARN("system call did not return 0, but %d",ret);
136   }
137 }
138
139 // static
140 Session* Session::fork(std::function<void()> code)
141 {
142   // Create a AF_LOCAL socketpair used for exchanging messages
143   // between the model-checker process (ourselves) and the model-checked
144   // process:
145   int res;
146   int sockets[2];
147   res = socketpair(AF_LOCAL, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sockets);
148   if (res == -1)
149     throw simgrid::xbt::errno_error("Could not create socketpair");
150
151   pid_t pid = do_fork([sockets, &code] {
152     ::close(sockets[1]);
153     setup_child_environment(sockets[0]);
154     code();
155     xbt_die("The model-checked process failed to exec()");
156   });
157
158   // Parent (model-checker):
159   ::close(sockets[0]);
160
161   return new Session(pid, sockets[1]);
162 }
163
164 // static
165 Session* Session::spawnv(const char *path, char *const argv[])
166 {
167   return Session::fork([path, argv] {
168     execv(path, argv);
169   });
170 }
171
172 // static
173 Session* Session::spawnvp(const char *file, char *const argv[])
174 {
175   return Session::fork([file, argv] {
176     execvp(file, argv);
177   });
178 }
179
180 void Session::close()
181 {
182   initialSnapshot_ = nullptr;
183   if (modelChecker_) {
184     modelChecker_->shutdown();
185     modelChecker_ = nullptr;
186     mc_model_checker = nullptr;
187   }
188 }
189
190 simgrid::mc::Session* session;
191
192 }
193 }