changeset 13582:be5006687c43

Add initialization checks to Unsafe.allocateInstance intrinsic
author Gilles Duboscq <duboscq@ssw.jku.at>
date Thu, 09 Jan 2014 16:29:32 +0100
parents 37b14ac9c5fb
children 5ca667814056
files graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/replacements/HotSpotReplacementsUtil.java graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/replacements/NewObjectSnippets.java graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/stubs/NewInstanceStub.java graal/com.oracle.graal.jtt/src/com/oracle/graal/jtt/jdk/UnsafeAllocateInstance01.java graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/java/DynamicNewInstanceNode.java
diffstat 5 files changed, 26 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/replacements/HotSpotReplacementsUtil.java	Thu Jan 09 15:11:34 2014 +0100
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/replacements/HotSpotReplacementsUtil.java	Thu Jan 09 16:29:32 2014 +0100
@@ -24,7 +24,7 @@
 
 import static com.oracle.graal.graph.UnsafeAccess.*;
 import static com.oracle.graal.hotspot.HotSpotGraalRuntime.*;
-import static com.oracle.graal.hotspot.meta.HotSpotHostForeignCallsProvider.*;
+import static com.oracle.graal.hotspot.meta.HotSpotForeignCallsProviderImpl.*;
 import static com.oracle.graal.nodes.extended.BranchProbabilityNode.*;
 import sun.misc.*;
 
@@ -506,6 +506,14 @@
         return config().klassStateFullyInitialized;
     }
 
+    public static boolean isKlassFullyInitialized(Word hub) {
+        return readKlassState(hub) == klassStateFullyInitialized();
+    }
+
+    public static byte readKlassState(Word hub) {
+        return hub.readByte(klassStateOffset(), CLASS_STATE_LOCATION);
+    }
+
     @Fold
     public static int klassModifierFlagsOffset() {
         return config().klassModifierFlagsOffset;
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/replacements/NewObjectSnippets.java	Thu Jan 09 15:11:34 2014 +0100
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/replacements/NewObjectSnippets.java	Thu Jan 09 16:29:32 2014 +0100
@@ -152,16 +152,18 @@
     public static Object allocateInstanceDynamic(Class<?> type, @ConstantParameter boolean fillContents, @ConstantParameter Register threadRegister, @ConstantParameter String typeContext) {
         Word hub = loadWordFromObject(type, klassOffset());
         if (!hub.equal(Word.zero())) {
-            int layoutHelper = readLayoutHelper(hub);
-            /*
-             * src/share/vm/oops/klass.hpp: For instances, layout helper is a positive number, the
-             * instance size. This size is already passed through align_object_size and scaled to
-             * bytes. The low order bit is set if instances of this class cannot be allocated using
-             * the fastpath.
-             */
-            if ((layoutHelper & 1) == 0) {
-                Word prototypeMarkWord = hub.readWord(prototypeMarkWordOffset(), PROTOTYPE_MARK_WORD_LOCATION);
-                return allocateInstance(layoutHelper, hub, prototypeMarkWord, fillContents, threadRegister, false, typeContext);
+            if (isKlassFullyInitialized(hub)) {
+                int layoutHelper = readLayoutHelper(hub);
+                /*
+                 * src/share/vm/oops/klass.hpp: For instances, layout helper is a positive number,
+                 * the instance size. This size is already passed through align_object_size and
+                 * scaled to bytes. The low order bit is set if instances of this class cannot be
+                 * allocated using the fastpath.
+                 */
+                if ((layoutHelper & 1) == 0) {
+                    Word prototypeMarkWord = hub.readWord(prototypeMarkWordOffset(), PROTOTYPE_MARK_WORD_LOCATION);
+                    return allocateInstance(layoutHelper, hub, prototypeMarkWord, fillContents, threadRegister, false, typeContext);
+                }
             }
         }
         return dynamicNewInstanceStub(type);
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/stubs/NewInstanceStub.java	Thu Jan 09 15:11:34 2014 +0100
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/stubs/NewInstanceStub.java	Thu Jan 09 16:29:32 2014 +0100
@@ -107,7 +107,7 @@
         int sizeInBytes = hub.readInt(klassInstanceSizeOffset(), LocationIdentity.FINAL_LOCATION);
         Word thread = registerAsWord(threadRegister);
         if (!forceSlowPath() && inlineContiguousAllocationSupported()) {
-            if (hub.readByte(klassStateOffset(), CLASS_STATE_LOCATION) == klassStateFullyInitialized()) {
+            if (isKlassFullyInitialized(hub)) {
                 Word memory = refillAllocate(thread, intArrayHub, sizeInBytes, logging());
                 if (memory.notEqual(0)) {
                     Word prototypeMarkWord = hub.readWord(prototypeMarkWordOffset(), PROTOTYPE_MARK_WORD_LOCATION);
--- a/graal/com.oracle.graal.jtt/src/com/oracle/graal/jtt/jdk/UnsafeAllocateInstance01.java	Thu Jan 09 15:11:34 2014 +0100
+++ b/graal/com.oracle.graal.jtt/src/com/oracle/graal/jtt/jdk/UnsafeAllocateInstance01.java	Thu Jan 09 16:29:32 2014 +0100
@@ -35,10 +35,6 @@
  */
 public class UnsafeAllocateInstance01 extends JTTTest {
 
-    private static abstract class AbstractClass {
-
-    }
-
     int field01 = 42;
 
     public static int testInstance() throws SecurityException, InstantiationException {
@@ -79,7 +75,7 @@
 
     @Test
     public void run2() throws Throwable {
-        runTest("testClassForException", AbstractClass.class);
+        runTest("testClassForException", AbstractList.class);
     }
 
     @Test
--- a/graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/java/DynamicNewInstanceNode.java	Thu Jan 09 15:11:34 2014 +0100
+++ b/graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/java/DynamicNewInstanceNode.java	Thu Jan 09 16:29:32 2014 +0100
@@ -44,7 +44,9 @@
             if (clazzConstant.getKind() == Kind.Object && clazzConstant.asObject() instanceof Class) {
                 Class staticClass = (Class) clazzConstant.asObject();
                 ResolvedJavaType type = tool.getMetaAccess().lookupJavaType(staticClass);
-                return new NewInstanceNode(type, fillContents());
+                if (type.isInitialized()) {
+                    return new NewInstanceNode(type, fillContents());
+                }
             }
         }
         return super.canonical(tool);