1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test TestCrashOnOutOfMemoryError
  26  * @summary Test using -XX:+CrashOnOutOfMemoryError
  27  * @modules java.base/jdk.internal.misc
  28  * @library /testlibrary
  29  * @build jdk.test.lib.*
  30  * @run driver TestCrashOnOutOfMemoryError
  31  * @bug 8138745
  32  */
  33 
  34 import jdk.test.lib.OutputAnalyzer;
  35 import jdk.test.lib.ProcessTools;
  36 import java.io.BufferedReader;
  37 import java.io.File;
  38 import java.io.FileInputStream;
  39 import java.io.InputStreamReader;
  40 import java.io.IOException;
  41 
  42 public class TestCrashOnOutOfMemoryError {
  43 
  44     public static void main(String[] args) throws Exception {
  45         if (args.length == 1) {
  46             // This should guarantee to throw:
  47             // java.lang.OutOfMemoryError: Requested array size exceeds VM limit
  48             try {
  49                 Object[] oa = new Object[Integer.MAX_VALUE];
  50                 throw new Error("OOME not triggered");
  51             } catch (OutOfMemoryError err) {
  52                 throw new Error("OOME didn't abort JVM!");
  53             }
  54         }
  55         // else this is the main test
  56         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+CrashOnOutOfMemoryError",
  57                  "-XX:-CreateCoredumpOnCrash", "-Xmx64m", TestCrashOnOutOfMemoryError.class.getName(),"throwOOME");
  58         OutputAnalyzer output = new OutputAnalyzer(pb.start());
  59         int exitValue = output.getExitValue();
  60         if (0 == exitValue) {
  61             //expecting a non zero value
  62             throw new Error("Expected to get non zero exit value");
  63         }
  64 
  65         /* Output should look something like this. The actual text will depend on the OS and its core dump processing.
  66            Aborting due to java.lang.OutOfMemoryError: Requested array size exceeds VM limit
  67            # To suppress the following error report, specify this argument
  68            # after -XX: or in .hotspotrc:  SuppressErrorAt=/debug.cpp:303
  69            #
  70            # A fatal error has been detected by the Java Runtime Environment:
  71            #
  72            #  Internal Error (/home/cheleswer/Desktop/jdk9/dev/hotspot/src/share/vm/utilities/debug.cpp:303), pid=6212, tid=6213
  73            #  fatal error: OutOfMemory encountered: Requested array size exceeds VM limit
  74            #
  75            # JRE version: OpenJDK Runtime Environment (9.0) (build 1.9.0-internal-debug-cheleswer_2015_10_20_14_32-b00)
  76            # Java VM: OpenJDK 64-Bit Server VM (1.9.0-internal-debug-cheleswer_2015_10_20_14_32-b00, mixed mode, tiered, compressed oops, serial gc, linux-amd64)
  77            # Core dump will be written. Default location: Core dumps may be processed with "/usr/share/apport/apport %p %s %c %P" (or dumping to
  78              /home/cheleswer/Desktop/core.6212)
  79            #
  80            # An error report file with more information is saved as:
  81            # /home/cheleswer/Desktop/hs_err_pid6212.log
  82            #
  83            # If you would like to submit a bug report, please visit:
  84            #   http://bugreport.java.com/bugreport/crash.jsp
  85            #
  86            Current thread is 6213
  87            Dumping core ...
  88            Aborted (core dumped)
  89         */
  90         output.shouldContain("Aborting due to java.lang.OutOfMemoryError: Requested array size exceeds VM limit");
  91         // extract hs-err file
  92         String hs_err_file = output.firstMatch("# *(\\S*hs_err_pid\\d+\\.log)", 1);
  93         if (hs_err_file == null) {
  94             throw new Error("Did not find hs-err file in output.\n");
  95         }
  96 
  97         /*
  98          * Check if hs_err files exist or not
  99          */
 100         File f = new File(hs_err_file);
 101         if (!f.exists()) {
 102             throw new Error("hs-err file missing at "+ f.getAbsolutePath() + ".\n");
 103         }
 104 
 105         /*
 106          * Checking the completness of hs_err file. If last line of hs_err file is "END"
 107          * then it proves that file is complete.
 108          */
 109         try (FileInputStream fis = new FileInputStream(f);
 110             BufferedReader br = new BufferedReader(new InputStreamReader(fis))) {
 111             String line = null;
 112             String lastLine = null;
 113             while ((line = br.readLine()) != null) {
 114                 lastLine = line;
 115             }
 116             if (!lastLine.equals("END.")) {
 117                 throw new Error("hs-err file incomplete (missing END marker.)");
 118             } else {
 119                 System.out.println("End marker found.");
 120             }
 121         }
 122         System.out.println("PASSED");
 123     }
 124 }