Skip to content

Commit c43d232

Browse files
authored
feat(catalyst): ship V8 with its JIT tiers on Mac Catalyst (#10)
Catalyst was built as lite mode despite the build script saying otherwise: gni/v8.gni turns v8_enable_lite_mode on for every target_platform "iphoneos" build, and Catalyst is only a target_environment of that platform, so the archives shipped without Sparkplug, Maglev or TurboFan. Set it off explicitly for both Catalyst variants. A JIT build alone faults on Apple silicon: Catalyst compiles as iOS, so V8 uses its iOS JIT-memory model and writes MAP_JIT pages without macOS's per-thread W^X toggle. v8_catalyst_jit_write_protect.patch gives Catalyst the macOS model: pthread_jit_write_protect_np around code writes (resolved with dlsym, since the SDK marks it unavailable on Catalyst) and the macOS code-range pre-commit. It is applied for iOS fetches and is a no-op outside Catalyst.
1 parent 438a577 commit c43d232

5 files changed

Lines changed: 117 additions & 3 deletions

File tree

‎README.md‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,20 @@ platform), dispatch the workflow with `platforms: android` or `platforms: ios`.
129129
|---|---|
130130
| `v8_resurrecting_finalizers.patch` | Restores `WeakCallbackType::kFinalizer`, removed upstream immediately after 10.3.22. Both runtimes' object managers depend on resurrecting finalizers. It also lifts, for the finalizer drain only, the `DisallowJavascriptExecution` scope that `Heap::CollectGarbage` now holds across the whole collection: 14.9 turns entering JS from a GC callback into a `GRACEFUL_FATAL`, and the iOS runtime reaches JS whenever a released ObjC object's `-dealloc` hits a JS-backed override. |
131131
| `android_build.patch` | Lowers chromium's minimum SDK to 21 (that floor is for Java/dex tooling; this builds only the native target), makes `android_ndk_root` a gn arg, and widens the hard Linux-host assert so macOS can build the 64-bit ABIs. |
132+
| `v8_catalyst_jit_write_protect.patch` | Mac Catalyst compiles as iOS (`TARGET_OS_IPHONE`), so V8 picks its iOS JIT-memory model, which assumes `MAP_JIT` pages are writable and executable at once. A Catalyst process runs on macOS, where Apple silicon makes `MAP_JIT` pages per-thread W^X, so the first JIT code write faults (`SIGBUS`). The patch gives Catalyst the macOS model: `pthread_jit_write_protect_np` around code writes (resolved with `dlsym`, since the SDK marks it unavailable on Catalyst) and the macOS code-range pre-commit. Applied to iOS fetches only; it is a no-op outside Catalyst. |
132133

133134
## The gn args are load-bearing
134135

135136
They are commented at their definition in `scripts/matrix/build-android.sh` and
136-
`scripts/matrix/build-ios.sh`. Three worth knowing:
137+
`scripts/matrix/build-ios.sh`. Four worth knowing:
138+
139+
- `v8_enable_lite_mode=false` (Catalyst only) — `gni/v8.gni` turns lite mode
140+
on for every `target_platform="iphoneos"` build, and Catalyst is only a
141+
`target_environment` of that platform, so without the explicit override the
142+
Catalyst archives ship without Sparkplug, Maglev or TurboFan. Catalyst keeps
143+
the full tier set so apps entitled to JIT (`com.apple.security.cs.allow-jit`)
144+
get it; the iOS runtime passes `--jitless` when the process cannot map JIT
145+
memory. This adds about 9.6 MB to the stripped arm64 Catalyst framework.
137146

138147
- `cppgc_enable_caged_heap=false` (iOS only) — it defaults to **true on arm64**
139148
and, contrary to how the flags read, does *not* track
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
diff --git a/src/base/build_config.h b/src/base/build_config.h
2+
index ee745539dfd..4182c592472 100644
3+
--- a/src/base/build_config.h
4+
+++ b/src/base/build_config.h
5+
@@ -36,8 +36,16 @@
6+
#endif
7+
#define V8_HAS_PKU_JIT_WRITE_PROTECT V8_HAS_PKU_SUPPORT
8+
9+
+// Mac Catalyst compiles as iOS (TARGET_OS_IPHONE) but runs on macOS, so its
10+
+// JIT pages follow the macOS MAP_JIT + pthread_jit_write_protect_np rules.
11+
+#if defined(V8_OS_IOS) && defined(TARGET_OS_MACCATALYST) && \
12+
+ TARGET_OS_MACCATALYST
13+
+#define V8_OS_MACCATALYST 1
14+
+#endif
15+
+
16+
// pthread_jit_write_protect is only available on arm64 Mac.
17+
-#if defined(V8_HOST_ARCH_ARM64) && defined(V8_OS_MACOS)
18+
+#if defined(V8_HOST_ARCH_ARM64) && \
19+
+ (defined(V8_OS_MACOS) || defined(V8_OS_MACCATALYST))
20+
#define V8_HAS_PTHREAD_JIT_WRITE_PROTECT 1
21+
#else
22+
#define V8_HAS_PTHREAD_JIT_WRITE_PROTECT 0
23+
diff --git a/src/base/platform/platform-darwin.cc b/src/base/platform/platform-darwin.cc
24+
index 41fc5ec3302..004ae7c002b 100644
25+
--- a/src/base/platform/platform-darwin.cc
26+
+++ b/src/base/platform/platform-darwin.cc
27+
@@ -234,6 +234,24 @@ V8_BASE_EXPORT void SetJitWriteProtected(int enable) {
28+
}
29+
30+
#pragma clang diagnostic pop
31+
+#elif V8_HAS_PTHREAD_JIT_WRITE_PROTECT && defined(V8_OS_MACCATALYST)
32+
+// The SDK marks pthread_jit_write_protect_np unavailable on Mac Catalyst, but
33+
+// a Catalyst process runs against macOS's libpthread, which exports it.
34+
+namespace {
35+
+using JitWriteProtectFn = void (*)(int);
36+
+
37+
+JitWriteProtectFn ResolveJitWriteProtect() {
38+
+ auto fn = reinterpret_cast<JitWriteProtectFn>(
39+
+ dlsym(RTLD_DEFAULT, "pthread_jit_write_protect_np"));
40+
+ CHECK_NOT_NULL(fn);
41+
+ return fn;
42+
+}
43+
+} // namespace
44+
+
45+
+V8_BASE_EXPORT void SetJitWriteProtected(int enable) {
46+
+ static const JitWriteProtectFn jit_write_protect = ResolveJitWriteProtect();
47+
+ jit_write_protect(enable);
48+
+}
49+
#endif
50+
51+
} // namespace base
52+
diff --git a/src/heap/code-range.cc b/src/heap/code-range.cc
53+
index ca0b1742b38..b979862d76f 100644
54+
--- a/src/heap/code-range.cc
55+
+++ b/src/heap/code-range.cc
56+
@@ -382,8 +382,8 @@ bool CodeRange::InitReservation(v8::PageAllocator* page_allocator,
57+
// Don't pre-commit the code cage on Windows since it uses memory and it's not
58+
// required for recommit.
59+
// iOS cannot adjust page permissions for MAP_JIT'd pages, they are set as RWX
60+
-// at the start.
61+
-#if !defined(V8_OS_WIN) && !defined(V8_OS_IOS)
62+
+// at the start. Mac Catalyst runs on macOS and needs the pre-commit.
63+
+#if !defined(V8_OS_WIN) && (!defined(V8_OS_IOS) || defined(V8_OS_MACCATALYST))
64+
if (params.page_initialization_mode ==
65+
base::PageInitializationMode::kRecommitOnly) {
66+
void* base = reinterpret_cast<void*>(page_allocator_->begin() +
67+
@@ -605,7 +605,7 @@ uint8_t* CodeRange::RemapEmbeddedBuiltins(Isolate* isolate,
68+
if (V8_HEAP_USE_PTHREAD_JIT_WRITE_PROTECT ||
69+
V8_HEAP_USE_BECORE_JIT_WRITE_PROTECT || ThreadIsolation::Enabled()) {
70+
// iOS code pages are already RWX and don't need to be modified.
71+
-#if !defined(V8_TARGET_OS_IOS)
72+
+#if !defined(V8_TARGET_OS_IOS) || defined(V8_OS_MACCATALYST)
73+
if (!page_allocator()->RecommitPages(embedded_blob_code_copy, code_size,
74+
PageAllocator::kReadWriteExecute)) {
75+
V8::FatalProcessOutOfMemory(isolate,
76+
diff --git a/src/heap/code-range.h b/src/heap/code-range.h
77+
index b42081b8aa1..acb2f57d420 100644
78+
--- a/src/heap/code-range.h
79+
+++ b/src/heap/code-range.h
80+
@@ -177,7 +177,8 @@ class CodeRange final : public VirtualMemoryCage {
81+
// Red zones that we should not allocate in.
82+
RedZones red_zones_;
83+
84+
-#if !defined(V8_OS_WIN) && !defined(V8_OS_IOS) && defined(DEBUG)
85+
+#if !defined(V8_OS_WIN) && \
86+
+ (!defined(V8_OS_IOS) || defined(V8_OS_MACCATALYST)) && defined(DEBUG)
87+
bool immutable_ = false;
88+
#endif
89+
};

‎scripts/matrix/build-ios.sh‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,13 @@ MODULES=(
7777
)
7878

7979
# iOS proper cannot JIT, so it is built in lite mode. Catalyst runs on macOS,
80-
# where JIT is permitted, and is deliberately not lite -- keeping the two arg
81-
# sets distinct preserves what each currently ships.
80+
# where an app holding com.apple.security.cs.allow-jit may JIT, so it ships the
81+
# full tier set (Sparkplug, Maglev, TurboFan) and the runtime passes --jitless
82+
# when the process may not map JIT memory. lite mode must be turned off
83+
# explicitly: gni/v8.gni defaults it on for target_platform "iphoneos", and
84+
# Catalyst is only a target_environment of that platform. The Catalyst JIT also
85+
# needs patches/v8_catalyst_jit_write_protect.patch; without it V8 writes JIT
86+
# code without macOS's per-thread W^X toggle and faults on Apple silicon.
8287
#
8388
# v8_expose_public_symbols marks the V8_EXPORT API default-visibility. Plugins
8489
# (e.g. @nativescript/canvas) compile against the vendored V8 headers and
@@ -128,6 +133,7 @@ if [ "$TARGET_ENV" = "catalyst" ]; then
128133
GN_ARGS="$GN_ARGS
129134
is_official_build=true
130135
use_thin_lto=false
136+
v8_enable_lite_mode=false
131137
ios_deployment_target=\"$CATALYST_DEPLOYMENT_TARGET\""
132138
else
133139
GN_ARGS="$GN_ARGS

‎scripts/matrix/fetch.sh‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ git -C v8 apply "$ROOT_DIR/patches/v8_resurrecting_finalizers.patch"
8787
# reserved once per isolate.
8888
git -C v8 apply "$ROOT_DIR/patches/v8_js_dispatch_table_reservation.patch"
8989

90+
if [ "$PLATFORM" = "ios" ]; then
91+
# Mac Catalyst compiles as iOS but runs on macOS; this gives its JIT build
92+
# the macOS MAP_JIT + pthread_jit_write_protect_np handling.
93+
git -C v8 apply "$ROOT_DIR/patches/v8_catalyst_jit_write_protect.patch"
94+
fi
95+
9096
if [ "$PLATFORM" = "android" ]; then
9197
# API 21 and a selectable android_ndk_root are needed on every host; the
9298
# host-assert change in the same patch is a no-op on Linux.

‎tests/apple-args.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,9 @@ def test_ios_and_catalyst_do_not_receive_tvos_overrides(self):
3535
self.assertIn('is_debug=false', args)
3636
self.assertEqual('v8_enable_lite_mode=true' in args, 'catalyst' not in variant)
3737

38+
def test_catalyst_is_built_with_the_jit(self):
39+
for variant in ['arm64-catalyst', 'x64-catalyst']:
40+
self.assertIn('v8_enable_lite_mode=false', self.args_for(variant))
41+
3842
if __name__ == '__main__':
3943
unittest.main()

0 commit comments

Comments
 (0)