Skip to content

Commit

Permalink
add target for script
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoerg committed Jun 20, 2024
1 parent 770c152 commit 4d08b82
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 1 deletion.
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/psbt.cpp targets/bech32.cpp targets/tx_des.cpp targets/miniscript_string.cpp targets/block_des.cpp targets/prefilledtransaction.cpp
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
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 @@ -12,6 +12,7 @@
#include "targets/tx_des.h"
#include "targets/bech32.h"
#include "targets/psbt.h"
#include "targets/script.h"

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

return 0; // Values other than 0 and -1 are reserved for future use.
Expand Down
1 change: 1 addition & 0 deletions rust_bitcoin_lib/rust_bitcoin_lib.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#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 char* rust_miniscript_from_str_check_key(const char* miniscript_str);
extern char* rust_bitcoin_des_block(const uint8_t *data, size_t len);
Expand Down
18 changes: 18 additions & 0 deletions rust_bitcoin_lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::str::Utf8Error;

use bitcoin::bip152::PrefilledTransaction;
use bitcoin::consensus::deserialize_partial;
use bitcoin::consensus::encode;
use bitcoin::Block;
use miniscript::bitcoin::script;
use miniscript::bitcoin::secp256k1::XOnlyPublicKey;
Expand Down Expand Up @@ -100,6 +101,23 @@ pub unsafe extern "C" fn rust_bitcoin_psbt(data: *const u8, len: usize) -> *mut
}
}

#[no_mangle]
pub unsafe extern "C" fn rust_bitcoin_script(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 script: Result<(bitcoin::script::ScriptBuf, usize), encode::Error> = encode::deserialize_partial(data_slice);
match script {
Err(_) => false,
Ok(s) => {
if s.0.is_op_return() || s.0.len() > 10_000 {
return false
}
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
31 changes: 31 additions & 0 deletions targets/script.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <fuzzer/FuzzedDataProvider.h>
#include <iostream>
#include <stdio.h>

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

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

bool CoreScript(Span<const uint8_t> buffer)
{
DataStream ds{buffer};
CScript script;
try {
ds >> script;
} catch (const std::ios_base::failure& e) {
return false;
}
if (script.IsUnspendable()) return false;
return true;
}


void Script(FuzzedDataProvider& provider)
{
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);
}
7 changes: 7 additions & 0 deletions targets/script.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef SCRIPT_H
#define SCRIPT_H

#include <fuzzer/FuzzedDataProvider.h>

void Script(FuzzedDataProvider& provider);
#endif

0 comments on commit 4d08b82

Please sign in to comment.