Logo AND Algorithmique Numérique Distribuée

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