Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix some easy sonar smells
[simgrid.git] / src / mc / api / RemoteApp.cpp
index d7fa977..45e595e 100644 (file)
 
 #include <algorithm>
 #include <array>
-#include <boost/tokenizer.hpp>
 #include <memory>
 #include <numeric>
 #include <string>
 
-#include <fcntl.h>
-#ifdef __linux__
-#include <sys/prctl.h>
-#endif
+#include <sys/ptrace.h>
+#include <sys/wait.h>
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_Session, mc, "Model-checker session");
 XBT_LOG_EXTERNAL_CATEGORY(mc_global);
 
-static simgrid::config::Flag<std::string> _sg_mc_setenv{
-    "model-check/setenv", "Extra environment variables to pass to the child process (ex: 'AZE=aze;QWE=qwe').", "",
-    [](std::string_view value) {
-      xbt_assert(value.empty() || value.find('=', 0) != std::string_view::npos,
-                 "The 'model-check/setenv' parameter must be like 'AZE=aze', but it does not contain an equal sign.");
-    }};
-
 namespace simgrid::mc {
 
-XBT_ATTRIB_NORETURN static void run_child_process(int socket, const std::vector<char*>& args)
+RemoteApp::RemoteApp(const std::vector<char*>& args, bool need_memory_introspection)
 {
-  /* On startup, simix_global_init() calls simgrid::mc::Client::initialize(), which checks whether the MC_ENV_SOCKET_FD
-   * env variable is set. If so, MC mode is assumed, and the client is setup from its side
-   */
-
-#ifdef __linux__
-  // Make sure we do not outlive our parent
-  sigset_t mask;
-  sigemptyset(&mask);
-  xbt_assert(sigprocmask(SIG_SETMASK, &mask, nullptr) >= 0, "Could not unblock signals");
-  xbt_assert(prctl(PR_SET_PDEATHSIG, SIGHUP) == 0, "Could not PR_SET_PDEATHSIG");
-#endif
-
-  // Remove CLOEXEC to pass the socket to the application
-  int fdflags = fcntl(socket, F_GETFD, 0);
-  xbt_assert(fdflags != -1 && fcntl(socket, F_SETFD, fdflags & ~FD_CLOEXEC) != -1,
-             "Could not remove CLOEXEC for socket");
-
-  setenv(MC_ENV_SOCKET_FD, std::to_string(socket).c_str(), 1);
-
-  /* Setup the tokenizer that parses the cfg:model-check/setenv parameter */
-  using Tokenizer = boost::tokenizer<boost::char_separator<char>>;
-  boost::char_separator<char> semicol_sep(";");
-  boost::char_separator<char> equal_sep("=");
-  Tokenizer token_vars(_sg_mc_setenv.get(), semicol_sep); /* Iterate over all FOO=foo parts */
-  for (const auto& token : token_vars) {
-    std::vector<std::string> kv;
-    Tokenizer token_kv(token, equal_sep);
-    for (const auto& t : token_kv) /* Iterate over 'FOO' and then 'foo' in that 'FOO=foo' */
-      kv.push_back(t);
-    xbt_assert(kv.size() == 2, "Parse error on 'model-check/setenv' value %s. Does it contain an equal sign?",
-               token.c_str());
-    XBT_INFO("setenv '%s'='%s'", kv[0].c_str(), kv[1].c_str());
-    setenv(kv[0].c_str(), kv[1].c_str(), 1);
-  }
-
-  /* And now, exec the child process */
-  int i = 1;
-  while (args[i] != nullptr && args[i][0] == '-')
-    i++;
+  for (auto* arg : args)
+    app_args_.push_back(arg);
 
-  xbt_assert(args[i] != nullptr,
-             "Unable to find a binary to exec on the command line. Did you only pass config flags?");
+  checker_side_ = std::make_unique<simgrid::mc::CheckerSide>(app_args_, need_memory_introspection);
 
-  execvp(args[i], args.data() + i);
-  XBT_CRITICAL("The model-checked process failed to exec(%s): %s.\n"
-               "        Make sure that your binary exists on disk and is executable.",
-               args[i], strerror(errno));
-  if (strchr(args[i], '=') != nullptr)
-    XBT_CRITICAL("If you want to pass environment variables to the application, please use --cfg=model-check/setenv:%s",
-                 args[i]);
-
-  xbt_die("Aborting now.");
-}
-
-RemoteApp::RemoteApp(const std::vector<char*>& args)
-{
-  // Create an AF_LOCAL socketpair used for exchanging messages
-  // between the model-checker process (ourselves) and the model-checked
-  // process:
-  int sockets[2];
-  xbt_assert(socketpair(AF_LOCAL, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sockets) != -1, "Could not create socketpair");
-
-  pid_t pid = fork();
-  xbt_assert(pid >= 0, "Could not fork model-checked process");
-
-  if (pid == 0) { // Child
-    ::close(sockets[1]);
-    run_child_process(sockets[0], args);
-    DIE_IMPOSSIBLE;
-  }
-
-  // Parent (model-checker):
-  ::close(sockets[0]);
-
-  xbt_assert(mc_model_checker == nullptr, "Did you manage to start the MC twice in this process?");
-
-  auto process   = std::make_unique<simgrid::mc::RemoteProcessMemory>(pid);
-  model_checker_ = std::make_unique<simgrid::mc::ModelChecker>(std::move(process), sockets[1]);
-
-  mc_model_checker = model_checker_.get();
-  model_checker_->start();
-
-  /* Take the initial snapshot */
-  wait_for_requests();
-  initial_snapshot_ =
-      std::make_shared<simgrid::mc::Snapshot>(0, page_store_, model_checker_->get_remote_process_memory());
+  if (need_memory_introspection)
+    initial_snapshot_ = std::make_shared<simgrid::mc::Snapshot>(0, page_store_, *checker_side_->get_remote_memory());
 }
 
 RemoteApp::~RemoteApp()
 {
   initial_snapshot_ = nullptr;
-  if (model_checker_) {
-    shutdown();
-    model_checker_   = nullptr;
-    mc_model_checker = nullptr;
-  }
+  checker_side_     = nullptr;
 }
 
-void RemoteApp::restore_initial_state() const
+void RemoteApp::restore_initial_state()
 {
-  this->initial_snapshot_->restore(model_checker_->get_remote_process_memory());
+  if (initial_snapshot_ == nullptr) { // No memory introspection
+    // We need to destroy the existing CheckerSide before creating the new one, or libevent gets crazy
+    checker_side_.reset(nullptr);
+    checker_side_.reset(new simgrid::mc::CheckerSide(app_args_, true));
+  } else
+    initial_snapshot_->restore(*checker_side_->get_remote_memory());
 }
 
 unsigned long RemoteApp::get_maxpid() const
@@ -149,9 +61,9 @@ unsigned long RemoteApp::get_maxpid() const
   // note: we could maybe cache it and count the actor creation on checker side too.
   // But counting correctly accross state checkpoint/restore would be annoying.
 
-  model_checker_->get_channel().send(MessageType::ACTORS_MAXPID);
+  checker_side_->get_channel().send(MessageType::ACTORS_MAXPID);
   s_mc_message_int_t answer;
-  ssize_t answer_size = model_checker_->get_channel().receive(answer);
+  ssize_t answer_size = checker_side_->get_channel().receive(answer);
   xbt_assert(answer_size != -1, "Could not receive message");
   xbt_assert(answer_size == sizeof answer, "Broken message (size=%zd; expected %zu)", answer_size, sizeof answer);
   xbt_assert(answer.type == MessageType::ACTORS_MAXPID_REPLY,
@@ -170,10 +82,10 @@ void RemoteApp::get_actors_status(std::map<aid_t, ActorState>& whereto) const
   //                    <----- send ACTORS_STATUS_REPLY
   //                    <----- send `N` `s_mc_message_actors_status_one_t` structs
   //                    <----- send `M` `s_mc_message_simcall_probe_one_t` structs
-  model_checker_->get_channel().send(MessageType::ACTORS_STATUS);
+  checker_side_->get_channel().send(MessageType::ACTORS_STATUS);
 
   s_mc_message_actors_status_answer_t answer;
-  ssize_t answer_size = model_checker_->get_channel().receive(answer);
+  ssize_t answer_size = checker_side_->get_channel().receive(answer);
   xbt_assert(answer_size != -1, "Could not receive message");
   xbt_assert(answer_size == sizeof answer, "Broken message (size=%zd; expected %zu)", answer_size, sizeof answer);
   xbt_assert(answer.type == MessageType::ACTORS_STATUS_REPLY,
@@ -191,7 +103,7 @@ void RemoteApp::get_actors_status(std::map<aid_t, ActorState>& whereto) const
   std::vector<s_mc_message_actors_status_one_t> status(answer.count);
   if (answer.count > 0) {
     size_t size      = status.size() * sizeof(s_mc_message_actors_status_one_t);
-    ssize_t received = model_checker_->get_channel().receive(status.data(), size);
+    ssize_t received = checker_side_->get_channel().receive(status.data(), size);
     xbt_assert(static_cast<size_t>(received) == size);
   }
 
@@ -207,7 +119,7 @@ void RemoteApp::get_actors_status(std::map<aid_t, ActorState>& whereto) const
   std::vector<s_mc_message_simcall_probe_one_t> probes(answer.transition_count);
   if (answer.transition_count > 0) {
     for (auto& probe : probes) {
-      ssize_t received = model_checker_->get_channel().receive(probe);
+      ssize_t received = checker_side_->get_channel().receive(probe);
       xbt_assert(received >= 0, "Could not receive response to ACTORS_PROBE message (%s)", strerror(errno));
       xbt_assert(static_cast<size_t>(received) == sizeof probe,
                  "Could not receive response to ACTORS_PROBE message (%zd bytes received != %zu bytes expected",
@@ -238,9 +150,9 @@ void RemoteApp::get_actors_status(std::map<aid_t, ActorState>& whereto) const
 
 void RemoteApp::check_deadlock() const
 {
-  xbt_assert(model_checker_->get_channel().send(MessageType::DEADLOCK_CHECK) == 0, "Could not check deadlock state");
+  xbt_assert(checker_side_->get_channel().send(MessageType::DEADLOCK_CHECK) == 0, "Could not check deadlock state");
   s_mc_message_int_t message;
-  ssize_t received = model_checker_->get_channel().receive(message);
+  ssize_t received = checker_side_->get_channel().receive(message);
   xbt_assert(received != -1, "Could not receive message");
   xbt_assert(received == sizeof message, "Broken message (size=%zd; expected %zu)", received, sizeof message);
   xbt_assert(message.type == MessageType::DEADLOCK_CHECK_REPLY,
@@ -248,39 +160,21 @@ void RemoteApp::check_deadlock() const
              to_c_str(message.type), (int)message.type, (int)MessageType::DEADLOCK_CHECK_REPLY);
 
   if (message.value != 0) {
+    auto* explo = Exploration::get_instance();
     XBT_CINFO(mc_global, "Counter-example execution trace:");
-    for (auto const& frame : model_checker_->get_exploration()->get_textual_trace())
+    for (auto const& frame : explo->get_textual_trace())
       XBT_CINFO(mc_global, "  %s", frame.c_str());
     XBT_INFO("You can debug the problem (and see the whole details) by rerunning out of simgrid-mc with "
              "--cfg=model-check/replay:'%s'",
-             model_checker_->get_exploration()->get_record_trace().to_string().c_str());
-    model_checker_->get_exploration()->log_state();
+             explo->get_record_trace().to_string().c_str());
+    explo->log_state();
     throw DeadlockError();
   }
 }
 
 void RemoteApp::wait_for_requests()
 {
-  /* Resume the application */
-  if (model_checker_->get_channel().send(MessageType::CONTINUE) != 0)
-    throw xbt::errno_error();
-  get_remote_process_memory().clear_cache();
-
-  if (this->get_remote_process_memory().running())
-    model_checker_->channel_handle_events();
-}
-
-void RemoteApp::shutdown()
-{
-  XBT_DEBUG("Shutting down model-checker");
-
-  RemoteProcessMemory& process = get_remote_process_memory();
-  if (process.running()) {
-    XBT_DEBUG("Killing process");
-    finalize_app(true);
-    kill(process.pid(), SIGKILL);
-    process.terminate();
-  }
+  checker_side_->wait_for_requests();
 }
 
 Transition* RemoteApp::handle_simcall(aid_t aid, int times_considered, bool new_transition)
@@ -289,14 +183,15 @@ Transition* RemoteApp::handle_simcall(aid_t aid, int times_considered, bool new_
   m.type                           = MessageType::SIMCALL_EXECUTE;
   m.aid_                           = aid;
   m.times_considered_              = times_considered;
-  model_checker_->get_channel().send(m);
+  checker_side_->get_channel().send(m);
 
-  get_remote_process_memory().clear_cache();
-  if (this->get_remote_process_memory().running())
-    model_checker_->channel_handle_events(); // The app may send messages while processing the transition
+  if (auto* memory = get_remote_process_memory(); memory != nullptr)
+    memory->clear_cache();
+  if (checker_side_->running())
+    checker_side_->dispatch_events(); // The app may send messages while processing the transition
 
   s_mc_message_simcall_execute_answer_t answer;
-  ssize_t s = model_checker_->get_channel().receive(answer);
+  ssize_t s = checker_side_->get_channel().receive(answer);
   xbt_assert(s != -1, "Could not receive message");
   xbt_assert(s == sizeof answer, "Broken message (size=%zd; expected %zu)", s, sizeof answer);
   xbt_assert(answer.type == MessageType::SIMCALL_EXECUTE_ANSWER,
@@ -312,18 +207,7 @@ Transition* RemoteApp::handle_simcall(aid_t aid, int times_considered, bool new_
 
 void RemoteApp::finalize_app(bool terminate_asap)
 {
-  s_mc_message_int_t m = {};
-  m.type               = MessageType::FINALIZE;
-  m.value              = terminate_asap;
-  xbt_assert(model_checker_->get_channel().send(m) == 0, "Could not ask the app to finalize on need");
-
-  s_mc_message_t answer;
-  ssize_t s = model_checker_->get_channel().receive(answer);
-  xbt_assert(s != -1, "Could not receive answer to FINALIZE");
-  xbt_assert(s == sizeof answer, "Broken message (size=%zd; expected %zu)", s, sizeof answer);
-  xbt_assert(answer.type == MessageType::FINALIZE_REPLY,
-             "Received unexpected message %s (%i); expected MessageType::FINALIZE_REPLY (%i)", to_c_str(answer.type),
-             (int)answer.type, (int)MessageType::FINALIZE_REPLY);
+  checker_side_->finalize(terminate_asap);
 }
 
 } // namespace simgrid::mc