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