forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cmake: Add fuzzing facilities #43
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,6 +72,26 @@ tristate_option(WITH_USDT | |
|
||
option(BUILD_TESTS "Build test_bitcoin executable." ON) | ||
option(BUILD_BENCH "Build bench_bitcoin executable." ON) | ||
cmake_dependent_option(BUILD_FUZZ_BINARY "Build fuzz binary." ON "NOT MSVC" OFF) | ||
cmake_dependent_option(FUZZ "Build for fuzzing. Enabling this will disable all other targets and override BUILD_FUZZ_BINARY." OFF "NOT MSVC" OFF) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And this one means: If not msvc, provide the FUZZ option and set it to OFF by default. |
||
|
||
if(FUZZ) | ||
message(WARNING "FUZZ=ON will disable all other targets and force BUILD_FUZZ_BINARY=ON.") | ||
set(BUILD_DAEMON OFF) | ||
set(BUILD_CLI OFF) | ||
set(BUILD_TX OFF) | ||
set(BUILD_UTIL OFF) | ||
set(BUILD_UTIL_CHAINSTATE OFF) | ||
set(BUILD_SHARED_LIBS OFF) | ||
set(BUILD_WALLET_TOOL OFF) | ||
set(WITH_NATPMP OFF) | ||
set(WITH_MINIUPNPC OFF) | ||
set(WITH_ZMQ OFF) | ||
set(BUILD_TESTS OFF) | ||
set(BUILD_BENCH OFF) | ||
set(BUILD_FUZZ_BINARY ON) | ||
endif() | ||
|
||
option(INSTALL_MAN "Install man pages." ON) | ||
|
||
set(configure_warnings) | ||
|
@@ -191,6 +211,40 @@ endif() | |
include(AddThreadsIfNeeded) | ||
add_threads_if_needed() | ||
|
||
add_library(sanitizing_interface INTERFACE) | ||
target_link_libraries(core_interface INTERFACE sanitizing_interface) | ||
if(SANITIZERS) | ||
# First check if the compiler accepts flags. If an incompatible pair like | ||
# -fsanitize=address,thread is used here, this check will fail. This will also | ||
# fail if a bad argument is passed, e.g. -fsanitize=undfeined | ||
try_append_cxx_flags("-fsanitize=${SANITIZERS}" TARGET sanitizing_interface | ||
RESULT_VAR cxx_supports_sanitizers | ||
) | ||
if(NOT cxx_supports_sanitizers) | ||
message(FATAL_ERROR "Compiler did not accept requested flags.") | ||
endif() | ||
|
||
# Some compilers (e.g. GCC) require additional libraries like libasan, | ||
# libtsan, libubsan, etc. Make sure linking still works with the sanitize | ||
# flag. This is a separate check so we can give a better error message when | ||
# the sanitize flags are supported by the compiler but the actual sanitizer | ||
# libs are missing. | ||
try_append_linker_flag("-fsanitize=${SANITIZERS}" VAR SANITIZER_LDFLAGS | ||
SOURCE " | ||
#include <cstdint> | ||
#include <cstddef> | ||
extern \"C\" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { return 0; } | ||
__attribute__((weak)) // allow for libFuzzer linking | ||
int main() { return 0; } | ||
" | ||
RESULT_VAR linker_supports_sanitizers | ||
) | ||
if(NOT linker_supports_sanitizers) | ||
message(FATAL_ERROR "Linker did not accept requested flags, you are missing required libraries.") | ||
endif() | ||
endif() | ||
target_link_options(sanitizing_interface INTERFACE ${SANITIZER_LDFLAGS}) | ||
|
||
include(AddBoostIfNeeded) | ||
add_boost_if_needed() | ||
|
||
|
@@ -352,6 +406,7 @@ message(" USDT tracing ........................ ${WITH_USDT}") | |
message("Tests:") | ||
message(" test_bitcoin ........................ ${BUILD_TESTS}") | ||
message(" bench_bitcoin ....................... ${BUILD_BENCH}") | ||
message(" fuzz binary ......................... ${BUILD_FUZZ_BINARY}") | ||
message("") | ||
if(CMAKE_CROSSCOMPILING) | ||
set(cross_status "TRUE, for ${CMAKE_SYSTEM_NAME}, ${CMAKE_SYSTEM_PROCESSOR}") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
# Copyright (c) 2023-present The Bitcoin Core developers | ||
# Distributed under the MIT software license, see the accompanying | ||
# file COPYING or https://opensource.org/license/mit/. | ||
|
||
add_subdirectory(util) | ||
|
||
add_executable(fuzz | ||
addition_overflow.cpp | ||
addrman.cpp | ||
asmap.cpp | ||
asmap_direct.cpp | ||
autofile.cpp | ||
banman.cpp | ||
base_encode_decode.cpp | ||
bech32.cpp | ||
bip324.cpp | ||
bitdeque.cpp | ||
block.cpp | ||
block_header.cpp | ||
blockfilter.cpp | ||
bloom_filter.cpp | ||
buffered_file.cpp | ||
chain.cpp | ||
checkqueue.cpp | ||
coins_view.cpp | ||
coinscache_sim.cpp | ||
connman.cpp | ||
crypto.cpp | ||
crypto_aes256.cpp | ||
crypto_aes256cbc.cpp | ||
crypto_chacha20.cpp | ||
crypto_common.cpp | ||
crypto_diff_fuzz_chacha20.cpp | ||
crypto_hkdf_hmac_sha256_l32.cpp | ||
crypto_poly1305.cpp | ||
cuckoocache.cpp | ||
decode_tx.cpp | ||
descriptor_parse.cpp | ||
deserialize.cpp | ||
eval_script.cpp | ||
fee_rate.cpp | ||
fees.cpp | ||
flatfile.cpp | ||
float.cpp | ||
golomb_rice.cpp | ||
headerssync.cpp | ||
hex.cpp | ||
http_request.cpp | ||
integer.cpp | ||
key.cpp | ||
key_io.cpp | ||
kitchen_sink.cpp | ||
load_external_block_file.cpp | ||
locale.cpp | ||
merkleblock.cpp | ||
message.cpp | ||
miniscript.cpp | ||
minisketch.cpp | ||
mini_miner.cpp | ||
muhash.cpp | ||
multiplication_overflow.cpp | ||
net.cpp | ||
net_permissions.cpp | ||
netaddress.cpp | ||
netbase_dns_lookup.cpp | ||
node_eviction.cpp | ||
p2p_transport_serialization.cpp | ||
package_eval.cpp | ||
parse_hd_keypath.cpp | ||
parse_numbers.cpp | ||
parse_script.cpp | ||
parse_univalue.cpp | ||
partially_downloaded_block.cpp | ||
policy_estimator.cpp | ||
policy_estimator_io.cpp | ||
poolresource.cpp | ||
pow.cpp | ||
prevector.cpp | ||
primitives_transaction.cpp | ||
process_message.cpp | ||
process_messages.cpp | ||
protocol.cpp | ||
psbt.cpp | ||
random.cpp | ||
rbf.cpp | ||
rolling_bloom_filter.cpp | ||
rpc.cpp | ||
script.cpp | ||
script_assets_test_minimizer.cpp | ||
script_descriptor_cache.cpp | ||
script_flags.cpp | ||
script_format.cpp | ||
script_interpreter.cpp | ||
script_ops.cpp | ||
script_sigcache.cpp | ||
script_sign.cpp | ||
scriptnum_ops.cpp | ||
secp256k1_ec_seckey_import_export_der.cpp | ||
secp256k1_ecdsa_signature_parse_der_lax.cpp | ||
signature_checker.cpp | ||
signet.cpp | ||
socks5.cpp | ||
span.cpp | ||
spanparsing.cpp | ||
string.cpp | ||
strprintf.cpp | ||
system.cpp | ||
timedata.cpp | ||
torcontrol.cpp | ||
transaction.cpp | ||
tx_in.cpp | ||
tx_out.cpp | ||
tx_pool.cpp | ||
txorphan.cpp | ||
txrequest.cpp | ||
utxo_snapshot.cpp | ||
utxo_total_supply.cpp | ||
validation_load_mempool.cpp | ||
versionbits.cpp | ||
) | ||
target_link_libraries(fuzz | ||
hebasto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
core_interface | ||
test_fuzz | ||
bitcoin_cli | ||
bitcoin_common | ||
minisketch | ||
leveldb | ||
univalue | ||
secp256k1 | ||
Boost::headers | ||
libevent::libevent | ||
) | ||
|
||
if(ENABLE_WALLET) | ||
target_sources(fuzz | ||
PRIVATE | ||
${CMAKE_SOURCE_DIR}/src/wallet/test/fuzz/coincontrol.cpp | ||
${CMAKE_SOURCE_DIR}/src/wallet/test/fuzz/coinselection.cpp | ||
${CMAKE_SOURCE_DIR}/src/wallet/test/fuzz/fees.cpp | ||
${CMAKE_SOURCE_DIR}/src/wallet/test/fuzz/parse_iso8601.cpp | ||
$<$<BOOL:${USE_SQLITE}>:${CMAKE_SOURCE_DIR}/src/wallet/test/fuzz/notifications.cpp> | ||
) | ||
target_link_libraries(fuzz bitcoin_wallet) | ||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Copyright (c) 2023-present The Bitcoin Core developers | ||
# Distributed under the MIT software license, see the accompanying | ||
# file COPYING or https://opensource.org/license/mit/. | ||
|
||
add_library(test_fuzz STATIC EXCLUDE_FROM_ALL | ||
descriptor.cpp | ||
mempool.cpp | ||
net.cpp | ||
../fuzz.cpp | ||
../util.cpp | ||
) | ||
|
||
target_link_libraries(test_fuzz | ||
PRIVATE | ||
core_interface | ||
test_util | ||
bitcoin_node | ||
Boost::headers | ||
) | ||
|
||
include(CheckSourceCompilesAndLinks) | ||
check_cxx_source_links_with_flags("${SANITIZER_LDFLAGS}" " | ||
#include <cstdint> | ||
#include <cstddef> | ||
extern \"C\" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { return 0; } | ||
// No main() function. | ||
" BINARY_LINKS_WITHOUT_MAIN_FUNCTION | ||
) | ||
if(NOT BINARY_LINKS_WITHOUT_MAIN_FUNCTION) | ||
target_compile_definitions(test_fuzz PRIVATE PROVIDE_FUZZ_MAIN_FUNCTION) | ||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note to other reviewers because I had to look up this syntax again. This does the obvious thing: If not msvc, provide the
BUILD_FUZZ_BINARY
option and set it to ON by default.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://cmake.org/cmake/help/latest/module/CMakeDependentOption.html