Skip to content

Commit

Permalink
[GR-60667] Backport to 24.2: Failing weekly-vm-ee-truffle-maven-isola…
Browse files Browse the repository at this point in the history
…te-jdklatest-darwin-amd64 gate.

PullRequest: graal/19678
  • Loading branch information
tzezula authored and ansalond committed Dec 21, 2024
2 parents 79d661f + a106811 commit ac15c4c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@
* static analysis starts, i.e., in a {@link Feature#beforeAnalysis} method.
*/
public final class SystemInOutErrSupport implements InitialLayerOnlyImageSingleton {
private InputStream in = new BufferedInputStream(new FileInputStream(FileDescriptor.in));
private final InputStream initialIn = new BufferedInputStream(new FileInputStream(FileDescriptor.in));
private InputStream in = initialIn;
private PrintStream out = newPrintStream(new FileOutputStream(FileDescriptor.out), System.getProperty("sun.stdout.encoding"));
private PrintStream err = newPrintStream(new FileOutputStream(FileDescriptor.err), System.getProperty("sun.stderr.encoding"));
private final PrintStream initialErr = newPrintStream(new FileOutputStream(FileDescriptor.err), System.getProperty("sun.stderr.encoding"));
private PrintStream err = initialErr;

@Platforms(Platform.HOSTED_ONLY.class) //
final AtomicBoolean isSealed = new AtomicBoolean(false);
Expand Down Expand Up @@ -123,6 +125,18 @@ public PrintStream err() {
return err;
}

@Fold
public InputStream initialIn() {
seal();
return initialIn;
}

@Fold
public PrintStream initialErr() {
seal();
return initialErr;
}

@Platforms(Platform.HOSTED_ONLY.class)
public static void setErr(PrintStream err) {
var support = singleton();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@

import java.io.InputStream;
import java.io.PrintStream;
import java.lang.reflect.Field;

import com.oracle.svm.core.util.VMError;
import com.oracle.svm.util.ReflectionUtil;
import jdk.internal.access.SharedSecrets;
import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.hosted.Feature;

Expand All @@ -51,19 +55,36 @@ public class SystemInOutErrFeature implements InternalFeature, FeatureSingleton
private final InputStream hostedIn;
private final PrintStream hostedOut;
private final PrintStream hostedErr;
private final InputStream hostedInitialIn;
private final PrintStream hostedInitialErr;

public SystemInOutErrFeature() {
hostedIn = System.in;
NativeImageSystemIOWrappers wrappers = NativeImageSystemIOWrappers.singleton();
hostedOut = wrappers.outWrapper;
hostedErr = wrappers.errWrapper;
hostedInitialIn = SharedSecrets.getJavaLangAccess().initialSystemIn();
/*
* GR-55515: Migrate to JavaLangAccess#initialSystemErr(). The method
* JavaLangAccess#initialSystemErr() and the System#initialErr field were both introduced in
* JDK 23. Once JDK 21 compatibility is no longer required, consider switching to
* SharedSecrets.getJavaLangAccess().initialSystemErr().
*/
Field initialErrField = ReflectionUtil.lookupField(true, System.class, "initialErr");
try {
hostedInitialErr = initialErrField != null ? (PrintStream) initialErrField.get(null) : null;
} catch (IllegalAccessException illegalAccess) {
throw VMError.shouldNotReachHere(illegalAccess);
}
}

private SystemInOutErrSupport runtime;

private static final String SYSTEM_IN_KEY_NAME = "System#in";
private static final String SYSTEM_ERR_KEY_NAME = "System#err";
private static final String SYSTEM_OUT_KEY_NAME = "System#out";
private static final String SYSTEM_INITIAL_IN_KEY_NAME = "System#initialIn";
private static final String SYSTEM_INITIAL_ERR_KEY_NAME = "System#initialErr";

@Override
public void afterRegistration(AfterRegistrationAccess access) {
Expand All @@ -82,6 +103,8 @@ public void duringSetup(DuringSetupAccess access) {
registry.registerHeapConstant(SYSTEM_IN_KEY_NAME, runtime.in());
registry.registerHeapConstant(SYSTEM_OUT_KEY_NAME, runtime.out());
registry.registerHeapConstant(SYSTEM_ERR_KEY_NAME, runtime.err());
registry.registerHeapConstant(SYSTEM_INITIAL_IN_KEY_NAME, runtime.initialIn());
registry.registerHeapConstant(SYSTEM_INITIAL_ERR_KEY_NAME, runtime.initialErr());
}
access.registerObjectReplacer(this::replaceStreamsWithRuntimeObject);
} else {
Expand All @@ -102,6 +125,10 @@ Object replaceStreamsWithRuntimeObject(Object object) {
return runtime.out();
} else if (object == hostedErr) {
return runtime.err();
} else if (object == hostedInitialErr) {
return runtime.initialErr();
} else if (object == hostedInitialIn) {
return runtime.initialIn();
} else {
return object;
}
Expand All @@ -114,6 +141,10 @@ ImageHeapConstant replaceStreamsWithLayerConstant(CrossLayerConstantRegistry reg
return registry.getConstant(SYSTEM_OUT_KEY_NAME);
} else if (object == hostedErr) {
return registry.getConstant(SYSTEM_ERR_KEY_NAME);
} else if (object == hostedInitialErr) {
return registry.getConstant(SYSTEM_INITIAL_ERR_KEY_NAME);
} else if (object == hostedInitialIn) {
return registry.getConstant(SYSTEM_INITIAL_IN_KEY_NAME);
} else {
return null;
}
Expand Down

0 comments on commit ac15c4c

Please sign in to comment.