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 /* Start the VM and let it crash. Specify TestUnresponsiveErrorHandler which will 53 * let five subsequent error reporting steps hang. The Timeout handling triggered 54 * by the WatcherThread should kick in and interrupt those steps. In theory, the 55 * text "timeout occurred during error reporting in step .." (the little timeouts) 56 * should occur in the error log up to four times, followed by the final big timeout 57 * "------ Timeout during error reporting after xx s. ------" 58 * 59 * Note that there are a number of uncertainties which make writing a 100% foolproof 60 * test challenging. The time the error reporting thread takes to react to the 61 * timeout triggers is unknown. So it is difficult to predict how many little timeouts 62 * will be visible before the big timeout kicks in. Also, once the big timeout hits, 63 * error reporting thread and Watcherthread will race. The former writes his last 64 * message to the error logs and flushes, the latter waits 200ms and then exits the 65 * process without further synchronization with the error reporting thread. 66 * 67 * Because of all this and the desire to write a bullet proof test which does 68 * not fail sporadically, we will not test for the final timeout message nor for all 69 * of the optimally expected little timeout messages. We just test for two of the 70 * little timeout messages to see that repeated timeout handling is basically working. 71 */ 72 73 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder( 74 "-XX:+UnlockDiagnosticVMOptions", 75 "-Xmx100M", 76 "-XX:ErrorHandlerTest=14", 77 "-XX:+TestUnresponsiveErrorHandler", 78 "-XX:ErrorLogTimeout=" + ERROR_LOG_TIMEOUT, 79 "-XX:-CreateCoredumpOnCrash", 80 "-version"); 81 82 OutputAnalyzer output_detail = new OutputAnalyzer(pb.start()); 83 84 if (verbose) { 85 System.err.println("<begin cmd output>"); 86 System.err.println(output_detail.getOutput()); 87 System.err.println("<end cmd output>"); 88 } 89 90 // we should have crashed with a SIGSEGV 91 output_detail.shouldMatch("# A fatal error has been detected by the Java Runtime Environment:.*"); 92 output_detail.shouldMatch("# +(?:SIGSEGV|SIGBUS|EXCEPTION_ACCESS_VIOLATION).*"); 93 94 // VM should have been aborted by WatcherThread 95 output_detail.shouldMatch(".*timer expired, abort.*"); 96 97 // extract hs-err file 98 String hs_err_file = output_detail.firstMatch("# *(\\S*hs_err_pid\\d+\\.log)", 1); 99 if (hs_err_file == null) { 100 if (!verbose) { 101 System.err.println("<begin cmd output>"); 102 System.err.println(output_detail.getOutput()); 103 System.err.println("<end cmd output>"); 104 } 105 throw new RuntimeException("Did not find hs-err file in output.\n"); 106 } 107 108 File f = new File(hs_err_file); 109 if (!f.exists()) { 110 if (!verbose) { 111 System.err.println("<begin cmd output>"); 112 System.err.println(output_detail.getOutput()); 113 System.err.println("<end cmd output>"); 114 } 115 throw new RuntimeException("hs-err file missing at " 116 + f.getAbsolutePath() + ".\n"); 117 } 118 119 System.out.println("Found hs_err file. Scanning..."); 120 121 FileInputStream fis = new FileInputStream(f); 122 BufferedReader br = new BufferedReader(new InputStreamReader(fis)); 123 String line = null; 124 125 126 Pattern [] pattern = new Pattern[] { 127 Pattern.compile(".*timeout occurred during error reporting in step.*"), 128 Pattern.compile(".*timeout occurred during error reporting in step.*") 129 }; 130 int currentPattern = 0; 131 132 String lastLine = null; 133 StringBuilder saved_hs_err = new StringBuilder(); 134 while ((line = br.readLine()) != null) { 135 saved_hs_err.append(line + System.lineSeparator()); 136 if (currentPattern < pattern.length) { 137 if (pattern[currentPattern].matcher(line).matches()) { 138 System.out.println("Found: " + line + "."); 139 currentPattern ++; 140 } 141 } 142 lastLine = line; 143 } 144 br.close(); 145 146 if (verbose) { 147 System.err.println("<begin hs_err contents>"); 148 System.err.print(saved_hs_err); 149 System.err.println("<end hs_err contents>"); 150 } 151 152 if (currentPattern < pattern.length) { 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 throw new RuntimeException("hs-err file incomplete (first missing pattern: " + currentPattern + ")"); 159 } 160 161 System.out.println("OK."); 162 163 } 164 165 }