Hi,

Please review the patch below.

Since Jigsaw encapsulates jdk.internal.misc.Unsafe there is no need to perform 
any runtime checks such as:

- hiding the getUnsafe method from reflection; and
- checking the class loader of the calling class for invocation of getUnsafe.

This more cleanly enables qualified modules to utilise the internal Unsafe 
without reflection gymnastics, even though we should be reviewing that usage 
and to reduce it where possible.

I also took the opportunity to hide the “theInternalUnsafe” field in the 
sun.misc.Unsafe. That just avoids any futile attempts to obtain the field’s 
value after which any reflective access on that value will fail.

Paul.

diff -r 4f5f82c457af src/java.base/share/classes/jdk/internal/misc/Unsafe.java
--- a/src/java.base/share/classes/jdk/internal/misc/Unsafe.java Mon Jul 18 
13:13:52 2016 +0800
+++ b/src/java.base/share/classes/jdk/internal/misc/Unsafe.java Mon Jul 18 
17:50:20 2016 +0200
@@ -26,8 +26,6 @@
 package jdk.internal.misc;

 import jdk.internal.HotSpotIntrinsicCandidate;
-import jdk.internal.reflect.CallerSensitive;
-import jdk.internal.reflect.Reflection;
 import jdk.internal.vm.annotation.ForceInline;

 import java.lang.reflect.Field;
@@ -57,7 +55,6 @@
     private static native void registerNatives();
     static {
         registerNatives();
-        Reflection.registerMethodsToFilter(Unsafe.class, "getUnsafe");
     }

     private Unsafe() {}
@@ -87,16 +84,8 @@
      * }}</pre>
      *
      * (It may assist compilers to make the local variable {@code final}.)
-     *
-     * @throws  SecurityException if the class loader of the caller
-     *          class is not in the system domain in which all permissions
-     *          are granted.
      */
-    @CallerSensitive
     public static Unsafe getUnsafe() {
-        Class<?> caller = Reflection.getCallerClass();
-        if (!VM.isSystemDomainLoader(caller.getClassLoader()))
-            throw new SecurityException("Unsafe");
         return theUnsafe;
     }

diff -r 4f5f82c457af src/jdk.unsupported/share/classes/sun/misc/Unsafe.java
--- a/src/jdk.unsupported/share/classes/sun/misc/Unsafe.java    Mon Jul 18 
13:13:52 2016 +0800
+++ b/src/jdk.unsupported/share/classes/sun/misc/Unsafe.java    Mon Jul 18 
17:50:20 2016 +0200
@@ -56,6 +56,7 @@

     static {
         Reflection.registerMethodsToFilter(Unsafe.class, "getUnsafe");
+        Reflection.registerFieldsToFilter(Unsafe.class, "theInternalUnsafe");
     }

     private Unsafe() {}

Reply via email to