1 /*
   2  * Copyright (c) 2013, 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 8012941
  27  * @summary JSR 292: too deep inlining might crash compiler because of stack overflow
  28  *
  29  * @run main/othervm -Xbatch
  30  *                   -Djava.lang.invoke.MethodHandle.COMPILE_THRESHOLD=0
  31  *                   -XX:CompileThreshold=10
  32  *                   -XX:CompilerThreadStackSize=50
  33  *                   -XX:CompileCommand=compileonly,DeepInliningTest,m2
  34  *                   -XX:CompileCommand=inline,java/lang/invoke*,*
  35  *                      DeepInliningTest
  36  */
  37 import java.lang.invoke.MethodHandle;
  38 import java.lang.invoke.MethodHandles;
  39 import java.lang.invoke.MethodType;
  40 
  41 public class DeepInliningTest {
  42     public static final MethodHandle constMH;
  43     static {
  44         try {
  45             MethodHandle localMH = MethodHandles.lookup().findStatic(DeepInliningTest.class, "m1", MethodType.methodType(boolean.class));
  46 
  47             // Nest MH type conversions to produce long chain of lambda forms
  48             final int DEPTH = Integer.getInteger("DEPTH", 200);
  49             for (int i = 0; i < DEPTH; i++) {
  50                 localMH = localMH.asType(MethodType.methodType(Boolean.class));
  51                 localMH = localMH.asType(MethodType.methodType(boolean.class));
  52             }
  53 
  54             localMH = localMH.asType(MethodType.methodType(Boolean.class));
  55             constMH = localMH;
  56         } catch (Exception e) {
  57             throw new Error(e);
  58         }
  59     }
  60 
  61     static boolean m1() { return true; }
  62 
  63     static boolean m2() throws Throwable {
  64         return (Boolean) constMH.invokeExact();
  65     }
  66 
  67     public static void main(String[] args) throws Throwable {
  68         for (int i = 0; i < 1000; i++) {
  69             try {
  70                 boolean result = m2();
  71             } catch (StackOverflowError e) {
  72                 System.out.println(i+": "+e);
  73             }
  74         }
  75         System.out.println("TEST PASSED");
  76     }
  77 }