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 struct mach_assert_hdr { 104 mach_assert_type_t type; 105 unsigned lineno : 24; 106 const char *filename; 107 } __attribute__((packed, aligned(4))); 108 109 struct mach_assert_default { 110 struct mach_assert_hdr hdr; 111 const char *expr; 112 } __attribute__((packed, aligned(4))); 113 114 struct mach_assert_3x { 115 struct mach_assert_hdr hdr; 116 const char *a; 117 const char *op; 118 const char *b; 119 } __attribute__((packed, aligned(4))); 120 121 #if MACH_ASSERT 122 # if XNU_KERNEL_PRIVATE 123 STATIC_IF_KEY_DECLARE_TRUE(mach_assert); 124 # define mach_assert_enabled() improbable_static_if(mach_assert) 125 # else 126 # define mach_assert_enabled() 1 127 # endif /* !XNU_KERNEL_PRIVATE */ 128 #elif APPLE_KEXT_ASSERTIONS 129 # define mach_assert_enabled() __builtin_expect(kext_assertions_enable, 0L) 130 #else /* !MACH_ASSERT && !APPLE_KEXT_ASSERTIONS */ 131 # define mach_assert_enabled() 0 132 #endif /* !MACH_ASSERT && !APPLE_KEXT_ASSERTIONS */ 133 134 #define MACH_ASSERT_TRAP_CODE 0xbffc /* XNU_HARD_TRAP_ASSERT_FAILURE */ 135 #define MACH_ASSERT_SEGSECT "__DATA_CONST,__assert" 136 137 /*! 138 * @abstract 139 * Wrap any arbitrary expression/code behind a conditional 140 * on whether assertions are enabled. 141 */ 142 #define MACH_ASSERT_DO(...) ({ \ 143 if (mach_assert_enabled()) { \ 144 __VA_ARGS__; \ 145 } \ 146 }) 147 148 #define mach_assert_abort(reason) ({ \ 149 __attribute__((used, section(MACH_ASSERT_SEGSECT))) \ 150 static const struct mach_assert_default __desc = { \ 151 { MACH_ASSERT_DEFAULT, __LINE__, __FILE_NAME__, }, \ 152 reason, \ 153 }; \ 154 \ 155 ml_fatal_trap_with_value(MACH_ASSERT_TRAP_CODE, &__desc); \ 156 }) 157 158 /*! 159 * @abstract 160 * assert() that is never elided or removed even in release builds. 161 */ 162 #define release_assert(ex) ({ \ 163 if (__builtin_expect(!(ex), 0L)) { \ 164 mach_assert_abort(#ex); \ 165 } \ 166 }) 167 168 #if MACH_ASSERT || APPLE_KEXT_ASSERTIONS 169 170 #define __assert_only 171 172 #define mach_assert_enabled_expr(ex) \ 173 (mach_assert_enabled() || __builtin_constant_p(!(ex))) 174 175 #define assert(ex) \ 176 (mach_assert_enabled_expr(ex) && !(ex) \ 177 ? (void)mach_assert_abort(#ex) : (void)0) 178 179 #define assertf(ex, fmt, args...) ({ \ 180 if (mach_assert_enabled_expr(ex) && __builtin_expect(!(ex), 0L)) { \ 181 __Panic("%s:%d Assertion failed: %s : " fmt, \ 182 __FILE_NAME__, __LINE__, # ex, ##args); \ 183 } \ 184 }) 185 186 /* 187 * Each of the following three macros takes three arguments instead of one for 188 * the assertion. The suffixes, 's', u' and 'p' indicate the type of arguments 189 * expected: 'signed', 'unsigned' or 'pointer' respectively. 190 * 191 * assert(a > b) -> file.c:123 Assertion failed: a > b 192 * assert3u(a, >, b) -> file.c:124 Assertion failed: a > b (1 >= 10) 193 * 194 */ 195 #define assert3u(a, op, b) ({ \ 196 if (mach_assert_enabled_expr((unsigned long long)(a) op \ 197 (unsigned long long)(b))) { \ 198 const unsigned long long a_ = (a); \ 199 const unsigned long long b_ = (b); \ 200 \ 201 if (__builtin_expect(!(a_ op b_), 0L)) { \ 202 __attribute__((used, section(MACH_ASSERT_SEGSECT))) \ 203 static const struct mach_assert_3x __desc3u = { \ 204 { MACH_ASSERT_3U, __LINE__, __FILE_NAME__, }, \ 205 #a, #op, #b, \ 206 }; \ 207 \ 208 ml_fatal_trap_with_value3(MACH_ASSERT_TRAP_CODE, \ 209 &__desc3u, a_, b_); \ 210 } \ 211 } \ 212 }) 213 214 #define assert3s(a, op, b) ({ \ 215 if (mach_assert_enabled_expr((long long)(a) op ((long long)b))) { \ 216 const signed long long a_ = (a); \ 217 const signed long long b_ = (b); \ 218 \ 219 if (__builtin_expect(!(a_ op b_), 0L)) { \ 220 __attribute__((used, section(MACH_ASSERT_SEGSECT))) \ 221 static const struct mach_assert_3x __desc3s = { \ 222 { MACH_ASSERT_3S, __LINE__, __FILE_NAME__, }, \ 223 #a, #op, #b, \ 224 }; \ 225 \ 226 ml_fatal_trap_with_value3(MACH_ASSERT_TRAP_CODE, \ 227 &__desc3s, a_, b_); \ 228 } \ 229 } \ 230 }) 231 232 #define assert3p(a, op, b) ({ \ 233 if (mach_assert_enabled_expr((const void *)(a) op (const void *)(b))) { \ 234 const void *a_ = (a); \ 235 const void *b_ = (b); \ 236 \ 237 if (__builtin_expect(!(a_ op b_), 0L)) { \ 238 __attribute__((used, section(MACH_ASSERT_SEGSECT))) \ 239 static const struct mach_assert_3x __desc3p = { \ 240 { MACH_ASSERT_3P, __LINE__, __FILE_NAME__, }, \ 241 #a, #op, #b, \ 242 }; \ 243 \ 244 ml_fatal_trap_with_value3(MACH_ASSERT_TRAP_CODE, \ 245 &__desc3p, a_, b_); \ 246 } \ 247 } \ 248 }) 249 250 #else /* !MACH_ASSERT && !XNU_KERNEL_PRIVATE */ 251 252 #define __assert_only __unused 253 #define mach_assert_enabled_expr(ex) 0 254 255 #define assert(ex) ((void)0) 256 #define assertf(ex, fmt, args...) ((void)0) 257 258 #define assert3s(a, op, b) ((void)0) 259 #define assert3u(a, op, b) ((void)0) 260 #define assert3p(a, op, b) ((void)0) 261 262 #endif /* !MACH_ASSERT && !XNU_KERNEL_PRIVATE */ 263 264 /* 265 * static_assert is a C11 / C++0x / C++1z feature. 266 * 267 * Beginning with C++0x, it is a keyword and should not be #defined 268 * 269 * static_assert is not disabled by MACH_ASSERT or NDEBUG 270 */ 271 272 #ifndef __cplusplus 273 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L 274 #define _STATIC_ASSERT_OVERLOADED_MACRO(_1, _2, NAME, ...) NAME 275 #define static_assert(...) _STATIC_ASSERT_OVERLOADED_MACRO(__VA_ARGS__, _static_assert_2_args, _static_assert_1_arg)(__VA_ARGS__) 276 277 #define _static_assert_2_args(ex, str) _Static_assert((ex), str) 278 #define _static_assert_1_arg(ex) _Static_assert((ex), #ex) 279 #endif 280 #else 281 #if !defined(__cpp_static_assert) 282 /* pre C++11 support */ 283 #define _STATIC_ASSERT_OVERLOADED_MACRO(_1, _2, NAME, ...) NAME 284 #define static_assert(...) _STATIC_ASSERT_OVERLOADED_MACRO(__VA_ARGS__, _static_assert_2_args, _static_assert_1_arg)(__VA_ARGS__) 285 286 #define _static_assert_2_args(ex, str) _Static_assert((ex), str) 287 #define _static_assert_1_arg(ex) _Static_assert((ex), #ex) 288 #else 289 /* 290 * C++11 only supports the 2 argument version of static_assert. 291 * C++1z has added support for the 1 argument version. 292 */ 293 #define _static_assert_1_arg(ex) static_assert((ex), #ex) 294 #endif 295 #endif 296 297 #endif /* _KERN_ASSERT_H_ */ 298