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