Path: blob/master/test/hotspot/jtreg/compiler/intrinsics/unsafe/AllocateUninitializedArray.java
41153 views
/*1* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8150465 825933926* @summary Unsafe methods to produce uninitialized arrays27* @modules java.base/jdk.internal.misc:+open28*29* @run main/othervm -ea -Diters=200 -Xint30* compiler.intrinsics.unsafe.AllocateUninitializedArray31* @run main/othervm -ea -Diters=30000 -XX:TieredStopAtLevel=132* compiler.intrinsics.unsafe.AllocateUninitializedArray33* @run main/othervm -ea -Diters=30000 -XX:TieredStopAtLevel=434* compiler.intrinsics.unsafe.AllocateUninitializedArray35*/3637package compiler.intrinsics.unsafe;3839import java.lang.reflect.Array;40import java.lang.reflect.Field;41import java.util.concurrent.Callable;4243public class AllocateUninitializedArray {44static final int ITERS = Integer.getInteger("iters", 1);45static final jdk.internal.misc.Unsafe UNSAFE;4647static {48try {49Field f = jdk.internal.misc.Unsafe.class.getDeclaredField("theUnsafe");50f.setAccessible(true);51UNSAFE = (jdk.internal.misc.Unsafe) f.get(null);52} catch (Exception e) {53throw new RuntimeException("Unable to get Unsafe instance.", e);54}55}5657public static void main(String... args) throws Exception {58testIAE(AllConstants::testObject);59testIAE(LengthIsConstant::testObject);60testIAE(ClassIsConstant::testObject);61testIAE(NothingIsConstant::testObject);6263testIAE(AllConstants::testArray);64testIAE(LengthIsConstant::testArray);65testIAE(ClassIsConstant::testArray);66testIAE(NothingIsConstant::testArray);6768testIAE(AllConstants::testNull);69testIAE(LengthIsConstant::testNull);70testIAE(ClassIsConstant::testNull);71testIAE(NothingIsConstant::testNull);7273testOK(boolean[].class, 10, AllConstants::testBoolean);74testOK(byte[].class, 10, AllConstants::testByte);75testOK(short[].class, 10, AllConstants::testShort);76testOK(char[].class, 10, AllConstants::testChar);77testOK(int[].class, 10, AllConstants::testInt);78testOK(float[].class, 10, AllConstants::testFloat);79testOK(long[].class, 10, AllConstants::testLong);80testOK(double[].class, 10, AllConstants::testDouble);81testOK(null, 10, AllConstants::testVoid);8283testOK(boolean[].class, 10, LengthIsConstant::testBoolean);84testOK(byte[].class, 10, LengthIsConstant::testByte);85testOK(short[].class, 10, LengthIsConstant::testShort);86testOK(char[].class, 10, LengthIsConstant::testChar);87testOK(int[].class, 10, LengthIsConstant::testInt);88testOK(float[].class, 10, LengthIsConstant::testFloat);89testOK(long[].class, 10, LengthIsConstant::testLong);90testOK(double[].class, 10, LengthIsConstant::testDouble);91testOK(null, 10, LengthIsConstant::testVoid);9293testOK(boolean[].class, 10, ClassIsConstant::testBoolean);94testOK(byte[].class, 10, ClassIsConstant::testByte);95testOK(short[].class, 10, ClassIsConstant::testShort);96testOK(char[].class, 10, ClassIsConstant::testChar);97testOK(int[].class, 10, ClassIsConstant::testInt);98testOK(float[].class, 10, ClassIsConstant::testFloat);99testOK(long[].class, 10, ClassIsConstant::testLong);100testOK(double[].class, 10, ClassIsConstant::testDouble);101testOK(null, 10, ClassIsConstant::testVoid);102103testOK(boolean[].class, 10, NothingIsConstant::testBoolean);104testOK(byte[].class, 10, NothingIsConstant::testByte);105testOK(short[].class, 10, NothingIsConstant::testShort);106testOK(char[].class, 10, NothingIsConstant::testChar);107testOK(int[].class, 10, NothingIsConstant::testInt);108testOK(float[].class, 10, NothingIsConstant::testFloat);109testOK(long[].class, 10, NothingIsConstant::testLong);110testOK(double[].class, 10, NothingIsConstant::testDouble);111testOK(null, 10, NothingIsConstant::testVoid);112}113114public static void testOK(Class<?> expectClass, int expectLen, Callable<Object> test) throws Exception {115for (int c = 0; c < ITERS; c++) {116Object res = test.call();117if (res == null) {118if (expectClass != null) {119throw new IllegalStateException("Unexpected null result");120}121continue;122}123Class<?> actualClass = res.getClass();124if (!actualClass.equals(expectClass)) {125throw new IllegalStateException("Wrong class: expected = " + expectClass + ", but got " + actualClass);126}127int actualLen = Array.getLength(res);128if (actualLen != expectLen) {129throw new IllegalStateException("Wrong length: expected = " + expectLen + ", but got " + actualLen);130}131}132}133134static volatile Object sink;135136public static void testIAE(Callable<Object> test) throws Exception {137for (int c = 0; c < ITERS; c++) {138try {139sink = test.call();140throw new IllegalStateException("Expected IAE");141} catch (IllegalArgumentException iae) {142// expected143}144}145}146147static volatile int sampleLenNeg = -1;148static volatile int sampleLenZero = 0;149static volatile int sampleLen = 10;150151152static volatile Class<?> classBoolean = boolean.class;153static volatile Class<?> classByte = byte.class;154static volatile Class<?> classShort = short.class;155static volatile Class<?> classChar = char.class;156static volatile Class<?> classInt = int.class;157static volatile Class<?> classFloat = float.class;158static volatile Class<?> classLong = long.class;159static volatile Class<?> classDouble = double.class;160static volatile Class<?> classVoid = void.class;161static volatile Class<?> classObject = Object.class;162static volatile Class<?> classArray = Object[].class;163static volatile Class<?> classNull = null;164165static class AllConstants {166static Object testBoolean() { return UNSAFE.allocateUninitializedArray(boolean.class, 10); }167static Object testByte() { return UNSAFE.allocateUninitializedArray(byte.class, 10); }168static Object testShort() { return UNSAFE.allocateUninitializedArray(short.class, 10); }169static Object testChar() { return UNSAFE.allocateUninitializedArray(char.class, 10); }170static Object testInt() { return UNSAFE.allocateUninitializedArray(int.class, 10); }171static Object testFloat() { return UNSAFE.allocateUninitializedArray(float.class, 10); }172static Object testLong() { return UNSAFE.allocateUninitializedArray(long.class, 10); }173static Object testDouble() { return UNSAFE.allocateUninitializedArray(double.class, 10); }174static Object testVoid() { return UNSAFE.allocateUninitializedArray(void.class, 10); }175static Object testObject() { return UNSAFE.allocateUninitializedArray(Object.class, 10); }176static Object testArray() { return UNSAFE.allocateUninitializedArray(Object[].class, 10); }177static Object testNull() { return UNSAFE.allocateUninitializedArray(null, 10); }178static Object testZero() { return UNSAFE.allocateUninitializedArray(int.class, 0); }179static Object testNeg() { return UNSAFE.allocateUninitializedArray(int.class, -1); }180}181182static class ClassIsConstant {183static Object testBoolean() { return UNSAFE.allocateUninitializedArray(boolean.class, sampleLen); }184static Object testByte() { return UNSAFE.allocateUninitializedArray(byte.class, sampleLen); }185static Object testShort() { return UNSAFE.allocateUninitializedArray(short.class, sampleLen); }186static Object testChar() { return UNSAFE.allocateUninitializedArray(char.class, sampleLen); }187static Object testInt() { return UNSAFE.allocateUninitializedArray(int.class, sampleLen); }188static Object testFloat() { return UNSAFE.allocateUninitializedArray(float.class, sampleLen); }189static Object testLong() { return UNSAFE.allocateUninitializedArray(long.class, sampleLen); }190static Object testDouble() { return UNSAFE.allocateUninitializedArray(double.class, sampleLen); }191static Object testVoid() { return UNSAFE.allocateUninitializedArray(void.class, sampleLen); }192static Object testObject() { return UNSAFE.allocateUninitializedArray(Object.class, sampleLen); }193static Object testArray() { return UNSAFE.allocateUninitializedArray(Object[].class, sampleLen); }194static Object testNull() { return UNSAFE.allocateUninitializedArray(null, sampleLen); }195static Object testZero() { return UNSAFE.allocateUninitializedArray(int.class, sampleLenZero); }196static Object testNeg() { return UNSAFE.allocateUninitializedArray(int.class, sampleLenNeg); }197}198199static class LengthIsConstant {200static Object testBoolean() { return UNSAFE.allocateUninitializedArray(classBoolean, 10); }201static Object testByte() { return UNSAFE.allocateUninitializedArray(classByte, 10); }202static Object testShort() { return UNSAFE.allocateUninitializedArray(classShort, 10); }203static Object testChar() { return UNSAFE.allocateUninitializedArray(classChar, 10); }204static Object testInt() { return UNSAFE.allocateUninitializedArray(classInt, 10); }205static Object testFloat() { return UNSAFE.allocateUninitializedArray(classFloat, 10); }206static Object testLong() { return UNSAFE.allocateUninitializedArray(classLong, 10); }207static Object testDouble() { return UNSAFE.allocateUninitializedArray(classDouble, 10); }208static Object testVoid() { return UNSAFE.allocateUninitializedArray(classVoid, 10); }209static Object testObject() { return UNSAFE.allocateUninitializedArray(classObject, 10); }210static Object testArray() { return UNSAFE.allocateUninitializedArray(classArray, 10); }211static Object testNull() { return UNSAFE.allocateUninitializedArray(classNull, 10); }212static Object testZero() { return UNSAFE.allocateUninitializedArray(classInt, 0); }213static Object testNeg() { return UNSAFE.allocateUninitializedArray(classInt, -1); }214}215216static class NothingIsConstant {217static Object testBoolean() { return UNSAFE.allocateUninitializedArray(classBoolean, sampleLen); }218static Object testByte() { return UNSAFE.allocateUninitializedArray(classByte, sampleLen); }219static Object testShort() { return UNSAFE.allocateUninitializedArray(classShort, sampleLen); }220static Object testChar() { return UNSAFE.allocateUninitializedArray(classChar, sampleLen); }221static Object testInt() { return UNSAFE.allocateUninitializedArray(classInt, sampleLen); }222static Object testFloat() { return UNSAFE.allocateUninitializedArray(classFloat, sampleLen); }223static Object testLong() { return UNSAFE.allocateUninitializedArray(classLong, sampleLen); }224static Object testDouble() { return UNSAFE.allocateUninitializedArray(classDouble, sampleLen); }225static Object testVoid() { return UNSAFE.allocateUninitializedArray(classVoid, sampleLen); }226static Object testObject() { return UNSAFE.allocateUninitializedArray(classObject, sampleLen); }227static Object testArray() { return UNSAFE.allocateUninitializedArray(classArray, sampleLen); }228static Object testNull() { return UNSAFE.allocateUninitializedArray(classNull, sampleLen); }229static Object testZero() { return UNSAFE.allocateUninitializedArray(classInt, sampleLenZero); }230static Object testNeg() { return UNSAFE.allocateUninitializedArray(classInt, sampleLenNeg); }231}232}233234235236