1 /*
   2  * Copyright (c) 2017, 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 jdk.internal.org.objectweb.asm.*;
  25 
  26 /*
  27  * @test
  28  * @summary Test that the JVM throws NCDFE for class file version 53 when a
  29  *          CONSTANT_Package type is in the constant pool and ACC_MODULE is
  30  *          set in the access_flags.
  31  * @bug 8175383
  32  * @library /test/lib
  33  * @modules java.base/jdk.internal.org.objectweb.asm
  34  * @compile -XDignore.symbol.file ConstPackageACC.java
  35  * @run main ConstPackageACC
  36  */
  37 
  38 public class ConstPackageACC {
  39 
  40     static final String CLASS_NAME = "FooPkgACC";
  41 
  42     public static void main(String[] args) throws Exception {
  43         ClassWriter cw = new ClassWriter(0);
  44         int ACC_MODULE = 0x8000;
  45         cw.visit(Opcodes.V1_9,
  46                 Opcodes.ACC_INTERFACE + Opcodes.ACC_ABSTRACT + Opcodes.ACC_SYNTHETIC + ACC_MODULE,
  47                 CLASS_NAME,
  48                 null,
  49                 "java/lang/Object",
  50                 null);
  51 
  52         cw.visitAttribute(new TestAttribute("jdk.fooPkgACC"));
  53 
  54         cw.visitEnd();
  55         byte[] bytes = cw.toByteArray();
  56 
  57 
  58         ClassLoader loader = new ClassLoader(ConstPackageACC.class.getClassLoader()) {
  59             @Override
  60             protected Class<?> findClass(String cn)throws ClassNotFoundException {
  61                 if (cn.equals(CLASS_NAME)) {
  62                     try {
  63                         Class superClass = super.defineClass(cn, bytes, 0, bytes.length);
  64                         throw new RuntimeException("Expected NoClassDefFoundError not thrown");
  65                     } catch (java.lang.NoClassDefFoundError e) {
  66                        if (!e.getMessage().contains(
  67                            "is not a class because access_flag ACC_MODULE is set")) {
  68                            throw new RuntimeException("Wrong NoClassDefFoundError exception: " + e.getMessage());
  69                        }
  70                     }
  71                 } else {
  72                     throw new ClassNotFoundException(cn);
  73                 }
  74                 return null;
  75             }
  76         };
  77 
  78         Class<?> clazz = loader.loadClass(CLASS_NAME);
  79     }
  80 
  81     /**
  82      * ConstPackageACCAttr attribute.
  83      *
  84      * <pre> {@code
  85      *
  86      * MainClass_attribute {
  87      *   // index to CONSTANT_utf8_info structure in constant pool representing
  88      *   // the string "ConstPackageACCAttr"
  89      *   u2 attribute_name_index;
  90      *   u4 attribute_length;
  91      *
  92      *   // index to CONSTANT_Package_info structure
  93      *   u2 package_name_index
  94      * }
  95      *
  96      * } </pre>
  97      */
  98     public static class TestAttribute extends Attribute {
  99         private final String packageName;
 100 
 101         public TestAttribute(String packageName) {
 102             super("ConstPackageACCAttr");
 103             this.packageName = packageName;
 104         }
 105 
 106         public TestAttribute() {
 107             this(null);
 108         }
 109 
 110         @Override
 111         protected Attribute read(ClassReader cr,
 112                                  int off,
 113                                  int len,
 114                                  char[] buf,
 115                                  int codeOff,
 116                                  Label[] labels)
 117         {
 118             String mn = cr.readPackage(off, buf);
 119             off += 2;
 120             return new TestAttribute(mn);
 121         }
 122 
 123         @Override
 124         protected ByteVector write(ClassWriter cw,
 125                                    byte[] code,
 126                                    int len,
 127                                    int maxStack,
 128                                    int maxLocals)
 129         {
 130             ByteVector attr = new ByteVector();
 131             attr.putShort(cw.newPackage(packageName));
 132             return attr;
 133         }
 134     }
 135 }