Skip to content

Commit 34d1a5b

Browse files
committed
Add test for load failures due to mismatching imports
Also unifies the error messages across engines. Signed-off-by: Matt Leon <mattleon@google.com>
1 parent a7cfd87 commit 34d1a5b

6 files changed

Lines changed: 54 additions & 3 deletions

File tree

src/wasmedge/wasmedge.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ bool WasmEdge::link(std::string_view /*debug_name*/) {
358358
res = WasmEdge_ExecutorRegisterImport(executor_.get(), store_.get(), it.second->cxt_);
359359
if (!WasmEdge_ResultOK(res)) {
360360
fail(FailState::UnableToInitializeCode,
361-
std::string("Failed to link Wasm module due to import: ") + it.first);
361+
std::string("Failed to load Wasm module due to import: ") + it.first);
362362
return false;
363363
}
364364
}
@@ -367,7 +367,7 @@ bool WasmEdge::link(std::string_view /*debug_name*/) {
367367
res = WasmEdge_ExecutorInstantiate(executor_.get(), &mod, store_.get(), ast_module_.get());
368368
if (!WasmEdge_ResultOK(res)) {
369369
fail(FailState::UnableToInitializeCode,
370-
std::string("Failed to link Wasm module: ") + std::string(WasmEdge_ResultGetMessage(res)));
370+
std::string("Failed to load Wasm module: ") + std::string(WasmEdge_ResultGetMessage(res)));
371371
return false;
372372
}
373373
// Get the function and memory exports.

src/wasmtime/wasmtime.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ bool Wasmtime::link(std::string_view /*debug_name*/) {
211211
TrapResult<Instance> instance = linker_.instantiate(store_->context(), *module_);
212212
if (!instance) {
213213
fail(FailState::UnableToInitializeCode,
214-
"Failed to create new Wasm instance: " + instance.err().message());
214+
"Failed to load Wasm module: " + instance.err().message());
215215
return false;
216216
}
217217
instance_.emplace(instance.ok());

test/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ cc_test(
7171
srcs = ["runtime_test.cc"],
7272
data = [
7373
"//test/test_data:callback.wasm",
74+
"//test/test_data:invalid_import_type.wasm",
7475
"//test/test_data:clock.wasm",
7576
"//test/test_data:resource_limits.wasm",
7677
"//test/test_data:trap.wasm",

test/runtime_test.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,22 @@ TEST_P(TestVm, Trap2) {
190190
}
191191
}
192192

193+
TEST_P(TestVm, ImportWithMismatchingTypeFailsLink) {
194+
auto source = readTestWasmFile("invalid_import_type.wasm");
195+
ASSERT_FALSE(source.empty());
196+
auto wasm = TestWasm(std::move(vm_));
197+
auto *host = dynamic_cast<TestIntegration *>(wasm.wasm_vm()->integration().get());
198+
199+
ASSERT_TRUE(wasm.load(source, false));
200+
ASSERT_FALSE(wasm.initialize());
201+
202+
EXPECT_TRUE(host->isErrorLogged("Failed to load Wasm module"));
203+
// TODO: WasmEdge logs the failing import to stderr, but not to the Proxy-Wasm integration logger.
204+
if (engine_ != "wasmedge") {
205+
EXPECT_TRUE(host->isErrorLogged("proxy_log"));
206+
}
207+
}
208+
193209
class TestCounterContext : public TestContext {
194210
public:
195211
TestCounterContext(WasmBase *wasm) : TestContext(wasm) {}

test/test_data/BUILD

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ wasm_rust_binary(
4747
srcs = ["bad_malloc.rs"],
4848
)
4949

50+
wasm_rust_binary(
51+
name = "invalid_import_type.wasm",
52+
srcs = ["invalid_import_type.rs"],
53+
)
54+
5055
wasm_rust_binary(
5156
name = "callback.wasm",
5257
srcs = ["callback.rs"],
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2026 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
extern "C" {
16+
// Wrong type!
17+
fn proxy_log(level: u32, message_data: *const u8) -> u32;
18+
}
19+
20+
#[no_mangle]
21+
pub extern "C" fn proxy_abi_version_0_2_0() {}
22+
23+
#[no_mangle]
24+
pub extern "C" fn proxy_on_memory_allocate(size: usize) -> *mut u8 {
25+
unsafe {
26+
proxy_log(0, "my_message".as_ptr());
27+
}
28+
std::ptr::null_mut()
29+
}

0 commit comments

Comments
 (0)