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