1 /*
   2  * Copyright (c) 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 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 8074552
  37  * @summary SafeFetch32 and SafeFetchN do not work in error handling
  38  * @modules java.base/jdk.internal.misc
  39  * @library /test/lib
  40  * @author Thomas Stuefe (SAP)
  41  */
  42 
  43 public class SafeFetchInErrorHandlingTest {
  44 
  45 
  46   public static void main(String[] args) throws Exception {
  47 
  48     if (!Platform.isDebugBuild() || Platform.isZero()) {
  49       return;
  50     }
  51 
  52     ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  53         "-XX:+UnlockDiagnosticVMOptions",
  54         "-Xmx100M",
  55         "-XX:ErrorHandlerTest=14",
  56         "-XX:+TestSafeFetchInErrorHandler",
  57         "-XX:-CreateCoredumpOnCrash",
  58         "-version");
  59 
  60     OutputAnalyzer output_detail = new OutputAnalyzer(pb.start());
  61 
  62     // we should have crashed with a SIGSEGV
  63     output_detail.shouldMatch("# A fatal error has been detected by the Java Runtime Environment:.*");
  64     output_detail.shouldMatch("# +(?:SIGSEGV|EXCEPTION_ACCESS_VIOLATION).*");
  65 
  66     // extract hs-err file
  67     String hs_err_file = output_detail.firstMatch("# *(\\S*hs_err_pid\\d+\\.log)", 1);
  68     if (hs_err_file == null) {
  69       throw new RuntimeException("Did not find hs-err file in output.\n");
  70     }
  71 
  72     File f = new File(hs_err_file);
  73     if (!f.exists()) {
  74       throw new RuntimeException("hs-err file missing at "
  75           + f.getAbsolutePath() + ".\n");
  76     }
  77 
  78     System.out.println("Found hs_err file. Scanning...");
  79 
  80     FileInputStream fis = new FileInputStream(f);
  81     BufferedReader br = new BufferedReader(new InputStreamReader(fis));
  82     String line = null;
  83 
  84     Pattern [] pattern = new Pattern[] {
  85         Pattern.compile("Will test SafeFetch..."),
  86         Pattern.compile("SafeFetch OK."),
  87     };
  88     int currentPattern = 0;
  89 
  90     String lastLine = null;
  91     while ((line = br.readLine()) != null) {
  92       if (currentPattern < pattern.length) {
  93         if (pattern[currentPattern].matcher(line).matches()) {
  94           System.out.println("Found: " + line + ".");
  95           currentPattern ++;
  96         }
  97       }
  98       lastLine = line;
  99     }
 100     br.close();
 101 
 102     if (currentPattern < pattern.length) {
 103       throw new RuntimeException("hs-err file incomplete (first missing pattern: " +  currentPattern + ")");
 104     }
 105 
 106     if (!lastLine.equals("END.")) {
 107       throw new RuntimeException("hs-err file incomplete (missing END marker.)");
 108     } else {
 109       System.out.println("End marker found.");
 110     }
 111 
 112     System.out.println("OK.");
 113 
 114   }
 115 
 116 }
 117