From bc62d296745bd48605302542eec4ccdde9b2256c Mon Sep 17 00:00:00 2001 From: Piotr Sikora Date: Tue, 12 Jul 2022 13:46:52 -0700 Subject: [PATCH 1/4] wasm: update V8 to v10.4.132.18. Signed-off-by: Piotr Sikora --- bazel/repository_locations.bzl | 18 +- bazel/v8.patch | 294 +++++++++++++++++++++++- test/tools/wee8_compile/wee8_compile.cc | 7 + 3 files changed, 300 insertions(+), 19 deletions(-) diff --git a/bazel/repository_locations.bzl b/bazel/repository_locations.bzl index 887d44ce1a74e..98e840de0c444 100644 --- a/bazel/repository_locations.bzl +++ b/bazel/repository_locations.bzl @@ -891,13 +891,13 @@ REPOSITORY_LOCATIONS_SPEC = dict( project_desc = "Google’s open source high-performance JavaScript and WebAssembly engine, written in C++", project_url = "https://v8.dev", # NOTE: Update together with com_googlesource_chromium_base_trace_event_common and com_googlesource_chromium_zlib. - version = "10.0.139.6", + version = "10.4.132.18", # Static snapshot created using https://storage.googleapis.com/envoyproxy-wee8/wee8-fetch-deps.sh. - sha256 = "3585ebc8db95e61b65260b3d141ed5392b843bd066744fa62ce92dab3708e2f8", + sha256 = "154223731091f531a3cb1a95ab9c14225983891240220a9e232c729358badd2c", urls = ["https://storage.googleapis.com/envoyproxy-wee8/v8-{version}.tar.gz"], use_category = ["dataplane_ext"], extensions = ["envoy.wasm.runtime.v8"], - release_date = "2022-03-07", + release_date = "2022-07-11", cpe = "cpe:2.3:a:google:v8:*", ), com_googlesource_chromium_base_trace_event_common = dict( @@ -921,13 +921,13 @@ REPOSITORY_LOCATIONS_SPEC = dict( project_url = "https://chromium.googlesource.com/chromium/src/third_party/zlib/", # NOTE: Update together with v8 and com_googlesource_chromium_base_trace_event_common. # Use version and sha256 from https://storage.googleapis.com/envoyproxy-wee8/v8--deps.sha256. - version = "9538f4194f6e5eff1bd59f2396ed9d05b1a8d801", + version = "64bbf988543996eb8df9a86877b32917187eba8f", # Static snapshot created using https://storage.googleapis.com/envoyproxy-wee8/wee8-fetch-deps.sh. - sha256 = "7045c6808267f3e803e3196fa06346647c2a060cfd1a2f96ef6325fba6eaa956", + sha256 = "6e70bbb25ad1c567e3b44f4ad76f50fcc8fb7d34c6e2e8858e033ffd579ac1af", urls = ["https://storage.googleapis.com/envoyproxy-wee8/chromium-zlib-{version}.tar.gz"], use_category = ["dataplane_ext"], extensions = ["envoy.wasm.runtime.v8"], - release_date = "2022-02-15", + release_date = "2022-05-31", cpe = "N/A", ), com_github_google_quiche = dict( @@ -1119,8 +1119,8 @@ REPOSITORY_LOCATIONS_SPEC = dict( project_name = "WebAssembly for Proxies (C++ host implementation)", project_desc = "WebAssembly for Proxies (C++ host implementation)", project_url = "https://github.com/proxy-wasm/proxy-wasm-cpp-host", - version = "694a0b073912ff3bd00b6ca70d16ca43b2aebbf2", - sha256 = "d8b8ed52c48a4847a76c2192095362972e4ce07b95311f36da844128d5b35ab5", + version = "bcd1c4a698ca2cee79959e150ba3b02ffa790eea", + sha256 = "b1124d3667c24d1ad3ddb8f12c3646fe9bf31a1316dd4ce50efc9f1a2f82f68a", strip_prefix = "proxy-wasm-cpp-host-{version}", urls = ["https://github.com/proxy-wasm/proxy-wasm-cpp-host/archive/{version}.tar.gz"], use_category = ["dataplane_ext"], @@ -1136,7 +1136,7 @@ REPOSITORY_LOCATIONS_SPEC = dict( "envoy.wasm.runtime.wavm", "envoy.wasm.runtime.wasmtime", ], - release_date = "2022-04-07", + release_date = "2022-07-12", cpe = "N/A", ), proxy_wasm_rust_sdk = dict( diff --git a/bazel/v8.patch b/bazel/v8.patch index a55ec77a3c933..ba8c7f41bf24e 100644 --- a/bazel/v8.patch +++ b/bazel/v8.patch @@ -1,8 +1,10 @@ # 1. Use already imported python dependencies. # 2. Fix the include path for //external:zlib. # 3. Add support for --define=no_debug_info=1. -# 4. Disable pointer compression (https://crbug.com/v8/12592). -# 5. Add M1 CPU support https://chromium-review.googlesource.com/c/v8/v8/+/3502848 +# 4. Disable pointer compression (limits the maximum number of WasmVMs). +# 5. Don't expose Wasm C API (only Wasm C++ API). +# 6. Fix cross-compilation (https://crrev.com/c/3735165). +# 7. Fix build errors in SIMD IndexOf/includes (https://crrev.com/c/3749192). diff --git a/BUILD.bazel b/BUILD.bazel index 13f2a5bebf..2197568c48 100644 @@ -42,14 +44,6 @@ index dee5e69cc4..d0b5a3c49a 100644 }), includes = ["include"], linkopts = select({ -@@ -383,6 +388,7 @@ def _v8_target_cpu_transition_impl(settings, attr): - "k8": "x64", - "x86_64": "x64", - "darwin": "x64", -+ "darwin_arm64": "arm64", - "darwin_x86_64": "x64", - "x64_windows": "x64", - "x86": "ia32", diff --git a/src/snapshot/snapshot-utils.cc b/src/snapshot/snapshot-utils.cc index 6db6698d7e..b56d31085f 100644 --- a/src/snapshot/snapshot-utils.cc @@ -63,3 +57,283 @@ index 6db6698d7e..b56d31085f 100644 namespace v8 { namespace internal { +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/src/execution/clobber-registers.cc b/src/execution/clobber-registers.cc +index 8f7fba765f..a7f5bf80cf 100644 +--- a/src/execution/clobber-registers.cc ++++ b/src/execution/clobber-registers.cc +@@ -5,19 +5,22 @@ + + #include "src/base/build_config.h" + +-#if V8_HOST_ARCH_ARM ++// Check both {HOST_ARCH} and {TARGET_ARCH} to disable the functionality of this ++// file for cross-compilation. The reason is that the inline assembly code below ++// does not work for cross-compilation. ++#if V8_HOST_ARCH_ARM && V8_TARGET_ARCH_ARM + #include "src/codegen/arm/register-arm.h" +-#elif V8_HOST_ARCH_ARM64 ++#elif V8_HOST_ARCH_ARM64 && V8_TARGET_ARCH_ARM64 + #include "src/codegen/arm64/register-arm64.h" +-#elif V8_HOST_ARCH_IA32 ++#elif V8_HOST_ARCH_IA32 && V8_TARGET_ARCH_IA32 + #include "src/codegen/ia32/register-ia32.h" +-#elif V8_HOST_ARCH_X64 ++#elif V8_HOST_ARCH_X64 && V8_TARGET_ARCH_X64 + #include "src/codegen/x64/register-x64.h" +-#elif V8_HOST_ARCH_LOONG64 ++#elif V8_HOST_ARCH_LOONG64 && V8_TARGET_ARCH_LOONG64 + #include "src/codegen/loong64/register-loong64.h" +-#elif V8_HOST_ARCH_MIPS ++#elif V8_HOST_ARCH_MIPS && V8_TARGET_ARCH_MIPS + #include "src/codegen/mips/register-mips.h" +-#elif V8_HOST_ARCH_MIPS64 ++#elif V8_HOST_ARCH_MIPS64 && V8_TARGET_ARCH_MIPS64 + #include "src/codegen/mips64/register-mips64.h" + #endif + +@@ -26,14 +29,15 @@ namespace internal { + + #if V8_CC_MSVC + // msvc only support inline assembly on x86 +-#if V8_HOST_ARCH_IA32 ++#if V8_HOST_ARCH_IA32 && V8_TARGET_ARCH_IA32 + #define CLOBBER_REGISTER(R) __asm xorps R, R + + #endif + + #else // !V8_CC_MSVC + +-#if V8_HOST_ARCH_X64 || V8_HOST_ARCH_IA32 ++#if (V8_HOST_ARCH_X64 && V8_TARGET_ARCH_X64) || \ ++ (V8_HOST_ARCH_IA32 && V8_TARGET_ARCH_IA32) + #define CLOBBER_REGISTER(R) \ + __asm__ volatile( \ + "xorps " \ +@@ -42,20 +46,19 @@ namespace internal { + "%%" #R :: \ + :); + +-#elif V8_HOST_ARCH_ARM64 ++#elif V8_HOST_ARCH_ARM64 && V8_TARGET_ARCH_ARM64 + #define CLOBBER_REGISTER(R) __asm__ volatile("fmov " #R ",xzr" :::); + +-#elif V8_HOST_ARCH_LOONG64 ++#elif V8_HOST_ARCH_LOONG64 && V8_TARGET_ARCH_LOONG64 + #define CLOBBER_REGISTER(R) __asm__ volatile("movgr2fr.d $" #R ",$zero" :::); + +-#elif V8_HOST_ARCH_MIPS ++#elif V8_HOST_ARCH_MIPS && V8_TARGET_ARCH_MIPS + #define CLOBBER_USE_REGISTER(R) __asm__ volatile("mtc1 $zero,$" #R :::); + +-#elif V8_HOST_ARCH_MIPS64 ++#elif V8_HOST_ARCH_MIPS64 && V8_TARGET_ARCH_MIPS64 + #define CLOBBER_USE_REGISTER(R) __asm__ volatile("dmtc1 $zero,$" #R :::); + +-#endif // V8_HOST_ARCH_X64 || V8_HOST_ARCH_IA32 || V8_HOST_ARCH_ARM64 || +- // V8_HOST_ARCH_LOONG64 || V8_HOST_ARCH_MIPS || V8_HOST_ARCH_MIPS64 ++#endif // V8_HOST_ARCH_XXX && V8_TARGET_ARCH_XXX + + #endif // V8_CC_MSVC + +diff --git a/src/objects/simd.cc b/src/objects/simd.cc +index 0a73b9c686..be6b72d157 100644 +--- a/src/objects/simd.cc ++++ b/src/objects/simd.cc +@@ -354,8 +354,13 @@ Address ArrayIndexOfIncludes(Address array_start, uintptr_t array_len, + if (reinterpret_cast(array) % sizeof(double) != 0) { + // Slow scalar search for unaligned double array. + for (; from_index < array_len; from_index++) { +- if (fixed_array.get_representation(static_cast(from_index)) == +- *reinterpret_cast(&search_num)) { ++ if (fixed_array.is_the_hole(static_cast(from_index))) { ++ // |search_num| cannot be NaN, so there is no need to check against ++ // holes. ++ continue; ++ } ++ if (fixed_array.get_scalar(static_cast(from_index)) == ++ search_num) { + return from_index; + } + } +diff --git a/src/objects/simd.cc b/src/objects/simd.cc +index d3cedfe330..0a73b9c686 100644 +--- a/src/objects/simd.cc ++++ b/src/objects/simd.cc +@@ -95,24 +95,21 @@ inline int extract_first_nonzero_index(T v) { + } + + template <> +-inline int extract_first_nonzero_index(int32x4_t v) { +- int32x4_t mask = {4, 3, 2, 1}; ++inline int extract_first_nonzero_index(uint32x4_t v) { ++ uint32x4_t mask = {4, 3, 2, 1}; + mask = vandq_u32(mask, v); + return 4 - vmaxvq_u32(mask); + } + + template <> +-inline int extract_first_nonzero_index(int64x2_t v) { +- int32x4_t mask = {2, 0, 1, 0}; // Could also be {2,2,1,1} or {0,2,0,1} +- mask = vandq_u32(mask, vreinterpretq_s32_s64(v)); ++inline int extract_first_nonzero_index(uint64x2_t v) { ++ uint32x4_t mask = {2, 0, 1, 0}; // Could also be {2,2,1,1} or {0,2,0,1} ++ mask = vandq_u32(mask, vreinterpretq_u32_u64(v)); + return 2 - vmaxvq_u32(mask); + } + +-template <> +-inline int extract_first_nonzero_index(float64x2_t v) { +- int32x4_t mask = {2, 0, 1, 0}; // Could also be {2,2,1,1} or {0,2,0,1} +- mask = vandq_u32(mask, vreinterpretq_s32_f64(v)); +- return 2 - vmaxvq_u32(mask); ++inline int32_t reinterpret_vmaxvq_u64(uint64x2_t v) { ++ return vmaxvq_u32(vreinterpretq_u32_u64(v)); + } + #endif + +@@ -204,14 +201,14 @@ inline uintptr_t fast_search_noavx(T* array, uintptr_t array_len, + } + #elif defined(NEON64) + if constexpr (std::is_same::value) { +- VECTORIZED_LOOP_Neon(int32x4_t, int32x4_t, vdupq_n_u32, vceqq_u32, ++ VECTORIZED_LOOP_Neon(uint32x4_t, uint32x4_t, vdupq_n_u32, vceqq_u32, + vmaxvq_u32) + } else if constexpr (std::is_same::value) { +- VECTORIZED_LOOP_Neon(int64x2_t, int64x2_t, vdupq_n_u64, vceqq_u64, +- vmaxvq_u32) ++ VECTORIZED_LOOP_Neon(uint64x2_t, uint64x2_t, vdupq_n_u64, vceqq_u64, ++ reinterpret_vmaxvq_u64) + } else if constexpr (std::is_same::value) { +- VECTORIZED_LOOP_Neon(float64x2_t, float64x2_t, vdupq_n_f64, vceqq_f64, +- vmaxvq_f64) ++ VECTORIZED_LOOP_Neon(float64x2_t, uint64x2_t, vdupq_n_f64, vceqq_f64, ++ reinterpret_vmaxvq_u64) + } + #else + UNREACHABLE(); +diff --git a/src/objects/simd.cc b/src/objects/simd.cc +index be6b72d157..a71968fd10 100644 +--- a/src/objects/simd.cc ++++ b/src/objects/simd.cc +@@ -148,9 +148,14 @@ inline int32_t reinterpret_vmaxvq_u64(uint64x2_t v) { + template + inline uintptr_t fast_search_noavx(T* array, uintptr_t array_len, + uintptr_t index, T search_element) { +- static_assert(std::is_same::value || +- std::is_same::value || +- std::is_same::value); ++ static constexpr bool is_uint32 = ++ sizeof(T) == sizeof(uint32_t) && std::is_integral::value; ++ static constexpr bool is_uint64 = ++ sizeof(T) == sizeof(uint64_t) && std::is_integral::value; ++ static constexpr bool is_double = ++ sizeof(T) == sizeof(double) && std::is_floating_point::value; ++ ++ static_assert(is_uint32 || is_uint64 || is_double); + + #if !(defined(__SSE3__) || defined(NEON64)) + // No SIMD available. +@@ -178,14 +183,14 @@ inline uintptr_t fast_search_noavx(T* array, uintptr_t array_len, + + // Inserting one of the vectorized loop + #ifdef __SSE3__ +- if constexpr (std::is_same::value) { ++ if constexpr (is_uint32) { + #define MOVEMASK(x) _mm_movemask_ps(_mm_castsi128_ps(x)) + #define EXTRACT(x) base::bits::CountTrailingZeros32(x) + VECTORIZED_LOOP_x86(__m128i, __m128i, _mm_set1_epi32, _mm_cmpeq_epi32, + MOVEMASK, EXTRACT) + #undef MOVEMASK + #undef EXTRACT +- } else if constexpr (std::is_same::value) { ++ } else if constexpr (is_uint64) { + #define SET1(x) _mm_castsi128_ps(_mm_set1_epi64x(x)) + #define CMP(a, b) _mm_cmpeq_pd(_mm_castps_pd(a), _mm_castps_pd(b)) + #define EXTRACT(x) base::bits::CountTrailingZeros32(x) +@@ -193,20 +198,20 @@ inline uintptr_t fast_search_noavx(T* array, uintptr_t array_len, + #undef SET1 + #undef CMP + #undef EXTRACT +- } else if constexpr (std::is_same::value) { ++ } else if constexpr (is_double) { + #define EXTRACT(x) base::bits::CountTrailingZeros32(x) + VECTORIZED_LOOP_x86(__m128d, __m128d, _mm_set1_pd, _mm_cmpeq_pd, + _mm_movemask_pd, EXTRACT) + #undef EXTRACT + } + #elif defined(NEON64) +- if constexpr (std::is_same::value) { ++ if constexpr (is_uint32) { + VECTORIZED_LOOP_Neon(uint32x4_t, uint32x4_t, vdupq_n_u32, vceqq_u32, + vmaxvq_u32) +- } else if constexpr (std::is_same::value) { ++ } else if constexpr (is_uint64) { + VECTORIZED_LOOP_Neon(uint64x2_t, uint64x2_t, vdupq_n_u64, vceqq_u64, + reinterpret_vmaxvq_u64) +- } else if constexpr (std::is_same::value) { ++ } else if constexpr (is_double) { + VECTORIZED_LOOP_Neon(float64x2_t, uint64x2_t, vdupq_n_f64, vceqq_f64, + reinterpret_vmaxvq_u64) + } +@@ -240,9 +245,14 @@ template + TARGET_AVX2 inline uintptr_t fast_search_avx(T* array, uintptr_t array_len, + uintptr_t index, + T search_element) { +- static_assert(std::is_same::value || +- std::is_same::value || +- std::is_same::value); ++ static constexpr bool is_uint32 = ++ sizeof(T) == sizeof(uint32_t) && std::is_integral::value; ++ static constexpr bool is_uint64 = ++ sizeof(T) == sizeof(uint64_t) && std::is_integral::value; ++ static constexpr bool is_double = ++ sizeof(T) == sizeof(double) && std::is_floating_point::value; ++ ++ static_assert(is_uint32 || is_uint64 || is_double); + + const int target_align = 32; + // Scalar loop to reach desired alignment +@@ -256,21 +266,21 @@ TARGET_AVX2 inline uintptr_t fast_search_avx(T* array, uintptr_t array_len, + } + + // Generating vectorized loop +- if constexpr (std::is_same::value) { ++ if constexpr (is_uint32) { + #define MOVEMASK(x) _mm256_movemask_ps(_mm256_castsi256_ps(x)) + #define EXTRACT(x) base::bits::CountTrailingZeros32(x) + VECTORIZED_LOOP_x86(__m256i, __m256i, _mm256_set1_epi32, _mm256_cmpeq_epi32, + MOVEMASK, EXTRACT) + #undef MOVEMASK + #undef EXTRACT +- } else if constexpr (std::is_same::value) { ++ } else if constexpr (is_uint64) { + #define MOVEMASK(x) _mm256_movemask_pd(_mm256_castsi256_pd(x)) + #define EXTRACT(x) base::bits::CountTrailingZeros32(x) + VECTORIZED_LOOP_x86(__m256i, __m256i, _mm256_set1_epi64x, + _mm256_cmpeq_epi64, MOVEMASK, EXTRACT) + #undef MOVEMASK + #undef EXTRACT +- } else if constexpr (std::is_same::value) { ++ } else if constexpr (is_double) { + #define CMP(a, b) _mm256_cmp_pd(a, b, _CMP_EQ_OQ) + #define EXTRACT(x) base::bits::CountTrailingZeros32(x) + VECTORIZED_LOOP_x86(__m256d, __m256d, _mm256_set1_pd, CMP, diff --git a/test/tools/wee8_compile/wee8_compile.cc b/test/tools/wee8_compile/wee8_compile.cc index 50e2aeb602f33..53ec81b8cd2a5 100644 --- a/test/tools/wee8_compile/wee8_compile.cc +++ b/test/tools/wee8_compile/wee8_compile.cc @@ -11,6 +11,11 @@ #include "v8-version.h" #include "wasm-api/wasm.hh" +namespace v8::internal { +extern bool FLAG_liftoff; +extern unsigned int FLAG_wasm_max_mem_pages; +} // namespace v8::internal + uint32_t parseVarint(const byte_t*& pos, const byte_t* end) { uint32_t n = 0; uint32_t shift = 0; @@ -149,6 +154,8 @@ wasm::vec stripWasmModule(const wasm::vec& module) { } wasm::vec serializeWasmModule(const char* path, const wasm::vec& content) { + ::v8::internal::FLAG_liftoff = false; + ::v8::internal::FLAG_wasm_max_mem_pages = 16384; const auto engine = wasm::Engine::make(); if (engine == nullptr) { std::cerr << "ERROR: Failed to start V8." << std::endl; From 5026f202d775e8f2ac20e599f6573b6de4e86c3d Mon Sep 17 00:00:00 2001 From: Piotr Sikora Date: Tue, 12 Jul 2022 19:23:54 -0700 Subject: [PATCH 2/4] review: Kick CI. Signed-off-by: Piotr Sikora From 4c1d72271470142adbcf0f53da30460339de5bb7 Mon Sep 17 00:00:00 2001 From: Piotr Sikora Date: Tue, 12 Jul 2022 20:24:16 -0700 Subject: [PATCH 3/4] review: fix build with GCC. Signed-off-by: Piotr Sikora --- test/tools/wee8_compile/BUILD | 1 + 1 file changed, 1 insertion(+) diff --git a/test/tools/wee8_compile/BUILD b/test/tools/wee8_compile/BUILD index d35416c42783d..f52efac7f61bb 100644 --- a/test/tools/wee8_compile/BUILD +++ b/test/tools/wee8_compile/BUILD @@ -18,6 +18,7 @@ envoy_cc_library( name = "wee8_compile_lib", srcs = ["wee8_compile.cc"], copts = [ + "-Wno-comments", "-Wno-non-virtual-dtor", "-Wno-unused-parameter", ], From e293d67528c8dbf80fb940e3a57d51640a4ccce9 Mon Sep 17 00:00:00 2001 From: Piotr Sikora Date: Thu, 14 Jul 2022 09:47:12 -0700 Subject: [PATCH 4/4] review: fix build on arm64. Signed-off-by: Piotr Sikora --- bazel/v8.patch | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/bazel/v8.patch b/bazel/v8.patch index ba8c7f41bf24e..6d64274a66f8c 100644 --- a/bazel/v8.patch +++ b/bazel/v8.patch @@ -5,6 +5,7 @@ # 5. Don't expose Wasm C API (only Wasm C++ API). # 6. Fix cross-compilation (https://crrev.com/c/3735165). # 7. Fix build errors in SIMD IndexOf/includes (https://crrev.com/c/3749192). +# 8. Fix build on arm64. diff --git a/BUILD.bazel b/BUILD.bazel index 13f2a5bebf..2197568c48 100644 @@ -29,10 +30,18 @@ index 13f2a5bebf..2197568c48 100644 # Default setting for v8_enable_pointer_compression. diff --git a/bazel/defs.bzl b/bazel/defs.bzl -index dee5e69cc4..d0b5a3c49a 100644 +index e957c0fad3..eee285ab60 100644 --- a/bazel/defs.bzl +++ b/bazel/defs.bzl -@@ -151,6 +151,11 @@ def _default_args(): +@@ -116,6 +116,7 @@ def _default_args(): + }) + select({ + "@v8//bazel/config:is_clang": [ + "-Wno-invalid-offsetof", ++ "-Wno-unneeded-internal-declaration", + "-std=c++17", + ], + "@v8//bazel/config:is_gcc": [ +@@ -151,6 +152,11 @@ def _default_args(): "-fno-integrated-as", ], "//conditions:default": [],