1 /* 2 * Copyright (c) 2017, 2020, 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 import java.io.BufferedReader; 25 import java.io.File; 26 import java.io.FileInputStream; 27 import java.io.InputStreamReader; 28 import java.util.regex.Pattern; 29 30 import jdk.test.lib.process.OutputAnalyzer; 31 import jdk.test.lib.Platform; 32 import jdk.test.lib.process.ProcessTools; 33 34 /* 35 * @test 36 * @bug 8166944 37 * @summary Hanging Error Reporting steps may lead to torn error logs 38 * @modules java.base/jdk.internal.misc 39 * @library /test/lib 40 * @requires (vm.debug == true) & (os.family != "windows") 41 * @run driver TimeoutInErrorHandlingTest 42 * @author Thomas Stuefe (SAP) 43 */ 44 45 public class TimeoutInErrorHandlingTest { 46 47 public static final boolean verbose = System.getProperty("verbose") != null; 48 // 16 seconds for hs_err generation timeout = 4 seconds per step timeout 49 public static final int ERROR_LOG_TIMEOUT = 16; 50 51 public static void main(String[] args) throws Exception { 52 53 int error_log_timeout = ERROR_LOG_TIMEOUT; 54 if ("SunOS".equals(System.getProperty("os.name"))) { 55 // Give Solaris machines 3X as much time: 56 error_log_timeout *= 3; 57 } 58 59 /* Start the VM and let it crash. Specify TestUnresponsiveErrorHandler which will 60 * let five subsequent error reporting steps hang. The Timeout handling triggered 61 * by the WatcherThread should kick in and interrupt those steps. In theory, the 62 * text "timeout occurred during error reporting in step .." (the little timeouts) 63 * should occur in the error log up to four times, followed by the final big timeout 64 * "------ Timeout during error reporting after xx s. ------" 65 * 66 * Note that there are a number of uncertainties which make writing a 100% foolproof 67 * test challenging. The time the error reporting thread takes to react to the 68 * timeout triggers is unknown. So it is difficult to predict how many little timeouts 69 * will be visible before the big timeout kicks in. Also, once the big timeout hits, 70 * error reporting thread and Watcherthread will race. The former writes his last 71 * message to the error logs and flushes, the latter waits 200ms and then exits the 72 * process without further synchronization with the error reporting thread. 73 * 74 * Because of all this and the desire to write a bullet proof test which does 75 * not fail sporadically, we will not test for the final timeout message nor for all 76 * of the optimally expected little timeout messages. We just test for two of the 77 * little timeout messages to see that repeated timeout handling is basically working. 78 */ 79 80 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder( 81 "-XX:+UnlockDiagnosticVMOptions", 82 "-Xmx100M", 83 "-XX:ErrorHandlerTest=14", 84 "-XX:+TestUnresponsiveErrorHandler", 85 "-XX:ErrorLogTimeout=" + error_log_timeout, 86 "-XX:-CreateCoredumpOnCrash", 87 "-version"); 88 89 OutputAnalyzer output_detail = new OutputAnalyzer(pb.start()); 90 91 if (verbose) { 92 System.err.println("<begin cmd output>"); 93 System.err.println(output_detail.getOutput()); 94 System.err.println("<end cmd output>"); 95 } 96 97 // we should have crashed with a SIGSEGV 98 output_detail.shouldMatch("# A fatal error has been detected by the Java Runtime Environment:.*"); 99 output_detail.shouldMatch("# +(?:SIGSEGV|SIGBUS|EXCEPTION_ACCESS_VIOLATION).*"); 100 101 // VM should have been aborted by WatcherThread 102 output_detail.shouldMatch(".*timer expired, abort.*"); 103 104 // extract hs-err file 105 String hs_err_file = output_detail.firstMatch("# *(\\S*hs_err_pid\\d+\\.log)", 1); 106 if (hs_err_file == null) { 107 if (!verbose) { 108 System.err.println("<begin cmd output>"); 109 System.err.println(output_detail.getOutput()); 110 System.err.println("<end cmd output>"); 111 } 112 throw new RuntimeException("Did not find hs-err file in output.\n"); 113 } 114 115 File f = new File(hs_err_file); 116 if (!f.exists()) { 117 if (!verbose) { 118 System.err.println("<begin cmd output>"); 119 System.err.println(output_detail.getOutput()); 120 System.err.println("<end cmd output>"); 121 } 122 throw new RuntimeException("hs-err file missing at " 123 + f.getAbsolutePath() + ".\n"); 124 } 125 126 System.out.println("Found hs_err file. Scanning..."); 127 128 FileInputStream fis = new FileInputStream(f); 129 BufferedReader br = new BufferedReader(new InputStreamReader(fis)); 130 String line = null; 131 132 133 Pattern [] pattern = new Pattern[] { 134 Pattern.compile(".*timeout occurred during error reporting in step.*"), 135 Pattern.compile(".*timeout occurred during error reporting in step.*") 136 }; 137 int currentPattern = 0; 138 139 String lastLine = null; 140 StringBuilder saved_hs_err = new StringBuilder(); 141 while ((line = br.readLine()) != null) { 142 saved_hs_err.append(line + System.lineSeparator()); 143 if (currentPattern < pattern.length) { 144 if (pattern[currentPattern].matcher(line).matches()) { 145 System.out.println("Found: " + line + "."); 146 currentPattern ++; 147 } 148 } 149 lastLine = line; 150 } 151 br.close(); 152 153 if (verbose) { 154 System.err.println("<begin hs_err contents>"); 155 System.err.print(saved_hs_err); 156 System.err.println("<end hs_err contents>"); 157 } 158 159 if (currentPattern < pattern.length) { 160 if (!verbose) { 161 System.err.println("<begin hs_err contents>"); 162 System.err.print(saved_hs_err); 163 System.err.println("<end hs_err contents>"); 164 } 165 throw new RuntimeException("hs-err file incomplete (first missing pattern: " + currentPattern + ")"); 166 } 167 168 System.out.println("OK."); 169 170 } 171 172 }