Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Documentation for Type and Member
[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 <signal.h>
14 #include <poll.h>
15
16 #include <unistd.h>
17
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <sys/wait.h>
21 #include <sys/ptrace.h>
22
23 #ifdef __linux__
24 #include <sys/prctl.h>
25 #endif
26
27 #include <xbt/log.h>
28
29 #include "simgrid/sg_config.h"
30 #include "src/xbt_modinter.h"
31
32 #include "src/mc/mc_base.h"
33 #include "src/mc/mc_private.h"
34 #include "src/mc/mc_protocol.h"
35 #include "src/mc/mc_safety.h"
36 #include "src/mc/mc_comm_pattern.h"
37 #include "src/mc/mc_liveness.h"
38 #include "src/mc/mc_exit.h"
39
40 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_main, mc, "Entry point for simgrid-mc");
41
42 static int do_child(int socket, char** argv)
43 {
44   XBT_DEBUG("Inside the child process PID=%i", (int) getpid());
45
46 #ifdef __linux__
47   // Make sure we do not outlive our parent:
48   sigset_t mask;
49   sigemptyset (&mask);
50   if (sigprocmask(SIG_SETMASK, &mask, nullptr) < 0) {
51     std::perror ("sigprocmask");
52     return SIMGRID_MC_EXIT_ERROR;
53   }
54
55   if (prctl(PR_SET_PDEATHSIG, SIGHUP) != 0) {
56     std::perror("simgrid-mc");
57     return SIMGRID_MC_EXIT_ERROR;
58   }
59 #endif
60
61   int res;
62
63   // Remove CLOEXEC in order to pass the socket to the exec-ed program:
64   int fdflags = fcntl(socket, F_GETFD, 0);
65   if (fdflags == -1) {
66     std::perror("simgrid-mc");
67     return SIMGRID_MC_EXIT_ERROR;
68   }
69   if (fcntl(socket, F_SETFD, fdflags & ~FD_CLOEXEC) == -1) {
70     std::perror("simgrid-mc");
71     return SIMGRID_MC_EXIT_ERROR;
72   }
73
74   XBT_DEBUG("CLOEXEC removed on socket %i", socket);
75
76   // Set environment:
77   setenv(MC_ENV_VARIABLE, "1", 1);
78
79   // Disable lazy relocation in the model-ched process.
80   // We don't want the model-checked process to modify its .got.plt during
81   // snapshot.
82   setenv("LC_BIND_NOW", "1", 1);
83
84   char buffer[64];
85   res = std::snprintf(buffer, sizeof(buffer), "%i", socket);
86   if ((size_t) res >= sizeof(buffer) || res == -1)
87     return SIMGRID_MC_EXIT_ERROR;
88   setenv(MC_ENV_SOCKET_FD, buffer, 1);
89
90   execvp(argv[1], argv+1);
91   XBT_ERROR("Could not execute the child process");
92   return SIMGRID_MC_EXIT_ERROR;
93 }
94
95 static int do_parent(int socket, pid_t child)
96 {
97   XBT_DEBUG("Inside the parent process");
98   if (mc_model_checker)
99     xbt_die("MC server already present");
100   try {
101     mc_mode = MC_MODE_SERVER;
102     std::unique_ptr<simgrid::mc::Process> process(new simgrid::mc::Process(child, socket));
103     process->privatized(sg_cfg_get_boolean("smpi/privatize_global_variables"));
104     mc_model_checker = new simgrid::mc::ModelChecker(std::move(process));
105     mc_model_checker->start();
106     int res = 0;
107     if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
108       res = MC_modelcheck_comm_determinism();
109     else if (!_sg_mc_property_file || _sg_mc_property_file[0] == '\0')
110       res = MC_modelcheck_safety();
111     else
112       res = MC_modelcheck_liveness();
113     mc_model_checker->shutdown();
114     return res;
115   }
116   catch(std::exception& e) {
117     XBT_ERROR("Exception: %s", e.what());
118     return SIMGRID_MC_EXIT_ERROR;
119   }
120 }
121
122 static char** argvdup(int argc, char** argv)
123 {
124   char** argv_copy = xbt_new(char*, argc+1);
125   std::memcpy(argv_copy, argv, sizeof(char*) * argc);
126   argv_copy[argc] = NULL;
127   return argv_copy;
128 }
129
130 int main(int argc, char** argv)
131 {
132   _sg_do_model_check = 1;
133
134   // We need to keep the original parameters in order to pass them to the
135   // model-checked process:
136   int argc_copy = argc;
137   char** argv_copy = argvdup(argc, argv);
138   xbt_log_init(&argc_copy, argv_copy);
139   sg_config_init(&argc_copy, argv_copy);
140
141   if (argc < 2)
142     xbt_die("Missing arguments.\n");
143
144   // Create a AF_LOCAL socketpair:
145   int res;
146
147   int sockets[2];
148   res = socketpair(AF_LOCAL, SOCK_DGRAM | SOCK_CLOEXEC, 0, sockets);
149   if (res == -1) {
150     perror("simgrid-mc");
151     return SIMGRID_MC_EXIT_ERROR;
152   }
153
154   XBT_DEBUG("Created socketpair");
155
156   pid_t pid = fork();
157   if (pid < 0) {
158     perror("simgrid-mc");
159     return SIMGRID_MC_EXIT_ERROR;
160   } else if (pid == 0) {
161     close(sockets[1]);
162     int res = do_child(sockets[0], argv);
163     XBT_DEBUG("Error in the child process creation");
164     return res;
165   } else {
166     close(sockets[0]);
167     return do_parent(sockets[1], pid);
168   }
169
170   return 0;
171 }