6 #include <magic_common.h>
7 #include <magic_structs.h>
9 /* Magic memory ranges */
10 #define magic_null_range _magic_vars->null_range
11 #define magic_data_range _magic_vars->data_range
12 #define magic_heap_range _magic_vars->heap_range
13 #define magic_map_range _magic_vars->map_range
14 #define magic_shm_range _magic_vars->shm_range
15 #define magic_stack_range _magic_vars->stack_range
16 #define magic_text_range _magic_vars->text_range
18 #define magic_sentry_range _magic_vars->sentry_range
19 #define magic_function_range _magic_vars->function_range
20 #define magic_dfunction_range _magic_vars->dfunction_range
22 #define magic_heap_start _magic_vars->heap_start
23 #define magic_heap_end _magic_vars->heap_end
24 #define magic_update_dsentry_ranges _magic_vars->update_dsentry_ranges
25 #define magic_update_dfunction_ranges _magic_vars->update_dfunction_ranges
27 /* Magic address ranges. */
28 #define MAGIC_ADDR_IS_WITHIN(A, MIN, MAX) ((MIN) <= (A) && (A) <= (MAX))
29 #define MAGIC_ADDR_IS_IN_RANGE(A, R) \
30 MAGIC_ADDR_IS_WITHIN(A, (R)[0], (R)[1])
31 #define MAGIC_RANGE_IS_IN_RANGE(r, R) \
32 (MAGIC_ADDR_IS_IN_RANGE((r)[0], R) && MAGIC_ADDR_IS_IN_RANGE((r)[1], R))
33 #define MAGIC_RANGE_COPY(RS, RD) memcpy(RD, RS, 2 * sizeof(void *))
34 #define MAGIC_RANGE_INIT(R) MAGIC_RANGE_COPY(magic_null_range,R)
35 #define MAGIC_RANGE_IS_NULL(R) \
36 ((R)[0] == magic_null_range[0] && (R)[1] == magic_null_range[1])
37 #define MAGIC_RANGE_IS_VALID(R) \
38 (MAGIC_RANGE_IS_NULL(R) || ((R)[0] <= (R)[1]))
39 #define MAGIC_RANGE_UPDATE(R, MIN, MAX) \
46 #define MAGIC_RANGE_SET_MIN(R, MIN) ((R)[0] = MIN)
47 #define MAGIC_RANGE_SET_MAX(R, MAX) ((R)[1] = MAX)
48 #define MAGIC_RANGE_SET(R, MIN, MAX) \
50 MAGIC_RANGE_SET_MIN(R, MIN); \
51 MAGIC_RANGE_SET_MAX(R, MAX); \
53 #define MAGIC_RANGE_PAGE_ROUND_MIN(R) \
54 ((R)[0] = (char*)(R)[0] - (unsigned long)(R)[0] % MAGIC_PAGE_SIZE)
55 #define MAGIC_RANGE_PAGE_ROUND_MAX(R) \
57 unsigned long diff = (unsigned long)(R)[1] % MAGIC_PAGE_SIZE; \
59 (R)[1] = (char *)(R)[1] + MAGIC_PAGE_SIZE - diff - 1; \
61 #define MAGIC_RANGE_PAGE_ROUND(R) \
63 MAGIC_RANGE_PAGE_ROUND_MIN(R); \
64 MAGIC_RANGE_PAGE_ROUND_MAX(R); \
66 #define MAGIC_RANGE_PRINT(R) \
67 _magic_printf("RANGE %s=[0x%08x;0x%08x]", #R, (unsigned long)((R)[0]), \
68 (unsigned long)((R)[1]))
69 #define MAGIC_RANGE_CHECK(R) \
70 assert(MAGIC_RANGE_IS_VALID(R) && "Invalid range " #R);
71 #define MAGIC_RANGE_SIZE(R) \
72 (!MAGIC_RANGE_IS_VALID(R) || MAGIC_RANGE_IS_NULL(R) ? \
73 0 : (char *)(R)[1] - (char *)(R)[0])
76 #define MAGIC_RANGE_DEBUG_ADDR(A, R) \
78 char *what = MAGIC_ADDR_IS_IN_RANGE(A, R) ? "" : "not "; \
79 _magic_printf("MRD: Address 0x%08x %sin ", A, what); \
80 MAGIC_RANGE_PRINT(R); \
81 _magic_printf("\n"); \
82 MAGIC_RANGE_CHECK(R); \
85 #define MAGIC_RANGE_DEBUG_ADDR(A,R) MAGIC_RANGE_CHECK(R)
88 #define MAGIC_ADDR_LOOKUP_USE_DSENTRY_RANGES 1
89 #define MAGIC_ADDR_LOOKUP_USE_DFUNCTION_RANGES 1
91 /* Magic range functions. */
92 PUBLIC
void magic_ranges_init(void);
93 PUBLIC
int magic_range_is_dfunction(void* addr
);
94 PUBLIC
int magic_range_is_dsentry(void* addr
);
95 PUBLIC
int magic_range_is_stack(void* addr
);
97 /* Lookup functions. */
98 PUBLIC
int magic_range_lookup_by_addr(void *addr
, void **container
);
100 #endif /* _MAGIC_RANGE_H */