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

Closed
wants to merge 3 commits 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
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 @@ -18,6 +18,7 @@ This changelog summarizes major changes to GraalVM Native Image.
* (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-64584) Experimental option `-H:+RelativeCodePointers` to significantly reduce relocation entries in position-independent executables and shared libraries.
* (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
5 changes: 5 additions & 0 deletions substratevm/mx.substratevm/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -1181,6 +1181,11 @@
"sdk:NATIVEIMAGE",
"com.oracle.svm.configure",
],
"requiresConcealed": {
"jdk.internal.vm.ci": [
"jdk.vm.ci.meta"
],
},
"checkstyle": "com.oracle.svm.test",
"workingSets": "SVM",
"annotationProcessors": [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright (c) 2025, 2025, 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.configure.test.config;

import java.util.List;

import org.junit.Assert;
import org.junit.Test;

import com.oracle.svm.configure.ClassNameSupport;
import com.oracle.svm.configure.test.AddExports;

import jdk.vm.ci.meta.MetaUtil;

@AddExports({"jdk.internal.vm.ci/jdk.vm.ci.meta"})
public class ClassNameSupportTest {
@Test
public void testNameAliases() {
for (Class<?> clazz : List.of(Object.class, int.class, EnclosingClass.class, EnclosingClass.InnerClass.class, EnclosingClass.StaticInnerClass.class,
new EnclosingClass().anonymousClass().getClass(), Object[].class, int[].class, EnclosingClass.InnerClass[].class)) {
testNameAliasesForClass(clazz);
}
}

private static void testNameAliasesForClass(Class<?> clazz) {
String typeName = clazz.getTypeName();
String reflectionName = clazz.getName();

Assert.assertTrue(ClassNameSupport.isValidTypeName(typeName));
Assert.assertTrue(ClassNameSupport.isValidReflectionName(reflectionName));

Assert.assertEquals(typeName, ClassNameSupport.reflectionNameToTypeName(reflectionName));
Assert.assertEquals(reflectionName, ClassNameSupport.typeNameToReflectionName(typeName));

/* Primitive classes cannot be accessed through JNI */
if (!clazz.isPrimitive()) {
String internalName = MetaUtil.toInternalName(reflectionName);
String jniName = internalName.startsWith("L") ? internalName.substring(1, internalName.length() - 1) : internalName;

Assert.assertTrue(ClassNameSupport.isValidJNIName(jniName));

Assert.assertEquals(typeName, ClassNameSupport.jniNameToTypeName(jniName));
Assert.assertEquals(reflectionName, ClassNameSupport.jniNameToReflectionName(jniName));
Assert.assertEquals(jniName, ClassNameSupport.typeNameToJNIName(typeName));
Assert.assertEquals(jniName, ClassNameSupport.reflectionNameToJNIName(reflectionName));
}
}
}

class EnclosingClass {
Object anonymousClass() {
return new Object() {
@Override
public String toString() {
return null;
}
};
}

class InnerClass {
}

static class StaticInnerClass {
}
}
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