Skip to content

Commit 3d85379

Browse files
committed
cmake: Add fuzzing options
1 parent 908530e commit 3d85379

File tree

5 files changed

+221
-1
lines changed

5 files changed

+221
-1
lines changed

CMakeLists.txt

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ endif()
141141
cmake_dependent_option(ENABLE_EXTERNAL_SIGNER "Enable external signer support." ON "NOT WIN32" OFF)
142142

143143
option(BUILD_BENCH "Build bench_bitcoin executable." OFF)
144+
option(BUILD_FUZZ_BINARY "Build fuzz binary." OFF)
145+
cmake_dependent_option(BUILD_FOR_FUZZING "Build for fuzzing. Enabling this will disable all other targets and override BUILD_FUZZ_BINARY." OFF "NOT MSVC" OFF)
144146

145147
set(configure_warnings)
146148

@@ -166,6 +168,26 @@ target_link_libraries(core_interface INTERFACE
166168
$<$<CONFIG:Debug>:core_interface_debug>
167169
)
168170

171+
if(BUILD_FOR_FUZZING)
172+
message(WARNING "BUILD_FOR_FUZZING=ON will disable all other targets and force BUILD_FUZZ_BINARY=ON.")
173+
set(BUILD_DAEMON OFF)
174+
set(BUILD_CLI OFF)
175+
set(BUILD_TX OFF)
176+
set(BUILD_UTIL OFF)
177+
set(BUILD_WALLET_TOOL OFF)
178+
set(ENABLE_EXTERNAL_SIGNER OFF)
179+
set(WITH_NATPMP OFF)
180+
set(WITH_MINIUPNPC OFF)
181+
set(WITH_ZMQ OFF)
182+
set(BUILD_TESTS OFF)
183+
set(BUILD_BENCH OFF)
184+
set(BUILD_FUZZ_BINARY ON)
185+
186+
target_compile_definitions(core_interface INTERFACE
187+
ABORT_ON_FAILED_ASSUME
188+
)
189+
endif()
190+
169191
include(ProcessConfigurations)
170192

171193
include(TryAppendCXXFlags)
@@ -298,10 +320,21 @@ if(SANITIZERS)
298320
endif()
299321
target_link_options(sanitize_interface INTERFACE ${SANITIZER_LDFLAGS})
300322

323+
if(BUILD_FUZZ_BINARY)
324+
include(CheckSourceCompilesAndLinks)
325+
check_cxx_source_links_with_flags("${SANITIZER_LDFLAGS}" "
326+
#include <cstdint>
327+
#include <cstddef>
328+
extern \"C\" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { return 0; }
329+
// No main() function.
330+
" FUZZ_BINARY_LINKS_WITHOUT_MAIN_FUNCTION
331+
)
332+
endif()
333+
301334
include(AddBoostIfNeeded)
302335
add_boost_if_needed()
303336

304-
if(BUILD_DAEMON OR BUILD_CLI OR BUILD_TESTS OR BUILD_BENCH)
337+
if(BUILD_DAEMON OR BUILD_CLI OR BUILD_TESTS OR BUILD_BENCH OR BUILD_FUZZ_BINARY)
305338
find_package(Libevent 2.1.8 MODULE REQUIRED)
306339
endif()
307340

@@ -358,6 +391,7 @@ message(" USDT tracing ........................ ${WITH_USDT}")
358391
message("Tests:")
359392
message(" test_bitcoin ........................ ${BUILD_TESTS}")
360393
message(" bench_bitcoin ....................... ${BUILD_BENCH}")
394+
message(" fuzz binary ......................... ${BUILD_FUZZ_BINARY}")
361395
message("")
362396
message("C++ compiler .......................... ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}, ${CMAKE_CXX_COMPILER}")
363397
include(FlagsSummary)

src/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,3 +352,7 @@ endif()
352352
if(BUILD_TESTS)
353353
add_subdirectory(test)
354354
endif()
355+
356+
if(BUILD_FUZZ_BINARY)
357+
add_subdirectory(test/fuzz)
358+
endif()

src/test/fuzz/CMakeLists.txt

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
# Copyright (c) 2023-present The Bitcoin Core developers
2+
# Distributed under the MIT software license, see the accompanying
3+
# file COPYING or https://opensource.org/license/mit/.
4+
5+
add_subdirectory(util)
6+
7+
add_executable(fuzz
8+
addition_overflow.cpp
9+
addrman.cpp
10+
asmap.cpp
11+
asmap_direct.cpp
12+
autofile.cpp
13+
banman.cpp
14+
base_encode_decode.cpp
15+
bech32.cpp
16+
bip324.cpp
17+
bitdeque.cpp
18+
bitset.cpp
19+
block.cpp
20+
block_header.cpp
21+
block_index.cpp
22+
blockfilter.cpp
23+
bloom_filter.cpp
24+
buffered_file.cpp
25+
chain.cpp
26+
checkqueue.cpp
27+
cluster_linearize.cpp
28+
coins_view.cpp
29+
coinscache_sim.cpp
30+
connman.cpp
31+
crypto.cpp
32+
crypto_aes256.cpp
33+
crypto_aes256cbc.cpp
34+
crypto_chacha20.cpp
35+
crypto_chacha20poly1305.cpp
36+
crypto_common.cpp
37+
crypto_diff_fuzz_chacha20.cpp
38+
crypto_hkdf_hmac_sha256_l32.cpp
39+
crypto_poly1305.cpp
40+
cuckoocache.cpp
41+
decode_tx.cpp
42+
descriptor_parse.cpp
43+
deserialize.cpp
44+
eval_script.cpp
45+
feefrac.cpp
46+
fee_rate.cpp
47+
feeratediagram.cpp
48+
fees.cpp
49+
flatfile.cpp
50+
float.cpp
51+
golomb_rice.cpp
52+
headerssync.cpp
53+
hex.cpp
54+
http_request.cpp
55+
integer.cpp
56+
key.cpp
57+
key_io.cpp
58+
kitchen_sink.cpp
59+
load_external_block_file.cpp
60+
locale.cpp
61+
merkleblock.cpp
62+
message.cpp
63+
miniscript.cpp
64+
minisketch.cpp
65+
mini_miner.cpp
66+
muhash.cpp
67+
multiplication_overflow.cpp
68+
net.cpp
69+
net_permissions.cpp
70+
netaddress.cpp
71+
netbase_dns_lookup.cpp
72+
node_eviction.cpp
73+
p2p_handshake.cpp
74+
p2p_transport_serialization.cpp
75+
package_eval.cpp
76+
parse_hd_keypath.cpp
77+
parse_numbers.cpp
78+
parse_script.cpp
79+
parse_univalue.cpp
80+
partially_downloaded_block.cpp
81+
policy_estimator.cpp
82+
policy_estimator_io.cpp
83+
poolresource.cpp
84+
pow.cpp
85+
prevector.cpp
86+
primitives_transaction.cpp
87+
process_message.cpp
88+
process_messages.cpp
89+
protocol.cpp
90+
psbt.cpp
91+
random.cpp
92+
rbf.cpp
93+
rolling_bloom_filter.cpp
94+
rpc.cpp
95+
script.cpp
96+
script_assets_test_minimizer.cpp
97+
script_descriptor_cache.cpp
98+
script_flags.cpp
99+
script_format.cpp
100+
script_interpreter.cpp
101+
script_ops.cpp
102+
script_parsing.cpp
103+
script_sigcache.cpp
104+
script_sign.cpp
105+
scriptnum_ops.cpp
106+
secp256k1_ec_seckey_import_export_der.cpp
107+
secp256k1_ecdsa_signature_parse_der_lax.cpp
108+
signature_checker.cpp
109+
signet.cpp
110+
socks5.cpp
111+
span.cpp
112+
string.cpp
113+
strprintf.cpp
114+
system.cpp
115+
timeoffsets.cpp
116+
torcontrol.cpp
117+
transaction.cpp
118+
tx_in.cpp
119+
tx_out.cpp
120+
tx_pool.cpp
121+
txorphan.cpp
122+
txrequest.cpp
123+
utxo_snapshot.cpp
124+
utxo_total_supply.cpp
125+
validation_load_mempool.cpp
126+
vecdeque.cpp
127+
versionbits.cpp
128+
)
129+
target_link_libraries(fuzz
130+
core_interface
131+
test_fuzz
132+
bitcoin_cli
133+
bitcoin_common
134+
minisketch
135+
leveldb
136+
univalue
137+
secp256k1
138+
Boost::headers
139+
$<TARGET_NAME_IF_EXISTS:libevent::libevent>
140+
)
141+
142+
if(ENABLE_WALLET)
143+
add_subdirectory(${PROJECT_SOURCE_DIR}/src/wallet/test/fuzz wallet)
144+
endif()

src/test/fuzz/util/CMakeLists.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright (c) 2023-present The Bitcoin Core developers
2+
# Distributed under the MIT software license, see the accompanying
3+
# file COPYING or https://opensource.org/license/mit/.
4+
5+
add_library(test_fuzz STATIC EXCLUDE_FROM_ALL
6+
descriptor.cpp
7+
mempool.cpp
8+
net.cpp
9+
../fuzz.cpp
10+
../util.cpp
11+
)
12+
13+
target_link_libraries(test_fuzz
14+
PRIVATE
15+
core_interface
16+
test_util
17+
bitcoin_node
18+
Boost::headers
19+
)
20+
21+
if(NOT FUZZ_BINARY_LINKS_WITHOUT_MAIN_FUNCTION)
22+
target_compile_definitions(test_fuzz PRIVATE PROVIDE_FUZZ_MAIN_FUNCTION)
23+
endif()

src/wallet/test/fuzz/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright (c) 2023-present The Bitcoin Core developers
2+
# Distributed under the MIT software license, see the accompanying
3+
# file COPYING or https://opensource.org/license/mit/.
4+
5+
target_sources(fuzz
6+
PRIVATE
7+
coincontrol.cpp
8+
coinselection.cpp
9+
fees.cpp
10+
$<$<BOOL:${USE_SQLITE}>:${CMAKE_CURRENT_LIST_DIR}/notifications.cpp>
11+
parse_iso8601.cpp
12+
$<$<BOOL:${USE_SQLITE}>:${CMAKE_CURRENT_LIST_DIR}/scriptpubkeyman.cpp>
13+
wallet_bdb_parser.cpp
14+
)
15+
target_link_libraries(fuzz bitcoin_wallet)

0 commit comments

Comments
 (0)