Skip to content

Commit

Permalink
prettified fuzz source
Browse files Browse the repository at this point in the history
  • Loading branch information
addisoncrump committed Nov 6, 2022
1 parent 4e6e8af commit f01a63f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
24 changes: 23 additions & 1 deletion boa_engine/fuzz/fuzz_targets/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use boa_ast::{
Expression, StatementList,
};
use boa_engine::context::{Context, ContextBuilder};
use boa_interner::Sym;
use boa_interner::{Sym, ToInternedString};
use libfuzzer_sys::arbitrary;
use libfuzzer_sys::arbitrary::{Arbitrary, Unstructured};
use std::fmt::{Debug, Formatter};
Expand Down Expand Up @@ -71,3 +71,25 @@ impl Debug for FuzzData {
.finish_non_exhaustive()
}
}

pub struct FuzzSource {
pub context: Context,
pub source: String,
}

impl<'a> Arbitrary<'a> for FuzzSource {
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
let data = FuzzData::arbitrary(u)?;
let source = data.ast.to_interned_string(data.context.interner());
Ok(Self {
context: data.context,
source,
})
}
}

impl Debug for FuzzSource {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!("Fuzzed source:\n{}", self.source))
}
}
14 changes: 5 additions & 9 deletions boa_engine/fuzz/fuzz_targets/vm-implied.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@

mod common;

use crate::common::FuzzData;
use crate::common::FuzzSource;
use boa_engine::{JsResult, JsValue};
use boa_interner::ToInternedString;
use libfuzzer_sys::fuzz_target;

fn do_fuzz(mut data: FuzzData) -> JsResult<JsValue> {
// Convert back to source; we may not actually produce valid code, so we need to re-parse it.
let original = data.ast.to_interned_string(data.context.interner());

data.context.eval(&original).into()
fn do_fuzz(mut original: FuzzSource) -> JsResult<JsValue> {
original.context.eval(&original.source).into()
}

fuzz_target!(|data: FuzzData| {
let _ = do_fuzz(data);
fuzz_target!(|original: FuzzSource| {
let _ = do_fuzz(original);
});

0 comments on commit f01a63f

Please sign in to comment.