Skip to content
This repository was archived by the owner on Dec 16, 2020. It is now read-only.
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
9 changes: 3 additions & 6 deletions bazel/external/wee8.BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ load(":genrule_cmd.bzl", "genrule_cmd")

cc_library(
name = "wee8",
srcs = select({
"@envoy//bazel:windows_x86_64": ["WINDOWS_IS_NOT_SUPPORTED_YET"],
"//conditions:default": [
"libwee8.a",
],
}),
srcs = [
"libwee8.a",
],
hdrs = [
"wee8/third_party/wasm-api/wasm.hh",
],
Expand Down
4 changes: 4 additions & 0 deletions bazel/external/wee8.genrule_cmd
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export NM=$${NM:-nm}
if [[ $${ENVOY_ASAN-} == "1" ]]; then
WEE8_BUILD_ARGS+=" is_asan=true"
WEE8_BUILD_ARGS+=" is_lsan=true"
WEE8_BUILD_ARGS+=" is_ubsan=true"
fi
if [[ $${ENVOY_UBSAN_VPTR-} == "1" ]]; then
WEE8_BUILD_ARGS+=" is_ubsan_vptr=true"
fi
if [[ $${ENVOY_MSAN-} == "1" ]]; then
WEE8_BUILD_ARGS+=" is_msan=true"
Expand Down
8 changes: 4 additions & 4 deletions bazel/external/wee8.patch
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# 1. Fix linking with unbundled toolchain on macOS.
# 2. Increase VSZ limit to 4TiB (allows us to start up to 370 VMs).
--- a/wee8/build/toolchain/gcc_toolchain.gni
+++ b/wee8/build/toolchain/gcc_toolchain.gni
--- wee8/build/toolchain/gcc_toolchain.gni
+++ wee8/build/toolchain/gcc_toolchain.gni
@@ -355,6 +355,8 @@ template("gcc_toolchain") {
# AIX does not support either -D (deterministic output) or response
# files.
Expand All @@ -20,8 +20,8 @@
# the "--start-group .. --end-group" feature isn't available on the aix ld.
start_group_flag = "-Wl,--start-group"
end_group_flag = "-Wl,--end-group "
--- a/wee8/src/wasm/wasm-memory.cc
+++ b/wee8/src/wasm/wasm-memory.cc
--- wee8/src/wasm/wasm-memory.cc
+++ wee8/src/wasm/wasm-memory.cc
@@ -142,7 +142,7 @@ void* TryAllocateBackingStore(WasmMemoryTracker* memory_tracker, Heap* heap,
// address space limits needs to be smaller.
constexpr size_t kAddressSpaceLimit = 0x8000000000L; // 512 GiB
Expand Down
2 changes: 1 addition & 1 deletion bazel/genrule_repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def _genrule_repository(ctx):
for ii, patch in enumerate(ctx.attr.patches):
patch_input = "patch-input-%d.patch" % (ii,)
ctx.symlink(patch, patch_input)
patch_result = ctx.execute(["patch", "-p1", "--input", patch_input])
patch_result = ctx.execute(["patch", "-p0", "--input", patch_input])
if patch_result.return_code != 0:
fail("Failed to apply patch %r: %s" % (patch, patch_result.stderr))

Expand Down
1 change: 1 addition & 0 deletions bazel/setup_clang.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ build:clang --action_env=CXX=clang++
build:clang --action_env=LD_LIBRARY_PATH=$(llvm-config --libdir)
build:clang --test_env=LD_LIBRARY_PATH=$(llvm-config --libdir)

build:clang-asan --action_env=ENVOY_UBSAN_VPTR=1
build:clang-asan --copt=-fsanitize=vptr,function
build:clang-asan --linkopt=-fsanitize=vptr,function
build:clang-asan --linkopt=-L${RT_LIBRARY_PATH}
Expand Down
2 changes: 0 additions & 2 deletions source/extensions/common/wasm/v8/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ envoy_cc_library(
"wee8",
],
deps = [
"//include/envoy/server:wasm_interface",
"//include/envoy/thread_local:thread_local_interface",
"//source/common/common:assert_lib",
"//source/extensions/common/wasm:wasm_vm_interface",
"//source/extensions/common/wasm:well_known_names",
Expand Down
125 changes: 64 additions & 61 deletions source/extensions/common/wasm/v8/v8.cc

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion source/extensions/common/wasm/v8/v8.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Common {
namespace Wasm {
namespace V8 {

std::unique_ptr<WasmVm> createVm();
WasmVmPtr createVm();

} // namespace V8
} // namespace Wasm
Expand Down
19 changes: 0 additions & 19 deletions source/extensions/common/wasm/wasm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,25 +73,6 @@ std::string base64Sha256(absl::string_view data) {

inline Word wasmResultToWord(WasmResult r) { return Word(static_cast<uint64_t>(r)); }

inline uint32_t convertWordToUint32(Word w) { return static_cast<uint32_t>(w.u64_); }

// Convert a function of the form Word(Word...) to one of the form uint32_t(uint32_t...).
template <typename F, F* fn> struct ConvertFunctionWordToUint32 {
static void convertFunctionWordToUint32() {}
};
template <typename R, typename... Args, auto (*F)(Args...)->R>
struct ConvertFunctionWordToUint32<R(Args...), F> {
static auto convertFunctionWordToUint32(typename ConvertWordTypeToUint32<Args>::type... args) {
return convertWordToUint32(F(std::forward<Args>(args)...));
}
};
template <typename... Args, auto (*F)(Args...)->void>
struct ConvertFunctionWordToUint32<void(Args...), F> {
static void convertFunctionWordToUint32(typename ConvertWordTypeToUint32<Args>::type... args) {
F(std::forward<Args>(args)...);
}
};

class SharedData {
public:
WasmResult get(absl::string_view vm_id, const absl::string_view key,
Expand Down
2 changes: 1 addition & 1 deletion source/extensions/common/wasm/wasm_vm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ WasmVmPtr createWasmVm(absl::string_view runtime) {
return Null::createVm();
} else
#ifdef ENVOY_WASM_V8
if (runtime == WasmRuntimeNames::get().v8) {
if (runtime == WasmRuntimeNames::get().V8) {
return V8::createVm();
} else
#endif
Expand Down
24 changes: 24 additions & 0 deletions source/extensions/common/wasm/wasm_vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,30 @@ template <typename R, typename... Args> struct ConvertFunctionTypeWordToUint32<R
typename ConvertWordTypeToUint32<Args>::type...);
};

template <typename T> inline auto convertWordToUint32(T t) { return t; }
template <> inline auto convertWordToUint32<Word>(Word t) { return static_cast<uint32_t>(t.u64_); }

// Convert a function of the form Word(Word...) to one of the form uint32_t(uint32_t...).
template <typename F, F* fn> struct ConvertFunctionWordToUint32 {
static void convertFunctionWordToUint32() {}
};
template <typename R, typename... Args, auto (*F)(Args...)->R>
struct ConvertFunctionWordToUint32<R(Args...), F> {
static typename ConvertWordTypeToUint32<R>::type
convertFunctionWordToUint32(typename ConvertWordTypeToUint32<Args>::type... args) {
return convertWordToUint32(F(std::forward<Args>(args)...));
}
};
template <typename... Args, auto (*F)(Args...)->void>
struct ConvertFunctionWordToUint32<void(Args...), F> {
static void convertFunctionWordToUint32(typename ConvertWordTypeToUint32<Args>::type... args) {
F(std::forward<Args>(args)...);
}
};

#define CONVERT_FUNCTION_WORD_TO_UINT32(_f) \
&ConvertFunctionWordToUint32<decltype(_f), _f>::convertFunctionWordToUint32

// These are templates and its helper for constructing signatures of functions calling into and out
// of WASM VMs.
// - WasmFuncTypeHelper is a helper for WasmFuncType and shouldn't be used anywhere else than
Expand Down
4 changes: 2 additions & 2 deletions source/extensions/common/wasm/well_known_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ namespace Wasm {
*/
class WasmRuntimeValues {
public:
// V8 (https://v8.dev) WASM VM.
const std::string v8 = "envoy.wasm.runtime.v8";
// WAVM (https://github.com/WAVM/WAVM) Wasm VM.
const std::string Wavm = "envoy.wasm.runtime.wavm";
// Null sandbox: modules must be compiled into envoy and registered name is given in the
// DataSource.inline_string.
const std::string Null = "envoy.wasm.runtime.null";
// V8-based (https://v8.dev) WebAssembly runtime.
const std::string V8 = "envoy.wasm.runtime.v8";

// Filter state name
const std::string FilterState = "envoy.wasm";
Expand Down
5 changes: 4 additions & 1 deletion test/extensions/common/wasm/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ licenses(["notice"]) # Apache 2
load(
"//bazel:envoy_build_system.bzl",
"envoy_cc_test",
"envoy_cc_test_library",
"envoy_package",
)

Expand All @@ -12,8 +11,12 @@ envoy_package()
envoy_cc_test(
name = "wasm_vm_test",
srcs = ["wasm_vm_test.cc"],
data = [
"//test/extensions/common/wasm/test_data:modules",
],
deps = [
"//source/extensions/common/wasm:wasm_vm_lib",
"//test/test_common:environment_lib",
"//test/test_common:utility_lib",
],
)
13 changes: 13 additions & 0 deletions test/extensions/common/wasm/test_data/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
licenses(["notice"]) # Apache 2

load(
"//bazel:envoy_build_system.bzl",
"envoy_package",
)

envoy_package()

filegroup(
name = "modules",
srcs = glob(["*.wasm"]),
)
33 changes: 33 additions & 0 deletions test/extensions/common/wasm/test_data/test_rust.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Build using:
// $ rustc -C lto -C opt-level=3 -C panic=abort -C link-arg=-S -C link-arg=-zstack-size=32768 --crate-type cdylib --target wasm32-unknown-unknown test_rust.rs

// Import functions exported from the host environment.
extern "C" {
fn pong(value: u32);
fn random() -> u32;
}

#[no_mangle]
extern "C" fn ping(value: u32) {
unsafe { pong(value) }
}

#[no_mangle]
extern "C" fn lucky(number: u32) -> bool {
unsafe { number == random() }
}

#[no_mangle]
extern "C" fn sum(a: u32, b: u32, c: u32) -> u32 {
a + b + c
}

#[no_mangle]
extern "C" fn div(a: u32, b: u32) -> u32 {
a / b
}

#[no_mangle]
extern "C" fn abort() {
panic!("abort")
}
Binary file not shown.
Loading