diff --git a/BUILD b/BUILD index 5beba11c..98270aa5 100644 --- a/BUILD +++ b/BUILD @@ -127,13 +127,39 @@ cc_library( ], ) -cc_library( - name = "wasmtime_lib", +genrule( + name = "prefixed_wasmtime_sources", srcs = [ - "src/common/types.h", "src/wasmtime/types.h", "src/wasmtime/wasmtime.cc", ], + outs = [ + "src/wasmtime/prefixed_types.h", + "src/wasmtime/prefixed_wasmtime.cc", + ], + cmd = """ + for file in $(SRCS); do + sed -e 's/wasm_/wasmtime_wasm_/g' \ + -e 's/wasmtime\\/types.h/wasmtime\\/prefixed_types.h/g' \ + $$file >$(@D)/$$(dirname $$file)/prefixed_$$(basename $$file) + done + """, +) + +cc_library( + name = "wasmtime_lib", + srcs = [ + "src/common/types.h", + ] + select({ + "@proxy_wasm_cpp_host//bazel:multiengine": [ + "src/wasmtime/prefixed_types.h", + "src/wasmtime/prefixed_wasmtime.cc", + ], + "//conditions:default": [ + "src/wasmtime/types.h", + "src/wasmtime/wasmtime.cc", + ], + }), hdrs = ["include/proxy-wasm/wasmtime.h"], defines = [ "PROXY_WASM_HAS_RUNTIME_WASMTIME", @@ -141,8 +167,14 @@ cc_library( ], deps = [ ":wasm_vm_headers", - "//external:wasmtime", - ], + ] + select({ + "@proxy_wasm_cpp_host//bazel:multiengine": [ + "//external:prefixed_wasmtime", + ], + "//conditions:default": [ + "//external:wasmtime", + ], + }), ) cc_library( diff --git a/bazel/BUILD b/bazel/BUILD index cb242058..2a2f41af 100644 --- a/bazel/BUILD +++ b/bazel/BUILD @@ -20,6 +20,11 @@ config_setting( values = {"define": "engine=wavm"}, ) +config_setting( + name = "multiengine", + values = {"define": "engine=multi"}, +) + config_setting( name = "requested_crypto_system", values = {"define": "crypto=system"}, diff --git a/bazel/external/v8.patch b/bazel/external/v8.patch index b13fad25..52af7b6a 100644 --- a/bazel/external/v8.patch +++ b/bazel/external/v8.patch @@ -1,7 +1,8 @@ -# Disable pointer compression (limits the maximum number of WasmVMs). +# 1. Disable pointer compression (limits the maximum number of WasmVMs). +# 2. Don't expose Wasm C API (only Wasm C++ API). diff --git a/BUILD.bazel b/BUILD.bazel -index 1cc0121e60..4947c1dba2 100644 +index 5fb10d3940..a19930d36e 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -161,7 +161,7 @@ v8_int( @@ -13,3 +14,22 @@ index 1cc0121e60..4947c1dba2 100644 ) # Default setting for v8_enable_pointer_compression. +diff --git a/src/wasm/c-api.cc b/src/wasm/c-api.cc +index ce3f569fd5..dc8a4c4f6a 100644 +--- a/src/wasm/c-api.cc ++++ b/src/wasm/c-api.cc +@@ -2238,6 +2238,8 @@ auto Instance::exports() const -> ownvec { + + } // namespace wasm + ++#if 0 ++ + // BEGIN FILE wasm-c.cc + + extern "C" { +@@ -3257,3 +3259,5 @@ wasm_instance_t* wasm_frame_instance(const wasm_frame_t* frame) { + #undef WASM_DEFINE_SHARABLE_REF + + } // extern "C" ++ ++#endif diff --git a/bazel/external/wasm-c-api.BUILD b/bazel/external/wasm-c-api.BUILD index 5d4be94f..e8fc1862 100644 --- a/bazel/external/wasm-c-api.BUILD +++ b/bazel/external/wasm-c-api.BUILD @@ -14,3 +14,48 @@ cc_library( "@com_github_bytecodealliance_wasmtime//:rust_c_api", ], ) + +genrule( + name = "prefixed_wasmtime_c_api_headers", + srcs = [ + "include/wasm.h", + ], + outs = [ + "wasmtime/include/wasm.h", + ], + cmd = """ + sed -e 's/\\ wasm_/\\ wasmtime_wasm_/g' \ + -e 's/\\*wasm_/\\*wasmtime_wasm_/g' \ + -e 's/(wasm_/(wasmtime_wasm_/g' \ + $(<) >$@ + """, +) + +genrule( + name = "prefixed_wasmtime_c_api_lib", + srcs = [ + "@com_github_bytecodealliance_wasmtime//:rust_c_api", + ], + outs = [ + "prefixed_wasmtime_c_api.a", + ], + cmd = """ + for symbol in $$(nm -P $(<) 2>/dev/null | grep -E ^_?wasm_ | cut -d" " -f1); do + echo $$symbol | sed -r 's/^(_?)(wasm_[a-z_]+)$$/\\1\\2 \\1wasmtime_\\2/' >>prefixed + done + # This should be OBJCOPY, but bazel-zig-cc doesn't define it. + objcopy --redefine-syms=prefixed $(<) $@ + """, + toolchains = ["@bazel_tools//tools/cpp:current_cc_toolchain"], +) + +cc_library( + name = "prefixed_wasmtime_lib", + srcs = [ + ":prefixed_wasmtime_c_api_lib", + ], + hdrs = [ + ":prefixed_wasmtime_c_api_headers", + ], + linkstatic = 1, +) diff --git a/bazel/repositories.bzl b/bazel/repositories.bzl index 057c65fb..30f35f89 100644 --- a/bazel/repositories.bzl +++ b/bazel/repositories.bzl @@ -192,6 +192,11 @@ def proxy_wasm_cpp_host_repositories(): actual = "@com_github_webassembly_wasm_c_api//:wasmtime_lib", ) + native.bind( + name = "prefixed_wasmtime", + actual = "@com_github_webassembly_wasm_c_api//:prefixed_wasmtime_lib", + ) + # WAVM with dependencies. maybe( diff --git a/bazel/select.bzl b/bazel/select.bzl index feff9ab8..578acf8d 100644 --- a/bazel/select.bzl +++ b/bazel/select.bzl @@ -15,23 +15,27 @@ def proxy_wasm_select_engine_v8(xs): return select({ "@proxy_wasm_cpp_host//bazel:engine_v8": xs, + "@proxy_wasm_cpp_host//bazel:multiengine": xs, "//conditions:default": [], }) def proxy_wasm_select_engine_wamr(xs): return select({ "@proxy_wasm_cpp_host//bazel:engine_wamr": xs, + "@proxy_wasm_cpp_host//bazel:multiengine": xs, "//conditions:default": [], }) def proxy_wasm_select_engine_wasmtime(xs): return select({ "@proxy_wasm_cpp_host//bazel:engine_wasmtime": xs, + "@proxy_wasm_cpp_host//bazel:multiengine": xs, "//conditions:default": [], }) def proxy_wasm_select_engine_wavm(xs): return select({ "@proxy_wasm_cpp_host//bazel:engine_wavm": xs, + "@proxy_wasm_cpp_host//bazel:multiengine": xs, "//conditions:default": [], }) diff --git a/src/wasmtime/wasmtime.cc b/src/wasmtime/wasmtime.cc index a57f778b..b8bba1e1 100644 --- a/src/wasmtime/wasmtime.cc +++ b/src/wasmtime/wasmtime.cc @@ -24,7 +24,6 @@ #include #include -#include "include/proxy-wasm/wasm_vm.h" #include "src/wasmtime/types.h" #include "wasmtime/include/wasm.h"