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