Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid
[simgrid.git] / src / mc / Session.cpp
1 /* Copyright (c) 2015-2019. The SimGrid Team. All rights reserved.          */
2
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. */
5
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"
12 #include "xbt/log.h"
13 #include "xbt/system_error.hpp"
14
15 #include <fcntl.h>
16 #ifdef __linux__
17 #include <sys/prctl.h>
18 #endif
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_Session, mc, "Model-checker session");
21
22 namespace simgrid {
23 namespace mc {
24
25 static void setup_child_environment(int socket)
26 {
27 #ifdef __linux__
28   // Make sure we do not outlive our parent
29   sigset_t mask;
30   sigemptyset (&mask);
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");
33 #endif
34
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");
39
40   // Set environment so that mmalloc gets used in application
41   setenv(MC_ENV_VARIABLE, "1", 1);
42
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);
46
47   char buffer[64];
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);
51 }
52
53 Session::Session(const std::function<void()>& code)
54 {
55 #if HAVE_SMPI
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");
59 #endif
60
61   // Create a AF_LOCAL socketpair used for exchanging messages
62   // between the model-checker process (ourselves) and the model-checked
63   // process:
64   int sockets[2];
65   int res = socketpair(AF_LOCAL, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sockets);
66   xbt_assert(res != -1, "Could not create socketpair");
67
68   pid_t pid = fork();
69   xbt_assert(pid >= 0, "Could not fork model-checked process");
70
71   if (pid == 0) { // Child
72     ::close(sockets[1]);
73     setup_child_environment(sockets[0]);
74     code();
75     xbt_die("The model-checked process failed to exec()");
76   }
77
78   // Parent (model-checker):
79   ::close(sockets[0]);
80
81   xbt_assert(mc_model_checker == nullptr, "Did you manage to start the MC twice in this process?");
82
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)));
85
86   mc_model_checker = model_checker_.get();
87   mc_model_checker->start();
88 }
89
90 Session::~Session()
91 {
92   this->close();
93 }
94
95 void Session::initialize()
96 {
97   xbt_assert(initial_snapshot_ == nullptr);
98   mc_model_checker->wait_for_requests();
99   initial_snapshot_ = std::make_shared<simgrid::mc::Snapshot>(0);
100 }
101
102 void Session::execute(Transition const& transition)
103 {
104   model_checker_->handle_simcall(transition);
105   model_checker_->wait_for_requests();
106 }
107
108 void Session::restore_initial_state()
109 {
110   this->initial_snapshot_->restore(&mc_model_checker->process());
111 }
112
113 void Session::log_state()
114 {
115   mc_model_checker->getChecker()->log_state();
116
117   if (not _sg_mc_dot_output_file.get().empty()) {
118     fprintf(dot_output, "}\n");
119     fclose(dot_output);
120   }
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);
124   }
125 }
126
127 void Session::close()
128 {
129   initial_snapshot_ = nullptr;
130   if (model_checker_) {
131     model_checker_->shutdown();
132     model_checker_   = nullptr;
133     mc_model_checker = nullptr;
134   }
135 }
136
137 simgrid::mc::Session* session;
138
139 }
140 }