Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / src / mc / simgrid_mc.cpp
1 /* Copyright (c) 2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <exception>
8
9 #include <cstdlib>
10 #include <cstdio>
11 #include <cstring>
12
13 #include <utility>
14
15 #include <signal.h>
16 #include <poll.h>
17
18 #include <unistd.h>
19
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <sys/wait.h>
23 #include <sys/ptrace.h>
24
25 #ifdef __linux__
26 #include <sys/prctl.h>
27 #endif
28
29 #include <xbt/log.h>
30 #include <xbt/system_error.hpp>
31
32 #include "simgrid/sg_config.h"
33 #include "src/xbt_modinter.h"
34
35 #include "src/mc/mc_base.h"
36 #include "src/mc/mc_private.h"
37 #include "src/mc/mc_protocol.h"
38 #include "src/mc/mc_safety.h"
39 #include "src/mc/mc_comm_pattern.h"
40 #include "src/mc/mc_liveness.h"
41 #include "src/mc/mc_exit.h"
42
43 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_main, mc, "Entry point for simgrid-mc");
44
45 /** Execute some code in a forked process */
46 template<class F>
47 static inline
48 pid_t do_fork(F f)
49 {
50   pid_t pid = fork();
51   if (pid < 0)
52     throw simgrid::xbt::errno_error(errno, "Could not fork model-checked process");
53   if (pid != 0)
54     return pid;
55
56   // Child-process:
57   try {
58     f();
59     std::exit(EXIT_SUCCESS);
60   }
61   catch(...) {
62     // The callback should catch exceptions:
63     abort();
64   }
65 }
66
67 static
68 int exec_model_checked(int socket, char** argv)
69 {
70   XBT_DEBUG("Inside the child process PID=%i", (int) getpid());
71
72 #ifdef __linux__
73   // Make sure we do not outlive our parent:
74   sigset_t mask;
75   sigemptyset (&mask);
76   if (sigprocmask(SIG_SETMASK, &mask, nullptr) < 0)
77     throw simgrid::xbt::errno_error(errno, "Could not unblock signals");
78   if (prctl(PR_SET_PDEATHSIG, SIGHUP) != 0)
79     throw simgrid::xbt::errno_error(errno, "Could not PR_SET_PDEATHSIG");
80 #endif
81
82   int res;
83
84   // Remove CLOEXEC in order to pass the socket to the exec-ed program:
85   int fdflags = fcntl(socket, F_GETFD, 0);
86   if (fdflags == -1 || fcntl(socket, F_SETFD, fdflags & ~FD_CLOEXEC) == -1)
87     throw simgrid::xbt::errno_error(errno, "Could not remove CLOEXEC for socket");
88
89   // Set environment:
90   setenv(MC_ENV_VARIABLE, "1", 1);
91
92   // Disable lazy relocation in the model-checked process.
93   // We don't want the model-checked process to modify its .got.plt during
94   // snapshot.
95   setenv("LC_BIND_NOW", "1", 1);
96
97   char buffer[64];
98   res = std::snprintf(buffer, sizeof(buffer), "%i", socket);
99   if ((size_t) res >= sizeof(buffer) || res == -1)
100     std::abort();
101   setenv(MC_ENV_SOCKET_FD, buffer, 1);
102
103   execvp(argv[1], argv+1);
104
105   XBT_ERROR("Could not run the model-checked program");
106   // This is the value used by system() and popen() in this case:
107   return 127;
108 }
109
110 static
111 std::pair<pid_t, int> create_model_checked(char** argv)
112 {
113   // Create a AF_LOCAL socketpair used for exchanging messages
114   // bewteen the model-checker process (ourselves) and the model-checked
115   // process:
116   int res;
117   int sockets[2];
118   res = socketpair(AF_LOCAL, SOCK_DGRAM | SOCK_CLOEXEC, 0, sockets);
119   if (res == -1)
120     throw simgrid::xbt::errno_error(errno, "Could not create socketpair");
121
122   pid_t pid = do_fork([&] {
123     close(sockets[1]);
124     int res = exec_model_checked(sockets[0], argv);
125     XBT_DEBUG("Error in the child process creation");
126     exit(res);
127   });
128
129   // Parent (model-checker):
130   close(sockets[0]);
131   return std::make_pair(pid, sockets[1]);
132 }
133
134 static
135 char** argvdup(int argc, char** argv)
136 {
137   char** argv_copy = xbt_new(char*, argc+1);
138   std::memcpy(argv_copy, argv, sizeof(char*) * argc);
139   argv_copy[argc] = NULL;
140   return argv_copy;
141 }
142
143 int main(int argc, char** argv)
144 {
145   try {
146     if (argc < 2)
147       xbt_die("Missing arguments.\n");
148
149     _sg_do_model_check = 1;
150
151     // The initialisation function can touch argv.
152     // We need to keep the original parameters in order to pass them to the
153     // model-checked process so we make a copy of them:
154     int argc_copy = argc;
155     char** argv_copy = argvdup(argc, argv);
156     xbt_log_init(&argc_copy, argv_copy);
157     sg_config_init(&argc_copy, argv_copy);
158
159     int sock;
160     pid_t model_checked_pid;
161     std::tie(model_checked_pid, sock) = create_model_checked(argv);
162     XBT_DEBUG("Inside the parent process");
163     if (mc_model_checker)
164       xbt_die("MC server already present");
165
166     mc_mode = MC_MODE_SERVER;
167     std::unique_ptr<simgrid::mc::Process> process(new simgrid::mc::Process(model_checked_pid, sock));
168     process->privatized(sg_cfg_get_boolean("smpi/privatize_global_variables"));
169     mc_model_checker = new simgrid::mc::ModelChecker(std::move(process));
170     mc_model_checker->start();
171     int res = 0;
172     if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
173       res = MC_modelcheck_comm_determinism();
174     else if (!_sg_mc_property_file || _sg_mc_property_file[0] == '\0')
175       res = MC_modelcheck_safety();
176     else
177       res = MC_modelcheck_liveness();
178     mc_model_checker->shutdown();
179     return res;
180   }
181   catch(std::exception& e) {
182     XBT_ERROR("Exception: %s", e.what());
183     return SIMGRID_MC_EXIT_ERROR;
184   }
185   catch(...) {
186     XBT_ERROR("Unknown exception");
187     return SIMGRID_MC_EXIT_ERROR;
188   }
189 }