From 8d3d2aeec26497c8099cc49990d7300a706c4e40 Mon Sep 17 00:00:00 2001 From: Kartik Agarwala Date: Tue, 2 Jul 2024 12:23:31 +0530 Subject: [PATCH 1/3] Use bitcoin's fuzz.[cpp, h] --- Makefile | 2 +- fuzzer.cpp | 46 -------------------------------- targets/addrv2.cpp | 9 +++---- targets/addrv2.h | 7 ----- targets/bech32.cpp | 8 +++--- targets/bech32.h | 7 ----- targets/block_des.cpp | 8 +++--- targets/block_des.h | 7 ----- targets/cmpctblocks.cpp | 7 +++-- targets/cmpctblocks.h | 7 ----- targets/miniscript_string.cpp | 7 +++-- targets/miniscript_string.h | 7 ----- targets/prefilledtransaction.cpp | 4 +-- targets/prefilledtransaction.h | 7 ----- targets/psbt.cpp | 7 ++--- targets/psbt.h | 7 ----- targets/script.cpp | 7 +++-- targets/script.h | 7 ----- targets/tx_des.cpp | 8 +++--- targets/tx_des.h | 7 ----- 20 files changed, 31 insertions(+), 145 deletions(-) delete mode 100644 fuzzer.cpp delete mode 100644 targets/addrv2.h delete mode 100644 targets/bech32.h delete mode 100644 targets/block_des.h delete mode 100644 targets/cmpctblocks.h delete mode 100644 targets/miniscript_string.h delete mode 100644 targets/prefilledtransaction.h delete mode 100644 targets/psbt.h delete mode 100644 targets/script.h delete mode 100644 targets/tx_des.h diff --git a/Makefile b/Makefile index 557e266..f19982b 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,7 @@ endif .PHONY: bitcoinfuzz bitcoin cargo go clean bitcoinfuzz: set $(OBJS) bitcoin cargo go - $(CXX) fuzzer.cpp -o $@ $(OBJS) $(CXXFLAGS) $(LDFLAGS) + $(CXX) dependencies/bitcoin/src/test/fuzz/fuzz.cpp -o $@ $(OBJS) $(CXXFLAGS) $(LDFLAGS) $(OBJS) : build/%.o: %.cpp @[ -d $(@D) ] || mkdir -p $(@D) diff --git a/fuzzer.cpp b/fuzzer.cpp deleted file mode 100644 index c116a62..0000000 --- a/fuzzer.cpp +++ /dev/null @@ -1,46 +0,0 @@ -#include -#include -#include -#include -#include - -#include - -#include "targets/miniscript_string.h" -#include "targets/block_des.h" -#include "targets/prefilledtransaction.h" -#include "targets/tx_des.h" -#include "targets/bech32.h" -#include "targets/psbt.h" -#include "targets/script.h" -#include "targets/addrv2.h" -#include "targets/cmpctblocks.h" - -extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { - FuzzedDataProvider provider(data, size); - - char *t = std::getenv("FUZZ"); - std::string target = t ? t : ""; - - if (target == "miniscript_string") { - MiniscriptFromString(provider); - } else if (target == "block_deserialization") { - BlockDes(provider); - } else if (target == "prefilledtransaction") { - PrefilledTransactionTarget(provider); - } else if (target == "tx_deserialization") { - TransactionDes(provider); - } else if (target == "bech32") { - Bech32(provider); - } else if (target == "psbt") { - Psbt(provider); - } else if (target == "script") { - Script(provider); - } else if (target == "addrv2") { - Addrv2(provider); - } else if (target == "cmpctblocks") { - CmpctBlocks(provider); - } - - return 0; // Values other than 0 and -1 are reserved for future use. -} diff --git a/targets/addrv2.cpp b/targets/addrv2.cpp index 8f299ce..087713e 100644 --- a/targets/addrv2.cpp +++ b/targets/addrv2.cpp @@ -3,12 +3,12 @@ #include #include -#include "addrv2.h" +#include "bitcoin/src/test/fuzz/fuzz.h" #include "bitcoin/src/protocol.h" #include "bitcoin/src/streams.h" -extern "C" bool rust_bitcoin_addrv2(uint8_t *data, size_t len, uint64_t *count); -extern "C" bool go_btcd_addrv2(uint8_t *data, size_t len, uint64_t *count); +extern "C" bool rust_bitcoin_addrv2(const uint8_t *data, size_t len, uint64_t *count); +extern "C" bool go_btcd_addrv2(const uint8_t *data, size_t len, uint64_t *count); std::optional> Addrv2Core(Span buffer) { @@ -29,9 +29,8 @@ std::optional> Addrv2Core(Span buff return std::make_pair(addrs.size(), clearnet_tor_count); } -void Addrv2(FuzzedDataProvider& provider) +FUZZ_TARGET(Addrv2) { - std::vector buffer{provider.ConsumeRemainingBytes()}; uint64_t count_rust = 0, count_btcd = 0; [[maybe_unused]] bool rust_bitcoin{rust_bitcoin_addrv2(buffer.data(), buffer.size(), &count_rust)}; [[maybe_unused]] bool btcd{go_btcd_addrv2(buffer.data(), buffer.size(), &count_btcd)}; diff --git a/targets/addrv2.h b/targets/addrv2.h deleted file mode 100644 index b7b061d..0000000 --- a/targets/addrv2.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef NET_ADDRESS_H -#define NET_ADDRESS_H - -#include - -void Addrv2(FuzzedDataProvider& provider); -#endif \ No newline at end of file diff --git a/targets/bech32.cpp b/targets/bech32.cpp index 8ad5e2c..4479c95 100644 --- a/targets/bech32.cpp +++ b/targets/bech32.cpp @@ -3,11 +3,11 @@ #include #include -#include "bech32.h" +#include "bitcoin/src/test/fuzz/fuzz.h" #include "bitcoin/src/bech32.h" #include "bitcoin/src/streams.h" -extern "C" char* go_btcd_bech32(uint8_t *data, size_t len); +extern "C" char* go_btcd_bech32(const uint8_t *data, size_t len); std::string Bech32Core(Span buffer) { @@ -20,10 +20,8 @@ std::string Bech32Core(Span buffer) } -void Bech32(FuzzedDataProvider& provider) +FUZZ_TARGET(Bech32) { - std::vector buffer{provider.ConsumeRemainingBytes()}; - std::string core{Bech32Core(buffer)}; std::string go_btcd{go_btcd_bech32(buffer.data(), buffer.size())}; diff --git a/targets/bech32.h b/targets/bech32.h deleted file mode 100644 index 4d02fcd..0000000 --- a/targets/bech32.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef BECH32_H -#define BECH32_H - -#include - -void Bech32(FuzzedDataProvider& provider); -#endif \ No newline at end of file diff --git a/targets/block_des.cpp b/targets/block_des.cpp index e328dbf..565233e 100644 --- a/targets/block_des.cpp +++ b/targets/block_des.cpp @@ -3,12 +3,12 @@ #include #include -#include "block_des.h" +#include "bitcoin/src/test/fuzz/fuzz.h" #include "bitcoin/src/primitives/block.h" #include "bitcoin/src/streams.h" extern "C" char* rust_bitcoin_des_block(const uint8_t *data, size_t len); -extern "C" char* go_btcd_des_block(uint8_t *data, size_t len); +extern "C" char* go_btcd_des_block(const uint8_t *data, size_t len); std::string BlockDesCore(Span buffer) { @@ -24,10 +24,8 @@ std::string BlockDesCore(Span buffer) } // This target is expected to crash, needs some verification (e.g. segwit version). -void BlockDes(FuzzedDataProvider& provider) +FUZZ_TARGET(BlockDes) { - std::vector buffer{provider.ConsumeRemainingBytes()}; - std::string core{BlockDesCore(buffer)}; std::string rust_bitcoin{rust_bitcoin_des_block(buffer.data(), buffer.size())}; std::string go_btcd{go_btcd_des_block(buffer.data(), buffer.size())}; diff --git a/targets/block_des.h b/targets/block_des.h deleted file mode 100644 index d1e954e..0000000 --- a/targets/block_des.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef BLOCK_DES_H -#define BLOCK_DES_H - -#include - -void BlockDes(FuzzedDataProvider& provider); -#endif \ No newline at end of file diff --git a/targets/cmpctblocks.cpp b/targets/cmpctblocks.cpp index 6e34122..b7d43e4 100644 --- a/targets/cmpctblocks.cpp +++ b/targets/cmpctblocks.cpp @@ -2,11 +2,11 @@ #include #include -#include "cmpctblocks.h" +#include "bitcoin/src/test/fuzz/fuzz.h" #include "bitcoin/src/blockencodings.h" #include "bitcoin/src/streams.h" -extern "C" int rust_bitcoin_cmpctblocks(uint8_t *data, size_t len); +extern "C" int rust_bitcoin_cmpctblocks(const uint8_t *data, size_t len); int CmpctBlocksCore(Span buffer) { @@ -22,9 +22,8 @@ int CmpctBlocksCore(Span buffer) return block_header_and_short_txids.BlockTxCount(); } -void CmpctBlocks(FuzzedDataProvider& provider) +FUZZ_TARGET(CmpctBlocks) { - std::vector buffer{provider.ConsumeRemainingBytes()}; int core{CmpctBlocksCore(buffer)}; int rust_bitcoin{rust_bitcoin_cmpctblocks(buffer.data(), buffer.size())}; diff --git a/targets/cmpctblocks.h b/targets/cmpctblocks.h deleted file mode 100644 index 602cd3e..0000000 --- a/targets/cmpctblocks.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef CMPCT_BLOCKS_H -#define CMPCT_BLOCKS_H - -#include - -void CmpctBlocks(FuzzedDataProvider& provider); -#endif \ No newline at end of file diff --git a/targets/miniscript_string.cpp b/targets/miniscript_string.cpp index 89b4ead..f6fda8c 100644 --- a/targets/miniscript_string.cpp +++ b/targets/miniscript_string.cpp @@ -3,7 +3,7 @@ #include #include