2 * Copyright (C) 2003, 2006, 2007 Apple Inc. All rights reserved.
3 * Copyright (C) 2013 Google Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #ifndef WTF_Assertions_h
28 #define WTF_Assertions_h
31 No namespaces because this file has to be includable from C and Objective-C.
33 Note, this file uses many GCC extensions, but it should be compatible with
34 C, Objective C, C++, and Objective C++.
36 For non-debug builds, everything is disabled by default, except for the
37 RELEASE_ASSERT family of macros.
39 Defining any of the symbols explicitly prevents this from having any effect.
43 #include "wtf/Compiler.h"
44 #include "wtf/WTFExport.h"
46 // Users must test "#if ENABLE(ASSERT)", which helps ensure that code
47 // testing this macro has included this header.
50 /* Disable ASSERT* macros in release mode by default. */
51 #define ENABLE_ASSERT 0
53 #define ENABLE_ASSERT 1
57 #ifndef BACKTRACE_DISABLED
58 #define BACKTRACE_DISABLED !ENABLE(ASSERT)
61 #ifndef ASSERT_MSG_DISABLED
62 #define ASSERT_MSG_DISABLED !ENABLE(ASSERT)
65 #ifndef ASSERT_ARG_DISABLED
66 #define ASSERT_ARG_DISABLED !ENABLE(ASSERT)
69 #ifndef FATAL_DISABLED
70 #define FATAL_DISABLED !ENABLE(ASSERT)
73 #ifndef ERROR_DISABLED
74 #define ERROR_DISABLED !ENABLE(ASSERT)
78 #define LOG_DISABLED !ENABLE(ASSERT)
81 /* WTF logging functions can process %@ in the format string to log a NSObject* but the printf format attribute
82 emits a warning when %@ is used in the format string. Until <rdar://problem/5195437> is resolved we can't include
83 the attribute when being used from Objective-C code in case it decides to use %@. */
84 #if COMPILER(GCC) && !defined(__OBJC__)
85 #define WTF_ATTRIBUTE_PRINTF(formatStringArgument, extraArguments) __attribute__((__format__(printf, formatStringArgument, extraArguments)))
87 #define WTF_ATTRIBUTE_PRINTF(formatStringArgument, extraArguments)
90 /* These helper functions are always declared, but not necessarily always defined if the corresponding function is disabled. */
96 typedef enum { WTFLogChannelOff, WTFLogChannelOn } WTFLogChannelState;
99 WTFLogChannelState state;
102 WTF_EXPORT void WTFReportAssertionFailure(const char* file, int line, const char* function, const char* assertion);
103 WTF_EXPORT void WTFReportAssertionFailureWithMessage(const char* file, int line, const char* function, const char* assertion, const char* format, ...) WTF_ATTRIBUTE_PRINTF(5, 6);
104 WTF_EXPORT void WTFReportArgumentAssertionFailure(const char* file, int line, const char* function, const char* argName, const char* assertion);
105 WTF_EXPORT void WTFReportFatalError(const char* file, int line, const char* function, const char* format, ...) WTF_ATTRIBUTE_PRINTF(4, 5);
106 WTF_EXPORT void WTFReportError(const char* file, int line, const char* function, const char* format, ...) WTF_ATTRIBUTE_PRINTF(4, 5);
107 WTF_EXPORT void WTFLog(WTFLogChannel*, const char* format, ...) WTF_ATTRIBUTE_PRINTF(2, 3);
108 WTF_EXPORT void WTFLogVerbose(const char* file, int line, const char* function, WTFLogChannel*, const char* format, ...) WTF_ATTRIBUTE_PRINTF(5, 6);
109 WTF_EXPORT void WTFLogAlways(const char* format, ...) WTF_ATTRIBUTE_PRINTF(1, 2);
111 WTF_EXPORT void WTFGetBacktrace(void** stack, int* size);
112 WTF_EXPORT void WTFReportBacktrace(int framesToShow = 31);
113 WTF_EXPORT void WTFPrintBacktrace(void** stack, int size);
115 typedef void (*WTFCrashHookFunction)();
116 WTF_EXPORT void WTFSetCrashHook(WTFCrashHookFunction);
117 WTF_EXPORT void WTFInvokeCrashHook();
118 WTF_EXPORT void WTFInstallReportBacktraceOnCrashHook();
125 class WTF_EXPORT FrameToNameScope {
127 explicit FrameToNameScope(void*);
129 const char* nullableName() { return m_name; }
133 char* m_cxaDemangled;
138 using WTF::FrameToNameScope;
141 /* IMMEDIATE_CRASH() - Like CRASH() below but crashes in the fastest, simplest possible way with no attempt at logging. */
142 #ifndef IMMEDIATE_CRASH
144 #define IMMEDIATE_CRASH() __builtin_trap()
146 #define IMMEDIATE_CRASH() ((void(*)())0)()
150 /* CRASH() - Raises a fatal error resulting in program termination and triggering either the debugger or the crash reporter.
152 Use CRASH() in response to known, unrecoverable errors like out-of-memory.
153 Macro is enabled in both debug and release mode.
154 To test for unknown errors and verify assumptions, use ASSERT instead, to avoid impacting performance in release builds.
156 Signals are ignored by the crash reporter on OS X so we must do better.
160 (WTFReportBacktrace(), \
161 WTFInvokeCrashHook(), \
162 (*(int*)0xfbadbeef = 0), \
167 #define NO_RETURN_DUE_TO_CRASH NO_RETURN
169 #define NO_RETURN_DUE_TO_CRASH
174 Print a backtrace to the same location as ASSERT messages.
176 #if BACKTRACE_DISABLED
178 #define BACKTRACE() ((void)0)
182 #define BACKTRACE() do { \
183 WTFReportBacktrace(); \
188 /* ASSERT, ASSERT_NOT_REACHED, ASSERT_UNUSED
190 These macros are compiled out of release builds.
191 Expressions inside them are evaluated in debug builds only.
194 /* FIXME: Change to use something other than ASSERT to avoid this conflict with the underlying platform */
200 #define ASSERT(assertion) \
202 (WTFReportAssertionFailure(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #assertion), \
206 #define ASSERT_AT(assertion, file, line, function) \
208 (WTFReportAssertionFailure(file, line, function, #assertion), \
212 #define ASSERT_NOT_REACHED() do { \
213 WTFReportAssertionFailure(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, 0); \
217 #define ASSERT_UNUSED(variable, assertion) ASSERT(assertion)
219 #define NO_RETURN_DUE_TO_ASSERT NO_RETURN_DUE_TO_CRASH
223 #define ASSERT(assertion) ((void)0)
224 #define ASSERT_AT(assertion, file, line, function) ((void)0)
225 #define ASSERT_NOT_REACHED() ((void)0)
226 #define NO_RETURN_DUE_TO_ASSERT
228 #define ASSERT_UNUSED(variable, assertion) ((void)variable)
232 /* ASSERT_WITH_SECURITY_IMPLICATION / RELEASE_ASSERT_WITH_SECURITY_IMPLICATION
234 Use in places where failure of the assertion indicates a possible security
235 vulnerability. Classes of these vulnerabilities include bad casts, out of
236 bounds accesses, use-after-frees, etc. Please be sure to file bugs for these
237 failures using the security template:
238 http://code.google.com/p/chromium/issues/entry?template=Security%20Bug
240 #ifdef ADDRESS_SANITIZER
242 #define ASSERT_WITH_SECURITY_IMPLICATION(assertion) \
244 (WTFReportAssertionFailure(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #assertion), \
248 #define RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(assertion) ASSERT_WITH_SECURITY_IMPLICATION(assertion)
252 #define ASSERT_WITH_SECURITY_IMPLICATION(assertion) ASSERT(assertion)
253 #define RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(assertion) RELEASE_ASSERT(assertion)
257 // Users must test "#if ENABLE(SECURITY_ASSERT)", which helps ensure
258 // that code testing this macro has included this header.
259 #if defined(ADDRESS_SANITIZER) || ENABLE(ASSERT)
260 #define ENABLE_SECURITY_ASSERT 1
262 #define ENABLE_SECURITY_ASSERT 0
265 /* ASSERT_WITH_MESSAGE */
267 #if ASSERT_MSG_DISABLED
268 #define ASSERT_WITH_MESSAGE(assertion, ...) ((void)0)
270 #define ASSERT_WITH_MESSAGE(assertion, ...) do \
271 if (!(assertion)) { \
272 WTFReportAssertionFailureWithMessage(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #assertion, __VA_ARGS__); \
278 /* ASSERT_WITH_MESSAGE_UNUSED */
280 #if ASSERT_MSG_DISABLED
281 #define ASSERT_WITH_MESSAGE_UNUSED(variable, assertion, ...) ((void)variable)
283 #define ASSERT_WITH_MESSAGE_UNUSED(variable, assertion, ...) do \
284 if (!(assertion)) { \
285 WTFReportAssertionFailureWithMessage(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #assertion, __VA_ARGS__); \
293 #if ASSERT_ARG_DISABLED
295 #define ASSERT_ARG(argName, assertion) ((void)0)
299 #define ASSERT_ARG(argName, assertion) do \
300 if (!(assertion)) { \
301 WTFReportArgumentAssertionFailure(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #argName, #assertion); \
309 #ifndef COMPILE_ASSERT
310 #if COMPILER_SUPPORTS(C_STATIC_ASSERT)
311 /* Unlike static_assert below, this also works in plain C code. */
312 #define COMPILE_ASSERT(exp, name) _Static_assert((exp), #name)
313 #elif COMPILER_SUPPORTS(CXX_STATIC_ASSERT)
314 #define COMPILE_ASSERT(exp, name) static_assert((exp), #name)
316 #define COMPILE_ASSERT(exp, name) typedef int dummy##name [(exp) ? 1 : -1]
323 #define FATAL(...) ((void)0)
325 #define FATAL(...) do { \
326 WTFReportFatalError(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, __VA_ARGS__); \
334 #define WTF_LOG_ERROR(...) ((void)0)
336 #define WTF_LOG_ERROR(...) WTFReportError(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, __VA_ARGS__)
342 #define WTF_LOG(channel, ...) ((void)0)
344 #define WTF_LOG(channel, ...) WTFLog(&JOIN_LOG_CHANNEL_WITH_PREFIX(LOG_CHANNEL_PREFIX, channel), __VA_ARGS__)
345 #define JOIN_LOG_CHANNEL_WITH_PREFIX(prefix, channel) JOIN_LOG_CHANNEL_WITH_PREFIX_LEVEL_2(prefix, channel)
346 #define JOIN_LOG_CHANNEL_WITH_PREFIX_LEVEL_2(prefix, channel) prefix ## channel
349 /* UNREACHABLE_FOR_PLATFORM */
352 /* This would be a macro except that its use of #pragma works best around
353 a function. Hence it uses macro naming convention. */
354 #pragma clang diagnostic push
355 #pragma clang diagnostic ignored "-Wmissing-noreturn"
356 static inline void UNREACHABLE_FOR_PLATFORM()
358 ASSERT_NOT_REACHED();
360 #pragma clang diagnostic pop
362 #define UNREACHABLE_FOR_PLATFORM() ASSERT_NOT_REACHED()
367 Use in places where failure of an assertion indicates a definite security
368 vulnerability from which execution must not continue even in a release build.
369 Please sure to file bugs for these failures using the security template:
370 http://code.google.com/p/chromium/issues/entry?template=Security%20Bug
374 #define RELEASE_ASSERT(assertion) ASSERT(assertion)
375 #define RELEASE_ASSERT_WITH_MESSAGE(assertion, ...) ASSERT_WITH_MESSAGE(assertion, __VA_ARGS__)
376 #define RELEASE_ASSERT_NOT_REACHED() ASSERT_NOT_REACHED()
378 #define RELEASE_ASSERT(assertion) (UNLIKELY(!(assertion)) ? (IMMEDIATE_CRASH()) : (void)0)
379 #define RELEASE_ASSERT_WITH_MESSAGE(assertion, ...) RELEASE_ASSERT(assertion)
380 #define RELEASE_ASSERT_NOT_REACHED() IMMEDIATE_CRASH()
383 /* DEFINE_COMPARISON_OPERATORS_WITH_REFERENCES */
385 // Allow equality comparisons of Objects by reference or pointer, interchangeably.
386 // This can be only used on types whose equality makes no other sense than pointer equality.
387 #define DEFINE_COMPARISON_OPERATORS_WITH_REFERENCES(thisType) \
388 inline bool operator==(const thisType& a, const thisType& b) { return &a == &b; } \
389 inline bool operator==(const thisType& a, const thisType* b) { return &a == b; } \
390 inline bool operator==(const thisType* a, const thisType& b) { return a == &b; } \
391 inline bool operator!=(const thisType& a, const thisType& b) { return !(a == b); } \
392 inline bool operator!=(const thisType& a, const thisType* b) { return !(a == b); } \
393 inline bool operator!=(const thisType* a, const thisType& b) { return !(a == b); }
395 #define DEFINE_COMPARISON_OPERATORS_WITH_REFERENCES_REFCOUNTED(thisType) \
396 DEFINE_COMPARISON_OPERATORS_WITH_REFERENCES(thisType) \
397 inline bool operator==(const PassRefPtr<thisType>& a, const thisType& b) { return a.get() == &b; } \
398 inline bool operator==(const thisType& a, const PassRefPtr<thisType>& b) { return &a == b.get(); } \
399 inline bool operator!=(const PassRefPtr<thisType>& a, const thisType& b) { return !(a == b); } \
400 inline bool operator!=(const thisType& a, const PassRefPtr<thisType>& b) { return !(a == b); }
402 /* DEFINE_TYPE_CASTS */
404 #define DEFINE_TYPE_CASTS(thisType, argumentType, argumentName, pointerPredicate, referencePredicate) \
405 inline thisType* to##thisType(argumentType* argumentName) \
407 ASSERT_WITH_SECURITY_IMPLICATION(!argumentName || (pointerPredicate)); \
408 return static_cast<thisType*>(argumentName); \
410 inline const thisType* to##thisType(const argumentType* argumentName) \
412 ASSERT_WITH_SECURITY_IMPLICATION(!argumentName || (pointerPredicate)); \
413 return static_cast<const thisType*>(argumentName); \
415 inline thisType& to##thisType(argumentType& argumentName) \
417 ASSERT_WITH_SECURITY_IMPLICATION(referencePredicate); \
418 return static_cast<thisType&>(argumentName); \
420 inline const thisType& to##thisType(const argumentType& argumentName) \
422 ASSERT_WITH_SECURITY_IMPLICATION(referencePredicate); \
423 return static_cast<const thisType&>(argumentName); \
425 void to##thisType(const thisType*); \
426 void to##thisType(const thisType&)
428 #endif /* WTF_Assertions_h */