Logo AND Algorithmique Numérique Distribuée

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