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