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>
30 #include <Availability.h>
31 #include <TargetConditionals.h>
40 /* This is useful for clients who wish for the messages generated by assumes()
41 * failures to go somewhere other than (or in addition to) the system log. If
42 * you don't wish for the message to be logged to the system log, then return
43 * true (to indicate that the message has been handled). If you want the default
44 * behavior, return false.
46 typedef bool (*os_redirect_t
)(const char *);
47 struct _os_redirect_assumes_s
{
48 os_redirect_t redirect
;
51 #define OS_ASSUMES_REDIRECT_SEG "__DATA"
52 #define OS_ASSUMES_REDIRECT_SECT "__os_assumes_log"
54 #define os_redirect_assumes(func) \
55 __attribute__((__used__)) \
56 __attribute__((__section__(OS_ASSUMES_REDIRECT_SEG "," OS_ASSUMES_REDIRECT_SECT))) \
57 static struct _os_redirect_assumes_s _os_redirect_##func = { \
61 /* The asl_message argument is a _SIMPLE_STRING that, when given to _simple_asl_send(), will
62 * direct the message to the MessageTracer diagnostic messages store rather than
63 * the default system log store.
65 typedef bool (*os_log_callout_t
)(_SIMPLE_STRING asl_message
, void *ctx
, const char *);
68 #define os_set_crash_message(arg) /* no-op */
70 #include <CrashReporterClient.h>
71 #define os_set_crash_message(arg) _crc_make_setter(message, (arg))
74 #define os_assumes(e) __extension__({ \
75 __typeof__(e) _e = os_fastpath(e); \
77 if (os_constant(e)) { \
78 __OS_COMPILETIME_ASSERT__(e); \
80 _os_assumes_log((uint64_t)(uintptr_t)_e); \
81 _os_avoid_tail_call(); \
86 #define os_assumes_zero(e) __extension__({ \
87 __typeof__(e) _e = os_slowpath(e); \
89 if (os_constant(e)) { \
90 __OS_COMPILETIME_ASSERT__(!e); \
92 _os_assumes_log((uint64_t)(uintptr_t)_e); \
93 _os_avoid_tail_call(); \
98 /* This variant is for use with old-style POSIX APIs that return -1 on failure
99 * and set errno. If the return code is -1, the value logged will be as though
100 * os_assumes_zero(errno) was used. It encapsulates the following pattern:
103 * if (pipe(tubes) == -1) {
104 * (void)os_assumes_zero(errno);
107 #define posix_assumes_zero(e) __extension__({ \
108 __typeof__(e) _e = os_slowpath(e); \
109 if (_e == (typeof(e))-1) { \
110 _os_assumes_log((uint64_t)(uintptr_t)errno); \
111 _os_avoid_tail_call(); \
116 #define os_assert(e) __extension__({ \
117 __typeof__(e) _e = os_fastpath(e); \
119 if (os_constant(e)) { \
120 __OS_COMPILETIME_ASSERT__(e); \
123 char *_fail_message = _os_assert_log((uint64_t)(uintptr_t)_e); \
124 os_set_crash_message(_fail_message); \
125 os_hardware_trap(); \
126 free(_fail_message); \
130 #define os_assert_zero(e) __extension__({ \
131 __typeof__(e) _e = os_slowpath(e); \
133 if (os_constant(e)) { \
134 __OS_COMPILETIME_ASSERT__(!e); \
137 char *_fail_message = _os_assert_log((uint64_t)(uintptr_t)_e); \
138 os_set_crash_message(_fail_message); \
139 os_hardware_trap(); \
140 free(_fail_message); \
144 #define posix_assert_zero(e) __extension__({ \
145 __typeof__(e) _e = os_slowpath(e); \
146 if (_e == (__typeof__(e))-1) { \
147 char *_fail_message = _os_assert_log((uint64_t)(uintptr_t)errno); \
148 os_set_crash_message(_fail_message); \
149 os_hardware_trap(); \
150 free(_fail_message); \
154 /* These are for defining your own assumes()-like wrapper calls so that you can
155 * log additional information, such as the about-PID, sender, etc. They're
156 * generally not useful for direct inclusion in your code.
158 #define os_assumes_ctx(f, ctx, e) __extension__({ \
159 __typeof__(e) _e = os_fastpath(e); \
161 if (os_constant(e)) { \
162 __OS_COMPILETIME_ASSERT__(e); \
164 _os_assumes_log_ctx(f, ctx, (uintptr_t)_e); \
165 _os_avoid_tail_call(); \
170 #define os_assumes_zero_ctx(f, ctx, e) __extension__({ \
171 __typeof__(e) _e = os_slowpath(e); \
173 if (os_constant(e)) { \
174 __OS_COMPILETIME_ASSERT__(!e); \
176 _os_assumes_log_ctx((f), (ctx), (uintptr_t)_e); \
177 _os_avoid_tail_call(); \
182 #define posix_assumes_zero_ctx(f, ctx, e) __extension__({ \
183 __typeof__(e) _e = os_slowpath(e); \
184 if (_e == (__typeof__(e))-1) { \
185 _os_assumes_log_ctx((f), (ctx), (uintptr_t)errno); \
186 _os_avoid_tail_call(); \
191 #define os_assert_ctx(f, ctx, e) __extension__({ \
192 __typeof__(e) _e = os_fastpath(e); \
194 if (os_constant(e)) { \
195 __OS_COMPILETIME_ASSERT__(e); \
198 char *_fail_message = _os_assert_log_ctx((f), (ctx), (uint64_t)(uintptr_t)_e); \
199 os_set_crash_message(_fail_message); \
200 os_hardware_trap(); \
201 free(_fail_message); \
205 #define os_assert_zero_ctx(f, ctx, e) __extension__({ \
206 __typeof__(e) _e = os_slowpath(e); \
208 if (os_constant(e)) { \
209 __OS_COMPILETIME_ASSERT__(!e); \
212 char *_fail_message = _os_assert_log_ctx((f), (ctx), (uint64_t)(uintptr_t)_e); \
213 os_set_crash_message(_fail_message); \
214 os_hardware_trap(); \
215 free(_fail_message); \
219 #define posix_assert_zero_ctx(f, ctx, e) __extension__({ \
220 __typeof__(e) _e = os_slowpath(e); \
221 if (_e == (__typeof__(e))-1) { \
222 char *_fail_message = _os_assert_log_ctx((f), (ctx), (uint64_t)(uintptr_t)errno); \
223 os_set_crash_message(_fail_message); \
224 os_hardware_trap(); \
225 free(_fail_message); \
229 __OSX_AVAILABLE_STARTING(__MAC_10_9
, __IPHONE_6_0
)
231 _os_assumes_log(uint64_t code
);
233 __OSX_AVAILABLE_STARTING(__MAC_10_9
, __IPHONE_6_0
)
235 _os_assert_log(uint64_t code
);
237 __OSX_AVAILABLE_STARTING(__MAC_10_9
, __IPHONE_6_0
)
239 _os_assumes_log_ctx(os_log_callout_t callout
, void *ctx
, uint64_t code
);
241 __OSX_AVAILABLE_STARTING(__MAC_10_9
, __IPHONE_6_0
)
243 _os_assert_log_ctx(os_log_callout_t callout
, void *ctx
, uint64_t code
);
245 __OSX_AVAILABLE_STARTING(__MAC_10_9
, __IPHONE_6_0
)
247 _os_avoid_tail_call(void);
251 #endif /* __OS_ASSUMES_H__ */