Logo AND Algorithmique Numérique Distribuée

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