hotspot/test/runtime/Unsafe/PrimitiveHostClass.java
author hseigel
Thu, 24 Mar 2016 08:13:28 -0400
changeset 37184 23e0d47c1f3e
child 37298 8df0cc9da41e
permissions -rw-r--r--
8140665: SIGSEGV when a primitive type's class is used as the host class in a call to DefineAnonymousClass call Summary: Instead of assertng, throw an exception Reviewed-by: acorn, coleenp
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.*;
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
    28
import sun.misc.Unsafe;
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
 * @library /testlibrary
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
    35
 * @modules java.base/jdk.internal.org.objectweb.asm
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
 *          java.base/sun.misc
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
 * @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
    38
 * @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
    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
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
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
    42
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
    static final Unsafe U;
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
    static {
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
        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
    46
            Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
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
            theUnsafe.setAccessible(true);
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
            U = (Unsafe) theUnsafe.get(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
    49
        } catch (Exception 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
    50
            throw new AssertionError(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
    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
    }
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
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
    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
    55
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
        // 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
    57
        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
    58
        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
    59
            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
    60
        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
    61
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
        // 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
    63
        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
    64
        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
    65
                                         + 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
    66
        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
    67
                 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
    68
        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
    69
        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
    70
        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
    71
            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
    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
    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
    75
        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
    76
            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
    77
        } 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
    78
            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
    79
            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
    80
            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
    81
            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
    82
        }
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
    }
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
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
    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
    86
        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
    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
    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
    90
        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
    91
        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
    92
            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
    93
            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
    94
                "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
    95
        } 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
    96
            // 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
    97
        }
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
    98
    }
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
    99
}