From c7a37fee7f0f86acdb08011cc6053f2d8d539bef Mon Sep 17 00:00:00 2001 From: Codrut Stancu Date: Sat, 22 Feb 2025 19:11:26 +0100 Subject: [PATCH 1/3] Make JavaMainSupport ApplicationLayerOnlyImageSingleton. --- .../com/oracle/svm/core/JavaMainWrapper.java | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/JavaMainWrapper.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/JavaMainWrapper.java index 497a1ce6e621..6ba18489ba1c 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/JavaMainWrapper.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/JavaMainWrapper.java @@ -32,10 +32,10 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.EnumSet; import java.util.List; import java.util.function.BooleanSupplier; -import jdk.graal.compiler.word.Word; import org.graalvm.nativeimage.CurrentIsolate; import org.graalvm.nativeimage.ImageSingletons; import org.graalvm.nativeimage.Isolate; @@ -65,10 +65,14 @@ import com.oracle.svm.core.c.function.CEntryPointOptions.NoEpilogue; import com.oracle.svm.core.c.function.CEntryPointOptions.NoPrologue; import com.oracle.svm.core.c.function.CEntryPointSetup; +import com.oracle.svm.core.graal.snippets.CEntryPointSnippets; import com.oracle.svm.core.jdk.InternalVMMethod; import com.oracle.svm.core.jdk.RuntimeSupport; import com.oracle.svm.core.jni.JNIJavaVMList; import com.oracle.svm.core.jni.functions.JNIFunctionTables; +import com.oracle.svm.core.layeredimagesingleton.ApplicationLayerOnlyImageSingleton; +import com.oracle.svm.core.layeredimagesingleton.LayeredImageSingletonBuilderFlags; +import com.oracle.svm.core.layeredimagesingleton.UnsavedSingleton; import com.oracle.svm.core.log.Log; import com.oracle.svm.core.thread.JavaThreads; import com.oracle.svm.core.thread.PlatformThreads; @@ -80,6 +84,8 @@ import com.oracle.svm.util.ClassUtil; import com.oracle.svm.util.ReflectionUtil; +import jdk.graal.compiler.word.Word; + @InternalVMMethod public class JavaMainWrapper { /* @@ -90,8 +96,12 @@ public class JavaMainWrapper { private static UnsignedWord argvLength = Word.zero(); - public static class JavaMainSupport { - + /** + * In a layered build the {@link JavaMainSupport} is installed in the last layer. However, code + * that uses it may be compiled as part of the base layer, e.g., such as + * {@link CEntryPointSnippets}. + */ + public static class JavaMainSupport implements ApplicationLayerOnlyImageSingleton, UnsavedSingleton { private final MethodHandle javaMainHandle; private final MethodHandle javaMainClassCtorHandle; final String javaMainClassName; @@ -157,6 +167,10 @@ public List getInputArguments() { return Collections.emptyList(); } + @Override + public EnumSet getImageBuilderFlags() { + return LayeredImageSingletonBuilderFlags.ALL_ACCESS; + } } public static void invokeMain(String[] args) throws Throwable { @@ -189,6 +203,7 @@ public static void invokeMain(String[] args) throws Throwable { * Determines whether instance main methodes are enabled. See JDK-8306112: Implementation of JEP * 445: Unnamed Classes and Instance Main Methods (Preview). */ + @Platforms(Platform.HOSTED_ONLY.class) public static boolean instanceMainMethodSupported() { var previewFeature = ReflectionUtil.lookupClass(true, "jdk.internal.misc.PreviewFeatures"); try { From 6a6115bb37cfaddb5dba5e0b8ab1fc37d7021a03 Mon Sep 17 00:00:00 2001 From: Codrut Stancu Date: Fri, 24 Jan 2025 18:44:01 +0100 Subject: [PATCH 2/3] Inject declared type state in base written field. Currently needed for JavaMainSupport.mainArgs, only seen as written in base layer. --- .../com/oracle/graal/pointsto/meta/AnalysisField.java | 10 ++++++++++ .../svm/hosted/imagelayer/SVMImageLayerLoader.java | 10 ++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/meta/AnalysisField.java b/substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/meta/AnalysisField.java index d3b2fc6b1cca..c5b8d0f1df42 100644 --- a/substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/meta/AnalysisField.java +++ b/substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/meta/AnalysisField.java @@ -31,6 +31,7 @@ import java.util.concurrent.ConcurrentMap; import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; +import com.oracle.graal.pointsto.BigBang; import com.oracle.graal.pointsto.api.PointstoOptions; import com.oracle.graal.pointsto.flow.ContextInsensitiveFieldTypeFlow; import com.oracle.graal.pointsto.flow.FieldTypeFlow; @@ -268,6 +269,15 @@ public boolean registerAsWritten(Object reason) { }); } + public void injectDeclaredType() { + BigBang bb = getUniverse().getBigbang(); + if (getStorageKind().isObject()) { + bb.injectFieldTypes(this, List.of(this.getType()), true); + } else if (bb.trackPrimitiveValues() && getStorageKind().isPrimitive()) { + ((PointsToAnalysisField) this).saturatePrimitiveField(); + } + } + public boolean isGuaranteeFolded() { return getAnnotation(GuaranteeFolded.class) != null; } diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/imagelayer/SVMImageLayerLoader.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/imagelayer/SVMImageLayerLoader.java index 40d6e2af50a6..09028de6d021 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/imagelayer/SVMImageLayerLoader.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/imagelayer/SVMImageLayerLoader.java @@ -1157,9 +1157,15 @@ public void initializeBaseLayerField(AnalysisField analysisField) { if (!analysisField.isStatic() && (isAccessed || isRead)) { analysisField.getDeclaringClass().getInstanceFields(true); } - registerFlag(isAccessed, debug -> analysisField.registerAsAccessed(PERSISTED)); + registerFlag(isAccessed, debug -> { + analysisField.injectDeclaredType(); + analysisField.registerAsAccessed(PERSISTED); + }); registerFlag(isRead, debug -> analysisField.registerAsRead(PERSISTED)); - registerFlag(fieldData.getIsWritten(), debug -> analysisField.registerAsWritten(PERSISTED)); + registerFlag(fieldData.getIsWritten(), debug -> { + analysisField.injectDeclaredType(); + analysisField.registerAsWritten(PERSISTED); + }); registerFlag(fieldData.getIsFolded(), debug -> analysisField.registerAsFolded(PERSISTED)); } From bacdca43582e50cafa6771401700ad6e9f64e4e2 Mon Sep 17 00:00:00 2001 From: Codrut Stancu Date: Thu, 27 Feb 2025 15:33:07 +0100 Subject: [PATCH 3/3] Allow layered singletons to be subtyped. --- .../src/com/oracle/svm/hosted/ImageSingletonsSupportImpl.java | 4 ---- .../svm/hosted/imagelayer/LoadImageSingletonFeature.java | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ImageSingletonsSupportImpl.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ImageSingletonsSupportImpl.java index d64b691325a3..dd8b57146a21 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ImageSingletonsSupportImpl.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ImageSingletonsSupportImpl.java @@ -228,10 +228,6 @@ private void doAddInternal(Class key, Object value) { if (singleton instanceof MultiLayeredImageSingleton || ApplicationLayerOnlyImageSingleton.isSingletonInstanceOf(singleton)) { - if (!key.equals(singleton.getClass())) { - throw UserError.abort("The implementation class must be the same as the key class. key: %s, singleton: %s", key, singleton); - } - if (singleton instanceof MultiLayeredImageSingleton && ApplicationLayerOnlyImageSingleton.isSingletonInstanceOf(singleton)) { throw UserError.abort("Singleton cannot implement both %s and %s. singleton: %s", MultiLayeredImageSingleton.class, ApplicationLayerOnlyImageSingleton.class, singleton); } diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/imagelayer/LoadImageSingletonFeature.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/imagelayer/LoadImageSingletonFeature.java index 793918b51d24..b77235c5a67b 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/imagelayer/LoadImageSingletonFeature.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/imagelayer/LoadImageSingletonFeature.java @@ -580,7 +580,7 @@ void assignSlots(HostedMetaAccess metaAccess) { int slotAssignment; LoadImageSingletonDataImpl info = entry.getValue(); var hType = metaAccess.lookupJavaType(info.getLoadType()); - if (hType.isInstantiated()) { + if (hType.getWrapped().isAnySubtypeInstantiated()) { Class keyClass = entry.getKey(); SlotInfo slotInfo = priorKeyToSlotInfoMap.get(entry.getKey());