2 * Copyright (c) 2000-2016 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
34 #if KASAN && !__has_feature(address_sanitizer)
35 # error "KASAN selected, but not enabled in compiler"
38 #if !KASAN && __has_feature(address_sanitizer)
39 # error "ASAN enabled in compiler, but kernel is not configured for KASAN"
42 #define KASAN_GLOBAL_SEGNAME "__DATA"
43 #define KASAN_GLOBAL_SECTNAME "__asan_globals"
45 typedef uintptr_t uptr
;
49 #define KASAN_KALLOC 1
50 #define KASAN_ZALLOC 1
51 #define KASAN_DYNAMIC_BLACKLIST 1
53 #define KASAN_GUARD_SIZE (16)
54 #define KASAN_GUARD_PAD (KASAN_GUARD_SIZE * 2)
56 #define KASAN_HEAP_ZALLOC 0
57 #define KASAN_HEAP_KALLOC 1
58 #define KASAN_HEAP_FAKESTACK 2
59 #define KASAN_HEAP_TYPES 3
61 /* shadow map byte values */
62 #define ASAN_VALID 0x00
63 #define ASAN_PARTIAL1 0x01
64 #define ASAN_PARTIAL2 0x02
65 #define ASAN_PARTIAL3 0x03
66 #define ASAN_PARTIAL4 0x04
67 #define ASAN_PARTIAL5 0x05
68 #define ASAN_PARTIAL6 0x06
69 #define ASAN_PARTIAL7 0x07
70 #define ASAN_ARRAY_COOKIE 0xac
71 #define ASAN_STACK_RZ 0xf0
72 #define ASAN_STACK_LEFT_RZ 0xf1
73 #define ASAN_STACK_MID_RZ 0xf2
74 #define ASAN_STACK_RIGHT_RZ 0xf3
75 #define ASAN_STACK_FREED 0xf5
76 #define ASAN_STACK_OOSCOPE 0xf8
77 #define ASAN_GLOBAL_RZ 0xf9
78 #define ASAN_HEAP_RZ 0xe9
79 #define ASAN_HEAP_LEFT_RZ 0xfa
80 #define ASAN_HEAP_RIGHT_RZ 0xfb
81 #define ASAN_HEAP_FREED 0xfd
84 * KASAN internal interface
88 void kasan_map_shadow(vm_offset_t address
, vm_size_t size
, bool is_zero
);
89 void kasan_disable(void);
90 void kasan_reserve_memory(void *);
91 void kasan_late_init(void);
92 void kasan_init(void);
93 void kasan_notify_stolen(vm_offset_t top
);
95 void kasan_load_kext(vm_offset_t base
, vm_size_t size
, const void *bundleid
);
96 void kasan_unload_kext(vm_offset_t base
, vm_size_t size
);
98 void kasan_unpoison(void *base
, vm_size_t size
);
99 void kasan_poison_range(vm_offset_t base
, vm_size_t sz
, uint8_t flags
);
100 void kasan_notify_address(vm_offset_t address
, vm_size_t size
);
101 void kasan_notify_address_nopoison(vm_offset_t address
, vm_size_t size
);
102 void kasan_unpoison_stack(vm_offset_t stack
, vm_size_t size
);
103 void kasan_unpoison_curstack(bool whole_stack
);
104 void kasan_unpoison_fakestack(thread_t thread
);
106 void kasan_fakestack_suspend(void);
107 void kasan_fakestack_resume(void);
110 void __kasan_runtests(struct kasan_test
*, int numtests
);
113 typedef int (*pmap_traverse_callback
)(vm_map_offset_t start
,
116 int kasan_traverse_mappings(pmap_traverse_callback
, void *context
);
118 #if XNU_KERNEL_PRIVATE
119 extern unsigned shadow_pages_total
;
122 void kasan_notify_address_zero(vm_offset_t
, vm_size_t
);
124 extern void kasan_map_low_fixed_regions(void);
125 extern unsigned shadow_stolen_idx
;
126 extern vm_offset_t shadow_pnext
, shadow_ptop
;
133 vm_size_t
kasan_alloc_resize(vm_size_t size
);
134 vm_size_t
kasan_user_size(vm_offset_t addr
);
136 vm_address_t
kasan_alloc(vm_offset_t addr
, vm_size_t size
, vm_size_t req
, vm_size_t leftrz
);
137 vm_address_t
kasan_dealloc(vm_offset_t addr
, vm_size_t
*size
);
139 void kasan_check_free(vm_offset_t addr
, vm_size_t size
, unsigned type
);
140 void kasan_free(void **addr
, vm_size_t
*size
, int type
, zone_t
*zone
, vm_size_t user_size
, bool doquarantine
);
144 /* thread interface */
145 struct kasan_thread_data
{
146 LIST_HEAD(fakestack_header_list
, fakestack_header
) fakestack_head
;
148 struct kasan_thread_data
*kasan_get_thread_data(thread_t
);
149 void kasan_init_thread(struct kasan_thread_data
*);
153 #if __has_feature(address_sanitizer)
154 # define NOKASAN __attribute__ ((no_sanitize_address))
160 * ASAN callbacks - inserted by the compiler
163 extern int __asan_option_detect_stack_use_after_return
;
164 extern const uintptr_t __asan_shadow_memory_dynamic_address
;
166 #define KASAN_DECLARE_FOREACH_WIDTH(ret, func, ...) \
167 ret func ## 1(__VA_ARGS__); \
168 ret func ## 2(__VA_ARGS__); \
169 ret func ## 4(__VA_ARGS__); \
170 ret func ## 8(__VA_ARGS__); \
171 ret func ## 16(__VA_ARGS__); \
175 KASAN_DECLARE_FOREACH_WIDTH(void, __asan_report_load
, uptr
);
176 KASAN_DECLARE_FOREACH_WIDTH(void, __asan_report_store
, uptr
);
177 KASAN_DECLARE_FOREACH_WIDTH(void, __asan_store
, uptr
);
178 KASAN_DECLARE_FOREACH_WIDTH(void, __asan_report_exp_load
, uptr
, int32_t);
179 KASAN_DECLARE_FOREACH_WIDTH(void, __asan_report_exp_store
, uptr
, int32_t);
180 KASAN_DECLARE_FOREACH_WIDTH(void, __asan_exp_load
, uptr
, int32_t);
181 KASAN_DECLARE_FOREACH_WIDTH(void, __asan_exp_store
, uptr
, int32_t);
182 KASAN_DECLARE_FOREACH_WIDTH(void, __asan_load
, uptr
);
184 void __asan_report_load_n(uptr p
, unsigned long size
);
185 void __asan_report_store_n(uptr p
, unsigned long size
);
186 void __asan_handle_no_return(void);
187 uptr
__asan_stack_malloc_0(size_t);
188 uptr
__asan_stack_malloc_1(size_t);
189 uptr
__asan_stack_malloc_2(size_t);
190 uptr
__asan_stack_malloc_3(size_t);
191 uptr
__asan_stack_malloc_4(size_t);
192 uptr
__asan_stack_malloc_5(size_t);
193 uptr
__asan_stack_malloc_6(size_t);
194 uptr
__asan_stack_malloc_7(size_t);
195 uptr
__asan_stack_malloc_8(size_t);
196 uptr
__asan_stack_malloc_9(size_t);
197 uptr
__asan_stack_malloc_10(size_t);
198 void __asan_stack_free_0(uptr
, size_t);
199 void __asan_stack_free_1(uptr
, size_t);
200 void __asan_stack_free_2(uptr
, size_t);
201 void __asan_stack_free_3(uptr
, size_t);
202 void __asan_stack_free_4(uptr
, size_t);
203 void __asan_stack_free_5(uptr
, size_t);
204 void __asan_stack_free_6(uptr
, size_t);
205 void __asan_stack_free_7(uptr
, size_t);
206 void __asan_stack_free_8(uptr
, size_t);
207 void __asan_stack_free_9(uptr
, size_t);
208 void __asan_stack_free_10(uptr
, size_t);
209 void __asan_poison_cxx_array_cookie(uptr
);
210 uptr
__asan_load_cxx_array_cookie(uptr
*);
211 void __asan_poison_stack_memory(uptr addr
, size_t size
);
212 void __asan_unpoison_stack_memory(uptr addr
, size_t size
);
213 void __asan_alloca_poison(uptr addr
, uptr size
);
214 void __asan_allocas_unpoison(uptr top
, uptr bottom
);
215 void __asan_loadN(uptr
, size_t);
216 void __asan_storeN(uptr
, size_t);
217 void __sanitizer_ptr_sub(uptr a
, uptr b
);
218 void __sanitizer_ptr_cmp(uptr a
, uptr b
);
219 void __sanitizer_annotate_contiguous_container(const void *beg
, const void *end
, const void *old_mid
, const void *new_mid
);
221 void __asan_exp_loadN(uptr addr
, size_t sz
, int32_t e
);
222 void __asan_exp_storeN(uptr addr
, size_t sz
, int32_t e
);
223 void __asan_report_exp_load_n(uptr addr
, unsigned long b
, int32_t c
);
224 void __asan_report_exp_store_n(uptr addr
, unsigned long b
, int32_t c
);
226 void __asan_set_shadow_00(uptr
, size_t);
227 void __asan_set_shadow_f1(uptr
, size_t);
228 void __asan_set_shadow_f2(uptr
, size_t);
229 void __asan_set_shadow_f3(uptr
, size_t);
230 void __asan_set_shadow_f5(uptr
, size_t);
231 void __asan_set_shadow_f8(uptr
, size_t);
233 void __asan_init_v5(void);
234 void __asan_register_globals(uptr a
, uptr b
);
235 void __asan_unregister_globals(uptr a
, uptr b
);
236 void __asan_register_elf_globals(uptr a
, uptr b
, uptr c
);
237 void __asan_unregister_elf_globals(uptr a
, uptr b
, uptr c
);
239 void __asan_before_dynamic_init(uptr
);
240 void __asan_after_dynamic_init(void);
241 void __asan_init(void);
242 void __asan_unregister_image_globals(uptr
);
243 void __asan_register_image_globals(uptr
);
246 #endif /* KERNEL_PRIVATE */
247 #endif /* _KASAN_H_ */