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