Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2b25c63917724fba6c5c659dc52a7b81e8f96ee6
[simgrid.git] / src / mc / remote / Client.cpp
1 /* Copyright (c) 2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include <cerrno>
8 #include <cstdlib>
9
10 #include <sys/ptrace.h>
11 #include <sys/socket.h>
12 #include <sys/types.h>
13
14 #include <xbt/log.h>
15 #include <xbt/mmalloc.h>
16 #include <xbt/swag.h>
17 #include <xbt/sysdep.h>
18
19 #include <simgrid/modelchecker.h>
20
21 #include "src/internal_config.h"
22
23 #include "src/mc/mc_request.h"
24 #include "src/mc/remote/Client.hpp"
25 #include "src/mc/remote/mc_protocol.h"
26
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"
30
31 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_client, mc, "MC client logic");
32
33 namespace simgrid {
34 namespace mc {
35
36 std::unique_ptr<Client> Client::client_;
37
38 Client* Client::initialize()
39 {
40   // We are not in MC mode:
41   // TODO, handle this more gracefully.
42   if (!getenv(MC_ENV_SOCKET_FD))
43     return nullptr;
44
45   // Do not break if we are called multiple times:
46   if (client_)
47     return client_.get();
48
49   _sg_do_model_check = 1;
50
51   // Fetch socket from MC_ENV_SOCKET_FD:
52   char* fd_env = std::getenv(MC_ENV_SOCKET_FD);
53   if (!fd_env)
54     xbt_die("No MC socket passed in the environment");
55   int fd =
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);
58
59   // Check the socket type/validity:
60   int type;
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");
67
68   client_ = std::unique_ptr<Client>(new simgrid::mc::Client(fd));
69
70   // Wait for the model-checker:
71   errno = 0;
72 #if defined __linux__
73   ptrace(PTRACE_TRACEME, 0, nullptr, nullptr);
74 #elif defined BSD
75   ptrace(PT_TRACE_ME, 0, nullptr, 0);
76 #else
77 #error "no ptrace equivalent coded for this platform"
78 #endif
79   if (errno != 0 || raise(SIGSTOP) != 0)
80     xbt_die("Could not wait for the model-checker");
81
82   client_->handleMessages();
83   return client_.get();
84 }
85
86 void Client::handleMessages()
87 {
88   while (1) {
89     XBT_DEBUG("Waiting messages from model-checker");
90
91     char message_buffer[MC_MESSAGE_LENGTH];
92     ssize_t s;
93
94     if ((s = channel_.receive(&message_buffer, sizeof(message_buffer))) < 0)
95       xbt_die("Could not receive commands from the model-checker");
96
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) {
102
103       case MC_MESSAGE_DEADLOCK_CHECK: {
104         // Check deadlock:
105         bool deadlock = false;
106         smx_actor_t actor;
107         if (xbt_swag_size(simix_global->process_list)) {
108           deadlock = true;
109           xbt_swag_foreach(actor, simix_global->process_list) if (simgrid::mc::actor_is_enabled(actor))
110           {
111             deadlock = false;
112             break;
113           }
114         }
115
116         // Send result:
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");
122       } break;
123
124       case MC_MESSAGE_CONTINUE:
125         return;
126
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);
133         if (!process)
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");
138       } break;
139
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));
145 #if HAVE_SMPI
146         smpi_really_switch_data_segment(message.index);
147 #endif
148       } break;
149
150       default:
151         xbt_die("Received unexpected message %s (%i)", MC_message_type_name(message.type), message.type);
152     }
153   }
154 }
155
156 void Client::mainLoop(void)
157 {
158   while (1) {
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();
163   }
164 }
165
166 void Client::reportAssertionFailure(const char* description)
167 {
168   if (channel_.send(MC_MESSAGE_ASSERTION_FAILED))
169     xbt_die("Could not send assertion to model-checker");
170   this->handleMessages();
171 }
172
173 void Client::ignoreMemory(void* addr, std::size_t size)
174 {
175   s_mc_ignore_memory_message_t message;
176   message.type = MC_MESSAGE_IGNORE_MEMORY;
177   message.addr = (std::uintptr_t)addr;
178   message.size = size;
179   if (channel_.send(message))
180     xbt_die("Could not send IGNORE_MEMORY mesage to model-checker");
181 }
182
183 void Client::ignoreHeap(void* address, std::size_t size)
184 {
185   xbt_mheap_t heap = mmalloc_get_current_heap();
186
187   s_mc_ignore_heap_message_t message;
188   message.type    = MC_MESSAGE_IGNORE_HEAP;
189   message.address = address;
190   message.size    = size;
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++;
195   } else {
196     message.fragment = ((uintptr_t)(ADDR2UINT(address) % (BLOCKSIZE))) >> heap->heapinfo[message.block].type;
197     heap->heapinfo[message.block].busy_frag.ignore[message.fragment]++;
198   }
199
200   if (channel_.send(message))
201     xbt_die("Could not send ignored region to MCer");
202 }
203
204 void Client::unignoreHeap(void* address, std::size_t size)
205 {
206   s_mc_ignore_memory_message_t message;
207   message.type = MC_MESSAGE_UNIGNORE_HEAP;
208   message.addr = (std::uintptr_t)address;
209   message.size = size;
210   if (channel_.send(message))
211     xbt_die("Could not send IGNORE_HEAP mesasge to model-checker");
212 }
213
214 void Client::declareSymbol(const char* name, int* value)
215 {
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");
225 }
226
227 void Client::declareStack(void* stack, size_t size, smx_actor_t process, ucontext_t* context)
228 {
229   xbt_mheap_t heap = mmalloc_get_current_heap();
230
231   s_stack_region_t region;
232   memset(&region, 0, sizeof(region));
233   region.address = stack;
234   region.context = context;
235   region.size    = size;
236   region.block   = ((char*)stack - (char*)heap->heapbase) / BLOCKSIZE + 1;
237 #if HAVE_SMPI
238   if (smpi_privatize_global_variables && process)
239     region.process_index = smpi_process_index_of_smx_process(process);
240   else
241 #endif
242     region.process_index = -1;
243
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");
249 }
250 }
251 }