1 #ifndef _MAGIC_STRUCTS_H 2 #define _MAGIC_STRUCTS_H 3 4 #include <magic_def.h> 5 #include <magic_common.h> 6 #include <stddef.h> 7 #include <common/ut/uthash.h> 8 9 /* Magic state type struct. */ 10 struct _magic_type { 11 _magic_id_t id; 12 const char *name; 13 const char **names; 14 unsigned num_names; 15 const char *type_str; 16 unsigned size; 17 unsigned num_child_types; 18 struct _magic_type **contained_types; 19 struct _magic_type **compatible_types; 20 const char **member_names; 21 unsigned *member_offsets; 22 void *value_set; 23 unsigned type_id; 24 int flags; 25 unsigned bit_width; 26 const void *ext; 27 }; 28 29 /* Magic state entry struct. */ 30 struct _magic_sentry { 31 _magic_id_t id; 32 const char *name; 33 struct _magic_type *type; 34 int flags; 35 void *address; 36 void *shadow_address; 37 }; 38 39 /* Magic state entry list struct. */ 40 struct _magic_sentry_list { 41 struct _magic_sentry *sentry; 42 struct _magic_sentry_list *next; 43 }; 44 45 /* Magic state entry hash struct. */ 46 #define MAGIC_SENTRY_NAME_MAX_KEY_LEN 512 47 struct _magic_sentry_hash { 48 struct _magic_sentry_list *sentry_list; 49 char key[MAGIC_SENTRY_NAME_MAX_KEY_LEN]; 50 UT_hash_handle hh; 51 }; 52 53 /* Magic state function struct. */ 54 struct _magic_function { 55 _magic_id_t id; 56 const char *name; 57 struct _magic_type *type; 58 int flags; 59 void *address; 60 }; 61 62 /* Magic state function hash struct. */ 63 struct _magic_function_hash { 64 struct _magic_function *function; 65 void *key; 66 UT_hash_handle hh; 67 }; 68 69 /* Magic dynamic function struct. */ 70 struct _magic_dfunction { 71 unsigned long magic_number; 72 const char *parent_name; 73 struct _magic_function function; 74 struct _magic_dfunction *prev; 75 struct _magic_dfunction *next; 76 }; 77 78 /* Magic dynamic state index struct. */ 79 struct _magic_dsindex { 80 struct _magic_type *type; 81 const char *name; 82 const char *parent_name; 83 int flags; 84 }; 85 86 /* Magic dynamic state entry struct. */ 87 #define MAGIC_DSENTRY_ALLOW_PREV 0 88 /* 89 * The name of an externally allocated dsentry will be: 90 * strlen("MAGIC_EXT_ALLOC_NAME") + strlen("MAGIC_ALLOC_NAME_SEP") + 91 * strlen(0xffffffff) + strlen("MAGIC_ALLOC_NAME_SUFFIX") + 1 = 92 * 4 + 1 + 10 + 1 + 1 = 17 93 */ 94 #define MAGIC_DSENTRY_EXT_NAME_BUFF_SIZE 17 95 96 struct _magic_dsentry { 97 unsigned long magic_number; 98 const char *parent_name; 99 char name_ext_buff[MAGIC_DSENTRY_EXT_NAME_BUFF_SIZE]; 100 struct _magic_sentry sentry; 101 struct _magic_type type; 102 struct _magic_type *type_array[1]; 103 #if MAGIC_DSENTRY_ALLOW_PREV 104 struct _magic_dsentry *prev; 105 #endif 106 struct _magic_dsentry *next; 107 struct _magic_dsentry *next_mpool; 108 struct _magic_dsentry *next_mblock; 109 /* 110 * The following 2 fields are only set if the dsentry 111 * is part of a super object. 112 * See llvm/shared/magic/libst/include/heap.h for more details. 113 */ 114 #ifndef __MINIX 115 struct _magic_dsentry *next_sobject; 116 void *sobject_base_addr; 117 #endif 118 void *ext; 119 unsigned long magic_state; 120 union __alloc_flags { 121 struct { 122 int flags; 123 int prot; 124 } mmap_call; 125 #define mmap_flags mmap_call.flags 126 #define mmap_prot mmap_call.prot 127 struct { 128 int flags; 129 int shmid; 130 } shmat_call; 131 #define shmat_flags shmat_call.flags 132 #define shmat_shmid shmat_call.shmid 133 } alloc_flags; 134 #define alloc_mmap_flags alloc_flags.mmap_call.flags 135 #define alloc_mmap_prot alloc_flags.mmap_call.prot 136 #define alloc_shmat_flags alloc_flags.shmat_call.flags 137 #define alloc_shmat_shmid alloc_flags.shmat_call.shmid 138 _magic_id_t site_id; /* Identifier of the call at a callsite. */ 139 }; 140 141 /* Magic out-of-band dynamic state entry struct. */ 142 #define MAGIC_MAX_OBDSENTRIES 32 143 #define MAGIC_MAX_OBDSENTRY_NAME_LEN 32 144 #define MAGIC_MAX_OBDSENTRY_PARENT_NAME_LEN 32 145 struct _magic_obdsentry { 146 char name[MAGIC_MAX_OBDSENTRY_NAME_LEN]; 147 char parent_name[MAGIC_MAX_OBDSENTRY_PARENT_NAME_LEN]; 148 struct _magic_dsentry dsentry; 149 }; 150 EXTERN struct _magic_obdsentry _magic_obdsentries[MAGIC_MAX_OBDSENTRIES]; 151 152 /* Magic memory pool state struct. */ 153 #define MAGIC_MAX_MEMPOOLS 1024 154 #define MAGIC_MAX_MEMPOOL_NAME_LEN 32 155 #define MAGIC_MEMPOOL_NAME_PREFIX "_magic_mempool_" 156 EXTERN const char *const MAGIC_MEMPOOL_NAME_UNKNOWN; 157 EXTERN const char *const MAGIC_MEMPOOL_NAME_DETACHED; 158 159 struct _magic_mpdesc { 160 int is_alive; 161 char name[MAGIC_MAX_MEMPOOL_NAME_LEN]; 162 /* pointer to the pool object */ 163 void *addr; 164 #if MAGIC_MEM_USAGE_OUTPUT_CTL 165 unsigned long dtype_id; 166 #endif 167 }; 168 EXTERN struct _magic_mpdesc _magic_mpdescs[MAGIC_MAX_MEMPOOLS]; 169 170 /* Magic state element. */ 171 struct _magic_selement_s { 172 struct _magic_dsentry dsentry_buff; 173 struct _magic_sentry *sentry; 174 const struct _magic_type *parent_type; 175 void *parent_address; 176 int child_num; 177 const struct _magic_type *type; 178 void *address; 179 int depth; 180 int num; 181 void *cb_args; 182 }; 183 typedef struct _magic_selement_s _magic_selement_t; 184 185 /* Magic external library descriptor. */ 186 struct _magic_libdesc { 187 const char *name; 188 void *text_range[2]; 189 void *data_range[2]; 190 void *alloc_address; 191 size_t alloc_size; 192 }; 193 194 /* Magic SO library descriptor. */ 195 struct _magic_sodesc { 196 struct _magic_libdesc lib; 197 struct _magic_sodesc *prev; 198 struct _magic_sodesc *next; 199 }; 200 201 /* Magic DSO library descriptor. */ 202 struct _magic_dsodesc { 203 struct _magic_libdesc lib; 204 void *handle; 205 int ref_count; 206 struct _magic_dsodesc *prev; 207 struct _magic_dsodesc *next; 208 }; 209 210 /* The following constant is specific to MINIX3; on other platforms, this 211 * functionality is unused altogether. On MINIX3, the libc malloc code uses 212 * mmap to create page directories. Since malloc state is discarded upon state 213 * transfer, we must not instrument its mmap calls in the regular way. On the 214 * other hand, since mmap'ed regions are transferred to new instances, we end 215 * up with a memory leak if we do not unmap those mmap'ed regions. Therefore, 216 * we specifically track the mmap/munmap calls made from the malloc code, and 217 * explicitly unmap its regions during state transfer. The following constant 218 * defines how many ranges can be mmap'ed at once. The malloc code uses only 219 * one page directory, but it may enlarge it by first allocating a new area 220 * and then unmapping the old one. Therefore, we need two entries. 221 */ 222 #ifdef __MINIX 223 #define MAGIC_UNMAP_MEM_ENTRIES 2 224 #endif 225 226 /* Magic vars. */ 227 struct _magic_vars_t { 228 229 /* Magic Address Space Randomization (ASRPass) */ 230 int asr_seed; 231 int asr_heap_map_do_permutate; 232 int asr_heap_max_offset; 233 int asr_heap_max_padding; 234 int asr_map_max_offset_pages; 235 int asr_map_max_padding_pages; 236 237 /* Runtime flags. */ 238 int no_mem_inst; 239 240 /* Magic type array. */ 241 struct _magic_type *types; 242 int types_num; 243 _magic_id_t types_next_id; 244 245 /* Magic function array. */ 246 struct _magic_function *functions; 247 int functions_num; 248 _magic_id_t functions_next_id; 249 250 /* Magic state entry array. */ 251 struct _magic_sentry *sentries; 252 int sentries_num; 253 int sentries_str_num; 254 _magic_id_t sentries_next_id; 255 256 /* Magic dynamic state index array. */ 257 struct _magic_dsindex *dsindexes; 258 int dsindexes_num; 259 260 /* Magic dynamic state entry list. */ 261 struct _magic_dsentry *first_dsentry; 262 unsigned long num_dead_dsentries; 263 unsigned long size_dead_dsentries; 264 265 /* Magic memory pool dynamic state entry list. */ 266 struct _magic_dsentry *first_mempool_dsentry; 267 268 /* Magic dynamic function list. */ 269 struct _magic_dfunction *first_dfunction; 270 struct _magic_dfunction *last_dfunction; 271 int dfunctions_num; 272 273 /* Magic SO library descriptor list. */ 274 struct _magic_sodesc *first_sodesc; 275 struct _magic_sodesc *last_sodesc; 276 int sodescs_num; 277 278 /* Magic DSO library descriptor list. */ 279 struct _magic_dsodesc *first_dsodesc; 280 struct _magic_dsodesc *last_dsodesc; 281 int dsodescs_num; 282 283 /* Magic stack-related variables. */ 284 struct _magic_dsentry *first_stack_dsentry; 285 struct _magic_dsentry *last_stack_dsentry; 286 287 /* Magic memory ranges */ 288 void *null_range[2]; 289 void *data_range[2]; 290 void *heap_range[2]; 291 void *map_range[2]; 292 void *shm_range[2]; 293 void *stack_range[2]; 294 void *text_range[2]; 295 296 void *sentry_range[2]; 297 void *function_range[2]; 298 void *dfunction_range[2]; 299 300 void *heap_start; 301 void *heap_end; 302 int update_dsentry_ranges; 303 int update_dfunction_ranges; 304 305 #ifdef __MINIX 306 /* Memory to unmap after state transfer (MINIX3 only) */ 307 struct { 308 void *start; 309 size_t length; 310 } unmap_mem[MAGIC_UNMAP_MEM_ENTRIES]; 311 #endif 312 313 /* Range lookup index */ 314 void *sentry_rl_buff; 315 size_t sentry_rl_buff_offset; 316 size_t sentry_rl_buff_size; 317 void *sentry_rl_index; 318 319 /* Sentry hash */ 320 void *sentry_hash_buff; 321 size_t sentry_hash_buff_offset; 322 size_t sentry_hash_buff_size; 323 void *sentry_hash_head; 324 325 /* Function hash */ 326 void *function_hash_buff; 327 size_t function_hash_buff_offset; 328 size_t function_hash_buff_size; 329 void *function_hash_head; 330 331 /* 332 * Don't call malloc() in magic_malloc_positioned(). 333 * Used in ST after RAW COPY. 334 */ 335 int fake_malloc; 336 }; 337 338 339 #endif /* _MAGIC_STRUCTS_H */ 340