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