Logo AND Algorithmique Numérique Distribuée

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