Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
10f0ba30c6fcb107a63a40744b4ea8f2525d3f8f
[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   errno = 0;
73 #ifdef __linux__
74   ptrace(PTRACE_SETOPTIONS, pid, nullptr, PTRACE_O_TRACEEXIT);
75   ptrace(PTRACE_CONT, pid, 0, 0);
76 #elif defined BSD
77   ptrace(PT_CONTINUE, pid, (caddr_t)1, 0);
78 #else
79 # error "no ptrace equivalent coded for this platform"
80 #endif
81   xbt_assert(errno == 0,
82              "Ptrace does not seem to be usable in your setup (errno: %d). "
83              "If you run from within a docker, adding `--cap-add SYS_PTRACE` to the docker line may help. "
84              "If it does not help, please report this bug.",
85              errno);
86 }
87
88 bool ModelChecker::handle_message(const char* buffer, ssize_t size)
89 {
90   s_mc_message_t base_message;
91   xbt_assert(size >= (ssize_t)sizeof(base_message), "Broken message");
92   memcpy(&base_message, buffer, sizeof(base_message));
93
94   switch(base_message.type) {
95     case MessageType::INITIAL_ADDRESSES: {
96       s_mc_message_initial_addresses_t message;
97       xbt_assert(size == sizeof(message), "Broken message. Got %d bytes instead of %d.", (int)size, (int)sizeof(message));
98       memcpy(&message, buffer, sizeof(message));
99
100       get_remote_process_memory().init(message.mmalloc_default_mdp);
101       break;
102     }
103
104     case MessageType::IGNORE_HEAP: {
105       s_mc_message_ignore_heap_t message;
106       xbt_assert(size == sizeof(message), "Broken message");
107       memcpy(&message, buffer, sizeof(message));
108
109       IgnoredHeapRegion region;
110       region.block    = message.block;
111       region.fragment = message.fragment;
112       region.address  = message.address;
113       region.size     = message.size;
114       get_remote_process_memory().ignore_heap(region);
115       break;
116     }
117
118     case MessageType::UNIGNORE_HEAP: {
119       s_mc_message_ignore_memory_t message;
120       xbt_assert(size == sizeof(message), "Broken message");
121       memcpy(&message, buffer, sizeof(message));
122       get_remote_process_memory().unignore_heap((void*)(std::uintptr_t)message.addr, message.size);
123       break;
124     }
125
126     case MessageType::IGNORE_MEMORY: {
127       s_mc_message_ignore_memory_t message;
128       xbt_assert(size == sizeof(message), "Broken message");
129       memcpy(&message, buffer, sizeof(message));
130       this->get_remote_process_memory().ignore_region(message.addr, message.size);
131       break;
132     }
133
134     case MessageType::STACK_REGION: {
135       s_mc_message_stack_region_t message;
136       xbt_assert(size == sizeof(message), "Broken message");
137       memcpy(&message, buffer, sizeof(message));
138       this->get_remote_process_memory().stack_areas().push_back(message.stack_region);
139     } break;
140
141     case MessageType::REGISTER_SYMBOL: {
142       s_mc_message_register_symbol_t message;
143       xbt_assert(size == sizeof(message), "Broken message");
144       memcpy(&message, buffer, sizeof(message));
145       xbt_assert(not message.callback, "Support for client-side function proposition is not implemented.");
146       XBT_DEBUG("Received symbol: %s", message.name.data());
147
148       LivenessChecker::automaton_register_symbol(get_remote_process_memory(), message.name.data(),
149                                                  remote((int*)message.data));
150       break;
151     }
152
153     case MessageType::WAITING:
154       return false;
155
156     case MessageType::ASSERTION_FAILED:
157       exploration_->report_assertion_failure();
158       break;
159
160     default:
161       xbt_die("Unexpected message from model-checked application");
162   }
163   return true;
164 }
165
166 void ModelChecker::handle_waitpid()
167 {
168   XBT_DEBUG("Check for wait event");
169   int status;
170   pid_t pid;
171   while ((pid = waitpid(-1, &status, WNOHANG)) != 0) {
172     if (pid == -1) {
173       if (errno == ECHILD) {
174         // No more children:
175         xbt_assert(not this->get_remote_process_memory().running(), "Inconsistent state");
176         break;
177       } else {
178         XBT_ERROR("Could not wait for pid");
179         throw simgrid::xbt::errno_error();
180       }
181     }
182
183     if (pid == this->get_remote_process_memory().pid()) {
184       // From PTRACE_O_TRACEEXIT:
185 #ifdef __linux__
186       if (status>>8 == (SIGTRAP | (PTRACE_EVENT_EXIT<<8))) {
187         unsigned long eventmsg;
188         xbt_assert(ptrace(PTRACE_GETEVENTMSG, get_remote_process_memory().pid(), 0, &eventmsg) != -1,
189                    "Could not get exit status");
190         status = static_cast<int>(eventmsg);
191         if (WIFSIGNALED(status))
192           exploration_->report_crash(status);
193       }
194 #endif
195
196       // We don't care about non-lethal signals, just reinject them:
197       if (WIFSTOPPED(status)) {
198         XBT_DEBUG("Stopped with signal %i", (int) WSTOPSIG(status));
199         errno = 0;
200 #ifdef __linux__
201         ptrace(PTRACE_CONT, get_remote_process_memory().pid(), 0, WSTOPSIG(status));
202 #elif defined BSD
203         ptrace(PT_CONTINUE, get_remote_process_memory().pid(), (caddr_t)1, WSTOPSIG(status));
204 #endif
205         xbt_assert(errno == 0, "Could not PTRACE_CONT");
206       }
207
208       else if (WIFSIGNALED(status)) {
209         exploration_->report_crash(status);
210       } else if (WIFEXITED(status)) {
211         XBT_DEBUG("Child process is over");
212         this->get_remote_process_memory().terminate();
213       }
214     }
215   }
216 }
217
218 Transition* ModelChecker::handle_simcall(aid_t aid, int times_considered, bool new_transition)
219 {
220   s_mc_message_simcall_execute_t m = {};
221   m.type              = MessageType::SIMCALL_EXECUTE;
222   m.aid_              = aid;
223   m.times_considered_ = times_considered;
224   checker_side_.get_channel().send(m);
225
226   this->remote_process_memory_->clear_cache();
227   if (this->remote_process_memory_->running())
228     checker_side_.dispatch(); // The app may send messages while processing the transition
229
230   s_mc_message_simcall_execute_answer_t answer;
231   ssize_t s = checker_side_.get_channel().receive(answer);
232   xbt_assert(s != -1, "Could not receive message");
233   xbt_assert(s == sizeof answer, "Broken message (size=%zd; expected %zu)", s, sizeof answer);
234   xbt_assert(answer.type == MessageType::SIMCALL_EXECUTE_ANSWER,
235              "Received unexpected message %s (%i); expected MessageType::SIMCALL_EXECUTE_ANSWER (%i)",
236              to_c_str(answer.type), (int)answer.type, (int)MessageType::SIMCALL_EXECUTE_ANSWER);
237
238   if (new_transition) {
239     std::stringstream stream(answer.buffer.data());
240     return deserialize_transition(aid, times_considered, stream);
241   } else
242     return nullptr;
243 }
244
245 } // namespace simgrid::mc