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