|
| 1 | +/* |
| 2 | + * Copyright IBM Corp. and others 2001 |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under |
| 5 | + * the terms of the Eclipse Public License 2.0 which accompanies this |
| 6 | + * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ |
| 7 | + * or the Apache License, Version 2.0 which accompanies this distribution and |
| 8 | + * is available at https://www.apache.org/licenses/LICENSE-2.0. |
| 9 | + * |
| 10 | + * This Source Code may also be made available under the following |
| 11 | + * Secondary Licenses when the conditions for such availability set |
| 12 | + * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU |
| 13 | + * General Public License, version 2 with the GNU Classpath |
| 14 | + * Exception [1] and GNU General Public License, version 2 with the |
| 15 | + * OpenJDK Assembly Exception [2]. |
| 16 | + * |
| 17 | + * [1] https://www.gnu.org/software/classpath/license.html |
| 18 | + * [2] https://openjdk.org/legal/assembly-exception.html |
| 19 | + * |
| 20 | + * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 |
| 21 | + */ |
| 22 | +package com.ibm.j9.jsr292.indyn; |
| 23 | + |
| 24 | +import org.openj9.test.util.VersionCheck; |
| 25 | + |
| 26 | +import org.testng.annotations.Test; |
| 27 | +import org.testng.annotations.DataProvider; |
| 28 | + |
| 29 | +import org.testng.Assert; |
| 30 | +import org.testng.AssertJUnit; |
| 31 | +import org.objectweb.asm.*; |
| 32 | +import static org.objectweb.asm.Opcodes.*; |
| 33 | + |
| 34 | +import java.lang.invoke.MethodHandle; |
| 35 | +import java.lang.invoke.MethodHandles; |
| 36 | +import java.lang.invoke.ConstantCallSite; |
| 37 | +import java.lang.invoke.CallSite; |
| 38 | +import java.lang.invoke.MethodType; |
| 39 | +import java.lang.reflect.Method; |
| 40 | + |
| 41 | +public class IndyOSRTest { |
| 42 | + |
| 43 | + private static Throwable thrower = null; |
| 44 | + private static String dummyClassPathName = "com/ibm/j9/jsr292/indyn/TestBSMError"; |
| 45 | + private static String dummyClassFullName = "com.ibm.j9.jsr292.indyn.TestBSMError"; |
| 46 | + |
| 47 | + // generate method with invokedynamic bytecode that uses the BSM "bootstrap" below |
| 48 | + private static byte[] generate(int version) { |
| 49 | + ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); |
| 50 | + MethodVisitor mv; |
| 51 | + cw.visit(VersionCheck.major() + V1_8 - 8, ACC_PUBLIC, dummyClassPathName + version, null, "java/lang/Object", null); |
| 52 | + |
| 53 | + mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "dummy", "()V", null, null); |
| 54 | + mv.visitCode(); |
| 55 | + |
| 56 | + Handle bsm = new Handle( |
| 57 | + H_INVOKESTATIC, |
| 58 | + "com/ibm/j9/jsr292/indyn/IndyOSRTest", |
| 59 | + "bootstrap", |
| 60 | + "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;", |
| 61 | + false |
| 62 | + ); |
| 63 | + |
| 64 | + mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); |
| 65 | + mv.visitLdcInsn(1L); |
| 66 | + mv.visitLdcInsn(2L); |
| 67 | + //mv.visitVarInsn(ILOAD, 0); |
| 68 | + mv.visitLdcInsn(3); |
| 69 | + mv.visitLdcInsn(4); |
| 70 | + mv.visitInvokeDynamicInsn("someName", "(JJII)Ljava/lang/String;", bsm); |
| 71 | + mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false); |
| 72 | + mv.visitInsn(RETURN); |
| 73 | + mv.visitMaxs(6, 4); |
| 74 | + mv.visitEnd(); |
| 75 | + cw.visitEnd(); |
| 76 | + return cw.toByteArray(); |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | + public static CallSite bootstrap(MethodHandles.Lookup lookup, String name, MethodType mt) throws Throwable { |
| 82 | + if (thrower == null) { |
| 83 | + MethodHandle target = MethodHandles.lookup().findStatic(IndyTest.class, "sanity", MethodType.methodType(String.class, long.class, long.class, int.class, int.class)); |
| 84 | + return new ConstantCallSite(target); |
| 85 | + } |
| 86 | + System.out.println("Throwing"); |
| 87 | + throw thrower; |
| 88 | + } |
| 89 | + |
| 90 | + public static String sanity(long a, long b, int c, int d) { |
| 91 | + return "bootstrap" + a + "," + b + "," + c + "," + d; |
| 92 | + } |
| 93 | + |
| 94 | + private class ByteArrayClassLoader extends ClassLoader { |
| 95 | + public Class<?> getc(String name, byte[] b) { |
| 96 | + return defineClass(name,b,0,b.length); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @DataProvider(name="throwableProvider") |
| 101 | + public static Object[][] throwableProvider() { |
| 102 | + return new Object[][] {{new NullPointerException(), 1}, {new StackOverflowError(), 2}, {new IllegalArgumentException(), 3}, |
| 103 | + {new ClassCastException(), 4}}; |
| 104 | + } |
| 105 | + |
| 106 | + @Test(groups = {"level.extended"}, dataProvider="throwableProvider", invocationCount=1) |
| 107 | + public void testBSMErrorThrow(Throwable t, int version) { |
| 108 | + thrower = t; |
| 109 | + ByteArrayClassLoader c = new ByteArrayClassLoader(); |
| 110 | + byte[] b = IndyOSRTest.generate(version); |
| 111 | + System.out.println(b.length); |
| 112 | + Class<?> cls = c.getc(dummyClassFullName + version, b); |
| 113 | + |
| 114 | + // attempt once in the interpreter |
| 115 | + try { |
| 116 | + if (t == null){ |
| 117 | + Assert.assertTrue(cls.getMethod("dummy").invoke(null).equals("bootstrap1,2,3,4")); |
| 118 | + } else { |
| 119 | + cls.getMethod("dummy").invoke(null); |
| 120 | + } |
| 121 | + } catch(IllegalAccessException e) { |
| 122 | + Assert.fail("Cannot access dummy method"); |
| 123 | + } catch(NoSuchMethodException e) { |
| 124 | + Assert.fail("Cannot find dummy method"); |
| 125 | + } catch (java.lang.reflect.InvocationTargetException e) { |
| 126 | + if (t == null) { |
| 127 | + Assert.fail("Bootsrap method should not throw error when parameter t is null"); |
| 128 | + } |
| 129 | + } catch (Throwable t2) { |
| 130 | + System.out.println("Caught something"); |
| 131 | + } |
| 132 | + |
| 133 | + // run again after compilation |
| 134 | + try { |
| 135 | + if (t == null){ |
| 136 | + Assert.assertTrue(cls.getMethod("dummy").invoke(null).equals("bootstrap1,2,3,4")); |
| 137 | + } else { |
| 138 | + cls.getMethod("dummy").invoke(null); |
| 139 | + } |
| 140 | + } catch(IllegalAccessException e) { |
| 141 | + Assert.fail("Cannot access dummy method"); |
| 142 | + } catch(NoSuchMethodException e) { |
| 143 | + Assert.fail("Cannot find dummy method"); |
| 144 | + } catch (java.lang.reflect.InvocationTargetException e) { |
| 145 | + if (t == null) { |
| 146 | + Assert.fail("Bootsrap method should not throw error when parameter t is null"); |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments