Skip to content

Commit 5a49039

Browse files
committed
adopt: "[JBS-8350118] Simplify the layout access VarHandle"
1 parent 999afa5 commit 5a49039

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

substratevm/src/com.oracle.svm.hosted.foreign/src/com/oracle/svm/hosted/foreign/ForeignFunctionsFeature.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,13 +422,44 @@ private static <S, T, U extends ResolvedJavaMethod> Map<S, U> createStubs(
422422
return created;
423423
}
424424

425+
private static final String JLI_PREFIX = "java.lang.invoke.";
426+
427+
/**
428+
* List of (generated) classes that provide accessor methods for memory segments. Those methods
429+
* are referenced with {@code java.lang.invoke.SegmentVarHandle}. Unfortunately, the classes
430+
* containing the methods are not subclasses of {@link java.lang.invoke.VarHandle} and so the
431+
* automatic registration for reflective access (see
432+
* {@link com.oracle.svm.hosted.methodhandles.MethodHandleFeature#beforeAnalysis}) does not
433+
* trigger.
434+
*/
435+
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-25+13/src/java.base/share/classes/java/lang/invoke/VarHandles.java#L313-L344") //
436+
private static final List<String> VAR_HANDLE_SEGMENT_ACCESSORS = List.of(
437+
"VarHandleSegmentAsBooleans",
438+
"VarHandleSegmentAsBytes",
439+
"VarHandleSegmentAsShorts",
440+
"VarHandleSegmentAsChars",
441+
"VarHandleSegmentAsInts",
442+
"VarHandleSegmentAsLongs",
443+
"VarHandleSegmentAsFloats",
444+
"VarHandleSegmentAsDoubles");
445+
446+
private static void registerVarHandleMethodsForReflection(FeatureAccess access, Class<?> subtype) {
447+
assert subtype.getPackage().getName().equals(JLI_PREFIX.substring(0, JLI_PREFIX.length() - 1));
448+
RuntimeReflection.register(subtype.getDeclaredMethods());
449+
}
450+
425451
@Override
426452
public void beforeAnalysis(BeforeAnalysisAccess a) {
427453
var access = (FeatureImpl.BeforeAnalysisAccessImpl) a;
428454
sealed = true;
429455

430456
AbiUtils.singleton().checkLibrarySupport();
431457

458+
for (String simpleName : VAR_HANDLE_SEGMENT_ACCESSORS) {
459+
Class<?> varHandleSegmentAsXClass = ReflectionUtil.lookupClass(JLI_PREFIX + simpleName);
460+
access.registerSubtypeReachabilityHandler(ForeignFunctionsFeature::registerVarHandleMethodsForReflection, varHandleSegmentAsXClass);
461+
}
462+
432463
/*
433464
* Specializing an adapter would define a new class at runtime, which is not allowed in
434465
* SubstrateVM

0 commit comments

Comments
 (0)