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