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  */
  35 
  36 import java.io.BufferedReader;
  37 import java.io.File;
  38 import java.io.FileInputStream;
  39 import java.io.InputStreamReader;
  40 import java.util.regex.Pattern;
  41 import java.util.Map;
  42 
  43 import jdk.test.lib.process.OutputAnalyzer;
  44 import jdk.test.lib.process.ProcessTools;
  45 
  46 public class VeryEarlyAssertTest {
  47 
  48   public static void main(String[] args) throws Exception {
  49 
  50 
  51     ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  52             "-version");
  53     Map<String, String> env = pb.environment();
  54     env.put("HOTSPOT_FATAL_ERROR_DURING_DYNAMIC_INITIALIZATION", "1");
  55 
  56     OutputAnalyzer output_detail = new OutputAnalyzer(pb.start());
  57 
  58     // we should have crashed with an assert with a specific message:
  59     output_detail.shouldMatch("# A fatal error has been detected by the Java Runtime Environment:.*");
  60     output_detail.shouldMatch("#.*HOTSPOT_FATAL_ERROR_DURING_DYNAMIC_INITIALIZATION.*");
  61 
  62     // extract hs-err file
  63     String hs_err_file = output_detail.firstMatch("# *(\\S*hs_err_pid\\d+\\.log)", 1);
  64     if (hs_err_file == null) {
  65       throw new RuntimeException("Did not find hs-err file in output.\n");
  66     }
  67 
  68     // scan hs-err file: File should contain the same assertion message. Other than that,
  69     // do not expect too much: file will be littered with secondary errors. The test
  70     // should test that we get a hs-err file at all.
  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(".*HOTSPOT_FATAL_ERROR_DURING_DYNAMIC_INITIALIZATION.*")
  85     };
  86     int currentPattern = 0;
  87 
  88     boolean endMarkerFound = false;
  89     while ((line = br.readLine()) != null) {
  90       if (currentPattern < pattern.length) {
  91         if (pattern[currentPattern].matcher(line).matches()) {
  92           System.out.println("Found: " + line + ".");
  93           currentPattern++;
  94         }
  95       }
  96       if (line.equals("END.")) {
  97         endMarkerFound = true;
  98         break;
  99       }
 100     }
 101     br.close();
 102 
 103     if (currentPattern < pattern.length) {
 104       throw new RuntimeException("hs-err file incomplete (first missing pattern: " + currentPattern + ")");
 105     }
 106 
 107     if (!endMarkerFound) {
 108       throw new RuntimeException("hs-err file incomplete (missing END marker.)");
 109     } else {
 110       System.out.println("End marker found.");
 111     }
 112 
 113     System.out.println("OK.");
 114 
 115   }
 116 
 117 }