Skip to content

Commit

Permalink
Merge pull request #54 from hax0kartik/bitcoin-fuzz
Browse files Browse the repository at this point in the history
Replace fuzzer.cpp with bitcoin core's fuzz.cpp
  • Loading branch information
brunoerg authored Jul 3, 2024
2 parents 21c9ab9 + 385d429 commit caf44c1
Show file tree
Hide file tree
Showing 21 changed files with 32 additions and 146 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ jobs:
fi
- name: Test
run: ./bitcoinfuzz -detect_leaks=0 -runs=0
run: PRINT_ALL_FUZZ_TARGETS_AND_ABORT=1 ./bitcoinfuzz
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
46 changes: 0 additions & 46 deletions fuzzer.cpp

This file was deleted.

9 changes: 4 additions & 5 deletions targets/addrv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
#include <optional>
#include <stdio.h>

#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<std::pair<uint64_t, uint64_t>> Addrv2Core(Span<const uint8_t> buffer)
{
Expand All @@ -29,9 +29,8 @@ std::optional<std::pair<uint64_t, uint64_t>> Addrv2Core(Span<const uint8_t> buff
return std::make_pair(addrs.size(), clearnet_tor_count);
}

void Addrv2(FuzzedDataProvider& provider)
FUZZ_TARGET(addrv2)
{
std::vector<uint8_t> buffer{provider.ConsumeRemainingBytes<uint8_t>()};
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)};
Expand Down
7 changes: 0 additions & 7 deletions targets/addrv2.h

This file was deleted.

8 changes: 3 additions & 5 deletions targets/bech32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
#include <iostream>
#include <stdio.h>

#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<const uint8_t> buffer)
{
Expand All @@ -20,10 +20,8 @@ std::string Bech32Core(Span<const uint8_t> buffer)
}


void Bech32(FuzzedDataProvider& provider)
FUZZ_TARGET(bech32)
{
std::vector<uint8_t> buffer{provider.ConsumeRemainingBytes<uint8_t>()};

std::string core{Bech32Core(buffer)};
std::string go_btcd{go_btcd_bech32(buffer.data(), buffer.size())};

Expand Down
7 changes: 0 additions & 7 deletions targets/bech32.h

This file was deleted.

8 changes: 3 additions & 5 deletions targets/block_des.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
#include <iostream>
#include <stdio.h>

#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<const uint8_t> buffer)
{
Expand All @@ -24,10 +24,8 @@ std::string BlockDesCore(Span<const uint8_t> buffer)
}

// This target is expected to crash, needs some verification (e.g. segwit version).
void BlockDes(FuzzedDataProvider& provider)
FUZZ_TARGET(block_des)
{
std::vector<uint8_t> buffer{provider.ConsumeRemainingBytes<uint8_t>()};

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())};
Expand Down
7 changes: 0 additions & 7 deletions targets/block_des.h

This file was deleted.

7 changes: 3 additions & 4 deletions targets/cmpctblocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
#include <string>
#include <iostream>

#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<const uint8_t> buffer)
{
Expand All @@ -22,9 +22,8 @@ int CmpctBlocksCore(Span<const uint8_t> buffer)
return block_header_and_short_txids.BlockTxCount();
}

void CmpctBlocks(FuzzedDataProvider& provider)
FUZZ_TARGET(cmpct_blocks)
{
std::vector<uint8_t> buffer{provider.ConsumeRemainingBytes<uint8_t>()};
int core{CmpctBlocksCore(buffer)};
int rust_bitcoin{rust_bitcoin_cmpctblocks(buffer.data(), buffer.size())};

Expand Down
7 changes: 0 additions & 7 deletions targets/cmpctblocks.h

This file was deleted.

7 changes: 5 additions & 2 deletions targets/miniscript_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <iostream>
#include <script/miniscript.h>

#include "miniscript_string.h"
#include "bitcoin/src/test/fuzz/fuzz.h"
#include "bitcoin/src/pubkey.h"
#include "bitcoin/src/key.h"

Expand Down Expand Up @@ -184,8 +184,11 @@ bool BitcoinCoreString(const std::string& input_str)
return false;
}

void MiniscriptFromString(FuzzedDataProvider& provider)
FUZZ_TARGET(miniscript_from_string)
{
if (buffer.empty()) return;
FuzzedDataProvider provider(buffer.data(), buffer.size());

std::string input_str{provider.ConsumeRemainingBytesAsString().c_str()};
const bool core{BitcoinCoreString(input_str)};
const std::string rust_miniscript{rust_miniscript_from_str_check_key(input_str.c_str())};
Expand Down
7 changes: 0 additions & 7 deletions targets/miniscript_string.h

This file was deleted.

4 changes: 2 additions & 2 deletions targets/prefilledtransaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <iostream>
#include <stdio.h>

#include "bitcoin/src/test/fuzz/fuzz.h"
#include "bitcoin/src/blockencodings.h"
#include "bitcoin/src/streams.h"

Expand All @@ -20,9 +21,8 @@ std::optional<uint16_t> PrefilledTransactionCore(Span<const uint8_t> buffer)
return tx.index;
}

void PrefilledTransactionTarget(FuzzedDataProvider& provider)
FUZZ_TARGET(prefilled_transaction)
{
std::vector<uint8_t> buffer{provider.ConsumeRemainingBytes<uint8_t>()};
auto core{PrefilledTransactionCore(buffer)};
std::string rust_bitcoin{rust_bitcoin_prefilledtransaction(buffer.data(), buffer.size())};
if (rust_bitcoin == "unsupported segwit version") return;
Expand Down
7 changes: 0 additions & 7 deletions targets/prefilledtransaction.h

This file was deleted.

7 changes: 4 additions & 3 deletions targets/psbt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
#include <stdio.h>

#include "psbt.h"

#include "bitcoin/src/test/fuzz/fuzz.h"
#include "bitcoin/src/psbt.h"
#include "bitcoin/src/span.h"
#include "bitcoin/src/node/psbt.h"

extern "C" char* rust_bitcoin_psbt(uint8_t *data, size_t len);
extern "C" char* rust_bitcoin_psbt(const uint8_t *data, size_t len);

bool PSBTCore(Span<const uint8_t> buffer)
{
Expand All @@ -21,9 +23,8 @@ bool PSBTCore(Span<const uint8_t> buffer)
}


void Psbt(FuzzedDataProvider& provider)
FUZZ_TARGET(psbt)
{
std::vector<uint8_t> buffer{provider.ConsumeRemainingBytes<uint8_t>()};
bool core{PSBTCore(buffer)};
std::string rust_bitcoin{rust_bitcoin_psbt(buffer.data(), buffer.size())};
if (rust_bitcoin == "invalid xonly public key" ||
Expand Down
7 changes: 0 additions & 7 deletions targets/psbt.h

This file was deleted.

7 changes: 3 additions & 4 deletions targets/script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
#include <iostream>
#include <stdio.h>

#include "script.h"
#include "bitcoin/src/test/fuzz/fuzz.h"
#include "bitcoin/src/streams.h"
#include "bitcoin/src/script/script.h"

extern "C" bool rust_bitcoin_script(uint8_t *data, size_t len);
extern "C" bool rust_bitcoin_script(const uint8_t *data, size_t len);

bool CoreScript(Span<const uint8_t> buffer)
{
Expand All @@ -22,9 +22,8 @@ bool CoreScript(Span<const uint8_t> buffer)
}


void Script(FuzzedDataProvider& provider)
FUZZ_TARGET(script)
{
std::vector<uint8_t> buffer{provider.ConsumeRemainingBytes<uint8_t>()};
bool core{CoreScript(buffer)};
bool rust_bitcoin{rust_bitcoin_script(buffer.data(), buffer.size())};
assert(core == rust_bitcoin);
Expand Down
7 changes: 0 additions & 7 deletions targets/script.h

This file was deleted.

8 changes: 3 additions & 5 deletions targets/tx_des.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
#include <iostream>
#include <stdio.h>

#include "tx_des.h"
#include "bitcoin/src/test/fuzz/fuzz.h"
#include "bitcoin/src/consensus/tx_check.h"
#include "bitcoin/src/consensus/validation.h"
#include "bitcoin/src/primitives/block.h"
#include "bitcoin/src/streams.h"

extern "C" char* go_btcd_des_tx(uint8_t *data, size_t len);
extern "C" char* go_btcd_des_tx(const uint8_t *data, size_t len);

std::string TransactionDesCore(Span<const uint8_t> buffer)
{
Expand All @@ -28,10 +28,8 @@ std::string TransactionDesCore(Span<const uint8_t> buffer)
return "";
}

void TransactionDes(FuzzedDataProvider& provider)
FUZZ_TARGET(transaction_des)
{
std::vector<uint8_t> buffer{provider.ConsumeRemainingBytes<uint8_t>()};

std::string core{TransactionDesCore(buffer)};
std::string go_btcd{go_btcd_des_tx(buffer.data(), buffer.size())};
assert(core == go_btcd);
Expand Down
7 changes: 0 additions & 7 deletions targets/tx_des.h

This file was deleted.

0 comments on commit caf44c1

Please sign in to comment.