Skip to content

Commit 03cff2c

Browse files
committed
Merge bitcoin/bitcoin#31191: build: Make G_FUZZING constexpr, require -DBUILD_FOR_FUZZING=ON to fuzz
fafbf8a Make G_FUZZING constexpr, require -DBUILD_FOR_FUZZING=ON to execute a fuzz target (MarcoFalke) fae3cf0 ci: Temporarily disable macOS/Windows fuzz step (MarcoFalke) Pull request description: `g_fuzzing` is used inside `Assume` at runtime, causing significant overhead in hot paths. See bitcoin/bitcoin#31178 One could simply remove the `g_fuzzing` check from the `Assume`, but this would make fuzzing a bit less useful. Also, it would be unclear if `g_fuzzing` adds a runtime overhead in other code paths today or in the future. Fix all issues by making `G_FUZZING` equal to the build option `BUILD_FOR_FUZZING`, and for consistency in fuzzing, require it to be set when executing any fuzz target. Fixes bitcoin/bitcoin#31178 Temporarily this drops fuzzing from two CI tasks, but they can be re-added in a follow-up with something like bitcoin/bitcoin#31073 ACKs for top commit: marcofleon: Tested ACK fafbf8a davidgumberg: I still ACK bitcoin/bitcoin@fafbf8a for fixing the regression measured in #31178. ryanofsky: Code review ACK fafbf8a but approach -0, because this approach means libraries built for fuzz testing do not function correctly if used in a release, and libraries built for releases are mostly useless for fuzz testing. So I would like to at least consider other solutions to this problem even if we go with this one. dergoegge: utACK fafbf8a Tree-SHA512: 124fc2e8b35e0c4df414436556a7a0a36cd1bec4b3000b40dcf2ab8c85f32e0610bf7f70d2fd79223d62f3c3665b6c09da21241654c7b9859461b8ca340d5421
2 parents b934954 + fafbf8a commit 03cff2c

File tree

7 files changed

+17
-22
lines changed

7 files changed

+17
-22
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -212,20 +212,6 @@ jobs:
212212
shell: cmd
213213
run: py -3 test\functional\test_runner.py --jobs %NUMBER_OF_PROCESSORS% --ci --quiet --tmpdirprefix=%RUNNER_TEMP% --combinedlogslen=99999999 --timeout-factor=%TEST_RUNNER_TIMEOUT_FACTOR% %TEST_RUNNER_EXTRA%
214214

215-
- name: Clone fuzz corpus
216-
run: |
217-
git clone --depth=1 https://github.com/bitcoin-core/qa-assets "$env:RUNNER_TEMP\qa-assets"
218-
Set-Location "$env:RUNNER_TEMP\qa-assets"
219-
Write-Host "Using qa-assets repo from commit ..."
220-
git log -1
221-
222-
- name: Run fuzz binaries
223-
working-directory: build
224-
env:
225-
BITCOINFUZZ: '${{ github.workspace }}\build\src\test\fuzz\Release\fuzz.exe'
226-
shell: cmd
227-
run: py -3 test\fuzz\test_runner.py --par %NUMBER_OF_PROCESSORS% --loglevel DEBUG %RUNNER_TEMP%\qa-assets\fuzz_corpora
228-
229215
asan-lsan-ubsan-integer-no-depends-usdt:
230216
name: 'ASan + LSan + UBSan + integer, no depends, USDT'
231217
runs-on: ubuntu-24.04 # has to match container in ci/test/00_setup_env_native_asan.sh for tracing tools

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,10 @@ if(BUILD_FOR_FUZZING)
225225
set(BUILD_GUI_TESTS OFF)
226226
set(BUILD_BENCH OFF)
227227
set(BUILD_FUZZ_BINARY ON)
228+
229+
target_compile_definitions(core_interface INTERFACE
230+
FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
231+
)
228232
endif()
229233

230234
include(ProcessConfigurations)

ci/test/00_setup_env_mac_native.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,3 @@ export BITCOIN_CONFIG="-DBUILD_GUI=ON -DWITH_ZMQ=ON -DREDUCE_EXPORTS=ON"
1515
export CI_OS_NAME="macos"
1616
export NO_DEPENDS=1
1717
export OSX_SDK=""
18-
export RUN_FUZZ_TESTS=true

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)