Skip to content

Replace sun.misc.Unsafe with MethodHandle for access control bypass#80

Open
chhsiao90 wants to merge 1 commit into
jhalterman:masterfrom
chhsiao90:unsafe
Open

Replace sun.misc.Unsafe with MethodHandle for access control bypass#80
chhsiao90 wants to merge 1 commit into
jhalterman:masterfrom
chhsiao90:unsafe

Conversation

@chhsiao90

@chhsiao90 chhsiao90 commented Feb 17, 2026

Copy link
Copy Markdown

Replace brittle reflection-based approaches with IMPL_LOOKUP obtained
through sun.misc.Unsafe raw memory reads, which bypasses module system
checks entirely and works across all Java 9-21 versions without --add-opens:

  • Java 9-15: replace Lookup constructor hack (setAccessible fails on
    Java 12+ due to AccessibleObject.override blacklist) with IMPL_LOOKUP
  • Java 16-21: replace objectFieldOffset(override)+putBoolean (fails on
    Java 21 where override is hidden from getDeclaredField) with IMPL_LOOKUP
  • Java 16+: MethodHandles.privateLookupIn when --add-opens is present
  • Java 22+ without --add-opens: throws UnsupportedOperationException
  • The --add-opens surefire profile activates only for Java 22+

sun.misc.Unsafe.theUnsafe is in the exported jdk.unsupported module so
setAccessible always succeeds. staticFieldOffset/staticFieldBase/getObject
operate at raw memory level, yielding the full-privileged IMPL_LOOKUP
which can find and set AccessibleObject.override even when hidden.

Also update minimum Java version from 1.6 to 1.8, add Java 21 to CI
matrix, and update GitHub Actions to v4.

Fixes: modelmapper/modelmapper#772

@chhsiao90 chhsiao90 changed the title Replace sun.misc.Unsafe with MethodHandle for access control bypass Draft: Replace sun.misc.Unsafe with MethodHandle for access control bypass Feb 17, 2026
… 9-21

Replace brittle reflection-based approaches with IMPL_LOOKUP obtained
through sun.misc.Unsafe raw memory reads, which bypasses module system
checks entirely and works across all Java 9-21 versions without --add-opens:

- Java 9-15: replace Lookup constructor hack (setAccessible fails on
  Java 12+ due to AccessibleObject.override blacklist) with IMPL_LOOKUP
- Java 16-21: replace objectFieldOffset(override)+putBoolean (fails on
  Java 21 where override is hidden from getDeclaredField) with IMPL_LOOKUP
- Java 16+: MethodHandles.privateLookupIn when --add-opens is present
- Java 22+ without --add-opens: throws UnsupportedOperationException
- The --add-opens surefire profile activates only for Java 22+

sun.misc.Unsafe.theUnsafe is in the exported jdk.unsupported module so
setAccessible always succeeds. staticFieldOffset/staticFieldBase/getObject
operate at raw memory level, yielding the full-privileged IMPL_LOOKUP
which can find and set AccessibleObject.override even when hidden.

Also update minimum Java version from 1.6 to 1.8, add Java 21 to CI
matrix, and update GitHub Actions to v4.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@chhsiao90 chhsiao90 changed the title Draft: Replace sun.misc.Unsafe with MethodHandle for access control bypass Replace sun.misc.Unsafe with MethodHandle for access control bypass Feb 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sun.misc.Unsafe

1 participant