annotate test/runtime/ClassUnload/KeepAliveSoftReference.java @ 12690:c3bdd186e6cf

fix possible NPE in TruffleCompiler with TraceTruffleCompilation and background compilation enabled. With background compilation, the installed code can already be invalidated when compiledMethod.getCode() is called, in which case it returns null.
author Andreas Woess <andreas.woess@jku.at>
date Wed, 06 Nov 2013 13:31:28 +0100
parents 33bcd9ead1d5
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10230
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
1 /*
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
4 *
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
7 * published by the Free Software Foundation.
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
8 *
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
13 * accompanied this code).
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
14 *
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
18 *
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
21 * questions.
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
22 */
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
23
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
24 /*
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
25 * @test KeepAliveSoftReference
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
26 * @summary This test case uses a java.lang.ref.SoftReference referencing a class instance to keep a class alive.
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
27 * @library /testlibrary /testlibrary/whitebox /runtime/testlibrary
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
28 * @library classes
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
29 * @build KeepAliveSoftReference test.Empty
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
30 * @build ClassUnloadCommon
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
31 * @run main ClassFileInstaller sun.hotspot.WhiteBox
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
32 * @run main/othervm -Xbootclasspath/a:. -Xmn8m -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI KeepAliveSoftReference
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
33 */
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
34
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
35 import java.lang.ref.SoftReference;
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
36 import sun.hotspot.WhiteBox;
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
37
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
38 /**
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
39 * Test that verifies that classes are not unloaded when specific types of references are kept to them.
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
40 */
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
41 public class KeepAliveSoftReference {
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
42 private static final String className = "test.Empty";
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
43 private static final WhiteBox wb = WhiteBox.getWhiteBox();
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
44
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
45 public static void main(String... args) throws Exception {
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
46 ClassLoader cl = ClassUnloadCommon.newClassLoader();
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
47 Class<?> c = cl.loadClass(className);
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
48 Object o = c.newInstance();
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
49 SoftReference<Object> sr = new SoftReference(o);
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
50 o = null; c = null; cl = null;
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
51
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
52 {
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
53 boolean isAlive = wb.isClassAlive(className);
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
54 System.out.println("testSoftReference (1) alive: " + isAlive);
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
55 boolean cleared = (sr.get() == null);
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
56 boolean shouldBeAlive = !cleared;
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
57 ClassUnloadCommon.failIf(isAlive != shouldBeAlive, "" + isAlive + " != " + shouldBeAlive);
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
58 }
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
59
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
60 ClassUnloadCommon.triggerUnloading();
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
61
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
62 {
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
63 boolean isAlive = wb.isClassAlive(className);
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
64 System.out.println("testSoftReference (2) alive: " + isAlive);
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
65 boolean cleared = (sr.get() == null);
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
66 boolean shouldBeAlive = !cleared;
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
67 ClassUnloadCommon.failIf(isAlive != shouldBeAlive, "" + isAlive + " != " + shouldBeAlive);
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
68 }
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
69 sr.clear();
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
70 ClassUnloadCommon.triggerUnloading();
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
71
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
72 {
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
73 boolean isAlive = wb.isClassAlive(className);
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
74 System.out.println("testSoftReference (3) alive: " + isAlive);
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
75 boolean cleared = (sr.get() == null);
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
76 boolean shouldBeAlive = !cleared;
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
77 ClassUnloadCommon.failIf(isAlive != shouldBeAlive, "" + isAlive + " != " + shouldBeAlive);
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
78 }
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
79 }
33bcd9ead1d5 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
80 }