Path: blob/master/test/hotspot/jtreg/runtime/ConstantPool/BadMethodHandles.java
40942 views
/*1* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8087223 819565026* @summary Adding constantTag to keep method call consistent with it.27* @modules java.base/jdk.internal.org.objectweb.asm28* java.base/jdk.internal.misc29* java.management30* @compile -XDignore.symbol.file BadMethodHandles.java31* @run main/othervm BadMethodHandles32*/3334import jdk.internal.org.objectweb.asm.*;35import java.io.FileOutputStream;36import java.lang.reflect.InvocationTargetException;37import java.lang.invoke.MethodHandle;38import java.lang.invoke.MethodHandles;39import java.lang.invoke.MethodType;40import static jdk.internal.org.objectweb.asm.Opcodes.*;4142public class BadMethodHandles {4344static byte[] dumpBadInterfaceMethodref() {45ClassWriter cw = new ClassWriter(0);46cw.visit(52, ACC_PUBLIC | ACC_SUPER, "BadInterfaceMethodref", null, "java/lang/Object", null);47Handle handle1 =48new Handle(Opcodes.H_INVOKEINTERFACE, "BadInterfaceMethodref", "m", "()V", true);49Handle handle2 =50new Handle(Opcodes.H_INVOKEINTERFACE, "BadInterfaceMethodref", "staticM", "()V", true);5152{53MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);54mv.visitCode();55mv.visitVarInsn(ALOAD, 0);56mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);57mv.visitInsn(RETURN);58mv.visitMaxs(1, 1);59mv.visitEnd();60}61{62MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "m", "()V", null, null);63mv.visitCode();64mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");65mv.visitLdcInsn("hello from m");66mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false/*intf*/);67mv.visitInsn(RETURN);68mv.visitMaxs(3, 1);69mv.visitEnd();70}71{72MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "staticM", "()V", null, null);73mv.visitCode();74mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");75mv.visitLdcInsn("hello from staticM");76mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false/*intf*/);77mv.visitInsn(RETURN);78mv.visitMaxs(3, 1);79mv.visitEnd();80}8182{83MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "runm", "()V", null, null);84mv.visitCode();85// REF_invokeStatic86mv.visitLdcInsn(handle1);87mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandle", "invoke", "()V", false);88mv.visitInsn(RETURN);89mv.visitMaxs(1, 1);90mv.visitEnd();91}9293{94MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "runStaticM", "()V", null, null);95mv.visitCode();96// REF_invokeStatic97mv.visitLdcInsn(handle2);98mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandle", "invoke", "()V", false);99mv.visitInsn(RETURN);100mv.visitMaxs(1, 1);101mv.visitEnd();102}103104cw.visitEnd();105return cw.toByteArray();106}107108static byte[] dumpIBad() {109ClassWriter cw = new ClassWriter(0);110cw.visit(52, ACC_PUBLIC | ACC_ABSTRACT | ACC_INTERFACE, "IBad", null, "java/lang/Object", null);111{112MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "m", "()V", null, null);113mv.visitCode();114mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");115mv.visitLdcInsn("hello from m");116mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false/*intf*/);117mv.visitInsn(RETURN);118mv.visitMaxs(3, 1);119mv.visitEnd();120}121{122MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "staticM", "()V", null, null);123mv.visitCode();124mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");125mv.visitLdcInsn("hello from staticM");126mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false/*intf*/);127mv.visitInsn(RETURN);128mv.visitMaxs(3, 1);129mv.visitEnd();130}131cw.visitEnd();132return cw.toByteArray();133}134135static byte[] dumpBadMethodref() {136ClassWriter cw = new ClassWriter(0);137cw.visit(52, ACC_PUBLIC | ACC_SUPER, "BadMethodref", null, "java/lang/Object", new String[]{"IBad"});138Handle handle1 =139new Handle(Opcodes.H_INVOKEINTERFACE, "BadMethodref", "m", "()V", true);140Handle handle2 =141new Handle(Opcodes.H_INVOKEINTERFACE, "BadMethodref", "staticM", "()V", true);142143{144MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);145mv.visitCode();146mv.visitVarInsn(ALOAD, 0);147mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);148mv.visitInsn(RETURN);149mv.visitMaxs(1, 1);150mv.visitEnd();151}152153{154MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "runm", "()V", null, null);155mv.visitCode();156// REF_invokeStatic157mv.visitLdcInsn(handle1);158mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandle", "invoke", "()V", false);159mv.visitInsn(RETURN);160mv.visitMaxs(1, 1);161mv.visitEnd();162}163164{165MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "runStaticM", "()V", null, null);166mv.visitCode();167// REF_invokeStatic168mv.visitLdcInsn(handle2);169mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandle", "invoke", "()V", false);170mv.visitInsn(RETURN);171mv.visitMaxs(1, 1);172mv.visitEnd();173}174175cw.visitEnd();176return cw.toByteArray();177}178179static byte[] dumpInvokeBasic() {180ClassWriter cw = new ClassWriter(0);181cw.visit(52, ACC_PUBLIC | ACC_SUPER, "InvokeBasicref", null, "java/lang/Object", null);182Handle handle =183new Handle(Opcodes.H_INVOKEVIRTUAL, "java/lang/invoke/MethodHandle", "invokeBasic", "([Ljava/lang/Object;)Ljava/lang/Object;", false);184185{186MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);187mv.visitCode();188mv.visitVarInsn(ALOAD, 0);189mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);190mv.visitInsn(RETURN);191mv.visitMaxs(1, 1);192mv.visitEnd();193}194195{196MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "runInvokeBasicM", "()V", null, null);197mv.visitCode();198mv.visitLdcInsn(handle);199mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandle", "invoke", "()V", false);200mv.visitInsn(RETURN);201mv.visitMaxs(1, 1);202mv.visitEnd();203}204205cw.visitEnd();206return cw.toByteArray();207}208209static class CL extends ClassLoader {210@Override211protected Class<?> findClass(String name) throws ClassNotFoundException {212byte[] classBytes = null;213switch (name) {214case "BadInterfaceMethodref": classBytes = dumpBadInterfaceMethodref(); break;215case "BadMethodref" : classBytes = dumpBadMethodref(); break;216case "IBad" : classBytes = dumpIBad(); break;217case "InvokeBasicref" : classBytes = dumpInvokeBasic(); break;218default : throw new ClassNotFoundException(name);219}220return defineClass(name, classBytes, 0, classBytes.length);221}222}223224public static void main(String[] args) throws Throwable {225try (FileOutputStream fos = new FileOutputStream("BadInterfaceMethodref.class")) {226fos.write(dumpBadInterfaceMethodref());227}228try (FileOutputStream fos = new FileOutputStream("IBad.class")) {229fos.write(dumpIBad());230}231try (FileOutputStream fos = new FileOutputStream("BadMethodref.class")) {232fos.write(dumpBadMethodref());233}234try (FileOutputStream fos = new FileOutputStream("InvokeBasicref.class")) {235fos.write(dumpInvokeBasic());236}237238Class<?> cls = (new CL()).loadClass("BadInterfaceMethodref");239String[] methods = {"runm", "runStaticM"};240System.out.println("Test BadInterfaceMethodref:");241int success = 0;242for (String name : methods) {243try {244System.out.printf("invoke %s: \n", name);245cls.getMethod(name).invoke(cls.newInstance());246System.out.println("FAILED - ICCE should be thrown");247} catch (Throwable e) {248if (e instanceof InvocationTargetException && e.getCause() != null &&249e.getCause() instanceof IncompatibleClassChangeError) {250System.out.println("PASSED - expected ICCE thrown");251success++;252continue;253} else {254System.out.println("FAILED with wrong exception" + e);255throw e;256}257}258}259if (success != methods.length) {260throw new Exception("BadInterfaceMethodRef Failed to catch IncompatibleClassChangeError");261}262System.out.println("Test BadMethodref:");263cls = (new CL()).loadClass("BadMethodref");264success = 0;265for (String name : methods) {266try {267System.out.printf("invoke %s: \n", name);268cls.getMethod(name).invoke(cls.newInstance());269System.out.println("FAILED - ICCE should be thrown");270} catch (Throwable e) {271if (e instanceof InvocationTargetException && e.getCause() != null &&272e.getCause() instanceof IncompatibleClassChangeError) {273System.out.println("PASSED - expected ICCE thrown");274success++;275continue;276} else {277System.out.println("FAILED with wrong exception" + e);278throw e;279}280}281}282if (success != methods.length) {283throw new Exception("BadMethodRef Failed to catch IncompatibleClassChangeError");284}285286System.out.println("Test InvokeBasicref:");287cls = (new CL()).loadClass("InvokeBasicref");288success = 0;289methods = new String[] {"runInvokeBasicM"};290for (String name : methods) {291try {292System.out.printf("invoke %s: \n", name);293cls.getMethod(name).invoke(cls.newInstance());294System.out.println("FAILED - ICCE should be thrown");295} catch (Throwable e) {296e.printStackTrace();297if (e instanceof InvocationTargetException && e.getCause() != null &&298e.getCause() instanceof IncompatibleClassChangeError) {299System.out.println("PASSED - expected ICCE thrown");300success++;301continue;302} else {303System.out.println("FAILED with wrong exception" + e);304throw e;305}306}307}308if (success != methods.length) {309throw new Exception("InvokeBasicref Failed to catch IncompatibleClassChangeError");310}311}312}313314315