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. */
10 #include <sys/ptrace.h>
11 #include <sys/socket.h>
12 #include <sys/types.h>
15 #include <xbt/mmalloc.h>
16 #include <xbt/sysdep.h>
18 #include <simgrid/modelchecker.h>
20 #include "src/internal_config.h"
22 #include "src/mc/mc_request.hpp"
23 #include "src/mc/remote/Client.hpp"
24 #include "src/mc/remote/mc_protocol.h"
26 #include "src/smpi/include/private.hpp"
28 // We won't need those once the separation MCer/MCed is complete:
29 #include "src/mc/mc_smx.hpp"
31 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_client, mc, "MC client logic");
36 std::unique_ptr<Client> Client::instance_;
38 Client* Client::initialize()
40 // We are not in MC mode:
41 // TODO, handle this more gracefully.
42 if (not std::getenv(MC_ENV_SOCKET_FD))
45 // Do not break if we are called multiple times:
47 return instance_.get();
49 _sg_do_model_check = 1;
51 // Fetch socket from MC_ENV_SOCKET_FD:
52 char* fd_env = std::getenv(MC_ENV_SOCKET_FD);
54 xbt_die("No MC socket passed in the environment");
56 xbt_str_parse_int(fd_env, bprintf("Variable %s should contain a number but contains '%%s'", MC_ENV_SOCKET_FD));
57 XBT_DEBUG("Model-checked application found socket FD %i", fd);
59 // Check the socket type/validity:
61 socklen_t socklen = sizeof(type);
62 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &socklen) != 0)
63 xbt_die("Could not check socket type");
64 if (type != SOCK_SEQPACKET)
65 xbt_die("Unexpected socket type %i", type);
66 XBT_DEBUG("Model-checked application found expected socket type");
68 instance_ = std::unique_ptr<Client>(new simgrid::mc::Client(fd));
70 // Wait for the model-checker:
73 ptrace(PTRACE_TRACEME, 0, nullptr, nullptr);
75 ptrace(PT_TRACE_ME, 0, nullptr, 0);
77 #error "no ptrace equivalent coded for this platform"
79 if (errno != 0 || raise(SIGSTOP) != 0)
80 xbt_die("Could not wait for the model-checker (errno = %d: %s)", errno, strerror(errno));
82 instance_->handleMessages();
83 return instance_.get();
86 void Client::handleDeadlockCheck(s_mc_message_t* msg)
88 bool deadlock = false;
89 if (not simix_global->process_list.empty()) {
91 for (auto const& kv : simix_global->process_list)
92 if (simgrid::mc::actor_is_enabled(kv.second)) {
99 s_mc_message_int_t answer{MC_MESSAGE_DEADLOCK_CHECK_REPLY, deadlock};
100 xbt_assert(channel_.send(answer) == 0, "Could not send response");
102 void Client::handleContinue(s_mc_message_t* msg)
106 void Client::handleSimcall(s_mc_message_simcall_handle_t* message)
108 smx_actor_t process = SIMIX_process_from_PID(message->pid);
110 xbt_die("Invalid pid %lu", (unsigned long)message->pid);
111 SIMIX_simcall_handle(&process->simcall, message->value);
112 if (channel_.send(MC_MESSAGE_WAITING))
113 xbt_die("Could not send MESSAGE_WAITING to model-checker");
115 void Client::handleRestore(s_mc_message_restore_t* message)
118 smpi_really_switch_data_segment(simgrid::s4u::Actor::by_pid(message->index));
121 void Client::handleActorEnabled(s_mc_message_actor_enabled_t* msg)
123 bool res = simgrid::mc::actor_is_enabled(SIMIX_process_from_PID(msg->aid));
124 s_mc_message_int_t answer{MC_MESSAGE_ACTOR_ENABLED_REPLY, res};
125 channel_.send(answer);
128 void Client::handleMessages()
131 XBT_DEBUG("Waiting messages from model-checker");
133 char message_buffer[MC_MESSAGE_LENGTH];
134 ssize_t received_size = channel_.receive(&message_buffer, sizeof(message_buffer));
136 if (received_size < 0)
137 xbt_die("Could not receive commands from the model-checker");
139 s_mc_message_t* message = (s_mc_message_t*)message_buffer;
140 switch (message->type) {
142 case MC_MESSAGE_DEADLOCK_CHECK:
143 xbt_assert(received_size == sizeof(s_mc_message_t), "Unexpected size for DEADLOCK_CHECK (%zd != %zu)",
144 received_size, sizeof(s_mc_message_t));
145 handleDeadlockCheck(message);
148 case MC_MESSAGE_CONTINUE:
149 xbt_assert(received_size == sizeof(s_mc_message_t), "Unexpected size for MESSAGE_CONTINUE (%zd != %zu)",
150 received_size, sizeof(s_mc_message_t));
151 handleContinue(message);
154 case MC_MESSAGE_SIMCALL_HANDLE:
155 xbt_assert(received_size == sizeof(s_mc_message_simcall_handle_t),
156 "Unexpected size for SIMCALL_HANDLE (%zd != %zu)", received_size,
157 sizeof(s_mc_message_simcall_handle_t));
158 handleSimcall((s_mc_message_simcall_handle_t*)message_buffer);
161 case MC_MESSAGE_RESTORE:
162 xbt_assert(received_size == sizeof(s_mc_message_t), "Unexpected size for MESSAGE_RESTORE (%zd != %zu)",
163 received_size, sizeof(s_mc_message_t));
164 handleRestore((s_mc_message_restore_t*)message_buffer);
167 case MC_MESSAGE_ACTOR_ENABLED:
168 xbt_assert(received_size == sizeof(s_mc_message_actor_enabled_t),
169 "Unexpected size for ACTOR_ENABLED (%zd != %zu)", received_size,
170 sizeof(s_mc_message_actor_enabled_t));
171 handleActorEnabled((s_mc_message_actor_enabled_t*)message_buffer);
175 xbt_die("Received unexpected message %s (%i)", MC_message_type_name(message->type), message->type);
181 void Client::mainLoop()
184 simgrid::mc::wait_for_requests();
185 xbt_assert(channel_.send(MC_MESSAGE_WAITING) == 0, "Could not send WAITING message to model-checker");
186 this->handleMessages();
190 void Client::reportAssertionFailure(const char* description)
192 if (channel_.send(MC_MESSAGE_ASSERTION_FAILED))
193 xbt_die("Could not send assertion to model-checker");
194 this->handleMessages();
197 void Client::ignoreMemory(void* addr, std::size_t size)
199 s_mc_message_ignore_memory_t message;
200 message.type = MC_MESSAGE_IGNORE_MEMORY;
201 message.addr = (std::uintptr_t)addr;
203 if (channel_.send(message))
204 xbt_die("Could not send IGNORE_MEMORY mesage to model-checker");
207 void Client::ignoreHeap(void* address, std::size_t size)
209 xbt_mheap_t heap = mmalloc_get_current_heap();
211 s_mc_message_ignore_heap_t message;
212 message.type = MC_MESSAGE_IGNORE_HEAP;
213 message.address = address;
215 message.block = ((char*)address - (char*)heap->heapbase) / BLOCKSIZE + 1;
216 if (heap->heapinfo[message.block].type == 0) {
217 message.fragment = -1;
218 heap->heapinfo[message.block].busy_block.ignore++;
220 message.fragment = ((uintptr_t)(ADDR2UINT(address) % (BLOCKSIZE))) >> heap->heapinfo[message.block].type;
221 heap->heapinfo[message.block].busy_frag.ignore[message.fragment]++;
224 if (channel_.send(message))
225 xbt_die("Could not send ignored region to MCer");
228 void Client::unignoreHeap(void* address, std::size_t size)
230 s_mc_message_ignore_memory_t message;
231 message.type = MC_MESSAGE_UNIGNORE_HEAP;
232 message.addr = (std::uintptr_t)address;
234 if (channel_.send(message))
235 xbt_die("Could not send IGNORE_HEAP message to model-checker");
238 void Client::declareSymbol(const char* name, int* value)
240 s_mc_message_register_symbol_t message;
241 message.type = MC_MESSAGE_REGISTER_SYMBOL;
242 if (strlen(name) + 1 > sizeof(message.name))
243 xbt_die("Symbol is too long");
244 strncpy(message.name, name, sizeof(message.name));
245 message.callback = nullptr;
246 message.data = value;
247 if (channel_.send(message))
248 xbt_die("Could send REGISTER_SYMBOL message to model-checker");
251 void Client::declareStack(void* stack, size_t size, smx_actor_t process, ucontext_t* context)
253 xbt_mheap_t heap = mmalloc_get_current_heap();
255 s_stack_region_t region;
256 memset(®ion, 0, sizeof(region));
257 region.address = stack;
258 region.context = context;
260 region.block = ((char*)stack - (char*)heap->heapbase) / BLOCKSIZE + 1;
262 if (smpi_privatize_global_variables == SmpiPrivStrategies::MMAP && process)
263 region.process_index = process->pid_ - 1;
266 region.process_index = -1;
268 s_mc_message_stack_region_t message;
269 message.type = MC_MESSAGE_STACK_REGION;
270 message.stack_region = region;
271 if (channel_.send(message))
272 xbt_die("Could not send STACK_REGION to model-checker");