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