Path: blob/master/test/hotspot/jtreg/runtime/ErrorHandling/TestCrashOnOutOfMemoryError.java
40942 views
/*1* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test TestCrashOnOutOfMemoryError25* @summary Test using -XX:+CrashOnOutOfMemoryError26* @modules java.base/jdk.internal.misc27* @library /test/lib28* @run driver TestCrashOnOutOfMemoryError29* @bug 813874530*/3132import jdk.test.lib.process.OutputAnalyzer;33import jdk.test.lib.process.ProcessTools;34import java.io.BufferedReader;35import java.io.File;36import java.io.FileInputStream;37import java.io.InputStreamReader;38import java.io.IOException;3940public class TestCrashOnOutOfMemoryError {4142public static void main(String[] args) throws Exception {43if (args.length == 1) {44// This should guarantee to throw:45// java.lang.OutOfMemoryError: Requested array size exceeds VM limit46try {47Object[] oa = new Object[Integer.MAX_VALUE];48throw new Error("OOME not triggered");49} catch (OutOfMemoryError err) {50throw new Error("OOME didn't abort JVM!");51}52}53// else this is the main test54ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+CrashOnOutOfMemoryError",55"-XX:-CreateCoredumpOnCrash", "-Xmx128m", TestCrashOnOutOfMemoryError.class.getName(),"throwOOME");56OutputAnalyzer output = new OutputAnalyzer(pb.start());57int exitValue = output.getExitValue();58if (0 == exitValue) {59//expecting a non zero value60throw new Error("Expected to get non zero exit value");61}6263/* Output should look something like this. The actual text will depend on the OS and its core dump processing.64Aborting due to java.lang.OutOfMemoryError: Requested array size exceeds VM limit65# To suppress the following error report, specify this argument66# after -XX: or in .hotspotrc: SuppressErrorAt=/debug.cpp:30367#68# A fatal error has been detected by the Java Runtime Environment:69#70# Internal Error (/home/cheleswer/Desktop/jdk9/dev/hotspot/src/share/vm/utilities/debug.cpp:303), pid=6212, tid=621371# fatal error: OutOfMemory encountered: Requested array size exceeds VM limit72#73# JRE version: OpenJDK Runtime Environment (9.0) (build 1.9.0-internal-debug-cheleswer_2015_10_20_14_32-b00)74# 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)75# Core dump will be written. Default location: Core dumps may be processed with "/usr/share/apport/apport %p %s %c %P" (or dumping to76/home/cheleswer/Desktop/core.6212)77#78# An error report file with more information is saved as:79# /home/cheleswer/Desktop/hs_err_pid6212.log80#81# If you would like to submit a bug report, please visit:82# http://bugreport.java.com/bugreport/crash.jsp83#84Current thread is 621385Dumping core ...86Aborted (core dumped)87*/88output.shouldContain("Aborting due to java.lang.OutOfMemoryError: Requested array size exceeds VM limit");89// extract hs-err file90String hs_err_file = output.firstMatch("# *(\\S*hs_err_pid\\d+\\.log)", 1);91if (hs_err_file == null) {92throw new Error("Did not find hs-err file in output.\n");93}9495/*96* Check if hs_err files exist or not97*/98File f = new File(hs_err_file);99if (!f.exists()) {100throw new Error("hs-err file missing at "+ f.getAbsolutePath() + ".\n");101}102103/*104* Checking the completness of hs_err file. If last line of hs_err file is "END"105* then it proves that file is complete.106*/107try (FileInputStream fis = new FileInputStream(f);108BufferedReader br = new BufferedReader(new InputStreamReader(fis))) {109String line = null;110String lastLine = null;111while ((line = br.readLine()) != null) {112lastLine = line;113}114if (!lastLine.equals("END.")) {115throw new Error("hs-err file incomplete (missing END marker.)");116} else {117System.out.println("End marker found.");118}119}120System.out.println("PASSED");121}122}123124125