Skip to content
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

[GR-46740] Support JEP 424 ("Panama") foreign down calls in Native Image. #7152

Closed
wants to merge 15 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
2 changes: 1 addition & 1 deletion compiler/mx.compiler/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@
"jdk.unsupported" # sun.misc.Unsafe
],
"exports" : [
"""* to com.oracle.graal.graal_enterprise,org.graalvm.nativeimage.pointsto,org.graalvm.nativeimage.builder,org.graalvm.nativeimage.llvm,com.oracle.svm.svm_enterprise,com.oracle.svm_enterprise.ml_dataset,org.graalvm.nativeimage.base,
"""* to com.oracle.graal.graal_enterprise,org.graalvm.nativeimage.pointsto,org.graalvm.nativeimage.builder,org.graalvm.nativeimage.foreign,org.graalvm.nativeimage.llvm,com.oracle.svm.svm_enterprise,com.oracle.svm_enterprise.ml_dataset,org.graalvm.nativeimage.base,
org.graalvm.extraimage.builder,com.oracle.svm.extraimage_enterprise""",
"org.graalvm.compiler.java to org.graalvm.nativeimage.agent.tracing,org.graalvm.nativeimage.configure",
"org.graalvm.compiler.core.common to org.graalvm.nativeimage.agent.tracing,org.graalvm.nativeimage.objectfile",
Expand Down
1 change: 1 addition & 0 deletions docs/reference-manual/native-image/BuildOptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Depending on the GraalVM version, the options to the `native-image` builder may
* `-J<flag>`: pass an option directly to the JVM running the `native-image` builder
* `--diagnostics-mode`: enable diagnostics output which includes class initialization, substitutions, etc.
* `--enable-preview`: allow classes to depend on preview features of this release
* `--enable-native-access <module name>[,<module name>...]` modules that are permitted to perform restricted native operations. `<module name>` can also be `ALL-UNNAMED`.
* `--verbose`: enable verbose output
* `--version`: print the product version and exit
* `--help`: print this help message
Expand Down
2 changes: 1 addition & 1 deletion sdk/mx.sdk/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@
"org.graalvm.word",
"org.graalvm.polyglot.impl to org.graalvm.truffle, com.oracle.graal.graal_enterprise",
"org.graalvm.word.impl to jdk.internal.vm.compiler",
"org.graalvm.nativeimage.impl to org.graalvm.nativeimage.pointsto,org.graalvm.nativeimage.base,org.graalvm.nativeimage.builder,org.graalvm.nativeimage.configure,com.oracle.svm.svm_enterprise,org.graalvm.extraimage.builder",
"org.graalvm.nativeimage.impl to org.graalvm.nativeimage.pointsto,org.graalvm.nativeimage.base,org.graalvm.nativeimage.builder,org.graalvm.nativeimage.configure,com.oracle.svm.svm_enterprise,org.graalvm.extraimage.builder,org.graalvm.nativeimage.foreign",
"org.graalvm.nativeimage.impl.clinit to org.graalvm.nativeimage.builder",
],
"uses" : [
Expand Down
4 changes: 4 additions & 0 deletions sdk/src/org.graalvm.nativeimage/snapshot.sigtest
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,10 @@ meth public !varargs static void initializeAtRunTime(java.lang.Class<?>[])
meth public !varargs static void initializeAtRunTime(java.lang.String[])
supr java.lang.Object

CLSS public final org.graalvm.nativeimage.hosted.RuntimeForeignAccess
meth public !varargs static void registerForDowncall(java.lang.Object,java.lang.Object[])
supr java.lang.Object

CLSS public final org.graalvm.nativeimage.hosted.RuntimeJNIAccess
meth public !varargs static void register(java.lang.Class<?>[])
meth public !varargs static void register(java.lang.reflect.Executable[])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* (a) the Software, and
*
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.graalvm.nativeimage.hosted;

import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
import org.graalvm.nativeimage.impl.ConfigurationCondition;
import org.graalvm.nativeimage.impl.RuntimeForeignAccessSupport;

@Platforms(Platform.HOSTED_ONLY.class)
public final class RuntimeForeignAccess {

/**
* Registers the provided descriptor and options pair for foreign downcalls at runtime. Needed
* in order to get a method handle using <a href=
* "https://download.java.net/java/early_access/jdk21/docs/api/java.base/java/lang/foreign/Linker.html#downcallHandle(java.lang.foreign.FunctionDescriptor,java.lang.foreign.Linker.Option...)">downcallHandle</a>
* <p>
* Since the foreign functions feature is currently a preview of the Java platform (as part of
* <a href="https://openjdk.org/projects/panama/">Project Panama</a>), this API is itself a
* preview subject to changes following changes in the feature itself. This also means that this
* feature must be manually enabled, using (on a JDK greater or equal to 21)
*
* <pre>
* {@code
* --enable-preview -H:+ForeignFunctions
* }
* </pre>
*
* Even though this method is weakly typed for compatibility reasons, runtime checks will be
* performed to ensure that the arguments have the expected type. It will be deprecated in favor
* of strongly typed variant as soon as possible.
*
* @param desc A {@link java.lang.foreign.FunctionDescriptor} to register for downcalls.
* @param options An array of {@link java.lang.foreign.Linker.Option} used for the downcalls.
*
* @since 23.1
*/
public static void registerForDowncall(Object desc, Object... options) {
ImageSingletons.lookup(RuntimeForeignAccessSupport.class).registerForDowncall(ConfigurationCondition.alwaysTrue(), desc, options);
}

private RuntimeForeignAccess() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* (a) the Software, and
*
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.graalvm.nativeimage.impl;

public interface RuntimeForeignAccessSupport {
void registerForDowncall(ConfigurationCondition condition, Object desc, Object... options);
}
16 changes: 16 additions & 0 deletions substratevm/mx.substratevm/mx_substratevm.py
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,22 @@ def _native_image_launcher_extra_jvm_args():
if llvm_supported:
mx_sdk_vm.register_graalvm_component(ce_llvm_backend)

if mx.get_jdk(tag='default').javaCompliance >= '21':
mx_sdk_vm.register_graalvm_component(mx_sdk_vm.GraalVmJreComponent(
suite=suite,
name='SubstrateVM Foreign API Preview Feature',
short_name='panama',
dir_name='svm-preview',
installable_id='native-image',
license_files=[],
third_party_license_files=[],
dependencies=['SubstrateVM'],
builder_jar_distributions=['substratevm:SVM_FOREIGN'],
installable=False,
jlink=False,
))



mx_sdk_vm.register_graalvm_component(mx_sdk_vm.GraalVmJreComponent(
suite=suite,
Expand Down
101 changes: 97 additions & 4 deletions substratevm/mx.substratevm/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,63 @@
],
},

"com.oracle.svm.core.foreign": {
"subDir": "src",
"sourceDirs": ["src"],
"dependencies": [
"com.oracle.svm.core"
],
"requiresConcealed": {
"java.base": [
"jdk.internal.loader",
"jdk.internal.foreign",
"jdk.internal.foreign.abi",
"jdk.internal.foreign.abi.x64",
"jdk.internal.foreign.abi.x64.sysv",
"jdk.internal.foreign.abi.x64.windows",
],
"jdk.internal.vm.ci" : [
],
},
"javaCompliance" : "21+",
"javaPreviewNeeded": "21+",
"annotationProcessors": [
"compiler:GRAAL_PROCESSOR",
"SVM_PROCESSOR",
],
"javac.lint.overrides": "-preview",
"checkstyle": "com.oracle.svm.hosted",
"workingSets": "SVM",
"jacoco" : "include",
},

"com.oracle.svm.hosted.foreign": {
"subDir": "src",
"sourceDirs": ["src"],
"dependencies": [
"com.oracle.svm.hosted",
"com.oracle.svm.core.foreign"
],
"requiresConcealed": {
"java.base": [
"jdk.internal.foreign.abi",
],
"jdk.internal.vm.ci" : [
"jdk.vm.ci.code"
],
},
"javaCompliance" : "21+",
"javaPreviewNeeded": "21+",
"annotationProcessors": [
"compiler:GRAAL_PROCESSOR",
"SVM_PROCESSOR",
],
"javac.lint.overrides": "-preview",
"checkstyle": "com.oracle.svm.hosted",
"workingSets": "SVM",
"jacoco" : "include",
},

# Native libraries below explicitly set _FORTIFY_SOURCE to 0. This constant controls how glibc handles some
# functions that can cause a stack overflow like snprintf. If set to 1 or 2, it causes glibc to use internal
# functions with extra checking that are not available in all libc implementations. Different distros use
Expand Down Expand Up @@ -1310,7 +1367,7 @@
"exports" : [
"com.oracle.svm.hosted to java.base",
"com.oracle.svm.truffle.api to org.graalvm.truffle",
"* to org.graalvm.nativeimage.base,jdk.internal.vm.compiler,org.graalvm.nativeimage.driver,org.graalvm.nativeimage.configure,org.graalvm.nativeimage.librarysupport,org.graalvm.nativeimage.junitsupport,org.graalvm.nativeimage.llvm,org.graalvm.nativeimage.agent.jvmtibase,org.graalvm.nativeimage.agent.tracing,org.graalvm.nativeimage.agent.diagnostics,com.oracle.svm.svm_enterprise,com.oracle.svm.svm_enterprise.llvm,com.oracle.svm_enterprise.ml_dataset,org.graalvm.extraimage.builder,com.oracle.svm.extraimage_enterprise",
"* to org.graalvm.nativeimage.base,jdk.internal.vm.compiler,org.graalvm.nativeimage.driver,org.graalvm.nativeimage.configure,org.graalvm.nativeimage.librarysupport,org.graalvm.nativeimage.junitsupport,org.graalvm.nativeimage.llvm,org.graalvm.nativeimage.agent.jvmtibase,org.graalvm.nativeimage.agent.tracing,org.graalvm.nativeimage.agent.diagnostics,com.oracle.svm.svm_enterprise,com.oracle.svm.svm_enterprise.llvm,com.oracle.svm_enterprise.ml_dataset,org.graalvm.extraimage.builder,com.oracle.svm.extraimage_enterprise,org.graalvm.nativeimage.foreign",
],
"opens" : [
"com.oracle.svm.core to jdk.internal.vm.compiler",
Expand Down Expand Up @@ -1637,9 +1694,9 @@
"moduleInfo" : {
"name" : "org.graalvm.nativeimage.base",
"exports" : [
"com.oracle.svm.util to org.graalvm.nativeimage.pointsto,org.graalvm.nativeimage.builder,org.graalvm.nativeimage.librarysupport,org.graalvm.nativeimage.driver,org.graalvm.nativeimage.llvm,org.graalvm.nativeimage.agent.jvmtibase,org.graalvm.nativeimage.agent.tracing,org.graalvm.nativeimage.agent.diagnostics,org.graalvm.nativeimage.junitsupport,com.oracle.svm.svm_enterprise,com.oracle.svm_enterprise.ml_dataset,org.graalvm.extraimage.builder,com.oracle.svm.extraimage_enterprise,org.graalvm.extraimage.librarysupport",
"com.oracle.svm.common.meta to org.graalvm.nativeimage.pointsto,org.graalvm.nativeimage.builder",
"com.oracle.svm.common.option to org.graalvm.nativeimage.pointsto,org.graalvm.nativeimage.builder,org.graalvm.nativeimage.driver",
"com.oracle.svm.util to org.graalvm.nativeimage.pointsto,org.graalvm.nativeimage.builder,org.graalvm.nativeimage.librarysupport,org.graalvm.nativeimage.driver,org.graalvm.nativeimage.llvm,org.graalvm.nativeimage.agent.jvmtibase,org.graalvm.nativeimage.agent.tracing,org.graalvm.nativeimage.agent.diagnostics,org.graalvm.nativeimage.junitsupport,com.oracle.svm.svm_enterprise,com.oracle.svm_enterprise.ml_dataset,org.graalvm.extraimage.builder,com.oracle.svm.extraimage_enterprise,org.graalvm.extraimage.librarysupport,org.graalvm.nativeimage.foreign",
"com.oracle.svm.common.meta to org.graalvm.nativeimage.pointsto,org.graalvm.nativeimage.builder,org.graalvm.nativeimage.foreign",
"com.oracle.svm.common.option to org.graalvm.nativeimage.pointsto,org.graalvm.nativeimage.builder,org.graalvm.nativeimage.driver,org.graalvm.nativeimage.foreign",
],
}
},
Expand Down Expand Up @@ -1870,6 +1927,42 @@
},
},

"SVM_FOREIGN": {
"subDir": "src",
"description" : "SubstrateVM support for the Foreign API",
"dependencies": [
"com.oracle.svm.hosted.foreign",
],
"distDependencies": [
"compiler:GRAAL",
"SVM"
],
"moduleInfo" : {
"name" : "org.graalvm.nativeimage.foreign",
"requires" : [
"org.graalvm.nativeimage.builder"
],
"exports" : [
"* to org.graalvm.nativeimage.builder",
],
"requiresConcealed": {
"jdk.internal.vm.ci" : [
"jdk.vm.ci.meta",
"jdk.vm.ci.code",
"jdk.vm.ci.amd64",
],
"java.base": [
"jdk.internal.foreign",
"jdk.internal.foreign.abi",
"jdk.internal.foreign.abi.x64",
"jdk.internal.foreign.abi.x64.sysv",
"jdk.internal.foreign.abi.x64.windows",
],
},
},
"maven" : False,
},

"SVM_LLVM" : {
"subDir" : "src",
"description" : "LLVM backend for Native Image",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ protected JavaConstant createFieldValue(AnalysisField field, ImageHeapInstance r
ObjectScanner.unsupportedFeatureDuringFieldScan(universe.getBigbang(), field, receiver, e, reason);
transformedValue = JavaConstant.NULL_POINTER;
}
assert transformedValue != null : field.getDeclaringClass().toJavaName() + "::" + field.getName();

return createImageHeapConstant(transformedValue, reason);
}
Expand Down
Loading