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