1 /* Copyright (c) 2015-2020. 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/mc/checker/Checker.hpp"
8 #include "src/mc/mc_config.hpp"
9 #include "src/internal_config.h" // HAVE_SMPI
11 #include "smpi/smpi.h"
13 #include "src/mc/mc_private.hpp"
14 #include "src/mc/mc_state.hpp"
16 #include "xbt/system_error.hpp"
20 #include <sys/prctl.h>
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_Session, mc, "Model-checker session");
28 static void setup_child_environment(int socket)
30 /* On startup, simix_global_init() calls simgrid::mc::Client::initialize(), which checks whether the MC_ENV_SOCKET_FD
31 * env variable is set. If so, MC mode is assumed, and the client is setup from its side
35 // Make sure we do not outlive our parent
38 xbt_assert(sigprocmask(SIG_SETMASK, &mask, nullptr) >= 0, "Could not unblock signals");
39 xbt_assert(prctl(PR_SET_PDEATHSIG, SIGHUP) == 0, "Could not PR_SET_PDEATHSIG");
42 // Remove CLOEXEC to pass the socket to the application
43 int fdflags = fcntl(socket, F_GETFD, 0);
44 xbt_assert(fdflags != -1 && fcntl(socket, F_SETFD, fdflags & ~FD_CLOEXEC) != -1,
45 "Could not remove CLOEXEC for socket");
47 // Set environment so that mmalloc gets used in application
48 setenv(MC_ENV_VARIABLE, "1", 1);
50 // Disable lazy relocation in the model-checked process to prevent the application from
51 // modifying its .got.plt during snapshot.
52 setenv("LC_BIND_NOW", "1", 1);
55 int res = std::snprintf(buffer, sizeof(buffer), "%i", socket);
56 xbt_assert((size_t)res < sizeof(buffer) && res != -1);
57 setenv(MC_ENV_SOCKET_FD, buffer, 1);
60 Session::Session(const std::function<void()>& code)
63 smpi_init_options();//only performed once
64 xbt_assert(smpi_cfg_privatization() != SmpiPrivStrategies::MMAP,
65 "Please use the dlopen privatization schema when model-checking SMPI code");
68 // Create a AF_LOCAL socketpair used for exchanging messages
69 // between the model-checker process (ourselves) and the model-checked
72 int res = socketpair(AF_LOCAL, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sockets);
73 xbt_assert(res != -1, "Could not create socketpair");
76 xbt_assert(pid >= 0, "Could not fork model-checked process");
78 if (pid == 0) { // Child
80 setup_child_environment(sockets[0]);
82 xbt_die("The model-checked process failed to exec(): %s", strerror(errno));
85 // Parent (model-checker):
88 xbt_assert(mc_model_checker == nullptr, "Did you manage to start the MC twice in this process?");
90 std::unique_ptr<simgrid::mc::RemoteClient> process(new simgrid::mc::RemoteClient(pid, sockets[1]));
91 model_checker_.reset(new simgrid::mc::ModelChecker(std::move(process)));
93 mc_model_checker = model_checker_.get();
94 mc_model_checker->start();
102 /** Take the initial snapshot of the application, that must be stopped. */
103 void Session::initialize()
105 xbt_assert(initial_snapshot_ == nullptr);
106 mc_model_checker->wait_for_requests();
107 initial_snapshot_ = std::make_shared<simgrid::mc::Snapshot>(0);
110 void Session::execute(Transition const& transition)
112 model_checker_->handle_simcall(transition);
113 model_checker_->wait_for_requests();
116 void Session::restore_initial_state()
118 this->initial_snapshot_->restore(&mc_model_checker->process());
121 void Session::log_state()
123 mc_model_checker->getChecker()->log_state();
125 if (not _sg_mc_dot_output_file.get().empty()) {
126 fprintf(dot_output, "}\n");
129 if (getenv("SIMGRID_MC_SYSTEM_STATISTICS")){
130 int ret=system("free");
132 XBT_WARN("Call to system(free) did not return 0, but %d", ret);
136 void Session::close()
138 initial_snapshot_ = nullptr;
139 if (model_checker_) {
140 model_checker_->shutdown();
141 model_checker_ = nullptr;
142 mc_model_checker = nullptr;
146 simgrid::mc::Session* session;