hotspot/test/compiler/jsr292/CallSiteDepContextTest.java
changeset 30296 95baefac8485
child 30311 89c0e067b0e0
equal deleted inserted replaced
30226:5f1a3a275862 30296:95baefac8485
       
     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 8057967
       
    27  * @run main/bootclasspath -Xbatch java.lang.invoke.CallSiteDepContextTest
       
    28  */
       
    29 package java.lang.invoke;
       
    30 
       
    31 import java.lang.ref.*;
       
    32 import jdk.internal.org.objectweb.asm.*;
       
    33 import sun.misc.Unsafe;
       
    34 
       
    35 import static jdk.internal.org.objectweb.asm.Opcodes.*;
       
    36 
       
    37 public class CallSiteDepContextTest {
       
    38     static final Unsafe               UNSAFE = Unsafe.getUnsafe();
       
    39     static final MethodHandles.Lookup LOOKUP = MethodHandles.Lookup.IMPL_LOOKUP;
       
    40     static final String           CLASS_NAME = "java/lang/invoke/Test";
       
    41     static final String          METHOD_NAME = "m";
       
    42     static final MethodType             TYPE = MethodType.methodType(int.class);
       
    43 
       
    44     static MutableCallSite mcs;
       
    45     static MethodHandle bsmMH;
       
    46 
       
    47     static {
       
    48         try {
       
    49             bsmMH = LOOKUP.findStatic(
       
    50                     CallSiteDepContextTest.class, "bootstrap",
       
    51                     MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class, MethodType.class));
       
    52         } catch(Throwable e) {
       
    53             throw new InternalError(e);
       
    54         }
       
    55     }
       
    56 
       
    57     public static CallSite bootstrap(MethodHandles.Lookup caller,
       
    58                                      String invokedName,
       
    59                                      MethodType invokedType) {
       
    60         return mcs;
       
    61     }
       
    62 
       
    63     static class T {
       
    64         static int f1() { return 1; }
       
    65         static int f2() { return 2; }
       
    66     }
       
    67 
       
    68     static byte[] getClassFile(String suffix) {
       
    69         ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
       
    70         MethodVisitor mv;
       
    71         cw.visit(52, ACC_PUBLIC | ACC_SUPER, CLASS_NAME + suffix, null, "java/lang/Object", null);
       
    72         {
       
    73             mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, METHOD_NAME, TYPE.toMethodDescriptorString(), null, null);
       
    74             mv.visitCode();
       
    75             Handle bsm = new Handle(H_INVOKESTATIC,
       
    76                     "java/lang/invoke/CallSiteDepContextTest", "bootstrap",
       
    77                     bsmMH.type().toMethodDescriptorString());
       
    78             mv.visitInvokeDynamicInsn("methodName", TYPE.toMethodDescriptorString(), bsm);
       
    79             mv.visitInsn(IRETURN);
       
    80             mv.visitMaxs(0, 0);
       
    81             mv.visitEnd();
       
    82         }
       
    83         cw.visitEnd();
       
    84         return cw.toByteArray();
       
    85     }
       
    86 
       
    87     private static void execute(int expected, MethodHandle... mhs) throws Throwable {
       
    88         for (int i = 0; i < 20_000; i++) {
       
    89             for (MethodHandle mh : mhs) {
       
    90                 int r = (int) mh.invokeExact();
       
    91                 if (r != expected) {
       
    92                     throw new Error(r + " != " + expected);
       
    93                 }
       
    94             }
       
    95         }
       
    96     }
       
    97 
       
    98     public static void testSharedCallSite() throws Throwable {
       
    99         Class<?> cls1 = UNSAFE.defineAnonymousClass(Object.class, getClassFile("CS_1"), null);
       
   100         Class<?> cls2 = UNSAFE.defineAnonymousClass(Object.class, getClassFile("CS_2"), null);
       
   101 
       
   102         MethodHandle[] mhs = new MethodHandle[] {
       
   103             LOOKUP.findStatic(cls1, METHOD_NAME, TYPE),
       
   104             LOOKUP.findStatic(cls2, METHOD_NAME, TYPE)
       
   105         };
       
   106 
       
   107         mcs = new MutableCallSite(LOOKUP.findStatic(T.class, "f1", TYPE));
       
   108         execute(1, mhs);
       
   109         mcs.setTarget(LOOKUP.findStatic(T.class, "f2", TYPE));
       
   110         execute(2, mhs);
       
   111     }
       
   112 
       
   113     public static void testNonBoundCallSite() throws Throwable {
       
   114         mcs = new MutableCallSite(LOOKUP.findStatic(T.class, "f1", TYPE));
       
   115 
       
   116         // mcs.context == null
       
   117         MethodHandle mh = mcs.dynamicInvoker();
       
   118         execute(1, mh);
       
   119 
       
   120         // mcs.context == cls1
       
   121         Class<?> cls1 = UNSAFE.defineAnonymousClass(Object.class, getClassFile("NonBound_1"), null);
       
   122         MethodHandle mh1 = LOOKUP.findStatic(cls1, METHOD_NAME, TYPE);
       
   123 
       
   124         execute(1, mh1);
       
   125 
       
   126         mcs.setTarget(LOOKUP.findStatic(T.class, "f2", TYPE));
       
   127 
       
   128         execute(2, mh, mh1);
       
   129     }
       
   130 
       
   131     static ReferenceQueue rq = new ReferenceQueue();
       
   132     static PhantomReference ref;
       
   133 
       
   134     public static void testGC() throws Throwable {
       
   135         mcs = new MutableCallSite(LOOKUP.findStatic(T.class, "f1", TYPE));
       
   136 
       
   137         Class<?>[] cls = new Class[] {
       
   138                 UNSAFE.defineAnonymousClass(Object.class, getClassFile("GC_1"), null),
       
   139                 UNSAFE.defineAnonymousClass(Object.class, getClassFile("GC_2"), null),
       
   140         };
       
   141 
       
   142         MethodHandle[] mhs = new MethodHandle[] {
       
   143                 LOOKUP.findStatic(cls[0], METHOD_NAME, TYPE),
       
   144                 LOOKUP.findStatic(cls[1], METHOD_NAME, TYPE),
       
   145         };
       
   146 
       
   147         // mcs.context == cls[0]
       
   148         int r = (int) mhs[0].invokeExact();
       
   149 
       
   150         execute(1, mhs);
       
   151 
       
   152         ref = new PhantomReference<>(cls[0], rq);
       
   153         cls[0] = UNSAFE.defineAnonymousClass(Object.class, getClassFile("GC_3"), null);
       
   154         mhs[0] = LOOKUP.findStatic(cls[0], METHOD_NAME, TYPE);
       
   155 
       
   156         do {
       
   157             System.gc();
       
   158             try {
       
   159                 Reference ref1 = rq.remove(1000);
       
   160                 if (ref1 == ref) {
       
   161                     ref1.clear();
       
   162                     System.gc(); // Ensure that the stale context is cleared
       
   163                     break;
       
   164                 }
       
   165             } catch(InterruptedException e) { /* ignore */ }
       
   166         } while (true);
       
   167 
       
   168         execute(1, mhs);
       
   169         mcs.setTarget(LOOKUP.findStatic(T.class, "f2", TYPE));
       
   170         execute(2, mhs);
       
   171     }
       
   172 
       
   173     public static void main(String[] args) throws Throwable {
       
   174         testSharedCallSite();
       
   175         testNonBoundCallSite();
       
   176         testGC();
       
   177         System.out.println("TEST PASSED");
       
   178     }
       
   179 }