From d24c2fe48c4e72dc4e17cd23bc317c0c7236b808 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 18 Jan 2023 18:39:35 -0600 Subject: [PATCH] Detect components in `wasmtime compile` more robustly (#5592) The binary version of component is going to change over time so use a more robust method than checking for a fixed 8 bytes. --- Cargo.lock | 1 + Cargo.toml | 1 + src/commands/compile.rs | 10 +++++++++- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 2ae1764cff..9de93d645b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3491,6 +3491,7 @@ dependencies = [ "tempfile", "test-programs", "tokio", + "wasmparser", "wasmtime", "wasmtime-cache", "wasmtime-cli-flags", diff --git a/Cargo.toml b/Cargo.toml index 55e238f042..b2b44636ed 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,6 +41,7 @@ listenfd = "1.0.0" wat = { workspace = true } serde = "1.0.94" serde_json = "1.0.26" +wasmparser = { workspace = true } [target.'cfg(unix)'.dependencies] rustix = { workspace = true, features = ["mm", "param"] } diff --git a/src/commands/compile.rs b/src/commands/compile.rs index e7562447a5..2ee93eb033 100644 --- a/src/commands/compile.rs +++ b/src/commands/compile.rs @@ -90,7 +90,15 @@ impl CompileCommand { // bytes with the current component model proposal. #[cfg(feature = "component-model")] { - if input.starts_with(b"\0asm\x0a\0\x01\0") { + if let Ok(wasmparser::Chunk::Parsed { + payload: + wasmparser::Payload::Version { + encoding: wasmparser::Encoding::Component, + .. + }, + .. + }) = wasmparser::Parser::new(0).parse(&input, true) + { fs::write(output, engine.precompile_component(&input)?)?; return Ok(()); }