1 /* Copyright (c) 2015-2019. 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. */
10 #include <sys/ptrace.h>
11 #include <sys/socket.h>
12 #include <sys/types.h>
14 #include <xbt/coverage.h>
16 #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.hpp"
24 #include "src/mc/remote/Client.hpp"
25 #include "src/mc/remote/mc_protocol.h"
27 #include "src/smpi/include/private.hpp"
29 // We won't need those once the separation MCer/MCed is complete:
30 #include "src/mc/mc_smx.hpp"
32 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_client, mc, "MC client logic");
37 std::unique_ptr<Client> Client::instance_;
39 Client* Client::initialize()
41 // We are not in MC mode:
42 // TODO, handle this more gracefully.
43 if (not std::getenv(MC_ENV_SOCKET_FD))
46 // Do not break if we are called multiple times:
48 return instance_.get();
50 _sg_do_model_check = 1;
52 // Fetch socket from MC_ENV_SOCKET_FD:
53 char* fd_env = std::getenv(MC_ENV_SOCKET_FD);
55 xbt_die("No MC socket passed in the environment");
57 xbt_str_parse_int(fd_env, bprintf("Variable %s should contain a number but contains '%%s'", MC_ENV_SOCKET_FD));
58 XBT_DEBUG("Model-checked application found socket FD %i", fd);
60 // Check the socket type/validity:
62 socklen_t socklen = sizeof(type);
63 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &socklen) != 0)
64 xbt_die("Could not check socket type");
65 if (type != SOCK_SEQPACKET)
66 xbt_die("Unexpected socket type %i", type);
67 XBT_DEBUG("Model-checked application found expected socket type");
69 instance_ = std::unique_ptr<Client>(new simgrid::mc::Client(fd));
71 // Wait for the model-checker:
74 ptrace(PTRACE_TRACEME, 0, nullptr, nullptr);
76 ptrace(PT_TRACE_ME, 0, nullptr, 0);
78 #error "no ptrace equivalent coded for this platform"
80 if (errno != 0 || raise(SIGSTOP) != 0)
81 xbt_die("Could not wait for the model-checker (errno = %d: %s)", errno, strerror(errno));
83 instance_->handleMessages();
84 return instance_.get();
87 void Client::handleDeadlockCheck(s_mc_message_t*)
89 bool deadlock = false;
90 if (not simix_global->process_list.empty()) {
92 for (auto const& kv : simix_global->process_list)
93 if (simgrid::mc::actor_is_enabled(kv.second)) {
100 s_mc_message_int_t answer{MC_MESSAGE_DEADLOCK_CHECK_REPLY, deadlock};
101 xbt_assert(channel_.send(answer) == 0, "Could not send response");
103 void Client::handleContinue(s_mc_message_t*)
107 void Client::handleSimcall(s_mc_message_simcall_handle_t* message)
109 smx_actor_t process = SIMIX_process_from_PID(message->pid);
111 xbt_die("Invalid pid %lu", (unsigned long)message->pid);
112 SIMIX_simcall_handle(&process->simcall, message->value);
113 if (channel_.send(MC_MESSAGE_WAITING))
114 xbt_die("Could not send MESSAGE_WAITING to model-checker");
116 void Client::handleRestore(s_mc_message_restore_t* message)
119 smpi_really_switch_data_segment(simgrid::s4u::Actor::by_pid(message->index));
122 void Client::handleActorEnabled(s_mc_message_actor_enabled_t* msg)
124 bool res = simgrid::mc::actor_is_enabled(SIMIX_process_from_PID(msg->aid));
125 s_mc_message_int_t answer{MC_MESSAGE_ACTOR_ENABLED_REPLY, res};
126 channel_.send(answer);
129 void Client::handleMessages()
132 XBT_DEBUG("Waiting messages from model-checker");
134 char message_buffer[MC_MESSAGE_LENGTH];
135 ssize_t received_size = channel_.receive(&message_buffer, sizeof(message_buffer));
137 if (received_size < 0)
138 xbt_die("Could not receive commands from the model-checker");
140 s_mc_message_t* message = (s_mc_message_t*)message_buffer;
141 switch (message->type) {
143 case MC_MESSAGE_DEADLOCK_CHECK:
144 xbt_assert(received_size == sizeof(s_mc_message_t), "Unexpected size for DEADLOCK_CHECK (%zd != %zu)",
145 received_size, sizeof(s_mc_message_t));
146 handleDeadlockCheck(message);
149 case MC_MESSAGE_CONTINUE:
150 xbt_assert(received_size == sizeof(s_mc_message_t), "Unexpected size for MESSAGE_CONTINUE (%zd != %zu)",
151 received_size, sizeof(s_mc_message_t));
152 handleContinue(message);
155 case MC_MESSAGE_SIMCALL_HANDLE:
156 xbt_assert(received_size == sizeof(s_mc_message_simcall_handle_t),
157 "Unexpected size for SIMCALL_HANDLE (%zd != %zu)", received_size,
158 sizeof(s_mc_message_simcall_handle_t));
159 handleSimcall((s_mc_message_simcall_handle_t*)message_buffer);
162 case MC_MESSAGE_RESTORE:
163 xbt_assert(received_size == sizeof(s_mc_message_t), "Unexpected size for MESSAGE_RESTORE (%zd != %zu)",
164 received_size, sizeof(s_mc_message_t));
165 handleRestore((s_mc_message_restore_t*)message_buffer);
168 case MC_MESSAGE_ACTOR_ENABLED:
169 xbt_assert(received_size == sizeof(s_mc_message_actor_enabled_t),
170 "Unexpected size for ACTOR_ENABLED (%zd != %zu)", received_size,
171 sizeof(s_mc_message_actor_enabled_t));
172 handleActorEnabled((s_mc_message_actor_enabled_t*)message_buffer);
176 xbt_die("Received unexpected message %s (%i)", MC_message_type_name(message->type), message->type);
182 void Client::mainLoop()
185 simgrid::mc::wait_for_requests();
186 xbt_assert(channel_.send(MC_MESSAGE_WAITING) == 0, "Could not send WAITING message to model-checker");
187 this->handleMessages();
188 coverage_checkpoint();
192 void Client::reportAssertionFailure()
194 if (channel_.send(MC_MESSAGE_ASSERTION_FAILED))
195 xbt_die("Could not send assertion to model-checker");
196 this->handleMessages();
199 void Client::ignoreMemory(void* addr, std::size_t size)
201 s_mc_message_ignore_memory_t message;
202 message.type = MC_MESSAGE_IGNORE_MEMORY;
203 message.addr = (std::uintptr_t)addr;
205 if (channel_.send(message))
206 xbt_die("Could not send IGNORE_MEMORY mesage to model-checker");
209 void Client::ignoreHeap(void* address, std::size_t size)
211 xbt_mheap_t heap = mmalloc_get_current_heap();
213 s_mc_message_ignore_heap_t message;
214 message.type = MC_MESSAGE_IGNORE_HEAP;
215 message.address = address;
217 message.block = ((char*)address - (char*)heap->heapbase) / BLOCKSIZE + 1;
218 if (heap->heapinfo[message.block].type == 0) {
219 message.fragment = -1;
220 heap->heapinfo[message.block].busy_block.ignore++;
222 message.fragment = ((uintptr_t)(ADDR2UINT(address) % (BLOCKSIZE))) >> heap->heapinfo[message.block].type;
223 heap->heapinfo[message.block].busy_frag.ignore[message.fragment]++;
226 if (channel_.send(message))
227 xbt_die("Could not send ignored region to MCer");
230 void Client::unignoreHeap(void* address, std::size_t size)
232 s_mc_message_ignore_memory_t message;
233 message.type = MC_MESSAGE_UNIGNORE_HEAP;
234 message.addr = (std::uintptr_t)address;
236 if (channel_.send(message))
237 xbt_die("Could not send IGNORE_HEAP message to model-checker");
240 void Client::declareSymbol(const char* name, int* value)
242 s_mc_message_register_symbol_t message;
243 message.type = MC_MESSAGE_REGISTER_SYMBOL;
244 if (strlen(name) + 1 > sizeof(message.name))
245 xbt_die("Symbol is too long");
246 strncpy(message.name, name, sizeof(message.name));
247 message.callback = nullptr;
248 message.data = value;
249 if (channel_.send(message))
250 xbt_die("Could send REGISTER_SYMBOL message to model-checker");
253 void Client::declareStack(void* stack, size_t size, smx_actor_t process, ucontext_t* context)
255 xbt_mheap_t heap = mmalloc_get_current_heap();
257 s_stack_region_t region;
258 memset(®ion, 0, sizeof(region));
259 region.address = stack;
260 region.context = context;
262 region.block = ((char*)stack - (char*)heap->heapbase) / BLOCKSIZE + 1;
264 if (smpi_privatize_global_variables == SmpiPrivStrategies::MMAP && process)
265 region.process_index = process->pid_ - 1;
268 region.process_index = -1;
270 s_mc_message_stack_region_t message;
271 message.type = MC_MESSAGE_STACK_REGION;
272 message.stack_region = region;
273 if (channel_.send(message))
274 xbt_die("Could not send STACK_REGION to model-checker");