Branch data Line data Source code
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 <cassert> // IWYU pragma: export
11 : : #include <stdexcept>
12 : : #include <string>
13 : : #include <string_view>
14 : : #include <utility>
15 : :
16 : : extern bool g_fuzzing;
17 : :
18 : : std::string StrFormatInternalBug(std::string_view msg, std::string_view file, int line, std::string_view func);
19 : :
20 : : class NonFatalCheckError : public std::runtime_error
21 : : {
22 : : public:
23 : : NonFatalCheckError(std::string_view msg, std::string_view file, int line, std::string_view func);
24 : : };
25 : :
26 : : /** Helper for CHECK_NONFATAL() */
27 : : template <typename T>
28 [ - + ]: 7871648 : T&& inline_check_non_fatal(LIFETIMEBOUND T&& val, const char* file, int line, const char* func, const char* assertion)
29 : : {
30 [ + + ]: 7871648 : if (!val) {
31 [ + - ]: 9 : throw NonFatalCheckError{assertion, file, line, func};
32 : : }
33 : 7871639 : return std::forward<T>(val);
34 : : }
35 : :
36 : : #if defined(NDEBUG)
37 : : #error "Cannot compile without assertions!"
38 : : #endif
39 : :
40 : : /** Helper for Assert() */
41 : : void assertion_fail(std::string_view file, int line, std::string_view func, std::string_view assertion);
42 : :
43 : : /** Helper for Assert()/Assume() */
44 : : template <bool IS_ASSERT, typename T>
45 [ - + ]: 98523326 : constexpr 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)
46 : : {
47 [ - + ]: 79636176 : if (IS_ASSERT || std::is_constant_evaluated() || g_fuzzing
48 : : #ifdef ABORT_ON_FAILED_ASSUME
49 : : || true
50 : : #endif
51 : : ) {
52 [ - + ]: 18887150 : if (!val) {
53 : 0 : assertion_fail(file, line, func, assertion);
54 : : }
55 : : }
56 : 98523326 : return std::forward<T>(val);
57 : : }
58 : :
59 : : // All macros may use __func__ inside a lambda, so put them under nolint.
60 : : // NOLINTBEGIN(bugprone-lambda-function-name)
61 : :
62 : : #define STR_INTERNAL_BUG(msg) StrFormatInternalBug((msg), __FILE__, __LINE__, __func__)
63 : :
64 : : /**
65 : : * Identity function. Throw a NonFatalCheckError when the condition evaluates to false
66 : : *
67 : : * This should only be used
68 : : * - where the condition is assumed to be true, not for error handling or validating user input
69 : : * - where a failure to fulfill the condition is recoverable and does not abort the program
70 : : *
71 : : * For example in RPC code, where it is undesirable to crash the whole program, this can be generally used to replace
72 : : * asserts or recoverable logic errors. A NonFatalCheckError in RPC code is caught and passed as a string to the RPC
73 : : * caller, which can then report the issue to the developers.
74 : : */
75 : : #define CHECK_NONFATAL(condition) \
76 : : inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition)
77 : :
78 : : /** Identity function. Abort if the value compares equal to zero */
79 : : #define Assert(val) inline_assertion_check<true>(val, __FILE__, __LINE__, __func__, #val)
80 : :
81 : : /**
82 : : * Assume is the identity function.
83 : : *
84 : : * - Should be used to run non-fatal checks. In debug builds it behaves like
85 : : * Assert()/assert() to notify developers and testers about non-fatal errors.
86 : : * In production it doesn't warn or log anything.
87 : : * - For fatal errors, use Assert().
88 : : * - For non-fatal errors in interactive sessions (e.g. RPC or command line
89 : : * interfaces), CHECK_NONFATAL() might be more appropriate.
90 : : */
91 : : #define Assume(val) inline_assertion_check<false>(val, __FILE__, __LINE__, __func__, #val)
92 : :
93 : : /**
94 : : * NONFATAL_UNREACHABLE() is a macro that is used to mark unreachable code. It throws a NonFatalCheckError.
95 : : */
96 : : #define NONFATAL_UNREACHABLE() \
97 : : throw NonFatalCheckError( \
98 : : "Unreachable code reached (non-fatal)", __FILE__, __LINE__, __func__)
99 : :
100 : : // NOLINTEND(bugprone-lambda-function-name)
101 : :
102 : : #endif // BITCOIN_UTIL_CHECK_H
|