Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / src / mc / 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 <cstdlib>
8 #include <cerrno>
9
10 #include <sys/types.h>
11 #include <sys/ptrace.h>
12 #include <sys/socket.h>
13
14 #include <xbt/log.h>
15 #include <xbt/sysdep.h>
16 #include <xbt/mmalloc.h>
17
18 #include "src/internal_config.h"
19
20 #include "src/mc/mc_protocol.h"
21 #include "src/mc/Client.hpp"
22
23 // We won't need those once the separation MCer/MCed is complete:
24 #include "src/mc/mc_ignore.h"
25 #include "src/mc/mc_private.h" // MC_deadlock_check()
26 #include "src/mc/mc_smx.h"
27
28 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_client, mc, "MC client logic");
29
30 namespace simgrid {
31 namespace mc {
32
33 std::unique_ptr<Client> Client::client_;
34
35 Client* Client::initialize()
36 {
37   // We are not in MC mode:
38   // TODO, handle this more gracefully.
39   if (!getenv(MC_ENV_SOCKET_FD))
40     return nullptr;
41
42   // Do not break if we are called multiple times:
43   if (client_)
44     return client_.get();
45
46   // Check and set the mode:
47   if (mc_mode != MC_MODE_NONE)
48     abort();
49   mc_mode = MC_MODE_CLIENT;
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 = 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);
57
58   // Check the socket type/validity:
59   int type;
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_DGRAM)
64     xbt_die("Unexpected socket type %i", type);
65   XBT_DEBUG("Model-checked application found expected socket type");
66
67   client_ = std::unique_ptr<Client>(new simgrid::mc::Client(fd));
68
69   // Wait for the model-checker:
70   if (ptrace(PTRACE_TRACEME, 0, nullptr, NULL) == -1 || raise(SIGSTOP) != 0)
71     xbt_die("Could not wait for the model-checker");
72
73   client_->handleMessages();
74   return client_.get();
75 }
76
77 void Client::handleMessages()
78 {
79   while (1) {
80     XBT_DEBUG("Waiting messages from model-checker");
81
82     char message_buffer[MC_MESSAGE_LENGTH];
83     ssize_t s;
84
85     if ((s = channel_.receive(&message_buffer, sizeof(message_buffer))) < 0)
86       xbt_die("Could not receive commands from the model-checker");
87
88     s_mc_message_t message;
89     if ((size_t) s < sizeof(message))
90       xbt_die("Received message is too small");
91     memcpy(&message, message_buffer, sizeof(message));
92     switch (message.type) {
93
94     case MC_MESSAGE_DEADLOCK_CHECK:
95       {
96         int result = MC_deadlock_check();
97         s_mc_int_message_t answer;
98         answer.type = MC_MESSAGE_DEADLOCK_CHECK_REPLY;
99         answer.value = result;
100         if (channel_.send(answer))
101           xbt_die("Could not send response");
102       }
103       break;
104
105     case MC_MESSAGE_CONTINUE:
106       return;
107
108     case MC_MESSAGE_SIMCALL_HANDLE:
109       {
110         s_mc_simcall_handle_message_t message;
111         if (s != sizeof(message))
112           xbt_die("Unexpected size for SIMCALL_HANDLE");
113         memcpy(&message, message_buffer, sizeof(message));
114         smx_process_t process = SIMIX_process_from_PID(message.pid);
115         if (!process)
116           xbt_die("Invalid pid %lu", (unsigned long) message.pid);
117         SIMIX_simcall_handle(&process->simcall, message.value);
118         if (channel_.send(MC_MESSAGE_WAITING))
119           xbt_die("Could not send MESSAGE_WAITING to model-checker");
120       }
121       break;
122
123     case MC_MESSAGE_RESTORE:
124       {
125         s_mc_restore_message_t message;
126         if (s != sizeof(message))
127           xbt_die("Unexpected size for SIMCALL_HANDLE");
128         memcpy(&message, message_buffer, sizeof(message));
129         smpi_really_switch_data_segment(message.index);
130       }
131       break;
132
133     default:
134       xbt_die("%s received unexpected message %s (%i)",
135         MC_mode_name(mc_mode),
136         MC_message_type_name(message.type),
137         message.type
138       );
139     }
140   }
141 }
142
143 void Client::mainLoop(void)
144 {
145   while (1) {
146     if (channel_.send(MC_MESSAGE_WAITING))
147       xbt_die("Could not send WAITING mesage to model-checker");
148     this->handleMessages();
149     simgrid::mc::wait_for_requests();
150   }
151 }
152
153 void Client::reportAssertionFailure(const char* description)
154 {
155   if (channel_.send(MC_MESSAGE_ASSERTION_FAILED))
156     xbt_die("Could not send assertion to model-checker");
157   this->handleMessages();
158 }
159
160 void Client::ignoreMemory(void* addr, std::size_t size)
161 {
162   s_mc_ignore_memory_message_t message;
163   message.type = MC_MESSAGE_IGNORE_MEMORY;
164   message.addr = (std::uintptr_t) addr;
165   message.size = size;
166   if (channel_.send(message))
167     xbt_die("Could not send IGNORE_MEMORY mesage to model-checker");
168 }
169
170 void Client::ignoreHeap(void* address, std::size_t size)
171 {
172   xbt_mheap_t heap = mmalloc_get_current_heap();
173
174   s_mc_ignore_heap_message_t message;
175   message.type = MC_MESSAGE_IGNORE_HEAP;
176   message.address = address;
177   message.size = size;
178   message.block =
179    ((char *) address -
180     (char *) heap->heapbase) / BLOCKSIZE + 1;
181   if (heap->heapinfo[message.block].type == 0) {
182     message.fragment = -1;
183     heap->heapinfo[message.block].busy_block.ignore++;
184   } else {
185     message.fragment =
186         ((uintptr_t) (ADDR2UINT(address) % (BLOCKSIZE))) >>
187         heap->heapinfo[message.block].type;
188     heap->heapinfo[message.block].busy_frag.ignore[message.fragment]++;
189   }
190
191   if (channel_.send(message))
192     xbt_die("Could not send ignored region to MCer");
193 }
194
195 void Client::unignoreHeap(void* address, std::size_t size)
196 {
197   s_mc_ignore_memory_message_t message;
198   message.type = MC_MESSAGE_UNIGNORE_HEAP;
199   message.addr = (std::uintptr_t) address;
200   message.size = size;
201   if (channel_.send(message))
202     xbt_die("Could not send IGNORE_HEAP mesasge to model-checker");
203 }
204
205 void Client::declareSymbol(const char *name, int* value)
206 {
207   s_mc_register_symbol_message_t message;
208   message.type = MC_MESSAGE_REGISTER_SYMBOL;
209   if (strlen(name) + 1 > sizeof(message.name))
210     xbt_die("Symbol is too long");
211   strncpy(message.name, name, sizeof(message.name));
212   message.callback = nullptr;
213   message.data = value;
214   if (channel_.send(message))
215     xbt_die("Could send REGISTER_SYMBOL message to model-checker");
216 }
217
218 void Client::declareStack(void *stack, size_t size, smx_process_t process, ucontext_t* context)
219 {
220   xbt_mheap_t heap = mmalloc_get_current_heap();
221
222   s_stack_region_t region;
223   memset(&region, 0, sizeof(region));
224   region.address = stack;
225   region.context = context;
226   region.size = size;
227   region.block =
228       ((char *) stack -
229        (char *) heap->heapbase) / BLOCKSIZE + 1;
230 #if HAVE_SMPI
231   if (smpi_privatize_global_variables && process)
232     region.process_index = smpi_process_index_of_smx_process(process);
233   else
234 #endif
235   region.process_index = -1;
236
237   s_mc_stack_region_message_t message;
238   message.type = MC_MESSAGE_STACK_REGION;
239   message.stack_region = region;
240   if (channel_.send(message))
241     xbt_die("Coule not send STACK_REGION to model-checker");
242 }
243
244 }
245 }