Bitcoin Core 29.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
49std::string StrFormatInternalBug(std::string_view msg, std::string_view file, int line, std::string_view func);
50
51class NonFatalCheckError : public std::runtime_error
52{
53public:
54 NonFatalCheckError(std::string_view msg, std::string_view file, int line, std::string_view func);
55};
56
58template <typename T>
59T&& inline_check_non_fatal(LIFETIMEBOUND T&& val, const char* file, int line, const char* func, const char* assertion)
60{
61 if (!val) {
62 throw NonFatalCheckError{assertion, file, line, func};
63 }
64 return std::forward<T>(val);
65}
66
67#if defined(NDEBUG)
68#error "Cannot compile without assertions!"
69#endif
70
72void assertion_fail(std::string_view file, int line, std::string_view func, std::string_view assertion);
73
75template <bool IS_ASSERT, typename T>
76constexpr 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)
77{
78 if (IS_ASSERT || std::is_constant_evaluated() || G_FUZZING_BUILD || G_ABORT_ON_FAILED_ASSUME) {
79 if (!val) {
80 assertion_fail(file, line, func, assertion);
81 }
82 }
83 return std::forward<T>(val);
84}
85
86// All macros may use __func__ inside a lambda, so put them under nolint.
87// NOLINTBEGIN(bugprone-lambda-function-name)
88
89#define STR_INTERNAL_BUG(msg) StrFormatInternalBug((msg), __FILE__, __LINE__, __func__)
90
102#define CHECK_NONFATAL(condition) \
103 inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition)
104
106#define Assert(val) inline_assertion_check<true>(val, __FILE__, __LINE__, __func__, #val)
107
118#define Assume(val) inline_assertion_check<false>(val, __FILE__, __LINE__, __func__, #val)
119
123#define NONFATAL_UNREACHABLE() \
124 throw NonFatalCheckError( \
125 "Unreachable code reached (non-fatal)", __FILE__, __LINE__, __func__)
126
127// NOLINTEND(bugprone-lambda-function-name)
128
129#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:59
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:76
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)
Helper for Assert()
Definition: check.cpp:30
std::atomic< bool > g_enable_dynamic_fuzz_determinism
Definition: check.cpp:37
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)