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
;
38 #define MiB(x) ((x) * 1024UL * 1024)
41 * KASAN features and config
45 #define MEMINTRINSICS 1
46 /* KASAN_KALLOC defined in kasan.h */
47 /* KASAN_ZALLOC defined in kasan.h */
48 #define FAKESTACK_QUARANTINE (1 && FAKESTACK)
50 #define QUARANTINE_ENTRIES 5000
51 #define QUARANTINE_MAXSIZE MiB(10)
54 * The amount of physical memory stolen by KASan at boot to back the shadow memory
55 * and page tables. Larger memory systems need to steal proportionally less.
58 /* Works out at about 25% of 512 MiB and 15% of 3GiB system */
59 # define STOLEN_MEM_PERCENT 13UL
60 # define STOLEN_MEM_BYTES MiB(62)
62 # define STOLEN_MEM_PERCENT 25UL
63 # define STOLEN_MEM_BYTES 0
67 # error KASAN undefined
71 # error KASAN_SHIFT undefined
74 #define ADDRESS_FOR_SHADOW(x) (((x) - KASAN_SHIFT) << 3)
75 #define SHADOW_FOR_ADDRESS(x) (uint8_t *)(((x) >> 3) + KASAN_SHIFT)
77 #define NOINLINE __attribute__ ((noinline))
78 #define ALWAYS_INLINE inline __attribute__((always_inline))
80 #define CLANG_MIN_VERSION(x) (defined(__apple_build_version__) && (__apple_build_version__ >= (x)))
82 #define BIT(x) (1U << (x))
84 enum kasan_access_type
{
85 /* exactly one of these bits must be set */
90 TYPE_FSFREE
= BIT(4), /* fakestack free */
91 TYPE_MEMLD
= BIT(5), /* memory intrinsic - load */
92 TYPE_MEMSTR
= BIT(6), /* memory intrinsic - store */
93 TYPE_STRINGLD
= BIT(7), /* string intrinsic - load */
94 TYPE_STRINGSTR
= BIT(8), /* string intrinsic - store */
98 TYPE_LDSTR
= TYPE_LOAD
|TYPE_STORE
, /* regular loads and stores */
99 TYPE_FREE
= TYPE_KFREE
|TYPE_ZFREE
|TYPE_FSFREE
,
100 TYPE_MEM
= TYPE_MEMLD
|TYPE_MEMSTR
,
101 TYPE_STRING
= TYPE_STRINGLD
|TYPE_STRINGSTR
,
102 TYPE_LOAD_ALL
= TYPE_LOAD
|TYPE_MEMLD
|TYPE_STRINGLD
,
103 TYPE_STORE_ALL
= TYPE_STORE
|TYPE_MEMSTR
|TYPE_STRINGSTR
,
107 bool kasan_range_poisoned(vm_offset_t base
, vm_size_t size
, vm_offset_t
*first_invalid
);
108 void kasan_check_range(const void *x
, size_t sz
, unsigned access_type
);
109 void kasan_test(int testno
, int fail
);
110 void kasan_handle_test(void);
111 void kasan_unpoison_curstack(void);
112 void kasan_free_internal(void **addrp
, vm_size_t
*sizep
, int type
, zone_t
*, vm_size_t user_size
, int locked
, bool doquarantine
);
113 void kasan_poison(vm_offset_t base
, vm_size_t size
, vm_size_t leftrz
, vm_size_t rightrz
, uint8_t flags
);
114 void kasan_unpoison(void *base
, vm_size_t size
);
115 void kasan_lock(boolean_t
*b
);
116 void kasan_unlock(boolean_t b
);
117 void kasan_init_fakestack(void);
119 /* dynamic blacklist */
120 void kasan_init_dybl(void);
121 bool kasan_is_blacklisted(unsigned type
);
122 void kasan_dybl_load_kext(uintptr_t addr
, const char *kextname
);
123 void kasan_dybl_unload_kext(uintptr_t addr
);
125 /* arch-specific interface */
126 void kasan_arch_init(void);
128 extern vm_address_t kernel_vbase
;
129 extern vm_address_t kernel_vtop
;
131 extern long shadow_pages_used
;
133 /* Describes the source location where a global is defined. */
134 struct asan_global_source_location
{
135 const char *filename
;
140 /* Describes an instrumented global variable. */
144 uptr size_with_redzone
;
147 uptr has_dynamic_init
;
148 struct asan_global_source_location
*location
;
149 #if CLANG_MIN_VERSION(8020000)
154 #if defined(__x86_64__)
155 # define _JBLEN ((9 * 2) + 3 + 16)
156 #elif defined(__arm64__)
157 # define _JBLEN ((14 + 8 + 2) * 2)
159 # error "Unknown arch"
162 typedef int jmp_buf[_JBLEN
];
163 void _longjmp(jmp_buf env
, int val
);
164 int _setjmp(jmp_buf env
);
166 #endif /* _KASAN_INTERNAL_H_ */