1b2ed49a5SDavid van Moolenbroek #ifndef ST_STATE_TRANSFER_H 2b2ed49a5SDavid van Moolenbroek #define ST_STATE_TRANSFER_H 3b2ed49a5SDavid van Moolenbroek 4b2ed49a5SDavid van Moolenbroek 5b2ed49a5SDavid van Moolenbroek #include <magic.h> 6b2ed49a5SDavid van Moolenbroek #include <magic_mem.h> 7b2ed49a5SDavid van Moolenbroek #include <magic_analysis.h> 8b2ed49a5SDavid van Moolenbroek #include <magic_asr.h> 9b2ed49a5SDavid van Moolenbroek #include <stdarg.h> 10b2ed49a5SDavid van Moolenbroek #include <stdint.h> 11b2ed49a5SDavid van Moolenbroek 12b2ed49a5SDavid van Moolenbroek #ifdef __MINIX 13b2ed49a5SDavid van Moolenbroek #include <minix/com.h> 14b2ed49a5SDavid van Moolenbroek #include <minix/sef.h> 15b2ed49a5SDavid van Moolenbroek #endif 16b2ed49a5SDavid van Moolenbroek 17b2ed49a5SDavid van Moolenbroek /* 18b2ed49a5SDavid van Moolenbroek * Used for debugging: Iterate the type transformations X times. 19b2ed49a5SDavid van Moolenbroek */ 20b2ed49a5SDavid van Moolenbroek #define MAGIC_ST_TYPE_TRANS_ITERATIONS 1 21b2ed49a5SDavid van Moolenbroek 22b2ed49a5SDavid van Moolenbroek #include <st/os_callback.h> 23b2ed49a5SDavid van Moolenbroek #include <st/special.h> 24b2ed49a5SDavid van Moolenbroek 25b2ed49a5SDavid van Moolenbroek #define ST_LU_ASR (1 << 1) 26b2ed49a5SDavid van Moolenbroek #define ST_LU_NOMMAP (1 << 2) 27b2ed49a5SDavid van Moolenbroek 28b2ed49a5SDavid van Moolenbroek /* 29b2ed49a5SDavid van Moolenbroek * Type definitions. These need to be OS independent. 30b2ed49a5SDavid van Moolenbroek */ 31b2ed49a5SDavid van Moolenbroek typedef struct _st_init_info_t { 32b2ed49a5SDavid van Moolenbroek int flags; 33b2ed49a5SDavid van Moolenbroek void *init_buff_start; 34b2ed49a5SDavid van Moolenbroek void *init_buff_cleanup_start; 35b2ed49a5SDavid van Moolenbroek size_t init_buff_len; 36b2ed49a5SDavid van Moolenbroek struct st_cbs_os_t st_cbs_os; 37b2ed49a5SDavid van Moolenbroek void *info_opaque; 38b2ed49a5SDavid van Moolenbroek } st_init_info_t; 39b2ed49a5SDavid van Moolenbroek 40b2ed49a5SDavid van Moolenbroek typedef struct st_alloc_pages { 41b2ed49a5SDavid van Moolenbroek int num_pages; 42b2ed49a5SDavid van Moolenbroek void *virt_addr; 43*3c71801eSrlfnb uint32_t phys_addr; 44b2ed49a5SDavid van Moolenbroek struct st_alloc_pages *previous; 45b2ed49a5SDavid van Moolenbroek } st_alloc_pages; 46b2ed49a5SDavid van Moolenbroek 47b2ed49a5SDavid van Moolenbroek #include <st/callback.h> 48b2ed49a5SDavid van Moolenbroek 49b2ed49a5SDavid van Moolenbroek /* struct for holding a mapping between pointers */ 50b2ed49a5SDavid van Moolenbroek typedef struct { 51b2ed49a5SDavid van Moolenbroek void *counterpart; 52b2ed49a5SDavid van Moolenbroek } st_ptr_mapping; 53b2ed49a5SDavid van Moolenbroek 54b2ed49a5SDavid van Moolenbroek /* struct for holding arrays of local counterparts to cached variables */ 55b2ed49a5SDavid van Moolenbroek typedef struct { 56b2ed49a5SDavid van Moolenbroek st_ptr_mapping *functions; 57b2ed49a5SDavid van Moolenbroek int functions_size; 58b2ed49a5SDavid van Moolenbroek st_ptr_mapping *types; 59b2ed49a5SDavid van Moolenbroek st_ptr_mapping *ptr_types; 60b2ed49a5SDavid van Moolenbroek int types_size; 61b2ed49a5SDavid van Moolenbroek st_ptr_mapping *sentries; 62b2ed49a5SDavid van Moolenbroek st_ptr_mapping *sentries_data; 63b2ed49a5SDavid van Moolenbroek int sentries_size; 64b2ed49a5SDavid van Moolenbroek } st_counterparts_t; 65b2ed49a5SDavid van Moolenbroek 66b2ed49a5SDavid van Moolenbroek #define ST_STATE_CHECKING_DEFAULT_MAX_CYCLES LONG_MAX 67b2ed49a5SDavid van Moolenbroek #define ST_STATE_CHECKING_DEFAULT_MAX_VIOLATIONS 50 68b2ed49a5SDavid van Moolenbroek 69b2ed49a5SDavid van Moolenbroek #define ST_SEL_ANALYZE_FLAGS (MAGIC_SEL_ANALYZE_ALL & (~MAGIC_SEL_ANALYZE_OUT_OF_BAND)) 70b2ed49a5SDavid van Moolenbroek 71b2ed49a5SDavid van Moolenbroek #define ST_MAX_COMPATIBLE_TYPES 32 72b2ed49a5SDavid van Moolenbroek 73b2ed49a5SDavid van Moolenbroek /* Fields of _magic_vars_t that need to be zeroed out after transferring. */ 74b2ed49a5SDavid van Moolenbroek #define ST_MAGIC_VARS_PTR_CLEAR_LIST \ 75b2ed49a5SDavid van Moolenbroek __X(first_sodesc), __X(last_sodesc), \ 76b2ed49a5SDavid van Moolenbroek __X(first_dsodesc), __X(last_dsodesc), \ 77b2ed49a5SDavid van Moolenbroek __X(sentry_rl_buff), __X(sentry_rl_index), \ 78b2ed49a5SDavid van Moolenbroek __X(sentry_hash_buff), __X(sentry_hash_head), \ 79b2ed49a5SDavid van Moolenbroek __X(function_hash_buff), __X(function_hash_head) 80b2ed49a5SDavid van Moolenbroek 81b2ed49a5SDavid van Moolenbroek /* Public utility functions. */ 82b2ed49a5SDavid van Moolenbroek PUBLIC int st_strcmp_wildcard(const char *with_wildcard, const char *without_wildcard); 83b2ed49a5SDavid van Moolenbroek PUBLIC void st_cb_print(int level, const char *msg, _magic_selement_t *selement, _magic_sel_analyzed_t *sel_analyzed, _magic_sel_stats_t *sel_stats, struct st_cb_info *cb_info); 84b2ed49a5SDavid van Moolenbroek PUBLIC void st_map_str_sentries(struct _magic_sentry **cached_sentry_ptr, struct _magic_sentry **local_sentry_ptr); 85b2ed49a5SDavid van Moolenbroek PUBLIC void st_map_sentries(struct _magic_sentry **cached_sentry_ptr, struct _magic_sentry **local_sentry_ptr); 86b2ed49a5SDavid van Moolenbroek PUBLIC void st_lookup_sentry_pair(struct _magic_sentry **cached_sentry_ptr, struct _magic_sentry **local_sentry_ptr); 87b2ed49a5SDavid van Moolenbroek PUBLIC void st_add_sentry_pair(struct _magic_sentry *cached_sentry, struct _magic_sentry *local_sentry); 88b2ed49a5SDavid van Moolenbroek PUBLIC int st_add_sentry_pair_alloc_by_dsindex(st_init_info_t *info, struct _magic_sentry *cached_sentry, struct _magic_dsindex *local_dsindex, int num_elements, const union __alloc_flags *p_alloc_flags); 89b2ed49a5SDavid van Moolenbroek PUBLIC void st_map_functions(struct _magic_function **cached_function_ptr, struct _magic_function **local_function_ptr); 90b2ed49a5SDavid van Moolenbroek PUBLIC void st_lookup_function_pair(struct _magic_function **cached_function_ptr, struct _magic_function **local_function_ptr); 91b2ed49a5SDavid van Moolenbroek PUBLIC void st_add_function_pair(struct _magic_function *cached_function, struct _magic_function *local_function); 92b2ed49a5SDavid van Moolenbroek PUBLIC int st_sentry_equals(struct _magic_sentry *cached_sentry, struct _magic_sentry *local_sentry); 93b2ed49a5SDavid van Moolenbroek PUBLIC int st_function_equals(struct _magic_function *cached_function, struct _magic_function *local_function); 94b2ed49a5SDavid van Moolenbroek PUBLIC void st_print_sentry_diff(st_init_info_t *info, struct _magic_sentry *cached_sentry, struct _magic_sentry *local_sentry, int raw_diff, int print_changed); 95b2ed49a5SDavid van Moolenbroek PUBLIC void st_print_function_diff(st_init_info_t *info, struct _magic_function *cached_function, struct _magic_function *local_function, int raw_diff, int print_changed); 96b2ed49a5SDavid van Moolenbroek PUBLIC void st_cb_selement_type_cast(const struct _magic_type* new_selement_type, const struct _magic_type* new_local_selement_type, _magic_selement_t *selement, _magic_sel_analyzed_t *sel_analyzed, _magic_sel_stats_t *sel_stats, struct st_cb_info *cb_info); 97b2ed49a5SDavid van Moolenbroek PUBLIC void st_map_local_selement_from_child_num(_magic_selement_t *local_selement, struct st_cb_info *cb_info, int child_num); 98b2ed49a5SDavid van Moolenbroek 99b2ed49a5SDavid van Moolenbroek /* Stack refs save/restore. */ 100b2ed49a5SDavid van Moolenbroek PUBLIC void st_stack_refs_save_restore(char *stack_buff, int is_save); 101b2ed49a5SDavid van Moolenbroek 102b2ed49a5SDavid van Moolenbroek #define st_stack_refs_save(B) st_stack_refs_save_restore(B,1) 103b2ed49a5SDavid van Moolenbroek #define st_stack_refs_restore(B) st_stack_refs_save_restore(B,0) 104b2ed49a5SDavid van Moolenbroek 105b2ed49a5SDavid van Moolenbroek #define ST_STACK_REFS_BUFF_SIZE (sizeof(struct st_stack_refs_buff)) 106b2ed49a5SDavid van Moolenbroek 107b2ed49a5SDavid van Moolenbroek /* 108b2ed49a5SDavid van Moolenbroek * The stack pointers to the environment and arguments differ from MINIX 109b2ed49a5SDavid van Moolenbroek * to Linux. 110b2ed49a5SDavid van Moolenbroek */ 111b2ed49a5SDavid van Moolenbroek #if defined(__MINIX) 112b2ed49a5SDavid van Moolenbroek #define ST_STACK_REFS_INT_NUM 3 113b2ed49a5SDavid van Moolenbroek #define ST_STACK_REFS_INT_LIST \ 114b2ed49a5SDavid van Moolenbroek __X(environ), __X(env_argv), __X(env_argc) 115b2ed49a5SDavid van Moolenbroek #define ST_STACK_REFS_CUSTOM_NUM 0 116b2ed49a5SDavid van Moolenbroek #define ST_STACK_REFS_CUSTOM_LIST 117b2ed49a5SDavid van Moolenbroek 118b2ed49a5SDavid van Moolenbroek #else 119b2ed49a5SDavid van Moolenbroek /* 120b2ed49a5SDavid van Moolenbroek * TODO: Complete the list of Linux stack pointer aliases. 121b2ed49a5SDavid van Moolenbroek */ 122b2ed49a5SDavid van Moolenbroek #define ST_STACK_REFS_INT_NUM 3 123b2ed49a5SDavid van Moolenbroek #define ST_STACK_REFS_INT_LIST __X(_environ), __X(__progname), \ 124b2ed49a5SDavid van Moolenbroek __X(__progname_full) 125b2ed49a5SDavid van Moolenbroek 126b2ed49a5SDavid van Moolenbroek #ifndef __USE_GNU 127b2ed49a5SDavid van Moolenbroek extern char *program_invocation_name, *program_invocation_short_name; 128b2ed49a5SDavid van Moolenbroek extern char **environ; 129b2ed49a5SDavid van Moolenbroek #endif 130b2ed49a5SDavid van Moolenbroek #define ST_STACK_REFS_CUSTOM_NUM 4 131b2ed49a5SDavid van Moolenbroek #define ST_STACK_REFS_CUSTOM_LIST __X(environ), __X(__environ), \ 132b2ed49a5SDavid van Moolenbroek __X(program_invocation_name), __X(program_invocation_short_name) 133b2ed49a5SDavid van Moolenbroek 134b2ed49a5SDavid van Moolenbroek #endif 135b2ed49a5SDavid van Moolenbroek 136b2ed49a5SDavid van Moolenbroek #define ST_STACK_REFS_NUM (ST_STACK_REFS_INT_NUM + ST_STACK_REFS_CUSTOM_NUM) 137b2ed49a5SDavid van Moolenbroek 138b2ed49a5SDavid van Moolenbroek struct st_stack_refs_buff { 139b2ed49a5SDavid van Moolenbroek struct _magic_dsentry *first_stack_dsentry; 140b2ed49a5SDavid van Moolenbroek struct _magic_dsentry *last_stack_dsentry; 141b2ed49a5SDavid van Moolenbroek struct _magic_obdsentry first_stack_obdsentry_buff; 142b2ed49a5SDavid van Moolenbroek void* stack_range[2]; 143b2ed49a5SDavid van Moolenbroek int stack_int_refs[ST_STACK_REFS_NUM]; 144b2ed49a5SDavid van Moolenbroek }; 145b2ed49a5SDavid van Moolenbroek 146b2ed49a5SDavid van Moolenbroek /* Public functions for metadata transfer. */ 147b2ed49a5SDavid van Moolenbroek PUBLIC int st_transfer_metadata_types(st_init_info_t *info, 148b2ed49a5SDavid van Moolenbroek struct _magic_vars_t *cached_magic_vars, 149b2ed49a5SDavid van Moolenbroek struct _magic_vars_t *remote_magic_vars, 150b2ed49a5SDavid van Moolenbroek st_counterparts_t *counterparts); 151b2ed49a5SDavid van Moolenbroek PUBLIC int st_transfer_metadata_dsentries(st_init_info_t *info, 152b2ed49a5SDavid van Moolenbroek struct _magic_vars_t *cached_magic_vars, 153b2ed49a5SDavid van Moolenbroek struct _magic_vars_t *remote_magic_vars, st_counterparts_t *counterparts, 154b2ed49a5SDavid van Moolenbroek size_t *max_buff_sz, int *dsentries_num); 155b2ed49a5SDavid van Moolenbroek 156b2ed49a5SDavid van Moolenbroek /* Public functions for state transfer. */ 157b2ed49a5SDavid van Moolenbroek PUBLIC int st_state_transfer(st_init_info_t *info); 158b2ed49a5SDavid van Moolenbroek PUBLIC void st_msync_all_shm_dsentries(void); 159b2ed49a5SDavid van Moolenbroek 160b2ed49a5SDavid van Moolenbroek PUBLIC int st_init(st_init_info_t *info); 161b2ed49a5SDavid van Moolenbroek PUBLIC void st_set_status_defaults(st_init_info_t *info); 162b2ed49a5SDavid van Moolenbroek PUBLIC int st_data_transfer(st_init_info_t *info); 163b2ed49a5SDavid van Moolenbroek PUBLIC void st_cleanup(st_init_info_t *info); 164b2ed49a5SDavid van Moolenbroek 165b2ed49a5SDavid van Moolenbroek PUBLIC void st_set_unpaired_types_ratios(float unpaired_types_ratio, float unpaired_struct_types_ratio); 166b2ed49a5SDavid van Moolenbroek 167b2ed49a5SDavid van Moolenbroek PUBLIC struct _magic_sentry* st_cached_to_remote_sentry(st_init_info_t *info, struct _magic_sentry *cached_sentry); 168b2ed49a5SDavid van Moolenbroek 169b2ed49a5SDavid van Moolenbroek PUBLIC void st_print_sentries_diff(st_init_info_t *info, int raw_diff, int print_changed); 170b2ed49a5SDavid van Moolenbroek PUBLIC void st_print_dsentries_diff(st_init_info_t *info, int raw_diff, int print_changed); 171b2ed49a5SDavid van Moolenbroek PUBLIC void st_print_functions_diff(st_init_info_t *info, int raw_diff, int print_changed); 172b2ed49a5SDavid van Moolenbroek PUBLIC void st_print_state_diff(st_init_info_t *info, int raw_diff, int print_changed); 173b2ed49a5SDavid van Moolenbroek 174b2ed49a5SDavid van Moolenbroek PUBLIC void st_set_status_by_state_flags(int status_flags, int status_op, int match_state_flags, int skip_state_flags); 175b2ed49a5SDavid van Moolenbroek PUBLIC int st_set_status_by_function_ids(int status_flags, int status_op, unsigned long *ids); 176b2ed49a5SDavid van Moolenbroek PUBLIC int st_set_status_by_sentry_ids(int status_flags, int status_op, unsigned long *ids); 177b2ed49a5SDavid van Moolenbroek PUBLIC int st_set_status_by_names(int status_flags, int status_op, const char **parent_names, const char **names, _magic_id_t *dsentry_site_ids); 178b2ed49a5SDavid van Moolenbroek PUBLIC int st_set_status_by_local_addrs(int status_flags, int status_op, void **addrs); 179b2ed49a5SDavid van Moolenbroek PUBLIC void st_set_status_by_sentry(int status_flags, int status_op, void *cached_sentry); 180b2ed49a5SDavid van Moolenbroek PUBLIC void st_set_status_by_function(int status_flags, int status_op, void *cached_function); 181b2ed49a5SDavid van Moolenbroek PUBLIC int st_set_status_by_function_id(int status_flags, int status_op, unsigned long id); 182b2ed49a5SDavid van Moolenbroek PUBLIC int st_set_status_by_sentry_id(int status_flags, int status_op, unsigned long id); 183b2ed49a5SDavid van Moolenbroek PUBLIC int st_set_status_by_name(int status_flags, int status_op, const char *parent_name, const char *name, _magic_id_t dsentry_site_id); 184b2ed49a5SDavid van Moolenbroek PUBLIC int st_set_status_by_local_addr(int status_flags, int status_op, void *addr); 185b2ed49a5SDavid van Moolenbroek 186b2ed49a5SDavid van Moolenbroek PUBLIC int st_pair_by_function_ids(unsigned long *cached_ids, unsigned long *local_ids, int status_flags, int status_op); 187b2ed49a5SDavid van Moolenbroek PUBLIC int st_pair_by_sentry_ids(unsigned long *cached_ids, unsigned long *local_ids, int status_flags, int status_op); 188b2ed49a5SDavid van Moolenbroek PUBLIC int st_pair_by_names(char **cached_parent_names, char **cached_names, char **local_parent_names, char **local_names, _magic_id_t *dsentry_site_ids, int status_flags, int status_op); 189b2ed49a5SDavid van Moolenbroek PUBLIC void st_pair_by_sentry(void *cached_sentry, void *local_sentry, int status_flags, int status_op); 190b2ed49a5SDavid van Moolenbroek PUBLIC void st_pair_by_function(void *cached_function, void* local_function, int status_flags, int status_op); 191b2ed49a5SDavid van Moolenbroek PUBLIC int st_pair_alloc_by_dsindex(st_init_info_t *info, void *cached_sentry, void *local_dsindex, int num_elements, const union __alloc_flags *p_alloc_flags, int status_flags, int status_op); 192b2ed49a5SDavid van Moolenbroek PUBLIC int st_pair_by_function_id(unsigned long cached_id, unsigned long local_id, int status_flags, int status_op); 193b2ed49a5SDavid van Moolenbroek PUBLIC int st_pair_by_sentry_id(unsigned long cached_id, unsigned long local_id, int status_flags, int status_op); 194b2ed49a5SDavid van Moolenbroek PUBLIC int st_pair_by_name(char *cached_parent_name, char *cached_name, char *local_parent_name, char *local_name, _magic_id_t dsentry_site_id, int status_flags, int status_op); 195b2ed49a5SDavid van Moolenbroek PUBLIC int st_pair_by_alloc_name(st_init_info_t *info, char *cached_parent_name, char *cached_name, _magic_id_t cached_dsentry_site_id, char *local_parent_name, char *local_name, _magic_id_t local_dsentry_site_id, int num_elements, const union __alloc_flags *p_alloc_flags, int status_flags, int status_op); 196b2ed49a5SDavid van Moolenbroek PUBLIC int st_pair_by_alloc_name_policies(st_init_info_t *info, char *cached_parent_name, char *cached_name, _magic_id_t cached_dsentry_site_id, char *local_parent_name, char *local_name, _magic_id_t local_dsentry_site_id, int num_elements, const union __alloc_flags *p_alloc_flags, int alloc_policies, int status_flags, int status_op); 197b2ed49a5SDavid van Moolenbroek 198b2ed49a5SDavid van Moolenbroek /* Public functions for state checking. */ 199b2ed49a5SDavid van Moolenbroek PUBLIC int st_do_state_cleanup(void); 200b2ed49a5SDavid van Moolenbroek PUBLIC int st_do_state_checking(void); 201b2ed49a5SDavid van Moolenbroek PUBLIC int st_state_checking_before_receive_is_enabled(void); 202b2ed49a5SDavid van Moolenbroek PUBLIC int st_state_checking_before_receive_set_enabled(int enabled, int max_cycles, int max_violations); 203b2ed49a5SDavid van Moolenbroek 204b2ed49a5SDavid van Moolenbroek /* State transfer status flags and operations. */ 205b2ed49a5SDavid van Moolenbroek #define ST_NEEDS_TRANSFER 0x01 206b2ed49a5SDavid van Moolenbroek #define ST_TRANSFER_DONE 0x02 207b2ed49a5SDavid van Moolenbroek #define ST_ON_PTRXFER_CASCADE 0x04 208b2ed49a5SDavid van Moolenbroek #define ST_ON_PTRXFER_SET_NULL 0x08 209b2ed49a5SDavid van Moolenbroek #define ST_ON_PTRXFER_SET_DEFAULT 0x10 210b2ed49a5SDavid van Moolenbroek #define ST_ON_PTRXFER_SKIP 0x20 211b2ed49a5SDavid van Moolenbroek 212b2ed49a5SDavid van Moolenbroek #define ST_OP_NONE 0 213b2ed49a5SDavid van Moolenbroek #define ST_OP_ADD 1 214b2ed49a5SDavid van Moolenbroek #define ST_OP_DEL 2 215b2ed49a5SDavid van Moolenbroek #define ST_OP_SET 3 216b2ed49a5SDavid van Moolenbroek #define ST_OP_CLEAR 4 217b2ed49a5SDavid van Moolenbroek 218b2ed49a5SDavid van Moolenbroek /* State transfer policies. */ 219b2ed49a5SDavid van Moolenbroek #define ST_DEFAULT_TRANSFER_NONE 0x00000001 220b2ed49a5SDavid van Moolenbroek #define ST_DEFAULT_ALLOC_CASCADE_XFER 0x00000002 221b2ed49a5SDavid van Moolenbroek #define ST_DEFAULT_SKIP_STACK 0x00000004 222b2ed49a5SDavid van Moolenbroek #define ST_DEFAULT_SKIP_LIB_STATE 0x00000008 223b2ed49a5SDavid van Moolenbroek #define ST_TRANSFER_DIRTY_ONLY 0x00000010 224b2ed49a5SDavid van Moolenbroek #define ST_IXFER_UNCAUGHT_UNIONS 0x00000020 225b2ed49a5SDavid van Moolenbroek #define ST_REPORT_UNCAUGHT_UNIONS 0x00000040 226b2ed49a5SDavid van Moolenbroek #define ST_REPORT_NONXFERRED_ALLOCS 0x00000080 227b2ed49a5SDavid van Moolenbroek #define ST_REPORT_NONXFERRED_UNPAIRED_ALLOCS 0x00000100 228b2ed49a5SDavid van Moolenbroek #define ST_REPORT_UNPAIRED_SENTRIES 0x00000200 229b2ed49a5SDavid van Moolenbroek #define ST_REPORT_UNPAIRED_DSENTRIES 0x00000400 230b2ed49a5SDavid van Moolenbroek #define ST_REPORT_UNPAIRED_STRINGS 0x00000800 231b2ed49a5SDavid van Moolenbroek #define ST_REPORT_UNPAIRED_SELEMENTS 0x00001000 232b2ed49a5SDavid van Moolenbroek #define ST_MAP_NAMED_STRINGS_BY_NAME 0x00002000 233b2ed49a5SDavid van Moolenbroek #define ST_MAP_NAMED_STRINGS_BY_CONTENT 0x00004000 234b2ed49a5SDavid van Moolenbroek #define ST_MAP_STRINGS_BY_CONTENT 0x00008000 235b2ed49a5SDavid van Moolenbroek #define ST_ON_ALLOC_UNPAIR_ERROR 0x00010000 236b2ed49a5SDavid van Moolenbroek #define ST_ON_ALLOC_UNPAIR_DEALLOCATE 0x00020000 237b2ed49a5SDavid van Moolenbroek #define ST_ON_ALLOC_UNPAIR_IGNORE 0x00040000 238b2ed49a5SDavid van Moolenbroek #define ST_DEFAULT_MAP_GUARD_PTRS_TO_ARRAY_END 0x00080000 239b2ed49a5SDavid van Moolenbroek #define ST_DEFAULT_MAP_GUARD_PTRS_TO_STR_END 0x00100000 240b2ed49a5SDavid van Moolenbroek #define ST_REPORT_PRECISION_LOSS 0x00200000 241b2ed49a5SDavid van Moolenbroek #define ST_REPORT_SIGN_CHANGE 0x00400000 242b2ed49a5SDavid van Moolenbroek #define ST_REPORT_SMALLER_ARRAYS 0x00800000 243b2ed49a5SDavid van Moolenbroek #define ST_REPORT_LARGER_ARRAYS 0x01000000 244b2ed49a5SDavid van Moolenbroek #define ST_REPORT_SMALLER_STRINGS 0x02000000 245b2ed49a5SDavid van Moolenbroek #define ST_REPORT_LARGER_STRINGS 0x04000000 246b2ed49a5SDavid van Moolenbroek 247b2ed49a5SDavid van Moolenbroek #define ST_REPORT_UNPAIRED_ALL \ 248b2ed49a5SDavid van Moolenbroek (ST_REPORT_UNPAIRED_SENTRIES | ST_REPORT_UNPAIRED_DSENTRIES | \ 249b2ed49a5SDavid van Moolenbroek ST_REPORT_UNPAIRED_STRINGS | ST_REPORT_UNPAIRED_SELEMENTS) 250b2ed49a5SDavid van Moolenbroek #define ST_MAP_STRINGS_DEFAULT \ 251b2ed49a5SDavid van Moolenbroek (ST_MAP_NAMED_STRINGS_BY_NAME | ST_MAP_STRINGS_BY_CONTENT) 252b2ed49a5SDavid van Moolenbroek #define ST_ON_ALLOC_UNPAIR_DEFAULT (ST_ON_ALLOC_UNPAIR_ERROR) 253b2ed49a5SDavid van Moolenbroek #define ST_ON_ALLOC_UNPAIR_MASK \ 254b2ed49a5SDavid van Moolenbroek (ST_ON_ALLOC_UNPAIR_ERROR | ST_ON_ALLOC_UNPAIR_DEALLOCATE | \ 255b2ed49a5SDavid van Moolenbroek ST_ON_ALLOC_UNPAIR_IGNORE) 256b2ed49a5SDavid van Moolenbroek #define ST_TYPE_TRANSFORM_DEFAULT \ 257b2ed49a5SDavid van Moolenbroek (ST_DEFAULT_MAP_GUARD_PTRS_TO_STR_END | ST_REPORT_PRECISION_LOSS | \ 258b2ed49a5SDavid van Moolenbroek ST_REPORT_SIGN_CHANGE | ST_REPORT_SMALLER_ARRAYS | ST_REPORT_LARGER_ARRAYS) 259b2ed49a5SDavid van Moolenbroek 260b2ed49a5SDavid van Moolenbroek #define ST_POLICIES_DEFAULT_TRANSFER_ALL \ 261b2ed49a5SDavid van Moolenbroek (ST_DEFAULT_ALLOC_CASCADE_XFER | ST_DEFAULT_SKIP_STACK | \ 262b2ed49a5SDavid van Moolenbroek ST_DEFAULT_SKIP_LIB_STATE | ST_REPORT_NONXFERRED_ALLOCS | \ 263b2ed49a5SDavid van Moolenbroek ST_REPORT_NONXFERRED_UNPAIRED_ALLOCS | ST_MAP_STRINGS_DEFAULT | \ 264b2ed49a5SDavid van Moolenbroek ST_ON_ALLOC_UNPAIR_DEFAULT | ST_TYPE_TRANSFORM_DEFAULT) 265b2ed49a5SDavid van Moolenbroek #define ST_POLICIES_DEFAULT_TRANSFER_NONE \ 266b2ed49a5SDavid van Moolenbroek (ST_DEFAULT_TRANSFER_NONE | ST_DEFAULT_SKIP_STACK | \ 267b2ed49a5SDavid van Moolenbroek ST_DEFAULT_SKIP_LIB_STATE | ST_REPORT_NONXFERRED_ALLOCS | \ 268b2ed49a5SDavid van Moolenbroek ST_REPORT_NONXFERRED_UNPAIRED_ALLOCS | ST_MAP_STRINGS_DEFAULT | \ 269b2ed49a5SDavid van Moolenbroek ST_ON_ALLOC_UNPAIR_DEFAULT | ST_TYPE_TRANSFORM_DEFAULT) 270b2ed49a5SDavid van Moolenbroek #define ST_POLICIES_DEFAULT_TRANSFER_ASR \ 271b2ed49a5SDavid van Moolenbroek (ST_POLICIES_DEFAULT_TRANSFER_ALL | ST_REPORT_UNPAIRED_ALL) 272b2ed49a5SDavid van Moolenbroek 273b2ed49a5SDavid van Moolenbroek #define ST_POLICIES_DEFAULT ST_POLICIES_DEFAULT_TRANSFER_ALL 274b2ed49a5SDavid van Moolenbroek 275b2ed49a5SDavid van Moolenbroek /* Macros for predefined state transfer names. */ 276b2ed49a5SDavid van Moolenbroek 277b2ed49a5SDavid van Moolenbroek #define ADJUST_POINTER(local, remote, pointer) ( (local) + ((pointer) - (remote)) ) 278b2ed49a5SDavid van Moolenbroek #define ST_STR_BUFF_SIZE (MAGIC_MAX_NAME_LEN > MAGIC_MAX_TYPE_STR_LEN ? MAGIC_MAX_NAME_LEN : MAGIC_MAX_TYPE_STR_LEN) 279b2ed49a5SDavid van Moolenbroek #define USE_PRE_ALLOCATED_BUFFER(INFO) ((INFO)->init_buff_start) 280b2ed49a5SDavid van Moolenbroek #define EXEC_WITH_MAGIC_VARS(CMD, MAGIC_VARS) \ 281b2ed49a5SDavid van Moolenbroek do { \ 282b2ed49a5SDavid van Moolenbroek struct _magic_vars_t *__saved_vars = _magic_vars; \ 283b2ed49a5SDavid van Moolenbroek _magic_vars = (MAGIC_VARS); \ 284b2ed49a5SDavid van Moolenbroek CMD; \ 285b2ed49a5SDavid van Moolenbroek _magic_vars = __saved_vars; \ 286b2ed49a5SDavid van Moolenbroek } while(0); 287b2ed49a5SDavid van Moolenbroek 288b2ed49a5SDavid van Moolenbroek #define MAX_NUM_TYPENAMES 20 289b2ed49a5SDavid van Moolenbroek 290b2ed49a5SDavid van Moolenbroek #define CHECK_SENTITY_PAIRS 1 291b2ed49a5SDavid van Moolenbroek #define ST_DEBUG_LEVEL 1 292b2ed49a5SDavid van Moolenbroek #define ST_DEBUG_DATA_TRANSFER 0 293b2ed49a5SDavid van Moolenbroek #define CHECK_ASR 0 294b2ed49a5SDavid van Moolenbroek 295b2ed49a5SDavid van Moolenbroek #define ST_ALLOW_RAW_UNPAIRED_TYPES 1 296b2ed49a5SDavid van Moolenbroek #define ST_UNPAIRED_TYPES_RATIO_DEFAULT 0 297b2ed49a5SDavid van Moolenbroek #define ST_UNPAIRED_STRUCT_TYPES_RATIO_DEFAULT 0 298b2ed49a5SDavid van Moolenbroek #define FORCE_SOME_UNPAIRED_TYPES 1 299b2ed49a5SDavid van Moolenbroek #define ST_TRANSFER_IDENTITY_FOR_NO_INNER_PTRS 1 300b2ed49a5SDavid van Moolenbroek 301b2ed49a5SDavid van Moolenbroek #ifndef ST_ASSUME_RAW_COPY_BEFORE_TRANSFER 302b2ed49a5SDavid van Moolenbroek #define ST_ASSUME_RAW_COPY_BEFORE_TRANSFER 0 303b2ed49a5SDavid van Moolenbroek #endif 304b2ed49a5SDavid van Moolenbroek 305b2ed49a5SDavid van Moolenbroek #if defined(__MINIX) 306b2ed49a5SDavid van Moolenbroek #define ST_IS_UNPAIRABLE_TYPE_OS(T) (sef_self_endpoint != VM_PROC_NR || strcmp((T)->name, "void")) 307b2ed49a5SDavid van Moolenbroek #else 308b2ed49a5SDavid van Moolenbroek #define ST_IS_UNPAIRABLE_TYPE_OS(T) 1 309b2ed49a5SDavid van Moolenbroek #endif 310b2ed49a5SDavid van Moolenbroek 311b2ed49a5SDavid van Moolenbroek #if ST_ALLOW_RAW_UNPAIRED_TYPES 312b2ed49a5SDavid van Moolenbroek #define ST_IS_UNPAIRABLE_TYPE(T) (ST_IS_UNPAIRABLE_TYPE_OS(T) && (T)->type_id != MAGIC_TYPE_UNION) 313b2ed49a5SDavid van Moolenbroek #else 314b2ed49a5SDavid van Moolenbroek #define ST_IS_UNPAIRABLE_TYPE(T) \ 315b2ed49a5SDavid van Moolenbroek (ST_IS_UNPAIRABLE_TYPE_OS(T) \ 316b2ed49a5SDavid van Moolenbroek && !MAGIC_TYPE_IS_RAW_ARRAY(T) \ 317b2ed49a5SDavid van Moolenbroek && !((T)->type_id == MAGIC_TYPE_ARRAY && !strcmp((T)->contained_types[0]->type_str, "i8")) \ 318b2ed49a5SDavid van Moolenbroek && !(T)->ext) 319b2ed49a5SDavid van Moolenbroek #endif 320b2ed49a5SDavid van Moolenbroek 321b2ed49a5SDavid van Moolenbroek #define ST_IS_UNPAIRABLE_STRUCT_TYPE(T) \ 322b2ed49a5SDavid van Moolenbroek ((T)->type_id == MAGIC_TYPE_STRUCT) 323b2ed49a5SDavid van Moolenbroek 324b2ed49a5SDavid van Moolenbroek #define ST_FLAGS_PRINT(E) printf(", sf_flags(NDndsc)=%d%d%d%d%d%d", \ 325b2ed49a5SDavid van Moolenbroek MAGIC_STATE_EXTF_FLAG(E,ST_NEEDS_TRANSFER), MAGIC_STATE_EXTF_FLAG(E,ST_TRANSFER_DONE), \ 326b2ed49a5SDavid van Moolenbroek MAGIC_STATE_EXTF_FLAG(E,ST_ON_PTRXFER_SET_NULL), MAGIC_STATE_EXTF_FLAG(E,ST_ON_PTRXFER_SET_DEFAULT), \ 327b2ed49a5SDavid van Moolenbroek MAGIC_STATE_EXTF_FLAG(E,ST_ON_PTRXFER_SKIP), MAGIC_STATE_EXTF_FLAG(E,ST_ON_PTRXFER_CASCADE)); 328b2ed49a5SDavid van Moolenbroek 329b2ed49a5SDavid van Moolenbroek #define ST_SENTRY_PRINT(E,T) do { MAGIC_SENTRY_PRINT(E, T); ST_FLAGS_PRINT(E); if (MAGIC_SENTRY_IS_STRING(E)) printf(", string=\"%s\"", (ST_SENTRY_IS_CACHED(E) ? st_lookup_str_local_data(E) : (char*)(E)->address)); } while(0) 330b2ed49a5SDavid van Moolenbroek #define ST_DSENTRY_PRINT(E,T) do { MAGIC_DSENTRY_PRINT(E, T); ST_FLAGS_PRINT(&E->sentry); } while(0) 331b2ed49a5SDavid van Moolenbroek #define ST_FUNCTION_PRINT(E,T) do { MAGIC_FUNCTION_PRINT(E, T); ST_FLAGS_PRINT(E); } while(0) 332b2ed49a5SDavid van Moolenbroek 333b2ed49a5SDavid van Moolenbroek #define ST_CHECK_INIT() assert(st_init_done && "st_init() should be called first!") 334b2ed49a5SDavid van Moolenbroek 335b2ed49a5SDavid van Moolenbroek #define ST_SENTRY_IS_CACHED(E) (MAGIC_SENTRY_ID(E) >= 1 && (int)MAGIC_SENTRY_ID(E) <= st_cached_magic_vars.sentries_num && st_cached_magic_vars.sentries[MAGIC_SENTRY_ID(E)-1].address == (E)->address) 336b2ed49a5SDavid van Moolenbroek #define ST_GET_CACHED_COUNTERPART(CE,T,CT,LE) do { \ 337b2ed49a5SDavid van Moolenbroek int _i = (CE)->id - 1; \ 338b2ed49a5SDavid van Moolenbroek assert(_i >= 0 && _i < st_counterparts.T##_size); \ 339b2ed49a5SDavid van Moolenbroek LE = st_counterparts.CT[_i].counterpart; \ 340b2ed49a5SDavid van Moolenbroek } while(0) 341b2ed49a5SDavid van Moolenbroek #define ST_SET_CACHED_COUNTERPART(CE,T,CT,LE) do { \ 342b2ed49a5SDavid van Moolenbroek int _i = (CE)->id - 1; \ 343b2ed49a5SDavid van Moolenbroek assert(_i >= 0 && _i < st_counterparts.T##_size); \ 344b2ed49a5SDavid van Moolenbroek st_counterparts.CT[_i].counterpart = LE; \ 345b2ed49a5SDavid van Moolenbroek } while(0) 346b2ed49a5SDavid van Moolenbroek #define ST_HAS_CACHED_COUNTERPART(CE,CT) (st_counterparts.CT[(CE)->id - 1].counterpart != NULL) 347b2ed49a5SDavid van Moolenbroek 348b2ed49a5SDavid van Moolenbroek #define ST_TYPE_IS_STATIC_CACHED_COUNTERPART(CE,LE) (((CE) >= &st_cached_magic_vars.types[0] && (CE) < &st_cached_magic_vars.types[st_cached_magic_vars.types_num]) && st_counterparts.types[(CE)->id - 1].counterpart == (LE)) 349b2ed49a5SDavid van Moolenbroek #define ST_TYPE_IS_DYNAMIC_CACHED_COUNTERPART(CE,LE) (MAGIC_TYPE_FLAG(CE,MAGIC_TYPE_DYNAMIC) && (CE)->type_id == MAGIC_TYPE_ARRAY && (LE)->type_id == MAGIC_TYPE_ARRAY && (CE)->num_child_types == (LE)->num_child_types && ST_TYPE_IS_STATIC_CACHED_COUNTERPART((CE)->contained_types[0], (LE)->contained_types[0])) 350b2ed49a5SDavid van Moolenbroek #define ST_TYPE_IS_CACHED_COUNTERPART(CE,LE) (ST_TYPE_IS_STATIC_CACHED_COUNTERPART(CE,LE) || ST_TYPE_IS_DYNAMIC_CACHED_COUNTERPART(CE,LE)) 351b2ed49a5SDavid van Moolenbroek #define ST_PTR_TYPE_IS_STATIC_CACHED_COUNTERPART(CE,LE) (((CE) >= &st_cached_magic_vars.types[0] && (CE) < &st_cached_magic_vars.types[st_cached_magic_vars.types_num]) && st_counterparts.ptr_types[(CE)->id - 1].counterpart == (LE)) 352b2ed49a5SDavid van Moolenbroek #define ST_PTR_TYPE_IS_DYNAMIC_CACHED_COUNTERPART(CE,LE) (MAGIC_TYPE_FLAG(CE,MAGIC_TYPE_DYNAMIC) && (CE)->type_id == MAGIC_TYPE_ARRAY && (LE)->type_id == MAGIC_TYPE_ARRAY && (CE)->num_child_types == (LE)->num_child_types && ST_PTR_TYPE_IS_STATIC_CACHED_COUNTERPART((CE)->contained_types[0], (LE)->contained_types[0])) 353b2ed49a5SDavid van Moolenbroek #define ST_PTR_TYPE_IS_CACHED_COUNTERPART(CE,LE) (ST_PTR_TYPE_IS_STATIC_CACHED_COUNTERPART(CE,LE) || ST_PTR_TYPE_IS_DYNAMIC_CACHED_COUNTERPART(CE,LE)) 354b2ed49a5SDavid van Moolenbroek 355b2ed49a5SDavid van Moolenbroek 356b2ed49a5SDavid van Moolenbroek /* Buffer allocation */ 357b2ed49a5SDavid van Moolenbroek PUBLIC void *st_cb_pages_allocate(st_init_info_t *info, uint32_t *phys, int num_pages); 358b2ed49a5SDavid van Moolenbroek PUBLIC void st_cb_pages_free(st_init_info_t *info, st_alloc_pages *current_page); 359b2ed49a5SDavid van Moolenbroek PUBLIC void *st_buff_allocate(st_init_info_t *info, size_t size); 360b2ed49a5SDavid van Moolenbroek PUBLIC void st_buff_cleanup(st_init_info_t *info); 361b2ed49a5SDavid van Moolenbroek 362b2ed49a5SDavid van Moolenbroek #endif /* ST_STATE_TRANSFER_H */ 363