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