1 /* Copyright (c) 2015. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * 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>
17 #include <xbt/sysdep.h>
19 #include <simgrid/modelchecker.h>
21 #include "src/internal_config.h"
23 #include "src/mc/mc_request.h"
24 #include "src/mc/remote/Client.hpp"
25 #include "src/mc/remote/mc_protocol.h"
27 // We won't need those once the separation MCer/MCed is complete:
28 #include "src/mc/mc_ignore.h"
29 #include "src/mc/mc_smx.h"
31 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_client, mc, "MC client logic");
36 std::unique_ptr<Client> Client::client_;
38 Client* Client::initialize()
40 // We are not in MC mode:
41 // TODO, handle this more gracefully.
42 if (!std::getenv(MC_ENV_SOCKET_FD))
45 // Do not break if we are called multiple times:
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_DGRAM)
65 xbt_die("Unexpected socket type %i", type);
66 XBT_DEBUG("Model-checked application found expected socket type");
68 client_ = 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");
82 client_->handleMessages();
86 void Client::handleMessages()
89 XBT_DEBUG("Waiting messages from model-checker");
91 char message_buffer[MC_MESSAGE_LENGTH];
92 ssize_t received_size;
94 if ((received_size = channel_.receive(&message_buffer, sizeof(message_buffer))) < 0)
95 xbt_die("Could not receive commands from the model-checker");
97 s_mc_message_t message;
98 if ((size_t)received_size < sizeof(message))
99 xbt_die("Received message is too small");
100 memcpy(&message, message_buffer, sizeof(message));
101 switch (message.type) {
103 case MC_MESSAGE_DEADLOCK_CHECK: {
105 bool deadlock = false;
106 if (!simix_global->process_list.empty()) {
108 for (auto kv : simix_global->process_list)
109 if (simgrid::mc::actor_is_enabled(kv.second)) {
116 s_mc_int_message_t answer;
117 answer.type = MC_MESSAGE_DEADLOCK_CHECK_REPLY;
118 answer.value = deadlock;
119 xbt_assert(channel_.send(answer) == 0, "Could not send response");
122 case MC_MESSAGE_CONTINUE:
125 case MC_MESSAGE_SIMCALL_HANDLE: {
126 s_mc_simcall_handle_message_t message;
127 if (received_size != sizeof(message))
128 xbt_die("Unexpected size for SIMCALL_HANDLE");
129 memcpy(&message, message_buffer, sizeof(message));
130 smx_actor_t process = SIMIX_process_from_PID(message.pid);
132 xbt_die("Invalid pid %lu", (unsigned long)message.pid);
133 SIMIX_simcall_handle(&process->simcall, message.value);
134 if (channel_.send(MC_MESSAGE_WAITING))
135 xbt_die("Could not send MESSAGE_WAITING to model-checker");
138 case MC_MESSAGE_RESTORE: {
139 s_mc_restore_message_t message;
140 if (received_size != sizeof(message))
141 xbt_die("Unexpected size for SIMCALL_HANDLE");
142 memcpy(&message, message_buffer, sizeof(message));
144 smpi_really_switch_data_segment(message.index);
149 xbt_die("Received unexpected message %s (%i)", MC_message_type_name(message.type), message.type);
154 void Client::mainLoop(void)
157 simgrid::mc::wait_for_requests();
158 xbt_assert(channel_.send(MC_MESSAGE_WAITING) == 0, "Could not send WAITING message to model-checker");
159 this->handleMessages();
163 void Client::reportAssertionFailure(const char* description)
165 if (channel_.send(MC_MESSAGE_ASSERTION_FAILED))
166 xbt_die("Could not send assertion to model-checker");
167 this->handleMessages();
170 void Client::ignoreMemory(void* addr, std::size_t size)
172 s_mc_ignore_memory_message_t message;
173 message.type = MC_MESSAGE_IGNORE_MEMORY;
174 message.addr = (std::uintptr_t)addr;
176 if (channel_.send(message))
177 xbt_die("Could not send IGNORE_MEMORY mesage to model-checker");
180 void Client::ignoreHeap(void* address, std::size_t size)
182 xbt_mheap_t heap = mmalloc_get_current_heap();
184 s_mc_ignore_heap_message_t message;
185 message.type = MC_MESSAGE_IGNORE_HEAP;
186 message.address = address;
188 message.block = ((char*)address - (char*)heap->heapbase) / BLOCKSIZE + 1;
189 if (heap->heapinfo[message.block].type == 0) {
190 message.fragment = -1;
191 heap->heapinfo[message.block].busy_block.ignore++;
193 message.fragment = ((uintptr_t)(ADDR2UINT(address) % (BLOCKSIZE))) >> heap->heapinfo[message.block].type;
194 heap->heapinfo[message.block].busy_frag.ignore[message.fragment]++;
197 if (channel_.send(message))
198 xbt_die("Could not send ignored region to MCer");
201 void Client::unignoreHeap(void* address, std::size_t size)
203 s_mc_ignore_memory_message_t message;
204 message.type = MC_MESSAGE_UNIGNORE_HEAP;
205 message.addr = (std::uintptr_t)address;
207 if (channel_.send(message))
208 xbt_die("Could not send IGNORE_HEAP message to model-checker");
211 void Client::declareSymbol(const char* name, int* value)
213 s_mc_register_symbol_message_t message;
214 message.type = MC_MESSAGE_REGISTER_SYMBOL;
215 if (strlen(name) + 1 > sizeof(message.name))
216 xbt_die("Symbol is too long");
217 strncpy(message.name, name, sizeof(message.name));
218 message.callback = nullptr;
219 message.data = value;
220 if (channel_.send(message))
221 xbt_die("Could send REGISTER_SYMBOL message to model-checker");
224 void Client::declareStack(void* stack, size_t size, smx_actor_t process, ucontext_t* context)
226 xbt_mheap_t heap = mmalloc_get_current_heap();
228 s_stack_region_t region;
229 memset(®ion, 0, sizeof(region));
230 region.address = stack;
231 region.context = context;
233 region.block = ((char*)stack - (char*)heap->heapbase) / BLOCKSIZE + 1;
235 if (smpi_privatize_global_variables && process)
236 region.process_index = smpi_process_index_of_smx_process(process);
239 region.process_index = -1;
241 s_mc_stack_region_message_t message;
242 message.type = MC_MESSAGE_STACK_REGION;
243 message.stack_region = region;
244 if (channel_.send(message))
245 xbt_die("Coule not send STACK_REGION to model-checker");