Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
code simplification in MC start sequence
[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(const std::function<void()>& code)
81 {
82 #if HAVE_SMPI
83   xbt_assert(smpi_privatize_global_variables != SmpiPrivStrategies::MMAP,
84              "Please use the dlopen privatization schema when model-checking SMPI code");
85 #endif
86
87   // Create a AF_LOCAL socketpair used for exchanging messages
88   // between the model-checker process (ourselves) and the model-checked
89   // process:
90   int res;
91   int sockets[2];
92   res = socketpair(AF_LOCAL, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sockets);
93   if (res == -1)
94     throw simgrid::xbt::errno_error("Could not create socketpair");
95
96   pid_t pid = do_fork([sockets, &code] {
97     ::close(sockets[1]);
98     setup_child_environment(sockets[0]);
99     code();
100     xbt_die("The model-checked process failed to exec()");
101   });
102
103   // Parent (model-checker):
104   ::close(sockets[0]);
105
106   std::unique_ptr<simgrid::mc::RemoteClient> process(new simgrid::mc::RemoteClient(pid, sockets[1]));
107   model_checker_.reset(new simgrid::mc::ModelChecker(std::move(process)));
108
109   xbt_assert(mc_model_checker == nullptr);
110   mc_model_checker = model_checker_.get();
111   mc_model_checker->start();
112 }
113
114 Session::~Session()
115 {
116   this->close();
117 }
118
119 void Session::initialize()
120 {
121   xbt_assert(initial_snapshot_ == nullptr);
122   mc_model_checker->wait_for_requests();
123   initial_snapshot_ = std::make_shared<simgrid::mc::Snapshot>(0);
124 }
125
126 void Session::execute(Transition const& transition)
127 {
128   model_checker_->handle_simcall(transition);
129   model_checker_->wait_for_requests();
130 }
131
132 void Session::restore_initial_state()
133 {
134   this->initial_snapshot_->restore(&mc_model_checker->process());
135 }
136
137 void Session::log_state()
138 {
139   mc_model_checker->getChecker()->log_state();
140
141   if (not _sg_mc_dot_output_file.get().empty()) {
142     fprintf(dot_output, "}\n");
143     fclose(dot_output);
144   }
145   if (getenv("SIMGRID_MC_SYSTEM_STATISTICS")){
146     int ret=system("free");
147     if(ret!=0)XBT_WARN("system call did not return 0, but %d",ret);
148   }
149 }
150
151 void Session::close()
152 {
153   initial_snapshot_ = nullptr;
154   if (model_checker_) {
155     model_checker_->shutdown();
156     model_checker_   = nullptr;
157     mc_model_checker = nullptr;
158   }
159 }
160
161 simgrid::mc::Session* session;
162
163 }
164 }