hotspot/test/runtime/Unsafe/PrimitiveHostClass.java
author gtriantafill
Wed, 14 Sep 2016 08:17:50 -0400
changeset 41092 c388d897472d
parent 40631 ed82623d7831
permissions -rw-r--r--
8165889: Remove jdk.test.lib.unsafe.UnsafeHelper Summary: Remove use of setAccessible() to get Unsafe. Reviewed-by: shade, lfoltan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37184
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
     1
/*
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
     4
 *
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
     7
 * published by the Free Software Foundation.
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
     8
 *
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    13
 * accompanied this code).
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    14
 *
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    18
 *
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    21
 * questions.
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    22
 */
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    23
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    24
import java.awt.Component;
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    25
import java.lang.reflect.Field;
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    26
import static jdk.internal.org.objectweb.asm.Opcodes.*;
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    27
import jdk.internal.org.objectweb.asm.*;
38152
80e5da81fb2c 8154258: [TESTBUG] Various serviceability tests fail compilation
dsamersoff
parents: 37298
diff changeset
    28
import jdk.internal.misc.Unsafe;
37184
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    29
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    30
/*
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    31
 * @test PrimitiveHostClass
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    32
 * @bug 8140665
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    33
 * @summary Throws IllegalArgumentException if host class is a primitive class.
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    34
 * @modules java.base/jdk.internal.org.objectweb.asm
37298
8df0cc9da41e 8154028: Several hotspot tests need to be updated after 8153737 (Unsupported Module)
amurillo
parents: 37184
diff changeset
    35
 *          java.base/jdk.internal.misc
37184
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    36
 * @compile -XDignore.symbol.file PrimitiveHostClass.java
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    37
 * @run main/othervm PrimitiveHostClass
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    38
 */
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    39
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    40
public class PrimitiveHostClass {
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    41
41092
c388d897472d 8165889: Remove jdk.test.lib.unsafe.UnsafeHelper
gtriantafill
parents: 40631
diff changeset
    42
    static final Unsafe U = Unsafe.getUnsafe();
37184
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    43
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    44
    public static void testVMAnonymousClass(Class<?> hostClass) {
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    45
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    46
        // choose a class name in the same package as the host class
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    47
        String prefix = packageName(hostClass);
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    48
        if (prefix.length() > 0)
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    49
            prefix = prefix.replace('.', '/') + "/";
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    50
        String className = prefix + "Anon";
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    51
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    52
        // create the class
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    53
        String superName = "java/lang/Object";
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    54
        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    55
                                         + ClassWriter.COMPUTE_FRAMES);
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    56
        cw.visit(V1_8, ACC_PUBLIC + ACC_FINAL + ACC_SUPER,
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    57
                 className, null, superName, null);
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    58
        byte[] classBytes = cw.toByteArray();
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    59
        int cpPoolSize = constantPoolSize(classBytes);
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    60
        Class<?> anonClass =
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    61
            U.defineAnonymousClass(hostClass, classBytes, new Object[cpPoolSize]);
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    62
    }
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    63
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    64
    private static String packageName(Class<?> c) {
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    65
        if (c.isArray()) {
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    66
            return packageName(c.getComponentType());
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    67
        } else {
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    68
            String name = c.getName();
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    69
            int dot = name.lastIndexOf('.');
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    70
            if (dot == -1) return "";
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    71
            return name.substring(0, dot);
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    72
        }
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    73
    }
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    74
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    75
    private static int constantPoolSize(byte[] classFile) {
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    76
        return ((classFile[8] & 0xFF) << 8) | (classFile[9] & 0xFF);
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    77
    }
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    78
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    79
    public static void main(String args[]) {
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    80
        testVMAnonymousClass(PrimitiveHostClass.class);
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    81
        try {
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    82
            testVMAnonymousClass(int.class);
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    83
            throw new RuntimeException(
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    84
                "Expected IllegalArgumentException not thrown");
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    85
        } catch (IllegalArgumentException e) {
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    86
            // Expected
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    87
        }
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    88
    }
23e0d47c1f3e 8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call
hseigel
parents:
diff changeset
    89
}