Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'coverity_scan' of github.com:mquinson/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 <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     mc_model_checker = new simgrid::mc::ModelChecker(child, socket);
103     mc_model_checker->start();
104     int res = 0;
105     if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
106       res = MC_modelcheck_comm_determinism();
107     else if (!_sg_mc_property_file || _sg_mc_property_file[0] == '\0')
108       res = MC_modelcheck_safety();
109     else
110       res = MC_modelcheck_liveness();
111     mc_model_checker->shutdown();
112     return res;
113   }
114   catch(std::exception& e) {
115     XBT_ERROR("Exception: %s", e.what());
116     return SIMGRID_MC_EXIT_ERROR;
117   }
118 }
119
120 static char** argvdup(int argc, char** argv)
121 {
122   char** argv_copy = xbt_new(char*, argc+1);
123   std::memcpy(argv_copy, argv, sizeof(char*) * argc);
124   argv_copy[argc] = NULL;
125   return argv_copy;
126 }
127
128 int main(int argc, char** argv)
129 {
130   _sg_do_model_check = 1;
131
132   // We need to keep the original parameters in order to pass them to the
133   // model-checked process:
134   int argc_copy = argc;
135   char** argv_copy = argvdup(argc, argv);
136   xbt_log_init(&argc_copy, argv_copy);
137   sg_config_init(&argc_copy, argv_copy);
138
139   if (argc < 2)
140     xbt_die("Missing arguments.\n");
141
142   // Create a AF_LOCAL socketpair:
143   int res;
144
145   int sockets[2];
146   res = socketpair(AF_LOCAL, SOCK_DGRAM | SOCK_CLOEXEC, 0, sockets);
147   if (res == -1) {
148     perror("simgrid-mc");
149     return SIMGRID_MC_EXIT_ERROR;
150   }
151
152   XBT_DEBUG("Created socketpair");
153
154   pid_t pid = fork();
155   if (pid < 0) {
156     perror("simgrid-mc");
157     return SIMGRID_MC_EXIT_ERROR;
158   } else if (pid == 0) {
159     close(sockets[1]);
160     int res = do_child(sockets[0], argv);
161     XBT_DEBUG("Error in the child process creation");
162     return res;
163   } else {
164     close(sockets[0]);
165     return do_parent(sockets[1], pid);
166   }
167
168   return 0;
169 }