From 0fca74bec6287485f53d9657f0558e979ab9071e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Thu, 3 Oct 2024 11:43:24 +0200 Subject: [PATCH] Enforce correct values for Services.IS_[IN|BUILDING]_NATIVE_IMAGE in image-runtime --- .../com/oracle/svm/core/SubstrateUtil.java | 9 +++- .../svm/core/jdk/JVMCISubstitutions.java | 43 ++++++++++++------- .../runtime/hotspot/libgraal/LibGraal.java | 25 +++-------- .../hotspot/libgraal/LibGraalScope.java | 7 +-- 4 files changed, 42 insertions(+), 42 deletions(-) diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/SubstrateUtil.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/SubstrateUtil.java index c348051cf78e..b599275f0287 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/SubstrateUtil.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/SubstrateUtil.java @@ -38,7 +38,9 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.graalvm.nativeimage.ImageInfo; import org.graalvm.nativeimage.Platform; +import org.graalvm.nativeimage.Platform.HOSTED_ONLY; import org.graalvm.nativeimage.Platforms; import org.graalvm.nativeimage.c.type.CCharPointer; import org.graalvm.nativeimage.c.type.CCharPointerPointer; @@ -93,18 +95,21 @@ public static String getArchitectureName() { return arch; } + private static final boolean libGraalContext = Services.IS_BUILDING_NATIVE_IMAGE; + /** * @return true if the standalone libgraal is being built instead of a normal SVM image. */ + @Platforms(HOSTED_ONLY.class) public static boolean isBuildingLibgraal() { - return Services.IS_BUILDING_NATIVE_IMAGE; + return libGraalContext; } /** * @return true if running in the standalone libgraal image. */ public static boolean isInLibgraal() { - return Services.IS_IN_NATIVE_IMAGE; + return libGraalContext && ImageInfo.inImageRuntimeCode(); } private static final Method IS_TERMINAL_METHOD = ReflectionUtil.lookupMethod(true, Console.class, "isTerminal"); diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/JVMCISubstitutions.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/JVMCISubstitutions.java index efae01988ad5..4ff7a612b638 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/JVMCISubstitutions.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/JVMCISubstitutions.java @@ -25,7 +25,6 @@ package com.oracle.svm.core.jdk; import java.util.Map; -import java.util.function.BooleanSupplier; import org.graalvm.nativeimage.Platform; import org.graalvm.nativeimage.Platforms; @@ -33,39 +32,51 @@ import com.oracle.svm.core.SubstrateUtil; import com.oracle.svm.core.annotate.Alias; import com.oracle.svm.core.annotate.Delete; +import com.oracle.svm.core.annotate.RecomputeFieldValue; import com.oracle.svm.core.annotate.Substitute; import com.oracle.svm.core.annotate.TargetClass; import jdk.vm.ci.amd64.AMD64; import jdk.vm.ci.amd64.AMD64Kind; -import jdk.vm.ci.services.Services; -final class IsNotLibgraal implements BooleanSupplier { - @Override - public boolean getAsBoolean() { - return !SubstrateUtil.isBuildingLibgraal(); - } -} - -/** - * In libgraal the saved properties are initialized by copying them from the HotSpot heap. - */ -@TargetClass(value = Services.class, onlyWith = IsNotLibgraal.class) +@TargetClass(jdk.vm.ci.services.Services.class) final class Target_jdk_vm_ci_services_Services { - @Delete // - static Map savedProperties; + /** + * Ensure field returns true if seen by the analysis. + */ + // Checkstyle: stop + @Alias // + @RecomputeFieldValue(kind = RecomputeFieldValue.Kind.FromAlias, isFinal = true)// + public static boolean IS_IN_NATIVE_IMAGE = true; + // Checkstyle: resume + + /** + * Ensure field returns false if seen by the analysis. + */ + // Checkstyle: stop + @Alias // + @RecomputeFieldValue(kind = RecomputeFieldValue.Kind.FromAlias, isFinal = true)// + public static boolean IS_BUILDING_NATIVE_IMAGE = false; + // Checkstyle: resume + + /** + * Redirect to {@link SystemPropertiesSupport#singleton()}. + */ @Substitute public static Map getSavedProperties() { return SystemPropertiesSupport.singleton().getSavedProperties(); } + + @Delete // + static Map savedProperties; } /** * Allow updating the value backing {@link AMD64#getLargestStorableKind}. */ @Platforms(Platform.AMD64.class) -@TargetClass(value = AMD64.class, onlyWith = IsNotLibgraal.class) +@TargetClass(value = AMD64.class) final class Target_jdk_vm_ci_amd64_AMD64 { @Alias AMD64Kind largestKind; diff --git a/truffle/src/com.oracle.truffle.runtime/src/com/oracle/truffle/runtime/hotspot/libgraal/LibGraal.java b/truffle/src/com.oracle.truffle.runtime/src/com/oracle/truffle/runtime/hotspot/libgraal/LibGraal.java index 5c91731438eb..4f7dcaf503d2 100644 --- a/truffle/src/com.oracle.truffle.runtime/src/com/oracle/truffle/runtime/hotspot/libgraal/LibGraal.java +++ b/truffle/src/com.oracle.truffle.runtime/src/com/oracle/truffle/runtime/hotspot/libgraal/LibGraal.java @@ -61,19 +61,15 @@ /** * Access to libgraal, a shared library containing an AOT compiled version of Graal produced by - * GraalVM Native Image. The libgraal library is only available if: - *
    - *
  • the {@linkplain #inLibGraal() current runtime} is libgraal, or
  • - *
  • the HotSpot {@code UseJVMCINativeLibrary} flag is true and the current runtime includes the - * relevant JVMCI API additions for accessing libgraal.
  • - *
+ * GraalVM Native Image. The libgraal library is only available if the HotSpot + * {@code UseJVMCINativeLibrary} flag is true and the current runtime includes the relevant JVMCI + * API additions for accessing libgraal. * * The {@link #isAvailable()} method is provided to test these conditions. It must be used to guard * usage of all other methods in this class. In addition, only the following methods can be called * from within libgraal: *
    *
  • {@link #isAvailable()}
  • - *
  • {@link #inLibGraal()}
  • *
  • {@link #translate(Object)}
  • *
  • {@link #unhand(Class, long)}
  • *
@@ -109,7 +105,7 @@ public class LibGraal { * Determines if libgraal is available for use. */ public static boolean isAvailable() { - return inLibGraal() || available; + return available; } /** @@ -119,24 +115,15 @@ public static boolean isSupported() { return getFailedSpeculationsAddress != null; } - /** - * Determines if the current runtime is libgraal. - */ - public static boolean inLibGraal() { - return Services.IS_IN_NATIVE_IMAGE; - } - /** * Links each native method in {@code clazz} to a {@link CEntryPoint} in libgraal. * - * This cannot be called from {@linkplain #inLibGraal() within} libgraal. * * @throws NullPointerException if {@code clazz == null} * @throws UnsupportedOperationException if libgraal is not enabled (i.e. * {@code -XX:-UseJVMCINativeLibrary}) * @throws IllegalArgumentException if {@code clazz} is {@link Class#isPrimitive()} - * @throws IllegalStateException if libgraal is {@linkplain #isAvailable() unavailable} or - * {@link #inLibGraal()} returns true + * @throws IllegalStateException if libgraal is {@linkplain #isAvailable() unavailable} * @throws UnsatisfiedLinkError if there's a problem linking a native method in {@code clazz} * (no matching JNI symbol or the native method is already linked to a different * address) @@ -145,7 +132,7 @@ public static void registerNativeMethods(Class clazz) { if (clazz.isPrimitive()) { throw new IllegalArgumentException(); } - if (inLibGraal() || !isAvailable()) { + if (!isAvailable()) { throw new IllegalStateException(); } try { diff --git a/truffle/src/com.oracle.truffle.runtime/src/com/oracle/truffle/runtime/hotspot/libgraal/LibGraalScope.java b/truffle/src/com.oracle.truffle.runtime/src/com/oracle/truffle/runtime/hotspot/libgraal/LibGraalScope.java index 4aec3af5481b..995bdd7ccab1 100644 --- a/truffle/src/com.oracle.truffle.runtime/src/com/oracle/truffle/runtime/hotspot/libgraal/LibGraalScope.java +++ b/truffle/src/com.oracle.truffle.runtime/src/com/oracle/truffle/runtime/hotspot/libgraal/LibGraalScope.java @@ -150,18 +150,15 @@ public LibGraalScope() { /** * Enters a scope for making calls into libgraal. If there is no existing libgraal scope for the - * current thread, the current thread is attached to libgraal. When the outer most scope is + * current thread, the current thread is attached to libgraal. When the outermost scope is * closed, the current thread is detached from libgraal. * * This must be used in a try-with-resources statement. * - * This cannot be called from {@linkplain LibGraal#inLibGraal() within} libgraal. - * * @throws IllegalStateException if libgraal is {@linkplain LibGraal#isAvailable() unavailable} - * or {@link LibGraal#inLibGraal()} returns true */ public LibGraalScope(DetachAction detachAction) { - if (LibGraal.inLibGraal() || !LibGraal.isAvailable()) { + if (!LibGraal.isAvailable()) { throw new IllegalStateException(); } id = nextId.getAndIncrement();