Skip to content

Commit 4d21a45

Browse files
committed
8262913: KlassFactory::create_from_stream should never return NULL
Reviewed-by: hseigel, iklam
1 parent fab5676 commit 4d21a45

File tree

9 files changed

+220
-45
lines changed

9 files changed

+220
-45
lines changed

src/hotspot/share/classfile/classFileStream.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -45,7 +45,9 @@ ClassFileStream::ClassFileStream(const u1* buffer,
4545
_current(buffer),
4646
_source(source),
4747
_need_verify(verify_stream),
48-
_from_boot_loader_modules_image(from_boot_loader_modules_image) {}
48+
_from_boot_loader_modules_image(from_boot_loader_modules_image) {
49+
assert(buffer != NULL, "caller should throw NPE");
50+
}
4951

5052
const u1* ClassFileStream::clone_buffer() const {
5153
u1* const new_buffer_start = NEW_RESOURCE_ARRAY(u1, length());

src/hotspot/share/classfile/klassFactory.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,7 @@ InstanceKlass* KlassFactory::create_from_stream(ClassFileStream* stream,
205205

206206
const ClassInstanceInfo* cl_inst_info = cl_info.class_hidden_info_ptr();
207207
InstanceKlass* result = parser.create_instance_klass(old_stream != stream, *cl_inst_info, CHECK_NULL);
208-
209-
if (result == NULL) {
210-
return NULL;
211-
}
208+
assert(result != NULL, "result cannot be null with no pending exception");
212209

213210
if (cached_class_file != NULL) {
214211
// JVMTI: we have an InstanceKlass now, tell it about the cached bytes

src/hotspot/share/classfile/systemDictionary.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -990,8 +990,9 @@ InstanceKlass* SystemDictionary::parse_stream(Symbol* class_name,
990990
loader_data,
991991
cl_info,
992992
CHECK_NULL);
993+
assert(k != NULL, "no klass created");
993994

994-
if ((cl_info.is_hidden() || is_unsafe_anon_class) && k != NULL) {
995+
if (cl_info.is_hidden() || is_unsafe_anon_class) {
995996
// Hidden classes that are not strong and unsafe anonymous classes must update
996997
// ClassLoaderData holder so that they can be unloaded when the mirror is no
997998
// longer referenced.
@@ -1035,7 +1036,8 @@ InstanceKlass* SystemDictionary::parse_stream(Symbol* class_name,
10351036
// JVM_DefineClass).
10361037
// Note: class_name can be NULL. In that case we do not know the name of
10371038
// the class until we have parsed the stream.
1038-
1039+
// This function either returns an InstanceKlass or throws an exception. It does
1040+
// not return NULL without a pending exception.
10391041
InstanceKlass* SystemDictionary::resolve_from_stream(Symbol* class_name,
10401042
Handle class_loader,
10411043
Handle protection_domain,
@@ -1068,9 +1070,6 @@ InstanceKlass* SystemDictionary::resolve_from_stream(Symbol* class_name,
10681070
#endif
10691071

10701072
if (k == NULL) {
1071-
if (st->buffer() == NULL) {
1072-
return NULL;
1073-
}
10741073
ClassLoadInfo cl_info(protection_domain);
10751074
k = KlassFactory::create_from_stream(st, class_name, loader_data, cl_info, CHECK_NULL);
10761075
}

src/hotspot/share/prims/jni.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ JNI_ENTRY(jclass, jni_DefineClass(JNIEnv *env, const char *name, jobject loaderR
288288
&st,
289289
CHECK_NULL);
290290

291-
if (log_is_enabled(Debug, class, resolve) && k != NULL) {
291+
if (log_is_enabled(Debug, class, resolve)) {
292292
trace_class_resolution(k);
293293
}
294294

src/hotspot/share/prims/jvm.cpp

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ static jclass jvm_define_class_common(const char *name,
866866
&st,
867867
CHECK_NULL);
868868

869-
if (log_is_enabled(Debug, class, resolve) && k != NULL) {
869+
if (log_is_enabled(Debug, class, resolve)) {
870870
trace_class_resolution(k);
871871
}
872872

@@ -945,19 +945,17 @@ static jclass jvm_lookup_define_class(jclass lookup, const char *name,
945945
const char* source = is_nestmate ? host_class->external_name() : "__JVM_LookupDefineClass__";
946946
ClassFileStream st((u1*)buf, len, source, ClassFileStream::verify);
947947

948-
Klass* defined_k;
949948
InstanceKlass* ik = NULL;
950949
if (!is_hidden) {
951-
defined_k = SystemDictionary::resolve_from_stream(class_name,
952-
class_loader,
953-
protection_domain,
954-
&st,
955-
CHECK_NULL);
956-
957-
if (log_is_enabled(Debug, class, resolve) && defined_k != NULL) {
958-
trace_class_resolution(defined_k);
950+
ik = SystemDictionary::resolve_from_stream(class_name,
951+
class_loader,
952+
protection_domain,
953+
&st,
954+
CHECK_NULL);
955+
956+
if (log_is_enabled(Debug, class, resolve)) {
957+
trace_class_resolution(ik);
959958
}
960-
ik = InstanceKlass::cast(defined_k);
961959
} else { // hidden
962960
Handle classData_h(THREAD, JNIHandles::resolve(classData));
963961
ClassLoadInfo cl_info(protection_domain,
@@ -968,16 +966,11 @@ static jclass jvm_lookup_define_class(jclass lookup, const char *name,
968966
is_hidden,
969967
is_strong,
970968
vm_annotations);
971-
defined_k = SystemDictionary::parse_stream(class_name,
972-
class_loader,
973-
&st,
974-
cl_info,
975-
CHECK_NULL);
976-
if (defined_k == NULL) {
977-
THROW_MSG_0(vmSymbols::java_lang_Error(), "Failure to define a hidden class");
978-
}
979-
980-
ik = InstanceKlass::cast(defined_k);
969+
ik = SystemDictionary::parse_stream(class_name,
970+
class_loader,
971+
&st,
972+
cl_info,
973+
CHECK_NULL);
981974

982975
// The hidden class loader data has been artificially been kept alive to
983976
// this point. The mirror and any instances of this class have to keep
@@ -994,7 +987,7 @@ static jclass jvm_lookup_define_class(jclass lookup, const char *name,
994987
ik->is_hidden() ? "is hidden" : "is not hidden");
995988
}
996989
}
997-
assert(Reflection::is_same_class_package(lookup_k, defined_k),
990+
assert(Reflection::is_same_class_package(lookup_k, ik),
998991
"lookup class and defined class are in different packages");
999992

1000993
if (init) {
@@ -1003,7 +996,7 @@ static jclass jvm_lookup_define_class(jclass lookup, const char *name,
1003996
ik->link_class(CHECK_NULL);
1004997
}
1005998

1006-
return (jclass) JNIHandles::make_local(THREAD, defined_k->java_mirror());
999+
return (jclass) JNIHandles::make_local(THREAD, ik->java_mirror());
10071000
}
10081001

10091002
JVM_ENTRY(jclass, JVM_DefineClass(JNIEnv *env, const char *name, jobject loader, const jbyte *buf, jsize len, jobject pd))

src/hotspot/share/prims/unsafe.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -862,16 +862,13 @@ Unsafe_DefineAnonymousClass_impl(JNIEnv *env,
862862
false, // is_strong_hidden
863863
true); // can_access_vm_annotations
864864

865-
Klass* anonk = SystemDictionary::parse_stream(no_class_name,
866-
host_loader,
867-
&st,
868-
cl_info,
869-
CHECK_NULL);
870-
if (anonk == NULL) {
871-
return NULL;
872-
}
873-
874-
return InstanceKlass::cast(anonk);
865+
InstanceKlass* anonk = SystemDictionary::parse_stream(no_class_name,
866+
host_loader,
867+
&st,
868+
cl_info,
869+
CHECK_NULL);
870+
assert(anonk != NULL, "no klass created");
871+
return anonk;
875872
}
876873

877874
UNSAFE_ENTRY(jclass, Unsafe_DefineAnonymousClass0(JNIEnv *env, jobject unsafe, jclass host_class, jbyteArray data, jobjectArray cp_patches_jh)) {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
public class A { public A() { System.out.println("A called"); } }
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
#include <jni.h>
25+
26+
JNIEXPORT void JNICALL
27+
Java_NullClassBytesTest_nativeDefineClass(JNIEnv *env, jclass klass, jstring className, jobject ldr,
28+
jbyte* class_bytes, jint length) {
29+
if (class_bytes == NULL) {
30+
jclass cls = (*env)->FindClass(env, "java/lang/NullPointerException");
31+
32+
if (cls != 0) {
33+
(*env)->ThrowNew(env, cls, "class_bytes are null");
34+
}
35+
return;
36+
}
37+
const char* c_name = (*env)->GetStringUTFChars(env, className, NULL);
38+
(*env)->DefineClass(env, c_name, ldr, class_bytes, length);
39+
}

0 commit comments

Comments
 (0)