From 89b325ac14c884d0f08680e8a4aa34a1d69d0c72 Mon Sep 17 00:00:00 2001 From: khanhduytran0 Date: Thu, 3 Jul 2025 13:01:49 +0700 Subject: [PATCH] Fix[JDK21]: fix patch, add breakpoint codepath for TXM devices --- 6_buildjdk.sh | 1 + patches/jre_21/ios/2_mirror_mapping.diff | 84 ++++++++++++++++++------ 2 files changed, 64 insertions(+), 21 deletions(-) diff --git a/6_buildjdk.sh b/6_buildjdk.sh index e9f3b7e..b2fdbad 100755 --- a/6_buildjdk.sh +++ b/6_buildjdk.sh @@ -88,6 +88,7 @@ ln -s -f $CUPS_DIR/cups $ANDROID_INCLUDE/ cd openjdk-${TARGET_VERSION} # Apply patches +git add . git reset --hard if [[ "$BUILD_IOS" != "1" ]]; then find ../patches/jre_${TARGET_VERSION}/android -name "*.diff" -print0 | xargs -0 -I {} sh -c 'echo "Applying {}" && git apply --reject --whitespace=fix {} || (echo "git apply failed (Android patch set)" && exit 1)' diff --git a/patches/jre_21/ios/2_mirror_mapping.diff b/patches/jre_21/ios/2_mirror_mapping.diff index 2caef10..0515708 100644 --- a/patches/jre_21/ios/2_mirror_mapping.diff +++ b/patches/jre_21/ios/2_mirror_mapping.diff @@ -117,41 +117,64 @@ index c895ff5cc..d864c9d8a 100644 if (s == nullptr) { return nullptr; diff --git a/src/hotspot/os/bsd/os_bsd.cpp b/src/hotspot/os/bsd/os_bsd.cpp -index faf9d9f64..9c2adc2e4 100644 +index faf9d9f64..8828e855b 100644 --- a/src/hotspot/os/bsd/os_bsd.cpp +++ b/src/hotspot/os/bsd/os_bsd.cpp -@@ -136,6 +136,106 @@ static volatile int processor_id_map[processor_id_map_size]; +@@ -136,6 +136,129 @@ static volatile int processor_id_map[processor_id_map_size]; static volatile int processor_id_next = 0; #endif +#ifdef __APPLE__ +address os::Bsd::debug_jit_mapping_mirrored = NULL; + -+__attribute__((noinline,optnone)) -+void BreakGetJITMapping(char **map, size_t bytes) { -+ ++__attribute__((naked,noinline,optnone)) ++char* BreakGetJITMapping(char *map, size_t bytes) { ++ asm("brk #0x69 \n" ++ "ret"); ++} ++ ++bool DeviceRequiresTXMWorkaround() { ++ if(__builtin_available(iOS 19.0, *)) { ++ DIR *d = opendir("/private/preboot"); ++ struct dirent *dir; ++ char txmPath[PATH_MAX]; ++ while ((dir = readdir(d)) != NULL) { ++ if(strlen(dir->d_name) == 96) { ++ snprintf(txmPath, sizeof(txmPath), "/private/preboot/%s/usr/standalone/firmware/FUD/Ap,TrustedExecutionMonitor.img4", dir->d_name); ++ break; ++ } ++ } ++ closedir(d); ++ return access(txmPath, F_OK) == 0; ++ } else { ++ return false; ++ } +} + +char* get_debug_jit_mapping(size_t bytes) { + // the map we got has debuggable flag, r-x, setup mirrored map + vm_address_t buf_rx = 0; + if(MirrorMappedCodeCache) { -+ printf("[JIT26] Requesting %zu MB for JIT mapping\n", bytes/ (1024 * 1024)); -+ BreakGetJITMapping((char**)&buf_rx, bytes); ++ if(DeviceRequiresTXMWorkaround()) { ++ printf("[JIT26] Requesting %zu MB for JIT mapping\n", bytes/ (1024 * 1024)); ++ buf_rx = (vm_address_t)BreakGetJITMapping(NULL, bytes); ++ } + if(buf_rx) { + printf("[JIT26] Got JIT mapping %p from debugger\n", (void*)buf_rx); + } else { + buf_rx = (vm_address_t)mmap(NULL, bytes, PROT_READ | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + } -+ } -+ if(!buf_rx) { -+ printf("[JIT26] Failed to allocate RX region\n"); ++ if(!buf_rx) { ++ printf("[JIT26] Failed to allocate RX region\n"); ++ return NULL; ++ } ++ } else { + return NULL; + } + vm_address_t buf_rw = 0; -+ ++ + vm_prot_t cur_prot, max_prot; -+ ++ + kern_return_t ret = vm_remap(mach_task_self(), &buf_rw, bytes, 0, + VM_FLAGS_ANYWHERE, mach_task_self(), buf_rx, false, &cur_prot, + &max_prot, VM_INHERIT_NONE); @@ -159,7 +182,7 @@ index faf9d9f64..9c2adc2e4 100644 + fprintf(stderr, "[JIT26] Failed to remap RX region %d\n", ret); + return NULL; + } -+ ++ + // Protect region as RW + ret = vm_protect(mach_task_self(), buf_rw, bytes, FALSE, + VM_PROT_READ | VM_PROT_WRITE); @@ -167,9 +190,9 @@ index faf9d9f64..9c2adc2e4 100644 + fprintf(stderr, "[JIT26] Failed to set RW protection %d\n", ret); + return NULL; + } -+ ++ + printf("[JIT26] mapping at RW=%p, RX=%p\n", (void*)buf_rw, (void*)buf_rx); -+ ++ + os::Bsd::debug_jit_mapping_mirrored = (address)buf_rw; + return (char *)buf_rx; + //os::Bsd::debug_jit_mapping_mirrored = (address)buf_rx; @@ -227,15 +250,34 @@ index faf9d9f64..9c2adc2e4 100644 //////////////////////////////////////////////////////////////////////////////// // utility functions -@@ -757,6 +857,7 @@ void os::Bsd::clock_init() { +@@ -757,26 +880,6 @@ void os::Bsd::clock_init() { -+ +-#ifdef __APPLE__ +-static bool rwxChecked, rwxAvailable; +-#endif +-bool os::Bsd::isRWXJITAvailable() { +-#ifdef __APPLE__ +- if (!rwxChecked) { +- rwxChecked = true; +- const int flags = MAP_PRIVATE | MAP_NORESERVE | MAP_ANONYMOUS | MAP_JIT; +- char* addr = (char*)::mmap(0, getpagesize(), PROT_NONE, flags, -1, 0); +- rwxAvailable = addr != MAP_FAILED; +- if (rwxAvailable) { +- ::munmap(addr, getpagesize()); +- } +- } +- return rwxAvailable; +-#else +- return true; +-#endif +-} +- #ifdef __APPLE__ - static bool rwxChecked, rwxAvailable; - #endif -@@ -1576,7 +1677,7 @@ bool os::pd_commit_memory(char* addr, size_t size, bool exec) { + + jlong os::javaTimeNanos() { +@@ -1576,7 +1679,7 @@ bool os::pd_commit_memory(char* addr, size_t size, bool exec) { #elif defined(__APPLE__) if (exec) { // Do not replace MAP_JIT mappings, see JDK-8234930 @@ -244,7 +286,7 @@ index faf9d9f64..9c2adc2e4 100644 return true; } } else { -@@ -1726,6 +1827,14 @@ static int anon_munmap(char * addr, size_t size) { +@@ -1726,6 +1829,14 @@ static int anon_munmap(char * addr, size_t size) { } char* os::pd_reserve_memory(size_t bytes, bool exec) {