Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Use std::vector for State::incomplete_comm_pattern (dexbtification)
[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 #include <xbt/swag.h>
18
19 #include "src/internal_config.h"
20
21 #include "src/mc/mc_protocol.h"
22 #include "src/mc/Client.hpp"
23 #include "src/mc/mc_request.h"
24
25 // We won't need those once the separation MCer/MCed is complete:
26 #include "src/mc/mc_ignore.h"
27 #include "src/mc/mc_smx.h"
28
29 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_client, mc, "MC client logic");
30
31 namespace simgrid {
32 namespace mc {
33
34 std::unique_ptr<Client> Client::client_;
35
36 Client* Client::initialize()
37 {
38   // We are not in MC mode:
39   // TODO, handle this more gracefully.
40   if (!getenv(MC_ENV_SOCKET_FD))
41     return nullptr;
42
43   // Do not break if we are called multiple times:
44   if (client_)
45     return client_.get();
46
47   // Check and set the mode:
48   if (mc_mode != MC_MODE_NONE)
49     abort();
50   mc_mode = MC_MODE_CLIENT;
51
52   // Fetch socket from MC_ENV_SOCKET_FD:
53   char* fd_env = std::getenv(MC_ENV_SOCKET_FD);
54   if (!fd_env)
55     xbt_die("No MC socket passed in the environment");
56   int fd = 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   if (ptrace(PTRACE_TRACEME, 0, nullptr, NULL) == -1 || raise(SIGSTOP) != 0)
72     xbt_die("Could not wait for the model-checker");
73
74   client_->handleMessages();
75   return client_.get();
76 }
77
78 void Client::handleMessages()
79 {
80   while (1) {
81     XBT_DEBUG("Waiting messages from model-checker");
82
83     char message_buffer[MC_MESSAGE_LENGTH];
84     ssize_t s;
85
86     if ((s = channel_.receive(&message_buffer, sizeof(message_buffer))) < 0)
87       xbt_die("Could not receive commands from the model-checker");
88
89     s_mc_message_t message;
90     if ((size_t) s < sizeof(message))
91       xbt_die("Received message is too small");
92     memcpy(&message, message_buffer, sizeof(message));
93     switch (message.type) {
94
95     case MC_MESSAGE_DEADLOCK_CHECK:
96       {
97         // Check deadlock:
98         bool deadlock = false;
99         smx_process_t process;
100         if (xbt_swag_size(simix_global->process_list)) {
101           deadlock = true;
102           xbt_swag_foreach(process, simix_global->process_list)
103             if (simgrid::mc::process_is_enabled(process)) {
104               deadlock = false;
105               break;
106             }
107         }
108
109         // Send result:
110         s_mc_int_message_t answer;
111         answer.type = MC_MESSAGE_DEADLOCK_CHECK_REPLY;
112         answer.value = deadlock;
113         if (channel_.send(answer))
114           xbt_die("Could not send response");
115       }
116       break;
117
118     case MC_MESSAGE_CONTINUE:
119       return;
120
121     case MC_MESSAGE_SIMCALL_HANDLE:
122       {
123         s_mc_simcall_handle_message_t message;
124         if (s != sizeof(message))
125           xbt_die("Unexpected size for SIMCALL_HANDLE");
126         memcpy(&message, message_buffer, sizeof(message));
127         smx_process_t process = SIMIX_process_from_PID(message.pid);
128         if (!process)
129           xbt_die("Invalid pid %lu", (unsigned long) message.pid);
130         SIMIX_simcall_handle(&process->simcall, message.value);
131         if (channel_.send(MC_MESSAGE_WAITING))
132           xbt_die("Could not send MESSAGE_WAITING to model-checker");
133       }
134       break;
135
136     case MC_MESSAGE_RESTORE:
137       {
138         s_mc_restore_message_t message;
139         if (s != sizeof(message))
140           xbt_die("Unexpected size for SIMCALL_HANDLE");
141         memcpy(&message, message_buffer, sizeof(message));
142         smpi_really_switch_data_segment(message.index);
143       }
144       break;
145
146     default:
147       xbt_die("%s received unexpected message %s (%i)",
148         MC_mode_name(mc_mode),
149         MC_message_type_name(message.type),
150         message.type
151       );
152     }
153   }
154 }
155
156 void Client::mainLoop(void)
157 {
158   while (1) {
159     if (channel_.send(MC_MESSAGE_WAITING))
160       xbt_die("Could not send WAITING mesage to model-checker");
161     this->handleMessages();
162     simgrid::mc::wait_for_requests();
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 =
192    ((char *) address -
193     (char *) heap->heapbase) / BLOCKSIZE + 1;
194   if (heap->heapinfo[message.block].type == 0) {
195     message.fragment = -1;
196     heap->heapinfo[message.block].busy_block.ignore++;
197   } else {
198     message.fragment =
199         ((uintptr_t) (ADDR2UINT(address) % (BLOCKSIZE))) >>
200         heap->heapinfo[message.block].type;
201     heap->heapinfo[message.block].busy_frag.ignore[message.fragment]++;
202   }
203
204   if (channel_.send(message))
205     xbt_die("Could not send ignored region to MCer");
206 }
207
208 void Client::unignoreHeap(void* address, std::size_t size)
209 {
210   s_mc_ignore_memory_message_t message;
211   message.type = MC_MESSAGE_UNIGNORE_HEAP;
212   message.addr = (std::uintptr_t) address;
213   message.size = size;
214   if (channel_.send(message))
215     xbt_die("Could not send IGNORE_HEAP mesasge to model-checker");
216 }
217
218 void Client::declareSymbol(const char *name, int* value)
219 {
220   s_mc_register_symbol_message_t message;
221   message.type = MC_MESSAGE_REGISTER_SYMBOL;
222   if (strlen(name) + 1 > sizeof(message.name))
223     xbt_die("Symbol is too long");
224   strncpy(message.name, name, sizeof(message.name));
225   message.callback = nullptr;
226   message.data = value;
227   if (channel_.send(message))
228     xbt_die("Could send REGISTER_SYMBOL message to model-checker");
229 }
230
231 void Client::declareStack(void *stack, size_t size, smx_process_t process, ucontext_t* context)
232 {
233   xbt_mheap_t heap = mmalloc_get_current_heap();
234
235   s_stack_region_t region;
236   memset(&region, 0, sizeof(region));
237   region.address = stack;
238   region.context = context;
239   region.size = size;
240   region.block =
241       ((char *) stack -
242        (char *) heap->heapbase) / BLOCKSIZE + 1;
243 #if HAVE_SMPI
244   if (smpi_privatize_global_variables && process)
245     region.process_index = smpi_process_index_of_smx_process(process);
246   else
247 #endif
248   region.process_index = -1;
249
250   s_mc_stack_region_message_t message;
251   message.type = MC_MESSAGE_STACK_REGION;
252   message.stack_region = region;
253   if (channel_.send(message))
254     xbt_die("Coule not send STACK_REGION to model-checker");
255 }
256
257 }
258 }