1 /*
   2  * Copyright (c) 2018, 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 8200167
  27  * @summary Test direct and MethodHandle access to interface methods using invokespecial semantics
  28  * @compile SpecialInterfaceCall.java
  29  * @compile SpecialInterfaceCallI4.jasm
  30  * @run main/othervm -Xint SpecialInterfaceCall
  31  * @run main/othervm -Xbatch -XX:+TieredCompilation -XX:TieredStopAtLevel=1 SpecialInterfaceCall
  32  * @run main/othervm -Xbatch -XX:-TieredCompilation SpecialInterfaceCall
  33  */
  34 
  35 import java.lang.invoke.*;
  36 
  37 public class SpecialInterfaceCall {
  38     interface I1 {
  39         default void pub_m() {};
  40         private void priv_m() {};
  41     }
  42     interface I2 extends I1 {
  43         // This needs to be a public method to avoid access control issues,
  44         // but logically we treat it as private and emulate invokespecial
  45         // using MethodHandles.
  46         default void pub_m() {};
  47 
  48         private void priv_m() {};
  49 
  50         static void invokeDirect(I2 i) {
  51             i.priv_m(); // generates invokespecial
  52         }
  53         static void invokeSpecialMH(I2 i) throws Throwable {
  54             // emulates behaviour of invokeDirect
  55             mh_I2_priv_m_from_I2.invokeExact(i);
  56         }
  57         // special case of invoking an Object method via an interface
  58         static void invokeSpecialObjectMH(I2 i) throws Throwable {
  59             // emulates invokespecial of I1.toString on i, which resolves
  60             // to Object.toString
  61             String s = (String) mh_I1_toString_from_I2.invokeExact(i);
  62         }
  63         // special case of invoking a final Object method via an interface
  64         static void invokeSpecialObjectFinalMH(I2 i) throws Throwable {
  65             // emulates invokespecial of I1.getClass on i, which resolves
  66             // to Object.getClass
  67             Class<?> c = (Class<?>) mh_I1_getClass_from_I2.invokeExact(i);
  68         }
  69     }
  70     interface I3 extends I2 {
  71         // Must take an I3 here rather than I2 else we get
  72         // WrongMethodTypeException: expected (I3)void but found (I2)void
  73         // Statically the receiver type is bounded by the caller type.
  74         static void invokeSpecialMH(I3 i) throws Throwable {
  75             // emulates an invokespecial of ((I2)i).pub_m()
  76             mh_I2_pub_m_from_I3.invokeExact(i);
  77         }
  78     }
  79     // This interface acts like I2 but we define directInvoke* methods
  80     // that we will rewrite the bytecode of to use invokespecial
  81     // (see SpecialInterfaceCallI4.jasm).
  82     interface I4 extends I1 {
  83         static void invokeDirect(I4 i) {
  84             // invokeSpecial Object.toString()
  85             throw new Error("Class file for I4 is not overwritten");
  86         }
  87         static void invokeDirectFinal(I4 i) {
  88             // invokeSpecial Object.getClass() - final method
  89             throw new Error("Class file for I4 is not overwritten");
  90         }
  91     }
  92 
  93     // Concrete classes
  94     static class C1 implements I1 { }
  95     static class C2 implements I2 { }
  96     static class C3 implements I3 { }
  97     static class C4 implements I4 { }
  98 
  99     // Classes that don't implement I2/I3 but do have a
 100     // priv_m/pub_m method in their hierarchy
 101     static class D1 implements I1 { }
 102     static class E {
 103         public void pub_m() {}
 104         private void priv_m() {}
 105     }
 106 
 107     // This MH acts like the direct invokespecial in I2.invokeDirect
 108     static final MethodHandle mh_I2_priv_m_from_I2;
 109 
 110     // This MH acts like an invokespecial of I2.pub_m from I3
 111     static final MethodHandle mh_I2_pub_m_from_I3;
 112 
 113     // This MH acts likes an invokespecial of I1.toString from I2
 114     static final MethodHandle mh_I1_toString_from_I2;
 115 
 116     // This MH acts likes an invokespecial of I1.getClass from I2
 117     static final MethodHandle mh_I1_getClass_from_I2;
 118 
 119     static {
 120         try {
 121             MethodType mt = MethodType.methodType(void.class);
 122             MethodHandles.Lookup lookup = MethodHandles.lookup();
 123 
 124             mh_I2_priv_m_from_I2 = lookup.findSpecial(I2.class, "priv_m", mt, I2.class);
 125             mh_I2_pub_m_from_I3 = lookup.findSpecial(I2.class, "pub_m", mt, I3.class);
 126 
 127             mt = MethodType.methodType(String.class);
 128             mh_I1_toString_from_I2 = lookup.findSpecial(I1.class, "toString", mt, I2.class);
 129 
 130             mt = MethodType.methodType(Class.class);
 131             mh_I1_getClass_from_I2 = lookup.findSpecial(I1.class, "getClass", mt, I2.class);
 132 
 133         } catch (Throwable e) {
 134             throw new Error(e);
 135         }
 136     }
 137 
 138     static void runPositiveTests() {
 139         shouldNotThrow(() -> I2.invokeDirect(new C2()));
 140         shouldNotThrow(() -> I2.invokeDirect(new C3()));
 141         shouldNotThrow(() -> I2.invokeSpecialMH(new C2()));
 142         shouldNotThrow(() -> I2.invokeSpecialMH(new C3()));
 143         shouldNotThrow(() -> I2.invokeSpecialObjectMH(new C2()));
 144         shouldNotThrow(() -> I2.invokeSpecialObjectMH(new C3()));
 145         shouldNotThrow(() -> I2.invokeSpecialObjectFinalMH(new C2()));
 146         shouldNotThrow(() -> I2.invokeSpecialObjectFinalMH(new C3()));
 147 
 148         shouldNotThrow(() -> I3.invokeSpecialMH(new C3()));
 149 
 150         shouldNotThrow(() -> I4.invokeDirect(new C4()));
 151         shouldNotThrow(() -> I4.invokeDirectFinal(new C4()));
 152     }
 153 
 154     static void runNegativeTests() {
 155         System.out.println("IAE I2.invokeDirect D1");
 156         shouldThrowIAE(() -> I2.invokeDirect(unsafeCastI2(new D1())));
 157         System.out.println("IAE I2.invokeDirect E");
 158         shouldThrowIAE(() -> I2.invokeDirect(unsafeCastI2(new E())));
 159         System.out.println("ICCE I2.invokeMH D1");
 160         shouldThrowICCE(() -> I2.invokeSpecialMH(unsafeCastI2(new D1())));
 161         System.out.println("ICCE I2.invokeMH E");
 162         shouldThrowICCE(() -> I2.invokeSpecialMH(unsafeCastI2(new E())));
 163         System.out.println("ICCE I3.invokeMH D1");
 164         shouldThrowICCE(() -> I3.invokeSpecialMH(unsafeCastI3(new D1())));
 165         System.out.println("ICCE I3.invokeMH E");
 166         shouldThrowICCE(() -> I3.invokeSpecialMH(unsafeCastI3(new E())));
 167         System.out.println("ICCE I3.invokeMH C2");
 168         shouldThrowICCE(() -> I3.invokeSpecialMH(unsafeCastI3(new C2())));
 169         System.out.println("ICCE I4.invokeDirect C1");
 170         shouldThrowIAE(() -> I4.invokeDirect(unsafeCastI4(new C1())));
 171         System.out.println("ICCE I4.invokeDirectFinal C1");
 172         shouldThrowIAE(() -> I4.invokeDirectFinal(unsafeCastI4(new C1())));
 173         System.out.println("ICCE I2.invokeObjectMH C1");
 174         shouldThrowICCE(() -> I2.invokeSpecialObjectMH(unsafeCastI2(new C1())));
 175         System.out.println("ICCE I2.invokeObjectFinalMH C1");
 176         shouldThrowICCE(() -> I2.invokeSpecialObjectFinalMH(unsafeCastI2(new C1())));
 177 
 178     }
 179 
 180     static void warmup() {
 181         for (int i = 0; i < 20_000; i++) {
 182             runPositiveTests();
 183         }
 184     }
 185 
 186     public static void main(String[] args) throws Throwable {
 187         System.out.println("UNRESOLVED:");
 188         runNegativeTests();
 189         runPositiveTests();
 190 
 191         System.out.println("RESOLVED:");
 192         runNegativeTests();
 193 
 194         System.out.println("WARMUP:");
 195         warmup();
 196 
 197         System.out.println("COMPILED:");
 198         runNegativeTests();
 199         runPositiveTests();
 200     }
 201 
 202     static interface Test {
 203         void run() throws Throwable;
 204     }
 205 
 206     static void shouldThrowICCE(Test t) {
 207         shouldThrow(IncompatibleClassChangeError.class,
 208                     "is not a subclass of caller class", t);
 209     }
 210 
 211     static void shouldThrowIAE(Test t) {
 212         shouldThrow(IllegalAccessError.class,
 213                     "must be the current class or a subtype of interface", t);
 214     }
 215 
 216     static void shouldThrow(Class<?> expectedError, String reason, Test t) {
 217         try {
 218             t.run();
 219         } catch (Throwable e) {
 220             if (expectedError.isInstance(e)) {
 221                 if (e.getMessage().contains(reason)) {
 222                     // passed
 223                     System.out.println("Threw expected: " + e);
 224                     return;
 225                 }
 226                 else {
 227                     throw new AssertionError("Wrong exception reason: expected '" + reason
 228                                              + "', got '" + e.getMessage() + "'", e);
 229                 }
 230             } else {
 231                 String msg = String.format("Wrong exception thrown: expected=%s; thrown=%s",
 232                                            expectedError.getName(), e.getClass().getName());
 233                 throw new AssertionError(msg, e);
 234             }
 235         }
 236         throw new AssertionError("No exception thrown: expected " + expectedError.getName());
 237     }
 238 
 239     static void shouldNotThrow(Test t) {
 240         try {
 241             t.run();
 242             // passed
 243         } catch (Throwable e) {
 244             throw new AssertionError("Exception was thrown: ", e);
 245         }
 246     }
 247 
 248     // Note: these unsafe casts are only possible for interface types
 249 
 250     static I2 unsafeCastI2(Object obj) {
 251         try {
 252             MethodHandle mh = MethodHandles.identity(Object.class);
 253             mh = MethodHandles.explicitCastArguments(mh, mh.type().changeReturnType(I2.class));
 254             return (I2)mh.invokeExact((Object) obj);
 255         } catch (Throwable e) {
 256             throw new Error(e);
 257         }
 258     }
 259 
 260     static I3 unsafeCastI3(Object obj) {
 261         try {
 262             MethodHandle mh = MethodHandles.identity(Object.class);
 263             mh = MethodHandles.explicitCastArguments(mh, mh.type().changeReturnType(I3.class));
 264             return (I3)mh.invokeExact((Object) obj);
 265         } catch (Throwable e) {
 266             throw new Error(e);
 267         }
 268     }
 269 
 270     static I4 unsafeCastI4(Object obj) {
 271         try {
 272             MethodHandle mh = MethodHandles.identity(Object.class);
 273             mh = MethodHandles.explicitCastArguments(mh, mh.type().changeReturnType(I4.class));
 274             return (I4)mh.invokeExact((Object) obj);
 275         } catch (Throwable e) {
 276             throw new Error(e);
 277         }
 278     }
 279 }