Skip to content

[GR-64941] Consolidate Native Image JNI classes after JDK 21 drop #11298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import com.oracle.svm.core.jni.headers.JNIInvokeInterface;
import com.oracle.svm.core.jni.headers.JNIJavaVM;
import com.oracle.svm.core.jni.headers.JNINativeInterface;
import com.oracle.svm.core.jni.headers.JNINativeInterfaceJDKLatest;
import com.oracle.svm.core.util.VMError;

import jdk.graal.compiler.word.Word;
Expand Down Expand Up @@ -74,7 +73,7 @@ public static JNIFunctionTables singleton() {
private final CIsolateData<JNIJavaVM> jniJavaVM = CIsolateDataFactory.createStruct("jniJavaVM", JNIJavaVM.class);

private static int getFunctionTableSize() {
return SizeOf.get(JNINativeInterfaceJDKLatest.class);
return SizeOf.get(JNINativeInterface.class);
}

@Platforms(Platform.HOSTED_ONLY.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
import com.oracle.svm.core.jni.headers.JNIObjectHandle;
import com.oracle.svm.core.jni.headers.JNIObjectRefType;
import com.oracle.svm.core.jni.headers.JNIValue;
import com.oracle.svm.core.jni.headers.JNIVersionJDKLatest;
import com.oracle.svm.core.jni.headers.JNIVersion;
import com.oracle.svm.core.log.Log;
import com.oracle.svm.core.monitor.MonitorInflationCause;
import com.oracle.svm.core.monitor.MonitorSupport;
Expand Down Expand Up @@ -168,7 +168,7 @@ public final class JNIFunctions {
@CEntryPointOptions(prologue = CEntryPointOptions.NoPrologue.class, epilogue = CEntryPointOptions.NoEpilogue.class)
@Uninterruptible(reason = "No need to enter the isolate and also no way to report errors if unable to.")
static int GetVersion(JNIEnvironment env) {
return JNIVersionJDKLatest.JNI_VERSION_LATEST();
return JNIVersion.JNI_VERSION_LATEST();
}

/*
Expand Down Expand Up @@ -1623,6 +1623,17 @@ static void SetStaticDoubleField(JNIEnvironment env, JNIObjectHandle clazz, JNIF
U.putDouble(JNIAccessibleField.getStaticPrimitiveFieldsAtRuntime(fieldId), offset, value);
}

/*
* jlong GetStringUTFLengthAsLong(JNIEnv *env, jstring string);
*/

@CEntryPoint(exceptionHandler = JNIExceptionHandlerReturnMinusOne.class, include = CEntryPoint.NotIncludedAutomatically.class, publishAs = Publish.NotPublished)
@CEntryPointOptions(prologue = JNIEnvEnterPrologue.class, prologueBailout = ReturnMinusOneLong.class)
static long GetStringUTFLengthAsLong(JNIEnvironment env, JNIObjectHandle hstr) {
String str = JNIObjectHandles.getObject(hstr);
return Utf8.utf8LengthAsLong(str);
}

// Checkstyle: resume

/**
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -1479,4 +1479,12 @@ public interface JNINativeInterface extends PointerBase {

@CField
void setIsVirtualThread(CFunctionPointer p);

// JNI_VERSION_24

@CField
CFunctionPointer getGetStringUTFLengthAsLong();

@CField
void setGetStringUTFLengthAsLong(CFunctionPointer p);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@
import org.graalvm.nativeimage.c.constant.CConstant;

import com.oracle.svm.core.Uninterruptible;
import com.oracle.svm.core.util.BasedOnJDKFile;

@CContext(JNIHeaderDirectives.class)
public final class JNIVersion {
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public static boolean isSupported(int version, boolean builtInLibrary) {
if (version == JNIVersionJDKLatest.JNI_VERSION_LATEST() || version == JNI_VERSION_21() || version == JNI_VERSION_20() || version == JNI_VERSION_19() || version == JNI_VERSION_10() ||
version == JNI_VERSION_9() || version == JNI_VERSION_1_8()) {
if (version == JNI_VERSION_LATEST() || version == JNI_VERSION_24() || version == JNI_VERSION_21() || version == JNI_VERSION_20() || version == JNI_VERSION_19() ||
version == JNI_VERSION_10() || version == JNI_VERSION_9() || version == JNI_VERSION_1_8()) {
return true;
}
if (builtInLibrary) {
Expand Down Expand Up @@ -76,6 +77,17 @@ public static boolean isSupported(int version, boolean builtInLibrary) {
@CConstant
public static native int JNI_VERSION_21();

@CConstant
public static native int JNI_VERSION_24();

/*
* GR-50948: there is not yet a JNI_VERSION_XX constant defined for JDK latest. As soon as it
* gets available, the "value" property of the CConstant annotation below must be removed.
*/
@CConstant(value = "JNI_VERSION_24")
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+16/src/java.base/share/native/include/jni.h#L1994-L2006")
public static native int JNI_VERSION_LATEST();

// Checkstyle: resume

private JNIVersion() {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@
import com.oracle.svm.core.jni.functions.JNIFunctions;
import com.oracle.svm.core.jni.functions.JNIFunctions.UnimplementedWithJNIEnvArgument;
import com.oracle.svm.core.jni.functions.JNIFunctions.UnimplementedWithJavaVMArgument;
import com.oracle.svm.core.jni.functions.JNIFunctionsJDKLatest;
import com.oracle.svm.core.jni.functions.JNIInvocationInterface;
import com.oracle.svm.core.jni.headers.JNIInvokeInterface;
import com.oracle.svm.core.jni.headers.JNINativeInterface;
import com.oracle.svm.core.jni.headers.JNINativeInterfaceJDKLatest;
import com.oracle.svm.core.meta.MethodPointer;
import com.oracle.svm.core.util.VMError;
import com.oracle.svm.hosted.FeatureImpl.BeforeAnalysisAccessImpl;
Expand Down Expand Up @@ -83,7 +81,6 @@ public class JNIFunctionTablesFeature implements Feature {
* for the Interface Function Table</a>
*/
private StructInfo functionTableMetadata;
private StructInfo functionTableMetadataJDKLatest;

/**
* Metadata about the table pointed to by the {@code JavaVM*} C pointer.
Expand Down Expand Up @@ -111,14 +108,12 @@ public void beforeAnalysis(BeforeAnalysisAccess arg) {
invokeInterfaceMetadata = (StructInfo) nativeLibraries.findElementInfo(invokeInterface);
AnalysisType functionTable = metaAccess.lookupJavaType(JNINativeInterface.class);
functionTableMetadata = (StructInfo) nativeLibraries.findElementInfo(functionTable);
functionTableMetadataJDKLatest = (StructInfo) nativeLibraries.findElementInfo(metaAccess.lookupJavaType(JNINativeInterfaceJDKLatest.class));

// Manually add functions as entry points so this is only done when JNI features are enabled
AnalysisType invokes = metaAccess.lookupJavaType(JNIInvocationInterface.class);
AnalysisType exports = metaAccess.lookupJavaType(JNIInvocationInterface.Exports.class);
AnalysisType functions = metaAccess.lookupJavaType(JNIFunctions.class);
AnalysisType functionsJDKLatest = metaAccess.lookupJavaType(JNIFunctionsJDKLatest.class);
Stream<AnalysisMethod> analysisMethods = Stream.of(invokes, functions, functionsJDKLatest, exports).flatMap(type -> Stream.of(type.getDeclaredMethods(false)));
Stream<AnalysisMethod> analysisMethods = Stream.of(invokes, functions, exports).flatMap(type -> Stream.of(type.getDeclaredMethods(false)));
Stream<AnalysisMethod> unimplementedMethods = Stream.of((AnalysisMethod) getSingleMethod(metaAccess, UnimplementedWithJNIEnvArgument.class),
(AnalysisMethod) getSingleMethod(metaAccess, UnimplementedWithJavaVMArgument.class));
Stream.concat(analysisMethods, unimplementedMethods).forEach(method -> {
Expand Down Expand Up @@ -184,12 +179,6 @@ private void fillJNIFunctionsTable(CompilationAccessImpl access) {
int offset = field.getOffsetInfo().getProperty();
tables.initFunctionEntry(offset, getStubFunctionPointer(access, method));
}
HostedType functionsJDKLatest = access.getMetaAccess().lookupJavaType(JNIFunctionsJDKLatest.class);
for (HostedMethod method : functionsJDKLatest.getDeclaredMethods(false)) {
StructFieldInfo field = findFieldFor(functionTableMetadataJDKLatest, method.getName());
int offset = field.getOffsetInfo().getProperty();
tables.initFunctionEntry(offset, getStubFunctionPointer(access, method));
}
for (CallVariant variant : CallVariant.values()) {
CFunctionPointer trampoline = prepareCallTrampoline(access, variant, false);
String suffix = (variant == CallVariant.ARRAY) ? "A" : ((variant == CallVariant.VA_LIST) ? "V" : "");
Expand Down