Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a1217a67525e930e57cfb03344511fdb65a9b7ae
[simgrid.git] / src / mc / api / RemoteApp.cpp
1 /* Copyright (c) 2015-2023. 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/api/RemoteApp.hpp"
7 #include "src/mc/explo/Exploration.hpp"
8 #include "src/mc/mc_config.hpp"
9 #include "xbt/asserts.h"
10 #include "src/mc/api/State.hpp"
11 #include "src/mc/mc_config.hpp"
12 #include "src/mc/mc_exit.hpp"
13 #include "src/mc/mc_private.hpp"
14 #include "xbt/log.h"
15 #include "xbt/system_error.hpp"
16 #include <signal.h>
17
18 #include <algorithm>
19 #include <array>
20 #include <limits.h>
21 #include <memory>
22 #include <numeric>
23 #include <string>
24 #include <sys/ptrace.h>
25 #include <sys/un.h>
26 #include <sys/wait.h>
27
28 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_Session, mc, "Model-checker session");
29 XBT_LOG_EXTERNAL_CATEGORY(mc_global);
30
31 namespace simgrid::mc {
32
33 static std::string master_socket_name;
34
35 RemoteApp::RemoteApp(const std::vector<char*>& args, bool need_memory_introspection) : app_args_(args)
36 {
37   if (need_memory_introspection) {
38 #if SIMGRID_HAVE_STATEFUL_MC
39     checker_side_     = std::make_unique<simgrid::mc::CheckerSide>(app_args_, need_memory_introspection);
40     initial_snapshot_ = std::make_shared<simgrid::mc::Snapshot>(0, page_store_, *checker_side_->get_remote_memory());
41 #else
42     xbt_die("SimGrid MC was compiled without memory introspection support.");
43 #endif
44   } else {
45     master_socket_ = socket(AF_UNIX,
46 #ifdef __APPLE__
47                             SOCK_STREAM, /* Mac OSX does not have AF_UNIX + SOCK_SEQPACKET, even if that's faster */
48 #else
49                             SOCK_SEQPACKET,
50 #endif
51                             0);
52     xbt_assert(master_socket_ != -1, "Cannot create the master socket: %s", strerror(errno));
53
54     master_socket_name = "/tmp/simgrid-mc-" + std::to_string(getpid());
55     master_socket_name.resize(MC_SOCKET_NAME_LEN); // truncate socket name if it's too long
56     master_socket_name.back() = '\0';              // ensure the data are null-terminated
57 #ifdef __linux__
58     master_socket_name[0] = '\0'; // abstract socket, automatically removed after close
59 #else
60     unlink(master_socket_name.c_str()); // remove possible stale socket before bind
61     atexit([]() {
62       if (not master_socket_name.empty())
63         unlink(master_socket_name.c_str());
64       master_socket_name.clear();
65     });
66 #endif
67
68     struct sockaddr_un serv_addr = {};
69     serv_addr.sun_family         = AF_UNIX;
70     master_socket_name.copy(serv_addr.sun_path, MC_SOCKET_NAME_LEN);
71
72     xbt_assert(bind(master_socket_, (struct sockaddr*)&serv_addr, sizeof serv_addr) >= 0,
73                "Cannot bind the master socket to %c%s: %s.", (serv_addr.sun_path[0] ? serv_addr.sun_path[0] : '@'),
74                serv_addr.sun_path + 1, strerror(errno));
75
76     xbt_assert(listen(master_socket_, SOMAXCONN) >= 0, "Cannot listen to the master socket: %s.", strerror(errno));
77
78     application_factory_ = std::make_unique<simgrid::mc::CheckerSide>(app_args_, need_memory_introspection);
79     checker_side_        = application_factory_->clone(master_socket_, master_socket_name);
80   }
81 }
82
83 void RemoteApp::restore_initial_state()
84 {
85   if (initial_snapshot_ == nullptr) // No memory introspection
86     checker_side_ = application_factory_->clone(master_socket_, master_socket_name);
87 #if SIMGRID_HAVE_STATEFUL_MC
88   else
89     initial_snapshot_->restore(*checker_side_->get_remote_memory());
90 #endif
91 }
92
93 unsigned long RemoteApp::get_maxpid() const
94 {
95   // note: we could maybe cache it and count the actor creation on checker side too.
96   // But counting correctly accross state checkpoint/restore would be annoying.
97
98   checker_side_->get_channel().send(MessageType::ACTORS_MAXPID);
99   s_mc_message_int_t answer;
100   ssize_t answer_size = checker_side_->get_channel().receive(answer);
101   xbt_assert(answer_size != -1, "Could not receive message");
102   xbt_assert(answer_size == sizeof answer, "Broken message (size=%zd; expected %zu)", answer_size, sizeof answer);
103   xbt_assert(answer.type == MessageType::ACTORS_MAXPID_REPLY,
104              "Received unexpected message %s (%i); expected MessageType::ACTORS_MAXPID_REPLY (%i)",
105              to_c_str(answer.type), (int)answer.type, (int)MessageType::ACTORS_MAXPID_REPLY);
106
107   return answer.value;
108 }
109
110 void RemoteApp::get_actors_status(std::map<aid_t, ActorState>& whereto) const
111 {
112   // The messaging happens as follows:
113   //
114   // CheckerSide                  AppSide
115   // send ACTORS_STATUS ---->
116   //                    <----- send ACTORS_STATUS_REPLY_COUNT
117   //                    <----- send `N` ACTORS_STATUS_REPLY_TRANSITION (s_mc_message_actors_status_one_t)
118   //                    <----- send `M` ACTORS_STATUS_REPLY_SIMCALL (s_mc_message_simcall_probe_one_t)
119   //
120   // Note that we also receive disabled transitions, because the guiding strategies need them to decide what could
121   // unlock actors.
122
123   checker_side_->get_channel().send(MessageType::ACTORS_STATUS);
124
125   s_mc_message_actors_status_answer_t answer;
126   ssize_t answer_size = checker_side_->get_channel().receive(answer);
127   xbt_assert(answer_size != -1, "Could not receive message");
128   xbt_assert(answer_size == sizeof answer, "Broken message (size=%zd; expected %zu)", answer_size, sizeof answer);
129   xbt_assert(answer.type == MessageType::ACTORS_STATUS_REPLY_COUNT,
130              "%d Received unexpected message %s (%i); expected MessageType::ACTORS_STATUS_REPLY_COUNT (%i)", getpid(),
131              to_c_str(answer.type), (int)answer.type, (int)MessageType::ACTORS_STATUS_REPLY_COUNT);
132
133   // Message sanity checks
134   xbt_assert(answer.count >= 0, "Received an ACTORS_STATUS_REPLY_COUNT message with an actor count of '%d' < 0",
135              answer.count);
136
137   std::vector<s_mc_message_actors_status_one_t> status(answer.count);
138   if (answer.count > 0) {
139     size_t size      = status.size() * sizeof(s_mc_message_actors_status_one_t);
140     ssize_t received = checker_side_->get_channel().receive(status.data(), size);
141     xbt_assert(static_cast<size_t>(received) == size);
142   }
143
144   whereto.clear();
145
146   for (const auto& actor : status) {
147     std::vector<std::shared_ptr<Transition>> actor_transitions;
148     int n_transitions = actor.max_considered;
149     for (int times_considered = 0; times_considered < n_transitions; times_considered++) {
150       s_mc_message_simcall_probe_one_t probe;
151       ssize_t received = checker_side_->get_channel().receive(probe);
152       xbt_assert(received >= 0, "Could not receive response to ACTORS_PROBE message (%s)", strerror(errno));
153       xbt_assert(static_cast<size_t>(received) == sizeof probe,
154                  "Could not receive response to ACTORS_PROBE message (%zd bytes received != %zu bytes expected",
155                  received, sizeof probe);
156
157       std::stringstream stream(probe.buffer.data());
158       actor_transitions.emplace_back(deserialize_transition(actor.aid, times_considered, stream));
159     }
160
161     XBT_DEBUG("Received %zu transitions for actor %ld. The first one is %s", actor_transitions.size(), actor.aid,
162               (actor_transitions.size() > 0 ? actor_transitions[0]->to_string().c_str() : "null"));
163     whereto.try_emplace(actor.aid, actor.aid, actor.enabled, actor.max_considered, std::move(actor_transitions));
164   }
165 }
166
167 void RemoteApp::check_deadlock() const
168 {
169   xbt_assert(checker_side_->get_channel().send(MessageType::DEADLOCK_CHECK) == 0, "Could not check deadlock state");
170   s_mc_message_int_t message;
171   ssize_t received = checker_side_->get_channel().receive(message);
172   xbt_assert(received != -1, "Could not receive message");
173   xbt_assert(received == sizeof message, "Broken message (size=%zd; expected %zu)", received, sizeof message);
174   xbt_assert(message.type == MessageType::DEADLOCK_CHECK_REPLY,
175              "Received unexpected message %s (%i); expected MessageType::DEADLOCK_CHECK_REPLY (%i)",
176              to_c_str(message.type), (int)message.type, (int)MessageType::DEADLOCK_CHECK_REPLY);
177
178   if (message.value != 0) {
179     auto* explo = Exploration::get_instance();
180     XBT_CINFO(mc_global, "Counter-example execution trace:");
181     for (auto const& frame : explo->get_textual_trace())
182       XBT_CINFO(mc_global, "  %s", frame.c_str());
183     XBT_INFO("You can debug the problem (and see the whole details) by rerunning out of simgrid-mc with "
184              "--cfg=model-check/replay:'%s'",
185              explo->get_record_trace().to_string().c_str());
186     explo->log_state();
187     throw McError(ExitStatus::DEADLOCK);
188   }
189 }
190
191 void RemoteApp::wait_for_requests()
192 {
193   checker_side_->wait_for_requests();
194 }
195
196 Transition* RemoteApp::handle_simcall(aid_t aid, int times_considered, bool new_transition)
197 {
198   s_mc_message_simcall_execute_t m = {};
199   m.type                           = MessageType::SIMCALL_EXECUTE;
200   m.aid_                           = aid;
201   m.times_considered_              = times_considered;
202   checker_side_->get_channel().send(m);
203
204 #if SIMGRID_HAVE_STATEFUL_MC
205   if (auto* memory = get_remote_process_memory(); memory != nullptr)
206     memory->clear_cache();
207 #endif
208   if (checker_side_->running())
209     checker_side_->dispatch_events(); // The app may send messages while processing the transition
210
211   s_mc_message_simcall_execute_answer_t answer;
212   ssize_t s = checker_side_->get_channel().receive(answer);
213   xbt_assert(s != -1, "Could not receive message");
214   xbt_assert(s > 0 && answer.type == MessageType::SIMCALL_EXECUTE_REPLY,
215              "%d Received unexpected message %s (%i); expected MessageType::SIMCALL_EXECUTE_REPLY (%i)", getpid(),
216              to_c_str(answer.type), (int)answer.type, (int)MessageType::SIMCALL_EXECUTE_REPLY);
217   xbt_assert(s == sizeof answer, "Broken message (size=%zd; expected %zu)", s, sizeof answer);
218
219   if (new_transition) {
220     std::stringstream stream(answer.buffer.data());
221     return deserialize_transition(aid, times_considered, stream);
222   } else
223     return nullptr;
224 }
225
226 void RemoteApp::finalize_app(bool terminate_asap)
227 {
228   checker_side_->finalize(terminate_asap);
229 }
230
231 } // namespace simgrid::mc