Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / wtf / Assertions.h
1 /*
2  * Copyright (C) 2003, 2006, 2007 Apple Inc.  All rights reserved.
3  * Copyright (C) 2013 Google Inc. All rights reserved.
4  *
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.
13  *
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.
25  */
26
27 #ifndef WTF_Assertions_h
28 #define WTF_Assertions_h
29
30 /*
31    No namespaces because this file has to be includable from C and Objective-C.
32
33    Note, this file uses many GCC extensions, but it should be compatible with
34    C, Objective C, C++, and Objective C++.
35
36    For non-debug builds, everything is disabled by default, except for the
37    RELEASE_ASSERT family of macros.
38
39    Defining any of the symbols explicitly prevents this from having any effect.
40
41 */
42
43 #include "wtf/Compiler.h"
44 #include "wtf/WTFExport.h"
45
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 #ifdef NDEBUG
50 /* Disable ASSERT* macros in release mode by default. */
51 #define ENABLE_ASSERT 0
52 #else
53 #define ENABLE_ASSERT 1
54 #endif /* NDEBUG */
55 #endif
56
57 #ifndef BACKTRACE_DISABLED
58 #define BACKTRACE_DISABLED !ENABLE(ASSERT)
59 #endif
60
61 #ifndef ASSERT_MSG_DISABLED
62 #define ASSERT_MSG_DISABLED !ENABLE(ASSERT)
63 #endif
64
65 #ifndef ASSERT_ARG_DISABLED
66 #define ASSERT_ARG_DISABLED !ENABLE(ASSERT)
67 #endif
68
69 #ifndef FATAL_DISABLED
70 #define FATAL_DISABLED !ENABLE(ASSERT)
71 #endif
72
73 #ifndef ERROR_DISABLED
74 #define ERROR_DISABLED !ENABLE(ASSERT)
75 #endif
76
77 #ifndef LOG_DISABLED
78 #define LOG_DISABLED !ENABLE(ASSERT)
79 #endif
80
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
89
90 /* These helper functions are always declared, but not necessarily always defined if the corresponding function is disabled. */
91
92 #ifdef __cplusplus
93 extern "C" {
94 #endif
95
96 typedef enum { WTFLogChannelOff, WTFLogChannelOn } WTFLogChannelState;
97
98 typedef struct {
99     WTFLogChannelState state;
100 } WTFLogChannel;
101
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);
110
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);
114
115 typedef void (*WTFCrashHookFunction)();
116 WTF_EXPORT void WTFSetCrashHook(WTFCrashHookFunction);
117 WTF_EXPORT void WTFInvokeCrashHook();
118 WTF_EXPORT void WTFInstallReportBacktraceOnCrashHook();
119
120 #ifdef __cplusplus
121 }
122
123 namespace WTF {
124
125 class WTF_EXPORT FrameToNameScope {
126 public:
127     explicit FrameToNameScope(void*);
128     ~FrameToNameScope();
129     const char* nullableName() { return m_name; }
130
131 private:
132     const char* m_name;
133     char* m_cxaDemangled;
134 };
135
136 } // namespace WTF
137
138 using WTF::FrameToNameScope;
139 #endif
140
141 /* IMMEDIATE_CRASH() - Like CRASH() below but crashes in the fastest, simplest possible way with no attempt at logging. */
142 #ifndef IMMEDIATE_CRASH
143 #if COMPILER(GCC)
144 #define IMMEDIATE_CRASH() __builtin_trap()
145 #else
146 #define IMMEDIATE_CRASH() ((void(*)())0)()
147 #endif
148 #endif
149
150 /* CRASH() - Raises a fatal error resulting in program termination and triggering either the debugger or the crash reporter.
151
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.
155
156    Signals are ignored by the crash reporter on OS X so we must do better.
157 */
158 #ifndef CRASH
159 #define CRASH() \
160     (WTFReportBacktrace(), \
161      WTFInvokeCrashHook(), \
162      (*(int*)0xfbadbeef = 0), \
163      IMMEDIATE_CRASH())
164 #endif
165
166 #if COMPILER(CLANG)
167 #define NO_RETURN_DUE_TO_CRASH NO_RETURN
168 #else
169 #define NO_RETURN_DUE_TO_CRASH
170 #endif
171
172 /* BACKTRACE
173
174   Print a backtrace to the same location as ASSERT messages.
175 */
176 #if BACKTRACE_DISABLED
177
178 #define BACKTRACE() ((void)0)
179
180 #else
181
182 #define BACKTRACE() do { \
183     WTFReportBacktrace(); \
184 } while(false)
185
186 #endif
187
188 /* ASSERT, ASSERT_NOT_REACHED, ASSERT_UNUSED
189
190   These macros are compiled out of release builds.
191   Expressions inside them are evaluated in debug builds only.
192 */
193 #if OS(WIN)
194 /* FIXME: Change to use something other than ASSERT to avoid this conflict with the underlying platform */
195 #undef ASSERT
196 #endif
197
198 #if ENABLE(ASSERT)
199
200 #define ASSERT(assertion) \
201     (!(assertion) ? \
202         (WTFReportAssertionFailure(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #assertion), \
203          CRASH()) : \
204         (void)0)
205
206 #define ASSERT_AT(assertion, file, line, function) \
207     (!(assertion) ? \
208         (WTFReportAssertionFailure(file, line, function, #assertion), \
209          CRASH()) :                                                   \
210         (void)0)
211
212 #define ASSERT_NOT_REACHED() do { \
213     WTFReportAssertionFailure(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, 0); \
214     CRASH(); \
215 } while (0)
216
217 #define ASSERT_UNUSED(variable, assertion) ASSERT(assertion)
218
219 #define NO_RETURN_DUE_TO_ASSERT NO_RETURN_DUE_TO_CRASH
220
221 #else
222
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
227
228 #define ASSERT_UNUSED(variable, assertion) ((void)variable)
229
230 #endif
231
232 /* ASSERT_WITH_SECURITY_IMPLICATION / RELEASE_ASSERT_WITH_SECURITY_IMPLICATION
233
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
239 */
240 #ifdef ADDRESS_SANITIZER
241
242 #define ASSERT_WITH_SECURITY_IMPLICATION(assertion) \
243     (!(assertion) ? \
244         (WTFReportAssertionFailure(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #assertion), \
245          CRASH()) : \
246         (void)0)
247
248 #define RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(assertion) ASSERT_WITH_SECURITY_IMPLICATION(assertion)
249
250 #else
251
252 #define ASSERT_WITH_SECURITY_IMPLICATION(assertion) ASSERT(assertion)
253 #define RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(assertion) RELEASE_ASSERT(assertion)
254
255 #endif
256
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
261 #else
262 #define ENABLE_SECURITY_ASSERT 0
263 #endif
264
265 /* ASSERT_WITH_MESSAGE */
266
267 #if ASSERT_MSG_DISABLED
268 #define ASSERT_WITH_MESSAGE(assertion, ...) ((void)0)
269 #else
270 #define ASSERT_WITH_MESSAGE(assertion, ...) do \
271     if (!(assertion)) { \
272         WTFReportAssertionFailureWithMessage(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #assertion, __VA_ARGS__); \
273         CRASH(); \
274     } \
275 while (0)
276 #endif
277
278 /* ASSERT_WITH_MESSAGE_UNUSED */
279
280 #if ASSERT_MSG_DISABLED
281 #define ASSERT_WITH_MESSAGE_UNUSED(variable, assertion, ...) ((void)variable)
282 #else
283 #define ASSERT_WITH_MESSAGE_UNUSED(variable, assertion, ...) do \
284     if (!(assertion)) { \
285         WTFReportAssertionFailureWithMessage(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #assertion, __VA_ARGS__); \
286         CRASH(); \
287     } \
288 while (0)
289 #endif
290
291 /* ASSERT_ARG */
292
293 #if ASSERT_ARG_DISABLED
294
295 #define ASSERT_ARG(argName, assertion) ((void)0)
296
297 #else
298
299 #define ASSERT_ARG(argName, assertion) do \
300     if (!(assertion)) { \
301         WTFReportArgumentAssertionFailure(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, #argName, #assertion); \
302         CRASH(); \
303     } \
304 while (0)
305
306 #endif
307
308 /* COMPILE_ASSERT */
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)
315 #else
316 #define COMPILE_ASSERT(exp, name) typedef int dummy##name [(exp) ? 1 : -1]
317 #endif
318 #endif
319
320 /* FATAL */
321
322 #if FATAL_DISABLED
323 #define FATAL(...) ((void)0)
324 #else
325 #define FATAL(...) do { \
326     WTFReportFatalError(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, __VA_ARGS__); \
327     CRASH(); \
328 } while (0)
329 #endif
330
331 /* WTF_LOG_ERROR */
332
333 #if ERROR_DISABLED
334 #define WTF_LOG_ERROR(...) ((void)0)
335 #else
336 #define WTF_LOG_ERROR(...) WTFReportError(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, __VA_ARGS__)
337 #endif
338
339 /* WTF_LOG */
340
341 #if LOG_DISABLED
342 #define WTF_LOG(channel, ...) ((void)0)
343 #else
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
347 #endif
348
349 /* UNREACHABLE_FOR_PLATFORM */
350
351 #if COMPILER(CLANG)
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()
357 {
358     ASSERT_NOT_REACHED();
359 }
360 #pragma clang diagnostic pop
361 #else
362 #define UNREACHABLE_FOR_PLATFORM() ASSERT_NOT_REACHED()
363 #endif
364
365 /* RELEASE_ASSERT
366
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
371 */
372
373 #if ENABLE(ASSERT)
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()
377 #else
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()
381 #endif
382
383 /* DEFINE_COMPARISON_OPERATORS_WITH_REFERENCES */
384
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); }
394
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); }
401
402 /* DEFINE_TYPE_CASTS */
403
404 #define DEFINE_TYPE_CASTS(thisType, argumentType, argumentName, pointerPredicate, referencePredicate) \
405 inline thisType* to##thisType(argumentType* argumentName) \
406 { \
407     ASSERT_WITH_SECURITY_IMPLICATION(!argumentName || (pointerPredicate)); \
408     return static_cast<thisType*>(argumentName); \
409 } \
410 inline const thisType* to##thisType(const argumentType* argumentName) \
411 { \
412     ASSERT_WITH_SECURITY_IMPLICATION(!argumentName || (pointerPredicate)); \
413     return static_cast<const thisType*>(argumentName); \
414 } \
415 inline thisType& to##thisType(argumentType& argumentName) \
416 { \
417     ASSERT_WITH_SECURITY_IMPLICATION(referencePredicate); \
418     return static_cast<thisType&>(argumentName); \
419 } \
420 inline const thisType& to##thisType(const argumentType& argumentName) \
421 { \
422     ASSERT_WITH_SECURITY_IMPLICATION(referencePredicate); \
423     return static_cast<const thisType&>(argumentName); \
424 } \
425 void to##thisType(const thisType*); \
426 void to##thisType(const thisType&)
427
428 #endif /* WTF_Assertions_h */