Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/except/except006.java
41155 views
/*1* Copyright (c) 1999, 2018, 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*/22232425/*26* @test27* @key stress28*29* @summary converted from VM testbase nsk/stress/except/except006.30* VM testbase keywords: [stress, diehard, slow, nonconcurrent, quick]31* VM testbase readme:32* DESCRIPTION33* This checks if various exceptions are thrown (and caught) correctly34* when there apparently are no free space in the heap to allocate new35* Throwable instance.36* The test tries to occupy all of memory available in the heap by allocating37* lots of new Object() instances. Instances of the type Object are the smallest38* objects, so they apparently should occupy most fine-grained fragments in the39* heap and leave no free space for new Throwable instance. After that, the test40* provokes various exceptions (e.g.: by executing integer division by 0 and so41* on), and checks if appropriate exceptions are thrown.42* COMMENTS43* The test needs a lot of memory to start up, so it should not run under older44* JDK 1.1.x release due to its poorer heap utilization. Also, some checks are45* skipped when testing classic VM, because OutOfMemoryError is correctly thrown46* instead of target exception.47* When the test is being self-initiating (i.e.: eating heap), memory occupation48* is terminated if memory allocation slows down crucially. This is a workaround49* intended to avoid the HotSpot bug:50* #4248801 (P1/S5) slow memory allocation when heap is almost exhausted51* There is also a workaround involved to avoid the following bugs known52* for HotSpot and for classic VM:53* #4239841 (P1/S5) 1.1: poor garbage collector performance (HotSpot bug)54* #4245060 (P4/S5) poor garbage collector performance (Classic VM bug)55* However, printing of the test's error messages, warnings, and of execution56* trace fails under JDK 1.2 for Win32 even so. If the test fails due to this57* problem, exit status 96 is returned instead of 97.58* JDK 1.3 classic VM for Sparc may crash (core dump) due to the known bug:59* #4245057 (P2/S3) VM crashes when heap is exhausted60*61* @run main/othervm -Xms50M -Xmx200M nsk.stress.except.except00662*/6364package nsk.stress.except;6566import java.io.PrintStream;6768/**69* This checks if various exceptions are thrown (and caught) correctly70* when there apparently are no free space in the heap to allocate new71* <code>Throwable</code> instance.72* <p>73* <p>The test tries to occupy all of memory available in the heap by74* allocating lots of new <code>Object()</code> instances. Instances of the75* type <code>Object</code> are the smallest objects, so they apparently should76* occupy most fine-grained fragments in the heap and leave no free space for77* new <code>Throwable</code> instance. After that, the test provokes various78* exceptions (e.g.: by executing integer division by 0 and so on), and checks79* if appropriate exceptions are thrown.80* <p>81* <p>Note, that memory occupation is terminated if memory allocation slows82* down crucially. This is a workaround intended to avoid the HotSpot bug:83* <br> 84* #4248801 (P1/S5) slow memory allocation when heap is almost exhausted85* <p>86* <p>There is also a workaround involved to avoid the following bugs known87* for HotSpot and for classic VM:88* <br> 89* #4239841 (P1/S5) 1.1: poor garbage collector performance90* <br> 91* #4245060 (P4/S5) poor garbage collector performance92* <br>However, printing of the test's error messages, warnings, and of93* execution trace may fail even so. If the test fails due to poor GC94* performance, exit status 96 is returned instead of 97.95* <p>96* <p>Also note, that the test needs a lot of memory to start up, so it should97* not run under older JDK 1.1.x release due to its poor heap utilization.98*/99public class except006 {100/**101* Either allow or supress printing of execution trace.102*/103private static boolean TRACE_ON = false;104/**105* Either allow or supress printing of warning messages.106*/107private static final boolean WARN_ON = true;108/*109* Storage for a lot of tiny objects110* "static volatile" keywords are for preventing heap optimization111*/112private static volatile Object pool[] = null;113/**114* Temporary <code>log</code> for error messages, warnings and/or execution trace.115*116* @see #messages117*/118private static String log[] = new String[1000]; // up to 1000 messages119/**120* How many <code>messages</code> were submitted to the <code>log</code>.121*122* @see #log123*/124private static int messages = 0;125126/**127* Re-call to the method <code>run(out)</code> (ignore <code>args[]</code>),128* and print the test summary - either test passed of failed.129*/130public static int run(String args[], PrintStream out) {131if (args.length > 0) {132if (args[0].toLowerCase().startsWith("-v"))133TRACE_ON = true;134}135136int exitCode = run(out);137pool = null;138System.gc();139// Print the log[] and the test summary:140try {141for (int i = 0; i < messages; i++)142out.println(log[i]);143if (exitCode == 0) {144if (TRACE_ON)145out.println("Test passed.");146} else147out.println("Test failed.");148} catch (OutOfMemoryError oome) {149// Poor performance of garbage collector:150exitCode = 1;151}152153return exitCode;154}155156/**157* Allocate as much <code>Object</code> instances as possible to bring JVM158* into stress, and then check if exceptions are correctly thrown accordingly159* to various situations like integer division by 0, etc.160*/161private static int run(PrintStream out) {162out.println("# While printing this message, JVM seems to initiate the output");163out.println("# stream, so that it will not need more memory to print later,");164out.println("# when the heap would fail to provide more memory.");165out.println("# ");166out.println("# Note, that the test maintains especial static log[] field in");167out.println("# order to avoid printing when the heap seems exhausted.");168out.println("# Nevertheless, printing could arise OutOfMemoryError even");169out.println("# after all the memory allocated by the test is released.");170out.println("# ");171out.println("# That problem is caused by the known JDK/HotSpot bugs:");172out.println("# 4239841 (P1/S5) 1.1: poor garbage collector performance");173out.println("# 4245060 (P4/S5) poor garbage collector performance");174out.println("# ");175out.println("# This message is just intended to work-around that problem.");176out.println("# If printing should fail even so, the test will try to return");177out.println("# the exit status 96 instead of 97 to indicate the problem.");178out.println("# However, the test may fail or even crash on some platforms");179out.println("# suffering the bug 4239841 or 4245060.");180181// Allocate repository for a lots of tiny objects:182for (int size = 1 << 30; size > 0 && pool == null; size >>= 1)183try {184pool = new Object[size];185} catch (OutOfMemoryError oome) {186}187if (pool == null)188throw new Error("HS bug: cannot allocate new Object[1]");189int poolSize = pool.length;190int index = 0;191// Sum up exit code:192int exitCode = 0; // apparently PASSED193int skipped = 0; // some checks may correctly suffer OutOfMemoryError194Object junkIt;195// Sum up time spent, when it was hard to JVM to allocate next object196// (i.e.: when JVM has spent more than 1 second to allocate new object):197double totalDelay = 0;198long timeMark = System.currentTimeMillis();199try {200for (; index < poolSize; index++) {201//-------------------------202pool[index] = new Object();203long nextTimeMark = System.currentTimeMillis();204long elapsed = nextTimeMark - timeMark;205timeMark = nextTimeMark;206//----------------------207if (elapsed > 1000) {208double seconds = elapsed / 1000.0;209if (TRACE_ON)210out.println(211"pool[" + index + "]=new Object(); // elapsed " + seconds + "s");212totalDelay += seconds;213if (totalDelay > 60) {214if (TRACE_ON)215out.println(216"Memory allocation became slow; so, heap seems exhausted.");217break;218}219}220}221} catch (OutOfMemoryError oome) {222if (TRACE_ON)223log[messages++] = "Heap seems exhausted - OutOfMemoryError thrown.";224}225if (index > poolSize - 1000) {226if (WARN_ON)227log[messages++] = "Warning: pool[] is full; so, checks would not be enough hard...";228}229230231// Check InstantiationException (negative):232try {233junkIt = Abra_Cadabra.class.newInstance(); // illegal - should fail234// Object junkIt = Abra.Cadabra.class.newInstance(); // legal - should pass235log[messages++] = "Failure: InstantiationException (negative)";236exitCode = 2;237} catch (IllegalAccessException iae) {238log[messages++] =239"Failure: InstantiationException (negative) incorrectly thrown IllegalAccessException";240exitCode = 2;241} catch (InstantiationException ie) {242if (TRACE_ON)243log[messages++] = "Success: InstantiationException (negative)";244} catch (OutOfMemoryError oome) {245if (WARN_ON)246log[messages++] = "Skipped: InstantiationException (negative)";247skipped++;248}249250return exitCode;251}252253/**254* Re-call to <code>run(args,out)</code>, and return JCK-like exit status.255* (The stream <code>out</code> is assigned to <code>System.out</code> here.)256*257* @see #run(String[], PrintStream)258*/259public static void main(String args[]) {260Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {261// Last try. If there is some exception outside the code, test should end correctly262@Override263public void uncaughtException(Thread t, Throwable e) {264try {265pool = null;266log = null;267System.gc();268if (e instanceof OutOfMemoryError) {269try {270System.out.println("OOME : Test Skipped");271System.exit(0);272} catch (Throwable ignore) {273} // No code in the handler can provoke correct exceptions.274} else {275e.printStackTrace();276throw (RuntimeException) e;277}278} catch (OutOfMemoryError oome) {279}280}281});282int exitCode = run(args, System.out);283System.exit(exitCode + 95);284// JCK-like exit status.285}286287/**288* This class should be used to check <code>CloneNotSupportedException</code>,289* <code>IllegalAccessException</code>, and <code>IllegalArgumentException</code>.290* The class extends <code>except006</code> in order that its (protected)291* method <code>clone()</code> be available from <code>except006</code>.292*/293private static class Abra {294/**295* Will correctly instantiate <code>Abra.Cadabra</code> object.296*/297public static class Cadabra {298}299300/**301* Will try to correctly instantiate <code>Abra.Cadabra</code>,302* not <code>Abra</code>.303*/304private Abra() {305}306307}308309/**310* Will try to incorrectly instantiate and object of this class.311*/312private interface Abra_Cadabra {313}314315}316317318