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 java.lang.ref.*;
  45 import static jdk.test.lib.Asserts.*;
  46 
  47 public class GCTest {
  48     static final MethodHandles.Lookup LOOKUP = MethodHandles.Lookup.IMPL_LOOKUP;
  49 
  50     static class T {
  51         static int f1() { return 0; }
  52         static int f2() { return 1; }
  53     }
  54 
  55     static @Stable MethodHandle mh;
  56     static PhantomReference<LambdaForm> lform;
  57 
  58     static final ReferenceQueue<LambdaForm> rq = new ReferenceQueue<>();
  59     static final WhiteBox WB = WhiteBox.getWhiteBox();
  60 
  61     @DontInline
  62     static int invokeBasic() {
  63         try {
  64             return (int) mh.invokeBasic();
  65         } catch (Throwable e) {
  66             throw new Error(e);
  67         }
  68     }
  69 
  70     static void test(int expected) {
  71         for (int i = 0; i < 20_000; i++) {
  72             invokeBasic();
  73         }
  74         assertEquals(invokeBasic(), expected);
  75     }
  76 
  77     public static void main(String[] args) throws Exception {
  78         mh = NonInlinedReinvoker.make(
  79                 LOOKUP.findStatic(T.class, "f1", MethodType.methodType(int.class)));
  80 
  81         // Monitor LambdaForm GC
  82         lform = new PhantomReference<>(mh.form, rq);
  83 
  84         test(0);
  85         WB.clearInlineCaches();
  86         test(0);
  87 
  88         mh = NonInlinedReinvoker.make(
  89                 LOOKUP.findStatic(T.class, "f2", MethodType.methodType(int.class)));
  90 
  91         Reference<?> ref = null;
  92         while (ref == null) {
  93             WB.fullGC();
  94             try {
  95                 ref = rq.remove(1000);
  96             } catch (InterruptedException e) { /*ignore*/ }
  97         }
  98 
  99         test(1);
 100         WB.clearInlineCaches();
 101         test(1);
 102 
 103         System.out.println("TEST PASSED");
 104     }
 105 }