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