A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Add module introspection and serialization API coverage to the public API fuzzer (#13452)
Nick Fitzgeraldc88edea65ff6
diff --git a/crates/fuzzing/src/generators/api.rs b/crates/fuzzing/src/generators/api.rsindex 65c6bbc8a3..f3ac9a728e 100644--- a/crates/fuzzing/src/generators/api.rs+++ b/crates/fuzzing/src/generators/api.rs@@ -97,6 +97,12 @@ struct Swarm {tag_eq: bool,get_tag_export: bool,tag_drop: bool,+ module_imports: bool,+ module_exports: bool,+ module_get_export: bool,+ module_name: bool,+ module_validate: bool,+ module_serialize_deserialize: bool,}/// A call to one of Wasmtime's public APIs.@@ -307,6 +313,26 @@ pub enum ApiCall {TagDrop {id: usize,},+ ModuleImports {+ module: usize,+ },+ ModuleExports {+ module: usize,+ },+ ModuleGetExport {+ module: usize,+ name: String,+ },+ ModuleName {+ module: usize,+ },+ ModuleValidate {+ wasm: Vec<u8>,+ },+ ModuleSerializeDeserialize {+ src_id: usize,+ dst_id: usize,+ },}use ApiCall::*;@@ -921,6 +947,55 @@ impl<'a> Arbitrary<'a> for ApiCalls {Ok(TagDrop { id })});}+ if swarm.module_imports && !scope.modules.is_empty() {+ choices.push(|input, scope| {+ let modules: Vec<_> = scope.modules.iter().collect();+ let module = **input.choose(&modules)?;+ Ok(ModuleImports { module })+ });+ }+ if swarm.module_exports && !scope.modules.is_empty() {+ choices.push(|input, scope| {+ let modules: Vec<_> = scope.modules.iter().collect();+ let module = **input.choose(&modules)?;+ Ok(ModuleExports { module })+ });+ }+ if swarm.module_get_export && !scope.modules.is_empty() {+ choices.push(|input, scope| {+ let modules: Vec<_> = scope.modules.iter().collect();+ let module = **input.choose(&modules)?;+ let name = String::arbitrary(input)?;+ Ok(ModuleGetExport { module, name })+ });+ }+ if swarm.module_name && !scope.modules.is_empty() {+ choices.push(|input, scope| {+ let modules: Vec<_> = scope.modules.iter().collect();+ let module = **input.choose(&modules)?;+ Ok(ModuleName { module })+ });+ }+ if swarm.module_validate {+ choices.push(|input, scope| {+ let use_valid_wasm = bool::arbitrary(input)?;+ let wasm = if use_valid_wasm {+ scope.config.generate(input, Some(1000))?.to_bytes()+ } else {+ Vec::<u8>::arbitrary(input)?+ };+ Ok(ModuleValidate { wasm })+ });+ }+ if swarm.module_serialize_deserialize && !scope.modules.is_empty() {+ choices.push(|input, scope| {+ let modules: Vec<_> = scope.modules.iter().collect();+ let src_id = **input.choose(&modules)?;+ let dst_id = scope.next_id();+ scope.modules.insert(dst_id);+ Ok(ModuleSerializeDeserialize { src_id, dst_id })+ });+ }if choices.is_empty() {break;diff --git a/crates/fuzzing/src/oracles/api.rs b/crates/fuzzing/src/oracles/api.rsindex 44ffca3b47..672802ae8b 100644--- a/crates/fuzzing/src/oracles/api.rs+++ b/crates/fuzzing/src/oracles/api.rs@@ -7,8 +7,11 @@ use wasmtime::*;pub fn make_api_calls(api: ApiCalls) {// The generator always starts with StoreNew; use its config to build the// shared engine that all stores in this sequence will use.- let engine = match api.calls.first() {- Some(ApiCall::StoreNew { config, .. }) => Engine::new(&config.to_wasmtime()).unwrap(),+ let (engine, gc_enabled) = match api.calls.first() {+ Some(ApiCall::StoreNew { config, .. }) => (+ Engine::new(&config.to_wasmtime()).unwrap(),+ config.module_config.config.gc_enabled,+ ),_ => return,};@@ -691,6 +694,9 @@ pub fn make_api_calls(api: ApiCalls) {Some(s) => s,None => continue,};+ if !gc_enabled {+ continue;+ }match Tag::new(&mut *st, &tt) {Ok(t) => {tags.insert(id, (t, store));@@ -756,6 +762,65 @@ pub fn make_api_calls(api: ApiCalls) {log::trace!("dropping tag {id}");tags.remove(&id);}++ ApiCall::ModuleImports { module } => {+ log::trace!("iterating imports of module {module}");+ let m = match modules.get(&module) {+ Some(m) => m,+ None => continue,+ };+ for _ in m.imports() {}+ }++ ApiCall::ModuleExports { module } => {+ log::trace!("iterating exports of module {module}");+ let m = match modules.get(&module) {+ Some(m) => m,+ None => continue,+ };+ for _ in m.exports() {}+ }++ ApiCall::ModuleGetExport { module, ref name } => {+ log::trace!("getting export {name:?} of module {module}");+ let m = match modules.get(&module) {+ Some(m) => m,+ None => continue,+ };+ let _ = m.get_export(name);+ }++ ApiCall::ModuleName { module } => {+ log::trace!("getting name of module {module}");+ let m = match modules.get(&module) {+ Some(m) => m,+ None => continue,+ };+ let _ = m.name();+ }++ ApiCall::ModuleValidate { ref wasm } => {+ log::trace!("validating {} bytes of wasm", wasm.len());+ let _ = Module::validate(&engine, wasm);+ }++ ApiCall::ModuleSerializeDeserialize { src_id, dst_id } => {+ log::trace!("serializing module {src_id} and deserializing as {dst_id}");+ let src = match modules.get(&src_id) {+ Some(m) => m,+ None => continue,+ };+ let bytes = match src.serialize() {+ Ok(b) => b,+ Err(_) => continue,+ };+ match unsafe { Module::deserialize(&engine, &bytes) } {+ Ok(m) => {+ modules.insert(dst_id, m);+ }+ Err(_) => continue,+ }+ }}}}