Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / wtf / Assertions.h
blobedd2aa66d4d9e2fe16b024a82f6599f69d7f1083
1 /*
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
7 * are met:
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.
48 #ifndef ENABLE_ASSERT
49 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
50 /* Disable ASSERT* macros in release mode by default. */
51 #define ENABLE_ASSERT 0
52 #else
53 #define ENABLE_ASSERT 1
54 #endif /* defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) */
55 #endif
57 #ifndef BACKTRACE_DISABLED
58 #define BACKTRACE_DISABLED !ENABLE(ASSERT)
59 #endif
61 #ifndef ASSERT_MSG_DISABLED
62 #define ASSERT_MSG_DISABLED !ENABLE(ASSERT)
63 #endif
65 #ifndef ASSERT_ARG_DISABLED
66 #define ASSERT_ARG_DISABLED !ENABLE(ASSERT)
67 #endif
69 #ifndef FATAL_DISABLED
70 #define FATAL_DISABLED !ENABLE(ASSERT)
71 #endif
73 #ifndef ERROR_DISABLED
74 #define ERROR_DISABLED !ENABLE(ASSERT)
75 #endif
77 #ifndef LOG_DISABLED
78 #define LOG_DISABLED !ENABLE(ASSERT)
79 #endif
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)))
86 #else
87 #define WTF_ATTRIBUTE_PRINTF(formatStringArgument, extraArguments)
88 #endif
90 /* These helper functions are always declared, but not necessarily always defined if the corresponding function is disabled. */
92 typedef enum { WTFLogChannelOff, WTFLogChannelOn } WTFLogChannelState;
94 typedef struct {
95 WTFLogChannelState state;
96 } WTFLogChannel;
98 WTF_EXPORT void WTFReportAssertionFailure(const char* file, int line, const char* function, const char* assertion);
99 WTF_EXPORT void WTFReportAssertionFailureWithMessage(const char* file, int line, const char* function, const char* assertion, const char* format, ...) WTF_ATTRIBUTE_PRINTF(5, 6);
100 WTF_EXPORT void WTFReportArgumentAssertionFailure(const char* file, int line, const char* function, const char* argName, const char* assertion);
101 WTF_EXPORT void WTFReportFatalError(const char* file, int line, const char* function, const char* format, ...) WTF_ATTRIBUTE_PRINTF(4, 5);
102 WTF_EXPORT void WTFReportError(const char* file, int line, const char* function, const char* format, ...) WTF_ATTRIBUTE_PRINTF(4, 5);
103 WTF_EXPORT void WTFLog(WTFLogChannel*, const char* format, ...) WTF_ATTRIBUTE_PRINTF(2, 3);
104 WTF_EXPORT void WTFLogVerbose(const char* file, int line, const char* function, WTFLogChannel*, const char* format, ...) WTF_ATTRIBUTE_PRINTF(5, 6);
105 WTF_EXPORT void WTFLogAlways(const char* format, ...) WTF_ATTRIBUTE_PRINTF(1, 2);
107 WTF_EXPORT void WTFGetBacktrace(void** stack, int* size);
108 WTF_EXPORT void WTFReportBacktrace(int framesToShow = 31);
109 WTF_EXPORT void WTFPrintBacktrace(void** stack, int size);
111 namespace WTF {
113 class WTF_EXPORT FrameToNameScope {
114 public:
115 explicit FrameToNameScope(void*);
116 ~FrameToNameScope();
117 const char* nullableName() { return m_name; }
119 private:
120 const char* m_name;
121 char* m_cxaDemangled;
124 } // namespace WTF
126 using WTF::FrameToNameScope;
128 /* IMMEDIATE_CRASH() - Like CRASH() below but crashes in the fastest, simplest possible way with no attempt at logging. */
129 #ifndef IMMEDIATE_CRASH
130 #if COMPILER(GCC) || COMPILER(CLANG)
131 #define IMMEDIATE_CRASH() __builtin_trap()
132 #else
133 #define IMMEDIATE_CRASH() ((void)(*(volatile char*)0 = 0))
134 #endif
135 #endif
137 /* CRASH() - Raises a fatal error resulting in program termination and triggering either the debugger or the crash reporter.
139 Use CRASH() in response to known, unrecoverable errors like out-of-memory.
140 Macro is enabled in both debug and release mode.
141 To test for unknown errors and verify assumptions, use ASSERT instead, to avoid impacting performance in release builds.
143 Signals are ignored by the crash reporter on OS X so we must do better.
145 #ifndef CRASH
146 #if COMPILER(MSVC)
147 #define CRASH() (__debugbreak(), IMMEDIATE_CRASH())
148 #else
149 #define CRASH() \
150 (WTFReportBacktrace(), \
151 (*(int*)0xfbadbeef = 0), \
152 IMMEDIATE_CRASH())
153 #endif
154 #endif
156 #if COMPILER(CLANG)
157 #define NO_RETURN_DUE_TO_CRASH NO_RETURN
158 #else
159 #define NO_RETURN_DUE_TO_CRASH
160 #endif
162 /* BACKTRACE
164 Print a backtrace to the same location as ASSERT messages.
166 #if BACKTRACE_DISABLED
168 #define BACKTRACE() ((void)0)
170 #else
172 #define BACKTRACE() do { \
173 WTFReportBacktrace(); \
174 } while(false)
176 #endif
178 /* ASSERT, ASSERT_NOT_REACHED, ASSERT_UNUSED
180 These macros are compiled out of release builds.
181 Expressions inside them are evaluated in debug builds only.
183 #if OS(WIN)
184 /* FIXME: Change to use something other than ASSERT to avoid this conflict with the underlying platform */
185 #undef ASSERT
186 #endif
188 #if ENABLE(ASSERT)
190 #define ASSERT(assertion) \
191 (!(assertion) ? \
192 (WTFReportAssertionFailure(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #assertion), \
193 CRASH()) : \
194 (void)0)
196 #define ASSERT_AT(assertion, file, line, function) \
197 (!(assertion) ? \
198 (WTFReportAssertionFailure(file, line, function, #assertion), \
199 CRASH()) : \
200 (void)0)
202 #define ASSERT_NOT_REACHED() do { \
203 WTFReportAssertionFailure(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, 0); \
204 CRASH(); \
205 } while (0)
207 #define ASSERT_UNUSED(variable, assertion) ASSERT(assertion)
209 #define NO_RETURN_DUE_TO_ASSERT NO_RETURN_DUE_TO_CRASH
211 #else
213 #define ASSERT(assertion) ((void)0)
214 #define ASSERT_AT(assertion, file, line, function) ((void)0)
215 #define ASSERT_NOT_REACHED() ((void)0)
216 #define NO_RETURN_DUE_TO_ASSERT
218 #define ASSERT_UNUSED(variable, assertion) ((void)variable)
220 #endif
222 /* ASSERT_WITH_SECURITY_IMPLICATION / RELEASE_ASSERT_WITH_SECURITY_IMPLICATION
224 Use in places where failure of the assertion indicates a possible security
225 vulnerability. Classes of these vulnerabilities include bad casts, out of
226 bounds accesses, use-after-frees, etc. Please be sure to file bugs for these
227 failures using the security template:
228 http://code.google.com/p/chromium/issues/entry?template=Security%20Bug
230 #ifdef ADDRESS_SANITIZER
232 #define ASSERT_WITH_SECURITY_IMPLICATION(assertion) \
233 (!(assertion) ? \
234 (WTFReportAssertionFailure(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #assertion), \
235 CRASH()) : \
236 (void)0)
238 #define RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(assertion) ASSERT_WITH_SECURITY_IMPLICATION(assertion)
240 #else
242 #define ASSERT_WITH_SECURITY_IMPLICATION(assertion) ASSERT(assertion)
243 #define RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(assertion) RELEASE_ASSERT(assertion)
245 #endif
247 // Users must test "#if ENABLE(SECURITY_ASSERT)", which helps ensure
248 // that code testing this macro has included this header.
249 #if defined(ADDRESS_SANITIZER) || ENABLE(ASSERT)
250 #define ENABLE_SECURITY_ASSERT 1
251 #else
252 #define ENABLE_SECURITY_ASSERT 0
253 #endif
255 /* ASSERT_WITH_MESSAGE */
257 #if ASSERT_MSG_DISABLED
258 #define ASSERT_WITH_MESSAGE(assertion, ...) ((void)0)
259 #else
260 #define ASSERT_WITH_MESSAGE(assertion, ...) do \
261 if (!(assertion)) { \
262 WTFReportAssertionFailureWithMessage(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #assertion, __VA_ARGS__); \
263 CRASH(); \
265 while (0)
266 #endif
268 /* ASSERT_WITH_MESSAGE_UNUSED */
270 #if ASSERT_MSG_DISABLED
271 #define ASSERT_WITH_MESSAGE_UNUSED(variable, assertion, ...) ((void)variable)
272 #else
273 #define ASSERT_WITH_MESSAGE_UNUSED(variable, assertion, ...) do \
274 if (!(assertion)) { \
275 WTFReportAssertionFailureWithMessage(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #assertion, __VA_ARGS__); \
276 CRASH(); \
278 while (0)
279 #endif
281 /* ASSERT_ARG */
283 #if ASSERT_ARG_DISABLED
285 #define ASSERT_ARG(argName, assertion) ((void)0)
287 #else
289 #define ASSERT_ARG(argName, assertion) do \
290 if (!(assertion)) { \
291 WTFReportArgumentAssertionFailure(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #argName, #assertion); \
292 CRASH(); \
294 while (0)
296 #endif
298 /* FATAL */
300 #if FATAL_DISABLED
301 #define FATAL(...) ((void)0)
302 #else
303 #define FATAL(...) do { \
304 WTFReportFatalError(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, __VA_ARGS__); \
305 CRASH(); \
306 } while (0)
307 #endif
309 /* WTF_LOG_ERROR */
311 #if ERROR_DISABLED
312 #define WTF_LOG_ERROR(...) ((void)0)
313 #else
314 #define WTF_LOG_ERROR(...) WTFReportError(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, __VA_ARGS__)
315 #endif
317 /* WTF_LOG */
319 #if LOG_DISABLED
320 #define WTF_LOG(channel, ...) ((void)0)
321 #else
322 #define WTF_LOG(channel, ...) WTFLog(&JOIN_LOG_CHANNEL_WITH_PREFIX(LOG_CHANNEL_PREFIX, channel), __VA_ARGS__)
323 #define JOIN_LOG_CHANNEL_WITH_PREFIX(prefix, channel) JOIN_LOG_CHANNEL_WITH_PREFIX_LEVEL_2(prefix, channel)
324 #define JOIN_LOG_CHANNEL_WITH_PREFIX_LEVEL_2(prefix, channel) prefix ## channel
325 #endif
327 /* UNREACHABLE_FOR_PLATFORM */
329 #if COMPILER(CLANG)
330 /* This would be a macro except that its use of #pragma works best around
331 a function. Hence it uses macro naming convention. */
332 #pragma clang diagnostic push
333 #pragma clang diagnostic ignored "-Wmissing-noreturn"
334 static inline void UNREACHABLE_FOR_PLATFORM()
336 ASSERT_NOT_REACHED();
338 #pragma clang diagnostic pop
339 #else
340 #define UNREACHABLE_FOR_PLATFORM() ASSERT_NOT_REACHED()
341 #endif
343 /* RELEASE_ASSERT
345 Use in places where failure of an assertion indicates a definite security
346 vulnerability from which execution must not continue even in a release build.
347 Please sure to file bugs for these failures using the security template:
348 http://code.google.com/p/chromium/issues/entry?template=Security%20Bug
351 #if ENABLE(ASSERT)
352 #define RELEASE_ASSERT(assertion) ASSERT(assertion)
353 #define RELEASE_ASSERT_WITH_MESSAGE(assertion, ...) ASSERT_WITH_MESSAGE(assertion, __VA_ARGS__)
354 #define RELEASE_ASSERT_NOT_REACHED() ASSERT_NOT_REACHED()
355 #else
356 #define RELEASE_ASSERT(assertion) (UNLIKELY(!(assertion)) ? (IMMEDIATE_CRASH()) : (void)0)
357 #define RELEASE_ASSERT_WITH_MESSAGE(assertion, ...) RELEASE_ASSERT(assertion)
358 #define RELEASE_ASSERT_NOT_REACHED() IMMEDIATE_CRASH()
359 #endif
361 /* DEFINE_COMPARISON_OPERATORS_WITH_REFERENCES */
363 // Allow equality comparisons of Objects by reference or pointer, interchangeably.
364 // This can be only used on types whose equality makes no other sense than pointer equality.
365 #define DEFINE_COMPARISON_OPERATORS_WITH_REFERENCES(thisType) \
366 inline bool operator==(const thisType& a, const thisType& b) { return &a == &b; } \
367 inline bool operator==(const thisType& a, const thisType* b) { return &a == b; } \
368 inline bool operator==(const thisType* a, const thisType& b) { return a == &b; } \
369 inline bool operator!=(const thisType& a, const thisType& b) { return !(a == b); } \
370 inline bool operator!=(const thisType& a, const thisType* b) { return !(a == b); } \
371 inline bool operator!=(const thisType* a, const thisType& b) { return !(a == b); }
373 #define DEFINE_COMPARISON_OPERATORS_WITH_REFERENCES_REFCOUNTED(thisType) \
374 DEFINE_COMPARISON_OPERATORS_WITH_REFERENCES(thisType) \
375 inline bool operator==(const PassRefPtr<thisType>& a, const thisType& b) { return a.get() == &b; } \
376 inline bool operator==(const thisType& a, const PassRefPtr<thisType>& b) { return &a == b.get(); } \
377 inline bool operator!=(const PassRefPtr<thisType>& a, const thisType& b) { return !(a == b); } \
378 inline bool operator!=(const thisType& a, const PassRefPtr<thisType>& b) { return !(a == b); }
380 /* DEFINE_TYPE_CASTS */
382 #define DEFINE_TYPE_CASTS(thisType, argumentType, argumentName, pointerPredicate, referencePredicate) \
383 inline thisType* to##thisType(argumentType* argumentName) \
385 ASSERT_WITH_SECURITY_IMPLICATION(!argumentName || (pointerPredicate)); \
386 return static_cast<thisType*>(argumentName); \
388 inline const thisType* to##thisType(const argumentType* argumentName) \
390 ASSERT_WITH_SECURITY_IMPLICATION(!argumentName || (pointerPredicate)); \
391 return static_cast<const thisType*>(argumentName); \
393 inline thisType& to##thisType(argumentType& argumentName) \
395 ASSERT_WITH_SECURITY_IMPLICATION(referencePredicate); \
396 return static_cast<thisType&>(argumentName); \
398 inline const thisType& to##thisType(const argumentType& argumentName) \
400 ASSERT_WITH_SECURITY_IMPLICATION(referencePredicate); \
401 return static_cast<const thisType&>(argumentName); \
403 void to##thisType(const thisType*); \
404 void to##thisType(const thisType&)
406 #endif /* WTF_Assertions_h */