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