Bitcoin Core 30.99.0
P2P Digital Currency
check.h
Go to the documentation of this file.
1// Copyright (c) 2019-2022 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 <stdexcept>
13#include <string>
14#include <string_view>
15#include <utility>
16
17constexpr bool G_FUZZING_BUILD{
18#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
19 true
20#else
21 false
22#endif
23};
25#ifdef ABORT_ON_FAILED_ASSUME
26 true
27#else
28 false
29#endif
30};
31
32extern std::atomic<bool> g_enable_dynamic_fuzz_determinism;
33
35{
36 if constexpr (G_FUZZING_BUILD) {
37 return true;
38 } else if constexpr (!G_ABORT_ON_FAILED_ASSUME) {
39 // Running fuzz tests is always disabled if Assume() doesn't abort
40 // (ie, non-fuzz non-debug builds), as otherwise tests which
41 // should fail due to a failing Assume may still pass. As such,
42 // we also statically disable fuzz determinism in that case.
43 return false;
44 } else {
46 }
47}
48
53};
54
55std::string StrFormatInternalBug(std::string_view msg, std::string_view file, int line, std::string_view func);
56
57class NonFatalCheckError : public std::runtime_error
58{
59public:
60 NonFatalCheckError(std::string_view msg, std::string_view file, int line, std::string_view func);
61};
62
64void assertion_fail(std::string_view file, int line, std::string_view func, std::string_view assertion);
65
67template <typename T>
68T&& inline_check_non_fatal(LIFETIMEBOUND T&& val, const char* file, int line, const char* func, const char* assertion)
69{
70 if (!val) {
71 if constexpr (G_ABORT_ON_FAILED_ASSUME) {
72 assertion_fail(file, line, func, assertion);
73 }
74 throw NonFatalCheckError{assertion, file, line, func};
75 }
76 return std::forward<T>(val);
77}
78
79#if defined(NDEBUG)
80#error "Cannot compile without assertions!"
81#endif
82
84template <bool IS_ASSERT, typename T>
85constexpr T&& inline_assertion_check(LIFETIMEBOUND T&& val, [[maybe_unused]] const char* file, [[maybe_unused]] int line, [[maybe_unused]] const char* func, [[maybe_unused]] const char* assertion)
86{
87 if (IS_ASSERT || std::is_constant_evaluated() || G_ABORT_ON_FAILED_ASSUME) {
88 if (!val) {
89 assertion_fail(file, line, func, assertion);
90 }
91 }
92 return std::forward<T>(val);
93}
94
95// All macros may use __func__ inside a lambda, so put them under nolint.
96// NOLINTBEGIN(bugprone-lambda-function-name)
97
98#define STR_INTERNAL_BUG(msg) StrFormatInternalBug((msg), __FILE__, __LINE__, __func__)
99
111#define CHECK_NONFATAL(condition) \
112 inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition)
113
115#define Assert(val) inline_assertion_check<true>(val, __FILE__, __LINE__, __func__, #val)
116
127#define Assume(val) inline_assertion_check<false>(val, __FILE__, __LINE__, __func__, #val)
128
132#define NONFATAL_UNREACHABLE() \
133 throw NonFatalCheckError( \
134 "Unreachable code reached (non-fatal)", __FILE__, __LINE__, __func__)
135
136// NOLINTEND(bugprone-lambda-function-name)
137
138#if defined(__has_feature)
139# if __has_feature(address_sanitizer)
140# include <sanitizer/asan_interface.h>
141# endif
142#endif
143
144#ifndef ASAN_POISON_MEMORY_REGION
145# define ASAN_POISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size))
146# define ASAN_UNPOISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size))
147#endif
148
149#endif // BITCOIN_UTIL_CHECK_H
#define LIFETIMEBOUND
Definition: attributes.h:16
T && inline_check_non_fatal(LIFETIMEBOUND T &&val, const char *file, int line, const char *func, const char *assertion)
Helper for CHECK_NONFATAL()
Definition: check.h:68
bool g_detail_test_only_CheckFailuresAreExceptionsNotAborts
Definition: check.cpp:30
constexpr T && inline_assertion_check(LIFETIMEBOUND T &&val, const char *file, int line, const char *func, const char *assertion)
Helper for Assert()/Assume()
Definition: check.h:85
std::string StrFormatInternalBug(std::string_view msg, std::string_view file, int line, std::string_view func)
Definition: check.cpp:17
void assertion_fail(std::string_view file, int line, std::string_view func, std::string_view assertion)
Internal helper.
Definition: check.cpp:32
std::atomic< bool > g_enable_dynamic_fuzz_determinism
Definition: check.cpp:42
bool EnableFuzzDeterminism()
Definition: check.h:34
constexpr bool G_ABORT_ON_FAILED_ASSUME
Definition: check.h:24
constexpr bool G_FUZZING_BUILD
Definition: check.h:17
NonFatalCheckError(std::string_view msg, std::string_view file, int line, std::string_view func)
Definition: check.cpp:25
#define T(expected, seed, data)