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