Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Add some coverage_checkpoint() for model-checked applications.
[simgrid.git] / src / mc / remote / AppSide.cpp
1 /* Copyright (c) 2015-2021. 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/remote/AppSide.hpp"
7 #include "src/internal_config.h"
8 #include "src/kernel/actor/ActorImpl.hpp"
9 #include "src/kernel/actor/SimcallObserver.hpp"
10 #include "src/mc/remote/RemoteProcess.hpp"
11 #include "xbt/coverage.h"
12 #include "xbt/xbt_modinter.h" /* mmalloc_preinit to get the default mmalloc arena address */
13 #include <simgrid/modelchecker.h>
14
15 #include <cerrno>
16 #include <cstdlib>
17 #include <cstring>
18 #include <memory>
19 #include <sys/ptrace.h>
20 #include <sys/socket.h>
21 #include <sys/types.h>
22
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_client, mc, "MC client logic");
24
25 namespace simgrid {
26 namespace mc {
27
28 std::unique_ptr<AppSide> AppSide::instance_;
29
30 AppSide* AppSide::initialize()
31 {
32   if (not std::getenv(MC_ENV_SOCKET_FD)) // We are not in MC mode: don't initialize the MC world
33     return nullptr;
34
35   // Do not break if we are called multiple times:
36   if (instance_)
37     return instance_.get();
38
39   _sg_do_model_check = 1;
40
41   // Fetch socket from MC_ENV_SOCKET_FD:
42   const char* fd_env = std::getenv(MC_ENV_SOCKET_FD);
43   int fd = xbt_str_parse_int(fd_env, "Variable '" MC_ENV_SOCKET_FD "' should contain a number but contains '%s'");
44   XBT_DEBUG("Model-checked application found socket FD %i", fd);
45
46   // Check the socket type/validity:
47   int type;
48   socklen_t socklen = sizeof(type);
49   if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &socklen) != 0)
50     xbt_die("Could not check socket type");
51   xbt_assert(type == SOCK_SEQPACKET, "Unexpected socket type %i", type);
52   XBT_DEBUG("Model-checked application found expected socket type");
53
54   instance_ = std::make_unique<simgrid::mc::AppSide>(fd);
55
56   // Wait for the model-checker:
57   errno = 0;
58 #if defined __linux__
59   ptrace(PTRACE_TRACEME, 0, nullptr, nullptr);
60 #elif defined BSD
61   ptrace(PT_TRACE_ME, 0, nullptr, 0);
62 #else
63 #error "no ptrace equivalent coded for this platform"
64 #endif
65   if (errno != 0 || raise(SIGSTOP) != 0)
66     xbt_die("Could not wait for the model-checker (errno = %d: %s)", errno, strerror(errno));
67
68   s_mc_message_initial_addresses_t message{
69       MessageType::INITIAL_ADDRESSES, mmalloc_preinit(), simgrid::kernel::actor::get_maxpid_addr(),
70       simgrid::simix::simix_global_get_actors_addr(), simgrid::simix::simix_global_get_dead_actors_addr()};
71   int send_res = instance_->channel_.send(message);
72   xbt_assert(send_res == 0, "Could not send the initial message with addresses.");
73
74   instance_->handle_messages();
75   return instance_.get();
76 }
77
78 void AppSide::handle_deadlock_check(const s_mc_message_t*) const
79 {
80   bool deadlock = false;
81   if (not simix_global->process_list.empty()) {
82     deadlock = true;
83     for (auto const& kv : simix_global->process_list)
84       if (simgrid::mc::actor_is_enabled(kv.second)) {
85         deadlock = false;
86         break;
87       }
88   }
89
90   // Send result:
91   s_mc_message_int_t answer{MessageType::DEADLOCK_CHECK_REPLY, deadlock};
92   int send_res = channel_.send(answer);
93   xbt_assert(send_res == 0, "Could not send response");
94 }
95 void AppSide::handle_simcall_execute(const s_mc_message_simcall_handle_t* message) const
96 {
97   kernel::actor::ActorImpl* process = kernel::actor::ActorImpl::by_pid(message->pid_);
98   xbt_assert(process != nullptr, "Invalid pid %lu", message->pid_);
99   process->simcall_handle(message->times_considered_);
100   if (channel_.send(MessageType::WAITING))
101     xbt_die("Could not send MESSAGE_WAITING to model-checker");
102 }
103
104 void AppSide::handle_actor_enabled(const s_mc_message_actor_enabled_t* msg) const
105 {
106   bool res = simgrid::mc::actor_is_enabled(kernel::actor::ActorImpl::by_pid(msg->aid));
107   s_mc_message_int_t answer{MessageType::ACTOR_ENABLED_REPLY, res};
108   channel_.send(answer);
109 }
110
111 #define assert_msg_size(_name_, _type_)                                                                                \
112   xbt_assert(received_size == sizeof(_type_), "Unexpected size for " _name_ " (%zd != %zu)", received_size,            \
113              sizeof(_type_))
114
115 void AppSide::handle_messages() const
116 {
117   while (true) { // Until we get a CONTINUE message
118     XBT_DEBUG("Waiting messages from model-checker");
119
120     std::array<char, MC_MESSAGE_LENGTH> message_buffer;
121     ssize_t received_size = channel_.receive(message_buffer.data(), message_buffer.size());
122
123     xbt_assert(received_size >= 0, "Could not receive commands from the model-checker");
124
125     const s_mc_message_t* message = (s_mc_message_t*)message_buffer.data();
126     switch (message->type) {
127       case MessageType::DEADLOCK_CHECK:
128         assert_msg_size("DEADLOCK_CHECK", s_mc_message_t);
129         handle_deadlock_check(message);
130         break;
131
132       case MessageType::CONTINUE:
133         assert_msg_size("MESSAGE_CONTINUE", s_mc_message_t);
134         return;
135
136       case MessageType::SIMCALL_HANDLE:
137         assert_msg_size("SIMCALL_HANDLE", s_mc_message_simcall_handle_t);
138         handle_simcall_execute((s_mc_message_simcall_handle_t*)message_buffer.data());
139         break;
140
141       case MessageType::SIMCALL_IS_VISIBLE: {
142         assert_msg_size("SIMCALL_IS_VISIBLE", s_mc_message_simcall_is_visible_t);
143         auto msg_simcall                = (s_mc_message_simcall_is_visible_t*)message_buffer.data();
144         const kernel::actor::ActorImpl* actor = kernel::actor::ActorImpl::by_pid(msg_simcall->aid);
145         xbt_assert(actor != nullptr, "Invalid pid %d", msg_simcall->aid);
146         xbt_assert(actor->simcall_.observer_, "The transition of %s has no observer", actor->get_cname());
147         bool value = actor->simcall_.observer_->is_visible();
148
149         // Send result:
150         s_mc_message_simcall_is_visible_answer_t answer{MessageType::SIMCALL_IS_VISIBLE_ANSWER, value};
151         int send_res = channel_.send(answer);
152         xbt_assert(send_res == 0, "Could not send response");
153         break;
154       }
155
156       case MessageType::SIMCALL_TO_STRING: {
157         assert_msg_size("SIMCALL_TO_STRING", s_mc_message_simcall_to_string_t);
158         auto msg_simcall                = (s_mc_message_simcall_to_string_t*)message_buffer.data();
159         const kernel::actor::ActorImpl* actor = kernel::actor::ActorImpl::by_pid(msg_simcall->aid);
160         xbt_assert(actor != nullptr, "Invalid pid %d", msg_simcall->aid);
161         xbt_assert(actor->simcall_.observer_, "The transition of %s has no observer", actor->get_cname());
162         std::string value = actor->simcall_.observer_->to_string(msg_simcall->time_considered);
163
164         // Send result:
165         s_mc_message_simcall_to_string_answer_t answer{MessageType::SIMCALL_TO_STRING_ANSWER, {0}};
166         value.copy(answer.value, (sizeof answer.value) - 1); // last byte was set to '\0' by initialization above
167         int send_res = channel_.send(answer);
168         xbt_assert(send_res == 0, "Could not send response");
169         break;
170       }
171
172       case MessageType::SIMCALL_DOT_LABEL: {
173         assert_msg_size("SIMCALL_DOT_LABEL", s_mc_message_simcall_to_string_t);
174         auto msg_simcall                = (s_mc_message_simcall_to_string_t*)message_buffer.data();
175         const kernel::actor::ActorImpl* actor = kernel::actor::ActorImpl::by_pid(msg_simcall->aid);
176         xbt_assert(actor != nullptr, "Invalid pid %d", msg_simcall->aid);
177         xbt_assert(actor->simcall_.observer_, "The transition of %s has no observer", actor->get_cname());
178         std::string value = actor->simcall_.observer_->dot_label();
179
180         // Send result:
181         s_mc_message_simcall_to_string_answer_t answer{MessageType::SIMCALL_TO_STRING_ANSWER, {0}};
182         value.copy(answer.value, (sizeof answer.value) - 1); // last byte was set to '\0' by initialization above
183         int send_res = channel_.send(answer);
184         xbt_assert(send_res == 0, "Could not send response");
185         break;
186       }
187
188       case MessageType::ACTOR_ENABLED:
189         assert_msg_size("ACTOR_ENABLED", s_mc_message_actor_enabled_t);
190         handle_actor_enabled((s_mc_message_actor_enabled_t*)message_buffer.data());
191         break;
192
193       case MessageType::FINALIZE: {
194         assert_msg_size("FINALIZE", s_mc_message_int_t);
195         bool terminate_asap = ((s_mc_message_int_t*)message_buffer.data())->value;
196 #if HAVE_SMPI
197         if (not terminate_asap) {
198           XBT_INFO("Finalize. Smpi_enabled: %d", (int)smpi_enabled());
199           simix_global->display_all_actor_status();
200           if (smpi_enabled())
201             SMPI_finalize();
202         }
203 #endif
204         coverage_checkpoint();
205         int send_res = channel_.send(MessageType::DEADLOCK_CHECK_REPLY); // really?
206         xbt_assert(send_res == 0, "Could not answer to FINALIZE");
207         if (terminate_asap)
208           ::_Exit(0);
209         break;
210       }
211
212       default:
213         xbt_die("Received unexpected message %s (%i)", to_c_str(message->type), static_cast<int>(message->type));
214         break;
215     }
216   }
217 }
218
219 void AppSide::main_loop() const
220 {
221   coverage_checkpoint();
222   while (true) {
223     simgrid::mc::execute_actors();
224     int send_res = channel_.send(MessageType::WAITING);
225     xbt_assert(send_res == 0, "Could not send WAITING message to model-checker");
226     this->handle_messages();
227   }
228 }
229
230 void AppSide::report_assertion_failure() const
231 {
232   if (channel_.send(MessageType::ASSERTION_FAILED))
233     xbt_die("Could not send assertion to model-checker");
234   this->handle_messages();
235 }
236
237 void AppSide::ignore_memory(void* addr, std::size_t size) const
238 {
239   s_mc_message_ignore_memory_t message;
240   message.type = MessageType::IGNORE_MEMORY;
241   message.addr = (std::uintptr_t)addr;
242   message.size = size;
243   if (channel_.send(message))
244     xbt_die("Could not send IGNORE_MEMORY message to model-checker");
245 }
246
247 void AppSide::ignore_heap(void* address, std::size_t size) const
248 {
249   const s_xbt_mheap_t* heap = mmalloc_get_current_heap();
250
251   s_mc_message_ignore_heap_t message;
252   message.type    = MessageType::IGNORE_HEAP;
253   message.address = address;
254   message.size    = size;
255   message.block   = ((char*)address - (char*)heap->heapbase) / BLOCKSIZE + 1;
256   if (heap->heapinfo[message.block].type == 0) {
257     message.fragment = -1;
258     heap->heapinfo[message.block].busy_block.ignore++;
259   } else {
260     message.fragment = (ADDR2UINT(address) % BLOCKSIZE) >> heap->heapinfo[message.block].type;
261     heap->heapinfo[message.block].busy_frag.ignore[message.fragment]++;
262   }
263
264   if (channel_.send(message))
265     xbt_die("Could not send ignored region to MCer");
266 }
267
268 void AppSide::unignore_heap(void* address, std::size_t size) const
269 {
270   s_mc_message_ignore_memory_t message;
271   message.type = MessageType::UNIGNORE_HEAP;
272   message.addr = (std::uintptr_t)address;
273   message.size = size;
274   if (channel_.send(message))
275     xbt_die("Could not send IGNORE_HEAP message to model-checker");
276 }
277
278 void AppSide::declare_symbol(const char* name, int* value) const
279 {
280   s_mc_message_register_symbol_t message;
281   message.type = MessageType::REGISTER_SYMBOL;
282   if (strlen(name) + 1 > message.name.size())
283     xbt_die("Symbol is too long");
284   strncpy(message.name.data(), name, message.name.size());
285   message.callback = nullptr;
286   message.data     = value;
287   if (channel_.send(message))
288     xbt_die("Could send REGISTER_SYMBOL message to model-checker");
289 }
290
291 void AppSide::declare_stack(void* stack, size_t size, ucontext_t* context) const
292 {
293   const s_xbt_mheap_t* heap = mmalloc_get_current_heap();
294
295   s_stack_region_t region;
296   memset(&region, 0, sizeof(region));
297   region.address = stack;
298   region.context = context;
299   region.size    = size;
300   region.block   = ((char*)stack - (char*)heap->heapbase) / BLOCKSIZE + 1;
301
302   s_mc_message_stack_region_t message;
303   message.type         = MessageType::STACK_REGION;
304   message.stack_region = region;
305   if (channel_.send(message))
306     xbt_die("Could not send STACK_REGION to model-checker");
307 }
308 } // namespace mc
309 } // namespace simgrid