Bitcoin Core
32.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
util
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
// 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
inline
bool
EnableFuzzDeterminism
()
39
{
40
if
constexpr
(
G_FUZZING_BUILD
) {
41
return
true
;
42
}
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
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
test_only_CheckFailuresAreExceptionsNotAborts
() {
g_detail_test_only_CheckFailuresAreExceptionsNotAborts
=
true
; };
56
~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
61
class
NonFatalCheckError
:
public
std::runtime_error
62
{
63
public
:
64
NonFatalCheckError
(std::string_view msg,
const
std::source_location& loc);
65
};
66
68
[[noreturn]]
void
assertion_fail
(
const
std::source_location& loc, std::string_view assertion);
69
71
template
<
typename
T>
72
T
&&
inline_check_non_fatal
(
LIFETIMEBOUND
T
&& val,
const
std::source_location& loc, std::string_view assertion)
73
{
74
if
(!val) {
75
if
constexpr
(
G_ABORT_ON_FAILED_ASSUME
) {
76
assertion_fail
(loc, assertion);
77
}
78
throw
NonFatalCheckError
{assertion, loc};
79
}
80
return
std::forward<T>(val);
81
}
82
83
#if defined(NDEBUG)
84
#error "Cannot compile without assertions!"
85
#endif
86
88
template
<
bool
IS_ASSERT,
typename
T>
89
constexpr
T
&&
inline_assertion_check
(
LIFETIMEBOUND
T
&& val, [[maybe_unused]]
const
std::source_location& loc, [[maybe_unused]] std::string_view assertion)
90
{
91
if
(IS_ASSERT || std::is_constant_evaluated() ||
G_ABORT_ON_FAILED_ASSUME
) {
92
if
(!val) {
93
assertion_fail
(loc, assertion);
94
}
95
}
96
return
std::forward<T>(val);
97
}
98
99
#define STR_INTERNAL_BUG(msg) StrFormatInternalBug((msg), std::source_location::current())
100
112
#define CHECK_NONFATAL(condition) \
113
inline_check_non_fatal(condition, std::source_location::current(), #condition)
114
116
#define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val)
117
128
#define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val)
129
133
#define NONFATAL_UNREACHABLE() \
134
throw NonFatalCheckError { "Unreachable code reached (non-fatal)", std::source_location::current() }
135
136
#if defined(__has_feature)
137
# if __has_feature(address_sanitizer)
138
# include <sanitizer/asan_interface.h>
139
# endif
140
#endif
141
142
#ifndef ASAN_POISON_MEMORY_REGION
143
# define ASAN_POISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size))
144
# define ASAN_UNPOISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size))
145
#endif
146
147
#endif
// BITCOIN_UTIL_CHECK_H
attributes.h
LIFETIMEBOUND
#define LIFETIMEBOUND
Definition
attributes.h:16
g_detail_test_only_CheckFailuresAreExceptionsNotAborts
bool g_detail_test_only_CheckFailuresAreExceptionsNotAborts
Definition
check.cpp:32
g_enable_dynamic_fuzz_determinism
std::atomic< bool > g_enable_dynamic_fuzz_determinism
Definition
check.cpp:44
StrFormatInternalBug
std::string StrFormatInternalBug(std::string_view msg, const std::source_location &loc)
Definition
check.cpp:18
assertion_fail
void assertion_fail(const std::source_location &loc, std::string_view assertion)
Internal helper. The noreturn enables optimizers to discard invalid paths.
Definition
check.cpp:34
g_detail_test_only_CheckFailuresAreExceptionsNotAborts
bool g_detail_test_only_CheckFailuresAreExceptionsNotAborts
Definition
check.cpp:32
inline_assertion_check
constexpr T && inline_assertion_check(LIFETIMEBOUND T &&val, const std::source_location &loc, std::string_view assertion)
Helper for Assert()/Assume().
Definition
check.h:89
inline_check_non_fatal
T && inline_check_non_fatal(LIFETIMEBOUND T &&val, const std::source_location &loc, std::string_view assertion)
Helper for CHECK_NONFATAL().
Definition
check.h:72
EnableFuzzDeterminism
bool EnableFuzzDeterminism()
Definition
check.h:38
G_ABORT_ON_FAILED_ASSUME
constexpr bool G_ABORT_ON_FAILED_ASSUME
Definition
check.h:28
G_FUZZING_BUILD
constexpr bool G_FUZZING_BUILD
Definition
check.h:21
NonFatalCheckError
Definition
check.h:62
NonFatalCheckError::NonFatalCheckError
NonFatalCheckError(std::string_view msg, const std::source_location &loc)
Definition
check.cpp:27
T
#define T(expected, seed, data)
test_only_CheckFailuresAreExceptionsNotAborts::test_only_CheckFailuresAreExceptionsNotAborts
test_only_CheckFailuresAreExceptionsNotAborts()
Definition
check.h:55
test_only_CheckFailuresAreExceptionsNotAborts::~test_only_CheckFailuresAreExceptionsNotAborts
~test_only_CheckFailuresAreExceptionsNotAborts()
Definition
check.h:56
Generated on
for Bitcoin Core by
1.18.0