Skip to content

Commit

Permalink
prefer interner over context
Browse files Browse the repository at this point in the history
  • Loading branch information
addisoncrump committed Nov 6, 2022
1 parent a5350f9 commit 0f4223c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
11 changes: 5 additions & 6 deletions fuzz/fuzz_targets/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use boa_ast::{
visitor::{VisitWith, VisitorMut},
Expression, StatementList,
};
use boa_engine::context::Context;
use boa_interner::Sym;
use boa_interner::{Interner, Sym};
use libfuzzer_sys::arbitrary;
use libfuzzer_sys::arbitrary::{Arbitrary, Unstructured};
use std::fmt::{Debug, Formatter};
Expand All @@ -12,16 +11,16 @@ use std::ops::ControlFlow;
/// Context for performing fuzzing. This structure contains both the generated AST as well as the
/// context used to resolve the symbols therein.
pub struct FuzzData {
pub context: Context,
pub interner: Interner,
pub ast: StatementList,
}

impl<'a> Arbitrary<'a> for FuzzData {
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
let mut context = Context::default();
let mut interner = Interner::with_capacity(8);
let mut syms_available = Vec::with_capacity(8);
for c in 'a'..='h' {
syms_available.push(context.interner_mut().get_or_intern(&*String::from(c)));
syms_available.push(interner.get_or_intern(&*String::from(c)));
}

let mut ast = StatementList::arbitrary(u)?;
Expand Down Expand Up @@ -61,7 +60,7 @@ impl<'a> Arbitrary<'a> for FuzzData {
if let ControlFlow::Break(e) = replacer.visit_statement_list_mut(&mut ast) {
Err(e)
} else {
Ok(Self { context, ast })
Ok(Self { interner, ast })
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions fuzz/fuzz_targets/parser-idempotency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ use std::io::Cursor;
///
/// See [README.md](../README.md) for details on the design of this fuzzer.
fn do_fuzz(mut data: FuzzData) -> Result<(), Box<dyn Error>> {
let original = data.ast.to_interned_string(data.context.interner());
let original = data.ast.to_interned_string(&data.interner);

let mut parser = Parser::new(Cursor::new(&original));

let before = data.context.interner().len();
let before = data.interner.len();
// For a variety of reasons, we may not actually produce valid code here (e.g., nameless function).
// Fail fast and only make the next checks if we were valid.
if let Ok(first) = parser.parse_all(data.context.interner_mut()) {
let after_first = data.context.interner().len();
let first_interned = first.to_interned_string(data.context.interner());
if let Ok(first) = parser.parse_all(&mut data.interner) {
let after_first = data.interner.len();
let first_interned = first.to_interned_string(&data.interner);

assert_eq!(
before,
Expand All @@ -38,10 +38,10 @@ fn do_fuzz(mut data: FuzzData) -> Result<(), Box<dyn Error>> {

// Now, we most assuredly should produce valid code. It has already gone through a first pass.
let second = parser
.parse_all(data.context.interner_mut())
.parse_all(&mut data.interner)
.expect("Could not parse the first-pass interned copy.");
let second_interned = second.to_interned_string(data.context.interner());
let after_second = data.context.interner().len();
let second_interned = second.to_interned_string(&data.interner);
let after_second = data.interner.len();
assert_eq!(
after_first,
after_second,
Expand Down

0 comments on commit 0f4223c

Please sign in to comment.