JDK-8283326 : Implement SafeFetch statically
  • Type: Enhancement
  • Component: hotspot
  • Sub-Component: runtime
  • Affected Version: 19
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2022-03-17
  • Updated: 2024-05-02
  • Resolved: 2022-04-15
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 17 JDK 19
17.0.10-oracleFixed 19 b19Fixed
Related Reports
Blocks :  
Duplicate :  
Duplicate :  
Relates :  
Relates :  
Relates :  
Relates :  
Description
SafeFetch is important - mostly when writing hs-err reports, but it is also used in low level utility functions like `os::print_hex_dump()` and `os::is_readable_pointer()`. It is also used (JDK-8282306) as part of call stack printing. At the moment it is implemented via dynamically generated stub routines. That has disadvantages:

1) on Mac m1, it causes problems with execution prevention for generated code, requiring awkward - and partly nonfunctional - workarounds like this: https://github.com/openjdk/jdk/blob/4df67426ed02f18af0757897acb28b636a317a91/src/hotspot/share/runtime/safefetch.inline.hpp#L37-L41 . Partly nonfunctional since this requires Thread::current() to be present, which cannot be guaranteed for all call sites of SafeFetch().

2) Since SafeFetch relies on dynamic stub generation, there is a time window during VM initialization where SafeFetch is unreliable. As a result, hs-err reports for crashes in that time can get compromised. It also may affect early NMT reports, when call stacks are printed.

3) Dynamic generation uses a tiny bit of startup time.

SAP proposed a patch for (1) with JDK-8282475 [2], but the discussion bogged down. Andrew Haley proposed a different approach [2], namely to generate SafeFetch statically. Generating it statically would mean the code lives in the text segment and is not subject to execution prevention, so it would solve (1). It also solves (2) since we generate it at compile time. And we would save a bit of time during VM start. Finally, it would be a good preparation in case other platforms introduce execution prevention mechanics like Mac M1.

I have a prototype that works on Linux (x64, x86, aarch64, arm), MacOS (x64, aarch64), Windows, and Zero. It uses static assembly (MacOS and Linux) resp. SEH (Windows) resp. sigsetjmp/longjmp (zero).

[1] https://github.com/openjdk/jdk/pull/7727
[2] https://github.com/openjdk/jdk/pull/7727#issuecomment-1064936019

Comments
Fix request (17u): I'd like to backport this to fix the crash on mac m1. There will be a follow up regarding fixing the arm32 thumb mode (8284997,8288719)
13-07-2023

A pull request was submitted for review. URL: https://git.openjdk.org/jdk17u-dev/pull/1557 Date: 2023-07-07 10:16:36 +0000
07-07-2023

Making backport to JDK17
06-07-2023

Could we backport this change to JDK 17? Would prevent bugs like https://bugs.openjdk.org/browse/JDK-8307549
15-06-2023

Hi Thomas, since this issue is merged my native OpenJDK builds on arm32 break with the following crash: # # A fatal error has been detected by the Java Runtime Environment: # # SIGBUS (0x7) at pc=0xb4300a28, pid=17036, tid=17048 # # JRE version: OpenJDK Runtime Environment (19.0) (build 19-internal-adhoc..workspace) # Java VM: OpenJDK Server VM (19-internal-adhoc..workspace, mixed mode, g1 gc, linux-arm) # Problematic frame: # v ~StubRoutines::atomic_cmpxchg Should I open a separate bug for this?
18-04-2022

Changeset: bdf8a2a2 Author: Thomas Stuefe <stuefe@openjdk.org> Date: 2022-04-15 10:39:19 +0000 URL: https://git.openjdk.java.net/jdk/commit/bdf8a2a2050393e91800786f8d5a5d6805f936eb
15-04-2022

A pull request was submitted for review. URL: https://git.openjdk.java.net/jdk/pull/7865 Date: 2022-03-18 08:52:46 +0000
10-04-2022