Skip to content

[GR-60238] Include JNI reachability metadata with reflection #11066

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
18 changes: 12 additions & 6 deletions docs/reference-manual/native-image/ReachabilityMetadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@ The _reachability-metadata.json_ configuration contains a single object with one
{
"reflection":[],
"resources":[],
"bundles":[],
"jni":[]
"bundles":[]
}
```

Expand Down Expand Up @@ -376,12 +375,14 @@ jclass clazz = FindClass(env, "jni/accessed/Type");
```
looks up the `jni.accessed.Type` class, which can then be used to instantiate `jni.accessed.Type`, invoke its methods or access its fields.

The metadata entry for the above call can *only* be provided via _reachability-metadata.json_. Specify the `type` entry in the `jni` field:
The metadata entry for the above call can *only* be provided via _reachability-metadata.json_. Specify
the `jniAccessible` field in the `type` entry in the `reflection` section:
```json
{
"jni":[
"reflection": [
{
"type": "jni.accessed.Type"
"type": "jni.accessed.Type",
"jniAccessibleType": true
}
]
}
Expand All @@ -393,13 +394,15 @@ To access field values, we need to provide field names:
```json
{
"type": "jni.accessed.Type",
"jniAccessible": true,
"fields": [{"name": "value"}]
}
```
To access all fields one can use the following attributes:
```json
{
"type": "jni.accessed.Type",
"jniAccessible": true,
"allDeclaredFields": true,
"allPublicFields": true
}
Expand All @@ -410,6 +413,7 @@ To call Java methods from JNI, we must provide metadata for the method signature
```json
{
"type": "jni.accessed.Type",
"jniAccessible": true,
"methods": [
{"name": "<methodName1>", "parameterTypes": ["<param-type1>", "<param-typeI>", "<param-typeN>"]},
{"name": "<methodName2>", "parameterTypes": ["<param-type1>", "<param-typeI>", "<param-typeN>"]}
Expand All @@ -420,6 +424,7 @@ As a convenience, one can allow method invocation for groups of methods by addin
```json
{
"type": "jni.accessed.Type",
"jniAccessible": true,
"allDeclaredConstructors": true,
"allPublicConstructors": true,
"allDeclaredMethods": true,
Expand All @@ -429,7 +434,8 @@ As a convenience, one can allow method invocation for groups of methods by addin
`allDeclaredConstructors` and `allDeclaredMethods` allow calls invocations of methods declared on a given type.
`allPublicConstructors` and `allPublicMethods` allow invocations of all public methods defined on a type and all of its supertypes.

To allocate objects of a type with `AllocObject`, the metadata must be stored in the `reflection` section:
To allocate objects of a type with `AllocObject`, the `unsafeAllocated` field must be set, but the `jniAccessible` field
is not required:
```json
{
"reflection": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@
"title": "Allow objects of this class to be serialized and deserialized",
"type": "boolean",
"default": false
},
"jniAccessible": {
"title": "Register the type, including all registered fields and methods, for runtime JNI access",
"type": "boolean",
"default": false
}
},
"additionalProperties": false
Expand Down

This file was deleted.

1 change: 1 addition & 0 deletions substratevm/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ This changelog summarizes major changes to GraalVM Native Image.
1. `run-time-initialized-jdk` shifts away from build-time initialization of the JDK, instead initializing most of it at run time. This transition is gradual, with individual components of the JDK becoming run-time initialized in each release. This process should complete with JDK 29 when this option should not be needed anymore. Unless you store classes from the JDK in the image heap, this option should not affect you. In case this option breaks your build, follow the suggestions in the error messages.
* (GR-63494) Recurring callback support is no longer enabled by default. If this feature is needed, please specify `-H:+SupportRecurringCallback` at image build-time.
* (GR-60209) New syntax for configuration of the [Foreign Function & Memory API](https://github.com/oracle/graal/blob/master/docs/reference-manual/native-image/ForeignInterface.md)
* (GR-60238) JNI registration is now included as part of the `"reflection"` section of _reachability-metadata.json_ using the `"jniAccessible"` attribute. Registrations performed through the `"jni"` section of _reachability-metadata.json_ and through _jni-config.json_ will still be accepted and parsed correctly.

## GraalVM for JDK 24 (Internal Version 24.2.0)
* (GR-59717) Added `DuringSetupAccess.registerObjectReachabilityHandler` to allow registering a callback that is executed when an object of a specified type is marked as reachable during heap scanning.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@
import java.util.function.Function;
import java.util.function.Predicate;

import org.graalvm.nativeimage.impl.UnresolvedConfigurationCondition;
import org.junit.Assert;
import org.junit.Test;

import com.oracle.svm.configure.ConfigurationTypeDescriptor;
import com.oracle.svm.configure.NamedConfigurationTypeDescriptor;
import com.oracle.svm.configure.UnresolvedConfigurationCondition;
import com.oracle.svm.configure.config.ConfigurationFileCollection;
import com.oracle.svm.configure.config.ConfigurationMemberInfo;
import com.oracle.svm.configure.config.ConfigurationMemberInfo.ConfigurationMemberAccessibility;
Expand Down Expand Up @@ -97,7 +97,6 @@ public void testSameConfig() {
ConfigurationSet config = loadTraceProcessorFromResourceDirectory(PREVIOUS_CONFIG_DIR_NAME, omittedConfig);
config = config.copyAndSubtract(omittedConfig);

assertTrue(config.getJniConfiguration().isEmpty());
assertTrue(config.getReflectionConfiguration().isEmpty());
assertTrue(config.getProxyConfiguration().isEmpty());
assertTrue(config.getResourceConfiguration().isEmpty());
Expand All @@ -112,7 +111,7 @@ public void testConfigDifference() {
config = config.copyAndSubtract(omittedConfig);

doTestGeneratedTypeConfig();
doTestTypeConfig(config.getJniConfiguration());
doTestTypeConfig(config.getReflectionConfiguration());

doTestProxyConfig(config.getProxyConfiguration());

Expand Down Expand Up @@ -242,8 +241,8 @@ class TypeMethodsWithFlagsTest {
final Map<ConfigurationMethod, ConfigurationMemberDeclaration> methodsThatMustExist = new HashMap<>();
final Map<ConfigurationMethod, ConfigurationMemberDeclaration> methodsThatMustNotExist = new HashMap<>();

final TypeConfiguration previousConfig = new TypeConfiguration("");
final TypeConfiguration currentConfig = new TypeConfiguration("");
final TypeConfiguration previousConfig = new TypeConfiguration();
final TypeConfiguration currentConfig = new TypeConfiguration();

TypeMethodsWithFlagsTest(ConfigurationMemberDeclaration methodKind) {
this.methodKind = methodKind;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@
import java.util.Locale;

import org.graalvm.nativeimage.impl.ConfigurationCondition;
import org.graalvm.nativeimage.impl.UnresolvedConfigurationCondition;
import org.junit.Assert;
import org.junit.Test;

import com.oracle.svm.configure.ConfigurationParserOption;
import com.oracle.svm.configure.ResourceConfigurationParser;
import com.oracle.svm.configure.ResourcesRegistry;
import com.oracle.svm.configure.UnresolvedConfigurationCondition;
import com.oracle.svm.configure.config.ResourceConfiguration;
import com.oracle.svm.configure.config.conditional.ConfigurationConditionResolver;

Expand Down
Loading