1 /* Copyright (c) 2015-2018. 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>
15 #include <xbt/sysdep.h>
17 #include <simgrid/modelchecker.h>
19 #include "src/internal_config.h"
21 #include "src/mc/mc_request.hpp"
22 #include "src/mc/remote/Client.hpp"
23 #include "src/mc/remote/mc_protocol.h"
25 #include "src/smpi/include/private.hpp"
27 // We won't need those once the separation MCer/MCed is complete:
28 #include "src/mc/mc_smx.hpp"
30 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_client, mc, "MC client logic");
35 std::unique_ptr<Client> Client::instance_;
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:
46 return instance_.get();
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_SEQPACKET)
64 xbt_die("Unexpected socket type %i", type);
65 XBT_DEBUG("Model-checked application found expected socket type");
67 instance_ = 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 instance_->handleMessages();
82 return instance_.get();
85 void Client::handleDeadlockCheck(s_mc_message_t* msg)
87 bool deadlock = false;
88 if (not simix_global->process_list.empty()) {
90 for (auto const& kv : simix_global->process_list)
91 if (simgrid::mc::actor_is_enabled(kv.second)) {
98 s_mc_message_int_t answer{MC_MESSAGE_DEADLOCK_CHECK_REPLY, deadlock};
99 xbt_assert(channel_.send(answer) == 0, "Could not send response");
101 void Client::handleContinue(s_mc_message_t* msg)
105 void Client::handleSimcall(s_mc_message_simcall_handle_t* message)
107 smx_actor_t process = SIMIX_process_from_PID(message->pid);
109 xbt_die("Invalid pid %lu", (unsigned long)message->pid);
110 SIMIX_simcall_handle(&process->simcall, message->value);
111 if (channel_.send(MC_MESSAGE_WAITING))
112 xbt_die("Could not send MESSAGE_WAITING to model-checker");
114 void Client::handleRestore(s_mc_message_restore_t* message)
117 smpi_really_switch_data_segment(simgrid::s4u::Actor::by_pid(message->index));
120 void Client::handleActorEnabled(s_mc_message_actor_enabled_t* msg)
122 bool res = simgrid::mc::actor_is_enabled(SIMIX_process_from_PID(msg->aid));
123 s_mc_message_int_t answer{MC_MESSAGE_ACTOR_ENABLED_REPLY, res};
124 channel_.send(answer);
127 void Client::handleMessages()
130 XBT_DEBUG("Waiting messages from model-checker");
132 char message_buffer[MC_MESSAGE_LENGTH];
133 ssize_t received_size = channel_.receive(&message_buffer, sizeof(message_buffer));
135 if (received_size < 0)
136 xbt_die("Could not receive commands from the model-checker");
138 s_mc_message_t* message = (s_mc_message_t*)message_buffer;
139 switch (message->type) {
141 case MC_MESSAGE_DEADLOCK_CHECK:
142 xbt_assert(received_size == sizeof(s_mc_message_t), "Unexpected size for DEADLOCK_CHECK (%zd != %zu)",
143 received_size, sizeof(s_mc_message_t));
144 handleDeadlockCheck(message);
147 case MC_MESSAGE_CONTINUE:
148 xbt_assert(received_size == sizeof(s_mc_message_t), "Unexpected size for MESSAGE_CONTINUE (%zd != %zu)",
149 received_size, sizeof(s_mc_message_t));
150 handleContinue(message);
153 case MC_MESSAGE_SIMCALL_HANDLE:
154 xbt_assert(received_size == sizeof(s_mc_message_simcall_handle_t),
155 "Unexpected size for SIMCALL_HANDLE (%zd != %zu)", received_size,
156 sizeof(s_mc_message_simcall_handle_t));
157 handleSimcall((s_mc_message_simcall_handle_t*)message_buffer);
160 case MC_MESSAGE_RESTORE:
161 xbt_assert(received_size == sizeof(s_mc_message_t), "Unexpected size for MESSAGE_RESTORE (%zd != %zu)",
162 received_size, sizeof(s_mc_message_t));
163 handleRestore((s_mc_message_restore_t*)message_buffer);
166 case MC_MESSAGE_ACTOR_ENABLED:
167 xbt_assert(received_size == sizeof(s_mc_message_actor_enabled_t),
168 "Unexpected size for ACTOR_ENABLED (%zd != %zu)", received_size,
169 sizeof(s_mc_message_actor_enabled_t));
170 handleActorEnabled((s_mc_message_actor_enabled_t*)message_buffer);
174 xbt_die("Received unexpected message %s (%i)", MC_message_type_name(message->type), message->type);
180 void Client::mainLoop()
183 simgrid::mc::wait_for_requests();
184 xbt_assert(channel_.send(MC_MESSAGE_WAITING) == 0, "Could not send WAITING message to model-checker");
185 this->handleMessages();
189 void Client::reportAssertionFailure(const char* description)
191 if (channel_.send(MC_MESSAGE_ASSERTION_FAILED))
192 xbt_die("Could not send assertion to model-checker");
193 this->handleMessages();
196 void Client::ignoreMemory(void* addr, std::size_t size)
198 s_mc_message_ignore_memory_t message;
199 message.type = MC_MESSAGE_IGNORE_MEMORY;
200 message.addr = (std::uintptr_t)addr;
202 if (channel_.send(message))
203 xbt_die("Could not send IGNORE_MEMORY mesage to model-checker");
206 void Client::ignoreHeap(void* address, std::size_t size)
208 xbt_mheap_t heap = mmalloc_get_current_heap();
210 s_mc_message_ignore_heap_t message;
211 message.type = MC_MESSAGE_IGNORE_HEAP;
212 message.address = address;
214 message.block = ((char*)address - (char*)heap->heapbase) / BLOCKSIZE + 1;
215 if (heap->heapinfo[message.block].type == 0) {
216 message.fragment = -1;
217 heap->heapinfo[message.block].busy_block.ignore++;
219 message.fragment = ((uintptr_t)(ADDR2UINT(address) % (BLOCKSIZE))) >> heap->heapinfo[message.block].type;
220 heap->heapinfo[message.block].busy_frag.ignore[message.fragment]++;
223 if (channel_.send(message))
224 xbt_die("Could not send ignored region to MCer");
227 void Client::unignoreHeap(void* address, std::size_t size)
229 s_mc_message_ignore_memory_t message;
230 message.type = MC_MESSAGE_UNIGNORE_HEAP;
231 message.addr = (std::uintptr_t)address;
233 if (channel_.send(message))
234 xbt_die("Could not send IGNORE_HEAP message to model-checker");
237 void Client::declareSymbol(const char* name, int* value)
239 s_mc_message_register_symbol_t message;
240 message.type = MC_MESSAGE_REGISTER_SYMBOL;
241 if (strlen(name) + 1 > sizeof(message.name))
242 xbt_die("Symbol is too long");
243 strncpy(message.name, name, sizeof(message.name));
244 message.callback = nullptr;
245 message.data = value;
246 if (channel_.send(message))
247 xbt_die("Could send REGISTER_SYMBOL message to model-checker");
250 void Client::declareStack(void* stack, size_t size, smx_actor_t process, ucontext_t* context)
252 xbt_mheap_t heap = mmalloc_get_current_heap();
254 s_stack_region_t region;
255 memset(®ion, 0, sizeof(region));
256 region.address = stack;
257 region.context = context;
259 region.block = ((char*)stack - (char*)heap->heapbase) / BLOCKSIZE + 1;
261 if (smpi_privatize_global_variables == SmpiPrivStrategies::MMAP && process)
262 region.process_index = process->pid_ - 1;
265 region.process_index = -1;
267 s_mc_message_stack_region_t message;
268 message.type = MC_MESSAGE_STACK_REGION;
269 message.stack_region = region;
270 if (channel_.send(message))
271 xbt_die("Could not send STACK_REGION to model-checker");