Path: blob/master/test/hotspot/jtreg/runtime/DefineClass/NullClassBytesTest.java
40943 views
/*1* Copyright (c) 2021, 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*/2223/*24* @test25* @bug 826291326* @summary Verifies DefineClass with null or truncate bytes gets appropriate exception27* @library /test/lib28* @modules java.base/jdk.internal.misc29* @compile A.java30* @run main/native NullClassBytesTest31*/3233import jdk.test.lib.classloader.ClassUnloadCommon;3435public class NullClassBytesTest {3637static native Class<?> nativeDefineClass(String name, ClassLoader ldr, byte[] class_bytes, int length);3839static {40System.loadLibrary("NullClassBytesTest");41}4243static class SimpleLoader extends ClassLoader {4445public Class<?> loadClass(String name) throws ClassNotFoundException {46synchronized(getClassLoadingLock(name)) {47Class<?> c = findLoadedClass(name);48if (c != null) return c;4950// load the class data from the connection51if (name.equals("A")) {52byte[] b = ClassUnloadCommon.getClassData("A");53return defineClass(name, b, 0, b.length);54} else if (name.equals("B")) {55byte[] b = new byte[0];56return defineClass(name, b, 0, b.length);57} else if (name.equals("C")) {58byte[] b = null;59return defineClass(name, b, 0, 0);60} else if (name.equals("D")) {61byte[] b = new byte[0];62return nativeDefineClass(name, this, b, b.length);63} else if (name.equals("E")) {64byte[] b = null;65return nativeDefineClass(name, this, b, 0);66} else {67return super.loadClass(name);68}69}70}71}7273public static void main(java.lang.String[] unused) throws Exception {74SimpleLoader ldr = new SimpleLoader();75Class<?> a = Class.forName("A", true, ldr);76Object obj = a.getConstructor().newInstance();7778// If byte array points to null, the JVM throws ClassFormatError("Truncated class file")79try {80Class<?> b = Class.forName("B", true, ldr);81} catch (ClassFormatError cfe) {82if (!cfe.getMessage().contains("Truncated class file")) {83cfe.printStackTrace();84throw new RuntimeException("Wrong message");85}86}8788// If byte array is null, ClassLoader native detects this and throws NPE89// before calling JVM_DefineClassWithSource90try {91Class<?> c = Class.forName("C", true, ldr);92} catch (NullPointerException npe) {}9394// Test JNI_DefineClass with truncated bytes95try {96Class<?> c = Class.forName("D", true, ldr);97} catch (ClassFormatError cfe) {98if (!cfe.getMessage().contains("Truncated class file")) {99cfe.printStackTrace();100throw new RuntimeException("Wrong message");101}102}103104// Native methods must throw their own NPE105try {106Class<?> c = Class.forName("E", true, ldr);107} catch (NullPointerException npe) {108if (!npe.getMessage().equals("class_bytes are null")) {109npe.printStackTrace();110throw new RuntimeException("Wrong message");111}112}113System.out.println("TEST PASSED");114}115}116117118