Logo AND Algorithmique Numérique Distribuée

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