Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
0beaf82facb738687782ff858721736d2e819153
[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 <xbt/mmalloc.h>
21
22 #include "mc_process.h"
23 #include "mc_object_info.h"
24 #include "mc_address_space.h"
25 #include "mc_unw.h"
26 #include "mc_snapshot.h"
27 #include "mc_ignore.h"
28
29 #include "simix/smx_private.h"
30
31 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_process, mc,
32                                 "MC process information");
33
34 static void MC_process_init_memory_map_info(mc_process_t process);
35 static void MC_process_open_memory_file(mc_process_t process);
36
37 // ***** Destructor callbacks
38
39 // ***** mc_address_space methods for mc_process
40
41 static mc_process_t MC_process_get_process(mc_process_t p) {
42   return p;
43 }
44
45 static const s_mc_address_space_class_t mc_process_class = {
46   .read = (void*) &MC_process_read,
47   .get_process = (void*) MC_process_get_process
48 };
49
50 bool MC_is_process(mc_address_space_t p)
51 {
52   return p->address_space_class == &mc_process_class;
53 }
54
55 // ***** mc_process
56
57 void MC_process_init(mc_process_t process, pid_t pid, int sockfd)
58 {
59   process->address_space.address_space_class = &mc_process_class;
60   process->process_flags = MC_PROCESS_NO_FLAG;
61   process->socket = sockfd;
62   process->pid = pid;
63   if (pid==getpid())
64     process->process_flags |= MC_PROCESS_SELF_FLAG;
65   process->running = true;
66   process->status = 0;
67   process->memory_map = MC_get_memory_map(pid);
68   process->memory_file = -1;
69   process->cache_flags = 0;
70   process->heap = NULL;
71   process->heap_info = NULL;
72   MC_process_init_memory_map_info(process);
73   MC_process_open_memory_file(process);
74
75   // Read std_heap (is a struct mdesc*):
76   dw_variable_t std_heap_var = MC_process_find_variable_by_name(process, "std_heap");
77   if (!std_heap_var)
78     xbt_die("No heap information in the target process");
79   if(!std_heap_var->address)
80     xbt_die("No constant address for this variable");
81   MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
82     &process->heap_address, std_heap_var->address, sizeof(struct mdesc*),
83     MC_PROCESS_INDEX_DISABLED);
84
85   process->smx_process_infos = mc_smx_process_info_list_new();
86   process->smx_old_process_infos = mc_smx_process_info_list_new();
87
88   process->checkpoint_ignore = MC_checkpoint_ignore_new();
89
90   process->unw_addr_space = unw_create_addr_space(&mc_unw_accessors  , __BYTE_ORDER);
91   if (process->process_flags & MC_PROCESS_SELF_FLAG) {
92     process->unw_underlying_addr_space = unw_local_addr_space;
93     process->unw_underlying_context = NULL;
94   } else {
95     process->unw_underlying_addr_space = unw_create_addr_space(&mc_unw_vmread_accessors, __BYTE_ORDER);
96     process->unw_underlying_context = _UPT_create(pid);
97   }
98 }
99
100 void MC_process_clear(mc_process_t process)
101 {
102   process->address_space.address_space_class = NULL;
103   process->process_flags = MC_PROCESS_NO_FLAG;
104   process->pid = 0;
105
106   MC_free_memory_map(process->memory_map);
107   process->memory_map = NULL;
108
109   process->maestro_stack_start = NULL;
110   process->maestro_stack_end = NULL;
111
112   xbt_dynar_free(&process->checkpoint_ignore);
113
114   xbt_dynar_free(&process->smx_process_infos);
115   xbt_dynar_free(&process->smx_old_process_infos);
116
117   size_t i;
118   for (i=0; i!=process->object_infos_size; ++i) {
119     MC_free_object_info(&process->object_infos[i]);
120   }
121   free(process->object_infos);
122   process->object_infos = NULL;
123   process->object_infos_size = 0;
124   if (process->memory_file >= 0) {
125     close(process->memory_file);
126   }
127
128   if (process->unw_underlying_addr_space != unw_local_addr_space) {
129     unw_destroy_addr_space(process->unw_underlying_addr_space);
130     _UPT_destroy(process->unw_underlying_context);
131   }
132   process->unw_underlying_context = NULL;
133   process->unw_underlying_addr_space = NULL;
134
135   unw_destroy_addr_space(process->unw_addr_space);
136   process->unw_addr_space = NULL;
137
138   process->cache_flags = 0;
139
140   free(process->heap);
141   process->heap = NULL;
142
143   free(process->heap_info);
144   process->heap_info = NULL;
145 }
146
147 void MC_process_refresh_heap(mc_process_t process)
148 {
149   assert(!MC_process_is_self(process));
150   // Read/dereference/refresh the std_heap pointer:
151   if (!process->heap) {
152     xbt_mheap_t oldheap  = mmalloc_set_current_heap(mc_heap);
153     process->heap = malloc(sizeof(struct mdesc));
154     mmalloc_set_current_heap(oldheap);
155   }
156   MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
157     process->heap, process->heap_address, sizeof(struct mdesc),
158     MC_PROCESS_INDEX_DISABLED
159     );
160 }
161
162 void MC_process_refresh_malloc_info(mc_process_t process)
163 {
164   assert(!MC_process_is_self(process));
165   if (!process->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP)
166     MC_process_refresh_heap(process);
167   // Refresh process->heapinfo:
168   size_t malloc_info_bytesize = process->heap->heaplimit * sizeof(malloc_info);
169
170   xbt_mheap_t heap  = mmalloc_set_current_heap(mc_heap);
171   process->heap_info = (malloc_info*) realloc(process->heap_info,
172     malloc_info_bytesize);
173   mmalloc_set_current_heap(heap);
174
175   MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
176     process->heap_info,
177     process->heap->heapinfo, malloc_info_bytesize,
178     MC_PROCESS_INDEX_DISABLED);
179 }
180
181 /** Load the remote swag of processes into a dynar
182  *
183  *  @param process     MCed process
184  *  @param target      Local dynar (to be filled with copies of `s_smx_process_t`)
185  *  @param remote_swag Address of the process SWAG in the remote list
186  */
187 static void MC_process_refresh_simix_process_list(
188   mc_process_t process,
189   xbt_dynar_t target, xbt_swag_t remote_swag)
190 {
191   // swag = REMOTE(*simix_global->process_list)
192   s_xbt_swag_t swag;
193   MC_process_read(process, MC_PROCESS_NO_FLAG, &swag, remote_swag, sizeof(swag),
194     MC_PROCESS_INDEX_ANY);
195
196   smx_process_t p;
197   xbt_dynar_reset(target);
198
199   int i = 0;
200   for (p = swag.head; p; ++i) {
201
202     s_mc_smx_process_info_t info;
203     info.address = p;
204     MC_process_read(process, MC_PROCESS_NO_FLAG,
205       &info.copy, p, sizeof(info.copy), MC_PROCESS_INDEX_ANY);
206     xbt_dynar_push(target, &info);
207
208     // Lookup next process address:
209     p = xbt_swag_getNext(&info.copy, swag.offset);
210   }
211   assert(i == swag.count);
212 }
213
214 void MC_process_refresh_simix_processes(mc_process_t process)
215 {
216   if (process->cache_flags & MC_PROCESS_CACHE_FLAG_SIMIX_PROCESSES)
217     return;
218
219   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
220
221   // TODO, avoid to reload `&simix_global`, `simix_global`, `*simix_global`
222
223   // simix_global_p = REMOTE(simix_global);
224   smx_global_t simix_global_p;
225   MC_process_read_variable(process, "simix_global", &simix_global_p, sizeof(simix_global_p));
226
227   // simix_global = REMOTE(*simix_global)
228   s_smx_global_t simix_global;
229   MC_process_read(process, MC_PROCESS_NO_FLAG, &simix_global, simix_global_p, sizeof(simix_global),
230     MC_PROCESS_INDEX_ANY);
231
232   MC_process_refresh_simix_process_list(
233     process, process->smx_process_infos, simix_global.process_list);
234   MC_process_refresh_simix_process_list(
235     process, process->smx_old_process_infos, simix_global.process_to_destroy);
236
237   process->cache_flags |= MC_PROCESS_CACHE_FLAG_SIMIX_PROCESSES;
238   mmalloc_set_current_heap(heap);
239 }
240
241 #define SO_RE "\\.so[\\.0-9]*$"
242 #define VERSION_RE "-[\\.0-9]*$"
243
244 const char* FILTERED_LIBS[] = {
245   "libstdc++",
246   "libc++",
247   "libm",
248   "libgcc_s",
249   "libpthread",
250   "libunwind",
251   "libunwind-x86_64",
252   "libunwind-x86",
253   "libunwind-ptrace",
254   "libdw",
255   "libdl",
256   "librt",
257   "liblzma",
258   "libelf",
259   "libbz2",
260   "libz",
261   "libelf",
262   "libc",
263   "ld"
264 };
265
266 static bool MC_is_simgrid_lib(const char* libname)
267 {
268   return !strcmp(libname, "libsimgrid");
269 }
270
271 static bool MC_is_filtered_lib(const char* libname)
272 {
273   const size_t n = sizeof(FILTERED_LIBS) / sizeof(const char*);
274   size_t i;
275   for (i=0; i!=n; ++i)
276     if (strcmp(libname, FILTERED_LIBS[i])==0)
277       return true;
278   return false;
279 }
280
281 struct s_mc_memory_map_re {
282   regex_t so_re;
283   regex_t version_re;
284 };
285
286 static char* MC_get_lib_name(const char* pathname, struct s_mc_memory_map_re* res) {
287   const char* map_basename = basename((char*) pathname);
288
289   regmatch_t match;
290   if(regexec(&res->so_re, map_basename, 1, &match, 0))
291     return NULL;
292
293   char* libname = strndup(map_basename, match.rm_so);
294
295   // Strip the version suffix:
296   if(libname && !regexec(&res->version_re, libname, 1, &match, 0)) {
297     char* temp = libname;
298     libname = strndup(temp, match.rm_so);
299     free(temp);
300   }
301
302   return libname;
303 }
304
305 /** @brief Finds the range of the different memory segments and binary paths */
306 static void MC_process_init_memory_map_info(mc_process_t process)
307 {
308   XBT_DEBUG("Get debug information ...");
309   process->maestro_stack_start = NULL;
310   process->maestro_stack_end = NULL;
311   process->object_infos = NULL;
312   process->object_infos_size = 0;
313   process->binary_info = NULL;
314   process->libsimgrid_info = NULL;
315
316   struct s_mc_memory_map_re res;
317
318   if(regcomp(&res.so_re, SO_RE, 0) || regcomp(&res.version_re, VERSION_RE, 0))
319     xbt_die(".so regexp did not compile");
320
321   memory_map_t maps = process->memory_map;
322
323   const char* current_name = NULL;
324
325   size_t i = 0;
326   for (i=0; i < maps->mapsize; i++) {
327     map_region_t reg = &(maps->regions[i]);
328     const char* pathname = maps->regions[i].pathname;
329
330     // Nothing to do
331     if (maps->regions[i].pathname == NULL) {
332       current_name = NULL;
333       continue;
334     }
335
336     // [stack], [vvar], [vsyscall], [vdso] ...
337     if (pathname[0] == '[') {
338       if ((reg->prot & PROT_WRITE) && !memcmp(pathname, "[stack]", 7)) {
339         process->maestro_stack_start = reg->start_addr;
340         process->maestro_stack_end = reg->end_addr;
341       }
342       current_name = NULL;
343       continue;
344     }
345
346     if (current_name && strcmp(current_name, pathname)==0)
347       continue;
348
349     current_name = pathname;
350     if (!(reg->prot & PROT_READ) && (reg->prot & PROT_EXEC))
351       continue;
352
353     const bool is_executable = !i;
354     char* libname = NULL;
355     if (!is_executable) {
356       libname = MC_get_lib_name(pathname, &res);
357       if(!libname)
358         continue;
359       if (MC_is_filtered_lib(libname)) {
360         free(libname);
361         continue;
362       }
363     }
364
365     mc_object_info_t info =
366       MC_find_object_info(process->memory_map, pathname, is_executable);
367     process->object_infos = (mc_object_info_t*) realloc(process->object_infos,
368       (process->object_infos_size+1) * sizeof(mc_object_info_t*));
369     process->object_infos[process->object_infos_size] = info;
370     process->object_infos_size++;
371     if (is_executable)
372       process->binary_info = info;
373     else if (libname && MC_is_simgrid_lib(libname))
374       process->libsimgrid_info = info;
375     free(libname);
376   }
377
378   regfree(&res.so_re);
379   regfree(&res.version_re);
380
381   // Resolve time (including accress differents objects):
382   for (i=0; i!=process->object_infos_size; ++i)
383     MC_post_process_object_info(process, process->object_infos[i]);
384
385   xbt_assert(process->maestro_stack_start, "Did not find maestro_stack_start");
386   xbt_assert(process->maestro_stack_end, "Did not find maestro_stack_end");
387
388   XBT_DEBUG("Get debug information done !");
389 }
390
391 mc_object_info_t MC_process_find_object_info(mc_process_t process, const void *addr)
392 {
393   size_t i;
394   for (i = 0; i != process->object_infos_size; ++i) {
395     if (addr >= (void *) process->object_infos[i]->start
396         && addr <= (void *) process->object_infos[i]->end) {
397       return process->object_infos[i];
398     }
399   }
400   return NULL;
401 }
402
403 mc_object_info_t MC_process_find_object_info_exec(mc_process_t process, const void *addr)
404 {
405   size_t i;
406   for (i = 0; i != process->object_infos_size; ++i) {
407     if (addr >= (void *) process->object_infos[i]->start_exec
408         && addr <= (void *) process->object_infos[i]->end_exec) {
409       return process->object_infos[i];
410     }
411   }
412   return NULL;
413 }
414
415 mc_object_info_t MC_process_find_object_info_rw(mc_process_t process, const void *addr)
416 {
417   size_t i;
418   for (i = 0; i != process->object_infos_size; ++i) {
419     if (addr >= (void *) process->object_infos[i]->start_rw
420         && addr <= (void *) process->object_infos[i]->end_rw) {
421       return process->object_infos[i];
422     }
423   }
424   return NULL;
425 }
426
427 // Functions, variables…
428
429 dw_frame_t MC_process_find_function(mc_process_t process, const void *ip)
430 {
431   mc_object_info_t info = MC_process_find_object_info_exec(process, ip);
432   if (info == NULL)
433     return NULL;
434   else
435     return MC_file_object_info_find_function(info, ip);
436 }
437
438 dw_variable_t MC_process_find_variable_by_name(mc_process_t process, const char* name)
439 {
440   const size_t n = process->object_infos_size;
441   size_t i;
442
443   // First lookup the variable in the executable shared object.
444   // A global variable used directly by the executable code from a library
445   // is reinstanciated in the executable memory .data/.bss.
446   // We need to look up the variable in the execvutable first.
447   if (process->binary_info) {
448     mc_object_info_t info = process->binary_info;
449     dw_variable_t var = MC_file_object_info_find_variable_by_name(info, name);
450     if (var)
451       return var;
452   }
453
454   for (i=0; i!=n; ++i) {
455     mc_object_info_t info =process->object_infos[i];
456     dw_variable_t var = MC_file_object_info_find_variable_by_name(info, name);
457     if (var)
458       return var;
459   }
460
461   return NULL;
462 }
463
464 void MC_process_read_variable(mc_process_t process, const char* name, void* target, size_t size)
465 {
466   dw_variable_t var = MC_process_find_variable_by_name(process, name);
467   if (!var->address)
468     xbt_die("No simple location for this variable");
469   MC_process_read(process, MC_PROCESS_NO_FLAG, target, var->address, size,
470     MC_PROCESS_INDEX_ANY);
471 }
472
473 // ***** Memory access
474
475 int MC_process_vm_open(pid_t pid, int flags)
476 {
477   const size_t buffer_size = 30;
478   char buffer[buffer_size];
479   int res = snprintf(buffer, buffer_size, "/proc/%lli/mem", (long long) pid);
480   if (res < 0 || res >= buffer_size) {
481     errno = ENAMETOOLONG;
482     return -1;
483   }
484   return open(buffer, flags);
485 }
486
487 static void MC_process_open_memory_file(mc_process_t process)
488 {
489   if (MC_process_is_self(process) || process->memory_file >= 0)
490     return;
491
492   int fd = MC_process_vm_open(process->pid, O_RDWR);
493   if (fd<0)
494     xbt_die("Could not open file for process virtual address space");
495   process->memory_file = fd;
496 }
497
498 static ssize_t pread_whole(int fd, void *buf, size_t count, off_t offset)
499 {
500   char* buffer = (char*) buf;
501   ssize_t real_count = count;
502   while (count) {
503     ssize_t res = pread(fd, buffer, count, offset);
504     if (res > 0) {
505       count  -= res;
506       buffer += res;
507       offset += res;
508     } else if (res==0) {
509       return -1;
510     } else if (errno != EINTR) {
511       return -1;
512     }
513   }
514   return real_count;
515 }
516
517 static ssize_t pwrite_whole(int fd, const void *buf, size_t count, off_t offset)
518 {
519   const char* buffer = (const char*) buf;
520   ssize_t real_count = count;
521   while (count) {
522     ssize_t res = pwrite(fd, buffer, count, offset);
523     if (res > 0) {
524       count  -= res;
525       buffer += res;
526       offset += res;
527     } else if (res==0) {
528       return -1;
529     } else if (errno != EINTR) {
530       return -1;
531     }
532   }
533   return real_count;
534 }
535
536 const void* MC_process_read(mc_process_t process, e_adress_space_read_flags_t flags,
537   void* local, const void* remote, size_t len,
538   int process_index)
539 {
540   if (process_index != MC_PROCESS_INDEX_DISABLED) {
541     mc_object_info_t info = MC_process_find_object_info_rw(process, remote);
542     // Segment overlap is not handled.
543     if (MC_object_info_is_privatized(info)) {
544       if (process_index < 0)
545         xbt_die("Missing process index");
546       // Address translation in the privaization segment:
547       size_t offset = (const char*) remote - info->start_rw;
548       remote = (const char*) remote - offset;
549     }
550   }
551
552   if (MC_process_is_self(process)) {
553     if (flags & MC_ADDRESS_SPACE_READ_FLAGS_LAZY)
554       return remote;
555     else {
556       memcpy(local, remote, len);
557       return local;
558     }
559   } else {
560     if (pread_whole(process->memory_file, local, len, (off_t) remote) < 0)
561       xbt_die("Read from process %lli failed", (long long) process->pid);
562     return local;
563   }
564 }
565
566 void MC_process_write(mc_process_t process, const void* local, void* remote, size_t len)
567 {
568   if (MC_process_is_self(process)) {
569     memcpy(remote, local, len);
570   } else {
571     if (pwrite_whole(process->memory_file, local, len, (off_t) remote) < 0)
572       xbt_die("Write to process %lli failed", (long long) process->pid);
573   }
574 }
575
576 static pthread_once_t zero_buffer_flag = PTHREAD_ONCE_INIT;
577 static const void* zero_buffer;
578 static const int zero_buffer_size = 10 * 4096;
579
580 static void MC_zero_buffer_init(void)
581 {
582   int fd = open("/dev/zero", O_RDONLY);
583   if (fd<0)
584     xbt_die("Could not open /dev/zero");
585   zero_buffer = mmap(NULL, zero_buffer_size, PROT_READ, MAP_SHARED, fd, 0);
586   if (zero_buffer == MAP_FAILED)
587     xbt_die("Could not map the zero buffer");
588   close(fd);
589 }
590
591 void MC_process_clear_memory(mc_process_t process, void* remote, size_t len)
592 {
593   if (MC_process_is_self(process)) {
594     memset(remote, 0, len);
595   } else {
596     pthread_once(&zero_buffer_flag, MC_zero_buffer_init);
597     while (len) {
598       size_t s = len > zero_buffer_size ? zero_buffer_size : len;
599       MC_process_write(process, zero_buffer, remote, s);
600       remote = (char*) remote + s;
601       len -= s;
602     }
603   }
604 }
605
606 /** Get the issuer of a simcall (`req->issuer`)
607  *
608  *  In split-process mode, it does the black magic necessary to get an address
609  *  of a (shallow) copy of the data structure the issuer SIMIX process in the local
610  *  address space.
611  *
612  *  @param process the MCed process
613  *  @param req     the simcall (copied in the local process)
614  */
615 smx_process_t MC_process_get_issuer(mc_process_t process, smx_simcall_t req)
616 {
617   if (MC_process_is_self(&mc_model_checker->process))
618     return req->issuer;
619
620   MC_process_refresh_simix_processes(process);
621
622   // This is the address of the smx_process in the MCed process:
623   void* address = req->issuer;
624
625   unsigned i;
626   mc_smx_process_info_t p;
627
628   xbt_dynar_foreach_ptr(process->smx_process_infos, i, p)
629     if (p->address == address)
630       return &p->copy;
631   xbt_dynar_foreach_ptr(process->smx_old_process_infos, i, p)
632     if (p->address == address)
633       return &p->copy;
634
635   xbt_die("Issuer not found");
636 }
637
638 void MC_simcall_handle(smx_simcall_t req, int value)
639 {
640   if (MC_process_is_self(&mc_model_checker->process)) {
641     SIMIX_simcall_handle(req, value);
642     return;
643   }
644
645   MC_process_refresh_simix_processes(&mc_model_checker->process);
646
647   unsigned i;
648   mc_smx_process_info_t pi = NULL;
649
650   xbt_dynar_foreach_ptr(mc_model_checker->process.smx_process_infos, i, pi) {
651     smx_process_t p = (smx_process_t) pi->address;
652     if (req == &pi->copy.simcall) {
653       smx_simcall_t real_req = &p->simcall;
654       // TODO, use a remote call
655       SIMIX_simcall_handle(real_req, value);
656       return;
657     }
658   }
659
660   // Check (remove afterwards):
661   xbt_dynar_foreach_ptr(mc_model_checker->process.smx_process_infos, i, pi) {
662     smx_process_t p = (smx_process_t) pi->address;
663     if (req == &p->simcall)
664       xbt_die("The real simcall was passed. We expected the local copy.");
665   }
666
667   xbt_die("Could not find the request");
668 }
669
670 void mc_smx_process_info_clear(mc_smx_process_info_t p)
671 {
672   // Nothing yet
673 }