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