Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/except/except005.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/except005.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.except00562*/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 except005 {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;108109/**110* Temporary <code>log</code> for error messages, warnings and/or execution trace.111*112* @see #messages113*/114private static String log[] = new String[1000]; // up to 1000 messages115/**116* <code>pool</code> to store tiny objects to fill up the Heap117*/118private static volatile Object pool[] = null;119/**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("# Test have been updated!");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// Sum up exit code:182int exitCode = 0; // apparently PASSED183int skipped = 0; // some checks may correctly suffer OutOfMemoryError184// Allocate repository for a lots of tiny objects:185for (int size = 1 << 30; size > 0 && pool == null; size >>= 1)186try {187pool = new Object[size];188} catch (OutOfMemoryError oome) {189}190if (pool == null)191throw new Error("HS bug: cannot allocate new Object[1]");192int poolSize = pool.length;193int index = 0;194195// 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");212}213totalDelay += seconds;214if (totalDelay > 60) {215if (TRACE_ON)216out.println(217"Memory allocation became slow; so, heap seems exhausted.");218break;219}220}221}222} catch (OutOfMemoryError oome) {223if (TRACE_ON)224log[messages++] = "Heap seems exhausted - OutOfMemoryError thrown.";225}226if (index > poolSize - 1000) {227if (WARN_ON)228log[messages++] = "Warning: pool[] is full; so, checks would not be enough hard...";229}230231// Check InstantiationException (positive):232try {233// Object junkIt = Abra_Cadabra.class.newInstance(); // illegal - should fail234Object junkIt = Abra.Cadabra.class.newInstance(); // legal - should pass235if (TRACE_ON)236log[messages++] = "Success: InstantiationException (positive)";237} catch (IllegalAccessException iae) {238log[messages++] =239"Failure: InstantiationException (positive) incorrectly thrown IllegalAccessException";240pool[index++] = iae;241exitCode = 2;242} catch (InstantiationException ie) {243log[messages++] = "Failure: InstantiationException (positive)";244pool[index++] = ie;245exitCode = 2;246} catch (OutOfMemoryError oome) {247if (WARN_ON)248log[messages++] = "Skipped: InstantiationException (positive)";249pool[index++] = oome;250skipped++;251}252253return exitCode;254}255256/**257* Re-call to <code>run(args,out)</code>, and return JCK-like exit status.258* (The stream <code>out</code> is assigned to <code>System.out</code> here.)259*260* @see #run(String[], PrintStream)261*/262public static void main(String args[]) {263Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {264// Last try. If there is some OOME, test should end correctly265@Override266public void uncaughtException(Thread t, Throwable e) {267try {268pool = null;269log = null;270System.gc();271if (e instanceof OutOfMemoryError) {272try {273System.out.println("OOME : Test Skipped");274System.exit(0);275} catch (Throwable ignore) {276} // No code in the handler can provoke correct exceptions.277} else {278e.printStackTrace();279throw (RuntimeException) e;280}281} catch (OutOfMemoryError oome) {282}283}284});285int exitCode = run(args, System.out);286System.exit(exitCode + 95);287// JCK-like exit status.288}289290/**291* This class should be used to check <code>CloneNotSupportedException</code>,292* <code>IllegalAccessException</code>, and <code>IllegalArgumentException</code>.293* The class extends <code>except005</code> in order that its (protected)294* method <code>clone()</code> be available from <code>except005</code>.295*/296private static class Abra {297/**298* Will correctly instantiate <code>Abra.Cadabra</code> object.299*/300public static class Cadabra {301}302303/**304* Will try to correctly instantiate <code>Abra.Cadabra</code>,305* not <code>Abra</code>.306*/307private Abra() {308}309310}311312/**313* Will try to incorrectly instantiate and object of this class.314*/315private interface Abra_Cadabra {316}317318}319320321