Logo AND Algorithmique Numérique Distribuée

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