1 /* Copyright (c) 2015-2022. 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/Session.hpp"
7 #include "src/internal_config.h" // HAVE_SMPI
8 #include "src/mc/explo/Exploration.hpp"
9 #include "src/mc/mc_config.hpp"
11 #include "smpi/smpi.h"
12 #include "src/smpi/include/private.hpp"
14 #include "src/mc/api/State.hpp"
15 #include "src/mc/mc_exit.hpp"
16 #include "src/mc/mc_private.hpp"
18 #include "xbt/system_error.hpp"
27 #include <sys/prctl.h>
30 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_Session, mc, "Model-checker session");
31 XBT_LOG_EXTERNAL_CATEGORY(mc_global);
33 namespace simgrid::mc {
35 template <class Code> void run_child_process(int socket, Code code)
37 /* On startup, simix_global_init() calls simgrid::mc::Client::initialize(), which checks whether the MC_ENV_SOCKET_FD
38 * env variable is set. If so, MC mode is assumed, and the client is setup from its side
42 // Make sure we do not outlive our parent
45 xbt_assert(sigprocmask(SIG_SETMASK, &mask, nullptr) >= 0, "Could not unblock signals");
46 xbt_assert(prctl(PR_SET_PDEATHSIG, SIGHUP) == 0, "Could not PR_SET_PDEATHSIG");
49 // Remove CLOEXEC to pass the socket to the application
50 int fdflags = fcntl(socket, F_GETFD, 0);
51 xbt_assert(fdflags != -1 && fcntl(socket, F_SETFD, fdflags & ~FD_CLOEXEC) != -1,
52 "Could not remove CLOEXEC for socket");
54 setenv(MC_ENV_SOCKET_FD, std::to_string(socket).c_str(), 1);
59 Session::Session(const std::function<void()>& code)
62 smpi_init_options();//only performed once
63 xbt_assert(smpi_cfg_privatization() != SmpiPrivStrategies::MMAP,
64 "Please use the dlopen privatization schema when model-checking SMPI code");
67 // Create an AF_LOCAL socketpair used for exchanging messages
68 // between the model-checker process (ourselves) and the model-checked
71 xbt_assert(socketpair(AF_LOCAL, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sockets) != -1, "Could not create socketpair");
74 xbt_assert(pid >= 0, "Could not fork model-checked process");
76 if (pid == 0) { // Child
78 run_child_process(sockets[0], code);
82 // Parent (model-checker):
85 xbt_assert(mc_model_checker == nullptr, "Did you manage to start the MC twice in this process?");
87 auto process = std::make_unique<simgrid::mc::RemoteProcess>(pid);
88 model_checker_ = std::make_unique<simgrid::mc::ModelChecker>(std::move(process), sockets[1]);
90 mc_model_checker = model_checker_.get();
91 model_checker_->start();
99 /** The application must be stopped. */
100 void Session::take_initial_snapshot()
102 xbt_assert(initial_snapshot_ == nullptr);
103 model_checker_->wait_for_requests();
104 initial_snapshot_ = std::make_shared<simgrid::mc::Snapshot>(0);
107 void Session::restore_initial_state() const
109 this->initial_snapshot_->restore(&model_checker_->get_remote_process());
112 void Session::log_state() const
114 model_checker_->get_exploration()->log_state();
116 if (not _sg_mc_dot_output_file.get().empty()) {
117 fprintf(dot_output, "}\n");
120 if (getenv("SIMGRID_MC_SYSTEM_STATISTICS")){
121 int ret=system("free");
123 XBT_WARN("Call to system(free) did not return 0, but %d", ret);
127 void Session::close()
129 initial_snapshot_ = nullptr;
130 if (model_checker_) {
131 model_checker_->shutdown();
132 model_checker_ = nullptr;
133 mc_model_checker = nullptr;
137 bool Session::actor_is_enabled(aid_t pid) const
139 s_mc_message_actor_enabled_t msg;
140 memset(&msg, 0, sizeof msg);
141 msg.type = simgrid::mc::MessageType::ACTOR_ENABLED;
143 model_checker_->channel().send(msg);
144 std::array<char, MC_MESSAGE_LENGTH> buff;
145 ssize_t received = model_checker_->channel().receive(buff.data(), buff.size(), true);
146 xbt_assert(received == sizeof(s_mc_message_int_t), "Unexpected size in answer to ACTOR_ENABLED");
147 return ((s_mc_message_int_t*)buff.data())->value;
150 void Session::check_deadlock() const
152 xbt_assert(model_checker_->channel().send(MessageType::DEADLOCK_CHECK) == 0, "Could not check deadlock state");
153 s_mc_message_int_t message;
154 ssize_t s = model_checker_->channel().receive(message);
155 xbt_assert(s != -1, "Could not receive message");
156 xbt_assert(s == sizeof(message) && message.type == MessageType::DEADLOCK_CHECK_REPLY,
157 "Received unexpected message %s (%i, size=%i) "
158 "expected MessageType::DEADLOCK_CHECK_REPLY (%i, size=%i)",
159 to_c_str(message.type), (int)message.type, (int)s, (int)MessageType::DEADLOCK_CHECK_REPLY,
160 (int)sizeof(message));
162 if (message.value != 0) {
163 XBT_CINFO(mc_global, "**************************");
164 XBT_CINFO(mc_global, "*** DEADLOCK DETECTED ***");
165 XBT_CINFO(mc_global, "**************************");
166 XBT_CINFO(mc_global, "Counter-example execution trace:");
167 for (auto const& frame : model_checker_->get_exploration()->get_textual_trace())
168 XBT_CINFO(mc_global, " %s", frame.c_str());
169 XBT_CINFO(mc_global, "Path = %s", model_checker_->get_exploration()->get_record_trace().to_string().c_str());
171 throw DeadlockError();
174 } // namespace simgrid::mc