Skip to content

Commit

Permalink
Merge bitcoin/bitcoin#31093: Introduce g_fuzzing global for fuzzing…
Browse files Browse the repository at this point in the history
… checks

9f243cd Introduce `g_fuzzing` global for fuzzing checks (dergoegge)

Pull request description:

  This PR introduces a global `g_fuzzing` that indicates if we are fuzzing.

  If `g_fuzzing` is `true` then:

  * Assume checks are enabled
  * Special fuzzing paths are taken (e.g. pow check is reduced to one bit)

  Closes #30950 #31057

ACKs for top commit:
  maflcko:
    review ACK 9f243cd 🗜
  brunoerg:
    crACK 9f243cd
  marcofleon:
    Tested ACK 9f243cd

Tree-SHA512: 56e4cad0555dec0c565ea5ecc529628ee4f37d20dc660c647fdc6948fbeed8291e6fe290de514bd4c2c7089654d9ce1add607dc9855462828b62be9ee45e4999
  • Loading branch information
fanquake committed Oct 28, 2024
2 parents 6e21ded + 9f243cd commit 1c7ca6e
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 10 deletions.
5 changes: 0 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,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)
Expand Down
6 changes: 2 additions & 4 deletions src/pow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <chain.h>
#include <primitives/block.h>
#include <uint256.h>
#include <util/check.h>

unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params)
{
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/test/fuzz/fuzz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions src/util/check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include <string>
#include <string_view>

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"
Expand Down
4 changes: 3 additions & 1 deletion src/util/check.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include <string_view>
#include <utility>

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
Expand Down Expand Up @@ -42,7 +44,7 @@ void assertion_fail(std::string_view file, int line, std::string_view func, std:
template <bool IS_ASSERT, typename T>
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)
{
if (IS_ASSERT || std::is_constant_evaluated()
if (IS_ASSERT || std::is_constant_evaluated() || g_fuzzing
#ifdef ABORT_ON_FAILED_ASSUME
|| true
#endif
Expand Down

0 comments on commit 1c7ca6e

Please sign in to comment.