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         "-XX:-CreateCoredumpOnCrash",
  34         "-version");
  35 
  36     OutputAnalyzer output_detail = new OutputAnalyzer(pb.start());
  37 
  38     // we should have crashed with a SIGSEGV
  39     output_detail.shouldMatch("# A fatal error has been detected by the Java Runtime Environment:.*");
  40     output_detail.shouldMatch("# +(?:SIGSEGV|EXCEPTION_ACCESS_VIOLATION).*");
  41 
  42     // extract hs-err file
  43     String hs_err_file = output_detail.firstMatch("# *(\\S*hs_err_pid\\d+\\.log)", 1);
  44     if (hs_err_file == null) {
  45       throw new RuntimeException("Did not find hs-err file in output.\n");
  46     }
  47 
  48     File f = new File(hs_err_file);
  49     if (!f.exists()) {
  50       throw new RuntimeException("hs-err file missing at "
  51           + f.getAbsolutePath() + ".\n");
  52     }
  53 
  54     System.out.println("Found hs_err file. Scanning...");
  55 
  56     FileInputStream fis = new FileInputStream(f);
  57     BufferedReader br = new BufferedReader(new InputStreamReader(fis));
  58     String line = null;
  59 
  60     Pattern [] pattern = new Pattern[] {
  61         Pattern.compile("Will test SafeFetch..."),
  62         Pattern.compile("SafeFetch OK."),
  63     };
  64     int currentPattern = 0;
  65 
  66     String lastLine = null;
  67     while ((line = br.readLine()) != null) {
  68       if (currentPattern < pattern.length) {
  69         if (pattern[currentPattern].matcher(line).matches()) {
  70           System.out.println("Found: " + line + ".");
  71           currentPattern ++;
  72         }
  73       }
  74       lastLine = line;
  75     }
  76     br.close();
  77 
  78     if (currentPattern < pattern.length) {
  79       throw new RuntimeException("hs-err file incomplete (first missing pattern: " +  currentPattern + ")");
  80     }
  81 
  82     if (!lastLine.equals("END.")) {
  83       throw new RuntimeException("hs-err file incomplete (missing END marker.)");
  84     } else {
  85       System.out.println("End marker found.");
  86     }
  87 
  88     System.out.println("OK.");
  89 
  90   }
  91 
  92 }
  93