8150465: Unsafe methods to produce uninitialized arrays
authorshade
Thu, 03 Mar 2016 23:57:29 +0300
changeset 36553 203b2b5d149b
parent 36552 0db9be43cebb
child 36554 a7eb9ee4680c
8150465: Unsafe methods to produce uninitialized arrays Reviewed-by: jrose, kvn, psandoz, aph, twisti, flar
hotspot/src/share/vm/classfile/vmSymbols.hpp
hotspot/src/share/vm/opto/c2compiler.cpp
hotspot/src/share/vm/opto/library_call.cpp
hotspot/test/compiler/intrinsics/unsafe/AllocateUninitializedArray.java
--- a/hotspot/src/share/vm/classfile/vmSymbols.hpp	Thu Mar 03 22:17:38 2016 +0300
+++ b/hotspot/src/share/vm/classfile/vmSymbols.hpp	Thu Mar 03 23:57:29 2016 +0300
@@ -1040,6 +1040,8 @@
   do_intrinsic(_allocateInstance,         jdk_internal_misc_Unsafe,     allocateInstance_name, allocateInstance_signature, F_RN) \
    do_name(     allocateInstance_name,                                  "allocateInstance")                                      \
    do_signature(allocateInstance_signature,                             "(Ljava/lang/Class;)Ljava/lang/Object;")                 \
+  do_intrinsic(_allocateUninitializedArray, jdk_internal_misc_Unsafe,   allocateUninitializedArray_name, newArray_signature,  F_R) \
+   do_name(     allocateUninitializedArray_name,                        "allocateUninitializedArray0")                           \
   do_intrinsic(_copyMemory,               jdk_internal_misc_Unsafe,     copyMemory_name, copyMemory_signature,         F_RN)     \
    do_name(     copyMemory_name,                                        "copyMemory0")                                           \
    do_signature(copyMemory_signature,                                   "(Ljava/lang/Object;JLjava/lang/Object;JJ)V")            \
--- a/hotspot/src/share/vm/opto/c2compiler.cpp	Thu Mar 03 22:17:38 2016 +0300
+++ b/hotspot/src/share/vm/opto/c2compiler.cpp	Thu Mar 03 23:57:29 2016 +0300
@@ -500,6 +500,7 @@
   case vmIntrinsics::_currentTimeMillis:
   case vmIntrinsics::_nanoTime:
   case vmIntrinsics::_allocateInstance:
+  case vmIntrinsics::_allocateUninitializedArray:
   case vmIntrinsics::_newArray:
   case vmIntrinsics::_getLength:
   case vmIntrinsics::_copyOf:
--- a/hotspot/src/share/vm/opto/library_call.cpp	Thu Mar 03 22:17:38 2016 +0300
+++ b/hotspot/src/share/vm/opto/library_call.cpp	Thu Mar 03 23:57:29 2016 +0300
@@ -247,6 +247,7 @@
   bool inline_unsafe_access(bool is_native_ptr, bool is_store, BasicType type, AccessKind kind, bool is_unaligned);
   static bool klass_needs_init_guard(Node* kls);
   bool inline_unsafe_allocate();
+  bool inline_unsafe_newArray(bool uninitialized);
   bool inline_unsafe_copyMemory();
   bool inline_native_currentThread();
 #ifdef TRACE_HAVE_INTRINSICS
@@ -257,8 +258,6 @@
   bool inline_native_isInterrupted();
   bool inline_native_Class_query(vmIntrinsics::ID id);
   bool inline_native_subtype_check();
-
-  bool inline_native_newArray();
   bool inline_native_getLength();
   bool inline_array_copyOf(bool is_copyOfRange);
   bool inline_array_equals(StrIntrinsicNode::ArgEnc ae);
@@ -715,7 +714,6 @@
   case vmIntrinsics::_nanoTime:                 return inline_native_time_funcs(CAST_FROM_FN_PTR(address, os::javaTimeNanos), "nanoTime");
   case vmIntrinsics::_allocateInstance:         return inline_unsafe_allocate();
   case vmIntrinsics::_copyMemory:               return inline_unsafe_copyMemory();
-  case vmIntrinsics::_newArray:                 return inline_native_newArray();
   case vmIntrinsics::_getLength:                return inline_native_getLength();
   case vmIntrinsics::_copyOf:                   return inline_array_copyOf(false);
   case vmIntrinsics::_copyOfRange:              return inline_array_copyOf(true);
@@ -724,6 +722,9 @@
   case vmIntrinsics::_Objects_checkIndex:       return inline_objects_checkIndex();
   case vmIntrinsics::_clone:                    return inline_native_clone(intrinsic()->is_virtual());
 
+  case vmIntrinsics::_allocateUninitializedArray: return inline_unsafe_newArray(true);
+  case vmIntrinsics::_newArray:                   return inline_unsafe_newArray(false);
+
   case vmIntrinsics::_isAssignableFrom:         return inline_native_subtype_check();
 
   case vmIntrinsics::_isInstance:
@@ -3829,9 +3830,17 @@
 
 //-----------------------inline_native_newArray--------------------------
 // private static native Object java.lang.reflect.newArray(Class<?> componentType, int length);
-bool LibraryCallKit::inline_native_newArray() {
-  Node* mirror    = argument(0);
-  Node* count_val = argument(1);
+// private        native Object Unsafe.allocateUninitializedArray0(Class<?> cls, int size);
+bool LibraryCallKit::inline_unsafe_newArray(bool uninitialized) {
+  Node* mirror;
+  Node* count_val;
+  if (uninitialized) {
+    mirror    = argument(1);
+    count_val = argument(2);
+  } else {
+    mirror    = argument(0);
+    count_val = argument(1);
+  }
 
   mirror = null_check(mirror);
   // If mirror or obj is dead, only null-path is taken.
@@ -3876,6 +3885,12 @@
     result_val->init_req(_normal_path, obj);
     result_io ->init_req(_normal_path, i_o());
     result_mem->init_req(_normal_path, reset_memory());
+
+    if (uninitialized) {
+      // Mark the allocation so that zeroing is skipped
+      AllocateArrayNode* alloc = AllocateArrayNode::Ideal_array_allocation(obj, &_gvn);
+      alloc->maybe_set_complete(&_gvn);
+    }
   }
 
   // Return the combined state.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hotspot/test/compiler/intrinsics/unsafe/AllocateUninitializedArray.java	Thu Mar 03 23:57:29 2016 +0300
@@ -0,0 +1,213 @@
+/*
+ * Copyright (c) 2016, 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.
+ *
+ */
+
+/*
+ * @test
+ * @bug 8150465
+ * @summary Unsafe methods to produce uninitialized arrays
+ * @modules java.base/jdk.internal.misc
+ * @run main/othervm -ea -Diters=200   -Xint                   AllocateUninitializedArray
+ * @run main/othervm -ea -Diters=30000 -XX:TieredStopAtLevel=1 AllocateUninitializedArray
+ * @run main/othervm -ea -Diters=30000 -XX:TieredStopAtLevel=4 AllocateUninitializedArray
+ */
+import java.lang.reflect.Field;
+import java.lang.reflect.Array;
+import java.util.concurrent.Callable;
+
+public class AllocateUninitializedArray {
+    static final int ITERS = Integer.getInteger("iters", 1);
+    static final jdk.internal.misc.Unsafe UNSAFE;
+
+    static {
+        try {
+            Field f = jdk.internal.misc.Unsafe.class.getDeclaredField("theUnsafe");
+            f.setAccessible(true);
+            UNSAFE = (jdk.internal.misc.Unsafe) f.get(null);
+        } catch (Exception e) {
+            throw new RuntimeException("Unable to get Unsafe instance.", e);
+        }
+    }
+
+    public static void main(String... args) throws Exception {
+        testIAE(AllConstants::testObject);
+        testIAE(LengthIsConstant::testObject);
+        testIAE(ClassIsConstant::testObject);
+        testIAE(NothingIsConstant::testObject);
+
+        testIAE(AllConstants::testArray);
+        testIAE(LengthIsConstant::testArray);
+        testIAE(ClassIsConstant::testArray);
+        testIAE(NothingIsConstant::testArray);
+
+        testIAE(AllConstants::testNull);
+        testIAE(LengthIsConstant::testNull);
+        testIAE(ClassIsConstant::testNull);
+        testIAE(NothingIsConstant::testNull);
+
+        testOK(boolean[].class, 10, AllConstants::testBoolean);
+        testOK(byte[].class,    10, AllConstants::testByte);
+        testOK(short[].class,   10, AllConstants::testShort);
+        testOK(char[].class,    10, AllConstants::testChar);
+        testOK(int[].class,     10, AllConstants::testInt);
+        testOK(float[].class,   10, AllConstants::testFloat);
+        testOK(long[].class,    10, AllConstants::testLong);
+        testOK(double[].class,  10, AllConstants::testDouble);
+
+        testOK(boolean[].class, 10, LengthIsConstant::testBoolean);
+        testOK(byte[].class,    10, LengthIsConstant::testByte);
+        testOK(short[].class,   10, LengthIsConstant::testShort);
+        testOK(char[].class,    10, LengthIsConstant::testChar);
+        testOK(int[].class,     10, LengthIsConstant::testInt);
+        testOK(float[].class,   10, LengthIsConstant::testFloat);
+        testOK(long[].class,    10, LengthIsConstant::testLong);
+        testOK(double[].class,  10, LengthIsConstant::testDouble);
+
+        testOK(boolean[].class, 10, ClassIsConstant::testBoolean);
+        testOK(byte[].class,    10, ClassIsConstant::testByte);
+        testOK(short[].class,   10, ClassIsConstant::testShort);
+        testOK(char[].class,    10, ClassIsConstant::testChar);
+        testOK(int[].class,     10, ClassIsConstant::testInt);
+        testOK(float[].class,   10, ClassIsConstant::testFloat);
+        testOK(long[].class,    10, ClassIsConstant::testLong);
+        testOK(double[].class,  10, ClassIsConstant::testDouble);
+
+        testOK(boolean[].class, 10, NothingIsConstant::testBoolean);
+        testOK(byte[].class,    10, NothingIsConstant::testByte);
+        testOK(short[].class,   10, NothingIsConstant::testShort);
+        testOK(char[].class,    10, NothingIsConstant::testChar);
+        testOK(int[].class,     10, NothingIsConstant::testInt);
+        testOK(float[].class,   10, NothingIsConstant::testFloat);
+        testOK(long[].class,    10, NothingIsConstant::testLong);
+        testOK(double[].class,  10, NothingIsConstant::testDouble);
+    }
+
+    public static void testOK(Class<?> expectClass, int expectLen, Callable<Object> test) throws Exception {
+        for (int c = 0; c < ITERS; c++) {
+            Object res = test.call();
+            Class<?> actualClass = res.getClass();
+            if (!actualClass.equals(expectClass)) {
+                throw new IllegalStateException("Wrong class: expected = " + expectClass + ", but got " + actualClass);
+            }
+            int actualLen = Array.getLength(res);
+            if (actualLen != expectLen) {
+                throw new IllegalStateException("Wrong length: expected = " + expectLen + ", but got " + actualLen);
+            }
+        }
+    }
+
+    static volatile Object sink;
+
+    public static void testIAE(Callable<Object> test) throws Exception {
+        for (int c = 0; c < ITERS; c++) {
+            try {
+               sink = test.call();
+               throw new IllegalStateException("Expected IAE");
+            } catch (IllegalArgumentException iae) {
+               // expected
+            }
+        }
+    }
+
+    static volatile int sampleLenNeg  = -1;
+    static volatile int sampleLenZero = 0;
+    static volatile int sampleLen     = 10;
+
+
+    static volatile Class<?> classBoolean = boolean.class;
+    static volatile Class<?> classByte    = byte.class;
+    static volatile Class<?> classShort   = short.class;
+    static volatile Class<?> classChar    = char.class;
+    static volatile Class<?> classInt     = int.class;
+    static volatile Class<?> classFloat   = float.class;
+    static volatile Class<?> classLong    = long.class;
+    static volatile Class<?> classDouble  = double.class;
+    static volatile Class<?> classObject  = Object.class;
+    static volatile Class<?> classArray   = Object[].class;
+    static volatile Class<?> classNull    = null;
+
+    static class AllConstants {
+        static Object testBoolean() { return UNSAFE.allocateUninitializedArray(boolean.class,  10); }
+        static Object testByte()    { return UNSAFE.allocateUninitializedArray(byte.class,     10); }
+        static Object testShort()   { return UNSAFE.allocateUninitializedArray(short.class,    10); }
+        static Object testChar()    { return UNSAFE.allocateUninitializedArray(char.class,     10); }
+        static Object testInt()     { return UNSAFE.allocateUninitializedArray(int.class,      10); }
+        static Object testFloat()   { return UNSAFE.allocateUninitializedArray(float.class,    10); }
+        static Object testLong()    { return UNSAFE.allocateUninitializedArray(long.class,     10); }
+        static Object testDouble()  { return UNSAFE.allocateUninitializedArray(double.class,   10); }
+        static Object testObject()  { return UNSAFE.allocateUninitializedArray(Object.class,   10); }
+        static Object testArray()   { return UNSAFE.allocateUninitializedArray(Object[].class, 10); }
+        static Object testNull()    { return UNSAFE.allocateUninitializedArray(null,           10); }
+        static Object testZero()    { return UNSAFE.allocateUninitializedArray(int.class,      0);  }
+        static Object testNeg()     { return UNSAFE.allocateUninitializedArray(int.class,      -1); }
+    }
+
+    static class ClassIsConstant {
+        static Object testBoolean() { return UNSAFE.allocateUninitializedArray(boolean.class,  sampleLen); }
+        static Object testByte()    { return UNSAFE.allocateUninitializedArray(byte.class,     sampleLen); }
+        static Object testShort()   { return UNSAFE.allocateUninitializedArray(short.class,    sampleLen); }
+        static Object testChar()    { return UNSAFE.allocateUninitializedArray(char.class,     sampleLen); }
+        static Object testInt()     { return UNSAFE.allocateUninitializedArray(int.class,      sampleLen); }
+        static Object testFloat()   { return UNSAFE.allocateUninitializedArray(float.class,    sampleLen); }
+        static Object testLong()    { return UNSAFE.allocateUninitializedArray(long.class,     sampleLen); }
+        static Object testDouble()  { return UNSAFE.allocateUninitializedArray(double.class,   sampleLen); }
+        static Object testObject()  { return UNSAFE.allocateUninitializedArray(Object.class,   sampleLen); }
+        static Object testArray()   { return UNSAFE.allocateUninitializedArray(Object[].class, sampleLen); }
+        static Object testNull()    { return UNSAFE.allocateUninitializedArray(null,           sampleLen); }
+        static Object testZero()    { return UNSAFE.allocateUninitializedArray(int.class,      sampleLenZero); }
+        static Object testNeg()     { return UNSAFE.allocateUninitializedArray(int.class,      sampleLenNeg); }
+    }
+
+    static class LengthIsConstant {
+        static Object testBoolean() { return UNSAFE.allocateUninitializedArray(classBoolean, 10); }
+        static Object testByte()    { return UNSAFE.allocateUninitializedArray(classByte,    10); }
+        static Object testShort()   { return UNSAFE.allocateUninitializedArray(classShort,   10); }
+        static Object testChar()    { return UNSAFE.allocateUninitializedArray(classChar,    10); }
+        static Object testInt()     { return UNSAFE.allocateUninitializedArray(classInt,     10); }
+        static Object testFloat()   { return UNSAFE.allocateUninitializedArray(classFloat,   10); }
+        static Object testLong()    { return UNSAFE.allocateUninitializedArray(classLong,    10); }
+        static Object testDouble()  { return UNSAFE.allocateUninitializedArray(classDouble,  10); }
+        static Object testObject()  { return UNSAFE.allocateUninitializedArray(classObject,  10); }
+        static Object testArray()   { return UNSAFE.allocateUninitializedArray(classArray,   10); }
+        static Object testNull()    { return UNSAFE.allocateUninitializedArray(classNull,    10); }
+        static Object testZero()    { return UNSAFE.allocateUninitializedArray(classInt,     0);  }
+        static Object testNeg()     { return UNSAFE.allocateUninitializedArray(classInt,     -1); }
+    }
+
+    static class NothingIsConstant {
+        static Object testBoolean() { return UNSAFE.allocateUninitializedArray(classBoolean, sampleLen); }
+        static Object testByte()    { return UNSAFE.allocateUninitializedArray(classByte,    sampleLen); }
+        static Object testShort()   { return UNSAFE.allocateUninitializedArray(classShort,   sampleLen); }
+        static Object testChar()    { return UNSAFE.allocateUninitializedArray(classChar,    sampleLen); }
+        static Object testInt()     { return UNSAFE.allocateUninitializedArray(classInt,     sampleLen); }
+        static Object testFloat()   { return UNSAFE.allocateUninitializedArray(classFloat,   sampleLen); }
+        static Object testLong()    { return UNSAFE.allocateUninitializedArray(classLong,    sampleLen); }
+        static Object testDouble()  { return UNSAFE.allocateUninitializedArray(classDouble,  sampleLen); }
+        static Object testObject()  { return UNSAFE.allocateUninitializedArray(classObject,  sampleLen); }
+        static Object testArray()   { return UNSAFE.allocateUninitializedArray(classArray,   sampleLen); }
+        static Object testNull()    { return UNSAFE.allocateUninitializedArray(classNull,    sampleLen); }
+        static Object testZero()    { return UNSAFE.allocateUninitializedArray(classInt,     sampleLenZero); }
+        static Object testNeg()     { return UNSAFE.allocateUninitializedArray(classInt,     sampleLenNeg); }
+    }
+}
+