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 char master_socket_name[65] = {};
34 static void cleanup_master_socket()
36 if (master_socket_name[0] != '\0')
37 unlink(master_socket_name);
38 master_socket_name[0] = '\0';
41 RemoteApp::RemoteApp(const std::vector<char*>& args, bool need_memory_introspection) : app_args_(args)
43 if (need_memory_introspection) {
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());
48 xbt_die("SimGrid was compiled without MC support.");
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));
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);
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);
64 xbt_assert(listen(master_socket_, SOMAXCONN) >= 0, "Cannot listen to the master socket: %s.", strerror(errno));
66 application_factory_ = std::make_unique<simgrid::mc::CheckerSide>(app_args_, need_memory_introspection);
67 checker_side_ = application_factory_->clone(master_socket_);
71 RemoteApp::~RemoteApp()
73 checker_side_ = nullptr;
76 void RemoteApp::restore_initial_state()
78 if (initial_snapshot_ == nullptr) // No memory introspection
79 checker_side_ = application_factory_->clone(master_socket_);
82 initial_snapshot_->restore(*checker_side_->get_remote_memory());
86 unsigned long RemoteApp::get_maxpid() const
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.
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);
103 void RemoteApp::get_actors_status(std::map<aid_t, ActorState>& whereto) const
105 // The messaging happens as follows:
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);
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);
122 // Message sanity checks
123 xbt_assert(answer.count >= 0, "Received an ACTORS_STATUS_REPLY_COUNT message with an actor count of '%d' < 0",
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);
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);
146 std::stringstream stream(probe.buffer.data());
147 actor_transitions.emplace_back(deserialize_transition(actor.aid, times_considered, stream));
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));
155 void RemoteApp::check_deadlock() const
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);
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());
175 throw DeadlockError();
179 void RemoteApp::wait_for_requests()
181 checker_side_->wait_for_requests();
184 Transition* RemoteApp::handle_simcall(aid_t aid, int times_considered, bool new_transition)
186 s_mc_message_simcall_execute_t m = {};
187 m.type = MessageType::SIMCALL_EXECUTE;
189 m.times_considered_ = times_considered;
190 checker_side_->get_channel().send(m);
193 if (auto* memory = get_remote_process_memory(); memory != nullptr)
194 memory->clear_cache();
196 if (checker_side_->running())
197 checker_side_->dispatch_events(); // The app may send messages while processing the transition
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);
207 if (new_transition) {
208 std::stringstream stream(answer.buffer.data());
209 return deserialize_transition(aid, times_considered, stream);
214 void RemoteApp::finalize_app(bool terminate_asap)
216 checker_side_->finalize(terminate_asap);
219 } // namespace simgrid::mc