1 /* Copyright (c) 2015-2017. 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. */
9 #include <sys/ptrace.h>
10 #include <sys/socket.h>
11 #include <sys/types.h>
14 #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.h"
23 #include "src/mc/remote/Client.hpp"
24 #include "src/mc/remote/mc_protocol.h"
26 #include "src/smpi/private.hpp"
28 // We won't need those once the separation MCer/MCed is complete:
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::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_DGRAM)
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");
82 instance_->handleMessages();
83 return instance_.get();
86 void Client::handleDeadlockCheck(mc_message_t* msg)
88 bool deadlock = false;
89 if (not simix_global->process_list.empty()) {
91 for (auto kv : simix_global->process_list)
92 if (simgrid::mc::actor_is_enabled(kv.second)) {
99 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(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(message->index);
122 void Client::handleMessages()
125 XBT_DEBUG("Waiting messages from model-checker");
127 char message_buffer[MC_MESSAGE_LENGTH];
128 ssize_t received_size = channel_.receive(&message_buffer, sizeof(message_buffer));
130 if (received_size < 0)
131 xbt_die("Could not receive commands from the model-checker");
133 mc_message_t* message = (mc_message_t*)message_buffer;
134 switch (message->type) {
136 case MC_MESSAGE_DEADLOCK_CHECK:
137 xbt_assert(received_size == sizeof(mc_message_t), "Unexpected size for DEADLOCK_CHECK (%zu != %zu)",
138 received_size, sizeof(mc_message_t));
139 handleDeadlockCheck(message);
142 case MC_MESSAGE_CONTINUE:
143 xbt_assert(received_size == sizeof(mc_message_t), "Unexpected size for MESSAGE_CONTINUE (%zu != %zu)",
144 received_size, sizeof(mc_message_t));
145 handleContinue(message);
148 case MC_MESSAGE_SIMCALL_HANDLE:
149 xbt_assert(received_size == sizeof(s_mc_message_simcall_handle_t),
150 "Unexpected size for SIMCALL_HANDLE (%zu != %zu)", received_size,
151 sizeof(s_mc_message_simcall_handle_t));
152 handleSimcall((s_mc_message_simcall_handle_t*)message_buffer);
155 case MC_MESSAGE_RESTORE:
156 xbt_assert(received_size == sizeof(mc_message_t), "Unexpected size for MESSAGE_RESTORE (%zu != %zu)",
157 received_size, sizeof(mc_message_t));
158 handleRestore((s_mc_message_restore_t*)message_buffer);
162 xbt_die("Received unexpected message %s (%i)", MC_message_type_name(message->type), message->type);
168 void Client::mainLoop()
171 simgrid::mc::wait_for_requests();
172 xbt_assert(channel_.send(MC_MESSAGE_WAITING) == 0, "Could not send WAITING message to model-checker");
173 this->handleMessages();
177 void Client::reportAssertionFailure(const char* description)
179 if (channel_.send(MC_MESSAGE_ASSERTION_FAILED))
180 xbt_die("Could not send assertion to model-checker");
181 this->handleMessages();
184 void Client::ignoreMemory(void* addr, std::size_t size)
186 s_mc_message_ignore_memory_t message;
187 message.type = MC_MESSAGE_IGNORE_MEMORY;
188 message.addr = (std::uintptr_t)addr;
190 if (channel_.send(message))
191 xbt_die("Could not send IGNORE_MEMORY mesage to model-checker");
194 void Client::ignoreHeap(void* address, std::size_t size)
196 xbt_mheap_t heap = mmalloc_get_current_heap();
198 s_mc_message_ignore_heap_t message;
199 message.type = MC_MESSAGE_IGNORE_HEAP;
200 message.address = address;
202 message.block = ((char*)address - (char*)heap->heapbase) / BLOCKSIZE + 1;
203 if (heap->heapinfo[message.block].type == 0) {
204 message.fragment = -1;
205 heap->heapinfo[message.block].busy_block.ignore++;
207 message.fragment = ((uintptr_t)(ADDR2UINT(address) % (BLOCKSIZE))) >> heap->heapinfo[message.block].type;
208 heap->heapinfo[message.block].busy_frag.ignore[message.fragment]++;
211 if (channel_.send(message))
212 xbt_die("Could not send ignored region to MCer");
215 void Client::unignoreHeap(void* address, std::size_t size)
217 s_mc_message_ignore_memory_t message;
218 message.type = MC_MESSAGE_UNIGNORE_HEAP;
219 message.addr = (std::uintptr_t)address;
221 if (channel_.send(message))
222 xbt_die("Could not send IGNORE_HEAP message to model-checker");
225 void Client::declareSymbol(const char* name, int* value)
227 s_mc_message_register_symbol_t message;
228 message.type = MC_MESSAGE_REGISTER_SYMBOL;
229 if (strlen(name) + 1 > sizeof(message.name))
230 xbt_die("Symbol is too long");
231 strncpy(message.name, name, sizeof(message.name));
232 message.callback = nullptr;
233 message.data = value;
234 if (channel_.send(message))
235 xbt_die("Could send REGISTER_SYMBOL message to model-checker");
238 void Client::declareStack(void* stack, size_t size, smx_actor_t process, ucontext_t* context)
240 xbt_mheap_t heap = mmalloc_get_current_heap();
242 s_stack_region_t region;
243 memset(®ion, 0, sizeof(region));
244 region.address = stack;
245 region.context = context;
247 region.block = ((char*)stack - (char*)heap->heapbase) / BLOCKSIZE + 1;
249 if (smpi_privatize_global_variables == SMPI_PRIVATIZE_MMAP && process)
250 region.process_index = process->pid - 1;
253 region.process_index = -1;
255 s_mc_message_stack_region_t message;
256 message.type = MC_MESSAGE_STACK_REGION;
257 message.stack_region = region;
258 if (channel_.send(message))
259 xbt_die("Could not send STACK_REGION to model-checker");