Skip to content

Commit b4e33e5

Browse files
authored
Merge pull request #95 from sanket1729/fuzz_fixes
Fuzz fixes
2 parents 8f6cf9b + b0c6452 commit b4e33e5

File tree

18 files changed

+207
-303
lines changed

18 files changed

+207
-303
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ matrix:
99
env: DO_FUZZ=true DO_LINT=true
1010
- rust: beta
1111
- rust: nightly
12+
env: DO_BENCH=true
1213
- rust: 1.22.0
1314

1415
script:

contrib/test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,5 @@ fi
4646
# Bench if told to
4747
if [ "$DO_BENCH" = true ]
4848
then
49-
cargo bench --features unstable
49+
cargo bench --features="unstable compiler"
5050
fi

fuzz/Cargo.toml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ honggfuzz_fuzz = ["honggfuzz"]
1414
[dependencies]
1515
honggfuzz = { version = "0.5", optional = true }
1616
afl = { version = "0.3", optional = true }
17+
regex = { version = "1.3.9"}
1718
miniscript = { path = "..", features = ["fuzztarget", "compiler"] }
1819

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

4344
[[bin]]
4445
name = "compile_descriptor"
45-
path = "fuzz_targets/compile_descriptor.rs"
46-
47-
[[bin]]
48-
name = "roundtrip_policy"
49-
path = "fuzz_targets/roundtrip_policy.rs"
46+
path = "fuzz_targets/compile_descriptor.rs"

fuzz/README

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Fuzz Tests
2+
3+
Repository for fuzz testing Miniscript.
4+
5+
## How to reproduce crashes?
6+
7+
Travis should output a offending hex("048531e80700ae6400670000af5168" in the example)
8+
which you can use as shown. Copy and paste the following code lines into file reporting crashes and
9+
replace the hex with the offending hex.
10+
Refer to file [roundtrip_concrete.rs](./fuzz_targets/roundtrip_concrete.rs) for an example.
11+
12+
```
13+
#[cfg(test)]
14+
mod tests {
15+
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
16+
let mut b = 0;
17+
for (idx, c) in hex.as_bytes().iter().enumerate() {
18+
b <<= 4;
19+
match *c {
20+
b'A'...b'F' => b |= c - b'A' + 10,
21+
b'a'...b'f' => b |= c - b'a' + 10,
22+
b'0'...b'9' => b |= c - b'0',
23+
_ => panic!("Bad hex"),
24+
}
25+
if (idx & 1) == 1 {
26+
out.push(b);
27+
b = 0;
28+
}
29+
}
30+
}
31+
32+
#[test]
33+
fn duplicate_crash() {
34+
let mut a = Vec::new();
35+
extend_vec_from_hex("048531e80700ae6400670000af5168", &mut a);
36+
super::do_test(&a);
37+
}
38+
}
39+
```

fuzz/fuzz_targets/compile_descriptor.rs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,31 +46,3 @@ fn main() {
4646
});
4747
}
4848
}
49-
50-
#[cfg(test)]
51-
mod tests {
52-
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
53-
let mut b = 0;
54-
for (idx, c) in hex.as_bytes().iter().enumerate() {
55-
b <<= 4;
56-
match *c {
57-
b'A'...b'F' => b |= c - b'A' + 10,
58-
b'a'...b'f' => b |= c - b'a' + 10,
59-
b'0'...b'9' => b |= c - b'0',
60-
_ => panic!("Bad hex"),
61-
}
62-
if (idx & 1) == 1 {
63-
out.push(b);
64-
b = 0;
65-
}
66-
}
67-
}
68-
69-
#[test]
70-
fn duplicate_crash() {
71-
super::do_test(b"pkh()");
72-
let mut a = Vec::new();
73-
extend_vec_from_hex("00", &mut a);
74-
super::do_test(&a);
75-
}
76-
}

fuzz/fuzz_targets/roundtrip_concrete.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11

22
extern crate miniscript;
3-
3+
extern crate regex;
44
use std::str::FromStr;
55
use miniscript::{policy, DummyKey};
6+
use regex::Regex;
67

78
type DummyPolicy = policy::Concrete<DummyKey>;
89

910
fn do_test(data: &[u8]) {
1011
let data_str = String::from_utf8_lossy(data);
1112
if let Ok(pol) = DummyPolicy::from_str(&data_str) {
1213
let output = pol.to_string();
14+
//remove all instances of 1@
15+
let re = Regex::new("(\\D)1@").unwrap();
16+
let output = re.replace_all(&output, "$1");
17+
let data_str = re.replace_all(&data_str, "$1");
1318
assert_eq!(data_str, output);
1419
}
1520
}

fuzz/fuzz_targets/roundtrip_descriptor.rs

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -32,38 +32,4 @@ fn main() {
3232
do_test(data);
3333
});
3434
}
35-
}
36-
37-
#[cfg(test)]
38-
mod tests {
39-
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
40-
let mut b = 0;
41-
for (idx, c) in hex.as_bytes().iter().enumerate() {
42-
b <<= 4;
43-
match *c {
44-
b'A'...b'F' => b |= c - b'A' + 10,
45-
b'a'...b'f' => b |= c - b'a' + 10,
46-
b'0'...b'9' => b |= c - b'0',
47-
_ => panic!("Bad hex"),
48-
}
49-
if (idx & 1) == 1 {
50-
out.push(b);
51-
b = 0;
52-
}
53-
}
54-
}
55-
56-
#[test]
57-
fn duplicate_crash() {
58-
let mut a = Vec::new();
59-
extend_vec_from_hex("00", &mut a);
60-
super::do_test(&a);
61-
}
62-
63-
#[test]
64-
fn test_cpkk_alias() {
65-
let mut a = Vec::new();
66-
extend_vec_from_hex("633a706b5f6b2829", &mut a); // c:pk_k()
67-
super::do_test(&a);
68-
}
69-
}
35+
}

fuzz/fuzz_targets/roundtrip_miniscript_script.rs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,3 @@ fn main() {
3434
});
3535
}
3636
}
37-
38-
#[cfg(test)]
39-
mod tests {
40-
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
41-
let mut b = 0;
42-
for (idx, c) in hex.as_bytes().iter().enumerate() {
43-
b <<= 4;
44-
match *c {
45-
b'A'...b'F' => b |= c - b'A' + 10,
46-
b'a'...b'f' => b |= c - b'a' + 10,
47-
b'0'...b'9' => b |= c - b'0',
48-
_ => panic!("Bad hex"),
49-
}
50-
if (idx & 1) == 1 {
51-
out.push(b);
52-
b = 0;
53-
}
54-
}
55-
}
56-
57-
#[test]
58-
fn duplicate_crash() {
59-
let mut a = Vec::new();
60-
extend_vec_from_hex("007c920092935187", &mut a);
61-
super::do_test(&a);
62-
}
63-
}

fuzz/fuzz_targets/roundtrip_miniscript_str.rs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,3 @@ fn main() {
3333
});
3434
}
3535
}
36-
37-
#[cfg(test)]
38-
mod tests {
39-
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
40-
let mut b = 0;
41-
for (idx, c) in hex.as_bytes().iter().enumerate() {
42-
b <<= 4;
43-
match *c {
44-
b'A'...b'F' => b |= c - b'A' + 10,
45-
b'a'...b'f' => b |= c - b'a' + 10,
46-
b'0'...b'9' => b |= c - b'0',
47-
_ => panic!("Bad hex"),
48-
}
49-
if (idx & 1) == 1 {
50-
out.push(b);
51-
b = 0;
52-
}
53-
}
54-
}
55-
56-
#[test]
57-
fn duplicate_crash() {
58-
let mut a = Vec::new();
59-
extend_vec_from_hex("00", &mut a);
60-
super::do_test(&a);
61-
}
62-
}

fuzz/fuzz_targets/roundtrip_policy.rs

Lines changed: 0 additions & 74 deletions
This file was deleted.

0 commit comments

Comments
 (0)