Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : clarify comments
[simgrid.git] / src / mc / mc_checkpoint.c
1 /* Copyright (c) 2008-2014. 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 #define _GNU_SOURCE
8 #define UNW_LOCAL_ONLY
9
10 #include <string.h>
11 #include <link.h>
12 #include "mc_private.h"
13 #include "xbt/module.h"
14 #include <xbt/mmalloc.h>
15 #include "../smpi/private.h"
16
17 #include "xbt/mmalloc/mmprivate.h"
18
19 #include "../simix/smx_private.h"
20
21 #include <libunwind.h>
22 #include <libelf.h>
23
24 #include "mc_private.h"
25
26 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_checkpoint, mc,
27                                 "Logging specific to mc_checkpoint");
28
29 char *libsimgrid_path;
30
31 /************************************  Free functions **************************************/
32 /*****************************************************************************************/
33
34 static void MC_snapshot_stack_free(mc_snapshot_stack_t s)
35 {
36   if (s) {
37     xbt_dynar_free(&(s->local_variables));
38     xbt_dynar_free(&(s->stack_frames));
39     xbt_free(s);
40   }
41 }
42
43 static void MC_snapshot_stack_free_voidp(void *s)
44 {
45   MC_snapshot_stack_free((mc_snapshot_stack_t) * (void **) s);
46 }
47
48 static void local_variable_free(local_variable_t v)
49 {
50   xbt_free(v->name);
51   xbt_free(v);
52 }
53
54 static void local_variable_free_voidp(void *v)
55 {
56   local_variable_free((local_variable_t) * (void **) v);
57 }
58
59 static void MC_region_destroy(mc_mem_region_t reg)
60 {
61   //munmap(reg->data, reg->size);
62   xbt_free(reg->data);
63   xbt_free(reg);
64 }
65
66 void MC_free_snapshot(mc_snapshot_t snapshot)
67 {
68   unsigned int i;
69   for (i = 0; i < NB_REGIONS; i++)
70     MC_region_destroy(snapshot->regions[i]);
71
72   xbt_free(snapshot->stack_sizes);
73   xbt_dynar_free(&(snapshot->stacks));
74   xbt_dynar_free(&(snapshot->to_ignore));
75   xbt_dynar_free(&snapshot->ignored_data);
76
77   if (snapshot->privatization_regions) {
78     size_t n = xbt_dynar_length(snapshot->enabled_processes);
79     for (i = 0; i != n; ++i) {
80       MC_region_destroy(snapshot->privatization_regions[i]);
81     }
82     xbt_free(snapshot->privatization_regions);
83   }
84
85   xbt_free(snapshot);
86 }
87
88
89 /*******************************  Snapshot regions ********************************/
90 /*********************************************************************************/
91
92 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size)
93 {
94   mc_mem_region_t new_reg = xbt_new(s_mc_mem_region_t, 1);
95   new_reg->start_addr = start_addr;
96   new_reg->size = size;
97   //new_reg->data = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
98   //if(new_reg->data==MAP_FAILED)
99   //xbt_die("Could not mmap new memory for snapshot.");
100   new_reg->data = xbt_malloc(size);
101   memcpy(new_reg->data, start_addr, size);
102   //madvise(new_reg->data, size, MADV_MERGEABLE);
103
104   XBT_DEBUG("New region : type : %d, data : %p (real addr %p), size : %zu",
105             type, new_reg->data, start_addr, size);
106
107   return new_reg;
108 }
109
110 static void MC_region_restore(mc_mem_region_t reg)
111 {
112   /*FIXME: check if start_addr is still mapped, if it is not, then map it
113      before copying the data */
114
115   memcpy(reg->start_addr, reg->data, reg->size);
116   return;
117 }
118
119 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type,
120                                    void *start_addr, size_t size)
121 {
122   mc_mem_region_t new_reg = MC_region_new(type, start_addr, size);
123   snapshot->regions[type] = new_reg;
124   return;
125 }
126
127 static void MC_get_memory_regions(mc_snapshot_t snapshot)
128 {
129   size_t i;
130
131   void *start_heap = ((xbt_mheap_t) std_heap)->base;
132   void *end_heap = ((xbt_mheap_t) std_heap)->breakval;
133   MC_snapshot_add_region(snapshot, 0, start_heap,
134                          (char *) end_heap - (char *) start_heap);
135   snapshot->heap_bytes_used = mmalloc_get_bytes_used(std_heap);
136
137   MC_snapshot_add_region(snapshot, 1, mc_libsimgrid_info->start_rw,
138                          mc_libsimgrid_info->end_rw -
139                          mc_libsimgrid_info->start_rw);
140   if (!smpi_privatize_global_variables) {
141     MC_snapshot_add_region(snapshot, 2, mc_binary_info->start_rw,
142                            mc_binary_info->end_rw - mc_binary_info->start_rw);
143     snapshot->privatization_regions = NULL;
144     snapshot->privatization_index = -1;
145   } else {
146     snapshot->privatization_regions =
147         xbt_new(mc_mem_region_t, SIMIX_process_count());
148     for (i = 0; i < SIMIX_process_count(); i++) {
149       snapshot->privatization_regions[i] =
150           MC_region_new(-1, mappings[i], size_data_exe);
151     }
152     snapshot->privatization_index = loaded_page;
153   }
154 }
155
156 /** @brief Finds the range of the different memory segments and binary paths */
157 void MC_init_memory_map_info()
158 {
159
160   unsigned int i = 0;
161   s_map_region_t reg;
162   memory_map_t maps = MC_get_memory_map();
163
164   maestro_stack_start = NULL;
165   maestro_stack_end = NULL;
166   libsimgrid_path = NULL;
167
168   while (i < maps->mapsize) {
169     reg = maps->regions[i];
170     if (maps->regions[i].pathname == NULL) {
171       // Nothing to do
172     } else if ((reg.prot & PROT_WRITE)
173                && !memcmp(maps->regions[i].pathname, "[stack]", 7)) {
174       maestro_stack_start = reg.start_addr;
175       maestro_stack_end = reg.end_addr;
176     } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC)
177                && !memcmp(basename(maps->regions[i].pathname), "libsimgrid",
178                           10)) {
179       if (libsimgrid_path == NULL)
180         libsimgrid_path = strdup(maps->regions[i].pathname);
181     }
182     i++;
183   }
184
185   xbt_assert(maestro_stack_start, "maestro_stack_start");
186   xbt_assert(maestro_stack_end, "maestro_stack_end");
187   xbt_assert(libsimgrid_path, "libsimgrid_path&");
188
189   MC_free_memory_map(maps);
190
191 }
192
193 /** \brief Fill/lookup the "subtype" field.
194  */
195 static void MC_resolve_subtype(mc_object_info_t info, dw_type_t type)
196 {
197
198   if (type->dw_type_id == NULL)
199     return;
200   type->subtype = xbt_dict_get_or_null(info->types, type->dw_type_id);
201   if (type->subtype == NULL)
202     return;
203   if (type->subtype->byte_size != 0)
204     return;
205   if (type->subtype->name == NULL)
206     return;
207   // Try to find a more complete description of the type:
208   // We need to fix in order to support C++.
209
210   dw_type_t subtype =
211       xbt_dict_get_or_null(info->full_types_by_name, type->subtype->name);
212   if (subtype != NULL) {
213     type->subtype = subtype;
214   }
215
216 }
217
218 void MC_post_process_types(mc_object_info_t info)
219 {
220   xbt_dict_cursor_t cursor = NULL;
221   char *origin;
222   dw_type_t type;
223
224   // Lookup "subtype" field:
225   xbt_dict_foreach(info->types, cursor, origin, type) {
226     MC_resolve_subtype(info, type);
227
228     dw_type_t member;
229     unsigned int i = 0;
230     if (type->members != NULL)
231       xbt_dynar_foreach(type->members, i, member) {
232       MC_resolve_subtype(info, member);
233       }
234   }
235 }
236
237 /** \brief Fills the position of the segments (executable, read-only, read/write).
238  *
239  * TODO, use dl_iterate_phdr to be more robust
240  * */
241 void MC_find_object_address(memory_map_t maps, mc_object_info_t result)
242 {
243
244   unsigned int i = 0;
245   s_map_region_t reg;
246   const char *name = basename(result->file_name);
247   while (i < maps->mapsize) {
248     reg = maps->regions[i];
249     if (maps->regions[i].pathname == NULL
250         || strcmp(basename(maps->regions[i].pathname), name)) {
251       // Nothing to do
252     } else if ((reg.prot & PROT_WRITE)) {
253       xbt_assert(!result->start_rw,
254                  "Multiple read-write segments for %s, not supported",
255                  maps->regions[i].pathname);
256       result->start_rw = reg.start_addr;
257       result->end_rw = reg.end_addr;
258       // .bss is usually after the .data:
259       s_map_region_t *next = &(maps->regions[i + 1]);
260       if (next->pathname == NULL && (next->prot & PROT_WRITE)
261           && next->start_addr == reg.end_addr) {
262         result->end_rw = maps->regions[i + 1].end_addr;
263       }
264     } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC)) {
265       xbt_assert(!result->start_exec,
266                  "Multiple executable segments for %s, not supported",
267                  maps->regions[i].pathname);
268       result->start_exec = reg.start_addr;
269       result->end_exec = reg.end_addr;
270     } else if ((reg.prot & PROT_READ) && !(reg.prot & PROT_EXEC)) {
271       xbt_assert(!result->start_ro,
272                  "Multiple read only segments for %s, not supported",
273                  maps->regions[i].pathname);
274       result->start_ro = reg.start_addr;
275       result->end_ro = reg.end_addr;
276     }
277     i++;
278   }
279
280   xbt_assert(result->file_name);
281   xbt_assert(result->start_rw);
282   xbt_assert(result->start_exec);
283 }
284
285 /************************************* Take Snapshot ************************************/
286 /****************************************************************************************/
287
288 /** \brief Checks whether the variable is in scope for a given IP.
289  *
290  *  A variable may be defined only from a given value of IP.
291  *
292  *  \param var   Variable description
293  *  \param frame Scope description
294  *  \param ip    Instruction pointer
295  *  \return      true if the variable is valid
296  * */
297 static bool mc_valid_variable(dw_variable_t var, dw_frame_t scope,
298                               const void *ip)
299 {
300   // The variable is not yet valid:
301   if ((const void *) ((const char *) scope->low_pc + var->start_scope) > ip)
302     return false;
303   else
304     return true;
305 }
306
307 static void mc_fill_local_variables_values(mc_stack_frame_t stack_frame,
308                                            dw_frame_t scope, xbt_dynar_t result)
309 {
310   void *ip = (void *) stack_frame->ip;
311   if (ip < scope->low_pc || ip >= scope->high_pc)
312     return;
313
314   unsigned cursor = 0;
315   dw_variable_t current_variable;
316   xbt_dynar_foreach(scope->variables, cursor, current_variable) {
317
318     if (!mc_valid_variable(current_variable, scope, (void *) stack_frame->ip))
319       continue;
320
321     int region_type;
322     if ((long) stack_frame->ip > (long) mc_libsimgrid_info->start_exec)
323       region_type = 1;
324     else
325       region_type = 2;
326
327     local_variable_t new_var = xbt_new0(s_local_variable_t, 1);
328     new_var->subprogram = stack_frame->frame;
329     new_var->ip = stack_frame->ip;
330     new_var->name = xbt_strdup(current_variable->name);
331     new_var->type = current_variable->type;
332     new_var->region = region_type;
333
334     if (current_variable->address != NULL) {
335       new_var->address = current_variable->address;
336     } else if (current_variable->locations.size != 0) {
337       new_var->address =
338           (void *) mc_dwarf_resolve_locations(&current_variable->locations,
339                                               current_variable->object_info,
340                                               &(stack_frame->unw_cursor),
341                                               (void *) stack_frame->frame_base,
342                                               NULL);
343     } else {
344       xbt_die("No address");
345     }
346
347     xbt_dynar_push(result, &new_var);
348   }
349
350   // Recursive processing of nested scopes:
351   dw_frame_t nested_scope = NULL;
352   xbt_dynar_foreach(scope->scopes, cursor, nested_scope) {
353     mc_fill_local_variables_values(stack_frame, nested_scope, result);
354   }
355 }
356
357 static xbt_dynar_t MC_get_local_variables_values(xbt_dynar_t stack_frames)
358 {
359
360   unsigned cursor1 = 0;
361   mc_stack_frame_t stack_frame;
362   xbt_dynar_t variables =
363       xbt_dynar_new(sizeof(local_variable_t), local_variable_free_voidp);
364
365   xbt_dynar_foreach(stack_frames, cursor1, stack_frame) {
366     mc_fill_local_variables_values(stack_frame, stack_frame->frame, variables);
367   }
368
369   return variables;
370 }
371
372 static void MC_stack_frame_free_voipd(void *s)
373 {
374   mc_stack_frame_t stack_frame = *(mc_stack_frame_t *) s;
375   if (stack_frame) {
376     xbt_free(stack_frame->frame_name);
377     xbt_free(stack_frame);
378   }
379 }
380
381 static xbt_dynar_t MC_unwind_stack_frames(void *stack_context)
382 {
383   xbt_dynar_t result =
384       xbt_dynar_new(sizeof(mc_stack_frame_t), MC_stack_frame_free_voipd);
385
386   unw_cursor_t c;
387
388   // TODO, check condition check (unw_init_local==0 means end of frame)
389   if (unw_init_local(&c, (unw_context_t *) stack_context) != 0) {
390
391     xbt_die("Could not initialize stack unwinding");
392
393   } else
394     while (1) {
395
396       mc_stack_frame_t stack_frame = xbt_new(s_mc_stack_frame_t, 1);
397       xbt_dynar_push(result, &stack_frame);
398
399       stack_frame->unw_cursor = c;
400
401       unw_word_t ip, sp;
402
403       unw_get_reg(&c, UNW_REG_IP, &ip);
404       unw_get_reg(&c, UNW_REG_SP, &sp);
405
406       stack_frame->ip = ip;
407       stack_frame->sp = sp;
408
409       // TODO, use real addresses in frame_t instead of fixing it here
410
411       dw_frame_t frame = MC_find_function_by_ip((void *) ip);
412       stack_frame->frame = frame;
413
414       if (frame) {
415         stack_frame->frame_name = xbt_strdup(frame->name);
416         stack_frame->frame_base =
417             (unw_word_t) mc_find_frame_base(frame, frame->object_info, &c);
418       } else {
419         stack_frame->frame_base = 0;
420         stack_frame->frame_name = NULL;
421       }
422
423       /* Stop before context switch with maestro */
424       if (frame != NULL && frame->name != NULL
425           && !strcmp(frame->name, "smx_ctx_sysv_wrapper"))
426         break;
427
428       int ret = ret = unw_step(&c);
429       if (ret == 0) {
430         xbt_die("Unexpected end of stack.");
431       } else if (ret < 0) {
432         xbt_die("Error while unwinding stack.");
433       }
434     }
435
436   if (xbt_dynar_length(result) == 0) {
437     XBT_INFO("unw_init_local failed");
438     xbt_abort();
439   }
440
441   return result;
442 };
443
444 static xbt_dynar_t MC_take_snapshot_stacks(mc_snapshot_t * snapshot, void *heap)
445 {
446
447   xbt_dynar_t res =
448       xbt_dynar_new(sizeof(s_mc_snapshot_stack_t),
449                     MC_snapshot_stack_free_voidp);
450
451   unsigned int cursor = 0;
452   stack_region_t current_stack;
453
454   xbt_dynar_foreach(stacks_areas, cursor, current_stack) {
455     mc_snapshot_stack_t st = xbt_new(s_mc_snapshot_stack_t, 1);
456     st->stack_frames = MC_unwind_stack_frames(current_stack->context);
457     st->local_variables = MC_get_local_variables_values(st->stack_frames);
458
459     unw_word_t sp = xbt_dynar_get_as(st->stack_frames, 0, mc_stack_frame_t)->sp;
460     st->stack_pointer =
461         ((char *) heap + (size_t) (((char *) ((long) sp) - (char *) std_heap)));
462
463     st->real_address = current_stack->address;
464     xbt_dynar_push(res, &st);
465     (*snapshot)->stack_sizes =
466         xbt_realloc((*snapshot)->stack_sizes, (cursor + 1) * sizeof(size_t));
467     (*snapshot)->stack_sizes[cursor] =
468         current_stack->size - ((char *) st->stack_pointer -
469                                (char *) ((char *) heap +
470                                          ((char *) current_stack->address -
471                                           (char *) std_heap)));
472   }
473
474   return res;
475
476 }
477
478 static xbt_dynar_t MC_take_snapshot_ignore()
479 {
480
481   if (mc_heap_comparison_ignore == NULL)
482     return NULL;
483
484   xbt_dynar_t cpy =
485       xbt_dynar_new(sizeof(mc_heap_ignore_region_t),
486                     heap_ignore_region_free_voidp);
487
488   unsigned int cursor = 0;
489   mc_heap_ignore_region_t current_region;
490
491   xbt_dynar_foreach(mc_heap_comparison_ignore, cursor, current_region) {
492     mc_heap_ignore_region_t new_region = NULL;
493     new_region = xbt_new0(s_mc_heap_ignore_region_t, 1);
494     new_region->address = current_region->address;
495     new_region->size = current_region->size;
496     new_region->block = current_region->block;
497     new_region->fragment = current_region->fragment;
498     xbt_dynar_push(cpy, &new_region);
499   }
500
501   return cpy;
502
503 }
504
505 static void mc_free_snapshot_ignored_data_pvoid(void* data) {
506   mc_snapshot_ignored_data_t ignored_data = (mc_snapshot_ignored_data_t) data;
507   free(ignored_data->data);
508 }
509
510 static void MC_snapshot_handle_ignore(mc_snapshot_t snapshot)
511 {
512   snapshot->ignored_data = xbt_dynar_new(sizeof(s_mc_snapshot_ignored_data_t), mc_free_snapshot_ignored_data_pvoid);
513
514   // Copy the memory:
515   unsigned int cursor = 0;
516   mc_checkpoint_ignore_region_t region;
517   xbt_dynar_foreach (mc_checkpoint_ignore, cursor, region) {
518     s_mc_snapshot_ignored_data_t ignored_data;
519     ignored_data.start = region->addr;
520     ignored_data.size = region->size;
521     ignored_data.data = malloc(region->size);
522     memcpy(ignored_data.data, region->addr, region->size);
523     xbt_dynar_push(snapshot->ignored_data, &ignored_data);
524   }
525
526   // Zero the memory:
527   xbt_dynar_foreach (mc_checkpoint_ignore, cursor, region) {
528     memset(region->addr, 0, region->size);
529   }
530
531 }
532
533 static void MC_snapshot_ignore_restore(mc_snapshot_t snapshot)
534 {
535   unsigned int cursor = 0;
536   s_mc_snapshot_ignored_data_t ignored_data;
537   xbt_dynar_foreach (snapshot->ignored_data, cursor, ignored_data) {
538     memcpy(ignored_data.start, ignored_data.data, ignored_data.size);
539   }
540 }
541
542 mc_snapshot_t MC_take_snapshot(int num_state)
543 {
544
545   mc_snapshot_t snapshot = xbt_new0(s_mc_snapshot_t, 1);
546   snapshot->enabled_processes = xbt_dynar_new(sizeof(int), NULL);
547   smx_process_t process;
548   xbt_swag_foreach(process, simix_global->process_list) {
549     xbt_dynar_push_as(snapshot->enabled_processes, int, (int)process->pid);
550   }
551
552   MC_snapshot_handle_ignore(snapshot);
553
554   /* Save the std heap and the writable mapped pages of libsimgrid and binary */
555   MC_get_memory_regions(snapshot);
556
557   snapshot->to_ignore = MC_take_snapshot_ignore();
558
559   if (_sg_mc_visited > 0 || strcmp(_sg_mc_property_file, "")) {
560     snapshot->stacks =
561         MC_take_snapshot_stacks(&snapshot, snapshot->regions[0]->data);
562     if (_sg_mc_hash && snapshot->stacks != NULL) {
563       snapshot->hash = mc_hash_processes_state(num_state, snapshot->stacks);
564     } else {
565       snapshot->hash = 0;
566     }
567   } else {
568     snapshot->hash = 0;
569   }
570
571   // mprotect the region after zero-ing ignored parts:
572   /*size_t i;
573      for(i=0; i!=NB_REGIONS; ++i) {
574      mc_mem_region_t region = snapshot->regions[i];
575      mprotect(region->data, region->size, PROT_READ);
576      } */
577
578   MC_snapshot_ignore_restore(snapshot);
579
580   return snapshot;
581
582 }
583
584 void MC_restore_snapshot(mc_snapshot_t snapshot)
585 {
586   unsigned int i;
587   for (i = 0; i < NB_REGIONS; i++) {
588     // For privatized, variables we decided it was not necessary to take the snapshot:
589     if (snapshot->regions[i])
590       MC_region_restore(snapshot->regions[i]);
591   }
592
593   if (snapshot->privatization_regions) {
594     for (i = 0; i < SIMIX_process_count(); i++) {
595       if (snapshot->privatization_regions[i]) {
596         MC_region_restore(snapshot->privatization_regions[i]);
597       }
598     }
599     switch_data_segment(snapshot->privatization_index);
600   }
601
602   MC_snapshot_ignore_restore(snapshot);
603 }
604
605 void *mc_translate_address(uintptr_t addr, mc_snapshot_t snapshot)
606 {
607
608   // If not in a process state/clone:
609   if (!snapshot) {
610     return (uintptr_t *) addr;
611   }
612   // If it is in a snapshot:
613   for (size_t i = 0; i != NB_REGIONS; ++i) {
614     mc_mem_region_t region = snapshot->regions[i];
615     uintptr_t start = (uintptr_t) region->start_addr;
616     uintptr_t end = start + region->size;
617
618     // The address is in this region:
619     if (addr >= start && addr < end) {
620       uintptr_t offset = addr - start;
621       return (void *) ((uintptr_t) region->data + offset);
622     }
623
624   }
625
626   // It is not in a snapshot:
627   return (void *) addr;
628 }
629
630 uintptr_t mc_untranslate_address(void *addr, mc_snapshot_t snapshot)
631 {
632   if (!snapshot) {
633     return (uintptr_t) addr;
634   }
635
636   for (size_t i = 0; i != NB_REGIONS; ++i) {
637     mc_mem_region_t region = snapshot->regions[i];
638     if (addr >= region->data
639         && addr <= (void *) (((char *) region->data) + region->size)) {
640       size_t offset = (size_t) ((char *) addr - (char *) region->data);
641       return ((uintptr_t) region->start_addr) + offset;
642     }
643   }
644
645   return (uintptr_t) addr;
646 }
647
648 mc_snapshot_t SIMIX_pre_mc_snapshot(smx_simcall_t simcall)
649 {
650   return MC_take_snapshot(1);
651 }
652
653 void *MC_snapshot(void)
654 {
655   return simcall_mc_snapshot();
656 }