Skip to content

Fix segfault when failing to instantiate Wasm module. #240

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 16 commits into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from 8 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
15 changes: 15 additions & 0 deletions bazel/external/v8.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Disable pointer compression (limits the maximum number of WasmVMs).

diff --git a/BUILD.bazel b/BUILD.bazel
index 1cc0121e60..4947c1dba2 100644
--- a/BUILD.bazel
+++ b/BUILD.bazel
@@ -161,7 +161,7 @@ v8_int(
# If no explicit value for v8_enable_pointer_compression, we set it to 'none'.
v8_string(
name = "v8_enable_pointer_compression",
- default = "none",
+ default = "False",
)

# Default setting for v8_enable_pointer_compression.
2 changes: 2 additions & 0 deletions bazel/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ def proxy_wasm_cpp_host_repositories():
commit = "90f089d97b6e4146ad106eee1829d86ad6392027",
remote = "https://chromium.googlesource.com/v8/v8",
shallow_since = "1643043727 +0000",
patches = ["@proxy_wasm_cpp_host//bazel/external:v8.patch"],
patch_args = ["-p1"],
)

native.bind(
Expand Down
9 changes: 7 additions & 2 deletions src/v8/v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ bool V8::link(std::string_view debug_name) {
fail(FailState::UnableToInitializeCode,
std::string("Failed to load Wasm module due to a missing import: ") +
std::string(module) + "." + std::string(name));
break;
return false;
}
auto func = it->second.get()->callback_.get();
if (!equalValTypes(import_type->func()->params(), func->type()->params()) ||
Expand All @@ -334,7 +334,7 @@ bool V8::link(std::string_view debug_name) {
printValTypes(import_type->func()->results()) +
", but host exports: " + printValTypes(func->type()->params()) + " -> " +
printValTypes(func->type()->results()));
break;
return false;
}
imports.push_back(func);
} break;
Expand All @@ -344,6 +344,7 @@ bool V8::link(std::string_view debug_name) {
fail(FailState::UnableToInitializeCode,
"Failed to load Wasm module due to a missing import: " + std::string(module) + "." +
std::string(name));
return false;
} break;

case wasm::EXTERN_MEMORY: {
Expand All @@ -369,6 +370,10 @@ bool V8::link(std::string_view debug_name) {
}

instance_ = wasm::Instance::make(store_.get(), module_.get(), imports.data());
if (!instance_) {
fail(FailState::UnableToInitializeCode, "Failed to create new Wasm instance");
return false;
}

const auto export_types = module_.get()->exports();
const auto exports = instance_.get()->exports();
Expand Down
10 changes: 7 additions & 3 deletions src/wamr/wamr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ bool Wamr::link(std::string_view debug_name) {
fail(FailState::UnableToInitializeCode,
std::string("Failed to load Wasm module due to a missing import: ") +
std::string(module_name) + "." + std::string(name));
break;
return false;
}

auto func = it->second->callback_.get();
Expand All @@ -242,7 +242,7 @@ bool Wamr::link(std::string_view debug_name) {
printValTypes(wasm_functype_results(exp_type)) +
", but host exports: " + printValTypes(wasm_functype_params(actual_type.get())) +
" -> " + printValTypes(wasm_functype_results(actual_type.get())));
break;
return false;
}
imports.push_back(wasm_func_as_extern(func));
} break;
Expand All @@ -251,6 +251,7 @@ bool Wamr::link(std::string_view debug_name) {
fail(FailState::UnableToInitializeCode,
"Failed to load Wasm module due to a missing import: " + std::string(module_name) + "." +
std::string(name));
return false;
} break;
case WASM_EXTERN_MEMORY: {
assert(memory_ == nullptr);
Expand All @@ -275,7 +276,10 @@ bool Wamr::link(std::string_view debug_name) {

wasm_extern_vec_t imports_vec = {imports.size(), imports.data()};
instance_ = wasm_instance_new(store_.get(), module_.get(), &imports_vec, nullptr);
assert(instance_ != nullptr);
if (!instance_) {
fail(FailState::UnableToInitializeCode, "Failed to create new Wasm instance");
return false;
}

WasmExportTypeVec export_types;
wasm_module_exports(module_.get(), export_types.get());
Expand Down
10 changes: 7 additions & 3 deletions src/wasmtime/wasmtime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ bool Wasmtime::link(std::string_view debug_name) {
fail(FailState::UnableToInitializeCode,
std::string("Failed to load Wasm module due to a missing import: ") +
std::string(module_name) + "." + std::string(name));
break;
return false;
}

auto func = it->second->callback_.get();
Expand All @@ -256,7 +256,7 @@ bool Wasmtime::link(std::string_view debug_name) {
printValTypes(wasm_functype_results(exp_type)) +
", but host exports: " + printValTypes(wasm_functype_params(actual_type.get())) +
" -> " + printValTypes(wasm_functype_results(actual_type.get())));
break;
return false;
}
imports.push_back(wasm_func_as_extern(func));
} break;
Expand All @@ -265,6 +265,7 @@ bool Wasmtime::link(std::string_view debug_name) {
fail(FailState::UnableToInitializeCode,
"Failed to load Wasm module due to a missing import: " + std::string(module_name) + "." +
std::string(name));
return false;
} break;
case WASM_EXTERN_MEMORY: {
assert(memory_ == nullptr);
Expand All @@ -289,7 +290,10 @@ bool Wasmtime::link(std::string_view debug_name) {

wasm_extern_vec_t imports_vec = {imports.size(), imports.data()};
instance_ = wasm_instance_new(store_.get(), module_.get(), &imports_vec, nullptr);
assert(instance_ != nullptr);
if (!instance_) {
fail(FailState::UnableToInitializeCode, "Failed to create new Wasm instance");
return false;
}

WasmExportTypeVec export_types;
wasm_module_exports(module_.get(), export_types.get());
Expand Down
10 changes: 10 additions & 0 deletions src/wavm/wavm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,22 @@ Wavm::~Wavm() {

std::unique_ptr<WasmVm> Wavm::clone() {
auto wavm = std::make_unique<Wavm>();
if (wavm == nullptr) {
return nullptr;
}

wavm->integration().reset(integration()->clone());

wavm->compartment_ = WAVM::Runtime::cloneCompartment(compartment_);
if (wavm->compartment_ == nullptr) {
return nullptr;
}
wavm->memory_ = WAVM::Runtime::remapToClonedCompartment(memory_, wavm->compartment_);
wavm->memory_base_ = WAVM::Runtime::getMemoryBaseAddress(wavm->memory_);
wavm->context_ = WAVM::Runtime::createContext(wavm->compartment_);
if (wavm->context_ == nullptr) {
return nullptr;
}

for (auto &p : intrinsic_module_instances_) {
wavm->intrinsic_module_instances_.emplace(
Expand Down
30 changes: 30 additions & 0 deletions test/runtime_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,36 @@ TEST_P(TestVM, Clone) {
ASSERT_NE(100, word.u64_);
}

TEST_P(TestVM, CloneUntilOutOfMemory) {
if (vm_->cloneable() == proxy_wasm::Cloneable::NotCloneable) {
return;
}
if (runtime_ == "wavm") {
// TODO(PiotrSikora): Figure out why this fails on the CI.
return;
}

auto source = readTestWasmFile("abi_export.wasm");
ASSERT_TRUE(vm_->load(source, {}, {}));
ASSERT_TRUE(vm_->link(""));

std::vector<std::unique_ptr<WasmVm>> clones;
for (;;) {
auto clone = vm_->clone();
if (clone == nullptr) {
break;
}
if (clone->cloneable() != proxy_wasm::Cloneable::InstantiatedModule) {
if (clone->link("") == false) {
break;
}
}
// Prevent clone from droping out of scope and freeing memory.
clones.push_back(std::move(clone));
}
ASSERT_GE(clones.size(), 1000);
}

class TestContext : public ContextBase {
public:
TestContext(){};
Expand Down