Skip to content

Commit f9f2eef

Browse files
committed
8263434: Dangling references after MethodComparator::methods_EMCP
Reviewed-by: coleenp, sspitsyn
1 parent 23fc2a4 commit f9f2eef

File tree

2 files changed

+86
-89
lines changed

2 files changed

+86
-89
lines changed

src/hotspot/share/prims/methodComparator.cpp

+78-82
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -32,11 +32,6 @@
3232
#include "runtime/handles.inline.hpp"
3333
#include "utilities/globalDefinitions.hpp"
3434

35-
BytecodeStream *MethodComparator::_s_old;
36-
BytecodeStream *MethodComparator::_s_new;
37-
ConstantPool* MethodComparator::_old_cp;
38-
ConstantPool* MethodComparator::_new_cp;
39-
4035
bool MethodComparator::methods_EMCP(Method* old_method, Method* new_method) {
4136
if (old_method->code_size() != new_method->code_size())
4237
return false;
@@ -50,26 +45,26 @@ bool MethodComparator::methods_EMCP(Method* old_method, Method* new_method) {
5045
return false;
5146
}
5247

53-
_old_cp = old_method->constants();
54-
_new_cp = new_method->constants();
48+
ConstantPool* old_cp = old_method->constants();
49+
ConstantPool* new_cp = new_method->constants();
5550
Thread* THREAD = Thread::current();
5651
BytecodeStream s_old(methodHandle(THREAD, old_method));
5752
BytecodeStream s_new(methodHandle(THREAD, new_method));
58-
_s_old = &s_old;
59-
_s_new = &s_new;
6053
Bytecodes::Code c_old, c_new;
6154

6255
while ((c_old = s_old.next()) >= 0) {
6356
if ((c_new = s_new.next()) < 0 || c_old != c_new)
6457
return false;
6558

66-
if (! args_same(c_old, c_new))
59+
if (!args_same(c_old, c_new, &s_old, &s_new, old_cp, new_cp))
6760
return false;
6861
}
6962
return true;
7063
}
7164

72-
bool MethodComparator::args_same(Bytecodes::Code c_old, Bytecodes::Code c_new) {
65+
bool MethodComparator::args_same(Bytecodes::Code const c_old, Bytecodes::Code const c_new,
66+
BytecodeStream* const s_old, BytecodeStream* const s_new,
67+
ConstantPool* const old_cp, ConstantPool* const new_cp) {
7368
// BytecodeStream returns the correct standard Java bytecodes for various "fast"
7469
// bytecode versions, so we don't have to bother about them here..
7570
switch (c_old) {
@@ -78,12 +73,12 @@ bool MethodComparator::args_same(Bytecodes::Code c_old, Bytecodes::Code c_new) {
7873
case Bytecodes::_multianewarray : // fall through
7974
case Bytecodes::_checkcast : // fall through
8075
case Bytecodes::_instanceof : {
81-
u2 cpi_old = _s_old->get_index_u2();
82-
u2 cpi_new = _s_new->get_index_u2();
83-
if ((_old_cp->klass_at_noresolve(cpi_old) != _new_cp->klass_at_noresolve(cpi_new)))
76+
u2 cpi_old = s_old->get_index_u2();
77+
u2 cpi_new = s_new->get_index_u2();
78+
if (old_cp->klass_at_noresolve(cpi_old) != new_cp->klass_at_noresolve(cpi_new))
8479
return false;
8580
if (c_old == Bytecodes::_multianewarray &&
86-
*(jbyte*)(_s_old->bcp() + 3) != *(jbyte*)(_s_new->bcp() + 3))
81+
*(jbyte*)(s_old->bcp() + 3) != *(jbyte*)(s_new->bcp() + 3))
8782
return false;
8883
break;
8984
}
@@ -96,88 +91,88 @@ bool MethodComparator::args_same(Bytecodes::Code c_old, Bytecodes::Code c_new) {
9691
case Bytecodes::_invokespecial : // fall through
9792
case Bytecodes::_invokestatic : // fall through
9893
case Bytecodes::_invokeinterface : {
99-
int cpci_old = _s_old->get_index_u2_cpcache();
100-
int cpci_new = _s_new->get_index_u2_cpcache();
94+
int cpci_old = s_old->get_index_u2_cpcache();
95+
int cpci_new = s_new->get_index_u2_cpcache();
10196
// Check if the names of classes, field/method names and signatures at these indexes
10297
// are the same. Indices which are really into constantpool cache (rather than constant
10398
// pool itself) are accepted by the constantpool query routines below.
104-
if ((_old_cp->klass_ref_at_noresolve(cpci_old) != _new_cp->klass_ref_at_noresolve(cpci_new)) ||
105-
(_old_cp->name_ref_at(cpci_old) != _new_cp->name_ref_at(cpci_new)) ||
106-
(_old_cp->signature_ref_at(cpci_old) != _new_cp->signature_ref_at(cpci_new)))
99+
if ((old_cp->klass_ref_at_noresolve(cpci_old) != new_cp->klass_ref_at_noresolve(cpci_new)) ||
100+
(old_cp->name_ref_at(cpci_old) != new_cp->name_ref_at(cpci_new)) ||
101+
(old_cp->signature_ref_at(cpci_old) != new_cp->signature_ref_at(cpci_new)))
107102
return false;
108103
break;
109104
}
110105
case Bytecodes::_invokedynamic: {
111-
int cpci_old = _s_old->get_index_u4();
112-
int cpci_new = _s_new->get_index_u4();
106+
int cpci_old = s_old->get_index_u4();
107+
int cpci_new = s_new->get_index_u4();
113108

114109
// Check if the names of classes, field/method names and signatures at these indexes
115110
// are the same. Indices which are really into constantpool cache (rather than constant
116111
// pool itself) are accepted by the constantpool query routines below.
117-
if ((_old_cp->name_ref_at(cpci_old) != _new_cp->name_ref_at(cpci_new)) ||
118-
(_old_cp->signature_ref_at(cpci_old) != _new_cp->signature_ref_at(cpci_new)))
112+
if ((old_cp->name_ref_at(cpci_old) != new_cp->name_ref_at(cpci_new)) ||
113+
(old_cp->signature_ref_at(cpci_old) != new_cp->signature_ref_at(cpci_new)))
119114
return false;
120115

121116
// Translate object indexes to constant pool cache indexes.
122-
cpci_old = _old_cp->invokedynamic_cp_cache_index(cpci_old);
123-
cpci_new = _new_cp->invokedynamic_cp_cache_index(cpci_new);
117+
cpci_old = old_cp->invokedynamic_cp_cache_index(cpci_old);
118+
cpci_new = new_cp->invokedynamic_cp_cache_index(cpci_new);
124119

125-
int cpi_old = _old_cp->cache()->entry_at(cpci_old)->constant_pool_index();
126-
int cpi_new = _new_cp->cache()->entry_at(cpci_new)->constant_pool_index();
127-
int bsm_old = _old_cp->bootstrap_method_ref_index_at(cpi_old);
128-
int bsm_new = _new_cp->bootstrap_method_ref_index_at(cpi_new);
129-
if (!pool_constants_same(bsm_old, bsm_new))
120+
int cpi_old = old_cp->cache()->entry_at(cpci_old)->constant_pool_index();
121+
int cpi_new = new_cp->cache()->entry_at(cpci_new)->constant_pool_index();
122+
int bsm_old = old_cp->bootstrap_method_ref_index_at(cpi_old);
123+
int bsm_new = new_cp->bootstrap_method_ref_index_at(cpi_new);
124+
if (!pool_constants_same(bsm_old, bsm_new, old_cp, new_cp))
130125
return false;
131-
int cnt_old = _old_cp->bootstrap_argument_count_at(cpi_old);
132-
int cnt_new = _new_cp->bootstrap_argument_count_at(cpi_new);
126+
int cnt_old = old_cp->bootstrap_argument_count_at(cpi_old);
127+
int cnt_new = new_cp->bootstrap_argument_count_at(cpi_new);
133128
if (cnt_old != cnt_new)
134129
return false;
135130
for (int arg_i = 0; arg_i < cnt_old; arg_i++) {
136-
int idx_old = _old_cp->bootstrap_argument_index_at(cpi_old, arg_i);
137-
int idx_new = _new_cp->bootstrap_argument_index_at(cpi_new, arg_i);
138-
if (!pool_constants_same(idx_old, idx_new))
131+
int idx_old = old_cp->bootstrap_argument_index_at(cpi_old, arg_i);
132+
int idx_new = new_cp->bootstrap_argument_index_at(cpi_new, arg_i);
133+
if (!pool_constants_same(idx_old, idx_new, old_cp, new_cp))
139134
return false;
140135
}
141136
break;
142137
}
143138

144139
case Bytecodes::_ldc : // fall through
145140
case Bytecodes::_ldc_w : {
146-
Bytecode_loadconstant ldc_old(_s_old->method(), _s_old->bci());
147-
Bytecode_loadconstant ldc_new(_s_new->method(), _s_new->bci());
141+
Bytecode_loadconstant ldc_old(s_old->method(), s_old->bci());
142+
Bytecode_loadconstant ldc_new(s_new->method(), s_new->bci());
148143
int cpi_old = ldc_old.pool_index();
149144
int cpi_new = ldc_new.pool_index();
150-
if (!pool_constants_same(cpi_old, cpi_new))
145+
if (!pool_constants_same(cpi_old, cpi_new, old_cp, new_cp))
151146
return false;
152147
break;
153148
}
154149

155150
case Bytecodes::_ldc2_w : {
156-
u2 cpi_old = _s_old->get_index_u2();
157-
u2 cpi_new = _s_new->get_index_u2();
158-
constantTag tag_old = _old_cp->tag_at(cpi_old);
159-
constantTag tag_new = _new_cp->tag_at(cpi_new);
151+
u2 cpi_old = s_old->get_index_u2();
152+
u2 cpi_new = s_new->get_index_u2();
153+
constantTag tag_old = old_cp->tag_at(cpi_old);
154+
constantTag tag_new = new_cp->tag_at(cpi_new);
160155
if (tag_old.value() != tag_new.value())
161156
return false;
162157
if (tag_old.is_long()) {
163-
if (_old_cp->long_at(cpi_old) != _new_cp->long_at(cpi_new))
158+
if (old_cp->long_at(cpi_old) != new_cp->long_at(cpi_new))
164159
return false;
165160
} else {
166161
// Use jlong_cast to compare the bits rather than numerical values.
167162
// This makes a difference for NaN constants.
168-
if (jlong_cast(_old_cp->double_at(cpi_old)) != jlong_cast(_new_cp->double_at(cpi_new)))
163+
if (jlong_cast(old_cp->double_at(cpi_old)) != jlong_cast(new_cp->double_at(cpi_new)))
169164
return false;
170165
}
171166
break;
172167
}
173168

174169
case Bytecodes::_bipush :
175-
if (_s_old->bcp()[1] != _s_new->bcp()[1])
170+
if (s_old->bcp()[1] != s_new->bcp()[1])
176171
return false;
177172
break;
178173

179174
case Bytecodes::_sipush :
180-
if (_s_old->get_index_u2() != _s_new->get_index_u2())
175+
if (s_old->get_index_u2() != s_new->get_index_u2())
181176
return false;
182177
break;
183178

@@ -192,9 +187,9 @@ bool MethodComparator::args_same(Bytecodes::Code c_old, Bytecodes::Code c_new) {
192187
case Bytecodes::_lload : // fall through
193188
case Bytecodes::_lstore : // fall through
194189
case Bytecodes::_ret :
195-
if (_s_old->is_wide() != _s_new->is_wide())
190+
if (s_old->is_wide() != s_new->is_wide())
196191
return false;
197-
if (_s_old->get_index() != _s_new->get_index())
192+
if (s_old->get_index() != s_new->get_index())
198193
return false;
199194
break;
200195

@@ -216,43 +211,43 @@ bool MethodComparator::args_same(Bytecodes::Code c_old, Bytecodes::Code c_new) {
216211
case Bytecodes::_ifnonnull : // fall through
217212
case Bytecodes::_ifnull : // fall through
218213
case Bytecodes::_jsr : {
219-
int old_ofs = _s_old->bytecode().get_offset_s2(c_old);
220-
int new_ofs = _s_new->bytecode().get_offset_s2(c_new);
214+
int old_ofs = s_old->bytecode().get_offset_s2(c_old);
215+
int new_ofs = s_new->bytecode().get_offset_s2(c_new);
221216
if (old_ofs != new_ofs)
222217
return false;
223218
break;
224219
}
225220

226221
case Bytecodes::_iinc :
227-
if (_s_old->is_wide() != _s_new->is_wide())
222+
if (s_old->is_wide() != s_new->is_wide())
228223
return false;
229-
if (! _s_old->is_wide()) {
224+
if (!s_old->is_wide()) {
230225
// We could use get_index_u1 and get_constant_u1, but it's simpler to grab both bytes at once:
231-
if (Bytes::get_Java_u2(_s_old->bcp() + 1) != Bytes::get_Java_u2(_s_new->bcp() + 1))
226+
if (Bytes::get_Java_u2(s_old->bcp() + 1) != Bytes::get_Java_u2(s_new->bcp() + 1))
232227
return false;
233228
} else {
234229
// We could use get_index_u2 and get_constant_u2, but it's simpler to grab all four bytes at once:
235-
if (Bytes::get_Java_u4(_s_old->bcp() + 1) != Bytes::get_Java_u4(_s_new->bcp() + 1))
230+
if (Bytes::get_Java_u4(s_old->bcp() + 1) != Bytes::get_Java_u4(s_new->bcp() + 1))
236231
return false;
237232
}
238233
break;
239234

240235
case Bytecodes::_goto_w : // fall through
241236
case Bytecodes::_jsr_w : {
242-
int old_ofs = _s_old->bytecode().get_offset_s4(c_old);
243-
int new_ofs = _s_new->bytecode().get_offset_s4(c_new);
237+
int old_ofs = s_old->bytecode().get_offset_s4(c_old);
238+
int new_ofs = s_new->bytecode().get_offset_s4(c_new);
244239
if (old_ofs != new_ofs)
245240
return false;
246241
break;
247242
}
248243

249244
case Bytecodes::_lookupswitch : // fall through
250245
case Bytecodes::_tableswitch : {
251-
int len_old = _s_old->instruction_size();
252-
int len_new = _s_new->instruction_size();
246+
int len_old = s_old->instruction_size();
247+
int len_new = s_new->instruction_size();
253248
if (len_old != len_new)
254249
return false;
255-
if (memcmp(_s_old->bcp(), _s_new->bcp(), len_old) != 0)
250+
if (memcmp(s_old->bcp(), s_new->bcp(), len_old) != 0)
256251
return false;
257252
break;
258253
}
@@ -264,48 +259,49 @@ bool MethodComparator::args_same(Bytecodes::Code c_old, Bytecodes::Code c_new) {
264259
return true;
265260
}
266261

267-
bool MethodComparator::pool_constants_same(int cpi_old, int cpi_new) {
268-
constantTag tag_old = _old_cp->tag_at(cpi_old);
269-
constantTag tag_new = _new_cp->tag_at(cpi_new);
262+
bool MethodComparator::pool_constants_same(const int cpi_old, const int cpi_new,
263+
ConstantPool* const old_cp, ConstantPool* const new_cp) {
264+
constantTag tag_old = old_cp->tag_at(cpi_old);
265+
constantTag tag_new = new_cp->tag_at(cpi_new);
270266
if (tag_old.is_int() || tag_old.is_float()) {
271267
if (tag_old.value() != tag_new.value())
272268
return false;
273269
if (tag_old.is_int()) {
274-
if (_old_cp->int_at(cpi_old) != _new_cp->int_at(cpi_new))
270+
if (old_cp->int_at(cpi_old) != new_cp->int_at(cpi_new))
275271
return false;
276272
} else {
277273
// Use jint_cast to compare the bits rather than numerical values.
278274
// This makes a difference for NaN constants.
279-
if (jint_cast(_old_cp->float_at(cpi_old)) != jint_cast(_new_cp->float_at(cpi_new)))
275+
if (jint_cast(old_cp->float_at(cpi_old)) != jint_cast(new_cp->float_at(cpi_new)))
280276
return false;
281277
}
282278
} else if (tag_old.is_string() && tag_new.is_string()) {
283-
if (strcmp(_old_cp->string_at_noresolve(cpi_old),
284-
_new_cp->string_at_noresolve(cpi_new)) != 0)
279+
if (strcmp(old_cp->string_at_noresolve(cpi_old),
280+
new_cp->string_at_noresolve(cpi_new)) != 0)
285281
return false;
286-
if (_old_cp->is_pseudo_string_at(cpi_old) || _new_cp->is_pseudo_string_at(cpi_new))
287-
return (_old_cp->is_pseudo_string_at(cpi_old) == _new_cp->is_pseudo_string_at(cpi_new));
282+
if (old_cp->is_pseudo_string_at(cpi_old) || new_cp->is_pseudo_string_at(cpi_new))
283+
return (old_cp->is_pseudo_string_at(cpi_old) == new_cp->is_pseudo_string_at(cpi_new));
288284
} else if (tag_old.is_klass() || tag_old.is_unresolved_klass()) {
289285
// tag_old should be klass - 4881222
290286
if (! (tag_new.is_unresolved_klass() || tag_new.is_klass()))
291287
return false;
292-
if (_old_cp->klass_at_noresolve(cpi_old) !=
293-
_new_cp->klass_at_noresolve(cpi_new))
288+
if (old_cp->klass_at_noresolve(cpi_old) !=
289+
new_cp->klass_at_noresolve(cpi_new))
294290
return false;
295291
} else if (tag_old.is_method_type() && tag_new.is_method_type()) {
296-
int mti_old = _old_cp->method_type_index_at(cpi_old);
297-
int mti_new = _new_cp->method_type_index_at(cpi_new);
298-
if ((_old_cp->symbol_at(mti_old) != _new_cp->symbol_at(mti_new)))
292+
int mti_old = old_cp->method_type_index_at(cpi_old);
293+
int mti_new = new_cp->method_type_index_at(cpi_new);
294+
if ((old_cp->symbol_at(mti_old) != new_cp->symbol_at(mti_new)))
299295
return false;
300296
} else if (tag_old.is_method_handle() && tag_new.is_method_handle()) {
301-
if (_old_cp->method_handle_ref_kind_at(cpi_old) !=
302-
_new_cp->method_handle_ref_kind_at(cpi_new))
297+
if (old_cp->method_handle_ref_kind_at(cpi_old) !=
298+
new_cp->method_handle_ref_kind_at(cpi_new))
303299
return false;
304-
int mhi_old = _old_cp->method_handle_index_at(cpi_old);
305-
int mhi_new = _new_cp->method_handle_index_at(cpi_new);
306-
if ((_old_cp->uncached_klass_ref_at_noresolve(mhi_old) != _new_cp->uncached_klass_ref_at_noresolve(mhi_new)) ||
307-
(_old_cp->uncached_name_ref_at(mhi_old) != _new_cp->uncached_name_ref_at(mhi_new)) ||
308-
(_old_cp->uncached_signature_ref_at(mhi_old) != _new_cp->uncached_signature_ref_at(mhi_new)))
300+
int mhi_old = old_cp->method_handle_index_at(cpi_old);
301+
int mhi_new = new_cp->method_handle_index_at(cpi_new);
302+
if ((old_cp->uncached_klass_ref_at_noresolve(mhi_old) != new_cp->uncached_klass_ref_at_noresolve(mhi_new)) ||
303+
(old_cp->uncached_name_ref_at(mhi_old) != new_cp->uncached_name_ref_at(mhi_new)) ||
304+
(old_cp->uncached_signature_ref_at(mhi_old) != new_cp->uncached_signature_ref_at(mhi_new)))
309305
return false;
310306
} else {
311307
return false; // unknown tag

src/hotspot/share/prims/methodComparator.hpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -34,13 +34,14 @@
3434

3535
class MethodComparator {
3636
private:
37-
static BytecodeStream *_s_old, *_s_new;
38-
static ConstantPool* _old_cp;
39-
static ConstantPool* _new_cp;
37+
static bool args_same(Bytecodes::Code const c_old, Bytecodes::Code const c_new,
38+
BytecodeStream* const s_old, BytecodeStream* const s_new,
39+
ConstantPool* const old_cp, ConstantPool* const new_cp);
4040

41-
static bool args_same(Bytecodes::Code c_old, Bytecodes::Code c_new);
42-
static bool pool_constants_same(int cpi_old, int cpi_new);
43-
static int check_stack_and_locals_size(Method* old_method, Method* new_method);
41+
static bool pool_constants_same(const int cpi_old, const int cpi_new,
42+
ConstantPool* const old_cp, ConstantPool* const new_cp);
43+
44+
static int check_stack_and_locals_size(Method* const old_method, Method* const new_method);
4445

4546
public:
4647
// Check if the new method is equivalent to the old one modulo constant pool (EMCP).

0 commit comments

Comments
 (0)