Skip to content

Commit

Permalink
[GR-58651] Enforce correct values for Services.IS_[IN|BUILDING]_NATIV…
Browse files Browse the repository at this point in the history
…E_IMAGE in image-runtime.

PullRequest: graal/18951
  • Loading branch information
olpaw committed Oct 9, 2024
2 parents 57f9a5e + 0fca74b commit c69a606
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,47 +25,58 @@
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;

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<String, String> 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<String, String> getSavedProperties() {
return SystemPropertiesSupport.singleton().getSavedProperties();
}

@Delete //
static Map<String, String> 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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
* <ul>
* <li>the {@linkplain #inLibGraal() current runtime} is libgraal, or</li>
* <li>the HotSpot {@code UseJVMCINativeLibrary} flag is true and the current runtime includes the
* relevant JVMCI API additions for accessing libgraal.</li>
* </ul>
* 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:
* <ul>
* <li>{@link #isAvailable()}</li>
* <li>{@link #inLibGraal()}</li>
* <li>{@link #translate(Object)}</li>
* <li>{@link #unhand(Class, long)}</li>
* </ul>
Expand Down Expand Up @@ -109,7 +105,7 @@ public class LibGraal {
* Determines if libgraal is available for use.
*/
public static boolean isAvailable() {
return inLibGraal() || available;
return available;
}

/**
Expand All @@ -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)
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit c69a606

Please sign in to comment.