Path: blob/master/test/hotspot/jtreg/runtime/AccModule/ConstModule.java
40942 views
/*1* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import jdk.internal.org.objectweb.asm.*;2425/*26* @test27* @summary Test scenarios for constant pool CONSTANT_Module and CONSTANT_Package28* types, for class file versions 53 and 52, when ACC_MODULE is set and29* not set in the access_flags.30* @bug 817538331* @library /test/lib32* @modules java.base/jdk.internal.org.objectweb.asm33* @compile -XDignore.symbol.file ConstModule.java34* @run main ConstModule35*/3637public class ConstModule {3839static final int ACC_MODULE = 0x8000;40static final boolean MODULE_TEST = true;41static final boolean PACKAGE_TEST = false;42static final boolean CFE_EXCEPTION = true;43static final boolean NCDFE_EXCEPTION = false;4445public static void main(String[] args) throws Exception {4647// Test that the JVM throws CFE for constant pool CONSTANT_Module type, for48// class file version 53, when ACC_MODULE is not set in the access_flags.49ConstModule.write_and_load(Opcodes.V9,50Opcodes.ACC_INTERFACE + Opcodes.ACC_ABSTRACT + Opcodes.ACC_SYNTHETIC,51"jdk.fooMod", "FooMod", MODULE_TEST, CFE_EXCEPTION);5253// Test that the JVM throws NCDFE for constant pool CONSTANT_Module type,54// for class file version 53, when ACC_MODULE is set in the access_flags.55ConstModule.write_and_load(Opcodes.V9,56Opcodes.ACC_INTERFACE + Opcodes.ACC_ABSTRACT + Opcodes.ACC_SYNTHETIC + ACC_MODULE,57"jdk.fooModACC", "FooModACC", MODULE_TEST, NCDFE_EXCEPTION);5859// Test that the JVM throws CFE for constant pool CONSTANT_Module type, for60// class file version 52, even when ACC_MODULE is set in the access_flags.61ConstModule.write_and_load(Opcodes.V1_8,62Opcodes.ACC_INTERFACE + Opcodes.ACC_ABSTRACT + Opcodes.ACC_SYNTHETIC + ACC_MODULE,63"jdk.fooModACC52", "FooModACC52", MODULE_TEST, CFE_EXCEPTION);6465// Test that the JVM throws CFE for constant pool CONSTANT_Package type, for66// class file version 53, when ACC_MODULE is not set in the access_flags.67ConstModule.write_and_load(Opcodes.V9,68Opcodes.ACC_INTERFACE + Opcodes.ACC_ABSTRACT + Opcodes.ACC_SYNTHETIC,69"jdk.fooPkg", "FooPkg", PACKAGE_TEST, CFE_EXCEPTION);7071// Test that the JVM throws NCDFE for constant pool CONSTANT_Package type,72// for class file version 53, when ACC_MODULE is set in the access_flags.73ConstModule.write_and_load(Opcodes.V9,74Opcodes.ACC_INTERFACE + Opcodes.ACC_ABSTRACT + Opcodes.ACC_SYNTHETIC + ACC_MODULE,75"jdk.fooModACC", "FooModACC", PACKAGE_TEST, NCDFE_EXCEPTION);7677// Test that the JVM throws CFE for constant pool CONSTANT_Package type, for78// class file version 52, even when ACC_MODULE is set in the access_flags.79ConstModule.write_and_load(Opcodes.V1_8,80Opcodes.ACC_INTERFACE + Opcodes.ACC_ABSTRACT + Opcodes.ACC_SYNTHETIC + ACC_MODULE,81"jdk.fooModACC52", "FooModACC52", PACKAGE_TEST, CFE_EXCEPTION);8283}8485public static void write_and_load(int version,86int access_flags,87String attr,88String class_name,89boolean module_test,90boolean throwCFE) throws Exception {91ClassWriter cw = new ClassWriter(0);92cw.visit(version,93access_flags,94class_name,95null,96"java/lang/Object",97null);9899if (module_test)100cw.visitAttribute(new TestModuleAttribute(attr));101else102cw.visitAttribute(new TestPackageAttribute(attr));103104cw.visitEnd();105byte[] bytes = cw.toByteArray();106107108ClassLoader loader = new ClassLoader(ConstModule.class.getClassLoader()) {109@Override110protected Class<?> findClass(String cn)throws ClassNotFoundException {111if (cn.equals(class_name)) {112try {113Class superClass = super.defineClass(cn, bytes, 0, bytes.length);114throw new RuntimeException("Expected ClassFormatError not thrown");115} catch (java.lang.ClassFormatError e) {116if (!throwCFE) {117throw new RuntimeException("Unexpected ClassFormatError exception: " + e.getMessage());118}119if (module_test && !e.getMessage().contains(120"Unknown constant tag 19 in class file")) {121throw new RuntimeException("Wrong ClassFormatError exception: " + e.getMessage());122} else if (!module_test && !e.getMessage().contains(123"Unknown constant tag 20 in class file")) {124throw new RuntimeException("Wrong ClassFormatError exception: " + e.getMessage());125}126} catch (java.lang.NoClassDefFoundError f) {127if (throwCFE) {128throw new RuntimeException("Unexpected NoClassDefFoundError exception: " + f.getMessage());129}130if (!f.getMessage().contains(131"is not a class because access_flag ACC_MODULE is set")) {132throw new RuntimeException("Wrong NoClassDefFoundError exception: " + f.getMessage());133}134}135} else {136throw new ClassNotFoundException(cn);137}138return null;139}140};141142Class<?> clazz = loader.loadClass(class_name);143}144145/**146* ConstModuleAttr attribute.147*148* <pre> {@code149*150* MainClass_attribute {151* // index to CONSTANT_utf8_info structure in constant pool representing152* // the string "ConstModuleAttr"153* u2 attribute_name_index;154* u4 attribute_length;155*156* // index to CONSTANT_Module_info structure157* u2 module_name_index158* }159*160* } </pre>161*/162public static class TestModuleAttribute extends Attribute {163private final String moduleName;164165public TestModuleAttribute(String moduleName) {166super("ConstModuleAttr");167this.moduleName = moduleName;168}169170public TestModuleAttribute() {171this(null);172}173174@Override175protected Attribute read(ClassReader cr,176int off,177int len,178char[] buf,179int codeOff,180Label[] labels)181{182String mn = cr.readModule(off, buf);183off += 2;184return new TestModuleAttribute(mn);185}186187@Override188protected ByteVector write(ClassWriter cw,189byte[] code,190int len,191int maxStack,192int maxLocals)193{194ByteVector attr = new ByteVector();195attr.putShort(cw.newModule(moduleName));196return attr;197}198}199200/**201* ConstPackageAttr attribute.202*203* <pre> {@code204*205* MainClass_attribute {206* // index to CONSTANT_utf8_info structure in constant pool representing207* // the string "ConstPackageAttr"208* u2 attribute_name_index;209* u4 attribute_length;210*211* // index to CONSTANT_Package_info structure212* u2 module_name_index213* }214*215* } </pre>216*/217public static class TestPackageAttribute extends Attribute {218private final String packageName;219220public TestPackageAttribute(String packageName) {221super("ConstPackageAttr");222this.packageName = packageName;223}224225public TestPackageAttribute() {226this(null);227}228229@Override230protected Attribute read(ClassReader cr,231int off,232int len,233char[] buf,234int codeOff,235Label[] labels)236{237String mn = cr.readPackage(off, buf);238off += 2;239return new TestPackageAttribute(mn);240}241242@Override243protected ByteVector write(ClassWriter cw,244byte[] code,245int len,246int maxStack,247int maxLocals)248{249ByteVector attr = new ByteVector();250attr.putShort(cw.newPackage(packageName));251return attr;252}253}254}255256257