Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a59b3aa892a740361d572212b34035635c4d01ff
[simgrid.git] / src / mc / ModelChecker.cpp
1 /* Copyright (c) 2008-2023. 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/ModelChecker.hpp"
7 #include "src/mc/explo/Exploration.hpp"
8 #include "src/mc/explo/LivenessChecker.hpp"
9 #include "src/mc/mc_config.hpp"
10 #include "src/mc/mc_exit.hpp"
11 #include "src/mc/mc_private.hpp"
12 #include "src/mc/sosp/RemoteProcessMemory.hpp"
13 #include "src/mc/transition/TransitionComm.hpp"
14 #include "xbt/automaton.hpp"
15 #include "xbt/system_error.hpp"
16
17 #include <array>
18 #include <csignal>
19 #include <sys/ptrace.h>
20 #include <sys/wait.h>
21
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_ModelChecker, mc, "ModelChecker");
23
24 ::simgrid::mc::ModelChecker* mc_model_checker = nullptr;
25
26 #ifdef __linux__
27 # define WAITPID_CHECKED_FLAGS __WALL
28 #else
29 # define WAITPID_CHECKED_FLAGS 0
30 #endif
31
32 namespace simgrid::mc {
33
34 ModelChecker::ModelChecker(std::unique_ptr<RemoteProcessMemory> remote_memory, int sockfd)
35     : checker_side_(sockfd), remote_process_memory_(std::move(remote_memory))
36 {
37 }
38
39 void ModelChecker::start()
40 {
41   checker_side_.start(
42       [](evutil_socket_t sig, short events, void* arg) {
43         auto mc = static_cast<simgrid::mc::ModelChecker*>(arg);
44         if (events == EV_READ) {
45           std::array<char, MC_MESSAGE_LENGTH> buffer;
46           ssize_t size = mc->checker_side_.get_channel().receive(buffer.data(), buffer.size(), false);
47           if (size == -1 && errno != EAGAIN)
48             throw simgrid::xbt::errno_error();
49
50           if (not mc->handle_message(buffer.data(), size))
51             mc->checker_side_.break_loop();
52         } else if (events == EV_SIGNAL) {
53           if (sig == SIGCHLD)
54             mc->handle_waitpid();
55           else
56             xbt_die("Unexpected signal: %d", sig);
57         } else {
58           xbt_die("Unexpected event");
59         }
60       },
61       this);
62
63   XBT_DEBUG("Waiting for the model-checked process");
64   int status;
65
66   // The model-checked process SIGSTOP itself to signal it's ready:
67   const pid_t pid = remote_process_memory_->pid();
68
69   xbt_assert(waitpid(pid, &status, WAITPID_CHECKED_FLAGS) == pid && WIFSTOPPED(status) && WSTOPSIG(status) == SIGSTOP,
70              "Could not wait model-checked process");
71
72   setup_ignore();
73
74   errno = 0;
75 #ifdef __linux__
76   ptrace(PTRACE_SETOPTIONS, pid, nullptr, PTRACE_O_TRACEEXIT);
77   ptrace(PTRACE_CONT, pid, 0, 0);
78 #elif defined BSD
79   ptrace(PT_CONTINUE, pid, (caddr_t)1, 0);
80 #else
81 # error "no ptrace equivalent coded for this platform"
82 #endif
83   xbt_assert(errno == 0,
84              "Ptrace does not seem to be usable in your setup (errno: %d). "
85              "If you run from within a docker, adding `--cap-add SYS_PTRACE` to the docker line may help. "
86              "If it does not help, please report this bug.",
87              errno);
88 }
89
90 static constexpr auto ignored_local_variables = {
91     std::make_pair("e", "*"),
92     std::make_pair("_log_ev", "*"),
93
94     /* Ignore local variable about time used for tracing */
95     std::make_pair("start_time", "*"),
96 };
97
98 void ModelChecker::setup_ignore()
99 {
100   const RemoteProcessMemory& process = this->get_remote_process_memory();
101   for (auto const& [var, frame] : ignored_local_variables)
102     process.ignore_local_variable(var, frame);
103
104   /* Static variable used for tracing */
105   process.ignore_global_variable("counter");
106 }
107
108 bool ModelChecker::handle_message(const char* buffer, ssize_t size)
109 {
110   s_mc_message_t base_message;
111   xbt_assert(size >= (ssize_t)sizeof(base_message), "Broken message");
112   memcpy(&base_message, buffer, sizeof(base_message));
113
114   switch(base_message.type) {
115     case MessageType::INITIAL_ADDRESSES: {
116       s_mc_message_initial_addresses_t message;
117       xbt_assert(size == sizeof(message), "Broken message. Got %d bytes instead of %d.", (int)size, (int)sizeof(message));
118       memcpy(&message, buffer, sizeof(message));
119
120       get_remote_process_memory().init(message.mmalloc_default_mdp);
121       break;
122     }
123
124     case MessageType::IGNORE_HEAP: {
125       s_mc_message_ignore_heap_t message;
126       xbt_assert(size == sizeof(message), "Broken message");
127       memcpy(&message, buffer, sizeof(message));
128
129       IgnoredHeapRegion region;
130       region.block    = message.block;
131       region.fragment = message.fragment;
132       region.address  = message.address;
133       region.size     = message.size;
134       get_remote_process_memory().ignore_heap(region);
135       break;
136     }
137
138     case MessageType::UNIGNORE_HEAP: {
139       s_mc_message_ignore_memory_t message;
140       xbt_assert(size == sizeof(message), "Broken message");
141       memcpy(&message, buffer, sizeof(message));
142       get_remote_process_memory().unignore_heap((void*)(std::uintptr_t)message.addr, message.size);
143       break;
144     }
145
146     case MessageType::IGNORE_MEMORY: {
147       s_mc_message_ignore_memory_t message;
148       xbt_assert(size == sizeof(message), "Broken message");
149       memcpy(&message, buffer, sizeof(message));
150       this->get_remote_process_memory().ignore_region(message.addr, message.size);
151       break;
152     }
153
154     case MessageType::STACK_REGION: {
155       s_mc_message_stack_region_t message;
156       xbt_assert(size == sizeof(message), "Broken message");
157       memcpy(&message, buffer, sizeof(message));
158       this->get_remote_process_memory().stack_areas().push_back(message.stack_region);
159     } break;
160
161     case MessageType::REGISTER_SYMBOL: {
162       s_mc_message_register_symbol_t message;
163       xbt_assert(size == sizeof(message), "Broken message");
164       memcpy(&message, buffer, sizeof(message));
165       xbt_assert(not message.callback, "Support for client-side function proposition is not implemented.");
166       XBT_DEBUG("Received symbol: %s", message.name.data());
167
168       LivenessChecker::automaton_register_symbol(get_remote_process_memory(), message.name.data(),
169                                                  remote((int*)message.data));
170       break;
171     }
172
173     case MessageType::WAITING:
174       return false;
175
176     case MessageType::ASSERTION_FAILED:
177       exploration_->report_assertion_failure();
178       break;
179
180     default:
181       xbt_die("Unexpected message from model-checked application");
182   }
183   return true;
184 }
185
186 void ModelChecker::handle_waitpid()
187 {
188   XBT_DEBUG("Check for wait event");
189   int status;
190   pid_t pid;
191   while ((pid = waitpid(-1, &status, WNOHANG)) != 0) {
192     if (pid == -1) {
193       if (errno == ECHILD) {
194         // No more children:
195         xbt_assert(not this->get_remote_process_memory().running(), "Inconsistent state");
196         break;
197       } else {
198         XBT_ERROR("Could not wait for pid");
199         throw simgrid::xbt::errno_error();
200       }
201     }
202
203     if (pid == this->get_remote_process_memory().pid()) {
204       // From PTRACE_O_TRACEEXIT:
205 #ifdef __linux__
206       if (status>>8 == (SIGTRAP | (PTRACE_EVENT_EXIT<<8))) {
207         unsigned long eventmsg;
208         xbt_assert(ptrace(PTRACE_GETEVENTMSG, get_remote_process_memory().pid(), 0, &eventmsg) != -1,
209                    "Could not get exit status");
210         status = static_cast<int>(eventmsg);
211         if (WIFSIGNALED(status))
212           exploration_->report_crash(status);
213       }
214 #endif
215
216       // We don't care about non-lethal signals, just reinject them:
217       if (WIFSTOPPED(status)) {
218         XBT_DEBUG("Stopped with signal %i", (int) WSTOPSIG(status));
219         errno = 0;
220 #ifdef __linux__
221         ptrace(PTRACE_CONT, get_remote_process_memory().pid(), 0, WSTOPSIG(status));
222 #elif defined BSD
223         ptrace(PT_CONTINUE, get_remote_process_memory().pid(), (caddr_t)1, WSTOPSIG(status));
224 #endif
225         xbt_assert(errno == 0, "Could not PTRACE_CONT");
226       }
227
228       else if (WIFSIGNALED(status)) {
229         exploration_->report_crash(status);
230       } else if (WIFEXITED(status)) {
231         XBT_DEBUG("Child process is over");
232         this->get_remote_process_memory().terminate();
233       }
234     }
235   }
236 }
237
238 Transition* ModelChecker::handle_simcall(aid_t aid, int times_considered, bool new_transition)
239 {
240   s_mc_message_simcall_execute_t m = {};
241   m.type              = MessageType::SIMCALL_EXECUTE;
242   m.aid_              = aid;
243   m.times_considered_ = times_considered;
244   checker_side_.get_channel().send(m);
245
246   this->remote_process_memory_->clear_cache();
247   if (this->remote_process_memory_->running())
248     checker_side_.dispatch(); // The app may send messages while processing the transition
249
250   s_mc_message_simcall_execute_answer_t answer;
251   ssize_t s = checker_side_.get_channel().receive(answer);
252   xbt_assert(s != -1, "Could not receive message");
253   xbt_assert(s == sizeof answer, "Broken message (size=%zd; expected %zu)", s, sizeof answer);
254   xbt_assert(answer.type == MessageType::SIMCALL_EXECUTE_ANSWER,
255              "Received unexpected message %s (%i); expected MessageType::SIMCALL_EXECUTE_ANSWER (%i)",
256              to_c_str(answer.type), (int)answer.type, (int)MessageType::SIMCALL_EXECUTE_ANSWER);
257
258   if (new_transition) {
259     std::stringstream stream(answer.buffer.data());
260     return deserialize_transition(aid, times_considered, stream);
261   } else
262     return nullptr;
263 }
264
265 } // namespace simgrid::mc