Skip to content

Commit a132583

Browse files
[GR-71157] [GR-71158] Fix some exceptions during type resolution
PullRequest: graal/22524
2 parents 2abb822 + 762d33c commit a132583

File tree

15 files changed

+121
-71
lines changed

15 files changed

+121
-71
lines changed

sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/impl/ClassLoading.java

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,48 @@ public static boolean isSupported() {
6060
}
6161

6262
/**
63-
* Opens a scope in which class loading is not constrained by the reflection configuration. Once
64-
* the returned {@link AutoCloseable} is closed, the previous state is restored.
63+
* Opens a scope in which class loading is not constrained by the reflection configuration for
64+
* the current thread. Once the returned {@link AutoCloseable} is closed, the previous state is
65+
* restored.
6566
*
6667
* @throws IllegalStateException if class loading is not {@linkplain #isSupported() supported}.
6768
*/
6869
public static ArbitraryClassLoadingScope allowArbitraryClassLoading() {
6970
if (!isSupported()) {
7071
throw new IllegalStateException("Runtime class loading is not supported. It must be enabled at build-time with -H:+RuntimeClassLoading.");
7172
}
72-
return new ArbitraryClassLoadingScope();
73+
return new RealArbitraryClassLoadingScope();
7374
}
7475

75-
public static final class ArbitraryClassLoadingScope implements AutoCloseable {
76+
/**
77+
* Conditionally opens a scope in which class loading is not constrained by the reflection
78+
* configuration for the current thread. If {@code allowArbitraryClassLoading} is true, behaves
79+
* like {@link #allowArbitraryClassLoading()}. Otherwise, it has no effect.
80+
*
81+
* @throws IllegalStateException if class loading is not {@linkplain #isSupported() supported}
82+
* and {@code allowArbitraryClassLoading} is true.
83+
*
84+
* @see #allowArbitraryClassLoading()
85+
*/
86+
public static ArbitraryClassLoadingScope allowArbitraryClassLoading(boolean allowArbitraryClassLoading) {
87+
if (allowArbitraryClassLoading) {
88+
return allowArbitraryClassLoading();
89+
}
90+
return NoopScope.INSTANCE;
91+
}
92+
93+
public abstract static class ArbitraryClassLoadingScope implements AutoCloseable {
94+
private ArbitraryClassLoadingScope() {
95+
}
96+
97+
@Override
98+
public abstract void close();
99+
}
100+
101+
public static final class RealArbitraryClassLoadingScope extends ArbitraryClassLoadingScope {
76102
private boolean closed;
77103

78-
ArbitraryClassLoadingScope() {
104+
RealArbitraryClassLoadingScope() {
79105
if (!ImageInfo.inImageRuntimeCode()) {
80106
return;
81107
}
@@ -94,4 +120,12 @@ public void close() {
94120
ClassLoadingSupport.singleton().endIgnoreReflectionConfigurationScope();
95121
}
96122
}
123+
124+
private static final class NoopScope extends ArbitraryClassLoadingScope {
125+
private static final NoopScope INSTANCE = new NoopScope();
126+
127+
@Override
128+
public void close() {
129+
}
130+
}
97131
}

sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/impl/ClassLoadingSupport.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,28 @@ static ClassLoadingSupport singleton() {
4747
return ImageSingletons.lookup(ClassLoadingSupport.class);
4848
}
4949

50+
/**
51+
* Returns whether runtime class loading is supported.
52+
*/
5053
boolean isSupported();
5154

55+
/**
56+
* Returns false iff the current thread is in a
57+
* {@linkplain #startIgnoreReflectionConfigurationScope() scope} where reflection configuration
58+
* is ignored.
59+
*/
5260
boolean followReflectionConfiguration();
5361

62+
/**
63+
* Starts a scope in which the reflection configuration is ignored for runtime class loading.
64+
* <p>
65+
* That scope can be closed by calling {@link #endIgnoreReflectionConfigurationScope()}.
66+
*/
5467
void startIgnoreReflectionConfigurationScope();
5568

69+
/**
70+
* Ends a scope started by {@link #startIgnoreReflectionConfigurationScope()} for the current
71+
* thread.
72+
*/
5673
void endIgnoreReflectionConfigurationScope();
5774
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/hub/OWNERS.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ all = [
66
]
77
any = [
88
]
9+
[[rule]]
10+
files = "RuntimeClassLoading.java RuntimeDynamicHubMetadata.java RuntimeReflectionMetadata.java"
11+
all = [
12+
"gilles.m.duboscq@oracle.com",
13+
]

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/hub/crema/CremaSupport.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@
3939
import com.oracle.svm.espresso.classfile.descriptors.Symbol;
4040
import com.oracle.svm.espresso.classfile.descriptors.Type;
4141

42-
import jdk.vm.ci.meta.JavaType;
4342
import jdk.vm.ci.meta.ResolvedJavaField;
4443
import jdk.vm.ci.meta.ResolvedJavaMethod;
4544
import jdk.vm.ci.meta.ResolvedJavaType;
45+
import jdk.vm.ci.meta.UnresolvedJavaType;
4646

4747
public interface CremaSupport {
4848
@Platforms(Platform.HOSTED_ONLY.class)
@@ -88,23 +88,23 @@ interface CremaDispatchTable {
8888

8989
Class<?> toClass(ResolvedJavaType resolvedJavaType);
9090

91-
default Class<?> resolveOrThrow(JavaType unresolvedJavaType, ResolvedJavaType accessingClass) {
91+
default Class<?> resolveOrThrow(UnresolvedJavaType unresolvedJavaType, ResolvedJavaType accessingClass) {
9292
ByteSequence type = ByteSequence.create(unresolvedJavaType.getName());
9393
Symbol<Type> symbolicType = SymbolsSupport.getTypes().getOrCreateValidType(type);
9494
return resolveOrThrow(symbolicType, accessingClass);
9595
}
9696

9797
Class<?> resolveOrThrow(Symbol<Type> type, ResolvedJavaType accessingClass);
9898

99-
default Class<?> resolveOrNull(JavaType unresolvedJavaType, ResolvedJavaType accessingClass) {
99+
default Class<?> resolveOrNull(UnresolvedJavaType unresolvedJavaType, ResolvedJavaType accessingClass) {
100100
ByteSequence type = ByteSequence.create(unresolvedJavaType.getName());
101101
Symbol<Type> symbolicType = SymbolsSupport.getTypes().getOrCreateValidType(type);
102102
return resolveOrNull(symbolicType, accessingClass);
103103
}
104104

105105
Class<?> resolveOrNull(Symbol<Type> type, ResolvedJavaType accessingClass);
106106

107-
default Class<?> findLoadedClass(JavaType unresolvedJavaType, ResolvedJavaType accessingClass) {
107+
default Class<?> findLoadedClass(UnresolvedJavaType unresolvedJavaType, ResolvedJavaType accessingClass) {
108108
ByteSequence type = ByteSequence.create(unresolvedJavaType.getName());
109109
Symbol<Type> symbolicType = SymbolsSupport.getTypes().getOrCreateValidType(type);
110110
return findLoadedClass(symbolicType, accessingClass);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[[rule]]
2+
files = "*"
3+
all = [
4+
"gilles.m.duboscq@oracle.com",
5+
]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[[rule]]
2+
files = "*"
3+
all = [
4+
"gilles.m.duboscq@oracle.com",
5+
]
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
[[rule]]
22
files = "*"
33
all = [
4-
"alfonso.peterssen@oracle.com",
54
"bernhard.urban-forster@oracle.com",
6-
"martin.entlicher@oracle.com",
7-
]
8-
any = [
9-
"alfonso.peterssen@oracle.com",
10-
"bernhard.urban-forster@oracle.com",
11-
"martin.entlicher@oracle.com",
5+
"gilles.m.duboscq@oracle.com",
126
]

substratevm/src/com.oracle.svm.interpreter.metadata/src/com/oracle/svm/interpreter/metadata/CremaResolvedJavaFieldImpl.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
*/
2525
package com.oracle.svm.interpreter.metadata;
2626

27+
import org.graalvm.nativeimage.impl.ClassLoading;
28+
2729
import com.oracle.svm.core.hub.DynamicHub;
2830
import com.oracle.svm.core.hub.crema.CremaResolvedJavaField;
2931
import com.oracle.svm.core.hub.crema.CremaSupport;
@@ -78,8 +80,10 @@ public JavaType getType() {
7880
@Override
7981
public InterpreterResolvedJavaType getResolvedType() {
8082
if (resolvedType == null) {
81-
Class<?> cls = CremaSupport.singleton().resolveOrThrow(getSymbolicType(), getDeclaringClass());
82-
resolvedType = (InterpreterResolvedJavaType) DynamicHub.fromClass(cls).getInterpreterType();
83+
try (var _ = ClassLoading.allowArbitraryClassLoading()) {
84+
Class<?> cls = CremaSupport.singleton().resolveOrThrow(getSymbolicType(), getDeclaringClass());
85+
resolvedType = (InterpreterResolvedJavaType) DynamicHub.fromClass(cls).getInterpreterType();
86+
}
8387
}
8488
return resolvedType;
8589
}
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
[[rule]]
22
files = "*"
33
all = [
4-
"alfonso.peterssen@oracle.com",
54
"bernhard.urban-forster@oracle.com",
6-
"martin.entlicher@oracle.com",
7-
]
8-
any = [
9-
"alfonso.peterssen@oracle.com",
10-
"bernhard.urban-forster@oracle.com",
11-
"martin.entlicher@oracle.com",
5+
"gilles.m.duboscq@oracle.com",
126
]

substratevm/src/com.oracle.svm.interpreter/src/com/oracle/svm/interpreter/CremaRuntimeAccess.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import java.lang.invoke.MethodHandle;
2828
import java.lang.invoke.MethodType;
2929

30+
import org.graalvm.nativeimage.impl.ClassLoading;
31+
3032
import com.oracle.svm.core.hub.DynamicHub;
3133
import com.oracle.svm.core.hub.crema.CremaSupport;
3234
import com.oracle.svm.core.hub.registry.SymbolsSupport;
@@ -94,9 +96,11 @@ public RuntimeException throwError(ErrorType errorType, String messageFormat, Ob
9496

9597
@Override
9698
public InterpreterResolvedObjectType lookupOrLoadType(Symbol<Type> type, InterpreterResolvedJavaType accessingClass) {
97-
Class<?> result = CremaSupport.singleton().resolveOrThrow(type, accessingClass);
98-
assert !result.isPrimitive();
99-
return (InterpreterResolvedObjectType) DynamicHub.fromClass(result).getInterpreterType();
99+
try (var _ = ClassLoading.allowArbitraryClassLoading()) {
100+
Class<?> result = CremaSupport.singleton().resolveOrThrow(type, accessingClass);
101+
assert !result.isPrimitive();
102+
return (InterpreterResolvedObjectType) DynamicHub.fromClass(result).getInterpreterType();
103+
}
100104
}
101105

102106
@Override

0 commit comments

Comments
 (0)