1 /* 2 * Copyright (c) 1998, 2025, 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 "cds/aotConstantPoolResolver.hpp" 26 #include "cds/archiveBuilder.hpp" 27 #include "cds/cdsConfig.hpp" 28 #include "cds/heapShared.hpp" 29 #include "classfile/resolutionErrors.hpp" 30 #include "classfile/systemDictionary.hpp" 31 #include "classfile/systemDictionaryShared.hpp" 32 #include "classfile/vmClasses.hpp" 33 #include "code/codeCache.hpp" 34 #include "interpreter/bytecodeStream.hpp" 35 #include "interpreter/bytecodes.hpp" 36 #include "interpreter/interpreter.hpp" 37 #include "interpreter/linkResolver.hpp" 38 #include "interpreter/rewriter.hpp" 39 #include "logging/log.hpp" 40 #include "logging/logStream.hpp" 41 #include "memory/metadataFactory.hpp" 42 #include "memory/metaspaceClosure.hpp" 43 #include "memory/resourceArea.hpp" 44 #include "oops/access.inline.hpp" 45 #include "oops/compressedOops.hpp" 46 #include "oops/constantPool.inline.hpp" 47 #include "oops/cpCache.inline.hpp" 48 #include "oops/method.inline.hpp" 49 #include "oops/objArrayOop.inline.hpp" 50 #include "oops/oop.inline.hpp" 51 #include "oops/resolvedFieldEntry.hpp" 52 #include "oops/resolvedIndyEntry.hpp" 53 #include "oops/resolvedMethodEntry.hpp" 54 #include "prims/methodHandles.hpp" 55 #include "runtime/arguments.hpp" 56 #include "runtime/atomic.hpp" 57 #include "runtime/handles.inline.hpp" 58 #include "runtime/mutexLocker.hpp" 59 #include "runtime/synchronizer.hpp" 60 #include "runtime/vm_version.hpp" 61 #include "utilities/macros.hpp" 62 63 // Implementation of ConstantPoolCache 64 65 template <class T> 66 static Array<T>* initialize_resolved_entries_array(ClassLoaderData* loader_data, GrowableArray<T> entries, TRAPS) { 67 Array<T>* resolved_entries; 68 if (entries.length() != 0) { 69 resolved_entries = MetadataFactory::new_array<T>(loader_data, entries.length(), CHECK_NULL); 70 for (int i = 0; i < entries.length(); i++) { 71 resolved_entries->at_put(i, entries.at(i)); 72 } 73 return resolved_entries; 74 } 75 return nullptr; 76 } 77 78 void ConstantPoolCache::set_direct_or_vtable_call(Bytecodes::Code invoke_code, 79 int method_index, 80 const methodHandle& method, 81 int vtable_index, 82 bool sender_is_interface) { 83 bool is_vtable_call = (vtable_index >= 0); // FIXME: split this method on this boolean 84 assert(method->interpreter_entry() != nullptr, "should have been set at this point"); 85 assert(!method->is_obsolete(), "attempt to write obsolete method to cpCache"); 86 87 int byte_no = -1; 88 bool change_to_virtual = false; 89 InstanceKlass* holder = nullptr; // have to declare this outside the switch 90 ResolvedMethodEntry* method_entry = resolved_method_entry_at(method_index); 91 switch (invoke_code) { 92 case Bytecodes::_invokeinterface: 93 holder = method->method_holder(); 94 // check for private interface method invocations 95 if (vtable_index == Method::nonvirtual_vtable_index && holder->is_interface() ) { 96 assert(method->is_private(), "unexpected non-private method"); 97 assert(method->can_be_statically_bound(), "unexpected non-statically-bound method"); 98 99 method_entry->set_flags(( 1 << ResolvedMethodEntry::is_vfinal_shift) | 100 ((method->is_final_method() ? 1 : 0) << ResolvedMethodEntry::is_final_shift)); 101 method_entry->fill_in((u1)as_TosState(method->result_type()), (u2)method()->size_of_parameters()); 102 assert(method_entry->is_vfinal(), "flags must be set"); 103 method_entry->set_method(method()); 104 byte_no = 2; 105 method_entry->set_klass(holder); 106 break; 107 } 108 else { 109 // We get here from InterpreterRuntime::resolve_invoke when an invokeinterface 110 // instruction links to a non-interface method (in Object). This can happen when 111 // an interface redeclares an Object method (like CharSequence declaring toString()) 112 // or when invokeinterface is used explicitly. 113 // In that case, the method has no itable index and must be invoked as a virtual. 114 // Set a flag to keep track of this corner case. 115 assert(holder->is_interface() || holder == vmClasses::Object_klass(), "unexpected holder class"); 116 assert(method->is_public(), "Calling non-public method in Object with invokeinterface"); 117 change_to_virtual = true; 118 119 // ...and fall through as if we were handling invokevirtual: 120 } 121 case Bytecodes::_invokevirtual: 122 { 123 if (!is_vtable_call) { 124 assert(method->can_be_statically_bound(), ""); 125 method_entry->set_flags(( 1 << ResolvedMethodEntry::is_vfinal_shift) | 126 ((method->is_final_method() ? 1 : 0) << ResolvedMethodEntry::is_final_shift) | 127 ((change_to_virtual ? 1 : 0) << ResolvedMethodEntry::is_forced_virtual_shift)); 128 method_entry->fill_in((u1)as_TosState(method->result_type()), (u2)method()->size_of_parameters()); 129 assert(method_entry->is_vfinal(), "flags must be set"); 130 method_entry->set_method(method()); 131 } else { 132 assert(!method->can_be_statically_bound(), ""); 133 assert(vtable_index >= 0, "valid index"); 134 assert(!method->is_final_method(), "sanity"); 135 method_entry->set_flags((change_to_virtual ? 1 : 0) << ResolvedMethodEntry::is_forced_virtual_shift); 136 method_entry->fill_in((u1)as_TosState(method->result_type()), (u2)method()->size_of_parameters()); 137 assert(!method_entry->is_vfinal(), "flags must not be set"); 138 method_entry->set_table_index(vtable_index); 139 } 140 byte_no = 2; 141 break; 142 } 143 144 case Bytecodes::_invokespecial: 145 case Bytecodes::_invokestatic: { 146 assert(!is_vtable_call, ""); 147 // Note: Read and preserve the value of the is_vfinal flag on any 148 // invokevirtual bytecode shared with this constant pool cache entry. 149 // It is cheap and safe to consult is_vfinal() at all times. 150 // Once is_vfinal is set, it must stay that way, lest we get a dangling oop. 151 bool vfinal = method_entry->is_vfinal(); 152 method_entry->set_flags(((method->is_final_method() ? 1 : 0) << ResolvedMethodEntry::is_final_shift)); 153 assert(vfinal == method_entry->is_vfinal(), "Vfinal flag must be preserved"); 154 method_entry->fill_in((u1)as_TosState(method->result_type()), (u2)method()->size_of_parameters()); 155 method_entry->set_method(method()); 156 byte_no = 1; 157 break; 158 } 159 default: 160 ShouldNotReachHere(); 161 break; 162 } 163 164 // Note: byte_no also appears in TemplateTable::resolve. 165 if (byte_no == 1) { 166 assert(invoke_code != Bytecodes::_invokevirtual && 167 invoke_code != Bytecodes::_invokeinterface, ""); 168 bool do_resolve = true; 169 // Don't mark invokespecial to method as resolved if sender is an interface. The receiver 170 // has to be checked that it is a subclass of the current class every time this bytecode 171 // is executed. 172 if (invoke_code == Bytecodes::_invokespecial && sender_is_interface && 173 method->name() != vmSymbols::object_initializer_name()) { 174 do_resolve = false; 175 } 176 if (invoke_code == Bytecodes::_invokestatic) { 177 assert(method->method_holder()->is_initialized() || 178 method->method_holder()->is_reentrant_initialization(JavaThread::current()), 179 "invalid class initialization state for invoke_static"); 180 181 if (!VM_Version::supports_fast_class_init_checks() && method->needs_clinit_barrier()) { 182 // Don't mark invokestatic to method as resolved if the holder class has not yet completed 183 // initialization. An invokestatic must only proceed if the class is initialized, but if 184 // we resolve it before then that class initialization check is skipped. 185 // 186 // When fast class initialization checks are supported (VM_Version::supports_fast_class_init_checks() == true), 187 // template interpreter supports fast class initialization check for 188 // invokestatic which doesn't require call site re-resolution to 189 // enforce class initialization barrier. 190 do_resolve = false; 191 } 192 } 193 if (do_resolve) { 194 method_entry->set_bytecode1(invoke_code); 195 } 196 } else if (byte_no == 2) { 197 if (change_to_virtual) { 198 assert(invoke_code == Bytecodes::_invokeinterface, ""); 199 // NOTE: THIS IS A HACK - BE VERY CAREFUL!!! 200 // 201 // Workaround for the case where we encounter an invokeinterface, but we 202 // should really have an _invokevirtual since the resolved method is a 203 // virtual method in java.lang.Object. This is a corner case in the spec 204 // but is presumably legal. javac does not generate this code. 205 // 206 // We do not set bytecode_1() to _invokeinterface, because that is the 207 // bytecode # used by the interpreter to see if it is resolved. In this 208 // case, the method gets reresolved with caller for each interface call 209 // because the actual selected method may not be public. 210 // 211 // We set bytecode_2() to _invokevirtual. 212 // See also interpreterRuntime.cpp. (8/25/2000) 213 } else { 214 assert(invoke_code == Bytecodes::_invokevirtual || 215 (invoke_code == Bytecodes::_invokeinterface && 216 ((method->is_private() || 217 (method->is_final() && method->method_holder() == vmClasses::Object_klass())))), 218 "unexpected invocation mode"); 219 if (invoke_code == Bytecodes::_invokeinterface && 220 (method->is_private() || method->is_final())) { 221 // We set bytecode_1() to _invokeinterface, because that is the 222 // bytecode # used by the interpreter to see if it is resolved. 223 // We set bytecode_2() to _invokevirtual. 224 method_entry->set_bytecode1(invoke_code); 225 } 226 } 227 // set up for invokevirtual, even if linking for invokeinterface also: 228 method_entry->set_bytecode2(Bytecodes::_invokevirtual); 229 } else { 230 ShouldNotReachHere(); 231 } 232 } 233 234 void ConstantPoolCache::set_direct_call(Bytecodes::Code invoke_code, int method_index, const methodHandle& method, 235 bool sender_is_interface) { 236 int index = Method::nonvirtual_vtable_index; 237 // index < 0; FIXME: inline and customize set_direct_or_vtable_call 238 set_direct_or_vtable_call(invoke_code, method_index, method, index, sender_is_interface); 239 } 240 241 void ConstantPoolCache::set_vtable_call(Bytecodes::Code invoke_code, int method_index, const methodHandle& method, int index) { 242 // either the method is a miranda or its holder should accept the given index 243 assert(method->method_holder()->is_interface() || method->method_holder()->verify_vtable_index(index), ""); 244 // index >= 0; FIXME: inline and customize set_direct_or_vtable_call 245 set_direct_or_vtable_call(invoke_code, method_index, method, index, false); 246 } 247 248 void ConstantPoolCache::set_itable_call(Bytecodes::Code invoke_code, 249 int method_index, 250 Klass* referenced_klass, 251 const methodHandle& method, int index) { 252 assert(method->method_holder()->verify_itable_index(index), ""); 253 assert(invoke_code == Bytecodes::_invokeinterface, ""); 254 InstanceKlass* interf = method->method_holder(); 255 assert(interf->is_interface(), "must be an interface"); 256 assert(!method->is_final_method(), "interfaces do not have final methods; cannot link to one here"); 257 ResolvedMethodEntry* method_entry = resolved_method_entry_at(method_index); 258 method_entry->set_klass(static_cast<InstanceKlass*>(referenced_klass)); 259 method_entry->set_method(method()); 260 method_entry->fill_in((u1)as_TosState(method->result_type()), (u2)method()->size_of_parameters()); 261 method_entry->set_bytecode1(Bytecodes::_invokeinterface); 262 } 263 264 ResolvedMethodEntry* ConstantPoolCache::set_method_handle(int method_index, const CallInfo &call_info) { 265 // NOTE: This method entry can be the subject of data races. 266 // There are three words to update: flags, refs[appendix_index], method (in that order). 267 // Writers must store all other values before method. 268 // Readers must test the method first for non-null before reading other fields. 269 // Competing writers must acquire exclusive access via a lock. 270 // A losing writer waits on the lock until the winner writes the method and leaves 271 // the lock, so that when the losing writer returns, he can use the linked 272 // cache entry. 273 274 // Lock fields to write 275 Bytecodes::Code invoke_code = Bytecodes::_invokehandle; 276 277 JavaThread* current = JavaThread::current(); 278 objArrayHandle resolved_references(current, constant_pool()->resolved_references()); 279 // Use the resolved_references() lock for this cpCache entry. 280 // resolved_references are created for all classes with Invokedynamic, MethodHandle 281 // or MethodType constant pool cache entries. 282 assert(resolved_references() != nullptr, 283 "a resolved_references array should have been created for this class"); 284 ObjectLocker ol(resolved_references, current); 285 286 ResolvedMethodEntry* method_entry = resolved_method_entry_at(method_index); 287 if (method_entry->is_resolved(invoke_code)) { 288 return method_entry; 289 } 290 291 Method* adapter = call_info.resolved_method(); 292 const Handle appendix = call_info.resolved_appendix(); 293 const bool has_appendix = appendix.not_null(); 294 295 // Write the flags. 296 // MHs are always sig-poly and have a local signature. 297 method_entry->fill_in((u1)as_TosState(adapter->result_type()), (u2)adapter->size_of_parameters()); 298 method_entry->set_flags(((has_appendix ? 1 : 0) << ResolvedMethodEntry::has_appendix_shift ) | 299 ( 1 << ResolvedMethodEntry::has_local_signature_shift ) | 300 ( 1 << ResolvedMethodEntry::is_final_shift )); 301 302 // Method handle invokes use both a method and a resolved references index. 303 // refs[appendix_index], if not null, contains a value passed as a trailing argument to the adapter. 304 // In the general case, this could be the call site's MethodType, 305 // for use with java.lang.Invokers.checkExactType, or else a CallSite object. 306 // method_entry->method() contains the adapter method which manages the actual call. 307 // In the general case, this is a compiled LambdaForm. 308 // (The Java code is free to optimize these calls by binding other 309 // sorts of methods and appendices to call sites.) 310 // JVM-level linking is via the method, as if for invokespecial, and signatures are erased. 311 // The appendix argument (if any) is added to the signature, and is counted in the parameter_size bits. 312 // Even with the appendix, the method will never take more than 255 parameter slots. 313 // 314 // This means that given a call site like (List)mh.invoke("foo"), 315 // the method has signature '(Ljl/Object;Ljl/invoke/MethodType;)Ljl/Object;', 316 // not '(Ljava/lang/String;)Ljava/util/List;'. 317 // The fact that String and List are involved is encoded in the MethodType in refs[appendix_index]. 318 // This allows us to create fewer Methods, while keeping type safety. 319 // 320 321 // Store appendix, if any. 322 if (has_appendix) { 323 const int appendix_index = method_entry->resolved_references_index(); 324 assert(appendix_index >= 0 && appendix_index < resolved_references->length(), "oob"); 325 assert(resolved_references->obj_at(appendix_index) == nullptr, "init just once"); 326 resolved_references->obj_at_put(appendix_index, appendix()); 327 } 328 329 method_entry->set_method(adapter); // This must be the last one to set (see NOTE above)! 330 331 // The interpreter assembly code does not check byte_2, 332 // but it is used by is_resolved, method_if_resolved, etc. 333 method_entry->set_bytecode1(invoke_code); 334 335 assert(has_appendix == method_entry->has_appendix(), "proper storage of appendix flag"); 336 assert(method_entry->has_local_signature(), "proper storage of signature flag"); 337 return method_entry; 338 } 339 340 Method* ConstantPoolCache::method_if_resolved(int method_index) const { 341 // Decode the action of set_method and set_interface_call 342 ResolvedMethodEntry* method_entry = resolved_method_entry_at(method_index); 343 344 Bytecodes::Code invoke_code = (Bytecodes::Code)method_entry->bytecode1(); 345 switch (invoke_code) { 346 case Bytecodes::_invokeinterface: 347 case Bytecodes::_invokestatic: 348 case Bytecodes::_invokespecial: 349 assert(!method_entry->has_appendix(), ""); 350 // fall through 351 case Bytecodes::_invokehandle: 352 return method_entry->method(); 353 case Bytecodes::_invokedynamic: 354 ShouldNotReachHere(); 355 default: 356 assert(invoke_code == (Bytecodes::Code)0, "unexpected bytecode"); 357 break; 358 } 359 360 invoke_code = (Bytecodes::Code)method_entry->bytecode2(); 361 if (invoke_code == Bytecodes::_invokevirtual) { 362 if (method_entry->is_vfinal()) { 363 return method_entry->method(); 364 } else { 365 int holder_index = constant_pool()->uncached_klass_ref_index_at(method_entry->constant_pool_index()); 366 if (constant_pool()->tag_at(holder_index).is_klass()) { 367 Klass* klass = constant_pool()->resolved_klass_at(holder_index); 368 return klass->method_at_vtable(method_entry->table_index()); 369 } 370 } 371 } 372 return nullptr; 373 } 374 375 ConstantPoolCache* ConstantPoolCache::allocate(ClassLoaderData* loader_data, 376 const intStack& invokedynamic_map, 377 const GrowableArray<ResolvedIndyEntry> indy_entries, 378 const GrowableArray<ResolvedFieldEntry> field_entries, 379 const GrowableArray<ResolvedMethodEntry> method_entries, 380 TRAPS) { 381 382 int size = ConstantPoolCache::size(); 383 384 // Initialize resolved entry arrays with available data 385 Array<ResolvedFieldEntry>* resolved_field_entries = initialize_resolved_entries_array(loader_data, field_entries, CHECK_NULL); 386 Array<ResolvedIndyEntry>* resolved_indy_entries = initialize_resolved_entries_array(loader_data, indy_entries, CHECK_NULL); 387 Array<ResolvedMethodEntry>* resolved_method_entries = initialize_resolved_entries_array(loader_data, method_entries, CHECK_NULL); 388 389 return new (loader_data, size, MetaspaceObj::ConstantPoolCacheType, THREAD) 390 ConstantPoolCache(invokedynamic_map, resolved_indy_entries, resolved_field_entries, resolved_method_entries); 391 } 392 393 // Record the GC marking cycle when redefined vs. when found in the loom stack chunks. 394 void ConstantPoolCache::record_gc_epoch() { 395 _gc_epoch = CodeCache::gc_epoch(); 396 } 397 398 #if INCLUDE_CDS 399 void ConstantPoolCache::remove_unshareable_info() { 400 assert(CDSConfig::is_dumping_archive(), "sanity"); 401 402 if (_resolved_indy_entries != nullptr) { 403 remove_resolved_indy_entries_if_non_deterministic(); 404 } 405 if (_resolved_field_entries != nullptr) { 406 remove_resolved_field_entries_if_non_deterministic(); 407 } 408 if (_resolved_method_entries != nullptr) { 409 remove_resolved_method_entries_if_non_deterministic(); 410 } 411 412 #if INCLUDE_CDS_JAVA_HEAP 413 _archived_references_index = -1; 414 if (CDSConfig::is_dumping_heap()) { 415 ConstantPool* src_cp = ArchiveBuilder::current()->get_source_addr(constant_pool()); 416 oop rr = HeapShared::scratch_resolved_references(src_cp); 417 if (rr != nullptr) { 418 _archived_references_index = HeapShared::append_root(rr); 419 } 420 } 421 #endif 422 } 423 424 void ConstantPoolCache::remove_resolved_field_entries_if_non_deterministic() { 425 ConstantPool* cp = constant_pool(); 426 ConstantPool* src_cp = ArchiveBuilder::current()->get_source_addr(cp); 427 for (int i = 0; i < _resolved_field_entries->length(); i++) { 428 ResolvedFieldEntry* rfi = _resolved_field_entries->adr_at(i); 429 int cp_index = rfi->constant_pool_index(); 430 bool archived = false; 431 bool resolved = rfi->is_resolved(Bytecodes::_getfield) || 432 rfi->is_resolved(Bytecodes::_putfield); 433 if (resolved && AOTConstantPoolResolver::is_resolution_deterministic(src_cp, cp_index)) { 434 rfi->mark_and_relocate(); 435 archived = true; 436 } else { 437 rfi->remove_unshareable_info(); 438 } 439 if (resolved) { 440 LogStreamHandle(Trace, cds, resolve) log; 441 if (log.is_enabled()) { 442 ResourceMark rm; 443 int klass_cp_index = cp->uncached_klass_ref_index_at(cp_index); 444 Symbol* klass_name = cp->klass_name_at(klass_cp_index); 445 Symbol* name = cp->uncached_name_ref_at(cp_index); 446 Symbol* signature = cp->uncached_signature_ref_at(cp_index); 447 log.print("%s field CP entry [%3d]: %s => %s.%s:%s", 448 (archived ? "archived" : "reverted"), 449 cp_index, 450 cp->pool_holder()->name()->as_C_string(), 451 klass_name->as_C_string(), name->as_C_string(), signature->as_C_string()); 452 } 453 } 454 ArchiveBuilder::alloc_stats()->record_field_cp_entry(archived, resolved && !archived); 455 } 456 } 457 458 void ConstantPoolCache::remove_resolved_method_entries_if_non_deterministic() { 459 ConstantPool* cp = constant_pool(); 460 ConstantPool* src_cp = ArchiveBuilder::current()->get_source_addr(cp); 461 for (int i = 0; i < _resolved_method_entries->length(); i++) { 462 ResolvedMethodEntry* rme = _resolved_method_entries->adr_at(i); 463 int cp_index = rme->constant_pool_index(); 464 bool archived = false; 465 bool resolved = rme->is_resolved(Bytecodes::_invokevirtual) || 466 rme->is_resolved(Bytecodes::_invokespecial) || 467 rme->is_resolved(Bytecodes::_invokeinterface) || 468 rme->is_resolved(Bytecodes::_invokehandle); 469 470 // Just for safety -- this should not happen, but do not archive if we ever see this. 471 resolved &= !(rme->is_resolved(Bytecodes::_invokestatic)); 472 473 if (resolved && can_archive_resolved_method(src_cp, rme)) { 474 rme->mark_and_relocate(src_cp); 475 archived = true; 476 } else { 477 rme->remove_unshareable_info(); 478 } 479 if (resolved) { 480 LogStreamHandle(Trace, cds, resolve) log; 481 if (log.is_enabled()) { 482 ResourceMark rm; 483 int klass_cp_index = cp->uncached_klass_ref_index_at(cp_index); 484 Symbol* klass_name = cp->klass_name_at(klass_cp_index); 485 Symbol* name = cp->uncached_name_ref_at(cp_index); 486 Symbol* signature = cp->uncached_signature_ref_at(cp_index); 487 log.print("%s%s method CP entry [%3d]: %s %s.%s:%s", 488 (archived ? "archived" : "reverted"), 489 (rme->is_resolved(Bytecodes::_invokeinterface) ? " interface" : ""), 490 cp_index, 491 cp->pool_holder()->name()->as_C_string(), 492 klass_name->as_C_string(), name->as_C_string(), signature->as_C_string()); 493 if (archived) { 494 Klass* resolved_klass = cp->resolved_klass_at(klass_cp_index); 495 log.print(" => %s%s", 496 resolved_klass->name()->as_C_string(), 497 (rme->is_resolved(Bytecodes::_invokestatic) ? " *** static" : "")); 498 } 499 } 500 ArchiveBuilder::alloc_stats()->record_method_cp_entry(archived, resolved && !archived); 501 } 502 } 503 } 504 505 void ConstantPoolCache::remove_resolved_indy_entries_if_non_deterministic() { 506 ConstantPool* cp = constant_pool(); 507 ConstantPool* src_cp = ArchiveBuilder::current()->get_source_addr(cp); 508 for (int i = 0; i < _resolved_indy_entries->length(); i++) { 509 ResolvedIndyEntry* rei = _resolved_indy_entries->adr_at(i); 510 int cp_index = rei->constant_pool_index(); 511 bool archived = false; 512 bool resolved = rei->is_resolved(); 513 if (resolved && AOTConstantPoolResolver::is_resolution_deterministic(src_cp, cp_index)) { 514 rei->mark_and_relocate(); 515 archived = true; 516 } else { 517 rei->remove_unshareable_info(); 518 } 519 if (resolved) { 520 LogStreamHandle(Trace, cds, resolve) log; 521 if (log.is_enabled()) { 522 ResourceMark rm; 523 int bsm = cp->bootstrap_method_ref_index_at(cp_index); 524 int bsm_ref = cp->method_handle_index_at(bsm); 525 Symbol* bsm_name = cp->uncached_name_ref_at(bsm_ref); 526 Symbol* bsm_signature = cp->uncached_signature_ref_at(bsm_ref); 527 Symbol* bsm_klass = cp->klass_name_at(cp->uncached_klass_ref_index_at(bsm_ref)); 528 log.print("%s indy CP entry [%3d]: %s (%d)", 529 (archived ? "archived" : "reverted"), 530 cp_index, cp->pool_holder()->name()->as_C_string(), i); 531 log.print(" %s %s.%s:%s", (archived ? "=>" : " "), bsm_klass->as_C_string(), 532 bsm_name->as_C_string(), bsm_signature->as_C_string()); 533 } 534 ArchiveBuilder::alloc_stats()->record_indy_cp_entry(archived, resolved && !archived); 535 } 536 } 537 } 538 539 bool ConstantPoolCache::can_archive_resolved_method(ConstantPool* src_cp, ResolvedMethodEntry* method_entry) { 540 InstanceKlass* pool_holder = constant_pool()->pool_holder(); 541 if (!(pool_holder->is_shared_boot_class() || pool_holder->is_shared_platform_class() || 542 pool_holder->is_shared_app_class())) { 543 // Archiving resolved cp entries for classes from non-builtin loaders 544 // is not yet supported. 545 return false; 546 } 547 548 if (CDSConfig::is_dumping_dynamic_archive()) { 549 // InstanceKlass::methods() has been resorted. We need to 550 // update the vtable_index in method_entry (not implemented) 551 return false; 552 } 553 554 if (!method_entry->is_resolved(Bytecodes::_invokevirtual)) { 555 if (method_entry->method() == nullptr) { 556 return false; 557 } 558 if (method_entry->method()->is_continuation_native_intrinsic()) { 559 return false; // FIXME: corresponding stub is generated on demand during method resolution (see LinkResolver::resolve_static_call). 560 } 561 } 562 563 int cp_index = method_entry->constant_pool_index(); 564 assert(src_cp->tag_at(cp_index).is_method() || src_cp->tag_at(cp_index).is_interface_method(), "sanity"); 565 566 if (!AOTConstantPoolResolver::is_resolution_deterministic(src_cp, cp_index)) { 567 return false; 568 } 569 570 if (method_entry->is_resolved(Bytecodes::_invokeinterface) || 571 method_entry->is_resolved(Bytecodes::_invokevirtual) || 572 method_entry->is_resolved(Bytecodes::_invokespecial)) { 573 return true; 574 } else if (method_entry->is_resolved(Bytecodes::_invokehandle)) { 575 if (CDSConfig::is_dumping_invokedynamic()) { 576 // invokehandle depends on archived MethodType and LambdaForms. 577 return true; 578 } else { 579 return false; 580 } 581 } else { 582 return false; 583 } 584 } 585 #endif // INCLUDE_CDS 586 587 void ConstantPoolCache::deallocate_contents(ClassLoaderData* data) { 588 assert(!is_shared(), "shared caches are not deallocated"); 589 data->remove_handle(_resolved_references); 590 set_resolved_references(OopHandle()); 591 MetadataFactory::free_array<u2>(data, _reference_map); 592 set_reference_map(nullptr); 593 #if INCLUDE_CDS 594 if (_resolved_indy_entries != nullptr) { 595 MetadataFactory::free_array<ResolvedIndyEntry>(data, _resolved_indy_entries); 596 _resolved_indy_entries = nullptr; 597 } 598 if (_resolved_field_entries != nullptr) { 599 MetadataFactory::free_array<ResolvedFieldEntry>(data, _resolved_field_entries); 600 _resolved_field_entries = nullptr; 601 } 602 if (_resolved_method_entries != nullptr) { 603 MetadataFactory::free_array<ResolvedMethodEntry>(data, _resolved_method_entries); 604 _resolved_method_entries = nullptr; 605 } 606 #endif 607 } 608 609 #if INCLUDE_CDS_JAVA_HEAP 610 oop ConstantPoolCache::archived_references() { 611 if (_archived_references_index < 0) { 612 return nullptr; 613 } 614 return HeapShared::get_root(_archived_references_index); 615 } 616 617 void ConstantPoolCache::clear_archived_references() { 618 if (_archived_references_index >= 0) { 619 HeapShared::clear_root(_archived_references_index); 620 _archived_references_index = -1; 621 } 622 } 623 #endif 624 625 #if INCLUDE_JVMTI 626 static void log_adjust(const char* entry_type, Method* old_method, Method* new_method, bool* trace_name_printed) { 627 ResourceMark rm; 628 629 if (!(*trace_name_printed)) { 630 log_info(redefine, class, update)("adjust: name=%s", old_method->method_holder()->external_name()); 631 *trace_name_printed = true; 632 } 633 log_trace(redefine, class, update, constantpool) 634 ("cpc %s entry update: %s", entry_type, new_method->external_name()); 635 } 636 637 // RedefineClasses() API support: 638 // If any entry of this ConstantPoolCache points to any of 639 // old_methods, replace it with the corresponding new_method. 640 void ConstantPoolCache::adjust_method_entries(bool * trace_name_printed) { 641 if (_resolved_indy_entries != nullptr) { 642 for (int j = 0; j < _resolved_indy_entries->length(); j++) { 643 Method* old_method = resolved_indy_entry_at(j)->method(); 644 if (old_method == nullptr || !old_method->is_old()) { 645 continue; 646 } 647 assert(!old_method->is_deleted(), "cannot delete these methods"); 648 Method* new_method = old_method->get_new_method(); 649 resolved_indy_entry_at(j)->adjust_method_entry(new_method); 650 log_adjust("indy", old_method, new_method, trace_name_printed); 651 } 652 } 653 if (_resolved_method_entries != nullptr) { 654 for (int i = 0; i < _resolved_method_entries->length(); i++) { 655 ResolvedMethodEntry* method_entry = resolved_method_entry_at(i); 656 // get interesting method entry 657 Method* old_method = method_entry->method(); 658 if (old_method == nullptr || !old_method->is_old()) { 659 continue; // skip uninteresting entries 660 } 661 if (old_method->is_deleted()) { 662 // clean up entries with deleted methods 663 method_entry->reset_entry(); 664 continue; 665 } 666 Method* new_method = old_method->get_new_method(); 667 method_entry->adjust_method_entry(new_method); 668 log_adjust("non-indy", old_method, new_method, trace_name_printed); 669 } 670 } 671 } 672 673 // the constant pool cache should never contain old or obsolete methods 674 bool ConstantPoolCache::check_no_old_or_obsolete_entries() { 675 ResourceMark rm; 676 if (_resolved_indy_entries != nullptr) { 677 for (int i = 0; i < _resolved_indy_entries->length(); i++) { 678 Method* m = resolved_indy_entry_at(i)->method(); 679 if (m != nullptr && !resolved_indy_entry_at(i)->check_no_old_or_obsolete_entry()) { 680 log_trace(redefine, class, update, constantpool) 681 ("cpcache check found old method entry: class: %s, old: %d, obsolete: %d, method: %s", 682 constant_pool()->pool_holder()->external_name(), m->is_old(), m->is_obsolete(), m->external_name()); 683 return false; 684 } 685 } 686 } 687 if (_resolved_method_entries != nullptr) { 688 for (int i = 0; i < _resolved_method_entries->length(); i++) { 689 ResolvedMethodEntry* method_entry = resolved_method_entry_at(i); 690 Method* m = method_entry->method(); 691 if (m != nullptr && !method_entry->check_no_old_or_obsolete_entry()) { 692 log_trace(redefine, class, update, constantpool) 693 ("cpcache check found old method entry: class: %s, old: %d, obsolete: %d, method: %s", 694 constant_pool()->pool_holder()->external_name(), m->is_old(), m->is_obsolete(), m->external_name()); 695 return false; 696 } 697 } 698 } 699 return true; 700 } 701 702 void ConstantPoolCache::dump_cache() { 703 print_on(tty); 704 } 705 #endif // INCLUDE_JVMTI 706 707 void ConstantPoolCache::metaspace_pointers_do(MetaspaceClosure* it) { 708 log_trace(cds)("Iter(ConstantPoolCache): %p", this); 709 it->push(&_constant_pool); 710 it->push(&_reference_map); 711 if (_resolved_indy_entries != nullptr) { 712 it->push(&_resolved_indy_entries, MetaspaceClosure::_writable); 713 } 714 if (_resolved_field_entries != nullptr) { 715 it->push(&_resolved_field_entries, MetaspaceClosure::_writable); 716 } 717 if (_resolved_method_entries != nullptr) { 718 it->push(&_resolved_method_entries, MetaspaceClosure::_writable); 719 } 720 } 721 722 bool ConstantPoolCache::save_and_throw_indy_exc( 723 const constantPoolHandle& cpool, int cpool_index, int index, constantTag tag, TRAPS) { 724 725 assert(HAS_PENDING_EXCEPTION, "No exception got thrown!"); 726 assert(PENDING_EXCEPTION->is_a(vmClasses::LinkageError_klass()), 727 "No LinkageError exception"); 728 729 // Use the resolved_references() lock for this cpCache entry. 730 // resolved_references are created for all classes with Invokedynamic, MethodHandle 731 // or MethodType constant pool cache entries. 732 JavaThread* current = THREAD; 733 objArrayHandle resolved_references(current, cpool->resolved_references()); 734 assert(resolved_references() != nullptr, 735 "a resolved_references array should have been created for this class"); 736 ObjectLocker ol(resolved_references, current); 737 738 // if the indy_info is resolved or the indy_resolution_failed flag is set then another 739 // thread either succeeded in resolving the method or got a LinkageError 740 // exception, before this thread was able to record its failure. So, clear 741 // this thread's exception and return false so caller can use the earlier 742 // thread's result. 743 if (resolved_indy_entry_at(index)->is_resolved() || resolved_indy_entry_at(index)->resolution_failed()) { 744 CLEAR_PENDING_EXCEPTION; 745 return false; 746 } 747 ResourceMark rm(THREAD); 748 Symbol* error = PENDING_EXCEPTION->klass()->name(); 749 const char* message = java_lang_Throwable::message_as_utf8(PENDING_EXCEPTION); 750 751 int encoded_index = ResolutionErrorTable::encode_indy_index(index); 752 SystemDictionary::add_resolution_error(cpool, encoded_index, error, message); 753 resolved_indy_entry_at(index)->set_resolution_failed(); 754 return true; 755 } 756 757 oop ConstantPoolCache::set_dynamic_call(const CallInfo &call_info, int index) { 758 ResourceMark rm; 759 760 // Use the resolved_references() lock for this cpCache entry. 761 // resolved_references are created for all classes with Invokedynamic, MethodHandle 762 // or MethodType constant pool cache entries. 763 JavaThread* current = JavaThread::current(); 764 constantPoolHandle cp(current, constant_pool()); 765 766 objArrayHandle resolved_references(current, cp->resolved_references()); 767 assert(resolved_references() != nullptr, 768 "a resolved_references array should have been created for this class"); 769 ObjectLocker ol(resolved_references, current); 770 assert(index >= 0, "Indy index must be positive at this point"); 771 772 if (resolved_indy_entry_at(index)->method() != nullptr) { 773 return cp->resolved_reference_from_indy(index); 774 } 775 776 if (resolved_indy_entry_at(index)->resolution_failed()) { 777 // Before we got here, another thread got a LinkageError exception during 778 // resolution. Ignore our success and throw their exception. 779 guarantee(index >= 0, "Invalid indy index"); 780 int encoded_index = ResolutionErrorTable::encode_indy_index(index); 781 ConstantPool::throw_resolution_error(cp, encoded_index, current); 782 return nullptr; 783 } 784 785 Method* adapter = call_info.resolved_method(); 786 const Handle appendix = call_info.resolved_appendix(); 787 const bool has_appendix = appendix.not_null(); 788 789 LogStream* log_stream = nullptr; 790 LogStreamHandle(Debug, methodhandles, indy) lsh_indy; 791 if (lsh_indy.is_enabled()) { 792 ResourceMark rm; 793 log_stream = &lsh_indy; 794 log_stream->print_cr("set_method_handle bc=%d appendix=" PTR_FORMAT "%s method=" PTR_FORMAT " (local signature) ", 795 0xba, 796 p2i(appendix()), 797 (has_appendix ? "" : " (unused)"), 798 p2i(adapter)); 799 adapter->print_on(log_stream); 800 if (has_appendix) appendix()->print_on(log_stream); 801 } 802 803 if (has_appendix) { 804 const int appendix_index = resolved_indy_entry_at(index)->resolved_references_index(); 805 assert(appendix_index >= 0 && appendix_index < resolved_references->length(), "oob"); 806 assert(resolved_references->obj_at(appendix_index) == nullptr, "init just once"); 807 resolved_references->obj_at_put(appendix_index, appendix()); 808 } 809 810 // Populate entry with resolved information 811 assert(resolved_indy_entries() != nullptr, "Invokedynamic array is empty, cannot fill with resolved information"); 812 resolved_indy_entry_at(index)->fill_in(adapter, adapter->size_of_parameters(), as_TosState(adapter->result_type()), has_appendix); 813 814 if (log_stream != nullptr) { 815 resolved_indy_entry_at(index)->print_on(log_stream); 816 } 817 return appendix(); 818 } 819 820 oop ConstantPoolCache::appendix_if_resolved(int method_index) const { 821 ResolvedMethodEntry* method_entry = resolved_method_entry_at(method_index); 822 return appendix_if_resolved(method_entry); 823 } 824 825 oop ConstantPoolCache::appendix_if_resolved(ResolvedMethodEntry* method_entry) const { 826 if (!method_entry->has_appendix()) 827 return nullptr; 828 const int ref_index = method_entry->resolved_references_index(); 829 return constant_pool()->resolved_reference_at(ref_index); 830 } 831 832 // Printing 833 834 void ConstantPoolCache::print_on(outputStream* st) const { 835 st->print_cr("%s", internal_name()); 836 // print constant pool cache entries 837 if (_resolved_field_entries != nullptr) { 838 print_resolved_field_entries(st); 839 } 840 if (_resolved_method_entries != nullptr) { 841 print_resolved_method_entries(st); 842 } 843 if (_resolved_indy_entries != nullptr) { 844 print_resolved_indy_entries(st); 845 } 846 } 847 848 void ConstantPoolCache::print_resolved_field_entries(outputStream* st) const { 849 for (int field_index = 0; field_index < resolved_field_entries_length(); field_index++) { 850 resolved_field_entry_at(field_index)->print_on(st); 851 } 852 } 853 854 void ConstantPoolCache::print_resolved_method_entries(outputStream* st) const { 855 for (int method_index = 0; method_index < resolved_method_entries_length(); method_index++) { 856 ResolvedMethodEntry* method_entry = resolved_method_entry_at(method_index); 857 method_entry->print_on(st); 858 if (method_entry->has_appendix()) { 859 st->print(" appendix: "); 860 constant_pool()->resolved_reference_from_method(method_index)->print_on(st); 861 } 862 } 863 } 864 865 void ConstantPoolCache::print_resolved_indy_entries(outputStream* st) const { 866 for (int indy_index = 0; indy_index < resolved_indy_entries_length(); indy_index++) { 867 ResolvedIndyEntry* indy_entry = resolved_indy_entry_at(indy_index); 868 indy_entry->print_on(st); 869 if (indy_entry->has_appendix()) { 870 st->print(" appendix: "); 871 constant_pool()->resolved_reference_from_indy(indy_index)->print_on(st); 872 } 873 } 874 }