1 /*
2  * Copyright (c) 2016, 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 8146416
27  * @library /test/lib /
28  *
29  * @build sun.hotspot.WhiteBox
30  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
31  *                                sun.hotspot.WhiteBox$WhiteBoxPermission
32  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
33  *      -XX:+WhiteBoxAPI -Xbatch -Xmx100m
34  *      -XX:CompileCommand=exclude,compiler.uncommontrap.DeoptReallocFailure::main
35  *      compiler.uncommontrap.DeoptReallocFailure
36  *
37  */
38 
39 package compiler.uncommontrap;
40 
41 import sun.hotspot.WhiteBox;
42 
43 import java.lang.reflect.Method;
44 
45 public class DeoptReallocFailure {
46     static class MemoryChunk {
47         MemoryChunk other;
48         Object[][] array;
49 
MemoryChunk(MemoryChunk other)50         MemoryChunk(MemoryChunk other) {
51             this.other = other;
52             array = new Object[1024 * 256][];
53         }
54     }
55 
56     static class NoEscape {
57         long f1;
58     }
59 
60     static MemoryChunk root;
61     private static final WhiteBox WB = WhiteBox.getWhiteBox();
62 
test()63     public static synchronized long  test() {
64         NoEscape[] noEscape = new NoEscape[45];
65         noEscape[0] = new NoEscape();
66         for (int i=0;i<1024*256;i++) {
67            root.array[i]= new Object[45];
68         }
69         return noEscape[0].f1;
70     }
71 
main(String[] args)72     public static void main(String[] args) throws Throwable {
73 
74         //Exhaust Memory
75         root = null;
76         try {
77             while (true) {
78                 root = new MemoryChunk(root);
79             }
80         } catch (OutOfMemoryError oom) {
81         }
82 
83         if (root == null) {
84           return;
85         }
86 
87         try {
88             NoEscape dummy = new NoEscape();
89             Method m = DeoptReallocFailure.class.getMethod("test");
90             WB.enqueueMethodForCompilation(m, 4);
91             test();
92         } catch (OutOfMemoryError oom) {
93             root = null;
94             oom.printStackTrace();
95         }
96         System.out.println("TEST PASSED");
97     }
98 }
99