From f8e35d52f3f8f49f5d83fc60a43380c3ade3f1f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Fern=C3=A1ndez=20L=C3=B3pez?= Date: Tue, 3 Oct 2023 11:28:36 +0200 Subject: [PATCH] chore: better identify errors when loading core modules or components --- Cargo.lock | 1 + Cargo.toml | 1 + crates/worker/Cargo.toml | 1 + crates/worker/src/errors.rs | 16 ++++++++++++++-- crates/worker/src/lib.rs | 23 ++++++++++++++++++----- 5 files changed, 35 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 87bbc95..d3994b8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4216,6 +4216,7 @@ dependencies = [ "tokio", "toml 0.7.8", "wasi-common", + "wasmparser 0.113.2", "wasmtime", "wasmtime-wasi", "wasmtime-wasi-nn", diff --git a/Cargo.toml b/Cargo.toml index e308c7d..b8a8f52 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -81,6 +81,7 @@ serde = { version = "1.0", features = ["derive"] } serde_json = "1.0.85" tokio = "1.28" toml = "0.7.0" +wasmparser = "0.113.2" wws-config = { path = "./crates/config" } wws-runtimes = { path = "./crates/runtimes" } wws-data-kv = { path = "./crates/data-kv" } diff --git a/crates/worker/Cargo.toml b/crates/worker/Cargo.toml index 6bade55..23aec7b 100644 --- a/crates/worker/Cargo.toml +++ b/crates/worker/Cargo.toml @@ -16,6 +16,7 @@ serde = { workspace = true } serde_json = { workspace = true } tokio = { workspace = true } toml = { workspace = true } +wasmparser = { workspace = true } wasmtime = { workspace = true } wasmtime-wasi = { workspace = true } wasmtime-wasi-nn = { workspace = true } diff --git a/crates/worker/src/errors.rs b/crates/worker/src/errors.rs index 83d1afc..7a8a89d 100644 --- a/crates/worker/src/errors.rs +++ b/crates/worker/src/errors.rs @@ -7,7 +7,13 @@ pub type Result = std::result::Result; #[derive(Debug)] pub enum WorkerError { - BadWasmModuleOrComponent, + BadWasmCoreModule { + error: String, + }, + BadWasmComponent { + error: String, + }, + BadWasmCoreModuleOrComponent, CannotLoadConfig, CannotParseConfig { path: PathBuf, @@ -37,7 +43,13 @@ impl From for WorkerError { impl std::fmt::Display for WorkerError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - WorkerError::BadWasmModuleOrComponent => write!(f, "Bad Wasm module or component"), + WorkerError::BadWasmCoreModule { error } => { + write!(f, "Bad Wasm core module: {}", error) + } + WorkerError::BadWasmComponent { error } => write!(f, "Bad Wasm component: {}", error), + WorkerError::BadWasmCoreModuleOrComponent => { + write!(f, "Bad Wasm core module or component") + } WorkerError::CannotLoadConfig => write!(f, "Could not load configuration"), WorkerError::CannotParseConfig { path, error } => write!( f, diff --git a/crates/worker/src/lib.rs b/crates/worker/src/lib.rs index 3e45c78..7a4f880 100644 --- a/crates/worker/src/lib.rs +++ b/crates/worker/src/lib.rs @@ -130,12 +130,25 @@ impl Worker { })?; let runtime = init_runtime(project_root, path, project_config)?; let bytes = runtime.module_bytes()?; - let module_or_component = if let Ok(module) = Module::from_binary(&engine, &bytes) { - Ok(ModuleOrComponent::Module(module)) - } else if let Ok(component) = Component::from_binary(&engine, &bytes) { - Ok(ModuleOrComponent::Component(component)) + + let module_or_component = if wasmparser::Parser::is_core_wasm(&bytes) { + Ok(ModuleOrComponent::Module( + Module::from_binary(&engine, &bytes).map_err(|err| { + errors::WorkerError::BadWasmCoreModule { + error: format!("{:?}", err), + } + })?, + )) + } else if wasmparser::Parser::is_component(&bytes) { + Ok(ModuleOrComponent::Component( + Component::from_binary(&engine, &bytes).map_err(|err| { + errors::WorkerError::BadWasmComponent { + error: format!("{:?}", err), + } + })?, + )) } else { - Err(errors::WorkerError::BadWasmModuleOrComponent) + Err(errors::WorkerError::BadWasmCoreModuleOrComponent) }?; // Prepare the environment if required