Skip to content

Commit 99d2bbf

Browse files
committed
8334433: jshell.exe runs an executable test.exe on startup
Reviewed-by: jpai
1 parent 6f4ddc2 commit 99d2bbf

File tree

3 files changed

+103
-2
lines changed

3 files changed

+103
-2
lines changed

src/jdk.internal.le/share/classes/jdk/internal/org/jline/utils/OSUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private static boolean isExecutable(File f) {
6363
String sttyfopt = null;
6464
String infocmp = null;
6565
String test = null;
66-
String path = System.getenv("PATH");
66+
String path = "/usr/bin" + File.pathSeparator + "/bin";//was: System.getenv("PATH");
6767
if (path != null) {
6868
String[] paths = path.split(File.pathSeparator);
6969
for (String p : paths) {

src/jdk.jshell/share/classes/jdk/internal/jshell/tool/ConsoleIOContext.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,12 @@ public int readBuffered(byte[] b) throws IOException {
154154
setupReader = setupReader.andThen(r -> r.option(Option.DISABLE_HIGHLIGHTER, !enableHighlighter));
155155
input.setInputStream(cmdin);
156156
} else {
157-
terminal = TerminalBuilder.builder().inputStreamWrapper(in -> {
157+
//on platforms which are known to be fully supported by
158+
//the FFMTerminalProvider, do not permit the ExecTerminalProvider:
159+
boolean allowExecTerminal = !OSUtils.IS_WINDOWS &&
160+
!OSUtils.IS_LINUX &&
161+
!OSUtils.IS_OSX;
162+
terminal = TerminalBuilder.builder().exec(allowExecTerminal).inputStreamWrapper(in -> {
158163
input.setInputStream(in);
159164
return nonBlockingInput;
160165
}).nativeSignals(false).build();
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8334433
27+
* @summary Verify that when running JShell on platforms that support FFMTerminalProvider,
28+
* no new processes are spawned.
29+
* @requires os.family == "windows" | os.family == "mac" | os.family == "linux"
30+
* @library /tools/lib
31+
* @modules jdk.compiler/com.sun.tools.javac.api
32+
* jdk.compiler/com.sun.tools.javac.main
33+
* jdk.compiler/com.sun.tools.javac.util
34+
* @build toolbox.ToolBox toolbox.JavaTask TerminalNoExecTest
35+
* @run main TerminalNoExecTest
36+
*/
37+
38+
import java.io.Writer;
39+
import java.nio.file.Files;
40+
import java.nio.file.Path;
41+
import java.nio.file.Paths;
42+
import java.util.concurrent.TimeUnit;
43+
import java.util.concurrent.atomic.AtomicBoolean;
44+
import jdk.jfr.consumer.RecordingStream;
45+
import jdk.jshell.tool.JavaShellToolBuilder;
46+
47+
import toolbox.ToolBox;
48+
49+
public class TerminalNoExecTest {
50+
51+
public static void main(String... args) throws Exception {
52+
if (args.length > 0) {
53+
AtomicBoolean spawnedNewProcess = new AtomicBoolean();
54+
try (var rs = new RecordingStream()) {
55+
rs.enable("jdk.ProcessStart").withoutThreshold();
56+
rs.onEvent(evt -> {
57+
System.err.println("evt: " + evt);
58+
spawnedNewProcess.set(true);
59+
});
60+
rs.startAsync();
61+
JavaShellToolBuilder.builder().run("--execution=local", "--no-startup");
62+
rs.stop();
63+
}
64+
if (spawnedNewProcess.get()) {
65+
System.err.println("Spawned a new process!");
66+
System.exit(1);
67+
}
68+
System.exit(0);
69+
} else {
70+
Path testScript = Paths.get("do-exit");
71+
try (Writer w = Files.newBufferedWriter(testScript)) {
72+
w.append("/exit\n");
73+
}
74+
75+
ToolBox tb = new ToolBox();
76+
Process target =
77+
new ProcessBuilder(tb.getJDKTool("java").toString(),
78+
"-classpath", System.getProperty("java.class.path"),
79+
TerminalNoExecTest.class.getName(),
80+
"run-test")
81+
.redirectError(ProcessBuilder.Redirect.INHERIT)
82+
.redirectOutput(ProcessBuilder.Redirect.INHERIT)
83+
.redirectInput(testScript.toFile())
84+
.start();
85+
86+
target.waitFor();
87+
88+
int exitCode = target.exitValue();
89+
90+
if (exitCode != 0) {
91+
throw new AssertionError("Incorrect exit value, expected 0, got: " + exitCode);
92+
}
93+
}
94+
}
95+
96+
}

0 commit comments

Comments
 (0)