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 (!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];
94 if ((s = 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)s < 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;
107 if (xbt_swag_size(simix_global->process_list)) {
109 xbt_swag_foreach(actor, simix_global->process_list) if (simgrid::mc::actor_is_enabled(actor))
117 s_mc_int_message_t answer;
118 answer.type = MC_MESSAGE_DEADLOCK_CHECK_REPLY;
119 answer.value = deadlock;
120 if (channel_.send(answer))
121 xbt_die("Could not send response");
124 case MC_MESSAGE_CONTINUE:
127 case MC_MESSAGE_SIMCALL_HANDLE: {
128 s_mc_simcall_handle_message_t message;
129 if (s != sizeof(message))
130 xbt_die("Unexpected size for SIMCALL_HANDLE");
131 memcpy(&message, message_buffer, sizeof(message));
132 smx_actor_t process = SIMIX_process_from_PID(message.pid);
134 xbt_die("Invalid pid %lu", (unsigned long)message.pid);
135 SIMIX_simcall_handle(&process->simcall, message.value);
136 if (channel_.send(MC_MESSAGE_WAITING))
137 xbt_die("Could not send MESSAGE_WAITING to model-checker");
140 case MC_MESSAGE_RESTORE: {
141 s_mc_restore_message_t message;
142 if (s != sizeof(message))
143 xbt_die("Unexpected size for SIMCALL_HANDLE");
144 memcpy(&message, message_buffer, sizeof(message));
146 smpi_really_switch_data_segment(message.index);
151 xbt_die("Received unexpected message %s (%i)", MC_message_type_name(message.type), message.type);
156 void Client::mainLoop(void)
159 simgrid::mc::wait_for_requests();
160 if (channel_.send(MC_MESSAGE_WAITING))
161 xbt_die("Could not send WAITING mesage to model-checker");
162 this->handleMessages();
166 void Client::reportAssertionFailure(const char* description)
168 if (channel_.send(MC_MESSAGE_ASSERTION_FAILED))
169 xbt_die("Could not send assertion to model-checker");
170 this->handleMessages();
173 void Client::ignoreMemory(void* addr, std::size_t size)
175 s_mc_ignore_memory_message_t message;
176 message.type = MC_MESSAGE_IGNORE_MEMORY;
177 message.addr = (std::uintptr_t)addr;
179 if (channel_.send(message))
180 xbt_die("Could not send IGNORE_MEMORY mesage to model-checker");
183 void Client::ignoreHeap(void* address, std::size_t size)
185 xbt_mheap_t heap = mmalloc_get_current_heap();
187 s_mc_ignore_heap_message_t message;
188 message.type = MC_MESSAGE_IGNORE_HEAP;
189 message.address = address;
191 message.block = ((char*)address - (char*)heap->heapbase) / BLOCKSIZE + 1;
192 if (heap->heapinfo[message.block].type == 0) {
193 message.fragment = -1;
194 heap->heapinfo[message.block].busy_block.ignore++;
196 message.fragment = ((uintptr_t)(ADDR2UINT(address) % (BLOCKSIZE))) >> heap->heapinfo[message.block].type;
197 heap->heapinfo[message.block].busy_frag.ignore[message.fragment]++;
200 if (channel_.send(message))
201 xbt_die("Could not send ignored region to MCer");
204 void Client::unignoreHeap(void* address, std::size_t size)
206 s_mc_ignore_memory_message_t message;
207 message.type = MC_MESSAGE_UNIGNORE_HEAP;
208 message.addr = (std::uintptr_t)address;
210 if (channel_.send(message))
211 xbt_die("Could not send IGNORE_HEAP mesasge to model-checker");
214 void Client::declareSymbol(const char* name, int* value)
216 s_mc_register_symbol_message_t message;
217 message.type = MC_MESSAGE_REGISTER_SYMBOL;
218 if (strlen(name) + 1 > sizeof(message.name))
219 xbt_die("Symbol is too long");
220 strncpy(message.name, name, sizeof(message.name));
221 message.callback = nullptr;
222 message.data = value;
223 if (channel_.send(message))
224 xbt_die("Could send REGISTER_SYMBOL message to model-checker");
227 void Client::declareStack(void* stack, size_t size, smx_actor_t process, ucontext_t* context)
229 xbt_mheap_t heap = mmalloc_get_current_heap();
231 s_stack_region_t region;
232 memset(®ion, 0, sizeof(region));
233 region.address = stack;
234 region.context = context;
236 region.block = ((char*)stack - (char*)heap->heapbase) / BLOCKSIZE + 1;
238 if (smpi_privatize_global_variables && process)
239 region.process_index = smpi_process_index_of_smx_process(process);
242 region.process_index = -1;
244 s_mc_stack_region_message_t message;
245 message.type = MC_MESSAGE_STACK_REGION;
246 message.stack_region = region;
247 if (channel_.send(message))
248 xbt_die("Coule not send STACK_REGION to model-checker");