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