Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/runtime/DefineClass/NullClassBytesTest.java
40943 views
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 jdk.test.lib.classloader.ClassUnloadCommon;
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 = ClassUnloadCommon.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
74
public static void main(java.lang.String[] unused) throws Exception {
75
SimpleLoader ldr = new SimpleLoader();
76
Class<?> a = Class.forName("A", true, ldr);
77
Object obj = a.getConstructor().newInstance();
78
79
// If byte array points to null, the JVM throws ClassFormatError("Truncated class file")
80
try {
81
Class<?> b = Class.forName("B", true, ldr);
82
} catch (ClassFormatError cfe) {
83
if (!cfe.getMessage().contains("Truncated class file")) {
84
cfe.printStackTrace();
85
throw new RuntimeException("Wrong message");
86
}
87
}
88
89
// If byte array is null, ClassLoader native detects this and throws NPE
90
// before calling JVM_DefineClassWithSource
91
try {
92
Class<?> c = Class.forName("C", true, ldr);
93
} catch (NullPointerException npe) {}
94
95
// Test JNI_DefineClass with truncated bytes
96
try {
97
Class<?> c = Class.forName("D", true, ldr);
98
} catch (ClassFormatError cfe) {
99
if (!cfe.getMessage().contains("Truncated class file")) {
100
cfe.printStackTrace();
101
throw new RuntimeException("Wrong message");
102
}
103
}
104
105
// Native methods must throw their own NPE
106
try {
107
Class<?> c = Class.forName("E", true, ldr);
108
} catch (NullPointerException npe) {
109
if (!npe.getMessage().equals("class_bytes are null")) {
110
npe.printStackTrace();
111
throw new RuntimeException("Wrong message");
112
}
113
}
114
System.out.println("TEST PASSED");
115
}
116
}
117
118