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) : app_args_(args)
34 checker_side_ = std::make_unique<simgrid::mc::CheckerSide>(app_args_, need_memory_introspection);
36 if (need_memory_introspection)
37 initial_snapshot_ = std::make_shared<simgrid::mc::Snapshot>(0, page_store_, *checker_side_->get_remote_memory());
40 RemoteApp::~RemoteApp()
42 initial_snapshot_ = nullptr;
43 checker_side_ = nullptr;
46 void RemoteApp::restore_initial_state()
48 if (initial_snapshot_ == nullptr) { // No memory introspection
49 // We need to destroy the existing CheckerSide before creating the new one, or libevent gets crazy
50 checker_side_.reset(nullptr);
51 checker_side_.reset(new simgrid::mc::CheckerSide(app_args_, false));
53 initial_snapshot_->restore(*checker_side_->get_remote_memory());
56 unsigned long RemoteApp::get_maxpid() const
58 // note: we could maybe cache it and count the actor creation on checker side too.
59 // But counting correctly accross state checkpoint/restore would be annoying.
61 checker_side_->get_channel().send(MessageType::ACTORS_MAXPID);
62 s_mc_message_int_t answer;
63 ssize_t answer_size = checker_side_->get_channel().receive(answer);
64 xbt_assert(answer_size != -1, "Could not receive message");
65 xbt_assert(answer_size == sizeof answer, "Broken message (size=%zd; expected %zu)", answer_size, sizeof answer);
66 xbt_assert(answer.type == MessageType::ACTORS_MAXPID_REPLY,
67 "Received unexpected message %s (%i); expected MessageType::ACTORS_MAXPID_REPLY (%i)",
68 to_c_str(answer.type), (int)answer.type, (int)MessageType::ACTORS_MAXPID_REPLY);
73 void RemoteApp::get_actors_status(std::map<aid_t, ActorState>& whereto) const
75 // The messaging happens as follows:
77 // CheckerSide AppSide
78 // send ACTORS_STATUS ---->
79 // <----- send ACTORS_STATUS_REPLY
80 // <----- send `N` `s_mc_message_actors_status_one_t` structs
81 // <----- send `M` `s_mc_message_simcall_probe_one_t` structs
82 checker_side_->get_channel().send(MessageType::ACTORS_STATUS);
84 s_mc_message_actors_status_answer_t answer;
85 ssize_t answer_size = checker_side_->get_channel().receive(answer);
86 xbt_assert(answer_size != -1, "Could not receive message");
87 xbt_assert(answer_size == sizeof answer, "Broken message (size=%zd; expected %zu)", answer_size, sizeof answer);
88 xbt_assert(answer.type == MessageType::ACTORS_STATUS_REPLY,
89 "Received unexpected message %s (%i); expected MessageType::ACTORS_STATUS_REPLY (%i)",
90 to_c_str(answer.type), (int)answer.type, (int)MessageType::ACTORS_STATUS_REPLY);
92 // Message sanity checks
93 xbt_assert(answer.count >= 0, "Received an ACTOR_STATUS_REPLY message with an actor count of '%d' < 0", answer.count);
95 std::vector<s_mc_message_actors_status_one_t> status(answer.count);
96 if (answer.count > 0) {
97 size_t size = status.size() * sizeof(s_mc_message_actors_status_one_t);
98 ssize_t received = checker_side_->get_channel().receive(status.data(), size);
99 xbt_assert(static_cast<size_t>(received) == size);
104 for (const auto& actor : status) {
105 std::vector<std::shared_ptr<Transition>> actor_transitions;
106 int n_transitions = actor.enabled ? actor.max_considered : 0;
107 for (int times_considered = 0; times_considered < n_transitions; times_considered++) {
108 s_mc_message_simcall_probe_one_t probe;
109 ssize_t received = checker_side_->get_channel().receive(probe);
110 xbt_assert(received >= 0, "Could not receive response to ACTORS_PROBE message (%s)", strerror(errno));
111 xbt_assert(static_cast<size_t>(received) == sizeof probe,
112 "Could not receive response to ACTORS_PROBE message (%zd bytes received != %zu bytes expected",
113 received, sizeof probe);
115 std::stringstream stream(probe.buffer.data());
116 actor_transitions.emplace_back(deserialize_transition(actor.aid, times_considered, stream));
119 XBT_DEBUG("Received %zu transitions for actor %ld", actor_transitions.size(), actor.aid);
120 whereto.try_emplace(actor.aid, actor.aid, actor.enabled, actor.max_considered, std::move(actor_transitions));
124 void RemoteApp::check_deadlock() const
126 xbt_assert(checker_side_->get_channel().send(MessageType::DEADLOCK_CHECK) == 0, "Could not check deadlock state");
127 s_mc_message_int_t message;
128 ssize_t received = checker_side_->get_channel().receive(message);
129 xbt_assert(received != -1, "Could not receive message");
130 xbt_assert(received == sizeof message, "Broken message (size=%zd; expected %zu)", received, sizeof message);
131 xbt_assert(message.type == MessageType::DEADLOCK_CHECK_REPLY,
132 "Received unexpected message %s (%i); expected MessageType::DEADLOCK_CHECK_REPLY (%i)",
133 to_c_str(message.type), (int)message.type, (int)MessageType::DEADLOCK_CHECK_REPLY);
135 if (message.value != 0) {
136 auto* explo = Exploration::get_instance();
137 XBT_CINFO(mc_global, "Counter-example execution trace:");
138 for (auto const& frame : explo->get_textual_trace())
139 XBT_CINFO(mc_global, " %s", frame.c_str());
140 XBT_INFO("You can debug the problem (and see the whole details) by rerunning out of simgrid-mc with "
141 "--cfg=model-check/replay:'%s'",
142 explo->get_record_trace().to_string().c_str());
144 throw DeadlockError();
148 void RemoteApp::wait_for_requests()
150 checker_side_->wait_for_requests();
153 Transition* RemoteApp::handle_simcall(aid_t aid, int times_considered, bool new_transition)
155 s_mc_message_simcall_execute_t m = {};
156 m.type = MessageType::SIMCALL_EXECUTE;
158 m.times_considered_ = times_considered;
159 checker_side_->get_channel().send(m);
161 if (auto* memory = get_remote_process_memory(); memory != nullptr)
162 memory->clear_cache();
163 if (checker_side_->running())
164 checker_side_->dispatch_events(); // The app may send messages while processing the transition
166 s_mc_message_simcall_execute_answer_t answer;
167 ssize_t s = checker_side_->get_channel().receive(answer);
168 xbt_assert(s != -1, "Could not receive message");
169 xbt_assert(s == sizeof answer, "Broken message (size=%zd; expected %zu)", s, sizeof answer);
170 xbt_assert(answer.type == MessageType::SIMCALL_EXECUTE_ANSWER,
171 "Received unexpected message %s (%i); expected MessageType::SIMCALL_EXECUTE_ANSWER (%i)",
172 to_c_str(answer.type), (int)answer.type, (int)MessageType::SIMCALL_EXECUTE_ANSWER);
174 if (new_transition) {
175 std::stringstream stream(answer.buffer.data());
176 return deserialize_transition(aid, times_considered, stream);
181 void RemoteApp::finalize_app(bool terminate_asap)
183 checker_side_->finalize(terminate_asap);
186 } // namespace simgrid::mc