Skip to content

Commit 1c7ca6e

Browse files
committed
Merge bitcoin/bitcoin#31093: Introduce g_fuzzing global for fuzzing 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
2 parents 6e21ded + 9f243cd commit 1c7ca6e

File tree

5 files changed

+9
-10
lines changed

5 files changed

+9
-10
lines changed

CMakeLists.txt

-5
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,6 @@ if(BUILD_FOR_FUZZING)
234234
set(BUILD_GUI_TESTS OFF)
235235
set(BUILD_BENCH OFF)
236236
set(BUILD_FUZZ_BINARY ON)
237-
238-
target_compile_definitions(core_interface INTERFACE
239-
ABORT_ON_FAILED_ASSUME
240-
FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
241-
)
242237
endif()
243238

244239
include(ProcessConfigurations)

src/pow.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <chain.h>
1010
#include <primitives/block.h>
1111
#include <uint256.h>
12+
#include <util/check.h>
1213

1314
unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params)
1415
{
@@ -138,11 +139,8 @@ bool PermittedDifficultyTransition(const Consensus::Params& params, int64_t heig
138139
// the most signficant bit of the last byte of the hash is set.
139140
bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params& params)
140141
{
141-
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
142-
return (hash.data()[31] & 0x80) == 0;
143-
#else
142+
if (g_fuzzing) return (hash.data()[31] & 0x80) == 0;
144143
return CheckProofOfWorkImpl(hash, nBits, params);
145-
#endif
146144
}
147145

148146
bool CheckProofOfWorkImpl(uint256 hash, unsigned int nBits, const Consensus::Params& params)

src/test/fuzz/fuzz.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ void ResetCoverageCounters() {}
102102

103103
void initialize()
104104
{
105+
g_fuzzing = true;
106+
105107
// By default, make the RNG deterministic with a fixed seed. This will affect all
106108
// randomness during the fuzz test, except:
107109
// - GetStrongRandBytes(), which is used for the creation of private key material.

src/util/check.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include <string>
1515
#include <string_view>
1616

17+
bool g_fuzzing = false;
18+
1719
std::string StrFormatInternalBug(std::string_view msg, std::string_view file, int line, std::string_view func)
1820
{
1921
return strprintf("Internal bug detected: %s\n%s:%d (%s)\n"

src/util/check.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <string_view>
1414
#include <utility>
1515

16+
extern bool g_fuzzing;
17+
1618
std::string StrFormatInternalBug(std::string_view msg, std::string_view file, int line, std::string_view func);
1719

1820
class NonFatalCheckError : public std::runtime_error
@@ -42,7 +44,7 @@ void assertion_fail(std::string_view file, int line, std::string_view func, std:
4244
template <bool IS_ASSERT, typename T>
4345
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)
4446
{
45-
if (IS_ASSERT || std::is_constant_evaluated()
47+
if (IS_ASSERT || std::is_constant_evaluated() || g_fuzzing
4648
#ifdef ABORT_ON_FAILED_ASSUME
4749
|| true
4850
#endif

0 commit comments

Comments
 (0)