From 97f21d6102c2a8b136ec48bcb0f3b23bd068b017 Mon Sep 17 00:00:00 2001 From: dergoegge Date: Tue, 15 Oct 2024 12:10:36 +0100 Subject: [PATCH] Introduce `g_fuzzing` global for fuzzing checks --- CMakeLists.txt | 5 ----- src/pow.cpp | 6 ++---- src/test/fuzz/fuzz.cpp | 2 ++ src/util/check.cpp | 2 ++ src/util/check.h | 4 +++- 5 files changed, 9 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8b687641f263b..ade57726695de 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -240,11 +240,6 @@ if(BUILD_FOR_FUZZING) set(BUILD_GUI_TESTS OFF) set(BUILD_BENCH OFF) set(BUILD_FUZZ_BINARY ON) - - target_compile_definitions(core_interface INTERFACE - ABORT_ON_FAILED_ASSUME - FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION - ) endif() include(ProcessConfigurations) diff --git a/src/pow.cpp b/src/pow.cpp index 6c8e7e5d98a6b..012841a86911a 100644 --- a/src/pow.cpp +++ b/src/pow.cpp @@ -9,6 +9,7 @@ #include #include #include +#include unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params) { @@ -138,11 +139,8 @@ bool PermittedDifficultyTransition(const Consensus::Params& params, int64_t heig // the most signficant bit of the last byte of the hash is set. bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params& params) { -#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION - return (hash.data()[31] & 0x80) == 0; -#else + if(g_fuzzing) return (hash.data()[31] & 0x80) == 0; return CheckProofOfWorkImpl(hash, nBits, params); -#endif } bool CheckProofOfWorkImpl(uint256 hash, unsigned int nBits, const Consensus::Params& params) diff --git a/src/test/fuzz/fuzz.cpp b/src/test/fuzz/fuzz.cpp index bba2dd8e3a393..654e1f8be2470 100644 --- a/src/test/fuzz/fuzz.cpp +++ b/src/test/fuzz/fuzz.cpp @@ -102,6 +102,8 @@ void ResetCoverageCounters() {} void initialize() { + g_fuzzing = true; + // By default, make the RNG deterministic with a fixed seed. This will affect all // randomness during the fuzz test, except: // - GetStrongRandBytes(), which is used for the creation of private key material. diff --git a/src/util/check.cpp b/src/util/check.cpp index e1956042c300e..57ff78f7a089a 100644 --- a/src/util/check.cpp +++ b/src/util/check.cpp @@ -14,6 +14,8 @@ #include #include +bool g_fuzzing = false; + std::string StrFormatInternalBug(std::string_view msg, std::string_view file, int line, std::string_view func) { return strprintf("Internal bug detected: %s\n%s:%d (%s)\n" diff --git a/src/util/check.h b/src/util/check.h index 8f28f5dc94127..86e26b2be0026 100644 --- a/src/util/check.h +++ b/src/util/check.h @@ -13,6 +13,8 @@ #include #include +extern bool g_fuzzing; + std::string StrFormatInternalBug(std::string_view msg, std::string_view file, int line, std::string_view func); class NonFatalCheckError : public std::runtime_error @@ -50,7 +52,7 @@ constexpr T&& inline_assertion_check(LIFETIMEBOUND T&& val, [[maybe_unused]] con if (!val) { assertion_fail(file, line, func, assertion); } - } + } else if (g_fuzzing && !val) abort(); return std::forward(val); }