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