Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
moved a line for comprehension
[simgrid.git] / src / mc / ModelChecker.cpp
1 /* Copyright (c) 2008-2020. 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/Session.hpp"
8 #include "src/mc/Transition.hpp"
9 #include "src/mc/checker/Checker.hpp"
10 #include "src/mc/mc_config.hpp"
11 #include "src/mc/mc_exit.hpp"
12 #include "src/mc/mc_private.hpp"
13 #include "src/mc/remote/RemoteSimulation.hpp"
14 #include "xbt/automaton.hpp"
15 #include "xbt/system_error.hpp"
16
17 #include <sys/ptrace.h>
18 #include <sys/wait.h>
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_ModelChecker, mc, "ModelChecker");
21
22 ::simgrid::mc::ModelChecker* mc_model_checker = nullptr;
23
24 using simgrid::mc::remote;
25
26 #ifdef __linux__
27 # define WAITPID_CHECKED_FLAGS __WALL
28 #else
29 # define WAITPID_CHECKED_FLAGS 0
30 #endif
31
32 namespace simgrid {
33 namespace mc {
34
35 ModelChecker::ModelChecker(std::unique_ptr<RemoteSimulation> remote_simulation, int sockfd)
36     : checker_side_(sockfd), remote_simulation_(std::move(remote_simulation))
37 {
38 }
39
40 void ModelChecker::start()
41 {
42   checker_side_.start([](evutil_socket_t sig, short events, void* arg) {
43     auto mc = static_cast<simgrid::mc::ModelChecker*>(arg);
44     if (events == EV_READ) {
45       char buffer[MC_MESSAGE_LENGTH];
46       ssize_t size = mc->checker_side_.get_channel().receive(buffer, sizeof(buffer), false);
47       if (size == -1 && errno != EAGAIN)
48         throw simgrid::xbt::errno_error();
49
50       if (not mc->handle_message(buffer, 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 event");
57     }
58   });
59
60   XBT_DEBUG("Waiting for the model-checked process");
61   int status;
62
63   // The model-checked process SIGSTOP itself to signal it's ready:
64   const pid_t pid = remote_simulation_->pid();
65
66   pid_t res = waitpid(pid, &status, WAITPID_CHECKED_FLAGS);
67   if (res < 0 || not WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP)
68     xbt_die("Could not wait model-checked process");
69
70   remote_simulation_->init();
71
72   if (not _sg_mc_dot_output_file.get().empty())
73     MC_init_dot_output();
74
75   setup_ignore();
76
77 #ifdef __linux__
78   ptrace(PTRACE_SETOPTIONS, pid, nullptr, PTRACE_O_TRACEEXIT);
79   ptrace(PTRACE_CONT, pid, 0, 0);
80 #elif defined BSD
81   ptrace(PT_CONTINUE, pid, (caddr_t)1, 0);
82 #else
83 # error "no ptrace equivalent coded for this platform"
84 #endif
85 }
86
87 static const std::pair<const char*, const char*> ignored_local_variables[] = {
88   std::pair<const char*, const char*>{  "e", "*" },
89   std::pair<const char*, const char*>{ "_log_ev", "*" },
90
91   /* Ignore local variable about time used for tracing */
92   std::pair<const char*, const char*>{ "start_time", "*" },
93 };
94
95 void ModelChecker::setup_ignore()
96 {
97   RemoteSimulation& process = this->get_remote_simulation();
98   for (std::pair<const char*, const char*> const& var :
99       ignored_local_variables)
100     process.ignore_local_variable(var.first, var.second);
101
102   /* Static variable used for tracing */
103   process.ignore_global_variable("counter");
104 }
105
106 void ModelChecker::shutdown()
107 {
108   XBT_DEBUG("Shuting down model-checker");
109
110   RemoteSimulation* process = &this->get_remote_simulation();
111   if (process->running()) {
112     XBT_DEBUG("Killing process");
113     kill(process->pid(), SIGKILL);
114     process->terminate();
115   }
116 }
117
118 void ModelChecker::resume(RemoteSimulation& process)
119 {
120   int res = checker_side_.get_channel().send(MC_MESSAGE_CONTINUE);
121   if (res)
122     throw xbt::errno_error();
123   process.clear_cache();
124 }
125
126 static void MC_report_crash(int status)
127 {
128   XBT_INFO("**************************");
129   XBT_INFO("** CRASH IN THE PROGRAM **");
130   XBT_INFO("**************************");
131   if (WIFSIGNALED(status))
132     XBT_INFO("From signal: %s", strsignal(WTERMSIG(status)));
133   else if (WIFEXITED(status))
134     XBT_INFO("From exit: %i", WEXITSTATUS(status));
135   if (not xbt_log_no_loc)
136     XBT_INFO("%s core dump was generated by the system.", WCOREDUMP(status) ? "A" : "No");
137   XBT_INFO("Counter-example execution trace:");
138   for (auto const& s : mc_model_checker->getChecker()->get_textual_trace())
139     XBT_INFO("  %s", s.c_str());
140   dumpRecordPath();
141   session->log_state();
142   if (xbt_log_no_loc) {
143     XBT_INFO("Stack trace not displayed because you passed --log=no_loc");
144   } else {
145     XBT_INFO("Stack trace:");
146     mc_model_checker->get_remote_simulation().dump_stack();
147   }
148 }
149
150 static void MC_report_assertion_error()
151 {
152   XBT_INFO("**************************");
153   XBT_INFO("*** PROPERTY NOT VALID ***");
154   XBT_INFO("**************************");
155   XBT_INFO("Counter-example execution trace:");
156   for (auto const& s : mc_model_checker->getChecker()->get_textual_trace())
157     XBT_INFO("  %s", s.c_str());
158   dumpRecordPath();
159   session->log_state();
160 }
161
162 bool ModelChecker::handle_message(const char* buffer, ssize_t size)
163 {
164   s_mc_message_t base_message;
165   xbt_assert(size >= (ssize_t)sizeof(base_message), "Broken message");
166   memcpy(&base_message, buffer, sizeof(base_message));
167
168   switch(base_message.type) {
169   case MC_MESSAGE_IGNORE_HEAP:
170     {
171     s_mc_message_ignore_heap_t message;
172     xbt_assert(size == sizeof(message), "Broken messsage");
173     memcpy(&message, buffer, sizeof(message));
174
175     IgnoredHeapRegion region;
176     region.block    = message.block;
177     region.fragment = message.fragment;
178     region.address  = message.address;
179     region.size     = message.size;
180     get_remote_simulation().ignore_heap(region);
181     break;
182     }
183
184   case MC_MESSAGE_UNIGNORE_HEAP:
185     {
186     s_mc_message_ignore_memory_t message;
187     xbt_assert(size == sizeof(message), "Broken messsage");
188     memcpy(&message, buffer, sizeof(message));
189     get_remote_simulation().unignore_heap((void*)(std::uintptr_t)message.addr, message.size);
190     break;
191     }
192
193   case MC_MESSAGE_IGNORE_MEMORY:
194     {
195     s_mc_message_ignore_memory_t message;
196     xbt_assert(size == sizeof(message), "Broken messsage");
197     memcpy(&message, buffer, sizeof(message));
198     this->get_remote_simulation().ignore_region(message.addr, message.size);
199     break;
200     }
201
202   case MC_MESSAGE_STACK_REGION:
203     {
204     s_mc_message_stack_region_t message;
205     xbt_assert(size == sizeof(message), "Broken messsage");
206     memcpy(&message, buffer, sizeof(message));
207     this->get_remote_simulation().stack_areas().push_back(message.stack_region);
208     }
209     break;
210
211   case MC_MESSAGE_REGISTER_SYMBOL:
212     {
213     s_mc_message_register_symbol_t message;
214     xbt_assert(size == sizeof(message), "Broken message");
215     memcpy(&message, buffer, sizeof(message));
216     xbt_assert(not message.callback, "Support for client-side function proposition is not implemented.");
217     XBT_DEBUG("Received symbol: %s", message.name);
218
219     if (property_automaton == nullptr)
220       property_automaton = xbt_automaton_new();
221
222     RemoteSimulation* process = &this->get_remote_simulation();
223     RemotePtr<int> address = remote((int*)message.data);
224     xbt::add_proposition(property_automaton, message.name, [process, address]() { return process->read(address); });
225
226     break;
227     }
228
229   case MC_MESSAGE_WAITING:
230     return false;
231
232   case MC_MESSAGE_ASSERTION_FAILED:
233     MC_report_assertion_error();
234     this->exit(SIMGRID_MC_EXIT_SAFETY);
235
236   default:
237     xbt_die("Unexpected message from model-checked application");
238   }
239   return true;
240 }
241
242 /** Terminate the model-checker application */
243 void ModelChecker::exit(int status)
244 {
245   // TODO, terminate the model checker politely instead of exiting rudely
246   if (get_remote_simulation().running())
247     kill(get_remote_simulation().pid(), SIGKILL);
248   ::exit(status);
249 }
250
251 void ModelChecker::handle_waitpid()
252 {
253   XBT_DEBUG("Check for wait event");
254   int status;
255   pid_t pid;
256   while ((pid = waitpid(-1, &status, WNOHANG)) != 0) {
257     if (pid == -1) {
258       if (errno == ECHILD) {
259         // No more children:
260         xbt_assert(not this->get_remote_simulation().running(), "Inconsistent state");
261         break;
262       } else {
263         XBT_ERROR("Could not wait for pid");
264         throw simgrid::xbt::errno_error();
265       }
266     }
267
268     if (pid == this->get_remote_simulation().pid()) {
269       // From PTRACE_O_TRACEEXIT:
270 #ifdef __linux__
271       if (status>>8 == (SIGTRAP | (PTRACE_EVENT_EXIT<<8))) {
272         xbt_assert(ptrace(PTRACE_GETEVENTMSG, remote_simulation_->pid(), 0, &status) != -1,
273                    "Could not get exit status");
274         if (WIFSIGNALED(status)) {
275           MC_report_crash(status);
276           mc_model_checker->exit(SIMGRID_MC_EXIT_PROGRAM_CRASH);
277         }
278       }
279 #endif
280
281       // We don't care about signals, just reinject them:
282       if (WIFSTOPPED(status)) {
283         XBT_DEBUG("Stopped with signal %i", (int) WSTOPSIG(status));
284         errno = 0;
285 #ifdef __linux__
286         ptrace(PTRACE_CONT, remote_simulation_->pid(), 0, WSTOPSIG(status));
287 #elif defined BSD
288         ptrace(PT_CONTINUE, remote_simulation_->pid(), (caddr_t)1, WSTOPSIG(status));
289 #endif
290         xbt_assert(errno == 0, "Could not PTRACE_CONT");
291       }
292
293       else if (WIFSIGNALED(status)) {
294         MC_report_crash(status);
295         mc_model_checker->exit(SIMGRID_MC_EXIT_PROGRAM_CRASH);
296       } else if (WIFEXITED(status)) {
297         XBT_DEBUG("Child process is over");
298         this->get_remote_simulation().terminate();
299       }
300     }
301   }
302 }
303
304 void ModelChecker::wait_for_requests()
305 {
306   this->resume(get_remote_simulation());
307   if (this->get_remote_simulation().running())
308     checker_side_.dispatch();
309 }
310
311 void ModelChecker::handle_simcall(Transition const& transition)
312 {
313   s_mc_message_simcall_handle_t m;
314   memset(&m, 0, sizeof(m));
315   m.type  = MC_MESSAGE_SIMCALL_HANDLE;
316   m.pid   = transition.pid_;
317   m.value = transition.argument_;
318   checker_side_.get_channel().send(m);
319   this->remote_simulation_->clear_cache();
320   if (this->remote_simulation_->running())
321     checker_side_.dispatch();
322 }
323
324 bool ModelChecker::checkDeadlock()
325 {
326   int res = checker_side_.get_channel().send(MC_MESSAGE_DEADLOCK_CHECK);
327   xbt_assert(res == 0, "Could not check deadlock state");
328   s_mc_message_int_t message;
329   ssize_t s = checker_side_.get_channel().receive(message);
330   xbt_assert(s != -1, "Could not receive message");
331   xbt_assert(s == sizeof(message) && message.type == MC_MESSAGE_DEADLOCK_CHECK_REPLY,
332              "Received unexpected message %s (%i, size=%i) "
333              "expected MC_MESSAGE_DEADLOCK_CHECK_REPLY (%i, size=%i)",
334              MC_message_type_name(message.type), (int)message.type, (int)s, (int)MC_MESSAGE_DEADLOCK_CHECK_REPLY,
335              (int)sizeof(message));
336   return message.value != 0;
337 }
338
339 } // namespace mc
340 } // namespace simgrid