1 /* Copyright (c) 2015-2019. 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/Client.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<Client> Client::instance_;
27 Client* Client::initialize()
29 // We are not in MC mode:
30 // TODO, handle this more gracefully.
31 if (not std::getenv(MC_ENV_SOCKET_FD))
34 // Do not break if we are called multiple times:
36 return instance_.get();
38 _sg_do_model_check = 1;
40 // Fetch socket from MC_ENV_SOCKET_FD:
41 char* fd_env = std::getenv(MC_ENV_SOCKET_FD);
43 xbt_die("No MC socket passed in the environment");
45 xbt_str_parse_int(fd_env, bprintf("Variable %s should contain a number but contains '%%s'", MC_ENV_SOCKET_FD));
46 XBT_DEBUG("Model-checked application found socket FD %i", fd);
48 // Check the socket type/validity:
50 socklen_t socklen = sizeof(type);
51 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &socklen) != 0)
52 xbt_die("Could not check socket type");
53 if (type != SOCK_SEQPACKET)
54 xbt_die("Unexpected socket type %i", type);
55 XBT_DEBUG("Model-checked application found expected socket type");
57 instance_.reset(new simgrid::mc::Client(fd));
59 // Wait for the model-checker:
62 ptrace(PTRACE_TRACEME, 0, nullptr, nullptr);
64 ptrace(PT_TRACE_ME, 0, nullptr, 0);
66 #error "no ptrace equivalent coded for this platform"
68 if (errno != 0 || raise(SIGSTOP) != 0)
69 xbt_die("Could not wait for the model-checker (errno = %d: %s)", errno, strerror(errno));
71 instance_->handle_messages();
72 return instance_.get();
75 void Client::handle_deadlock_check(s_mc_message_t*)
77 bool deadlock = false;
78 if (not simix_global->process_list.empty()) {
80 for (auto const& kv : simix_global->process_list)
81 if (simgrid::mc::actor_is_enabled(kv.second)) {
88 s_mc_message_int_t answer{MC_MESSAGE_DEADLOCK_CHECK_REPLY, deadlock};
89 xbt_assert(channel_.send(answer) == 0, "Could not send response");
91 void Client::handle_continue(s_mc_message_t*)
95 void Client::handle_simcall(s_mc_message_simcall_handle_t* message)
97 smx_actor_t process = SIMIX_process_from_PID(message->pid);
99 xbt_die("Invalid pid %lu", (unsigned long)message->pid);
100 process->simcall_handle(message->value);
101 if (channel_.send(MC_MESSAGE_WAITING))
102 xbt_die("Could not send MESSAGE_WAITING to model-checker");
105 void Client::handle_actor_enabled(s_mc_message_actor_enabled_t* msg)
107 bool res = simgrid::mc::actor_is_enabled(SIMIX_process_from_PID(msg->aid));
108 s_mc_message_int_t answer{MC_MESSAGE_ACTOR_ENABLED_REPLY, res};
109 channel_.send(answer);
112 void Client::handle_messages()
115 XBT_DEBUG("Waiting messages from model-checker");
117 char message_buffer[MC_MESSAGE_LENGTH];
118 ssize_t received_size = channel_.receive(&message_buffer, sizeof(message_buffer));
120 if (received_size < 0)
121 xbt_die("Could not receive commands from the model-checker");
123 s_mc_message_t* message = (s_mc_message_t*)message_buffer;
124 switch (message->type) {
125 case MC_MESSAGE_DEADLOCK_CHECK:
126 xbt_assert(received_size == sizeof(s_mc_message_t), "Unexpected size for DEADLOCK_CHECK (%zd != %zu)",
127 received_size, sizeof(s_mc_message_t));
128 handle_deadlock_check(message);
131 case MC_MESSAGE_CONTINUE:
132 xbt_assert(received_size == sizeof(s_mc_message_t), "Unexpected size for MESSAGE_CONTINUE (%zd != %zu)",
133 received_size, sizeof(s_mc_message_t));
134 handle_continue(message);
137 case MC_MESSAGE_SIMCALL_HANDLE:
138 xbt_assert(received_size == sizeof(s_mc_message_simcall_handle_t),
139 "Unexpected size for SIMCALL_HANDLE (%zd != %zu)", received_size,
140 sizeof(s_mc_message_simcall_handle_t));
141 handle_simcall((s_mc_message_simcall_handle_t*)message_buffer);
144 case MC_MESSAGE_ACTOR_ENABLED:
145 xbt_assert(received_size == sizeof(s_mc_message_actor_enabled_t),
146 "Unexpected size for ACTOR_ENABLED (%zd != %zu)", received_size,
147 sizeof(s_mc_message_actor_enabled_t));
148 handle_actor_enabled((s_mc_message_actor_enabled_t*)message_buffer);
152 xbt_die("Received unexpected message %s (%i)", MC_message_type_name(message->type), message->type);
158 void Client::main_loop()
161 simgrid::mc::wait_for_requests();
162 xbt_assert(channel_.send(MC_MESSAGE_WAITING) == 0, "Could not send WAITING message to model-checker");
163 this->handle_messages();
167 void Client::report_assertion_failure()
169 if (channel_.send(MC_MESSAGE_ASSERTION_FAILED))
170 xbt_die("Could not send assertion to model-checker");
171 this->handle_messages();
174 void Client::ignore_memory(void* addr, std::size_t size)
176 s_mc_message_ignore_memory_t message;
177 message.type = MC_MESSAGE_IGNORE_MEMORY;
178 message.addr = (std::uintptr_t)addr;
180 if (channel_.send(message))
181 xbt_die("Could not send IGNORE_MEMORY mesage to model-checker");
184 void Client::ignore_heap(void* address, std::size_t size)
186 xbt_mheap_t heap = mmalloc_get_current_heap();
188 s_mc_message_ignore_heap_t message;
189 message.type = MC_MESSAGE_IGNORE_HEAP;
190 message.address = address;
192 message.block = ((char*)address - (char*)heap->heapbase) / BLOCKSIZE + 1;
193 if (heap->heapinfo[message.block].type == 0) {
194 message.fragment = -1;
195 heap->heapinfo[message.block].busy_block.ignore++;
197 message.fragment = ((uintptr_t)(ADDR2UINT(address) % (BLOCKSIZE))) >> heap->heapinfo[message.block].type;
198 heap->heapinfo[message.block].busy_frag.ignore[message.fragment]++;
201 if (channel_.send(message))
202 xbt_die("Could not send ignored region to MCer");
205 void Client::unignore_heap(void* address, std::size_t size)
207 s_mc_message_ignore_memory_t message;
208 message.type = MC_MESSAGE_UNIGNORE_HEAP;
209 message.addr = (std::uintptr_t)address;
211 if (channel_.send(message))
212 xbt_die("Could not send IGNORE_HEAP message to model-checker");
215 void Client::declare_symbol(const char* name, int* value)
217 s_mc_message_register_symbol_t message;
218 message.type = MC_MESSAGE_REGISTER_SYMBOL;
219 if (strlen(name) + 1 > sizeof(message.name))
220 xbt_die("Symbol is too long");
221 strncpy(message.name, name, sizeof(message.name));
222 message.callback = nullptr;
223 message.data = value;
224 if (channel_.send(message))
225 xbt_die("Could send REGISTER_SYMBOL message to model-checker");
228 void Client::declare_stack(void* stack, size_t size, ucontext_t* context)
230 xbt_mheap_t heap = mmalloc_get_current_heap();
232 s_stack_region_t region;
233 memset(®ion, 0, sizeof(region));
234 region.address = stack;
235 region.context = context;
237 region.block = ((char*)stack - (char*)heap->heapbase) / BLOCKSIZE + 1;
239 s_mc_message_stack_region_t message;
240 message.type = MC_MESSAGE_STACK_REGION;
241 message.stack_region = region;
242 if (channel_.send(message))
243 xbt_die("Could not send STACK_REGION to model-checker");
246 } // namespace simgrid