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