Fix[JDK21]: fix patch, add breakpoint codepath for TXM devices

This commit is contained in:
khanhduytran0 2025-07-03 13:01:49 +07:00
parent a8ca6a4c1c
commit 89b325ac14
2 changed files with 64 additions and 21 deletions

View File

@ -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)'

View File

@ -117,35 +117,58 @@ 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;
@ -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) {