Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid into no_simix_global
[simgrid.git] / src / mc / Session.cpp
1 /* Copyright (c) 2015-2021. 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/internal_config.h" // HAVE_SMPI
10 #if HAVE_SMPI
11 #include "smpi/smpi.h"
12 #include "src/smpi/include/private.hpp"
13 #endif
14 #include "src/mc/mc_private.hpp"
15 #include "src/mc/mc_state.hpp"
16 #include "xbt/log.h"
17 #include "xbt/system_error.hpp"
18
19 #include "signal.h"
20 #include <array>
21 #include <memory>
22 #include <string>
23
24 #include <fcntl.h>
25 #ifdef __linux__
26 #include <sys/prctl.h>
27 #endif
28
29 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_Session, mc, "Model-checker session");
30
31 namespace simgrid {
32 namespace mc {
33
34 template <class Code> void run_child_process(int socket, Code code)
35 {
36   /* On startup, simix_global_init() calls simgrid::mc::Client::initialize(), which checks whether the MC_ENV_SOCKET_FD
37    * env variable is set. If so, MC mode is assumed, and the client is setup from its side
38    */
39
40 #ifdef __linux__
41   // Make sure we do not outlive our parent
42   sigset_t mask;
43   sigemptyset (&mask);
44   xbt_assert(sigprocmask(SIG_SETMASK, &mask, nullptr) >= 0, "Could not unblock signals");
45   xbt_assert(prctl(PR_SET_PDEATHSIG, SIGHUP) == 0, "Could not PR_SET_PDEATHSIG");
46 #endif
47
48   // Remove CLOEXEC to pass the socket to the application
49   int fdflags = fcntl(socket, F_GETFD, 0);
50   xbt_assert(fdflags != -1 && fcntl(socket, F_SETFD, fdflags & ~FD_CLOEXEC) != -1,
51              "Could not remove CLOEXEC for socket");
52
53   // Disable lazy relocation in the model-checked process to prevent the application from
54   // modifying its .got.plt during snapshot.
55   setenv("LC_BIND_NOW", "1", 1);
56
57   setenv(MC_ENV_SOCKET_FD, std::to_string(socket).c_str(), 1);
58
59   code();
60 }
61
62 Session::Session(const std::function<void()>& code)
63 {
64 #if HAVE_SMPI
65   smpi_init_options();//only performed once
66   xbt_assert(smpi_cfg_privatization() != SmpiPrivStrategies::MMAP,
67              "Please use the dlopen privatization schema when model-checking SMPI code");
68 #endif
69
70   // Create an AF_LOCAL socketpair used for exchanging messages
71   // between the model-checker process (ourselves) and the model-checked
72   // process:
73   int sockets[2];
74   xbt_assert(socketpair(AF_LOCAL, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sockets) != -1, "Could not create socketpair");
75
76   pid_t pid = fork();
77   xbt_assert(pid >= 0, "Could not fork model-checked process");
78
79   if (pid == 0) { // Child
80     ::close(sockets[1]);
81     run_child_process(sockets[0], code);
82     DIE_IMPOSSIBLE;
83   }
84
85   // Parent (model-checker):
86   ::close(sockets[0]);
87
88   xbt_assert(mc_model_checker == nullptr, "Did you manage to start the MC twice in this process?");
89
90   auto process   = std::make_unique<simgrid::mc::RemoteProcess>(pid);
91   model_checker_ = std::make_unique<simgrid::mc::ModelChecker>(std::move(process), sockets[1]);
92
93   mc_model_checker = model_checker_.get();
94   model_checker_->start();
95 }
96
97 Session::~Session()
98 {
99   this->close();
100 }
101
102 /** The application must be stopped. */
103 void Session::take_initial_snapshot()
104 {
105   xbt_assert(initial_snapshot_ == nullptr);
106   model_checker_->wait_for_requests();
107   initial_snapshot_ = std::make_shared<simgrid::mc::Snapshot>(0);
108 }
109
110 void Session::execute(Transition const& transition) const
111 {
112   model_checker_->handle_simcall(transition);
113   model_checker_->wait_for_requests();
114 }
115
116 void Session::restore_initial_state() const
117 {
118   this->initial_snapshot_->restore(&model_checker_->get_remote_process());
119 }
120
121 void Session::log_state() const
122 {
123   model_checker_->getChecker()->log_state();
124
125   if (not _sg_mc_dot_output_file.get().empty()) {
126     fprintf(dot_output, "}\n");
127     fclose(dot_output);
128   }
129   if (getenv("SIMGRID_MC_SYSTEM_STATISTICS")){
130     int ret=system("free");
131     if (ret != 0)
132       XBT_WARN("Call to system(free) did not return 0, but %d", ret);
133   }
134 }
135
136 void Session::close()
137 {
138   initial_snapshot_ = nullptr;
139   if (model_checker_) {
140     model_checker_->shutdown();
141     model_checker_   = nullptr;
142     mc_model_checker = nullptr;
143   }
144 }
145
146 bool Session::actor_is_enabled(aid_t pid) const
147 {
148   s_mc_message_actor_enabled_t msg;
149   memset(&msg, 0, sizeof msg);
150   msg.type = simgrid::mc::MessageType::ACTOR_ENABLED;
151   msg.aid  = pid;
152   model_checker_->channel().send(msg);
153   std::array<char, MC_MESSAGE_LENGTH> buff;
154   ssize_t received = model_checker_->channel().receive(buff.data(), buff.size(), true);
155   xbt_assert(received == sizeof(s_mc_message_int_t), "Unexpected size in answer to ACTOR_ENABLED");
156   return ((s_mc_message_int_t*)buff.data())->value;
157 }
158
159 simgrid::mc::Session* session_singleton;
160 }
161 }