This repository has been archived by the owner. It is now read-only.
Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
8262913: KlassFactory::create_from_stream should never return NULL
Reviewed-by: hseigel, iklam
- Loading branch information
Showing
with
220 additions
and 45 deletions.
- +4 −2 src/hotspot/share/classfile/classFileStream.cpp
- +1 −4 src/hotspot/share/classfile/klassFactory.cpp
- +4 −5 src/hotspot/share/classfile/systemDictionary.cpp
- +1 −1 src/hotspot/share/prims/jni.cpp
- +16 −23 src/hotspot/share/prims/jvm.cpp
- +7 −10 src/hotspot/share/prims/unsafe.cpp
- +24 −0 test/hotspot/jtreg/runtime/DefineClass/A.java
- +124 −0 test/hotspot/jtreg/runtime/DefineClass/NullClassBytesTest.java
- +39 −0 test/hotspot/jtreg/runtime/DefineClass/libNullClassBytesTest.c
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -288,7 +288,7 @@ JNI_ENTRY(jclass, jni_DefineClass(JNIEnv *env, const char *name, jobject loaderR | ||
&st, | ||
CHECK_NULL); | ||
|
||
if (log_is_enabled(Debug, class, resolve)) { | ||
trace_class_resolution(k); | ||
} | ||
|
||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
* or visit www.oracle.com if you need additional information or have any | ||
* questions. | ||
*/ | ||
|
||
public class A { public A() { System.out.println("A called"); } } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,124 @@ | ||
/* | ||
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
* or visit www.oracle.com if you need additional information or have any | ||
* questions. | ||
*/ | ||
|
||
/* | ||
* @test | ||
* @bug 8262913 | ||
* @summary Verifies DefineClass with null or truncate bytes gets appropriate exception | ||
* @library /test/lib | ||
* @modules java.base/jdk.internal.misc | ||
* @compile A.java | ||
* @run main/native NullClassBytesTest | ||
*/ | ||
|
||
import java.io.*; | ||
|
||
public class NullClassBytesTest { | ||
|
||
static native Class<?> nativeDefineClass(String name, ClassLoader ldr, byte[] class_bytes, int length); | ||
|
||
static { | ||
System.loadLibrary("NullClassBytesTest"); | ||
} | ||
|
||
static class SimpleLoader extends ClassLoader { | ||
|
||
public Class<?> loadClass(String name) throws ClassNotFoundException { | ||
synchronized(getClassLoadingLock(name)) { | ||
Class<?> c = findLoadedClass(name); | ||
if (c != null) return c; | ||
|
||
// load the class data from the connection | ||
if (name.equals("A")) { | ||
byte[] b = getClassData("A"); | ||
return defineClass(name, b, 0, b.length); | ||
} else if (name.equals("B")) { | ||
byte[] b = new byte[0]; | ||
return defineClass(name, b, 0, b.length); | ||
} else if (name.equals("C")) { | ||
byte[] b = null; | ||
return defineClass(name, b, 0, 0); | ||
} else if (name.equals("D")) { | ||
byte[] b = new byte[0]; | ||
return nativeDefineClass(name, this, b, b.length); | ||
} else if (name.equals("E")) { | ||
byte[] b = null; | ||
return nativeDefineClass(name, this, b, 0); | ||
} else { | ||
return super.loadClass(name); | ||
} | ||
} | ||
} | ||
|
||
byte[] getClassData(String name) { | ||
try { | ||
return SimpleLoader.class.getClassLoader().getResourceAsStream(name + ".class").readAllBytes(); | ||
} catch (IOException e) { | ||
return null; | ||
} | ||
} | ||
} | ||
|
||
public static void main(java.lang.String[] unused) throws Exception { | ||
SimpleLoader ldr = new SimpleLoader(); | ||
Class<?> a = Class.forName("A", true, ldr); | ||
Object obj = a.getConstructor().newInstance(); | ||
|
||
// If byte array points to null, the JVM throws ClassFormatError("Truncated class file") | ||
try { | ||
Class<?> b = Class.forName("B", true, ldr); | ||
} catch (ClassFormatError cfe) { | ||
if (!cfe.getMessage().contains("Truncated class file")) { | ||
cfe.printStackTrace(); | ||
throw new RuntimeException("Wrong message"); | ||
} | ||
} | ||
|
||
// If byte array is null, ClassLoader native detects this and throws NPE | ||
// before calling JVM_DefineClassWithSource | ||
try { | ||
Class<?> c = Class.forName("C", true, ldr); | ||
} catch (NullPointerException npe) {} | ||
|
||
// Test JNI_DefineClass with truncated bytes | ||
try { | ||
Class<?> c = Class.forName("D", true, ldr); | ||
} catch (ClassFormatError cfe) { | ||
if (!cfe.getMessage().contains("Truncated class file")) { | ||
cfe.printStackTrace(); | ||
throw new RuntimeException("Wrong message"); | ||
} | ||
} | ||
|
||
// Native methods must throw their own NPE | ||
try { | ||
Class<?> c = Class.forName("E", true, ldr); | ||
} catch (NullPointerException npe) { | ||
if (!npe.getMessage().equals("class_bytes are null")) { | ||
npe.printStackTrace(); | ||
throw new RuntimeException("Wrong message"); | ||
} | ||
} | ||
System.out.println("TEST PASSED"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
* or visit www.oracle.com if you need additional information or have any | ||
* questions. | ||
*/ | ||
|
||
#include <jni.h> | ||
|
||
JNIEXPORT void JNICALL | ||
Java_NullClassBytesTest_nativeDefineClass(JNIEnv *env, jclass klass, jstring className, jobject ldr, | ||
jbyte* class_bytes, jint length) { | ||
if (class_bytes == NULL) { | ||
jclass cls = (*env)->FindClass(env, "java/lang/NullPointerException"); | ||
|
||
if (cls != 0) { | ||
(*env)->ThrowNew(env, cls, "class_bytes are null"); | ||
} | ||
return; | ||
} | ||
const char* c_name = (*env)->GetStringUTFChars(env, className, NULL); | ||
(*env)->DefineClass(env, c_name, ldr, class_bytes, length); | ||
} |