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

Failed to load .wasm on macOS running on Apple silicon #23243

Closed
dio opened this issue Sep 26, 2022 · 2 comments · Fixed by #23257
Closed

Failed to load .wasm on macOS running on Apple silicon #23243

dio opened this issue Sep 26, 2022 · 2 comments · Fixed by #23257
Labels
area/wasm investigate Potential bug that needs verification

Comments

@dio
Copy link
Member

dio commented Sep 26, 2022

Background:

Currently, since the following PR is merged,

macOS binaries v1.23.x (e.g. fetched via Homebrew), and main by default doesn't support Wasm.

Relevant:

Since @keith's CL on v8 upstream is merged. Also, that CL is actually included in the current (also release/1.23) linked v8: 10.4.132.18:

version = "10.4.132.18",
.

Therefore, now we can successfully build envoy on macOS on Apple silicon (tested on M1) 🎉. However, when we run it to load a .wasm file (from examples/wasm-cc), we caught on: https://chromium.googlesource.com/v8/v8.git/+/refs/tags/10.4.132.18/src/base/platform/platform-posix.cc#478, we expect ENOMEM (12) but got EACCES (13, Permission denied). (full log: https://gist.githubusercontent.com/dio/7ef745c09327aa6da0be3b32ef28016a/raw/d220e891afe52208d346ed0dae0d72220275fe73/v8-log.txt).

#
# Fatal error in external/v8/src/base/platform/platform-posix.cc, line 478
# Check failed: 12 == (*__error()) (12 vs. 13).
#
#
#
#FailureMessage Object: 0x16b63df60
==== C stack trace ===============================

    0   envoy-static                        0x000000010a935be4 v8::base::debug::StackTrace::StackTrace() + 32
    1   envoy-static                        0x000000010a935c20 v8::base::debug::StackTrace::StackTrace() + 28
    2   envoy-static                        0x0000000109e23308 v8::platform::(anonymous namespace)::PrintStackTrace() + 28
    3   envoy-static                        0x000000010a8ff940 V8_Fatal(char const*, int, char const*, ...) + 208
    4   envoy-static                        0x000000010a92fdd0 v8::base::OS::SetPermissions(void*, unsigned long, v8::base::OS::MemoryPermission) + 424
    5   envoy-static                        0x000000010a90fb20 v8::base::PageAllocator::SetPermissions(void*, unsigned long, v8::PageAllocator::Permission) + 44
    6   envoy-static                        0x000000010a8f13f8 v8::base::BoundedPageAllocator::SetPermissions(void*, unsigned long, v8::PageAllocator::Permission) + 252
    7   envoy-static                        0x0000000108a5c2b0 v8::internal::VirtualMemory::SetPermissions(unsigned long, unsigned long, v8::PageAllocator::Permission) + 140
    8   envoy-static                        0x0000000108044b08 v8::internal::MemoryAllocator::PartialFreeMemory(v8::internal::BasicMemoryChunk*, unsigned long, unsigned long, unsigned long) + 520
    9   envoy-static                        0x000000010810aa78 v8::internal::Page::ShrinkToHighWaterMark() + 1136
    10  envoy-static                        0x00000001080961a4 v8::internal::PagedSpaceBase::ShrinkPageToHighWaterMark(v8::internal::Page*) + 36
    11  envoy-static                        0x0000000108096524 v8::internal::PagedSpaceBase::ShrinkImmortalImmovablePages() + 256
    12  envoy-static                        0x0000000107ee83b0 v8::internal::Heap::NotifyDeserializationComplete() + 92
    13  envoy-static                        0x0000000107d21a94 v8::internal::Isolate::Init(v8::internal::SnapshotData*, v8::internal::SnapshotData*, v8::internal::SnapshotData*, bool) + 5192
    14  envoy-static                        0x0000000107d22230 v8::internal::Isolate::InitWithSnapshot(v8::internal::SnapshotData*, v8::internal::SnapshotData*, v8::internal::SnapshotData*, bool) + 216
    15  envoy-static                        0x0000000108a132a0 v8::internal::Snapshot::Initialize(v8::internal::Isolate*) + 792
    16  envoy-static                        0x0000000107862a54 v8::Isolate::Initialize(v8::Isolate*, v8::Isolate::CreateParams const&) + 980
    17  envoy-static                        0x00000001078631e4 v8::Isolate::New(v8::Isolate::CreateParams const&) + 36
    18  envoy-static                        0x00000001072bf48c wasm::Store::make(wasm::Engine*) + 308
    19  envoy-static                        0x00000001072779d4 proxy_wasm::v8::V8::load(std::__1::basic_string_view<char, std::__1::char_traits<char> >, std::__1::basic_string_view<char, std::__1::char_traits<char> >, std::__1::unordered_map<unsigned int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::hash<unsigned int>, std::__1::equal_to<unsigned int>, std::__1::allocator<std::__1::pair<unsigned int const, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > > const&) + 64
    20  envoy-static                        0x0000000107247b04 proxy_wasm::WasmBase::load(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool) + 1536
    21  envoy-static                        0x000000010724a014 proxy_wasm::createWasm(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::shared_ptr<proxy_wasm::PluginBase> const&, std::__1::function<std::__1::shared_ptr<proxy_wasm::WasmHandleBase> (std::__1::basic_string_view<char, std::__1::char_traits<char> >)> const&, std::__1::function<std::__1::shared_ptr<proxy_wasm::WasmHandleBase> (std::__1::shared_ptr<proxy_wasm::WasmHandleBase>)> const&, bool) + 576

That check was introduced by: https://chromium-review.googlesource.com/c/v8/v8/+/3610445. Seems like, when on macOS, it (if (ret != 0) CHECK_EQ(ENOMEM, errno);) should be executed after the "workaround" for macOS on Apple silicon is executed (to use madvise, caused by the mprotect "bug" on Apple silicon since macOS 11.2: https://bugs.chromium.org/p/v8/issues/detail?id=11389#c14. As additional info: this workaround was introduced by https://chromium-review.googlesource.com/c/v8/v8/+/2679688).

The proposed additional entry to bazel/v8.patch:

diff --git a/src/base/platform/platform-posix.cc b/src/base/platform/platform-posix.cc
index 131ff9614e..8699699861 100644
--- a/src/base/platform/platform-posix.cc
+++ b/src/base/platform/platform-posix.cc
@@ -472,11 +472,6 @@ bool OS::SetPermissions(void* address, size_t size, MemoryPermission access) {
   int prot = GetProtectionFromMemoryPermission(access);
   int ret = mprotect(address, size, prot);

-  // Any failure that's not OOM likely indicates a bug in the caller (e.g.
-  // using an invalid mapping) so attempt to catch that here to facilitate
-  // debugging of these failures.
-  if (ret != 0) CHECK_EQ(ENOMEM, errno);
-
   // MacOS 11.2 on Apple Silicon refuses to switch permissions from
   // rwx to none. Just use madvise instead.
 #if defined(V8_OS_DARWIN)
@@ -486,6 +481,11 @@ bool OS::SetPermissions(void* address, size_t size, MemoryPermission access) {
   }
 #endif

+  // Any failure that's not OOM likely indicates a bug in the caller (e.g.
+  // using an invalid mapping) so attempt to catch that here to facilitate
+  // debugging of these failures.
+  if (ret != 0) CHECK_EQ(ENOMEM, errno);
+
   if (ret == 0 && access == OS::MemoryPermission::kNoAccess) {
     // This is advisory; ignore errors and continue execution.
     USE(DiscardSystemPages(address, size));

I tested to build and run the produced binary on macOS running on M1 (it successfully loads the .wasm file):

   wasm-cc (mac-wasm): uname -a
Darwin Dhis-MacBook-Air.local 21.6.0 Darwin Kernel Version 21.6.0: Mon Aug 22 20:20:05 PDT 2022; root:xnu-8020.140.49~2/RELEASE_ARM64_T8101 arm64
   wasm-cc (mac-wasm): file ~/src/envoy/bazel-bin/source/exe/envoy-static
/Users/dio/src/envoy/bazel-bin/source/exe/envoy-static: Mach-O 64-bit executable arm64
   wasm-cc (mac-wasm): pwd
/Users/dio/src/envoy/examples/wasm-cc
   wasm-cc (mac-wasm): ~/src/envoy/bazel-bin/source/exe/envoy-static -c envoy.yaml 
...
[2022-09-25 20:43:18.254][2501953][info][main] [source/server/server.cc:904] starting main dispatch loop

If this patch does make sense, I'd like to raise PRs both for release/1.23 and main branches, since both of them are linked to the same v8 version: 10.4.132.18.

Thanks!

@dio dio added the triage Issue requires triage label Sep 26, 2022
@dio
Copy link
Member Author

dio commented Sep 26, 2022

Another suggestion is to set v8_enable_external_code_space to true (see: https://chromium-review.googlesource.com/c/v8/v8/+/3610445/comment/6294ada3_a3cab643/), but it seems like v8_enable_external_code_space is part of V8 GN flags that are not currently supported from bazel.

Also need to try to update v8 :) nodejs/node-v8#226 (comment)

dio added a commit to dio/envoy that referenced this issue Sep 27, 2022
This applies https://chromium-review.googlesource.com/c/v8/v8/+/3700352 as a fix for
MemoryAllocator::PartialFreeMemory() which shouldn't try to change permissions of RWX pages.

This mainly affects macOS > 11.2 due to mprotect behavior changes (envoyproxy#23243)
on Apple silicon.

Signed-off-by: Dhi Aurrahman <dio@rockybars.com>
@wbpcode
Copy link
Member

wbpcode commented Sep 28, 2022

cc @PiotrSikora

@wbpcode wbpcode added investigate Potential bug that needs verification area/wasm and removed triage Issue requires triage labels Sep 28, 2022
wbpcode pushed a commit that referenced this issue Sep 29, 2022
* macOS: Allow to load .wasm on Apple silicon

This applies https://chromium-review.googlesource.com/c/v8/v8/+/3700352 as a fix for
MemoryAllocator::PartialFreeMemory() which shouldn't try to change permissions of RWX pages.

This mainly affects macOS > 11.2 due to mprotect behavior changes (#23243)
on Apple silicon.
dio added a commit to dio/envoy that referenced this issue Sep 29, 2022
…xy#23257)

* macOS: Allow to load .wasm on Apple silicon

This applies https://chromium-review.googlesource.com/c/v8/v8/+/3700352 as a fix for
MemoryAllocator::PartialFreeMemory() which shouldn't try to change permissions of RWX pages.

This mainly affects macOS > 11.2 due to mprotect behavior changes (envoyproxy#23243)
on Apple silicon.
dio added a commit to dio/envoy that referenced this issue Sep 29, 2022
This applies https://chromium-review.googlesource.com/c/v8/v8/+/3700352 as a fix for
MemoryAllocator::PartialFreeMemory() which shouldn't try to change permissions of RWX pages.

This mainly affects macOS > 11.2 due to mprotect behavior changes (envoyproxy#23243)
on Apple silicon.

This is cherry-picked from: envoyproxy@63f27a6

Signed-off-by: Dhi Aurrahman <dio@rockybars.com>
wbpcode pushed a commit that referenced this issue Sep 30, 2022
backport: macOS: Enable wasm and allow to load .wasm on Apple silicon

This applies https://chromium-review.googlesource.com/c/v8/v8/+/3700352 as a fix for
MemoryAllocator::PartialFreeMemory() which shouldn't try to change permissions of RWX pages.

This mainly affects macOS > 11.2 due to mprotect behavior changes (#23243)
on Apple silicon.

This is cherry-picked from: 63f27a6

Signed-off-by: Dhi Aurrahman <dio@rockybars.com>

Signed-off-by: Dhi Aurrahman <dio@rockybars.com>
oschaaf pushed a commit to maistra/envoy that referenced this issue Oct 26, 2022
* macOS: Allow to load .wasm on Apple silicon

This applies https://chromium-review.googlesource.com/c/v8/v8/+/3700352 as a fix for
MemoryAllocator::PartialFreeMemory() which shouldn't try to change permissions of RWX pages.

This mainly affects macOS > 11.2 due to mprotect behavior changes (envoyproxy/envoy#23243)
on Apple silicon.
phlax pushed a commit to phlax/envoy that referenced this issue Nov 23, 2022
backport: macOS: Enable wasm and allow to load .wasm on Apple silicon

This applies https://chromium-review.googlesource.com/c/v8/v8/+/3700352 as a fix for
MemoryAllocator::PartialFreeMemory() which shouldn't try to change permissions of RWX pages.

This mainly affects macOS > 11.2 due to mprotect behavior changes (envoyproxy#23243)
on Apple silicon.

This is cherry-picked from: envoyproxy@63f27a6

Signed-off-by: Dhi Aurrahman <dio@rockybars.com>

Signed-off-by: Dhi Aurrahman <dio@rockybars.com>
Signed-off-by: Ryan Northey <ryan@synca.io>
phlax pushed a commit to phlax/envoy that referenced this issue Nov 24, 2022
backport: macOS: Enable wasm and allow to load .wasm on Apple silicon

This applies https://chromium-review.googlesource.com/c/v8/v8/+/3700352 as a fix for
MemoryAllocator::PartialFreeMemory() which shouldn't try to change permissions of RWX pages.

This mainly affects macOS > 11.2 due to mprotect behavior changes (envoyproxy#23243)
on Apple silicon.

This is cherry-picked from: envoyproxy@63f27a6

Signed-off-by: Dhi Aurrahman <dio@rockybars.com>

Signed-off-by: Dhi Aurrahman <dio@rockybars.com>
Signed-off-by: Ryan Northey <ryan@synca.io>
phlax pushed a commit that referenced this issue Nov 26, 2022
backport: macOS: Enable wasm and allow to load .wasm on Apple silicon

This applies https://chromium-review.googlesource.com/c/v8/v8/+/3700352 as a fix for
MemoryAllocator::PartialFreeMemory() which shouldn't try to change permissions of RWX pages.

This mainly affects macOS > 11.2 due to mprotect behavior changes (#23243)
on Apple silicon.

This is cherry-picked from: 63f27a6

Signed-off-by: Dhi Aurrahman <dio@rockybars.com>

Signed-off-by: Dhi Aurrahman <dio@rockybars.com>
Signed-off-by: Ryan Northey <ryan@synca.io>
phlax added a commit that referenced this issue Nov 30, 2022
…24089)

* deps: bump `com_github_wasmtime` -> 1.0.0 (#23232)

Signed-off-by: river phillips <riverphillips1@gmail.com>
Signed-off-by: Ryan Northey <ryan@synca.io>

* wasm: update WAVM to nightly/2022-05-14. (#22491)


Signed-off-by: Piotr Sikora <piotrsikora@google.com>
Signed-off-by: Ryan Northey <ryan@synca.io>

* wasm: update Proxy-Wasm C++ Host to latest. (#22575)

Signed-off-by: Piotr Sikora <piotrsikora@google.com>

Signed-off-by: Ryan Northey <ryan@synca.io>

* deps: Bump `com_github_wasmtime` -> 1.0.2 (#24086)

Fix:

- CVE-2022-39392
- CVE-2022-39393
- CVE-2022-39394

Signed-off-by: Ryan Northey <ryan@synca.io>

* bazel: update rules_rust

This gets us on rust 1.60

Signed-off-by: Keith Smiley <keithbsmiley@gmail.com>

Signed-off-by: Ryan Northey <ryan@synca.io>

* deps: Bump `rules_rust` -> 0.8.1 (#22253)

Fix #22073

Signed-off-by: Ryan Northey <ryan@synca.io>

* wasm: fix V8 build on older versions of Linux. (#22228)

wasm: fix build on older versions of Linux.

Signed-off-by: Piotr Sikora <piotrsikora@google.com>
Signed-off-by: Ryan Northey <ryan@synca.io>

* backport: macOS: Allow to load .wasm on Apple silicon (#23299)

backport: macOS: Enable wasm and allow to load .wasm on Apple silicon

This applies https://chromium-review.googlesource.com/c/v8/v8/+/3700352 as a fix for
MemoryAllocator::PartialFreeMemory() which shouldn't try to change permissions of RWX pages.

This mainly affects macOS > 11.2 due to mprotect behavior changes (#23243)
on Apple silicon.

This is cherry-picked from: 63f27a6

Signed-off-by: Dhi Aurrahman <dio@rockybars.com>

Signed-off-by: Dhi Aurrahman <dio@rockybars.com>
Signed-off-by: Ryan Northey <ryan@synca.io>

* deps: Bump `v8` to 10.7.193.13 and `proxy_wasm_cpp_host` to b0a0594 (#23434)

Signed-off-by: Dhi Aurrahman <dio@rockybars.com>

* ci: Disable wasm coverage tests (#24169)

and adjust-coverage-total

Signed-off-by: Ryan Northey <ryan@synca.io>

Signed-off-by: phlax <phlax@users.noreply.github.com>

* deps: Bump `com_github_wasmtime` -> 2.0.2 (+related) (#24150)

deps: Bump `com_github_wasmtime` -> 2.0.2

- `proxy_wasm_cpp_host`
- `proxy_wasm_rust_sdk`

Signed-off-by: Ryan Northey <ryan@synca.io>

Signed-off-by: river phillips <riverphillips1@gmail.com>
Signed-off-by: Ryan Northey <ryan@synca.io>
Signed-off-by: Piotr Sikora <piotrsikora@google.com>
Signed-off-by: Dhi Aurrahman <dio@rockybars.com>
Signed-off-by: phlax <phlax@users.noreply.github.com>
Co-authored-by: River <6375745+RiverPhillips@users.noreply.github.com>
Co-authored-by: Piotr Sikora <piotrsikora@google.com>
Co-authored-by: Keith Smiley <keithbsmiley@gmail.com>
Co-authored-by: Dhi Aurrahman <dio@rockybars.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/wasm investigate Potential bug that needs verification
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants