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. */
6 #include "src/mc/remote/Client.hpp"
7 #include "src/internal_config.h"
8 #include <simgrid/modelchecker.h>
9 #include <xbt/system_error.hpp>
13 #include <sys/socket.h>
14 #include <sys/types.h>
16 // We won't need those once the separation MCer/MCed is complete:
17 #include "src/mc/mc_smx.hpp"
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_client, mc, "MC client logic");
24 std::unique_ptr<Client> Client::instance_;
26 Client* Client::initialize()
28 // We are not in MC mode:
29 // TODO, handle this more gracefully.
30 if (not std::getenv(MC_ENV_SOCKET_FD))
33 // Do not break if we are called multiple times:
35 return instance_.get();
37 _sg_do_model_check = 1;
39 // Fetch socket from MC_ENV_SOCKET_FD:
40 char* fd_env = std::getenv(MC_ENV_SOCKET_FD);
42 xbt_die("No MC socket passed in the environment");
44 xbt_str_parse_int(fd_env, bprintf("Variable %s should contain a number but contains '%%s'", MC_ENV_SOCKET_FD));
45 XBT_DEBUG("Model-checked application found socket FD %i", fd);
47 // Check the socket type/validity:
49 socklen_t socklen = sizeof(type);
50 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &socklen) != 0)
51 xbt_die("Could not check socket type");
52 if (type != SOCK_SEQPACKET)
53 xbt_die("Unexpected socket type %i", type);
54 XBT_DEBUG("Model-checked application found expected socket type");
56 instance_.reset(new simgrid::mc::Client(fd));
58 // Wait for the model-checker:
59 if (raise(SIGSTOP) != 0)
60 throw simgrid::xbt::errno_error("Could not wait for the model-checker");
62 instance_->handle_messages();
63 return instance_.get();
66 void Client::handle_deadlock_check(s_mc_message_t*)
68 bool deadlock = false;
69 if (not simix_global->process_list.empty()) {
71 for (auto const& kv : simix_global->process_list)
72 if (simgrid::mc::actor_is_enabled(kv.second)) {
79 s_mc_message_int_t answer{MC_MESSAGE_DEADLOCK_CHECK_REPLY, deadlock};
80 xbt_assert(channel_.send(answer) == 0, "Could not send response");
82 void Client::handle_continue(s_mc_message_t*)
86 void Client::handle_simcall(s_mc_message_simcall_handle_t* message)
88 smx_actor_t process = SIMIX_process_from_PID(message->pid);
90 xbt_die("Invalid pid %lu", (unsigned long)message->pid);
91 SIMIX_simcall_handle(&process->simcall, message->value);
92 if (channel_.send(MC_MESSAGE_WAITING))
93 xbt_die("Could not send MESSAGE_WAITING to model-checker");
96 void Client::handle_actor_enabled(s_mc_message_actor_enabled_t* msg)
98 bool res = simgrid::mc::actor_is_enabled(SIMIX_process_from_PID(msg->aid));
99 s_mc_message_int_t answer{MC_MESSAGE_ACTOR_ENABLED_REPLY, res};
100 channel_.send(answer);
103 void Client::handle_messages()
106 XBT_DEBUG("Waiting messages from model-checker");
108 char message_buffer[MC_MESSAGE_LENGTH];
109 ssize_t received_size = channel_.receive(&message_buffer, sizeof(message_buffer));
111 if (received_size < 0)
112 xbt_die("Could not receive commands from the model-checker");
114 s_mc_message_t* message = (s_mc_message_t*)message_buffer;
115 switch (message->type) {
117 case MC_MESSAGE_DEADLOCK_CHECK:
118 xbt_assert(received_size == sizeof(s_mc_message_t), "Unexpected size for DEADLOCK_CHECK (%zd != %zu)",
119 received_size, sizeof(s_mc_message_t));
120 handle_deadlock_check(message);
123 case MC_MESSAGE_CONTINUE:
124 xbt_assert(received_size == sizeof(s_mc_message_t), "Unexpected size for MESSAGE_CONTINUE (%zd != %zu)",
125 received_size, sizeof(s_mc_message_t));
126 handle_continue(message);
129 case MC_MESSAGE_SIMCALL_HANDLE:
130 xbt_assert(received_size == sizeof(s_mc_message_simcall_handle_t),
131 "Unexpected size for SIMCALL_HANDLE (%zd != %zu)", received_size,
132 sizeof(s_mc_message_simcall_handle_t));
133 handle_simcall((s_mc_message_simcall_handle_t*)message_buffer);
136 case MC_MESSAGE_ACTOR_ENABLED:
137 xbt_assert(received_size == sizeof(s_mc_message_actor_enabled_t),
138 "Unexpected size for ACTOR_ENABLED (%zd != %zu)", received_size,
139 sizeof(s_mc_message_actor_enabled_t));
140 handle_actor_enabled((s_mc_message_actor_enabled_t*)message_buffer);
144 xbt_die("Received unexpected message %s (%i)", MC_message_type_name(message->type), message->type);
150 void Client::main_loop()
153 simgrid::mc::wait_for_requests();
154 xbt_assert(channel_.send(MC_MESSAGE_WAITING) == 0, "Could not send WAITING message to model-checker");
155 this->handle_messages();
159 void Client::report_assertion_failure()
161 if (channel_.send(MC_MESSAGE_ASSERTION_FAILED))
162 xbt_die("Could not send assertion to model-checker");
163 this->handle_messages();
166 void Client::ignore_memory(void* addr, std::size_t size)
168 s_mc_message_ignore_memory_t message;
169 message.type = MC_MESSAGE_IGNORE_MEMORY;
170 message.addr = (std::uintptr_t)addr;
172 if (channel_.send(message))
173 xbt_die("Could not send IGNORE_MEMORY mesage to model-checker");
176 void Client::ignore_heap(void* address, std::size_t size)
178 xbt_mheap_t heap = mmalloc_get_current_heap();
180 s_mc_message_ignore_heap_t message;
181 message.type = MC_MESSAGE_IGNORE_HEAP;
182 message.address = address;
184 message.block = ((char*)address - (char*)heap->heapbase) / BLOCKSIZE + 1;
185 if (heap->heapinfo[message.block].type == 0) {
186 message.fragment = -1;
187 heap->heapinfo[message.block].busy_block.ignore++;
189 message.fragment = ((uintptr_t)(ADDR2UINT(address) % (BLOCKSIZE))) >> heap->heapinfo[message.block].type;
190 heap->heapinfo[message.block].busy_frag.ignore[message.fragment]++;
193 if (channel_.send(message))
194 xbt_die("Could not send ignored region to MCer");
197 void Client::unignore_heap(void* address, std::size_t size)
199 s_mc_message_ignore_memory_t message;
200 message.type = MC_MESSAGE_UNIGNORE_HEAP;
201 message.addr = (std::uintptr_t)address;
203 if (channel_.send(message))
204 xbt_die("Could not send IGNORE_HEAP message to model-checker");
207 void Client::declare_symbol(const char* name, int* value)
209 s_mc_message_register_symbol_t message;
210 message.type = MC_MESSAGE_REGISTER_SYMBOL;
211 if (strlen(name) + 1 > sizeof(message.name))
212 xbt_die("Symbol is too long");
213 strncpy(message.name, name, sizeof(message.name));
214 message.callback = nullptr;
215 message.data = value;
216 if (channel_.send(message))
217 xbt_die("Could send REGISTER_SYMBOL message to model-checker");
220 void Client::declare_stack(void* stack, size_t size, ucontext_t* context)
222 xbt_mheap_t heap = mmalloc_get_current_heap();
224 s_stack_region_t region;
225 memset(®ion, 0, sizeof(region));
226 region.address = stack;
227 region.context = context;
229 region.block = ((char*)stack - (char*)heap->heapbase) / BLOCKSIZE + 1;
231 s_mc_message_stack_region_t message;
232 message.type = MC_MESSAGE_STACK_REGION;
233 message.stack_region = region;
234 if (channel_.send(message))
235 xbt_die("Could not send STACK_REGION to model-checker");