1 /* Copyright (c) 2015-2023. The SimGrid Team. All rights reserved. */
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. */
6 #include "src/mc/remote/AppSide.hpp"
7 #include "simgrid/s4u/Host.hpp"
8 #include "src/internal_config.h"
9 #include "src/kernel/EngineImpl.hpp"
10 #include "src/kernel/actor/ActorImpl.hpp"
11 #include "src/kernel/actor/SimcallObserver.hpp"
12 #include "src/mc/mc_base.hpp"
13 #include "src/mc/mc_config.hpp"
14 #include "src/mc/remote/RemoteProcess.hpp"
16 #include "src/smpi/include/private.hpp"
18 #include "src/sthread/sthread.h"
19 #include "xbt/coverage.h"
21 #include "xbt/xbt_modinter.h" /* mmalloc_preinit to get the default mmalloc arena address */
22 #include <simgrid/modelchecker.h>
25 #include <cstdio> // setvbuf
29 #include <sys/ptrace.h>
30 #include <sys/socket.h>
31 #include <sys/types.h>
33 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_client, mc, "MC client logic");
34 XBT_LOG_EXTERNAL_CATEGORY(mc_global);
36 namespace simgrid::mc {
38 std::unique_ptr<AppSide> AppSide::instance_;
40 AppSide* AppSide::initialize()
42 if (not std::getenv(MC_ENV_SOCKET_FD)) // We are not in MC mode: don't initialize the MC world
45 // Do not break if we are called multiple times:
47 return instance_.get();
49 simgrid::mc::cfg_do_model_check = true;
51 setvbuf(stdout, nullptr, _IOLBF, 0);
53 // Fetch socket from MC_ENV_SOCKET_FD:
54 const char* fd_env = std::getenv(MC_ENV_SOCKET_FD);
55 int fd = xbt_str_parse_int(fd_env, "Not a number in variable '" MC_ENV_SOCKET_FD "'");
56 XBT_DEBUG("Model-checked application found socket FD %i", fd);
58 // Check the socket type/validity:
60 socklen_t socklen = sizeof(type);
61 xbt_assert(getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &socklen) == 0, "Could not check socket type");
62 xbt_assert(type == SOCK_SEQPACKET, "Unexpected socket type %i", type);
63 XBT_DEBUG("Model-checked application found expected socket type");
65 instance_ = std::make_unique<simgrid::mc::AppSide>(fd);
67 // Wait for the model-checker:
70 ptrace(PTRACE_TRACEME, 0, nullptr, nullptr);
72 ptrace(PT_TRACE_ME, 0, nullptr, 0);
74 #error "no ptrace equivalent coded for this platform"
76 xbt_assert(errno == 0 && raise(SIGSTOP) == 0, "Could not wait for the model-checker (errno = %d: %s)", errno,
79 s_mc_message_initial_addresses_t message{MessageType::INITIAL_ADDRESSES, mmalloc_get_current_heap(),
80 kernel::actor::ActorImpl::get_maxpid_addr()};
81 xbt_assert(instance_->channel_.send(message) == 0, "Could not send the initial message with addresses.");
83 instance_->handle_messages();
84 return instance_.get();
87 void AppSide::handle_deadlock_check(const s_mc_message_t*) const
89 const auto* engine = kernel::EngineImpl::get_instance();
90 const auto& actor_list = engine->get_actor_list();
91 bool deadlock = not actor_list.empty() && std::none_of(begin(actor_list), end(actor_list), [](const auto& kv) {
92 return mc::actor_is_enabled(kv.second);
96 XBT_CINFO(mc_global, "**************************");
97 XBT_CINFO(mc_global, "*** DEADLOCK DETECTED ***");
98 XBT_CINFO(mc_global, "**************************");
99 engine->display_all_actor_status();
102 s_mc_message_int_t answer{MessageType::DEADLOCK_CHECK_REPLY, deadlock};
103 xbt_assert(channel_.send(answer) == 0, "Could not send response");
105 void AppSide::handle_simcall_execute(const s_mc_message_simcall_execute_t* message) const
107 kernel::actor::ActorImpl* actor = kernel::EngineImpl::get_instance()->get_actor_by_pid(message->aid_);
108 xbt_assert(actor != nullptr, "Invalid pid %ld", message->aid_);
110 // The client may send some messages to the server while processing the transition
111 actor->simcall_handle(message->times_considered_);
112 // Say the server that the transition is over and that it should proceed
113 xbt_assert(channel_.send(MessageType::WAITING) == 0, "Could not send MESSAGE_WAITING to model-checker");
115 // Finish the RPC from the server: return a serialized observer, to build a Transition on Checker side
116 s_mc_message_simcall_execute_answer_t answer;
117 memset(&answer, 0, sizeof(answer));
118 answer.type = MessageType::SIMCALL_EXECUTE_ANSWER;
119 std::stringstream stream;
120 if (actor->simcall_.observer_ != nullptr) {
121 actor->simcall_.observer_->serialize(stream);
123 stream << (short)mc::Transition::Type::UNKNOWN;
125 std::string str = stream.str();
126 xbt_assert(str.size() + 1 <= answer.buffer.size(),
127 "The serialized simcall is too large for the buffer. Please fix the code.");
128 strncpy(answer.buffer.data(), str.c_str(), answer.buffer.size() - 1);
129 answer.buffer.back() = '\0';
131 XBT_DEBUG("send SIMCALL_EXECUTE_ANSWER(%s) ~> '%s'", actor->get_cname(), str.c_str());
132 xbt_assert(channel_.send(answer) == 0, "Could not send response");
135 void AppSide::handle_finalize(const s_mc_message_int_t* msg) const
137 bool terminate_asap = msg->value;
138 XBT_DEBUG("Finalize (terminate = %d)", (int)terminate_asap);
139 if (not terminate_asap) {
140 if (XBT_LOG_ISENABLED(mc_client, xbt_log_priority_debug))
141 kernel::EngineImpl::get_instance()->display_all_actor_status();
143 XBT_DEBUG("Smpi_enabled: %d", SMPI_is_inited());
144 if (SMPI_is_inited())
148 coverage_checkpoint();
149 xbt_assert(channel_.send(MessageType::FINALIZE_REPLY) == 0, "Could not answer to FINALIZE");
154 void AppSide::handle_actors_status() const
156 auto const& actor_list = kernel::EngineImpl::get_instance()->get_actor_list();
157 int count = actor_list.size();
158 XBT_DEBUG("Serialize the actors to answer ACTORS_STATUS from the checker. %d actors to go.", count);
160 struct s_mc_message_actors_status_answer_t answer {
161 MessageType::ACTORS_STATUS_REPLY, count
163 std::vector<s_mc_message_actors_status_one_t> status(count);
165 for (auto const& [aid, actor] : actor_list) {
167 status[i].enabled = mc::actor_is_enabled(actor);
168 status[i].max_considered = actor->simcall_.observer_->get_max_consider();
171 xbt_assert(channel_.send(answer) == 0, "Could not send ACTORS_STATUS_REPLY msg");
172 if (answer.count > 0) {
173 size_t size = status.size() * sizeof(s_mc_message_actors_status_one_t);
174 xbt_assert(channel_.send(status.data(), size) == 0, "Could not send ACTORS_STATUS_REPLY data");
178 #define assert_msg_size(_name_, _type_) \
179 xbt_assert(received_size == sizeof(_type_), "Unexpected size for " _name_ " (%zd != %zu)", received_size, \
182 void AppSide::handle_messages() const
184 while (true) { // Until we get a CONTINUE message
185 XBT_DEBUG("Waiting messages from model-checker");
187 std::array<char, MC_MESSAGE_LENGTH> message_buffer;
188 ssize_t received_size = channel_.receive(message_buffer.data(), message_buffer.size());
190 xbt_assert(received_size >= 0, "Could not receive commands from the model-checker");
192 const s_mc_message_t* message = (s_mc_message_t*)message_buffer.data();
193 switch (message->type) {
194 case MessageType::DEADLOCK_CHECK:
195 assert_msg_size("DEADLOCK_CHECK", s_mc_message_t);
196 handle_deadlock_check(message);
199 case MessageType::CONTINUE:
200 assert_msg_size("MESSAGE_CONTINUE", s_mc_message_t);
203 case MessageType::SIMCALL_EXECUTE:
204 assert_msg_size("SIMCALL_EXECUTE", s_mc_message_simcall_execute_t);
205 handle_simcall_execute((s_mc_message_simcall_execute_t*)message_buffer.data());
208 case MessageType::FINALIZE:
209 assert_msg_size("FINALIZE", s_mc_message_int_t);
210 handle_finalize((s_mc_message_int_t*)message_buffer.data());
213 case MessageType::ACTORS_STATUS:
214 assert_msg_size("ACTORS_STATUS", s_mc_message_t);
215 handle_actors_status();
219 xbt_die("Received unexpected message %s (%i)", to_c_str(message->type), static_cast<int>(message->type));
225 void AppSide::main_loop() const
227 simgrid::mc::processes_time.resize(simgrid::kernel::actor::ActorImpl::get_maxpid());
228 MC_ignore_heap(simgrid::mc::processes_time.data(),
229 simgrid::mc::processes_time.size() * sizeof(simgrid::mc::processes_time[0]));
232 coverage_checkpoint();
235 simgrid::mc::execute_actors();
236 xbt_assert(channel_.send(MessageType::WAITING) == 0, "Could not send WAITING message to model-checker");
237 this->handle_messages();
241 void AppSide::report_assertion_failure() const
243 xbt_assert(channel_.send(MessageType::ASSERTION_FAILED) == 0, "Could not send assertion to model-checker");
244 this->handle_messages();
247 void AppSide::ignore_memory(void* addr, std::size_t size) const
249 if (not MC_is_active())
252 s_mc_message_ignore_memory_t message;
253 message.type = MessageType::IGNORE_MEMORY;
254 message.addr = (std::uintptr_t)addr;
256 xbt_assert(channel_.send(message) == 0, "Could not send IGNORE_MEMORY message to model-checker");
259 void AppSide::ignore_heap(void* address, std::size_t size) const
261 if (not MC_is_active())
264 const s_xbt_mheap_t* heap = mmalloc_get_current_heap();
266 s_mc_message_ignore_heap_t message;
267 message.type = MessageType::IGNORE_HEAP;
268 message.address = address;
270 message.block = ((char*)address - (char*)heap->heapbase) / BLOCKSIZE + 1;
271 if (heap->heapinfo[message.block].type == 0) {
272 message.fragment = -1;
273 heap->heapinfo[message.block].busy_block.ignore++;
275 message.fragment = (ADDR2UINT(address) % BLOCKSIZE) >> heap->heapinfo[message.block].type;
276 heap->heapinfo[message.block].busy_frag.ignore[message.fragment]++;
279 xbt_assert(channel_.send(message) == 0, "Could not send ignored region to MCer");
282 void AppSide::unignore_heap(void* address, std::size_t size) const
284 if (not MC_is_active())
287 s_mc_message_ignore_memory_t message;
288 message.type = MessageType::UNIGNORE_HEAP;
289 message.addr = (std::uintptr_t)address;
291 xbt_assert(channel_.send(message) == 0, "Could not send IGNORE_HEAP message to model-checker");
294 void AppSide::declare_symbol(const char* name, int* value) const
296 if (not MC_is_active())
299 s_mc_message_register_symbol_t message;
300 memset(&message, 0, sizeof(message));
301 message.type = MessageType::REGISTER_SYMBOL;
302 xbt_assert(strlen(name) + 1 <= message.name.size(), "Symbol is too long");
303 strncpy(message.name.data(), name, message.name.size() - 1);
304 message.callback = nullptr;
305 message.data = value;
306 xbt_assert(channel_.send(message) == 0, "Could send REGISTER_SYMBOL message to model-checker");
309 /** Register a stack in the model checker
311 * The stacks are allocated in the heap. The MC handle them specifically
312 * when we analyze/compare the content of the heap so it must be told where
313 * they are with this function.
315 void AppSide::declare_stack(void* stack, size_t size, ucontext_t* context) const
317 if (not MC_is_active())
320 const s_xbt_mheap_t* heap = mmalloc_get_current_heap();
322 s_stack_region_t region;
323 memset(®ion, 0, sizeof(region));
324 region.address = stack;
325 region.context = context;
327 region.block = ((char*)stack - (char*)heap->heapbase) / BLOCKSIZE + 1;
329 s_mc_message_stack_region_t message;
330 message.type = MessageType::STACK_REGION;
331 message.stack_region = region;
332 xbt_assert(channel_.send(message) == 0, "Could not send STACK_REGION to model-checker");
334 } // namespace simgrid::mc