Bitcoin Core  31.0.0
P2P Digital Currency
check.h
Go to the documentation of this file.
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 #include <cassert> // IWYU pragma: export
12 #include <source_location>
13 #include <stdexcept>
14 #include <string>
15 #include <string_view>
16 #include <utility>
17 
18 constexpr bool G_FUZZING_BUILD{
19 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
20  true
21 #else
22  false
23 #endif
24 };
26 #ifdef ABORT_ON_FAILED_ASSUME
27  true
28 #else
29  false
30 #endif
31 };
32 
33 extern std::atomic<bool> g_enable_dynamic_fuzz_determinism;
34 
35 inline bool EnableFuzzDeterminism()
36 {
37  if constexpr (G_FUZZING_BUILD) {
38  return true;
39  } else if constexpr (!G_ABORT_ON_FAILED_ASSUME) {
40  // Running fuzz tests is always disabled if Assume() doesn't abort
41  // (ie, non-fuzz non-debug builds), as otherwise tests which
42  // should fail due to a failing Assume may still pass. As such,
43  // we also statically disable fuzz determinism in that case.
44  return false;
45  } else {
47  }
48 }
49 
54 };
55 
56 std::string StrFormatInternalBug(std::string_view msg, const std::source_location& loc);
57 
58 class NonFatalCheckError : public std::runtime_error
59 {
60 public:
61  NonFatalCheckError(std::string_view msg, const std::source_location& loc);
62 };
63 
65 void assertion_fail(const std::source_location& loc, std::string_view assertion);
66 
68 template <typename T>
69 T&& inline_check_non_fatal(LIFETIMEBOUND T&& val, const std::source_location& loc, std::string_view assertion)
70 {
71  if (!val) {
72  if constexpr (G_ABORT_ON_FAILED_ASSUME) {
73  assertion_fail(loc, assertion);
74  }
75  throw NonFatalCheckError{assertion, loc};
76  }
77  return std::forward<T>(val);
78 }
79 
80 #if defined(NDEBUG)
81 #error "Cannot compile without assertions!"
82 #endif
83 
85 template <bool IS_ASSERT, typename T>
86 constexpr T&& inline_assertion_check(LIFETIMEBOUND T&& val, [[maybe_unused]] const std::source_location& loc, [[maybe_unused]] std::string_view assertion)
87 {
88  if (IS_ASSERT || std::is_constant_evaluated() || G_ABORT_ON_FAILED_ASSUME) {
89  if (!val) {
90  assertion_fail(loc, assertion);
91  }
92  }
93  return std::forward<T>(val);
94 }
95 
96 #define STR_INTERNAL_BUG(msg) StrFormatInternalBug((msg), std::source_location::current())
97 
109 #define CHECK_NONFATAL(condition) \
110  inline_check_non_fatal(condition, std::source_location::current(), #condition)
111 
113 #define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val)
114 
125 #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val)
126 
130 #define NONFATAL_UNREACHABLE() \
131  throw NonFatalCheckError { "Unreachable code reached (non-fatal)", std::source_location::current() }
132 
133 #if defined(__has_feature)
134 # if __has_feature(address_sanitizer)
135 # include <sanitizer/asan_interface.h>
136 # endif
137 #endif
138 
139 #ifndef ASAN_POISON_MEMORY_REGION
140 # define ASAN_POISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size))
141 # define ASAN_UNPOISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size))
142 #endif
143 
144 #endif // BITCOIN_UTIL_CHECK_H
constexpr bool G_ABORT_ON_FAILED_ASSUME
Definition: check.h:25
constexpr T && inline_assertion_check(LIFETIMEBOUND T &&val, [[maybe_unused]] const std::source_location &loc, [[maybe_unused]] std::string_view assertion)
Helper for Assert()/Assume()
Definition: check.h:86
constexpr bool G_FUZZING_BUILD
Definition: check.h:18
std::string StrFormatInternalBug(std::string_view msg, const std::source_location &loc)
Definition: check.cpp:18
T && inline_check_non_fatal(LIFETIMEBOUND T &&val, const std::source_location &loc, std::string_view assertion)
Helper for CHECK_NONFATAL()
Definition: check.h:69
#define LIFETIMEBOUND
Definition: attributes.h:16
bool g_detail_test_only_CheckFailuresAreExceptionsNotAborts
Definition: check.cpp:32
std::atomic< bool > g_enable_dynamic_fuzz_determinism
Definition: check.cpp:44
void assertion_fail(const std::source_location &loc, std::string_view assertion)
Internal helper.
Definition: check.cpp:34
bool EnableFuzzDeterminism()
Definition: check.h:35
NonFatalCheckError(std::string_view msg, const std::source_location &loc)
Definition: check.cpp:27
#define T(expected, seed, data)