1 /* Copyright (c) 2015-2020. 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 "src/internal_config.h"
8 #include <simgrid/modelchecker.h>
13 #include <sys/ptrace.h>
14 #include <sys/socket.h>
15 #include <sys/types.h>
17 // We won't need those once the separation MCer/MCed is complete:
18 #include "src/mc/mc_smx.hpp"
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_client, mc, "MC client logic");
25 std::unique_ptr<AppSide> AppSide::instance_;
27 AppSide* AppSide::initialize()
29 if (not std::getenv(MC_ENV_SOCKET_FD)) // We are not in MC mode: don't initialize the MC world
32 // Do not break if we are called multiple times:
34 return instance_.get();
36 _sg_do_model_check = 1;
38 // Fetch socket from MC_ENV_SOCKET_FD:
39 const char* fd_env = std::getenv(MC_ENV_SOCKET_FD);
40 int fd = xbt_str_parse_int(fd_env, "Variable '" MC_ENV_SOCKET_FD "' should contain a number but contains '%s'");
41 XBT_DEBUG("Model-checked application found socket FD %i", fd);
43 // Check the socket type/validity:
45 socklen_t socklen = sizeof(type);
46 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &socklen) != 0)
47 xbt_die("Could not check socket type");
48 xbt_assert(type == SOCK_SEQPACKET, "Unexpected socket type %i", type);
49 XBT_DEBUG("Model-checked application found expected socket type");
51 instance_.reset(new simgrid::mc::AppSide(fd));
53 // Wait for the model-checker:
56 ptrace(PTRACE_TRACEME, 0, nullptr, nullptr);
58 ptrace(PT_TRACE_ME, 0, nullptr, 0);
60 #error "no ptrace equivalent coded for this platform"
62 if (errno != 0 || raise(SIGSTOP) != 0)
63 xbt_die("Could not wait for the model-checker (errno = %d: %s)", errno, strerror(errno));
65 instance_->handle_messages();
66 return instance_.get();
69 void AppSide::handle_deadlock_check(const s_mc_message_t*) const
71 bool deadlock = false;
72 if (not simix_global->process_list.empty()) {
74 for (auto const& kv : simix_global->process_list)
75 if (simgrid::mc::actor_is_enabled(kv.second)) {
82 s_mc_message_int_t answer{MC_MESSAGE_DEADLOCK_CHECK_REPLY, deadlock};
83 xbt_assert(channel_.send(answer) == 0, "Could not send response");
85 void AppSide::handle_continue(const s_mc_message_t*) const
89 void AppSide::handle_simcall(const s_mc_message_simcall_handle_t* message) const
91 smx_actor_t process = SIMIX_process_from_PID(message->pid);
92 xbt_assert(process != nullptr, "Invalid pid %lu", message->pid);
93 process->simcall_handle(message->value);
94 if (channel_.send(MC_MESSAGE_WAITING))
95 xbt_die("Could not send MESSAGE_WAITING to model-checker");
98 void AppSide::handle_actor_enabled(const s_mc_message_actor_enabled_t* msg) const
100 bool res = simgrid::mc::actor_is_enabled(SIMIX_process_from_PID(msg->aid));
101 s_mc_message_int_t answer{MC_MESSAGE_ACTOR_ENABLED_REPLY, res};
102 channel_.send(answer);
105 #define assert_msg_size(_name_, _type_) \
106 xbt_assert(received_size == sizeof(_type_), "Unexpected size for " _name_ " (%zd != %zu)", received_size, \
109 void AppSide::handle_messages() const
112 XBT_DEBUG("Waiting messages from model-checker");
114 char message_buffer[MC_MESSAGE_LENGTH];
115 ssize_t received_size = channel_.receive(&message_buffer, sizeof(message_buffer));
117 xbt_assert(received_size >= 0, "Could not receive commands from the model-checker");
119 const s_mc_message_t* message = (s_mc_message_t*)message_buffer;
120 switch (message->type) {
121 case MC_MESSAGE_DEADLOCK_CHECK:
122 assert_msg_size("DEADLOCK_CHECK", s_mc_message_t);
123 handle_deadlock_check(message);
126 case MC_MESSAGE_CONTINUE:
127 assert_msg_size("MESSAGE_CONTINUE", s_mc_message_t);
128 handle_continue(message);
131 case MC_MESSAGE_SIMCALL_HANDLE:
132 assert_msg_size("SIMCALL_HANDLE", s_mc_message_simcall_handle_t);
133 handle_simcall((s_mc_message_simcall_handle_t*)message_buffer);
136 case MC_MESSAGE_ACTOR_ENABLED:
137 assert_msg_size("ACTOR_ENABLED", s_mc_message_actor_enabled_t);
138 handle_actor_enabled((s_mc_message_actor_enabled_t*)message_buffer);
142 xbt_die("Received unexpected message %s (%i)", MC_message_type_name(message->type), message->type);
148 void AppSide::main_loop()
151 simgrid::mc::wait_for_requests();
152 xbt_assert(channel_.send(MC_MESSAGE_WAITING) == 0, "Could not send WAITING message to model-checker");
153 this->handle_messages();
157 void AppSide::report_assertion_failure() const
159 if (channel_.send(MC_MESSAGE_ASSERTION_FAILED))
160 xbt_die("Could not send assertion to model-checker");
161 this->handle_messages();
164 void AppSide::ignore_memory(void* addr, std::size_t size) const
166 s_mc_message_ignore_memory_t message;
167 message.type = MC_MESSAGE_IGNORE_MEMORY;
168 message.addr = (std::uintptr_t)addr;
170 if (channel_.send(message))
171 xbt_die("Could not send IGNORE_MEMORY message to model-checker");
174 void AppSide::ignore_heap(void* address, std::size_t size) const
176 const s_xbt_mheap_t* heap = mmalloc_get_current_heap();
178 s_mc_message_ignore_heap_t message;
179 message.type = MC_MESSAGE_IGNORE_HEAP;
180 message.address = address;
182 message.block = ((char*)address - (char*)heap->heapbase) / BLOCKSIZE + 1;
183 if (heap->heapinfo[message.block].type == 0) {
184 message.fragment = -1;
185 heap->heapinfo[message.block].busy_block.ignore++;
187 message.fragment = (ADDR2UINT(address) % BLOCKSIZE) >> heap->heapinfo[message.block].type;
188 heap->heapinfo[message.block].busy_frag.ignore[message.fragment]++;
191 if (channel_.send(message))
192 xbt_die("Could not send ignored region to MCer");
195 void AppSide::unignore_heap(void* address, std::size_t size) const
197 s_mc_message_ignore_memory_t message;
198 message.type = MC_MESSAGE_UNIGNORE_HEAP;
199 message.addr = (std::uintptr_t)address;
201 if (channel_.send(message))
202 xbt_die("Could not send IGNORE_HEAP message to model-checker");
205 void AppSide::declare_symbol(const char* name, int* value) const
207 s_mc_message_register_symbol_t message;
208 message.type = MC_MESSAGE_REGISTER_SYMBOL;
209 if (strlen(name) + 1 > sizeof(message.name))
210 xbt_die("Symbol is too long");
211 strncpy(message.name, name, sizeof(message.name));
212 message.callback = nullptr;
213 message.data = value;
214 if (channel_.send(message))
215 xbt_die("Could send REGISTER_SYMBOL message to model-checker");
218 void AppSide::declare_stack(void* stack, size_t size, ucontext_t* context) const
220 const s_xbt_mheap_t* heap = mmalloc_get_current_heap();
222 s_stack_region_t region;
223 memset(®ion, 0, sizeof(region));
224 region.address = stack;
225 region.context = context;
227 region.block = ((char*)stack - (char*)heap->heapbase) / BLOCKSIZE + 1;
229 s_mc_message_stack_region_t message;
230 message.type = MC_MESSAGE_STACK_REGION;
231 message.stack_region = region;
232 if (channel_.send(message))
233 xbt_die("Could not send STACK_REGION to model-checker");
236 } // namespace simgrid