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