Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2019-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#ifndef BITCOIN_UTIL_CHECK_H6#define BITCOIN_UTIL_CHECK_H8#include <attributes.h>10#include <atomic>11// We use `util/check.h` to provide the `assert()` macro12// to ensure that `NDEBUG` is not defined.13#include <cassert> // IWYU pragma: export14#include <source_location>15#include <stdexcept>16#include <string>17#include <string_view>18#include <type_traits>19#include <utility>21inline constexpr bool G_FUZZING_BUILD{22#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION23true24#else25false26#endif27};28inline constexpr bool G_ABORT_ON_FAILED_ASSUME{G_FUZZING_BUILD ||29#ifdef ABORT_ON_FAILED_ASSUME30true31#else32false33#endif34};36extern std::atomic<bool> g_enable_dynamic_fuzz_determinism;38inline bool EnableFuzzDeterminism()39{40if constexpr (G_FUZZING_BUILD) {41return true;42} else if constexpr (!G_ABORT_ON_FAILED_ASSUME) {43// Running fuzz tests is always disabled if Assume() doesn't abort44// (ie, non-fuzz non-debug builds), as otherwise tests which45// should fail due to a failing Assume may still pass. As such,46// we also statically disable fuzz determinism in that case.47return false;48} else {49return g_enable_dynamic_fuzz_determinism;50}51}53extern bool g_detail_test_only_CheckFailuresAreExceptionsNotAborts;54struct test_only_CheckFailuresAreExceptionsNotAborts {55test_only_CheckFailuresAreExceptionsNotAborts() { g_detail_test_only_CheckFailuresAreExceptionsNotAborts = true; };56~test_only_CheckFailuresAreExceptionsNotAborts() { g_detail_test_only_CheckFailuresAreExceptionsNotAborts = false; };57};59std::string StrFormatInternalBug(std::string_view msg, const std::source_location& loc);61class NonFatalCheckError : public std::runtime_error62{63public:64NonFatalCheckError(std::string_view msg, const std::source_location& loc);65};67/// Internal helper. The noreturn enables optimizers to discard invalid paths.68[[noreturn]] void assertion_fail(const std::source_location& loc, std::string_view assertion);70/** Helper for CHECK_NONFATAL() */71template <typename T>72T&& inline_check_non_fatal(LIFETIMEBOUND T&& val, const std::source_location& loc, std::string_view assertion)73{74if (!val) {75if constexpr (G_ABORT_ON_FAILED_ASSUME) {76assertion_fail(loc, assertion);77}78throw NonFatalCheckError{assertion, loc};79}80return std::forward<T>(val);81}83#if defined(NDEBUG)84#error "Cannot compile without assertions!"85#endif87/** Helper for Assert()/Assume() */88template <bool IS_ASSERT, typename T>89constexpr T&& inline_assertion_check(LIFETIMEBOUND T&& val, [[maybe_unused]] const std::source_location& loc, [[maybe_unused]] std::string_view assertion)90{91if (IS_ASSERT || std::is_constant_evaluated() || G_ABORT_ON_FAILED_ASSUME) {92if (!val) {93assertion_fail(loc, assertion);94}95}96return std::forward<T>(val);97}99#define STR_INTERNAL_BUG(msg) StrFormatInternalBug((msg), std::source_location::current())101/**102* Identity function. Throw a NonFatalCheckError when the condition evaluates to false103*104* This should only be used105* - where the condition is assumed to be true, not for error handling or validating user input106* - where a failure to fulfill the condition is recoverable and does not abort the program107*108* For example in RPC code, where it is undesirable to crash the whole program, this can be generally used to replace109* asserts or recoverable logic errors. A NonFatalCheckError in RPC code is caught and passed as a string to the RPC110* caller, which can then report the issue to the developers.111*/112#define CHECK_NONFATAL(condition) \113inline_check_non_fatal(condition, std::source_location::current(), #condition)115/** Identity function. Abort if the value compares equal to zero */116#define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val)118/**119* Assume is the identity function.120*121* - Should be used to run non-fatal checks. In debug builds it behaves like122* Assert()/assert() to notify developers and testers about non-fatal errors.123* In production it doesn't warn or log anything.124* - For fatal errors, use Assert().125* - For non-fatal errors in interactive sessions (e.g. RPC or command line126* interfaces), CHECK_NONFATAL() might be more appropriate.127*/128#define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val)130/**131* NONFATAL_UNREACHABLE() is a macro that is used to mark unreachable code. It throws a NonFatalCheckError.132*/133#define NONFATAL_UNREACHABLE() \134throw NonFatalCheckError { "Unreachable code reached (non-fatal)", std::source_location::current() }136#if defined(__has_feature)137# if __has_feature(address_sanitizer)138# include <sanitizer/asan_interface.h>139# endif140#endif142#ifndef ASAN_POISON_MEMORY_REGION143# define ASAN_POISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size))144# define ASAN_UNPOISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size))145#endif147#endif // BITCOIN_UTIL_CHECK_H
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.