Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
mc: don't catch exceptions we cannot deal with
[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   xbt_assert(sigprocmask(SIG_SETMASK, &mask, nullptr) >= 0, "Could not unblock signals");
31   xbt_assert(prctl(PR_SET_PDEATHSIG, SIGHUP) == 0, "Could not PR_SET_PDEATHSIG");
32 #endif
33
34   // Remove CLOEXEC in order to pass the socket to the exec-ed program:
35   int fdflags = fcntl(socket, F_GETFD, 0);
36   xbt_assert(fdflags != -1 && fcntl(socket, F_SETFD, fdflags & ~FD_CLOEXEC) != -1,
37              "Could not remove CLOEXEC for socket");
38
39   // Set environment:
40   setenv(MC_ENV_VARIABLE, "1", 1);
41
42   // Disable lazy relocation in the model-checked process.
43   // We don't want the model-checked process to modify its .got.plt during
44   // snapshot.
45   setenv("LC_BIND_NOW", "1", 1);
46
47   char buffer[64];
48   int res = std::snprintf(buffer, sizeof(buffer), "%i", socket);
49   xbt_assert((size_t)res < sizeof(buffer) && res != -1);
50   setenv(MC_ENV_SOCKET_FD, buffer, 1);
51 }
52
53 /** Execute some code in a forked process */
54 template<class F>
55 static inline
56 pid_t do_fork(F code)
57 {
58   pid_t pid = fork();
59   xbt_assert(pid >= 0, "Could not fork model-checked process");
60   if (pid != 0)
61     return pid;
62
63   // Child-process:
64   code();
65   _exit(EXIT_SUCCESS);
66 }
67
68 Session::Session(const std::function<void()>& code)
69 {
70 #if HAVE_SMPI
71   xbt_assert(smpi_privatize_global_variables != SmpiPrivStrategies::MMAP,
72              "Please use the dlopen privatization schema when model-checking SMPI code");
73 #endif
74
75   // Create a AF_LOCAL socketpair used for exchanging messages
76   // between the model-checker process (ourselves) and the model-checked
77   // process:
78   int res;
79   int sockets[2];
80   res = socketpair(AF_LOCAL, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sockets);
81   if (res == -1)
82     throw simgrid::xbt::errno_error("Could not create socketpair");
83
84   pid_t pid = do_fork([sockets, &code] {
85     ::close(sockets[1]);
86     setup_child_environment(sockets[0]);
87     code();
88     xbt_die("The model-checked process failed to exec()");
89   });
90
91   // Parent (model-checker):
92   ::close(sockets[0]);
93
94   std::unique_ptr<simgrid::mc::RemoteClient> process(new simgrid::mc::RemoteClient(pid, sockets[1]));
95   model_checker_.reset(new simgrid::mc::ModelChecker(std::move(process)));
96
97   xbt_assert(mc_model_checker == nullptr);
98   mc_model_checker = model_checker_.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(initial_snapshot_ == nullptr);
110   mc_model_checker->wait_for_requests();
111   initial_snapshot_ = std::make_shared<simgrid::mc::Snapshot>(0);
112 }
113
114 void Session::execute(Transition const& transition)
115 {
116   model_checker_->handle_simcall(transition);
117   model_checker_->wait_for_requests();
118 }
119
120 void Session::restore_initial_state()
121 {
122   this->initial_snapshot_->restore(&mc_model_checker->process());
123 }
124
125 void Session::log_state()
126 {
127   mc_model_checker->getChecker()->log_state();
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 void Session::close()
140 {
141   initial_snapshot_ = nullptr;
142   if (model_checker_) {
143     model_checker_->shutdown();
144     model_checker_   = nullptr;
145     mc_model_checker = nullptr;
146   }
147 }
148
149 simgrid::mc::Session* session;
150
151 }
152 }