Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'depencencies' of https://framagit.org/simgrid/simgrid into depencencies
[simgrid.git] / src / mc / Session.cpp
1 /* Copyright (c) 2015-2020. 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/internal_config.h" // HAVE_SMPI
10 #if HAVE_SMPI
11 #include "smpi/smpi.h"
12 #endif
13 #include "src/mc/mc_private.hpp"
14 #include "src/mc/mc_state.hpp"
15 #include "xbt/log.h"
16 #include "xbt/system_error.hpp"
17
18 #include <fcntl.h>
19 #ifdef __linux__
20 #include <sys/prctl.h>
21 #endif
22
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_Session, mc, "Model-checker session");
24
25 namespace simgrid {
26 namespace mc {
27
28 static void setup_child_environment(int socket)
29 {
30 #ifdef __linux__
31   // Make sure we do not outlive our parent
32   sigset_t mask;
33   sigemptyset (&mask);
34   xbt_assert(sigprocmask(SIG_SETMASK, &mask, nullptr) >= 0, "Could not unblock signals");
35   xbt_assert(prctl(PR_SET_PDEATHSIG, SIGHUP) == 0, "Could not PR_SET_PDEATHSIG");
36 #endif
37
38   // Remove CLOEXEC to pass the socket to the application
39   int fdflags = fcntl(socket, F_GETFD, 0);
40   xbt_assert(fdflags != -1 && fcntl(socket, F_SETFD, fdflags & ~FD_CLOEXEC) != -1,
41              "Could not remove CLOEXEC for socket");
42
43   // Set environment so that mmalloc gets used in application
44   setenv(MC_ENV_VARIABLE, "1", 1);
45
46   // Disable lazy relocation in the model-checked process to prevent the application from
47   // modifying its .got.plt during snapshot.
48   setenv("LC_BIND_NOW", "1", 1);
49
50   char buffer[64];
51   int res = std::snprintf(buffer, sizeof(buffer), "%i", socket);
52   xbt_assert((size_t)res < sizeof(buffer) && res != -1);
53   setenv(MC_ENV_SOCKET_FD, buffer, 1);
54 }
55
56 Session::Session(const std::function<void()>& code)
57 {
58 #if HAVE_SMPI
59   smpi_init_options();//only performed once
60   xbt_assert(smpi_cfg_privatization() != SmpiPrivStrategies::MMAP,
61              "Please use the dlopen privatization schema when model-checking SMPI code");
62 #endif
63
64   // Create a AF_LOCAL socketpair used for exchanging messages
65   // between the model-checker process (ourselves) and the model-checked
66   // process:
67   int sockets[2];
68   int res = socketpair(AF_LOCAL, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sockets);
69   xbt_assert(res != -1, "Could not create socketpair");
70
71   pid_t pid = fork();
72   xbt_assert(pid >= 0, "Could not fork model-checked process");
73
74   if (pid == 0) { // Child
75     ::close(sockets[1]);
76     setup_child_environment(sockets[0]);
77     code();
78     xbt_die("The model-checked process failed to exec()");
79   }
80
81   // Parent (model-checker):
82   ::close(sockets[0]);
83
84   xbt_assert(mc_model_checker == nullptr, "Did you manage to start the MC twice in this process?");
85
86   std::unique_ptr<simgrid::mc::RemoteClient> process(new simgrid::mc::RemoteClient(pid, sockets[1]));
87   model_checker_.reset(new simgrid::mc::ModelChecker(std::move(process)));
88
89   mc_model_checker = model_checker_.get();
90   mc_model_checker->start();
91 }
92
93 Session::~Session()
94 {
95   this->close();
96 }
97
98 void Session::initialize()
99 {
100   xbt_assert(initial_snapshot_ == nullptr);
101   mc_model_checker->wait_for_requests();
102   initial_snapshot_ = std::make_shared<simgrid::mc::Snapshot>(0);
103 }
104
105 void Session::execute(Transition const& transition)
106 {
107   model_checker_->handle_simcall(transition);
108   model_checker_->wait_for_requests();
109 }
110
111 void Session::restore_initial_state()
112 {
113   this->initial_snapshot_->restore(&mc_model_checker->process());
114 }
115
116 void Session::log_state()
117 {
118   mc_model_checker->getChecker()->log_state();
119
120   if (not _sg_mc_dot_output_file.get().empty()) {
121     fprintf(dot_output, "}\n");
122     fclose(dot_output);
123   }
124   if (getenv("SIMGRID_MC_SYSTEM_STATISTICS")){
125     int ret=system("free");
126     if(ret!=0)XBT_WARN("system call did not return 0, but %d",ret);
127   }
128 }
129
130 void Session::close()
131 {
132   initial_snapshot_ = nullptr;
133   if (model_checker_) {
134     model_checker_->shutdown();
135     model_checker_   = nullptr;
136     mc_model_checker = nullptr;
137   }
138 }
139
140 simgrid::mc::Session* session;
141
142 }
143 }