1 /* Copyright (c) 2015-2019. 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/smpi/include/smpi_config.hpp"
10 #include "src/mc/mc_private.hpp"
11 #include "src/mc/mc_state.hpp"
13 #include "xbt/system_error.hpp"
17 #include <sys/prctl.h>
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_Session, mc, "Model-checker session");
25 static void setup_child_environment(int socket)
28 // Make sure we do not outlive our parent
31 xbt_assert(sigprocmask(SIG_SETMASK, &mask, nullptr) >= 0, "Could not unblock signals");
32 xbt_assert(prctl(PR_SET_PDEATHSIG, SIGHUP) == 0, "Could not PR_SET_PDEATHSIG");
35 // Remove CLOEXEC to pass the socket to the application
36 int fdflags = fcntl(socket, F_GETFD, 0);
37 xbt_assert(fdflags != -1 && fcntl(socket, F_SETFD, fdflags & ~FD_CLOEXEC) != -1,
38 "Could not remove CLOEXEC for socket");
40 // Set environment so that mmalloc gets used in application
41 setenv(MC_ENV_VARIABLE, "1", 1);
43 // Disable lazy relocation in the model-checked process to prevent the application from
44 // modifying its .got.plt during snapshot.
45 setenv("LC_BIND_NOW", "1", 1);
48 int res = std::snprintf(buffer, sizeof(buffer), "%i", socket);
49 xbt_assert((size_t)res < sizeof(buffer) && res != -1);
50 setenv(MC_ENV_SOCKET_FD, buffer, 1);
53 Session::Session(const std::function<void()>& code)
56 smpi_init_options();//only performed once
57 xbt_assert(smpi_cfg_privatization() != SmpiPrivStrategies::MMAP,
58 "Please use the dlopen privatization schema when model-checking SMPI code");
61 // Create a AF_LOCAL socketpair used for exchanging messages
62 // between the model-checker process (ourselves) and the model-checked
65 int res = socketpair(AF_LOCAL, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sockets);
66 xbt_assert(res != -1, "Could not create socketpair");
69 xbt_assert(pid >= 0, "Could not fork model-checked process");
71 if (pid == 0) { // Child
73 setup_child_environment(sockets[0]);
75 xbt_die("The model-checked process failed to exec()");
78 // Parent (model-checker):
81 xbt_assert(mc_model_checker == nullptr, "Did you manage to start the MC twice in this process?");
83 std::unique_ptr<simgrid::mc::RemoteClient> process(new simgrid::mc::RemoteClient(pid, sockets[1]));
84 model_checker_.reset(new simgrid::mc::ModelChecker(std::move(process)));
86 mc_model_checker = model_checker_.get();
87 mc_model_checker->start();
95 void Session::initialize()
97 xbt_assert(initial_snapshot_ == nullptr);
98 mc_model_checker->wait_for_requests();
99 initial_snapshot_ = std::make_shared<simgrid::mc::Snapshot>(0);
102 void Session::execute(Transition const& transition)
104 model_checker_->handle_simcall(transition);
105 model_checker_->wait_for_requests();
108 void Session::restore_initial_state()
110 this->initial_snapshot_->restore(&mc_model_checker->process());
113 void Session::log_state()
115 mc_model_checker->getChecker()->log_state();
117 if (not _sg_mc_dot_output_file.get().empty()) {
118 fprintf(dot_output, "}\n");
121 if (getenv("SIMGRID_MC_SYSTEM_STATISTICS")){
122 int ret=system("free");
123 if(ret!=0)XBT_WARN("system call 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 simgrid::mc::Session* session;