methodComparator.cpp revision 1472:c18cbe5936b8
1/* 2 * Copyright (c) 2000, 2009, 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 "incls/_precompiled.incl" 26# include "incls/_methodComparator.cpp.incl" 27 28BytecodeStream *MethodComparator::_s_old; 29BytecodeStream *MethodComparator::_s_new; 30constantPoolOop MethodComparator::_old_cp; 31constantPoolOop MethodComparator::_new_cp; 32BciMap *MethodComparator::_bci_map; 33bool MethodComparator::_switchable_test; 34GrowableArray<int> *MethodComparator::_fwd_jmps; 35 36bool MethodComparator::methods_EMCP(methodOop old_method, methodOop new_method) { 37 if (old_method->code_size() != new_method->code_size()) 38 return false; 39 if (check_stack_and_locals_size(old_method, new_method) != 0) { 40 // RC_TRACE macro has an embedded ResourceMark 41 RC_TRACE(0x00800000, ("Methods %s non-comparable with diagnosis %d", 42 old_method->name()->as_C_string(), 43 check_stack_and_locals_size(old_method, new_method))); 44 return false; 45 } 46 47 _old_cp = old_method->constants(); 48 _new_cp = new_method->constants(); 49 BytecodeStream s_old(old_method); 50 BytecodeStream s_new(new_method); 51 _s_old = &s_old; 52 _s_new = &s_new; 53 _switchable_test = false; 54 Bytecodes::Code c_old, c_new; 55 56 while ((c_old = s_old.next()) >= 0) { 57 if ((c_new = s_new.next()) < 0 || c_old != c_new) 58 return false; 59 60 if (! args_same(c_old, c_new)) 61 return false; 62 } 63 return true; 64} 65 66 67bool MethodComparator::methods_switchable(methodOop old_method, methodOop new_method, 68 BciMap &bci_map) { 69 if (old_method->code_size() > new_method->code_size()) 70 // Something has definitely been deleted in the new method, compared to the old one. 71 return false; 72 73 if (! check_stack_and_locals_size(old_method, new_method)) 74 return false; 75 76 _old_cp = old_method->constants(); 77 _new_cp = new_method->constants(); 78 BytecodeStream s_old(old_method); 79 BytecodeStream s_new(new_method); 80 _s_old = &s_old; 81 _s_new = &s_new; 82 _bci_map = &bci_map; 83 _switchable_test = true; 84 GrowableArray<int> fwd_jmps(16); 85 _fwd_jmps = &fwd_jmps; 86 Bytecodes::Code c_old, c_new; 87 88 while ((c_old = s_old.next()) >= 0) { 89 if ((c_new = s_new.next()) < 0) 90 return false; 91 if (! (c_old == c_new && args_same(c_old, c_new))) { 92 int old_bci = s_old.bci(); 93 int new_st_bci = s_new.bci(); 94 bool found_match = false; 95 do { 96 c_new = s_new.next(); 97 if (c_new == c_old && args_same(c_old, c_new)) { 98 found_match = true; 99 break; 100 } 101 } while (c_new >= 0); 102 if (! found_match) 103 return false; 104 int new_end_bci = s_new.bci(); 105 bci_map.store_fragment_location(old_bci, new_st_bci, new_end_bci); 106 } 107 } 108 109 // Now we can test all forward jumps 110 for (int i = 0; i < fwd_jmps.length() / 2; i++) { 111 if (! bci_map.old_and_new_locations_same(fwd_jmps.at(i*2), fwd_jmps.at(i*2+1))) { 112 RC_TRACE(0x00800000, 113 ("Fwd jump miss: old dest = %d, calc new dest = %d, act new dest = %d", 114 fwd_jmps.at(i*2), bci_map.new_bci_for_old(fwd_jmps.at(i*2)), 115 fwd_jmps.at(i*2+1))); 116 return false; 117 } 118 } 119 120 return true; 121} 122 123 124bool MethodComparator::args_same(Bytecodes::Code c_old, Bytecodes::Code c_new) { 125 // BytecodeStream returns the correct standard Java bytecodes for various "fast" 126 // bytecode versions, so we don't have to bother about them here.. 127 switch (c_old) { 128 case Bytecodes::_new : // fall through 129 case Bytecodes::_anewarray : // fall through 130 case Bytecodes::_multianewarray : // fall through 131 case Bytecodes::_checkcast : // fall through 132 case Bytecodes::_instanceof : { 133 u2 cpi_old = _s_old->get_index_big(); 134 u2 cpi_new = _s_new->get_index_big(); 135 if ((_old_cp->klass_at_noresolve(cpi_old) != _new_cp->klass_at_noresolve(cpi_new))) 136 return false; 137 if (c_old == Bytecodes::_multianewarray && 138 *(jbyte*)(_s_old->bcp() + 3) != *(jbyte*)(_s_new->bcp() + 3)) 139 return false; 140 break; 141 } 142 143 case Bytecodes::_getstatic : // fall through 144 case Bytecodes::_putstatic : // fall through 145 case Bytecodes::_getfield : // fall through 146 case Bytecodes::_putfield : // fall through 147 case Bytecodes::_invokevirtual : // fall through 148 case Bytecodes::_invokespecial : // fall through 149 case Bytecodes::_invokestatic : // fall through 150 case Bytecodes::_invokeinterface : { 151 u2 cpci_old = _s_old->get_index_int(); 152 u2 cpci_new = _s_new->get_index_int(); 153 // Check if the names of classes, field/method names and signatures at these indexes 154 // are the same. Indices which are really into constantpool cache (rather than constant 155 // pool itself) are accepted by the constantpool query routines below. 156 if ((_old_cp->klass_ref_at_noresolve(cpci_old) != _new_cp->klass_ref_at_noresolve(cpci_new)) || 157 (_old_cp->name_ref_at(cpci_old) != _new_cp->name_ref_at(cpci_new)) || 158 (_old_cp->signature_ref_at(cpci_old) != _new_cp->signature_ref_at(cpci_new))) 159 return false; 160 break; 161 } 162 163 case Bytecodes::_ldc : // fall through 164 case Bytecodes::_ldc_w : { 165 u2 cpi_old, cpi_new; 166 if (c_old == Bytecodes::_ldc) { 167 cpi_old = _s_old->bcp()[1]; 168 cpi_new = _s_new->bcp()[1]; 169 } else { 170 cpi_old = _s_old->get_index_big(); 171 cpi_new = _s_new->get_index_big(); 172 } 173 constantTag tag_old = _old_cp->tag_at(cpi_old); 174 constantTag tag_new = _new_cp->tag_at(cpi_new); 175 if (tag_old.is_int() || tag_old.is_float()) { 176 if (tag_old.value() != tag_new.value()) 177 return false; 178 if (tag_old.is_int()) { 179 if (_old_cp->int_at(cpi_old) != _new_cp->int_at(cpi_new)) 180 return false; 181 } else { 182 if (_old_cp->float_at(cpi_old) != _new_cp->float_at(cpi_new)) 183 return false; 184 } 185 } else if (tag_old.is_string() || tag_old.is_unresolved_string()) { 186 if (! (tag_new.is_unresolved_string() || tag_new.is_string())) 187 return false; 188 if (strcmp(_old_cp->string_at_noresolve(cpi_old), 189 _new_cp->string_at_noresolve(cpi_new)) != 0) 190 return false; 191 } else { // tag_old should be klass - 4881222 192 if (! (tag_new.is_unresolved_klass() || tag_new.is_klass())) 193 return false; 194 if (_old_cp->klass_at_noresolve(cpi_old) != 195 _new_cp->klass_at_noresolve(cpi_new)) 196 return false; 197 } 198 break; 199 } 200 201 case Bytecodes::_ldc2_w : { 202 u2 cpi_old = _s_old->get_index_big(); 203 u2 cpi_new = _s_new->get_index_big(); 204 constantTag tag_old = _old_cp->tag_at(cpi_old); 205 constantTag tag_new = _new_cp->tag_at(cpi_new); 206 if (tag_old.value() != tag_new.value()) 207 return false; 208 if (tag_old.is_long()) { 209 if (_old_cp->long_at(cpi_old) != _new_cp->long_at(cpi_new)) 210 return false; 211 } else { 212 if (_old_cp->double_at(cpi_old) != _new_cp->double_at(cpi_new)) 213 return false; 214 } 215 break; 216 } 217 218 case Bytecodes::_bipush : 219 if (_s_old->bcp()[1] != _s_new->bcp()[1]) 220 return false; 221 break; 222 223 case Bytecodes::_sipush : 224 if (_s_old->get_index_big() != _s_new->get_index_big()) 225 return false; 226 break; 227 228 case Bytecodes::_aload : // fall through 229 case Bytecodes::_astore : // fall through 230 case Bytecodes::_dload : // fall through 231 case Bytecodes::_dstore : // fall through 232 case Bytecodes::_fload : // fall through 233 case Bytecodes::_fstore : // fall through 234 case Bytecodes::_iload : // fall through 235 case Bytecodes::_istore : // fall through 236 case Bytecodes::_lload : // fall through 237 case Bytecodes::_lstore : // fall through 238 case Bytecodes::_ret : 239 if (_s_old->is_wide() != _s_new->is_wide()) 240 return false; 241 if (_s_old->get_index() != _s_new->get_index()) 242 return false; 243 break; 244 245 case Bytecodes::_goto : // fall through 246 case Bytecodes::_if_acmpeq : // fall through 247 case Bytecodes::_if_acmpne : // fall through 248 case Bytecodes::_if_icmpeq : // fall through 249 case Bytecodes::_if_icmpne : // fall through 250 case Bytecodes::_if_icmplt : // fall through 251 case Bytecodes::_if_icmpge : // fall through 252 case Bytecodes::_if_icmpgt : // fall through 253 case Bytecodes::_if_icmple : // fall through 254 case Bytecodes::_ifeq : // fall through 255 case Bytecodes::_ifne : // fall through 256 case Bytecodes::_iflt : // fall through 257 case Bytecodes::_ifge : // fall through 258 case Bytecodes::_ifgt : // fall through 259 case Bytecodes::_ifle : // fall through 260 case Bytecodes::_ifnonnull : // fall through 261 case Bytecodes::_ifnull : // fall through 262 case Bytecodes::_jsr : { 263 short old_ofs = (short) _s_old->get_index_big(); 264 short new_ofs = (short) _s_new->get_index_big(); 265 if (_switchable_test) { 266 int old_dest = _s_old->bci() + old_ofs; 267 int new_dest = _s_new->bci() + new_ofs; 268 if (old_ofs < 0 && new_ofs < 0) { 269 if (! _bci_map->old_and_new_locations_same(old_dest, new_dest)) 270 return false; 271 } else if (old_ofs > 0 && new_ofs > 0) { 272 _fwd_jmps->append(old_dest); 273 _fwd_jmps->append(new_dest); 274 } else { 275 return false; 276 } 277 } else { 278 if (old_ofs != new_ofs) 279 return false; 280 } 281 break; 282 } 283 284 case Bytecodes::_iinc : 285 if (_s_old->is_wide() != _s_new->is_wide()) 286 return false; 287 if (! _s_old->is_wide()) { 288 if (_s_old->get_index_big() != _s_new->get_index_big()) 289 return false; 290 } else { 291 if (Bytes::get_Java_u4(_s_old->bcp() + 1) != Bytes::get_Java_u4(_s_new->bcp() + 1)) 292 return false; 293 } 294 break; 295 296 case Bytecodes::_goto_w : // fall through 297 case Bytecodes::_jsr_w : { 298 int old_ofs = (int) Bytes::get_Java_u4(_s_old->bcp() + 1); 299 int new_ofs = (int) Bytes::get_Java_u4(_s_new->bcp() + 1); 300 if (_switchable_test) { 301 int old_dest = _s_old->bci() + old_ofs; 302 int new_dest = _s_new->bci() + new_ofs; 303 if (old_ofs < 0 && new_ofs < 0) { 304 if (! _bci_map->old_and_new_locations_same(old_dest, new_dest)) 305 return false; 306 } else if (old_ofs > 0 && new_ofs > 0) { 307 _fwd_jmps->append(old_dest); 308 _fwd_jmps->append(new_dest); 309 } else { 310 return false; 311 } 312 } else { 313 if (old_ofs != new_ofs) 314 return false; 315 } 316 break; 317 } 318 319 case Bytecodes::_lookupswitch : // fall through 320 case Bytecodes::_tableswitch : { 321 if (_switchable_test) { 322 address aligned_bcp_old = (address) round_to((intptr_t)_s_old->bcp() + 1, jintSize); 323 address aligned_bcp_new = (address) round_to((intptr_t)_s_new->bcp() + 1, jintSize); 324 int default_old = (int) Bytes::get_Java_u4(aligned_bcp_old); 325 int default_new = (int) Bytes::get_Java_u4(aligned_bcp_new); 326 _fwd_jmps->append(_s_old->bci() + default_old); 327 _fwd_jmps->append(_s_new->bci() + default_new); 328 if (c_old == Bytecodes::_lookupswitch) { 329 int npairs_old = (int) Bytes::get_Java_u4(aligned_bcp_old + jintSize); 330 int npairs_new = (int) Bytes::get_Java_u4(aligned_bcp_new + jintSize); 331 if (npairs_old != npairs_new) 332 return false; 333 for (int i = 0; i < npairs_old; i++) { 334 int match_old = (int) Bytes::get_Java_u4(aligned_bcp_old + (2+2*i)*jintSize); 335 int match_new = (int) Bytes::get_Java_u4(aligned_bcp_new + (2+2*i)*jintSize); 336 if (match_old != match_new) 337 return false; 338 int ofs_old = (int) Bytes::get_Java_u4(aligned_bcp_old + (2+2*i+1)*jintSize); 339 int ofs_new = (int) Bytes::get_Java_u4(aligned_bcp_new + (2+2*i+1)*jintSize); 340 _fwd_jmps->append(_s_old->bci() + ofs_old); 341 _fwd_jmps->append(_s_new->bci() + ofs_new); 342 } 343 } else if (c_old == Bytecodes::_tableswitch) { 344 int lo_old = (int) Bytes::get_Java_u4(aligned_bcp_old + jintSize); 345 int lo_new = (int) Bytes::get_Java_u4(aligned_bcp_new + jintSize); 346 if (lo_old != lo_new) 347 return false; 348 int hi_old = (int) Bytes::get_Java_u4(aligned_bcp_old + 2*jintSize); 349 int hi_new = (int) Bytes::get_Java_u4(aligned_bcp_new + 2*jintSize); 350 if (hi_old != hi_new) 351 return false; 352 for (int i = 0; i < hi_old - lo_old + 1; i++) { 353 int ofs_old = (int) Bytes::get_Java_u4(aligned_bcp_old + (3+i)*jintSize); 354 int ofs_new = (int) Bytes::get_Java_u4(aligned_bcp_new + (3+i)*jintSize); 355 _fwd_jmps->append(_s_old->bci() + ofs_old); 356 _fwd_jmps->append(_s_new->bci() + ofs_new); 357 } 358 } 359 } else { // !_switchable_test, can use fast rough compare 360 int len_old = _s_old->next_bcp() - _s_old->bcp(); 361 int len_new = _s_new->next_bcp() - _s_new->bcp(); 362 if (len_old != len_new) 363 return false; 364 if (memcmp(_s_old->bcp(), _s_new->bcp(), len_old) != 0) 365 return false; 366 } 367 break; 368 } 369 } 370 371 return true; 372} 373 374 375int MethodComparator::check_stack_and_locals_size(methodOop old_method, methodOop new_method) { 376 if (old_method->max_stack() != new_method->max_stack()) { 377 return 1; 378 } else if (old_method->max_locals() != new_method->max_locals()) { 379 return 2; 380 } else if (old_method->size_of_parameters() != new_method->size_of_parameters()) { 381 return 3; 382 } else return 0; 383} 384