Logo AND Algorithmique Numérique Distribuée

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