From 1459d4eaf0a340f66b4c0e98eb1af5da533998fb Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Tue, 16 Mar 2021 22:40:34 +0800 Subject: [PATCH] Add access bridge --- .../unsafeaccessor/ProtectedObject.java | 17 ++++++ .../unsafeaccessor/SecurityCheck.java | 11 +--- .../unsafeaccessor/UnsafeAccess.java | 61 +++++++++++++++++++ 3 files changed, 79 insertions(+), 10 deletions(-) create mode 100644 api/src/main/java/io/github/karlatemp/unsafeaccessor/ProtectedObject.java create mode 100644 api/src/main/java/io/github/karlatemp/unsafeaccessor/UnsafeAccess.java diff --git a/api/src/main/java/io/github/karlatemp/unsafeaccessor/ProtectedObject.java b/api/src/main/java/io/github/karlatemp/unsafeaccessor/ProtectedObject.java new file mode 100644 index 0000000..92134fb --- /dev/null +++ b/api/src/main/java/io/github/karlatemp/unsafeaccessor/ProtectedObject.java @@ -0,0 +1,17 @@ +package io.github.karlatemp.unsafeaccessor; + +class ProtectedObject { + boolean trusted; + + void checkTrusted() { + if (!trusted) throw new SecurityException(); + } + + public ProtectedObject(boolean t) { + this.trusted = t; + } + + public ProtectedObject() { + this(true); + } +} diff --git a/api/src/main/java/io/github/karlatemp/unsafeaccessor/SecurityCheck.java b/api/src/main/java/io/github/karlatemp/unsafeaccessor/SecurityCheck.java index 786b69b..6ec2406 100644 --- a/api/src/main/java/io/github/karlatemp/unsafeaccessor/SecurityCheck.java +++ b/api/src/main/java/io/github/karlatemp/unsafeaccessor/SecurityCheck.java @@ -9,16 +9,12 @@ * * @since 1.3.0 */ -public final class SecurityCheck { - private final boolean trusted; +public final class SecurityCheck extends ProtectedObject { SecurityCheck() { trusted = true; } - SecurityCheck(boolean trusted) { - this.trusted = trusted; - } static final SecurityCheck INSTANCE = new SecurityCheck(); static Permission PERMISSION_GET_UNSAFE; @@ -33,11 +29,6 @@ public static SecurityCheck getInstance() { return INSTANCE; } - private void checkTrusted() { - if (!trusted) - throw new IllegalStateException(); - } - public Permission getPermission() { checkTrusted(); return PERMISSION_GET_UNSAFE; diff --git a/api/src/main/java/io/github/karlatemp/unsafeaccessor/UnsafeAccess.java b/api/src/main/java/io/github/karlatemp/unsafeaccessor/UnsafeAccess.java new file mode 100644 index 0000000..b6d8117 --- /dev/null +++ b/api/src/main/java/io/github/karlatemp/unsafeaccessor/UnsafeAccess.java @@ -0,0 +1,61 @@ +package io.github.karlatemp.unsafeaccessor; + +import java.lang.invoke.MethodHandles; +import java.lang.reflect.AccessibleObject; + +/** + * Direct unsafe access + *

+ * A bridge object to access unsafe. + *

+ * Unlike other methods, using this bridge will not trigger any permission checks. + * The permission check will only happen in {@link #getInstance()} + * + * @since 1.4.0 + */ +public final class UnsafeAccess extends ProtectedObject { + static final UnsafeAccess INSTANCE = new UnsafeAccess(); + + public static UnsafeAccess getInstance() { + SecurityCheck.getInstance(); + return INSTANCE; + } + + public SecurityCheck getSecuritySettings() { + checkTrusted(); + return SecurityCheck.INSTANCE; + } + + public Unsafe getUnsafe() { + checkTrusted(); + return Unsafe.getUnsafe0(); + } + + /** + * Use {@link #getTrustedIn(Class)} + * + * @return MethodHandles.Lookup.IMPL_LOOKUP + */ + @Deprecated + public MethodHandles.Lookup getTrusted() { + checkTrusted(); + return Root.RootLookupHolder.ROOT; + } + + public MethodHandles.Lookup getTrustedIn(Class target) { + checkTrusted(); + return Root.RootLookupHolder.trustedIn(target); + } + + public T openAccess(T object) { + checkTrusted(); + Root.OpenAccess.openAccess0(object, true); + return object; + } + + public T setAccessible(T object, boolean accessible) { + checkTrusted(); + Root.OpenAccess.openAccess0(object, accessible); + return object; + } +}