1 /*
   2  * Copyright (c) 2009, 2016, 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 /* @test
  25  * @summary white-box testing of method handle sub-primitives
  26  * @run junit test.java.lang.invoke.PrivateInvokeTest
  27  */
  28 
  29 package test.java.lang.invoke;
  30 
  31 import java.lang.invoke.*;
  32 import static java.lang.invoke.MethodHandles.*;
  33 import static java.lang.invoke.MethodType.*;
  34 import java.lang.reflect.*;
  35 import java.util.ArrayList;
  36 import java.util.Arrays;
  37 import org.junit.*;
  38 import static org.junit.Assert.*;
  39 
  40 public class PrivateInvokeTest {
  41     // Utility functions
  42     private static final Lookup LOOKUP = lookup();
  43     private static final Class<?> THIS_CLASS = PrivateInvokeTest.class;
  44     private static final int
  45             REF_NONE                    = 0,  // null value
  46             REF_getField                = 1,
  47             REF_getStatic               = 2,
  48             REF_putField                = 3,
  49             REF_putStatic               = 4,
  50             REF_invokeVirtual           = 5,
  51             REF_invokeStatic            = 6,
  52             REF_invokeSpecial           = 7,
  53             REF_newInvokeSpecial        = 8,
  54             REF_invokeInterface         = 9,
  55             REF_LIMIT                  = 10,
  56             REF_MH_invokeBasic         = REF_NONE;;
  57     private static final String[] REF_KIND_NAMES = {
  58         "MH::invokeBasic",
  59         "REF_getField", "REF_getStatic", "REF_putField", "REF_putStatic",
  60         "REF_invokeVirtual", "REF_invokeStatic", "REF_invokeSpecial",
  61         "REF_newInvokeSpecial", "REF_invokeInterface"
  62     };
  63     private int verbose;
  64     //{ verbose = 99; }  // for debugging
  65     {
  66         String vstr = System.getProperty(THIS_CLASS.getSimpleName()+".verbose");
  67         if (vstr == null)
  68             vstr = System.getProperty(THIS_CLASS.getName()+".verbose");
  69         if (vstr == null)
  70             vstr = System.getProperty("test.verbose");
  71         if (vstr != null)  verbose = Integer.parseInt(vstr);
  72     }
  73     private static int referenceKind(Method m) {
  74         if (Modifier.isStatic(m.getModifiers()))
  75             return REF_invokeStatic;
  76         else if (m.getDeclaringClass().isInterface())
  77             return REF_invokeInterface;
  78         else if (Modifier.isFinal(m.getModifiers()) ||
  79             Modifier.isFinal(m.getDeclaringClass().getModifiers()))
  80             return REF_invokeSpecial;
  81         else
  82             return REF_invokeVirtual;
  83     }
  84     private static MethodType basicType(MethodType mtype) {
  85         MethodType btype = mtype.erase();
  86         if (btype.hasPrimitives()) {
  87             for (int i = -1; i < mtype.parameterCount(); i++) {
  88                 Class<?> type = (i < 0 ? mtype.returnType() : mtype.parameterType(i));
  89                 if (type == boolean.class ||
  90                     type == byte.class ||
  91                     type == char.class ||
  92                     type == short.class) {
  93                     type = int.class;
  94                     if (i < 0)
  95                         btype = btype.changeReturnType(type);
  96                     else
  97                         btype = btype.changeParameterType(i, type);
  98                 }
  99             }
 100         }
 101         return btype;
 102     }
 103     private static Method getMethod(Class<?> defc, String name, Class<?>... ptypes) {
 104         try {
 105             return defc.getDeclaredMethod(name, ptypes);
 106         } catch (NoSuchMethodException ex) {
 107         }
 108         try {
 109             return defc.getMethod(name, ptypes);
 110         } catch (NoSuchMethodException ex) {
 111             throw new IllegalArgumentException(ex);
 112         }
 113     }
 114     private static MethodHandle unreflect(Method m) {
 115         try {
 116             MethodHandle mh = LOOKUP.unreflect(m);
 117             if (Modifier.isTransient(m.getModifiers()))
 118                 mh = mh.asFixedArity();  // remove varargs wrapper
 119             return mh;
 120         } catch (IllegalAccessException ex) {
 121             throw new IllegalArgumentException(ex);
 122         }
 123     }
 124     private static final Lookup DIRECT_INVOKER_LOOKUP;
 125     private static final Class<?> MEMBER_NAME_CLASS;
 126     private static final MethodHandle MH_INTERNAL_MEMBER_NAME;
 127     private static final MethodHandle MH_DEBUG_STRING;
 128     static {
 129         try {
 130             // This is white box testing.  Use reflection to grab private implementation bits.
 131             String magicName = "IMPL_LOOKUP";
 132             Field magicLookup = MethodHandles.Lookup.class.getDeclaredField(magicName);
 133             // This unit test will fail if a security manager is installed.
 134             magicLookup.setAccessible(true);
 135             // Forbidden fruit...
 136             DIRECT_INVOKER_LOOKUP = (Lookup) magicLookup.get(null);
 137             MEMBER_NAME_CLASS = Class.forName("java.lang.invoke.MemberName", false, MethodHandle.class.getClassLoader());
 138             MH_INTERNAL_MEMBER_NAME = DIRECT_INVOKER_LOOKUP
 139                     .findVirtual(MethodHandle.class, "internalMemberName", methodType(MEMBER_NAME_CLASS))
 140                     .asType(methodType(Object.class, MethodHandle.class));
 141             MH_DEBUG_STRING = DIRECT_INVOKER_LOOKUP
 142                     .findVirtual(MethodHandle.class, "debugString", methodType(String.class));
 143         } catch (ReflectiveOperationException ex) {
 144             throw new Error(ex);
 145         }
 146     }
 147     private Object internalMemberName(MethodHandle mh) {
 148         try {
 149             return MH_INTERNAL_MEMBER_NAME.invokeExact(mh);
 150         } catch (Throwable ex) {
 151             throw new Error(ex);
 152         }
 153     }
 154     private String debugString(MethodHandle mh) {
 155         try {
 156             return (String) MH_DEBUG_STRING.invokeExact(mh);
 157         } catch (Throwable ex) {
 158             throw new Error(ex);
 159         }
 160     }
 161     private static MethodHandle directInvoker(int refKind, MethodType mtype) {
 162         return directInvoker(REF_KIND_NAMES[refKind], mtype);
 163     }
 164     private static MethodHandle directInvoker(String name, MethodType mtype) {
 165         boolean isStatic;
 166         mtype = mtype.erase();
 167         if (name.startsWith("MH::")) {
 168             isStatic = false;
 169             name = strip("MH::", name);
 170         } else if (name.startsWith("REF_")) {
 171             isStatic = true;
 172             name = strip("REF_", name);
 173             if (name.startsWith("invoke"))
 174                 name = "linkTo"+strip("invoke", name);
 175             mtype = mtype.appendParameterTypes(MEMBER_NAME_CLASS);
 176         } else {
 177             throw new AssertionError("name="+name);
 178         }
 179         //System.out.println("directInvoker = "+name+mtype);
 180         try {
 181             if (isStatic)
 182                 return DIRECT_INVOKER_LOOKUP
 183                         .findStatic(MethodHandle.class, name, mtype);
 184             else
 185                 return DIRECT_INVOKER_LOOKUP
 186                         .findVirtual(MethodHandle.class, name, mtype);
 187         } catch (ReflectiveOperationException ex) {
 188             throw new IllegalArgumentException(ex);
 189         }
 190     }
 191     private Object invokeWithArguments(Method m, Object... args) {
 192         Object recv = null;
 193         if (!Modifier.isStatic(m.getModifiers())) {
 194             recv = args[0];
 195             args = pop(1, args);
 196         }
 197         try {
 198             return m.invoke(recv, args);
 199         } catch (IllegalAccessException|IllegalArgumentException|InvocationTargetException ex) {
 200             throw new IllegalArgumentException(ex);
 201         }
 202     }
 203     private Object invokeWithArguments(MethodHandle mh, Object... args) {
 204         try {
 205             return mh.invokeWithArguments(args);
 206         } catch (Throwable ex) {
 207             throw new IllegalArgumentException(ex);
 208         }
 209     }
 210     private int counter;
 211     private Object makeArgument(Class<?> type) {
 212         final String cname = type.getSimpleName();
 213         final int n = ++counter;
 214         final int nn = (n << 10) + 13;
 215         if (type.isAssignableFrom(String.class)) {
 216             return "<"+cname+"#"+nn+">";
 217         }
 218         if (type == THIS_CLASS)  return this.withCounter(nn);
 219         if (type == Integer.class   || type == int.class)     return nn;
 220         if (type == Character.class || type == char.class)    return (char)(n % 100+' ');
 221         if (type == Byte.class      || type == byte.class)    return (byte)-(n % 100);
 222         if (type == Long.class      || type == long.class)    return (long)nn;
 223         throw new IllegalArgumentException("don't know how to make argument of type: "+type);
 224     }
 225     private Object[] makeArguments(Class<?>... ptypes) {
 226         Object[] args = new Object[ptypes.length];
 227         for (int i = 0; i < args.length; i++)
 228             args[i] = makeArgument(ptypes[i]);
 229         return args;
 230     }
 231     private Object[] makeArguments(MethodType mtype) {
 232         return makeArguments(mtype.parameterArray());
 233     }
 234     private Object[] pop(int n, Object[] args) {
 235         if (n >= 0)
 236             return Arrays.copyOfRange(args, n, args.length);
 237         else
 238             return Arrays.copyOfRange(args, 0, args.length+n);
 239     }
 240     private Object[] pushAtFront(Object arg1, Object[] args) {
 241         Object[] res = new Object[1+args.length];
 242         res[0] = arg1;
 243         System.arraycopy(args, 0, res, 1, args.length);
 244         return res;
 245     }
 246     private Object[] pushAtBack(Object[] args, Object argN) {
 247         Object[] res = new Object[1+args.length];
 248         System.arraycopy(args, 0, res, 0, args.length);
 249         res[args.length] = argN;
 250         return res;
 251     }
 252     private static String strip(String prefix, String s) {
 253         assert(s.startsWith(prefix));
 254         return s.substring(prefix.length());
 255     }
 256 
 257     private final int[] refKindTestCounts = new int[REF_KIND_NAMES.length];
 258     @After
 259     public void printCounts() {
 260         ArrayList<String> zeroes = new ArrayList<>();
 261         for (int i = 0; i < refKindTestCounts.length; i++) {
 262             final int count = refKindTestCounts[i];
 263             final String name = REF_KIND_NAMES[i];
 264             if (count == 0) {
 265                 if (name != null)  zeroes.add(name);
 266                 continue;
 267             }
 268             if (verbose >= 0)
 269                 System.out.println("test count for "+name+" : "+count);
 270             else if (name != null)
 271                 zeroes.add(name);
 272         }
 273         if (verbose >= 0)
 274             System.out.println("test counts zero for "+zeroes);
 275     }
 276 
 277     // Test subjects
 278     public static String makeString(Object x) { return "makeString("+x+")"; }
 279     public static String dupString(String x) { return "("+x+"+"+x+")"; }
 280     public static String intString(int x) { return "intString("+x+")"; }
 281     public static String byteString(byte x) { return "byteString("+x+")"; }
 282     public static String longString(String x, long y, String z) { return "longString("+x+y+z+")"; }
 283 
 284     public final String toString() {
 285         return "<"+getClass().getSimpleName()+"#"+counter+">";
 286     }
 287     public final String hello() { return "hello from "+this; }
 288     private PrivateInvokeTest withCounter(int counter) {
 289         PrivateInvokeTest res = new PrivateInvokeTest();
 290         res.counter = counter;
 291         return res;
 292     }
 293 
 294     public static void main(String... av) throws Throwable {
 295         new PrivateInvokeTest().run();
 296     }
 297     public void run() throws Throwable {
 298         testFirst();
 299         testInvokeDirect();
 300     }
 301 
 302     @Test
 303     public void testFirst() throws Throwable {
 304         if (true)  return;  // nothing here
 305         try {
 306             System.out.println("start of testFirst");
 307         } finally {
 308             System.out.println("end of testFirst");
 309         }
 310     }
 311 
 312     @Test
 313     public void testInvokeDirect() {
 314         testInvokeDirect(getMethod(THIS_CLASS, "hello"));
 315         testInvokeDirect(getMethod(Object.class, "toString"));
 316         testInvokeDirect(getMethod(Comparable.class, "compareTo", Object.class));
 317         testInvokeDirect(getMethod(THIS_CLASS, "makeString", Object.class));
 318         testInvokeDirect(getMethod(THIS_CLASS, "dupString", String.class));
 319         testInvokeDirect(getMethod(THIS_CLASS, "intString", int.class));
 320         testInvokeDirect(getMethod(THIS_CLASS, "byteString", byte.class));
 321         testInvokeDirect(getMethod(THIS_CLASS, "longString", String.class, long.class, String.class));
 322     }
 323 
 324     void testInvokeDirect(Method m) {
 325         final int refKind = referenceKind(m);
 326         testInvokeDirect(m, refKind);
 327         testInvokeDirect(m, REF_MH_invokeBasic);
 328     }
 329     void testInvokeDirect(Method m, int refKind) {
 330         if (verbose >= 1)
 331             System.out.println("testInvoke m="+m+" : "+REF_KIND_NAMES[refKind]);
 332         final MethodHandle mh = unreflect(m);
 333         Object[] args = makeArguments(mh.type());
 334         Object res1 = invokeWithArguments(m, args);
 335         // res1 comes from java.lang.reflect.Method::invoke
 336         if (verbose >= 1)
 337             System.out.println("m"+Arrays.asList(args)+" => "+res1);
 338         // res2 comes from java.lang.invoke.MethodHandle::invoke
 339         Object res2 = invokeWithArguments(mh, args);
 340         assertEquals(res1, res2);
 341         MethodType mtype = mh.type();
 342         testInvokeVia("DMH invoker", refKind, directInvoker(refKind, mtype), mh, res1, args);
 343         MethodType etype = mtype.erase();
 344         if (etype != mtype) {
 345             // Try a detuned invoker.
 346             testInvokeVia("erased DMH invoker", refKind, directInvoker(refKind, etype), mh, res1, args);
 347         }
 348         MethodType btype = basicType(mtype);
 349         if (btype != mtype && btype != etype) {
 350             // Try a detuned invoker.
 351             testInvokeVia("basic DMH invoker", refKind, directInvoker(refKind, btype), mh, res1, args);
 352         }
 353         if (false) {
 354             // this can crash the JVM
 355             testInvokeVia("generic DMH invoker", refKind, directInvoker(refKind, mtype.generic()), mh, res1, args);
 356         }
 357         refKindTestCounts[refKind] += 1;
 358     }
 359 
 360     void testInvokeVia(String kind, int refKind, MethodHandle invoker, MethodHandle mh, Object res1, Object... args) {
 361         Object[] args1;
 362         if (refKind == REF_MH_invokeBasic)
 363             args1 = pushAtFront(mh, args);
 364         else
 365             args1 = pushAtBack(args, internalMemberName(mh));
 366         if (verbose >= 2) {
 367             System.out.println(kind+" invoker="+invoker+" mh="+debugString(mh)+" args="+Arrays.asList(args1));
 368         }
 369         Object res3 = invokeWithArguments(invoker, args1);
 370         assertEquals(res1, res3);
 371     }
 372 }