Branch data Line data Source code
1 : : // Copyright (c) 2019-present The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #ifndef BITCOIN_UTIL_CHECK_H
6 : : #define BITCOIN_UTIL_CHECK_H
7 : :
8 : : #include <attributes.h>
9 : :
10 : : #include <atomic>
11 : : // We use `util/check.h` to provide the `assert()` macro
12 : : // to ensure that `NDEBUG` is not defined.
13 : : #include <cassert> // IWYU pragma: export
14 : : #include <source_location>
15 : : #include <stdexcept>
16 : : #include <string>
17 : : #include <string_view>
18 : : #include <type_traits>
19 : : #include <utility>
20 : :
21 : : inline constexpr bool G_FUZZING_BUILD{
22 : : #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
23 : : true
24 : : #else
25 : : false
26 : : #endif
27 : : };
28 : : inline constexpr bool G_ABORT_ON_FAILED_ASSUME{G_FUZZING_BUILD ||
29 : : #ifdef ABORT_ON_FAILED_ASSUME
30 : : true
31 : : #else
32 : : false
33 : : #endif
34 : : };
35 : :
36 : : extern std::atomic<bool> g_enable_dynamic_fuzz_determinism;
37 : :
38 : 1798 : inline bool EnableFuzzDeterminism()
39 : : {
40 : 1798 : if constexpr (G_FUZZING_BUILD) {
41 : : return true;
42 : 1798 : } else if constexpr (!G_ABORT_ON_FAILED_ASSUME) {
43 : : // Running fuzz tests is always disabled if Assume() doesn't abort
44 : : // (ie, non-fuzz non-debug builds), as otherwise tests which
45 : : // should fail due to a failing Assume may still pass. As such,
46 : : // we also statically disable fuzz determinism in that case.
47 [ + - + - ]: 1798 : return false;
48 : : } else {
49 : : return g_enable_dynamic_fuzz_determinism;
50 : : }
51 : : }
52 : :
53 : : extern bool g_detail_test_only_CheckFailuresAreExceptionsNotAborts;
54 : : struct test_only_CheckFailuresAreExceptionsNotAborts {
55 [ + - ]: 7 : test_only_CheckFailuresAreExceptionsNotAborts() { g_detail_test_only_CheckFailuresAreExceptionsNotAborts = true; };
[ + - + - ]
56 : 7 : ~test_only_CheckFailuresAreExceptionsNotAborts() { g_detail_test_only_CheckFailuresAreExceptionsNotAborts = false; };
57 : : };
58 : :
59 : : std::string StrFormatInternalBug(std::string_view msg, const std::source_location& loc);
60 : : std::string StrFormatFailedCheck(std::string_view assertion);
61 : :
62 : : class NonFatalCheckError : public std::runtime_error
63 : : {
64 : : public:
65 : : NonFatalCheckError(std::string_view msg, const std::source_location& loc);
66 : : };
67 : :
68 : : /// Internal helper. The noreturn enables optimizers to discard invalid paths.
69 : : [[noreturn]] void internal_abort_helper(const std::source_location& loc, std::string_view error_msg);
70 : :
71 : : /// Internal helper. The noreturn enables optimizers to discard invalid paths.
72 : : [[noreturn]] void internal_non_fatal_helper(const std::source_location& loc, std::string_view error_msg);
73 : :
74 : : /** Helper for CHECK_NONFATAL() */
75 : : template <typename T>
76 [ - + ]: 78113045 : T&& inline_check_non_fatal(LIFETIMEBOUND T&& val, const std::source_location& loc, std::string_view assertion)
77 : : {
78 [ + + ]: 78113045 : if (!val) {
79 [ - + ]: 12 : internal_non_fatal_helper(loc, StrFormatFailedCheck(assertion));
80 : : }
81 : 78113033 : return std::forward<T>(val);
82 : : }
83 : :
84 : : #if defined(NDEBUG)
85 : : #error "Cannot compile without assertions!"
86 : : #endif
87 : :
88 : : /** Helper for Assert()/Assume() */
89 : : template <bool IS_ASSERT, typename T>
90 [ + + ][ + + : 1894870204 : constexpr T&& inline_assertion_check(LIFETIMEBOUND T&& val, [[maybe_unused]] const std::source_location& loc, [[maybe_unused]] std::string_view assertion)
+ + + - -
+ + + + -
+ + + - +
- + + + -
- + - + -
+ + - ][ -
+ + + - -
+ + + + -
+ + + - +
+ + - + +
+ + + + +
- + + - +
- + - + -
+ - ][ + +
+ - + - -
+ + + + -
+ + + + +
+ + + + -
+ + + + +
+ + - + -
- + + - +
- + - + -
+ - - + ]
[ - - - -
- - + + +
- + + + -
+ - + - +
- # # # #
# # # # #
# ][ - + +
- + + + +
+ - - - +
- - - - -
- - - + +
- ][ - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
+ + + - +
+ + + + +
+ + - - -
- - - - -
- - - - -
- - - - -
- - - - -
- + + + -
+ + - - -
- - - - -
+ - + - +
+ + - + +
+ + + - +
+ - + + -
+ - - + -
+ - + - +
- + - - -
- - + - +
- + + + +
- + - + -
- + + - -
+ + - - +
- + + + +
- - + + +
+ + + + +
+ + + - +
- + - + +
- - + - -
+ + + + +
+ + + - -
+ + + - +
- - + - -
+ + - + +
+ + + + +
+ + ][ - -
- - - - +
+ + + - +
- + - + +
+ - + + +
+ + + + -
+ + - + -
+ - + - -
- # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ][ +
+ - - - -
- - + - +
- - - - +
- + + - +
- + - + -
+ - + - +
- - + - -
- - - - +
- + - -
+ ][ + - +
- + + + +
+ + + + ]
[ + + + +
+ + + - +
+ # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ][ + +
+ + + + +
+ + + + +
+ + + + +
+ + + +
+ ][ + + +
+ + + + -
+ - + + +
+ + + ][ +
+ + + + -
+ + + - +
- + - + -
+ - + - #
# ][ + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + ]
[ + + + +
+ + + + +
- + - + -
+ + + - +
+ + - + +
+ + - - +
- + + + +
+ + + + +
+ + - + +
+ + + + -
- + + + +
- - + + +
+ - - + -
+ + + + +
+ + + + +
+ + + + +
- + + - +
+ + + + +
+ - - + +
+ + + + +
+ + + + +
+ + + - +
+ + + + +
+ + + + +
+ + + + -
+ - + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + - + +
+ + + + +
+ + + + +
+ + + + +
- + - + +
+ + + + +
+ + + + +
+ + + + +
+ + + - +
+ + + - +
+ + + + +
+ + + + +
- + + + -
+ - + - +
+ + + + +
+ - + + +
+ + + + +
+ - + + +
+ + + + -
+ - + + +
+ + - + +
+ - + - +
- + + + +
+ + + + +
+ + + + +
+ + + - +
+ + - + -
+ - + + +
+ + + + +
+ + + + +
- + + + -
+ - + - +
+ + + + +
+ - + + +
+ + + + +
+ - + + +
+ + + + -
+ - + + +
+ + - + +
+ - - - +
- + - + +
+ + + + +
+ - + - +
+ + - + +
- - + - +
+ - - + -
+ - + - +
- + + + -
+ - + + +
- + - + +
+ - + + -
- - - + -
+ + - - +
- + - + -
+ - + +
+ ][ + + +
- + + + +
+ - + + +
- + - + -
+ - + + +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- - - # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ][ + - +
+ + + + +
+ + + + +
+ + - + -
+ - + + +
+ + + + -
+ + + - +
+ + + + +
+ + + + +
+ + - + -
+ + + + +
- + + + +
+ + + + +
+ + + + -
+ + + - +
+ + + + +
+ + + + +
+ + + + -
+ - + + +
- + - + -
+ - + - +
- + + + -
+ + + + +
+ + + + -
+ + + - -
- + - + +
+ - + - +
+ - + + -
+ - - + -
+ - + - +
- + - - -
- - + - +
- + + + +
- + - - -
- + + - -
+ + - - +
- + + + +
+ + + + +
+ + + + +
+ + - - +
- + - + +
+ + + - -
+ + + + +
+ + + + +
+ + + - +
- - + + -
+ + - + +
+ + + + +
+ + ]
91 : : {
92 : : if (IS_ASSERT || std::is_constant_evaluated() || G_ABORT_ON_FAILED_ASSUME) {
93 [ + + ]: 30581276 : if (!val) {
94 [ - + ]: 8 : internal_abort_helper(loc, StrFormatFailedCheck(assertion));
95 : : }
96 : : }
97 : 55224471 : return std::forward<T>(val);
98 : : }
99 : :
100 : : #define STR_INTERNAL_BUG(msg) StrFormatInternalBug((msg), std::source_location::current())
101 : :
102 : : /**
103 : : * Identity function. Throw a NonFatalCheckError when the condition evaluates to false
104 : : *
105 : : * This should only be used
106 : : * - where the condition is assumed to be true, not for error handling or validating user input
107 : : * - where a failure to fulfill the condition is recoverable and does not abort the program
108 : : *
109 : : * For example in RPC code, where it is undesirable to crash the whole program, this can be generally used to replace
110 : : * asserts or recoverable logic errors. A NonFatalCheckError in RPC code is caught and passed as a string to the RPC
111 : : * caller, which can then report the issue to the developers.
112 : : *
113 : : * In debug builds an abort is triggered.
114 : : */
115 : : #define CHECK_NONFATAL(condition) \
116 : : inline_check_non_fatal(condition, std::source_location::current(), #condition)
117 : :
118 : : /** Identity function. Abort if the value compares equal to zero */
119 : : #define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val)
120 : :
121 : : /**
122 : : * Assume is the identity function.
123 : : *
124 : : * - Should be used to run non-fatal checks. In debug builds it behaves like
125 : : * Assert()/assert() to notify developers and testers about non-fatal errors.
126 : : * In production it doesn't warn or log anything.
127 : : * - For fatal errors, use Assert().
128 : : * - For non-fatal errors in interactive sessions (e.g. RPC or command line
129 : : * interfaces), CHECK_NONFATAL() might be more appropriate.
130 : : */
131 : : #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val)
132 : :
133 : : /// Mark unreachable code. Aborts if called.
134 : : #define AssertUnreachable() internal_abort_helper(std::source_location::current(), "unreachable")
135 : :
136 : : /**
137 : : * NONFATAL_UNREACHABLE() is a macro that is used to mark unreachable code. It throws a NonFatalCheckError.
138 : : * Control flow will not continue past this macro.
139 : : *
140 : : * In debug builds an abort is triggered.
141 : : */
142 : : #define NONFATAL_UNREACHABLE() \
143 : : internal_non_fatal_helper(std::source_location::current(), "unreachable")
144 : :
145 : : #if defined(__has_feature)
146 : : # if __has_feature(address_sanitizer)
147 : : # include <sanitizer/asan_interface.h>
148 : : # endif
149 : : #endif
150 : :
151 : : #ifndef ASAN_POISON_MEMORY_REGION
152 : : # define ASAN_POISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size))
153 : : # define ASAN_UNPOISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size))
154 : : #endif
155 : :
156 : : #endif // BITCOIN_UTIL_CHECK_H
|