1 /* 2 * Copyright (c) 1997, 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/archiveHeapLoader.hpp" 28 #include "cds/archiveHeapWriter.hpp" 29 #include "cds/cdsConfig.hpp" 30 #include "cds/heapShared.hpp" 31 #include "classfile/classLoader.hpp" 32 #include "classfile/classLoaderData.hpp" 33 #include "classfile/javaClasses.inline.hpp" 34 #include "classfile/metadataOnStackMark.hpp" 35 #include "classfile/stringTable.hpp" 36 #include "classfile/systemDictionary.hpp" 37 #include "classfile/systemDictionaryShared.hpp" 38 #include "classfile/vmClasses.hpp" 39 #include "classfile/vmSymbols.hpp" 40 #include "code/codeCache.hpp" 41 #include "interpreter/bootstrapInfo.hpp" 42 #include "interpreter/linkResolver.hpp" 43 #include "jvm.h" 44 #include "logging/log.hpp" 45 #include "logging/logStream.hpp" 46 #include "memory/allocation.inline.hpp" 47 #include "memory/metadataFactory.hpp" 48 #include "memory/metaspaceClosure.hpp" 49 #include "memory/oopFactory.hpp" 50 #include "memory/resourceArea.hpp" 51 #include "memory/universe.hpp" 52 #include "oops/array.hpp" 53 #include "oops/constantPool.inline.hpp" 54 #include "oops/cpCache.inline.hpp" 55 #include "oops/fieldStreams.inline.hpp" 56 #include "oops/instanceKlass.hpp" 57 #include "oops/klass.inline.hpp" 58 #include "oops/objArrayKlass.hpp" 59 #include "oops/objArrayOop.inline.hpp" 60 #include "oops/oop.inline.hpp" 61 #include "oops/typeArrayOop.inline.hpp" 62 #include "prims/jvmtiExport.hpp" 63 #include "runtime/atomic.hpp" 64 #include "runtime/fieldDescriptor.inline.hpp" 65 #include "runtime/handles.inline.hpp" 66 #include "runtime/init.hpp" 67 #include "runtime/javaCalls.hpp" 68 #include "runtime/javaThread.inline.hpp" 69 #include "runtime/perfData.hpp" 70 #include "runtime/signature.hpp" 71 #include "runtime/vframe.inline.hpp" 72 #include "utilities/checkedCast.hpp" 73 #include "utilities/copy.hpp" 74 75 ConstantPool* ConstantPool::allocate(ClassLoaderData* loader_data, int length, TRAPS) { 76 Array<u1>* tags = MetadataFactory::new_array<u1>(loader_data, length, 0, CHECK_NULL); 77 int size = ConstantPool::size(length); 78 return new (loader_data, size, MetaspaceObj::ConstantPoolType, THREAD) ConstantPool(tags); 79 } 80 81 void ConstantPool::copy_fields(const ConstantPool* orig) { 82 // Preserve dynamic constant information from the original pool 83 if (orig->has_dynamic_constant()) { 84 set_has_dynamic_constant(); 85 } 86 87 set_major_version(orig->major_version()); 88 set_minor_version(orig->minor_version()); 89 90 set_source_file_name_index(orig->source_file_name_index()); 91 set_generic_signature_index(orig->generic_signature_index()); 92 } 93 94 #ifdef ASSERT 95 96 // MetaspaceObj allocation invariant is calloc equivalent memory 97 // simple verification of this here (JVM_CONSTANT_Invalid == 0 ) 98 static bool tag_array_is_zero_initialized(Array<u1>* tags) { 99 assert(tags != nullptr, "invariant"); 100 const int length = tags->length(); 101 for (int index = 0; index < length; ++index) { 102 if (JVM_CONSTANT_Invalid != tags->at(index)) { 103 return false; 104 } 105 } 106 return true; 107 } 108 109 #endif 110 111 ConstantPool::ConstantPool() { 112 assert(CDSConfig::is_dumping_static_archive() || CDSConfig::is_using_archive(), "only for CDS"); 113 } 114 115 ConstantPool::ConstantPool(Array<u1>* tags) : 116 _tags(tags), 117 _length(tags->length()) { 118 119 assert(_tags != nullptr, "invariant"); 120 assert(tags->length() == _length, "invariant"); 121 assert(tag_array_is_zero_initialized(tags), "invariant"); 122 assert(0 == flags(), "invariant"); 123 assert(0 == version(), "invariant"); 124 assert(nullptr == _pool_holder, "invariant"); 125 } 126 127 void ConstantPool::deallocate_contents(ClassLoaderData* loader_data) { 128 if (cache() != nullptr) { 129 MetadataFactory::free_metadata(loader_data, cache()); 130 set_cache(nullptr); 131 } 132 133 MetadataFactory::free_array<Klass*>(loader_data, resolved_klasses()); 134 set_resolved_klasses(nullptr); 135 136 MetadataFactory::free_array<jushort>(loader_data, operands()); 137 set_operands(nullptr); 138 139 release_C_heap_structures(); 140 141 // free tag array 142 MetadataFactory::free_array<u1>(loader_data, tags()); 143 set_tags(nullptr); 144 } 145 146 void ConstantPool::release_C_heap_structures() { 147 // walk constant pool and decrement symbol reference counts 148 unreference_symbols(); 149 } 150 151 void ConstantPool::metaspace_pointers_do(MetaspaceClosure* it) { 152 log_trace(cds)("Iter(ConstantPool): %p", this); 153 154 it->push(&_tags, MetaspaceClosure::_writable); 155 it->push(&_cache); 156 it->push(&_pool_holder); 157 it->push(&_operands); 158 it->push(&_resolved_klasses, MetaspaceClosure::_writable); 159 160 for (int i = 0; i < length(); i++) { 161 // The only MSO's embedded in the CP entries are Symbols: 162 // JVM_CONSTANT_String 163 // JVM_CONSTANT_Utf8 164 constantTag ctag = tag_at(i); 165 if (ctag.is_string() || ctag.is_utf8()) { 166 it->push(symbol_at_addr(i)); 167 } 168 } 169 } 170 171 objArrayOop ConstantPool::resolved_references() const { 172 return _cache->resolved_references(); 173 } 174 175 // Called from outside constant pool resolution where a resolved_reference array 176 // may not be present. 177 objArrayOop ConstantPool::resolved_references_or_null() const { 178 if (_cache == nullptr) { 179 return nullptr; 180 } else { 181 return _cache->resolved_references(); 182 } 183 } 184 185 oop ConstantPool::resolved_reference_at(int index) const { 186 oop result = resolved_references()->obj_at(index); 187 assert(oopDesc::is_oop_or_null(result), "Must be oop"); 188 return result; 189 } 190 191 // Use a CAS for multithreaded access 192 oop ConstantPool::set_resolved_reference_at(int index, oop new_result) { 193 assert(oopDesc::is_oop_or_null(new_result), "Must be oop"); 194 return resolved_references()->replace_if_null(index, new_result); 195 } 196 197 // Create resolved_references array and mapping array for original cp indexes 198 // The ldc bytecode was rewritten to have the resolved reference array index so need a way 199 // to map it back for resolving and some unlikely miscellaneous uses. 200 // The objects created by invokedynamic are appended to this list. 201 void ConstantPool::initialize_resolved_references(ClassLoaderData* loader_data, 202 const intStack& reference_map, 203 int constant_pool_map_length, 204 TRAPS) { 205 // Initialized the resolved object cache. 206 int map_length = reference_map.length(); 207 if (map_length > 0) { 208 // Only need mapping back to constant pool entries. The map isn't used for 209 // invokedynamic resolved_reference entries. For invokedynamic entries, 210 // the constant pool cache index has the mapping back to both the constant 211 // pool and to the resolved reference index. 212 if (constant_pool_map_length > 0) { 213 Array<u2>* om = MetadataFactory::new_array<u2>(loader_data, constant_pool_map_length, CHECK); 214 215 for (int i = 0; i < constant_pool_map_length; i++) { 216 int x = reference_map.at(i); 217 assert(x == (int)(jushort) x, "klass index is too big"); 218 om->at_put(i, (jushort)x); 219 } 220 set_reference_map(om); 221 } 222 223 // Create Java array for holding resolved strings, methodHandles, 224 // methodTypes, invokedynamic and invokehandle appendix objects, etc. 225 objArrayOop stom = oopFactory::new_objArray(vmClasses::Object_klass(), map_length, CHECK); 226 HandleMark hm(THREAD); 227 Handle refs_handle (THREAD, stom); // must handleize. 228 set_resolved_references(loader_data->add_handle(refs_handle)); 229 230 // Create a "scratch" copy of the resolved references array to archive 231 if (CDSConfig::is_dumping_heap()) { 232 objArrayOop scratch_references = oopFactory::new_objArray(vmClasses::Object_klass(), map_length, CHECK); 233 HeapShared::add_scratch_resolved_references(this, scratch_references); 234 } 235 } 236 } 237 238 void ConstantPool::allocate_resolved_klasses(ClassLoaderData* loader_data, int num_klasses, TRAPS) { 239 // A ConstantPool can't possibly have 0xffff valid class entries, 240 // because entry #0 must be CONSTANT_Invalid, and each class entry must refer to a UTF8 241 // entry for the class's name. So at most we will have 0xfffe class entries. 242 // This allows us to use 0xffff (ConstantPool::_temp_resolved_klass_index) to indicate 243 // UnresolvedKlass entries that are temporarily created during class redefinition. 244 assert(num_klasses < CPKlassSlot::_temp_resolved_klass_index, "sanity"); 245 assert(resolved_klasses() == nullptr, "sanity"); 246 Array<Klass*>* rk = MetadataFactory::new_array<Klass*>(loader_data, num_klasses, CHECK); 247 set_resolved_klasses(rk); 248 } 249 250 void ConstantPool::initialize_unresolved_klasses(ClassLoaderData* loader_data, TRAPS) { 251 int len = length(); 252 int num_klasses = 0; 253 for (int i = 1; i <len; i++) { 254 switch (tag_at(i).value()) { 255 case JVM_CONSTANT_ClassIndex: 256 { 257 const int class_index = klass_index_at(i); 258 unresolved_klass_at_put(i, class_index, num_klasses++); 259 } 260 break; 261 #ifndef PRODUCT 262 case JVM_CONSTANT_Class: 263 case JVM_CONSTANT_UnresolvedClass: 264 case JVM_CONSTANT_UnresolvedClassInError: 265 // All of these should have been reverted back to ClassIndex before calling 266 // this function. 267 ShouldNotReachHere(); 268 #endif 269 } 270 } 271 allocate_resolved_klasses(loader_data, num_klasses, THREAD); 272 } 273 274 // Hidden class support: 275 void ConstantPool::klass_at_put(int class_index, Klass* k) { 276 assert(k != nullptr, "must be valid klass"); 277 CPKlassSlot kslot = klass_slot_at(class_index); 278 int resolved_klass_index = kslot.resolved_klass_index(); 279 Klass** adr = resolved_klasses()->adr_at(resolved_klass_index); 280 Atomic::release_store(adr, k); 281 282 // The interpreter assumes when the tag is stored, the klass is resolved 283 // and the Klass* non-null, so we need hardware store ordering here. 284 release_tag_at_put(class_index, JVM_CONSTANT_Class); 285 } 286 287 #if INCLUDE_CDS_JAVA_HEAP 288 template <typename Function> 289 void ConstantPool::iterate_archivable_resolved_references(Function function) { 290 objArrayOop rr = resolved_references(); 291 if (rr != nullptr && cache() != nullptr && CDSConfig::is_dumping_method_handles()) { 292 Array<ResolvedIndyEntry>* indy_entries = cache()->resolved_indy_entries(); 293 if (indy_entries != nullptr) { 294 for (int i = 0; i < indy_entries->length(); i++) { 295 ResolvedIndyEntry *rie = indy_entries->adr_at(i); 296 if (rie->is_resolved() && AOTConstantPoolResolver::is_resolution_deterministic(this, rie->constant_pool_index())) { 297 int rr_index = rie->resolved_references_index(); 298 assert(resolved_reference_at(rr_index) != nullptr, "must exist"); 299 function(rr_index); 300 301 // Save the BSM as well (sometimes the JIT looks up the BSM it for replay) 302 int indy_cp_index = rie->constant_pool_index(); 303 int bsm_mh_cp_index = bootstrap_method_ref_index_at(indy_cp_index); 304 int bsm_rr_index = cp_to_object_index(bsm_mh_cp_index); 305 assert(resolved_reference_at(bsm_rr_index) != nullptr, "must exist"); 306 function(bsm_rr_index); 307 } 308 } 309 } 310 311 Array<ResolvedMethodEntry>* method_entries = cache()->resolved_method_entries(); 312 if (method_entries != nullptr) { 313 for (int i = 0; i < method_entries->length(); i++) { 314 ResolvedMethodEntry* rme = method_entries->adr_at(i); 315 if (rme->is_resolved(Bytecodes::_invokehandle) && rme->has_appendix() && 316 cache()->can_archive_resolved_method(this, rme)) { 317 int rr_index = rme->resolved_references_index(); 318 assert(resolved_reference_at(rr_index) != nullptr, "must exist"); 319 function(rr_index); 320 } 321 } 322 } 323 } 324 } 325 326 // Returns the _resolved_reference array after removing unarchivable items from it. 327 // Returns null if this class is not supported, or _resolved_reference doesn't exist. 328 objArrayOop ConstantPool::prepare_resolved_references_for_archiving() { 329 if (_cache == nullptr) { 330 return nullptr; // nothing to do 331 } 332 333 InstanceKlass *ik = pool_holder(); 334 if (!SystemDictionaryShared::is_builtin_loader(ik->class_loader_data())) { 335 // Archiving resolved references for classes from non-builtin loaders 336 // is not yet supported. 337 return nullptr; 338 } 339 340 objArrayOop rr = resolved_references(); 341 if (rr != nullptr) { 342 ResourceMark rm; 343 int rr_len = rr->length(); 344 GrowableArray<bool> keep_resolved_refs(rr_len, rr_len, false); 345 346 iterate_archivable_resolved_references([&](int rr_index) { 347 keep_resolved_refs.at_put(rr_index, true); 348 }); 349 350 objArrayOop scratch_rr = HeapShared::scratch_resolved_references(this); 351 Array<u2>* ref_map = reference_map(); 352 int ref_map_len = ref_map == nullptr ? 0 : ref_map->length(); 353 for (int i = 0; i < rr_len; i++) { 354 oop obj = rr->obj_at(i); 355 scratch_rr->obj_at_put(i, nullptr); 356 if (obj != nullptr) { 357 if (i < ref_map_len) { 358 int index = object_to_cp_index(i); 359 if (tag_at(index).is_string()) { 360 assert(java_lang_String::is_instance(obj), "must be"); 361 if (!ArchiveHeapWriter::is_string_too_large_to_archive(obj)) { 362 scratch_rr->obj_at_put(i, obj); 363 } 364 continue; 365 } 366 } 367 368 if (keep_resolved_refs.at(i)) { 369 scratch_rr->obj_at_put(i, obj); 370 } 371 } 372 } 373 return scratch_rr; 374 } 375 return rr; 376 } 377 378 void ConstantPool::add_dumped_interned_strings() { 379 InstanceKlass* ik = pool_holder(); 380 if (!ik->is_linked()) { 381 // resolved_references() doesn't exist yet, so we have no resolved CONSTANT_String entries. However, 382 // some static final fields may have default values that were initialized when the class was parsed. 383 // We need to enter those into the CDS archive strings table. 384 for (JavaFieldStream fs(ik); !fs.done(); fs.next()) { 385 if (fs.access_flags().is_static()) { 386 fieldDescriptor& fd = fs.field_descriptor(); 387 if (fd.field_type() == T_OBJECT) { 388 int offset = fd.offset(); 389 check_and_add_dumped_interned_string(ik->java_mirror()->obj_field(offset)); 390 } 391 } 392 } 393 } else { 394 objArrayOop rr = resolved_references(); 395 if (rr != nullptr) { 396 int rr_len = rr->length(); 397 for (int i = 0; i < rr_len; i++) { 398 check_and_add_dumped_interned_string(rr->obj_at(i)); 399 } 400 } 401 } 402 } 403 404 void ConstantPool::check_and_add_dumped_interned_string(oop obj) { 405 if (obj != nullptr && java_lang_String::is_instance(obj) && 406 !ArchiveHeapWriter::is_string_too_large_to_archive(obj)) { 407 HeapShared::add_to_dumped_interned_strings(obj); 408 } 409 } 410 411 #endif 412 413 #if INCLUDE_CDS 414 // CDS support. Create a new resolved_references array. 415 void ConstantPool::restore_unshareable_info(TRAPS) { 416 if (!_pool_holder->is_linked() && !_pool_holder->is_rewritten()) { 417 return; 418 } 419 assert(is_constantPool(), "ensure C++ vtable is restored"); 420 assert(on_stack(), "should always be set for shared constant pools"); 421 assert(is_shared(), "should always be set for shared constant pools"); 422 if (is_for_method_handle_intrinsic()) { 423 // See the same check in remove_unshareable_info() below. 424 assert(cache() == nullptr, "must not have cpCache"); 425 return; 426 } 427 assert(_cache != nullptr, "constant pool _cache should not be null"); 428 429 // Only create the new resolved references array if it hasn't been attempted before 430 if (resolved_references() != nullptr) return; 431 432 if (vmClasses::Object_klass_loaded()) { 433 ClassLoaderData* loader_data = pool_holder()->class_loader_data(); 434 #if INCLUDE_CDS_JAVA_HEAP 435 if (ArchiveHeapLoader::is_in_use() && 436 _cache->archived_references() != nullptr) { 437 oop archived = _cache->archived_references(); 438 // Create handle for the archived resolved reference array object 439 HandleMark hm(THREAD); 440 Handle refs_handle(THREAD, archived); 441 set_resolved_references(loader_data->add_handle(refs_handle)); 442 _cache->clear_archived_references(); 443 } else 444 #endif 445 { 446 // No mapped archived resolved reference array 447 // Recreate the object array and add to ClassLoaderData. 448 int map_length = resolved_reference_length(); 449 if (map_length > 0) { 450 objArrayOop stom = oopFactory::new_objArray(vmClasses::Object_klass(), map_length, CHECK); 451 HandleMark hm(THREAD); 452 Handle refs_handle(THREAD, stom); // must handleize. 453 set_resolved_references(loader_data->add_handle(refs_handle)); 454 } 455 } 456 } 457 458 if (CDSConfig::is_dumping_final_static_archive() && CDSConfig::is_dumping_heap() && resolved_references() != nullptr) { 459 objArrayOop scratch_references = oopFactory::new_objArray(vmClasses::Object_klass(), resolved_references()->length(), CHECK); 460 HeapShared::add_scratch_resolved_references(this, scratch_references); 461 } 462 } 463 464 void ConstantPool::remove_unshareable_info() { 465 // Shared ConstantPools are in the RO region, so the _flags cannot be modified. 466 // The _on_stack flag is used to prevent ConstantPools from deallocation during 467 // class redefinition. Since shared ConstantPools cannot be deallocated anyway, 468 // we always set _on_stack to true to avoid having to change _flags during runtime. 469 _flags |= (_on_stack | _is_shared); 470 471 if (is_for_method_handle_intrinsic()) { 472 // This CP was created by Method::make_method_handle_intrinsic() and has nothing 473 // that need to be removed/restored. It has no cpCache since the intrinsic methods 474 // don't have any bytecodes. 475 assert(cache() == nullptr, "must not have cpCache"); 476 return; 477 } 478 479 bool update_resolved_reference = true; 480 if (CDSConfig::is_dumping_final_static_archive()) { 481 ConstantPool* src_cp = ArchiveBuilder::current()->get_source_addr(this); 482 InstanceKlass* src_holder = src_cp->pool_holder(); 483 if (src_holder->is_shared_unregistered_class()) { 484 // Unregistered classes are not loaded in the AOT assembly phase. The resolved reference length 485 // is already saved during the training run. 486 precond(!src_holder->is_loaded()); 487 precond(resolved_reference_length() >= 0); 488 precond(resolved_references() == nullptr); 489 update_resolved_reference = false; 490 } 491 } 492 493 // resolved_references(): remember its length. If it cannot be restored 494 // from the archived heap objects at run time, we need to dynamically allocate it. 495 if (update_resolved_reference && cache() != nullptr) { 496 set_resolved_reference_length( 497 resolved_references() != nullptr ? resolved_references()->length() : 0); 498 set_resolved_references(OopHandle()); 499 } 500 remove_unshareable_entries(); 501 } 502 503 static const char* get_type(Klass* k) { 504 const char* type; 505 Klass* src_k; 506 if (ArchiveBuilder::is_active() && ArchiveBuilder::current()->is_in_buffer_space(k)) { 507 src_k = ArchiveBuilder::current()->get_source_addr(k); 508 } else { 509 src_k = k; 510 } 511 512 if (src_k->is_objArray_klass()) { 513 src_k = ObjArrayKlass::cast(src_k)->bottom_klass(); 514 assert(!src_k->is_objArray_klass(), "sanity"); 515 } 516 517 if (src_k->is_typeArray_klass()) { 518 type = "prim"; 519 } else { 520 InstanceKlass* src_ik = InstanceKlass::cast(src_k); 521 if (CDSConfig::is_dumping_final_static_archive() && src_ik->class_loader_data() == nullptr) { 522 return "unreg"; 523 } 524 oop loader = src_ik->class_loader(); 525 if (loader == nullptr) { 526 type = "boot"; 527 } else if (loader == SystemDictionary::java_platform_loader()) { 528 type = "plat"; 529 } else if (loader == SystemDictionary::java_system_loader()) { 530 type = "app"; 531 } else { 532 type = "unreg"; 533 } 534 } 535 536 return type; 537 } 538 539 void ConstantPool::remove_unshareable_entries() { 540 ResourceMark rm; 541 log_info(cds, resolve)("Archiving CP entries for %s", pool_holder()->name()->as_C_string()); 542 for (int cp_index = 1; cp_index < length(); cp_index++) { // cp_index 0 is unused 543 int cp_tag = tag_at(cp_index).value(); 544 switch (cp_tag) { 545 case JVM_CONSTANT_UnresolvedClass: 546 ArchiveBuilder::alloc_stats()->record_klass_cp_entry(false, false); 547 break; 548 case JVM_CONSTANT_UnresolvedClassInError: 549 tag_at_put(cp_index, JVM_CONSTANT_UnresolvedClass); 550 ArchiveBuilder::alloc_stats()->record_klass_cp_entry(false, true); 551 break; 552 case JVM_CONSTANT_MethodHandleInError: 553 tag_at_put(cp_index, JVM_CONSTANT_MethodHandle); 554 break; 555 case JVM_CONSTANT_MethodTypeInError: 556 tag_at_put(cp_index, JVM_CONSTANT_MethodType); 557 break; 558 case JVM_CONSTANT_DynamicInError: 559 tag_at_put(cp_index, JVM_CONSTANT_Dynamic); 560 break; 561 case JVM_CONSTANT_Class: 562 remove_resolved_klass_if_non_deterministic(cp_index); 563 break; 564 default: 565 break; 566 } 567 } 568 569 if (cache() != nullptr) { 570 // cache() is null if this class is not yet linked. 571 cache()->remove_unshareable_info(); 572 } 573 } 574 575 void ConstantPool::remove_resolved_klass_if_non_deterministic(int cp_index) { 576 assert(ArchiveBuilder::current()->is_in_buffer_space(this), "must be"); 577 assert(tag_at(cp_index).is_klass(), "must be resolved"); 578 579 Klass* k = resolved_klass_at(cp_index); 580 bool can_archive; 581 582 if (k == nullptr) { 583 // We'd come here if the referenced class has been excluded via 584 // SystemDictionaryShared::is_excluded_class(). As a result, ArchiveBuilder 585 // has cleared the resolved_klasses()->at(...) pointer to null. Thus, we 586 // need to revert the tag to JVM_CONSTANT_UnresolvedClass. 587 can_archive = false; 588 } else { 589 ConstantPool* src_cp = ArchiveBuilder::current()->get_source_addr(this); 590 can_archive = AOTConstantPoolResolver::is_resolution_deterministic(src_cp, cp_index); 591 } 592 593 if (!can_archive) { 594 int resolved_klass_index = klass_slot_at(cp_index).resolved_klass_index(); 595 // This might be at a safepoint but do this in the right order. 596 tag_at_put(cp_index, JVM_CONSTANT_UnresolvedClass); 597 resolved_klasses()->at_put(resolved_klass_index, nullptr); 598 } 599 600 LogStreamHandle(Trace, cds, resolve) log; 601 if (log.is_enabled()) { 602 ResourceMark rm; 603 log.print("%s klass CP entry [%3d]: %s %s", 604 (can_archive ? "archived" : "reverted"), 605 cp_index, pool_holder()->name()->as_C_string(), get_type(pool_holder())); 606 if (can_archive) { 607 log.print(" => %s %s%s", k->name()->as_C_string(), get_type(k), 608 (!k->is_instance_klass() || pool_holder()->is_subtype_of(k)) ? "" : " (not supertype)"); 609 } else { 610 Symbol* name = klass_name_at(cp_index); 611 log.print(" => %s", name->as_C_string()); 612 } 613 } 614 615 ArchiveBuilder::alloc_stats()->record_klass_cp_entry(can_archive, /*reverted=*/!can_archive); 616 } 617 #endif // INCLUDE_CDS 618 619 int ConstantPool::cp_to_object_index(int cp_index) { 620 // this is harder don't do this so much. 621 int i = reference_map()->find(checked_cast<u2>(cp_index)); 622 // We might not find the index for jsr292 call. 623 return (i < 0) ? _no_index_sentinel : i; 624 } 625 626 void ConstantPool::string_at_put(int obj_index, oop str) { 627 oop result = set_resolved_reference_at(obj_index, str); 628 assert(result == nullptr || result == str, "Only set once or to the same string."); 629 } 630 631 void ConstantPool::trace_class_resolution(const constantPoolHandle& this_cp, Klass* k) { 632 ResourceMark rm; 633 int line_number = -1; 634 const char * source_file = nullptr; 635 if (JavaThread::current()->has_last_Java_frame()) { 636 // try to identify the method which called this function. 637 vframeStream vfst(JavaThread::current()); 638 if (!vfst.at_end()) { 639 line_number = vfst.method()->line_number_from_bci(vfst.bci()); 640 Symbol* s = vfst.method()->method_holder()->source_file_name(); 641 if (s != nullptr) { 642 source_file = s->as_C_string(); 643 } 644 } 645 } 646 if (k != this_cp->pool_holder()) { 647 // only print something if the classes are different 648 if (source_file != nullptr) { 649 log_debug(class, resolve)("%s %s %s:%d", 650 this_cp->pool_holder()->external_name(), 651 k->external_name(), source_file, line_number); 652 } else { 653 log_debug(class, resolve)("%s %s", 654 this_cp->pool_holder()->external_name(), 655 k->external_name()); 656 } 657 } 658 } 659 660 Klass* ConstantPool::klass_at_impl(const constantPoolHandle& this_cp, int cp_index, 661 TRAPS) { 662 JavaThread* javaThread = THREAD; 663 664 // A resolved constantPool entry will contain a Klass*, otherwise a Symbol*. 665 // It is not safe to rely on the tag bit's here, since we don't have a lock, and 666 // the entry and tag is not updated atomically. 667 CPKlassSlot kslot = this_cp->klass_slot_at(cp_index); 668 int resolved_klass_index = kslot.resolved_klass_index(); 669 int name_index = kslot.name_index(); 670 assert(this_cp->tag_at(name_index).is_symbol(), "sanity"); 671 672 // The tag must be JVM_CONSTANT_Class in order to read the correct value from 673 // the unresolved_klasses() array. 674 if (this_cp->tag_at(cp_index).is_klass()) { 675 Klass* klass = this_cp->resolved_klasses()->at(resolved_klass_index); 676 assert(klass != nullptr, "must be resolved"); 677 return klass; 678 } 679 680 // This tag doesn't change back to unresolved class unless at a safepoint. 681 if (this_cp->tag_at(cp_index).is_unresolved_klass_in_error()) { 682 // The original attempt to resolve this constant pool entry failed so find the 683 // class of the original error and throw another error of the same class 684 // (JVMS 5.4.3). 685 // If there is a detail message, pass that detail message to the error. 686 // The JVMS does not strictly require us to duplicate the same detail message, 687 // or any internal exception fields such as cause or stacktrace. But since the 688 // detail message is often a class name or other literal string, we will repeat it 689 // if we can find it in the symbol table. 690 throw_resolution_error(this_cp, cp_index, CHECK_NULL); 691 ShouldNotReachHere(); 692 } 693 694 HandleMark hm(THREAD); 695 Handle mirror_handle; 696 Symbol* name = this_cp->symbol_at(name_index); 697 Handle loader (THREAD, this_cp->pool_holder()->class_loader()); 698 699 Klass* k; 700 { 701 // Turn off the single stepping while doing class resolution 702 JvmtiHideSingleStepping jhss(javaThread); 703 k = SystemDictionary::resolve_or_fail(name, loader, true, THREAD); 704 } // JvmtiHideSingleStepping jhss(javaThread); 705 706 if (!HAS_PENDING_EXCEPTION) { 707 // preserve the resolved klass from unloading 708 mirror_handle = Handle(THREAD, k->java_mirror()); 709 // Do access check for klasses 710 verify_constant_pool_resolve(this_cp, k, THREAD); 711 } 712 713 // Failed to resolve class. We must record the errors so that subsequent attempts 714 // to resolve this constant pool entry fail with the same error (JVMS 5.4.3). 715 if (HAS_PENDING_EXCEPTION) { 716 save_and_throw_exception(this_cp, cp_index, constantTag(JVM_CONSTANT_UnresolvedClass), CHECK_NULL); 717 // If CHECK_NULL above doesn't return the exception, that means that 718 // some other thread has beaten us and has resolved the class. 719 // To preserve old behavior, we return the resolved class. 720 Klass* klass = this_cp->resolved_klasses()->at(resolved_klass_index); 721 assert(klass != nullptr, "must be resolved if exception was cleared"); 722 return klass; 723 } 724 725 // logging for class+resolve. 726 if (log_is_enabled(Debug, class, resolve)){ 727 trace_class_resolution(this_cp, k); 728 } 729 730 // The interpreter assumes when the tag is stored, the klass is resolved 731 // and the Klass* stored in _resolved_klasses is non-null, so we need 732 // hardware store ordering here. 733 // We also need to CAS to not overwrite an error from a racing thread. 734 Klass** adr = this_cp->resolved_klasses()->adr_at(resolved_klass_index); 735 Atomic::release_store(adr, k); 736 737 jbyte old_tag = Atomic::cmpxchg((jbyte*)this_cp->tag_addr_at(cp_index), 738 (jbyte)JVM_CONSTANT_UnresolvedClass, 739 (jbyte)JVM_CONSTANT_Class); 740 741 // We need to recheck exceptions from racing thread and return the same. 742 if (old_tag == JVM_CONSTANT_UnresolvedClassInError) { 743 // Remove klass. 744 Atomic::store(adr, (Klass*)nullptr); 745 throw_resolution_error(this_cp, cp_index, CHECK_NULL); 746 } 747 748 return k; 749 } 750 751 752 // Does not update ConstantPool* - to avoid any exception throwing. Used 753 // by compiler and exception handling. Also used to avoid classloads for 754 // instanceof operations. Returns null if the class has not been loaded or 755 // if the verification of constant pool failed 756 Klass* ConstantPool::klass_at_if_loaded(const constantPoolHandle& this_cp, int which) { 757 CPKlassSlot kslot = this_cp->klass_slot_at(which); 758 int resolved_klass_index = kslot.resolved_klass_index(); 759 int name_index = kslot.name_index(); 760 assert(this_cp->tag_at(name_index).is_symbol(), "sanity"); 761 762 if (this_cp->tag_at(which).is_klass()) { 763 Klass* k = this_cp->resolved_klasses()->at(resolved_klass_index); 764 assert(k != nullptr, "must be resolved"); 765 return k; 766 } else if (this_cp->tag_at(which).is_unresolved_klass_in_error()) { 767 return nullptr; 768 } else { 769 Thread* current = Thread::current(); 770 HandleMark hm(current); 771 Symbol* name = this_cp->symbol_at(name_index); 772 oop loader = this_cp->pool_holder()->class_loader(); 773 Handle h_loader (current, loader); 774 Klass* k = SystemDictionary::find_instance_klass(current, name, h_loader); 775 776 // Avoid constant pool verification at a safepoint, as it takes the Module_lock. 777 if (k != nullptr && current->is_Java_thread()) { 778 // Make sure that resolving is legal 779 JavaThread* THREAD = JavaThread::cast(current); // For exception macros. 780 ExceptionMark em(THREAD); 781 // return null if verification fails 782 verify_constant_pool_resolve(this_cp, k, THREAD); 783 if (HAS_PENDING_EXCEPTION) { 784 CLEAR_PENDING_EXCEPTION; 785 return nullptr; 786 } 787 return k; 788 } else { 789 return k; 790 } 791 } 792 } 793 794 Method* ConstantPool::method_at_if_loaded(const constantPoolHandle& cpool, 795 int which) { 796 if (cpool->cache() == nullptr) return nullptr; // nothing to load yet 797 if (!(which >= 0 && which < cpool->resolved_method_entries_length())) { 798 // FIXME: should be an assert 799 log_debug(class, resolve)("bad operand %d in:", which); cpool->print(); 800 return nullptr; 801 } 802 return cpool->cache()->method_if_resolved(which); 803 } 804 805 806 bool ConstantPool::has_appendix_at_if_loaded(const constantPoolHandle& cpool, int which, Bytecodes::Code code) { 807 if (cpool->cache() == nullptr) return false; // nothing to load yet 808 if (code == Bytecodes::_invokedynamic) { 809 return cpool->resolved_indy_entry_at(which)->has_appendix(); 810 } else { 811 return cpool->resolved_method_entry_at(which)->has_appendix(); 812 } 813 } 814 815 oop ConstantPool::appendix_at_if_loaded(const constantPoolHandle& cpool, int which, Bytecodes::Code code) { 816 if (cpool->cache() == nullptr) return nullptr; // nothing to load yet 817 if (code == Bytecodes::_invokedynamic) { 818 return cpool->resolved_reference_from_indy(which); 819 } else { 820 return cpool->cache()->appendix_if_resolved(which); 821 } 822 } 823 824 825 bool ConstantPool::has_local_signature_at_if_loaded(const constantPoolHandle& cpool, int which, Bytecodes::Code code) { 826 if (cpool->cache() == nullptr) return false; // nothing to load yet 827 if (code == Bytecodes::_invokedynamic) { 828 return cpool->resolved_indy_entry_at(which)->has_local_signature(); 829 } else { 830 return cpool->resolved_method_entry_at(which)->has_local_signature(); 831 } 832 } 833 834 // Translate index, which could be CPCache index or Indy index, to a constant pool index 835 int ConstantPool::to_cp_index(int index, Bytecodes::Code code) { 836 assert(cache() != nullptr, "'index' is a rewritten index so this class must have been rewritten"); 837 switch(code) { 838 case Bytecodes::_invokedynamic: 839 return invokedynamic_bootstrap_ref_index_at(index); 840 case Bytecodes::_getfield: 841 case Bytecodes::_getstatic: 842 case Bytecodes::_putfield: 843 case Bytecodes::_putstatic: 844 return resolved_field_entry_at(index)->constant_pool_index(); 845 case Bytecodes::_invokeinterface: 846 case Bytecodes::_invokehandle: 847 case Bytecodes::_invokespecial: 848 case Bytecodes::_invokestatic: 849 case Bytecodes::_invokevirtual: 850 case Bytecodes::_fast_invokevfinal: // Bytecode interpreter uses this 851 return resolved_method_entry_at(index)->constant_pool_index(); 852 default: 853 fatal("Unexpected bytecode: %s", Bytecodes::name(code)); 854 } 855 } 856 857 bool ConstantPool::is_resolved(int index, Bytecodes::Code code) { 858 assert(cache() != nullptr, "'index' is a rewritten index so this class must have been rewritten"); 859 switch(code) { 860 case Bytecodes::_invokedynamic: 861 return resolved_indy_entry_at(index)->is_resolved(); 862 863 case Bytecodes::_getfield: 864 case Bytecodes::_getstatic: 865 case Bytecodes::_putfield: 866 case Bytecodes::_putstatic: 867 return resolved_field_entry_at(index)->is_resolved(code); 868 869 case Bytecodes::_invokeinterface: 870 case Bytecodes::_invokehandle: 871 case Bytecodes::_invokespecial: 872 case Bytecodes::_invokestatic: 873 case Bytecodes::_invokevirtual: 874 case Bytecodes::_fast_invokevfinal: // Bytecode interpreter uses this 875 return resolved_method_entry_at(index)->is_resolved(code); 876 877 default: 878 fatal("Unexpected bytecode: %s", Bytecodes::name(code)); 879 } 880 } 881 882 u2 ConstantPool::uncached_name_and_type_ref_index_at(int cp_index) { 883 if (tag_at(cp_index).has_bootstrap()) { 884 u2 pool_index = bootstrap_name_and_type_ref_index_at(cp_index); 885 assert(tag_at(pool_index).is_name_and_type(), ""); 886 return pool_index; 887 } 888 assert(tag_at(cp_index).is_field_or_method(), "Corrupted constant pool"); 889 assert(!tag_at(cp_index).has_bootstrap(), "Must be handled above"); 890 jint ref_index = *int_at_addr(cp_index); 891 return extract_high_short_from_int(ref_index); 892 } 893 894 u2 ConstantPool::name_and_type_ref_index_at(int index, Bytecodes::Code code) { 895 return uncached_name_and_type_ref_index_at(to_cp_index(index, code)); 896 } 897 898 constantTag ConstantPool::tag_ref_at(int which, Bytecodes::Code code) { 899 // which may be either a Constant Pool index or a rewritten index 900 int pool_index = which; 901 assert(cache() != nullptr, "'index' is a rewritten index so this class must have been rewritten"); 902 pool_index = to_cp_index(which, code); 903 return tag_at(pool_index); 904 } 905 906 u2 ConstantPool::uncached_klass_ref_index_at(int cp_index) { 907 assert(tag_at(cp_index).is_field_or_method(), "Corrupted constant pool"); 908 jint ref_index = *int_at_addr(cp_index); 909 return extract_low_short_from_int(ref_index); 910 } 911 912 u2 ConstantPool::klass_ref_index_at(int index, Bytecodes::Code code) { 913 assert(code != Bytecodes::_invokedynamic, 914 "an invokedynamic instruction does not have a klass"); 915 return uncached_klass_ref_index_at(to_cp_index(index, code)); 916 } 917 918 void ConstantPool::verify_constant_pool_resolve(const constantPoolHandle& this_cp, Klass* k, TRAPS) { 919 if (!(k->is_instance_klass() || k->is_objArray_klass())) { 920 return; // short cut, typeArray klass is always accessible 921 } 922 Klass* holder = this_cp->pool_holder(); 923 LinkResolver::check_klass_accessibility(holder, k, CHECK); 924 } 925 926 927 u2 ConstantPool::name_ref_index_at(int cp_index) { 928 jint ref_index = name_and_type_at(cp_index); 929 return extract_low_short_from_int(ref_index); 930 } 931 932 933 u2 ConstantPool::signature_ref_index_at(int cp_index) { 934 jint ref_index = name_and_type_at(cp_index); 935 return extract_high_short_from_int(ref_index); 936 } 937 938 939 Klass* ConstantPool::klass_ref_at(int which, Bytecodes::Code code, TRAPS) { 940 return klass_at(klass_ref_index_at(which, code), THREAD); 941 } 942 943 Symbol* ConstantPool::klass_name_at(int cp_index) const { 944 return symbol_at(klass_slot_at(cp_index).name_index()); 945 } 946 947 Symbol* ConstantPool::klass_ref_at_noresolve(int which, Bytecodes::Code code) { 948 jint ref_index = klass_ref_index_at(which, code); 949 return klass_at_noresolve(ref_index); 950 } 951 952 Symbol* ConstantPool::uncached_klass_ref_at_noresolve(int cp_index) { 953 jint ref_index = uncached_klass_ref_index_at(cp_index); 954 return klass_at_noresolve(ref_index); 955 } 956 957 char* ConstantPool::string_at_noresolve(int cp_index) { 958 return unresolved_string_at(cp_index)->as_C_string(); 959 } 960 961 BasicType ConstantPool::basic_type_for_signature_at(int cp_index) const { 962 return Signature::basic_type(symbol_at(cp_index)); 963 } 964 965 966 void ConstantPool::resolve_string_constants_impl(const constantPoolHandle& this_cp, TRAPS) { 967 for (int index = 1; index < this_cp->length(); index++) { // Index 0 is unused 968 if (this_cp->tag_at(index).is_string()) { 969 this_cp->string_at(index, CHECK); 970 } 971 } 972 } 973 974 static const char* exception_message(const constantPoolHandle& this_cp, int which, constantTag tag, oop pending_exception) { 975 // Note: caller needs ResourceMark 976 977 // Dig out the detailed message to reuse if possible 978 const char* msg = java_lang_Throwable::message_as_utf8(pending_exception); 979 if (msg != nullptr) { 980 return msg; 981 } 982 983 Symbol* message = nullptr; 984 // Return specific message for the tag 985 switch (tag.value()) { 986 case JVM_CONSTANT_UnresolvedClass: 987 // return the class name in the error message 988 message = this_cp->klass_name_at(which); 989 break; 990 case JVM_CONSTANT_MethodHandle: 991 // return the method handle name in the error message 992 message = this_cp->method_handle_name_ref_at(which); 993 break; 994 case JVM_CONSTANT_MethodType: 995 // return the method type signature in the error message 996 message = this_cp->method_type_signature_at(which); 997 break; 998 case JVM_CONSTANT_Dynamic: 999 // return the name of the condy in the error message 1000 message = this_cp->uncached_name_ref_at(which); 1001 break; 1002 default: 1003 ShouldNotReachHere(); 1004 } 1005 1006 return message != nullptr ? message->as_C_string() : nullptr; 1007 } 1008 1009 static void add_resolution_error(JavaThread* current, const constantPoolHandle& this_cp, int which, 1010 constantTag tag, oop pending_exception) { 1011 1012 ResourceMark rm(current); 1013 Symbol* error = pending_exception->klass()->name(); 1014 oop cause = java_lang_Throwable::cause(pending_exception); 1015 1016 // Also dig out the exception cause, if present. 1017 Symbol* cause_sym = nullptr; 1018 const char* cause_msg = nullptr; 1019 if (cause != nullptr && cause != pending_exception) { 1020 cause_sym = cause->klass()->name(); 1021 cause_msg = java_lang_Throwable::message_as_utf8(cause); 1022 } 1023 1024 const char* message = exception_message(this_cp, which, tag, pending_exception); 1025 SystemDictionary::add_resolution_error(this_cp, which, error, message, cause_sym, cause_msg); 1026 } 1027 1028 1029 void ConstantPool::throw_resolution_error(const constantPoolHandle& this_cp, int which, TRAPS) { 1030 ResourceMark rm(THREAD); 1031 const char* message = nullptr; 1032 Symbol* cause = nullptr; 1033 const char* cause_msg = nullptr; 1034 Symbol* error = SystemDictionary::find_resolution_error(this_cp, which, &message, &cause, &cause_msg); 1035 assert(error != nullptr, "checking"); 1036 1037 CLEAR_PENDING_EXCEPTION; 1038 if (message != nullptr) { 1039 if (cause != nullptr) { 1040 Handle h_cause = Exceptions::new_exception(THREAD, cause, cause_msg); 1041 THROW_MSG_CAUSE(error, message, h_cause); 1042 } else { 1043 THROW_MSG(error, message); 1044 } 1045 } else { 1046 if (cause != nullptr) { 1047 Handle h_cause = Exceptions::new_exception(THREAD, cause, cause_msg); 1048 THROW_CAUSE(error, h_cause); 1049 } else { 1050 THROW(error); 1051 } 1052 } 1053 } 1054 1055 // If resolution for Class, Dynamic constant, MethodHandle or MethodType fails, save the 1056 // exception in the resolution error table, so that the same exception is thrown again. 1057 void ConstantPool::save_and_throw_exception(const constantPoolHandle& this_cp, int cp_index, 1058 constantTag tag, TRAPS) { 1059 1060 int error_tag = tag.error_value(); 1061 1062 if (!PENDING_EXCEPTION-> 1063 is_a(vmClasses::LinkageError_klass())) { 1064 // Just throw the exception and don't prevent these classes from 1065 // being loaded due to virtual machine errors like StackOverflow 1066 // and OutOfMemoryError, etc, or if the thread was hit by stop() 1067 // Needs clarification to section 5.4.3 of the VM spec (see 6308271) 1068 } else if (this_cp->tag_at(cp_index).value() != error_tag) { 1069 add_resolution_error(THREAD, this_cp, cp_index, tag, PENDING_EXCEPTION); 1070 // CAS in the tag. If a thread beat us to registering this error that's fine. 1071 // If another thread resolved the reference, this is a race condition. This 1072 // thread may have had a security manager or something temporary. 1073 // This doesn't deterministically get an error. So why do we save this? 1074 // We save this because jvmti can add classes to the bootclass path after 1075 // this error, so it needs to get the same error if the error is first. 1076 jbyte old_tag = Atomic::cmpxchg((jbyte*)this_cp->tag_addr_at(cp_index), 1077 (jbyte)tag.value(), 1078 (jbyte)error_tag); 1079 if (old_tag != error_tag && old_tag != tag.value()) { 1080 // MethodHandles and MethodType doesn't change to resolved version. 1081 assert(this_cp->tag_at(cp_index).is_klass(), "Wrong tag value"); 1082 // Forget the exception and use the resolved class. 1083 CLEAR_PENDING_EXCEPTION; 1084 } 1085 } else { 1086 // some other thread put this in error state 1087 throw_resolution_error(this_cp, cp_index, CHECK); 1088 } 1089 } 1090 1091 constantTag ConstantPool::constant_tag_at(int cp_index) { 1092 constantTag tag = tag_at(cp_index); 1093 if (tag.is_dynamic_constant()) { 1094 BasicType bt = basic_type_for_constant_at(cp_index); 1095 return constantTag(constantTag::type2tag(bt)); 1096 } 1097 return tag; 1098 } 1099 1100 BasicType ConstantPool::basic_type_for_constant_at(int cp_index) { 1101 constantTag tag = tag_at(cp_index); 1102 if (tag.is_dynamic_constant() || 1103 tag.is_dynamic_constant_in_error()) { 1104 // have to look at the signature for this one 1105 Symbol* constant_type = uncached_signature_ref_at(cp_index); 1106 return Signature::basic_type(constant_type); 1107 } 1108 return tag.basic_type(); 1109 } 1110 1111 // Called to resolve constants in the constant pool and return an oop. 1112 // Some constant pool entries cache their resolved oop. This is also 1113 // called to create oops from constants to use in arguments for invokedynamic 1114 oop ConstantPool::resolve_constant_at_impl(const constantPoolHandle& this_cp, 1115 int cp_index, int cache_index, 1116 bool* status_return, TRAPS) { 1117 oop result_oop = nullptr; 1118 1119 if (cache_index == _possible_index_sentinel) { 1120 // It is possible that this constant is one which is cached in the objects. 1121 // We'll do a linear search. This should be OK because this usage is rare. 1122 // FIXME: If bootstrap specifiers stress this code, consider putting in 1123 // a reverse index. Binary search over a short array should do it. 1124 assert(cp_index > 0, "valid constant pool index"); 1125 cache_index = this_cp->cp_to_object_index(cp_index); 1126 } 1127 assert(cache_index == _no_index_sentinel || cache_index >= 0, ""); 1128 assert(cp_index == _no_index_sentinel || cp_index >= 0, ""); 1129 1130 if (cache_index >= 0) { 1131 result_oop = this_cp->resolved_reference_at(cache_index); 1132 if (result_oop != nullptr) { 1133 if (result_oop == Universe::the_null_sentinel()) { 1134 DEBUG_ONLY(int temp_index = (cp_index >= 0 ? cp_index : this_cp->object_to_cp_index(cache_index))); 1135 assert(this_cp->tag_at(temp_index).is_dynamic_constant(), "only condy uses the null sentinel"); 1136 result_oop = nullptr; 1137 } 1138 if (status_return != nullptr) (*status_return) = true; 1139 return result_oop; 1140 // That was easy... 1141 } 1142 cp_index = this_cp->object_to_cp_index(cache_index); 1143 } 1144 1145 jvalue prim_value; // temp used only in a few cases below 1146 1147 constantTag tag = this_cp->tag_at(cp_index); 1148 1149 if (status_return != nullptr) { 1150 // don't trigger resolution if the constant might need it 1151 switch (tag.value()) { 1152 case JVM_CONSTANT_Class: 1153 assert(this_cp->resolved_klass_at(cp_index) != nullptr, "must be resolved"); 1154 break; 1155 case JVM_CONSTANT_String: 1156 case JVM_CONSTANT_Integer: 1157 case JVM_CONSTANT_Float: 1158 case JVM_CONSTANT_Long: 1159 case JVM_CONSTANT_Double: 1160 // these guys trigger OOM at worst 1161 break; 1162 default: 1163 (*status_return) = false; 1164 return nullptr; 1165 } 1166 // from now on there is either success or an OOME 1167 (*status_return) = true; 1168 } 1169 1170 switch (tag.value()) { 1171 1172 case JVM_CONSTANT_UnresolvedClass: 1173 case JVM_CONSTANT_Class: 1174 { 1175 assert(cache_index == _no_index_sentinel, "should not have been set"); 1176 Klass* resolved = klass_at_impl(this_cp, cp_index, CHECK_NULL); 1177 // ldc wants the java mirror. 1178 result_oop = resolved->java_mirror(); 1179 break; 1180 } 1181 1182 case JVM_CONSTANT_Dynamic: 1183 { PerfTraceTimedEvent timer(ClassLoader::perf_resolve_invokedynamic_time(), 1184 ClassLoader::perf_resolve_invokedynamic_count()); 1185 1186 // Resolve the Dynamically-Computed constant to invoke the BSM in order to obtain the resulting oop. 1187 BootstrapInfo bootstrap_specifier(this_cp, cp_index); 1188 1189 // The initial step in resolving an unresolved symbolic reference to a 1190 // dynamically-computed constant is to resolve the symbolic reference to a 1191 // method handle which will be the bootstrap method for the dynamically-computed 1192 // constant. If resolution of the java.lang.invoke.MethodHandle for the bootstrap 1193 // method fails, then a MethodHandleInError is stored at the corresponding 1194 // bootstrap method's CP index for the CONSTANT_MethodHandle_info. No need to 1195 // set a DynamicConstantInError here since any subsequent use of this 1196 // bootstrap method will encounter the resolution of MethodHandleInError. 1197 // Both the first, (resolution of the BSM and its static arguments), and the second tasks, 1198 // (invocation of the BSM), of JVMS Section 5.4.3.6 occur within invoke_bootstrap_method() 1199 // for the bootstrap_specifier created above. 1200 SystemDictionary::invoke_bootstrap_method(bootstrap_specifier, THREAD); 1201 Exceptions::wrap_dynamic_exception(/* is_indy */ false, THREAD); 1202 if (HAS_PENDING_EXCEPTION) { 1203 // Resolution failure of the dynamically-computed constant, save_and_throw_exception 1204 // will check for a LinkageError and store a DynamicConstantInError. 1205 save_and_throw_exception(this_cp, cp_index, tag, CHECK_NULL); 1206 } 1207 result_oop = bootstrap_specifier.resolved_value()(); 1208 BasicType type = Signature::basic_type(bootstrap_specifier.signature()); 1209 if (!is_reference_type(type)) { 1210 // Make sure the primitive value is properly boxed. 1211 // This is a JDK responsibility. 1212 const char* fail = nullptr; 1213 if (result_oop == nullptr) { 1214 fail = "null result instead of box"; 1215 } else if (!is_java_primitive(type)) { 1216 // FIXME: support value types via unboxing 1217 fail = "can only handle references and primitives"; 1218 } else if (!java_lang_boxing_object::is_instance(result_oop, type)) { 1219 fail = "primitive is not properly boxed"; 1220 } 1221 if (fail != nullptr) { 1222 // Since this exception is not a LinkageError, throw exception 1223 // but do not save a DynamicInError resolution result. 1224 // See section 5.4.3 of the VM spec. 1225 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), fail); 1226 } 1227 } 1228 1229 LogTarget(Debug, methodhandles, condy) lt_condy; 1230 if (lt_condy.is_enabled()) { 1231 LogStream ls(lt_condy); 1232 bootstrap_specifier.print_msg_on(&ls, "resolve_constant_at_impl"); 1233 } 1234 break; 1235 } 1236 1237 case JVM_CONSTANT_String: 1238 assert(cache_index != _no_index_sentinel, "should have been set"); 1239 result_oop = string_at_impl(this_cp, cp_index, cache_index, CHECK_NULL); 1240 break; 1241 1242 case JVM_CONSTANT_MethodHandle: 1243 { PerfTraceTimedEvent timer(ClassLoader::perf_resolve_method_handle_time(), 1244 ClassLoader::perf_resolve_method_handle_count()); 1245 1246 int ref_kind = this_cp->method_handle_ref_kind_at(cp_index); 1247 int callee_index = this_cp->method_handle_klass_index_at(cp_index); 1248 Symbol* name = this_cp->method_handle_name_ref_at(cp_index); 1249 Symbol* signature = this_cp->method_handle_signature_ref_at(cp_index); 1250 constantTag m_tag = this_cp->tag_at(this_cp->method_handle_index_at(cp_index)); 1251 { ResourceMark rm(THREAD); 1252 log_debug(class, resolve)("resolve JVM_CONSTANT_MethodHandle:%d [%d/%d/%d] %s.%s", 1253 ref_kind, cp_index, this_cp->method_handle_index_at(cp_index), 1254 callee_index, name->as_C_string(), signature->as_C_string()); 1255 } 1256 1257 Klass* callee = klass_at_impl(this_cp, callee_index, THREAD); 1258 if (HAS_PENDING_EXCEPTION) { 1259 save_and_throw_exception(this_cp, cp_index, tag, CHECK_NULL); 1260 } 1261 1262 // Check constant pool method consistency 1263 if ((callee->is_interface() && m_tag.is_method()) || 1264 (!callee->is_interface() && m_tag.is_interface_method())) { 1265 ResourceMark rm(THREAD); 1266 stringStream ss; 1267 ss.print("Inconsistent constant pool data in classfile for class %s. " 1268 "Method '", callee->name()->as_C_string()); 1269 signature->print_as_signature_external_return_type(&ss); 1270 ss.print(" %s(", name->as_C_string()); 1271 signature->print_as_signature_external_parameters(&ss); 1272 ss.print(")' at index %d is %s and should be %s", 1273 cp_index, 1274 callee->is_interface() ? "CONSTANT_MethodRef" : "CONSTANT_InterfaceMethodRef", 1275 callee->is_interface() ? "CONSTANT_InterfaceMethodRef" : "CONSTANT_MethodRef"); 1276 // Names are all known to be < 64k so we know this formatted message is not excessively large. 1277 Exceptions::fthrow(THREAD_AND_LOCATION, vmSymbols::java_lang_IncompatibleClassChangeError(), "%s", ss.as_string()); 1278 save_and_throw_exception(this_cp, cp_index, tag, CHECK_NULL); 1279 } 1280 1281 Klass* klass = this_cp->pool_holder(); 1282 HandleMark hm(THREAD); 1283 Handle value = SystemDictionary::link_method_handle_constant(klass, ref_kind, 1284 callee, name, signature, 1285 THREAD); 1286 if (HAS_PENDING_EXCEPTION) { 1287 save_and_throw_exception(this_cp, cp_index, tag, CHECK_NULL); 1288 } 1289 result_oop = value(); 1290 break; 1291 } 1292 1293 case JVM_CONSTANT_MethodType: 1294 { PerfTraceTimedEvent timer(ClassLoader::perf_resolve_method_type_time(), 1295 ClassLoader::perf_resolve_method_type_count()); 1296 1297 Symbol* signature = this_cp->method_type_signature_at(cp_index); 1298 { ResourceMark rm(THREAD); 1299 log_debug(class, resolve)("resolve JVM_CONSTANT_MethodType [%d/%d] %s", 1300 cp_index, this_cp->method_type_index_at(cp_index), 1301 signature->as_C_string()); 1302 } 1303 Klass* klass = this_cp->pool_holder(); 1304 HandleMark hm(THREAD); 1305 Handle value = SystemDictionary::find_method_handle_type(signature, klass, THREAD); 1306 result_oop = value(); 1307 if (HAS_PENDING_EXCEPTION) { 1308 save_and_throw_exception(this_cp, cp_index, tag, CHECK_NULL); 1309 } 1310 break; 1311 } 1312 1313 case JVM_CONSTANT_Integer: 1314 assert(cache_index == _no_index_sentinel, "should not have been set"); 1315 prim_value.i = this_cp->int_at(cp_index); 1316 result_oop = java_lang_boxing_object::create(T_INT, &prim_value, CHECK_NULL); 1317 break; 1318 1319 case JVM_CONSTANT_Float: 1320 assert(cache_index == _no_index_sentinel, "should not have been set"); 1321 prim_value.f = this_cp->float_at(cp_index); 1322 result_oop = java_lang_boxing_object::create(T_FLOAT, &prim_value, CHECK_NULL); 1323 break; 1324 1325 case JVM_CONSTANT_Long: 1326 assert(cache_index == _no_index_sentinel, "should not have been set"); 1327 prim_value.j = this_cp->long_at(cp_index); 1328 result_oop = java_lang_boxing_object::create(T_LONG, &prim_value, CHECK_NULL); 1329 break; 1330 1331 case JVM_CONSTANT_Double: 1332 assert(cache_index == _no_index_sentinel, "should not have been set"); 1333 prim_value.d = this_cp->double_at(cp_index); 1334 result_oop = java_lang_boxing_object::create(T_DOUBLE, &prim_value, CHECK_NULL); 1335 break; 1336 1337 case JVM_CONSTANT_UnresolvedClassInError: 1338 case JVM_CONSTANT_DynamicInError: 1339 case JVM_CONSTANT_MethodHandleInError: 1340 case JVM_CONSTANT_MethodTypeInError: 1341 throw_resolution_error(this_cp, cp_index, CHECK_NULL); 1342 break; 1343 1344 default: 1345 fatal("unexpected constant tag at CP %p[%d/%d] = %d", this_cp(), cp_index, cache_index, tag.value()); 1346 break; 1347 } 1348 1349 if (cache_index >= 0) { 1350 // Benign race condition: resolved_references may already be filled in. 1351 // The important thing here is that all threads pick up the same result. 1352 // It doesn't matter which racing thread wins, as long as only one 1353 // result is used by all threads, and all future queries. 1354 oop new_result = (result_oop == nullptr ? Universe::the_null_sentinel() : result_oop); 1355 oop old_result = this_cp->set_resolved_reference_at(cache_index, new_result); 1356 if (old_result == nullptr) { 1357 return result_oop; // was installed 1358 } else { 1359 // Return the winning thread's result. This can be different than 1360 // the result here for MethodHandles. 1361 if (old_result == Universe::the_null_sentinel()) 1362 old_result = nullptr; 1363 return old_result; 1364 } 1365 } else { 1366 assert(result_oop != Universe::the_null_sentinel(), ""); 1367 return result_oop; 1368 } 1369 } 1370 1371 oop ConstantPool::uncached_string_at(int cp_index, TRAPS) { 1372 Symbol* sym = unresolved_string_at(cp_index); 1373 oop str = StringTable::intern(sym, CHECK_(nullptr)); 1374 assert(java_lang_String::is_instance(str), "must be string"); 1375 return str; 1376 } 1377 1378 void ConstantPool::copy_bootstrap_arguments_at_impl(const constantPoolHandle& this_cp, int cp_index, 1379 int start_arg, int end_arg, 1380 objArrayHandle info, int pos, 1381 bool must_resolve, Handle if_not_available, 1382 TRAPS) { 1383 int limit = pos + end_arg - start_arg; 1384 // checks: cp_index in range [0..this_cp->length), 1385 // tag at cp_index, start..end in range [0..this_cp->bootstrap_argument_count], 1386 // info array non-null, pos..limit in [0..info.length] 1387 if ((0 >= cp_index || cp_index >= this_cp->length()) || 1388 !(this_cp->tag_at(cp_index).is_invoke_dynamic() || 1389 this_cp->tag_at(cp_index).is_dynamic_constant()) || 1390 (0 > start_arg || start_arg > end_arg) || 1391 (end_arg > this_cp->bootstrap_argument_count_at(cp_index)) || 1392 (0 > pos || pos > limit) || 1393 (info.is_null() || limit > info->length())) { 1394 // An index or something else went wrong; throw an error. 1395 // Since this is an internal API, we don't expect this, 1396 // so we don't bother to craft a nice message. 1397 THROW_MSG(vmSymbols::java_lang_LinkageError(), "bad BSM argument access"); 1398 } 1399 // now we can loop safely 1400 int info_i = pos; 1401 for (int i = start_arg; i < end_arg; i++) { 1402 int arg_index = this_cp->bootstrap_argument_index_at(cp_index, i); 1403 oop arg_oop; 1404 if (must_resolve) { 1405 arg_oop = this_cp->resolve_possibly_cached_constant_at(arg_index, CHECK); 1406 } else { 1407 bool found_it = false; 1408 arg_oop = this_cp->find_cached_constant_at(arg_index, found_it, CHECK); 1409 if (!found_it) arg_oop = if_not_available(); 1410 } 1411 info->obj_at_put(info_i++, arg_oop); 1412 } 1413 } 1414 1415 oop ConstantPool::string_at_impl(const constantPoolHandle& this_cp, int cp_index, int obj_index, TRAPS) { 1416 // If the string has already been interned, this entry will be non-null 1417 oop str = this_cp->resolved_reference_at(obj_index); 1418 assert(str != Universe::the_null_sentinel(), ""); 1419 if (str != nullptr) return str; 1420 Symbol* sym = this_cp->unresolved_string_at(cp_index); 1421 str = StringTable::intern(sym, CHECK_(nullptr)); 1422 this_cp->string_at_put(obj_index, str); 1423 assert(java_lang_String::is_instance(str), "must be string"); 1424 return str; 1425 } 1426 1427 1428 bool ConstantPool::klass_name_at_matches(const InstanceKlass* k, int cp_index) { 1429 // Names are interned, so we can compare Symbol*s directly 1430 Symbol* cp_name = klass_name_at(cp_index); 1431 return (cp_name == k->name()); 1432 } 1433 1434 1435 // Iterate over symbols and decrement ones which are Symbol*s 1436 // This is done during GC. 1437 // Only decrement the UTF8 symbols. Strings point to 1438 // these symbols but didn't increment the reference count. 1439 void ConstantPool::unreference_symbols() { 1440 for (int index = 1; index < length(); index++) { // Index 0 is unused 1441 constantTag tag = tag_at(index); 1442 if (tag.is_symbol()) { 1443 symbol_at(index)->decrement_refcount(); 1444 } 1445 } 1446 } 1447 1448 1449 // Compare this constant pool's entry at index1 to the constant pool 1450 // cp2's entry at index2. 1451 bool ConstantPool::compare_entry_to(int index1, const constantPoolHandle& cp2, 1452 int index2) { 1453 1454 // The error tags are equivalent to non-error tags when comparing 1455 jbyte t1 = tag_at(index1).non_error_value(); 1456 jbyte t2 = cp2->tag_at(index2).non_error_value(); 1457 1458 // Some classes are pre-resolved (like Throwable) which may lead to 1459 // consider it as a different entry. We then revert them back temporarily 1460 // to ensure proper comparison. 1461 if (t1 == JVM_CONSTANT_Class) { 1462 t1 = JVM_CONSTANT_UnresolvedClass; 1463 } 1464 if (t2 == JVM_CONSTANT_Class) { 1465 t2 = JVM_CONSTANT_UnresolvedClass; 1466 } 1467 1468 if (t1 != t2) { 1469 // Not the same entry type so there is nothing else to check. Note 1470 // that this style of checking will consider resolved/unresolved 1471 // class pairs as different. 1472 // From the ConstantPool* API point of view, this is correct 1473 // behavior. See VM_RedefineClasses::merge_constant_pools() to see how this 1474 // plays out in the context of ConstantPool* merging. 1475 return false; 1476 } 1477 1478 switch (t1) { 1479 case JVM_CONSTANT_ClassIndex: 1480 { 1481 int recur1 = klass_index_at(index1); 1482 int recur2 = cp2->klass_index_at(index2); 1483 if (compare_entry_to(recur1, cp2, recur2)) { 1484 return true; 1485 } 1486 } break; 1487 1488 case JVM_CONSTANT_Double: 1489 { 1490 jdouble d1 = double_at(index1); 1491 jdouble d2 = cp2->double_at(index2); 1492 if (d1 == d2) { 1493 return true; 1494 } 1495 } break; 1496 1497 case JVM_CONSTANT_Fieldref: 1498 case JVM_CONSTANT_InterfaceMethodref: 1499 case JVM_CONSTANT_Methodref: 1500 { 1501 int recur1 = uncached_klass_ref_index_at(index1); 1502 int recur2 = cp2->uncached_klass_ref_index_at(index2); 1503 bool match = compare_entry_to(recur1, cp2, recur2); 1504 if (match) { 1505 recur1 = uncached_name_and_type_ref_index_at(index1); 1506 recur2 = cp2->uncached_name_and_type_ref_index_at(index2); 1507 if (compare_entry_to(recur1, cp2, recur2)) { 1508 return true; 1509 } 1510 } 1511 } break; 1512 1513 case JVM_CONSTANT_Float: 1514 { 1515 jfloat f1 = float_at(index1); 1516 jfloat f2 = cp2->float_at(index2); 1517 if (f1 == f2) { 1518 return true; 1519 } 1520 } break; 1521 1522 case JVM_CONSTANT_Integer: 1523 { 1524 jint i1 = int_at(index1); 1525 jint i2 = cp2->int_at(index2); 1526 if (i1 == i2) { 1527 return true; 1528 } 1529 } break; 1530 1531 case JVM_CONSTANT_Long: 1532 { 1533 jlong l1 = long_at(index1); 1534 jlong l2 = cp2->long_at(index2); 1535 if (l1 == l2) { 1536 return true; 1537 } 1538 } break; 1539 1540 case JVM_CONSTANT_NameAndType: 1541 { 1542 int recur1 = name_ref_index_at(index1); 1543 int recur2 = cp2->name_ref_index_at(index2); 1544 if (compare_entry_to(recur1, cp2, recur2)) { 1545 recur1 = signature_ref_index_at(index1); 1546 recur2 = cp2->signature_ref_index_at(index2); 1547 if (compare_entry_to(recur1, cp2, recur2)) { 1548 return true; 1549 } 1550 } 1551 } break; 1552 1553 case JVM_CONSTANT_StringIndex: 1554 { 1555 int recur1 = string_index_at(index1); 1556 int recur2 = cp2->string_index_at(index2); 1557 if (compare_entry_to(recur1, cp2, recur2)) { 1558 return true; 1559 } 1560 } break; 1561 1562 case JVM_CONSTANT_UnresolvedClass: 1563 { 1564 Symbol* k1 = klass_name_at(index1); 1565 Symbol* k2 = cp2->klass_name_at(index2); 1566 if (k1 == k2) { 1567 return true; 1568 } 1569 } break; 1570 1571 case JVM_CONSTANT_MethodType: 1572 { 1573 int k1 = method_type_index_at(index1); 1574 int k2 = cp2->method_type_index_at(index2); 1575 if (compare_entry_to(k1, cp2, k2)) { 1576 return true; 1577 } 1578 } break; 1579 1580 case JVM_CONSTANT_MethodHandle: 1581 { 1582 int k1 = method_handle_ref_kind_at(index1); 1583 int k2 = cp2->method_handle_ref_kind_at(index2); 1584 if (k1 == k2) { 1585 int i1 = method_handle_index_at(index1); 1586 int i2 = cp2->method_handle_index_at(index2); 1587 if (compare_entry_to(i1, cp2, i2)) { 1588 return true; 1589 } 1590 } 1591 } break; 1592 1593 case JVM_CONSTANT_Dynamic: 1594 { 1595 int k1 = bootstrap_name_and_type_ref_index_at(index1); 1596 int k2 = cp2->bootstrap_name_and_type_ref_index_at(index2); 1597 int i1 = bootstrap_methods_attribute_index(index1); 1598 int i2 = cp2->bootstrap_methods_attribute_index(index2); 1599 bool match_entry = compare_entry_to(k1, cp2, k2); 1600 bool match_operand = compare_operand_to(i1, cp2, i2); 1601 return (match_entry && match_operand); 1602 } break; 1603 1604 case JVM_CONSTANT_InvokeDynamic: 1605 { 1606 int k1 = bootstrap_name_and_type_ref_index_at(index1); 1607 int k2 = cp2->bootstrap_name_and_type_ref_index_at(index2); 1608 int i1 = bootstrap_methods_attribute_index(index1); 1609 int i2 = cp2->bootstrap_methods_attribute_index(index2); 1610 bool match_entry = compare_entry_to(k1, cp2, k2); 1611 bool match_operand = compare_operand_to(i1, cp2, i2); 1612 return (match_entry && match_operand); 1613 } break; 1614 1615 case JVM_CONSTANT_String: 1616 { 1617 Symbol* s1 = unresolved_string_at(index1); 1618 Symbol* s2 = cp2->unresolved_string_at(index2); 1619 if (s1 == s2) { 1620 return true; 1621 } 1622 } break; 1623 1624 case JVM_CONSTANT_Utf8: 1625 { 1626 Symbol* s1 = symbol_at(index1); 1627 Symbol* s2 = cp2->symbol_at(index2); 1628 if (s1 == s2) { 1629 return true; 1630 } 1631 } break; 1632 1633 // Invalid is used as the tag for the second constant pool entry 1634 // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should 1635 // not be seen by itself. 1636 case JVM_CONSTANT_Invalid: // fall through 1637 1638 default: 1639 ShouldNotReachHere(); 1640 break; 1641 } 1642 1643 return false; 1644 } // end compare_entry_to() 1645 1646 1647 // Resize the operands array with delta_len and delta_size. 1648 // Used in RedefineClasses for CP merge. 1649 void ConstantPool::resize_operands(int delta_len, int delta_size, TRAPS) { 1650 int old_len = operand_array_length(operands()); 1651 int new_len = old_len + delta_len; 1652 int min_len = (delta_len > 0) ? old_len : new_len; 1653 1654 int old_size = operands()->length(); 1655 int new_size = old_size + delta_size; 1656 int min_size = (delta_size > 0) ? old_size : new_size; 1657 1658 ClassLoaderData* loader_data = pool_holder()->class_loader_data(); 1659 Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, new_size, CHECK); 1660 1661 // Set index in the resized array for existing elements only 1662 for (int idx = 0; idx < min_len; idx++) { 1663 int offset = operand_offset_at(idx); // offset in original array 1664 operand_offset_at_put(new_ops, idx, offset + 2*delta_len); // offset in resized array 1665 } 1666 // Copy the bootstrap specifiers only 1667 Copy::conjoint_memory_atomic(operands()->adr_at(2*old_len), 1668 new_ops->adr_at(2*new_len), 1669 (min_size - 2*min_len) * sizeof(u2)); 1670 // Explicitly deallocate old operands array. 1671 // Note, it is not needed for 7u backport. 1672 if ( operands() != nullptr) { // the safety check 1673 MetadataFactory::free_array<u2>(loader_data, operands()); 1674 } 1675 set_operands(new_ops); 1676 } // end resize_operands() 1677 1678 1679 // Extend the operands array with the length and size of the ext_cp operands. 1680 // Used in RedefineClasses for CP merge. 1681 void ConstantPool::extend_operands(const constantPoolHandle& ext_cp, TRAPS) { 1682 int delta_len = operand_array_length(ext_cp->operands()); 1683 if (delta_len == 0) { 1684 return; // nothing to do 1685 } 1686 int delta_size = ext_cp->operands()->length(); 1687 1688 assert(delta_len > 0 && delta_size > 0, "extended operands array must be bigger"); 1689 1690 if (operand_array_length(operands()) == 0) { 1691 ClassLoaderData* loader_data = pool_holder()->class_loader_data(); 1692 Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, delta_size, CHECK); 1693 // The first element index defines the offset of second part 1694 operand_offset_at_put(new_ops, 0, 2*delta_len); // offset in new array 1695 set_operands(new_ops); 1696 } else { 1697 resize_operands(delta_len, delta_size, CHECK); 1698 } 1699 1700 } // end extend_operands() 1701 1702 1703 // Shrink the operands array to a smaller array with new_len length. 1704 // Used in RedefineClasses for CP merge. 1705 void ConstantPool::shrink_operands(int new_len, TRAPS) { 1706 int old_len = operand_array_length(operands()); 1707 if (new_len == old_len) { 1708 return; // nothing to do 1709 } 1710 assert(new_len < old_len, "shrunken operands array must be smaller"); 1711 1712 int free_base = operand_next_offset_at(new_len - 1); 1713 int delta_len = new_len - old_len; 1714 int delta_size = 2*delta_len + free_base - operands()->length(); 1715 1716 resize_operands(delta_len, delta_size, CHECK); 1717 1718 } // end shrink_operands() 1719 1720 1721 void ConstantPool::copy_operands(const constantPoolHandle& from_cp, 1722 const constantPoolHandle& to_cp, 1723 TRAPS) { 1724 1725 int from_oplen = operand_array_length(from_cp->operands()); 1726 int old_oplen = operand_array_length(to_cp->operands()); 1727 if (from_oplen != 0) { 1728 ClassLoaderData* loader_data = to_cp->pool_holder()->class_loader_data(); 1729 // append my operands to the target's operands array 1730 if (old_oplen == 0) { 1731 // Can't just reuse from_cp's operand list because of deallocation issues 1732 int len = from_cp->operands()->length(); 1733 Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, len, CHECK); 1734 Copy::conjoint_memory_atomic( 1735 from_cp->operands()->adr_at(0), new_ops->adr_at(0), len * sizeof(u2)); 1736 to_cp->set_operands(new_ops); 1737 } else { 1738 int old_len = to_cp->operands()->length(); 1739 int from_len = from_cp->operands()->length(); 1740 int old_off = old_oplen * sizeof(u2); 1741 int from_off = from_oplen * sizeof(u2); 1742 // Use the metaspace for the destination constant pool 1743 Array<u2>* new_operands = MetadataFactory::new_array<u2>(loader_data, old_len + from_len, CHECK); 1744 int fillp = 0, len = 0; 1745 // first part of dest 1746 Copy::conjoint_memory_atomic(to_cp->operands()->adr_at(0), 1747 new_operands->adr_at(fillp), 1748 (len = old_off) * sizeof(u2)); 1749 fillp += len; 1750 // first part of src 1751 Copy::conjoint_memory_atomic(from_cp->operands()->adr_at(0), 1752 new_operands->adr_at(fillp), 1753 (len = from_off) * sizeof(u2)); 1754 fillp += len; 1755 // second part of dest 1756 Copy::conjoint_memory_atomic(to_cp->operands()->adr_at(old_off), 1757 new_operands->adr_at(fillp), 1758 (len = old_len - old_off) * sizeof(u2)); 1759 fillp += len; 1760 // second part of src 1761 Copy::conjoint_memory_atomic(from_cp->operands()->adr_at(from_off), 1762 new_operands->adr_at(fillp), 1763 (len = from_len - from_off) * sizeof(u2)); 1764 fillp += len; 1765 assert(fillp == new_operands->length(), ""); 1766 1767 // Adjust indexes in the first part of the copied operands array. 1768 for (int j = 0; j < from_oplen; j++) { 1769 int offset = operand_offset_at(new_operands, old_oplen + j); 1770 assert(offset == operand_offset_at(from_cp->operands(), j), "correct copy"); 1771 offset += old_len; // every new tuple is preceded by old_len extra u2's 1772 operand_offset_at_put(new_operands, old_oplen + j, offset); 1773 } 1774 1775 // replace target operands array with combined array 1776 to_cp->set_operands(new_operands); 1777 } 1778 } 1779 } // end copy_operands() 1780 1781 1782 // Copy this constant pool's entries at start_i to end_i (inclusive) 1783 // to the constant pool to_cp's entries starting at to_i. A total of 1784 // (end_i - start_i) + 1 entries are copied. 1785 void ConstantPool::copy_cp_to_impl(const constantPoolHandle& from_cp, int start_i, int end_i, 1786 const constantPoolHandle& to_cp, int to_i, TRAPS) { 1787 1788 1789 int dest_cpi = to_i; // leave original alone for debug purposes 1790 1791 for (int src_cpi = start_i; src_cpi <= end_i; /* see loop bottom */ ) { 1792 copy_entry_to(from_cp, src_cpi, to_cp, dest_cpi); 1793 1794 switch (from_cp->tag_at(src_cpi).value()) { 1795 case JVM_CONSTANT_Double: 1796 case JVM_CONSTANT_Long: 1797 // double and long take two constant pool entries 1798 src_cpi += 2; 1799 dest_cpi += 2; 1800 break; 1801 1802 default: 1803 // all others take one constant pool entry 1804 src_cpi++; 1805 dest_cpi++; 1806 break; 1807 } 1808 } 1809 copy_operands(from_cp, to_cp, CHECK); 1810 1811 } // end copy_cp_to_impl() 1812 1813 1814 // Copy this constant pool's entry at from_i to the constant pool 1815 // to_cp's entry at to_i. 1816 void ConstantPool::copy_entry_to(const constantPoolHandle& from_cp, int from_i, 1817 const constantPoolHandle& to_cp, int to_i) { 1818 1819 int tag = from_cp->tag_at(from_i).value(); 1820 switch (tag) { 1821 case JVM_CONSTANT_ClassIndex: 1822 { 1823 jint ki = from_cp->klass_index_at(from_i); 1824 to_cp->klass_index_at_put(to_i, ki); 1825 } break; 1826 1827 case JVM_CONSTANT_Double: 1828 { 1829 jdouble d = from_cp->double_at(from_i); 1830 to_cp->double_at_put(to_i, d); 1831 // double takes two constant pool entries so init second entry's tag 1832 to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid); 1833 } break; 1834 1835 case JVM_CONSTANT_Fieldref: 1836 { 1837 int class_index = from_cp->uncached_klass_ref_index_at(from_i); 1838 int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i); 1839 to_cp->field_at_put(to_i, class_index, name_and_type_index); 1840 } break; 1841 1842 case JVM_CONSTANT_Float: 1843 { 1844 jfloat f = from_cp->float_at(from_i); 1845 to_cp->float_at_put(to_i, f); 1846 } break; 1847 1848 case JVM_CONSTANT_Integer: 1849 { 1850 jint i = from_cp->int_at(from_i); 1851 to_cp->int_at_put(to_i, i); 1852 } break; 1853 1854 case JVM_CONSTANT_InterfaceMethodref: 1855 { 1856 int class_index = from_cp->uncached_klass_ref_index_at(from_i); 1857 int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i); 1858 to_cp->interface_method_at_put(to_i, class_index, name_and_type_index); 1859 } break; 1860 1861 case JVM_CONSTANT_Long: 1862 { 1863 jlong l = from_cp->long_at(from_i); 1864 to_cp->long_at_put(to_i, l); 1865 // long takes two constant pool entries so init second entry's tag 1866 to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid); 1867 } break; 1868 1869 case JVM_CONSTANT_Methodref: 1870 { 1871 int class_index = from_cp->uncached_klass_ref_index_at(from_i); 1872 int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i); 1873 to_cp->method_at_put(to_i, class_index, name_and_type_index); 1874 } break; 1875 1876 case JVM_CONSTANT_NameAndType: 1877 { 1878 int name_ref_index = from_cp->name_ref_index_at(from_i); 1879 int signature_ref_index = from_cp->signature_ref_index_at(from_i); 1880 to_cp->name_and_type_at_put(to_i, name_ref_index, signature_ref_index); 1881 } break; 1882 1883 case JVM_CONSTANT_StringIndex: 1884 { 1885 jint si = from_cp->string_index_at(from_i); 1886 to_cp->string_index_at_put(to_i, si); 1887 } break; 1888 1889 case JVM_CONSTANT_Class: 1890 case JVM_CONSTANT_UnresolvedClass: 1891 case JVM_CONSTANT_UnresolvedClassInError: 1892 { 1893 // Revert to JVM_CONSTANT_ClassIndex 1894 int name_index = from_cp->klass_slot_at(from_i).name_index(); 1895 assert(from_cp->tag_at(name_index).is_symbol(), "sanity"); 1896 to_cp->klass_index_at_put(to_i, name_index); 1897 } break; 1898 1899 case JVM_CONSTANT_String: 1900 { 1901 Symbol* s = from_cp->unresolved_string_at(from_i); 1902 to_cp->unresolved_string_at_put(to_i, s); 1903 } break; 1904 1905 case JVM_CONSTANT_Utf8: 1906 { 1907 Symbol* s = from_cp->symbol_at(from_i); 1908 // Need to increase refcount, the old one will be thrown away and deferenced 1909 s->increment_refcount(); 1910 to_cp->symbol_at_put(to_i, s); 1911 } break; 1912 1913 case JVM_CONSTANT_MethodType: 1914 case JVM_CONSTANT_MethodTypeInError: 1915 { 1916 jint k = from_cp->method_type_index_at(from_i); 1917 to_cp->method_type_index_at_put(to_i, k); 1918 } break; 1919 1920 case JVM_CONSTANT_MethodHandle: 1921 case JVM_CONSTANT_MethodHandleInError: 1922 { 1923 int k1 = from_cp->method_handle_ref_kind_at(from_i); 1924 int k2 = from_cp->method_handle_index_at(from_i); 1925 to_cp->method_handle_index_at_put(to_i, k1, k2); 1926 } break; 1927 1928 case JVM_CONSTANT_Dynamic: 1929 case JVM_CONSTANT_DynamicInError: 1930 { 1931 int k1 = from_cp->bootstrap_methods_attribute_index(from_i); 1932 int k2 = from_cp->bootstrap_name_and_type_ref_index_at(from_i); 1933 k1 += operand_array_length(to_cp->operands()); // to_cp might already have operands 1934 to_cp->dynamic_constant_at_put(to_i, k1, k2); 1935 } break; 1936 1937 case JVM_CONSTANT_InvokeDynamic: 1938 { 1939 int k1 = from_cp->bootstrap_methods_attribute_index(from_i); 1940 int k2 = from_cp->bootstrap_name_and_type_ref_index_at(from_i); 1941 k1 += operand_array_length(to_cp->operands()); // to_cp might already have operands 1942 to_cp->invoke_dynamic_at_put(to_i, k1, k2); 1943 } break; 1944 1945 // Invalid is used as the tag for the second constant pool entry 1946 // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should 1947 // not be seen by itself. 1948 case JVM_CONSTANT_Invalid: // fall through 1949 1950 default: 1951 { 1952 ShouldNotReachHere(); 1953 } break; 1954 } 1955 } // end copy_entry_to() 1956 1957 // Search constant pool search_cp for an entry that matches this 1958 // constant pool's entry at pattern_i. Returns the index of a 1959 // matching entry or zero (0) if there is no matching entry. 1960 int ConstantPool::find_matching_entry(int pattern_i, 1961 const constantPoolHandle& search_cp) { 1962 1963 // index zero (0) is not used 1964 for (int i = 1; i < search_cp->length(); i++) { 1965 bool found = compare_entry_to(pattern_i, search_cp, i); 1966 if (found) { 1967 return i; 1968 } 1969 } 1970 1971 return 0; // entry not found; return unused index zero (0) 1972 } // end find_matching_entry() 1973 1974 1975 // Compare this constant pool's bootstrap specifier at idx1 to the constant pool 1976 // cp2's bootstrap specifier at idx2. 1977 bool ConstantPool::compare_operand_to(int idx1, const constantPoolHandle& cp2, int idx2) { 1978 int k1 = operand_bootstrap_method_ref_index_at(idx1); 1979 int k2 = cp2->operand_bootstrap_method_ref_index_at(idx2); 1980 bool match = compare_entry_to(k1, cp2, k2); 1981 1982 if (!match) { 1983 return false; 1984 } 1985 int argc = operand_argument_count_at(idx1); 1986 if (argc == cp2->operand_argument_count_at(idx2)) { 1987 for (int j = 0; j < argc; j++) { 1988 k1 = operand_argument_index_at(idx1, j); 1989 k2 = cp2->operand_argument_index_at(idx2, j); 1990 match = compare_entry_to(k1, cp2, k2); 1991 if (!match) { 1992 return false; 1993 } 1994 } 1995 return true; // got through loop; all elements equal 1996 } 1997 return false; 1998 } // end compare_operand_to() 1999 2000 // Search constant pool search_cp for a bootstrap specifier that matches 2001 // this constant pool's bootstrap specifier data at pattern_i index. 2002 // Return the index of a matching bootstrap attribute record or (-1) if there is no match. 2003 int ConstantPool::find_matching_operand(int pattern_i, 2004 const constantPoolHandle& search_cp, int search_len) { 2005 for (int i = 0; i < search_len; i++) { 2006 bool found = compare_operand_to(pattern_i, search_cp, i); 2007 if (found) { 2008 return i; 2009 } 2010 } 2011 return -1; // bootstrap specifier data not found; return unused index (-1) 2012 } // end find_matching_operand() 2013 2014 2015 #ifndef PRODUCT 2016 2017 const char* ConstantPool::printable_name_at(int cp_index) { 2018 2019 constantTag tag = tag_at(cp_index); 2020 2021 if (tag.is_string()) { 2022 return string_at_noresolve(cp_index); 2023 } else if (tag.is_klass() || tag.is_unresolved_klass()) { 2024 return klass_name_at(cp_index)->as_C_string(); 2025 } else if (tag.is_symbol()) { 2026 return symbol_at(cp_index)->as_C_string(); 2027 } 2028 return ""; 2029 } 2030 2031 #endif // PRODUCT 2032 2033 2034 // Returns size of constant pool entry. 2035 jint ConstantPool::cpool_entry_size(jint idx) { 2036 switch(tag_at(idx).value()) { 2037 case JVM_CONSTANT_Invalid: 2038 case JVM_CONSTANT_Unicode: 2039 return 1; 2040 2041 case JVM_CONSTANT_Utf8: 2042 return 3 + symbol_at(idx)->utf8_length(); 2043 2044 case JVM_CONSTANT_Class: 2045 case JVM_CONSTANT_String: 2046 case JVM_CONSTANT_ClassIndex: 2047 case JVM_CONSTANT_UnresolvedClass: 2048 case JVM_CONSTANT_UnresolvedClassInError: 2049 case JVM_CONSTANT_StringIndex: 2050 case JVM_CONSTANT_MethodType: 2051 case JVM_CONSTANT_MethodTypeInError: 2052 return 3; 2053 2054 case JVM_CONSTANT_MethodHandle: 2055 case JVM_CONSTANT_MethodHandleInError: 2056 return 4; //tag, ref_kind, ref_index 2057 2058 case JVM_CONSTANT_Integer: 2059 case JVM_CONSTANT_Float: 2060 case JVM_CONSTANT_Fieldref: 2061 case JVM_CONSTANT_Methodref: 2062 case JVM_CONSTANT_InterfaceMethodref: 2063 case JVM_CONSTANT_NameAndType: 2064 return 5; 2065 2066 case JVM_CONSTANT_Dynamic: 2067 case JVM_CONSTANT_DynamicInError: 2068 case JVM_CONSTANT_InvokeDynamic: 2069 // u1 tag, u2 bsm, u2 nt 2070 return 5; 2071 2072 case JVM_CONSTANT_Long: 2073 case JVM_CONSTANT_Double: 2074 return 9; 2075 } 2076 assert(false, "cpool_entry_size: Invalid constant pool entry tag"); 2077 return 1; 2078 } /* end cpool_entry_size */ 2079 2080 2081 // SymbolHash is used to find a constant pool index from a string. 2082 // This function fills in SymbolHashs, one for utf8s and one for 2083 // class names, returns size of the cpool raw bytes. 2084 jint ConstantPool::hash_entries_to(SymbolHash *symmap, 2085 SymbolHash *classmap) { 2086 jint size = 0; 2087 2088 for (u2 idx = 1; idx < length(); idx++) { 2089 u2 tag = tag_at(idx).value(); 2090 size += cpool_entry_size(idx); 2091 2092 switch(tag) { 2093 case JVM_CONSTANT_Utf8: { 2094 Symbol* sym = symbol_at(idx); 2095 symmap->add_if_absent(sym, idx); 2096 break; 2097 } 2098 case JVM_CONSTANT_Class: 2099 case JVM_CONSTANT_UnresolvedClass: 2100 case JVM_CONSTANT_UnresolvedClassInError: { 2101 Symbol* sym = klass_name_at(idx); 2102 classmap->add_if_absent(sym, idx); 2103 break; 2104 } 2105 case JVM_CONSTANT_Long: 2106 case JVM_CONSTANT_Double: { 2107 idx++; // Both Long and Double take two cpool slots 2108 break; 2109 } 2110 } 2111 } 2112 return size; 2113 } /* end hash_utf8_entries_to */ 2114 2115 2116 // Copy cpool bytes. 2117 // Returns: 2118 // 0, in case of OutOfMemoryError 2119 // -1, in case of internal error 2120 // > 0, count of the raw cpool bytes that have been copied 2121 int ConstantPool::copy_cpool_bytes(int cpool_size, 2122 SymbolHash* tbl, 2123 unsigned char *bytes) { 2124 u2 idx1, idx2; 2125 jint size = 0; 2126 jint cnt = length(); 2127 unsigned char *start_bytes = bytes; 2128 2129 for (jint idx = 1; idx < cnt; idx++) { 2130 u1 tag = tag_at(idx).value(); 2131 jint ent_size = cpool_entry_size(idx); 2132 2133 assert(size + ent_size <= cpool_size, "Size mismatch"); 2134 2135 *bytes = tag; 2136 switch(tag) { 2137 case JVM_CONSTANT_Invalid: { 2138 break; 2139 } 2140 case JVM_CONSTANT_Unicode: { 2141 assert(false, "Wrong constant pool tag: JVM_CONSTANT_Unicode"); 2142 break; 2143 } 2144 case JVM_CONSTANT_Utf8: { 2145 Symbol* sym = symbol_at(idx); 2146 char* str = sym->as_utf8(); 2147 // Warning! It's crashing on x86 with len = sym->utf8_length() 2148 int len = (int) strlen(str); 2149 Bytes::put_Java_u2((address) (bytes+1), (u2) len); 2150 for (int i = 0; i < len; i++) { 2151 bytes[3+i] = (u1) str[i]; 2152 } 2153 break; 2154 } 2155 case JVM_CONSTANT_Integer: { 2156 jint val = int_at(idx); 2157 Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val); 2158 break; 2159 } 2160 case JVM_CONSTANT_Float: { 2161 jfloat val = float_at(idx); 2162 Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val); 2163 break; 2164 } 2165 case JVM_CONSTANT_Long: { 2166 jlong val = long_at(idx); 2167 Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val); 2168 idx++; // Long takes two cpool slots 2169 break; 2170 } 2171 case JVM_CONSTANT_Double: { 2172 jdouble val = double_at(idx); 2173 Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val); 2174 idx++; // Double takes two cpool slots 2175 break; 2176 } 2177 case JVM_CONSTANT_Class: 2178 case JVM_CONSTANT_UnresolvedClass: 2179 case JVM_CONSTANT_UnresolvedClassInError: { 2180 *bytes = JVM_CONSTANT_Class; 2181 Symbol* sym = klass_name_at(idx); 2182 idx1 = tbl->symbol_to_value(sym); 2183 assert(idx1 != 0, "Have not found a hashtable entry"); 2184 Bytes::put_Java_u2((address) (bytes+1), idx1); 2185 break; 2186 } 2187 case JVM_CONSTANT_String: { 2188 *bytes = JVM_CONSTANT_String; 2189 Symbol* sym = unresolved_string_at(idx); 2190 idx1 = tbl->symbol_to_value(sym); 2191 assert(idx1 != 0, "Have not found a hashtable entry"); 2192 Bytes::put_Java_u2((address) (bytes+1), idx1); 2193 break; 2194 } 2195 case JVM_CONSTANT_Fieldref: 2196 case JVM_CONSTANT_Methodref: 2197 case JVM_CONSTANT_InterfaceMethodref: { 2198 idx1 = uncached_klass_ref_index_at(idx); 2199 idx2 = uncached_name_and_type_ref_index_at(idx); 2200 Bytes::put_Java_u2((address) (bytes+1), idx1); 2201 Bytes::put_Java_u2((address) (bytes+3), idx2); 2202 break; 2203 } 2204 case JVM_CONSTANT_NameAndType: { 2205 idx1 = name_ref_index_at(idx); 2206 idx2 = signature_ref_index_at(idx); 2207 Bytes::put_Java_u2((address) (bytes+1), idx1); 2208 Bytes::put_Java_u2((address) (bytes+3), idx2); 2209 break; 2210 } 2211 case JVM_CONSTANT_ClassIndex: { 2212 *bytes = JVM_CONSTANT_Class; 2213 idx1 = checked_cast<u2>(klass_index_at(idx)); 2214 Bytes::put_Java_u2((address) (bytes+1), idx1); 2215 break; 2216 } 2217 case JVM_CONSTANT_StringIndex: { 2218 *bytes = JVM_CONSTANT_String; 2219 idx1 = checked_cast<u2>(string_index_at(idx)); 2220 Bytes::put_Java_u2((address) (bytes+1), idx1); 2221 break; 2222 } 2223 case JVM_CONSTANT_MethodHandle: 2224 case JVM_CONSTANT_MethodHandleInError: { 2225 *bytes = JVM_CONSTANT_MethodHandle; 2226 int kind = method_handle_ref_kind_at(idx); 2227 idx1 = checked_cast<u2>(method_handle_index_at(idx)); 2228 *(bytes+1) = (unsigned char) kind; 2229 Bytes::put_Java_u2((address) (bytes+2), idx1); 2230 break; 2231 } 2232 case JVM_CONSTANT_MethodType: 2233 case JVM_CONSTANT_MethodTypeInError: { 2234 *bytes = JVM_CONSTANT_MethodType; 2235 idx1 = checked_cast<u2>(method_type_index_at(idx)); 2236 Bytes::put_Java_u2((address) (bytes+1), idx1); 2237 break; 2238 } 2239 case JVM_CONSTANT_Dynamic: 2240 case JVM_CONSTANT_DynamicInError: { 2241 *bytes = tag; 2242 idx1 = extract_low_short_from_int(*int_at_addr(idx)); 2243 idx2 = extract_high_short_from_int(*int_at_addr(idx)); 2244 assert(idx2 == bootstrap_name_and_type_ref_index_at(idx), "correct half of u4"); 2245 Bytes::put_Java_u2((address) (bytes+1), idx1); 2246 Bytes::put_Java_u2((address) (bytes+3), idx2); 2247 break; 2248 } 2249 case JVM_CONSTANT_InvokeDynamic: { 2250 *bytes = tag; 2251 idx1 = extract_low_short_from_int(*int_at_addr(idx)); 2252 idx2 = extract_high_short_from_int(*int_at_addr(idx)); 2253 assert(idx2 == bootstrap_name_and_type_ref_index_at(idx), "correct half of u4"); 2254 Bytes::put_Java_u2((address) (bytes+1), idx1); 2255 Bytes::put_Java_u2((address) (bytes+3), idx2); 2256 break; 2257 } 2258 } 2259 bytes += ent_size; 2260 size += ent_size; 2261 } 2262 assert(size == cpool_size, "Size mismatch"); 2263 2264 return (int)(bytes - start_bytes); 2265 } /* end copy_cpool_bytes */ 2266 2267 bool ConstantPool::is_maybe_on_stack() const { 2268 // This method uses the similar logic as nmethod::is_maybe_on_stack() 2269 if (!Continuations::enabled()) { 2270 return false; 2271 } 2272 2273 // If the condition below is true, it means that the nmethod was found to 2274 // be alive the previous completed marking cycle. 2275 return cache()->gc_epoch() >= CodeCache::previous_completed_gc_marking_cycle(); 2276 } 2277 2278 // For redefinition, if any methods found in loom stack chunks, the gc_epoch is 2279 // recorded in their constant pool cache. The on_stack-ness of the constant pool controls whether 2280 // memory for the method is reclaimed. 2281 bool ConstantPool::on_stack() const { 2282 if ((_flags &_on_stack) != 0) { 2283 return true; 2284 } 2285 2286 if (_cache == nullptr) { 2287 return false; 2288 } 2289 2290 return is_maybe_on_stack(); 2291 } 2292 2293 void ConstantPool::set_on_stack(const bool value) { 2294 if (value) { 2295 // Only record if it's not already set. 2296 if (!on_stack()) { 2297 assert(!is_shared(), "should always be set for shared constant pools"); 2298 _flags |= _on_stack; 2299 MetadataOnStackMark::record(this); 2300 } 2301 } else { 2302 // Clearing is done single-threadedly. 2303 if (!is_shared()) { 2304 _flags &= (u2)(~_on_stack); 2305 } 2306 } 2307 } 2308 2309 // Printing 2310 2311 void ConstantPool::print_on(outputStream* st) const { 2312 assert(is_constantPool(), "must be constantPool"); 2313 st->print_cr("%s", internal_name()); 2314 if (flags() != 0) { 2315 st->print(" - flags: 0x%x", flags()); 2316 if (has_preresolution()) st->print(" has_preresolution"); 2317 if (on_stack()) st->print(" on_stack"); 2318 st->cr(); 2319 } 2320 if (pool_holder() != nullptr) { 2321 st->print_cr(" - holder: " PTR_FORMAT, p2i(pool_holder())); 2322 } 2323 st->print_cr(" - cache: " PTR_FORMAT, p2i(cache())); 2324 st->print_cr(" - resolved_references: " PTR_FORMAT, p2i(resolved_references_or_null())); 2325 st->print_cr(" - reference_map: " PTR_FORMAT, p2i(reference_map())); 2326 st->print_cr(" - resolved_klasses: " PTR_FORMAT, p2i(resolved_klasses())); 2327 st->print_cr(" - cp length: %d", length()); 2328 2329 for (int index = 1; index < length(); index++) { // Index 0 is unused 2330 ((ConstantPool*)this)->print_entry_on(index, st); 2331 switch (tag_at(index).value()) { 2332 case JVM_CONSTANT_Long : 2333 case JVM_CONSTANT_Double : 2334 index++; // Skip entry following eigth-byte constant 2335 } 2336 2337 } 2338 st->cr(); 2339 } 2340 2341 // Print one constant pool entry 2342 void ConstantPool::print_entry_on(const int cp_index, outputStream* st) { 2343 EXCEPTION_MARK; 2344 st->print(" - %3d : ", cp_index); 2345 tag_at(cp_index).print_on(st); 2346 st->print(" : "); 2347 switch (tag_at(cp_index).value()) { 2348 case JVM_CONSTANT_Class : 2349 { Klass* k = klass_at(cp_index, CATCH); 2350 guarantee(k != nullptr, "need klass"); 2351 k->print_value_on(st); 2352 st->print(" {" PTR_FORMAT "}", p2i(k)); 2353 } 2354 break; 2355 case JVM_CONSTANT_Fieldref : 2356 case JVM_CONSTANT_Methodref : 2357 case JVM_CONSTANT_InterfaceMethodref : 2358 st->print("klass_index=%d", uncached_klass_ref_index_at(cp_index)); 2359 st->print(" name_and_type_index=%d", uncached_name_and_type_ref_index_at(cp_index)); 2360 break; 2361 case JVM_CONSTANT_String : 2362 unresolved_string_at(cp_index)->print_value_on(st); 2363 break; 2364 case JVM_CONSTANT_Integer : 2365 st->print("%d", int_at(cp_index)); 2366 break; 2367 case JVM_CONSTANT_Float : 2368 st->print("%f", float_at(cp_index)); 2369 break; 2370 case JVM_CONSTANT_Long : 2371 st->print_jlong(long_at(cp_index)); 2372 break; 2373 case JVM_CONSTANT_Double : 2374 st->print("%lf", double_at(cp_index)); 2375 break; 2376 case JVM_CONSTANT_NameAndType : 2377 st->print("name_index=%d", name_ref_index_at(cp_index)); 2378 st->print(" signature_index=%d", signature_ref_index_at(cp_index)); 2379 break; 2380 case JVM_CONSTANT_Utf8 : 2381 symbol_at(cp_index)->print_value_on(st); 2382 break; 2383 case JVM_CONSTANT_ClassIndex: { 2384 int name_index = *int_at_addr(cp_index); 2385 st->print("klass_index=%d ", name_index); 2386 symbol_at(name_index)->print_value_on(st); 2387 } 2388 break; 2389 case JVM_CONSTANT_UnresolvedClass : // fall-through 2390 case JVM_CONSTANT_UnresolvedClassInError: { 2391 CPKlassSlot kslot = klass_slot_at(cp_index); 2392 int resolved_klass_index = kslot.resolved_klass_index(); 2393 int name_index = kslot.name_index(); 2394 assert(tag_at(name_index).is_symbol(), "sanity"); 2395 symbol_at(name_index)->print_value_on(st); 2396 } 2397 break; 2398 case JVM_CONSTANT_MethodHandle : 2399 case JVM_CONSTANT_MethodHandleInError : 2400 st->print("ref_kind=%d", method_handle_ref_kind_at(cp_index)); 2401 st->print(" ref_index=%d", method_handle_index_at(cp_index)); 2402 break; 2403 case JVM_CONSTANT_MethodType : 2404 case JVM_CONSTANT_MethodTypeInError : 2405 st->print("signature_index=%d", method_type_index_at(cp_index)); 2406 break; 2407 case JVM_CONSTANT_Dynamic : 2408 case JVM_CONSTANT_DynamicInError : 2409 { 2410 st->print("bootstrap_method_index=%d", bootstrap_method_ref_index_at(cp_index)); 2411 st->print(" type_index=%d", bootstrap_name_and_type_ref_index_at(cp_index)); 2412 int argc = bootstrap_argument_count_at(cp_index); 2413 if (argc > 0) { 2414 for (int arg_i = 0; arg_i < argc; arg_i++) { 2415 int arg = bootstrap_argument_index_at(cp_index, arg_i); 2416 st->print((arg_i == 0 ? " arguments={%d" : ", %d"), arg); 2417 } 2418 st->print("}"); 2419 } 2420 } 2421 break; 2422 case JVM_CONSTANT_InvokeDynamic : 2423 { 2424 st->print("bootstrap_method_index=%d", bootstrap_method_ref_index_at(cp_index)); 2425 st->print(" name_and_type_index=%d", bootstrap_name_and_type_ref_index_at(cp_index)); 2426 int argc = bootstrap_argument_count_at(cp_index); 2427 if (argc > 0) { 2428 for (int arg_i = 0; arg_i < argc; arg_i++) { 2429 int arg = bootstrap_argument_index_at(cp_index, arg_i); 2430 st->print((arg_i == 0 ? " arguments={%d" : ", %d"), arg); 2431 } 2432 st->print("}"); 2433 } 2434 } 2435 break; 2436 default: 2437 ShouldNotReachHere(); 2438 break; 2439 } 2440 st->cr(); 2441 } 2442 2443 void ConstantPool::print_value_on(outputStream* st) const { 2444 assert(is_constantPool(), "must be constantPool"); 2445 st->print("constant pool [%d]", length()); 2446 if (has_preresolution()) st->print("/preresolution"); 2447 if (operands() != nullptr) st->print("/operands[%d]", operands()->length()); 2448 print_address_on(st); 2449 if (pool_holder() != nullptr) { 2450 st->print(" for "); 2451 pool_holder()->print_value_on(st); 2452 bool extra = (pool_holder()->constants() != this); 2453 if (extra) st->print(" (extra)"); 2454 } 2455 if (cache() != nullptr) { 2456 st->print(" cache=" PTR_FORMAT, p2i(cache())); 2457 } 2458 } 2459 2460 // Verification 2461 2462 void ConstantPool::verify_on(outputStream* st) { 2463 guarantee(is_constantPool(), "object must be constant pool"); 2464 for (int i = 0; i< length(); i++) { 2465 constantTag tag = tag_at(i); 2466 if (tag.is_klass() || tag.is_unresolved_klass()) { 2467 guarantee(klass_name_at(i)->refcount() != 0, "should have nonzero reference count"); 2468 } else if (tag.is_symbol()) { 2469 Symbol* entry = symbol_at(i); 2470 guarantee(entry->refcount() != 0, "should have nonzero reference count"); 2471 } else if (tag.is_string()) { 2472 Symbol* entry = unresolved_string_at(i); 2473 guarantee(entry->refcount() != 0, "should have nonzero reference count"); 2474 } 2475 } 2476 if (pool_holder() != nullptr) { 2477 // Note: pool_holder() can be null in temporary constant pools 2478 // used during constant pool merging 2479 guarantee(pool_holder()->is_klass(), "should be klass"); 2480 } 2481 }