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/internal_config.h" // HAVE_SMPI
8 #include "src/mc/explo/Exploration.hpp"
9 #include "src/mc/mc_config.hpp"
10 #include "xbt/asserts.h"
12 #include "smpi/smpi.h"
13 #include "src/smpi/include/private.hpp"
16 #include "src/mc/api/State.hpp"
17 #include "src/mc/mc_config.hpp"
18 #include "src/mc/mc_exit.hpp"
19 #include "src/mc/mc_private.hpp"
21 #include "xbt/system_error.hpp"
25 #include <boost/tokenizer.hpp>
32 #include <sys/prctl.h>
35 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_Session, mc, "Model-checker session");
36 XBT_LOG_EXTERNAL_CATEGORY(mc_global);
38 static simgrid::config::Flag<std::string> _sg_mc_setenv{
39 "model-check/setenv", "Extra environment variables to pass to the child process (ex: 'AZE=aze;QWE=qwe').", "",
40 [](std::string_view value) {
41 xbt_assert(value.empty() || value.find('=', 0) != std::string_view::npos,
42 "The 'model-check/setenv' parameter must be like 'AZE=aze', but it does not contain an equal sign.");
45 namespace simgrid::mc {
47 XBT_ATTRIB_NORETURN static void run_child_process(int socket, const std::vector<char*>& args)
49 /* On startup, simix_global_init() calls simgrid::mc::Client::initialize(), which checks whether the MC_ENV_SOCKET_FD
50 * env variable is set. If so, MC mode is assumed, and the client is setup from its side
54 // Make sure we do not outlive our parent
57 xbt_assert(sigprocmask(SIG_SETMASK, &mask, nullptr) >= 0, "Could not unblock signals");
58 xbt_assert(prctl(PR_SET_PDEATHSIG, SIGHUP) == 0, "Could not PR_SET_PDEATHSIG");
61 // Remove CLOEXEC to pass the socket to the application
62 int fdflags = fcntl(socket, F_GETFD, 0);
63 xbt_assert(fdflags != -1 && fcntl(socket, F_SETFD, fdflags & ~FD_CLOEXEC) != -1,
64 "Could not remove CLOEXEC for socket");
66 setenv(MC_ENV_SOCKET_FD, std::to_string(socket).c_str(), 1);
68 /* Setup the tokenizer that parses the cfg:model-check/setenv parameter */
69 using Tokenizer = boost::tokenizer<boost::char_separator<char>>;
70 boost::char_separator<char> semicol_sep(";");
71 boost::char_separator<char> equal_sep("=");
72 Tokenizer token_vars(_sg_mc_setenv.get(), semicol_sep); /* Iterate over all FOO=foo parts */
73 for (const auto& token : token_vars) {
74 std::vector<std::string> kv;
75 Tokenizer token_kv(token, equal_sep);
76 for (const auto& t : token_kv) /* Iterate over 'FOO' and then 'foo' in that 'FOO=foo' */
78 xbt_assert(kv.size() == 2, "Parse error on 'model-check/setenv' value %s. Does it contain an equal sign?",
80 XBT_INFO("setenv '%s'='%s'", kv[0].c_str(), kv[1].c_str());
81 setenv(kv[0].c_str(), kv[1].c_str(), 1);
84 /* And now, exec the child process */
86 while (args[i] != nullptr && args[i][0] == '-')
89 xbt_assert(args[i] != nullptr,
90 "Unable to find a binary to exec on the command line. Did you only pass config flags?");
92 execvp(args[i], args.data() + i);
93 XBT_CRITICAL("The model-checked process failed to exec(%s): %s.\n"
94 " Make sure that your binary exists on disk and is executable.",
95 args[i], strerror(errno));
96 if (strchr(args[i], '=') != nullptr)
97 XBT_CRITICAL("If you want to pass environment variables to the application, please use --cfg=model-check/setenv:%s",
100 xbt_die("Aborting now.");
103 RemoteApp::RemoteApp(const std::vector<char*>& args)
106 smpi_init_options(); // only performed once
107 xbt_assert(smpi_cfg_privatization() != SmpiPrivStrategies::MMAP,
108 "Please use the dlopen privatization schema when model-checking SMPI code");
111 // Create an AF_LOCAL socketpair used for exchanging messages
112 // between the model-checker process (ourselves) and the model-checked
115 xbt_assert(socketpair(AF_LOCAL, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sockets) != -1, "Could not create socketpair");
118 xbt_assert(pid >= 0, "Could not fork model-checked process");
120 if (pid == 0) { // Child
122 run_child_process(sockets[0], args);
126 // Parent (model-checker):
129 xbt_assert(mc_model_checker == nullptr, "Did you manage to start the MC twice in this process?");
131 auto process = std::make_unique<simgrid::mc::RemoteProcess>(pid);
132 model_checker_ = std::make_unique<simgrid::mc::ModelChecker>(std::move(process), sockets[1]);
134 mc_model_checker = model_checker_.get();
135 model_checker_->start();
137 /* Take the initial snapshot */
138 model_checker_->wait_for_requests();
139 initial_snapshot_ = std::make_shared<simgrid::mc::Snapshot>(0);
142 RemoteApp::~RemoteApp()
144 initial_snapshot_ = nullptr;
145 if (model_checker_) {
146 model_checker_->shutdown();
147 model_checker_ = nullptr;
148 mc_model_checker = nullptr;
152 void RemoteApp::restore_initial_state() const
154 this->initial_snapshot_->restore(&model_checker_->get_remote_process());
157 unsigned long RemoteApp::get_maxpid() const
159 return model_checker_->get_remote_process().get_maxpid();
162 void RemoteApp::get_actors_status(std::map<aid_t, ActorState>& whereto) const
164 // The messaging happens as follows:
166 // CheckerSide AppSide
167 // send ACTORS_STATUS ---->
168 // <----- send ACTORS_STATUS_REPLY
169 // <----- send `N` `s_mc_message_actors_status_one_t` structs
170 // <----- send `M` `s_mc_message_simcall_probe_one_t` structs
172 memset(&msg, 0, sizeof msg);
173 msg.type = simgrid::mc::MessageType::ACTORS_STATUS;
174 model_checker_->channel().send(msg);
176 s_mc_message_actors_status_answer_t answer;
177 ssize_t received = model_checker_->channel().receive(answer);
178 xbt_assert(received != -1, "Could not receive message");
179 xbt_assert(received == sizeof(answer) && answer.type == MessageType::ACTORS_STATUS_REPLY,
180 "Received unexpected message %s (%i, size=%i) "
181 "expected MessageType::ACTORS_STATUS_REPLY (%i, size=%i)",
182 to_c_str(answer.type), (int)answer.type, (int)received, (int)MessageType::ACTORS_STATUS_REPLY,
183 (int)sizeof(answer));
185 std::vector<s_mc_message_actors_status_one_t> status(answer.count);
186 if (answer.count > 0) {
187 size_t size = status.size() * sizeof(s_mc_message_actors_status_one_t);
188 received = model_checker_->channel().receive(status.data(), size);
189 xbt_assert(static_cast<size_t>(received) == size);
192 std::vector<s_mc_message_simcall_probe_one_t> action_pool(answer.transition_count);
193 if (answer.transition_count > 0) {
194 size_t size = action_pool.size() * sizeof(s_mc_message_simcall_probe_one_t);
195 received = model_checker_->channel().receive(action_pool.data(), size);
196 xbt_assert(static_cast<size_t>(received) == size);
199 // Ensures that each actor sends precisely `actor.max_considered` transitions. While technically
200 // this doesn't catch the edge case where actor A sends 3 instead of 2 and actor B sends 2 instead
201 // of 3 transitions, that is ignored here since that invariant needs to be enforced on the AppSide
202 const auto expected_transitions = std::accumulate(
203 status.begin(), status.end(), 0, [](int total, const auto& actor) { return total + actor.n_transitions; });
204 xbt_assert(expected_transitions == static_cast<int>(action_pool.size()),
205 "Expected to receive %d transition(s) but was only notified of %lu by the app side", expected_transitions,
209 auto action_pool_iter = std::move_iterator(action_pool.begin());
211 for (const auto& actor : status) {
212 xbt_assert(actor.n_transitions == 0 || actor.n_transitions == actor.max_considered,
213 "If any transitions are serialized for an actor, it must match the "
214 "total number of transitions that can be considered for the actor "
215 "(currently %d), but only %d transition(s) was/were said to be encoded",
216 actor.max_considered, actor.n_transitions);
218 auto actor_transitions = std::vector<std::unique_ptr<Transition>>(actor.n_transitions);
219 for (int times_considered = 0; times_considered < actor.n_transitions; times_considered++, action_pool_iter++) {
220 std::stringstream stream((*action_pool_iter).buffer.data());
221 auto transition = std::unique_ptr<Transition>(deserialize_transition(actor.aid, times_considered, stream));
222 actor_transitions[times_considered] = std::move(transition);
225 XBT_DEBUG("Received %d transitions for actor %ld", actor.n_transitions, actor.aid);
226 whereto.try_emplace(actor.aid, actor.aid, actor.enabled, actor.max_considered, std::move(actor_transitions));
230 void RemoteApp::check_deadlock() const
232 xbt_assert(model_checker_->channel().send(MessageType::DEADLOCK_CHECK) == 0, "Could not check deadlock state");
233 s_mc_message_int_t message;
234 ssize_t s = model_checker_->channel().receive(message);
235 xbt_assert(s != -1, "Could not receive message");
236 xbt_assert(s == sizeof(message) && message.type == MessageType::DEADLOCK_CHECK_REPLY,
237 "Received unexpected message %s (%i, size=%i) "
238 "expected MessageType::DEADLOCK_CHECK_REPLY (%i, size=%i)",
239 to_c_str(message.type), (int)message.type, (int)s, (int)MessageType::DEADLOCK_CHECK_REPLY,
240 (int)sizeof(message));
242 if (message.value != 0) {
243 XBT_CINFO(mc_global, "Counter-example execution trace:");
244 for (auto const& frame : model_checker_->get_exploration()->get_textual_trace())
245 XBT_CINFO(mc_global, " %s", frame.c_str());
246 XBT_INFO("You can debug the problem (and see the whole details) by rerunning out of simgrid-mc with "
247 "--cfg=model-check/replay:'%s'",
248 model_checker_->get_exploration()->get_record_trace().to_string().c_str());
249 model_checker_->get_exploration()->log_state();
250 throw DeadlockError();
253 } // namespace simgrid::mc