Skip to content

Commit ef367e3

Browse files
committed
Added fuzz test for satisfy
1 parent dd3d78a commit ef367e3

File tree

4 files changed

+130
-2
lines changed

4 files changed

+130
-2
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ description = "Miniscript: a subset of Bitcoin Script designed for analysis"
77
license = "CC0-1.0"
88

99
[features]
10-
fuzztarget = ["bitcoin/fuzztarget"]
1110
compiler = []
1211
trace = []
1312
unstable = []

examples/parse.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ extern crate miniscript;
2020
use miniscript::NullCtx;
2121
use std::str::FromStr;
2222

23+
use miniscript::miniscript::satisfy::bitcoinsig_from_rawsig;
24+
2325
fn main() {
26+
let v : Vec<u8> = bitcoin::hashes::hex::FromHex::from_hex("30440220510316cbecd5c8057783d1bf15d050e34298fe44547ad3267a1d6dbdf912064c022066ac4137a1a8dc6d5ae1c37329ea022d45a7ac5c4500c325b570e4542d5e19f801").unwrap();
27+
let sig = bitcoinsig_from_rawsig(&v).unwrap();
2428
let my_descriptor = miniscript::Descriptor::<bitcoin::PublicKey>::from_str(
2529
"wsh(c:pk_k(020202020202020202020202020202020202020202020202020202020202020202))",
2630
)

fuzz/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ honggfuzz_fuzz = ["honggfuzz"]
1515
honggfuzz = { version = "0.5", optional = true }
1616
afl = { version = "0.8", optional = true }
1717
regex = { version = "1.4"}
18-
miniscript = { path = "..", features = ["fuzztarget", "compiler"] }
18+
miniscript = { path = "..", features = ["compiler"] }
1919

2020
# Prevent this from interfering with workspaces
2121
[workspace]
@@ -52,3 +52,7 @@ path = "fuzz_targets/parse_descriptor.rs"
5252
[[bin]]
5353
name = "parse_descriptor_secret"
5454
path = "fuzz_targets/parse_descriptor_secret.rs"
55+
56+
[[bin]]
57+
name = "miniscript_satisfy"
58+
path = "fuzz_targets/miniscript_satisfy.rs"
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
extern crate miniscript;
2+
3+
use miniscript::miniscript::satisfy::bitcoinsig_from_rawsig;
4+
use miniscript::Segwitv0;
5+
use miniscript::{
6+
bitcoin, BitcoinSig, DummyKey, DummyKeyHash, Miniscript, NullCtx, Satisfier, ToPublicKey,
7+
};
8+
9+
use bitcoin::hashes::{hash160, ripemd160, sha256, sha256d};
10+
11+
use std::str::FromStr;
12+
13+
struct DummySatisfier {}
14+
15+
impl Satisfier<NullCtx, DummyKey> for DummySatisfier {
16+
fn lookup_sig(&self, pk: &DummyKey, _to_pk_ctx: NullCtx) -> Option<BitcoinSig> {
17+
let v : Vec<u8> = bitcoin::hashes::hex::FromHex::from_hex("30440220510316cbecd5c8057783d1bf15d050e34298fe44547ad3267a1d6dbdf912064c022066ac4137a1a8dc6d5ae1c37329ea022d45a7ac5c4500c325b570e4542d5e19f801").unwrap();
18+
dbg!("here");
19+
Some(bitcoinsig_from_rawsig(&v).unwrap())
20+
}
21+
22+
fn lookup_pkh_pk(&self, _: &DummyKeyHash) -> Option<DummyKey> {
23+
Some(DummyKey)
24+
}
25+
26+
fn lookup_pkh_sig(
27+
&self,
28+
_: &DummyKeyHash,
29+
_to_pk_ctx: NullCtx,
30+
) -> Option<(bitcoin::PublicKey, BitcoinSig)> {
31+
dbg!("here");
32+
let dummy = DummyKey;
33+
let v : Vec<u8> = bitcoin::hashes::hex::FromHex::from_hex("30440220510316cbecd5c8057783d1bf15d050e34298fe44547ad3267a1d6dbdf912064c022066ac4137a1a8dc6d5ae1c37329ea022d45a7ac5c4500c325b570e4542d5e19f801").unwrap();
34+
panic!();
35+
Some((
36+
dummy.to_public_key(NullCtx),
37+
(bitcoinsig_from_rawsig(&v).unwrap()),
38+
))
39+
}
40+
41+
fn lookup_sha256(&self, _: sha256::Hash) -> Option<[u8; 32]> {
42+
Some([0u8; 32])
43+
}
44+
45+
fn lookup_hash256(&self, _: sha256d::Hash) -> Option<[u8; 32]> {
46+
Some([0u8; 32])
47+
}
48+
49+
fn lookup_ripemd160(&self, _: ripemd160::Hash) -> Option<[u8; 32]> {
50+
Some([0u8; 32])
51+
}
52+
53+
fn lookup_hash160(&self, _: hash160::Hash) -> Option<[u8; 32]> {
54+
Some([0u8; 32])
55+
}
56+
57+
fn check_older(&self, _: u32) -> bool {
58+
true
59+
}
60+
61+
fn check_after(&self, _: u32) -> bool {
62+
true
63+
}
64+
}
65+
66+
fn do_test(data: &[u8]) {
67+
let data_str = String::from_utf8_lossy(data);
68+
if let Ok(ms) = Miniscript::<DummyKey, Segwitv0>::from_str(&data_str) {
69+
if let Err(e) = ms.satisfy(DummySatisfier {}, NullCtx) {
70+
dbg!(e);
71+
}
72+
}
73+
}
74+
75+
#[cfg(feature = "afl")]
76+
extern crate afl;
77+
#[cfg(feature = "afl")]
78+
fn main() {
79+
afl::read_stdio_bytes(|data| {
80+
do_test(&data);
81+
});
82+
}
83+
84+
#[cfg(feature = "honggfuzz")]
85+
#[macro_use]
86+
extern crate honggfuzz;
87+
#[cfg(feature = "honggfuzz")]
88+
fn main() {
89+
loop {
90+
fuzz!(|data| {
91+
do_test(data);
92+
});
93+
}
94+
}
95+
96+
#[cfg(test)]
97+
mod tests {
98+
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
99+
let mut b = 0;
100+
for (idx, c) in hex.as_bytes().iter().enumerate() {
101+
b <<= 4;
102+
match *c {
103+
b'A'...b'F' => b |= c - b'A' + 10,
104+
b'a'...b'f' => b |= c - b'a' + 10,
105+
b'0'...b'9' => b |= c - b'0',
106+
_ => panic!("Bad hex"),
107+
}
108+
if (idx & 1) == 1 {
109+
out.push(b);
110+
b = 0;
111+
}
112+
}
113+
}
114+
115+
#[test]
116+
fn duplicate_crash_here() {
117+
let mut a = Vec::new();
118+
extend_vec_from_hex("706b2829", &mut a);
119+
super::do_test(&a);
120+
}
121+
}

0 commit comments

Comments
 (0)