8263434: Dangling references after MethodComparator::methods_EMCP

Reviewed-by: coleenp, sspitsyn
This commit is contained in:
Aleksey Shipilev 2021-03-17 10:58:35 +00:00
parent 23fc2a4d97
commit f9f2eef91d
2 changed files with 86 additions and 89 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2021, 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
@ -32,11 +32,6 @@
#include "runtime/handles.inline.hpp"
#include "utilities/globalDefinitions.hpp"
BytecodeStream *MethodComparator::_s_old;
BytecodeStream *MethodComparator::_s_new;
ConstantPool* MethodComparator::_old_cp;
ConstantPool* MethodComparator::_new_cp;
bool MethodComparator::methods_EMCP(Method* old_method, Method* new_method) {
if (old_method->code_size() != new_method->code_size())
return false;
@ -50,26 +45,26 @@ bool MethodComparator::methods_EMCP(Method* old_method, Method* new_method) {
return false;
}
_old_cp = old_method->constants();
_new_cp = new_method->constants();
ConstantPool* old_cp = old_method->constants();
ConstantPool* new_cp = new_method->constants();
Thread* THREAD = Thread::current();
BytecodeStream s_old(methodHandle(THREAD, old_method));
BytecodeStream s_new(methodHandle(THREAD, new_method));
_s_old = &s_old;
_s_new = &s_new;
Bytecodes::Code c_old, c_new;
while ((c_old = s_old.next()) >= 0) {
if ((c_new = s_new.next()) < 0 || c_old != c_new)
return false;
if (! args_same(c_old, c_new))
if (!args_same(c_old, c_new, &s_old, &s_new, old_cp, new_cp))
return false;
}
return true;
}
bool MethodComparator::args_same(Bytecodes::Code c_old, Bytecodes::Code c_new) {
bool MethodComparator::args_same(Bytecodes::Code const c_old, Bytecodes::Code const c_new,
BytecodeStream* const s_old, BytecodeStream* const s_new,
ConstantPool* const old_cp, ConstantPool* const new_cp) {
// BytecodeStream returns the correct standard Java bytecodes for various "fast"
// bytecode versions, so we don't have to bother about them here..
switch (c_old) {
@ -78,12 +73,12 @@ bool MethodComparator::args_same(Bytecodes::Code c_old, Bytecodes::Code c_new) {
case Bytecodes::_multianewarray : // fall through
case Bytecodes::_checkcast : // fall through
case Bytecodes::_instanceof : {
u2 cpi_old = _s_old->get_index_u2();
u2 cpi_new = _s_new->get_index_u2();
if ((_old_cp->klass_at_noresolve(cpi_old) != _new_cp->klass_at_noresolve(cpi_new)))
u2 cpi_old = s_old->get_index_u2();
u2 cpi_new = s_new->get_index_u2();
if (old_cp->klass_at_noresolve(cpi_old) != new_cp->klass_at_noresolve(cpi_new))
return false;
if (c_old == Bytecodes::_multianewarray &&
*(jbyte*)(_s_old->bcp() + 3) != *(jbyte*)(_s_new->bcp() + 3))
*(jbyte*)(s_old->bcp() + 3) != *(jbyte*)(s_new->bcp() + 3))
return false;
break;
}
@ -96,46 +91,46 @@ bool MethodComparator::args_same(Bytecodes::Code c_old, Bytecodes::Code c_new) {
case Bytecodes::_invokespecial : // fall through
case Bytecodes::_invokestatic : // fall through
case Bytecodes::_invokeinterface : {
int cpci_old = _s_old->get_index_u2_cpcache();
int cpci_new = _s_new->get_index_u2_cpcache();
int cpci_old = s_old->get_index_u2_cpcache();
int cpci_new = s_new->get_index_u2_cpcache();
// Check if the names of classes, field/method names and signatures at these indexes
// are the same. Indices which are really into constantpool cache (rather than constant
// pool itself) are accepted by the constantpool query routines below.
if ((_old_cp->klass_ref_at_noresolve(cpci_old) != _new_cp->klass_ref_at_noresolve(cpci_new)) ||
(_old_cp->name_ref_at(cpci_old) != _new_cp->name_ref_at(cpci_new)) ||
(_old_cp->signature_ref_at(cpci_old) != _new_cp->signature_ref_at(cpci_new)))
if ((old_cp->klass_ref_at_noresolve(cpci_old) != new_cp->klass_ref_at_noresolve(cpci_new)) ||
(old_cp->name_ref_at(cpci_old) != new_cp->name_ref_at(cpci_new)) ||
(old_cp->signature_ref_at(cpci_old) != new_cp->signature_ref_at(cpci_new)))
return false;
break;
}
case Bytecodes::_invokedynamic: {
int cpci_old = _s_old->get_index_u4();
int cpci_new = _s_new->get_index_u4();
int cpci_old = s_old->get_index_u4();
int cpci_new = s_new->get_index_u4();
// Check if the names of classes, field/method names and signatures at these indexes
// are the same. Indices which are really into constantpool cache (rather than constant
// pool itself) are accepted by the constantpool query routines below.
if ((_old_cp->name_ref_at(cpci_old) != _new_cp->name_ref_at(cpci_new)) ||
(_old_cp->signature_ref_at(cpci_old) != _new_cp->signature_ref_at(cpci_new)))
if ((old_cp->name_ref_at(cpci_old) != new_cp->name_ref_at(cpci_new)) ||
(old_cp->signature_ref_at(cpci_old) != new_cp->signature_ref_at(cpci_new)))
return false;
// Translate object indexes to constant pool cache indexes.
cpci_old = _old_cp->invokedynamic_cp_cache_index(cpci_old);
cpci_new = _new_cp->invokedynamic_cp_cache_index(cpci_new);
cpci_old = old_cp->invokedynamic_cp_cache_index(cpci_old);
cpci_new = new_cp->invokedynamic_cp_cache_index(cpci_new);
int cpi_old = _old_cp->cache()->entry_at(cpci_old)->constant_pool_index();
int cpi_new = _new_cp->cache()->entry_at(cpci_new)->constant_pool_index();
int bsm_old = _old_cp->bootstrap_method_ref_index_at(cpi_old);
int bsm_new = _new_cp->bootstrap_method_ref_index_at(cpi_new);
if (!pool_constants_same(bsm_old, bsm_new))
int cpi_old = old_cp->cache()->entry_at(cpci_old)->constant_pool_index();
int cpi_new = new_cp->cache()->entry_at(cpci_new)->constant_pool_index();
int bsm_old = old_cp->bootstrap_method_ref_index_at(cpi_old);
int bsm_new = new_cp->bootstrap_method_ref_index_at(cpi_new);
if (!pool_constants_same(bsm_old, bsm_new, old_cp, new_cp))
return false;
int cnt_old = _old_cp->bootstrap_argument_count_at(cpi_old);
int cnt_new = _new_cp->bootstrap_argument_count_at(cpi_new);
int cnt_old = old_cp->bootstrap_argument_count_at(cpi_old);
int cnt_new = new_cp->bootstrap_argument_count_at(cpi_new);
if (cnt_old != cnt_new)
return false;
for (int arg_i = 0; arg_i < cnt_old; arg_i++) {
int idx_old = _old_cp->bootstrap_argument_index_at(cpi_old, arg_i);
int idx_new = _new_cp->bootstrap_argument_index_at(cpi_new, arg_i);
if (!pool_constants_same(idx_old, idx_new))
int idx_old = old_cp->bootstrap_argument_index_at(cpi_old, arg_i);
int idx_new = new_cp->bootstrap_argument_index_at(cpi_new, arg_i);
if (!pool_constants_same(idx_old, idx_new, old_cp, new_cp))
return false;
}
break;
@ -143,41 +138,41 @@ bool MethodComparator::args_same(Bytecodes::Code c_old, Bytecodes::Code c_new) {
case Bytecodes::_ldc : // fall through
case Bytecodes::_ldc_w : {
Bytecode_loadconstant ldc_old(_s_old->method(), _s_old->bci());
Bytecode_loadconstant ldc_new(_s_new->method(), _s_new->bci());
Bytecode_loadconstant ldc_old(s_old->method(), s_old->bci());
Bytecode_loadconstant ldc_new(s_new->method(), s_new->bci());
int cpi_old = ldc_old.pool_index();
int cpi_new = ldc_new.pool_index();
if (!pool_constants_same(cpi_old, cpi_new))
if (!pool_constants_same(cpi_old, cpi_new, old_cp, new_cp))
return false;
break;
}
case Bytecodes::_ldc2_w : {
u2 cpi_old = _s_old->get_index_u2();
u2 cpi_new = _s_new->get_index_u2();
constantTag tag_old = _old_cp->tag_at(cpi_old);
constantTag tag_new = _new_cp->tag_at(cpi_new);
u2 cpi_old = s_old->get_index_u2();
u2 cpi_new = s_new->get_index_u2();
constantTag tag_old = old_cp->tag_at(cpi_old);
constantTag tag_new = new_cp->tag_at(cpi_new);
if (tag_old.value() != tag_new.value())
return false;
if (tag_old.is_long()) {
if (_old_cp->long_at(cpi_old) != _new_cp->long_at(cpi_new))
if (old_cp->long_at(cpi_old) != new_cp->long_at(cpi_new))
return false;
} else {
// Use jlong_cast to compare the bits rather than numerical values.
// This makes a difference for NaN constants.
if (jlong_cast(_old_cp->double_at(cpi_old)) != jlong_cast(_new_cp->double_at(cpi_new)))
if (jlong_cast(old_cp->double_at(cpi_old)) != jlong_cast(new_cp->double_at(cpi_new)))
return false;
}
break;
}
case Bytecodes::_bipush :
if (_s_old->bcp()[1] != _s_new->bcp()[1])
if (s_old->bcp()[1] != s_new->bcp()[1])
return false;
break;
case Bytecodes::_sipush :
if (_s_old->get_index_u2() != _s_new->get_index_u2())
if (s_old->get_index_u2() != s_new->get_index_u2())
return false;
break;
@ -192,9 +187,9 @@ bool MethodComparator::args_same(Bytecodes::Code c_old, Bytecodes::Code c_new) {
case Bytecodes::_lload : // fall through
case Bytecodes::_lstore : // fall through
case Bytecodes::_ret :
if (_s_old->is_wide() != _s_new->is_wide())
if (s_old->is_wide() != s_new->is_wide())
return false;
if (_s_old->get_index() != _s_new->get_index())
if (s_old->get_index() != s_new->get_index())
return false;
break;
@ -216,31 +211,31 @@ bool MethodComparator::args_same(Bytecodes::Code c_old, Bytecodes::Code c_new) {
case Bytecodes::_ifnonnull : // fall through
case Bytecodes::_ifnull : // fall through
case Bytecodes::_jsr : {
int old_ofs = _s_old->bytecode().get_offset_s2(c_old);
int new_ofs = _s_new->bytecode().get_offset_s2(c_new);
int old_ofs = s_old->bytecode().get_offset_s2(c_old);
int new_ofs = s_new->bytecode().get_offset_s2(c_new);
if (old_ofs != new_ofs)
return false;
break;
}
case Bytecodes::_iinc :
if (_s_old->is_wide() != _s_new->is_wide())
if (s_old->is_wide() != s_new->is_wide())
return false;
if (! _s_old->is_wide()) {
if (!s_old->is_wide()) {
// We could use get_index_u1 and get_constant_u1, but it's simpler to grab both bytes at once:
if (Bytes::get_Java_u2(_s_old->bcp() + 1) != Bytes::get_Java_u2(_s_new->bcp() + 1))
if (Bytes::get_Java_u2(s_old->bcp() + 1) != Bytes::get_Java_u2(s_new->bcp() + 1))
return false;
} else {
// We could use get_index_u2 and get_constant_u2, but it's simpler to grab all four bytes at once:
if (Bytes::get_Java_u4(_s_old->bcp() + 1) != Bytes::get_Java_u4(_s_new->bcp() + 1))
if (Bytes::get_Java_u4(s_old->bcp() + 1) != Bytes::get_Java_u4(s_new->bcp() + 1))
return false;
}
break;
case Bytecodes::_goto_w : // fall through
case Bytecodes::_jsr_w : {
int old_ofs = _s_old->bytecode().get_offset_s4(c_old);
int new_ofs = _s_new->bytecode().get_offset_s4(c_new);
int old_ofs = s_old->bytecode().get_offset_s4(c_old);
int new_ofs = s_new->bytecode().get_offset_s4(c_new);
if (old_ofs != new_ofs)
return false;
break;
@ -248,11 +243,11 @@ bool MethodComparator::args_same(Bytecodes::Code c_old, Bytecodes::Code c_new) {
case Bytecodes::_lookupswitch : // fall through
case Bytecodes::_tableswitch : {
int len_old = _s_old->instruction_size();
int len_new = _s_new->instruction_size();
int len_old = s_old->instruction_size();
int len_new = s_new->instruction_size();
if (len_old != len_new)
return false;
if (memcmp(_s_old->bcp(), _s_new->bcp(), len_old) != 0)
if (memcmp(s_old->bcp(), s_new->bcp(), len_old) != 0)
return false;
break;
}
@ -264,48 +259,49 @@ bool MethodComparator::args_same(Bytecodes::Code c_old, Bytecodes::Code c_new) {
return true;
}
bool MethodComparator::pool_constants_same(int cpi_old, int cpi_new) {
constantTag tag_old = _old_cp->tag_at(cpi_old);
constantTag tag_new = _new_cp->tag_at(cpi_new);
bool MethodComparator::pool_constants_same(const int cpi_old, const int cpi_new,
ConstantPool* const old_cp, ConstantPool* const new_cp) {
constantTag tag_old = old_cp->tag_at(cpi_old);
constantTag tag_new = new_cp->tag_at(cpi_new);
if (tag_old.is_int() || tag_old.is_float()) {
if (tag_old.value() != tag_new.value())
return false;
if (tag_old.is_int()) {
if (_old_cp->int_at(cpi_old) != _new_cp->int_at(cpi_new))
if (old_cp->int_at(cpi_old) != new_cp->int_at(cpi_new))
return false;
} else {
// Use jint_cast to compare the bits rather than numerical values.
// This makes a difference for NaN constants.
if (jint_cast(_old_cp->float_at(cpi_old)) != jint_cast(_new_cp->float_at(cpi_new)))
if (jint_cast(old_cp->float_at(cpi_old)) != jint_cast(new_cp->float_at(cpi_new)))
return false;
}
} else if (tag_old.is_string() && tag_new.is_string()) {
if (strcmp(_old_cp->string_at_noresolve(cpi_old),
_new_cp->string_at_noresolve(cpi_new)) != 0)
if (strcmp(old_cp->string_at_noresolve(cpi_old),
new_cp->string_at_noresolve(cpi_new)) != 0)
return false;
if (_old_cp->is_pseudo_string_at(cpi_old) || _new_cp->is_pseudo_string_at(cpi_new))
return (_old_cp->is_pseudo_string_at(cpi_old) == _new_cp->is_pseudo_string_at(cpi_new));
if (old_cp->is_pseudo_string_at(cpi_old) || new_cp->is_pseudo_string_at(cpi_new))
return (old_cp->is_pseudo_string_at(cpi_old) == new_cp->is_pseudo_string_at(cpi_new));
} else if (tag_old.is_klass() || tag_old.is_unresolved_klass()) {
// tag_old should be klass - 4881222
if (! (tag_new.is_unresolved_klass() || tag_new.is_klass()))
return false;
if (_old_cp->klass_at_noresolve(cpi_old) !=
_new_cp->klass_at_noresolve(cpi_new))
if (old_cp->klass_at_noresolve(cpi_old) !=
new_cp->klass_at_noresolve(cpi_new))
return false;
} else if (tag_old.is_method_type() && tag_new.is_method_type()) {
int mti_old = _old_cp->method_type_index_at(cpi_old);
int mti_new = _new_cp->method_type_index_at(cpi_new);
if ((_old_cp->symbol_at(mti_old) != _new_cp->symbol_at(mti_new)))
int mti_old = old_cp->method_type_index_at(cpi_old);
int mti_new = new_cp->method_type_index_at(cpi_new);
if ((old_cp->symbol_at(mti_old) != new_cp->symbol_at(mti_new)))
return false;
} else if (tag_old.is_method_handle() && tag_new.is_method_handle()) {
if (_old_cp->method_handle_ref_kind_at(cpi_old) !=
_new_cp->method_handle_ref_kind_at(cpi_new))
if (old_cp->method_handle_ref_kind_at(cpi_old) !=
new_cp->method_handle_ref_kind_at(cpi_new))
return false;
int mhi_old = _old_cp->method_handle_index_at(cpi_old);
int mhi_new = _new_cp->method_handle_index_at(cpi_new);
if ((_old_cp->uncached_klass_ref_at_noresolve(mhi_old) != _new_cp->uncached_klass_ref_at_noresolve(mhi_new)) ||
(_old_cp->uncached_name_ref_at(mhi_old) != _new_cp->uncached_name_ref_at(mhi_new)) ||
(_old_cp->uncached_signature_ref_at(mhi_old) != _new_cp->uncached_signature_ref_at(mhi_new)))
int mhi_old = old_cp->method_handle_index_at(cpi_old);
int mhi_new = new_cp->method_handle_index_at(cpi_new);
if ((old_cp->uncached_klass_ref_at_noresolve(mhi_old) != new_cp->uncached_klass_ref_at_noresolve(mhi_new)) ||
(old_cp->uncached_name_ref_at(mhi_old) != new_cp->uncached_name_ref_at(mhi_new)) ||
(old_cp->uncached_signature_ref_at(mhi_old) != new_cp->uncached_signature_ref_at(mhi_new)))
return false;
} else {
return false; // unknown tag

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2021, 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
@ -34,13 +34,14 @@
class MethodComparator {
private:
static BytecodeStream *_s_old, *_s_new;
static ConstantPool* _old_cp;
static ConstantPool* _new_cp;
static bool args_same(Bytecodes::Code const c_old, Bytecodes::Code const c_new,
BytecodeStream* const s_old, BytecodeStream* const s_new,
ConstantPool* const old_cp, ConstantPool* const new_cp);
static bool args_same(Bytecodes::Code c_old, Bytecodes::Code c_new);
static bool pool_constants_same(int cpi_old, int cpi_new);
static int check_stack_and_locals_size(Method* old_method, Method* new_method);
static bool pool_constants_same(const int cpi_old, const int cpi_new,
ConstantPool* const old_cp, ConstantPool* const new_cp);
static int check_stack_and_locals_size(Method* const old_method, Method* const new_method);
public:
// Check if the new method is equivalent to the old one modulo constant pool (EMCP).