changeset 22541:d2035d841611

restricted Unsafe capability access to package or class scope
author Doug Simon <doug.simon@oracle.com>
date Wed, 09 Sep 2015 12:22:15 +0200
parents df2764d92a3c
children 15579668ec3a
files jvmci/jdk.internal.jvmci.common/src/jdk/internal/jvmci/common/UnsafeAccess.java jvmci/jdk.internal.jvmci.hotspot/src/jdk/internal/jvmci/hotspot/HotSpotConstantPool.java jvmci/jdk.internal.jvmci.hotspot/src/jdk/internal/jvmci/hotspot/HotSpotInstalledCode.java jvmci/jdk.internal.jvmci.hotspot/src/jdk/internal/jvmci/hotspot/HotSpotMemoryAccessProviderImpl.java jvmci/jdk.internal.jvmci.hotspot/src/jdk/internal/jvmci/hotspot/HotSpotMetaAccessProvider.java jvmci/jdk.internal.jvmci.hotspot/src/jdk/internal/jvmci/hotspot/HotSpotMethodData.java jvmci/jdk.internal.jvmci.hotspot/src/jdk/internal/jvmci/hotspot/HotSpotResolvedJavaMethodImpl.java jvmci/jdk.internal.jvmci.hotspot/src/jdk/internal/jvmci/hotspot/HotSpotResolvedObjectTypeImpl.java jvmci/jdk.internal.jvmci.hotspot/src/jdk/internal/jvmci/hotspot/HotSpotVMConfig.java jvmci/jdk.internal.jvmci.hotspot/src/jdk/internal/jvmci/hotspot/HotSpotVmSymbols.java jvmci/jdk.internal.jvmci.hotspot/src/jdk/internal/jvmci/hotspot/UnsafeAccess.java
diffstat 11 files changed, 71 insertions(+), 61 deletions(-) [+]
line wrap: on
line diff
--- a/jvmci/jdk.internal.jvmci.common/src/jdk/internal/jvmci/common/UnsafeAccess.java	Tue Sep 08 18:08:38 2015 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,45 +0,0 @@
-/*
- * Copyright (c) 2012, 2012, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-package jdk.internal.jvmci.common;
-
-import java.lang.reflect.Field;
-
-import sun.misc.Unsafe;
-
-public class UnsafeAccess {
-
-    /**
-     * An instance of {@link Unsafe} for use within JVMCI.
-     */
-    public static final Unsafe unsafe;
-
-    static {
-        try {
-            Field theUnsafeInstance = Unsafe.class.getDeclaredField("theUnsafe");
-            theUnsafeInstance.setAccessible(true);
-            unsafe = (Unsafe) theUnsafeInstance.get(Unsafe.class);
-        } catch (Exception e) {
-            throw new RuntimeException("exception while trying to get Unsafe", e);
-        }
-    }
-}
--- a/jvmci/jdk.internal.jvmci.hotspot/src/jdk/internal/jvmci/hotspot/HotSpotConstantPool.java	Tue Sep 08 18:08:38 2015 -0700
+++ b/jvmci/jdk.internal.jvmci.hotspot/src/jdk/internal/jvmci/hotspot/HotSpotConstantPool.java	Wed Sep 09 12:22:15 2015 +0200
@@ -22,8 +22,8 @@
  */
 package jdk.internal.jvmci.hotspot;
 
-import static jdk.internal.jvmci.common.UnsafeAccess.*;
 import static jdk.internal.jvmci.hotspot.HotSpotJVMCIRuntime.*;
+import static jdk.internal.jvmci.hotspot.UnsafeAccess.unsafe;
 
 import java.lang.invoke.*;
 
--- a/jvmci/jdk.internal.jvmci.hotspot/src/jdk/internal/jvmci/hotspot/HotSpotInstalledCode.java	Tue Sep 08 18:08:38 2015 -0700
+++ b/jvmci/jdk.internal.jvmci.hotspot/src/jdk/internal/jvmci/hotspot/HotSpotInstalledCode.java	Wed Sep 09 12:22:15 2015 +0200
@@ -22,10 +22,10 @@
  */
 package jdk.internal.jvmci.hotspot;
 
-import static jdk.internal.jvmci.common.UnsafeAccess.*;
-import jdk.internal.jvmci.code.*;
-import jdk.internal.jvmci.inittimer.*;
-import sun.misc.*;
+import static jdk.internal.jvmci.hotspot.UnsafeAccess.unsafe;
+import jdk.internal.jvmci.code.InstalledCode;
+import jdk.internal.jvmci.inittimer.SuppressFBWarnings;
+import sun.misc.Unsafe;
 
 /**
  * Implementation of {@link InstalledCode} for HotSpot.
--- a/jvmci/jdk.internal.jvmci.hotspot/src/jdk/internal/jvmci/hotspot/HotSpotMemoryAccessProviderImpl.java	Tue Sep 08 18:08:38 2015 -0700
+++ b/jvmci/jdk.internal.jvmci.hotspot/src/jdk/internal/jvmci/hotspot/HotSpotMemoryAccessProviderImpl.java	Wed Sep 09 12:22:15 2015 +0200
@@ -22,11 +22,15 @@
  */
 package jdk.internal.jvmci.hotspot;
 
-import static jdk.internal.jvmci.common.UnsafeAccess.*;
-import jdk.internal.jvmci.code.*;
-import jdk.internal.jvmci.common.*;
-import jdk.internal.jvmci.hotspot.HotSpotVMConfig.*;
-import jdk.internal.jvmci.meta.*;
+import static jdk.internal.jvmci.hotspot.UnsafeAccess.unsafe;
+import jdk.internal.jvmci.code.TargetDescription;
+import jdk.internal.jvmci.common.JVMCIError;
+import jdk.internal.jvmci.hotspot.HotSpotVMConfig.CompressEncoding;
+import jdk.internal.jvmci.meta.Constant;
+import jdk.internal.jvmci.meta.JavaConstant;
+import jdk.internal.jvmci.meta.JavaKind;
+import jdk.internal.jvmci.meta.MemoryAccessProvider;
+import jdk.internal.jvmci.meta.PrimitiveConstant;
 
 /**
  * HotSpot implementation of {@link MemoryAccessProvider}.
--- a/jvmci/jdk.internal.jvmci.hotspot/src/jdk/internal/jvmci/hotspot/HotSpotMetaAccessProvider.java	Tue Sep 08 18:08:38 2015 -0700
+++ b/jvmci/jdk.internal.jvmci.hotspot/src/jdk/internal/jvmci/hotspot/HotSpotMetaAccessProvider.java	Wed Sep 09 12:22:15 2015 +0200
@@ -22,8 +22,8 @@
  */
 package jdk.internal.jvmci.hotspot;
 
-import static jdk.internal.jvmci.common.UnsafeAccess.*;
 import static jdk.internal.jvmci.hotspot.HotSpotResolvedObjectTypeImpl.*;
+import static jdk.internal.jvmci.hotspot.UnsafeAccess.unsafe;
 
 import java.lang.reflect.*;
 
--- a/jvmci/jdk.internal.jvmci.hotspot/src/jdk/internal/jvmci/hotspot/HotSpotMethodData.java	Tue Sep 08 18:08:38 2015 -0700
+++ b/jvmci/jdk.internal.jvmci.hotspot/src/jdk/internal/jvmci/hotspot/HotSpotMethodData.java	Wed Sep 09 12:22:15 2015 +0200
@@ -23,8 +23,8 @@
 package jdk.internal.jvmci.hotspot;
 
 import static java.lang.String.*;
-import static jdk.internal.jvmci.common.UnsafeAccess.*;
 import static jdk.internal.jvmci.hotspot.HotSpotJVMCIRuntime.*;
+import static jdk.internal.jvmci.hotspot.UnsafeAccess.unsafe;
 
 import java.util.*;
 
--- a/jvmci/jdk.internal.jvmci.hotspot/src/jdk/internal/jvmci/hotspot/HotSpotResolvedJavaMethodImpl.java	Tue Sep 08 18:08:38 2015 -0700
+++ b/jvmci/jdk.internal.jvmci.hotspot/src/jdk/internal/jvmci/hotspot/HotSpotResolvedJavaMethodImpl.java	Wed Sep 09 12:22:15 2015 +0200
@@ -22,9 +22,9 @@
  */
 package jdk.internal.jvmci.hotspot;
 
-import static jdk.internal.jvmci.common.UnsafeAccess.*;
 import static jdk.internal.jvmci.hotspot.HotSpotJVMCIRuntime.*;
 import static jdk.internal.jvmci.hotspot.HotSpotResolvedJavaMethodImpl.Options.*;
+import static jdk.internal.jvmci.hotspot.UnsafeAccess.unsafe;
 
 import java.lang.annotation.*;
 import java.lang.reflect.*;
--- a/jvmci/jdk.internal.jvmci.hotspot/src/jdk/internal/jvmci/hotspot/HotSpotResolvedObjectTypeImpl.java	Tue Sep 08 18:08:38 2015 -0700
+++ b/jvmci/jdk.internal.jvmci.hotspot/src/jdk/internal/jvmci/hotspot/HotSpotResolvedObjectTypeImpl.java	Wed Sep 09 12:22:15 2015 +0200
@@ -23,8 +23,8 @@
 package jdk.internal.jvmci.hotspot;
 
 import static java.util.Objects.*;
-import static jdk.internal.jvmci.common.UnsafeAccess.*;
 import static jdk.internal.jvmci.hotspot.HotSpotJVMCIRuntime.*;
+import static jdk.internal.jvmci.hotspot.UnsafeAccess.unsafe;
 
 import java.lang.annotation.*;
 import java.lang.reflect.*;
--- a/jvmci/jdk.internal.jvmci.hotspot/src/jdk/internal/jvmci/hotspot/HotSpotVMConfig.java	Tue Sep 08 18:08:38 2015 -0700
+++ b/jvmci/jdk.internal.jvmci.hotspot/src/jdk/internal/jvmci/hotspot/HotSpotVMConfig.java	Wed Sep 09 12:22:15 2015 +0200
@@ -22,8 +22,8 @@
  */
 package jdk.internal.jvmci.hotspot;
 
-import static jdk.internal.jvmci.common.UnsafeAccess.*;
 import static jdk.internal.jvmci.common.UnsafeUtil.readCString;
+import static jdk.internal.jvmci.hotspot.UnsafeAccess.unsafe;
 
 import java.lang.reflect.*;
 import java.util.*;
--- a/jvmci/jdk.internal.jvmci.hotspot/src/jdk/internal/jvmci/hotspot/HotSpotVmSymbols.java	Tue Sep 08 18:08:38 2015 -0700
+++ b/jvmci/jdk.internal.jvmci.hotspot/src/jdk/internal/jvmci/hotspot/HotSpotVmSymbols.java	Wed Sep 09 12:22:15 2015 +0200
@@ -22,8 +22,8 @@
  */
 package jdk.internal.jvmci.hotspot;
 
-import static jdk.internal.jvmci.common.UnsafeAccess.*;
 import static jdk.internal.jvmci.hotspot.HotSpotJVMCIRuntime.*;
+import static jdk.internal.jvmci.hotspot.UnsafeAccess.unsafe;
 import sun.misc.*;
 
 /**
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jvmci/jdk.internal.jvmci.hotspot/src/jdk/internal/jvmci/hotspot/UnsafeAccess.java	Wed Sep 09 12:22:15 2015 +0200
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2012, 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package jdk.internal.jvmci.hotspot;
+
+import java.lang.reflect.Field;
+
+import sun.misc.Unsafe;
+
+/**
+ * Package private access to the {@link Unsafe} capability.
+ */
+class UnsafeAccess {
+
+    static final Unsafe unsafe = initUnsafe();
+
+    private static Unsafe initUnsafe() {
+        try {
+            // Fast path when we are trusted.
+            return Unsafe.getUnsafe();
+        } catch (SecurityException se) {
+            // Slow path when we are not trusted.
+            try {
+                Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
+                theUnsafe.setAccessible(true);
+                return (Unsafe) theUnsafe.get(Unsafe.class);
+            } catch (Exception e) {
+                throw new RuntimeException("exception while trying to get Unsafe", e);
+            }
+        }
+    }
+}