From 72ce32f7b11f9190edf874028255e1309e41690f Mon Sep 17 00:00:00 2001 From: "liang.he" Date: Fri, 16 Dec 2022 10:05:25 +0800 Subject: [PATCH] WAMR: adds support for `clone`. (#317) Signed-off-by: liang.he@intel.com --- bazel/external/wamr.BUILD | 12 +++++++----- bazel/repositories.bzl | 8 ++++---- src/wamr/types.h | 1 + src/wamr/wamr.cc | 37 +++++++++++++++++++++++++++++++++++-- test/wasm_vm_test.cc | 4 ++-- 5 files changed, 49 insertions(+), 13 deletions(-) diff --git a/bazel/external/wamr.BUILD b/bazel/external/wamr.BUILD index f25b9f07..9e7445ff 100644 --- a/bazel/external/wamr.BUILD +++ b/bazel/external/wamr.BUILD @@ -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", diff --git a/bazel/repositories.bzl b/bazel/repositories.bzl index dbf5a0cd..48b3c75c 100644 --- a/bazel/repositories.bzl +++ b/bazel/repositories.bzl @@ -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( diff --git a/src/wamr/types.h b/src/wamr/types.h index f6e51688..7252c5c3 100644 --- a/src/wamr/types.h +++ b/src/wamr/types.h @@ -21,6 +21,7 @@ using WasmEnginePtr = common::CSmartPtr; using WasmFuncPtr = common::CSmartPtr; using WasmStorePtr = common::CSmartPtr; using WasmModulePtr = common::CSmartPtr; +using WasmSharedModulePtr = common::CSmartPtr; using WasmMemoryPtr = common::CSmartPtr; using WasmTablePtr = common::CSmartPtr; using WasmInstancePtr = common::CSmartPtr; diff --git a/src/wamr/wamr.cc b/src/wamr/wamr.cc index 2ca6863b..577b6784 100644 --- a/src/wamr/wamr.cc +++ b/src/wamr/wamr.cc @@ -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 clone() override { return nullptr; } + Cloneable cloneable() override { return Cloneable::CompiledBytecode; } + std::unique_ptr clone() override; bool load(std::string_view bytecode, std::string_view precompiled, const std::unordered_map &function_names) override; @@ -108,6 +108,7 @@ class Wamr : public WasmVm { WasmStorePtr store_; WasmModulePtr module_; + WasmSharedModulePtr shared_module_; WasmInstancePtr instance_; WasmMemoryPtr memory_; @@ -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 Wamr::clone() { + assert(module_ != nullptr); + + auto vm = std::make_unique(); + 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; diff --git a/test/wasm_vm_test.cc b/test/wasm_vm_test.cc index a9caac93..6b4ece60 100644 --- a/test/wasm_vm_test.cc +++ b/test/wasm_vm_test.cc @@ -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);