Skip to content

Fuzz fixes #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ matrix:
env: DO_FUZZ=true DO_LINT=true
- rust: beta
- rust: nightly
env: DO_BENCH=true
- rust: 1.22.0

script:
Expand Down
2 changes: 1 addition & 1 deletion contrib/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ fi
# Bench if told to
if [ "$DO_BENCH" = true ]
then
cargo bench --features unstable
cargo bench --features="unstable compiler"
fi
7 changes: 2 additions & 5 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ honggfuzz_fuzz = ["honggfuzz"]
[dependencies]
honggfuzz = { version = "0.5", optional = true }
afl = { version = "0.3", optional = true }
regex = { version = "1.3.9"}
miniscript = { path = "..", features = ["fuzztarget", "compiler"] }

# Prevent this from interfering with workspaces
Expand Down Expand Up @@ -42,8 +43,4 @@ path = "fuzz_targets/roundtrip_semantic.rs"

[[bin]]
name = "compile_descriptor"
path = "fuzz_targets/compile_descriptor.rs"

[[bin]]
name = "roundtrip_policy"
path = "fuzz_targets/roundtrip_policy.rs"
path = "fuzz_targets/compile_descriptor.rs"
39 changes: 39 additions & 0 deletions fuzz/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Fuzz Tests

Repository for fuzz testing Miniscript.

## How to reproduce crashes?

Travis should output a offending hex("048531e80700ae6400670000af5168" in the example)
which you can use as shown. Copy and paste the following code lines into file reporting crashes and
replace the hex with the offending hex.
Refer to file [roundtrip_concrete.rs](./fuzz_targets/roundtrip_concrete.rs) for an example.

```
#[cfg(test)]
mod tests {
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
let mut b = 0;
for (idx, c) in hex.as_bytes().iter().enumerate() {
b <<= 4;
match *c {
b'A'...b'F' => b |= c - b'A' + 10,
b'a'...b'f' => b |= c - b'a' + 10,
b'0'...b'9' => b |= c - b'0',
_ => panic!("Bad hex"),
}
if (idx & 1) == 1 {
out.push(b);
b = 0;
}
}
}

#[test]
fn duplicate_crash() {
let mut a = Vec::new();
extend_vec_from_hex("048531e80700ae6400670000af5168", &mut a);
super::do_test(&a);
}
}
```
28 changes: 0 additions & 28 deletions fuzz/fuzz_targets/compile_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,31 +46,3 @@ fn main() {
});
}
}

#[cfg(test)]
mod tests {
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
let mut b = 0;
for (idx, c) in hex.as_bytes().iter().enumerate() {
b <<= 4;
match *c {
b'A'...b'F' => b |= c - b'A' + 10,
b'a'...b'f' => b |= c - b'a' + 10,
b'0'...b'9' => b |= c - b'0',
_ => panic!("Bad hex"),
}
if (idx & 1) == 1 {
out.push(b);
b = 0;
}
}
}

#[test]
fn duplicate_crash() {
super::do_test(b"pkh()");
let mut a = Vec::new();
extend_vec_from_hex("00", &mut a);
super::do_test(&a);
}
}
7 changes: 6 additions & 1 deletion fuzz/fuzz_targets/roundtrip_concrete.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@

extern crate miniscript;

extern crate regex;
use std::str::FromStr;
use miniscript::{policy, DummyKey};
use regex::Regex;

type DummyPolicy = policy::Concrete<DummyKey>;

fn do_test(data: &[u8]) {
let data_str = String::from_utf8_lossy(data);
if let Ok(pol) = DummyPolicy::from_str(&data_str) {
let output = pol.to_string();
//remove all instances of 1@
let re = Regex::new("(\\D)1@").unwrap();
let output = re.replace_all(&output, "$1");
let data_str = re.replace_all(&data_str, "$1");
assert_eq!(data_str, output);
}
}
Expand Down
36 changes: 1 addition & 35 deletions fuzz/fuzz_targets/roundtrip_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,38 +32,4 @@ fn main() {
do_test(data);
});
}
}

#[cfg(test)]
mod tests {
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
let mut b = 0;
for (idx, c) in hex.as_bytes().iter().enumerate() {
b <<= 4;
match *c {
b'A'...b'F' => b |= c - b'A' + 10,
b'a'...b'f' => b |= c - b'a' + 10,
b'0'...b'9' => b |= c - b'0',
_ => panic!("Bad hex"),
}
if (idx & 1) == 1 {
out.push(b);
b = 0;
}
}
}

#[test]
fn duplicate_crash() {
let mut a = Vec::new();
extend_vec_from_hex("00", &mut a);
super::do_test(&a);
}

#[test]
fn test_cpkk_alias() {
let mut a = Vec::new();
extend_vec_from_hex("633a706b5f6b2829", &mut a); // c:pk_k()
super::do_test(&a);
}
}
}
27 changes: 0 additions & 27 deletions fuzz/fuzz_targets/roundtrip_miniscript_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,3 @@ fn main() {
});
}
}

#[cfg(test)]
mod tests {
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
let mut b = 0;
for (idx, c) in hex.as_bytes().iter().enumerate() {
b <<= 4;
match *c {
b'A'...b'F' => b |= c - b'A' + 10,
b'a'...b'f' => b |= c - b'a' + 10,
b'0'...b'9' => b |= c - b'0',
_ => panic!("Bad hex"),
}
if (idx & 1) == 1 {
out.push(b);
b = 0;
}
}
}

#[test]
fn duplicate_crash() {
let mut a = Vec::new();
extend_vec_from_hex("007c920092935187", &mut a);
super::do_test(&a);
}
}
27 changes: 0 additions & 27 deletions fuzz/fuzz_targets/roundtrip_miniscript_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,3 @@ fn main() {
});
}
}

#[cfg(test)]
mod tests {
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
let mut b = 0;
for (idx, c) in hex.as_bytes().iter().enumerate() {
b <<= 4;
match *c {
b'A'...b'F' => b |= c - b'A' + 10,
b'a'...b'f' => b |= c - b'a' + 10,
b'0'...b'9' => b |= c - b'0',
_ => panic!("Bad hex"),
}
if (idx & 1) == 1 {
out.push(b);
b = 0;
}
}
}

#[test]
fn duplicate_crash() {
let mut a = Vec::new();
extend_vec_from_hex("00", &mut a);
super::do_test(&a);
}
}
74 changes: 0 additions & 74 deletions fuzz/fuzz_targets/roundtrip_policy.rs

This file was deleted.

29 changes: 1 addition & 28 deletions fuzz/fuzz_targets/roundtrip_semantic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,4 @@ fn main() {
do_test(data);
});
}
}

#[cfg(test)]
mod tests {
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
let mut b = 0;
for (idx, c) in hex.as_bytes().iter().enumerate() {
b <<= 4;
match *c {
b'A'...b'F' => b |= c - b'A' + 10,
b'a'...b'f' => b |= c - b'a' + 10,
b'0'...b'9' => b |= c - b'0',
_ => panic!("Bad hex"),
}
if (idx & 1) == 1 {
out.push(b);
b = 0;
}
}
}

#[test]
fn duplicate_crash() {
let mut a = Vec::new();
extend_vec_from_hex("048531e80700ae6400670000af5168", &mut a);
super::do_test(&a);
}
}
}
21 changes: 16 additions & 5 deletions src/descriptor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ where
fn deserialize<D: de::Deserializer<'de>>(d: D) -> Result<Descriptor<Pk>, D::Error> {
use std::marker::PhantomData;

struct StrVisitor<Qk>(PhantomData<(Qk)>);
struct StrVisitor<Qk>(PhantomData<Qk>);

impl<'de, Qk> de::Visitor<'de> for StrVisitor<Qk>
where
Expand Down Expand Up @@ -536,14 +536,25 @@ mod tests {
use bitcoin::{self, secp256k1, PublicKey};
use miniscript::satisfy::BitcoinSig;
use std::str::FromStr;
use Descriptor;
use Miniscript;
use Satisfier;
use {Descriptor, DummyKey, Miniscript, Satisfier};

type StdDescriptor = Descriptor<PublicKey>;
const TEST_PK: &'static str =
"pk(020000000000000000000000000000000000000000000000000000000000000002)";

fn roundtrip_descriptor(s: &str) {
let desc = Descriptor::<DummyKey>::from_str(&s).unwrap();
let output = desc.to_string();
let normalize_aliases = s.replace("c:pk_k(", "pk(");
assert_eq!(normalize_aliases, output);
}

#[test]
fn desc_rtt_tests() {
roundtrip_descriptor("c:pk_k()");
roundtrip_descriptor("wsh(pk())");
roundtrip_descriptor("wsh(c:pk_k())");
}
#[test]
fn parse_descriptor() {
StdDescriptor::from_str("(").unwrap_err();
Expand Down Expand Up @@ -898,7 +909,7 @@ mod tests {
}

#[test]
fn empty_multi() {
fn roundtrip_tests() {
let descriptor = Descriptor::<bitcoin::PublicKey>::from_str("multi");
assert_eq!(
descriptor.unwrap_err().to_string(),
Expand Down
Loading