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