1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.awt.Component;
  25 import java.lang.reflect.Field;
  26 import static jdk.internal.org.objectweb.asm.Opcodes.*;
  27 import jdk.internal.org.objectweb.asm.*;
  28 import jdk.internal.misc.Unsafe;
  29 
  30 /*
  31  * @test PrimitiveHostClass
  32  * @bug 8140665
  33  * @summary Throws IllegalArgumentException if host class is a primitive class.
  34  * @modules java.base/jdk.internal.org.objectweb.asm
  35  *          java.base/jdk.internal.misc
  36  * @compile -XDignore.symbol.file PrimitiveHostClass.java
  37  * @run main/othervm PrimitiveHostClass
  38  */
  39 
  40 public class PrimitiveHostClass {
  41 
  42     static final Unsafe U;
  43     static {
  44         try {
  45             Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
  46             theUnsafe.setAccessible(true);
  47             U = (Unsafe) theUnsafe.get(null);
  48         } catch (Exception e) {
  49             throw new AssertionError(e);
  50         }
  51     }
  52 
  53     public static void testVMAnonymousClass(Class<?> hostClass) {
  54 
  55         // choose a class name in the same package as the host class
  56         String prefix = packageName(hostClass);
  57         if (prefix.length() > 0)
  58             prefix = prefix.replace('.', '/') + "/";
  59         String className = prefix + "Anon";
  60 
  61         // create the class
  62         String superName = "java/lang/Object";
  63         ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
  64                                          + ClassWriter.COMPUTE_FRAMES);
  65         cw.visit(V1_8, ACC_PUBLIC + ACC_FINAL + ACC_SUPER,
  66                  className, null, superName, null);
  67         byte[] classBytes = cw.toByteArray();
  68         int cpPoolSize = constantPoolSize(classBytes);
  69         Class<?> anonClass =
  70             U.defineAnonymousClass(hostClass, classBytes, new Object[cpPoolSize]);
  71     }
  72 
  73     private static String packageName(Class<?> c) {
  74         if (c.isArray()) {
  75             return packageName(c.getComponentType());
  76         } else {
  77             String name = c.getName();
  78             int dot = name.lastIndexOf('.');
  79             if (dot == -1) return "";
  80             return name.substring(0, dot);
  81         }
  82     }
  83 
  84     private static int constantPoolSize(byte[] classFile) {
  85         return ((classFile[8] & 0xFF) << 8) | (classFile[9] & 0xFF);
  86     }
  87 
  88     public static void main(String args[]) {
  89         testVMAnonymousClass(PrimitiveHostClass.class);
  90         try {
  91             testVMAnonymousClass(int.class);
  92             throw new RuntimeException(
  93                 "Expected IllegalArgumentException not thrown");
  94         } catch (IllegalArgumentException e) {
  95             // Expected
  96         }
  97     }
  98 }