1 /* Copyright (c) 2012, 2012 Apple Inc. All rights reserved.
3 * @APPLE_LICENSE_HEADER_START@
5 * This file contains Original Code and/or Modifications of Original Code
6 * as defined in and that are subject to the Apple Public Source License
7 * Version 2.0 (the 'License'). You may not use this file except in
8 * compliance with the License. Please obtain a copy of the License at
9 * http://www.opensource.apple.com/apsl/ and read it before using this
12 * The Original Code and all software distributed under the License are
13 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
17 * Please see the License for the specific language governing rights and
18 * limitations under the License.
20 * @APPLE_LICENSE_HEADER_END@
23 #ifndef __OS_ASSUMES_H__
24 #define __OS_ASSUMES_H__
26 #include <sys/cdefs.h>
31 #include <Availability.h>
32 #include <TargetConditionals.h>
39 #include <os/base_private.h>
43 #define os_constant(x) __builtin_constant_p((x))
44 #define os_hardware_trap() __asm__ __volatile__ (""); __builtin_trap()
45 #define __OS_COMPILETIME_ASSERT__(e) __extension__({ \
46 _Pragma("clang diagnostic push") \
47 _Pragma("clang diagnostic ignored \"-Wvla\"") \
48 char __compile_time_assert__[(e) ? 1 : -1]; \
49 (void)__compile_time_assert__; \
50 _Pragma("clang diagnostic pop") \
53 #define os_constant(x) ((long)0)
54 #define os_hardware_trap() abort()
55 #define __OS_COMPILETIME_ASSERT__(e) (e)
61 * os_crash() is like os_hardware_trap(), except you get to pass in a crash
62 * message, and it can be redirected to a callback function using
63 * os_set_crash_callback()
66 #define __os_crash_simple(msg) \
72 #if defined(OS_CRASH_ENABLE_EXPERIMENTAL_LIBTRACE)
73 #include <os/log_private.h>
75 #define __os_crash_fmt(...) \
77 const size_t size = os_log_pack_size(__VA_ARGS__); \
78 uint8_t buf[size] __attribute__((aligned(alignof(os_log_pack_s)))); \
79 os_log_pack_t pack = (os_log_pack_t)&buf; \
80 os_log_pack_fill(pack, size, errno, __VA_ARGS__); \
81 _os_crash_fmt(pack, size); \
85 #define __os_crash_N(msg) __os_crash_simple(msg)
86 #define __os_crash_Y(...) __os_crash_fmt(__VA_ARGS__)
88 // Returns Y if >1 argument, N if just one argument.
89 #define __thirty_second_argument(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, \
90 _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, \
91 _26, _27, _28, _29, _30, _31, _32, ...) _32
92 #define __has_more_than_one_argument(...) __thirty_second_argument(__VA_ARGS__, \
93 Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, \
94 Y, Y, Y, Y, Y, Y, Y, N, EXTRA)
96 #define __os_crash_invoke(variant, ...) \
97 OS_CONCAT(__os_crash_, variant)(__VA_ARGS__)
99 #define os_crash(...) \
100 __os_crash_invoke(__has_more_than_one_argument(__VA_ARGS__), __VA_ARGS__)
104 _os_crash_fmt(os_log_pack_t
, size_t);
107 * @function os_assert_sprintf
108 * A routine to assert the result of a call to snprintf(3) or vsnprintf(3).
111 * The return value from {v}snprintf(3).
114 * The size of the buffer given to {v}snprintf(3).
117 * If ret is less than zero or greater than size, the routine will abort the
118 * caller with a message indicating the nature of the failure in the Application
119 * Specific Information section of the resulting crash log.
121 * This routine is useful for printing paths that are expected to succeed with a
122 * statically-sized buffer.
124 API_AVAILABLE(macos(10.14), ios(12.0), tvos(12.0), watchos(5.0))
125 OS_ALWAYS_INLINE OS_COLD
127 os_assert_sprintf(int ret
, size_t buff_size
)
130 os_crash("error printing buffer: %s", strerror(errno
));
133 if ((size_t)ret
> buff_size
) {
134 os_crash("buffer too small: needed = %d, actual = %lu",
140 * @function os_assert_asprintf
141 * A routine to assert the result of a call to {v}asprintf(3).
144 * The return value from {v}asnprintf(3).
147 * If ret is less than zero, the routine will abort the caller with a message
148 * indicating the nature of the failure in the Application Specific Information
149 * section of the resulting crash log.
151 API_AVAILABLE(macos(10.15.2), ios(13.3), tvos(13.3), watchos(6.1.1))
152 OS_ALWAYS_INLINE OS_COLD
154 os_assert_asprintf(int ret
)
157 os_crash("error printing buffer: %s", strerror(errno
));
162 * @function os_assert_malloc
163 * A routine to assert the result of allocations which may fail.
166 * A string describing the object whose allocation was attempted.
169 * The result of a call to malloc(3), calloc(3), et al.
172 * The size of the attempted allocation.
175 * If {@link p} is NULL, the routine will abort the caller with a message
176 * indicating the nature of the failure in the Application Specific Information
177 * section of the resulting crash log.
179 API_AVAILABLE(macos(10.14), ios(12.0), tvos(12.0), watchos(5.0))
182 os_assert_malloc(const char *desc
, const void *const p
, size_t alloc_size
)
185 os_crash("allocation failed: obj = %s, size = %lu, error = %s",
186 desc
, alloc_size
, strerror(errno
));
191 * @function os_assert_mach
192 * A routine to assert the result of a Mach kernel routine.
195 * A human-readable description of the operation.
201 * If {@link kr} is non-zero, this routine will abort the caller with a message
202 * indicating the nature of the failure in the Application Specific Information
203 * section of the resulting crash log.
205 API_AVAILABLE(macos(10.14), ios(12.0), tvos(12.0), watchos(5.0))
206 OS_EXPORT OS_NONNULL1 OS_COLD
208 os_assert_mach(const char *op
, kern_return_t kr
);
210 #define os_assert_mach(op, kr) ({ \
211 kern_return_t __kr = (kr); \
212 if (os_unlikely(__kr != KERN_SUCCESS)) os_assert_mach(op, kr); \
216 * @function os_assert_mach_port_status
217 * A routine to assert the status of a Mach port.
220 * A human-readable description of the port's purpose.
226 * A pointer to a mach_port_status_t describing the expected attributes of the
227 * port. If no particular value is expected for a given field in the structure,
228 * a sentinel value may be provided for each expected field to indicate that its
229 * check should be elided. The sentival values are:
231 * mps_pset => UINT32_MAX
232 * mps_seqno => UINT32_MAX
233 * mps_mscount => UINT32_MAX
234 * mps_qlimit => UINT32_MAX
235 * mps_msgcount => UINT32_MAX
236 * mps_sorights => UINT32_MAX
237 * mps_srights => INT32_MAX
238 * mps_pdrequest => INT32_MAX
239 * mps_nsrequest => INT32_MAX
243 * If there are any mismatches in the expected and actual status of the port,
244 * the implementation will abort the caller. If status cannot be obtained for
245 * the given port, the implementation will abort the caller.
247 API_AVAILABLE(macos(10.14), ios(12.0), tvos(12.0), watchos(5.0))
250 os_assert_mach_port_status(const char *desc
, mach_port_t p
,
251 mach_port_status_t
*expected
);
253 #else // OS_CRASH_ENABLE_EXPERIMENTAL_LIBTRACE
255 #define os_crash(msg) __os_crash_simple(msg)
257 #endif // OS_CRASH_ENABLE_EXPERIMENTAL_LIBTRACE
260 * An executable can register a callback to be made upon a crash using the
261 * os_set_crash_callback function. If a crash callback is not set, the symbol
262 * `os_crash_function` will be called in the main binary, if it exists.
265 typedef void (*os_crash_callback_t
) (const char *);
267 /* private: use accessors below */
268 extern os_crash_callback_t _os_crash_callback
;
270 static inline os_crash_callback_t
271 os_get_crash_callback() {
272 return _os_crash_callback
;
276 os_set_crash_callback(os_crash_callback_t callback
) {
277 _os_crash_callback
= callback
;
280 #pragma mark os_assert
282 #if defined(OS_CRASH_ENABLE_EXPERIMENTAL_LIBTRACE)
284 #define _os_assert_crash(value, expression) ({ \
285 os_crash("assertion failure: \"" expression "\" -> %lld", value); \
288 #define _os_assert_crash_errno(value, expression) ({ \
289 os_crash("assertion failure: \"" expression "\" -> %{errno}d", value); \
292 #else // OS_CRASH_ENABLE_EXPERIMENTAL_LIBTRACE
294 #define _os_assert_crash(e, ...) ({ \
295 char *_fail_message = _os_assert_log(e); \
296 os_crash(_fail_message); \
297 free(_fail_message); \
300 #define _os_assert_crash_errno(...) _os_assert_crash(__VA_ARGS__)
302 #endif // OS_CRASH_ENABLE_EXPERIMENTAL_LIBTRACE
304 #define __os_assert(e) __extension__({ \
305 __typeof__(e) _e = (e); \
306 if (os_unlikely(!_e)) { \
307 if (os_constant(e)) { __OS_COMPILETIME_ASSERT__((e)); } \
308 _os_assert_crash((uint64_t)(uintptr_t)_e, #e); \
312 #define __os_assert_zero(e) __extension__({ \
313 __typeof__(e) _e = (e); \
314 if (os_unlikely(_e)) { \
315 if (os_constant(e)) { __OS_COMPILETIME_ASSERT__(!(e)); } \
316 _os_assert_crash((uint64_t)(uintptr_t)_e, #e); \
321 * This variant is for use with old-style POSIX APIs that return -1 on failure
322 * and set errno. If the return code is -1, the value logged will be as though
323 * os_assert_zero(errno) was used. It encapsulates the following pattern:
326 * if (pipe(tubes) == -1) {
327 * (void)os_assert_zero(errno);
330 #define __posix_assert_zero(e) __extension__({ \
331 __typeof__(e) _e = (e); \
332 if (os_unlikely(_e == (__typeof__(e))-1)) { \
333 _os_assert_crash_errno(errno, #e); \
337 #if defined(OS_CRASH_ENABLE_EXPERIMENTAL_LIBTRACE)
339 #define __os_assert_msg(e, fmt, ...) __extension__({ \
340 __typeof__(e) _e = (e); \
341 if (os_unlikely(!_e)) { \
342 os_crash("assertion failure: " fmt, ##__VA_ARGS__); \
346 #define __os_assert_zero_msg(e, fmt, ...) __extension__({ \
347 __typeof__(e) _e = (e); \
348 if (os_unlikely(_e)) { \
349 os_crash("assertion failure (%lld): " fmt, value, ##__VA_ARGS__); \
353 #define __posix_assert_zero_msg(e, fmt, ...) __extension__({ \
354 __typeof__(e) _e = (e); \
355 if (os_unlikely(_e == (__typeof__(e))-1)) { \
356 os_crash("assertion failure (%{errno}d): " fmt, errno, ##__VA_ARGS__); \
360 #define __os_assert_N(e) __os_assert(e)
361 #define __os_assert_Y(...) __os_assert_msg(__VA_ARGS__)
362 #define __os_assert_zero_N(e) __os_assert_zero(e)
363 #define __os_assert_zero_Y(...) __os_assert_zero_msg(__VA_ARGS__)
364 #define __posix_assert_zero_N(e) __posix_assert_zero(e)
365 #define __posix_assert_zero_Y(...) __posix_assert_zero_msg(__VA_ARGS__)
367 #define __os_assert_invoke(function, variant, ...) \
368 OS_CONCAT(function, variant)(__VA_ARGS__)
370 #define os_assert(...) \
371 __os_assert_invoke(__os_assert_, \
372 __has_more_than_one_argument(__VA_ARGS__), __VA_ARGS__)
373 #define os_assert_zero(...) \
374 __os_assert_invoke(__os_assert_zero_, \
375 __has_more_than_one_argument(__VA_ARGS__), __VA_ARGS__)
376 #define posix_assert_zero(...) \
377 __os_assert_invoke(__posix_assert_zero_, \
378 __has_more_than_one_argument(__VA_ARGS__), __VA_ARGS__)
380 #else // OS_CRASH_ENABLE_EXPERIMENTAL_LIBTRACE
382 #define os_assert(e) __os_assert(e)
383 #define os_assert_zero(e) __os_assert_zero(e)
384 #define posix_assert_zero(e) __posix_assert_zero(e)
388 #pragma mark os_assumes
391 #define os_assumes(e) __extension__({ \
392 __typeof__(e) _e = os_fastpath(e); \
394 if (os_constant(e)) { \
395 __OS_COMPILETIME_ASSERT__(e); \
397 _os_assumes_log((uint64_t)(uintptr_t)_e); \
402 #define os_assumes_zero(e) __extension__({ \
403 __typeof__(e) _e = os_slowpath(e); \
405 if (os_constant(e)) { \
406 __OS_COMPILETIME_ASSERT__(!(e)); \
408 _os_assumes_log((uint64_t)(uintptr_t)_e); \
413 #define posix_assumes_zero(e) __extension__({ \
414 __typeof__(e) _e = os_slowpath(e); \
415 if (_e == (__typeof__(e))-1) { \
416 _os_assumes_log((uint64_t)(uintptr_t)errno); \
421 #pragma mark assumes redirection
423 /* This is useful for clients who wish for the messages generated by assumes()
424 * failures to go somewhere other than (or in addition to) the system log. If
425 * you don't wish for the message to be logged to the system log, then return
426 * true (to indicate that the message has been handled). If you want the default
427 * behavior, return false.
429 typedef bool (*os_redirect_t
)(const char *);
430 struct _os_redirect_assumes_s
{
431 os_redirect_t redirect
;
434 #define OS_ASSUMES_REDIRECT_SEG "__DATA"
435 #define OS_ASSUMES_REDIRECT_SECT "__os_assumes_log"
437 #define os_redirect_assumes(func) \
438 __attribute__((__used__)) \
439 __attribute__((__section__(OS_ASSUMES_REDIRECT_SEG "," OS_ASSUMES_REDIRECT_SECT))) \
440 static struct _os_redirect_assumes_s _os_redirect_##func = { \
444 #pragma mark _ctx variants
447 * These are for defining your own assumes()-like wrapper calls so that you can
448 * log additional information, such as the about-PID, sender, etc. They're
449 * generally not useful for direct inclusion in your code.
453 * The asl_message argument is a _SIMPLE_STRING that, when given to _simple_asl_send(), will
454 * direct the message to the MessageTracer diagnostic messages store rather than
455 * the default system log store.
457 typedef bool (*os_log_callout_t
)(_SIMPLE_STRING asl_message
, void *ctx
, const char *);
460 #define os_assumes_ctx(f, ctx, e) __extension__({ \
461 __typeof__(e) _e = os_fastpath(e); \
463 if (os_constant(e)) { \
464 __OS_COMPILETIME_ASSERT__(e); \
466 _os_assumes_log_ctx(f, ctx, (uintptr_t)_e); \
471 #define os_assumes_zero_ctx(f, ctx, e) __extension__({ \
472 __typeof__(e) _e = os_slowpath(e); \
474 if (os_constant(e)) { \
475 __OS_COMPILETIME_ASSERT__(!(e)); \
477 _os_assumes_log_ctx((f), (ctx), (uintptr_t)_e); \
482 #define posix_assumes_zero_ctx(f, ctx, e) __extension__({ \
483 __typeof__(e) _e = os_slowpath(e); \
484 if (_e == (__typeof__(e))-1) { \
485 _os_assumes_log_ctx((f), (ctx), (uintptr_t)errno); \
490 #define os_assert_ctx(f, ctx, e) __extension__({ \
491 __typeof__(e) _e = os_fastpath(e); \
493 if (os_constant(e)) { \
494 __OS_COMPILETIME_ASSERT__(e); \
497 char *_fail_message = _os_assert_log_ctx((f), (ctx), (uint64_t)(uintptr_t)_e); \
498 os_crash(_fail_message); \
499 free(_fail_message); \
503 #define os_assert_zero_ctx(f, ctx, e) __extension__({ \
504 __typeof__(e) _e = os_slowpath(e); \
506 if (os_constant(e)) { \
507 __OS_COMPILETIME_ASSERT__(!(e)); \
510 char *_fail_message = _os_assert_log_ctx((f), (ctx), (uint64_t)(uintptr_t)_e); \
511 os_crash(_fail_message); \
512 free(_fail_message); \
516 #define posix_assert_zero_ctx(f, ctx, e) __extension__({ \
517 __typeof__(e) _e = os_slowpath(e); \
518 if (_e == (__typeof__(e))-1) { \
519 char *_fail_message = _os_assert_log_ctx((f), (ctx), (uint64_t)(uintptr_t)errno); \
520 os_crash(_fail_message); \
521 free(_fail_message); \
525 #pragma mark internal symbols
527 __OSX_AVAILABLE_STARTING(__MAC_10_11
, __IPHONE_9_0
)
528 OS_COLD OS_NOT_TAIL_CALLED
530 _os_crash(const char *);
532 __OSX_AVAILABLE_STARTING(__MAC_10_9
, __IPHONE_6_0
)
533 OS_COLD OS_NOT_TAIL_CALLED
535 _os_assumes_log(uint64_t code
);
537 __OSX_AVAILABLE_STARTING(__MAC_10_9
, __IPHONE_6_0
)
538 OS_COLD OS_NOT_TAIL_CALLED
540 _os_assert_log(uint64_t code
);
542 __OSX_AVAILABLE_STARTING(__MAC_10_9
, __IPHONE_6_0
)
543 OS_COLD OS_NOT_TAIL_CALLED
545 _os_assumes_log_ctx(os_log_callout_t callout
, void *ctx
, uint64_t code
);
547 __OSX_AVAILABLE_STARTING(__MAC_10_9
, __IPHONE_6_0
)
548 OS_COLD OS_NOT_TAIL_CALLED
550 _os_assert_log_ctx(os_log_callout_t callout
, void *ctx
, uint64_t code
);
552 __OSX_AVAILABLE_STARTING(__MAC_10_9
, __IPHONE_6_0
)
554 _os_avoid_tail_call(void);
558 #endif /* __OS_ASSUMES_H__ */