Bitcoin Core 32.99.0
P2P Digital Currency
Loading...
Searching...
No Matches
Classes | Macros | Functions | Variables
check.h File Reference
#include <attributes.h>
#include <atomic>
#include <cassert>
#include <source_location>
#include <stdexcept>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
Include dependency graph for check.h:

Go to the source code of this file.

Classes

struct  test_only_CheckFailuresAreExceptionsNotAborts
 
class  NonFatalCheckError
 

Macros

#define STR_INTERNAL_BUG(msg)   StrFormatInternalBug((msg), std::source_location::current())
 
#define CHECK_NONFATAL(condition)    inline_check_non_fatal(condition, std::source_location::current(), #condition)
 Identity function.
 
#define Assert(val)   inline_assertion_check<true>(val, std::source_location::current(), #val)
 Identity function.
 
#define Assume(val)   inline_assertion_check<false>(val, std::source_location::current(), #val)
 Assume is the identity function.
 
#define NONFATAL_UNREACHABLE()    internal_non_fatal_helper(std::source_location::current(), "unreachable")
 NONFATAL_UNREACHABLE() is a macro that is used to mark unreachable code.
 
#define ASAN_POISON_MEMORY_REGION(addr, size)   ((void)(addr), (void)(size))
 
#define ASAN_UNPOISON_MEMORY_REGION(addr, size)   ((void)(addr), (void)(size))
 

Functions

bool EnableFuzzDeterminism ()
 
std::string StrFormatInternalBug (std::string_view msg, const std::source_location &loc)
 
std::string StrFormatFailedCheck (std::string_view assertion)
 
void internal_abort_helper (const std::source_location &loc, std::string_view error_msg)
 Internal helper. The noreturn enables optimizers to discard invalid paths.
 
void internal_non_fatal_helper (const std::source_location &loc, std::string_view error_msg)
 Internal helper. The noreturn enables optimizers to discard invalid paths.
 
template<typename T >
T && inline_check_non_fatal (LIFETIMEBOUND T &&val, const std::source_location &loc, std::string_view assertion)
 Helper for CHECK_NONFATAL()
 
template<bool IS_ASSERT, typename T >
constexpr T && inline_assertion_check (LIFETIMEBOUND T &&val, const std::source_location &loc, std::string_view assertion)
 Helper for Assert()/Assume()
 

Variables

constexpr bool G_FUZZING_BUILD
 
constexpr bool G_ABORT_ON_FAILED_ASSUME
 
std::atomic< bool > g_enable_dynamic_fuzz_determinism
 
bool g_detail_test_only_CheckFailuresAreExceptionsNotAborts
 

Macro Definition Documentation

◆ ASAN_POISON_MEMORY_REGION

#define ASAN_POISON_MEMORY_REGION (   addr,
  size 
)    ((void)(addr), (void)(size))

Definition at line 149 of file check.h.

◆ ASAN_UNPOISON_MEMORY_REGION

#define ASAN_UNPOISON_MEMORY_REGION (   addr,
  size 
)    ((void)(addr), (void)(size))

Definition at line 150 of file check.h.

◆ Assert

#define Assert (   val)    inline_assertion_check<true>(val, std::source_location::current(), #val)

Identity function.

Abort if the value compares equal to zero

Definition at line 119 of file check.h.

◆ Assume

#define Assume (   val)    inline_assertion_check<false>(val, std::source_location::current(), #val)

Assume is the identity function.

  • Should be used to run non-fatal checks. In debug builds it behaves like Assert()/assert() to notify developers and testers about non-fatal errors. In production it doesn't warn or log anything.
  • For fatal errors, use Assert().
  • For non-fatal errors in interactive sessions (e.g. RPC or command line interfaces), CHECK_NONFATAL() might be more appropriate.

Definition at line 131 of file check.h.

◆ CHECK_NONFATAL

Identity function.

Throw a NonFatalCheckError when the condition evaluates to false

This should only be used

  • where the condition is assumed to be true, not for error handling or validating user input
  • where a failure to fulfill the condition is recoverable and does not abort the program

For example in RPC code, where it is undesirable to crash the whole program, this can be generally used to replace asserts or recoverable logic errors. A NonFatalCheckError in RPC code is caught and passed as a string to the RPC caller, which can then report the issue to the developers.

In debug builds an abort is triggered.

Definition at line 115 of file check.h.

◆ NONFATAL_UNREACHABLE

#define NONFATAL_UNREACHABLE ( )     internal_non_fatal_helper(std::source_location::current(), "unreachable")

NONFATAL_UNREACHABLE() is a macro that is used to mark unreachable code.

It throws a NonFatalCheckError. Control flow will not continue past this macro.

In debug builds an abort is triggered.

Definition at line 139 of file check.h.

◆ STR_INTERNAL_BUG

#define STR_INTERNAL_BUG (   msg)    StrFormatInternalBug((msg), std::source_location::current())

Definition at line 100 of file check.h.

Function Documentation

◆ EnableFuzzDeterminism()

bool EnableFuzzDeterminism ( )
inline

Definition at line 38 of file check.h.

Here is the caller graph for this function:

◆ inline_assertion_check()

template<bool IS_ASSERT, typename T >
constexpr T && inline_assertion_check ( LIFETIMEBOUND T &&  val,
const std::source_location &  loc,
std::string_view  assertion 
)
constexpr

Helper for Assert()/Assume()

Definition at line 90 of file check.h.

Here is the call graph for this function:

◆ inline_check_non_fatal()

template<typename T >
T && inline_check_non_fatal ( LIFETIMEBOUND T &&  val,
const std::source_location &  loc,
std::string_view  assertion 
)

Helper for CHECK_NONFATAL()

Definition at line 76 of file check.h.

Here is the call graph for this function:

◆ internal_abort_helper()

void internal_abort_helper ( const std::source_location &  loc,
std::string_view  error_msg 
)

Internal helper. The noreturn enables optimizers to discard invalid paths.

Definition at line 39 of file check.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ internal_non_fatal_helper()

void internal_non_fatal_helper ( const std::source_location &  loc,
std::string_view  error_msg 
)

Internal helper. The noreturn enables optimizers to discard invalid paths.

Definition at line 49 of file check.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ StrFormatFailedCheck()

std::string StrFormatFailedCheck ( std::string_view  assertion)

Definition at line 27 of file check.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ StrFormatInternalBug()

std::string StrFormatInternalBug ( std::string_view  msg,
const std::source_location &  loc 
)

Definition at line 18 of file check.cpp.

Here is the call graph for this function:

Variable Documentation

◆ G_ABORT_ON_FAILED_ASSUME

constexpr bool G_ABORT_ON_FAILED_ASSUME
inlineconstexpr
Initial value:
false
}
constexpr bool G_FUZZING_BUILD
Definition check.h:21

Definition at line 28 of file check.h.

◆ g_detail_test_only_CheckFailuresAreExceptionsNotAborts

bool g_detail_test_only_CheckFailuresAreExceptionsNotAborts
extern

Definition at line 37 of file check.cpp.

◆ g_enable_dynamic_fuzz_determinism

std::atomic<bool> g_enable_dynamic_fuzz_determinism
extern

Definition at line 57 of file check.cpp.

◆ G_FUZZING_BUILD

constexpr bool G_FUZZING_BUILD
inlineconstexpr
Initial value:
{
false
}

Definition at line 21 of file check.h.