Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f1e89a971f8064645832741c8efd0d8573b29f51
[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 sig, short events, void* arg) {
40     ((ModelChecker*)arg)->handle_events(sig, 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 sig, 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     if (sig == SIGCHLD)
247       this->handle_waitpid();
248   }
249   else {
250     xbt_die("Unexpected event");
251   }
252 }
253
254 void ModelChecker::handle_waitpid()
255 {
256   XBT_DEBUG("Check for wait event");
257   int status;
258   pid_t pid;
259   while ((pid = waitpid(-1, &status, WNOHANG)) != 0) {
260     if (pid == -1) {
261       if (errno == ECHILD) {
262         // No more children:
263         xbt_assert(not this->process().running(), "Inconsistent state");
264         break;
265       } else {
266         XBT_ERROR("Could not wait for pid");
267         throw simgrid::xbt::errno_error();
268       }
269     }
270
271     if (pid == this->process().pid()) {
272       // From PTRACE_O_TRACEEXIT:
273 #ifdef __linux__
274       if (status>>8 == (SIGTRAP | (PTRACE_EVENT_EXIT<<8))) {
275         xbt_assert(ptrace(PTRACE_GETEVENTMSG, this->process().pid(), 0, &status) != -1, "Could not get exit status");
276         if (WIFSIGNALED(status)) {
277           MC_report_crash(status);
278           mc_model_checker->exit(SIMGRID_MC_EXIT_PROGRAM_CRASH);
279         }
280       }
281 #endif
282
283       // We don't care about signals, just reinject them:
284       if (WIFSTOPPED(status)) {
285         XBT_DEBUG("Stopped with signal %i", (int) WSTOPSIG(status));
286         errno = 0;
287 #ifdef __linux__
288         ptrace(PTRACE_CONT, this->process().pid(), 0, WSTOPSIG(status));
289 #elif defined BSD
290         ptrace(PT_CONTINUE, this->process().pid(), (caddr_t)1, WSTOPSIG(status));
291 #endif
292         xbt_assert(errno == 0, "Could not PTRACE_CONT");
293       }
294
295       else if (WIFSIGNALED(status)) {
296         MC_report_crash(status);
297         mc_model_checker->exit(SIMGRID_MC_EXIT_PROGRAM_CRASH);
298       } else if (WIFEXITED(status)) {
299         XBT_DEBUG("Child process is over");
300         this->process().terminate();
301       }
302     }
303   }
304 }
305
306 void ModelChecker::wait_for_requests()
307 {
308   this->resume(process());
309   if (this->process().running())
310     event_loop_.dispatch();
311 }
312
313 void ModelChecker::handle_simcall(Transition const& transition)
314 {
315   s_mc_message_simcall_handle_t m;
316   memset(&m, 0, sizeof(m));
317   m.type  = MC_MESSAGE_SIMCALL_HANDLE;
318   m.pid   = transition.pid_;
319   m.value = transition.argument_;
320   this->process_->get_channel().send(m);
321   this->process_->clear_cache();
322   if (this->process_->running())
323     event_loop_.dispatch();
324 }
325
326 bool ModelChecker::checkDeadlock()
327 {
328   int res = this->process().get_channel().send(MC_MESSAGE_DEADLOCK_CHECK);
329   xbt_assert(res == 0, "Could not check deadlock state");
330   s_mc_message_int_t message;
331   ssize_t s = mc_model_checker->process().get_channel().receive(message);
332   xbt_assert(s != -1, "Could not receive message");
333   xbt_assert(s == sizeof(message) && message.type == MC_MESSAGE_DEADLOCK_CHECK_REPLY,
334              "Received unexpected message %s (%i, size=%i) "
335              "expected MC_MESSAGE_DEADLOCK_CHECK_REPLY (%i, size=%i)",
336              MC_message_type_name(message.type), (int)message.type, (int)s, (int)MC_MESSAGE_DEADLOCK_CHECK_REPLY,
337              (int)sizeof(message));
338   return message.value != 0;
339 }
340
341 } // namespace mc
342 } // namespace simgrid