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