1 /*
   2  * Copyright (c) 2015, 2016, 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  * @library /testlibrary
  28  * @build com.oracle.java.testlibrary.*
  29  * @run driver TestCrashOnOutOfMemoryError
  30  * @bug 8138745
  31  */
  32 
  33 import com.oracle.java.testlibrary.OutputAnalyzer;
  34 import com.oracle.java.testlibrary.ProcessTools;
  35 import java.io.BufferedReader;
  36 import java.io.File;
  37 import java.io.FileInputStream;
  38 import java.io.InputStreamReader;
  39 import java.io.IOException;
  40 
  41 public class TestCrashOnOutOfMemoryError {
  42 
  43     public static void main(String[] args) throws Exception {
  44         if (args.length == 1) {
  45             // This should guarantee to throw:
  46             // java.lang.OutOfMemoryError: Requested array size exceeds VM limit
  47             try {
  48                 Object[] oa = new Object[Integer.MAX_VALUE];
  49                 throw new Error("OOME not triggered");
  50             } catch (OutOfMemoryError err) {
  51                 throw new Error("OOME didn't abort JVM!");
  52             }
  53         }
  54         // else this is the main test
  55         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+CrashOnOutOfMemoryError",
  56                 "-Xmx64m", TestCrashOnOutOfMemoryError.class.getName(),"throwOOME");
  57         OutputAnalyzer output = new OutputAnalyzer(pb.start());
  58         int exitValue = output.getExitValue();
  59         if (0 == exitValue) {
  60             //expecting a non zero value
  61             throw new Error("Expected to get non zero exit value");
  62         }
  63 
  64         /* Output should look something like this. The actual text will depend on the OS and its core dump processing.
  65            Aborting due to java.lang.OutOfMemoryError: Requested array size exceeds VM limit
  66            # To suppress the following error report, specify this argument
  67            # after -XX: or in .hotspotrc:  SuppressErrorAt=/debug.cpp:303
  68            #
  69            # A fatal error has been detected by the Java Runtime Environment:
  70            #
  71            #  Internal Error (/home/cheleswer/Desktop/jdk9/dev/hotspot/src/share/vm/utilities/debug.cpp:303), pid=6212, tid=6213
  72            #  fatal error: OutOfMemory encountered: Requested array size exceeds VM limit
  73            #
  74            # JRE version: OpenJDK Runtime Environment (9.0) (build 1.9.0-internal-debug-cheleswer_2015_10_20_14_32-b00)
  75            # 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)
  76            # Core dump will be written. Default location: Core dumps may be processed with "/usr/share/apport/apport %p %s %c %P" (or dumping to
  77              /home/cheleswer/Desktop/core.6212)
  78            #
  79            # An error report file with more information is saved as:
  80            # /home/cheleswer/Desktop/hs_err_pid6212.log
  81            #
  82            # If you would like to submit a bug report, please visit:
  83            #   http://bugreport.java.com/bugreport/crash.jsp
  84            #
  85            Current thread is 6213
  86            Dumping core ...
  87            Aborted (core dumped)
  88         */
  89         output.shouldContain("Aborting due to java.lang.OutOfMemoryError: Requested array size exceeds VM limit");
  90         // extract hs-err file
  91         String hs_err_file = output.firstMatch("# *(\\S*hs_err_pid\\d+\\.log)", 1);
  92         if (hs_err_file == null) {
  93             throw new Error("Did not find hs-err file in output.\n");
  94         }
  95 
  96         /*
  97          * Check if hs_err files exist or not
  98          */
  99         File f = new File(hs_err_file);
 100         if (!f.exists()) {
 101             throw new Error("hs-err file missing at "+ f.getAbsolutePath() + ".\n");
 102         }
 103 
 104         System.out.println("PASSED");
 105     }
 106 }