1 /*
  2  * Copyright Amazon.com Inc. 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 /*
 26  * @test
 27  * @bug 8307990
 28  * @requires (os.family == "linux") | (os.family == "aix")
 29  * @requires vm.debug
 30  * @library /test/lib
 31  * @run main/othervm/timeout=300 JspawnhelperProtocol
 32  */
 33 
 34 import java.io.BufferedReader;
 35 import java.io.IOException;
 36 import java.nio.file.Path;
 37 import java.util.Optional;
 38 import java.util.concurrent.TimeUnit;
 39 import java.util.concurrent.TimeoutException;
 40 
 41 import jdk.test.lib.process.ProcessTools;
 42 
 43 public class JspawnhelperProtocol {
 44     // Timout in seconds
 45     private static final int TIMEOUT = 60;
 46     // Base error code to communicate various error states from the parent process to the top-level test
 47     private static final int ERROR = 10;
 48     private static final String[] CMD = { "pwd" };
 49     private static final String ENV_KEY = "JTREG_JSPAWNHELPER_PROTOCOL_TEST";
 50 
 51     private static void parentCode(String arg) throws IOException, InterruptedException {
 52         System.out.println("Recursively executing 'JspawnhelperProtocol " + arg + "'");
 53         Process p = null;
 54         try {
 55             p = Runtime.getRuntime().exec(CMD);
 56         } catch (Exception e) {
 57             e.printStackTrace(System.out);
 58             System.exit(ERROR);
 59         }
 60         if (!p.waitFor(TIMEOUT, TimeUnit.SECONDS)) {
 61             System.out.println("Child process timed out");
 62             System.exit(ERROR + 1);
 63         }
 64         if (p.exitValue() == 0) {
 65             String pwd = p.inputReader().readLine();
 66             String realPwd = Path.of("").toAbsolutePath().toString();
 67             if (!realPwd.equals(pwd)) {
 68                 System.out.println("Child process returned '" + pwd + "' (expected '" + realPwd + "')");
 69                 System.exit(ERROR + 2);
 70             }
 71             System.out.println("  Successfully executed '" + CMD[0] + "'");
 72             System.exit(0);
 73         } else {
 74             System.out.println("  Failed to executed '" + CMD[0] + "' (exitValue=" + p.exitValue() + ")");
 75             System.exit(ERROR + 3);
 76         }
 77     }
 78 
 79     private static void normalExec() throws Exception {
 80         ProcessBuilder pb;
 81         pb = ProcessTools.createLimitedTestJavaProcessBuilder("-Djdk.lang.Process.launchMechanism=posix_spawn",
 82                                                               "JspawnhelperProtocol",
 83                                                               "normalExec");
 84         pb.inheritIO();
 85         Process p = pb.start();
 86         if (!p.waitFor(TIMEOUT, TimeUnit.SECONDS)) {
 87             throw new Exception("Parent process timed out");
 88         }
 89         if (p.exitValue() != 0) {
 90             throw new Exception("Parent process exited with " + p.exitValue());
 91         }
 92     }
 93 
 94     private static void simulateCrashInChild(int stage) throws Exception {
 95         ProcessBuilder pb;
 96         pb = ProcessTools.createLimitedTestJavaProcessBuilder("-Djdk.lang.Process.launchMechanism=posix_spawn",
 97                                                               "JspawnhelperProtocol",
 98                                                               "simulateCrashInChild" + stage);
 99         pb.environment().put(ENV_KEY, Integer.toString(stage));
100         Process p = pb.start();
101 
102         boolean foundCrashInfo = false;
103         try (BufferedReader br = p.inputReader()) {
104             String line = br.readLine();
105             while (line != null) {
106                 System.out.println(line);
107                 if (line.equals("posix_spawn:0")) {
108                     foundCrashInfo = true;
109                 }
110                 line = br.readLine();
111             }
112         }
113         if (!foundCrashInfo) {
114             throw new Exception("Wrong output from child process");
115         }
116         if (!p.waitFor(TIMEOUT, TimeUnit.SECONDS)) {
117             throw new Exception("Parent process timed out");
118         }
119 
120         int ret = p.exitValue();
121         if (ret == 0) {
122             throw new Exception("Expected error during child execution");
123         }
124         System.out.println("Parent exit code: " + ret);
125     }
126 
127     private static void simulateCrashInParent(int stage) throws Exception {
128         ProcessBuilder pb;
129         pb = ProcessTools.createLimitedTestJavaProcessBuilder("-Djdk.lang.Process.launchMechanism=posix_spawn",
130                                                               "JspawnhelperProtocol",
131                                                               "simulateCrashInParent" + stage);
132         pb.environment().put(ENV_KEY, Integer.toString(stage));
133         Process p = pb.start();
134 
135         String line = null;
136         try (BufferedReader br = p.inputReader()) {
137             line = br.readLine();
138             while (line != null && !line.startsWith("posix_spawn:")) {
139                 System.out.println(line);
140                 line = br.readLine();
141             }
142         }
143         if (line == null) {
144             throw new Exception("Wrong output from parent process");
145         }
146         System.out.println(line);
147         long childPid = Integer.parseInt(line.substring(line.indexOf(':') + 1));
148 
149         if (!p.waitFor(TIMEOUT, TimeUnit.SECONDS)) {
150             throw new Exception("Parent process timed out");
151         }
152 
153         Optional<ProcessHandle> oph = ProcessHandle.of(childPid);
154         if (!oph.isEmpty()) {
155             ProcessHandle ph = oph.get();
156             try {
157                 // Give jspawnhelper a chance to exit gracefully
158                 ph.onExit().get(TIMEOUT, TimeUnit.SECONDS);
159             } catch (TimeoutException te) {
160                 Optional<String> cmd = ph.info().command();
161                 if (cmd.isPresent() && cmd.get().endsWith("jspawnhelper")) {
162                     throw new Exception("jspawnhelper still alive after parent Java process terminated");
163                 }
164             }
165         }
166         int ret = p.exitValue();
167         if (ret != stage) {
168             throw new Exception("Expected exit code " + stage + " but got " + ret);
169         }
170         System.out.println("Parent exit code: " + ret);
171     }
172 
173     private static void simulateTruncatedWriteInParent(int stage) throws Exception {
174         ProcessBuilder pb;
175         pb = ProcessTools.createLimitedTestJavaProcessBuilder("-Djdk.lang.Process.launchMechanism=posix_spawn",
176                                                               "JspawnhelperProtocol",
177                                                               "simulateTruncatedWriteInParent" + stage);
178         pb.environment().put(ENV_KEY, Integer.toString(stage));
179         Process p = pb.start();
180 
181         BufferedReader br = p.inputReader();
182         String line = br.readLine();
183         while (line != null && !line.startsWith("posix_spawn:")) {
184             System.out.println(line);
185             line = br.readLine();
186         }
187         if (line == null) {
188             throw new Exception("Wrong output from parent process");
189         }
190         System.out.println(line);
191 
192         if (!p.waitFor(TIMEOUT, TimeUnit.SECONDS)) {
193             throw new Exception("Parent process timed out");
194         }
195         line = br.readLine();
196         while (line != null) {
197             System.out.println(line);
198             line = br.readLine();
199         }
200 
201         int ret = p.exitValue();
202         if (ret != ERROR) {
203             throw new Exception("Expected exit code " + ERROR + " but got " + ret);
204         }
205         System.out.println("Parent exit code: " + ret);
206     }
207 
208     public static void main(String[] args) throws Exception {
209         // This test works as follows:
210         //  - jtreg executes the test class `JspawnhelperProtocol` without arguments.
211         //    This is the initial "grandparent" process.
212         //  - For each sub-test (i.e. `normalExec()`, `simulateCrashInParent()` and
213         //    `simulateCrashInChild()`), a new sub-process (called the "parent") will be
214         //    forked which executes `JspawnhelperProtocol` recursively with a corresponding
215         //    command line argument.
216         //  - The forked `JspawnhelperProtocol` process (i.e. the "parent") runs
217         //    `JspawnhelperProtocol::parentCode()` which forks off yet another sub-process
218         //    (called the "child").
219         //  - The sub-tests in the "grandparent" check that various abnormal program
220         //    terminations in the "parent" or the "child" process are handled gracefully and
221         //    don't lead to deadlocks or zombie processes.
222         if (args.length > 0) {
223             // Entry point for recursive execution in the "parent" process
224             parentCode(args[0]);
225         } else {
226             // Main test entry for execution from jtreg
227             normalExec();
228             simulateCrashInParent(1);
229             simulateCrashInParent(2);
230             simulateCrashInParent(3);
231             simulateCrashInChild(4);
232             simulateCrashInChild(5);
233             simulateCrashInChild(6);
234             simulateTruncatedWriteInParent(99);
235         }
236     }
237 }