1 /*
   2  * Copyright (c) 2018, 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.Platform;
  31 import jdk.test.lib.process.OutputAnalyzer;
  32 import jdk.test.lib.process.ProcessTools;
  33 
  34 /*
  35  * @test
  36  * @bug 8194652
  37  * @summary Printing native stack shows an "error occurred during error reporting".
  38  * @modules java.base/jdk.internal.misc
  39  * @library /test/lib
  40  */
  41 
  42 // This test was adapted from SafeFetchInErrorHandlingTest.java.
  43 public class BadNativeStackInErrorHandlingTest {
  44   public static void main(String[] args) throws Exception {
  45     if (!Platform.isDebugBuild() || Platform.isZero()) {
  46       return;
  47     }
  48 
  49     ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  50         "-XX:+UnlockDiagnosticVMOptions",
  51         "-Xmx100M",
  52         "-XX:ErrorHandlerTest=14",
  53         "-XX:-CreateCoredumpOnCrash",
  54         "-version");
  55 
  56     OutputAnalyzer output_detail = new OutputAnalyzer(pb.start());
  57 
  58     // we should have crashed with a SIGSEGV
  59     output_detail.shouldMatch("# A fatal error has been detected by the Java Runtime Environment:.*");
  60     output_detail.shouldMatch("# +(?:SIGSEGV|EXCEPTION_ACCESS_VIOLATION).*");
  61 
  62     // extract hs-err file
  63     String hs_err_file = output_detail.firstMatch("# *(\\S*hs_err_pid\\d+\\.log)", 1);
  64     if (hs_err_file == null) {
  65         throw new RuntimeException("Did not find hs-err file in output.\n");
  66     }
  67 
  68     File f = new File(hs_err_file);
  69     if (!f.exists()) {
  70         throw new RuntimeException("hs-err file missing at " +
  71                                    f.getAbsolutePath() + ".\n");
  72     }
  73 
  74     System.out.println("Found hs_err file. Scanning...");
  75 
  76     FileInputStream fis = new FileInputStream(f);
  77     BufferedReader br = new BufferedReader(new InputStreamReader(fis));
  78     String line = null;
  79 
  80     // The failing line looks like this:
  81     // [error occurred during error reporting (printing native stack), id 0xb]
  82     Pattern pattern =
  83         Pattern.compile("\\[error occurred during error reporting \\(printing native stack\\), id .*\\]");
  84 
  85     String lastLine = null;
  86     while ((line = br.readLine()) != null) {
  87         if (pattern.matcher(line).matches()) {
  88             System.out.println("Found: " + line + ".");
  89             throw new RuntimeException("hs-err file should not contain: '" +
  90                                        pattern + "'");
  91         }
  92         lastLine = line;
  93     }
  94     br.close();
  95 
  96     if (!lastLine.equals("END.")) {
  97         throw new RuntimeException("hs-err file incomplete (missing END marker.)");
  98     } else {
  99         System.out.println("End marker found.");
 100     }
 101 
 102     System.out.println("OK.");
 103   }
 104 }