1 /* 2 * Copyright (c) 1998, 2014, 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 "interpreter/bytecodes.hpp" 27 #include "interpreter/interpreter.hpp" 28 #include "interpreter/rewriter.hpp" 29 #include "memory/metaspaceShared.hpp" 30 #include "memory/gcLocker.hpp" 31 #include "memory/resourceArea.hpp" 32 #include "oops/generateOopMap.hpp" 33 #include "prims/methodHandles.hpp" 34 35 // Computes a CPC map (new_index -> original_index) for constant pool entries 36 // that are referred to by the interpreter at runtime via the constant pool cache. 37 // Also computes a CP map (original_index -> new_index). 38 // Marks entries in CP which require additional processing. 39 void Rewriter::compute_index_maps() { 40 const int length = _pool->length(); 41 init_maps(length); 42 bool saw_mh_symbol = false; 43 for (int i = 0; i < length; i++) { 44 int tag = _pool->tag_at(i).value(); 45 switch (tag) { 46 case JVM_CONSTANT_InterfaceMethodref: 47 case JVM_CONSTANT_Fieldref : // fall through 48 case JVM_CONSTANT_Methodref : // fall through 49 add_cp_cache_entry(i); 50 break; 51 case JVM_CONSTANT_String: 52 case JVM_CONSTANT_MethodHandle : // fall through 53 case JVM_CONSTANT_MethodType : // fall through 54 add_resolved_references_entry(i); 55 break; 56 case JVM_CONSTANT_Utf8: 57 if (_pool->symbol_at(i) == vmSymbols::java_lang_invoke_MethodHandle()) 58 saw_mh_symbol = true; 59 break; 60 } 61 } 62 63 // Record limits of resolved reference map for constant pool cache indices 64 record_map_limits(); 65 66 guarantee((int)_cp_cache_map.length()-1 <= (int)((u2)-1), 67 "all cp cache indexes fit in a u2"); 68 69 if (saw_mh_symbol) 70 _method_handle_invokers.initialize(length, (int)0); 71 } 72 73 // Unrewrite the bytecodes if an error occurs. 74 void Rewriter::restore_bytecodes() { 75 int len = _methods->length(); 76 bool invokespecial_error = false; 77 78 for (int i = len-1; i >= 0; i--) { 79 Method* method = _methods->at(i); 80 scan_method(method, true, &invokespecial_error); 81 assert(!invokespecial_error, "reversing should not get an invokespecial error"); 82 } 83 } 84 85 // Creates a constant pool cache given a CPC map 86 void Rewriter::make_constant_pool_cache(TRAPS) { 87 ClassLoaderData* loader_data = _pool->pool_holder()->class_loader_data(); 88 ConstantPoolCache* cache = 89 ConstantPoolCache::allocate(loader_data, _cp_cache_map, 90 _invokedynamic_cp_cache_map, 91 _invokedynamic_references_map, CHECK); 92 93 // initialize object cache in constant pool 94 _pool->initialize_resolved_references(loader_data, _resolved_references_map, 95 _resolved_reference_limit, 96 CHECK); 97 _pool->set_cache(cache); 98 cache->set_constant_pool(_pool()); 99 } 100 101 102 103 // The new finalization semantics says that registration of 104 // finalizable objects must be performed on successful return from the 105 // Object.<init> constructor. We could implement this trivially if 106 // <init> were never rewritten but since JVMTI allows this to occur, a 107 // more complicated solution is required. A special return bytecode 108 // is used only by Object.<init> to signal the finalization 109 // registration point. Additionally local 0 must be preserved so it's 110 // available to pass to the registration function. For simplicty we 111 // require that local 0 is never overwritten so it's available as an 112 // argument for registration. 113 114 void Rewriter::rewrite_Object_init(methodHandle method, TRAPS) { 115 RawBytecodeStream bcs(method); 116 while (!bcs.is_last_bytecode()) { 117 Bytecodes::Code opcode = bcs.raw_next(); 118 switch (opcode) { 119 case Bytecodes::_return: *bcs.bcp() = Bytecodes::_return_register_finalizer; break; 120 121 case Bytecodes::_istore: 122 case Bytecodes::_lstore: 123 case Bytecodes::_fstore: 124 case Bytecodes::_dstore: 125 case Bytecodes::_astore: 126 if (bcs.get_index() != 0) continue; 127 128 // fall through 129 case Bytecodes::_istore_0: 130 case Bytecodes::_lstore_0: 131 case Bytecodes::_fstore_0: 132 case Bytecodes::_dstore_0: 133 case Bytecodes::_astore_0: 134 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), 135 "can't overwrite local 0 in Object.<init>"); 136 break; 137 } 138 } 139 } 140 141 142 // Rewrite a classfile-order CP index into a native-order CPC index. 143 void Rewriter::rewrite_member_reference(address bcp, int offset, bool reverse) { 144 address p = bcp + offset; 145 if (!reverse) { 146 assert(DumpSharedSpaces || !MetaspaceShared::is_in_shared_space(bcp), 147 "rewirting to _fast_xxxx for archived methods should only happen at dump time"); 148 int cp_index = Bytes::get_Java_u2(p); 149 int cache_index = cp_entry_to_cp_cache(cp_index); 150 Bytes::put_native_u2(p, cache_index); 151 if (!_method_handle_invokers.is_empty()) 152 maybe_rewrite_invokehandle(p - 1, cp_index, cache_index, reverse); 153 } else { 154 int cache_index = Bytes::get_native_u2(p); 155 int pool_index = cp_cache_entry_pool_index(cache_index); 156 Bytes::put_Java_u2(p, pool_index); 157 if (!_method_handle_invokers.is_empty()) 158 maybe_rewrite_invokehandle(p - 1, pool_index, cache_index, reverse); 159 } 160 } 161 162 // If the constant pool entry for invokespecial is InterfaceMethodref, 163 // we need to add a separate cpCache entry for its resolution, because it is 164 // different than the resolution for invokeinterface with InterfaceMethodref. 165 // These cannot share cpCache entries. It's unclear if all invokespecial to 166 // InterfaceMethodrefs would resolve to the same thing so a new cpCache entry 167 // is created for each one. This was added with lambda. 168 void Rewriter::rewrite_invokespecial(address bcp, int offset, bool reverse, bool* invokespecial_error) { 169 address p = bcp + offset; 170 if (!reverse) { 171 assert(DumpSharedSpaces || !MetaspaceShared::is_in_shared_space(bcp), 172 "rewirting to _fast_invokevfinal for archived methods should only happen at dump time"); 173 int cp_index = Bytes::get_Java_u2(p); 174 if (_pool->tag_at(cp_index).is_interface_method()) { 175 int cache_index = add_invokespecial_cp_cache_entry(cp_index); 176 if (cache_index != (int)(jushort) cache_index) { 177 *invokespecial_error = true; 178 } 179 Bytes::put_native_u2(p, cache_index); 180 } else { 181 rewrite_member_reference(bcp, offset, reverse); 182 } 183 } else { 184 rewrite_member_reference(bcp, offset, reverse); 185 } 186 } 187 188 189 // Adjust the invocation bytecode for a signature-polymorphic method (MethodHandle.invoke, etc.) 190 void Rewriter::maybe_rewrite_invokehandle(address opc, int cp_index, int cache_index, bool reverse) { 191 if (!reverse) { 192 if ((*opc) == (u1)Bytecodes::_invokevirtual || 193 // allow invokespecial as an alias, although it would be very odd: 194 (*opc) == (u1)Bytecodes::_invokespecial) { 195 assert(_pool->tag_at(cp_index).is_method(), "wrong index"); 196 // Determine whether this is a signature-polymorphic method. 197 if (cp_index >= _method_handle_invokers.length()) return; 198 int status = _method_handle_invokers[cp_index]; 199 assert(status >= -1 && status <= 1, "oob tri-state"); 200 if (status == 0) { 201 if (_pool->klass_ref_at_noresolve(cp_index) == vmSymbols::java_lang_invoke_MethodHandle() && 202 MethodHandles::is_signature_polymorphic_name(SystemDictionary::MethodHandle_klass(), 203 _pool->name_ref_at(cp_index))) { 204 // we may need a resolved_refs entry for the appendix 205 add_invokedynamic_resolved_references_entries(cp_index, cache_index); 206 status = +1; 207 } else { 208 status = -1; 209 } 210 _method_handle_invokers[cp_index] = status; 211 } 212 // We use a special internal bytecode for such methods (if non-static). 213 // The basic reason for this is that such methods need an extra "appendix" argument 214 // to transmit the call site's intended call type. 215 if (status > 0) { 216 (*opc) = (u1)Bytecodes::_invokehandle; 217 } 218 } 219 } else { 220 // Do not need to look at cp_index. 221 if ((*opc) == (u1)Bytecodes::_invokehandle) { 222 (*opc) = (u1)Bytecodes::_invokevirtual; 223 // Ignore corner case of original _invokespecial instruction. 224 // This is safe because (a) the signature polymorphic method was final, and 225 // (b) the implementation of MethodHandle will not call invokespecial on it. 226 } 227 } 228 } 229 230 231 void Rewriter::rewrite_invokedynamic(address bcp, int offset, bool reverse) { 232 address p = bcp + offset; 233 assert(p[-1] == Bytecodes::_invokedynamic, "not invokedynamic bytecode"); 234 if (!reverse) { 235 int cp_index = Bytes::get_Java_u2(p); 236 int cache_index = add_invokedynamic_cp_cache_entry(cp_index); 237 int resolved_index = add_invokedynamic_resolved_references_entries(cp_index, cache_index); 238 // Replace the trailing four bytes with a CPC index for the dynamic 239 // call site. Unlike other CPC entries, there is one per bytecode, 240 // not just one per distinct CP entry. In other words, the 241 // CPC-to-CP relation is many-to-one for invokedynamic entries. 242 // This means we must use a larger index size than u2 to address 243 // all these entries. That is the main reason invokedynamic 244 // must have a five-byte instruction format. (Of course, other JVM 245 // implementations can use the bytes for other purposes.) 246 // Note: We use native_u4 format exclusively for 4-byte indexes. 247 Bytes::put_native_u4(p, ConstantPool::encode_invokedynamic_index(cache_index)); 248 // add the bcp in case we need to patch this bytecode if we also find a 249 // invokespecial/InterfaceMethodref in the bytecode stream 250 _patch_invokedynamic_bcps->push(p); 251 _patch_invokedynamic_refs->push(resolved_index); 252 } else { 253 int cache_index = ConstantPool::decode_invokedynamic_index( 254 Bytes::get_native_u4(p)); 255 // We will reverse the bytecode rewriting _after_ adjusting them. 256 // Adjust the cache index by offset to the invokedynamic entries in the 257 // cpCache plus the delta if the invokedynamic bytecodes were adjusted. 258 int adjustment = cp_cache_delta() + _first_iteration_cp_cache_limit; 259 int cp_index = invokedynamic_cp_cache_entry_pool_index(cache_index - adjustment); 260 assert(_pool->tag_at(cp_index).is_invoke_dynamic(), "wrong index"); 261 // zero out 4 bytes 262 Bytes::put_Java_u4(p, 0); 263 Bytes::put_Java_u2(p, cp_index); 264 } 265 } 266 267 void Rewriter::patch_invokedynamic_bytecodes() { 268 // If the end of the cp_cache is the same as after initializing with the 269 // cpool, nothing needs to be done. Invokedynamic bytecodes are at the 270 // correct offsets. ie. no invokespecials added 271 int delta = cp_cache_delta(); 272 if (delta > 0) { 273 int length = _patch_invokedynamic_bcps->length(); 274 assert(length == _patch_invokedynamic_refs->length(), 275 "lengths should match"); 276 for (int i = 0; i < length; i++) { 277 address p = _patch_invokedynamic_bcps->at(i); 278 int cache_index = ConstantPool::decode_invokedynamic_index( 279 Bytes::get_native_u4(p)); 280 Bytes::put_native_u4(p, ConstantPool::encode_invokedynamic_index(cache_index + delta)); 281 282 // invokedynamic resolved references map also points to cp cache and must 283 // add delta to each. 284 int resolved_index = _patch_invokedynamic_refs->at(i); 285 for (int entry = 0; entry < ConstantPoolCacheEntry::_indy_resolved_references_entries; entry++) { 286 assert(_invokedynamic_references_map[resolved_index+entry] == cache_index, 287 "should be the same index"); 288 _invokedynamic_references_map.at_put(resolved_index+entry, 289 cache_index + delta); 290 } 291 } 292 } 293 } 294 295 296 // Rewrite some ldc bytecodes to _fast_aldc 297 void Rewriter::maybe_rewrite_ldc(address bcp, int offset, bool is_wide, 298 bool reverse) { 299 if (!reverse) { 300 assert((*bcp) == (is_wide ? Bytecodes::_ldc_w : Bytecodes::_ldc), "not ldc bytecode"); 301 assert(DumpSharedSpaces || !MetaspaceShared::is_in_shared_space(bcp), 302 "rewirting to _fast_aldc or _fast_aldc_w for archived methods should only happen at dump time"); 303 address p = bcp + offset; 304 int cp_index = is_wide ? Bytes::get_Java_u2(p) : (u1)(*p); 305 constantTag tag = _pool->tag_at(cp_index).value(); 306 if (tag.is_method_handle() || tag.is_method_type() || tag.is_string()) { 307 int ref_index = cp_entry_to_resolved_references(cp_index); 308 if (is_wide) { 309 (*bcp) = Bytecodes::_fast_aldc_w; 310 assert(ref_index == (u2)ref_index, "index overflow"); 311 Bytes::put_native_u2(p, ref_index); 312 } else { 313 (*bcp) = Bytecodes::_fast_aldc; 314 assert(ref_index == (u1)ref_index, "index overflow"); 315 (*p) = (u1)ref_index; 316 } 317 } 318 } else { 319 Bytecodes::Code rewritten_bc = 320 (is_wide ? Bytecodes::_fast_aldc_w : Bytecodes::_fast_aldc); 321 if ((*bcp) == rewritten_bc) { 322 address p = bcp + offset; 323 int ref_index = is_wide ? Bytes::get_native_u2(p) : (u1)(*p); 324 int pool_index = resolved_references_entry_to_pool_index(ref_index); 325 if (is_wide) { 326 (*bcp) = Bytecodes::_ldc_w; 327 assert(pool_index == (u2)pool_index, "index overflow"); 328 Bytes::put_Java_u2(p, pool_index); 329 } else { 330 (*bcp) = Bytecodes::_ldc; 331 assert(pool_index == (u1)pool_index, "index overflow"); 332 (*p) = (u1)pool_index; 333 } 334 } 335 } 336 } 337 338 339 // Rewrites a method given the index_map information 340 void Rewriter::scan_method(Method* method, bool reverse, bool* invokespecial_error) { 341 342 int nof_jsrs = 0; 343 bool has_monitor_bytecodes = false; 344 345 { 346 // We cannot tolerate a GC in this block, because we've 347 // cached the bytecodes in 'code_base'. If the Method* 348 // moves, the bytecodes will also move. 349 No_Safepoint_Verifier nsv; 350 Bytecodes::Code c; 351 352 // Bytecodes and their length 353 const address code_base = method->code_base(); 354 const int code_length = method->code_size(); 355 356 int bc_length; 357 for (int bci = 0; bci < code_length; bci += bc_length) { 358 address bcp = code_base + bci; 359 int prefix_length = 0; 360 c = (Bytecodes::Code)(*bcp); 361 362 // Since we have the code, see if we can get the length 363 // directly. Some more complicated bytecodes will report 364 // a length of zero, meaning we need to make another method 365 // call to calculate the length. 366 bc_length = Bytecodes::length_for(c); 367 if (bc_length == 0) { 368 bc_length = Bytecodes::length_at(method, bcp); 369 370 // length_at will put us at the bytecode after the one modified 371 // by 'wide'. We don't currently examine any of the bytecodes 372 // modified by wide, but in case we do in the future... 373 if (c == Bytecodes::_wide) { 374 prefix_length = 1; 375 c = (Bytecodes::Code)bcp[1]; 376 } 377 } 378 379 assert(bc_length != 0, "impossible bytecode length"); 380 381 switch (c) { 382 case Bytecodes::_lookupswitch : { 383 #ifndef CC_INTERP 384 assert(DumpSharedSpaces || !MetaspaceShared::is_in_shared_space(bcp), 385 "rewirting to _fast_xxxswitch for archived methods should only happen at dump time"); 386 Bytecode_lookupswitch bc(method, bcp); 387 (*bcp) = ( 388 bc.number_of_pairs() < BinarySwitchThreshold 389 ? Bytecodes::_fast_linearswitch 390 : Bytecodes::_fast_binaryswitch 391 ); 392 #endif 393 break; 394 } 395 case Bytecodes::_fast_linearswitch: 396 case Bytecodes::_fast_binaryswitch: { 397 #ifndef CC_INTERP 398 (*bcp) = Bytecodes::_lookupswitch; 399 #endif 400 break; 401 } 402 403 case Bytecodes::_invokespecial : { 404 rewrite_invokespecial(bcp, prefix_length+1, reverse, invokespecial_error); 405 break; 406 } 407 408 case Bytecodes::_getstatic : // fall through 409 case Bytecodes::_putstatic : // fall through 410 case Bytecodes::_getfield : // fall through 411 case Bytecodes::_putfield : // fall through 412 case Bytecodes::_invokevirtual : // fall through 413 assert(DumpSharedSpaces || !MetaspaceShared::is_in_shared_space(bcp), 414 "rewirting to _fast_getXXX/putXXX or _fast_invokeXXX for archived methods should" 415 " only happen at dump time"); 416 case Bytecodes::_invokestatic : 417 case Bytecodes::_invokeinterface: 418 case Bytecodes::_invokehandle : // if reverse=true 419 rewrite_member_reference(bcp, prefix_length+1, reverse); 420 break; 421 case Bytecodes::_invokedynamic: 422 assert(DumpSharedSpaces || !MetaspaceShared::is_in_shared_space(bcp), 423 "rewirting _invoke_dynamic for archived methods should only happen at dump time"); 424 rewrite_invokedynamic(bcp, prefix_length+1, reverse); 425 break; 426 case Bytecodes::_ldc: 427 case Bytecodes::_fast_aldc: // if reverse=true 428 maybe_rewrite_ldc(bcp, prefix_length+1, false, reverse); 429 break; 430 case Bytecodes::_ldc_w: 431 case Bytecodes::_fast_aldc_w: // if reverse=true 432 maybe_rewrite_ldc(bcp, prefix_length+1, true, reverse); 433 break; 434 case Bytecodes::_jsr : // fall through 435 case Bytecodes::_jsr_w : nof_jsrs++; break; 436 case Bytecodes::_monitorenter : // fall through 437 case Bytecodes::_monitorexit : has_monitor_bytecodes = true; break; 438 } 439 } 440 } 441 442 // Update access flags 443 if (has_monitor_bytecodes) { 444 method->set_has_monitor_bytecodes(); 445 } 446 447 // The present of a jsr bytecode implies that the method might potentially 448 // have to be rewritten, so we run the oopMapGenerator on the method 449 if (nof_jsrs > 0) { 450 method->set_has_jsrs(); 451 // Second pass will revisit this method. 452 assert(method->has_jsrs(), "didn't we just set this?"); 453 } 454 } 455 456 // After constant pool is created, revisit methods containing jsrs. 457 methodHandle Rewriter::rewrite_jsrs(methodHandle method, TRAPS) { 458 ResourceMark rm(THREAD); 459 ResolveOopMapConflicts romc(method); 460 methodHandle original_method = method; 461 method = romc.do_potential_rewrite(CHECK_(methodHandle())); 462 // Update monitor matching info. 463 if (romc.monitor_safe()) { 464 method->set_guaranteed_monitor_matching(); 465 } 466 467 return method; 468 } 469 470 void Rewriter::rewrite_bytecodes(TRAPS) { 471 assert(_pool->cache() == NULL, "constant pool cache must not be set yet"); 472 473 // determine index maps for Method* rewriting 474 compute_index_maps(); 475 476 if (RegisterFinalizersAtInit && _klass->name() == vmSymbols::java_lang_Object()) { 477 bool did_rewrite = false; 478 int i = _methods->length(); 479 while (i-- > 0) { 480 Method* method = _methods->at(i); 481 if (method->intrinsic_id() == vmIntrinsics::_Object_init) { 482 // rewrite the return bytecodes of Object.<init> to register the 483 // object for finalization if needed. 484 methodHandle m(THREAD, method); 485 rewrite_Object_init(m, CHECK); 486 did_rewrite = true; 487 break; 488 } 489 } 490 assert(did_rewrite, "must find Object::<init> to rewrite it"); 491 } 492 493 // rewrite methods, in two passes 494 int len = _methods->length(); 495 bool invokespecial_error = false; 496 497 for (int i = len-1; i >= 0; i--) { 498 Method* method = _methods->at(i); 499 scan_method(method, false, &invokespecial_error); 500 if (invokespecial_error) { 501 // If you get an error here, there is no reversing bytecodes 502 // This exception is stored for this class and no further attempt is 503 // made at verifying or rewriting. 504 THROW_MSG(vmSymbols::java_lang_InternalError(), 505 "This classfile overflows invokespecial for interfaces " 506 "and cannot be loaded"); 507 return; 508 } 509 } 510 511 // May have to fix invokedynamic bytecodes if invokestatic/InterfaceMethodref 512 // entries had to be added. 513 patch_invokedynamic_bytecodes(); 514 } 515 516 void Rewriter::rewrite(instanceKlassHandle klass, TRAPS) { 517 ResourceMark rm(THREAD); 518 Rewriter rw(klass, klass->constants(), klass->methods(), CHECK); 519 // (That's all, folks.) 520 } 521 522 523 Rewriter::Rewriter(instanceKlassHandle klass, constantPoolHandle cpool, Array<Method*>* methods, TRAPS) 524 : _klass(klass), 525 _pool(cpool), 526 _methods(methods) 527 { 528 529 // Rewrite bytecodes - exception here exits. 530 rewrite_bytecodes(CHECK); 531 532 // Stress restoring bytecodes 533 if (StressRewriter) { 534 restore_bytecodes(); 535 rewrite_bytecodes(CHECK); 536 } 537 538 // allocate constant pool cache, now that we've seen all the bytecodes 539 make_constant_pool_cache(THREAD); 540 541 // Restore bytecodes to their unrewritten state if there are exceptions 542 // rewriting bytecodes or allocating the cpCache 543 if (HAS_PENDING_EXCEPTION) { 544 restore_bytecodes(); 545 return; 546 } 547 548 // Relocate after everything, but still do this under the is_rewritten flag, 549 // so methods with jsrs in custom class lists in aren't attempted to be 550 // rewritten in the RO section of the shared archive. 551 // Relocated bytecodes don't have to be restored, only the cp cache entries 552 int len = _methods->length(); 553 for (int i = len-1; i >= 0; i--) { 554 methodHandle m(THREAD, _methods->at(i)); 555 556 if (m->has_jsrs()) { 557 m = rewrite_jsrs(m, THREAD); 558 // Restore bytecodes to their unrewritten state if there are exceptions 559 // relocating bytecodes. If some are relocated, that is ok because that 560 // doesn't affect constant pool to cpCache rewriting. 561 if (HAS_PENDING_EXCEPTION) { 562 restore_bytecodes(); 563 return; 564 } 565 // Method might have gotten rewritten. 566 methods->at_put(i, m()); 567 } 568 } 569 }