Skip to content

Commit

Permalink
dyn vm implementation
Browse files Browse the repository at this point in the history
fix comments

format and lints

Signed-off-by: Protryon <max.bruce12@gmail.com>
  • Loading branch information
Protryon committed Feb 29, 2024
1 parent 9be9637 commit 70b1002
Show file tree
Hide file tree
Showing 13 changed files with 1,003 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/bazel-*
.vscode
28 changes: 28 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
load("@rules_cc//cc:defs.bzl", "cc_library")
load(
"@proxy_wasm_cpp_host//bazel:select.bzl",
"proxy_wasm_select_engine_dyn",
"proxy_wasm_select_engine_null",
"proxy_wasm_select_engine_v8",
"proxy_wasm_select_engine_wamr",
Expand Down Expand Up @@ -122,6 +123,31 @@ cc_library(
],
)

cc_library(
name = "dyn_lib",
srcs = [
"src/dyn/dyn.cc",
"src/dyn/dyn_ffi.cc",
"src/dyn/dyn_vm.cc",
"src/dyn/dyn_vm_plugin.cc",
],
hdrs = [
"include/proxy-wasm/dyn_vm.h",
"include/proxy-wasm/dyn_vm_plugin.h",
"include/proxy-wasm/wasm_api_impl.h",
],
defines = [
"PROXY_WASM_HAS_RUNTIME_DYN",
"PROXY_WASM_HOST_ENGINE_DYN",
],
deps = [
":headers",
"@com_google_protobuf//:protobuf_lite",
"@proxy_wasm_cpp_sdk//:api_lib",
],
alwayslink = 1,
)

cc_library(
name = "v8_lib",
srcs = [
Expand Down Expand Up @@ -315,6 +341,8 @@ cc_library(
":base_lib",
] + proxy_wasm_select_engine_null(
[":null_lib"],
) + proxy_wasm_select_engine_dyn(
[":dyn_lib"],
) + proxy_wasm_select_engine_v8(
[":v8_lib"],
) + proxy_wasm_select_engine_wamr(
Expand Down
5 changes: 5 additions & 0 deletions bazel/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ config_setting(
values = {"define": "engine=null"},
)

config_setting(
name = "engine_dyn",
values = {"define": "engine=dyn"},
)

config_setting(
name = "engine_v8",
values = {"define": "engine=v8"},
Expand Down
7 changes: 7 additions & 0 deletions bazel/select.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ def proxy_wasm_select_engine_null(xs):
"//conditions:default": [],
})

def proxy_wasm_select_engine_dyn(xs):
return select({
"@proxy_wasm_cpp_host//bazel:engine_dyn": xs,
"@proxy_wasm_cpp_host//bazel:multiengine": xs,
"//conditions:default": [],
})

def proxy_wasm_select_engine_v8(xs):
return select({
"@proxy_wasm_cpp_host//bazel:engine_v8": xs,
Expand Down
70 changes: 70 additions & 0 deletions include/proxy-wasm/dyn_vm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2016-2019 Envoy Project Authors
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include <memory>
#include <utility>
#include <vector>

#include "include/proxy-wasm/dyn_vm_plugin.h"
#include "include/proxy-wasm/wasm_vm.h"

namespace proxy_wasm {

class WasmVm;
std::unique_ptr<WasmVm> createDynVm();

// The DynVm wraps a C++ Wasm plugin which has been compiled with the Wasm API
// and dynamically linked into the proxy.
struct DynVm : public WasmVm {
DynVm() : WasmVm() {}

// WasmVm
std::string_view getEngineName() override { return "dyn"; }
Cloneable cloneable() override { return Cloneable::InstantiatedModule; };
std::unique_ptr<WasmVm> clone() override;
bool load(std::string_view plugin_name, std::string_view precompiled,
const std::unordered_map<uint32_t, std::string> &function_names) override;
bool link(std::string_view debug_name) override;
uint64_t getMemorySize() override;
std::optional<std::string_view> getMemory(uint64_t pointer, uint64_t size) override;
bool setMemory(uint64_t pointer, uint64_t size, const void *data) override;
bool setWord(uint64_t pointer, Word data) override;
bool getWord(uint64_t pointer, Word *data) override;
size_t getWordSize() override;
std::string_view getPrecompiledSectionName() override;

#define _FORWARD_GET_FUNCTION(_T) \
void getFunction(std::string_view function_name, _T *f) override { \
plugin_->getFunction(function_name, f); \
}
FOR_ALL_WASM_VM_EXPORTS(_FORWARD_GET_FUNCTION)
#undef _FORWARD_GET_FUNCTION

// These are not needed for DynVm which invokes the handlers directly.
#define _REGISTER_CALLBACK(_T) \
void registerCallback(std::string_view, std::string_view, _T, \
typename ConvertFunctionTypeWordToUint32<_T>::type) override{};
FOR_ALL_WASM_VM_IMPORTS(_REGISTER_CALLBACK)
#undef _REGISTER_CALLBACK

void terminate() override {}
bool usesWasmByteOrder() override { return false; }

std::unique_ptr<DynVmPlugin> plugin_;
};

} // namespace proxy_wasm
45 changes: 45 additions & 0 deletions include/proxy-wasm/dyn_vm_plugin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2016-2019 Envoy Project Authors
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include "include/proxy-wasm/wasm_vm.h"

namespace proxy_wasm {

class DynVmPluginSource {
public:
virtual ~DynVmPluginSource();
void *dl_handle = nullptr;
int memfd = -1;
};

// A wrapper for the dynamically linked DynVm plugin which implements the Wasm ABI.
class DynVmPlugin {
public:
DynVmPlugin() = default;
virtual ~DynVmPlugin() = default;

// NB: These are defined rather than declared PURE because gmock uses __LINE__ internally for
// uniqueness, making it impossible to use FOR_ALL_WASM_VM_EXPORTS with MOCK_METHOD.
#define _DEFINE_GET_FUNCTION(_T) virtual void getFunction(std::string_view, _T *f);
FOR_ALL_WASM_VM_EXPORTS(_DEFINE_GET_FUNCTION)
#undef _DEFINE_GET_FUNCTION

WasmVm *wasm_vm_ = nullptr;
std::shared_ptr<DynVmPluginSource> source;
};

} // namespace proxy_wasm
4 changes: 4 additions & 0 deletions include/proxy-wasm/exports.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ Word wasi_unstable_random_get(Word, Word);
Word pthread_equal(Word left, Word right);
void emscripten_notify_memory_growth(Word);

Word set_effective_context(Word context_id);
void setLimitedEffectiveContext(ContextBase *context);
ContextBase *getBaseContext();

// Support for embedders, not exported to Wasm.

#define FOR_ALL_HOST_FUNCTIONS(_f) \
Expand Down
22 changes: 22 additions & 0 deletions src/dyn/dyn.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2016-2019 Envoy Project Authors
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "include/proxy-wasm/dyn_vm.h"

namespace proxy_wasm {

std::unique_ptr<WasmVm> createDynVm() { return std::make_unique<DynVm>(); }

} // namespace proxy_wasm
Loading

0 comments on commit 70b1002

Please sign in to comment.