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_smx.h"
30 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_client, mc, "MC client logic");
35 std::unique_ptr<Client> Client::client_;
37 Client* Client::initialize()
39 // We are not in MC mode:
40 // TODO, handle this more gracefully.
41 if (not std::getenv(MC_ENV_SOCKET_FD))
44 // Do not break if we are called multiple times:
48 _sg_do_model_check = 1;
50 // Fetch socket from MC_ENV_SOCKET_FD:
51 char* fd_env = std::getenv(MC_ENV_SOCKET_FD);
53 xbt_die("No MC socket passed in the environment");
55 xbt_str_parse_int(fd_env, bprintf("Variable %s should contain a number but contains '%%s'", 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 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &socklen) != 0)
62 xbt_die("Could not check socket type");
63 if (type != SOCK_DGRAM)
64 xbt_die("Unexpected socket type %i", type);
65 XBT_DEBUG("Model-checked application found expected socket type");
67 client_ = std::unique_ptr<Client>(new simgrid::mc::Client(fd));
69 // Wait for the model-checker:
72 ptrace(PTRACE_TRACEME, 0, nullptr, nullptr);
74 ptrace(PT_TRACE_ME, 0, nullptr, 0);
76 #error "no ptrace equivalent coded for this platform"
78 if (errno != 0 || raise(SIGSTOP) != 0)
79 xbt_die("Could not wait for the model-checker");
81 client_->handleMessages();
85 void Client::handleMessages()
88 XBT_DEBUG("Waiting messages from model-checker");
90 char message_buffer[MC_MESSAGE_LENGTH];
91 ssize_t received_size;
93 if ((received_size = channel_.receive(&message_buffer, sizeof(message_buffer))) < 0)
94 xbt_die("Could not receive commands from the model-checker");
96 s_mc_message_t message;
97 if ((size_t)received_size < sizeof(message))
98 xbt_die("Received message is too small");
99 memcpy(&message, message_buffer, sizeof(message));
100 switch (message.type) {
102 case MC_MESSAGE_DEADLOCK_CHECK: {
104 bool deadlock = false;
105 if (not simix_global->process_list.empty()) {
107 for (auto kv : simix_global->process_list)
108 if (simgrid::mc::actor_is_enabled(kv.second)) {
115 s_mc_int_message_t answer;
116 answer.type = MC_MESSAGE_DEADLOCK_CHECK_REPLY;
117 answer.value = deadlock;
118 xbt_assert(channel_.send(answer) == 0, "Could not send response");
121 case MC_MESSAGE_CONTINUE:
124 case MC_MESSAGE_SIMCALL_HANDLE: {
125 s_mc_simcall_handle_message_t message;
126 if (received_size != sizeof(message))
127 xbt_die("Unexpected size for SIMCALL_HANDLE");
128 memcpy(&message, message_buffer, sizeof(message));
129 smx_actor_t process = SIMIX_process_from_PID(message.pid);
131 xbt_die("Invalid pid %lu", (unsigned long)message.pid);
132 SIMIX_simcall_handle(&process->simcall, message.value);
133 if (channel_.send(MC_MESSAGE_WAITING))
134 xbt_die("Could not send MESSAGE_WAITING to model-checker");
137 case MC_MESSAGE_RESTORE: {
138 s_mc_restore_message_t message;
139 if (received_size != sizeof(message))
140 xbt_die("Unexpected size for SIMCALL_HANDLE");
141 memcpy(&message, message_buffer, sizeof(message));
143 smpi_really_switch_data_segment(message.index);
148 xbt_die("Received unexpected message %s (%i)", MC_message_type_name(message.type), message.type);
153 void Client::mainLoop()
156 simgrid::mc::wait_for_requests();
157 xbt_assert(channel_.send(MC_MESSAGE_WAITING) == 0, "Could not send WAITING message to model-checker");
158 this->handleMessages();
162 void Client::reportAssertionFailure(const char* description)
164 if (channel_.send(MC_MESSAGE_ASSERTION_FAILED))
165 xbt_die("Could not send assertion to model-checker");
166 this->handleMessages();
169 void Client::ignoreMemory(void* addr, std::size_t size)
171 s_mc_ignore_memory_message_t message;
172 message.type = MC_MESSAGE_IGNORE_MEMORY;
173 message.addr = (std::uintptr_t)addr;
175 if (channel_.send(message))
176 xbt_die("Could not send IGNORE_MEMORY mesage to model-checker");
179 void Client::ignoreHeap(void* address, std::size_t size)
181 xbt_mheap_t heap = mmalloc_get_current_heap();
183 s_mc_ignore_heap_message_t message;
184 message.type = MC_MESSAGE_IGNORE_HEAP;
185 message.address = address;
187 message.block = ((char*)address - (char*)heap->heapbase) / BLOCKSIZE + 1;
188 if (heap->heapinfo[message.block].type == 0) {
189 message.fragment = -1;
190 heap->heapinfo[message.block].busy_block.ignore++;
192 message.fragment = ((uintptr_t)(ADDR2UINT(address) % (BLOCKSIZE))) >> heap->heapinfo[message.block].type;
193 heap->heapinfo[message.block].busy_frag.ignore[message.fragment]++;
196 if (channel_.send(message))
197 xbt_die("Could not send ignored region to MCer");
200 void Client::unignoreHeap(void* address, std::size_t size)
202 s_mc_ignore_memory_message_t message;
203 message.type = MC_MESSAGE_UNIGNORE_HEAP;
204 message.addr = (std::uintptr_t)address;
206 if (channel_.send(message))
207 xbt_die("Could not send IGNORE_HEAP message to model-checker");
210 void Client::declareSymbol(const char* name, int* value)
212 s_mc_register_symbol_message_t message;
213 message.type = MC_MESSAGE_REGISTER_SYMBOL;
214 if (strlen(name) + 1 > sizeof(message.name))
215 xbt_die("Symbol is too long");
216 strncpy(message.name, name, sizeof(message.name));
217 message.callback = nullptr;
218 message.data = value;
219 if (channel_.send(message))
220 xbt_die("Could send REGISTER_SYMBOL message to model-checker");
223 void Client::declareStack(void* stack, size_t size, smx_actor_t process, ucontext_t* context)
225 xbt_mheap_t heap = mmalloc_get_current_heap();
227 s_stack_region_t region;
228 memset(®ion, 0, sizeof(region));
229 region.address = stack;
230 region.context = context;
232 region.block = ((char*)stack - (char*)heap->heapbase) / BLOCKSIZE + 1;
234 if (smpi_privatize_global_variables == SMPI_PRIVATIZE_MMAP && process)
235 region.process_index = process->pid - 1;
238 region.process_index = -1;
240 s_mc_stack_region_message_t message;
241 message.type = MC_MESSAGE_STACK_REGION;
242 message.stack_region = region;
243 if (channel_.send(message))
244 xbt_die("Could not send STACK_REGION to model-checker");