Skip to content

Commit

Permalink
add target for net addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoerg committed Jun 22, 2024
1 parent e62d377 commit 38cd68a
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CXX = clang++
CC = clang
SOURCES := targets/script.cpp targets/psbt.cpp targets/bech32.cpp targets/tx_des.cpp targets/miniscript_string.cpp targets/block_des.cpp targets/prefilledtransaction.cpp
SOURCES := targets/netaddress.cpp targets/script.cpp targets/psbt.cpp targets/bech32.cpp targets/tx_des.cpp targets/miniscript_string.cpp targets/block_des.cpp targets/prefilledtransaction.cpp
INCLUDES = dependencies/ dependencies/bitcoin/src/ dependencies/bitcoin/src/secp256k1/include
LIB_DIR = dependencies/bitcoin/src/ dependencies/bitcoin/src/.libs dependencies/bitcoin/src/secp256k1/.libs rust_bitcoin_lib/target/debug btcd_lib
OBJS := $(patsubst %.cpp, build/%.o, $(SOURCES))
Expand Down
3 changes: 3 additions & 0 deletions fuzzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "targets/bech32.h"
#include "targets/psbt.h"
#include "targets/script.h"
#include "targets/netaddress.h"

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
FuzzedDataProvider provider(data, size);
Expand All @@ -34,6 +35,8 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
Psbt(provider);
} else if (target == "script") {
Script(provider);
} else if (target == "net_address") {
NetAddress(provider);
}

return 0; // Values other than 0 and -1 are reserved for future use.
Expand Down
5 changes: 3 additions & 2 deletions rust_bitcoin_lib/rust_bitcoin_lib.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#include <string>

extern bool rust_miniscript_from_str(const char* miniscript_str);
extern bool rust_bitcoin_script(const char* miniscript_str);
extern char* rust_bitcoin_psbt(const char* miniscript_str);
extern bool rust_bitcoin_script(const uint8_t *data, size_t len);
extern char* rust_bitcoin_psbt(const uint8_t *data, size_t len);
extern char* rust_miniscript_from_str_check_key(const char* miniscript_str);
extern char* rust_bitcoin_des_block(const uint8_t *data, size_t len);
extern char* rust_bitcoin_prefilledtransaction(const uint8_t *data, size_t len);
extern bool rust_bitcoin_net_address(uint8_t *data, size_t len);
12 changes: 12 additions & 0 deletions rust_bitcoin_lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ pub unsafe extern "C" fn rust_bitcoin_script(data: *const u8, len: usize) -> boo
}
}

#[no_mangle]
pub unsafe extern "C" fn rust_bitcoin_net_address(data: *const u8, len: usize) -> bool {
// Safety: Ensure that the data pointer is valid for the given length
let data_slice = slice::from_raw_parts(data, len);

let addr: Result<(bitcoin::p2p::address::AddrV2, usize), _> = encode::deserialize_partial(data_slice);
match addr {
Err(_) => false,
Ok(_) => true
}
}

#[no_mangle]
pub unsafe extern "C" fn rust_miniscript_from_str_check_key(input: *const c_char) -> *mut c_char {
let Ok(desc) = c_str_to_str(input) else {
Expand Down
32 changes: 32 additions & 0 deletions targets/netaddress.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <fuzzer/FuzzedDataProvider.h>
#include <string>
#include <iostream>
#include <stdio.h>

#include "netaddress.h"
#include "bitcoin/src/netaddress.h"
#include "bitcoin/src/streams.h"

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

bool NetAddressCore(Span<const uint8_t> buffer)
{
DataStream ds{buffer};
CNetAddr net_addr;
try {
ds >> CNetAddr::SerParams{CNetAddr::Encoding::V2}(net_addr);
if (net_addr.ToStringAddr() == "::") return false;
} catch (const std::ios_base::failure& e) {
return false;
}
return net_addr.IsValid();
}


void NetAddress(FuzzedDataProvider& provider)
{
std::vector<uint8_t> buffer{provider.ConsumeRemainingBytes<uint8_t>()};
bool core{NetAddressCore(buffer)};
bool rust_bitcoin{rust_bitcoin_net_address(buffer.data(), buffer.size())};
if (core) assert(rust_bitcoin);
}
7 changes: 7 additions & 0 deletions targets/netaddress.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef NET_ADDRESS_H
#define NET_ADDRESS_H

#include <fuzzer/FuzzedDataProvider.h>

void NetAddress(FuzzedDataProvider& provider);
#endif

0 comments on commit 38cd68a

Please sign in to comment.