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 /*
  25  * @test
  26  * @bug 8058575
  27  * @summary Test that bad host classes cause exceptions to get thrown.
  28  * @modules java.base/jdk.internal.misc
  29  *          java.base/jdk.internal.org.objectweb.asm
  30  * @run main TestBadHostClass
  31  */
  32 
  33 
  34 import java.lang.*;
  35 import java.lang.reflect.Field;
  36 import jdk.internal.misc.Unsafe;
  37 import jdk.internal.org.objectweb.asm.ClassWriter;
  38 import static jdk.internal.org.objectweb.asm.Opcodes.*;
  39 
  40 // Test that bad host classes cause exceptions.
  41 public class TestBadHostClass {
  42 
  43     private static final Unsafe unsafe = Unsafe.getUnsafe();
  44 
  45     private static String packageName(Class<?> c) {
  46         if (c.isArray()) {
  47             return packageName(c.getComponentType());
  48         } else {
  49             String name = c.getName();
  50             int dot = name.lastIndexOf('.');
  51             if (dot == -1) return "";
  52             return name.substring(0, dot);
  53         }
  54     }
  55 
  56     private static int constantPoolSize(byte[] classFile) {
  57         return ((classFile[8] & 0xFF) << 8) | (classFile[9] & 0xFF);
  58     }
  59 
  60     static public void badHostClass(Class<?> hostClass) {
  61         // choose a class name in the same package as the host class
  62         String className;
  63         if (hostClass != null) {
  64             String prefix = packageName(hostClass);
  65             if (prefix.length() > 0)
  66                 prefix = prefix.replace('.', '/') + "/";
  67             className = prefix + "Anon";
  68         } else {
  69            className = "Anon";
  70         }
  71 
  72         // create the class
  73         String superName = "java/lang/Object";
  74         ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
  75                                          + ClassWriter.COMPUTE_FRAMES);
  76         cw.visit(V1_8, ACC_PUBLIC + ACC_FINAL + ACC_SUPER,
  77                  className, null, superName, null);
  78         byte[] classBytes = cw.toByteArray();
  79         int cpPoolSize = constantPoolSize(classBytes);
  80         Class<?> anonClass
  81             = unsafe.defineAnonymousClass(hostClass, classBytes, new Object[cpPoolSize]);
  82     }
  83 
  84     public static void main(String args[]) throws Exception {
  85         // host class is an array of java.lang.Objects.
  86         try {
  87             badHostClass(Object[].class);
  88         } catch (IllegalArgumentException ex) {
  89         }
  90 
  91         // host class is an array of objects of this class.
  92         try {
  93             badHostClass(TestBadHostClass[].class);
  94         } catch (IllegalArgumentException ex) {
  95         }
  96 
  97         // host class is null.
  98         try {
  99             badHostClass(null);
 100         } catch (NullPointerException ex) {
 101         }
 102 
 103         // host class is a primitive array class.
 104         try {
 105             badHostClass(int[].class);
 106         } catch (IllegalArgumentException ex) {
 107         }
 108     }
 109 }