1 /*
   2  * Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2018, SAP. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 
  26 /*
  27  * @test
  28  * @bug 8214975
  29  * @summary No hs-err file if fatal error is raised during dynamic initialization.
  30  * @library /test/lib
  31  * @modules java.base/jdk.internal.misc
  32  * @requires (vm.debug == true)
  33  * @requires os.family == "linux"
  34  * @run driver VeryEarlyAssertTest
  35  */
  36 
  37 import java.io.BufferedReader;
  38 import java.io.File;
  39 import java.io.FileInputStream;
  40 import java.io.InputStreamReader;
  41 import java.util.regex.Pattern;
  42 import java.util.Map;
  43 
  44 import jdk.test.lib.process.OutputAnalyzer;
  45 import jdk.test.lib.process.ProcessTools;
  46 
  47 public class VeryEarlyAssertTest {
  48 
  49   public static void main(String[] args) throws Exception {
  50 
  51 
  52     ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  53             "-version");
  54     Map<String, String> env = pb.environment();
  55     env.put("HOTSPOT_FATAL_ERROR_DURING_DYNAMIC_INITIALIZATION", "1");
  56 
  57     OutputAnalyzer output_detail = new OutputAnalyzer(pb.start());
  58 
  59     // we should have crashed with an assert with a specific message:
  60     output_detail.shouldMatch("# A fatal error has been detected by the Java Runtime Environment:.*");
  61     output_detail.shouldMatch("#.*HOTSPOT_FATAL_ERROR_DURING_DYNAMIC_INITIALIZATION.*");
  62 
  63     // extract hs-err file
  64     String hs_err_file = output_detail.firstMatch("# *(\\S*hs_err_pid\\d+\\.log)", 1);
  65     if (hs_err_file == null) {
  66       throw new RuntimeException("Did not find hs-err file in output.\n");
  67     }
  68 
  69     // scan hs-err file: File should contain the same assertion message. Other than that,
  70     // do not expect too much: file will be littered with secondary errors. The test
  71     // should test that we get a hs-err file at all.
  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(".*HOTSPOT_FATAL_ERROR_DURING_DYNAMIC_INITIALIZATION.*")
  86     };
  87     int currentPattern = 0;
  88 
  89     boolean endMarkerFound = false;
  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       if (line.equals("END.")) {
  98         endMarkerFound = true;
  99         break;
 100       }
 101     }
 102     br.close();
 103 
 104     if (currentPattern < pattern.length) {
 105       throw new RuntimeException("hs-err file incomplete (first missing pattern: " + currentPattern + ")");
 106     }
 107 
 108     if (!endMarkerFound) {
 109       throw new RuntimeException("hs-err file incomplete (missing END marker.)");
 110     } else {
 111       System.out.println("End marker found.");
 112     }
 113 
 114     System.out.println("OK.");
 115 
 116   }
 117 
 118 }