Skip to content

Commit

Permalink
improved argument handling
Browse files Browse the repository at this point in the history
  • Loading branch information
matt24smith committed Jan 11, 2023
1 parent 076166c commit 0b93211
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 95 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ documentation = "https://docs.rs/ecfuzz/"
license = "MIT"
readme = "./readme.md"
name = "ecfuzz"
version = "0.1.2"
version = "0.1.3"
edition = "2021"

[[bin]]
Expand Down
2 changes: 1 addition & 1 deletion examples/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@


int main(int argc, char** argv) {
//fprintf(stdout, "input was: %s %s %s %s %s %s\n", argv[1], argv[2], argv[3], argv[4], argv[5], argv[6]);
unsigned int n1 ;
sscanf(argv[1], "%u", &n1);
unsigned int n2 ;
Expand All @@ -11,6 +12,5 @@ int main(int argc, char** argv) {
sscanf(argv[3], "%u", &n3);

insert_name(n1, n2, n3, argv[4], argv[5], argv[6]);

return 0;
}
12 changes: 6 additions & 6 deletions examples/example_custom_fuzzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use ecfuzz::execute::{
check_report_coverage, count_branch_total, exec_target_args, index_target_report, Config, Exec,
};
use ecfuzz::mutator::Mutation;
use ecfuzz::mutator::SeededMutation;

#[repr(C)]
#[derive(Clone)]
Expand Down Expand Up @@ -101,7 +100,7 @@ impl MyTargetInput {
impl MyFuzzEngine {
pub fn new() -> Self {
MyFuzzEngine {
mutation_engine: <Mutation as SeededMutation>::new(None, [].to_vec()),
mutation_engine: Mutation::new(None),
firstname_seeds: load_corpus(&PathBuf::from("examples/firstname.dict")),
lastname_seeds: load_corpus(&PathBuf::from("examples/lastname.dict")),
data: MyTargetInput {
Expand Down Expand Up @@ -214,6 +213,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
cfg.iter_check = 8;
cfg.target_path = PathBuf::from("./examples/example.c");
cfg.iterations = 10_000;
cfg.objects = vec![PathBuf::from("./a.out")];

// compile target with instrumentation
Exec::initialize(&cfg)?;
Expand All @@ -226,9 +226,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
num1: 0,
num2: 0,
num3: 0,
str1: CString::from_vec_with_nul(b"\0".to_vec())?,
str2: CString::from_vec_with_nul(b"\0".to_vec())?,
str3: CString::from_vec_with_nul(b"\0".to_vec())?,
str1: CString::from_vec_with_nul(b"\0".to_vec()).unwrap(),
str2: CString::from_vec_with_nul(b"\0".to_vec()).unwrap(),
str3: CString::from_vec_with_nul(b"\0".to_vec()).unwrap(),
};
cov_corpus.add(seed.serialize(HashSet::new()));

Expand All @@ -249,7 +249,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

// run the program with mutated inputs, log crashes to crash corpus
let args = engine.get_target_args();
let crashed = exec_target_args(rawprof, &args)?;
let crashed = exec_target_args(rawprof, &args).unwrap();
if crashed {
crash_corpus
.inputs
Expand Down
2 changes: 1 addition & 1 deletion fuzz_target.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void do_comparison(char* data) {

int main() {
char str1[256];
scanf_s("%255[^\n]s", str1);
scanf("%255[^\n]s", str1);
do_comparison(str1);
return 0;
}
13 changes: 9 additions & 4 deletions src/corpus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ pub struct CorpusInput {
pub data: Vec<u8>,
pub coverage: HashSet<u64>,
}

impl std::fmt::Debug for CorpusInput {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("\n CorpusInput")
.field("data", &String::from_utf8_lossy(&self.data))
let mut maxlen = 64;
if &self.data.len() < &64 {
maxlen = self.data.len();
}
f.debug_struct("\n CorpusInput: ")
.field("data", &String::from_utf8_lossy(&self.data[0..maxlen]))
.field("coverage", &self.coverage)
.finish()
}
Expand Down Expand Up @@ -67,7 +72,7 @@ impl Corpus {
/// all corpus entries with branch coverage that is a
/// subset of the newest coverage will be pruned
pub fn add_and_distill_corpus(&mut self, new_input: CorpusInput) {
println!("new branch hit! updating inputs... {}", self);
println!("new code coverage hit! updating inputs... {}", self);
let diff: Vec<u64> = new_input
.coverage
.difference(&self.total_coverage)
Expand Down Expand Up @@ -122,7 +127,7 @@ impl Corpus {

/// load a corpus of inputs from a single file, separated by newlines
pub fn load_corpus(corpus_path: &PathBuf) -> Vec<Vec<u8>> {
let f: Vec<u8> = read(corpus_path).expect("reading file");
let f: Vec<u8> = read(corpus_path).expect("couldn't find corpus path!");
let s = f
.split(|x| x == &b'\n')
.map(|x| x.to_vec())
Expand Down
Loading

0 comments on commit 0b93211

Please sign in to comment.