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 2, 2022
1 parent 07c2e53 commit e568272
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
@@ -1,7 +1,7 @@
use boa_engine::context::{Context, ContextBuilder};
use boa_engine::syntax::ast::visitor::{VisitWith, VisitorMut};
use boa_engine::syntax::ast::{Expression, StatementList};
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 @@ -69,3 +69,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 e568272

Please sign in to comment.