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 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 bool MethodComparator::args_same(Bytecodes::Code c_old, Bytecodes::Code c_new) { 72 // BytecodeStream returns the correct standard Java bytecodes for various "fast" 73 // bytecode versions, so we don't have to bother about them here.. 74 switch (c_old) { 75 case Bytecodes::_new : // fall through 76 case Bytecodes::_anewarray : // fall through 77 case Bytecodes::_multianewarray : // fall through 78 case Bytecodes::_checkcast : // fall through 79 case Bytecodes::_instanceof : { 80 u2 cpi_old = _s_old->get_index_u2(); 81 u2 cpi_new = _s_new->get_index_u2(); 82 if ((_old_cp->klass_at_noresolve(cpi_old) != _new_cp->klass_at_noresolve(cpi_new))) 83 return false; 84 if (c_old == Bytecodes::_multianewarray && 85 *(jbyte*)(_s_old->bcp() + 3) != *(jbyte*)(_s_new->bcp() + 3)) 86 return false; 87 break; 88 } 89 90 case Bytecodes::_getstatic : // fall through 91 case Bytecodes::_putstatic : // fall through 92 case Bytecodes::_getfield : // fall through 93 case Bytecodes::_putfield : // fall through 94 case Bytecodes::_invokevirtual : // fall through 95 case Bytecodes::_invokespecial : // fall through 96 case Bytecodes::_invokestatic : // fall through 97 case Bytecodes::_invokeinterface : { 98 int cpci_old = _s_old->get_index_u2_cpcache(); 99 int cpci_new = _s_new->get_index_u2_cpcache(); 100 // Check if the names of classes, field/method names and signatures at these indexes 101 // are the same. Indices which are really into constantpool cache (rather than constant 102 // pool itself) are accepted by the constantpool query routines below. 103 if ((_old_cp->klass_ref_at_noresolve(cpci_old) != _new_cp->klass_ref_at_noresolve(cpci_new)) || 104 (_old_cp->name_ref_at(cpci_old) != _new_cp->name_ref_at(cpci_new)) || 105 (_old_cp->signature_ref_at(cpci_old) != _new_cp->signature_ref_at(cpci_new))) 106 return false; 107 break; 108 } 109 case Bytecodes::_invokedynamic: { 110 int cpci_old = _s_old->get_index_u4(); 111 int cpci_new = _s_new->get_index_u4(); 112 113 // Check if the names of classes, field/method names and signatures at these indexes 114 // are the same. Indices which are really into constantpool cache (rather than constant 115 // pool itself) are accepted by the constantpool query routines below. 116 if ((_old_cp->name_ref_at(cpci_old) != _new_cp->name_ref_at(cpci_new)) || 117 (_old_cp->signature_ref_at(cpci_old) != _new_cp->signature_ref_at(cpci_new))) 118 return false; 119 120 // Translate object indexes to constant pool cache indexes. 121 cpci_old = _old_cp->invokedynamic_cp_cache_index(cpci_old); 122 cpci_new = _new_cp->invokedynamic_cp_cache_index(cpci_new); 123 124 int cpi_old = _old_cp->cache()->entry_at(cpci_old)->constant_pool_index(); 125 int cpi_new = _new_cp->cache()->entry_at(cpci_new)->constant_pool_index(); 126 int bsm_old = _old_cp->invoke_dynamic_bootstrap_method_ref_index_at(cpi_old); 127 int bsm_new = _new_cp->invoke_dynamic_bootstrap_method_ref_index_at(cpi_new); 128 if (!pool_constants_same(bsm_old, bsm_new)) 129 return false; 130 int cnt_old = _old_cp->invoke_dynamic_argument_count_at(cpi_old); 131 int cnt_new = _new_cp->invoke_dynamic_argument_count_at(cpi_new); 132 if (cnt_old != cnt_new) 133 return false; 134 for (int arg_i = 0; arg_i < cnt_old; arg_i++) { 135 int idx_old = _old_cp->invoke_dynamic_argument_index_at(cpi_old, arg_i); 136 int idx_new = _new_cp->invoke_dynamic_argument_index_at(cpi_new, arg_i); 137 if (!pool_constants_same(idx_old, idx_new)) 138 return false; 139 } 140 break; 141 } 142 143 case Bytecodes::_ldc : // fall through 144 case Bytecodes::_ldc_w : { 145 Bytecode_loadconstant ldc_old(_s_old->method(), _s_old->bci()); 146 Bytecode_loadconstant ldc_new(_s_new->method(), _s_new->bci()); 147 int cpi_old = ldc_old.pool_index(); 148 int cpi_new = ldc_new.pool_index(); 149 if (!pool_constants_same(cpi_old, cpi_new)) 150 return false; 151 break; 152 } 153 154 case Bytecodes::_ldc2_w : { 155 u2 cpi_old = _s_old->get_index_u2(); 156 u2 cpi_new = _s_new->get_index_u2(); 157 constantTag tag_old = _old_cp->tag_at(cpi_old); 158 constantTag tag_new = _new_cp->tag_at(cpi_new); 159 if (tag_old.value() != tag_new.value()) 160 return false; 161 if (tag_old.is_long()) { 162 if (_old_cp->long_at(cpi_old) != _new_cp->long_at(cpi_new)) 163 return false; 164 } else { 165 // Use jlong_cast to compare the bits rather than numerical values. 166 // This makes a difference for NaN constants. 167 if (jlong_cast(_old_cp->double_at(cpi_old)) != jlong_cast(_new_cp->double_at(cpi_new))) 168 return false; 169 } 170 break; 171 } 172 173 case Bytecodes::_bipush : 174 if (_s_old->bcp()[1] != _s_new->bcp()[1]) 175 return false; 176 break; 177 178 case Bytecodes::_sipush : 179 if (_s_old->get_index_u2() != _s_new->get_index_u2()) 180 return false; 181 break; 182 183 case Bytecodes::_aload : // fall through 184 case Bytecodes::_astore : // fall through 185 case Bytecodes::_dload : // fall through 186 case Bytecodes::_dstore : // fall through 187 case Bytecodes::_fload : // fall through 188 case Bytecodes::_fstore : // fall through 189 case Bytecodes::_iload : // fall through 190 case Bytecodes::_istore : // fall through 191 case Bytecodes::_lload : // fall through 192 case Bytecodes::_lstore : // fall through 193 case Bytecodes::_ret : 194 if (_s_old->is_wide() != _s_new->is_wide()) 195 return false; 196 if (_s_old->get_index() != _s_new->get_index()) 197 return false; 198 break; 199 200 case Bytecodes::_goto : // fall through 201 case Bytecodes::_if_acmpeq : // fall through 202 case Bytecodes::_if_acmpne : // fall through 203 case Bytecodes::_if_icmpeq : // fall through 204 case Bytecodes::_if_icmpne : // fall through 205 case Bytecodes::_if_icmplt : // fall through 206 case Bytecodes::_if_icmpge : // fall through 207 case Bytecodes::_if_icmpgt : // fall through 208 case Bytecodes::_if_icmple : // fall through 209 case Bytecodes::_ifeq : // fall through 210 case Bytecodes::_ifne : // fall through 211 case Bytecodes::_iflt : // fall through 212 case Bytecodes::_ifge : // fall through 213 case Bytecodes::_ifgt : // fall through 214 case Bytecodes::_ifle : // fall through 215 case Bytecodes::_ifnonnull : // fall through 216 case Bytecodes::_ifnull : // fall through 217 case Bytecodes::_jsr : { 218 int old_ofs = _s_old->bytecode().get_offset_s2(c_old); 219 int new_ofs = _s_new->bytecode().get_offset_s2(c_new); 220 if (_switchable_test) { 221 int old_dest = _s_old->bci() + old_ofs; 222 int new_dest = _s_new->bci() + new_ofs; 223 if (old_ofs < 0 && new_ofs < 0) { 224 if (! _bci_map->old_and_new_locations_same(old_dest, new_dest)) 225 return false; 226 } else if (old_ofs > 0 && new_ofs > 0) { 227 _fwd_jmps->append(old_dest); 228 _fwd_jmps->append(new_dest); 229 } else { 230 return false; 231 } 232 } else { 233 if (old_ofs != new_ofs) 234 return false; 235 } 236 break; 237 } 238 239 case Bytecodes::_iinc : 240 if (_s_old->is_wide() != _s_new->is_wide()) 241 return false; 242 if (! _s_old->is_wide()) { 243 // We could use get_index_u1 and get_constant_u1, but it's simpler to grab both bytes at once: 244 if (Bytes::get_Java_u2(_s_old->bcp() + 1) != Bytes::get_Java_u2(_s_new->bcp() + 1)) 245 return false; 246 } else { 247 // We could use get_index_u2 and get_constant_u2, but it's simpler to grab all four bytes at once: 248 if (Bytes::get_Java_u4(_s_old->bcp() + 1) != Bytes::get_Java_u4(_s_new->bcp() + 1)) 249 return false; 250 } 251 break; 252 253 case Bytecodes::_goto_w : // fall through 254 case Bytecodes::_jsr_w : { 255 int old_ofs = _s_old->bytecode().get_offset_s4(c_old); 256 int new_ofs = _s_new->bytecode().get_offset_s4(c_new); 257 if (_switchable_test) { 258 int old_dest = _s_old->bci() + old_ofs; 259 int new_dest = _s_new->bci() + new_ofs; 260 if (old_ofs < 0 && new_ofs < 0) { 261 if (! _bci_map->old_and_new_locations_same(old_dest, new_dest)) 262 return false; 263 } else if (old_ofs > 0 && new_ofs > 0) { 264 _fwd_jmps->append(old_dest); 265 _fwd_jmps->append(new_dest); 266 } else { 267 return false; 268 } 269 } else { 270 if (old_ofs != new_ofs) 271 return false; 272 } 273 break; 274 } 275 276 case Bytecodes::_lookupswitch : // fall through 277 case Bytecodes::_tableswitch : { 278 if (_switchable_test) { 279 address aligned_bcp_old = (address) round_to((intptr_t)_s_old->bcp() + 1, jintSize); 280 address aligned_bcp_new = (address) round_to((intptr_t)_s_new->bcp() + 1, jintSize); 281 int default_old = (int) Bytes::get_Java_u4(aligned_bcp_old); 282 int default_new = (int) Bytes::get_Java_u4(aligned_bcp_new); 283 _fwd_jmps->append(_s_old->bci() + default_old); 284 _fwd_jmps->append(_s_new->bci() + default_new); 285 if (c_old == Bytecodes::_lookupswitch) { 286 int npairs_old = (int) Bytes::get_Java_u4(aligned_bcp_old + jintSize); 287 int npairs_new = (int) Bytes::get_Java_u4(aligned_bcp_new + jintSize); 288 if (npairs_old != npairs_new) 289 return false; 290 for (int i = 0; i < npairs_old; i++) { 291 int match_old = (int) Bytes::get_Java_u4(aligned_bcp_old + (2+2*i)*jintSize); 292 int match_new = (int) Bytes::get_Java_u4(aligned_bcp_new + (2+2*i)*jintSize); 293 if (match_old != match_new) 294 return false; 295 int ofs_old = (int) Bytes::get_Java_u4(aligned_bcp_old + (2+2*i+1)*jintSize); 296 int ofs_new = (int) Bytes::get_Java_u4(aligned_bcp_new + (2+2*i+1)*jintSize); 297 _fwd_jmps->append(_s_old->bci() + ofs_old); 298 _fwd_jmps->append(_s_new->bci() + ofs_new); 299 } 300 } else if (c_old == Bytecodes::_tableswitch) { 301 int lo_old = (int) Bytes::get_Java_u4(aligned_bcp_old + jintSize); 302 int lo_new = (int) Bytes::get_Java_u4(aligned_bcp_new + jintSize); 303 if (lo_old != lo_new) 304 return false; 305 int hi_old = (int) Bytes::get_Java_u4(aligned_bcp_old + 2*jintSize); 306 int hi_new = (int) Bytes::get_Java_u4(aligned_bcp_new + 2*jintSize); 307 if (hi_old != hi_new) 308 return false; 309 for (int i = 0; i < hi_old - lo_old + 1; i++) { 310 int ofs_old = (int) Bytes::get_Java_u4(aligned_bcp_old + (3+i)*jintSize); 311 int ofs_new = (int) Bytes::get_Java_u4(aligned_bcp_new + (3+i)*jintSize); 312 _fwd_jmps->append(_s_old->bci() + ofs_old); 313 _fwd_jmps->append(_s_new->bci() + ofs_new); 314 } 315 } 316 } else { // !_switchable_test, can use fast rough compare 317 int len_old = _s_old->instruction_size(); 318 int len_new = _s_new->instruction_size(); 319 if (len_old != len_new) 320 return false; 321 if (memcmp(_s_old->bcp(), _s_new->bcp(), len_old) != 0) 322 return false; 323 } 324 break; 325 } 326 } 327 328 return true; 329 } 330 331 bool MethodComparator::pool_constants_same(int cpi_old, int cpi_new) { 332 constantTag tag_old = _old_cp->tag_at(cpi_old); 333 constantTag tag_new = _new_cp->tag_at(cpi_new); 334 if (tag_old.is_int() || tag_old.is_float()) { 335 if (tag_old.value() != tag_new.value()) 336 return false; 337 if (tag_old.is_int()) { 338 if (_old_cp->int_at(cpi_old) != _new_cp->int_at(cpi_new)) 339 return false; 340 } else { 341 // Use jint_cast to compare the bits rather than numerical values. 342 // This makes a difference for NaN constants. 343 if (jint_cast(_old_cp->float_at(cpi_old)) != jint_cast(_new_cp->float_at(cpi_new))) 344 return false; 345 } 346 } else if (tag_old.is_string() && tag_new.is_string()) { 347 if (strcmp(_old_cp->string_at_noresolve(cpi_old), 348 _new_cp->string_at_noresolve(cpi_new)) != 0) 349 return false; 350 if (_old_cp->is_pseudo_string_at(cpi_old) || _new_cp->is_pseudo_string_at(cpi_new)) 351 return (_old_cp->is_pseudo_string_at(cpi_old) == _new_cp->is_pseudo_string_at(cpi_new)); 352 } else if (tag_old.is_klass() || tag_old.is_unresolved_klass()) { 353 // tag_old should be klass - 4881222 354 if (! (tag_new.is_unresolved_klass() || tag_new.is_klass())) 355 return false; 356 if (_old_cp->klass_at_noresolve(cpi_old) != 357 _new_cp->klass_at_noresolve(cpi_new)) 358 return false; 359 } else if (tag_old.is_method_type() && tag_new.is_method_type()) { 360 int mti_old = _old_cp->method_type_index_at(cpi_old); 361 int mti_new = _new_cp->method_type_index_at(cpi_new); 362 if ((_old_cp->symbol_at(mti_old) != _new_cp->symbol_at(mti_new))) 363 return false; 364 } else if (tag_old.is_method_handle() && tag_new.is_method_handle()) { 365 if (_old_cp->method_handle_ref_kind_at(cpi_old) != 366 _new_cp->method_handle_ref_kind_at(cpi_new)) 367 return false; 368 int mhi_old = _old_cp->method_handle_index_at(cpi_old); 369 int mhi_new = _new_cp->method_handle_index_at(cpi_new); 370 if ((_old_cp->uncached_klass_ref_at_noresolve(mhi_old) != _new_cp->uncached_klass_ref_at_noresolve(mhi_new)) || 371 (_old_cp->uncached_name_ref_at(mhi_old) != _new_cp->uncached_name_ref_at(mhi_new)) || 372 (_old_cp->uncached_signature_ref_at(mhi_old) != _new_cp->uncached_signature_ref_at(mhi_new))) 373 return false; 374 } else { 375 return false; // unknown tag 376 } 377 return true; 378 } 379 380 381 int MethodComparator::check_stack_and_locals_size(Method* old_method, Method* new_method) { 382 if (old_method->max_stack() != new_method->max_stack()) { 383 return 1; 384 } else if (old_method->max_locals() != new_method->max_locals()) { 385 return 2; 386 } else if (old_method->size_of_parameters() != new_method->size_of_parameters()) { 387 return 3; 388 } else return 0; 389 }