Logo AND Algorithmique Numérique Distribuée

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