Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Basic infrastructure for a real model-checker process
[simgrid.git] / src / mc / mc_process.c
1 #include <assert.h>
2 #include <stddef.h>
3 #include <stdbool.h>
4 #include <stdint.h>
5 #include <errno.h>
6
7 #include <sys/types.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10 #include <regex.h>
11 #include <sys/mman.h> // PROT_*
12
13 #include <pthread.h>
14
15 #include <libgen.h>
16
17 #include <libunwind.h>
18 #include <libunwind-ptrace.h>
19
20 #include "mc_process.h"
21 #include "mc_object_info.h"
22 #include "mc_address_space.h"
23 #include "mc_unw.h"
24
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_process, mc,
26                                 "MC process information");
27
28 static void MC_process_init_memory_map_info(mc_process_t process);
29 static void MC_process_open_memory_file(mc_process_t process);
30
31 static mc_process_t MC_process_get_process(mc_process_t p) {
32   return p;
33 }
34
35 static const s_mc_address_space_class_t mc_process_class = {
36   .read = (void*) &MC_process_read,
37   .get_process = (void*) MC_process_get_process
38 };
39
40 bool MC_is_process(mc_address_space_t p)
41 {
42   return p->address_space_class == &mc_process_class;
43 }
44
45 void MC_process_init(mc_process_t process, pid_t pid, int sockfd)
46 {
47   process->address_space.address_space_class = &mc_process_class;
48   process->process_flags = MC_PROCESS_NO_FLAG;
49   process->socket = sockfd;
50   process->pid = pid;
51   if (pid==getpid())
52     process->process_flags |= MC_PROCESS_SELF_FLAG;
53   process->running = true;
54   process->status = 0;
55   process->memory_map = MC_get_memory_map(pid);
56   process->memory_file = -1;
57   process->cache_flags = 0;
58   process->heap = NULL;
59   process->heap_info = NULL;
60   MC_process_init_memory_map_info(process);
61   MC_process_open_memory_file(process);
62
63   // Read std_heap (is a struct mdesc*):
64   dw_variable_t std_heap_var = MC_process_find_variable_by_name(process, "std_heap");
65   if (!std_heap_var)
66     xbt_die("No heap information in the target process");
67   if(!std_heap_var->address)
68     xbt_die("No constant address for this variable");
69   MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
70     &process->heap_address, std_heap_var->address, sizeof(struct mdesc*),
71     MC_PROCESS_INDEX_DISABLED);
72
73   process->unw_addr_space = unw_create_addr_space(&mc_unw_accessors  , __BYTE_ORDER);
74   if (process->process_flags & MC_PROCESS_SELF_FLAG) {
75     process->unw_underlying_addr_space = unw_local_addr_space;
76     process->unw_underlying_context = NULL;
77   } else {
78     process->unw_underlying_addr_space = unw_create_addr_space(&mc_unw_vmread_accessors, __BYTE_ORDER);
79     process->unw_underlying_context = _UPT_create(pid);
80   }
81 }
82
83 void MC_process_clear(mc_process_t process)
84 {
85   process->address_space.address_space_class = NULL;
86   process->process_flags = MC_PROCESS_NO_FLAG;
87   process->pid = 0;
88
89   MC_free_memory_map(process->memory_map);
90   process->memory_map = NULL;
91
92   process->maestro_stack_start = NULL;
93   process->maestro_stack_end = NULL;
94
95   size_t i;
96   for (i=0; i!=process->object_infos_size; ++i) {
97     MC_free_object_info(&process->object_infos[i]);
98   }
99   free(process->object_infos);
100   process->object_infos = NULL;
101   process->object_infos_size = 0;
102   if (process->memory_file >= 0) {
103     close(process->memory_file);
104   }
105
106   if (process->unw_underlying_addr_space != unw_local_addr_space) {
107     unw_destroy_addr_space(process->unw_underlying_addr_space);
108     _UPT_destroy(process->unw_underlying_context);
109   }
110   process->unw_underlying_context = NULL;
111   process->unw_underlying_addr_space = NULL;
112
113   unw_destroy_addr_space(process->unw_addr_space);
114   process->unw_addr_space = NULL;
115
116   process->cache_flags = 0;
117
118   free(process->heap);
119   process->heap = NULL;
120
121   free(process->heap_info);
122   process->heap_info = NULL;
123 }
124
125 void MC_process_refresh_heap(mc_process_t process)
126 {
127   assert(!MC_process_is_self(process));
128   // Read/dereference/refresh the std_heap pointer:
129   if (!process->heap) {
130     xbt_mheap_t oldheap  = mmalloc_get_current_heap();
131     MC_SET_MC_HEAP;
132     process->heap = malloc(sizeof(struct mdesc));
133     mmalloc_set_current_heap(oldheap);
134   }
135   MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
136     process->heap, process->heap_address, sizeof(struct mdesc),
137     MC_PROCESS_INDEX_DISABLED
138     );
139 }
140
141 void MC_process_refresh_malloc_info(mc_process_t process)
142 {
143   assert(!MC_process_is_self(process));
144   if (!process->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP)
145     MC_process_refresh_heap(process);
146   // Refresh process->heapinfo:
147   size_t malloc_info_bytesize = process->heap->heaplimit * sizeof(malloc_info);
148
149   xbt_mheap_t oldheap  = mmalloc_get_current_heap();
150   MC_SET_MC_HEAP;
151   process->heap_info = (malloc_info*) realloc(process->heap_info,
152     malloc_info_bytesize);
153   mmalloc_set_current_heap(oldheap);
154
155   MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
156     process->heap_info,
157     process->heap->heapinfo, malloc_info_bytesize,
158     MC_PROCESS_INDEX_DISABLED);
159 }
160
161 #define SO_RE "\\.so[\\.0-9]*$"
162 #define VERSION_RE "-[\\.0-9]*$"
163
164 const char* FILTERED_LIBS[] = {
165   "libstdc++",
166   "libc++",
167   "libm",
168   "libgcc_s",
169   "libpthread",
170   "libunwind",
171   "libunwind-x86_64",
172   "libunwind-x86",
173   "libunwind-ptrace",
174   "libdw",
175   "libdl",
176   "librt",
177   "liblzma",
178   "libelf",
179   "libbz2",
180   "libz",
181   "libelf",
182   "libc",
183   "ld"
184 };
185
186 static bool MC_is_simgrid_lib(const char* libname)
187 {
188   return !strcmp(libname, "libsimgrid");
189 }
190
191 static bool MC_is_filtered_lib(const char* libname)
192 {
193   const size_t n = sizeof(FILTERED_LIBS) / sizeof(const char*);
194   size_t i;
195   for (i=0; i!=n; ++i)
196     if (strcmp(libname, FILTERED_LIBS[i])==0)
197       return true;
198   return false;
199 }
200
201 struct s_mc_memory_map_re {
202   regex_t so_re;
203   regex_t version_re;
204 };
205
206 static char* MC_get_lib_name(const char* pathname, struct s_mc_memory_map_re* res) {
207   const char* map_basename = basename((char*) pathname);
208
209   regmatch_t match;
210   if(regexec(&res->so_re, map_basename, 1, &match, 0))
211     return NULL;
212
213   char* libname = strndup(map_basename, match.rm_so);
214
215   // Strip the version suffix:
216   if(libname && !regexec(&res->version_re, libname, 1, &match, 0)) {
217     char* temp = libname;
218     libname = strndup(temp, match.rm_so);
219     free(temp);
220   }
221
222   return libname;
223 }
224
225 /** @brief Finds the range of the different memory segments and binary paths */
226 static void MC_process_init_memory_map_info(mc_process_t process)
227 {
228   XBT_DEBUG("Get debug information ...");
229   process->maestro_stack_start = NULL;
230   process->maestro_stack_end = NULL;
231   process->object_infos = NULL;
232   process->object_infos_size = 0;
233   process->binary_info = NULL;
234   process->libsimgrid_info = NULL;
235
236   struct s_mc_memory_map_re res;
237
238   if(regcomp(&res.so_re, SO_RE, 0) || regcomp(&res.version_re, VERSION_RE, 0))
239     xbt_die(".so regexp did not compile");
240
241   memory_map_t maps = process->memory_map;
242
243   const char* current_name = NULL;
244
245   size_t i = 0;
246   for (i=0; i < maps->mapsize; i++) {
247     map_region_t reg = &(maps->regions[i]);
248     const char* pathname = maps->regions[i].pathname;
249
250     // Nothing to do
251     if (maps->regions[i].pathname == NULL) {
252       current_name = NULL;
253       continue;
254     }
255
256     // [stack], [vvar], [vsyscall], [vdso] ...
257     if (pathname[0] == '[') {
258       if ((reg->prot & PROT_WRITE) && !memcmp(pathname, "[stack]", 7)) {
259         process->maestro_stack_start = reg->start_addr;
260         process->maestro_stack_end = reg->end_addr;
261       }
262       current_name = NULL;
263       continue;
264     }
265
266     if (current_name && strcmp(current_name, pathname)==0)
267       continue;
268
269     current_name = pathname;
270     if (!(reg->prot & PROT_READ) && (reg->prot & PROT_EXEC))
271       continue;
272
273     const bool is_executable = !i;
274     char* libname = NULL;
275     if (!is_executable) {
276       libname = MC_get_lib_name(pathname, &res);
277       if(!libname)
278         continue;
279       if (MC_is_filtered_lib(libname)) {
280         free(libname);
281         continue;
282       }
283     }
284
285     mc_object_info_t info =
286       MC_find_object_info(process->memory_map, pathname, is_executable);
287     process->object_infos = (mc_object_info_t*) realloc(process->object_infos,
288       (process->object_infos_size+1) * sizeof(mc_object_info_t*));
289     process->object_infos[process->object_infos_size] = info;
290     process->object_infos_size++;
291     if (is_executable)
292       process->binary_info = info;
293     else if (libname && MC_is_simgrid_lib(libname))
294       process->libsimgrid_info = info;
295     free(libname);
296   }
297
298   regfree(&res.so_re);
299   regfree(&res.version_re);
300
301   // Resolve time (including accress differents objects):
302   for (i=0; i!=process->object_infos_size; ++i)
303     MC_post_process_object_info(process, process->object_infos[i]);
304
305   xbt_assert(process->maestro_stack_start, "Did not find maestro_stack_start");
306   xbt_assert(process->maestro_stack_end, "Did not find maestro_stack_end");
307
308   XBT_DEBUG("Get debug information done !");
309 }
310
311 mc_object_info_t MC_process_find_object_info(mc_process_t process, const void *addr)
312 {
313   size_t i;
314   for (i = 0; i != process->object_infos_size; ++i) {
315     if (addr >= (void *) process->object_infos[i]->start
316         && addr <= (void *) process->object_infos[i]->end) {
317       return process->object_infos[i];
318     }
319   }
320   return NULL;
321 }
322
323 mc_object_info_t MC_process_find_object_info_exec(mc_process_t process, const void *addr)
324 {
325   size_t i;
326   for (i = 0; i != process->object_infos_size; ++i) {
327     if (addr >= (void *) process->object_infos[i]->start_exec
328         && addr <= (void *) process->object_infos[i]->end_exec) {
329       return process->object_infos[i];
330     }
331   }
332   return NULL;
333 }
334
335 mc_object_info_t MC_process_find_object_info_rw(mc_process_t process, const void *addr)
336 {
337   size_t i;
338   for (i = 0; i != process->object_infos_size; ++i) {
339     if (addr >= (void *) process->object_infos[i]->start_rw
340         && addr <= (void *) process->object_infos[i]->end_rw) {
341       return process->object_infos[i];
342     }
343   }
344   return NULL;
345 }
346
347 // Functions, variables…
348
349 dw_frame_t MC_process_find_function(mc_process_t process, const void *ip)
350 {
351   mc_object_info_t info = MC_process_find_object_info_exec(process, ip);
352   if (info == NULL)
353     return NULL;
354   else
355     return MC_file_object_info_find_function(info, ip);
356 }
357
358 dw_variable_t MC_process_find_variable_by_name(mc_process_t process, const char* name)
359 {
360   const size_t n = process->object_infos_size;
361   size_t i;
362   for (i=0; i!=n; ++i) {
363     mc_object_info_t info =process->object_infos[i];
364     dw_variable_t var = MC_file_object_info_find_variable_by_name(info, name);
365     if (var)
366       return var;
367   }
368   return NULL;
369 }
370
371 // ***** Memory access
372
373 int MC_process_vm_open(pid_t pid, int flags)
374 {
375   const size_t buffer_size = 30;
376   char buffer[buffer_size];
377   int res = snprintf(buffer, buffer_size, "/proc/%lli/mem", (long long) pid);
378   if (res < 0 || res >= buffer_size) {
379     errno = ENAMETOOLONG;
380     return -1;
381   }
382   return open(buffer, flags);
383 }
384
385 static void MC_process_open_memory_file(mc_process_t process)
386 {
387   if (MC_process_is_self(process) || process->memory_file >= 0)
388     return;
389
390   int fd = MC_process_vm_open(process->pid, O_RDWR);
391   if (fd<0)
392     xbt_die("Could not open file for process virtual address space");
393   process->memory_file = fd;
394 }
395
396 static ssize_t pread_whole(int fd, void *buf, size_t count, off_t offset)
397 {
398   char* buffer = (char*) buf;
399   ssize_t real_count = count;
400   while (count) {
401     ssize_t res = pread(fd, buffer, count, offset);
402     if (res > 0) {
403       count  -= res;
404       buffer += res;
405       offset += res;
406     } else if (res==0) {
407       return -1;
408     } else if (errno != EINTR) {
409       return -1;
410     }
411   }
412   return real_count;
413 }
414
415 static ssize_t pwrite_whole(int fd, const void *buf, size_t count, off_t offset)
416 {
417   const char* buffer = (const char*) buf;
418   ssize_t real_count = count;
419   while (count) {
420     ssize_t res = pwrite(fd, buffer, count, offset);
421     if (res > 0) {
422       count  -= res;
423       buffer += res;
424       offset += res;
425     } else if (res==0) {
426       return -1;
427     } else if (errno != EINTR) {
428       return -1;
429     }
430   }
431   return real_count;
432 }
433
434 const void* MC_process_read(mc_process_t process, e_adress_space_read_flags_t flags,
435   void* local, const void* remote, size_t len,
436   int process_index)
437 {
438   if (process_index != MC_PROCESS_INDEX_DISABLED) {
439     mc_object_info_t info = MC_process_find_object_info_rw(process, remote);
440     // Segment overlap is not handled.
441     if (MC_object_info_is_privatized(info)) {
442       if (process_index < 0)
443         xbt_die("Missing process index");
444       // Address translation in the privaization segment:
445       size_t offset = (const char*) remote - info->start_rw;
446       remote = (const char*) remote - offset;
447     }
448   }
449
450   if (MC_process_is_self(process)) {
451     if (flags & MC_ADDRESS_SPACE_READ_FLAGS_LAZY)
452       return remote;
453     else {
454       memcpy(local, remote, len);
455       return local;
456     }
457   } else {
458     if (pread_whole(process->memory_file, local, len, (off_t) remote) < 0)
459       xbt_die("Read from process %lli failed", (long long) process->pid);
460     return local;
461   }
462 }
463
464 void MC_process_write(mc_process_t process, const void* local, void* remote, size_t len)
465 {
466   if (MC_process_is_self(process)) {
467     memcpy(remote, local, len);
468   } else {
469     if (pwrite_whole(process->memory_file, local, len, (off_t) remote) < 0)
470       xbt_die("Write to process %lli failed", (long long) process->pid);
471   }
472 }
473
474 static pthread_once_t zero_buffer_flag = PTHREAD_ONCE_INIT;
475 static const void* zero_buffer;
476 static const int zero_buffer_size = 10 * 4096;
477
478 static void MC_zero_buffer_init(void)
479 {
480   int fd = open("/dev/zero", O_RDONLY);
481   if (fd<0)
482     xbt_die("Could not open /dev/zero");
483   zero_buffer = mmap(NULL, zero_buffer_size, PROT_READ, MAP_SHARED, fd, 0);
484   if (zero_buffer == MAP_FAILED)
485     xbt_die("Could not map the zero buffer");
486   close(fd);
487 }
488
489 void MC_process_clear_memory(mc_process_t process, void* remote, size_t len)
490 {
491   if (MC_process_is_self(process)) {
492     memset(remote, 0, len);
493   } else {
494     pthread_once(&zero_buffer_flag, MC_zero_buffer_init);
495     while (len) {
496       size_t s = len > zero_buffer_size ? zero_buffer_size : len;
497       MC_process_write(process, zero_buffer, remote, s);
498       remote = (char*) remote + s;
499       len -= s;
500     }
501   }
502 }