1 | /* |
2 | * Copyright (C) 2014-2016 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #pragma once |
27 | |
28 | #include "BPlatform.h" |
29 | #include "Logging.h" |
30 | |
31 | #if BUSE(OS_LOG) |
32 | #include <os/log.h> |
33 | #endif |
34 | |
35 | #if defined(NDEBUG) && BOS(DARWIN) |
36 | |
37 | #if BASAN_ENABLED |
38 | #define BBreakpointTrap() __builtin_trap() |
39 | #elif BCPU(X86_64) || BCPU(X86) |
40 | #define BBreakpointTrap() __asm__ volatile ("int3") |
41 | #elif BCPU(ARM_THUMB2) |
42 | #define BBreakpointTrap() __asm__ volatile ("bkpt #0") |
43 | #elif BCPU(ARM64) |
44 | #define BBreakpointTrap() __asm__ volatile ("brk #0") |
45 | #else |
46 | #error "Unsupported CPU". |
47 | #endif |
48 | |
49 | // Crash with a SIGTRAP i.e EXC_BREAKPOINT. |
50 | // We are not using __builtin_trap because it is only guaranteed to abort, but not necessarily |
51 | // trigger a SIGTRAP. Instead, we use inline asm to ensure that we trigger the SIGTRAP. |
52 | #define BCRASH() do { \ |
53 | BBreakpointTrap(); \ |
54 | __builtin_unreachable(); \ |
55 | } while (false) |
56 | |
57 | #else // not defined(NDEBUG) && BOS(DARWIN) |
58 | |
59 | #if BASAN_ENABLED |
60 | #define BCRASH() __builtin_trap() |
61 | #else |
62 | |
63 | #if defined(__GNUC__) // GCC or Clang |
64 | #define BCRASH() do { \ |
65 | *(int*)0xbbadbeef = 0; \ |
66 | __builtin_trap(); \ |
67 | } while (0) |
68 | #else |
69 | #define BCRASH() do { \ |
70 | *(int*)0xbbadbeef = 0; \ |
71 | ((void(*)())0)(); \ |
72 | } while (0) |
73 | #endif // defined(__GNUC__) |
74 | #endif // BASAN_ENABLED |
75 | |
76 | #endif // defined(NDEBUG) && BOS(DARWIN) |
77 | |
78 | #define BASSERT_IMPL(x) do { \ |
79 | if (!(x)) \ |
80 | BCRASH(); \ |
81 | } while (0) |
82 | |
83 | #define RELEASE_BASSERT(x) BASSERT_IMPL(x) |
84 | #define RELEASE_BASSERT_NOT_REACHED() BCRASH() |
85 | |
86 | #if BUSE(OS_LOG) |
87 | #define BMALLOC_LOGGING_PREFIX "bmalloc: " |
88 | #define BLOG_ERROR(format, ...) os_log_error(OS_LOG_DEFAULT, BMALLOC_LOGGING_PREFIX format, __VA_ARGS__) |
89 | #else |
90 | #define BLOG_ERROR(format, ...) bmalloc::reportAssertionFailureWithMessage(__FILE__, __LINE__, __PRETTY_FUNCTION__, format, __VA_ARGS__) |
91 | #endif |
92 | |
93 | #if defined(NDEBUG) |
94 | #define RELEASE_BASSERT_WITH_MESSAGE(x, format, ...) BASSERT_IMPL(x) |
95 | #else |
96 | #define RELEASE_BASSERT_WITH_MESSAGE(x, format, ...) do { \ |
97 | if (!(x)) { \ |
98 | BLOG_ERROR("ASSERTION FAILED: " #x " :: " format, ##__VA_ARGS__); \ |
99 | BCRASH(); \ |
100 | } \ |
101 | } while (0) |
102 | #endif |
103 | |
104 | #define BUNUSED(x) ((void)x) |
105 | |
106 | // ===== Release build ===== |
107 | |
108 | #if defined(NDEBUG) |
109 | |
110 | #define BASSERT(x) |
111 | |
112 | #define IF_DEBUG(x) |
113 | |
114 | #endif // defined(NDEBUG) |
115 | |
116 | |
117 | // ===== Debug build ===== |
118 | |
119 | #if !defined(NDEBUG) |
120 | |
121 | #define BASSERT(x) BASSERT_IMPL(x) |
122 | |
123 | #define IF_DEBUG(x) (x) |
124 | |
125 | #endif // !defined(NDEBUG) |
126 | |