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