Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Added cargo-fuzz tests for Rlp crate #6145

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions util/rlp/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ cargo-fuzz = true
nightly = []

[dependencies]
ethcore-bigint = { path = "../../bigint" }
rand = "0.3"

[build-dependencies]
Expand All @@ -39,3 +40,6 @@ path = "fuzz_targets/append_raw.rs"
name = "untrusted_data"
path = "fuzz_targets/untrusted_data.rs"

[[bin]]
name = "gen_untrusted_corpus"
path = "gen_untrusted_corpus.rs"
15 changes: 11 additions & 4 deletions util/rlp/fuzz/fuzz_targets/untrusted_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@ extern crate rlp;

use rlp::UntrustedRlp;

fn iter_recursive(rlp: UntrustedRlp) {
for x in rlp.iter() {
// Internally calls BasicDecoder::payload_info(self.bytes)
// Which will hit the code-path we're interested in fuzzing
let _ = x.data();
iter_recursive(x);
}
}

fuzz_target!(|data: &[u8]| {
// Create UntrustedRlp to build BasicDecoder
let urlp = UntrustedRlp::new(data);

// Internally calls BasicDecoder::payload_info(self.bytes)
// Which will hit the code-path we're interested in fuzzing
let _ = urlp.data();
// Attempt to recursively iterate over entire RLP structure
iter_recursive(urlp);
});
94 changes: 94 additions & 0 deletions util/rlp/fuzz/gen_untrusted_corpus.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
extern crate rlp;
extern crate ethcore_bigint;

use std::env;
use std::error::Error;
use std::io::prelude::*;
use std::path::Path;
use std::fs::File;
use rlp::RlpStream;
use ethcore_bigint::prelude::{U128, U256, H64, H128, H160, H256, H512, H520, H2048};

fn create_file(path: &Path) -> File {
match File::create(&path) {
Err(why) => panic!("couldn't create {}: {}",
path.display(),
why.description()),
Ok(file) => file
}
}

fn write_to_file(f: &File, rlp: RlpStream) {
let mut g = f.clone();
match g.write_all(rlp.as_raw()) {
Err(why) => {
panic!("couldn't write to file: {}", why.description())
},
Ok(_) => println!("successfully wrote to file")
}

}

fn create_uint_stream() {
// Create RLP Stream to encode values
let mut rlp = RlpStream::new();
// U8, U16, U32, U64, U128, U256 bytes
let u8b = 8 as u8;
let u16b = 16 as u16;
let u32b = 32 as u32;
let u64b = 64 as u64;
let u128b = U128::from(128);
let u256b = U256::from(256);

rlp.append(&u8b);
rlp.append(&u16b);
rlp.append(&u32b);
rlp.append(&u64b);
rlp.append(&u128b);
rlp.append(&u256b);

// Read in base path to fuzzing corpus directory from `RLPCORPUS` environment var
let corp = env::var("RLPCORPUS").unwrap();
let fp = format!("{}{}", corp, "/untrusted_data/well-formed-list-uint");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should use PathBuf and appending methods for this

let path = Path::new(&fp);

// Write RLP Stream to corpus file
let f = create_file(&path);
write_to_file(&f, rlp);
}

fn create_hash_stream() {
// Create RLP Stream to encode values
let mut rlp = RlpStream::new();
// H64, H128, H160, H256, H512, H520, H2048 list
let h64 = H64::random();
let h128 = H128::random();
let h160 = H160::random();
let h256 = H256::random();
let h512 = H512::random();
let h520 = H520::random();
let h2048 = H2048::random();


rlp.append(&h64);
rlp.append(&h128);
rlp.append(&h160);
rlp.append(&h256);
rlp.append(&h512);
rlp.append(&h520);
rlp.append(&h2048);

// Read in base path to fuzzing corpus directory from `RLPCORPUS` environment var
let corp = env::var("RLPCORPUS").unwrap();
let fp = format!("{}{}", corp, "/untrusted_data/well-formed-list-hash");
let path = Path::new(&fp);

// Write RLP Stream to corpus file
let f = create_file(&path);
write_to_file(&f, rlp);
}

fn main() {
create_uint_stream();
create_hash_stream();
}