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 all 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
59 changes: 51 additions & 8 deletions src/v8/v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -250,25 +250,32 @@ template <typename T, typename U> constexpr T convertValTypesToArgsTuple(const U
bool V8::load(std::string_view bytecode, std::string_view precompiled,
const std::unordered_map<uint32_t, std::string> function_names) {
store_ = wasm::Store::make(engine());
if (store_ == nullptr) {
return false;
}

if (!precompiled.empty()) {
auto vec = wasm::vec<byte_t>::make_uninitialized(precompiled.size());
::memcpy(vec.get(), precompiled.data(), precompiled.size());
module_ = wasm::Module::deserialize(store_.get(), vec);
if (module_ == nullptr) {
return false;
}

} else {
auto vec = wasm::vec<byte_t>::make_uninitialized(bytecode.size());
::memcpy(vec.get(), bytecode.data(), bytecode.size());
module_ = wasm::Module::make(store_.get(), vec);
if (module_ == nullptr) {
return false;
}
}

if (!module_) {
shared_module_ = module_->share();
if (shared_module_ == nullptr) {
return false;
}

shared_module_ = module_->share();
assert(shared_module_ != nullptr);

function_names_index_ = function_names;

return true;
Expand All @@ -278,10 +285,26 @@ std::unique_ptr<WasmVm> V8::clone() {
assert(shared_module_ != nullptr);

auto clone = std::make_unique<V8>();
clone->integration().reset(integration()->clone());
if (clone == nullptr) {
return nullptr;
}

clone->store_ = wasm::Store::make(engine());
if (clone->store_ == nullptr) {
return nullptr;
}

clone->module_ = wasm::Module::obtain(clone->store_.get(), shared_module_.get());
if (clone->module_ == nullptr) {
return nullptr;
}

auto integration_clone = integration()->clone();
if (integration_clone == nullptr) {
return nullptr;
}
clone->integration().reset(integration_clone);

clone->function_names_index_ = function_names_index_;

return clone;
Expand Down Expand Up @@ -322,7 +345,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 +357,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,12 +367,19 @@ 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: {
assert(memory_ == nullptr);
auto type = wasm::MemoryType::make(import_type->memory()->limits());
if (type == nullptr) {
return false;
}
memory_ = wasm::Memory::make(store_.get(), type.get());
if (memory_ == nullptr) {
return false;
}
imports.push_back(memory_.get());
} break;

Expand All @@ -358,7 +388,13 @@ bool V8::link(std::string_view debug_name) {
auto type =
wasm::TableType::make(wasm::ValType::make(import_type->table()->element()->kind()),
import_type->table()->limits());
if (type == nullptr) {
return false;
}
table_ = wasm::Table::make(store_.get(), type.get());
if (table_ == nullptr) {
return false;
}
imports.push_back(table_.get());
} break;
}
Expand All @@ -369,6 +405,10 @@ bool V8::link(std::string_view debug_name) {
}

instance_ = wasm::Instance::make(store_.get(), module_.get(), imports.data());
if (instance_ == nullptr) {
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 All @@ -395,6 +435,9 @@ bool V8::link(std::string_view debug_name) {
assert(export_item->memory() != nullptr);
assert(memory_ == nullptr);
memory_ = exports[i]->memory()->copy();
if (memory_ == nullptr) {
return false;
}
} break;

case wasm::EXTERN_TABLE: {
Expand All @@ -403,7 +446,7 @@ bool V8::link(std::string_view debug_name) {
}
}

return !isFailed();
return true;
}

uint64_t V8::getMemorySize() { return memory_->data_size(); }
Expand Down
35 changes: 30 additions & 5 deletions src/wamr/wamr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,19 @@ class Wamr : public WasmVm {
bool Wamr::load(std::string_view bytecode, std::string_view,
const std::unordered_map<uint32_t, std::string>) {
store_ = wasm_store_new(engine());
if (store_ == nullptr) {
return false;
}

WasmByteVec vec;
wasm_byte_vec_new(vec.get(), bytecode.size(), bytecode.data());

module_ = wasm_module_new(store_.get(), vec.get());
if (module_ == nullptr) {
return false;
}

return module_ != nullptr;
return true;
}

static bool equalValTypes(const wasm_valtype_vec_t *left, const wasm_valtype_vec_t *right) {
Expand Down Expand Up @@ -225,7 +232,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 +249,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,19 +258,32 @@ 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);
const wasm_memorytype_t *memory_type =
wasm_externtype_as_memorytype_const(extern_type); // owned by `extern_type`
if (memory_type == nullptr) {
return false;
}
memory_ = wasm_memory_new(store_.get(), memory_type);
if (memory_ == nullptr) {
return false;
}
imports.push_back(wasm_memory_as_extern(memory_.get()));
} break;
case WASM_EXTERN_TABLE: {
assert(table_ == nullptr);
const wasm_tabletype_t *table_type =
wasm_externtype_as_tabletype_const(extern_type); // owned by `extern_type`
if (table_type == nullptr) {
return false;
}
table_ = wasm_table_new(store_.get(), table_type, nullptr);
if (table_ == nullptr) {
return false;
}
imports.push_back(wasm_table_as_extern(table_.get()));
} break;
}
Expand All @@ -275,7 +295,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_ == nullptr) {
fail(FailState::UnableToInitializeCode, "Failed to create new Wasm instance");
return false;
}

WasmExportTypeVec export_types;
wasm_module_exports(module_.get(), export_types.get());
Expand All @@ -302,7 +325,9 @@ bool Wamr::link(std::string_view debug_name) {
case WASM_EXTERN_MEMORY: {
assert(memory_ == nullptr);
memory_ = wasm_memory_copy(wasm_extern_as_memory(actual_extern));
assert(memory_ != nullptr);
if (memory_ == nullptr) {
return false;
}
} break;
case WASM_EXTERN_TABLE: {
// TODO(mathetake): add support when/if needed.
Expand Down
53 changes: 46 additions & 7 deletions src/wasmtime/wasmtime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -112,28 +112,49 @@ class Wasmtime : public WasmVm {
bool Wasmtime::load(std::string_view bytecode, std::string_view,
const std::unordered_map<uint32_t, std::string>) {
store_ = wasm_store_new(engine());
if (store_ == nullptr) {
return false;
}

WasmByteVec vec;
wasm_byte_vec_new(vec.get(), bytecode.size(), bytecode.data());

module_ = wasm_module_new(store_.get(), vec.get());
if (!module_) {
if (module_ == nullptr) {
return false;
}

shared_module_ = wasm_module_share(module_.get());
assert(shared_module_ != nullptr);
if (shared_module_ == nullptr) {
return false;
}

return true;
}

std::unique_ptr<WasmVm> Wasmtime::clone() {
assert(shared_module_ != nullptr);

auto clone = std::make_unique<Wasmtime>();
if (clone == nullptr) {
return nullptr;
}

clone->integration().reset(integration()->clone());
clone->store_ = wasm_store_new(engine());
if (clone->store_ == nullptr) {
return nullptr;
}

clone->module_ = wasm_module_obtain(clone->store_.get(), shared_module_.get());
if (clone->module_ == nullptr) {
return nullptr;
}

auto integration_clone = integration()->clone();
if (integration_clone == nullptr) {
return nullptr;
}
clone->integration().reset(integration_clone);

return clone;
}
Expand Down Expand Up @@ -239,7 +260,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 +277,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,19 +286,32 @@ 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);
const wasm_memorytype_t *memory_type =
wasm_externtype_as_memorytype_const(extern_type); // owned by `extern_type`
if (memory_type == nullptr) {
return false;
}
memory_ = wasm_memory_new(store_.get(), memory_type);
if (memory_ == nullptr) {
return false;
}
imports.push_back(wasm_memory_as_extern(memory_.get()));
} break;
case WASM_EXTERN_TABLE: {
assert(table_ == nullptr);
const wasm_tabletype_t *table_type =
wasm_externtype_as_tabletype_const(extern_type); // owned by `extern_type`
if (table_type == nullptr) {
return false;
}
table_ = wasm_table_new(store_.get(), table_type, nullptr);
if (table_ == nullptr) {
return false;
}
imports.push_back(wasm_table_as_extern(table_.get()));
} break;
}
Expand All @@ -289,7 +323,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_ == nullptr) {
fail(FailState::UnableToInitializeCode, "Failed to create new Wasm instance");
return false;
}

WasmExportTypeVec export_types;
wasm_module_exports(module_.get(), export_types.get());
Expand All @@ -316,7 +353,9 @@ bool Wasmtime::link(std::string_view debug_name) {
case WASM_EXTERN_MEMORY: {
assert(memory_ == nullptr);
memory_ = wasm_memory_copy(wasm_extern_as_memory(actual_extern));
assert(memory_ != nullptr);
if (memory_ == nullptr) {
return false;
}
} break;
case WASM_EXTERN_TABLE: {
// TODO(mathetake): add support when/if needed.
Expand Down
Loading