Logo AND Algorithmique Numérique Distribuée

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