1 /* 2 * Copyright (c) 2000-2016 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 * @OSF_COPYRIGHT@ 30 */ 31 /* 32 * Mach Operating System 33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University 34 * All Rights Reserved. 35 * 36 * Permission to use, copy, modify and distribute this software and its 37 * documentation is hereby granted, provided that both the copyright 38 * notice and this permission notice appear in all copies of the 39 * software, derivative works or modified versions, and any portions 40 * thereof, and that both notices appear in supporting documentation. 41 * 42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 45 * 46 * Carnegie Mellon requests users of this software to return to 47 * 48 * Software Distribution Coordinator or [email protected] 49 * School of Computer Science 50 * Carnegie Mellon University 51 * Pittsburgh PA 15213-3890 52 * 53 * any improvements or extensions that they make and grant Carnegie Mellon 54 * the rights to redistribute these changes. 55 */ 56 /* 57 */ 58 59 #ifndef _KERN_ASSERT_H_ 60 #define _KERN_ASSERT_H_ 61 62 /* assert.h 4.2 85/01/21 */ 63 64 #include <kern/macro_help.h> 65 #include <sys/cdefs.h> 66 #include <machine/trap.h> 67 68 #ifdef XNU_KERNEL_PRIVATE 69 #include <machine/static_if.h> 70 #endif 71 #ifdef MACH_KERNEL_PRIVATE 72 #include <mach_assert.h> 73 #endif 74 75 __BEGIN_DECLS 76 77 __abortlike 78 extern void Assert( 79 const char *file, 80 int line, 81 const char *expression) __attribute__((noinline)); 82 83 extern int kext_assertions_enable; 84 85 #ifndef __FILE_NAME__ 86 #define __FILE_NAME__ __FILE__ 87 #endif 88 #define __Panic(fmt, args...) (panic)(fmt, ##args) 89 90 __END_DECLS 91 92 #ifndef APPLE_KEXT_ASSERTIONS 93 #define APPLE_KEXT_ASSERTIONS 0 94 #endif 95 96 __enum_decl(mach_assert_type_t, unsigned char, { 97 MACH_ASSERT_DEFAULT, 98 MACH_ASSERT_3P, 99 MACH_ASSERT_3S, 100 MACH_ASSERT_3U, 101 }); 102 103 #ifndef __BUILDING_XNU_LIBRARY__ 104 #define MACH_ASSERT_DESC_ALIGN __attribute__((packed, aligned(4))) 105 #else /* __BUILDING_XNU_LIBRARY__ */ 106 /* The assert __desc struct is packed to 4 bytes to save stack usage. 107 * This is not done in user build since there is some difference between the 108 * user-mode linker and the kernel linker which causes this to produce 109 * unaligned pointer exception */ 110 #define MACH_ASSERT_DESC_ALIGN 111 #endif /* __BUILDING_XNU_LIBRARY__ */ 112 113 struct mach_assert_hdr { 114 mach_assert_type_t type; 115 unsigned lineno : 24; 116 const char *filename; 117 } MACH_ASSERT_DESC_ALIGN; 118 119 struct mach_assert_default { 120 struct mach_assert_hdr hdr; 121 const char *expr; 122 } MACH_ASSERT_DESC_ALIGN; 123 124 struct mach_assert_3x { 125 struct mach_assert_hdr hdr; 126 const char *a; 127 const char *op; 128 const char *b; 129 } MACH_ASSERT_DESC_ALIGN; 130 131 #if MACH_ASSERT 132 # if XNU_KERNEL_PRIVATE 133 STATIC_IF_KEY_DECLARE_TRUE(mach_assert); 134 # define mach_assert_enabled() improbable_static_if(mach_assert) 135 # else 136 # define mach_assert_enabled() 1 137 # endif /* !XNU_KERNEL_PRIVATE */ 138 #elif APPLE_KEXT_ASSERTIONS 139 # define mach_assert_enabled() __builtin_expect(kext_assertions_enable, 0L) 140 #else /* !MACH_ASSERT && !APPLE_KEXT_ASSERTIONS */ 141 # define mach_assert_enabled() 0 142 #endif /* !MACH_ASSERT && !APPLE_KEXT_ASSERTIONS */ 143 144 #define MACH_ASSERT_TRAP_CODE 0xbffc /* XNU_HARD_TRAP_ASSERT_FAILURE */ 145 #define MACH_ASSERT_SEGSECT "__DATA_CONST,__assert" 146 147 /*! 148 * @abstract 149 * Wrap any arbitrary expression/code behind a conditional 150 * on whether assertions are enabled. 151 */ 152 #define MACH_ASSERT_DO(...) ({ \ 153 if (mach_assert_enabled()) { \ 154 __VA_ARGS__; \ 155 } \ 156 }) 157 158 #define mach_assert_abort(reason) ({ \ 159 __attribute__((used, section(MACH_ASSERT_SEGSECT))) \ 160 static const struct mach_assert_default __desc = { \ 161 { MACH_ASSERT_DEFAULT, __LINE__, __FILE_NAME__, }, \ 162 reason, \ 163 }; \ 164 \ 165 ml_fatal_trap_with_value(MACH_ASSERT_TRAP_CODE, &__desc); \ 166 }) 167 168 #define mach_assert_abort3x(how, s_a, s_op, s_b, v_a, v_b) ({ \ 169 __attribute__((used, section(MACH_ASSERT_SEGSECT))) \ 170 static const struct mach_assert_3x __desc_ ## how = { \ 171 { MACH_ASSERT_ ## how, __LINE__, __FILE_NAME__, }, \ 172 s_a, s_op, s_b, \ 173 }; \ 174 \ 175 ml_fatal_trap_with_value3(MACH_ASSERT_TRAP_CODE, \ 176 &__desc_ ## how, v_a, v_b); \ 177 }) 178 179 /*! 180 * @abstract 181 * assert() that is never elided or removed even in release builds. 182 */ 183 #define release_assert(ex) ({ \ 184 if (__builtin_expect(!(ex), 0L)) { \ 185 mach_assert_abort(#ex); \ 186 } \ 187 }) 188 189 #if MACH_ASSERT || APPLE_KEXT_ASSERTIONS 190 191 #define __assert_only 192 193 #define mach_assert_enabled_expr(ex) \ 194 (mach_assert_enabled() || __builtin_constant_p(!(ex))) 195 196 #define assert(ex) \ 197 (mach_assert_enabled_expr(ex) && !(ex) \ 198 ? (void)mach_assert_abort(#ex) : (void)0) 199 200 #define assertf(ex, fmt, args...) ({ \ 201 if (mach_assert_enabled_expr(ex) && __builtin_expect(!(ex), 0L)) { \ 202 __Panic("%s:%d Assertion failed: %s : " fmt, \ 203 __FILE_NAME__, __LINE__, # ex, ##args); \ 204 } \ 205 }) 206 207 /* 208 * Each of the following three macros takes three arguments instead of one for 209 * the assertion. The suffixes, 's', u' and 'p' indicate the type of arguments 210 * expected: 'signed', 'unsigned' or 'pointer' respectively. 211 * 212 * assert(a > b) -> file.c:123 Assertion failed: a > b 213 * assert3u(a, >, b) -> file.c:124 Assertion failed: a > b (1 >= 10) 214 * 215 * These macros define a local variable with name starting with __desc which 216 * contain the assert info and then call the brk instruction. The trap 217 * is then handled and panic_assert_format() is called to parse this struct. 218 */ 219 #define assert3u(a, op, b) ({ \ 220 if (mach_assert_enabled_expr((unsigned long long)(a) op \ 221 (unsigned long long)(b))) { \ 222 const unsigned long long a_ = (a); \ 223 const unsigned long long b_ = (b); \ 224 \ 225 if (__builtin_expect(!(a_ op b_), 0L)) { \ 226 mach_assert_abort3x(3U, #a, #op, #b, a_, b_); \ 227 } \ 228 } \ 229 }) 230 231 #define assert3s(a, op, b) ({ \ 232 if (mach_assert_enabled_expr((long long)(a) op ((long long)b))) { \ 233 const signed long long a_ = (a); \ 234 const signed long long b_ = (b); \ 235 \ 236 if (__builtin_expect(!(a_ op b_), 0L)) { \ 237 mach_assert_abort3x(3S, #a, #op, #b, a_, b_); \ 238 } \ 239 } \ 240 }) 241 242 #define assert3p(a, op, b) ({ \ 243 if (mach_assert_enabled_expr((const void *)(a) op (const void *)(b))) { \ 244 const void *a_ = (a); \ 245 const void *b_ = (b); \ 246 \ 247 if (__builtin_expect(!(a_ op b_), 0L)) { \ 248 mach_assert_abort3x(3P, #a, #op, #b, a_, b_); \ 249 } \ 250 } \ 251 }) 252 253 #else /* !MACH_ASSERT && !XNU_KERNEL_PRIVATE */ 254 255 #define __assert_only __unused 256 #define mach_assert_enabled_expr(ex) 0 257 258 #define assert(ex) ((void)0) 259 #define assertf(ex, fmt, args...) ((void)0) 260 261 #define assert3s(a, op, b) ((void)0) 262 #define assert3u(a, op, b) ((void)0) 263 #define assert3p(a, op, b) ((void)0) 264 265 #endif /* !MACH_ASSERT && !XNU_KERNEL_PRIVATE */ 266 267 /* 268 * static_assert is a C11 / C++0x / C++1z feature. 269 * 270 * Beginning with C++0x, it is a keyword and should not be #defined 271 * 272 * static_assert is not disabled by MACH_ASSERT or NDEBUG 273 */ 274 275 #ifndef __cplusplus 276 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L 277 #define _STATIC_ASSERT_OVERLOADED_MACRO(_1, _2, NAME, ...) NAME 278 #define static_assert(...) _STATIC_ASSERT_OVERLOADED_MACRO(__VA_ARGS__, _static_assert_2_args, _static_assert_1_arg)(__VA_ARGS__) 279 280 #define _static_assert_2_args(ex, str) _Static_assert((ex), str) 281 #define _static_assert_1_arg(ex) _Static_assert((ex), #ex) 282 #endif 283 #else 284 #if !defined(__cpp_static_assert) 285 /* pre C++11 support */ 286 #define _STATIC_ASSERT_OVERLOADED_MACRO(_1, _2, NAME, ...) NAME 287 #define static_assert(...) _STATIC_ASSERT_OVERLOADED_MACRO(__VA_ARGS__, _static_assert_2_args, _static_assert_1_arg)(__VA_ARGS__) 288 289 #define _static_assert_2_args(ex, str) _Static_assert((ex), str) 290 #define _static_assert_1_arg(ex) _Static_assert((ex), #ex) 291 #else 292 /* 293 * C++11 only supports the 2 argument version of static_assert. 294 * C++1z has added support for the 1 argument version. 295 */ 296 #define _static_assert_1_arg(ex) static_assert((ex), #ex) 297 #endif 298 #endif 299 300 #endif /* _KERN_ASSERT_H_ */ 301