Skip to content

Commit eb6d73e

Browse files
committed
[GR-65636] Make foreign API support optional in parser.
PullRequest: graal/21033
2 parents bd3008c + 59235a6 commit eb6d73e

File tree

3 files changed

+32
-20
lines changed

3 files changed

+32
-20
lines changed

substratevm/src/com.oracle.svm.core.foreign/src/com/oracle/svm/core/foreign/Target_jdk_internal_misc_ScopedMemoryAccess.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,11 @@
2525
package com.oracle.svm.core.foreign;
2626

2727
import java.io.FileDescriptor;
28-
import java.lang.annotation.Retention;
29-
import java.lang.annotation.RetentionPolicy;
3028
import java.lang.ref.Reference;
3129

3230
import com.oracle.svm.core.AlwaysInline;
3331
import com.oracle.svm.core.ArenaIntrinsics;
32+
import com.oracle.svm.core.ForeignSupport;
3433
import com.oracle.svm.core.annotate.Substitute;
3534
import com.oracle.svm.core.annotate.TargetClass;
3635
import com.oracle.svm.core.annotate.TargetElement;
@@ -100,7 +99,7 @@ static void registerNatives() {
10099
@SuppressWarnings("static-method")
101100
@Substitute
102101
@TargetElement(onlyWith = SharedArenasEnabled.class)
103-
@SVMScoped
102+
@ForeignSupport.Scoped
104103
@AlwaysInline("Safepoints must be visible in caller")
105104
public void loadInternal(MemorySessionImpl session, MappedMemoryUtilsProxy mappedUtils, long address, boolean isSync, long size) {
106105
SubstrateForeignUtil.checkIdentity(mappedUtils, Target_java_nio_MappedMemoryUtils.PROXY);
@@ -121,7 +120,7 @@ public void loadInternal(MemorySessionImpl session, MappedMemoryUtilsProxy mappe
121120
@SuppressWarnings("static-method")
122121
@Substitute
123122
@TargetElement(onlyWith = SharedArenasEnabled.class)
124-
@SVMScoped
123+
@ForeignSupport.Scoped
125124
@AlwaysInline("Safepoints must be visible in caller")
126125
public boolean isLoadedInternal(MemorySessionImpl session, MappedMemoryUtilsProxy mappedUtils, long address, boolean isSync, long size) {
127126
SubstrateForeignUtil.checkIdentity(mappedUtils, Target_java_nio_MappedMemoryUtils.PROXY);
@@ -143,7 +142,7 @@ public boolean isLoadedInternal(MemorySessionImpl session, MappedMemoryUtilsProx
143142
@SuppressWarnings("static-method")
144143
@Substitute
145144
@TargetElement(onlyWith = SharedArenasEnabled.class)
146-
@SVMScoped
145+
@ForeignSupport.Scoped
147146
@AlwaysInline("Safepoints must be visible in caller")
148147
public void unloadInternal(MemorySessionImpl session, MappedMemoryUtilsProxy mappedUtils, long address, boolean isSync, long size) {
149148
SubstrateForeignUtil.checkIdentity(mappedUtils, Target_java_nio_MappedMemoryUtils.PROXY);
@@ -166,7 +165,7 @@ public void unloadInternal(MemorySessionImpl session, MappedMemoryUtilsProxy map
166165
@SuppressWarnings("static-method")
167166
@Substitute
168167
@TargetElement(onlyWith = SharedArenasEnabled.class)
169-
@SVMScoped
168+
@ForeignSupport.Scoped
170169
@AlwaysInline("Safepoints must be visible in caller")
171170
public void forceInternal(MemorySessionImpl session, MappedMemoryUtilsProxy mappedUtils, FileDescriptor fd, long address, boolean isSync, long index, long length) {
172171
SubstrateForeignUtil.checkIdentity(mappedUtils, Target_java_nio_MappedMemoryUtils.PROXY);
@@ -215,7 +214,3 @@ void closeScope0Unsupported(Target_jdk_internal_foreign_MemorySessionImpl sessio
215214
throw SharedArenasDisabled.fail();
216215
}
217216
}
218-
219-
@Retention(RetentionPolicy.RUNTIME)
220-
@interface SVMScoped {
221-
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/ForeignSupport.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
*/
2525
package com.oracle.svm.core;
2626

27+
import java.lang.annotation.Retention;
28+
import java.lang.annotation.RetentionPolicy;
29+
2730
import org.graalvm.nativeimage.ImageSingletons;
2831

2932
import com.oracle.svm.core.image.DisallowedImageHeapObjects.DisallowedObjectReporter;
@@ -33,9 +36,7 @@
3336
public interface ForeignSupport {
3437
@Fold
3538
static boolean isAvailable() {
36-
boolean result = ImageSingletons.contains(ForeignSupport.class);
37-
assert result || !SubstrateOptions.isForeignAPIEnabled();
38-
return result;
39+
return ImageSingletons.contains(ForeignSupport.class);
3940
}
4041

4142
@Fold
@@ -48,4 +49,14 @@ static ForeignSupport singleton() {
4849
void onMemorySegmentReachable(Object obj, DisallowedObjectReporter reporter);
4950

5051
void onScopeReachable(Object obj, DisallowedObjectReporter reporter);
52+
53+
/**
54+
* This annotation is used to mark substitution methods that substitute an
55+
* {@code jdk.internal.misc.ScopedMemoryAccess.Scoped}-annotated method. This will signal the
56+
* bytecode parser that special instrumentation support is required. Such substitution methods
57+
* are expected to already have a certain structure.
58+
*/
59+
@Retention(RetentionPolicy.RUNTIME)
60+
@interface Scoped {
61+
}
5162
}

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/phases/SharedGraphBuilderPhase.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import static com.oracle.svm.core.SubstrateUtil.toUnboxedClass;
2828
import static jdk.graal.compiler.bytecode.Bytecodes.LDC2_W;
2929

30-
import java.lang.annotation.Annotation;
3130
import java.lang.constant.ConstantDescs;
3231
import java.lang.invoke.LambdaConversionException;
3332
import java.lang.invoke.MethodHandles;
@@ -55,6 +54,7 @@
5554
import com.oracle.graal.pointsto.meta.AnalysisMethod;
5655
import com.oracle.graal.pointsto.meta.AnalysisType;
5756
import com.oracle.svm.common.meta.MultiMethod;
57+
import com.oracle.svm.core.ForeignSupport;
5858
import com.oracle.svm.core.bootstrap.BootstrapMethodConfiguration;
5959
import com.oracle.svm.core.bootstrap.BootstrapMethodConfiguration.BootstrapMethodRecord;
6060
import com.oracle.svm.core.bootstrap.BootstrapMethodInfo;
@@ -178,15 +178,21 @@ protected void run(StructuredGraph graph) {
178178

179179
public abstract static class SharedBytecodeParser extends BytecodeParser {
180180

181-
private static final Class<?> SCOPED_SUBSTRATE_ANNOTATION;
182181
private static final Executable SESSION_EXCEPTION_HANDLER_METHOD;
183182
private static final Class<?> MAPPED_MEMORY_UTILS_PROXY_CLASS;
184183
private static final Class<?> ABSTRACT_MEMORY_SEGMENT_IMPL_CLASS;
185184

186185
static {
187-
SCOPED_SUBSTRATE_ANNOTATION = ReflectionUtil.lookupClass("com.oracle.svm.core.foreign.SVMScoped");
188-
Class<?> substrateForeignUtilClass = ReflectionUtil.lookupClass("com.oracle.svm.core.foreign.SubstrateForeignUtil");
189-
SESSION_EXCEPTION_HANDLER_METHOD = ReflectionUtil.lookupMethod(substrateForeignUtilClass, "sessionExceptionHandler", MemorySessionImpl.class, Object.class, long.class);
186+
/*
187+
* Class 'SubstrateForeignUtil' is optional because it is contained in a different
188+
* distribution which may not always be available.
189+
*/
190+
Class<?> substrateForeignUtilClass = ReflectionUtil.lookupClass(true, "com.oracle.svm.core.foreign.SubstrateForeignUtil");
191+
if (substrateForeignUtilClass != null) {
192+
SESSION_EXCEPTION_HANDLER_METHOD = ReflectionUtil.lookupMethod(substrateForeignUtilClass, "sessionExceptionHandler", MemorySessionImpl.class, Object.class, long.class);
193+
} else {
194+
SESSION_EXCEPTION_HANDLER_METHOD = null;
195+
}
190196
MAPPED_MEMORY_UTILS_PROXY_CLASS = ReflectionUtil.lookupClass("jdk.internal.access.foreign.MappedMemoryUtilsProxy");
191197
ABSTRACT_MEMORY_SEGMENT_IMPL_CLASS = ReflectionUtil.lookupClass("jdk.internal.foreign.AbstractMemorySegmentImpl");
192198
}
@@ -247,9 +253,9 @@ protected void build(FixedWithNextNode startInstruction, FrameStateBuilder start
247253
}
248254
}
249255

250-
if (SCOPED_SUBSTRATE_ANNOTATION != null && SharedArenaSupport.SCOPED_ANNOTATION != null && graph.method() != null) {
256+
if (graph.method() != null) {
251257
try {
252-
if (AnnotationAccess.isAnnotationPresent(method, (Class<? extends Annotation>) SCOPED_SUBSTRATE_ANNOTATION) && SharedArenaSupport.isAvailable()) {
258+
if (AnnotationAccess.isAnnotationPresent(method, ForeignSupport.Scoped.class) && SharedArenaSupport.isAvailable()) {
253259
// substituted, only add the scoped node
254260
introduceScopeNodes();
255261
} else if (AnnotationAccess.isAnnotationPresent(method, SharedArenaSupport.SCOPED_ANNOTATION) && SharedArenaSupport.isAvailable()) {

0 commit comments

Comments
 (0)