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, bool need_memory_introspection)
34 for (auto* arg : args)
35 app_args_.push_back(arg);
37 checker_side_ = std::make_unique<simgrid::mc::CheckerSide>(app_args_, need_memory_introspection);
39 if (need_memory_introspection)
40 initial_snapshot_ = std::make_shared<simgrid::mc::Snapshot>(0, page_store_, *checker_side_->get_remote_memory());
43 RemoteApp::~RemoteApp()
45 initial_snapshot_ = nullptr;
46 checker_side_ = nullptr;
49 void RemoteApp::restore_initial_state()
51 if (initial_snapshot_ == nullptr) { // No memory introspection
52 // We need to destroy the existing CheckerSide before creating the new one, or libevent gets crazy
53 checker_side_.reset(nullptr);
54 checker_side_.reset(new simgrid::mc::CheckerSide(app_args_, true));
56 initial_snapshot_->restore(*checker_side_->get_remote_memory());
59 unsigned long RemoteApp::get_maxpid() const
61 // note: we could maybe cache it and count the actor creation on checker side too.
62 // But counting correctly accross state checkpoint/restore would be annoying.
64 checker_side_->get_channel().send(MessageType::ACTORS_MAXPID);
65 s_mc_message_int_t answer;
66 ssize_t answer_size = checker_side_->get_channel().receive(answer);
67 xbt_assert(answer_size != -1, "Could not receive message");
68 xbt_assert(answer_size == sizeof answer, "Broken message (size=%zd; expected %zu)", answer_size, sizeof answer);
69 xbt_assert(answer.type == MessageType::ACTORS_MAXPID_REPLY,
70 "Received unexpected message %s (%i); expected MessageType::ACTORS_MAXPID_REPLY (%i)",
71 to_c_str(answer.type), (int)answer.type, (int)MessageType::ACTORS_MAXPID_REPLY);
76 void RemoteApp::get_actors_status(std::map<aid_t, ActorState>& whereto) const
78 // The messaging happens as follows:
80 // CheckerSide AppSide
81 // send ACTORS_STATUS ---->
82 // <----- send ACTORS_STATUS_REPLY
83 // <----- send `N` `s_mc_message_actors_status_one_t` structs
84 // <----- send `M` `s_mc_message_simcall_probe_one_t` structs
85 checker_side_->get_channel().send(MessageType::ACTORS_STATUS);
87 s_mc_message_actors_status_answer_t answer;
88 ssize_t answer_size = checker_side_->get_channel().receive(answer);
89 xbt_assert(answer_size != -1, "Could not receive message");
90 xbt_assert(answer_size == sizeof answer, "Broken message (size=%zd; expected %zu)", answer_size, sizeof answer);
91 xbt_assert(answer.type == MessageType::ACTORS_STATUS_REPLY,
92 "Received unexpected message %s (%i); expected MessageType::ACTORS_STATUS_REPLY (%i)",
93 to_c_str(answer.type), (int)answer.type, (int)MessageType::ACTORS_STATUS_REPLY);
95 // Message sanity checks
96 xbt_assert(answer.count >= 0, "Received an ACTOR_STATUS_REPLY message with an actor count of '%d' < 0", answer.count);
97 xbt_assert(answer.transition_count >= 0, "Received an ACTOR_STATUS_REPLY message with transition_count '%d' < 0",
98 answer.transition_count);
99 xbt_assert(answer.transition_count == 0 || answer.count >= 0,
100 "Received an ACTOR_STATUS_REPLY message with no actor data "
101 "but with transition data nonetheless");
103 std::vector<s_mc_message_actors_status_one_t> status(answer.count);
104 if (answer.count > 0) {
105 size_t size = status.size() * sizeof(s_mc_message_actors_status_one_t);
106 ssize_t received = checker_side_->get_channel().receive(status.data(), size);
107 xbt_assert(static_cast<size_t>(received) == size);
110 // Ensures that each actor sends precisely `answer.transition_count` transitions. While technically
111 // this doesn't catch the edge case where actor A sends 3 instead of 2 and actor B sends 2 instead
112 // of 3 transitions, that is ignored here since that invariant needs to be enforced on the AppSide
113 const auto expected_transitions = std::accumulate(
114 status.begin(), status.end(), 0, [](int total, const auto& actor) { return total + actor.n_transitions; });
115 xbt_assert(expected_transitions == answer.transition_count,
116 "Expected to receive %d transition(s) but was only notified of %d by the app side", expected_transitions,
117 answer.transition_count);
121 for (const auto& actor : status) {
122 xbt_assert(actor.n_transitions == 0 || actor.n_transitions == actor.max_considered,
123 "If any transitions are serialized for an actor, it must match the "
124 "total number of transitions that can be considered for the actor "
125 "(currently %d), but only %d transition(s) was/were said to be encoded",
126 actor.max_considered, actor.n_transitions);
128 std::vector<std::shared_ptr<Transition>> actor_transitions;
129 for (int times_considered = 0; times_considered < actor.n_transitions; times_considered++) {
130 s_mc_message_simcall_probe_one_t probe;
131 ssize_t received = checker_side_->get_channel().receive(probe);
132 xbt_assert(received >= 0, "Could not receive response to ACTORS_PROBE message (%s)", strerror(errno));
133 xbt_assert(static_cast<size_t>(received) == sizeof probe,
134 "Could not receive response to ACTORS_PROBE message (%zd bytes received != %zu bytes expected",
135 received, sizeof probe);
137 std::stringstream stream(probe.buffer.data());
138 actor_transitions.emplace_back(deserialize_transition(actor.aid, times_considered, stream));
141 XBT_DEBUG("Received %zu transitions for actor %ld", actor_transitions.size(), actor.aid);
142 whereto.try_emplace(actor.aid, actor.aid, actor.enabled, actor.max_considered, std::move(actor_transitions));
146 void RemoteApp::check_deadlock() const
148 xbt_assert(checker_side_->get_channel().send(MessageType::DEADLOCK_CHECK) == 0, "Could not check deadlock state");
149 s_mc_message_int_t message;
150 ssize_t received = checker_side_->get_channel().receive(message);
151 xbt_assert(received != -1, "Could not receive message");
152 xbt_assert(received == sizeof message, "Broken message (size=%zd; expected %zu)", received, sizeof message);
153 xbt_assert(message.type == MessageType::DEADLOCK_CHECK_REPLY,
154 "Received unexpected message %s (%i); expected MessageType::DEADLOCK_CHECK_REPLY (%i)",
155 to_c_str(message.type), (int)message.type, (int)MessageType::DEADLOCK_CHECK_REPLY);
157 if (message.value != 0) {
158 auto* explo = Exploration::get_instance();
159 XBT_CINFO(mc_global, "Counter-example execution trace:");
160 for (auto const& frame : explo->get_textual_trace())
161 XBT_CINFO(mc_global, " %s", frame.c_str());
162 XBT_INFO("You can debug the problem (and see the whole details) by rerunning out of simgrid-mc with "
163 "--cfg=model-check/replay:'%s'",
164 explo->get_record_trace().to_string().c_str());
166 throw DeadlockError();
170 void RemoteApp::wait_for_requests()
172 checker_side_->wait_for_requests();
175 Transition* RemoteApp::handle_simcall(aid_t aid, int times_considered, bool new_transition)
177 s_mc_message_simcall_execute_t m = {};
178 m.type = MessageType::SIMCALL_EXECUTE;
180 m.times_considered_ = times_considered;
181 checker_side_->get_channel().send(m);
183 if (auto* memory = get_remote_process_memory(); memory != nullptr)
184 memory->clear_cache();
185 if (checker_side_->running())
186 checker_side_->dispatch_events(); // The app may send messages while processing the transition
188 s_mc_message_simcall_execute_answer_t answer;
189 ssize_t s = checker_side_->get_channel().receive(answer);
190 xbt_assert(s != -1, "Could not receive message");
191 xbt_assert(s == sizeof answer, "Broken message (size=%zd; expected %zu)", s, sizeof answer);
192 xbt_assert(answer.type == MessageType::SIMCALL_EXECUTE_ANSWER,
193 "Received unexpected message %s (%i); expected MessageType::SIMCALL_EXECUTE_ANSWER (%i)",
194 to_c_str(answer.type), (int)answer.type, (int)MessageType::SIMCALL_EXECUTE_ANSWER);
196 if (new_transition) {
197 std::stringstream stream(answer.buffer.data());
198 return deserialize_transition(aid, times_considered, stream);
203 void RemoteApp::finalize_app(bool terminate_asap)
205 checker_side_->finalize(terminate_asap);
208 } // namespace simgrid::mc