Skip to content

Commit ac15c4c

Browse files
tzezulaansalond
authored andcommitted
[GR-60667] Backport to 24.2: Failing weekly-vm-ee-truffle-maven-isolate-jdklatest-darwin-amd64 gate.
PullRequest: graal/19678
2 parents 79d661f + a106811 commit ac15c4c

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/SystemInOutErrSupport.java

+16-2
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,11 @@
5959
* static analysis starts, i.e., in a {@link Feature#beforeAnalysis} method.
6060
*/
6161
public final class SystemInOutErrSupport implements InitialLayerOnlyImageSingleton {
62-
private InputStream in = new BufferedInputStream(new FileInputStream(FileDescriptor.in));
62+
private final InputStream initialIn = new BufferedInputStream(new FileInputStream(FileDescriptor.in));
63+
private InputStream in = initialIn;
6364
private PrintStream out = newPrintStream(new FileOutputStream(FileDescriptor.out), System.getProperty("sun.stdout.encoding"));
64-
private PrintStream err = newPrintStream(new FileOutputStream(FileDescriptor.err), System.getProperty("sun.stderr.encoding"));
65+
private final PrintStream initialErr = newPrintStream(new FileOutputStream(FileDescriptor.err), System.getProperty("sun.stderr.encoding"));
66+
private PrintStream err = initialErr;
6567

6668
@Platforms(Platform.HOSTED_ONLY.class) //
6769
final AtomicBoolean isSealed = new AtomicBoolean(false);
@@ -123,6 +125,18 @@ public PrintStream err() {
123125
return err;
124126
}
125127

128+
@Fold
129+
public InputStream initialIn() {
130+
seal();
131+
return initialIn;
132+
}
133+
134+
@Fold
135+
public PrintStream initialErr() {
136+
seal();
137+
return initialErr;
138+
}
139+
126140
@Platforms(Platform.HOSTED_ONLY.class)
127141
public static void setErr(PrintStream err) {
128142
var support = singleton();

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/SystemInOutErrFeature.java

+31
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727

2828
import java.io.InputStream;
2929
import java.io.PrintStream;
30+
import java.lang.reflect.Field;
3031

32+
import com.oracle.svm.core.util.VMError;
33+
import com.oracle.svm.util.ReflectionUtil;
34+
import jdk.internal.access.SharedSecrets;
3135
import org.graalvm.nativeimage.ImageSingletons;
3236
import org.graalvm.nativeimage.hosted.Feature;
3337

@@ -51,19 +55,36 @@ public class SystemInOutErrFeature implements InternalFeature, FeatureSingleton
5155
private final InputStream hostedIn;
5256
private final PrintStream hostedOut;
5357
private final PrintStream hostedErr;
58+
private final InputStream hostedInitialIn;
59+
private final PrintStream hostedInitialErr;
5460

5561
public SystemInOutErrFeature() {
5662
hostedIn = System.in;
5763
NativeImageSystemIOWrappers wrappers = NativeImageSystemIOWrappers.singleton();
5864
hostedOut = wrappers.outWrapper;
5965
hostedErr = wrappers.errWrapper;
66+
hostedInitialIn = SharedSecrets.getJavaLangAccess().initialSystemIn();
67+
/*
68+
* GR-55515: Migrate to JavaLangAccess#initialSystemErr(). The method
69+
* JavaLangAccess#initialSystemErr() and the System#initialErr field were both introduced in
70+
* JDK 23. Once JDK 21 compatibility is no longer required, consider switching to
71+
* SharedSecrets.getJavaLangAccess().initialSystemErr().
72+
*/
73+
Field initialErrField = ReflectionUtil.lookupField(true, System.class, "initialErr");
74+
try {
75+
hostedInitialErr = initialErrField != null ? (PrintStream) initialErrField.get(null) : null;
76+
} catch (IllegalAccessException illegalAccess) {
77+
throw VMError.shouldNotReachHere(illegalAccess);
78+
}
6079
}
6180

6281
private SystemInOutErrSupport runtime;
6382

6483
private static final String SYSTEM_IN_KEY_NAME = "System#in";
6584
private static final String SYSTEM_ERR_KEY_NAME = "System#err";
6685
private static final String SYSTEM_OUT_KEY_NAME = "System#out";
86+
private static final String SYSTEM_INITIAL_IN_KEY_NAME = "System#initialIn";
87+
private static final String SYSTEM_INITIAL_ERR_KEY_NAME = "System#initialErr";
6788

6889
@Override
6990
public void afterRegistration(AfterRegistrationAccess access) {
@@ -82,6 +103,8 @@ public void duringSetup(DuringSetupAccess access) {
82103
registry.registerHeapConstant(SYSTEM_IN_KEY_NAME, runtime.in());
83104
registry.registerHeapConstant(SYSTEM_OUT_KEY_NAME, runtime.out());
84105
registry.registerHeapConstant(SYSTEM_ERR_KEY_NAME, runtime.err());
106+
registry.registerHeapConstant(SYSTEM_INITIAL_IN_KEY_NAME, runtime.initialIn());
107+
registry.registerHeapConstant(SYSTEM_INITIAL_ERR_KEY_NAME, runtime.initialErr());
85108
}
86109
access.registerObjectReplacer(this::replaceStreamsWithRuntimeObject);
87110
} else {
@@ -102,6 +125,10 @@ Object replaceStreamsWithRuntimeObject(Object object) {
102125
return runtime.out();
103126
} else if (object == hostedErr) {
104127
return runtime.err();
128+
} else if (object == hostedInitialErr) {
129+
return runtime.initialErr();
130+
} else if (object == hostedInitialIn) {
131+
return runtime.initialIn();
105132
} else {
106133
return object;
107134
}
@@ -114,6 +141,10 @@ ImageHeapConstant replaceStreamsWithLayerConstant(CrossLayerConstantRegistry reg
114141
return registry.getConstant(SYSTEM_OUT_KEY_NAME);
115142
} else if (object == hostedErr) {
116143
return registry.getConstant(SYSTEM_ERR_KEY_NAME);
144+
} else if (object == hostedInitialErr) {
145+
return registry.getConstant(SYSTEM_INITIAL_ERR_KEY_NAME);
146+
} else if (object == hostedInitialIn) {
147+
return registry.getConstant(SYSTEM_INITIAL_IN_KEY_NAME);
117148
} else {
118149
return null;
119150
}

0 commit comments

Comments
 (0)