1 /*
2  * Copyright (c) 2020, 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 8257594
27  * @summary Test that failing checkcast does not trigger repeated recompilation until cutoff is hit.
28  * @requires vm.compiler2.enabled
29  * @modules java.base/jdk.internal.misc
30  * @library /test/lib
31  * @build sun.hotspot.WhiteBox
32  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
33  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
34  *                   -Xbatch -XX:CompileCommand=dontinline,compiler.uncommontrap.TestNullAssertAtCheckCast::test*
35  *                   -XX:CompileCommand=inline,compiler.uncommontrap.TestNullAssertAtCheckCast::cast
36  *                   -XX:CompileCommand=inline,compiler.uncommontrap.TestNullAssertAtCheckCast::store
37  *                   compiler.uncommontrap.TestNullAssertAtCheckCast
38  */
39 
40 package compiler.uncommontrap;
41 
42 import sun.hotspot.WhiteBox;
43 
44 import java.lang.reflect.Method;
45 
46 public class TestNullAssertAtCheckCast {
47     private static final WhiteBox WB = WhiteBox.getWhiteBox();
48     private static final int COMP_LEVEL_FULL_OPTIMIZATION = 4;
49 
cast(Object val)50     static Long cast(Object val) {
51         return (Long)val;
52     }
53 
test1()54     static void test1() {
55         try {
56             // Always fails
57             cast(new Integer(42));
58         } catch (ClassCastException cce) {
59             // Ignored
60         }
61     }
62 
test2(Integer val)63     static void test2(Integer val) {
64         try {
65             // Always fails
66             cast(val);
67         } catch (ClassCastException cce) {
68             // Ignored
69         }
70     }
71 
store(Object[] array, Object val)72     static void store(Object[] array, Object val) {
73         array[0] = val;
74     }
75 
test3()76     static void test3() {
77         try {
78             // Always fails
79             store(new Long[1], new Integer(42));
80         } catch (ArrayStoreException cce) {
81             // Ignored
82         }
83     }
84 
test4(Integer val)85     static void test4(Integer val) {
86         try {
87             // Always fails
88             store(new Long[1], val);
89         } catch (ArrayStoreException cce) {
90             // Ignored
91         }
92     }
93 
main(String[] args)94     public static void main(String[] args) throws Exception {
95         for (int i = 0; i < 1_000_000; ++i) {
96             test1();
97             test2((i % 2 == 0) ? null : 42);
98             test3();
99             test4((i % 2 == 0) ? null : 42);
100         }
101         Method method = TestNullAssertAtCheckCast.class.getDeclaredMethod("test1");
102         if (!WB.isMethodCompilable(method, COMP_LEVEL_FULL_OPTIMIZATION, false)) {
103             throw new RuntimeException("TestNullAssertAtCheckCast::test1 not compilable");
104         }
105         method = TestNullAssertAtCheckCast.class.getDeclaredMethod("test2", Integer.class);
106         if (!WB.isMethodCompilable(method, COMP_LEVEL_FULL_OPTIMIZATION, false)) {
107             throw new RuntimeException("TestNullAssertAtCheckCast::test2 not compilable");
108         }
109         method = TestNullAssertAtCheckCast.class.getDeclaredMethod("test3");
110         if (!WB.isMethodCompilable(method, COMP_LEVEL_FULL_OPTIMIZATION, false)) {
111             throw new RuntimeException("TestNullAssertAtCheckCast::test3 not compilable");
112         }
113         method = TestNullAssertAtCheckCast.class.getDeclaredMethod("test4", Integer.class);
114         if (!WB.isMethodCompilable(method, COMP_LEVEL_FULL_OPTIMIZATION, false)) {
115             throw new RuntimeException("TestNullAssertAtCheckCast::test4 not compilable");
116         }
117     }
118 }
119 
120