From 108a3738459bd3f37e1b5b3fed06f0c2a9709223 Mon Sep 17 00:00:00 2001 From: Vojin Jovanovic Date: Wed, 23 Jul 2025 14:32:10 +0200 Subject: [PATCH] Enable metadata tracing with -H:Preserve=all Also move other feature-enabling flags to be used only with -H:Preserve=all. The flags were blowing up image size overly much for simple -H:Preserve usage. --- .../hosted/NativeImageClassLoaderSupport.java | 6 +++--- .../hosted/image/PreserveOptionsSupport.java | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageClassLoaderSupport.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageClassLoaderSupport.java index b2de12f446dc..b9bc2d9913fe 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageClassLoaderSupport.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageClassLoaderSupport.java @@ -147,13 +147,13 @@ public void clearDynamicAccessSelectors() { } public boolean isPreserveMode() { - return !preserveSelectors.classpathEntries.isEmpty() || !preserveSelectors.moduleNames.isEmpty() || !preserveSelectors.packages.isEmpty() || preserveAll(); + return !preserveSelectors.classpathEntries.isEmpty() || !preserveSelectors.moduleNames.isEmpty() || !preserveSelectors.packages.isEmpty() || isPreserveAll(); } /** * @return true if {@link PreserveOptionsSupport#PRESERVE_ALL preserve all} is enabled. */ - private boolean preserveAll() { + public boolean isPreserveAll() { return preserveAllOrigin().isPresent(); } @@ -312,7 +312,7 @@ private ModuleFinder getModulePathsFinder() { public void loadAllClasses(ForkJoinPool executor, ImageClassLoader imageClassLoader) { VMError.guarantee(!includeConfigSealed, "This method should be executed only once."); - if (preserveAll()) { + if (isPreserveAll()) { String msg = """ This image build includes all classes from the classpath and the JDK via the %s option. This will lead to noticeably bigger images and increased startup times. If you notice '--initialize-at-build-time' related errors during the build, this is because unanticipated types ended up in the image heap.\ diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/PreserveOptionsSupport.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/PreserveOptionsSupport.java index c06f03668d69..fac075df54c5 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/PreserveOptionsSupport.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/PreserveOptionsSupport.java @@ -28,6 +28,7 @@ import static com.oracle.svm.core.SubstrateOptions.EnableURLProtocols; import static com.oracle.svm.core.SubstrateOptions.Preserve; import static com.oracle.svm.core.jdk.JRTSupport.Options.AllowJRTFileSystem; +import static com.oracle.svm.core.metadata.MetadataTracer.Options.MetadataTracingSupport; import static com.oracle.svm.hosted.SecurityServicesFeature.Options.AdditionalSecurityProviders; import static com.oracle.svm.hosted.jdk.localization.LocalizationFeature.Options.AddAllCharsets; import static com.oracle.svm.hosted.jdk.localization.LocalizationFeature.Options.IncludeAllLocales; @@ -40,6 +41,7 @@ import java.security.Security; import java.util.Arrays; import java.util.Comparator; +import java.util.List; import java.util.Set; import java.util.StringJoiner; import java.util.stream.Stream; @@ -140,6 +142,7 @@ public static void parsePreserveOption(EconomicMap, Object> hostedV } }); if (classLoaderSupport.isPreserveMode()) { + /* Significantly speeds up analysis */ if (UseConservativeUnsafeAccess.hasBeenSet(optionValues)) { UserError.guarantee(UseConservativeUnsafeAccess.getValue(optionValues), "%s can not be used together with %s. Please unset %s.", SubstrateOptionsParser.commandArgument(UseConservativeUnsafeAccess, "-"), @@ -147,12 +150,24 @@ public static void parsePreserveOption(EconomicMap, Object> hostedV SubstrateOptionsParser.commandArgument(UseConservativeUnsafeAccess, "-")); } UseConservativeUnsafeAccess.update(hostedValues, true); + } + if (classLoaderSupport.isPreserveAll()) { + /* Include all parts of native image that are stripped */ AddAllCharsets.update(hostedValues, true); IncludeAllLocales.update(hostedValues, true); AllowJRTFileSystem.update(hostedValues, true); - EnableURLProtocols.update(hostedValues, "http,https,ftp,jar,file,mailto,jrt,jmod"); + + /* Should be removed with GR-61365 */ + var missingJDKProtocols = List.of("http", "https", "ftp", "jar", "mailto", "jrt", "jmod"); + for (String missingProtocol : missingJDKProtocols) { + EnableURLProtocols.update(hostedValues, missingProtocol); + } + AdditionalSecurityProviders.update(hostedValues, getSecurityProvidersCSV()); + + /* Allow metadata tracing in preserve all images */ + MetadataTracingSupport.update(hostedValues, true); } }