|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, 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 |
| 26 | + * @bug 8048190 |
| 27 | + * @summary Test that the NCDFE saves the stack trace for the original exception |
| 28 | + * during class initialization with ExceptionInInitializationError, |
| 29 | + * and doesn't prevent the classes in the stacktrace to be unloaded. |
| 30 | + * @requires vm.opt.final.ClassUnloading |
| 31 | + * @modules java.base/jdk.internal.misc |
| 32 | + * @library /test/lib |
| 33 | + * @build sun.hotspot.WhiteBox |
| 34 | + * @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox |
| 35 | + * @run main/othervm -Xbootclasspath/a:. -Xmn8m -XX:+UnlockDiagnosticVMOptions -Xlog:class+unload -XX:+WhiteBoxAPI InitExceptionUnloadTest |
| 36 | + */ |
| 37 | + |
| 38 | +import java.io.ByteArrayOutputStream; |
| 39 | +import java.io.PrintStream; |
| 40 | + |
| 41 | +import sun.hotspot.WhiteBox; |
| 42 | +import jdk.test.lib.classloader.ClassUnloadCommon; |
| 43 | + |
| 44 | +public class InitExceptionUnloadTest { |
| 45 | + static public class ThrowsRuntimeException { static int x = 1/0; } |
| 46 | + static public class ThrowsError { static { if (true) throw new Error(); } } |
| 47 | + static public class SpecialException extends RuntimeException { |
| 48 | + SpecialException(int count, String message) { |
| 49 | + super(message + count); |
| 50 | + } |
| 51 | + } |
| 52 | + static public class ThrowsSpecialException { |
| 53 | + static { |
| 54 | + if (true) throw new SpecialException(3, "Very Special "); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + static public class ThrowsOOM { |
| 59 | + static { |
| 60 | + if (true) { |
| 61 | + // Actually getting an OOM might be fragile but it was tested. |
| 62 | + throw new OutOfMemoryError("Java heap space"); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private static void verify_stack(Throwable e, String expected, String cause) throws Exception { |
| 68 | + ByteArrayOutputStream byteOS = new ByteArrayOutputStream(); |
| 69 | + PrintStream printStream = new PrintStream(byteOS); |
| 70 | + e.printStackTrace(printStream); |
| 71 | + printStream.close(); |
| 72 | + String stackTrace = byteOS.toString("ASCII"); |
| 73 | + if (!stackTrace.contains(expected) || (cause != null && !stackTrace.contains(cause))) { |
| 74 | + throw new RuntimeException(expected + " and " + cause + " missing from stacktrace"); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + static String[] expected = new String[] { |
| 79 | + "java.lang.ExceptionInInitializerError", |
| 80 | + "Caused by: java.lang.ArithmeticException: / by zero", |
| 81 | + "java.lang.NoClassDefFoundError: Could not initialize class InitExceptionUnloadTest$ThrowsRuntimeException", |
| 82 | + "Caused by: java.lang.ExceptionInInitializerError: Exception java.lang.ArithmeticException: / by zero [in thread", |
| 83 | + "java.lang.Error", |
| 84 | + null, |
| 85 | + "java.lang.NoClassDefFoundError: Could not initialize class InitExceptionUnloadTest$ThrowsError", |
| 86 | + "Caused by: java.lang.ExceptionInInitializerError: Exception java.lang.Error [in thread", |
| 87 | + "java.lang.ExceptionInInitializerError", |
| 88 | + "Caused by: InitExceptionUnloadTest$SpecialException: Very Special 3", |
| 89 | + "java.lang.NoClassDefFoundError: Could not initialize class InitExceptionUnloadTest$ThrowsSpecialException", |
| 90 | + "Caused by: java.lang.ExceptionInInitializerError: Exception InitExceptionUnloadTest$SpecialException: Very Special 3", |
| 91 | + "java.lang.OutOfMemoryError", |
| 92 | + "Java heap space", |
| 93 | + "java.lang.NoClassDefFoundError: Could not initialize class InitExceptionUnloadTest$ThrowsOOM", |
| 94 | + "Caused by: java.lang.ExceptionInInitializerError: Exception java.lang.OutOfMemoryError: Java heap space [in thread" |
| 95 | + }; |
| 96 | + |
| 97 | + static String[] classNames = new String[] { |
| 98 | + "InitExceptionUnloadTest$ThrowsRuntimeException", |
| 99 | + "InitExceptionUnloadTest$ThrowsError", |
| 100 | + "InitExceptionUnloadTest$ThrowsSpecialException", |
| 101 | + "InitExceptionUnloadTest$ThrowsOOM" }; |
| 102 | + |
| 103 | + public static WhiteBox wb = WhiteBox.getWhiteBox(); |
| 104 | + |
| 105 | + static void test() throws Throwable { |
| 106 | + ClassLoader cl = ClassUnloadCommon.newClassLoader(); |
| 107 | + int i = 0; |
| 108 | + for (String className : classNames) { |
| 109 | + for (int tries = 2; tries-- > 0; ) { |
| 110 | + System.err.println("--- try to load " + className); |
| 111 | + try { |
| 112 | + Class<?> c = cl.loadClass(className); |
| 113 | + Object inst = c.newInstance(); |
| 114 | + } catch (Throwable t) { |
| 115 | + t.printStackTrace(); |
| 116 | + System.err.println(); |
| 117 | + System.err.println("Check results"); |
| 118 | + verify_stack(t, expected[i], expected[i+1]); |
| 119 | + i += 2; |
| 120 | + System.err.println(); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + cl = null; |
| 125 | + ClassUnloadCommon.triggerUnloading(); // should unload these classes |
| 126 | + for (String className : classNames) { |
| 127 | + ClassUnloadCommon.failIf(wb.isClassAlive(className), "should be unloaded"); |
| 128 | + } |
| 129 | + } |
| 130 | + public static void main(java.lang.String[] unused) throws Throwable { |
| 131 | + test(); |
| 132 | + test(); |
| 133 | + } |
| 134 | +} |
0 commit comments