提交 8c5a1210 编写于 作者: D drchase

8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of...

8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
Summary: Copied null-checks from templateInterpreter_CPU into methodHandles_CPU
Reviewed-by: jrose, twisti
上级 6bb23764
/* /*
* Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -121,6 +121,7 @@ void MethodHandles::verify_ref_kind(MacroAssembler* _masm, int ref_kind, Registe ...@@ -121,6 +121,7 @@ void MethodHandles::verify_ref_kind(MacroAssembler* _masm, int ref_kind, Registe
void MethodHandles::jump_from_method_handle(MacroAssembler* _masm, Register method, Register target, Register temp, void MethodHandles::jump_from_method_handle(MacroAssembler* _masm, Register method, Register target, Register temp,
bool for_compiler_entry) { bool for_compiler_entry) {
Label L_no_such_method;
assert(method == G5_method, "interpreter calling convention"); assert(method == G5_method, "interpreter calling convention");
assert_different_registers(method, target, temp); assert_different_registers(method, target, temp);
...@@ -133,6 +134,9 @@ void MethodHandles::jump_from_method_handle(MacroAssembler* _masm, Register meth ...@@ -133,6 +134,9 @@ void MethodHandles::jump_from_method_handle(MacroAssembler* _masm, Register meth
const Address interp_only(G2_thread, JavaThread::interp_only_mode_offset()); const Address interp_only(G2_thread, JavaThread::interp_only_mode_offset());
__ ld(interp_only, temp); __ ld(interp_only, temp);
__ cmp_and_br_short(temp, 0, Assembler::zero, Assembler::pt, run_compiled_code); __ cmp_and_br_short(temp, 0, Assembler::zero, Assembler::pt, run_compiled_code);
// Null method test is replicated below in compiled case,
// it might be able to address across the verify_thread()
__ br_null_short(G5_method, Assembler::pn, L_no_such_method);
__ ld_ptr(G5_method, in_bytes(Method::interpreter_entry_offset()), target); __ ld_ptr(G5_method, in_bytes(Method::interpreter_entry_offset()), target);
__ jmp(target, 0); __ jmp(target, 0);
__ delayed()->nop(); __ delayed()->nop();
...@@ -141,11 +145,19 @@ void MethodHandles::jump_from_method_handle(MacroAssembler* _masm, Register meth ...@@ -141,11 +145,19 @@ void MethodHandles::jump_from_method_handle(MacroAssembler* _masm, Register meth
// it doesn't matter, since this is interpreter code. // it doesn't matter, since this is interpreter code.
} }
// Compiled case, either static or fall-through from runtime conditional
__ br_null_short(G5_method, Assembler::pn, L_no_such_method);
const ByteSize entry_offset = for_compiler_entry ? Method::from_compiled_offset() : const ByteSize entry_offset = for_compiler_entry ? Method::from_compiled_offset() :
Method::from_interpreted_offset(); Method::from_interpreted_offset();
__ ld_ptr(G5_method, in_bytes(entry_offset), target); __ ld_ptr(G5_method, in_bytes(entry_offset), target);
__ jmp(target, 0); __ jmp(target, 0);
__ delayed()->nop(); __ delayed()->nop();
__ bind(L_no_such_method);
AddressLiteral ame(StubRoutines::throw_AbstractMethodError_entry());
__ jump_to(ame, temp);
__ delayed()->nop();
} }
void MethodHandles::jump_to_lambda_form(MacroAssembler* _masm, void MethodHandles::jump_to_lambda_form(MacroAssembler* _masm,
......
/* /*
* Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -114,6 +114,11 @@ void MethodHandles::verify_ref_kind(MacroAssembler* _masm, int ref_kind, Registe ...@@ -114,6 +114,11 @@ void MethodHandles::verify_ref_kind(MacroAssembler* _masm, int ref_kind, Registe
void MethodHandles::jump_from_method_handle(MacroAssembler* _masm, Register method, Register temp, void MethodHandles::jump_from_method_handle(MacroAssembler* _masm, Register method, Register temp,
bool for_compiler_entry) { bool for_compiler_entry) {
assert(method == rbx, "interpreter calling convention"); assert(method == rbx, "interpreter calling convention");
Label L_no_such_method;
__ testptr(rbx, rbx);
__ jcc(Assembler::zero, L_no_such_method);
__ verify_method_ptr(method); __ verify_method_ptr(method);
if (!for_compiler_entry && JvmtiExport::can_post_interpreter_events()) { if (!for_compiler_entry && JvmtiExport::can_post_interpreter_events()) {
...@@ -138,6 +143,9 @@ void MethodHandles::jump_from_method_handle(MacroAssembler* _masm, Register meth ...@@ -138,6 +143,9 @@ void MethodHandles::jump_from_method_handle(MacroAssembler* _masm, Register meth
const ByteSize entry_offset = for_compiler_entry ? Method::from_compiled_offset() : const ByteSize entry_offset = for_compiler_entry ? Method::from_compiled_offset() :
Method::from_interpreted_offset(); Method::from_interpreted_offset();
__ jmp(Address(method, entry_offset)); __ jmp(Address(method, entry_offset));
__ bind(L_no_such_method);
__ jump(RuntimeAddress(StubRoutines::throw_AbstractMethodError_entry()));
} }
void MethodHandles::jump_to_lambda_form(MacroAssembler* _masm, void MethodHandles::jump_to_lambda_form(MacroAssembler* _masm,
......
/*
* 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.
*
*/
/**
* A minimal classloader for loading bytecodes that could not result from
* properly compiled Java.
*
* @author dr2chase
*/
public class ByteClassLoader extends ClassLoader {
/**
* (pre)load class name using classData for the definition.
*
* @param name
* @param classData
* @return
*/
public Class<?> loadBytes(String name, byte[] classData) {
Class<?> clazz = defineClass(name, classData, 0, classData.length);
resolveClass(clazz);
return clazz;
}
}
/*
* 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.
*
*/
/**
* Test class -- implements I, which provides default for m, but this class
* declares it abstract which (should) hide the interface default, and throw
* an abstract method error if it is called (calling it requires bytecode hacking
* or inconsistent compilation).
*/
public abstract class C implements I {
public abstract int m();
}
/*
* 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.
*
*/
public interface I {
default public int m() { return 1; }
}
/*
* 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 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;
/**
* @test
* @bug 8025260
* @summary Ensure that AbstractMethodError is thrown, not NullPointerException, through MethodHandles::jump_from_method_handle code path
*
* @compile -XDignore.symbol.file ByteClassLoader.java I.java C.java TestAMEnotNPE.java
* @run main/othervm TestAMEnotNPE
*/
public class TestAMEnotNPE implements Opcodes {
/**
* 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.
class D extends C {
D() { super(); }
// does not define m
}
* @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, "D", null, "C", null);
{
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "C", "<init>", "()V");
mv.visitInsn(RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
cw.visitEnd();
return cw.toByteArray();
}
/**
* The bytecodes for an invokeExact of a particular methodHandle, I.m, invoked on a D
class T {
T() { super(); } // boring constructor
int test() {
MethodHandle mh = `I.m():int`;
D d = new D();
return mh.invokeExact(d); // Should explode here, AbstractMethodError
}
}
* @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, "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();
}
{
mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "test", "()I", null, null);
mv.visitCode();
mv.visitLdcInsn(new Handle(Opcodes.H_INVOKEINTERFACE, "I", "m", "()I"));
mv.visitTypeInsn(NEW, "D");
mv.visitInsn(DUP);
mv.visitMethodInsn(INVOKESPECIAL, "D", "<init>", "()V");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandle", "invokeExact", "(LI;)I");
mv.visitInsn(IRETURN);
mv.visitMaxs(0,0);
mv.visitEnd();
}
cw.visitEnd();
return cw.toByteArray();
}
public static void main(String args[] ) throws Throwable {
ByteClassLoader bcl = new ByteClassLoader();
Class<?> d = bcl.loadBytes("D", bytesForD());
Class<?> t = bcl.loadBytes("T", bytesForT());
try {
Object result = t.getMethod("test").invoke(null);
System.out.println("Expected AbstractMethodError wrapped in InvocationTargetException, saw no exception");
throw new Error("Missing expected exception");
} catch (InvocationTargetException e) {
Throwable th = e.getCause();
if (th instanceof AbstractMethodError) {
th.printStackTrace(System.out);
System.out.println("PASS, saw expected exception (AbstractMethodError, wrapped in InvocationTargetException).");
} else {
System.out.println("Expected AbstractMethodError wrapped in InvocationTargetException, saw " + th);
throw th;
}
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册