Logo AND Algorithmique Numérique Distribuée

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