1 /*
2 * Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "oops/oop.inline.hpp"
27 #include "oops/symbol.hpp"
28 #include "prims/jvmtiRedefineClassesTrace.hpp"
29 #include "prims/methodComparator.hpp"
30 #include "runtime/handles.inline.hpp"
31 #include "utilities/globalDefinitions.hpp"
32
33 BytecodeStream *MethodComparator::_s_old;
34 BytecodeStream *MethodComparator::_s_new;
35 ConstantPool* MethodComparator::_old_cp;
36 ConstantPool* MethodComparator::_new_cp;
37 BciMap *MethodComparator::_bci_map;
38 bool MethodComparator::_switchable_test;
39 GrowableArray<int> *MethodComparator::_fwd_jmps;
40
41 bool MethodComparator::methods_EMCP(Method* old_method, Method* new_method) {
42 if (old_method->code_size() != new_method->code_size())
43 return false;
44 if (check_stack_and_locals_size(old_method, new_method) != 0) {
45 // RC_TRACE macro has an embedded ResourceMark
46 RC_TRACE(0x00800000, ("Methods %s non-comparable with diagnosis %d",
47 old_method->name()->as_C_string(),
48 check_stack_and_locals_size(old_method, new_method)));
49 return false;
50 }
51
52 _old_cp = old_method->constants();
53 _new_cp = new_method->constants();
54 BytecodeStream s_old(old_method);
55 BytecodeStream s_new(new_method);
56 _s_old = &s_old;
57 _s_new = &s_new;
58 _switchable_test = false;
59 Bytecodes::Code c_old, c_new;
60
61 while ((c_old = s_old.next()) >= 0) {
62 if ((c_new = s_new.next()) < 0 || c_old != c_new)
63 return false;
64
65 if (! args_same(c_old, c_new))
66 return false;
67 }
68 return true;
69 }
70
71
72 bool MethodComparator::methods_switchable(Method* old_method, Method* new_method,
73 BciMap &bci_map) {
74 if (old_method->code_size() > new_method->code_size())
75 // Something has definitely been deleted in the new method, compared to the old one.
76 return false;
77
78 if (! check_stack_and_locals_size(old_method, new_method))
79 return false;
80
81 _old_cp = old_method->constants();
82 _new_cp = new_method->constants();
83 BytecodeStream s_old(old_method);
84 BytecodeStream s_new(new_method);
85 _s_old = &s_old;
86 _s_new = &s_new;
87 _bci_map = &bci_map;
88 _switchable_test = true;
89 GrowableArray<int> fwd_jmps(16);
90 _fwd_jmps = &fwd_jmps;
91 Bytecodes::Code c_old, c_new;
92
93 while ((c_old = s_old.next()) >= 0) {
94 if ((c_new = s_new.next()) < 0)
95 return false;
96 if (! (c_old == c_new && args_same(c_old, c_new))) {
97 int old_bci = s_old.bci();
98 int new_st_bci = s_new.bci();
99 bool found_match = false;
100 do {
101 c_new = s_new.next();
102 if (c_new == c_old && args_same(c_old, c_new)) {
103 found_match = true;
104 break;
105 }
106 } while (c_new >= 0);
107 if (! found_match)
108 return false;
109 int new_end_bci = s_new.bci();
110 bci_map.store_fragment_location(old_bci, new_st_bci, new_end_bci);
111 }
112 }
113
114 // Now we can test all forward jumps
115 for (int i = 0; i < fwd_jmps.length() / 2; i++) {
116 if (! bci_map.old_and_new_locations_same(fwd_jmps.at(i*2), fwd_jmps.at(i*2+1))) {
117 RC_TRACE(0x00800000,
118 ("Fwd jump miss: old dest = %d, calc new dest = %d, act new dest = %d",
119 fwd_jmps.at(i*2), bci_map.new_bci_for_old(fwd_jmps.at(i*2)),
120 fwd_jmps.at(i*2+1)));
121 return false;
122 }
123 }
124
125 return true;
126 }
127
128
129 bool MethodComparator::args_same(Bytecodes::Code c_old, Bytecodes::Code c_new) {
130 // BytecodeStream returns the correct standard Java bytecodes for various "fast"
131 // bytecode versions, so we don't have to bother about them here..
132 switch (c_old) {
133 case Bytecodes::_new : // fall through
134 case Bytecodes::_anewarray : // fall through
135 case Bytecodes::_multianewarray : // fall through
136 case Bytecodes::_checkcast : // fall through
137 case Bytecodes::_instanceof : {
138 u2 cpi_old = _s_old->get_index_u2();
139 u2 cpi_new = _s_new->get_index_u2();
140 if ((_old_cp->klass_at_noresolve(cpi_old) != _new_cp->klass_at_noresolve(cpi_new)))
141 return false;
142 if (c_old == Bytecodes::_multianewarray &&
143 *(jbyte*)(_s_old->bcp() + 3) != *(jbyte*)(_s_new->bcp() + 3))
144 return false;
145 break;
146 }
147
148 case Bytecodes::_getstatic : // fall through
258 case Bytecodes::_goto : // fall through
259 case Bytecodes::_if_acmpeq : // fall through
260 case Bytecodes::_if_acmpne : // fall through
261 case Bytecodes::_if_icmpeq : // fall through
262 case Bytecodes::_if_icmpne : // fall through
263 case Bytecodes::_if_icmplt : // fall through
264 case Bytecodes::_if_icmpge : // fall through
265 case Bytecodes::_if_icmpgt : // fall through
266 case Bytecodes::_if_icmple : // fall through
267 case Bytecodes::_ifeq : // fall through
268 case Bytecodes::_ifne : // fall through
269 case Bytecodes::_iflt : // fall through
270 case Bytecodes::_ifge : // fall through
271 case Bytecodes::_ifgt : // fall through
272 case Bytecodes::_ifle : // fall through
273 case Bytecodes::_ifnonnull : // fall through
274 case Bytecodes::_ifnull : // fall through
275 case Bytecodes::_jsr : {
276 int old_ofs = _s_old->bytecode().get_offset_s2(c_old);
277 int new_ofs = _s_new->bytecode().get_offset_s2(c_new);
278 if (_switchable_test) {
279 int old_dest = _s_old->bci() + old_ofs;
280 int new_dest = _s_new->bci() + new_ofs;
281 if (old_ofs < 0 && new_ofs < 0) {
282 if (! _bci_map->old_and_new_locations_same(old_dest, new_dest))
283 return false;
284 } else if (old_ofs > 0 && new_ofs > 0) {
285 _fwd_jmps->append(old_dest);
286 _fwd_jmps->append(new_dest);
287 } else {
288 return false;
289 }
290 } else {
291 if (old_ofs != new_ofs)
292 return false;
293 }
294 break;
295 }
296
297 case Bytecodes::_iinc :
298 if (_s_old->is_wide() != _s_new->is_wide())
299 return false;
300 if (! _s_old->is_wide()) {
301 // We could use get_index_u1 and get_constant_u1, but it's simpler to grab both bytes at once:
302 if (Bytes::get_Java_u2(_s_old->bcp() + 1) != Bytes::get_Java_u2(_s_new->bcp() + 1))
303 return false;
304 } else {
305 // We could use get_index_u2 and get_constant_u2, but it's simpler to grab all four bytes at once:
306 if (Bytes::get_Java_u4(_s_old->bcp() + 1) != Bytes::get_Java_u4(_s_new->bcp() + 1))
307 return false;
308 }
309 break;
310
311 case Bytecodes::_goto_w : // fall through
312 case Bytecodes::_jsr_w : {
313 int old_ofs = _s_old->bytecode().get_offset_s4(c_old);
314 int new_ofs = _s_new->bytecode().get_offset_s4(c_new);
315 if (_switchable_test) {
316 int old_dest = _s_old->bci() + old_ofs;
317 int new_dest = _s_new->bci() + new_ofs;
318 if (old_ofs < 0 && new_ofs < 0) {
319 if (! _bci_map->old_and_new_locations_same(old_dest, new_dest))
320 return false;
321 } else if (old_ofs > 0 && new_ofs > 0) {
322 _fwd_jmps->append(old_dest);
323 _fwd_jmps->append(new_dest);
324 } else {
325 return false;
326 }
327 } else {
328 if (old_ofs != new_ofs)
329 return false;
330 }
331 break;
332 }
333
334 case Bytecodes::_lookupswitch : // fall through
335 case Bytecodes::_tableswitch : {
336 if (_switchable_test) {
337 address aligned_bcp_old = (address) round_to((intptr_t)_s_old->bcp() + 1, jintSize);
338 address aligned_bcp_new = (address) round_to((intptr_t)_s_new->bcp() + 1, jintSize);
339 int default_old = (int) Bytes::get_Java_u4(aligned_bcp_old);
340 int default_new = (int) Bytes::get_Java_u4(aligned_bcp_new);
341 _fwd_jmps->append(_s_old->bci() + default_old);
342 _fwd_jmps->append(_s_new->bci() + default_new);
343 if (c_old == Bytecodes::_lookupswitch) {
344 int npairs_old = (int) Bytes::get_Java_u4(aligned_bcp_old + jintSize);
345 int npairs_new = (int) Bytes::get_Java_u4(aligned_bcp_new + jintSize);
346 if (npairs_old != npairs_new)
347 return false;
348 for (int i = 0; i < npairs_old; i++) {
349 int match_old = (int) Bytes::get_Java_u4(aligned_bcp_old + (2+2*i)*jintSize);
350 int match_new = (int) Bytes::get_Java_u4(aligned_bcp_new + (2+2*i)*jintSize);
351 if (match_old != match_new)
352 return false;
353 int ofs_old = (int) Bytes::get_Java_u4(aligned_bcp_old + (2+2*i+1)*jintSize);
354 int ofs_new = (int) Bytes::get_Java_u4(aligned_bcp_new + (2+2*i+1)*jintSize);
355 _fwd_jmps->append(_s_old->bci() + ofs_old);
356 _fwd_jmps->append(_s_new->bci() + ofs_new);
357 }
358 } else if (c_old == Bytecodes::_tableswitch) {
359 int lo_old = (int) Bytes::get_Java_u4(aligned_bcp_old + jintSize);
360 int lo_new = (int) Bytes::get_Java_u4(aligned_bcp_new + jintSize);
361 if (lo_old != lo_new)
362 return false;
363 int hi_old = (int) Bytes::get_Java_u4(aligned_bcp_old + 2*jintSize);
364 int hi_new = (int) Bytes::get_Java_u4(aligned_bcp_new + 2*jintSize);
365 if (hi_old != hi_new)
366 return false;
367 for (int i = 0; i < hi_old - lo_old + 1; i++) {
368 int ofs_old = (int) Bytes::get_Java_u4(aligned_bcp_old + (3+i)*jintSize);
369 int ofs_new = (int) Bytes::get_Java_u4(aligned_bcp_new + (3+i)*jintSize);
370 _fwd_jmps->append(_s_old->bci() + ofs_old);
371 _fwd_jmps->append(_s_new->bci() + ofs_new);
372 }
373 }
374 } else { // !_switchable_test, can use fast rough compare
375 int len_old = _s_old->instruction_size();
376 int len_new = _s_new->instruction_size();
377 if (len_old != len_new)
378 return false;
379 if (memcmp(_s_old->bcp(), _s_new->bcp(), len_old) != 0)
380 return false;
381 }
382 break;
383 }
384 }
385
386 return true;
387 }
388
389 bool MethodComparator::pool_constants_same(int cpi_old, int cpi_new) {
390 constantTag tag_old = _old_cp->tag_at(cpi_old);
391 constantTag tag_new = _new_cp->tag_at(cpi_new);
392 if (tag_old.is_int() || tag_old.is_float()) {
393 if (tag_old.value() != tag_new.value())
394 return false;
395 if (tag_old.is_int()) {
396 if (_old_cp->int_at(cpi_old) != _new_cp->int_at(cpi_new))
397 return false;
398 } else {
399 // Use jint_cast to compare the bits rather than numerical values.
400 // This makes a difference for NaN constants.
401 if (jint_cast(_old_cp->float_at(cpi_old)) != jint_cast(_new_cp->float_at(cpi_new)))
|
1 /*
2 * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "oops/oop.inline.hpp"
27 #include "oops/symbol.hpp"
28 #include "prims/jvmtiRedefineClassesTrace.hpp"
29 #include "prims/methodComparator.hpp"
30 #include "runtime/handles.inline.hpp"
31 #include "utilities/globalDefinitions.hpp"
32
33 BytecodeStream *MethodComparator::_s_old;
34 BytecodeStream *MethodComparator::_s_new;
35 ConstantPool* MethodComparator::_old_cp;
36 ConstantPool* MethodComparator::_new_cp;
37 BciMap *MethodComparator::_bci_map;
38 GrowableArray<int> *MethodComparator::_fwd_jmps;
39
40 bool MethodComparator::methods_EMCP(Method* old_method, Method* new_method) {
41 if (old_method->code_size() != new_method->code_size())
42 return false;
43 if (check_stack_and_locals_size(old_method, new_method) != 0) {
44 // RC_TRACE macro has an embedded ResourceMark
45 RC_TRACE(0x00800000, ("Methods %s non-comparable with diagnosis %d",
46 old_method->name()->as_C_string(),
47 check_stack_and_locals_size(old_method, new_method)));
48 return false;
49 }
50
51 _old_cp = old_method->constants();
52 _new_cp = new_method->constants();
53 BytecodeStream s_old(old_method);
54 BytecodeStream s_new(new_method);
55 _s_old = &s_old;
56 _s_new = &s_new;
57 Bytecodes::Code c_old, c_new;
58
59 while ((c_old = s_old.next()) >= 0) {
60 if ((c_new = s_new.next()) < 0 || c_old != c_new)
61 return false;
62
63 if (! args_same(c_old, c_new))
64 return false;
65 }
66 return true;
67 }
68
69 bool MethodComparator::args_same(Bytecodes::Code c_old, Bytecodes::Code c_new) {
70 // BytecodeStream returns the correct standard Java bytecodes for various "fast"
71 // bytecode versions, so we don't have to bother about them here..
72 switch (c_old) {
73 case Bytecodes::_new : // fall through
74 case Bytecodes::_anewarray : // fall through
75 case Bytecodes::_multianewarray : // fall through
76 case Bytecodes::_checkcast : // fall through
77 case Bytecodes::_instanceof : {
78 u2 cpi_old = _s_old->get_index_u2();
79 u2 cpi_new = _s_new->get_index_u2();
80 if ((_old_cp->klass_at_noresolve(cpi_old) != _new_cp->klass_at_noresolve(cpi_new)))
81 return false;
82 if (c_old == Bytecodes::_multianewarray &&
83 *(jbyte*)(_s_old->bcp() + 3) != *(jbyte*)(_s_new->bcp() + 3))
84 return false;
85 break;
86 }
87
88 case Bytecodes::_getstatic : // fall through
198 case Bytecodes::_goto : // fall through
199 case Bytecodes::_if_acmpeq : // fall through
200 case Bytecodes::_if_acmpne : // fall through
201 case Bytecodes::_if_icmpeq : // fall through
202 case Bytecodes::_if_icmpne : // fall through
203 case Bytecodes::_if_icmplt : // fall through
204 case Bytecodes::_if_icmpge : // fall through
205 case Bytecodes::_if_icmpgt : // fall through
206 case Bytecodes::_if_icmple : // fall through
207 case Bytecodes::_ifeq : // fall through
208 case Bytecodes::_ifne : // fall through
209 case Bytecodes::_iflt : // fall through
210 case Bytecodes::_ifge : // fall through
211 case Bytecodes::_ifgt : // fall through
212 case Bytecodes::_ifle : // fall through
213 case Bytecodes::_ifnonnull : // fall through
214 case Bytecodes::_ifnull : // fall through
215 case Bytecodes::_jsr : {
216 int old_ofs = _s_old->bytecode().get_offset_s2(c_old);
217 int new_ofs = _s_new->bytecode().get_offset_s2(c_new);
218 if (old_ofs != new_ofs)
219 return false;
220 break;
221 }
222
223 case Bytecodes::_iinc :
224 if (_s_old->is_wide() != _s_new->is_wide())
225 return false;
226 if (! _s_old->is_wide()) {
227 // We could use get_index_u1 and get_constant_u1, but it's simpler to grab both bytes at once:
228 if (Bytes::get_Java_u2(_s_old->bcp() + 1) != Bytes::get_Java_u2(_s_new->bcp() + 1))
229 return false;
230 } else {
231 // We could use get_index_u2 and get_constant_u2, but it's simpler to grab all four bytes at once:
232 if (Bytes::get_Java_u4(_s_old->bcp() + 1) != Bytes::get_Java_u4(_s_new->bcp() + 1))
233 return false;
234 }
235 break;
236
237 case Bytecodes::_goto_w : // fall through
238 case Bytecodes::_jsr_w : {
239 int old_ofs = _s_old->bytecode().get_offset_s4(c_old);
240 int new_ofs = _s_new->bytecode().get_offset_s4(c_new);
241 if (old_ofs != new_ofs)
242 return false;
243 break;
244 }
245
246 case Bytecodes::_lookupswitch : // fall through
247 case Bytecodes::_tableswitch : {
248 int len_old = _s_old->instruction_size();
249 int len_new = _s_new->instruction_size();
250 if (len_old != len_new)
251 return false;
252 if (memcmp(_s_old->bcp(), _s_new->bcp(), len_old) != 0)
253 return false;
254 break;
255 }
256 }
257
258 return true;
259 }
260
261 bool MethodComparator::pool_constants_same(int cpi_old, int cpi_new) {
262 constantTag tag_old = _old_cp->tag_at(cpi_old);
263 constantTag tag_new = _new_cp->tag_at(cpi_new);
264 if (tag_old.is_int() || tag_old.is_float()) {
265 if (tag_old.value() != tag_new.value())
266 return false;
267 if (tag_old.is_int()) {
268 if (_old_cp->int_at(cpi_old) != _new_cp->int_at(cpi_new))
269 return false;
270 } else {
271 // Use jint_cast to compare the bits rather than numerical values.
272 // This makes a difference for NaN constants.
273 if (jint_cast(_old_cp->float_at(cpi_old)) != jint_cast(_new_cp->float_at(cpi_new)))
|