From a43f3ee3ed05d99ad1350207e8a2b78e775b837e Mon Sep 17 00:00:00 2001 From: Doug Simon Date: Mon, 10 Mar 2025 12:15:04 +0100 Subject: [PATCH] avoid system property access in ImageInfo.inImage[Build|Run]timeCode --- .../src/org/graalvm/nativeimage/ImageInfo.java | 13 +++++++++---- .../src/com/oracle/svm/driver/NativeImage.java | 3 +++ .../com/oracle/svm/hosted/NativeImageGenerator.java | 3 +-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/ImageInfo.java b/sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/ImageInfo.java index 6575bb126f1f..efcafcf02764 100644 --- a/sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/ImageInfo.java +++ b/sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/ImageInfo.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 2025, 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 @@ -60,7 +60,7 @@ private ImageInfo() { * {@link #PROPERTY_IMAGE_CODE_VALUE_BUILDTIME} the code is executing in the context of image * building (e.g. in a static initializer of a class that will be contained in the image). If * the property returns the string given by {@link #PROPERTY_IMAGE_CODE_VALUE_RUNTIME} the code - * is executing at image runtime. Otherwise the property is not set. + * is executing at image runtime. Otherwise, the property is not set. * * @since 19.0 */ @@ -118,6 +118,7 @@ private ImageInfo() { * @since 19.0 */ public static boolean inImageCode() { + // A plugin in SubstrateGraphBuilderPlugins constant-folds this method to return true. return inImageBuildtimeCode() || inImageRuntimeCode(); } @@ -129,9 +130,12 @@ public static boolean inImageCode() { * @since 19.0 */ public static boolean inImageRuntimeCode() { - return PROPERTY_IMAGE_CODE_VALUE_RUNTIME.equals(System.getProperty(PROPERTY_IMAGE_CODE_KEY)); + // A plugin in SubstrateGraphBuilderPlugins constant-folds this method to return true. + return false; } + private static final boolean BUILDTIME = PROPERTY_IMAGE_CODE_VALUE_BUILDTIME.equals(System.getProperty(PROPERTY_IMAGE_CODE_KEY)); + /** * Returns true if (at the time of the call) code is executing in the context of image building * (e.g. in a static initializer of class that will be contained in the image). @@ -139,7 +143,8 @@ public static boolean inImageRuntimeCode() { * @since 19.0 */ public static boolean inImageBuildtimeCode() { - return PROPERTY_IMAGE_CODE_VALUE_BUILDTIME.equals(System.getProperty(PROPERTY_IMAGE_CODE_KEY)); + // A plugin in SubstrateGraphBuilderPlugins constant-folds this method to return false. + return BUILDTIME; } /** diff --git a/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/NativeImage.java b/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/NativeImage.java index eb2cd05ea3b6..1f497f439a4d 100644 --- a/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/NativeImage.java +++ b/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/NativeImage.java @@ -71,6 +71,7 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import org.graalvm.nativeimage.ImageInfo; import org.graalvm.nativeimage.Platform; import org.graalvm.nativeimage.ProcessProperties; @@ -998,6 +999,8 @@ private void prepareImageBuildArgs() { addImageBuilderJavaArgs("-Dcom.oracle.graalvm.isaot=true"); addImageBuilderJavaArgs("-Djava.system.class.loader=" + CUSTOM_SYSTEM_CLASS_LOADER); + addImageBuilderJavaArgs("-D" + ImageInfo.PROPERTY_IMAGE_CODE_KEY + "=" + ImageInfo.PROPERTY_IMAGE_CODE_VALUE_BUILDTIME); + /* * The presence of CDS and custom system class loaders disables the use of archived * non-system class and triggers a warning. diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageGenerator.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageGenerator.java index eae4aa443200..b54a33153edd 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageGenerator.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageGenerator.java @@ -541,7 +541,7 @@ public void run(Map entryPoints, } protected static void setSystemPropertiesForImageEarly() { - System.setProperty(ImageInfo.PROPERTY_IMAGE_CODE_KEY, ImageInfo.PROPERTY_IMAGE_CODE_VALUE_BUILDTIME); + VMError.guarantee(ImageInfo.inImageBuildtimeCode(), "Expected ImageInfo.inImageBuildtimeCode() to return true"); } private static void setSystemPropertiesForImageLate(NativeImageKind imageKind) { @@ -554,7 +554,6 @@ private static void setSystemPropertiesForImageLate(NativeImageKind imageKind) { } public static void clearSystemPropertiesForImage() { - System.clearProperty(ImageInfo.PROPERTY_IMAGE_CODE_KEY); System.clearProperty(ImageInfo.PROPERTY_IMAGE_KIND_KEY); }