|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, 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 8262913 |
| 27 | + * @summary Verifies DefineClass with null or truncate bytes gets appropriate exception |
| 28 | + * @library /test/lib |
| 29 | + * @modules java.base/jdk.internal.misc |
| 30 | + * @compile A.java |
| 31 | + * @run main/native NullClassBytesTest |
| 32 | + */ |
| 33 | + |
| 34 | +import java.io.*; |
| 35 | + |
| 36 | +public class NullClassBytesTest { |
| 37 | + |
| 38 | + static native Class<?> nativeDefineClass(String name, ClassLoader ldr, byte[] class_bytes, int length); |
| 39 | + |
| 40 | + static { |
| 41 | + System.loadLibrary("NullClassBytesTest"); |
| 42 | + } |
| 43 | + |
| 44 | + static class SimpleLoader extends ClassLoader { |
| 45 | + |
| 46 | + public Class<?> loadClass(String name) throws ClassNotFoundException { |
| 47 | + synchronized(getClassLoadingLock(name)) { |
| 48 | + Class<?> c = findLoadedClass(name); |
| 49 | + if (c != null) return c; |
| 50 | + |
| 51 | + // load the class data from the connection |
| 52 | + if (name.equals("A")) { |
| 53 | + byte[] b = getClassData("A"); |
| 54 | + return defineClass(name, b, 0, b.length); |
| 55 | + } else if (name.equals("B")) { |
| 56 | + byte[] b = new byte[0]; |
| 57 | + return defineClass(name, b, 0, b.length); |
| 58 | + } else if (name.equals("C")) { |
| 59 | + byte[] b = null; |
| 60 | + return defineClass(name, b, 0, 0); |
| 61 | + } else if (name.equals("D")) { |
| 62 | + byte[] b = new byte[0]; |
| 63 | + return nativeDefineClass(name, this, b, b.length); |
| 64 | + } else if (name.equals("E")) { |
| 65 | + byte[] b = null; |
| 66 | + return nativeDefineClass(name, this, b, 0); |
| 67 | + } else { |
| 68 | + return super.loadClass(name); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + byte[] getClassData(String name) { |
| 74 | + try { |
| 75 | + return SimpleLoader.class.getClassLoader().getResourceAsStream(name + ".class").readAllBytes(); |
| 76 | + } catch (IOException e) { |
| 77 | + return null; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public static void main(java.lang.String[] unused) throws Exception { |
| 83 | + SimpleLoader ldr = new SimpleLoader(); |
| 84 | + Class<?> a = Class.forName("A", true, ldr); |
| 85 | + Object obj = a.getConstructor().newInstance(); |
| 86 | + |
| 87 | + // If byte array points to null, the JVM throws ClassFormatError("Truncated class file") |
| 88 | + try { |
| 89 | + Class<?> b = Class.forName("B", true, ldr); |
| 90 | + } catch (ClassFormatError cfe) { |
| 91 | + if (!cfe.getMessage().contains("Truncated class file")) { |
| 92 | + cfe.printStackTrace(); |
| 93 | + throw new RuntimeException("Wrong message"); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + // If byte array is null, ClassLoader native detects this and throws NPE |
| 98 | + // before calling JVM_DefineClassWithSource |
| 99 | + try { |
| 100 | + Class<?> c = Class.forName("C", true, ldr); |
| 101 | + } catch (NullPointerException npe) {} |
| 102 | + |
| 103 | + // Test JNI_DefineClass with truncated bytes |
| 104 | + try { |
| 105 | + Class<?> c = Class.forName("D", true, ldr); |
| 106 | + } catch (ClassFormatError cfe) { |
| 107 | + if (!cfe.getMessage().contains("Truncated class file")) { |
| 108 | + cfe.printStackTrace(); |
| 109 | + throw new RuntimeException("Wrong message"); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // Native methods must throw their own NPE |
| 114 | + try { |
| 115 | + Class<?> c = Class.forName("E", true, ldr); |
| 116 | + } catch (NullPointerException npe) { |
| 117 | + if (!npe.getMessage().equals("class_bytes are null")) { |
| 118 | + npe.printStackTrace(); |
| 119 | + throw new RuntimeException("Wrong message"); |
| 120 | + } |
| 121 | + } |
| 122 | + System.out.println("TEST PASSED"); |
| 123 | + } |
| 124 | +} |
0 commit comments