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