Skip to content

Commit fafbf8a

Browse files
author
MarcoFalke
committed
Make G_FUZZING constexpr, require -DBUILD_FOR_FUZZING=ON to execute a fuzz target
1 parent fae3cf0 commit fafbf8a

File tree

5 files changed

+17
-7
lines changed

5 files changed

+17
-7
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,10 @@ 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+
FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
240+
)
237241
endif()
238242

239243
include(ProcessConfigurations)

src/pow.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ bool PermittedDifficultyTransition(const Consensus::Params& params, int64_t heig
139139
// the most signficant bit of the last byte of the hash is set.
140140
bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params& params)
141141
{
142-
if (g_fuzzing) return (hash.data()[31] & 0x80) == 0;
142+
if constexpr (G_FUZZING) return (hash.data()[31] & 0x80) == 0;
143143
return CheckProofOfWorkImpl(hash, nBits, params);
144144
}
145145

src/test/fuzz/fuzz.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,6 @@ void ResetCoverageCounters() {}
102102

103103
void initialize()
104104
{
105-
g_fuzzing = true;
106-
107105
// By default, make the RNG deterministic with a fixed seed. This will affect all
108106
// randomness during the fuzz test, except:
109107
// - GetStrongRandBytes(), which is used for the creation of private key material.
@@ -156,6 +154,10 @@ void initialize()
156154
std::cerr << "No fuzz target compiled for " << g_fuzz_target << "." << std::endl;
157155
std::exit(EXIT_FAILURE);
158156
}
157+
if constexpr (!G_FUZZING) {
158+
std::cerr << "Must compile with -DBUILD_FOR_FUZZING=ON to execute a fuzz target." << std::endl;
159+
std::exit(EXIT_FAILURE);
160+
}
159161
Assert(!g_test_one_input);
160162
g_test_one_input = &it->second.test_one_input;
161163
it->second.opts.init();

src/util/check.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
#include <string>
1515
#include <string_view>
1616

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

src/util/check.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@
1313
#include <string_view>
1414
#include <utility>
1515

16-
extern bool g_fuzzing;
16+
constexpr bool G_FUZZING{
17+
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
18+
true
19+
#else
20+
false
21+
#endif
22+
};
1723

1824
std::string StrFormatInternalBug(std::string_view msg, std::string_view file, int line, std::string_view func);
1925

@@ -44,7 +50,7 @@ void assertion_fail(std::string_view file, int line, std::string_view func, std:
4450
template <bool IS_ASSERT, typename T>
4551
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)
4652
{
47-
if (IS_ASSERT || std::is_constant_evaluated() || g_fuzzing
53+
if (IS_ASSERT || std::is_constant_evaluated() || G_FUZZING
4854
#ifdef ABORT_ON_FAILED_ASSUME
4955
|| true
5056
#endif

0 commit comments

Comments
 (0)