From 9bb9761980bbbd001a7232c274a19321a61b6105 Mon Sep 17 00:00:00 2001 From: Matt Leon Date: Wed, 20 May 2026 11:18:36 -0400 Subject: [PATCH 1/3] Add test for load failures due to mismatching imports Also unifies the error messages across engines. Signed-off-by: Matt Leon --- src/wasmedge/wasmedge.cc | 4 ++-- src/wasmtime/wasmtime.cc | 2 +- test/BUILD | 1 + test/runtime_test.cc | 16 +++++++++++++++ test/test_data/BUILD | 5 +++++ test/test_data/invalid_import_type.rs | 29 +++++++++++++++++++++++++++ 6 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 test/test_data/invalid_import_type.rs diff --git a/src/wasmedge/wasmedge.cc b/src/wasmedge/wasmedge.cc index 3359e1b27..69b0e1d9d 100644 --- a/src/wasmedge/wasmedge.cc +++ b/src/wasmedge/wasmedge.cc @@ -361,7 +361,7 @@ bool WasmEdge::link(std::string_view /*debug_name*/) { res = WasmEdge_ExecutorRegisterImport(executor_.get(), store_.get(), it.second->cxt_); if (!WasmEdge_ResultOK(res)) { fail(FailState::UnableToInitializeCode, - std::string("Failed to link Wasm module due to import: ") + it.first); + std::string("Failed to load Wasm module due to import: ") + it.first); return false; } } @@ -370,7 +370,7 @@ bool WasmEdge::link(std::string_view /*debug_name*/) { res = WasmEdge_ExecutorInstantiate(executor_.get(), &mod, store_.get(), ast_module_.get()); if (!WasmEdge_ResultOK(res)) { fail(FailState::UnableToInitializeCode, - std::string("Failed to link Wasm module: ") + std::string(WasmEdge_ResultGetMessage(res))); + std::string("Failed to load Wasm module: ") + std::string(WasmEdge_ResultGetMessage(res))); return false; } // Get the function and memory exports. diff --git a/src/wasmtime/wasmtime.cc b/src/wasmtime/wasmtime.cc index 1961542c0..6d2dbd8b0 100644 --- a/src/wasmtime/wasmtime.cc +++ b/src/wasmtime/wasmtime.cc @@ -254,7 +254,7 @@ bool Wasmtime::link(std::string_view /*debug_name*/) { TrapResult instance = linker_.instantiate(store_->context(), *module_); if (!instance) { fail(FailState::UnableToInitializeCode, - "Failed to create new Wasm instance: " + instance.err().message()); + "Failed to load Wasm module: " + instance.err().message()); return false; } instance_.emplace(instance.ok()); diff --git a/test/BUILD b/test/BUILD index 1b45b1318..e2442cf74 100644 --- a/test/BUILD +++ b/test/BUILD @@ -72,6 +72,7 @@ cc_test( data = [ "//test/test_data:callback.wasm", "//test/test_data:clock.wasm", + "//test/test_data:invalid_import_type.wasm", "//test/test_data:resource_limits.wasm", "//test/test_data:trap.wasm", ], diff --git a/test/runtime_test.cc b/test/runtime_test.cc index a8123806d..e4ccae247 100644 --- a/test/runtime_test.cc +++ b/test/runtime_test.cc @@ -229,6 +229,22 @@ TEST_P(TestVm, SerializeAndDeserializeRoundTripWorks) { EXPECT_LT(precompiled * 2, unprecompiled); } +TEST_P(TestVm, ImportWithMismatchingTypeFailsLink) { + auto source = readTestWasmFile("invalid_import_type.wasm"); + ASSERT_FALSE(source.empty()); + auto wasm = TestWasm(std::move(vm_)); + auto *host = dynamic_cast(wasm.wasm_vm()->integration().get()); + + ASSERT_TRUE(wasm.load(source, false)); + ASSERT_FALSE(wasm.initialize()); + + EXPECT_TRUE(host->isErrorLogged("Failed to load Wasm module")); + // TODO: WasmEdge logs the failing import to stderr, but not to the Proxy-Wasm integration logger. + if (engine_ != "wasmedge") { + EXPECT_TRUE(host->isErrorLogged("proxy_log")); + } +} + class TestCounterContext : public TestContext { public: TestCounterContext(WasmBase *wasm) : TestContext(wasm) {} diff --git a/test/test_data/BUILD b/test/test_data/BUILD index 28f723c34..51455aae4 100644 --- a/test/test_data/BUILD +++ b/test/test_data/BUILD @@ -47,6 +47,11 @@ wasm_rust_binary( srcs = ["bad_malloc.rs"], ) +wasm_rust_binary( + name = "invalid_import_type.wasm", + srcs = ["invalid_import_type.rs"], +) + wasm_rust_binary( name = "callback.wasm", srcs = ["callback.rs"], diff --git a/test/test_data/invalid_import_type.rs b/test/test_data/invalid_import_type.rs new file mode 100644 index 000000000..9684c38f0 --- /dev/null +++ b/test/test_data/invalid_import_type.rs @@ -0,0 +1,29 @@ +// Copyright 2026 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +extern "C" { + // Wrong type! + fn proxy_log(level: u32, message_data: *const u8) -> u32; +} + +#[no_mangle] +pub extern "C" fn proxy_abi_version_0_2_0() {} + +#[no_mangle] +pub extern "C" fn proxy_on_memory_allocate(size: usize) -> *mut u8 { + unsafe { + proxy_log(0, "my_message".as_ptr()); + } + std::ptr::null_mut() +} From e3d27eb250b14861870aa7192300b976c449a37f Mon Sep 17 00:00:00 2001 From: Matt Leon Date: Wed, 20 May 2026 13:48:50 -0400 Subject: [PATCH 2/3] Rename invalid_import_type to incorrect_import_type and change import from proxy_log to proxy_done Signed-off-by: Matt Leon --- test/BUILD | 2 +- test/runtime_test.cc | 5 +++-- test/test_data/BUILD | 4 ++-- .../{invalid_import_type.rs => incorrect_import_type.rs} | 4 ++-- 4 files changed, 8 insertions(+), 7 deletions(-) rename test/test_data/{invalid_import_type.rs => incorrect_import_type.rs} (88%) diff --git a/test/BUILD b/test/BUILD index e2442cf74..57afb906e 100644 --- a/test/BUILD +++ b/test/BUILD @@ -72,7 +72,7 @@ cc_test( data = [ "//test/test_data:callback.wasm", "//test/test_data:clock.wasm", - "//test/test_data:invalid_import_type.wasm", + "//test/test_data:incorrect_import_type.wasm", "//test/test_data:resource_limits.wasm", "//test/test_data:trap.wasm", ], diff --git a/test/runtime_test.cc b/test/runtime_test.cc index e4ccae247..af0322419 100644 --- a/test/runtime_test.cc +++ b/test/runtime_test.cc @@ -230,7 +230,7 @@ TEST_P(TestVm, SerializeAndDeserializeRoundTripWorks) { } TEST_P(TestVm, ImportWithMismatchingTypeFailsLink) { - auto source = readTestWasmFile("invalid_import_type.wasm"); + auto source = readTestWasmFile("incorrect_import_type.wasm"); ASSERT_FALSE(source.empty()); auto wasm = TestWasm(std::move(vm_)); auto *host = dynamic_cast(wasm.wasm_vm()->integration().get()); @@ -241,7 +241,8 @@ TEST_P(TestVm, ImportWithMismatchingTypeFailsLink) { EXPECT_TRUE(host->isErrorLogged("Failed to load Wasm module")); // TODO: WasmEdge logs the failing import to stderr, but not to the Proxy-Wasm integration logger. if (engine_ != "wasmedge") { - EXPECT_TRUE(host->isErrorLogged("proxy_log")); + // The function that has the incorrect type + EXPECT_TRUE(host->isErrorLogged("proxy_done")); } } diff --git a/test/test_data/BUILD b/test/test_data/BUILD index 51455aae4..28ee875ef 100644 --- a/test/test_data/BUILD +++ b/test/test_data/BUILD @@ -48,8 +48,8 @@ wasm_rust_binary( ) wasm_rust_binary( - name = "invalid_import_type.wasm", - srcs = ["invalid_import_type.rs"], + name = "incorrect_import_type.wasm", + srcs = ["incorrect_import_type.rs"], ) wasm_rust_binary( diff --git a/test/test_data/invalid_import_type.rs b/test/test_data/incorrect_import_type.rs similarity index 88% rename from test/test_data/invalid_import_type.rs rename to test/test_data/incorrect_import_type.rs index 9684c38f0..9fb778b2c 100644 --- a/test/test_data/invalid_import_type.rs +++ b/test/test_data/incorrect_import_type.rs @@ -14,7 +14,7 @@ extern "C" { // Wrong type! - fn proxy_log(level: u32, message_data: *const u8) -> u32; + fn proxy_done(incorrect_arg: u32) -> u32; } #[no_mangle] @@ -23,7 +23,7 @@ pub extern "C" fn proxy_abi_version_0_2_0() {} #[no_mangle] pub extern "C" fn proxy_on_memory_allocate(size: usize) -> *mut u8 { unsafe { - proxy_log(0, "my_message".as_ptr()); + proxy_done(1234); } std::ptr::null_mut() } From 70dfc1e8c3cedb075800c4056cf7304559ca9a2c Mon Sep 17 00:00:00 2001 From: Matt Leon Date: Thu, 11 Jun 2026 11:07:37 -0400 Subject: [PATCH 3/3] Fix compilation warning in incorrect_import_type.rs Signed-off-by: Matt Leon --- test/test_data/incorrect_import_type.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_data/incorrect_import_type.rs b/test/test_data/incorrect_import_type.rs index 9fb778b2c..199ee4cac 100644 --- a/test/test_data/incorrect_import_type.rs +++ b/test/test_data/incorrect_import_type.rs @@ -21,7 +21,7 @@ extern "C" { pub extern "C" fn proxy_abi_version_0_2_0() {} #[no_mangle] -pub extern "C" fn proxy_on_memory_allocate(size: usize) -> *mut u8 { +pub extern "C" fn proxy_on_memory_allocate(_: usize) -> *mut u8 { unsafe { proxy_done(1234); }