2 * Copyright (c) 2000-2014 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@
29 #ifndef _KASAN_INTERNAL_H_
30 #define _KASAN_INTERNAL_H_
33 #include <mach/mach_vm.h>
34 #include <kern/zalloc.h>
36 typedef uintptr_t uptr
;
39 * KASAN features and config
43 #define MEMINTRINSICS 1
44 /* KASAN_KALLOC defined in kasan.h */
45 /* KASAN_ZALLOC defined in kasan.h */
46 #define FAKESTACK_QUARANTINE (1 && FAKESTACK)
48 #define QUARANTINE_ENTRIES 5000
49 #define QUARANTINE_MAXSIZE (10UL * 1024 * 1024)
52 # error KASAN undefined
56 # error KASAN_SHIFT undefined
59 #define ADDRESS_FOR_SHADOW(x) (((x) - KASAN_SHIFT) << 3)
60 #define SHADOW_FOR_ADDRESS(x) (uint8_t *)(((x) >> 3) + KASAN_SHIFT)
62 #define NOINLINE __attribute__ ((noinline))
63 #define ALWAYS_INLINE inline __attribute__((always_inline))
65 #define CLANG_MIN_VERSION(x) (defined(__apple_build_version__) && (__apple_build_version__ >= (x)))
67 #define BIT(x) (1U << (x))
69 enum kasan_access_type
{
70 /* exactly one of these bits must be set */
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 */
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
,
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);
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
);
110 /* arch-specific interface */
111 void kasan_arch_init(void);
113 extern vm_address_t kernel_vbase
;
114 extern vm_address_t kernel_vtop
;
116 extern long shadow_pages_used
;
118 /* Describes the source location where a global is defined. */
119 struct asan_global_source_location
{
120 const char *filename
;
125 /* Describes an instrumented global variable. */
129 uptr size_with_redzone
;
132 uptr has_dynamic_init
;
133 struct asan_global_source_location
*location
;
134 #if CLANG_MIN_VERSION(8020000)
139 #if defined(__x86_64__)
140 # define _JBLEN ((9 * 2) + 3 + 16)
144 typedef int jmp_buf[_JBLEN
];
145 void _longjmp(jmp_buf env
, int val
);
146 int _setjmp(jmp_buf env
);
148 #endif /* _KASAN_INTERNAL_H_ */