File: TestAMEnotNPE.java

package info (click to toggle)
openjdk-8-jre-dcevm 8u112-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 65,396 kB
  • sloc: cpp: 570,249; java: 257,943; xml: 16,359; ansic: 9,543; asm: 2,774; sh: 2,403; makefile: 2,391
file content (496 lines) | stat: -rw-r--r-- 21,034 bytes parent folder | download | duplicates (2)
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/*
 * Copyright (c) 2013, 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.
 *
 */
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import jdk.internal.org.objectweb.asm.ClassWriter;
import jdk.internal.org.objectweb.asm.Handle;
import jdk.internal.org.objectweb.asm.MethodVisitor;
import jdk.internal.org.objectweb.asm.Opcodes;
import p.Dok;

/**
 * @test @bug 8025260 8016839
 * @summary Ensure that AbstractMethodError and IllegalAccessError are thrown appropriately, not NullPointerException
 *
 * @compile -XDignore.symbol.file TestAMEnotNPE.java ByteClassLoader.java p/C.java p/Dok.java p/E.java p/F.java p/I.java p/Tdirect.java p/Treflect.java
 *
 * @run main/othervm TestAMEnotNPE
 * @run main/othervm -Xint TestAMEnotNPE
 * @run main/othervm -Xcomp TestAMEnotNPE
 */
public class TestAMEnotNPE implements Opcodes {

    static boolean writeJarFiles = false;
    static boolean readJarFiles = false;

    /**
     * Optional command line parameter (any case-insensitive prefix of)
     * "writejarfiles" or "readjarfiles".
     *
     * "Writejarfiles" creates a jar file for each different set of tested classes.
     * "Readjarfiles" causes the classloader to use the copies of the classes
     * found in the corresponding jar files.
     *
     * Jarfilenames look something like pD_ext_pF (p.D extends p.F)
     * and qD_m_pp_imp_pI (q.D with package-private m implements p.I)
     *
     */
    public static void main(String args[]) throws Throwable {
        ArrayList<Throwable> lt = new ArrayList<Throwable>();

        if (args.length > 0) {
            String a0 = args[0].toLowerCase();
            if (a0.length() > 0) {
                writeJarFiles = ("writejarfiles").startsWith(a0);
                readJarFiles = ("readjarfiles").startsWith(a0);
            }
            if (!(writeJarFiles || readJarFiles)) {
                throw new Error("Command line parameter (if any) should be prefix of writeJarFiles or readJarFiles");
            }
        }

        try {
            System.out.println("TRYING p.D.m PRIVATE interface-invoked as p.I.m, p.D extends p.F, p.F.m FINAL");
            tryAndCheckThrown(lt, bytesForDprivateSubWhat("p/F"),
                    "p.D extends p.F (p.F implements p.I, FINAL public m), private m",
                    IllegalAccessError.class, "pD_ext_pF");
            // We'll take either a VerifyError (pre 2013-11-30)
            // or an IllegalAccessError (post 2013-11-22)
        } catch (VerifyError ve) {
            System.out.println("Saw expected VerifyError " + ve);
        }
        System.out.println();

        System.out.println("TRYING p.D.m PRIVATE interface-invoked as p.I.m, p.D extends p.E");
        tryAndCheckThrown(lt, bytesForDprivateSubWhat("p/E"),
                "p.D extends p.E (p.E implements p.I, public m), private m",
                IllegalAccessError.class, "pD_ext_pE");

        System.out.println("TRYING p.D.m ABSTRACT interface-invoked as p.I.m");
        tryAndCheckThrown(lt, bytesForD(),
                "D extends abstract C, no m",
                AbstractMethodError.class, "pD_ext_pC");

        System.out.println("TRYING q.D.m PACKAGE interface-invoked as p.I.m");
        tryAndCheckThrown(lt, "q.D", bytesForDsomeAccess("q/D", 0),
                "q.D implements p.I, protected m", IllegalAccessError.class,
                "qD_m_pp_imp_pI");

        // Note jar file name is used in the plural-arg case.
        System.out.println("TRYING p.D.m PRIVATE interface-invoked as p.I.m");
        tryAndCheckThrown(lt, bytesForDsomeAccess("p/D", ACC_PRIVATE),
                "p.D implements p.I, private m",
                IllegalAccessError.class, "pD_m_pri_imp_pI");

        // Plural-arg test.
        System.out.println("TRYING p.D.m PRIVATE MANY ARG interface-invoked as p.I.m");
        tryAndCheckThrownMany(lt, bytesForDsomeAccess("p/D", ACC_PRIVATE),
                "p.D implements p.I, private m", IllegalAccessError.class);

        if (lt.size() > 0) {
            System.out.flush();
            Thread.sleep(250); // This de-interleaves output and error in Netbeans, sigh.
            for (Throwable th : lt)
              System.err.println(th);
            throw new Error("Test failed, there were " + lt.size() + " failures listed above");
        } else {
            System.out.println("ALL PASS, HOORAY!");
        }
    }

    /**
     * The bytes for D, a NOT abstract class extending abstract class C without
     * supplying an implementation for abstract method m. There is a default
     * method in the interface I, but it should lose to the abstract class.
     *
     * @return
     * @throws Exception
     */
    public static byte[] bytesForD() throws Exception {

        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES
                | ClassWriter.COMPUTE_MAXS);
        MethodVisitor mv;

        cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, "p/D", null, "p/C", null);

        {
            mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
            mv.visitCode();
            mv.visitVarInsn(ALOAD, 0);
            mv.visitMethodInsn(INVOKESPECIAL, "p/C", "<init>", "()V");
            mv.visitInsn(RETURN);
            mv.visitMaxs(0, 0);
            mv.visitEnd();
        }
        cw.visitEnd();

        return cw.toByteArray();
    }

    /**
     * The bytes for D, implements I, does not extend C, declares m()I with
     * access method_acc.
     *
     * @param d_name Name of class defined
     * @param method_acc Accessibility of that class's method m.
     * @return
     * @throws Exception
     */
    public static byte[] bytesForDsomeAccess(String d_name, int method_acc) throws Exception {
        return bytesForSomeDsubSomethingSomeAccess(d_name, "java/lang/Object", method_acc);
    }

    /**
     * The bytes for D implements I, extends some class, declares m()I as
     * private.
     *
     * Invokeinterface of I.m applied to this D should throw IllegalAccessError
     *
     * @param sub_what The name of the class that D will extend.
     * @return
     * @throws Exception
     */
    public static byte[] bytesForDprivateSubWhat(String sub_what) throws Exception {
        return bytesForSomeDsubSomethingSomeAccess("p/D", sub_what, ACC_PRIVATE);
    }

    /**
     * Returns the bytes for a class with name d_name (presumably "D" in some
     * package), extending some class with name sub_what, implementing p.I,
     * and defining two methods m() and m(11args) with access method_acc.
     *
     * @param d_name      Name of class that is defined
     * @param sub_what    Name of class that it extends
     * @param method_acc  Accessibility of method(s) m in defined class.
     * @return
     * @throws Exception
     */
    public static byte[] bytesForSomeDsubSomethingSomeAccess
            (String d_name, String sub_what, int method_acc)
            throws Exception {

        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES
                | ClassWriter.COMPUTE_MAXS);
        MethodVisitor mv;
        String[] interfaces = {"p/I"};

        cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, d_name, null, sub_what, interfaces);
        {
            mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
            mv.visitCode();
            mv.visitVarInsn(ALOAD, 0);
            mv.visitMethodInsn(INVOKESPECIAL, sub_what, "<init>", "()V");
            mv.visitInsn(RETURN);
            mv.visitMaxs(0, 0);
            mv.visitEnd();
        }
        // int m() {return 3;}
        {
            mv = cw.visitMethod(method_acc, "m", "()I", null, null);
            mv.visitCode();
            mv.visitLdcInsn(new Integer(3));
            mv.visitInsn(IRETURN);
            mv.visitMaxs(0, 0);
            mv.visitEnd();
        }
        // int m(11args) {return 3;}
        {
            mv = cw.visitMethod(method_acc, "m", "(BCSIJ"
                    + "Ljava/lang/Object;"
                    + "Ljava/lang/Object;"
                    + "Ljava/lang/Object;"
                    + "Ljava/lang/Object;"
                    + "Ljava/lang/Object;"
                    + "Ljava/lang/Object;"
                    + ")I", null, null);
            mv.visitCode();
            mv.visitLdcInsn(new Integer(3));
            mv.visitInsn(IRETURN);
            mv.visitMaxs(0, 0);
            mv.visitEnd();
        }
        cw.visitEnd();
        return cw.toByteArray();
    }

    /**
     * The bytecodes for a class p/T defining a methods test() and test(11args)
     * that contain an invokeExact of a particular methodHandle, I.m.
     *
     * Test will be passed values that may imperfectly implement I,
     * and thus may in turn throw exceptions.
     *
     * @return
     * @throws Exception
     */
    public static byte[] bytesForT() throws Exception {

        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES
                | ClassWriter.COMPUTE_MAXS);
        MethodVisitor mv;

        cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, "p/T", null, "java/lang/Object", null);
        {
            mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
            mv.visitCode();
            mv.visitVarInsn(ALOAD, 0);
            mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
            mv.visitInsn(RETURN);
            mv.visitMaxs(0, 0);
            mv.visitEnd();
        }
        // static int test(I)
        {
            mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "test", "(Lp/I;)I", null, null);
            mv.visitCode();
            mv.visitLdcInsn(new Handle(Opcodes.H_INVOKEINTERFACE, "p/I", "m", "()I"));
            mv.visitVarInsn(ALOAD, 0);
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandle",
                    "invokeExact", "(Lp/I;)I");
            mv.visitInsn(IRETURN);
            mv.visitMaxs(0, 0);
            mv.visitEnd();
        }
        // static int test(I,11args)
        {
            mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "test", "(Lp/I;BCSIJLjava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)I", null, null);
            mv.visitCode();
            mv.visitLdcInsn(new Handle(Opcodes.H_INVOKEINTERFACE, "p/I", "m", "(BCSIJLjava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)I"));
            mv.visitVarInsn(ALOAD, 0);
            mv.visitVarInsn(ILOAD, 1);
            mv.visitVarInsn(ILOAD, 2);
            mv.visitVarInsn(ILOAD, 3);
            mv.visitVarInsn(ILOAD, 4);
            mv.visitVarInsn(LLOAD, 5);
            mv.visitVarInsn(ALOAD, 7);
            mv.visitVarInsn(ALOAD, 8);
            mv.visitVarInsn(ALOAD, 9);
            mv.visitVarInsn(ALOAD, 10);
            mv.visitVarInsn(ALOAD, 11);
            mv.visitVarInsn(ALOAD, 12);
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandle",
                    "invokeExact", "(Lp/I;BCSIJLjava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)I");
            mv.visitInsn(IRETURN);
            mv.visitMaxs(0, 0);
            mv.visitEnd();
        }
        cw.visitEnd();
        return cw.toByteArray();
    }

    private static void tryAndCheckThrown(
            List<Throwable> lt, byte[] dBytes, String what, Class<?> expected, String jar_name)
            throws Throwable {
        tryAndCheckThrown(lt, "p.D", dBytes, what, expected, jar_name);
    }

    private static void tryAndCheckThrown(List<Throwable> lt, String d_name, byte[] dBytes, String what, Class<?> expected, String jar_name)
            throws Throwable {

        System.out.println("Methodhandle invokeExact I.m() for instance of " + what);
        ByteClassLoader bcl1 = new ByteClassLoader(jar_name, readJarFiles, writeJarFiles);
        try {
            Class<?> d1 = bcl1.loadBytes(d_name, dBytes);
            Class<?> t1 = bcl1.loadBytes("p.T", bytesForT());
            invokeTest(t1, d1, expected, lt);
        } finally {
            // Not necessary for others -- all class files are written in this call.
            // (unless the VM crashes first).
            bcl1.close();
        }

        System.out.println("Reflection invoke I.m() for instance of " + what);
        ByteClassLoader bcl3 = new ByteClassLoader(jar_name, readJarFiles, false);
        Class<?> d3 = bcl3.loadBytes(d_name, dBytes);
        Class<?> t3 = bcl3.loadClass("p.Treflect");
        invokeTest(t3, d3, expected, lt);

        System.out.println("Bytecode invokeInterface I.m() for instance of " + what);
        ByteClassLoader bcl2 = new ByteClassLoader(jar_name, readJarFiles, false);
        Class<?> d2 = bcl2.loadBytes(d_name, dBytes);
        Class<?> t2 = bcl2.loadClass("p.Tdirect");
        badGoodBadGood(t2, d2, expected, lt);
    }

    private static void invokeTest(Class<?> t, Class<?> d, Class<?> expected, List<Throwable> lt)
            throws Throwable {
        try {
            Method m = t.getMethod("test", p.I.class);
            Object o = d.newInstance();
            Object result = m.invoke(null, o);
            if (expected != null) {
                System.out.println("FAIL, Expected " + expected.getName()
                        + " wrapped in InvocationTargetException, but nothing was thrown");
                lt.add(new Error("Exception " + expected.getName() + " was not thrown"));
            } else {
                System.out.println("PASS, saw expected return.");
            }
        } catch (InvocationTargetException e) {
            Throwable th = e.getCause();
            th.printStackTrace(System.out);
            if (expected != null) {
                if (expected.isInstance(th)) {
                    System.out.println("PASS, saw expected exception (" + expected.getName() + ").");
                } else {
                    System.out.println("FAIL, Expected " + expected.getName()
                            + " wrapped in InvocationTargetException, saw " + th);
                    lt.add(th);
                }
            } else {
                System.out.println("FAIL, expected no exception, saw " + th);
                lt.add(th);
            }
        }
        System.out.println();
    }

    /* Many-arg versions of above */
    private static void tryAndCheckThrownMany(List<Throwable> lt, byte[] dBytes, String what, Class<?> expected)
            throws Throwable {

        System.out.println("Methodhandle invokeExact I.m(11params) for instance of " + what);
        ByteClassLoader bcl1 = new ByteClassLoader("p.D", readJarFiles, false);
        try {
            Class<?> d1 = bcl1.loadBytes("p.D", dBytes);
            Class<?> t1 = bcl1.loadBytes("p.T", bytesForT());
            invokeTestMany(t1, d1, expected, lt);
        } finally {
            bcl1.close(); // Not necessary for others -- all class files are written in this call.
        }

        {
            System.out.println("Bytecode invokeInterface I.m(11params) for instance of " + what);
            ByteClassLoader bcl2 = new ByteClassLoader("pD_m_pri_imp_pI", readJarFiles, false);
            Class<?> d2 = bcl2.loadBytes("p.D", dBytes);
            Class<?> t2 = bcl2.loadClass("p.Tdirect");
            badGoodBadGoodMany(t2, d2, expected, lt);

        }
        {
            System.out.println("Reflection invokeInterface I.m(11params) for instance of " + what);
            ByteClassLoader bcl2 = new ByteClassLoader("pD_m_pri_imp_pI", readJarFiles, false);
            Class<?> d2 = bcl2.loadBytes("p.D", dBytes);
            Class<?> t2 = bcl2.loadClass("p.Treflect");
            invokeTestMany(t2, d2, expected, lt);
        }
    }

    private static void invokeTestMany(Class<?> t, Class<?> d, Class<?> expected, List<Throwable> lt)
            throws Throwable {
        try {
            Method m = t.getMethod("test", p.I.class,
                    Byte.TYPE, Character.TYPE, Short.TYPE, Integer.TYPE, Long.TYPE,
                    Object.class, Object.class, Object.class,
                    Object.class, Object.class, Object.class);
            Object o = d.newInstance();
            Byte b = 1;
            Character c = 2;
            Short s = 3;
            Integer i = 4;
            Long j = 5L;
            Object o1 = b;
            Object o2 = c;
            Object o3 = s;
            Object o4 = i;
            Object o5 = j;
            Object o6 = "6";

            Object result = m.invoke(null, o, b, c, s, i, j,
                    o1, o2, o3, o4, o5, o6);
            if (expected != null) {
                System.out.println("FAIL, Expected " + expected.getName()
                        + " wrapped in InvocationTargetException, but nothing was thrown");
                lt.add(new Error("Exception " + expected.getName()
                        + " was not thrown"));
            } else {
                System.out.println("PASS, saw expected return.");
            }
        } catch (InvocationTargetException e) {
            Throwable th = e.getCause();
            th.printStackTrace(System.out);
            if (expected != null) {
                if (expected.isInstance(th)) {
                    System.out.println("PASS, saw expected exception ("
                            + expected.getName() + ").");
                } else {
                    System.out.println("FAIL, Expected " + expected.getName()
                            + " wrapped in InvocationTargetException, saw " + th);
                    lt.add(th);
                }
            } else {
                System.out.println("FAIL, expected no exception, saw " + th);
                lt.add(th);
            }
        }
        System.out.println();
    }

    /**
     * This tests a peculiar idiom for tickling the bug on older VMs that lack
     * methodhandles.  The bug (if not fixed) acts in the following way:
     *
     *  When a broken receiver is passed to the first execution of an invokeinterface
     * bytecode, the illegal access is detected before the effects of resolution are
     * cached for later use, and so repeated calls with a broken receiver will always
     * throw the correct error.
     *
     * If, however, a good receiver is passed to the invokeinterface, the effects of
     * resolution will be successfully cached.  A subsequent execution with a broken
     * receiver will reuse the cached information, skip the detailed resolution work,
     * and instead encounter a null pointer.  By convention, that is the encoding for a
     * missing abstract method, and an AbstractMethodError is thrown -- not the expected
     * IllegalAccessError.
     *
     * @param t2 Test invocation class
     * @param d2 Test receiver class
     * @param expected expected exception type
     * @param lt list of unexpected throwables seen
     */
    private static void badGoodBadGood(Class<?> t2, Class<?> d2, Class<?> expected, List<Throwable> lt)
            throws Throwable {
        System.out.println("  Error input 1st time");
        invokeTest(t2, d2, expected, lt);
        System.out.println("  Good input (instance of Dok)");
        invokeTest(t2, Dok.class, null, lt);
        System.out.println("  Error input 2nd time");
        invokeTest(t2, d2, expected, lt);
        System.out.println("  Good input (instance of Dok)");
        invokeTest(t2, Dok.class, null, lt);
    }

    private static void badGoodBadGoodMany(Class<?> t2, Class<?> d2, Class<?> expected, List<Throwable> lt)
            throws Throwable {
        System.out.println("  Error input 1st time");
        invokeTestMany(t2, d2, expected, lt);
        System.out.println("  Good input (instance of Dok)");
        invokeTestMany(t2, Dok.class, null, lt);
        System.out.println("  Error input 2nd time");
        invokeTestMany(t2, d2, expected, lt);
        System.out.println("  Good input (instance of Dok)");
        invokeTestMany(t2, Dok.class, null, lt);
    }
}