1 import java.io.BufferedReader;
   2 import java.io.File;
   3 import java.io.FileInputStream;
   4 import java.io.InputStreamReader;
   5 import java.util.regex.Pattern;
   6 
   7 import com.oracle.java.testlibrary.OutputAnalyzer;
   8 import com.oracle.java.testlibrary.Platform;
   9 import com.oracle.java.testlibrary.ProcessTools;
  10 
  11 /*
  12  * @test
  13  * @bug 8074552
  14  * @summary SafeFetch32 and SafeFetchN do not work in error handling
  15  * @library /testlibrary
  16  * @author Thomas Stuefe (SAP)
  17  */
  18 
  19 public class SafeFetchInErrorHandlingTest {
  20 
  21 
  22   public static void main(String[] args) throws Exception {
  23 
  24     if (!Platform.isDebugBuild()) {
  25       return;
  26     }
  27 
  28     ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  29         "-XX:+UnlockDiagnosticVMOptions",
  30         "-Xmx100M",
  31         "-XX:ErrorHandlerTest=14",
  32         "-XX:+TestSafeFetchInErrorHandler",
  33         "-version");
  34 
  35     OutputAnalyzer output_detail = new OutputAnalyzer(pb.start());
  36 
  37     // we should have crashed with a SIGSEGV
  38     output_detail.shouldMatch("# A fatal error has been detected by the Java Runtime Environment:.*");
  39     output_detail.shouldMatch("# +(?:SIGSEGV|EXCEPTION_ACCESS_VIOLATION).*");
  40 
  41     // extract hs-err file
  42     String hs_err_file = output_detail.firstMatch("# *(\\S*hs_err_pid\\d+\\.log)", 1);
  43     if (hs_err_file == null) {
  44       throw new RuntimeException("Did not find hs-err file in output.\n");
  45     }
  46 
  47     File f = new File(hs_err_file);
  48     if (!f.exists()) {
  49       throw new RuntimeException("hs-err file missing at "
  50           + f.getAbsolutePath() + ".\n");
  51     }
  52 
  53     System.out.println("Found hs_err file. Scanning...");
  54 
  55     FileInputStream fis = new FileInputStream(f);
  56     BufferedReader br = new BufferedReader(new InputStreamReader(fis));
  57     String line = null;
  58 
  59     Pattern [] pattern = new Pattern[] {
  60         Pattern.compile("Will test SafeFetch..."),
  61         Pattern.compile("SafeFetch OK."),
  62     };
  63     int currentPattern = 0;
  64 
  65     String lastLine = null;
  66     while ((line = br.readLine()) != null) {
  67       if (currentPattern < pattern.length) {
  68         if (pattern[currentPattern].matcher(line).matches()) {
  69           System.out.println("Found: " + line + ".");
  70           currentPattern ++;
  71         }
  72       }
  73       lastLine = line;
  74     }
  75     br.close();
  76 
  77     if (currentPattern < pattern.length) {
  78       throw new RuntimeException("hs-err file incomplete (first missing pattern: " +  currentPattern + ")");
  79     }
  80 
  81     if (!lastLine.equals("END.")) {
  82       throw new RuntimeException("hs-err file incomplete (missing END marker.)");
  83     } else {
  84       System.out.println("End marker found.");
  85     }
  86 
  87     System.out.println("OK.");
  88 
  89   }
  90 
  91 }
  92