Skip to content

Add terminate execution API. #268

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 12 commits into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from 11 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: 2 additions & 0 deletions include/proxy-wasm/null_vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ struct NullVm : public WasmVm {
FOR_ALL_WASM_VM_IMPORTS(_REGISTER_CALLBACK)
#undef _REGISTER_CALLBACK

void terminate() override {}

std::string plugin_name_;
std::unique_ptr<NullVmPlugin> plugin_;
};
Expand Down
5 changes: 5 additions & 0 deletions include/proxy-wasm/wasm_vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,11 @@ class WasmVm {
FOR_ALL_WASM_VM_IMPORTS(_REGISTER_CALLBACK)
#undef _REGISTER_CALLBACK

/**
* Terminate execution of this WasmVM. It shouldn't be used after being terminated.
*/
virtual void terminate() = 0;

bool isFailed() { return failed_ != FailState::Ok; }
void fail(FailState fail_state, std::string_view message) {
integration()->error(message);
Expand Down
14 changes: 14 additions & 0 deletions src/v8/v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
#include <mutex>
#include <optional>
#include <sstream>
#include <thread>
#include <unordered_map>
#include <utility>
#include <vector>

#include "include/v8-version.h"
#include "include/v8.h"
#include "src/wasm/c-api.h"
#include "wasm-api/wasm.hh"

namespace proxy_wasm {
Expand Down Expand Up @@ -92,6 +94,8 @@ class V8 : public WasmVm {
FOR_ALL_WASM_VM_EXPORTS(_GET_MODULE_FUNCTION)
#undef _GET_MODULE_FUNCTION

void terminate() override;

private:
std::string getFailMessage(std::string_view function_name, wasm::own<wasm::Trap> trap);

Expand Down Expand Up @@ -657,6 +661,16 @@ void V8::getModuleFunctionImpl(std::string_view function_name,
};
}

void V8::terminate() {
auto *store_impl = reinterpret_cast<wasm::StoreImpl *>(store_.get());
auto *isolate = store_impl->isolate();
isolate->TerminateExecution();
while (isolate->IsExecutionTerminating()) {
std::this_thread::yield();
}
integration()->trace("[host->vm] Terminated");
}

std::string V8::getFailMessage(std::string_view function_name, wasm::own<wasm::Trap> trap) {
auto message = "Function: " + std::string(function_name) + " failed: ";
message += std::string(trap->message().get(), trap->message().size());
Expand Down
3 changes: 3 additions & 0 deletions src/wamr/wamr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ class Wamr : public WasmVm {
};
FOR_ALL_WASM_VM_EXPORTS(_GET_MODULE_FUNCTION)
#undef _GET_MODULE_FUNCTION

void terminate() override {}

private:
template <typename... Args>
void registerHostFunctionImpl(std::string_view module_name, std::string_view function_name,
Expand Down
2 changes: 2 additions & 0 deletions src/wasmtime/wasmtime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ class Wasmtime : public WasmVm {
void getModuleFunctionImpl(std::string_view function_name,
std::function<R(ContextBase *, Args...)> *function);

void terminate() override {}

WasmStorePtr store_;
WasmModulePtr module_;
WasmSharedModulePtr shared_module_;
Expand Down
2 changes: 2 additions & 0 deletions src/wavm/wavm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ struct Wavm : public WasmVm {
FOR_ALL_WASM_VM_IMPORTS(_REGISTER_CALLBACK)
#undef _REGISTER_CALLBACK

void terminate() override {}

IR::Module ir_module_;
WAVM::Runtime::ModuleRef module_ = nullptr;
WAVM::Runtime::GCPointer<WAVM::Runtime::Instance> module_instance_;
Expand Down
1 change: 1 addition & 0 deletions test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ cc_test(
data = [
"//test/test_data:abi_export.wasm",
"//test/test_data:callback.wasm",
"//test/test_data:infinite_loop.wasm",
"//test/test_data:trap.wasm",
],
linkstatic = 1,
Expand Down
29 changes: 29 additions & 0 deletions test/runtime_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <memory>
#include <sstream>
#include <string>
#include <thread>
#include <vector>

#include "include/proxy-wasm/context.h"
Expand Down Expand Up @@ -245,6 +246,34 @@ TEST_P(TestVM, Callback) {
ASSERT_EQ(res.u32(), 100100); // 10000 (global) + 100(in callback)
}

TEST_P(TestVM, TerminateExecution) {
// TODO(chaoqin-li1123): implement execution termination for other runtime.
if (engine_ != "v8") {
return;
}
auto source = readTestWasmFile("infinite_loop.wasm");
ASSERT_TRUE(vm_->load(source, {}, {}));

TestContext context;

std::thread terminate([&]() {
std::this_thread::sleep_for(std::chrono::seconds(3));
vm_->terminate();
});

ASSERT_TRUE(vm_->link(""));
WasmCallVoid<0> infinite_loop;
vm_->getFunction("infinite_loop", &infinite_loop);
ASSERT_TRUE(infinite_loop != nullptr);
infinite_loop(&context);

terminate.join();

std::string exp_message = "Function: infinite_loop failed: Uncaught Error: termination_exception";
auto *integration = dynamic_cast<DummyIntegration *>(vm_->integration().get());
ASSERT_TRUE(integration->error_message_.find(exp_message) != std::string::npos);
}

TEST_P(TestVM, Trap) {
auto source = readTestWasmFile("trap.wasm");
ASSERT_TRUE(vm_->load(source, {}, {}));
Expand Down
5 changes: 5 additions & 0 deletions test/test_data/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ wasm_rust_binary(
srcs = ["trap.rs"],
)

wasm_rust_binary(
name = "infinite_loop.wasm",
srcs = ["infinite_loop.rs"],
)

wasm_rust_binary(
name = "env.wasm",
srcs = ["env.rs"],
Expand Down
21 changes: 21 additions & 0 deletions test/test_data/infinite_loop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2022 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.

#[no_mangle]
pub extern "C" fn infinite_loop() {
let mut _count: u64 = 0;
loop {
_count += 1;
}
}