Skip to content

Commit 7f6a716

Browse files
authored
Wasm upstreaming: required bazel/* support. (envoyproxy#12116)
Merge in changes from envoyproxy/envoy-wasm in the bazel/ directory as part of upstreaming Wasm. Risk Level: Low Testing: Unit tests pass. Docs Changes: N/A Release Notes: N/A Signed-off-by: John Plevyak <[email protected]>
1 parent c1d116e commit 7f6a716

10 files changed

+280
-6
lines changed

bazel/antlr.patch

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
diff --git a/runtime/Cpp/runtime/src/atn/ATNDeserializer.cpp b/runtime/Cpp/runtime/src/atn/ATNDeserializer.cpp
2+
index c6cceda13..e86533759 100755
3+
--- a/runtime/Cpp/runtime/src/atn/ATNDeserializer.cpp
4+
+++ b/runtime/Cpp/runtime/src/atn/ATNDeserializer.cpp
5+
@@ -104,7 +104,7 @@ void deserializeSets(
6+
7+
}
8+
9+
-ATNDeserializer::ATNDeserializer(): ATNDeserializer(ATNDeserializationOptions::getDefaultOptions()) {
10+
+ATNDeserializer::ATNDeserializer(): ATNDeserializer(ATNDeserializationOptions()) {
11+
}
12+
13+
ATNDeserializer::ATNDeserializer(const ATNDeserializationOptions& dso): deserializationOptions(dso) {
14+
diff --git a/runtime/Cpp/runtime/src/atn/LexerATNSimulator.cpp b/runtime/Cpp/runtime/src/atn/LexerATNSimulator.cpp
15+
index 827c3d59f..62914cf55 100755
16+
--- a/runtime/Cpp/runtime/src/atn/LexerATNSimulator.cpp
17+
+++ b/runtime/Cpp/runtime/src/atn/LexerATNSimulator.cpp
18+
@@ -69,7 +69,7 @@ void LexerATNSimulator::copyState(LexerATNSimulator *simulator) {
19+
}
20+
21+
size_t LexerATNSimulator::match(CharStream *input, size_t mode) {
22+
- match_calls++;
23+
+ // match_calls++;
24+
_mode = mode;
25+
ssize_t mark = input->mark();
26+

bazel/dependency_imports.bzl

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ load("@build_bazel_rules_apple//apple:repositories.bzl", "apple_rules_dependenci
77
load("@upb//bazel:repository_defs.bzl", upb_bazel_version_repository = "bazel_version_repository")
88
load("@config_validation_pip3//:requirements.bzl", config_validation_pip_install = "pip_install")
99
load("@protodoc_pip3//:requirements.bzl", protodoc_pip_install = "pip_install")
10+
load("@rules_antlr//antlr:deps.bzl", "antlr_dependencies")
1011

1112
# go version for rules_go
1213
GO_VERSION = "1.14.4"
@@ -19,6 +20,7 @@ def envoy_dependency_imports(go_version = GO_VERSION):
1920
gazelle_dependencies()
2021
apple_rules_dependencies()
2122
upb_bazel_version_repository(name = "upb_bazel_version")
23+
antlr_dependencies(471)
2224

2325
custom_exec_properties(
2426
name = "envoy_large_machine_exec_property",
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
load("@rules_cc//cc:defs.bzl", "cc_library")
2+
3+
licenses(["notice"]) # Apache 2
4+
5+
package(default_visibility = ["//visibility:public"])
6+
7+
cc_library(
8+
name = "include",
9+
hdrs = glob(["include/proxy-wasm/**/*.h"]),
10+
deps = [
11+
"@proxy_wasm_cpp_sdk//:common_lib",
12+
],
13+
)
14+
15+
cc_library(
16+
name = "lib",
17+
srcs = glob(
18+
[
19+
"src/**/*.h",
20+
"src/**/*.cc",
21+
],
22+
exclude = ["src/**/wavm*"],
23+
),
24+
copts = ["-std=c++14"],
25+
deps = [
26+
":include",
27+
"//external:abseil_flat_hash_map",
28+
"//external:abseil_optional",
29+
"//external:abseil_strings",
30+
"//external:protobuf",
31+
"//external:ssl",
32+
"//external:wee8",
33+
"//external:zlib",
34+
"@proxy_wasm_cpp_sdk//:api_lib",
35+
"@proxy_wasm_cpp_sdk//:common_lib",
36+
],
37+
)

bazel/external/wee8.genrule_cmd

+26-6
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,17 @@ if [[ $${ENVOY_TSAN-} == "1" ]]; then
5151
WEE8_BUILD_ARGS+=" is_tsan=true"
5252
fi
5353

54-
# Release build.
55-
WEE8_BUILD_ARGS+=" is_debug=false"
54+
# Debug/release build.
55+
if [[ $(COMPILATION_MODE) == "dbg" && $${ENVOY_UBSAN_VPTR-} != "1" && $${ENVOY_MSAN-} != "1" && $${ENVOY_TSAN-} != "1" ]]; then
56+
WEE8_BUILD_ARGS+=" is_debug=true"
57+
WEE8_BUILD_ARGS+=" v8_symbol_level=2"
58+
WEE8_BUILD_ARGS+=" v8_optimized_debug=false"
59+
else
60+
WEE8_BUILD_ARGS+=" is_debug=false"
61+
WEE8_BUILD_ARGS+=" v8_symbol_level=1"
62+
WEE8_BUILD_ARGS+=" v8_enable_handle_zapping=false"
63+
fi
64+
5665
# Clang or not Clang, that is the question.
5766
WEE8_BUILD_ARGS+=" is_clang=$$IS_CLANG"
5867
# Hack to disable bleeding-edge compiler flags.
@@ -81,17 +90,28 @@ if [[ `uname -m` == "aarch64" ]]; then
8190
fi
8291

8392
# Build wee8.
84-
if [[ "$$(uname -s)" == "Darwin" ]]; then
93+
if [[ -f /etc/centos-release ]] && [[ $$(cat /etc/centos-release) =~ "CentOS Linux release 7" ]] && [[ -x "$$(command -v gn)" ]]; then
94+
# Using system default gn tools
95+
# This is done only for CentOS 7, as it has an old version of GLIBC which is otherwise incompatible
96+
gn=$$(command -v gn)
97+
elif [[ "$$(uname -s)" == "Darwin" ]]; then
8598
gn=buildtools/mac/gn
86-
ninja=third_party/depot_tools/ninja
8799
elif [[ "$$(uname -s)-$$(uname -m)" == "Linux-x86_64" ]]; then
88100
gn=buildtools/linux64/gn
89-
ninja=third_party/depot_tools/ninja
90101
else
91-
# Using system default ninja & gn tools
102+
# Using system default gn tools
92103
gn=$$(command -v gn)
104+
fi
105+
106+
if [[ "$$(uname -s)" == "Darwin" ]]; then
107+
ninja=third_party/depot_tools/ninja
108+
elif [[ "$$(uname -s)-$$(uname -m)" == "Linux-x86_64" ]]; then
109+
ninja=third_party/depot_tools/ninja
110+
else
111+
# Using system default ninja tools
93112
ninja=$$(command -v ninja)
94113
fi
114+
95115
"$$gn" gen out/wee8 --args="$$WEE8_BUILD_ARGS"
96116
"$$ninja" -C out/wee8 wee8
97117

bazel/repositories.bzl

+38
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,12 @@ def envoy_dependencies(skip_targets = []):
191191
_io_opentracing_cpp()
192192
_net_zlib()
193193
_upb()
194+
_proxy_wasm_cpp_sdk()
195+
_proxy_wasm_cpp_host()
196+
_emscripten_toolchain()
194197
_repository_impl("com_googlesource_code_re2")
195198
_com_google_cel_cpp()
199+
_repository_impl("com_github_google_flatbuffers")
196200
_repository_impl("bazel_toolchains")
197201
_repository_impl("bazel_compdb")
198202
_repository_impl("envoy_build_tools")
@@ -379,6 +383,24 @@ def _net_zlib():
379383

380384
def _com_google_cel_cpp():
381385
_repository_impl("com_google_cel_cpp")
386+
_repository_impl("rules_antlr")
387+
location = _get_location("antlr4_runtimes")
388+
http_archive(
389+
name = "antlr4_runtimes",
390+
build_file_content = """
391+
package(default_visibility = ["//visibility:public"])
392+
cc_library(
393+
name = "cpp",
394+
srcs = glob(["runtime/Cpp/runtime/src/**/*.cpp"]),
395+
hdrs = glob(["runtime/Cpp/runtime/src/**/*.h"]),
396+
includes = ["runtime/Cpp/runtime/src"],
397+
)
398+
""",
399+
patch_args = ["-p1"],
400+
# Patches ASAN violation of initialization fiasco
401+
patches = ["@envoy//bazel:antlr.patch"],
402+
**location
403+
)
382404

383405
def _com_github_nghttp2_nghttp2():
384406
location = _get_location("com_github_nghttp2_nghttp2")
@@ -770,6 +792,22 @@ def _upb():
770792
actual = "@upb//:upb",
771793
)
772794

795+
def _proxy_wasm_cpp_sdk():
796+
_repository_impl(name = "proxy_wasm_cpp_sdk")
797+
798+
def _proxy_wasm_cpp_host():
799+
_repository_impl(
800+
name = "proxy_wasm_cpp_host",
801+
build_file = "@envoy//bazel/external:proxy_wasm_cpp_host.BUILD",
802+
)
803+
804+
def _emscripten_toolchain():
805+
_repository_impl(
806+
name = "emscripten_toolchain",
807+
build_file_content = BUILD_ALL_CONTENT,
808+
patch_cmds = REPOSITORY_LOCATIONS["emscripten_toolchain"]["patch_cmds"],
809+
)
810+
773811
def _com_github_google_jwt_verify():
774812
_repository_impl("com_github_google_jwt_verify")
775813

bazel/repository_locations.bzl

+43
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,13 @@ DEPENDENCY_REPOSITORIES = dict(
429429
use_category = ["dataplane"],
430430
cpe = "N/A",
431431
),
432+
com_github_google_flatbuffers = dict(
433+
sha256 = "b8efbc25721e76780752bad775a97c3f77a0250271e2db37fc747b20e8b0f24a",
434+
strip_prefix = "flatbuffers-a83caf5910644ba1c421c002ef68e42f21c15f9f",
435+
urls = ["https://github.com/google/flatbuffers/archive/a83caf5910644ba1c421c002ef68e42f21c15f9f.tar.gz"],
436+
use_category = ["dataplane"],
437+
cpe = "N/A",
438+
),
432439
com_googlesource_code_re2 = dict(
433440
sha256 = "2e9489a31ae007c81e90e8ec8a15d62d58a9c18d4fd1603f6441ef248556b41f",
434441
strip_prefix = "re2-2020-07-06",
@@ -481,4 +488,40 @@ DEPENDENCY_REPOSITORIES = dict(
481488
use_category = ["dataplane"],
482489
cpe = "cpe:2.3:a:icu-project:international_components_for_unicode",
483490
),
491+
proxy_wasm_cpp_sdk = dict(
492+
sha256 = "7d9e1f2e299215ed3e5fa8c8149740872b1100cfe3230fc639f967d9dcfd812e",
493+
strip_prefix = "proxy-wasm-cpp-sdk-5cec30b448975e1fd3f4117311f0957309df5cb0",
494+
urls = ["https://github.com/proxy-wasm/proxy-wasm-cpp-sdk/archive/5cec30b448975e1fd3f4117311f0957309df5cb0.tar.gz"],
495+
use_category = ["dataplane"],
496+
cpe = "N/A",
497+
),
498+
proxy_wasm_cpp_host = dict(
499+
sha256 = "494d3f81156b92bac640c26000497fbf3a7b1bc35f9789594280450c6e5d8129",
500+
strip_prefix = "proxy-wasm-cpp-host-928db4d79ec7b90aea3ad13ea5df36dc60c9c31d",
501+
urls = ["https://github.com/proxy-wasm/proxy-wasm-cpp-host/archive/928db4d79ec7b90aea3ad13ea5df36dc60c9c31d.tar.gz"],
502+
use_category = ["dataplane"],
503+
cpe = "N/A",
504+
),
505+
emscripten_toolchain = dict(
506+
sha256 = "2bdbee6947e32ad1e03cd075b48fda493ab16157b2b0225b445222cd528e1843",
507+
patch_cmds = [
508+
"./emsdk install 1.39.19-upstream",
509+
"./emsdk activate --embedded 1.39.19-upstream",
510+
],
511+
strip_prefix = "emsdk-dec8a63594753fe5f4ad3b47850bf64d66c14a4e",
512+
urls = ["https://github.com/emscripten-core/emsdk/archive/dec8a63594753fe5f4ad3b47850bf64d66c14a4e.tar.gz"],
513+
use_category = ["build"],
514+
),
515+
rules_antlr = dict(
516+
sha256 = "7249d1569293d9b239e23c65f6b4c81a07da921738bde0dfeb231ed98be40429",
517+
strip_prefix = "rules_antlr-3cc2f9502a54ceb7b79b37383316b23c4da66f9a",
518+
urls = ["https://github.com/marcohu/rules_antlr/archive/3cc2f9502a54ceb7b79b37383316b23c4da66f9a.tar.gz"],
519+
use_category = ["build"],
520+
),
521+
antlr4_runtimes = dict(
522+
sha256 = "4d0714f441333a63e50031c9e8e4890c78f3d21e053d46416949803e122a6574",
523+
strip_prefix = "antlr4-4.7.1",
524+
urls = ["https://github.com/antlr/antlr4/archive/4.7.1.tar.gz"],
525+
use_category = ["build"],
526+
),
484527
)

bazel/wasm/BUILD

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
licenses(["notice"]) # Apache 2

bazel/wasm/wasm.bzl

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
load("@rules_cc//cc:defs.bzl", "cc_binary")
2+
3+
def _wasm_transition_impl(settings, attr):
4+
return {
5+
"//command_line_option:cpu": "wasm32",
6+
"//command_line_option:crosstool_top": "@proxy_wasm_cpp_sdk//toolchain:emscripten",
7+
8+
# Overriding copt/cxxopt/linkopt to prevent sanitizers/coverage options leak
9+
# into WASM build configuration
10+
"//command_line_option:copt": [],
11+
"//command_line_option:cxxopt": [],
12+
"//command_line_option:linkopt": [],
13+
"//command_line_option:collect_code_coverage": "false",
14+
}
15+
16+
wasm_transition = transition(
17+
implementation = _wasm_transition_impl,
18+
inputs = [],
19+
outputs = [
20+
"//command_line_option:cpu",
21+
"//command_line_option:crosstool_top",
22+
"//command_line_option:copt",
23+
"//command_line_option:cxxopt",
24+
"//command_line_option:linkopt",
25+
"//command_line_option:collect_code_coverage",
26+
],
27+
)
28+
29+
def _wasm_binary_impl(ctx):
30+
out = ctx.actions.declare_file(ctx.label.name)
31+
ctx.actions.run_shell(
32+
command = 'cp "{}" "{}"'.format(ctx.files.binary[0].path, out.path),
33+
outputs = [out],
34+
inputs = ctx.files.binary,
35+
)
36+
37+
return [DefaultInfo(runfiles = ctx.runfiles([out]))]
38+
39+
# WASM binary rule implementation.
40+
# This copies the binary specified in binary attribute in WASM configuration to
41+
# target configuration, so a binary in non-WASM configuration can depend on them.
42+
wasm_binary = rule(
43+
implementation = _wasm_binary_impl,
44+
attrs = {
45+
"binary": attr.label(mandatory = True, cfg = wasm_transition),
46+
"_whitelist_function_transition": attr.label(default = "@bazel_tools//tools/whitelists/function_transition_whitelist"),
47+
},
48+
)
49+
50+
def wasm_cc_binary(name, **kwargs):
51+
wasm_name = "_wasm_" + name
52+
kwargs.setdefault("additional_linker_inputs", ["@proxy_wasm_cpp_sdk//:jslib"])
53+
kwargs.setdefault("linkopts", ["--js-library external/proxy_wasm_cpp_sdk/proxy_wasm_intrinsics.js"])
54+
kwargs.setdefault("visibility", ["//visibility:public"])
55+
cc_binary(
56+
name = wasm_name,
57+
# Adding manual tag it won't be built in non-WASM (e.g. x86_64 config)
58+
# when an wildcard is specified, but it will be built in WASM configuration
59+
# when the wasm_binary below is built.
60+
tags = ["manual"],
61+
**kwargs
62+
)
63+
64+
wasm_binary(
65+
name = name,
66+
binary = ":" + wasm_name,
67+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
all: test_rust.wasm
2+
3+
test_rust.wasm: test_rust.rs
4+
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
5+
../../../../../bazel-bin/test/tools/wee8_compile/wee8_compile_tool test_rust.wasm test_rust.wasm

test/extensions/common/wasm/wasm_vm_test.cc

+35
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,13 @@ TEST_P(WasmVmTest, V8BadCode) {
150150
}
151151

152152
TEST_P(WasmVmTest, V8Code) {
153+
#ifndef NDEBUG
154+
// Do not execute pre-compilation tests in debug mode because V8 will fail to load because the
155+
// flags do not match. TODO: restore this test when the rust toolchain is integrated.
156+
if (GetParam() == 1) {
157+
return;
158+
}
159+
#endif
153160
auto wasm_vm = createWasmVm("envoy.wasm.runtime.v8", scope_);
154161
ASSERT_TRUE(wasm_vm != nullptr);
155162
EXPECT_TRUE(wasm_vm->runtime() == "envoy.wasm.runtime.v8");
@@ -170,6 +177,13 @@ TEST_P(WasmVmTest, V8Code) {
170177
}
171178

172179
TEST_P(WasmVmTest, V8BadHostFunctions) {
180+
#ifndef NDEBUG
181+
// Do not execute pre-compilation tests in debug mode because V8 will fail to load because the
182+
// flags do not match. TODO: restore this test when the rust toolchain is integrated.
183+
if (GetParam() == 1) {
184+
return;
185+
}
186+
#endif
173187
auto wasm_vm = createWasmVm("envoy.wasm.runtime.v8", scope_);
174188
ASSERT_TRUE(wasm_vm != nullptr);
175189

@@ -198,6 +212,13 @@ TEST_P(WasmVmTest, V8BadHostFunctions) {
198212
}
199213

200214
TEST_P(WasmVmTest, V8BadModuleFunctions) {
215+
#ifndef NDEBUG
216+
// Do not execute pre-compilation tests in debug mode because V8 will fail to load because the
217+
// flags do not match. TODO: restore this test when the rust toolchain is integrated.
218+
if (GetParam() == 1) {
219+
return;
220+
}
221+
#endif
201222
auto wasm_vm = createWasmVm("envoy.wasm.runtime.v8", scope_);
202223
ASSERT_TRUE(wasm_vm != nullptr);
203224

@@ -226,6 +247,13 @@ TEST_P(WasmVmTest, V8BadModuleFunctions) {
226247
}
227248

228249
TEST_P(WasmVmTest, V8FunctionCalls) {
250+
#ifndef NDEBUG
251+
// Do not execute pre-compilation tests in debug mode because V8 will fail to load because the
252+
// flags do not match. TODO: restore this test when the rust toolchain is integrated.
253+
if (GetParam() == 1) {
254+
return;
255+
}
256+
#endif
229257
auto wasm_vm = createWasmVm("envoy.wasm.runtime.v8", scope_);
230258
ASSERT_TRUE(wasm_vm != nullptr);
231259

@@ -264,6 +292,13 @@ TEST_P(WasmVmTest, V8FunctionCalls) {
264292
}
265293

266294
TEST_P(WasmVmTest, V8Memory) {
295+
#ifndef NDEBUG
296+
// Do not execute pre-compilation tests in debug mode because V8 will fail to load because the
297+
// flags do not match. TODO: restore this test when the rust toolchain is integrated.
298+
if (GetParam() == 1) {
299+
return;
300+
}
301+
#endif
267302
auto wasm_vm = createWasmVm("envoy.wasm.runtime.v8", scope_);
268303
ASSERT_TRUE(wasm_vm != nullptr);
269304

0 commit comments

Comments
 (0)