Skip to content

v8: set upper limit for maximum Wasm memory size. #281

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 5 commits into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions src/v8/v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
#include "src/wasm/c-api.h"
#include "wasm-api/wasm.hh"

namespace v8::internal {
extern unsigned int FLAG_wasm_max_mem_pages;
} // namespace v8::internal

namespace proxy_wasm {
namespace v8 {

Expand All @@ -39,6 +43,8 @@ wasm::Engine *engine() {
static wasm::own<wasm::Engine> engine;

std::call_once(init, []() {
// TODO(PiotrSikora): make this configurable and default to 64/128 MiB.
::v8::internal::FLAG_wasm_max_mem_pages = 16384; // 1 GiB
::v8::V8::EnableWebAssemblyTrapHandler(true);
engine = wasm::Engine::make();
});
Expand Down
2 changes: 1 addition & 1 deletion test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ cc_test(
data = [
"//test/test_data:callback.wasm",
"//test/test_data:clock.wasm",
"//test/test_data:infinite_loop.wasm",
"//test/test_data:resource_limits.wasm",
"//test/test_data:trap.wasm",
],
linkstatic = 1,
Expand Down
30 changes: 29 additions & 1 deletion test/runtime_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ TEST_P(TestVm, TerminateExecution) {
if (engine_ != "v8") {
return;
}
auto source = readTestWasmFile("infinite_loop.wasm");
auto source = readTestWasmFile("resource_limits.wasm");
ASSERT_FALSE(source.empty());
auto wasm = TestWasm(std::move(vm_));
ASSERT_TRUE(wasm.load(source, false));
Expand All @@ -112,6 +112,34 @@ TEST_P(TestVm, TerminateExecution) {
}
}

TEST_P(TestVm, WasmMemoryLimit) {
// TODO(PiotrSikora): enforce memory limits in other engines.
if (engine_ != "v8") {
return;
}
auto source = readTestWasmFile("resource_limits.wasm");
ASSERT_FALSE(source.empty());
auto wasm = TestWasm(std::move(vm_));
ASSERT_TRUE(wasm.load(source, false));
ASSERT_TRUE(wasm.initialize());

WasmCallVoid<0> infinite_memory;
wasm.wasm_vm()->getFunction("infinite_memory", &infinite_memory);
ASSERT_TRUE(infinite_memory != nullptr);
infinite_memory(wasm.vm_context());

EXPECT_TRUE(wasm.wasm_vm()->getMemorySize() == 1024 * 1024 * 1024);

// Check integration logs.
auto *host = dynamic_cast<TestIntegration *>(wasm.wasm_vm()->integration().get());
EXPECT_TRUE(host->isErrorLogged("Function: infinite_memory failed"));
if (engine_ == "v8") {
EXPECT_TRUE(host->isErrorLogged("Uncaught RuntimeError: unreachable"));
EXPECT_TRUE(host->isErrorLogged("Proxy-Wasm plugin in-VM backtrace:"));
EXPECT_TRUE(host->isErrorLogged(" - rust_oom"));
}
}

TEST_P(TestVm, Trap) {
auto source = readTestWasmFile("trap.wasm");
ASSERT_FALSE(source.empty());
Expand Down
4 changes: 2 additions & 2 deletions test/test_data/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ wasm_rust_binary(
)

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

wasm_rust_binary(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,11 @@ pub extern "C" fn infinite_loop() {
_count += 1;
}
}

#[no_mangle]
pub extern "C" fn infinite_memory() {
let mut vec = Vec::new();
loop {
vec.push(Vec::<u32>::with_capacity(8192));
}
}