1 /*
   2  * Copyright (c) 2013, 2015, 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 /*
  26  * @test
  27  * @bug 8065896
  28  * @summary Synchronous signals during error reporting may terminate or hang VM process
  29  * @library /testlibrary
  30  * @author Thomas Stuefe (SAP)
  31  * @modules java.base/jdk.internal.misc
  32  *          java.management
  33  */
  34 
  35 import java.io.BufferedReader;
  36 import java.io.File;
  37 import java.io.FileInputStream;
  38 import java.io.InputStreamReader;
  39 import java.util.regex.Pattern;
  40 
  41 import jdk.test.lib.OutputAnalyzer;
  42 import jdk.test.lib.Platform;
  43 import jdk.test.lib.ProcessTools;
  44 
  45 public class SecondaryErrorTest {
  46 
  47 
  48   public static void main(String[] args) throws Exception {
  49 
  50     // Do not execute for windows, nor for non-debug builds
  51     if (Platform.isWindows()) {
  52       return;
  53     }
  54 
  55     if (!Platform.isDebugBuild()) {
  56       return;
  57     }
  58 
  59     ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  60         "-XX:+UnlockDiagnosticVMOptions",
  61         "-Xmx100M",
  62         "-XX:-CreateCoredumpOnCrash",
  63         "-XX:ErrorHandlerTest=15",
  64         "-XX:TestCrashInErrorHandler=14",
  65         "-version");
  66 
  67     OutputAnalyzer output_detail = new OutputAnalyzer(pb.start());
  68 
  69     // we should have crashed with a SIGFPE
  70     output_detail.shouldMatch("# A fatal error has been detected by the Java Runtime Environment:.*");
  71     output_detail.shouldMatch("# +SIGFPE.*");
  72 
  73     // extract hs-err file
  74     String hs_err_file = output_detail.firstMatch("# *(\\S*hs_err_pid\\d+\\.log)", 1);
  75     if (hs_err_file == null) {
  76       throw new RuntimeException("Did not find hs-err file in output.\n");
  77     }
  78 
  79     // scan hs-err file: File should contain the "[error occurred during error reporting..]"
  80     // markers which show that the secondary error handling kicked in and handled the
  81     // error successfully. As an added test, we check that the last line contains "END.",
  82     // which is an end marker written in the last step and proves that hs-err file was
  83     // completely written.
  84     File f = new File(hs_err_file);
  85     if (!f.exists()) {
  86       throw new RuntimeException("hs-err file missing at "
  87           + f.getAbsolutePath() + ".\n");
  88     }
  89 
  90     System.out.println("Found hs_err file. Scanning...");
  91 
  92     FileInputStream fis = new FileInputStream(f);
  93     BufferedReader br = new BufferedReader(new InputStreamReader(fis));
  94     String line = null;
  95 
  96     Pattern [] pattern = new Pattern[] {
  97         Pattern.compile("Will crash now \\(TestCrashInErrorHandler=14\\)..."),
  98         Pattern.compile("\\[error occurred during error reporting \\(test secondary crash 1\\).*\\]"),
  99         Pattern.compile("Will crash now \\(TestCrashInErrorHandler=14\\)..."),
 100         Pattern.compile("\\[error occurred during error reporting \\(test secondary crash 2\\).*\\]"),
 101     };
 102     int currentPattern = 0;
 103 
 104     String lastLine = null;
 105     while ((line = br.readLine()) != null) {
 106       if (currentPattern < pattern.length) {
 107         if (pattern[currentPattern].matcher(line).matches()) {
 108           System.out.println("Found: " + line + ".");
 109           currentPattern ++;
 110         }
 111       }
 112       lastLine = line;
 113     }
 114     br.close();
 115 
 116     if (currentPattern < pattern.length) {
 117       throw new RuntimeException("hs-err file incomplete (first missing pattern: " +  currentPattern + ")");
 118     }
 119 
 120     if (!lastLine.equals("END.")) {
 121       throw new RuntimeException("hs-err file incomplete (missing END marker.)");
 122     } else {
 123       System.out.println("End marker found.");
 124     }
 125 
 126     System.out.println("OK.");
 127 
 128   }
 129 
 130 }
 131 
 132