mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-21 11:34:38 +02:00
8014013: CallInfo structure no longer accurately reports the result of a LinkResolver operation
Enhance method resolution and resulting data structures, plus some refactoring. Reviewed-by: twisti, acorn, jrose
This commit is contained in:
parent
98d8f57603
commit
222c735458
40 changed files with 715 additions and 601 deletions
|
@ -46,19 +46,6 @@
|
|||
#include "runtime/thread.inline.hpp"
|
||||
#include "runtime/vmThread.hpp"
|
||||
|
||||
//------------------------------------------------------------------------------------------------------------------------
|
||||
// Implementation of FieldAccessInfo
|
||||
|
||||
void FieldAccessInfo::set(KlassHandle klass, Symbol* name, int field_index, int field_offset,
|
||||
BasicType field_type, AccessFlags access_flags) {
|
||||
_klass = klass;
|
||||
_name = name;
|
||||
_field_index = field_index;
|
||||
_field_offset = field_offset;
|
||||
_field_type = field_type;
|
||||
_access_flags = access_flags;
|
||||
}
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------------------------------------------------
|
||||
// Implementation of CallInfo
|
||||
|
@ -66,26 +53,25 @@ BasicType field_type, AccessFlags access_flags) {
|
|||
|
||||
void CallInfo::set_static(KlassHandle resolved_klass, methodHandle resolved_method, TRAPS) {
|
||||
int vtable_index = Method::nonvirtual_vtable_index;
|
||||
set_common(resolved_klass, resolved_klass, resolved_method, resolved_method, vtable_index, CHECK);
|
||||
set_common(resolved_klass, resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK);
|
||||
}
|
||||
|
||||
|
||||
void CallInfo::set_interface(KlassHandle resolved_klass, KlassHandle selected_klass, methodHandle resolved_method, methodHandle selected_method, TRAPS) {
|
||||
void CallInfo::set_interface(KlassHandle resolved_klass, KlassHandle selected_klass, methodHandle resolved_method, methodHandle selected_method, int itable_index, TRAPS) {
|
||||
// This is only called for interface methods. If the resolved_method
|
||||
// comes from java/lang/Object, it can be the subject of a virtual call, so
|
||||
// we should pick the vtable index from the resolved method.
|
||||
// Other than that case, there is no valid vtable index to specify.
|
||||
int vtable_index = Method::invalid_vtable_index;
|
||||
if (resolved_method->method_holder() == SystemDictionary::Object_klass()) {
|
||||
assert(resolved_method->vtable_index() == selected_method->vtable_index(), "sanity check");
|
||||
vtable_index = resolved_method->vtable_index();
|
||||
}
|
||||
set_common(resolved_klass, selected_klass, resolved_method, selected_method, vtable_index, CHECK);
|
||||
// In that case, the caller must call set_virtual instead of set_interface.
|
||||
assert(resolved_method->method_holder()->is_interface(), "");
|
||||
assert(itable_index == resolved_method()->itable_index(), "");
|
||||
set_common(resolved_klass, selected_klass, resolved_method, selected_method, CallInfo::itable_call, itable_index, CHECK);
|
||||
}
|
||||
|
||||
void CallInfo::set_virtual(KlassHandle resolved_klass, KlassHandle selected_klass, methodHandle resolved_method, methodHandle selected_method, int vtable_index, TRAPS) {
|
||||
assert(vtable_index >= 0 || vtable_index == Method::nonvirtual_vtable_index, "valid index");
|
||||
set_common(resolved_klass, selected_klass, resolved_method, selected_method, vtable_index, CHECK);
|
||||
assert(vtable_index < 0 || !resolved_method->has_vtable_index() || vtable_index == resolved_method->vtable_index(), "");
|
||||
CallKind kind = (vtable_index >= 0 && !resolved_method->can_be_statically_bound() ? CallInfo::vtable_call : CallInfo::direct_call);
|
||||
set_common(resolved_klass, selected_klass, resolved_method, selected_method, kind, vtable_index, CHECK);
|
||||
assert(!resolved_method->is_compiled_lambda_form(), "these must be handled via an invokehandle call");
|
||||
}
|
||||
|
||||
|
@ -98,20 +84,29 @@ void CallInfo::set_handle(methodHandle resolved_method, Handle resolved_appendix
|
|||
resolved_method->is_compiled_lambda_form(),
|
||||
"linkMethod must return one of these");
|
||||
int vtable_index = Method::nonvirtual_vtable_index;
|
||||
assert(resolved_method->vtable_index() == vtable_index, "");
|
||||
set_common(resolved_klass, resolved_klass, resolved_method, resolved_method, vtable_index, CHECK);
|
||||
assert(!resolved_method->has_vtable_index(), "");
|
||||
set_common(resolved_klass, resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK);
|
||||
_resolved_appendix = resolved_appendix;
|
||||
_resolved_method_type = resolved_method_type;
|
||||
}
|
||||
|
||||
void CallInfo::set_common(KlassHandle resolved_klass, KlassHandle selected_klass, methodHandle resolved_method, methodHandle selected_method, int vtable_index, TRAPS) {
|
||||
void CallInfo::set_common(KlassHandle resolved_klass,
|
||||
KlassHandle selected_klass,
|
||||
methodHandle resolved_method,
|
||||
methodHandle selected_method,
|
||||
CallKind kind,
|
||||
int index,
|
||||
TRAPS) {
|
||||
assert(resolved_method->signature() == selected_method->signature(), "signatures must correspond");
|
||||
_resolved_klass = resolved_klass;
|
||||
_selected_klass = selected_klass;
|
||||
_resolved_method = resolved_method;
|
||||
_selected_method = selected_method;
|
||||
_vtable_index = vtable_index;
|
||||
_call_kind = kind;
|
||||
_call_index = index;
|
||||
_resolved_appendix = Handle();
|
||||
DEBUG_ONLY(verify()); // verify before making side effects
|
||||
|
||||
if (CompilationPolicy::must_be_compiled(selected_method)) {
|
||||
// This path is unusual, mostly used by the '-Xcomp' stress test mode.
|
||||
|
||||
|
@ -138,6 +133,65 @@ void CallInfo::set_common(KlassHandle resolved_klass, KlassHandle selected_klass
|
|||
}
|
||||
}
|
||||
|
||||
// utility query for unreflecting a method
|
||||
CallInfo::CallInfo(Method* resolved_method, Klass* resolved_klass) {
|
||||
Klass* resolved_method_holder = resolved_method->method_holder();
|
||||
if (resolved_klass == NULL) { // 2nd argument defaults to holder of 1st
|
||||
resolved_klass = resolved_method_holder;
|
||||
}
|
||||
_resolved_klass = resolved_klass;
|
||||
_selected_klass = resolved_klass;
|
||||
_resolved_method = resolved_method;
|
||||
_selected_method = resolved_method;
|
||||
// classify:
|
||||
CallKind kind = CallInfo::unknown_kind;
|
||||
int index = resolved_method->vtable_index();
|
||||
if (resolved_method->can_be_statically_bound()) {
|
||||
kind = CallInfo::direct_call;
|
||||
} else if (!resolved_method_holder->is_interface()) {
|
||||
// Could be an Object method inherited into an interface, but still a vtable call.
|
||||
kind = CallInfo::vtable_call;
|
||||
} else if (!resolved_klass->is_interface()) {
|
||||
// A miranda method. Compute the vtable index.
|
||||
ResourceMark rm;
|
||||
klassVtable* vt = InstanceKlass::cast(resolved_klass)->vtable();
|
||||
index = vt->index_of_miranda(resolved_method->name(),
|
||||
resolved_method->signature());
|
||||
kind = CallInfo::vtable_call;
|
||||
} else {
|
||||
// A regular interface call.
|
||||
kind = CallInfo::itable_call;
|
||||
index = resolved_method->itable_index();
|
||||
}
|
||||
assert(index == Method::nonvirtual_vtable_index || index >= 0, err_msg("bad index %d", index));
|
||||
_call_kind = kind;
|
||||
_call_index = index;
|
||||
_resolved_appendix = Handle();
|
||||
DEBUG_ONLY(verify());
|
||||
}
|
||||
|
||||
#ifdef ASSERT
|
||||
void CallInfo::verify() {
|
||||
switch (call_kind()) { // the meaning and allowed value of index depends on kind
|
||||
case CallInfo::direct_call:
|
||||
if (_call_index == Method::nonvirtual_vtable_index) break;
|
||||
// else fall through to check vtable index:
|
||||
case CallInfo::vtable_call:
|
||||
assert(resolved_klass()->verify_vtable_index(_call_index), "");
|
||||
break;
|
||||
case CallInfo::itable_call:
|
||||
assert(resolved_method()->method_holder()->verify_itable_index(_call_index), "");
|
||||
break;
|
||||
case CallInfo::unknown_kind:
|
||||
assert(call_kind() != CallInfo::unknown_kind, "CallInfo must be set");
|
||||
break;
|
||||
default:
|
||||
fatal(err_msg_res("Unexpected call kind %d", call_kind()));
|
||||
}
|
||||
}
|
||||
#endif //ASSERT
|
||||
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------------------------------------------------
|
||||
// Klass resolution
|
||||
|
@ -163,13 +217,6 @@ void LinkResolver::resolve_klass(KlassHandle& result, constantPoolHandle pool, i
|
|||
result = KlassHandle(THREAD, result_oop);
|
||||
}
|
||||
|
||||
void LinkResolver::resolve_klass_no_update(KlassHandle& result, constantPoolHandle pool, int index, TRAPS) {
|
||||
Klass* result_oop =
|
||||
ConstantPool::klass_ref_at_if_loaded_check(pool, index, CHECK);
|
||||
result = KlassHandle(THREAD, result_oop);
|
||||
}
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------------------------------------------------
|
||||
// Method resolution
|
||||
//
|
||||
|
@ -360,7 +407,12 @@ void LinkResolver::check_method_accessability(KlassHandle ref_klass,
|
|||
|
||||
void LinkResolver::resolve_method_statically(methodHandle& resolved_method, KlassHandle& resolved_klass,
|
||||
Bytecodes::Code code, constantPoolHandle pool, int index, TRAPS) {
|
||||
|
||||
// This method is used only
|
||||
// (1) in C2 from InlineTree::ok_to_inline (via ciMethod::check_call),
|
||||
// and
|
||||
// (2) in Bytecode_invoke::static_target
|
||||
// It appears to fail when applied to an invokeinterface call site.
|
||||
// FIXME: Remove this method and ciMethod::check_call; refactor to use the other LinkResolver entry points.
|
||||
// resolve klass
|
||||
if (code == Bytecodes::_invokedynamic) {
|
||||
resolved_klass = SystemDictionary::MethodHandle_klass();
|
||||
|
@ -580,45 +632,49 @@ void LinkResolver::check_field_accessability(KlassHandle ref_klass,
|
|||
}
|
||||
}
|
||||
|
||||
void LinkResolver::resolve_field(FieldAccessInfo& result, constantPoolHandle pool, int index, Bytecodes::Code byte, bool check_only, TRAPS) {
|
||||
resolve_field(result, pool, index, byte, check_only, true, CHECK);
|
||||
void LinkResolver::resolve_field_access(fieldDescriptor& result, constantPoolHandle pool, int index, Bytecodes::Code byte, TRAPS) {
|
||||
// Load these early in case the resolve of the containing klass fails
|
||||
Symbol* field = pool->name_ref_at(index);
|
||||
Symbol* sig = pool->signature_ref_at(index);
|
||||
|
||||
// resolve specified klass
|
||||
KlassHandle resolved_klass;
|
||||
resolve_klass(resolved_klass, pool, index, CHECK);
|
||||
|
||||
KlassHandle current_klass(THREAD, pool->pool_holder());
|
||||
resolve_field(result, resolved_klass, field, sig, current_klass, byte, true, true, CHECK);
|
||||
}
|
||||
|
||||
void LinkResolver::resolve_field(FieldAccessInfo& result, constantPoolHandle pool, int index, Bytecodes::Code byte, bool check_only, bool update_pool, TRAPS) {
|
||||
void LinkResolver::resolve_field(fieldDescriptor& fd, KlassHandle resolved_klass, Symbol* field, Symbol* sig,
|
||||
KlassHandle current_klass, Bytecodes::Code byte, bool check_access, bool initialize_class,
|
||||
TRAPS) {
|
||||
assert(byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic ||
|
||||
byte == Bytecodes::_getfield || byte == Bytecodes::_putfield, "bad bytecode");
|
||||
byte == Bytecodes::_getfield || byte == Bytecodes::_putfield ||
|
||||
(byte == Bytecodes::_nop && !check_access), "bad field access bytecode");
|
||||
|
||||
bool is_static = (byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic);
|
||||
bool is_put = (byte == Bytecodes::_putfield || byte == Bytecodes::_putstatic);
|
||||
|
||||
// resolve specified klass
|
||||
KlassHandle resolved_klass;
|
||||
if (update_pool) {
|
||||
resolve_klass(resolved_klass, pool, index, CHECK);
|
||||
} else {
|
||||
resolve_klass_no_update(resolved_klass, pool, index, CHECK);
|
||||
}
|
||||
// Load these early in case the resolve of the containing klass fails
|
||||
Symbol* field = pool->name_ref_at(index);
|
||||
Symbol* sig = pool->signature_ref_at(index);
|
||||
// Check if there's a resolved klass containing the field
|
||||
if( resolved_klass.is_null() ) {
|
||||
if (resolved_klass.is_null()) {
|
||||
ResourceMark rm(THREAD);
|
||||
THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());
|
||||
}
|
||||
|
||||
// Resolve instance field
|
||||
fieldDescriptor fd; // find_field initializes fd if found
|
||||
KlassHandle sel_klass(THREAD, InstanceKlass::cast(resolved_klass())->find_field(field, sig, &fd));
|
||||
// check if field exists; i.e., if a klass containing the field def has been selected
|
||||
if (sel_klass.is_null()){
|
||||
if (sel_klass.is_null()) {
|
||||
ResourceMark rm(THREAD);
|
||||
THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());
|
||||
}
|
||||
|
||||
if (!check_access)
|
||||
// Access checking may be turned off when calling from within the VM.
|
||||
return;
|
||||
|
||||
// check access
|
||||
KlassHandle ref_klass(THREAD, pool->pool_holder());
|
||||
check_field_accessability(ref_klass, resolved_klass, sel_klass, fd, CHECK);
|
||||
check_field_accessability(current_klass, resolved_klass, sel_klass, fd, CHECK);
|
||||
|
||||
// check for errors
|
||||
if (is_static != fd.is_static()) {
|
||||
|
@ -629,7 +685,7 @@ void LinkResolver::resolve_field(FieldAccessInfo& result, constantPoolHandle poo
|
|||
}
|
||||
|
||||
// Final fields can only be accessed from its own class.
|
||||
if (is_put && fd.access_flags().is_final() && sel_klass() != pool->pool_holder()) {
|
||||
if (is_put && fd.access_flags().is_final() && sel_klass() != current_klass()) {
|
||||
THROW(vmSymbols::java_lang_IllegalAccessError());
|
||||
}
|
||||
|
||||
|
@ -639,19 +695,18 @@ void LinkResolver::resolve_field(FieldAccessInfo& result, constantPoolHandle poo
|
|||
//
|
||||
// note 2: we don't want to force initialization if we are just checking
|
||||
// if the field access is legal; e.g., during compilation
|
||||
if (is_static && !check_only) {
|
||||
if (is_static && initialize_class) {
|
||||
sel_klass->initialize(CHECK);
|
||||
}
|
||||
|
||||
{
|
||||
if (sel_klass() != current_klass()) {
|
||||
HandleMark hm(THREAD);
|
||||
Handle ref_loader (THREAD, InstanceKlass::cast(ref_klass())->class_loader());
|
||||
Handle ref_loader (THREAD, InstanceKlass::cast(current_klass())->class_loader());
|
||||
Handle sel_loader (THREAD, InstanceKlass::cast(sel_klass())->class_loader());
|
||||
Symbol* signature_ref = pool->signature_ref_at(index);
|
||||
{
|
||||
ResourceMark rm(THREAD);
|
||||
Symbol* failed_type_symbol =
|
||||
SystemDictionary::check_signature_loaders(signature_ref,
|
||||
SystemDictionary::check_signature_loaders(sig,
|
||||
ref_loader, sel_loader,
|
||||
false,
|
||||
CHECK);
|
||||
|
@ -677,9 +732,6 @@ void LinkResolver::resolve_field(FieldAccessInfo& result, constantPoolHandle poo
|
|||
|
||||
// return information. note that the klass is set to the actual klass containing the
|
||||
// field, otherwise access of static fields in superclasses will not work.
|
||||
KlassHandle holder (THREAD, fd.field_holder());
|
||||
Symbol* name = fd.name();
|
||||
result.set(holder, name, fd.index(), fd.offset(), fd.field_type(), fd.access_flags());
|
||||
}
|
||||
|
||||
|
||||
|
@ -906,10 +958,6 @@ void LinkResolver::runtime_resolve_virtual_method(CallInfo& result,
|
|||
THROW(vmSymbols::java_lang_NullPointerException());
|
||||
}
|
||||
|
||||
// Virtual methods cannot be resolved before its klass has been linked, for otherwise the Method*'s
|
||||
// has not been rewritten, and the vtable initialized.
|
||||
assert(resolved_method->method_holder()->is_linked(), "must be linked");
|
||||
|
||||
// Virtual methods cannot be resolved before its klass has been linked, for otherwise the Method*'s
|
||||
// has not been rewritten, and the vtable initialized. Make sure to do this after the nullcheck, since
|
||||
// a missing receiver might result in a bogus lookup.
|
||||
|
@ -920,6 +968,7 @@ void LinkResolver::runtime_resolve_virtual_method(CallInfo& result,
|
|||
vtable_index = vtable_index_of_miranda_method(resolved_klass,
|
||||
resolved_method->name(),
|
||||
resolved_method->signature(), CHECK);
|
||||
|
||||
assert(vtable_index >= 0 , "we should have valid vtable index at this point");
|
||||
|
||||
InstanceKlass* inst = InstanceKlass::cast(recv_klass());
|
||||
|
@ -927,6 +976,7 @@ void LinkResolver::runtime_resolve_virtual_method(CallInfo& result,
|
|||
} else {
|
||||
// at this point we are sure that resolved_method is virtual and not
|
||||
// a miranda method; therefore, it must have a valid vtable index.
|
||||
assert(!resolved_method->has_itable_index(), "");
|
||||
vtable_index = resolved_method->vtable_index();
|
||||
// We could get a negative vtable_index for final methods,
|
||||
// because as an optimization they are they are never put in the vtable,
|
||||
|
@ -1006,6 +1056,12 @@ void LinkResolver::runtime_resolve_interface_method(CallInfo& result, methodHand
|
|||
lookup_instance_method_in_klasses(sel_method, recv_klass,
|
||||
resolved_method->name(),
|
||||
resolved_method->signature(), CHECK);
|
||||
if (sel_method.is_null() && !check_null_and_abstract) {
|
||||
// In theory this is a harmless placeholder value, but
|
||||
// in practice leaving in null affects the nsk default method tests.
|
||||
// This needs further study.
|
||||
sel_method = resolved_method;
|
||||
}
|
||||
// check if method exists
|
||||
if (sel_method.is_null()) {
|
||||
ResourceMark rm(THREAD);
|
||||
|
@ -1046,7 +1102,14 @@ void LinkResolver::runtime_resolve_interface_method(CallInfo& result, methodHand
|
|||
sel_method->signature()));
|
||||
}
|
||||
// setup result
|
||||
result.set_interface(resolved_klass, recv_klass, resolved_method, sel_method, CHECK);
|
||||
if (!resolved_method->has_itable_index()) {
|
||||
int vtable_index = resolved_method->vtable_index();
|
||||
assert(vtable_index == sel_method->vtable_index(), "sanity check");
|
||||
result.set_virtual(resolved_klass, recv_klass, resolved_method, sel_method, vtable_index, CHECK);
|
||||
return;
|
||||
}
|
||||
int itable_index = resolved_method()->itable_index();
|
||||
result.set_interface(resolved_klass, recv_klass, resolved_method, sel_method, itable_index, CHECK);
|
||||
}
|
||||
|
||||
|
||||
|
@ -1293,7 +1356,8 @@ void LinkResolver::resolve_invokedynamic(CallInfo& result, constantPoolHandle po
|
|||
}
|
||||
|
||||
if (TraceMethodHandles) {
|
||||
tty->print_cr("resolve_invokedynamic #%d %s %s",
|
||||
ResourceMark rm(THREAD);
|
||||
tty->print_cr("resolve_invokedynamic #%d %s %s",
|
||||
ConstantPool::decode_invokedynamic_index(index),
|
||||
method_name->as_C_string(), method_signature->as_C_string());
|
||||
tty->print(" BSM info: "); bootstrap_specifier->print();
|
||||
|
@ -1342,9 +1406,16 @@ void LinkResolver::resolve_dynamic_call(CallInfo& result,
|
|||
//------------------------------------------------------------------------------------------------------------------------
|
||||
#ifndef PRODUCT
|
||||
|
||||
void FieldAccessInfo::print() {
|
||||
void CallInfo::print() {
|
||||
ResourceMark rm;
|
||||
tty->print_cr("Field %s@%d", name()->as_C_string(), field_offset());
|
||||
const char* kindstr = "unknown";
|
||||
switch (_call_kind) {
|
||||
case direct_call: kindstr = "direct"; break;
|
||||
case vtable_call: kindstr = "vtable"; break;
|
||||
case itable_call: kindstr = "itable"; break;
|
||||
}
|
||||
tty->print_cr("Call %s@%d %s", kindstr, _call_index,
|
||||
_resolved_method.is_null() ? "(none)" : _resolved_method->name_and_sig_as_C_string());
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue