1 /*
   2  * Copyright (c) 2015, 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 8072008
  27  * @library /testlibrary /test/lib ../patches
  28  * @modules java.base/jdk.internal.misc
  29  *          java.base/jdk.internal.vm.annotation
  30  *
  31  * @build java.base/java.lang.invoke.MethodHandleHelper
  32  * @build sun.hotspot.WhiteBox
  33  * @run main/bootclasspath/othervm -XX:+IgnoreUnrecognizedVMOptions
  34  *                                 -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  35  *                                 -Xbatch -XX:-TieredCompilation -XX:CICompilerCount=1
  36  *                                 -XX:+FoldStableValues
  37  *                                 compiler.jsr292.NonInlinedCall.GCTest
  38  */
  39 
  40 package compiler.jsr292.NonInlinedCall;
  41 
  42 import jdk.internal.vm.annotation.DontInline;
  43 import jdk.internal.vm.annotation.Stable;
  44 import sun.hotspot.WhiteBox;
  45 
  46 import java.lang.invoke.MethodHandle;
  47 import java.lang.invoke.MethodHandleHelper;
  48 import java.lang.invoke.MethodHandleHelper.NonInlinedReinvoker;
  49 import java.lang.invoke.MethodHandles;
  50 import java.lang.invoke.MethodType;
  51 import java.lang.ref.PhantomReference;
  52 import java.lang.ref.Reference;
  53 import java.lang.ref.ReferenceQueue;
  54 
  55 import static jdk.test.lib.Asserts.assertEquals;
  56 
  57 public class GCTest {
  58     static final MethodHandles.Lookup LOOKUP = MethodHandleHelper.IMPL_LOOKUP;
  59 
  60     static class T {
  61         static int f1() { return 0; }
  62         static int f2() { return 1; }
  63     }
  64 
  65     static @Stable MethodHandle mh;
  66     static PhantomReference<Object> lform;
  67 
  68     static final ReferenceQueue<Object> rq = new ReferenceQueue<>();
  69     static final WhiteBox WB = WhiteBox.getWhiteBox();
  70 
  71     @DontInline
  72     static int invokeBasic() {
  73         try {
  74             return MethodHandleHelper.invokeBasicI(mh);
  75         } catch (Throwable e) {
  76             throw new Error(e);
  77         }
  78     }
  79 
  80     static void test(int expected) {
  81         for (int i = 0; i < 20_000; i++) {
  82             invokeBasic();
  83         }
  84         assertEquals(invokeBasic(), expected);
  85     }
  86 
  87     public static void main(String[] args) throws Exception {
  88         mh = NonInlinedReinvoker.make(
  89                 LOOKUP.findStatic(T.class, "f1", MethodType.methodType(int.class)));
  90 
  91         // Monitor LambdaForm GC
  92         lform = new PhantomReference<>(MethodHandleHelper.getLambdaForm(mh), rq);
  93 
  94         test(0);
  95         WB.clearInlineCaches();
  96         test(0);
  97 
  98         mh = NonInlinedReinvoker.make(
  99                 LOOKUP.findStatic(T.class, "f2", MethodType.methodType(int.class)));
 100 
 101         Reference<?> ref = null;
 102         while (ref == null) {
 103             WB.fullGC();
 104             try {
 105                 ref = rq.remove(1000);
 106             } catch (InterruptedException e) { /*ignore*/ }
 107         }
 108 
 109         test(1);
 110         WB.clearInlineCaches();
 111         test(1);
 112 
 113         System.out.println("TEST PASSED");
 114     }
 115 }