From 9b48f0c1e395ea0dc9a80dd388d003c8286384b6 Mon Sep 17 00:00:00 2001 From: Jakub Chaloupka Date: Fri, 6 Dec 2024 11:25:25 +0100 Subject: [PATCH] Use RuntimeResourceAccess to inject resources in TruffleBaseFeature; use reachability-metadata.json config instead of feature API to add Truffle jcodings resources. --- .../oracle/svm/truffle/NeedsAllEncodings.java | 33 +++++++++++++++++++ .../svm/truffle/TruffleBaseFeature.java | 14 ++++---- .../polyglot/InternalResourceCache.java | 2 +- .../jcodings/reachability-metadata.json | 17 ++++++++++ 4 files changed, 57 insertions(+), 9 deletions(-) create mode 100644 substratevm/src/com.oracle.svm.truffle/src/com/oracle/svm/truffle/NeedsAllEncodings.java create mode 100644 truffle/src/org.graalvm.shadowed.org.jcodings/src/META-INF/native-image/org.graalvm.shadowed/jcodings/reachability-metadata.json diff --git a/substratevm/src/com.oracle.svm.truffle/src/com/oracle/svm/truffle/NeedsAllEncodings.java b/substratevm/src/com.oracle.svm.truffle/src/com/oracle/svm/truffle/NeedsAllEncodings.java new file mode 100644 index 000000000000..2afed05a52b3 --- /dev/null +++ b/substratevm/src/com.oracle.svm.truffle/src/com/oracle/svm/truffle/NeedsAllEncodings.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2024, 2024, 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.truffle; + +/** + * When this class is reachable for native image, the resources of the module + * org.graalvm.shadowed.jcodings are included in the image in case the module is on + * module path or class path and {@link TruffleBaseFeature} is enabled. + */ +public class NeedsAllEncodings { +} diff --git a/substratevm/src/com.oracle.svm.truffle/src/com/oracle/svm/truffle/TruffleBaseFeature.java b/substratevm/src/com.oracle.svm.truffle/src/com/oracle/svm/truffle/TruffleBaseFeature.java index e4b1db6ae312..de4f3ec1bbfd 100644 --- a/substratevm/src/com.oracle.svm.truffle/src/com/oracle/svm/truffle/TruffleBaseFeature.java +++ b/substratevm/src/com.oracle.svm.truffle/src/com/oracle/svm/truffle/TruffleBaseFeature.java @@ -62,7 +62,6 @@ import java.util.function.Consumer; import java.util.stream.Stream; -import com.oracle.svm.core.log.Log; import org.graalvm.collections.Pair; import org.graalvm.home.HomeFinder; import org.graalvm.home.Version; @@ -72,8 +71,7 @@ import org.graalvm.nativeimage.hosted.FieldValueTransformer; import org.graalvm.nativeimage.hosted.RuntimeClassInitialization; import org.graalvm.nativeimage.hosted.RuntimeReflection; -import org.graalvm.nativeimage.impl.ConfigurationCondition; -import org.graalvm.nativeimage.impl.RuntimeResourceSupport; +import org.graalvm.nativeimage.hosted.RuntimeResourceAccess; import org.graalvm.polyglot.Engine; import com.oracle.graal.pointsto.ObjectScanner; @@ -99,6 +97,7 @@ import com.oracle.svm.core.fieldvaluetransformer.FieldValueTransformerWithAvailability; import com.oracle.svm.core.graal.word.SubstrateWordTypes; import com.oracle.svm.core.heap.Pod; +import com.oracle.svm.core.log.Log; import com.oracle.svm.core.option.HostedOptionKey; import com.oracle.svm.core.option.HostedOptionValues; import com.oracle.svm.core.reflect.target.ReflectionSubstitutionSupport; @@ -554,10 +553,6 @@ public void duringSetup(DuringSetupAccess a) { access.registerObjectReachableCallback(TruffleFile.class, (a1, file, reason) -> checkTruffleFile(classInitializationSupport, file)); } - if (needsAllEncodings) { - RuntimeResourceSupport.singleton().addResources(ConfigurationCondition.alwaysTrue(), "org/graalvm/shadowed/org/jcodings/tables/.*bin$", "Truffle needsAllEncodings flag is set"); - } - if (includeLanguageResources) { Path resourcesForNativeImageTempDir; try { @@ -570,7 +565,7 @@ public void duringSetup(DuringSetupAccess a) { new BiConsumer>() { @Override public void accept(Module module, Pair resource) { - RuntimeResourceSupport.singleton().injectResource(module, resource.getLeft(), resource.getRight(), "Truffle Language Internal Resources"); + RuntimeResourceAccess.addResource(module, resource.getLeft(), resource.getRight()); } }); } catch (Exception e) { @@ -608,6 +603,9 @@ void setGraalGraphObjectReplacer(GraalGraphObjectReplacer graalGraphObjectReplac @Override public void beforeAnalysis(BeforeAnalysisAccess access) { + if (needsAllEncodings) { + access.registerAsUsed(NeedsAllEncodings.class); + } if (graalGraphObjectReplacer == null) { BeforeAnalysisAccessImpl config = (BeforeAnalysisAccessImpl) access; SubstrateWordTypes wordTypes = new SubstrateWordTypes(config.getMetaAccess(), ConfigurationValues.getWordKind()); diff --git a/truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/InternalResourceCache.java b/truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/InternalResourceCache.java index 97c1ba2a6d9f..b136e2e71a19 100644 --- a/truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/InternalResourceCache.java +++ b/truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/InternalResourceCache.java @@ -227,7 +227,7 @@ private Path installResource(Function re Objects.requireNonNull(resourceEnvProvider, "ResourceEnvProvider must be non-null."); assert Thread.holdsLock(this) : "Unpacking must be called under lock"; assert owningRoot.kind() == InternalResourceRoots.Root.Kind.VERSIONED; - assert !ImageInfo.inImageRuntimeCode() : "Must not be called in the image execution time."; + assert !ImageInfo.inImageRuntimeCode() || aggregatedFileListHash != null : "InternalResource#unpackFiles must not be called in the image execution time."; InternalResource resource = resourceFactory.get(); InternalResource.Env env = resourceEnvProvider.apply(resource); String versionHash = aggregatedFileListHash == null || env.inNativeImageBuild() ? resource.versionHash(env) diff --git a/truffle/src/org.graalvm.shadowed.org.jcodings/src/META-INF/native-image/org.graalvm.shadowed/jcodings/reachability-metadata.json b/truffle/src/org.graalvm.shadowed.org.jcodings/src/META-INF/native-image/org.graalvm.shadowed/jcodings/reachability-metadata.json new file mode 100644 index 000000000000..0f7f3df1bbc9 --- /dev/null +++ b/truffle/src/org.graalvm.shadowed.org.jcodings/src/META-INF/native-image/org.graalvm.shadowed/jcodings/reachability-metadata.json @@ -0,0 +1,17 @@ +{ + "resources": [ + { + "condition": { + "typeReached": "com.oracle.svm.truffle.NeedsAllEncodings" + }, + "module": "org.graalvm.shadowed.jcodings", + "glob": "org/graalvm/shadowed/org/jcodings/tables/*.bin" + }, + { + "condition": { + "typeReached": "com.oracle.svm.truffle.NeedsAllEncodings" + }, + "glob": "org/graalvm/shadowed/org/jcodings/tables/*.bin" + } + ] +} \ No newline at end of file