1 /* Copyright (c) 2007-2014. The SimGrid Team.
2 * All rights reserved. */
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. */
10 #include "simgrid_config.h"
16 #include <elfutils/libdw.h>
19 #include "mc/datatypes.h"
21 #include "xbt/config.h"
22 #include "xbt/function_types.h"
23 #include "xbt/mmalloc.h"
24 #include "../simix/smx_private.h"
25 #include "../xbt/mmalloc/mmprivate.h"
26 #include "xbt/automaton.h"
29 #include "msg/datatypes.h"
30 #include "xbt/strbuff.h"
31 #include "xbt/parmap.h"
34 typedef struct s_dw_frame s_dw_frame_t, *dw_frame_t;
35 typedef struct s_mc_function_index_item s_mc_function_index_item_t, *mc_function_index_item_t;
37 /****************************** Snapshots ***********************************/
39 #define NB_REGIONS 3 /* binary data (data + BSS) (type = 2), libsimgrid data (data + BSS) (type = 1), std_heap (type = 0)*/
41 typedef struct s_mc_mem_region{
46 // Size of the data region:
48 // For per-page snapshots, this is an array to the number of
50 } s_mc_mem_region_t, *mc_mem_region_t;
52 static inline bool mc_region_contain(mc_mem_region_t region, void* p)
54 return p >= region->start_addr &&
55 p < (void*)((char*) region->start_addr + region->size);
60 * Some parts of the snapshot are ignored by zeroing them out: the real
61 * values is stored here.
63 typedef struct s_mc_snapshot_ignored_data {
67 } s_mc_snapshot_ignored_data_t, *mc_snapshot_ignored_data_t;
69 typedef struct s_mc_snapshot{
70 size_t heap_bytes_used;
71 mc_mem_region_t regions[NB_REGIONS];
72 xbt_dynar_t enabled_processes;
73 mc_mem_region_t* privatization_regions;
74 int privatization_index;
77 xbt_dynar_t to_ignore;
79 xbt_dynar_t ignored_data;
80 } s_mc_snapshot_t, *mc_snapshot_t;
82 mc_mem_region_t mc_get_snapshot_region(void* addr, mc_snapshot_t snapshot);
84 static inline mc_mem_region_t mc_get_region_hinted(void* addr, mc_snapshot_t snapshot, mc_mem_region_t region)
86 if (mc_region_contain(region, addr))
89 return mc_get_snapshot_region(addr, snapshot);
92 /** Information about a given stack frame
95 typedef struct s_mc_stack_frame {
96 /** Instruction pointer */
100 unw_word_t frame_base;
103 unw_cursor_t unw_cursor;
104 } s_mc_stack_frame_t, *mc_stack_frame_t;
106 typedef struct s_mc_snapshot_stack{
107 xbt_dynar_t local_variables;
108 xbt_dynar_t stack_frames; // mc_stack_frame_t
109 }s_mc_snapshot_stack_t, *mc_snapshot_stack_t;
111 typedef struct s_mc_global_t{
112 mc_snapshot_t snapshot;
116 int initial_communications_pattern_done;
117 int comm_deterministic;
118 int send_deterministic;
119 }s_mc_global_t, *mc_global_t;
121 typedef struct s_mc_checkpoint_ignore_region{
124 }s_mc_checkpoint_ignore_region_t, *mc_checkpoint_ignore_region_t;
128 inline static void* mc_snapshot_get_heap_end(mc_snapshot_t snapshot) {
130 xbt_die("snapshot is NULL");
131 xbt_mheap_t heap = (xbt_mheap_t)snapshot->regions[0]->data;
132 return heap->breakval;
135 mc_snapshot_t SIMIX_pre_mc_snapshot(smx_simcall_t simcall);
136 mc_snapshot_t MC_take_snapshot(int num_state);
137 void MC_restore_snapshot(mc_snapshot_t);
138 void MC_free_snapshot(mc_snapshot_t);
140 int mc_important_snapshot(mc_snapshot_t snapshot);
142 size_t* mc_take_page_snapshot_region(void* data, size_t page_count, uint64_t* pagemap, size_t* reference_pages);
143 void mc_free_page_snapshot_region(size_t* pagenos, size_t page_count);
144 void mc_restore_page_snapshot_region(mc_mem_region_t region, size_t page_count, uint64_t* pagemap, mc_mem_region_t reference_region);
146 mc_mem_region_t mc_region_new_sparse(int type, void *start_addr, size_t size, mc_mem_region_t ref_reg);
147 void mc_region_restore_sparse(mc_mem_region_t reg, mc_mem_region_t ref_reg);
148 void mc_softdirty_reset();
150 typedef struct s_mc_pages_store s_mc_pages_store_t, * mc_pages_store_t;
151 mc_pages_store_t mc_pages_store_new();
153 static inline bool mc_snapshot_region_linear(mc_mem_region_t region) {
154 return !region || !region->data;
157 void* mc_snapshot_read_region(void* addr, mc_mem_region_t region, void* target, size_t size);
158 void* mc_snapshot_read(void* addr, mc_snapshot_t snapshot, void* target, size_t size);
159 int mc_snapshot_memcp(
160 void* addr1, mc_snapshot_t snapshot1,
161 void* addr2, mc_snapshot_t snapshot2, size_t size);
163 static inline void* mc_snapshot_read_pointer(void* addr, mc_snapshot_t snapshot)
166 return *(void**) mc_snapshot_read(addr, snapshot, &res, sizeof(void*));
169 /** @brief State of the model-checker (global variables for the model checker)
171 * Each part of the state of the model chercker represented as a global
172 * variable prevents some sharing between snapshots and must be ignored.
173 * By moving as much state as possible in this structure allocated
174 * on the model-chercker heap, we avoid those issues.
176 typedef struct s_mc_model_checker {
177 // This is the parent snapshot of the current state:
178 mc_snapshot_t parent_snapshot;
179 mc_pages_store_t pages;
182 } s_mc_model_checker_t, *mc_model_checker_t;
184 extern mc_model_checker_t mc_model_checker;
186 void* mc_translate_address_region(uintptr_t addr, mc_mem_region_t region);
188 /** \brief Translate a pointer from process address space to snapshot address space
190 * The address space contains snapshot of the main/application memory:
191 * this function finds the address in a given snaphot for a given
192 * real/application address.
194 * For read only memory regions and other regions which are not int the
195 * snapshot, the address is not changed.
197 * \param addr Application address
198 * \param snapshot The snapshot of interest (if NULL no translation is done)
199 * \return Translated address in the snapshot address space
201 void* mc_translate_address(uintptr_t addr, mc_snapshot_t snapshot);
203 extern xbt_dynar_t mc_checkpoint_ignore;
205 /********************************* MC Global **********************************/
207 extern double *mc_time;
208 extern FILE *dot_output;
209 extern const char* colors[13];
210 extern xbt_parmap_t parmap;
212 extern int user_max_depth_reached;
214 int MC_deadlock_check(void);
215 void MC_replay(xbt_fifo_t stack, int start);
216 void MC_replay_liveness(xbt_fifo_t stack, int all_stack);
217 void MC_wait_for_requests(void);
218 void MC_show_deadlock(smx_simcall_t req);
219 void MC_show_stack_safety(xbt_fifo_t stack);
220 void MC_dump_stack_safety(xbt_fifo_t stack);
221 int SIMIX_pre_mc_random(smx_simcall_t simcall, int min, int max);
223 extern xbt_fifo_t mc_stack;
224 int get_search_interval(xbt_dynar_t list, void *ref, int *min, int *max);
227 /********************************* Requests ***********************************/
229 int MC_request_depend(smx_simcall_t req1, smx_simcall_t req2);
230 char* MC_request_to_string(smx_simcall_t req, int value);
231 unsigned int MC_request_testany_fail(smx_simcall_t req);
232 /*int MC_waitany_is_enabled_by_comm(smx_req_t req, unsigned int comm);*/
233 int MC_request_is_visible(smx_simcall_t req);
234 int MC_request_is_enabled(smx_simcall_t req);
235 int MC_request_is_enabled_by_idx(smx_simcall_t req, unsigned int idx);
236 int MC_process_is_enabled(smx_process_t process);
237 char *MC_request_get_dot_output(smx_simcall_t req, int value);
240 /******************************** States **************************************/
242 extern mc_global_t initial_global_state;
244 /* Possible exploration status of a process in a state */
246 MC_NOT_INTERLEAVE=0, /* Do not interleave (do not execute) */
247 MC_INTERLEAVE, /* Interleave the process (one or more request) */
248 MC_MORE_INTERLEAVE, /* Interleave twice the process (for mc_random simcall) */
249 MC_DONE /* Already interleaved */
250 } e_mc_process_state_t;
252 /* On every state, each process has an entry of the following type */
253 typedef struct mc_procstate{
254 e_mc_process_state_t state; /* Exploration control information */
255 unsigned int interleave_count; /* Number of times that the process was
257 } s_mc_procstate_t, *mc_procstate_t;
259 /* An exploration state is composed of: */
260 typedef struct mc_state {
261 unsigned long max_pid; /* Maximum pid at state's creation time */
262 mc_procstate_t proc_status; /* State's exploration status by process */
263 s_smx_action_t internal_comm; /* To be referenced by the internal_req */
264 s_smx_simcall_t internal_req; /* Internal translation of request */
265 s_smx_simcall_t executed_req; /* The executed request of the state */
266 int req_num; /* The request number (in the case of a
267 multi-request like waitany ) */
268 mc_snapshot_t system_state; /* Snapshot of system state */
270 } s_mc_state_t, *mc_state_t;
272 mc_state_t MC_state_new(void);
273 void MC_state_delete(mc_state_t state);
274 void MC_state_interleave_process(mc_state_t state, smx_process_t process);
275 unsigned int MC_state_interleave_size(mc_state_t state);
276 int MC_state_process_is_done(mc_state_t state, smx_process_t process);
277 void MC_state_set_executed_request(mc_state_t state, smx_simcall_t req, int value);
278 smx_simcall_t MC_state_get_executed_request(mc_state_t state, int *value);
279 smx_simcall_t MC_state_get_internal_request(mc_state_t state);
280 smx_simcall_t MC_state_get_request(mc_state_t state, int *value);
281 void MC_state_remove_interleave_process(mc_state_t state, smx_process_t process);
284 /****************************** Statistics ************************************/
286 typedef struct mc_stats {
287 unsigned long state_size;
288 unsigned long visited_states;
289 unsigned long visited_pairs;
290 unsigned long expanded_states;
291 unsigned long expanded_pairs;
292 unsigned long executed_transitions;
293 } s_mc_stats_t, *mc_stats_t;
295 extern mc_stats_t mc_stats;
297 void MC_print_statistics(mc_stats_t);
300 /********************************** MEMORY ******************************/
301 /* The possible memory modes for the modelchecker are standard and raw. */
302 /* Normally the system should operate in std, for switching to raw mode */
303 /* you must wrap the code between MC_SET_RAW_MODE and MC_UNSET_RAW_MODE */
305 extern void *std_heap;
306 extern void *mc_heap;
309 /* FIXME: Horrible hack! because the mmalloc library doesn't provide yet of */
310 /* an API to query about the status of a heap, we simply call mmstats and */
311 /* because I now how does structure looks like, then I redefine it here */
313 /* struct mstats { */
314 /* size_t bytes_total; /\* Total size of the heap. *\/ */
315 /* size_t chunks_used; /\* Chunks allocated by the user. *\/ */
316 /* size_t bytes_used; /\* Byte total of user-allocated chunks. *\/ */
317 /* size_t chunks_free; /\* Chunks in the free list. *\/ */
318 /* size_t bytes_free; /\* Byte total of chunks in the free list. *\/ */
321 #define MC_SET_MC_HEAP mmalloc_set_current_heap(mc_heap)
322 #define MC_SET_STD_HEAP mmalloc_set_current_heap(std_heap)
325 /******************************* MEMORY MAPPINGS ***************************/
326 /* These functions and data structures implements a binary interface for */
327 /* the proc maps ascii interface */
329 /* Each field is defined as documented in proc's manual page */
330 typedef struct s_map_region {
332 void *start_addr; /* Start address of the map */
333 void *end_addr; /* End address of the map */
334 int prot; /* Memory protection */
335 int flags; /* Additional memory flags */
336 void *offset; /* Offset in the file/whatever */
337 char dev_major; /* Major of the device */
338 char dev_minor; /* Minor of the device */
339 unsigned long inode; /* Inode in the device */
340 char *pathname; /* Path name of the mapped file */
344 typedef struct s_memory_map {
346 s_map_region_t *regions; /* Pointer to an array of regions */
347 int mapsize; /* Number of regions in the memory */
349 } s_memory_map_t, *memory_map_t;
352 void MC_init_memory_map_info(void);
353 memory_map_t MC_get_memory_map(void);
354 void MC_free_memory_map(memory_map_t map);
356 extern char *libsimgrid_path;
358 /********************************** Snapshot comparison **********************************/
360 typedef struct s_mc_comparison_times{
361 double nb_processes_comparison_time;
362 double bytes_used_comparison_time;
363 double stacks_sizes_comparison_time;
364 double binary_global_variables_comparison_time;
365 double libsimgrid_global_variables_comparison_time;
366 double heap_comparison_time;
367 double stacks_comparison_time;
368 }s_mc_comparison_times_t, *mc_comparison_times_t;
370 extern __thread mc_comparison_times_t mc_comp_times;
371 extern __thread double mc_snapshot_comparison_time;
373 int snapshot_compare(void *state1, void *state2);
374 int SIMIX_pre_mc_compare_snapshots(smx_simcall_t simcall, mc_snapshot_t s1, mc_snapshot_t s2);
375 void print_comparison_times(void);
380 /********************************** Safety verification **************************************/
388 extern e_mc_reduce_t mc_reduce_kind;
389 extern xbt_dict_t first_enabled_state;
391 void MC_pre_modelcheck_safety(void);
392 void MC_modelcheck_safety(void);
394 typedef struct s_mc_visited_state{
395 mc_snapshot_t system_state;
396 size_t heap_bytes_used;
399 int other_num; // dot_output for
400 }s_mc_visited_state_t, *mc_visited_state_t;
402 extern xbt_dynar_t visited_states;
403 int is_visited_state(void);
404 void visited_state_free(mc_visited_state_t state);
405 void visited_state_free_voidp(void *s);
407 /********************************** Liveness verification **************************************/
409 extern xbt_automaton_t _mc_property_automaton;
411 typedef struct s_mc_pair{
414 mc_state_t graph_state; /* System state included */
415 xbt_automaton_state_t automaton_state;
416 xbt_dynar_t atomic_propositions;
418 }s_mc_pair_t, *mc_pair_t;
420 typedef struct s_mc_visited_pair{
422 int other_num; /* Dot output for */
424 mc_state_t graph_state; /* System state included */
425 xbt_automaton_state_t automaton_state;
426 xbt_dynar_t atomic_propositions;
427 size_t heap_bytes_used;
429 int acceptance_removed;
431 }s_mc_visited_pair_t, *mc_visited_pair_t;
433 mc_pair_t MC_pair_new(void);
434 void MC_pair_delete(mc_pair_t);
435 void mc_pair_free_voidp(void *p);
436 mc_visited_pair_t MC_visited_pair_new(int pair_num, xbt_automaton_state_t automaton_state, xbt_dynar_t atomic_propositions);
437 void MC_visited_pair_delete(mc_visited_pair_t p);
439 void MC_pre_modelcheck_liveness(void);
440 void MC_modelcheck_liveness(void);
441 void MC_show_stack_liveness(xbt_fifo_t stack);
442 void MC_dump_stack_liveness(xbt_fifo_t stack);
444 extern xbt_dynar_t visited_pairs;
445 int is_visited_pair(mc_visited_pair_t pair, int pair_num, xbt_automaton_state_t automaton_state, xbt_dynar_t atomic_propositions);
448 /********************************** Variables with DWARF **********************************/
450 #define MC_OBJECT_INFO_EXECUTABLE 1
452 struct s_mc_object_info {
455 char *start_exec, *end_exec; // Executable segment
456 char *start_rw, *end_rw; // Read-write segment
457 char *start_ro, *end_ro; // read-only segment
458 xbt_dict_t subprograms; // xbt_dict_t<origin as hexadecimal string, dw_frame_t>
459 xbt_dynar_t global_variables; // xbt_dynar_t<dw_variable_t>
460 xbt_dict_t types; // xbt_dict_t<origin as hexadecimal string, dw_type_t>
461 xbt_dict_t full_types_by_name; // xbt_dict_t<name, dw_type_t> (full defined type only)
463 // Here we sort the minimal information for an efficient (and cache-efficient)
464 // lookup of a function given an instruction pointer.
465 // The entries are sorted by low_pc and a binary search can be used to look them up.
466 xbt_dynar_t functions_index;
469 mc_object_info_t MC_new_object_info(void);
470 mc_object_info_t MC_find_object_info(memory_map_t maps, char* name, int executable);
471 void MC_free_object_info(mc_object_info_t* p);
473 void MC_dwarf_get_variables(mc_object_info_t info);
474 void MC_dwarf_get_variables_libdw(mc_object_info_t info);
475 const char* MC_dwarf_attrname(int attr);
476 const char* MC_dwarf_tagname(int tag);
478 dw_frame_t MC_find_function_by_ip(void* ip);
479 mc_object_info_t MC_ip_find_object_info(void* ip);
481 extern mc_object_info_t mc_libsimgrid_info;
482 extern mc_object_info_t mc_binary_info;
483 extern mc_object_info_t mc_object_infos[2];
484 extern size_t mc_object_infos_size;
486 void MC_find_object_address(memory_map_t maps, mc_object_info_t result);
487 void MC_post_process_types(mc_object_info_t info);
488 void MC_post_process_object_info(mc_object_info_t info);
492 /** \brief a DWARF expression with optional validity contraints */
493 typedef struct s_mc_expression {
496 // Optional validity:
497 void* lowpc, *highpc;
498 } s_mc_expression_t, *mc_expression_t;
500 /** A location list (list of location expressions) */
501 typedef struct s_mc_location_list {
503 mc_expression_t locations;
504 } s_mc_location_list_t, *mc_location_list_t;
506 uintptr_t mc_dwarf_resolve_location(mc_expression_t expression, mc_object_info_t object_info, unw_cursor_t* c, void* frame_pointer_address, mc_snapshot_t snapshot);
507 uintptr_t mc_dwarf_resolve_locations(mc_location_list_t locations, mc_object_info_t object_info, unw_cursor_t* c, void* frame_pointer_address, mc_snapshot_t snapshot);
509 void mc_dwarf_expression_clear(mc_expression_t expression);
510 void mc_dwarf_expression_init(mc_expression_t expression, size_t len, Dwarf_Op* ops);
512 void mc_dwarf_location_list_clear(mc_location_list_t list);
514 void mc_dwarf_location_list_init_from_expression(mc_location_list_t target, size_t len, Dwarf_Op* ops);
515 void mc_dwarf_location_list_init(mc_location_list_t target, mc_object_info_t info, Dwarf_Die* die, Dwarf_Attribute* attr);
517 // ***** Variables and functions
521 Dwarf_Off id; /* Offset in the section (in hexadecimal form) */
522 char *name; /* Name of the type */
523 int byte_size; /* Size in bytes */
524 int element_count; /* Number of elements for array type */
525 char *dw_type_id; /* DW_AT_type id */
526 xbt_dynar_t members; /* if DW_TAG_structure_type, DW_TAG_class_type, DW_TAG_union_type*/
529 // Location (for members) is either of:
530 struct s_mc_expression location;
533 dw_type_t subtype; // DW_AT_type
534 dw_type_t full_type; // The same (but more complete) type
537 void* mc_member_resolve(const void* base, dw_type_t type, dw_type_t member, mc_snapshot_t snapshot);
539 typedef struct s_dw_variable{
540 Dwarf_Off dwarf_offset; /* Global offset of the field. */
547 s_mc_location_list_t locations;
551 mc_object_info_t object_info;
553 }s_dw_variable_t, *dw_variable_t;
560 s_mc_location_list_t frame_base;
561 xbt_dynar_t /* <dw_variable_t> */ variables; /* Cannot use dict, there may be several variables with the same name (in different lexical blocks)*/
562 unsigned long int id; /* DWARF offset of the subprogram */
563 xbt_dynar_t /* <dw_frame_t> */ scopes;
564 Dwarf_Off abstract_origin_id;
565 mc_object_info_t object_info;
568 struct s_mc_function_index_item {
569 void* low_pc, *high_pc;
573 void mc_frame_free(dw_frame_t freme);
575 void dw_type_free(dw_type_t t);
576 void dw_variable_free(dw_variable_t v);
577 void dw_variable_free_voidp(void *t);
579 void MC_dwarf_register_global_variable(mc_object_info_t info, dw_variable_t variable);
580 void MC_register_variable(mc_object_info_t info, dw_frame_t frame, dw_variable_t variable);
581 void MC_dwarf_register_non_global_variable(mc_object_info_t info, dw_frame_t frame, dw_variable_t variable);
582 void MC_dwarf_register_variable(mc_object_info_t info, dw_frame_t frame, dw_variable_t variable);
584 /** Find the DWARF offset for this ELF object
586 * An offset is applied to address found in DWARF:
589 * <li>for an executable obejct, addresses are virtual address
590 * (there is no offset) i.e. \f$\text{virtual address} = \{dwarf address}\f$;</li>
591 * <li>for a shared object, the addreses are offset from the begining
592 * of the shared object (the base address of the mapped shared
593 * object must be used as offset
594 * i.e. \f$\text{virtual address} = \text{shared object base address}
595 * + \text{dwarf address}\f$.</li>
598 void* MC_object_base_address(mc_object_info_t info);
600 /********************************** DWARF **********************************/
602 #define MC_EXPRESSION_STACK_SIZE 64
604 #define MC_EXPRESSION_OK 0
605 #define MC_EXPRESSION_E_UNSUPPORTED_OPERATION 1
606 #define MC_EXPRESSION_E_STACK_OVERFLOW 2
607 #define MC_EXPRESSION_E_STACK_UNDERFLOW 3
608 #define MC_EXPRESSION_E_MISSING_STACK_CONTEXT 4
609 #define MC_EXPRESSION_E_MISSING_FRAME_BASE 5
610 #define MC_EXPRESSION_E_NO_BASE_ADDRESS 6
612 typedef struct s_mc_expression_state {
613 uintptr_t stack[MC_EXPRESSION_STACK_SIZE];
616 unw_cursor_t* cursor;
618 mc_snapshot_t snapshot;
619 mc_object_info_t object_info;
620 } s_mc_expression_state_t, *mc_expression_state_t;
622 int mc_dwarf_execute_expression(size_t n, const Dwarf_Op* ops, mc_expression_state_t state);
624 void* mc_find_frame_base(dw_frame_t frame, mc_object_info_t object_info, unw_cursor_t* unw_cursor);
626 /********************************** Miscellaneous **********************************/
628 typedef struct s_local_variable{
629 dw_frame_t subprogram;
635 }s_local_variable_t, *local_variable_t;
637 /********************************* Communications pattern ***************************/
639 typedef struct s_mc_comm_pattern{
642 e_smx_comm_type_t type;
643 unsigned long src_proc;
644 unsigned long dst_proc;
645 const char *src_host;
646 const char *dst_host;
650 }s_mc_comm_pattern_t, *mc_comm_pattern_t;
652 extern xbt_dynar_t communications_pattern;
653 extern xbt_dynar_t incomplete_communications_pattern;
655 void get_comm_pattern(xbt_dynar_t communications_pattern, smx_simcall_t request, int call);
656 void complete_comm_pattern(xbt_dynar_t list, smx_action_t comm);
657 void MC_pre_modelcheck_comm_determinism(void);
658 void MC_modelcheck_comm_determinism(void);
660 /* *********** Sets *********** */
662 typedef struct s_mc_address_set *mc_address_set_t;
664 mc_address_set_t mc_address_set_new();
665 void mc_address_set_free(mc_address_set_t* p);
666 void mc_address_add(mc_address_set_t p, const void* value);
667 bool mc_address_test(mc_address_set_t p, const void* value);
669 /* *********** Hash *********** */
671 /** \brief Hash the current state
672 * \param num_state number of states
673 * \param stacks stacks (mc_snapshot_stak_t) used fot the stack unwinding informations
674 * \result resulting hash
676 uint64_t mc_hash_processes_state(int num_state, xbt_dynar_t stacks);