Skip to content

Commit

Permalink
WAMR: adds support for clone. (#317)
Browse files Browse the repository at this point in the history
  • Loading branch information
lum1n0us authored Dec 16, 2022
1 parent 624ef2e commit 72ce32f
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 13 deletions.
12 changes: 7 additions & 5 deletions bazel/external/wamr.BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ filegroup(
cmake(
name = "wamr_lib",
cache_entries = {
"WAMR_BUILD_INTERP": "1",
"WAMR_BUILD_AOT": "0",
"WAMR_BUILD_FAST_INTERP": "1",
"WAMR_BUILD_INTERP": "1",
"WAMR_BUILD_JIT": "0",
"WAMR_BUILD_LAZY_JIT": "0",
"WAMR_BUILD_AOT": "0",
"WAMR_BUILD_SIMD": "0",
"WAMR_BUILD_MULTI_MODULE": "1",
"WAMR_BUILD_LIBC_WASI": "0",
"WAMR_BUILD_MULTI_MODULE": "0",
"WAMR_BUILD_SIMD": "0",
"WAMR_BUILD_TAIL_CALL": "1",
"WAMR_BUILD_WASM_CACHE": "0",
"WAMR_DISABLE_HW_BOUND_CHECK": "0",
"WAMR_DISABLE_STACK_HW_BOUND_CHECK": "1",
},
generate_args = ["-GNinja"],
lib_source = ":srcs",
Expand Down
8 changes: 4 additions & 4 deletions bazel/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@ def proxy_wasm_cpp_host_repositories():
http_archive,
name = "com_github_bytecodealliance_wasm_micro_runtime",
build_file = "@proxy_wasm_cpp_host//bazel/external:wamr.BUILD",
# WAMR-05-18-2022
sha256 = "350736fffdc49533f5f372221d01e3b570ecd7b85f4429b22f5d89594eb99d9c",
strip_prefix = "wasm-micro-runtime-d7a2888b18c478d87ce8094e1419b4e061db289f",
url = "https://github.com/bytecodealliance/wasm-micro-runtime/archive/d7a2888b18c478d87ce8094e1419b4e061db289f.tar.gz",
# WAMR-2022-12-07
sha256 = "6a5263ad022176257a93b39b02f95f615c0c590da1798c86c935f501a51c30c4",
strip_prefix = "wasm-micro-runtime-c3d66f916ef8093e5c8cacf3329ed968f807cf58",
url = "https://github.com/bytecodealliance/wasm-micro-runtime/archive/c3d66f916ef8093e5c8cacf3329ed968f807cf58.tar.gz",
)

native.bind(
Expand Down
1 change: 1 addition & 0 deletions src/wamr/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ using WasmEnginePtr = common::CSmartPtr<wasm_engine_t, wasm_engine_delete>;
using WasmFuncPtr = common::CSmartPtr<wasm_func_t, wasm_func_delete>;
using WasmStorePtr = common::CSmartPtr<wasm_store_t, wasm_store_delete>;
using WasmModulePtr = common::CSmartPtr<wasm_module_t, wasm_module_delete>;
using WasmSharedModulePtr = common::CSmartPtr<wasm_shared_module_t, wasm_shared_module_delete>;
using WasmMemoryPtr = common::CSmartPtr<wasm_memory_t, wasm_memory_delete>;
using WasmTablePtr = common::CSmartPtr<wasm_table_t, wasm_table_delete>;
using WasmInstancePtr = common::CSmartPtr<wasm_instance_t, wasm_instance_delete>;
Expand Down
37 changes: 35 additions & 2 deletions src/wamr/wamr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class Wamr : public WasmVm {
std::string_view getEngineName() override { return "wamr"; }
std::string_view getPrecompiledSectionName() override { return ""; }

Cloneable cloneable() override { return Cloneable::NotCloneable; }
std::unique_ptr<WasmVm> clone() override { return nullptr; }
Cloneable cloneable() override { return Cloneable::CompiledBytecode; }
std::unique_ptr<WasmVm> clone() override;

bool load(std::string_view bytecode, std::string_view precompiled,
const std::unordered_map<uint32_t, std::string> &function_names) override;
Expand Down Expand Up @@ -108,6 +108,7 @@ class Wamr : public WasmVm {

WasmStorePtr store_;
WasmModulePtr module_;
WasmSharedModulePtr shared_module_;
WasmInstancePtr instance_;

WasmMemoryPtr memory_;
Expand All @@ -132,9 +133,41 @@ bool Wamr::load(std::string_view bytecode, std::string_view /*precompiled*/,
return false;
}

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

return true;
}

std::unique_ptr<WasmVm> Wamr::clone() {
assert(module_ != nullptr);

auto vm = std::make_unique<Wamr>();
if (vm == nullptr) {
return nullptr;
}

vm->store_ = wasm_store_new(engine());
if (vm->store_ == nullptr) {
return nullptr;
}

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

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

return vm;
}

static bool equalValTypes(const wasm_valtype_vec_t *left, const wasm_valtype_vec_t *right) {
if (left->size != right->size) {
return false;
Expand Down
4 changes: 2 additions & 2 deletions test/wasm_vm_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ INSTANTIATE_TEST_SUITE_P(WasmEngines, TestVm, testing::ValuesIn(getWasmEngines()
});

TEST_P(TestVm, Basic) {
if (engine_ == "wamr" || engine_ == "wasmedge") {
if (engine_ == "wasmedge") {
EXPECT_EQ(vm_->cloneable(), proxy_wasm::Cloneable::NotCloneable);
} else if (engine_ == "wasmtime" || engine_ == "v8") {
} else if (engine_ == "wasmtime" || engine_ == "v8" || engine_ == "wamr") {
EXPECT_EQ(vm_->cloneable(), proxy_wasm::Cloneable::CompiledBytecode);
} else if (engine_ == "wavm") {
EXPECT_EQ(vm_->cloneable(), proxy_wasm::Cloneable::InstantiatedModule);
Expand Down

0 comments on commit 72ce32f

Please sign in to comment.