Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
mc: snake_case the checkers
[simgrid.git] / src / mc / ModelChecker.cpp
1 /* Copyright (c) 2008-2019. 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/RemoteClient.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<RemoteClient> process)
36     : base_(nullptr)
37     , socket_event_(nullptr)
38     , signal_event_(nullptr)
39     , page_store_(500)
40     , process_(std::move(process))
41     , parent_snapshot_(nullptr)
42 {
43
44 }
45
46 ModelChecker::~ModelChecker() {
47   if (socket_event_ != nullptr)
48     event_free(socket_event_);
49   if (signal_event_ != nullptr)
50     event_free(signal_event_);
51   if (base_ != nullptr)
52     event_base_free(base_);
53 }
54
55 void ModelChecker::start()
56 {
57   const pid_t pid = process_->pid();
58
59   base_ = event_base_new();
60   event_callback_fn event_callback = [](evutil_socket_t fd, short events, void *arg)
61   {
62     ((ModelChecker *)arg)->handle_events(fd, events);
63   };
64   socket_event_ = event_new(base_,
65                             process_->getChannel().getSocket(),
66                             EV_READ|EV_PERSIST,
67                             event_callback, this);
68   event_add(socket_event_, NULL);
69   signal_event_ = event_new(base_,
70                             SIGCHLD,
71                             EV_SIGNAL|EV_PERSIST,
72                             event_callback, this);
73   event_add(signal_event_, NULL);
74
75   XBT_DEBUG("Waiting for the model-checked process");
76   int status;
77
78   // The model-checked process SIGSTOP itself to signal it's ready:
79   pid_t res = waitpid(pid, &status, WAITPID_CHECKED_FLAGS);
80   if (res < 0 || not WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP)
81     xbt_die("Could not wait model-checked process");
82
83   process_->init();
84
85   if (not _sg_mc_dot_output_file.get().empty())
86     MC_init_dot_output();
87
88   setup_ignore();
89
90 #ifdef __linux__
91   ptrace(PTRACE_SETOPTIONS, pid, nullptr, PTRACE_O_TRACEEXIT);
92   ptrace(PTRACE_CONT, pid, 0, 0);
93 #elif defined BSD
94   ptrace(PT_CONTINUE, pid, (caddr_t)1, 0);
95 #else
96 # error "no ptrace equivalent coded for this platform"
97 #endif
98 }
99
100 static const std::pair<const char*, const char*> ignored_local_variables[] = {
101   std::pair<const char*, const char*>{  "e", "*" },
102   std::pair<const char*, const char*>{ "_log_ev", "*" },
103
104   /* Ignore local variable about time used for tracing */
105   std::pair<const char*, const char*>{ "start_time", "*" },
106 };
107
108 void ModelChecker::setup_ignore()
109 {
110   RemoteClient& process = this->process();
111   for (std::pair<const char*, const char*> const& var :
112       ignored_local_variables)
113     process.ignore_local_variable(var.first, var.second);
114
115   /* Static variable used for tracing */
116   process.ignore_global_variable("counter");
117 }
118
119 void ModelChecker::shutdown()
120 {
121   XBT_DEBUG("Shuting down model-checker");
122
123   simgrid::mc::RemoteClient* process = &this->process();
124   if (process->running()) {
125     XBT_DEBUG("Killing process");
126     kill(process->pid(), SIGKILL);
127     process->terminate();
128   }
129 }
130
131 void ModelChecker::resume(simgrid::mc::RemoteClient& process)
132 {
133   int res = process.getChannel().send(MC_MESSAGE_CONTINUE);
134   if (res)
135     throw simgrid::xbt::errno_error();
136   process.clear_cache();
137 }
138
139 static void MC_report_crash(int status)
140 {
141   XBT_INFO("**************************");
142   XBT_INFO("** CRASH IN THE PROGRAM **");
143   XBT_INFO("**************************");
144   if (WIFSIGNALED(status))
145     XBT_INFO("From signal: %s", strsignal(WTERMSIG(status)));
146   else if (WIFEXITED(status))
147     XBT_INFO("From exit: %i", WEXITSTATUS(status));
148   if (WCOREDUMP(status))
149     XBT_INFO("A core dump was generated by the system.");
150   else
151     XBT_INFO("No core dump was generated by the system.");
152   XBT_INFO("Counter-example execution trace:");
153   for (auto const& s : mc_model_checker->getChecker()->get_textual_trace())
154     XBT_INFO("  %s", s.c_str());
155   simgrid::mc::dumpRecordPath();
156   simgrid::mc::session->log_state();
157   XBT_INFO("Stack trace:");
158   mc_model_checker->process().dumpStack();
159 }
160
161 static void MC_report_assertion_error()
162 {
163   XBT_INFO("**************************");
164   XBT_INFO("*** PROPERTY NOT VALID ***");
165   XBT_INFO("**************************");
166   XBT_INFO("Counter-example execution trace:");
167   for (auto const& s : mc_model_checker->getChecker()->get_textual_trace())
168     XBT_INFO("  %s", s.c_str());
169   simgrid::mc::dumpRecordPath();
170   simgrid::mc::session->log_state();
171 }
172
173 bool ModelChecker::handle_message(char* buffer, ssize_t size)
174 {
175   s_mc_message_t base_message;
176   if (size < (ssize_t) sizeof(base_message))
177     xbt_die("Broken message");
178   memcpy(&base_message, buffer, sizeof(base_message));
179
180   switch(base_message.type) {
181
182   case MC_MESSAGE_IGNORE_HEAP:
183     {
184     s_mc_message_ignore_heap_t message;
185     if (size != sizeof(message))
186       xbt_die("Broken messsage");
187     memcpy(&message, buffer, sizeof(message));
188
189     IgnoredHeapRegion region;
190     region.block    = message.block;
191     region.fragment = message.fragment;
192     region.address  = message.address;
193     region.size     = message.size;
194     process().ignore_heap(region);
195     break;
196     }
197
198   case MC_MESSAGE_UNIGNORE_HEAP:
199     {
200     s_mc_message_ignore_memory_t message;
201     if (size != sizeof(message))
202       xbt_die("Broken messsage");
203     memcpy(&message, buffer, sizeof(message));
204     process().unignore_heap((void*)(std::uintptr_t)message.addr, message.size);
205     break;
206     }
207
208   case MC_MESSAGE_IGNORE_MEMORY:
209     {
210     s_mc_message_ignore_memory_t message;
211     if (size != sizeof(message))
212       xbt_die("Broken messsage");
213     memcpy(&message, buffer, sizeof(message));
214     this->process().ignore_region(message.addr, message.size);
215     break;
216     }
217
218   case MC_MESSAGE_STACK_REGION:
219     {
220     s_mc_message_stack_region_t message;
221     if (size != sizeof(message))
222       xbt_die("Broken messsage");
223     memcpy(&message, buffer, sizeof(message));
224     this->process().stack_areas().push_back(message.stack_region);
225     }
226     break;
227
228   case MC_MESSAGE_REGISTER_SYMBOL:
229     {
230     s_mc_message_register_symbol_t message;
231     if (size != sizeof(message))
232       xbt_die("Broken message");
233     memcpy(&message, buffer, sizeof(message));
234     if (message.callback)
235       xbt_die("Support for client-side function proposition is not implemented.");
236     XBT_DEBUG("Received symbol: %s", message.name);
237
238     if (simgrid::mc::property_automaton == nullptr)
239       simgrid::mc::property_automaton = xbt_automaton_new();
240
241     simgrid::mc::RemoteClient* process  = &this->process();
242     simgrid::mc::RemotePtr<int> address = simgrid::mc::remote((int*)message.data);
243     simgrid::xbt::add_proposition(simgrid::mc::property_automaton, message.name,
244                                   [process, address]() { return process->read(address); });
245
246     break;
247     }
248
249   case MC_MESSAGE_WAITING:
250     return false;
251
252   case MC_MESSAGE_ASSERTION_FAILED:
253     MC_report_assertion_error();
254     this->exit(SIMGRID_MC_EXIT_SAFETY);
255
256   default:
257     xbt_die("Unexpected message from model-checked application");
258
259   }
260   return true;
261 }
262
263 /** Terminate the model-checker application */
264 void ModelChecker::exit(int status)
265 {
266   // TODO, terminate the model checker politely instead of exiting rudely
267   if (process().running())
268     kill(process().pid(), SIGKILL);
269   ::exit(status);
270 }
271
272 void ModelChecker::handle_events(int fd, short events)
273 {
274   if (events == EV_READ) {
275     char buffer[MC_MESSAGE_LENGTH];
276     ssize_t size = process_->getChannel().receive(buffer, sizeof(buffer), false);
277     if (size == -1 && errno != EAGAIN)
278       throw simgrid::xbt::errno_error();
279     if (not handle_message(buffer, size)) {
280       event_base_loopbreak(base_);
281     }
282   }
283   else if (events == EV_SIGNAL) {
284     on_signal(fd);
285   }
286   else {
287     xbt_die("Unexpected event");
288   }
289 }
290
291 void ModelChecker::loop()
292 {
293   if (this->process().running())
294     event_base_dispatch(base_);
295 }
296
297 void ModelChecker::handle_waitpid()
298 {
299   XBT_DEBUG("Check for wait event");
300   int status;
301   pid_t pid;
302   while ((pid = waitpid(-1, &status, WNOHANG)) != 0) {
303     if (pid == -1) {
304       if (errno == ECHILD) {
305         // No more children:
306         if (this->process().running())
307           xbt_die("Inconsistent state");
308         else
309           break;
310       } else {
311         XBT_ERROR("Could not wait for pid");
312         throw simgrid::xbt::errno_error();
313       }
314     }
315
316     if (pid == this->process().pid()) {
317
318       // From PTRACE_O_TRACEEXIT:
319 #ifdef __linux__
320       if (status>>8 == (SIGTRAP | (PTRACE_EVENT_EXIT<<8))) {
321         if (ptrace(PTRACE_GETEVENTMSG, this->process().pid(), 0, &status) == -1)
322           xbt_die("Could not get exit status");
323         if (WIFSIGNALED(status)) {
324           MC_report_crash(status);
325           mc_model_checker->exit(SIMGRID_MC_EXIT_PROGRAM_CRASH);
326         }
327       }
328 #endif
329
330       // We don't care about signals, just reinject them:
331       if (WIFSTOPPED(status)) {
332         XBT_DEBUG("Stopped with signal %i", (int) WSTOPSIG(status));
333         errno = 0;
334 #ifdef __linux__
335         ptrace(PTRACE_CONT, this->process().pid(), 0, WSTOPSIG(status));
336 #elif defined BSD
337         ptrace(PT_CONTINUE, this->process().pid(), (caddr_t)1, WSTOPSIG(status));
338 #endif
339         if (errno != 0)
340           xbt_die("Could not PTRACE_CONT");
341       }
342
343       else if (WIFEXITED(status) || WIFSIGNALED(status)) {
344         XBT_DEBUG("Child process is over");
345         this->process().terminate();
346       }
347     }
348   }
349 }
350
351 void ModelChecker::on_signal(int signo)
352 {
353   if (signo == SIGCHLD)
354     this->handle_waitpid();
355 }
356
357 void ModelChecker::wait_for_requests()
358 {
359   this->resume(process());
360   if (this->process().running())
361     event_base_dispatch(base_);
362 }
363
364 void ModelChecker::handle_simcall(Transition const& transition)
365 {
366   s_mc_message_simcall_handle_t m;
367   memset(&m, 0, sizeof(m));
368   m.type  = MC_MESSAGE_SIMCALL_HANDLE;
369   m.pid   = transition.pid;
370   m.value = transition.argument;
371   this->process_->getChannel().send(m);
372   this->process_->clear_cache();
373   if (this->process_->running())
374     event_base_dispatch(base_);
375 }
376
377 bool ModelChecker::checkDeadlock()
378 {
379   int res;
380   if ((res = this->process().getChannel().send(MC_MESSAGE_DEADLOCK_CHECK)))
381     xbt_die("Could not check deadlock state");
382   s_mc_message_int_t message;
383   ssize_t s = mc_model_checker->process().getChannel().receive(message);
384   if (s == -1)
385     xbt_die("Could not receive message");
386   if (s != sizeof(message) || message.type != MC_MESSAGE_DEADLOCK_CHECK_REPLY)
387     xbt_die("Received unexpected message %s (%i, size=%i) "
388       "expected MC_MESSAGE_DEADLOCK_CHECK_REPLY (%i, size=%i)",
389       MC_message_type_name(message.type), (int) message.type, (int) s,
390       (int) MC_MESSAGE_DEADLOCK_CHECK_REPLY, (int) sizeof(message)
391       );
392   return message.value != 0;
393 }
394
395 }
396 }