From 958269be2375e65fcae913218eb116444dba6b40 Mon Sep 17 00:00:00 2001 From: Chris Fallin Date: Fri, 24 Feb 2023 22:07:35 -0800 Subject: [PATCH] add missing fuzzing.rs --- src/fuzzing.rs | 103 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 src/fuzzing.rs diff --git a/src/fuzzing.rs b/src/fuzzing.rs new file mode 100644 index 0000000..2bf20f3 --- /dev/null +++ b/src/fuzzing.rs @@ -0,0 +1,103 @@ +//! Fuzzing-specific utilities. + +use libfuzzer_sys::arbitrary; + +pub fn reject(bytes: &[u8]) -> bool { + let parser = wasmparser::Parser::new(0); + let mut has_start = false; + let mut has_global_set = false; + let mut num_globals = 0; + for payload in parser.parse_all(bytes) { + match payload.unwrap() { + wasmparser::Payload::CodeSectionEntry(body) => { + for op in body.get_operators_reader().unwrap() { + let op = op.unwrap(); + match op { + wasmparser::Operator::GlobalSet { .. } => { + has_global_set = true; + } + _ => {} + } + } + } + wasmparser::Payload::StartSection { .. } => { + has_start = true; + } + wasmparser::Payload::ExportSection(mut reader) => { + for _ in 0..reader.get_count() { + let e = reader.read().unwrap(); + match &e.kind { + &wasmparser::ExternalKind::Global => { + num_globals += 1; + } + _ => {} + } + } + } + _ => {} + } + } + + if !has_start || !has_global_set || num_globals < 1 { + return true; + } + + false +} + +#[derive(Debug)] +pub struct Config; + +impl<'a> arbitrary::Arbitrary<'a> for Config { + fn arbitrary(_u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result { + Ok(Config) + } +} + +impl wasm_smith::Config for Config { + fn min_funcs(&self) -> usize { + 1 + } + fn max_funcs(&self) -> usize { + 1 + } + fn min_memories(&self) -> u32 { + 1 + } + fn max_memories(&self) -> usize { + 1 + } + fn min_globals(&self) -> usize { + 10 + } + fn max_globals(&self) -> usize { + 10 + } + fn min_tables(&self) -> u32 { + 0 + } + fn max_tables(&self) -> usize { + 0 + } + fn min_imports(&self) -> usize { + 0 + } + fn max_imports(&self) -> usize { + 0 + } + fn min_exports(&self) -> usize { + 12 + } + fn max_exports(&self) -> usize { + 12 + } + fn allow_start_export(&self) -> bool { + true + } + fn canonicalize_nans(&self) -> bool { + true + } + fn max_memory_pages(&self, _is_64: bool) -> u64 { + 1 + } +}