Skip to content

Commit

Permalink
Use JNIRuntimeAccess API were appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
olpaw committed Aug 8, 2022
1 parent 415fddf commit 2406922
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
import org.graalvm.nativeimage.hosted.Feature.AfterAnalysisAccess;
import org.graalvm.nativeimage.hosted.Feature.DuringAnalysisAccess;
import org.graalvm.nativeimage.hosted.Feature.FeatureAccess;
import org.graalvm.nativeimage.hosted.RuntimeJNIAccess;
import org.graalvm.nativeimage.impl.RuntimeClassInitializationSupport;

import com.oracle.svm.core.jni.JNIRuntimeAccess;
import com.oracle.svm.core.util.ConcurrentIdentityHashMap;
import com.oracle.svm.core.util.VMError;
import com.oracle.svm.util.ReflectionUtil;
Expand Down Expand Up @@ -106,8 +106,8 @@ protected static Field[] fields(FeatureAccess access, String className, String..

protected static void registerForThrowNew(FeatureAccess access, String... exceptionClassNames) {
for (String exceptionClassName : exceptionClassNames) {
JNIRuntimeAccess.register(clazz(access, exceptionClassName));
JNIRuntimeAccess.register(constructor(access, exceptionClassName, String.class));
RuntimeJNIAccess.register(clazz(access, exceptionClassName));
RuntimeJNIAccess.register(constructor(access, exceptionClassName, String.class));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,9 @@
import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
import org.graalvm.nativeimage.hosted.RuntimeJNIAccess;
import org.graalvm.nativeimage.impl.ConfigurationCondition;
import org.graalvm.nativeimage.impl.ReflectionRegistry;

import com.oracle.svm.core.SubstrateOptions;
import com.oracle.svm.core.option.SubstrateOptionsParser;
import com.oracle.svm.core.util.UserError;
import org.graalvm.nativeimage.impl.RuntimeJNIAccessSupport;

/**
* Support for registering classes, methods and fields before and during the analysis so they are
Expand All @@ -47,28 +44,18 @@ private JNIRuntimeAccess() {
}

public static void register(Class<?>... classes) {
getSupport().register(ConfigurationCondition.alwaysTrue(), classes);
RuntimeJNIAccess.register(classes);
}

public static void register(Executable... methods) {
getSupport().register(ConfigurationCondition.alwaysTrue(), false, methods);
RuntimeJNIAccess.register(methods);
}

public static void register(Field... fields) {
register(false, fields);
RuntimeJNIAccess.register(fields);
}

public static void register(boolean finalIsWritable, Field... fields) {
getSupport().register(ConfigurationCondition.alwaysTrue(), finalIsWritable, fields);
}

private static JNIRuntimeAccessibilitySupport getSupport() {
if (!ImageSingletons.contains(JNIRuntimeAccessibilitySupport.class)) {
throw UserError.abort("Support for JNI is not enabled. The option %s must be set.", SubstrateOptionsParser.commandArgument(SubstrateOptions.JNI, "+"));
}
return ImageSingletons.lookup(JNIRuntimeAccessibilitySupport.class);
}

public interface JNIRuntimeAccessibilitySupport extends ReflectionRegistry {
ImageSingletons.lookup(RuntimeJNIAccessSupport.class).register(ConfigurationCondition.alwaysTrue(), finalIsWritable, fields);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@
import org.graalvm.nativeimage.StackValue;
import org.graalvm.nativeimage.VMRuntime;
import org.graalvm.nativeimage.hosted.Feature;
import org.graalvm.nativeimage.hosted.RuntimeJNIAccess;
import org.graalvm.nativeimage.hosted.RuntimeReflection;
import org.graalvm.nativeimage.impl.ConfigurationCondition;
import org.graalvm.word.LocationIdentity;
import org.graalvm.word.Pointer;
import org.graalvm.word.WordFactory;
Expand All @@ -126,7 +126,6 @@
import com.oracle.svm.core.graal.snippets.NodeLoweringProvider;
import com.oracle.svm.core.heap.GCCause;
import com.oracle.svm.core.heap.Heap;
import com.oracle.svm.core.jni.JNIRuntimeAccess;
import com.oracle.svm.core.log.FunctionPointerLogHandler;
import com.oracle.svm.core.option.HostedOptionKey;
import com.oracle.svm.core.option.RuntimeOptionKey;
Expand Down Expand Up @@ -216,10 +215,9 @@ public void afterRegistration(AfterRegistrationAccess access) {

@Override
public void duringSetup(DuringSetupAccess access) {
JNIRuntimeAccess.JNIRuntimeAccessibilitySupport registry = ImageSingletons.lookup(JNIRuntimeAccess.JNIRuntimeAccessibilitySupport.class);
ImageClassLoader imageClassLoader = ((DuringSetupAccessImpl) access).getImageClassLoader();
registerJNIConfiguration(registry, imageClassLoader);

registerJNIConfiguration(imageClassLoader);
EconomicMap<String, OptionDescriptor> descriptors = EconomicMap.create();
for (Class<? extends OptionDescriptors> optionsClass : imageClassLoader.findSubclasses(OptionDescriptors.class, false)) {
if (!Modifier.isAbstract(optionsClass.getModifiers()) && !OptionDescriptorsMap.class.isAssignableFrom(optionsClass)) {
Expand Down Expand Up @@ -349,10 +347,9 @@ UserException error(String format, Object... args) {
}
}

private static void registerJNIConfiguration(JNIRuntimeAccess.JNIRuntimeAccessibilitySupport registry, ImageClassLoader loader) {
private static void registerJNIConfiguration(ImageClassLoader loader) {
try (JNIConfigSource source = new JNIConfigSource(loader)) {
Map<String, Class<?>> classes = new HashMap<>();
ConfigurationCondition condition = ConfigurationCondition.alwaysTrue();
for (String line : source.lines) {
source.lineNo++;
String[] tokens = line.split(" ");
Expand All @@ -361,8 +358,8 @@ private static void registerJNIConfiguration(JNIRuntimeAccess.JNIRuntimeAccessib
Class<?> clazz = classes.get(className);
if (clazz == null) {
clazz = source.findClass(className);
registry.register(condition, clazz);
registry.register(condition, Array.newInstance(clazz, 0).getClass());
RuntimeJNIAccess.register(clazz);
RuntimeJNIAccess.register(Array.newInstance(clazz, 0).getClass());
classes.put(className, clazz);
}

Expand All @@ -371,7 +368,7 @@ private static void registerJNIConfiguration(JNIRuntimeAccess.JNIRuntimeAccessib
source.check(tokens.length == 4, "Expected 4 tokens for a field");
String fieldName = tokens[2];
try {
registry.register(condition, false, clazz.getDeclaredField(fieldName));
RuntimeJNIAccess.register(clazz.getDeclaredField(fieldName));
} catch (NoSuchFieldException e) {
throw source.error("Field %s.%s not found", clazz.getTypeName(), fieldName);
} catch (NoClassDefFoundError e) {
Expand All @@ -390,15 +387,15 @@ private static void registerJNIConfiguration(JNIRuntimeAccess.JNIRuntimeAccessib
try {
if ("<init>".equals(methodName)) {
Constructor<?> cons = clazz.getDeclaredConstructor(parameters);
registry.register(condition, false, cons);
RuntimeJNIAccess.register(cons);
if (Throwable.class.isAssignableFrom(clazz) && !Modifier.isAbstract(clazz.getModifiers())) {
if (usedInTranslatedException(parameters)) {
RuntimeReflection.register(clazz);
RuntimeReflection.register(cons);
}
}
} else {
registry.register(condition, false, clazz.getDeclaredMethod(methodName, parameters));
RuntimeJNIAccess.register(clazz.getDeclaredMethod(methodName, parameters));
}
} catch (NoSuchMethodException e) {
throw source.error("Method %s.%s%s not found: %s", clazz.getTypeName(), methodName, descriptor, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
import org.graalvm.compiler.serviceprovider.JavaVersionUtil;
import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.hosted.Feature;
import org.graalvm.nativeimage.hosted.RuntimeJNIAccess;
import org.graalvm.nativeimage.hosted.RuntimeReflection;
import org.graalvm.nativeimage.impl.RuntimeClassInitializationSupport;

Expand All @@ -100,7 +101,6 @@
import com.oracle.svm.core.jdk.NativeLibrarySupport;
import com.oracle.svm.core.jdk.PlatformNativeLibrarySupport;
import com.oracle.svm.core.jdk.SecurityProvidersFilter;
import com.oracle.svm.core.jni.JNIRuntimeAccess;
import com.oracle.svm.core.option.HostedOptionKey;
import com.oracle.svm.core.option.LocatableMultiOptionValue;
import com.oracle.svm.core.util.UserError;
Expand Down Expand Up @@ -474,26 +474,26 @@ private static void registerSunMSCAPIConfig(BeforeAnalysisAccess a) {
}

private static void registerLoadKeysOrCertificateChains(DuringAnalysisAccess a) {
JNIRuntimeAccess.register(constructor(a, "java.util.ArrayList"));
JNIRuntimeAccess.register(method(a, "sun.security.mscapi.CKeyStore", "generateCertificate", byte[].class, Collection.class));
JNIRuntimeAccess.register(method(a, "sun.security.mscapi.CKeyStore", "generateCertificateChain", String.class, Collection.class));
JNIRuntimeAccess.register(method(a, "sun.security.mscapi.CKeyStore", "generateKeyAndCertificateChain", boolean.class, String.class, long.class, long.class, int.class, Collection.class));
RuntimeJNIAccess.register(constructor(a, "java.util.ArrayList"));
RuntimeJNIAccess.register(method(a, "sun.security.mscapi.CKeyStore", "generateCertificate", byte[].class, Collection.class));
RuntimeJNIAccess.register(method(a, "sun.security.mscapi.CKeyStore", "generateCertificateChain", String.class, Collection.class));
RuntimeJNIAccess.register(method(a, "sun.security.mscapi.CKeyStore", "generateKeyAndCertificateChain", boolean.class, String.class, long.class, long.class, int.class, Collection.class));
}

private static void registerGenerateCKeyPair(DuringAnalysisAccess a) {
JNIRuntimeAccess.register(constructor(a, "sun.security.mscapi.CKeyPair", String.class, long.class, long.class, int.class));
RuntimeJNIAccess.register(constructor(a, "sun.security.mscapi.CKeyPair", String.class, long.class, long.class, int.class));
}

private static void registerCPrivateKeyOf(DuringAnalysisAccess a) {
JNIRuntimeAccess.register(method(a, "sun.security.mscapi.CPrivateKey", "of", String.class, long.class, long.class, int.class));
RuntimeJNIAccess.register(method(a, "sun.security.mscapi.CPrivateKey", "of", String.class, long.class, long.class, int.class));
}

private static void registerCPublicKeyOf(DuringAnalysisAccess a) {
JNIRuntimeAccess.register(method(a, "sun.security.mscapi.CPublicKey", "of", String.class, long.class, long.class, int.class));
RuntimeJNIAccess.register(method(a, "sun.security.mscapi.CPublicKey", "of", String.class, long.class, long.class, int.class));
}

private static void linkJaas(DuringAnalysisAccess a) {
JNIRuntimeAccess.register(fields(a, "com.sun.security.auth.module.UnixSystem", "username", "uid", "gid", "groups"));
RuntimeJNIAccess.register(fields(a, "com.sun.security.auth.module.UnixSystem", "username", "uid", "gid", "groups"));

NativeLibraries nativeLibraries = ((DuringAnalysisAccessImpl) a).getNativeLibraries();
/* We can statically link jaas, thus we classify it as builtIn library */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
import org.graalvm.nativeimage.hosted.Feature;
import org.graalvm.nativeimage.hosted.RuntimeJNIAccess;
import org.graalvm.nativeimage.hosted.RuntimeResourceAccess;
import org.graalvm.nativeimage.impl.InternalPlatform;

Expand All @@ -37,7 +38,6 @@
import com.oracle.svm.core.jdk.JNIRegistrationUtil;
import com.oracle.svm.core.jdk.NativeLibrarySupport;
import com.oracle.svm.core.jdk.PlatformNativeLibrarySupport;
import com.oracle.svm.core.jni.JNIRuntimeAccess;
import com.oracle.svm.hosted.FeatureImpl;
import com.oracle.svm.hosted.c.NativeLibraries;

Expand Down Expand Up @@ -97,11 +97,11 @@ public void beforeAnalysis(BeforeAnalysisAccess access) {

private static void handlePreferencesClassReachable(DuringAnalysisAccess access) {

JNIRuntimeAccess.register(method(access, "java.lang.System", "setProperty", String.class, String.class));
JNIRuntimeAccess.register(method(access, "java.lang.System", "loadLibrary", String.class));
RuntimeJNIAccess.register(method(access, "java.lang.System", "setProperty", String.class, String.class));
RuntimeJNIAccess.register(method(access, "java.lang.System", "loadLibrary", String.class));

JNIRuntimeAccess.register(java.awt.GraphicsEnvironment.class);
JNIRuntimeAccess.register(method(access, "java.awt.GraphicsEnvironment", "isHeadless"));
RuntimeJNIAccess.register(GraphicsEnvironment.class);
RuntimeJNIAccess.register(method(access, "java.awt.GraphicsEnvironment", "isHeadless"));

NativeLibraries nativeLibraries = getNativeLibraries(access);

Expand Down Expand Up @@ -163,13 +163,13 @@ private static void registerFreeType(DuringAnalysisAccess access) {
nativeLibraries.addStaticJniLibrary("fontmanager", isHeadless() ? "awt_headless" : "awt_xawt");
nativeLibraries.addDynamicNonJniLibrary("freetype");

JNIRuntimeAccess.register(clazz(access, "sun.font.FontConfigManager$FontConfigInfo"));
JNIRuntimeAccess.register(fields(access, "sun.font.FontConfigManager$FontConfigInfo", "fcVersion", "cacheDirs"));
JNIRuntimeAccess.register(clazz(access, "sun.font.FontConfigManager$FcCompFont"));
JNIRuntimeAccess.register(fields(access, "sun.font.FontConfigManager$FcCompFont", "fcName", "firstFont", "allFonts"));
JNIRuntimeAccess.register(clazz(access, "sun.font.FontConfigManager$FontConfigFont"));
JNIRuntimeAccess.register(constructor(access, "sun.font.FontConfigManager$FontConfigFont"));
JNIRuntimeAccess.register(fields(access, "sun.font.FontConfigManager$FontConfigFont", "familyName", "styleStr", "fullName", "fontFile"));
RuntimeJNIAccess.register(clazz(access, "sun.font.FontConfigManager$FontConfigInfo"));
RuntimeJNIAccess.register(fields(access, "sun.font.FontConfigManager$FontConfigInfo", "fcVersion", "cacheDirs"));
RuntimeJNIAccess.register(clazz(access, "sun.font.FontConfigManager$FcCompFont"));
RuntimeJNIAccess.register(fields(access, "sun.font.FontConfigManager$FcCompFont", "fcName", "firstFont", "allFonts"));
RuntimeJNIAccess.register(clazz(access, "sun.font.FontConfigManager$FontConfigFont"));
RuntimeJNIAccess.register(constructor(access, "sun.font.FontConfigManager$FontConfigFont"));
RuntimeJNIAccess.register(fields(access, "sun.font.FontConfigManager$FontConfigFont", "familyName", "styleStr", "fullName", "fontFile"));
}

private static void registerColorProfiles(DuringAnalysisAccess duringAnalysisAccess) {
Expand Down
Loading

0 comments on commit 2406922

Please sign in to comment.