Logo AND Algorithmique Numérique Distribuée

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