Skip to content

Commit

Permalink
Intrinsify reflective calls when the argument is a constant.
Browse files Browse the repository at this point in the history
  • Loading branch information
cstancu committed Jul 26, 2018
1 parent 1205804 commit 3c85875
Show file tree
Hide file tree
Showing 16 changed files with 605 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1929,9 +1929,15 @@ String error(String format, Object... a) {
}

boolean check(boolean pluginResult) {
if (pluginResult == true) {
if (pluginResult) {
/*
* If lastInstr is null, even if this method has a non-void return type, the method
* doesn't return a value, it probably throws an exception.
*/
int expectedStackSize = beforeStackSize + resultType.getSlotCount();
assert expectedStackSize == frameState.stackSize() : error("plugin manipulated the stack incorrectly: expected=%d, actual=%d", expectedStackSize, frameState.stackSize());
assert lastInstr == null || expectedStackSize == frameState.stackSize() : error("plugin manipulated the stack incorrectly: expected=%d, actual=%d", expectedStackSize,
frameState.stackSize());

NodeIterable<Node> newNodes = graph.getNewNodes(mark);
assert !needsNullCheck || isPointerNonNull(args[0].stamp(NodeView.DEFAULT)) : error("plugin needs to null check the receiver of %s: receiver=%s", targetMethod.format("%H.%n(%p)"),
args[0]);
Expand Down
2 changes: 1 addition & 1 deletion substratevm/LIMITATIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Reflection

What: Calling `Class.forName()`; listing methods and fields of a class; invoking methods and accessing fields reflectively; most classes in the package `java.lang.reflect`.

Individual classes, methods, and fields that should be accessible via reflection must be specified during native image generation in a configuration file via the option `-H:ReflectionConfigurationFiles=`, or by using [`RuntimeReflection`](http://www.graalvm.org/sdk/javadoc/org/graalvm/nativeimage/RuntimeReflection.html) from a [`Feature`](http://www.graalvm.org/sdk/javadoc/org/graalvm/nativeimage/Feature.html). Elements (classes, methods, and fields) that are not included in a configuration cannot be accessed reflectively. For more details, read our [documentation on reflection](REFLECTION.md).
Individual classes, methods, and fields that should be accessible via reflection need to be known ahead-of-time. SubstrateVM tries to resolve these elements through a static analysis that detects calls to the reflection API. Where the analysis fails the program elements reflectively accessed at run time must be specified during native image generation in a configuration file via the option `-H:ReflectionConfigurationFiles=`, or by using [`RuntimeReflection`](http://www.graalvm.org/sdk/javadoc/org/graalvm/nativeimage/RuntimeReflection.html) from a [`Feature`](http://www.graalvm.org/sdk/javadoc/org/graalvm/nativeimage/Feature.html). For more details, read our [documentation on reflection](REFLECTION.md).

During native image generation, reflection can be used without restrictions during native image generation, for example in static initializers.

Expand Down
59 changes: 49 additions & 10 deletions substratevm/REFLECTION.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,54 @@
# Reflection on Substrate VM

Java reflection support (`java.lang.reflect`) enables Java code to examine its own classes, methods and fields and their properties at runtime. One useful application of this feature is the serialization of arbitrary data structures. Substrate VM has partial support for reflection and requires a configuration with those program elements that should be examinable at image runtime.
Java reflection support (the `java.lang.reflect.*` API) enables Java code to examine its own classes, methods and fields and their properties at runtime.

## Configuration
Substrate VM has partial support for reflection and it needs to know ahead of time the reflectively accessed program elements. Examining and accessing program elements through `java.lang.reflect.*` or loading classes with `Class.forName(String)` at run time requires preparing additional metadata for those program elements. (Note: We include here loading classes with `Class.forName(String)` since it is closely related to reflection.)

Examining and accessing program elements through `java.lang.reflect` at runtime requires preparing additional metadata for those program elements. A configuration that specifies those program elements must be provided during the image build as follows:
SubstrateVM tries to resolve the target elements through a static analysis that detects calls to the reflection API. Where the analysis fails the program elements reflectively accessed at run time must be specified using a manual configuration.

## Automatic detection

The analysis intercepts calls to `Class.forName(String)`, `Class.forName(String, ClassLoader)`, `Class.getDeclaredField(String)`, `Class.getField(String)`, `Class.getDeclaredMethod(String, Class[])`, `Class.getMethod(String, Class[])`, `Class.getDeclaredConstructor(Class[])` and `Class.getConstructor(Class[])`. If the arguments to these calls can be reduced to a constant we try to resolve the target elements. If the target elements can be resolved the calls are removed and instead the target elements are embedded in the code. If the target elements cannot be resolved, e.g., a class is not on the classpath or it doesn't declare a field/method/constructor, then the calls are replaced with a snippet that throws the appropriate exception at run time. The benefits are two fold. First, at run time there are no calls to the reflection API. Second, Graal can employ constant folding and optimize the code further.

The calls are intercepted and processed only when it can be unequivocally determined that the parameters can be reduced to a constant. For example the call `Class.forName(String)` will be replaced with a `Class` literal only if the `String` argument can be constant folded, assuming that the class is actually on the classpath. Additionally a call to `Class.getMethod(String, Class[])` will be processed only if the contents of the `Class[]` argument can be determined with certainty. The last restriction is due to the fact that Java doesn't have immutable arrays. Therefore all the changes to the array between the time it is allocated and the time it is passed as an argument need to be tracked. The analysis follows a simple rule: if all the writes to the array happen in linear sections of code, i.e., no control flow splits, then the array is effectively constant for the purpose of analyzing the call. That is why the analysis doesn't accept `Class[]` arguments coming from static fields since the contents of those can change at any time, even if the fields are final. Although this may seem too restrictive it covers the most commonly used patterns of reflection API calls. The only exception to the constant arguments rule is that the `ClassLoader` argument of `Class.forName(String, ClassLoader)` doesn't need to be a constant; it is ignored and instead a class loader that can load all the classes on the class path is used. The analysis runs to a fix point which means that a chain of calls like `Class.forName(String).getMethod(String, Class[])` will first replace the class constant and then the method effectively reducing this to a `java.lang.reflect.Method`.

Following are examples of calls that can be intercepted and replaced with the corresponding element:

```
Class.forName("java.lang.Integer")
Class.forName("java.lang.Integer", true, ClassLoader.getSystemClassLoader())
Class.forName("java.lang.Integer").getMethod("equals", Object.class)
Integer.class.getDeclaredMethod("bitCount", int.class)
Integer.class.getConstructor(String.class)
Integer.class.getDeclaredConstructor(int.class)
Integer.class.getField("MAX_VALUE")
Integer.class.getDeclaredField("value")
```

The following ways to declare and populate an array are equivalent from the point of view of the analysis:

```
Class<?>[] params0 = new Class<?>[]{String.class, int.class};
Integer.class.getMethod("parseInt", params0);
```

```
Class<?>[] params1 = new Class<?>[2];
params1[0] = Class.forName("java.lang.String");
params1[1] = int.class;
Integer.class.getMethod("parseInt", params1);
```

```
Class<?>[] params2 = {String.class, int.class};
Integer.class.getMethod("parseInt", params2);
```

If a call cannot be processed it is simply skipped. For these situations a manual configuration as described below can be provided.

## Manual configuration

A configuration that specifies the program elements that will be accessed reflectively can be provided during the image build as follows:

-H:ReflectionConfigurationFiles=/path/to/reflectconfig

Expand All @@ -13,8 +57,8 @@ where `reflectconfig` is a JSON file in the following format (use `--expert-opti
[
{
"name" : "java.lang.Class",
"allDeclaredConstructors" : true,
"allPublicConstructors" : true
"allDeclaredConstructors" : true,
"allPublicConstructors" : true
"allDeclaredMethods" : true,
"allPublicMethods" : true
},
Expand Down Expand Up @@ -60,10 +104,5 @@ Alternatively, a custom `Feature` implementation can register program elements b
}


## Limitations at Runtime
Dynamic class loading using `Class.forName()` is not available due to the ahead-of-time image generation model of Substrate VM.

See also our [list of general limitations](#LIMITATIONS.md).

## Use during Native Image Generation
Reflection can be used without restrictions during native image generation, for example in static initializers. At this point, code can collect information about methods and fields and store them in own data structures, which are then reflection-free at run time.
Original file line number Diff line number Diff line change
Expand Up @@ -52,30 +52,31 @@ public interface GraalFeature extends Feature {
* @param providers Providers that the lowering can use.
* @param snippetReflection Snippet reflection providers.
* @param foreignCalls The foreign call registry to add to.
* @param hosted True if registering for ahead-of-time compilation, false if registering for
* @param hosted True if registering for ahead-of-time compilation, false otherwise
*/
default void registerForeignCalls(RuntimeConfiguration runtimeConfig, Providers providers, SnippetReflectionProvider snippetReflection,
Map<SubstrateForeignCallDescriptor, SubstrateForeignCallLinkage> foreignCalls, boolean hosted) {
}

/**
* Called to register Graal invocation plugins.
*
*
* @param providers Providers that the lowering can use.
* @param snippetReflection Snippet reflection providers.
* @param invocationPlugins The invocation plugins to add to.
* @param hosted True if registering for ahead-of-time compilation, false if registering for
* @param analysis true if registering for analysis, false if registering for compilation
* @param hosted True if registering for ahead-of-time compilation, false otherwise
*/
default void registerInvocationPlugins(Providers providers, SnippetReflectionProvider snippetReflection, InvocationPlugins invocationPlugins, boolean hosted) {
default void registerInvocationPlugins(Providers providers, SnippetReflectionProvider snippetReflection, InvocationPlugins invocationPlugins, boolean analysis, boolean hosted) {
}

/**
* Called to register Graal node plugins.
*
* @param metaAccess MetaAccessProvider that the node plugins can use.
* @param plugins The Plugins object where node plugins can be added to.
* @param analysis true if registering for analysis, false if registering for compilation
* @param hosted true if registering for ahead-of-time compilation, false if registering for
* @param analysis true if registering for analysis, false if registering for compilaiton
*/
default void registerNodePlugins(MetaAccessProvider metaAccess, Plugins plugins, boolean analysis, boolean hosted) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void registerLowerings(RuntimeConfiguration runtimeConfig, OptionValues o
}

@Override
public void registerInvocationPlugins(Providers providers, SnippetReflectionProvider snippetReflection, InvocationPlugins plugins, boolean hosted) {
public void registerInvocationPlugins(Providers providers, SnippetReflectionProvider snippetReflection, InvocationPlugins plugins, boolean analysis, boolean hosted) {
registerSystemPlugins(plugins);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2018, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.oracle.svm.core.reflect;

// Checkstyle: allow reflection

import java.lang.reflect.Executable;
import java.lang.reflect.Field;

public class ReflectionPluginExceptions {

public static Class<?> throwClassNotFoundException(String message) throws ClassNotFoundException {
throw new ClassNotFoundException(message);
}

public static Field throwNoSuchFieldException(String message) throws NoSuchFieldException {
throw new NoSuchFieldException(message);
}

public static Executable throwNoSuchMethodException(String message) throws NoSuchMethodException {
throw new NoSuchMethodException(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -515,9 +515,9 @@ private void doRun(Map<Method, CEntryPointData> entryPoints, Method mainEntryPoi
ImageSingletons.add(AnnotationSubstitutionProcessor.class, annotationSubstitutions);
annotationSubstitutions.init();

UnsafeAutomaticSubstitutionProcessor automaticSubstitutions = new UnsafeAutomaticSubstitutionProcessor(annotationSubstitutions);
UnsafeAutomaticSubstitutionProcessor automaticSubstitutions = new UnsafeAutomaticSubstitutionProcessor(annotationSubstitutions, originalSnippetReflection);
ImageSingletons.add(UnsafeAutomaticSubstitutionProcessor.class, automaticSubstitutions);
automaticSubstitutions.init(originalMetaAccess);
automaticSubstitutions.init(loader, originalMetaAccess);

CEnumCallWrapperSubstitutionProcessor cEnumProcessor = new CEnumCallWrapperSubstitutionProcessor();

Expand Down Expand Up @@ -1029,7 +1029,7 @@ public static void registerGraphBuilderPlugins(FeatureHandler featureHandler, Ru
SubstrateGraphBuilderPlugins.registerInvocationPlugins(pluginsMetaAccess, providers.getConstantReflection(), hostedSnippetReflection, plugins.getInvocationPlugins(),
replacementBytecodeProvider, analysis);

featureHandler.forEachGraalFeature(feature -> feature.registerInvocationPlugins(providers, hostedSnippetReflection, plugins.getInvocationPlugins(), hosted));
featureHandler.forEachGraalFeature(feature -> feature.registerInvocationPlugins(providers, hostedSnippetReflection, plugins.getInvocationPlugins(), analysis, hosted));

providers.setGraphBuilderPlugins(plugins);
replacements.setGraphBuilderPlugins(plugins);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void afterHeapLayout(AfterHeapLayoutAccess access) {
}

@Override
public void registerInvocationPlugins(Providers providers, SnippetReflectionProvider snippetReflection, InvocationPlugins invocationPlugins, boolean hosted) {
public void registerInvocationPlugins(Providers providers, SnippetReflectionProvider snippetReflection, InvocationPlugins invocationPlugins, boolean analysis, boolean hosted) {
Registration r = new Registration(invocationPlugins, CGlobalData.class);
r.register1("get", Receiver.class, new InvocationPlugin() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
@AutomaticFeature
public class CEntryPointSupport implements GraalFeature {
@Override
public void registerInvocationPlugins(Providers providers, SnippetReflectionProvider snippetReflection, InvocationPlugins invocationPlugins, boolean hosted) {
public void registerInvocationPlugins(Providers providers, SnippetReflectionProvider snippetReflection, InvocationPlugins invocationPlugins, boolean analysis, boolean hosted) {
registerEntryPointActionsPlugins(invocationPlugins);
registerEntryPointContextPlugins(invocationPlugins);
}
Expand Down
Loading

0 comments on commit 3c85875

Please sign in to comment.