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>
27 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_Session, mc, "Model-checker session");
28 XBT_LOG_EXTERNAL_CATEGORY(mc_global);
30 namespace simgrid::mc {
32 RemoteApp::RemoteApp(const std::vector<char*>& args)
34 checker_side_ = std::make_unique<simgrid::mc::CheckerSide>(args);
36 initial_snapshot_ = std::make_shared<simgrid::mc::Snapshot>(0, page_store_, checker_side_->get_remote_memory());
39 RemoteApp::~RemoteApp()
41 initial_snapshot_ = nullptr;
45 void RemoteApp::restore_initial_state() const
47 this->initial_snapshot_->restore(checker_side_->get_remote_memory());
50 unsigned long RemoteApp::get_maxpid() const
52 // note: we could maybe cache it and count the actor creation on checker side too.
53 // But counting correctly accross state checkpoint/restore would be annoying.
55 checker_side_->get_channel().send(MessageType::ACTORS_MAXPID);
56 s_mc_message_int_t answer;
57 ssize_t answer_size = checker_side_->get_channel().receive(answer);
58 xbt_assert(answer_size != -1, "Could not receive message");
59 xbt_assert(answer_size == sizeof answer, "Broken message (size=%zd; expected %zu)", answer_size, sizeof answer);
60 xbt_assert(answer.type == MessageType::ACTORS_MAXPID_REPLY,
61 "Received unexpected message %s (%i); expected MessageType::ACTORS_MAXPID_REPLY (%i)",
62 to_c_str(answer.type), (int)answer.type, (int)MessageType::ACTORS_MAXPID_REPLY);
67 void RemoteApp::get_actors_status(std::map<aid_t, ActorState>& whereto) const
69 // The messaging happens as follows:
71 // CheckerSide AppSide
72 // send ACTORS_STATUS ---->
73 // <----- send ACTORS_STATUS_REPLY
74 // <----- send `N` `s_mc_message_actors_status_one_t` structs
75 // <----- send `M` `s_mc_message_simcall_probe_one_t` structs
76 checker_side_->get_channel().send(MessageType::ACTORS_STATUS);
78 s_mc_message_actors_status_answer_t answer;
79 ssize_t answer_size = checker_side_->get_channel().receive(answer);
80 xbt_assert(answer_size != -1, "Could not receive message");
81 xbt_assert(answer_size == sizeof answer, "Broken message (size=%zd; expected %zu)", answer_size, sizeof answer);
82 xbt_assert(answer.type == MessageType::ACTORS_STATUS_REPLY,
83 "Received unexpected message %s (%i); expected MessageType::ACTORS_STATUS_REPLY (%i)",
84 to_c_str(answer.type), (int)answer.type, (int)MessageType::ACTORS_STATUS_REPLY);
86 // Message sanity checks
87 xbt_assert(answer.count >= 0, "Received an ACTOR_STATUS_REPLY message with an actor count of '%d' < 0", answer.count);
88 xbt_assert(answer.transition_count >= 0, "Received an ACTOR_STATUS_REPLY message with transition_count '%d' < 0",
89 answer.transition_count);
90 xbt_assert(answer.transition_count == 0 || answer.count >= 0,
91 "Received an ACTOR_STATUS_REPLY message with no actor data "
92 "but with transition data nonetheless");
94 std::vector<s_mc_message_actors_status_one_t> status(answer.count);
95 if (answer.count > 0) {
96 size_t size = status.size() * sizeof(s_mc_message_actors_status_one_t);
97 ssize_t received = checker_side_->get_channel().receive(status.data(), size);
98 xbt_assert(static_cast<size_t>(received) == size);
101 // Ensures that each actor sends precisely `answer.transition_count` transitions. While technically
102 // this doesn't catch the edge case where actor A sends 3 instead of 2 and actor B sends 2 instead
103 // of 3 transitions, that is ignored here since that invariant needs to be enforced on the AppSide
104 const auto expected_transitions = std::accumulate(
105 status.begin(), status.end(), 0, [](int total, const auto& actor) { return total + actor.n_transitions; });
106 xbt_assert(expected_transitions == answer.transition_count,
107 "Expected to receive %d transition(s) but was only notified of %d by the app side", expected_transitions,
108 answer.transition_count);
110 std::vector<s_mc_message_simcall_probe_one_t> probes(answer.transition_count);
111 if (answer.transition_count > 0) {
112 for (auto& probe : probes) {
113 ssize_t received = checker_side_->get_channel().receive(probe);
114 xbt_assert(received >= 0, "Could not receive response to ACTORS_PROBE message (%s)", strerror(errno));
115 xbt_assert(static_cast<size_t>(received) == sizeof probe,
116 "Could not receive response to ACTORS_PROBE message (%zd bytes received != %zu bytes expected",
117 received, sizeof probe);
122 std::move_iterator probes_iter(probes.begin());
124 for (const auto& actor : status) {
125 xbt_assert(actor.n_transitions == 0 || actor.n_transitions == actor.max_considered,
126 "If any transitions are serialized for an actor, it must match the "
127 "total number of transitions that can be considered for the actor "
128 "(currently %d), but only %d transition(s) was/were said to be encoded",
129 actor.max_considered, actor.n_transitions);
131 std::vector<std::shared_ptr<Transition>> actor_transitions;
132 for (int times_considered = 0; times_considered < actor.n_transitions; times_considered++, probes_iter++) {
133 std::stringstream stream((*probes_iter).buffer.data());
134 actor_transitions.emplace_back(deserialize_transition(actor.aid, times_considered, stream));
137 XBT_DEBUG("Received %zu transitions for actor %ld", actor_transitions.size(), actor.aid);
138 whereto.try_emplace(actor.aid, actor.aid, actor.enabled, actor.max_considered, std::move(actor_transitions));
142 void RemoteApp::check_deadlock() const
144 xbt_assert(checker_side_->get_channel().send(MessageType::DEADLOCK_CHECK) == 0, "Could not check deadlock state");
145 s_mc_message_int_t message;
146 ssize_t received = checker_side_->get_channel().receive(message);
147 xbt_assert(received != -1, "Could not receive message");
148 xbt_assert(received == sizeof message, "Broken message (size=%zd; expected %zu)", received, sizeof message);
149 xbt_assert(message.type == MessageType::DEADLOCK_CHECK_REPLY,
150 "Received unexpected message %s (%i); expected MessageType::DEADLOCK_CHECK_REPLY (%i)",
151 to_c_str(message.type), (int)message.type, (int)MessageType::DEADLOCK_CHECK_REPLY);
153 if (message.value != 0) {
154 auto* explo = Exploration::get_instance();
155 XBT_CINFO(mc_global, "Counter-example execution trace:");
156 for (auto const& frame : explo->get_textual_trace())
157 XBT_CINFO(mc_global, " %s", frame.c_str());
158 XBT_INFO("You can debug the problem (and see the whole details) by rerunning out of simgrid-mc with "
159 "--cfg=model-check/replay:'%s'",
160 explo->get_record_trace().to_string().c_str());
162 throw DeadlockError();
166 void RemoteApp::wait_for_requests()
168 checker_side_->wait_for_requests();
171 void RemoteApp::shutdown()
173 XBT_DEBUG("Shutting down model-checker");
175 if (checker_side_->running()) {
176 XBT_DEBUG("Killing process");
178 kill(checker_side_->get_pid(), SIGKILL);
179 checker_side_->terminate();
183 Transition* RemoteApp::handle_simcall(aid_t aid, int times_considered, bool new_transition)
185 s_mc_message_simcall_execute_t m = {};
186 m.type = MessageType::SIMCALL_EXECUTE;
188 m.times_considered_ = times_considered;
189 checker_side_->get_channel().send(m);
191 get_remote_process_memory().clear_cache();
192 if (checker_side_->running())
193 checker_side_->dispatch_events(); // The app may send messages while processing the transition
195 s_mc_message_simcall_execute_answer_t answer;
196 ssize_t s = checker_side_->get_channel().receive(answer);
197 xbt_assert(s != -1, "Could not receive message");
198 xbt_assert(s == sizeof answer, "Broken message (size=%zd; expected %zu)", s, sizeof answer);
199 xbt_assert(answer.type == MessageType::SIMCALL_EXECUTE_ANSWER,
200 "Received unexpected message %s (%i); expected MessageType::SIMCALL_EXECUTE_ANSWER (%i)",
201 to_c_str(answer.type), (int)answer.type, (int)MessageType::SIMCALL_EXECUTE_ANSWER);
203 if (new_transition) {
204 std::stringstream stream(answer.buffer.data());
205 return deserialize_transition(aid, times_considered, stream);
210 void RemoteApp::finalize_app(bool terminate_asap)
212 s_mc_message_int_t m = {};
213 m.type = MessageType::FINALIZE;
214 m.value = terminate_asap;
215 xbt_assert(checker_side_->get_channel().send(m) == 0, "Could not ask the app to finalize on need");
217 s_mc_message_t answer;
218 ssize_t s = checker_side_->get_channel().receive(answer);
219 xbt_assert(s != -1, "Could not receive answer to FINALIZE");
220 xbt_assert(s == sizeof answer, "Broken message (size=%zd; expected %zu)", s, sizeof answer);
221 xbt_assert(answer.type == MessageType::FINALIZE_REPLY,
222 "Received unexpected message %s (%i); expected MessageType::FINALIZE_REPLY (%i)", to_c_str(answer.type),
223 (int)answer.type, (int)MessageType::FINALIZE_REPLY);
226 } // namespace simgrid::mc