File: PrivateInterfaceCall.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (316 lines) | stat: -rw-r--r-- 13,323 bytes parent folder | download | duplicates (16)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/**
 * @test
 * @bug 8046171
 * @summary Test direct and MethodHandle access to private interface methods using invokeinterface semantics
 *          to ensure all receiver typechecks occur as required.
 * @comment This complements SpecialInterfaceCall which tests invokespecial semantics.
 * @compile PrivateInterfaceCall.java
 * @compile PrivateInterfaceCallI4.jasm
 * @run main/othervm -Xint PrivateInterfaceCall
 * @run main/othervm -Xbatch -XX:+TieredCompilation -XX:TieredStopAtLevel=1 PrivateInterfaceCall
 * @run main/othervm -Xbatch -XX:+TieredCompilation -XX:TieredStopAtLevel=2 PrivateInterfaceCall
 * @run main/othervm -Xbatch -XX:+TieredCompilation -XX:TieredStopAtLevel=3 PrivateInterfaceCall
 * @run main/othervm -Xbatch -XX:-TieredCompilation PrivateInterfaceCall
 */

// This is an adaptation of SpecialInterfaceCall to only use private interface methods and with
// virtual invocation semantics. Because we don't have the same corner cases as for invokespecial
// there's no practical difference between the I3 and I2 cases here. But we do have to ensure the
// correct versions of the methods get executed.
// In addition we add tests that involve calls from nestmates - which also covers the distinction
// between the caller being a class and being an interface.

import java.lang.invoke.*;

public class PrivateInterfaceCall {
    interface I1 {
        private void priv_m() { throw new Error("Should not call this"); };
    }
    interface I2 extends I1 {
        private void priv_m() { };

        static void invokeDirect(I2 i) {
            i.priv_m(); // generates invokeinterface
        }
        static void invokeInterfaceMH(I2 i) throws Throwable {
            // emulates behaviour of invokeDirect
            mh_I2_priv_m_from_I2.invokeExact(i);
        }
        // special case of invoking an Object method via an interface
        static void invokeInterfaceObjectMH(I2 i) throws Throwable {
            // emulates invokeInterface of I2.toString on i, which resolves
            // to Object.toString
            String s = (String) mh_I2_toString_from_I2.invokeExact(i);
        }
        // special case of invoking a final Object method via an interface
        static void invokeInterfaceObjectFinalMH(I2 i) throws Throwable {
            // emulates invokeInterface of I1.getClass on i, which resolves
            // to Object.getClass
            Class<?> c = (Class<?>) mh_I2_getClass_from_I2.invokeExact(i);
        }

        static void init() throws Throwable {
            MethodType mt = MethodType.methodType(void.class);
            MethodHandles.Lookup lookup = MethodHandles.lookup();
            mh_I2_priv_m_from_I2 = lookup.findVirtual(I2.class, "priv_m", mt);

            mt = MethodType.methodType(String.class);
            mh_I2_toString_from_I2 = lookup.findVirtual(I2.class, "toString", mt);

            mt = MethodType.methodType(Class.class);
            mh_I2_getClass_from_I2 = lookup.findVirtual(I2.class, "getClass", mt);
        }
    }
    interface I3 extends I2 {
        static void invokeInterfaceMH(I2 i) throws Throwable {
            // emulates behaviour of I2.invokeDirect
            mh_I2_priv_m_from_I3.invokeExact(i);
        }
        static void init() throws Throwable {
            MethodType mt = MethodType.methodType(void.class);
            mh_I2_priv_m_from_I3 = MethodHandles.lookup().findVirtual(I2.class, "priv_m", mt);
        }
    }

    // This interface acts like I2 but we define directInvoke* methods
    // that we will rewrite the bytecode of to use invokeinterface
    // (see PrivateInterfaceCallI4.jasm).
    interface I4 extends I1 {
        static void invokeDirect(I4 i) {
            // invokeinterface I4.toString()
            throw new Error("Class file for I4 is not overwritten");
        }
        static void invokeDirectFinal(I4 i) {
            // invokeinterface I4.getClass() - final method
            throw new Error("Class file for I4 is not overwritten");
        }
    }

    // check invocations from nestmates outside the
    // inheritance hierarchy - and from a class not interface
    static void invokeDirect(I2 i) {
        i.priv_m(); // generates invokeinterface
    }
    static void invokeInterfaceMH(I2 i) throws Throwable {
        mh_I2_priv_m_from_PIC.invokeExact(i);
    }

    // Concrete classes
    static class C2 implements I2 { }
    static class C3 implements I3 { }
    static class C4 implements I4 { }

    // Classes that don't implement I2/I3 but do have a
    // priv_m method in their hierarchy
    static class D1 implements I1 { }
    static class E {
        private void priv_m() { throw new Error("Should not call this"); }
    }

    // This MH acts like the invocation in I2.invokeDirect with caller I2
    static MethodHandle mh_I2_priv_m_from_I2;

    // This MH acts like the invocation in I3.invokeDirect with caller I3
    static MethodHandle mh_I2_priv_m_from_I3;

    // This MH acts like the invocation in PrivateInterfaceCall.invokeDirect
    // with caller PrivateInterfaceCall
    static MethodHandle mh_I2_priv_m_from_PIC;

   // This MH acts likes an invokeinterface of I2.toString from I2
    static MethodHandle mh_I2_toString_from_I2;

    // This MH acts likes an invokeinterface of I2.getClass from I2
    static MethodHandle mh_I2_getClass_from_I2;

    static {
        try {
            MethodType mt = MethodType.methodType(void.class);
            mh_I2_priv_m_from_PIC = MethodHandles.lookup().findVirtual(I2.class, "priv_m", mt);
            I2.init();
            I3.init();
        } catch (Throwable e) {
            throw new Error(e);
        }
    }

    static void runPositiveTests() {
        shouldNotThrow(() -> PrivateInterfaceCall.invokeDirect(new C2()));
        shouldNotThrow(() -> PrivateInterfaceCall.invokeDirect(new C3()));
        shouldNotThrow(() -> PrivateInterfaceCall.invokeInterfaceMH(new C2()));
        shouldNotThrow(() -> PrivateInterfaceCall.invokeInterfaceMH(new C3()));

        shouldNotThrow(() -> I2.invokeDirect(new C2()));
        shouldNotThrow(() -> I2.invokeDirect(new C3()));
        shouldNotThrow(() -> I2.invokeInterfaceMH(new C2()));
        shouldNotThrow(() -> I2.invokeInterfaceMH(new C3()));
        shouldNotThrow(() -> I2.invokeInterfaceObjectMH(new C2()));
        shouldNotThrow(() -> I2.invokeInterfaceObjectMH(new C3()));
        shouldNotThrow(() -> I2.invokeInterfaceObjectFinalMH(new C2()));
        shouldNotThrow(() -> I2.invokeInterfaceObjectFinalMH(new C3()));

        // This looks odd but at runtime the only constraint is that the
        // receiver is an I2. In contrast in the invokespecial case the
        // receiver must be an I3.
        shouldNotThrow(() -> I3.invokeInterfaceMH(unsafeCastI3(new C2())));
        shouldNotThrow(() -> I3.invokeInterfaceMH(new C3()));

        shouldNotThrow(() -> I4.invokeDirect(new C4()));
        shouldNotThrow(() -> I4.invokeDirectFinal(new C4()));
    }

    static void runNegativeTests() {
        System.out.println("ICCE PrivateInterfaceCall.invokeDirect D1");
        shouldThrowICCE(() -> PrivateInterfaceCall.invokeDirect(unsafeCastI2(new D1())));
        System.out.println("ICCE PrivateInterfaceCall.invokeDirect E");
        shouldThrowICCE(() -> PrivateInterfaceCall.invokeDirect(unsafeCastI2(new E())));
        System.out.println("ICCE PrivateInterfaceCall.invokeInterfaceMH D1");
        shouldThrowICCE(() -> PrivateInterfaceCall.invokeInterfaceMH(unsafeCastI2(new D1())));
        System.out.println("ICCE PrivateInterfaceCall.invokeInterfaceMH E");
        shouldThrowICCE(() -> PrivateInterfaceCall.invokeInterfaceMH(unsafeCastI2(new E())));


        System.out.println("ICCE I2.invokeInterfaceMH D1");
        shouldThrowICCE(() -> I2.invokeInterfaceMH(unsafeCastI2(new D1())));
        System.out.println("ICCE I2.invokeInterfaceMH E");
        shouldThrowICCE(() -> I2.invokeInterfaceMH(unsafeCastI2(new E())));

        System.out.println("ICCE I2.invokeInterfaceObjectFinalMH D1");
        shouldThrowICCE(() -> I2.invokeInterfaceObjectFinalMH(unsafeCastI2(new D1())));
        System.out.println("ICCE I2.invokeInterfaceObjectFinalMH E");
        shouldThrowICCE(() -> I2.invokeInterfaceObjectFinalMH(unsafeCastI2(new E())));

        System.out.println("ICCE I3.invokeInterfaceMH D1");
        shouldThrowICCE(() -> I3.invokeInterfaceMH(unsafeCastI3(new D1())));
        System.out.println("ICCE I3.invokeInterfaceMH E");
        shouldThrowICCE(() -> I3.invokeInterfaceMH(unsafeCastI3(new E())));

        System.out.println("ICCE I4.invokeDirect D1");
        shouldThrowICCE(() -> I4.invokeDirect(unsafeCastI4(new D1())));
        System.out.println("ICCE I4.invokeDirect E");
        shouldThrowICCE(() -> I4.invokeDirect(unsafeCastI4(new E())));
    }

    static void warmup() {
        for (int i = 0; i < 20_000; i++) {
            runPositiveTests();
        }
    }

    public static void main(String[] args) throws Throwable {
        System.out.println("UNRESOLVED:");
        runNegativeTests();
        runPositiveTests();

        System.out.println("RESOLVED:");
        runNegativeTests();

        System.out.println("WARMUP:");
        warmup();

        System.out.println("COMPILED:");
        runNegativeTests();
        runPositiveTests();
    }

    static interface Test {
        void run() throws Throwable;
    }

    static void shouldThrowICCE(Test t) {
        shouldThrow(IncompatibleClassChangeError.class,
                    "does not implement the requested interface", t);
    }

    // Depending on whether the exception originates in the linkResolver, the interpreter
    // or the compiler, the message can be different - which is unfortunate and could be
    // fixed. So we allow the listed reason or else a null message.
    static void shouldThrow(Class<?> expectedError, String reason, Test t) {
        try {
            t.run();
        } catch (Throwable e) {
            // Don't accept subclasses as they can hide unexpected failure modes
            if (expectedError == e.getClass()) {
                String msg = e.getMessage();
                if ((msg != null && msg.contains(reason)) || msg == null) {
                    // passed
                    System.out.println("Threw expected: " + e);
                    return;
                }
                else {
                    throw new AssertionError("Wrong exception reason: expected '" + reason
                                             + "', got '" + msg + "'", e);
                }
            } else {
                String msg = String.format("Wrong exception thrown: expected=%s; thrown=%s",
                                           expectedError.getName(), e.getClass().getName());
                throw new AssertionError(msg, e);
            }
        }
        throw new AssertionError("No exception thrown: expected " + expectedError.getName());
    }

    static void shouldNotThrow(Test t) {
        try {
            t.run();
            // passed
        } catch (Throwable e) {
            throw new AssertionError("Exception was thrown: ", e);
        }
    }

    // Note: these unsafe casts are only possible for interface types

    static I2 unsafeCastI2(Object obj) {
        try {
            MethodHandle mh = MethodHandles.identity(Object.class);
            mh = MethodHandles.explicitCastArguments(mh, mh.type().changeReturnType(I2.class));
            return (I2)mh.invokeExact((Object) obj);
        } catch (Throwable e) {
            throw new Error(e);
        }
    }

    static I3 unsafeCastI3(Object obj) {
        try {
            MethodHandle mh = MethodHandles.identity(Object.class);
            mh = MethodHandles.explicitCastArguments(mh, mh.type().changeReturnType(I3.class));
            return (I3)mh.invokeExact((Object) obj);
        } catch (Throwable e) {
            throw new Error(e);
        }
    }

    static I4 unsafeCastI4(Object obj) {
        try {
            MethodHandle mh = MethodHandles.identity(Object.class);
            mh = MethodHandles.explicitCastArguments(mh, mh.type().changeReturnType(I4.class));
            return (I4)mh.invokeExact((Object) obj);
        } catch (Throwable e) {
            throw new Error(e);
        }
    }
}