[GEODE-10519] Security : Remove Unsafe Reflection Breaking Java Module System Encapsulation #7954
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.
Summary
This PR eliminates reflection-based access to private
java.langinternals inUnsafeThreadLocal, removing the requirement for the--add-opens=java.base/java.lang=ALL-UNNAMEDJVM flag. The refactoring replaces the fragile reflection approach with aWeakHashMap-based implementation that is safer, more maintainable, and fully compliant with the Java Platform Module System (JPMS).Security & Compliance Benefits
Module System Compliance
--add-opensflagsSecurity Hardening
java.langpackage to all modulesOperational Benefits
Technical Changes
Files Modified (3 files, 49 insertions(+), 59 deletions(-))
1. geode-core/src/main/java/.../deadlock/UnsafeThreadLocal.java (100 lines changed)
Before: Used reflection to access private ThreadLocal internals
After: Uses WeakHashMap to track thread-to-value mappings
Key improvements:
invokePrivate,getPrivatemethods)WeakHashMap<Thread, T>for cross-thread accessset()andremove()to maintain WeakHashMap consistencyget(Thread)to use map lookup instead of reflection2. geode-gfsh/src/main/java/.../commands/MemberJvmOptions.java (6 lines deleted)
JAVA_LANG_OPENconstant and importJAVA_11_OPTIONSlist from 5 to 4 flags--add-opens=java.base/java.lang=ALL-UNNAMED3. build-tools/scripts/src/main/groovy/geode-test.gradle (1 line changed)
--add-opens=java.base/java.lang=ALL-UNNAMEDfrom test JVM argsArchitecture Decision
Why WeakHashMap?
Collections.synchronizedMap()Why not alternatives?
Validation & Testing
Test Coverage
Backward Compatibility
Public API preserved - No changes to method signatures
Behavior unchanged - Functionally equivalent to reflection-based approach
Consumer files unchanged - DLockService, MessageDependencyMonitor, DLockDependencyMonitor require no modifications
Deadlock detection accuracy - No regression in detection or timing
Impact Analysis
No Changes Required For
These files use
UnsafeThreadLocalbut required zero modifications because the public API was preserved:This demonstrates successful encapsulation - internal implementation changed while external contract remained stable.
Performance Considerations
Maintenance Benefits
Related Work
Future Opportunities
This PR is part of a broader initiative to eliminate reflection-based access across Geode:
--add-opens=java.base/java.nio=ALL-UNNAMEDJava Version Support
Checklist
Acceptance Criteria (All Met)
Additional Context
Why This Matters
java.langpackage with--add-opensweakens module boundariesHistorical Context
The original
UnsafeThreadLocalimplementation was created when Java module system didn't exist. With modern Java (11+), the JPMS violations became problematic:--add-opensflagsThis refactoring modernizes the implementation while maintaining all functionality.
Total changes: 3 files, 49 insertions(+), 59 deletions(-), ~66% code rewrite
For all changes, please confirm:
develop)?gradlew buildrun cleanly?