Skip to content

wavm: fix function export bug. #200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/proxy-wasm/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ std::shared_ptr<WasmHandleBase>
createWasm(std::string vm_key, std::string code, std::shared_ptr<PluginBase> plugin,
WasmHandleFactory factory, WasmHandleCloneFactory clone_factory, bool allow_precompiled);
// Get an existing ThreadLocal VM matching 'vm_id' or nullptr if there isn't one.
std::shared_ptr<WasmHandleBase> getThreadLocalWasm(std::string_view vm_id);
std::shared_ptr<WasmHandleBase> getThreadLocalWasm(std::string_view vm_key);

class PluginHandleBase : public std::enable_shared_from_this<PluginHandleBase> {
public:
Expand Down
4 changes: 4 additions & 0 deletions src/wavm/wavm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,10 @@ void getFunctionWavm(WasmVm *vm, std::string_view function_name,
return;
}
if (!checkFunctionType(f, inferStdFunctionType(function))) {
*function = nullptr;
wavm->fail(FailState::UnableToInitializeCode,
"Bad function signature for: " + std::string(function_name));
return;
}
*function = [wavm, f, function_name](ContextBase *context, Args... args) -> R {
WasmUntaggedValue values[] = {args...};
Expand Down Expand Up @@ -427,8 +429,10 @@ void getFunctionWavm(WasmVm *vm, std::string_view function_name,
return;
}
if (!checkFunctionType(f, inferStdFunctionType(function))) {
*function = nullptr;
wavm->fail(FailState::UnableToInitializeCode,
"Bad function signature for: " + std::string(function_name));
return;
}
*function = [wavm, f, function_name](ContextBase *context, Args... args) {
WasmUntaggedValue values[] = {args...};
Expand Down
29 changes: 29 additions & 0 deletions test/runtime_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,35 @@ TEST_P(TestVM, StraceLogLevel) {
EXPECT_NE(integration->trace_message_, "");
}

TEST_P(TestVM, BadExportFunction) {
auto source = readTestWasmFile("callback.wasm");
ASSERT_TRUE(vm_->load(source, {}, {}));

TestContext context;
vm_->registerCallback(
"env", "callback", &callback,
&ConvertFunctionWordToUint32<decltype(callback), callback>::convertFunctionWordToUint32);
vm_->registerCallback(
"env", "callback2", &callback2,
&ConvertFunctionWordToUint32<decltype(callback2), callback2>::convertFunctionWordToUint32);
ASSERT_TRUE(vm_->link(""));

WasmCallVoid<0> run;
vm_->getFunction("non-existent", &run);
EXPECT_TRUE(run == nullptr);

WasmCallWord<2> bad_signature_run;
vm_->getFunction("run", &bad_signature_run);
EXPECT_TRUE(bad_signature_run == nullptr);

vm_->getFunction("run", &run);
EXPECT_TRUE(run != nullptr);
for (auto i = 0; i < 100; i++) {
run(&context);
}
ASSERT_EQ(context.counter, 100);
}

TEST_P(TestVM, Callback) {
auto source = readTestWasmFile("callback.wasm");
ASSERT_TRUE(vm_->load(source, {}, {}));
Expand Down