Logo AND Algorithmique Numérique Distribuée

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