]> git.saurik.com Git - apple/xnu.git/blob - san/kasan_internal.h
xnu-4570.1.46.tar.gz
[apple/xnu.git] / san / kasan_internal.h
1 /*
2 * Copyright (c) 2000-2014 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #ifndef _KASAN_INTERNAL_H_
30 #define _KASAN_INTERNAL_H_
31
32 #include <stdbool.h>
33 #include <mach/mach_vm.h>
34 #include <kern/zalloc.h>
35
36 typedef uintptr_t uptr;
37
38 /*
39 * KASAN features and config
40 */
41 #define KASAN_DEBUG 1
42 #define FAKESTACK 1
43 #define MEMINTRINSICS 1
44 /* KASAN_KALLOC defined in kasan.h */
45 /* KASAN_ZALLOC defined in kasan.h */
46 #define FAKESTACK_QUARANTINE (1 && FAKESTACK)
47
48 #define QUARANTINE_ENTRIES 5000
49 #define QUARANTINE_MAXSIZE (10UL * 1024 * 1024)
50
51 #ifndef KASAN
52 # error KASAN undefined
53 #endif
54
55 #ifndef KASAN_SHIFT
56 # error KASAN_SHIFT undefined
57 #endif
58
59 #define ADDRESS_FOR_SHADOW(x) (((x) - KASAN_SHIFT) << 3)
60 #define SHADOW_FOR_ADDRESS(x) (uint8_t *)(((x) >> 3) + KASAN_SHIFT)
61
62 #define NOINLINE __attribute__ ((noinline))
63 #define ALWAYS_INLINE inline __attribute__((always_inline))
64
65 #define CLANG_MIN_VERSION(x) (defined(__apple_build_version__) && (__apple_build_version__ >= (x)))
66
67 #define BIT(x) (1U << (x))
68
69 enum kasan_access_type {
70 /* exactly one of these bits must be set */
71 TYPE_LOAD = BIT(0),
72 TYPE_STORE = BIT(1),
73 TYPE_KFREE = BIT(2),
74 TYPE_ZFREE = BIT(3),
75 TYPE_FSFREE = BIT(4), /* fakestack free */
76 TYPE_MEMLD = BIT(5), /* memory intrinsic - load */
77 TYPE_MEMSTR = BIT(6), /* memory intrinsic - store */
78 TYPE_STRINGLD = BIT(7), /* string intrinsic - load */
79 TYPE_STRINGSTR = BIT(8), /* string intrinsic - store */
80 TYPE_TEST = BIT(15),
81
82 /* masks */
83 TYPE_LDSTR = TYPE_LOAD|TYPE_STORE, /* regular loads and stores */
84 TYPE_FREE = TYPE_KFREE|TYPE_ZFREE|TYPE_FSFREE,
85 TYPE_MEM = TYPE_MEMLD|TYPE_MEMSTR,
86 TYPE_STRING = TYPE_STRINGLD|TYPE_STRINGSTR,
87 TYPE_LOAD_ALL = TYPE_LOAD|TYPE_MEMLD|TYPE_STRINGLD,
88 TYPE_STORE_ALL = TYPE_STORE|TYPE_MEMSTR|TYPE_STRINGSTR,
89 TYPE_ALL = ~0U
90 };
91
92 bool kasan_range_poisoned(vm_offset_t base, vm_size_t size, vm_offset_t *first_invalid);
93 void kasan_check_range(const void *x, size_t sz, unsigned access_type);
94 void kasan_test(int testno, int fail);
95 void kasan_handle_test(void);
96 void kasan_unpoison_curstack(void);
97 void kasan_free_internal(void **addrp, vm_size_t *sizep, int type, zone_t *, vm_size_t user_size, int locked, bool doquarantine);
98 void kasan_poison(vm_offset_t base, vm_size_t size, vm_size_t leftrz, vm_size_t rightrz, uint8_t flags);
99 void kasan_unpoison(void *base, vm_size_t size);
100 void kasan_lock(boolean_t *b);
101 void kasan_unlock(boolean_t b);
102 void kasan_init_fakestack(void);
103
104 /* dynamic blacklist */
105 void kasan_init_dybl(void);
106 bool kasan_is_blacklisted(unsigned type);
107 void kasan_dybl_load_kext(uintptr_t addr, const char *kextname);
108 void kasan_dybl_unload_kext(uintptr_t addr);
109
110 /* arch-specific interface */
111 void kasan_arch_init(void);
112
113 extern vm_address_t kernel_vbase;
114 extern vm_address_t kernel_vtop;
115
116 extern long shadow_pages_used;
117
118 /* Describes the source location where a global is defined. */
119 struct asan_global_source_location {
120 const char *filename;
121 int line_no;
122 int column_no;
123 };
124
125 /* Describes an instrumented global variable. */
126 struct asan_global {
127 uptr addr;
128 uptr size;
129 uptr size_with_redzone;
130 const char *name;
131 const char *module;
132 uptr has_dynamic_init;
133 struct asan_global_source_location *location;
134 #if CLANG_MIN_VERSION(8020000)
135 uptr odr_indicator;
136 #endif
137 };
138
139 #if defined(__x86_64__)
140 # define _JBLEN ((9 * 2) + 3 + 16)
141 #endif
142
143
144 typedef int jmp_buf[_JBLEN];
145 void _longjmp(jmp_buf env, int val);
146 int _setjmp(jmp_buf env);
147
148 #endif /* _KASAN_INTERNAL_H_ */