Skip to content

Commit 75e19e0

Browse files
author
Doug Simon
committed
8314819: [JVMCI] HotSpotJVMCIRuntime.lookupType throws unexpected ClassNotFoundException
Reviewed-by: never, thartmann
1 parent c418933 commit 75e19e0

File tree

5 files changed

+62
-28
lines changed

5 files changed

+62
-28
lines changed

src/hotspot/share/jvmci/jvmciCompilerToVM.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -617,10 +617,7 @@ C2V_VMENTRY_NULL(jobject, lookupType, (JNIEnv* env, jobject, jstring jname, ARGU
617617
}
618618

619619
if (resolve) {
620-
resolved_klass = SystemDictionary::resolve_or_null(class_name, class_loader, protection_domain, CHECK_NULL);
621-
if (resolved_klass == nullptr) {
622-
JVMCI_THROW_MSG_NULL(NoClassDefFoundError, str);
623-
}
620+
resolved_klass = SystemDictionary::resolve_or_fail(class_name, class_loader, protection_domain, true, CHECK_NULL);
624621
} else {
625622
if (Signature::has_envelope(class_name)) {
626623
// This is a name from a signature. Strip off the trimmings.

src/jdk.internal.vm.ci/share/classes/jdk/vm/ci/hotspot/CompilerToVM.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ boolean methodIsIgnoredBySecurityStackWalk(HotSpotResolvedJavaMethodImpl method)
243243
* @param accessingClass the class loader of this class is used for resolution. Must not be null.
244244
* @param resolve force resolution to a {@link ResolvedJavaType}. If true, this method will
245245
* either return a {@link ResolvedJavaType} or throw an exception
246-
* @return the type for {@code name} or 0 if resolution failed and {@code resolve == false}
246+
* @return the type for {@code name} or {@code null} if resolution failed and {@code resolve == false}
247247
* @throws NoClassDefFoundError if {@code resolve == true} and the resolution failed
248248
*/
249249
HotSpotResolvedJavaType lookupType(String name, HotSpotResolvedObjectTypeImpl accessingClass, boolean resolve) throws NoClassDefFoundError {

test/hotspot/jtreg/compiler/jvmci/common/patches/jdk.internal.vm.ci/jdk/vm/ci/hotspot/CompilerToVMHelper.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public static boolean methodIsIgnoredBySecurityStackWalk(HotSpotResolvedJavaMeth
8484
}
8585

8686
public static HotSpotResolvedObjectType lookupType(String name,
87-
Class<?> accessClass, boolean resolve) throws ClassNotFoundException {
87+
Class<?> accessClass, boolean resolve) throws NoClassDefFoundError {
8888
if (accessClass == null) {
8989
throw new NullPointerException();
9090
}
@@ -94,11 +94,7 @@ public static HotSpotResolvedObjectType lookupType(String name,
9494

9595
public static HotSpotResolvedObjectType lookupTypeHelper(String name,
9696
Class<?> accessingClass, boolean resolve) {
97-
try {
98-
return lookupType(name, accessingClass, resolve);
99-
} catch (ClassNotFoundException e) {
100-
throw (NoClassDefFoundError) new NoClassDefFoundError().initCause(e);
101-
}
97+
return lookupType(name, accessingClass, resolve);
10298
}
10399

104100
public static Object lookupConstantInPool(ConstantPool constantPool, int cpi, boolean resolve) {

test/hotspot/jtreg/compiler/jvmci/compilerToVM/LookupTypeTest.java

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
* @library ../common/patches
3030
* @modules java.base/jdk.internal.access
3131
* @modules jdk.internal.vm.ci/jdk.vm.ci.hotspot
32+
* jdk.internal.vm.ci/jdk.vm.ci.runtime
33+
* jdk.internal.vm.ci/jdk.vm.ci.meta
3234
* @build jdk.internal.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper
3335
* @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
3436
* -XX:-UseJVMCICompiler
@@ -43,35 +45,64 @@
4345
import jdk.test.lib.Asserts;
4446
import jdk.test.lib.Utils;
4547
import jdk.vm.ci.hotspot.CompilerToVMHelper;
48+
import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime;
4649
import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;
50+
import jdk.vm.ci.meta.MetaAccessProvider;
51+
import jdk.vm.ci.meta.ResolvedJavaType;
4752

48-
import java.util.HashSet;
49-
import java.util.Set;
53+
import java.io.ByteArrayOutputStream;
54+
import java.io.CharArrayWriter;
55+
import java.io.PrintStream;
56+
import java.util.ArrayList;
57+
import java.util.List;
5058

5159
public class LookupTypeTest {
60+
61+
/**
62+
* Abstracts which lookup method is being tested.
63+
*/
64+
public interface Lookup {
65+
ResolvedJavaType lookupType(String name, Class<?> accessingClass, boolean resolve);
66+
}
67+
5268
public static void main(String args[]) {
5369
LookupTypeTest test = new LookupTypeTest();
54-
for (TestCase tcase : createTestCases()) {
55-
test.runTest(tcase);
70+
71+
// Test CompilerToVM.lookupType
72+
for (TestCase tcase : createTestCases(false, true)) {
73+
test.runTest(tcase, CompilerToVMHelper::lookupType);
74+
}
75+
76+
// Test HotSpotJVMCIRuntime.lookupType
77+
HotSpotJVMCIRuntime runtime = HotSpotJVMCIRuntime.runtime();
78+
MetaAccessProvider metaAccess = runtime.getHostJVMCIBackend().getMetaAccess();
79+
for (TestCase tcase : createTestCases(true, false)) {
80+
test.runTest(tcase, (name, accessingClass, resolve) -> (ResolvedJavaType) runtime.lookupType(name,
81+
(HotSpotResolvedObjectType) metaAccess.lookupJavaType(accessingClass), resolve));
5682
}
5783
}
5884

59-
private static Set<TestCase> createTestCases() {
60-
Set<TestCase> result = new HashSet<>();
85+
private static List<TestCase> createTestCases(boolean allowPrimitive, boolean allowNullAccessingClass) {
86+
List<TestCase> result = new ArrayList<>();
6187
// a primitive class
62-
result.add(new TestCase(Utils.toJVMTypeSignature(int.class),
88+
if (allowPrimitive) {
89+
result.add(new TestCase(Utils.toJVMTypeSignature(int.class),
90+
LookupTypeTest.class, true, true));
91+
} else {
92+
result.add(new TestCase(Utils.toJVMTypeSignature(int.class),
6393
LookupTypeTest.class, true, false, InternalError.class));
94+
}
6495
// lookup not existing class
6596
result.add(new TestCase("Lsome_not_existing;", LookupTypeTest.class,
66-
true, false, ClassNotFoundException.class));
97+
true, false, NoClassDefFoundError.class));
6798
// lookup invalid classname
6899
result.add(new TestCase("L!@#$%^&**()[]{}?;", LookupTypeTest.class,
69-
true, false, ClassNotFoundException.class));
100+
true, false, NoClassDefFoundError.class));
70101
// lookup package private class
71102
result.add(new TestCase(
72103
"Lcompiler/jvmci/compilerToVM/testcases/PackagePrivateClass;",
73104
LookupTypeTest.class, true, false,
74-
ClassNotFoundException.class));
105+
NoClassDefFoundError.class));
75106
// lookup usual class with resolve=true
76107
result.add(new TestCase(Utils.toJVMTypeSignature(SingleSubclass.class),
77108
LookupTypeTest.class, true, true));
@@ -80,25 +111,30 @@ private static Set<TestCase> createTestCases() {
80111
Utils.toJVMTypeSignature(DoNotExtendClass.class),
81112
LookupTypeTest.class, false, true));
82113
// lookup usual class with null accessor
83-
result.add(new TestCase(
114+
if (allowNullAccessingClass) {
115+
result.add(new TestCase(
84116
Utils.toJVMTypeSignature(MultiSubclassedClass.class), null,
85117
false, false, NullPointerException.class));
118+
}
86119
return result;
87120
}
88121

89-
private void runTest(TestCase tcase) {
122+
private void runTest(TestCase tcase, Lookup lookup) {
90123
System.out.println(tcase);
91-
HotSpotResolvedObjectType metaspaceKlass;
124+
ResolvedJavaType metaspaceKlass;
92125
try {
93-
metaspaceKlass = CompilerToVMHelper.lookupType(tcase.className,
126+
metaspaceKlass = lookup.lookupType(tcase.className,
94127
tcase.accessing, tcase.resolve);
95128
} catch (Throwable t) {
129+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
130+
t.printStackTrace(new PrintStream(bos));
131+
String tString = bos.toString();
96132
Asserts.assertNotNull(tcase.expectedException,
97-
"Assumed no exception, but got " + t);
133+
"Assumed no exception, but got " + tString);
98134
Asserts.assertFalse(tcase.isPositive,
99-
"Got unexpected exception " + t);
135+
"Got unexpected exception " + tString);
100136
Asserts.assertEQ(t.getClass(), tcase.expectedException,
101-
"Unexpected exception");
137+
"Unexpected exception: " + tString);
102138
// passed
103139
return;
104140
}

test/hotspot/jtreg/compiler/jvmci/jdk.vm.ci.hotspot.test/src/jdk/vm/ci/hotspot/test/TestHotSpotJVMCIRuntime.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,9 @@ public void jniEnomemTest() throws Exception {
183183
output.shouldNotHaveExitValue(0);
184184
}
185185
}
186+
187+
@Test
188+
public void lookupTypeTest() throws Exception {
189+
// This is tested by compiler/jvmci/compilerToVM/LookupTypeTest.java
190+
}
186191
}

0 commit comments

Comments
 (0)