LCOV - code coverage report
Current view: top level - src/util - check.h (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 87.5 % 8 7
Test Date: 2024-12-04 04:00:22 Functions: 60.9 % 92 56
Branches: 60.0 % 10 6

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

Generated by: LCOV version 2.0-1