Skip to content
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

DynVm Implementation: Dynamically loaded native modules #379

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ jobs:
arch: x86_64
action: test
flags: --config=gcc
- name: 'DynVM on Linux/x86_64'
engine: 'dyn'
os: ubuntu-22.04
arch: x86_64
action: test
flags: --config=gcc
- name: 'NullVM on Linux/x86_64 with ASan'
engine: 'null'
os: ubuntu-22.04
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/bazel-*
.vscode
29 changes: 29 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

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,32 @@ 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.h",
"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 +342,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
39 changes: 39 additions & 0 deletions bazel/wasm.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

load("@rules_rust//rust:defs.bzl", "rust_binary")
load("@rules_rust//rust:defs.bzl", "rust_shared_library")

def _wasm_rust_transition_impl(settings, attr):
return {
Expand Down Expand Up @@ -59,6 +60,17 @@ def _wasm_binary_impl(ctx):

return [DefaultInfo(files = depset([out]), runfiles = ctx.runfiles([out]))]

def _dyn_binary_impl(ctx):
out = ctx.actions.declare_file(ctx.label.name)
ctx.actions.run(
executable = "cp",
arguments = [ctx.files.binary[0].path, out.path],
outputs = [out],
inputs = ctx.files.binary,
)

return [DefaultInfo(files = depset([out]), runfiles = ctx.runfiles([out]))]

def _wasm_attrs(transition):
return {
"binary": attr.label(mandatory = True, cfg = transition),
Expand All @@ -67,6 +79,11 @@ def _wasm_attrs(transition):
"_whitelist_function_transition": attr.label(default = "@bazel_tools//tools/whitelists/function_transition_whitelist"),
}

def _dyn_attrs():
return {
"binary": attr.label(mandatory = True),
}

wasm_rust_binary_rule = rule(
implementation = _wasm_binary_impl,
attrs = _wasm_attrs(wasm_rust_transition),
Expand All @@ -77,6 +94,11 @@ wasi_rust_binary_rule = rule(
attrs = _wasm_attrs(wasi_rust_transition),
)

dyn_rust_binary_rule = rule(
implementation = _dyn_binary_impl,
attrs = _dyn_attrs(),
)

def wasm_rust_binary(name, tags = [], wasi = False, signing_key = [], **kwargs):
wasm_name = "_wasm_" + name.replace(".", "_")
kwargs.setdefault("visibility", ["//visibility:public"])
Expand All @@ -100,3 +122,20 @@ def wasm_rust_binary(name, tags = [], wasi = False, signing_key = [], **kwargs):
signing_key = signing_key,
tags = tags + ["manual"],
)

def dyn_rust_library(name, tags = [], **kwargs):
dyn_name = "_dyn_" + name.replace(".", "_")
kwargs.setdefault("visibility", ["//visibility:public"])

rust_shared_library(
name = dyn_name,
edition = "2018",
tags = ["manual"],
**kwargs
)

dyn_rust_binary_rule(
name = name,
binary = ":" + dyn_name,
tags = tags + ["manual"],
)
25 changes: 25 additions & 0 deletions include/proxy-wasm/dyn.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 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>

namespace proxy_wasm {

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

} // namespace proxy_wasm
67 changes: 67 additions & 0 deletions include/proxy-wasm/dyn_vm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 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 {

// 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);
Protryon marked this conversation as resolved.
Show resolved Hide resolved
ContextBase *getBaseContext();
Protryon marked this conversation as resolved.
Show resolved Hide resolved

// Support for embedders, not exported to Wasm.

#define FOR_ALL_HOST_FUNCTIONS(_f) \
Expand Down
23 changes: 23 additions & 0 deletions src/dyn/dyn.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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.h"
#include "include/proxy-wasm/dyn_vm.h"

namespace proxy_wasm {

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

} // namespace proxy_wasm
Loading
Loading