Logo AND Algorithmique Numérique Distribuée

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