[Hotspot] Refactor Reference and Cleaner handling to fix Unsafe nativ… - #162
Open
superajun-wsj wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
Refactors HotSpot and CVM class-library integration around Reference/Cleaner handling, aiming to reduce coupling to JDK 17-specific implementations and address Unsafe-related native link issues by removing/rewiring several alt-kernel classes and adding regression tests.
Changes:
- Gates HotSpot known-method initialization for
jdk.internal.misc.Unsafethrow helpers and stack-walker support behindHOTSPOT_TARGET_CLASSLIB == 17. - Removes a set of JDK 17-derived reference/cleaner/unsafe support classes from
cvm/alt_kernel/src17uand updatesReference/Threadbehavior accordingly. - Adds CVM tests covering reference forwarding, cleaner dispatch, and direct-buffer cleanup behavior.
Reviewed changes
Copilot reviewed 19 out of 20 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/hotspot/share/memory/universe.cpp | Conditionalizes known-method cache initialization for JDK17-specific helpers. |
| src/hotspot/share/classfile/vmClassMacros.hpp | Adjusts VM class list for classlib 8 vs non-8 targets (removes internal_Unsafe_klass from the 8 list). |
| cvm/test/java/nio/Buffer/DirectBufferCleanerTest.java | New test validating direct-buffer cleaner behavior and direct-memory accounting. |
| cvm/test/java/lang/ref/ReferenceForwardingTest.java | New test validating mixed pending-list forwarding for finalizers and phantom references. |
| cvm/test/java/lang/ref/CleanerReferenceForwardingTest.java | New test validating cleaner dispatch does not break phantom forwarding. |
| cvm/alt_kernel/src8u/share/classes/java/nio/Bits.java | Removes alt-kernel JDK8 Bits implementation. |
| cvm/alt_kernel/src17u/share/classes/sun/nio/cs/ArrayEncoder.java | Removes alt-kernel JDK17 charset fastpath interface. |
| cvm/alt_kernel/src17u/share/classes/sun/nio/cs/ArrayDecoder.java | Removes alt-kernel JDK17 charset fastpath interface. |
| cvm/alt_kernel/src17u/share/classes/sun/misc/JavaLangRefAccess.java | Removes alt-kernel sun.misc reference-access hook. |
| cvm/alt_kernel/src17u/share/classes/jdk/internal/util/ArraysSupport.java | Removes alt-kernel JDK17 array utilities. |
| cvm/alt_kernel/src17u/share/classes/jdk/internal/ref/PhantomCleanable.java | Removes alt-kernel JDK17 cleaner infrastructure. |
| cvm/alt_kernel/src17u/share/classes/jdk/internal/ref/CleanerImpl.java | Removes alt-kernel JDK17 cleaner runtime. |
| cvm/alt_kernel/src17u/share/classes/jdk/internal/ref/CleanerFactory.java | Removes alt-kernel JDK17 common-cleaner factory. |
| cvm/alt_kernel/src17u/share/classes/jdk/internal/misc/Unsafe.java | Removes alt-kernel JDK17 jdk.internal.misc.Unsafe copy. |
| cvm/alt_kernel/src17u/share/classes/jdk/internal/misc/InnocuousThread.java | Removes alt-kernel JDK17 innocuous thread implementation. |
| cvm/alt_kernel/src17u/share/classes/java/lang/Thread.java | Updates WeakClassKey.equals to avoid refersTo(...). |
| cvm/alt_kernel/src17u/share/classes/java/lang/ref/ReferenceQueue.java | Removes alt-kernel ReferenceQueue implementation. |
| cvm/alt_kernel/src17u/share/classes/java/lang/ref/Reference.java | Reworks reference processing, pending handling, and SharedSecrets wiring. |
| cvm/alt_kernel/src17u/share/classes/java/lang/ref/FinalReference.java | Removes alt-kernel FinalReference. |
| cvm/alt_kernel/src17u/share/classes/java/lang/ref/Cleaner.java | Removes alt-kernel java.lang.ref.Cleaner. |
Suppressed comments (2)
src/hotspot/share/memory/universe.cpp:909
- Wrapping initialization of
_throw_illegal_access_error_cache/_throw_no_such_method_error_cachein#if HOTSPOT_TARGET_CLASSLIB == 17leaves these caches uninitialized for other targets, but the returned methods are still used unconditionally elsewhere (e.g.,klassVtable.cpp:1342installsUniverse::throw_illegal_access_error()into itables, andjavaClasses.cpp:4356/resolvedMethodTable.cpp:356-359useUniverse::throw_no_such_method_error()for deleted methods). If the caches are never initialized, these accessors returnNULL, which changes behavior (itable entries remain empty; deleted method fallback becomesNULL) and can lead to incorrect exceptions or later crashes.
#if HOTSPOT_TARGET_CLASSLIB == 17
initialize_known_method(_throw_illegal_access_error_cache,
vmClasses::internal_Unsafe_klass(),
"throwIllegalAccessError",
vmSymbols::void_method_signature(), true, CHECK);
cvm/alt_kernel/src17u/share/classes/java/lang/ref/Reference.java:260
SharedSecrets.setJavaLangRefAccess(...)is wiring an API (tryHandlePendingReference) that does not exist on thejdk.internal.access.JavaLangRefAccessinterface in this repo (it expectswaitForReferenceProcessing()andrunFinalization()). This will not compile once the imports are corrected, and it also breaksBits/other callers that rely on the existingwaitForReferenceProcessingcontract.
SharedSecrets.setJavaLangRefAccess(new JavaLangRefAccess() {
@Override
public boolean tryHandlePendingReference() {
return tryHandlePending(false);
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import java.util.concurrent.atomic.AtomicIntegerArray; | ||
|
|
||
| import sun.misc.Cleaner; |
Comment on lines
+28
to
+32
| import sun.misc.Cleaner; | ||
| import sun.misc.JavaLangRefAccess; | ||
| import sun.misc.SharedSecrets; | ||
| import sun.misc.Cleaner; | ||
|
|
||
| import java.lang.ref.ReferenceQueue; |
superajun-wsj
force-pushed
the
bugfix/refactor-reference-cleaner-to-resolve-unsafe-link-bug-upstream
branch
6 times, most recently
from
August 7, 2026 09:53
979835c to
ed1da90
Compare
…e link errors Refactor the Reference and Cleaner handling logic in CVM to simplify the reference dispatch mechanism and remove unnecessary dependencies on JDK 17 Reference/Cleaner implementations. This change eliminates excessive coupling with JDK 17-specific classes and fundamentally resolves the Unsafe native link errors introduced by jdk.internal.misc.Cleaner.
superajun-wsj
force-pushed
the
bugfix/refactor-reference-cleaner-to-resolve-unsafe-link-bug-upstream
branch
from
August 10, 2026 08:58
ed1da90 to
5089293
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
…e link errors
Refactor the Reference and Cleaner handling logic in CVM to simplify the reference dispatch mechanism and remove unnecessary dependencies on JDK 17 Reference/Cleaner implementations. This change eliminates excessive coupling with JDK 17-specific classes and fundamentally resolves the Unsafe native link errors introduced by jdk.internal.misc.Cleaner.