1 /* Copyright (c) 2015-2023. The SimGrid Team. All rights reserved. */
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. */
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"
15 #include "xbt/system_error.hpp"
24 #include <sys/ptrace.h>
28 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_Session, mc, "Model-checker session");
29 XBT_LOG_EXTERNAL_CATEGORY(mc_global);
31 namespace simgrid::mc {
33 static std::string master_socket_name;
35 RemoteApp::RemoteApp(const std::vector<char*>& args, bool need_memory_introspection) : app_args_(args)
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());
42 xbt_die("SimGrid MC was compiled without memory introspection support.");
45 master_socket_ = socket(AF_UNIX,
47 SOCK_STREAM, /* Mac OSX does not have AF_UNIX + SOCK_SEQPACKET, even if that's faster */
52 xbt_assert(master_socket_ != -1, "Cannot create the master socket: %s", strerror(errno));
54 struct sockaddr_un serv_addr = {};
55 serv_addr.sun_family = AF_UNIX;
56 snprintf(serv_addr.sun_path, 64, "/tmp/simgrid-mc-%d", getpid());
57 master_socket_name = serv_addr.sun_path;
58 auto addr_size = offsetof(struct sockaddr_un, sun_path) + strlen(serv_addr.sun_path);
60 serv_addr.sun_path[0] = '\0'; // abstract socket, automatically removed after close
62 unlink(master_socket_name.c_str()); // remove possible stale socket before bind
64 if (not master_socket_name.empty())
65 unlink(master_socket_name.c_str());
66 master_socket_name.clear();
71 xbt_assert(bind(master_socket_, (struct sockaddr*)&serv_addr, addr_size) >= 0,
72 "Cannot bind the master socket to %c%s: %s.", (serv_addr.sun_path[0] ? serv_addr.sun_path[0] : '@'),
73 serv_addr.sun_path + 1, strerror(errno));
75 xbt_assert(listen(master_socket_, SOMAXCONN) >= 0, "Cannot listen to the master socket: %s.", strerror(errno));
77 application_factory_ = std::make_unique<simgrid::mc::CheckerSide>(app_args_, need_memory_introspection);
78 checker_side_ = application_factory_->clone(master_socket_);
82 void RemoteApp::restore_initial_state()
84 if (initial_snapshot_ == nullptr) // No memory introspection
85 checker_side_ = application_factory_->clone(master_socket_);
86 #if SIMGRID_HAVE_STATEFUL_MC
88 initial_snapshot_->restore(*checker_side_->get_remote_memory());
92 unsigned long RemoteApp::get_maxpid() const
94 // note: we could maybe cache it and count the actor creation on checker side too.
95 // But counting correctly accross state checkpoint/restore would be annoying.
97 checker_side_->get_channel().send(MessageType::ACTORS_MAXPID);
98 s_mc_message_int_t answer;
99 ssize_t answer_size = checker_side_->get_channel().receive(answer);
100 xbt_assert(answer_size != -1, "Could not receive message");
101 xbt_assert(answer_size == sizeof answer, "Broken message (size=%zd; expected %zu)", answer_size, sizeof answer);
102 xbt_assert(answer.type == MessageType::ACTORS_MAXPID_REPLY,
103 "Received unexpected message %s (%i); expected MessageType::ACTORS_MAXPID_REPLY (%i)",
104 to_c_str(answer.type), (int)answer.type, (int)MessageType::ACTORS_MAXPID_REPLY);
109 void RemoteApp::get_actors_status(std::map<aid_t, ActorState>& whereto) const
111 // The messaging happens as follows:
113 // CheckerSide AppSide
114 // send ACTORS_STATUS ---->
115 // <----- send ACTORS_STATUS_REPLY_COUNT
116 // <----- send `N` ACTORS_STATUS_REPLY_TRANSITION (s_mc_message_actors_status_one_t)
117 // <----- send `M` ACTORS_STATUS_REPLY_SIMCALL (s_mc_message_simcall_probe_one_t)
119 // Note that we also receive disabled transitions, because the guiding strategies need them to decide what could
122 checker_side_->get_channel().send(MessageType::ACTORS_STATUS);
124 s_mc_message_actors_status_answer_t answer;
125 ssize_t answer_size = checker_side_->get_channel().receive(answer);
126 xbt_assert(answer_size != -1, "Could not receive message");
127 xbt_assert(answer_size == sizeof answer, "Broken message (size=%zd; expected %zu)", answer_size, sizeof answer);
128 xbt_assert(answer.type == MessageType::ACTORS_STATUS_REPLY_COUNT,
129 "%d Received unexpected message %s (%i); expected MessageType::ACTORS_STATUS_REPLY_COUNT (%i)", getpid(),
130 to_c_str(answer.type), (int)answer.type, (int)MessageType::ACTORS_STATUS_REPLY_COUNT);
132 // Message sanity checks
133 xbt_assert(answer.count >= 0, "Received an ACTORS_STATUS_REPLY_COUNT message with an actor count of '%d' < 0",
136 std::vector<s_mc_message_actors_status_one_t> status(answer.count);
137 if (answer.count > 0) {
138 size_t size = status.size() * sizeof(s_mc_message_actors_status_one_t);
139 ssize_t received = checker_side_->get_channel().receive(status.data(), size);
140 xbt_assert(static_cast<size_t>(received) == size);
145 for (const auto& actor : status) {
146 std::vector<std::shared_ptr<Transition>> actor_transitions;
147 int n_transitions = actor.max_considered;
148 for (int times_considered = 0; times_considered < n_transitions; times_considered++) {
149 s_mc_message_simcall_probe_one_t probe;
150 ssize_t received = checker_side_->get_channel().receive(probe);
151 xbt_assert(received >= 0, "Could not receive response to ACTORS_PROBE message (%s)", strerror(errno));
152 xbt_assert(static_cast<size_t>(received) == sizeof probe,
153 "Could not receive response to ACTORS_PROBE message (%zd bytes received != %zu bytes expected",
154 received, sizeof probe);
156 std::stringstream stream(probe.buffer.data());
157 actor_transitions.emplace_back(deserialize_transition(actor.aid, times_considered, stream));
160 XBT_DEBUG("Received %zu transitions for actor %ld", actor_transitions.size(), actor.aid);
161 whereto.try_emplace(actor.aid, actor.aid, actor.enabled, actor.max_considered, std::move(actor_transitions));
165 void RemoteApp::check_deadlock() const
167 xbt_assert(checker_side_->get_channel().send(MessageType::DEADLOCK_CHECK) == 0, "Could not check deadlock state");
168 s_mc_message_int_t message;
169 ssize_t received = checker_side_->get_channel().receive(message);
170 xbt_assert(received != -1, "Could not receive message");
171 xbt_assert(received == sizeof message, "Broken message (size=%zd; expected %zu)", received, sizeof message);
172 xbt_assert(message.type == MessageType::DEADLOCK_CHECK_REPLY,
173 "Received unexpected message %s (%i); expected MessageType::DEADLOCK_CHECK_REPLY (%i)",
174 to_c_str(message.type), (int)message.type, (int)MessageType::DEADLOCK_CHECK_REPLY);
176 if (message.value != 0) {
177 auto* explo = Exploration::get_instance();
178 XBT_CINFO(mc_global, "Counter-example execution trace:");
179 for (auto const& frame : explo->get_textual_trace())
180 XBT_CINFO(mc_global, " %s", frame.c_str());
181 XBT_INFO("You can debug the problem (and see the whole details) by rerunning out of simgrid-mc with "
182 "--cfg=model-check/replay:'%s'",
183 explo->get_record_trace().to_string().c_str());
185 throw McError(ExitStatus::DEADLOCK);
189 void RemoteApp::wait_for_requests()
191 checker_side_->wait_for_requests();
194 Transition* RemoteApp::handle_simcall(aid_t aid, int times_considered, bool new_transition)
196 s_mc_message_simcall_execute_t m = {};
197 m.type = MessageType::SIMCALL_EXECUTE;
199 m.times_considered_ = times_considered;
200 checker_side_->get_channel().send(m);
202 #if SIMGRID_HAVE_STATEFUL_MC
203 if (auto* memory = get_remote_process_memory(); memory != nullptr)
204 memory->clear_cache();
206 if (checker_side_->running())
207 checker_side_->dispatch_events(); // The app may send messages while processing the transition
209 s_mc_message_simcall_execute_answer_t answer;
210 ssize_t s = checker_side_->get_channel().receive(answer);
211 xbt_assert(s != -1, "Could not receive message");
212 xbt_assert(s > 0 && answer.type == MessageType::SIMCALL_EXECUTE_REPLY,
213 "%d Received unexpected message %s (%i); expected MessageType::SIMCALL_EXECUTE_REPLY (%i)", getpid(),
214 to_c_str(answer.type), (int)answer.type, (int)MessageType::SIMCALL_EXECUTE_REPLY);
215 xbt_assert(s == sizeof answer, "Broken message (size=%zd; expected %zu)", s, sizeof answer);
217 if (new_transition) {
218 std::stringstream stream(answer.buffer.data());
219 return deserialize_transition(aid, times_considered, stream);
224 void RemoteApp::finalize_app(bool terminate_asap)
226 checker_side_->finalize(terminate_asap);
229 } // namespace simgrid::mc