Skip to content

Commit

Permalink
Create fallback image on calls to System.loadLibrary without JNI config.
Browse files Browse the repository at this point in the history
  • Loading branch information
cstancu committed Jun 25, 2019
1 parent e48cc9c commit a0ebaa3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public class FallbackFeature implements Feature {

private final List<String> reflectionCalls = new ArrayList<>();
private final List<String> resourceCalls = new ArrayList<>();
private final List<String> jniCalls = new ArrayList<>();
private final List<String> proxyCalls = new ArrayList<>();

private static class AutoProxyInvoke {
Expand Down Expand Up @@ -170,6 +171,8 @@ public FallbackFeature() {

addCheck(Proxy.class.getMethod("getProxyClass", ClassLoader.class, Class[].class), this::collectProxyInvokes);
addCheck(Proxy.class.getMethod("newProxyInstance", ClassLoader.class, Class[].class, InvocationHandler.class), this::collectProxyInvokes);

addCheck(System.class.getMethod("loadLibrary", String.class), this::collectJNIInvokes);
} catch (NoSuchMethodException e) {
throw VMError.shouldNotReachHere("Registering ReflectionInvocationChecks failed", e);
}
Expand All @@ -183,6 +186,10 @@ private void collectResourceInvokes(ReflectionInvocationCheck check, InvokeTypeF
resourceCalls.add("Resource access method " + check.locationString(invoke));
}

private void collectJNIInvokes(ReflectionInvocationCheck check, InvokeTypeFlow invoke) {
jniCalls.add("System method " + check.locationString(invoke));
}

private void collectProxyInvokes(ReflectionInvocationCheck check, InvokeTypeFlow invoke) {
if (!containsAutoProxyInvoke(getCallerMethod(invoke), invoke.getLocation().getBci())) {
proxyCalls.add("Dynamic proxy method " + check.locationString(invoke));
Expand Down Expand Up @@ -252,6 +259,7 @@ public void beforeAnalysis(BeforeAnalysisAccess a) {

public FallbackImageRequest reflectionFallback = null;
public FallbackImageRequest resourceFallback = null;
public FallbackImageRequest jniFallback = null;
public FallbackImageRequest proxyFallback = null;

@Override
Expand Down Expand Up @@ -287,6 +295,10 @@ public void afterAnalysis(AfterAnalysisAccess a) {
resourceCalls.add(ABORT_MSG_PREFIX + " due to accessing resources without configuration.");
resourceFallback = new FallbackImageRequest(resourceCalls);
}
if (!jniCalls.isEmpty()) {
jniCalls.add(ABORT_MSG_PREFIX + " due to loading native libraries without configuration.");
jniFallback = new FallbackImageRequest(jniCalls);
}
if (!proxyCalls.isEmpty()) {
proxyCalls.add(ABORT_MSG_PREFIX + " due to dynamic proxy use without configuration.");
proxyFallback = new FallbackImageRequest(proxyCalls);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import java.util.Collections;
import java.util.List;

import com.oracle.svm.hosted.FallbackFeature;
import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.hosted.Feature;

import com.oracle.svm.core.SubstrateOptions;
Expand All @@ -48,4 +50,17 @@ public boolean isInConfiguration(IsInConfigurationAccess access) {
public List<Class<? extends Feature>> getRequiredFeatures() {
return Collections.singletonList(JNIFeature.class);
}

@Override
public void beforeCompilation(BeforeCompilationAccess access) {
if (!ImageSingletons.contains(FallbackFeature.class)) {
return;
}
FallbackFeature.FallbackImageRequest jniFallback = ImageSingletons.lookup(FallbackFeature.class).jniFallback;
if (jniFallback != null && ConfigurationFiles.Options.JNIConfigurationFiles.getValue() == null &&
ConfigurationFiles.Options.JNIConfigurationResources.getValue() == null) {
throw jniFallback;
}
}

}

0 comments on commit a0ebaa3

Please sign in to comment.