1 /* Copyright (c) 2015. The SimGrid Team.
2 * All rights reserved. */
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. */
18 #include <sys/types.h>
19 #include <sys/socket.h>
21 #include <sys/ptrace.h>
24 #include <sys/prctl.h>
29 #include "simgrid/sg_config.h"
30 #include "src/xbt_modinter.h"
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"
40 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_main, mc, "Entry point for simgrid-mc");
42 static int do_child(int socket, char** argv)
44 XBT_DEBUG("Inside the child process PID=%i", (int) getpid());
47 // Make sure we do not outlive our parent:
50 if (sigprocmask(SIG_SETMASK, &mask, nullptr) < 0) {
51 std::perror ("sigprocmask");
52 return SIMGRID_MC_EXIT_ERROR;
55 if (prctl(PR_SET_PDEATHSIG, SIGHUP) != 0) {
56 std::perror("simgrid-mc");
57 return SIMGRID_MC_EXIT_ERROR;
63 // Remove CLOEXEC in order to pass the socket to the exec-ed program:
64 int fdflags = fcntl(socket, F_GETFD, 0);
66 std::perror("simgrid-mc");
67 return SIMGRID_MC_EXIT_ERROR;
69 if (fcntl(socket, F_SETFD, fdflags & ~FD_CLOEXEC) == -1) {
70 std::perror("simgrid-mc");
71 return SIMGRID_MC_EXIT_ERROR;
74 XBT_DEBUG("CLOEXEC removed on socket %i", socket);
77 setenv(MC_ENV_VARIABLE, "1", 1);
79 // Disable lazy relocation in the model-ched process.
80 // We don't want the model-checked process to modify its .got.plt during
82 setenv("LC_BIND_NOW", "1", 1);
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);
90 execvp(argv[1], argv+1);
91 XBT_ERROR("Could not execute the child process");
92 return SIMGRID_MC_EXIT_ERROR;
95 static int do_parent(int socket, pid_t child)
97 XBT_DEBUG("Inside the parent process");
99 xbt_die("MC server already present");
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();
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();
112 res = MC_modelcheck_liveness();
113 mc_model_checker->shutdown();
116 catch(std::exception& e) {
117 XBT_ERROR("Exception: %s", e.what());
118 return SIMGRID_MC_EXIT_ERROR;
122 static char** argvdup(int argc, char** argv)
124 char** argv_copy = xbt_new(char*, argc+1);
125 std::memcpy(argv_copy, argv, sizeof(char*) * argc);
126 argv_copy[argc] = NULL;
130 int main(int argc, char** argv)
132 _sg_do_model_check = 1;
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);
142 xbt_die("Missing arguments.\n");
144 // Create a AF_LOCAL socketpair:
148 res = socketpair(AF_LOCAL, SOCK_DGRAM | SOCK_CLOEXEC, 0, sockets);
150 perror("simgrid-mc");
151 return SIMGRID_MC_EXIT_ERROR;
154 XBT_DEBUG("Created socketpair");
158 perror("simgrid-mc");
159 return SIMGRID_MC_EXIT_ERROR;
160 } else if (pid == 0) {
162 int res = do_child(sockets[0], argv);
163 XBT_DEBUG("Error in the child process creation");
167 return do_parent(sockets[1], pid);