test/compiler/jsr292/VMAnonymousClasses.java

Wed, 01 Oct 2014 12:34:38 -0700

author
vlivanov
date
Wed, 01 Oct 2014 12:34:38 -0700
changeset 7288
9dc314de223d
child 8451
649f01d13b2d
permissions
-rw-r--r--

8058828: Wrong ciConstant type for arrays from ConstantPool::_resolved_reference
Reviewed-by: kvn, jrose

vlivanov@7288 1 /*
vlivanov@7288 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
vlivanov@7288 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
vlivanov@7288 4 *
vlivanov@7288 5 * This code is free software; you can redistribute it and/or modify it
vlivanov@7288 6 * under the terms of the GNU General Public License version 2 only, as
vlivanov@7288 7 * published by the Free Software Foundation.
vlivanov@7288 8 *
vlivanov@7288 9 * This code is distributed in the hope that it will be useful, but WITHOUT
vlivanov@7288 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
vlivanov@7288 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
vlivanov@7288 12 * version 2 for more details (a copy is included in the LICENSE file that
vlivanov@7288 13 * accompanied this code).
vlivanov@7288 14 *
vlivanov@7288 15 * You should have received a copy of the GNU General Public License version
vlivanov@7288 16 * 2 along with this work; if not, write to the Free Software Foundation,
vlivanov@7288 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
vlivanov@7288 18 *
vlivanov@7288 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
vlivanov@7288 20 * or visit www.oracle.com if you need additional information or have any
vlivanov@7288 21 * questions.
vlivanov@7288 22 */
vlivanov@7288 23
vlivanov@7288 24 /**
vlivanov@7288 25 * @test
vlivanov@7288 26 * @bug 8058828
vlivanov@7288 27 * @run main/bootclasspath -Xbatch VMAnonymousClasses
vlivanov@7288 28 */
vlivanov@7288 29
vlivanov@7288 30 import jdk.internal.org.objectweb.asm.ClassWriter;
vlivanov@7288 31 import jdk.internal.org.objectweb.asm.MethodVisitor;
vlivanov@7288 32 import jdk.internal.org.objectweb.asm.Opcodes;
vlivanov@7288 33 import sun.misc.Unsafe;
vlivanov@7288 34
vlivanov@7288 35 import java.lang.invoke.ConstantCallSite;
vlivanov@7288 36 import java.lang.invoke.MethodHandle;
vlivanov@7288 37 import java.lang.invoke.MethodHandles;
vlivanov@7288 38 import java.lang.invoke.MethodType;
vlivanov@7288 39 import java.lang.invoke.MutableCallSite;
vlivanov@7288 40 import java.lang.invoke.VolatileCallSite;
vlivanov@7288 41
vlivanov@7288 42 public class VMAnonymousClasses {
vlivanov@7288 43 static final String TEST_METHOD_NAME = "constant";
vlivanov@7288 44
vlivanov@7288 45 static final Unsafe UNSAFE = Unsafe.getUnsafe();
vlivanov@7288 46
vlivanov@7288 47 static int getConstantPoolSize(byte[] classFile) {
vlivanov@7288 48 // The first few bytes:
vlivanov@7288 49 // u4 magic;
vlivanov@7288 50 // u2 minor_version;
vlivanov@7288 51 // u2 major_version;
vlivanov@7288 52 // u2 constant_pool_count;
vlivanov@7288 53 return ((classFile[8] & 0xFF) << 8) | (classFile[9] & 0xFF);
vlivanov@7288 54 }
vlivanov@7288 55
vlivanov@7288 56 static void test(Object value) throws ReflectiveOperationException {
vlivanov@7288 57 System.out.printf("Test: %s", value != null ? value.getClass() : "null");
vlivanov@7288 58
vlivanov@7288 59 ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
vlivanov@7288 60 cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER, "Test", null, "java/lang/Object", null);
vlivanov@7288 61
vlivanov@7288 62 MethodVisitor mv = cw.visitMethod(Opcodes.ACC_STATIC | Opcodes.ACC_PUBLIC, TEST_METHOD_NAME, "()Ljava/lang/Object;", null, null);
vlivanov@7288 63
vlivanov@7288 64 String placeholder = "CONSTANT";
vlivanov@7288 65 int index = cw.newConst(placeholder);
vlivanov@7288 66 mv.visitLdcInsn(placeholder);
vlivanov@7288 67 mv.visitInsn(Opcodes.ARETURN);
vlivanov@7288 68
vlivanov@7288 69 mv.visitMaxs(0, 0);
vlivanov@7288 70 mv.visitEnd();
vlivanov@7288 71
vlivanov@7288 72 byte[] classFile = cw.toByteArray();
vlivanov@7288 73
vlivanov@7288 74 Object[] cpPatches = new Object[getConstantPoolSize(classFile)];
vlivanov@7288 75 cpPatches[index] = value;
vlivanov@7288 76
vlivanov@7288 77 Class<?> test = UNSAFE.defineAnonymousClass(VMAnonymousClasses.class, classFile, cpPatches);
vlivanov@7288 78
vlivanov@7288 79 Object expectedResult = (value != null) ? value : placeholder;
vlivanov@7288 80 for (int i = 0; i<15000; i++) {
vlivanov@7288 81 Object result = test.getMethod(TEST_METHOD_NAME).invoke(null);
vlivanov@7288 82 if (result != expectedResult) {
vlivanov@7288 83 throw new AssertionError(String.format("Wrong value returned: %s != %s", value, result));
vlivanov@7288 84 }
vlivanov@7288 85 }
vlivanov@7288 86 System.out.println(" PASSED");
vlivanov@7288 87 }
vlivanov@7288 88
vlivanov@7288 89 public static void main(String[] args) throws ReflectiveOperationException {
vlivanov@7288 90 // Objects
vlivanov@7288 91 test(new Object());
vlivanov@7288 92 test("TEST");
vlivanov@7288 93 test(new VMAnonymousClasses());
vlivanov@7288 94 test(null);
vlivanov@7288 95
vlivanov@7288 96 // Class
vlivanov@7288 97 test(String.class);
vlivanov@7288 98
vlivanov@7288 99 // Arrays
vlivanov@7288 100 test(new boolean[0]);
vlivanov@7288 101 test(new byte[0]);
vlivanov@7288 102 test(new char[0]);
vlivanov@7288 103 test(new short[0]);
vlivanov@7288 104 test(new int[0]);
vlivanov@7288 105 test(new long[0]);
vlivanov@7288 106 test(new float[0]);
vlivanov@7288 107 test(new double[0]);
vlivanov@7288 108 test(new Object[0]);
vlivanov@7288 109
vlivanov@7288 110 // Multi-dimensional arrays
vlivanov@7288 111 test(new byte[0][0]);
vlivanov@7288 112 test(new Object[0][0]);
vlivanov@7288 113
vlivanov@7288 114 // MethodHandle-related
vlivanov@7288 115 MethodType mt = MethodType.methodType(void.class, String[].class);
vlivanov@7288 116 MethodHandle mh = MethodHandles.lookup().findStatic(VMAnonymousClasses.class, "main", mt);
vlivanov@7288 117 test(mt);
vlivanov@7288 118 test(mh);
vlivanov@7288 119 test(new ConstantCallSite(mh));
vlivanov@7288 120 test(new MutableCallSite(MethodType.methodType(void.class)));
vlivanov@7288 121 test(new VolatileCallSite(MethodType.methodType(void.class)));
vlivanov@7288 122
vlivanov@7288 123 System.out.println("TEST PASSED");
vlivanov@7288 124 }
vlivanov@7288 125 }

mercurial