constantPool.cpp revision 9099:115188e14c15
1/* 2 * Copyright (c) 1997, 2015, 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 "classfile/classLoaderData.hpp" 27#include "classfile/javaClasses.inline.hpp" 28#include "classfile/metadataOnStackMark.hpp" 29#include "classfile/stringTable.hpp" 30#include "classfile/systemDictionary.hpp" 31#include "classfile/vmSymbols.hpp" 32#include "interpreter/linkResolver.hpp" 33#include "memory/heapInspection.hpp" 34#include "memory/metadataFactory.hpp" 35#include "memory/oopFactory.hpp" 36#include "oops/constantPool.hpp" 37#include "oops/instanceKlass.hpp" 38#include "oops/objArrayKlass.hpp" 39#include "oops/objArrayOop.inline.hpp" 40#include "oops/oop.inline.hpp" 41#include "runtime/fieldType.hpp" 42#include "runtime/init.hpp" 43#include "runtime/javaCalls.hpp" 44#include "runtime/signature.hpp" 45#include "runtime/vframe.hpp" 46#include "utilities/copy.hpp" 47 48ConstantPool* ConstantPool::allocate(ClassLoaderData* loader_data, int length, TRAPS) { 49 // Tags are RW but comment below applies to tags also. 50 Array<u1>* tags = MetadataFactory::new_writeable_array<u1>(loader_data, length, 0, CHECK_NULL); 51 52 int size = ConstantPool::size(length); 53 54 // CDS considerations: 55 // Allocate read-write but may be able to move to read-only at dumping time 56 // if all the klasses are resolved. The only other field that is writable is 57 // the resolved_references array, which is recreated at startup time. 58 // But that could be moved to InstanceKlass (although a pain to access from 59 // assembly code). Maybe it could be moved to the cpCache which is RW. 60 return new (loader_data, size, false, MetaspaceObj::ConstantPoolType, THREAD) ConstantPool(tags); 61} 62 63ConstantPool::ConstantPool(Array<u1>* tags) { 64 set_length(tags->length()); 65 set_tags(NULL); 66 set_cache(NULL); 67 set_reference_map(NULL); 68 set_resolved_references(NULL); 69 set_operands(NULL); 70 set_pool_holder(NULL); 71 set_flags(0); 72 73 // only set to non-zero if constant pool is merged by RedefineClasses 74 set_version(0); 75 76 // initialize tag array 77 int length = tags->length(); 78 for (int index = 0; index < length; index++) { 79 tags->at_put(index, JVM_CONSTANT_Invalid); 80 } 81 set_tags(tags); 82} 83 84void ConstantPool::deallocate_contents(ClassLoaderData* loader_data) { 85 MetadataFactory::free_metadata(loader_data, cache()); 86 set_cache(NULL); 87 MetadataFactory::free_array<u2>(loader_data, reference_map()); 88 set_reference_map(NULL); 89 90 MetadataFactory::free_array<jushort>(loader_data, operands()); 91 set_operands(NULL); 92 93 release_C_heap_structures(); 94 95 // free tag array 96 MetadataFactory::free_array<u1>(loader_data, tags()); 97 set_tags(NULL); 98} 99 100void ConstantPool::release_C_heap_structures() { 101 // walk constant pool and decrement symbol reference counts 102 unreference_symbols(); 103} 104 105objArrayOop ConstantPool::resolved_references() const { 106 return (objArrayOop)JNIHandles::resolve(_resolved_references); 107} 108 109// Create resolved_references array and mapping array for original cp indexes 110// The ldc bytecode was rewritten to have the resolved reference array index so need a way 111// to map it back for resolving and some unlikely miscellaneous uses. 112// The objects created by invokedynamic are appended to this list. 113void ConstantPool::initialize_resolved_references(ClassLoaderData* loader_data, 114 intStack reference_map, 115 int constant_pool_map_length, 116 TRAPS) { 117 // Initialized the resolved object cache. 118 int map_length = reference_map.length(); 119 if (map_length > 0) { 120 // Only need mapping back to constant pool entries. The map isn't used for 121 // invokedynamic resolved_reference entries. For invokedynamic entries, 122 // the constant pool cache index has the mapping back to both the constant 123 // pool and to the resolved reference index. 124 if (constant_pool_map_length > 0) { 125 Array<u2>* om = MetadataFactory::new_array<u2>(loader_data, constant_pool_map_length, CHECK); 126 127 for (int i = 0; i < constant_pool_map_length; i++) { 128 int x = reference_map.at(i); 129 assert(x == (int)(jushort) x, "klass index is too big"); 130 om->at_put(i, (jushort)x); 131 } 132 set_reference_map(om); 133 } 134 135 // Create Java array for holding resolved strings, methodHandles, 136 // methodTypes, invokedynamic and invokehandle appendix objects, etc. 137 objArrayOop stom = oopFactory::new_objArray(SystemDictionary::Object_klass(), map_length, CHECK); 138 Handle refs_handle (THREAD, (oop)stom); // must handleize. 139 set_resolved_references(loader_data->add_handle(refs_handle)); 140 } 141} 142 143// CDS support. Create a new resolved_references array. 144void ConstantPool::restore_unshareable_info(TRAPS) { 145 146 // Only create the new resolved references array if it hasn't been attempted before 147 if (resolved_references() != NULL) return; 148 149 // restore the C++ vtable from the shared archive 150 restore_vtable(); 151 152 if (SystemDictionary::Object_klass_loaded()) { 153 // Recreate the object array and add to ClassLoaderData. 154 int map_length = resolved_reference_length(); 155 if (map_length > 0) { 156 objArrayOop stom = oopFactory::new_objArray(SystemDictionary::Object_klass(), map_length, CHECK); 157 Handle refs_handle (THREAD, (oop)stom); // must handleize. 158 159 ClassLoaderData* loader_data = pool_holder()->class_loader_data(); 160 set_resolved_references(loader_data->add_handle(refs_handle)); 161 } 162 } 163} 164 165void ConstantPool::remove_unshareable_info() { 166 // Resolved references are not in the shared archive. 167 // Save the length for restoration. It is not necessarily the same length 168 // as reference_map.length() if invokedynamic is saved. 169 set_resolved_reference_length( 170 resolved_references() != NULL ? resolved_references()->length() : 0); 171 set_resolved_references(NULL); 172} 173 174int ConstantPool::cp_to_object_index(int cp_index) { 175 // this is harder don't do this so much. 176 int i = reference_map()->find(cp_index); 177 // We might not find the index for jsr292 call. 178 return (i < 0) ? _no_index_sentinel : i; 179} 180 181void ConstantPool::trace_class_resolution(constantPoolHandle this_cp, KlassHandle k) { 182 ResourceMark rm; 183 int line_number = -1; 184 const char * source_file = NULL; 185 if (JavaThread::current()->has_last_Java_frame()) { 186 // try to identify the method which called this function. 187 vframeStream vfst(JavaThread::current()); 188 if (!vfst.at_end()) { 189 line_number = vfst.method()->line_number_from_bci(vfst.bci()); 190 Symbol* s = vfst.method()->method_holder()->source_file_name(); 191 if (s != NULL) { 192 source_file = s->as_C_string(); 193 } 194 } 195 } 196 if (k() != this_cp->pool_holder()) { 197 // only print something if the classes are different 198 if (source_file != NULL) { 199 tty->print("RESOLVE %s %s %s:%d\n", 200 this_cp->pool_holder()->external_name(), 201 InstanceKlass::cast(k())->external_name(), source_file, line_number); 202 } else { 203 tty->print("RESOLVE %s %s\n", 204 this_cp->pool_holder()->external_name(), 205 InstanceKlass::cast(k())->external_name()); 206 } 207 } 208} 209 210Klass* ConstantPool::klass_at_impl(constantPoolHandle this_cp, int which, 211 bool save_resolution_error, TRAPS) { 212 assert(THREAD->is_Java_thread(), "must be a Java thread"); 213 214 // A resolved constantPool entry will contain a Klass*, otherwise a Symbol*. 215 // It is not safe to rely on the tag bit's here, since we don't have a lock, and 216 // the entry and tag is not updated atomicly. 217 CPSlot entry = this_cp->slot_at(which); 218 if (entry.is_resolved()) { 219 assert(entry.get_klass()->is_klass(), "must be"); 220 // Already resolved - return entry. 221 return entry.get_klass(); 222 } 223 224 // This tag doesn't change back to unresolved class unless at a safepoint. 225 if (this_cp->tag_at(which).is_unresolved_klass_in_error()) { 226 // The original attempt to resolve this constant pool entry failed so find the 227 // class of the original error and throw another error of the same class 228 // (JVMS 5.4.3). 229 // If there is a detail message, pass that detail message to the error. 230 // The JVMS does not strictly require us to duplicate the same detail message, 231 // or any internal exception fields such as cause or stacktrace. But since the 232 // detail message is often a class name or other literal string, we will repeat it 233 // if we can find it in the symbol table. 234 throw_resolution_error(this_cp, which, CHECK_0); 235 ShouldNotReachHere(); 236 } 237 238 Handle mirror_handle; 239 Symbol* name = entry.get_symbol(); 240 Handle loader (THREAD, this_cp->pool_holder()->class_loader()); 241 Handle protection_domain (THREAD, this_cp->pool_holder()->protection_domain()); 242 Klass* kk = SystemDictionary::resolve_or_fail(name, loader, protection_domain, true, THREAD); 243 KlassHandle k (THREAD, kk); 244 if (!HAS_PENDING_EXCEPTION) { 245 // preserve the resolved klass from unloading 246 mirror_handle = Handle(THREAD, kk->java_mirror()); 247 // Do access check for klasses 248 verify_constant_pool_resolve(this_cp, k, THREAD); 249 } 250 251 // Failed to resolve class. We must record the errors so that subsequent attempts 252 // to resolve this constant pool entry fail with the same error (JVMS 5.4.3). 253 if (HAS_PENDING_EXCEPTION) { 254 if (save_resolution_error) { 255 save_and_throw_exception(this_cp, which, constantTag(JVM_CONSTANT_UnresolvedClass), CHECK_NULL); 256 // If CHECK_NULL above doesn't return the exception, that means that 257 // some other thread has beaten us and has resolved the class. 258 // To preserve old behavior, we return the resolved class. 259 entry = this_cp->resolved_klass_at(which); 260 assert(entry.is_resolved(), "must be resolved if exception was cleared"); 261 assert(entry.get_klass()->is_klass(), "must be resolved to a klass"); 262 return entry.get_klass(); 263 } else { 264 return NULL; // return the pending exception 265 } 266 } 267 268 // Make this class loader depend upon the class loader owning the class reference 269 ClassLoaderData* this_key = this_cp->pool_holder()->class_loader_data(); 270 this_key->record_dependency(k(), CHECK_NULL); // Can throw OOM 271 272 if (TraceClassResolution && !k->oop_is_array()) { 273 // skip resolving the constant pool so that this code gets 274 // called the next time some bytecodes refer to this class. 275 trace_class_resolution(this_cp, k); 276 return k(); 277 } else { 278 this_cp->klass_at_put(which, k()); 279 } 280 281 entry = this_cp->resolved_klass_at(which); 282 assert(entry.is_resolved() && entry.get_klass()->is_klass(), "must be resolved at this point"); 283 return entry.get_klass(); 284} 285 286 287// Does not update ConstantPool* - to avoid any exception throwing. Used 288// by compiler and exception handling. Also used to avoid classloads for 289// instanceof operations. Returns NULL if the class has not been loaded or 290// if the verification of constant pool failed 291Klass* ConstantPool::klass_at_if_loaded(constantPoolHandle this_cp, int which) { 292 CPSlot entry = this_cp->slot_at(which); 293 if (entry.is_resolved()) { 294 assert(entry.get_klass()->is_klass(), "must be"); 295 return entry.get_klass(); 296 } else { 297 assert(entry.is_unresolved(), "must be either symbol or klass"); 298 Thread *thread = Thread::current(); 299 Symbol* name = entry.get_symbol(); 300 oop loader = this_cp->pool_holder()->class_loader(); 301 oop protection_domain = this_cp->pool_holder()->protection_domain(); 302 Handle h_prot (thread, protection_domain); 303 Handle h_loader (thread, loader); 304 Klass* k = SystemDictionary::find(name, h_loader, h_prot, thread); 305 306 if (k != NULL) { 307 // Make sure that resolving is legal 308 EXCEPTION_MARK; 309 KlassHandle klass(THREAD, k); 310 // return NULL if verification fails 311 verify_constant_pool_resolve(this_cp, klass, THREAD); 312 if (HAS_PENDING_EXCEPTION) { 313 CLEAR_PENDING_EXCEPTION; 314 return NULL; 315 } 316 return klass(); 317 } else { 318 return k; 319 } 320 } 321} 322 323 324Klass* ConstantPool::klass_ref_at_if_loaded(constantPoolHandle this_cp, int which) { 325 return klass_at_if_loaded(this_cp, this_cp->klass_ref_index_at(which)); 326} 327 328 329Method* ConstantPool::method_at_if_loaded(constantPoolHandle cpool, 330 int which) { 331 if (cpool->cache() == NULL) return NULL; // nothing to load yet 332 int cache_index = decode_cpcache_index(which, true); 333 if (!(cache_index >= 0 && cache_index < cpool->cache()->length())) { 334 // FIXME: should be an assert 335 if (PrintMiscellaneous && (Verbose||WizardMode)) { 336 tty->print_cr("bad operand %d in:", which); cpool->print(); 337 } 338 return NULL; 339 } 340 ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index); 341 return e->method_if_resolved(cpool); 342} 343 344 345bool ConstantPool::has_appendix_at_if_loaded(constantPoolHandle cpool, int which) { 346 if (cpool->cache() == NULL) return false; // nothing to load yet 347 int cache_index = decode_cpcache_index(which, true); 348 ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index); 349 return e->has_appendix(); 350} 351 352oop ConstantPool::appendix_at_if_loaded(constantPoolHandle cpool, int which) { 353 if (cpool->cache() == NULL) return NULL; // nothing to load yet 354 int cache_index = decode_cpcache_index(which, true); 355 ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index); 356 return e->appendix_if_resolved(cpool); 357} 358 359 360bool ConstantPool::has_method_type_at_if_loaded(constantPoolHandle cpool, int which) { 361 if (cpool->cache() == NULL) return false; // nothing to load yet 362 int cache_index = decode_cpcache_index(which, true); 363 ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index); 364 return e->has_method_type(); 365} 366 367oop ConstantPool::method_type_at_if_loaded(constantPoolHandle cpool, int which) { 368 if (cpool->cache() == NULL) return NULL; // nothing to load yet 369 int cache_index = decode_cpcache_index(which, true); 370 ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index); 371 return e->method_type_if_resolved(cpool); 372} 373 374 375Symbol* ConstantPool::impl_name_ref_at(int which, bool uncached) { 376 int name_index = name_ref_index_at(impl_name_and_type_ref_index_at(which, uncached)); 377 return symbol_at(name_index); 378} 379 380 381Symbol* ConstantPool::impl_signature_ref_at(int which, bool uncached) { 382 int signature_index = signature_ref_index_at(impl_name_and_type_ref_index_at(which, uncached)); 383 return symbol_at(signature_index); 384} 385 386 387int ConstantPool::impl_name_and_type_ref_index_at(int which, bool uncached) { 388 int i = which; 389 if (!uncached && cache() != NULL) { 390 if (ConstantPool::is_invokedynamic_index(which)) { 391 // Invokedynamic index is index into resolved_references 392 int pool_index = invokedynamic_cp_cache_entry_at(which)->constant_pool_index(); 393 pool_index = invoke_dynamic_name_and_type_ref_index_at(pool_index); 394 assert(tag_at(pool_index).is_name_and_type(), ""); 395 return pool_index; 396 } 397 // change byte-ordering and go via cache 398 i = remap_instruction_operand_from_cache(which); 399 } else { 400 if (tag_at(which).is_invoke_dynamic()) { 401 int pool_index = invoke_dynamic_name_and_type_ref_index_at(which); 402 assert(tag_at(pool_index).is_name_and_type(), ""); 403 return pool_index; 404 } 405 } 406 assert(tag_at(i).is_field_or_method(), "Corrupted constant pool"); 407 assert(!tag_at(i).is_invoke_dynamic(), "Must be handled above"); 408 jint ref_index = *int_at_addr(i); 409 return extract_high_short_from_int(ref_index); 410} 411 412 413int ConstantPool::impl_klass_ref_index_at(int which, bool uncached) { 414 guarantee(!ConstantPool::is_invokedynamic_index(which), 415 "an invokedynamic instruction does not have a klass"); 416 int i = which; 417 if (!uncached && cache() != NULL) { 418 // change byte-ordering and go via cache 419 i = remap_instruction_operand_from_cache(which); 420 } 421 assert(tag_at(i).is_field_or_method(), "Corrupted constant pool"); 422 jint ref_index = *int_at_addr(i); 423 return extract_low_short_from_int(ref_index); 424} 425 426 427 428int ConstantPool::remap_instruction_operand_from_cache(int operand) { 429 int cpc_index = operand; 430 DEBUG_ONLY(cpc_index -= CPCACHE_INDEX_TAG); 431 assert((int)(u2)cpc_index == cpc_index, "clean u2"); 432 int member_index = cache()->entry_at(cpc_index)->constant_pool_index(); 433 return member_index; 434} 435 436 437void ConstantPool::verify_constant_pool_resolve(constantPoolHandle this_cp, KlassHandle k, TRAPS) { 438 if (k->oop_is_instance() || k->oop_is_objArray()) { 439 instanceKlassHandle holder (THREAD, this_cp->pool_holder()); 440 Klass* elem = k->oop_is_instance() ? k() : ObjArrayKlass::cast(k())->bottom_klass(); 441 KlassHandle element (THREAD, elem); 442 443 // The element type could be a typeArray - we only need the access check if it is 444 // an reference to another class 445 if (element->oop_is_instance()) { 446 LinkResolver::check_klass_accessability(holder, element, CHECK); 447 } 448 } 449} 450 451 452int ConstantPool::name_ref_index_at(int which_nt) { 453 jint ref_index = name_and_type_at(which_nt); 454 return extract_low_short_from_int(ref_index); 455} 456 457 458int ConstantPool::signature_ref_index_at(int which_nt) { 459 jint ref_index = name_and_type_at(which_nt); 460 return extract_high_short_from_int(ref_index); 461} 462 463 464Klass* ConstantPool::klass_ref_at(int which, TRAPS) { 465 return klass_at(klass_ref_index_at(which), THREAD); 466} 467 468 469Symbol* ConstantPool::klass_name_at(int which) { 470 assert(tag_at(which).is_unresolved_klass() || tag_at(which).is_klass(), 471 "Corrupted constant pool"); 472 // A resolved constantPool entry will contain a Klass*, otherwise a Symbol*. 473 // It is not safe to rely on the tag bit's here, since we don't have a lock, and the entry and 474 // tag is not updated atomicly. 475 CPSlot entry = slot_at(which); 476 if (entry.is_resolved()) { 477 // Already resolved - return entry's name. 478 assert(entry.get_klass()->is_klass(), "must be"); 479 return entry.get_klass()->name(); 480 } else { 481 assert(entry.is_unresolved(), "must be either symbol or klass"); 482 return entry.get_symbol(); 483 } 484} 485 486Symbol* ConstantPool::klass_ref_at_noresolve(int which) { 487 jint ref_index = klass_ref_index_at(which); 488 return klass_at_noresolve(ref_index); 489} 490 491Symbol* ConstantPool::uncached_klass_ref_at_noresolve(int which) { 492 jint ref_index = uncached_klass_ref_index_at(which); 493 return klass_at_noresolve(ref_index); 494} 495 496char* ConstantPool::string_at_noresolve(int which) { 497 return unresolved_string_at(which)->as_C_string(); 498} 499 500BasicType ConstantPool::basic_type_for_signature_at(int which) { 501 return FieldType::basic_type(symbol_at(which)); 502} 503 504 505void ConstantPool::resolve_string_constants_impl(constantPoolHandle this_cp, TRAPS) { 506 for (int index = 1; index < this_cp->length(); index++) { // Index 0 is unused 507 if (this_cp->tag_at(index).is_string()) { 508 this_cp->string_at(index, CHECK); 509 } 510 } 511} 512 513// Resolve all the classes in the constant pool. If they are all resolved, 514// the constant pool is read-only. Enhancement: allocate cp entries to 515// another metaspace, and copy to read-only or read-write space if this 516// bit is set. 517bool ConstantPool::resolve_class_constants(TRAPS) { 518 constantPoolHandle cp(THREAD, this); 519 for (int index = 1; index < length(); index++) { // Index 0 is unused 520 if (tag_at(index).is_unresolved_klass() && 521 klass_at_if_loaded(cp, index) == NULL) { 522 return false; 523 } 524 } 525 // set_preresolution(); or some bit for future use 526 return true; 527} 528 529Symbol* ConstantPool::exception_message(constantPoolHandle this_cp, int which, constantTag tag, oop pending_exception) { 530 // Dig out the detailed message to reuse if possible 531 Symbol* message = java_lang_Throwable::detail_message(pending_exception); 532 if (message != NULL) { 533 return message; 534 } 535 536 // Return specific message for the tag 537 switch (tag.value()) { 538 case JVM_CONSTANT_UnresolvedClass: 539 // return the class name in the error message 540 message = this_cp->klass_name_at(which); 541 break; 542 case JVM_CONSTANT_MethodHandle: 543 // return the method handle name in the error message 544 message = this_cp->method_handle_name_ref_at(which); 545 break; 546 case JVM_CONSTANT_MethodType: 547 // return the method type signature in the error message 548 message = this_cp->method_type_signature_at(which); 549 break; 550 default: 551 ShouldNotReachHere(); 552 } 553 554 return message; 555} 556 557void ConstantPool::throw_resolution_error(constantPoolHandle this_cp, int which, TRAPS) { 558 Symbol* message = NULL; 559 Symbol* error = SystemDictionary::find_resolution_error(this_cp, which, &message); 560 assert(error != NULL && message != NULL, "checking"); 561 CLEAR_PENDING_EXCEPTION; 562 ResourceMark rm; 563 THROW_MSG(error, message->as_C_string()); 564} 565 566// If resolution for Class, MethodHandle or MethodType fails, save the exception 567// in the resolution error table, so that the same exception is thrown again. 568void ConstantPool::save_and_throw_exception(constantPoolHandle this_cp, int which, 569 constantTag tag, TRAPS) { 570 Symbol* error = PENDING_EXCEPTION->klass()->name(); 571 572 int error_tag = tag.error_value(); 573 574 if (!PENDING_EXCEPTION-> 575 is_a(SystemDictionary::LinkageError_klass())) { 576 // Just throw the exception and don't prevent these classes from 577 // being loaded due to virtual machine errors like StackOverflow 578 // and OutOfMemoryError, etc, or if the thread was hit by stop() 579 // Needs clarification to section 5.4.3 of the VM spec (see 6308271) 580 } else if (this_cp->tag_at(which).value() != error_tag) { 581 Symbol* message = exception_message(this_cp, which, tag, PENDING_EXCEPTION); 582 SystemDictionary::add_resolution_error(this_cp, which, error, message); 583 // CAS in the tag. If a thread beat us to registering this error that's fine. 584 // If another thread resolved the reference, this is a race condition. This 585 // thread may have had a security manager or something temporary. 586 // This doesn't deterministically get an error. So why do we save this? 587 // We save this because jvmti can add classes to the bootclass path after 588 // this error, so it needs to get the same error if the error is first. 589 jbyte old_tag = Atomic::cmpxchg((jbyte)error_tag, 590 (jbyte*)this_cp->tag_addr_at(which), (jbyte)tag.value()); 591 if (old_tag != error_tag && old_tag != tag.value()) { 592 // MethodHandles and MethodType doesn't change to resolved version. 593 assert(this_cp->tag_at(which).is_klass(), "Wrong tag value"); 594 // Forget the exception and use the resolved class. 595 CLEAR_PENDING_EXCEPTION; 596 } 597 } else { 598 // some other thread put this in error state 599 throw_resolution_error(this_cp, which, CHECK); 600 } 601} 602 603// Called to resolve constants in the constant pool and return an oop. 604// Some constant pool entries cache their resolved oop. This is also 605// called to create oops from constants to use in arguments for invokedynamic 606oop ConstantPool::resolve_constant_at_impl(constantPoolHandle this_cp, int index, int cache_index, TRAPS) { 607 oop result_oop = NULL; 608 Handle throw_exception; 609 610 if (cache_index == _possible_index_sentinel) { 611 // It is possible that this constant is one which is cached in the objects. 612 // We'll do a linear search. This should be OK because this usage is rare. 613 assert(index > 0, "valid index"); 614 cache_index = this_cp->cp_to_object_index(index); 615 } 616 assert(cache_index == _no_index_sentinel || cache_index >= 0, ""); 617 assert(index == _no_index_sentinel || index >= 0, ""); 618 619 if (cache_index >= 0) { 620 result_oop = this_cp->resolved_references()->obj_at(cache_index); 621 if (result_oop != NULL) { 622 return result_oop; 623 // That was easy... 624 } 625 index = this_cp->object_to_cp_index(cache_index); 626 } 627 628 jvalue prim_value; // temp used only in a few cases below 629 630 constantTag tag = this_cp->tag_at(index); 631 632 switch (tag.value()) { 633 634 case JVM_CONSTANT_UnresolvedClass: 635 case JVM_CONSTANT_UnresolvedClassInError: 636 case JVM_CONSTANT_Class: 637 { 638 assert(cache_index == _no_index_sentinel, "should not have been set"); 639 Klass* resolved = klass_at_impl(this_cp, index, true, CHECK_NULL); 640 // ldc wants the java mirror. 641 result_oop = resolved->java_mirror(); 642 break; 643 } 644 645 case JVM_CONSTANT_String: 646 assert(cache_index != _no_index_sentinel, "should have been set"); 647 if (this_cp->is_pseudo_string_at(index)) { 648 result_oop = this_cp->pseudo_string_at(index, cache_index); 649 break; 650 } 651 result_oop = string_at_impl(this_cp, index, cache_index, CHECK_NULL); 652 break; 653 654 case JVM_CONSTANT_MethodHandleInError: 655 case JVM_CONSTANT_MethodTypeInError: 656 { 657 throw_resolution_error(this_cp, index, CHECK_NULL); 658 break; 659 } 660 661 case JVM_CONSTANT_MethodHandle: 662 { 663 int ref_kind = this_cp->method_handle_ref_kind_at(index); 664 int callee_index = this_cp->method_handle_klass_index_at(index); 665 Symbol* name = this_cp->method_handle_name_ref_at(index); 666 Symbol* signature = this_cp->method_handle_signature_ref_at(index); 667 if (PrintMiscellaneous) 668 tty->print_cr("resolve JVM_CONSTANT_MethodHandle:%d [%d/%d/%d] %s.%s", 669 ref_kind, index, this_cp->method_handle_index_at(index), 670 callee_index, name->as_C_string(), signature->as_C_string()); 671 KlassHandle callee; 672 { Klass* k = klass_at_impl(this_cp, callee_index, true, CHECK_NULL); 673 callee = KlassHandle(THREAD, k); 674 } 675 KlassHandle klass(THREAD, this_cp->pool_holder()); 676 Handle value = SystemDictionary::link_method_handle_constant(klass, ref_kind, 677 callee, name, signature, 678 THREAD); 679 result_oop = value(); 680 if (HAS_PENDING_EXCEPTION) { 681 save_and_throw_exception(this_cp, index, tag, CHECK_NULL); 682 } 683 break; 684 } 685 686 case JVM_CONSTANT_MethodType: 687 { 688 Symbol* signature = this_cp->method_type_signature_at(index); 689 if (PrintMiscellaneous) 690 tty->print_cr("resolve JVM_CONSTANT_MethodType [%d/%d] %s", 691 index, this_cp->method_type_index_at(index), 692 signature->as_C_string()); 693 KlassHandle klass(THREAD, this_cp->pool_holder()); 694 Handle value = SystemDictionary::find_method_handle_type(signature, klass, THREAD); 695 result_oop = value(); 696 if (HAS_PENDING_EXCEPTION) { 697 save_and_throw_exception(this_cp, index, tag, CHECK_NULL); 698 } 699 break; 700 } 701 702 case JVM_CONSTANT_Integer: 703 assert(cache_index == _no_index_sentinel, "should not have been set"); 704 prim_value.i = this_cp->int_at(index); 705 result_oop = java_lang_boxing_object::create(T_INT, &prim_value, CHECK_NULL); 706 break; 707 708 case JVM_CONSTANT_Float: 709 assert(cache_index == _no_index_sentinel, "should not have been set"); 710 prim_value.f = this_cp->float_at(index); 711 result_oop = java_lang_boxing_object::create(T_FLOAT, &prim_value, CHECK_NULL); 712 break; 713 714 case JVM_CONSTANT_Long: 715 assert(cache_index == _no_index_sentinel, "should not have been set"); 716 prim_value.j = this_cp->long_at(index); 717 result_oop = java_lang_boxing_object::create(T_LONG, &prim_value, CHECK_NULL); 718 break; 719 720 case JVM_CONSTANT_Double: 721 assert(cache_index == _no_index_sentinel, "should not have been set"); 722 prim_value.d = this_cp->double_at(index); 723 result_oop = java_lang_boxing_object::create(T_DOUBLE, &prim_value, CHECK_NULL); 724 break; 725 726 default: 727 DEBUG_ONLY( tty->print_cr("*** %p: tag at CP[%d/%d] = %d", 728 this_cp(), index, cache_index, tag.value())); 729 assert(false, "unexpected constant tag"); 730 break; 731 } 732 733 if (cache_index >= 0) { 734 // Benign race condition: resolved_references may already be filled in. 735 // The important thing here is that all threads pick up the same result. 736 // It doesn't matter which racing thread wins, as long as only one 737 // result is used by all threads, and all future queries. 738 oop old_result = this_cp->resolved_references()->atomic_compare_exchange_oop(cache_index, result_oop, NULL); 739 if (old_result == NULL) { 740 return result_oop; // was installed 741 } else { 742 // Return the winning thread's result. This can be different than 743 // the result here for MethodHandles. 744 return old_result; 745 } 746 } else { 747 return result_oop; 748 } 749} 750 751oop ConstantPool::uncached_string_at(int which, TRAPS) { 752 Symbol* sym = unresolved_string_at(which); 753 oop str = StringTable::intern(sym, CHECK_(NULL)); 754 assert(java_lang_String::is_instance(str), "must be string"); 755 return str; 756} 757 758 759oop ConstantPool::resolve_bootstrap_specifier_at_impl(constantPoolHandle this_cp, int index, TRAPS) { 760 assert(this_cp->tag_at(index).is_invoke_dynamic(), "Corrupted constant pool"); 761 762 Handle bsm; 763 int argc; 764 { 765 // JVM_CONSTANT_InvokeDynamic is an ordered pair of [bootm, name&type], plus optional arguments 766 // The bootm, being a JVM_CONSTANT_MethodHandle, has its own cache entry. 767 // It is accompanied by the optional arguments. 768 int bsm_index = this_cp->invoke_dynamic_bootstrap_method_ref_index_at(index); 769 oop bsm_oop = this_cp->resolve_possibly_cached_constant_at(bsm_index, CHECK_NULL); 770 if (!java_lang_invoke_MethodHandle::is_instance(bsm_oop)) { 771 THROW_MSG_NULL(vmSymbols::java_lang_LinkageError(), "BSM not an MethodHandle"); 772 } 773 774 // Extract the optional static arguments. 775 argc = this_cp->invoke_dynamic_argument_count_at(index); 776 if (argc == 0) return bsm_oop; 777 778 bsm = Handle(THREAD, bsm_oop); 779 } 780 781 objArrayHandle info; 782 { 783 objArrayOop info_oop = oopFactory::new_objArray(SystemDictionary::Object_klass(), 1+argc, CHECK_NULL); 784 info = objArrayHandle(THREAD, info_oop); 785 } 786 787 info->obj_at_put(0, bsm()); 788 for (int i = 0; i < argc; i++) { 789 int arg_index = this_cp->invoke_dynamic_argument_index_at(index, i); 790 oop arg_oop = this_cp->resolve_possibly_cached_constant_at(arg_index, CHECK_NULL); 791 info->obj_at_put(1+i, arg_oop); 792 } 793 794 return info(); 795} 796 797oop ConstantPool::string_at_impl(constantPoolHandle this_cp, int which, int obj_index, TRAPS) { 798 // If the string has already been interned, this entry will be non-null 799 oop str = this_cp->resolved_references()->obj_at(obj_index); 800 if (str != NULL) return str; 801 Symbol* sym = this_cp->unresolved_string_at(which); 802 str = StringTable::intern(sym, CHECK_(NULL)); 803 this_cp->string_at_put(which, obj_index, str); 804 assert(java_lang_String::is_instance(str), "must be string"); 805 return str; 806} 807 808 809bool ConstantPool::klass_name_at_matches(instanceKlassHandle k, 810 int which) { 811 // Names are interned, so we can compare Symbol*s directly 812 Symbol* cp_name = klass_name_at(which); 813 return (cp_name == k->name()); 814} 815 816 817// Iterate over symbols and decrement ones which are Symbol*s 818// This is done during GC. 819// Only decrement the UTF8 symbols. Unresolved classes and strings point to 820// these symbols but didn't increment the reference count. 821void ConstantPool::unreference_symbols() { 822 for (int index = 1; index < length(); index++) { // Index 0 is unused 823 constantTag tag = tag_at(index); 824 if (tag.is_symbol()) { 825 symbol_at(index)->decrement_refcount(); 826 } 827 } 828} 829 830 831// Compare this constant pool's entry at index1 to the constant pool 832// cp2's entry at index2. 833bool ConstantPool::compare_entry_to(int index1, constantPoolHandle cp2, 834 int index2, TRAPS) { 835 836 // The error tags are equivalent to non-error tags when comparing 837 jbyte t1 = tag_at(index1).non_error_value(); 838 jbyte t2 = cp2->tag_at(index2).non_error_value(); 839 840 if (t1 != t2) { 841 // Not the same entry type so there is nothing else to check. Note 842 // that this style of checking will consider resolved/unresolved 843 // class pairs as different. 844 // From the ConstantPool* API point of view, this is correct 845 // behavior. See VM_RedefineClasses::merge_constant_pools() to see how this 846 // plays out in the context of ConstantPool* merging. 847 return false; 848 } 849 850 switch (t1) { 851 case JVM_CONSTANT_Class: 852 { 853 Klass* k1 = klass_at(index1, CHECK_false); 854 Klass* k2 = cp2->klass_at(index2, CHECK_false); 855 if (k1 == k2) { 856 return true; 857 } 858 } break; 859 860 case JVM_CONSTANT_ClassIndex: 861 { 862 int recur1 = klass_index_at(index1); 863 int recur2 = cp2->klass_index_at(index2); 864 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false); 865 if (match) { 866 return true; 867 } 868 } break; 869 870 case JVM_CONSTANT_Double: 871 { 872 jdouble d1 = double_at(index1); 873 jdouble d2 = cp2->double_at(index2); 874 if (d1 == d2) { 875 return true; 876 } 877 } break; 878 879 case JVM_CONSTANT_Fieldref: 880 case JVM_CONSTANT_InterfaceMethodref: 881 case JVM_CONSTANT_Methodref: 882 { 883 int recur1 = uncached_klass_ref_index_at(index1); 884 int recur2 = cp2->uncached_klass_ref_index_at(index2); 885 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false); 886 if (match) { 887 recur1 = uncached_name_and_type_ref_index_at(index1); 888 recur2 = cp2->uncached_name_and_type_ref_index_at(index2); 889 match = compare_entry_to(recur1, cp2, recur2, CHECK_false); 890 if (match) { 891 return true; 892 } 893 } 894 } break; 895 896 case JVM_CONSTANT_Float: 897 { 898 jfloat f1 = float_at(index1); 899 jfloat f2 = cp2->float_at(index2); 900 if (f1 == f2) { 901 return true; 902 } 903 } break; 904 905 case JVM_CONSTANT_Integer: 906 { 907 jint i1 = int_at(index1); 908 jint i2 = cp2->int_at(index2); 909 if (i1 == i2) { 910 return true; 911 } 912 } break; 913 914 case JVM_CONSTANT_Long: 915 { 916 jlong l1 = long_at(index1); 917 jlong l2 = cp2->long_at(index2); 918 if (l1 == l2) { 919 return true; 920 } 921 } break; 922 923 case JVM_CONSTANT_NameAndType: 924 { 925 int recur1 = name_ref_index_at(index1); 926 int recur2 = cp2->name_ref_index_at(index2); 927 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false); 928 if (match) { 929 recur1 = signature_ref_index_at(index1); 930 recur2 = cp2->signature_ref_index_at(index2); 931 match = compare_entry_to(recur1, cp2, recur2, CHECK_false); 932 if (match) { 933 return true; 934 } 935 } 936 } break; 937 938 case JVM_CONSTANT_StringIndex: 939 { 940 int recur1 = string_index_at(index1); 941 int recur2 = cp2->string_index_at(index2); 942 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false); 943 if (match) { 944 return true; 945 } 946 } break; 947 948 case JVM_CONSTANT_UnresolvedClass: 949 { 950 Symbol* k1 = klass_name_at(index1); 951 Symbol* k2 = cp2->klass_name_at(index2); 952 if (k1 == k2) { 953 return true; 954 } 955 } break; 956 957 case JVM_CONSTANT_MethodType: 958 { 959 int k1 = method_type_index_at_error_ok(index1); 960 int k2 = cp2->method_type_index_at_error_ok(index2); 961 bool match = compare_entry_to(k1, cp2, k2, CHECK_false); 962 if (match) { 963 return true; 964 } 965 } break; 966 967 case JVM_CONSTANT_MethodHandle: 968 { 969 int k1 = method_handle_ref_kind_at_error_ok(index1); 970 int k2 = cp2->method_handle_ref_kind_at_error_ok(index2); 971 if (k1 == k2) { 972 int i1 = method_handle_index_at_error_ok(index1); 973 int i2 = cp2->method_handle_index_at_error_ok(index2); 974 bool match = compare_entry_to(i1, cp2, i2, CHECK_false); 975 if (match) { 976 return true; 977 } 978 } 979 } break; 980 981 case JVM_CONSTANT_InvokeDynamic: 982 { 983 int k1 = invoke_dynamic_name_and_type_ref_index_at(index1); 984 int k2 = cp2->invoke_dynamic_name_and_type_ref_index_at(index2); 985 int i1 = invoke_dynamic_bootstrap_specifier_index(index1); 986 int i2 = cp2->invoke_dynamic_bootstrap_specifier_index(index2); 987 // separate statements and variables because CHECK_false is used 988 bool match_entry = compare_entry_to(k1, cp2, k2, CHECK_false); 989 bool match_operand = compare_operand_to(i1, cp2, i2, CHECK_false); 990 return (match_entry && match_operand); 991 } break; 992 993 case JVM_CONSTANT_String: 994 { 995 Symbol* s1 = unresolved_string_at(index1); 996 Symbol* s2 = cp2->unresolved_string_at(index2); 997 if (s1 == s2) { 998 return true; 999 } 1000 } break; 1001 1002 case JVM_CONSTANT_Utf8: 1003 { 1004 Symbol* s1 = symbol_at(index1); 1005 Symbol* s2 = cp2->symbol_at(index2); 1006 if (s1 == s2) { 1007 return true; 1008 } 1009 } break; 1010 1011 // Invalid is used as the tag for the second constant pool entry 1012 // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should 1013 // not be seen by itself. 1014 case JVM_CONSTANT_Invalid: // fall through 1015 1016 default: 1017 ShouldNotReachHere(); 1018 break; 1019 } 1020 1021 return false; 1022} // end compare_entry_to() 1023 1024 1025// Resize the operands array with delta_len and delta_size. 1026// Used in RedefineClasses for CP merge. 1027void ConstantPool::resize_operands(int delta_len, int delta_size, TRAPS) { 1028 int old_len = operand_array_length(operands()); 1029 int new_len = old_len + delta_len; 1030 int min_len = (delta_len > 0) ? old_len : new_len; 1031 1032 int old_size = operands()->length(); 1033 int new_size = old_size + delta_size; 1034 int min_size = (delta_size > 0) ? old_size : new_size; 1035 1036 ClassLoaderData* loader_data = pool_holder()->class_loader_data(); 1037 Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, new_size, CHECK); 1038 1039 // Set index in the resized array for existing elements only 1040 for (int idx = 0; idx < min_len; idx++) { 1041 int offset = operand_offset_at(idx); // offset in original array 1042 operand_offset_at_put(new_ops, idx, offset + 2*delta_len); // offset in resized array 1043 } 1044 // Copy the bootstrap specifiers only 1045 Copy::conjoint_memory_atomic(operands()->adr_at(2*old_len), 1046 new_ops->adr_at(2*new_len), 1047 (min_size - 2*min_len) * sizeof(u2)); 1048 // Explicitly deallocate old operands array. 1049 // Note, it is not needed for 7u backport. 1050 if ( operands() != NULL) { // the safety check 1051 MetadataFactory::free_array<u2>(loader_data, operands()); 1052 } 1053 set_operands(new_ops); 1054} // end resize_operands() 1055 1056 1057// Extend the operands array with the length and size of the ext_cp operands. 1058// Used in RedefineClasses for CP merge. 1059void ConstantPool::extend_operands(constantPoolHandle ext_cp, TRAPS) { 1060 int delta_len = operand_array_length(ext_cp->operands()); 1061 if (delta_len == 0) { 1062 return; // nothing to do 1063 } 1064 int delta_size = ext_cp->operands()->length(); 1065 1066 assert(delta_len > 0 && delta_size > 0, "extended operands array must be bigger"); 1067 1068 if (operand_array_length(operands()) == 0) { 1069 ClassLoaderData* loader_data = pool_holder()->class_loader_data(); 1070 Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, delta_size, CHECK); 1071 // The first element index defines the offset of second part 1072 operand_offset_at_put(new_ops, 0, 2*delta_len); // offset in new array 1073 set_operands(new_ops); 1074 } else { 1075 resize_operands(delta_len, delta_size, CHECK); 1076 } 1077 1078} // end extend_operands() 1079 1080 1081// Shrink the operands array to a smaller array with new_len length. 1082// Used in RedefineClasses for CP merge. 1083void ConstantPool::shrink_operands(int new_len, TRAPS) { 1084 int old_len = operand_array_length(operands()); 1085 if (new_len == old_len) { 1086 return; // nothing to do 1087 } 1088 assert(new_len < old_len, "shrunken operands array must be smaller"); 1089 1090 int free_base = operand_next_offset_at(new_len - 1); 1091 int delta_len = new_len - old_len; 1092 int delta_size = 2*delta_len + free_base - operands()->length(); 1093 1094 resize_operands(delta_len, delta_size, CHECK); 1095 1096} // end shrink_operands() 1097 1098 1099void ConstantPool::copy_operands(constantPoolHandle from_cp, 1100 constantPoolHandle to_cp, 1101 TRAPS) { 1102 1103 int from_oplen = operand_array_length(from_cp->operands()); 1104 int old_oplen = operand_array_length(to_cp->operands()); 1105 if (from_oplen != 0) { 1106 ClassLoaderData* loader_data = to_cp->pool_holder()->class_loader_data(); 1107 // append my operands to the target's operands array 1108 if (old_oplen == 0) { 1109 // Can't just reuse from_cp's operand list because of deallocation issues 1110 int len = from_cp->operands()->length(); 1111 Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, len, CHECK); 1112 Copy::conjoint_memory_atomic( 1113 from_cp->operands()->adr_at(0), new_ops->adr_at(0), len * sizeof(u2)); 1114 to_cp->set_operands(new_ops); 1115 } else { 1116 int old_len = to_cp->operands()->length(); 1117 int from_len = from_cp->operands()->length(); 1118 int old_off = old_oplen * sizeof(u2); 1119 int from_off = from_oplen * sizeof(u2); 1120 // Use the metaspace for the destination constant pool 1121 Array<u2>* new_operands = MetadataFactory::new_array<u2>(loader_data, old_len + from_len, CHECK); 1122 int fillp = 0, len = 0; 1123 // first part of dest 1124 Copy::conjoint_memory_atomic(to_cp->operands()->adr_at(0), 1125 new_operands->adr_at(fillp), 1126 (len = old_off) * sizeof(u2)); 1127 fillp += len; 1128 // first part of src 1129 Copy::conjoint_memory_atomic(from_cp->operands()->adr_at(0), 1130 new_operands->adr_at(fillp), 1131 (len = from_off) * sizeof(u2)); 1132 fillp += len; 1133 // second part of dest 1134 Copy::conjoint_memory_atomic(to_cp->operands()->adr_at(old_off), 1135 new_operands->adr_at(fillp), 1136 (len = old_len - old_off) * sizeof(u2)); 1137 fillp += len; 1138 // second part of src 1139 Copy::conjoint_memory_atomic(from_cp->operands()->adr_at(from_off), 1140 new_operands->adr_at(fillp), 1141 (len = from_len - from_off) * sizeof(u2)); 1142 fillp += len; 1143 assert(fillp == new_operands->length(), ""); 1144 1145 // Adjust indexes in the first part of the copied operands array. 1146 for (int j = 0; j < from_oplen; j++) { 1147 int offset = operand_offset_at(new_operands, old_oplen + j); 1148 assert(offset == operand_offset_at(from_cp->operands(), j), "correct copy"); 1149 offset += old_len; // every new tuple is preceded by old_len extra u2's 1150 operand_offset_at_put(new_operands, old_oplen + j, offset); 1151 } 1152 1153 // replace target operands array with combined array 1154 to_cp->set_operands(new_operands); 1155 } 1156 } 1157} // end copy_operands() 1158 1159 1160// Copy this constant pool's entries at start_i to end_i (inclusive) 1161// to the constant pool to_cp's entries starting at to_i. A total of 1162// (end_i - start_i) + 1 entries are copied. 1163void ConstantPool::copy_cp_to_impl(constantPoolHandle from_cp, int start_i, int end_i, 1164 constantPoolHandle to_cp, int to_i, TRAPS) { 1165 1166 1167 int dest_i = to_i; // leave original alone for debug purposes 1168 1169 for (int src_i = start_i; src_i <= end_i; /* see loop bottom */ ) { 1170 copy_entry_to(from_cp, src_i, to_cp, dest_i, CHECK); 1171 1172 switch (from_cp->tag_at(src_i).value()) { 1173 case JVM_CONSTANT_Double: 1174 case JVM_CONSTANT_Long: 1175 // double and long take two constant pool entries 1176 src_i += 2; 1177 dest_i += 2; 1178 break; 1179 1180 default: 1181 // all others take one constant pool entry 1182 src_i++; 1183 dest_i++; 1184 break; 1185 } 1186 } 1187 copy_operands(from_cp, to_cp, CHECK); 1188 1189} // end copy_cp_to_impl() 1190 1191 1192// Copy this constant pool's entry at from_i to the constant pool 1193// to_cp's entry at to_i. 1194void ConstantPool::copy_entry_to(constantPoolHandle from_cp, int from_i, 1195 constantPoolHandle to_cp, int to_i, 1196 TRAPS) { 1197 1198 int tag = from_cp->tag_at(from_i).value(); 1199 switch (tag) { 1200 case JVM_CONSTANT_Class: 1201 { 1202 Klass* k = from_cp->klass_at(from_i, CHECK); 1203 to_cp->klass_at_put(to_i, k); 1204 } break; 1205 1206 case JVM_CONSTANT_ClassIndex: 1207 { 1208 jint ki = from_cp->klass_index_at(from_i); 1209 to_cp->klass_index_at_put(to_i, ki); 1210 } break; 1211 1212 case JVM_CONSTANT_Double: 1213 { 1214 jdouble d = from_cp->double_at(from_i); 1215 to_cp->double_at_put(to_i, d); 1216 // double takes two constant pool entries so init second entry's tag 1217 to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid); 1218 } break; 1219 1220 case JVM_CONSTANT_Fieldref: 1221 { 1222 int class_index = from_cp->uncached_klass_ref_index_at(from_i); 1223 int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i); 1224 to_cp->field_at_put(to_i, class_index, name_and_type_index); 1225 } break; 1226 1227 case JVM_CONSTANT_Float: 1228 { 1229 jfloat f = from_cp->float_at(from_i); 1230 to_cp->float_at_put(to_i, f); 1231 } break; 1232 1233 case JVM_CONSTANT_Integer: 1234 { 1235 jint i = from_cp->int_at(from_i); 1236 to_cp->int_at_put(to_i, i); 1237 } break; 1238 1239 case JVM_CONSTANT_InterfaceMethodref: 1240 { 1241 int class_index = from_cp->uncached_klass_ref_index_at(from_i); 1242 int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i); 1243 to_cp->interface_method_at_put(to_i, class_index, name_and_type_index); 1244 } break; 1245 1246 case JVM_CONSTANT_Long: 1247 { 1248 jlong l = from_cp->long_at(from_i); 1249 to_cp->long_at_put(to_i, l); 1250 // long takes two constant pool entries so init second entry's tag 1251 to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid); 1252 } break; 1253 1254 case JVM_CONSTANT_Methodref: 1255 { 1256 int class_index = from_cp->uncached_klass_ref_index_at(from_i); 1257 int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i); 1258 to_cp->method_at_put(to_i, class_index, name_and_type_index); 1259 } break; 1260 1261 case JVM_CONSTANT_NameAndType: 1262 { 1263 int name_ref_index = from_cp->name_ref_index_at(from_i); 1264 int signature_ref_index = from_cp->signature_ref_index_at(from_i); 1265 to_cp->name_and_type_at_put(to_i, name_ref_index, signature_ref_index); 1266 } break; 1267 1268 case JVM_CONSTANT_StringIndex: 1269 { 1270 jint si = from_cp->string_index_at(from_i); 1271 to_cp->string_index_at_put(to_i, si); 1272 } break; 1273 1274 case JVM_CONSTANT_UnresolvedClass: 1275 case JVM_CONSTANT_UnresolvedClassInError: 1276 { 1277 // Can be resolved after checking tag, so check the slot first. 1278 CPSlot entry = from_cp->slot_at(from_i); 1279 if (entry.is_resolved()) { 1280 assert(entry.get_klass()->is_klass(), "must be"); 1281 // Already resolved 1282 to_cp->klass_at_put(to_i, entry.get_klass()); 1283 } else { 1284 to_cp->unresolved_klass_at_put(to_i, entry.get_symbol()); 1285 } 1286 } break; 1287 1288 case JVM_CONSTANT_String: 1289 { 1290 Symbol* s = from_cp->unresolved_string_at(from_i); 1291 to_cp->unresolved_string_at_put(to_i, s); 1292 } break; 1293 1294 case JVM_CONSTANT_Utf8: 1295 { 1296 Symbol* s = from_cp->symbol_at(from_i); 1297 // Need to increase refcount, the old one will be thrown away and deferenced 1298 s->increment_refcount(); 1299 to_cp->symbol_at_put(to_i, s); 1300 } break; 1301 1302 case JVM_CONSTANT_MethodType: 1303 case JVM_CONSTANT_MethodTypeInError: 1304 { 1305 jint k = from_cp->method_type_index_at_error_ok(from_i); 1306 to_cp->method_type_index_at_put(to_i, k); 1307 } break; 1308 1309 case JVM_CONSTANT_MethodHandle: 1310 case JVM_CONSTANT_MethodHandleInError: 1311 { 1312 int k1 = from_cp->method_handle_ref_kind_at_error_ok(from_i); 1313 int k2 = from_cp->method_handle_index_at_error_ok(from_i); 1314 to_cp->method_handle_index_at_put(to_i, k1, k2); 1315 } break; 1316 1317 case JVM_CONSTANT_InvokeDynamic: 1318 { 1319 int k1 = from_cp->invoke_dynamic_bootstrap_specifier_index(from_i); 1320 int k2 = from_cp->invoke_dynamic_name_and_type_ref_index_at(from_i); 1321 k1 += operand_array_length(to_cp->operands()); // to_cp might already have operands 1322 to_cp->invoke_dynamic_at_put(to_i, k1, k2); 1323 } break; 1324 1325 // Invalid is used as the tag for the second constant pool entry 1326 // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should 1327 // not be seen by itself. 1328 case JVM_CONSTANT_Invalid: // fall through 1329 1330 default: 1331 { 1332 ShouldNotReachHere(); 1333 } break; 1334 } 1335} // end copy_entry_to() 1336 1337 1338// Search constant pool search_cp for an entry that matches this 1339// constant pool's entry at pattern_i. Returns the index of a 1340// matching entry or zero (0) if there is no matching entry. 1341int ConstantPool::find_matching_entry(int pattern_i, 1342 constantPoolHandle search_cp, TRAPS) { 1343 1344 // index zero (0) is not used 1345 for (int i = 1; i < search_cp->length(); i++) { 1346 bool found = compare_entry_to(pattern_i, search_cp, i, CHECK_0); 1347 if (found) { 1348 return i; 1349 } 1350 } 1351 1352 return 0; // entry not found; return unused index zero (0) 1353} // end find_matching_entry() 1354 1355 1356// Compare this constant pool's bootstrap specifier at idx1 to the constant pool 1357// cp2's bootstrap specifier at idx2. 1358bool ConstantPool::compare_operand_to(int idx1, constantPoolHandle cp2, int idx2, TRAPS) { 1359 int k1 = operand_bootstrap_method_ref_index_at(idx1); 1360 int k2 = cp2->operand_bootstrap_method_ref_index_at(idx2); 1361 bool match = compare_entry_to(k1, cp2, k2, CHECK_false); 1362 1363 if (!match) { 1364 return false; 1365 } 1366 int argc = operand_argument_count_at(idx1); 1367 if (argc == cp2->operand_argument_count_at(idx2)) { 1368 for (int j = 0; j < argc; j++) { 1369 k1 = operand_argument_index_at(idx1, j); 1370 k2 = cp2->operand_argument_index_at(idx2, j); 1371 match = compare_entry_to(k1, cp2, k2, CHECK_false); 1372 if (!match) { 1373 return false; 1374 } 1375 } 1376 return true; // got through loop; all elements equal 1377 } 1378 return false; 1379} // end compare_operand_to() 1380 1381// Search constant pool search_cp for a bootstrap specifier that matches 1382// this constant pool's bootstrap specifier at pattern_i index. 1383// Return the index of a matching bootstrap specifier or (-1) if there is no match. 1384int ConstantPool::find_matching_operand(int pattern_i, 1385 constantPoolHandle search_cp, int search_len, TRAPS) { 1386 for (int i = 0; i < search_len; i++) { 1387 bool found = compare_operand_to(pattern_i, search_cp, i, CHECK_(-1)); 1388 if (found) { 1389 return i; 1390 } 1391 } 1392 return -1; // bootstrap specifier not found; return unused index (-1) 1393} // end find_matching_operand() 1394 1395 1396#ifndef PRODUCT 1397 1398const char* ConstantPool::printable_name_at(int which) { 1399 1400 constantTag tag = tag_at(which); 1401 1402 if (tag.is_string()) { 1403 return string_at_noresolve(which); 1404 } else if (tag.is_klass() || tag.is_unresolved_klass()) { 1405 return klass_name_at(which)->as_C_string(); 1406 } else if (tag.is_symbol()) { 1407 return symbol_at(which)->as_C_string(); 1408 } 1409 return ""; 1410} 1411 1412#endif // PRODUCT 1413 1414 1415// JVMTI GetConstantPool support 1416 1417// For debugging of constant pool 1418const bool debug_cpool = false; 1419 1420#define DBG(code) do { if (debug_cpool) { (code); } } while(0) 1421 1422static void print_cpool_bytes(jint cnt, u1 *bytes) { 1423 const char* WARN_MSG = "Must not be such entry!"; 1424 jint size = 0; 1425 u2 idx1, idx2; 1426 1427 for (jint idx = 1; idx < cnt; idx++) { 1428 jint ent_size = 0; 1429 u1 tag = *bytes++; 1430 size++; // count tag 1431 1432 printf("const #%03d, tag: %02d ", idx, tag); 1433 switch(tag) { 1434 case JVM_CONSTANT_Invalid: { 1435 printf("Invalid"); 1436 break; 1437 } 1438 case JVM_CONSTANT_Unicode: { 1439 printf("Unicode %s", WARN_MSG); 1440 break; 1441 } 1442 case JVM_CONSTANT_Utf8: { 1443 u2 len = Bytes::get_Java_u2(bytes); 1444 char str[128]; 1445 if (len > 127) { 1446 len = 127; 1447 } 1448 strncpy(str, (char *) (bytes+2), len); 1449 str[len] = '\0'; 1450 printf("Utf8 \"%s\"", str); 1451 ent_size = 2 + len; 1452 break; 1453 } 1454 case JVM_CONSTANT_Integer: { 1455 u4 val = Bytes::get_Java_u4(bytes); 1456 printf("int %d", *(int *) &val); 1457 ent_size = 4; 1458 break; 1459 } 1460 case JVM_CONSTANT_Float: { 1461 u4 val = Bytes::get_Java_u4(bytes); 1462 printf("float %5.3ff", *(float *) &val); 1463 ent_size = 4; 1464 break; 1465 } 1466 case JVM_CONSTANT_Long: { 1467 u8 val = Bytes::get_Java_u8(bytes); 1468 printf("long " INT64_FORMAT, (int64_t) *(jlong *) &val); 1469 ent_size = 8; 1470 idx++; // Long takes two cpool slots 1471 break; 1472 } 1473 case JVM_CONSTANT_Double: { 1474 u8 val = Bytes::get_Java_u8(bytes); 1475 printf("double %5.3fd", *(jdouble *)&val); 1476 ent_size = 8; 1477 idx++; // Double takes two cpool slots 1478 break; 1479 } 1480 case JVM_CONSTANT_Class: { 1481 idx1 = Bytes::get_Java_u2(bytes); 1482 printf("class #%03d", idx1); 1483 ent_size = 2; 1484 break; 1485 } 1486 case JVM_CONSTANT_String: { 1487 idx1 = Bytes::get_Java_u2(bytes); 1488 printf("String #%03d", idx1); 1489 ent_size = 2; 1490 break; 1491 } 1492 case JVM_CONSTANT_Fieldref: { 1493 idx1 = Bytes::get_Java_u2(bytes); 1494 idx2 = Bytes::get_Java_u2(bytes+2); 1495 printf("Field #%03d, #%03d", (int) idx1, (int) idx2); 1496 ent_size = 4; 1497 break; 1498 } 1499 case JVM_CONSTANT_Methodref: { 1500 idx1 = Bytes::get_Java_u2(bytes); 1501 idx2 = Bytes::get_Java_u2(bytes+2); 1502 printf("Method #%03d, #%03d", idx1, idx2); 1503 ent_size = 4; 1504 break; 1505 } 1506 case JVM_CONSTANT_InterfaceMethodref: { 1507 idx1 = Bytes::get_Java_u2(bytes); 1508 idx2 = Bytes::get_Java_u2(bytes+2); 1509 printf("InterfMethod #%03d, #%03d", idx1, idx2); 1510 ent_size = 4; 1511 break; 1512 } 1513 case JVM_CONSTANT_NameAndType: { 1514 idx1 = Bytes::get_Java_u2(bytes); 1515 idx2 = Bytes::get_Java_u2(bytes+2); 1516 printf("NameAndType #%03d, #%03d", idx1, idx2); 1517 ent_size = 4; 1518 break; 1519 } 1520 case JVM_CONSTANT_ClassIndex: { 1521 printf("ClassIndex %s", WARN_MSG); 1522 break; 1523 } 1524 case JVM_CONSTANT_UnresolvedClass: { 1525 printf("UnresolvedClass: %s", WARN_MSG); 1526 break; 1527 } 1528 case JVM_CONSTANT_UnresolvedClassInError: { 1529 printf("UnresolvedClassInErr: %s", WARN_MSG); 1530 break; 1531 } 1532 case JVM_CONSTANT_StringIndex: { 1533 printf("StringIndex: %s", WARN_MSG); 1534 break; 1535 } 1536 } 1537 printf(";\n"); 1538 bytes += ent_size; 1539 size += ent_size; 1540 } 1541 printf("Cpool size: %d\n", size); 1542 fflush(0); 1543 return; 1544} /* end print_cpool_bytes */ 1545 1546 1547// Returns size of constant pool entry. 1548jint ConstantPool::cpool_entry_size(jint idx) { 1549 switch(tag_at(idx).value()) { 1550 case JVM_CONSTANT_Invalid: 1551 case JVM_CONSTANT_Unicode: 1552 return 1; 1553 1554 case JVM_CONSTANT_Utf8: 1555 return 3 + symbol_at(idx)->utf8_length(); 1556 1557 case JVM_CONSTANT_Class: 1558 case JVM_CONSTANT_String: 1559 case JVM_CONSTANT_ClassIndex: 1560 case JVM_CONSTANT_UnresolvedClass: 1561 case JVM_CONSTANT_UnresolvedClassInError: 1562 case JVM_CONSTANT_StringIndex: 1563 case JVM_CONSTANT_MethodType: 1564 case JVM_CONSTANT_MethodTypeInError: 1565 return 3; 1566 1567 case JVM_CONSTANT_MethodHandle: 1568 case JVM_CONSTANT_MethodHandleInError: 1569 return 4; //tag, ref_kind, ref_index 1570 1571 case JVM_CONSTANT_Integer: 1572 case JVM_CONSTANT_Float: 1573 case JVM_CONSTANT_Fieldref: 1574 case JVM_CONSTANT_Methodref: 1575 case JVM_CONSTANT_InterfaceMethodref: 1576 case JVM_CONSTANT_NameAndType: 1577 return 5; 1578 1579 case JVM_CONSTANT_InvokeDynamic: 1580 // u1 tag, u2 bsm, u2 nt 1581 return 5; 1582 1583 case JVM_CONSTANT_Long: 1584 case JVM_CONSTANT_Double: 1585 return 9; 1586 } 1587 assert(false, "cpool_entry_size: Invalid constant pool entry tag"); 1588 return 1; 1589} /* end cpool_entry_size */ 1590 1591 1592// SymbolHashMap is used to find a constant pool index from a string. 1593// This function fills in SymbolHashMaps, one for utf8s and one for 1594// class names, returns size of the cpool raw bytes. 1595jint ConstantPool::hash_entries_to(SymbolHashMap *symmap, 1596 SymbolHashMap *classmap) { 1597 jint size = 0; 1598 1599 for (u2 idx = 1; idx < length(); idx++) { 1600 u2 tag = tag_at(idx).value(); 1601 size += cpool_entry_size(idx); 1602 1603 switch(tag) { 1604 case JVM_CONSTANT_Utf8: { 1605 Symbol* sym = symbol_at(idx); 1606 symmap->add_entry(sym, idx); 1607 DBG(printf("adding symbol entry %s = %d\n", sym->as_utf8(), idx)); 1608 break; 1609 } 1610 case JVM_CONSTANT_Class: 1611 case JVM_CONSTANT_UnresolvedClass: 1612 case JVM_CONSTANT_UnresolvedClassInError: { 1613 Symbol* sym = klass_name_at(idx); 1614 classmap->add_entry(sym, idx); 1615 DBG(printf("adding class entry %s = %d\n", sym->as_utf8(), idx)); 1616 break; 1617 } 1618 case JVM_CONSTANT_Long: 1619 case JVM_CONSTANT_Double: { 1620 idx++; // Both Long and Double take two cpool slots 1621 break; 1622 } 1623 } 1624 } 1625 return size; 1626} /* end hash_utf8_entries_to */ 1627 1628 1629// Copy cpool bytes. 1630// Returns: 1631// 0, in case of OutOfMemoryError 1632// -1, in case of internal error 1633// > 0, count of the raw cpool bytes that have been copied 1634int ConstantPool::copy_cpool_bytes(int cpool_size, 1635 SymbolHashMap* tbl, 1636 unsigned char *bytes) { 1637 u2 idx1, idx2; 1638 jint size = 0; 1639 jint cnt = length(); 1640 unsigned char *start_bytes = bytes; 1641 1642 for (jint idx = 1; idx < cnt; idx++) { 1643 u1 tag = tag_at(idx).value(); 1644 jint ent_size = cpool_entry_size(idx); 1645 1646 assert(size + ent_size <= cpool_size, "Size mismatch"); 1647 1648 *bytes = tag; 1649 DBG(printf("#%03hd tag=%03hd, ", idx, tag)); 1650 switch(tag) { 1651 case JVM_CONSTANT_Invalid: { 1652 DBG(printf("JVM_CONSTANT_Invalid")); 1653 break; 1654 } 1655 case JVM_CONSTANT_Unicode: { 1656 assert(false, "Wrong constant pool tag: JVM_CONSTANT_Unicode"); 1657 DBG(printf("JVM_CONSTANT_Unicode")); 1658 break; 1659 } 1660 case JVM_CONSTANT_Utf8: { 1661 Symbol* sym = symbol_at(idx); 1662 char* str = sym->as_utf8(); 1663 // Warning! It's crashing on x86 with len = sym->utf8_length() 1664 int len = (int) strlen(str); 1665 Bytes::put_Java_u2((address) (bytes+1), (u2) len); 1666 for (int i = 0; i < len; i++) { 1667 bytes[3+i] = (u1) str[i]; 1668 } 1669 DBG(printf("JVM_CONSTANT_Utf8: %s ", str)); 1670 break; 1671 } 1672 case JVM_CONSTANT_Integer: { 1673 jint val = int_at(idx); 1674 Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val); 1675 break; 1676 } 1677 case JVM_CONSTANT_Float: { 1678 jfloat val = float_at(idx); 1679 Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val); 1680 break; 1681 } 1682 case JVM_CONSTANT_Long: { 1683 jlong val = long_at(idx); 1684 Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val); 1685 idx++; // Long takes two cpool slots 1686 break; 1687 } 1688 case JVM_CONSTANT_Double: { 1689 jdouble val = double_at(idx); 1690 Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val); 1691 idx++; // Double takes two cpool slots 1692 break; 1693 } 1694 case JVM_CONSTANT_Class: 1695 case JVM_CONSTANT_UnresolvedClass: 1696 case JVM_CONSTANT_UnresolvedClassInError: { 1697 *bytes = JVM_CONSTANT_Class; 1698 Symbol* sym = klass_name_at(idx); 1699 idx1 = tbl->symbol_to_value(sym); 1700 assert(idx1 != 0, "Have not found a hashtable entry"); 1701 Bytes::put_Java_u2((address) (bytes+1), idx1); 1702 DBG(printf("JVM_CONSTANT_Class: idx=#%03hd, %s", idx1, sym->as_utf8())); 1703 break; 1704 } 1705 case JVM_CONSTANT_String: { 1706 *bytes = JVM_CONSTANT_String; 1707 Symbol* sym = unresolved_string_at(idx); 1708 idx1 = tbl->symbol_to_value(sym); 1709 assert(idx1 != 0, "Have not found a hashtable entry"); 1710 Bytes::put_Java_u2((address) (bytes+1), idx1); 1711 DBG(printf("JVM_CONSTANT_String: idx=#%03hd, %s", idx1, sym->as_utf8())); 1712 break; 1713 } 1714 case JVM_CONSTANT_Fieldref: 1715 case JVM_CONSTANT_Methodref: 1716 case JVM_CONSTANT_InterfaceMethodref: { 1717 idx1 = uncached_klass_ref_index_at(idx); 1718 idx2 = uncached_name_and_type_ref_index_at(idx); 1719 Bytes::put_Java_u2((address) (bytes+1), idx1); 1720 Bytes::put_Java_u2((address) (bytes+3), idx2); 1721 DBG(printf("JVM_CONSTANT_Methodref: %hd %hd", idx1, idx2)); 1722 break; 1723 } 1724 case JVM_CONSTANT_NameAndType: { 1725 idx1 = name_ref_index_at(idx); 1726 idx2 = signature_ref_index_at(idx); 1727 Bytes::put_Java_u2((address) (bytes+1), idx1); 1728 Bytes::put_Java_u2((address) (bytes+3), idx2); 1729 DBG(printf("JVM_CONSTANT_NameAndType: %hd %hd", idx1, idx2)); 1730 break; 1731 } 1732 case JVM_CONSTANT_ClassIndex: { 1733 *bytes = JVM_CONSTANT_Class; 1734 idx1 = klass_index_at(idx); 1735 Bytes::put_Java_u2((address) (bytes+1), idx1); 1736 DBG(printf("JVM_CONSTANT_ClassIndex: %hd", idx1)); 1737 break; 1738 } 1739 case JVM_CONSTANT_StringIndex: { 1740 *bytes = JVM_CONSTANT_String; 1741 idx1 = string_index_at(idx); 1742 Bytes::put_Java_u2((address) (bytes+1), idx1); 1743 DBG(printf("JVM_CONSTANT_StringIndex: %hd", idx1)); 1744 break; 1745 } 1746 case JVM_CONSTANT_MethodHandle: 1747 case JVM_CONSTANT_MethodHandleInError: { 1748 *bytes = JVM_CONSTANT_MethodHandle; 1749 int kind = method_handle_ref_kind_at_error_ok(idx); 1750 idx1 = method_handle_index_at_error_ok(idx); 1751 *(bytes+1) = (unsigned char) kind; 1752 Bytes::put_Java_u2((address) (bytes+2), idx1); 1753 DBG(printf("JVM_CONSTANT_MethodHandle: %d %hd", kind, idx1)); 1754 break; 1755 } 1756 case JVM_CONSTANT_MethodType: 1757 case JVM_CONSTANT_MethodTypeInError: { 1758 *bytes = JVM_CONSTANT_MethodType; 1759 idx1 = method_type_index_at_error_ok(idx); 1760 Bytes::put_Java_u2((address) (bytes+1), idx1); 1761 DBG(printf("JVM_CONSTANT_MethodType: %hd", idx1)); 1762 break; 1763 } 1764 case JVM_CONSTANT_InvokeDynamic: { 1765 *bytes = tag; 1766 idx1 = extract_low_short_from_int(*int_at_addr(idx)); 1767 idx2 = extract_high_short_from_int(*int_at_addr(idx)); 1768 assert(idx2 == invoke_dynamic_name_and_type_ref_index_at(idx), "correct half of u4"); 1769 Bytes::put_Java_u2((address) (bytes+1), idx1); 1770 Bytes::put_Java_u2((address) (bytes+3), idx2); 1771 DBG(printf("JVM_CONSTANT_InvokeDynamic: %hd %hd", idx1, idx2)); 1772 break; 1773 } 1774 } 1775 DBG(printf("\n")); 1776 bytes += ent_size; 1777 size += ent_size; 1778 } 1779 assert(size == cpool_size, "Size mismatch"); 1780 1781 // Keep temorarily for debugging until it's stable. 1782 DBG(print_cpool_bytes(cnt, start_bytes)); 1783 return (int)(bytes - start_bytes); 1784} /* end copy_cpool_bytes */ 1785 1786#undef DBG 1787 1788 1789void ConstantPool::set_on_stack(const bool value) { 1790 if (value) { 1791 // Only record if it's not already set. 1792 if (!on_stack()) { 1793 _flags |= _on_stack; 1794 MetadataOnStackMark::record(this); 1795 } 1796 } else { 1797 // Clearing is done single-threadedly. 1798 _flags &= ~_on_stack; 1799 } 1800} 1801 1802// JSR 292 support for patching constant pool oops after the class is linked and 1803// the oop array for resolved references are created. 1804// We can't do this during classfile parsing, which is how the other indexes are 1805// patched. The other patches are applied early for some error checking 1806// so only defer the pseudo_strings. 1807void ConstantPool::patch_resolved_references(GrowableArray<Handle>* cp_patches) { 1808 for (int index = 1; index < cp_patches->length(); index++) { // Index 0 is unused 1809 Handle patch = cp_patches->at(index); 1810 if (patch.not_null()) { 1811 assert (tag_at(index).is_string(), "should only be string left"); 1812 // Patching a string means pre-resolving it. 1813 // The spelling in the constant pool is ignored. 1814 // The constant reference may be any object whatever. 1815 // If it is not a real interned string, the constant is referred 1816 // to as a "pseudo-string", and must be presented to the CP 1817 // explicitly, because it may require scavenging. 1818 int obj_index = cp_to_object_index(index); 1819 pseudo_string_at_put(index, obj_index, patch()); 1820 DEBUG_ONLY(cp_patches->at_put(index, Handle());) 1821 } 1822 } 1823#ifdef ASSERT 1824 // Ensure that all the patches have been used. 1825 for (int index = 0; index < cp_patches->length(); index++) { 1826 assert(cp_patches->at(index).is_null(), 1827 "Unused constant pool patch at %d in class file %s", 1828 index, 1829 pool_holder()->external_name()); 1830 } 1831#endif // ASSERT 1832} 1833 1834#ifndef PRODUCT 1835 1836// CompileTheWorld support. Preload all classes loaded references in the passed in constantpool 1837void ConstantPool::preload_and_initialize_all_classes(ConstantPool* obj, TRAPS) { 1838 guarantee(obj->is_constantPool(), "object must be constant pool"); 1839 constantPoolHandle cp(THREAD, (ConstantPool*)obj); 1840 guarantee(cp->pool_holder() != NULL, "must be fully loaded"); 1841 1842 for (int i = 0; i< cp->length(); i++) { 1843 if (cp->tag_at(i).is_unresolved_klass()) { 1844 // This will force loading of the class 1845 Klass* klass = cp->klass_at(i, CHECK); 1846 if (klass->oop_is_instance()) { 1847 // Force initialization of class 1848 InstanceKlass::cast(klass)->initialize(CHECK); 1849 } 1850 } 1851 } 1852} 1853 1854#endif 1855 1856 1857// Printing 1858 1859void ConstantPool::print_on(outputStream* st) const { 1860 assert(is_constantPool(), "must be constantPool"); 1861 st->print_cr("%s", internal_name()); 1862 if (flags() != 0) { 1863 st->print(" - flags: 0x%x", flags()); 1864 if (has_preresolution()) st->print(" has_preresolution"); 1865 if (on_stack()) st->print(" on_stack"); 1866 st->cr(); 1867 } 1868 if (pool_holder() != NULL) { 1869 st->print_cr(" - holder: " INTPTR_FORMAT, p2i(pool_holder())); 1870 } 1871 st->print_cr(" - cache: " INTPTR_FORMAT, p2i(cache())); 1872 st->print_cr(" - resolved_references: " INTPTR_FORMAT, p2i(resolved_references())); 1873 st->print_cr(" - reference_map: " INTPTR_FORMAT, p2i(reference_map())); 1874 1875 for (int index = 1; index < length(); index++) { // Index 0 is unused 1876 ((ConstantPool*)this)->print_entry_on(index, st); 1877 switch (tag_at(index).value()) { 1878 case JVM_CONSTANT_Long : 1879 case JVM_CONSTANT_Double : 1880 index++; // Skip entry following eigth-byte constant 1881 } 1882 1883 } 1884 st->cr(); 1885} 1886 1887// Print one constant pool entry 1888void ConstantPool::print_entry_on(const int index, outputStream* st) { 1889 EXCEPTION_MARK; 1890 st->print(" - %3d : ", index); 1891 tag_at(index).print_on(st); 1892 st->print(" : "); 1893 switch (tag_at(index).value()) { 1894 case JVM_CONSTANT_Class : 1895 { Klass* k = klass_at(index, CATCH); 1896 guarantee(k != NULL, "need klass"); 1897 k->print_value_on(st); 1898 st->print(" {" PTR_FORMAT "}", p2i(k)); 1899 } 1900 break; 1901 case JVM_CONSTANT_Fieldref : 1902 case JVM_CONSTANT_Methodref : 1903 case JVM_CONSTANT_InterfaceMethodref : 1904 st->print("klass_index=%d", uncached_klass_ref_index_at(index)); 1905 st->print(" name_and_type_index=%d", uncached_name_and_type_ref_index_at(index)); 1906 break; 1907 case JVM_CONSTANT_String : 1908 if (is_pseudo_string_at(index)) { 1909 oop anObj = pseudo_string_at(index); 1910 anObj->print_value_on(st); 1911 st->print(" {" PTR_FORMAT "}", p2i(anObj)); 1912 } else { 1913 unresolved_string_at(index)->print_value_on(st); 1914 } 1915 break; 1916 case JVM_CONSTANT_Integer : 1917 st->print("%d", int_at(index)); 1918 break; 1919 case JVM_CONSTANT_Float : 1920 st->print("%f", float_at(index)); 1921 break; 1922 case JVM_CONSTANT_Long : 1923 st->print_jlong(long_at(index)); 1924 break; 1925 case JVM_CONSTANT_Double : 1926 st->print("%lf", double_at(index)); 1927 break; 1928 case JVM_CONSTANT_NameAndType : 1929 st->print("name_index=%d", name_ref_index_at(index)); 1930 st->print(" signature_index=%d", signature_ref_index_at(index)); 1931 break; 1932 case JVM_CONSTANT_Utf8 : 1933 symbol_at(index)->print_value_on(st); 1934 break; 1935 case JVM_CONSTANT_UnresolvedClass : // fall-through 1936 case JVM_CONSTANT_UnresolvedClassInError: { 1937 CPSlot entry = slot_at(index); 1938 if (entry.is_resolved()) { 1939 entry.get_klass()->print_value_on(st); 1940 } else { 1941 entry.get_symbol()->print_value_on(st); 1942 } 1943 } 1944 break; 1945 case JVM_CONSTANT_MethodHandle : 1946 case JVM_CONSTANT_MethodHandleInError : 1947 st->print("ref_kind=%d", method_handle_ref_kind_at_error_ok(index)); 1948 st->print(" ref_index=%d", method_handle_index_at_error_ok(index)); 1949 break; 1950 case JVM_CONSTANT_MethodType : 1951 case JVM_CONSTANT_MethodTypeInError : 1952 st->print("signature_index=%d", method_type_index_at_error_ok(index)); 1953 break; 1954 case JVM_CONSTANT_InvokeDynamic : 1955 { 1956 st->print("bootstrap_method_index=%d", invoke_dynamic_bootstrap_method_ref_index_at(index)); 1957 st->print(" name_and_type_index=%d", invoke_dynamic_name_and_type_ref_index_at(index)); 1958 int argc = invoke_dynamic_argument_count_at(index); 1959 if (argc > 0) { 1960 for (int arg_i = 0; arg_i < argc; arg_i++) { 1961 int arg = invoke_dynamic_argument_index_at(index, arg_i); 1962 st->print((arg_i == 0 ? " arguments={%d" : ", %d"), arg); 1963 } 1964 st->print("}"); 1965 } 1966 } 1967 break; 1968 default: 1969 ShouldNotReachHere(); 1970 break; 1971 } 1972 st->cr(); 1973} 1974 1975void ConstantPool::print_value_on(outputStream* st) const { 1976 assert(is_constantPool(), "must be constantPool"); 1977 st->print("constant pool [%d]", length()); 1978 if (has_preresolution()) st->print("/preresolution"); 1979 if (operands() != NULL) st->print("/operands[%d]", operands()->length()); 1980 print_address_on(st); 1981 st->print(" for "); 1982 pool_holder()->print_value_on(st); 1983 if (pool_holder() != NULL) { 1984 bool extra = (pool_holder()->constants() != this); 1985 if (extra) st->print(" (extra)"); 1986 } 1987 if (cache() != NULL) { 1988 st->print(" cache=" PTR_FORMAT, p2i(cache())); 1989 } 1990} 1991 1992#if INCLUDE_SERVICES 1993// Size Statistics 1994void ConstantPool::collect_statistics(KlassSizeStats *sz) const { 1995 sz->_cp_all_bytes += (sz->_cp_bytes = sz->count(this)); 1996 sz->_cp_all_bytes += (sz->_cp_tags_bytes = sz->count_array(tags())); 1997 sz->_cp_all_bytes += (sz->_cp_cache_bytes = sz->count(cache())); 1998 sz->_cp_all_bytes += (sz->_cp_operands_bytes = sz->count_array(operands())); 1999 sz->_cp_all_bytes += (sz->_cp_refmap_bytes = sz->count_array(reference_map())); 2000 2001 sz->_ro_bytes += sz->_cp_operands_bytes + sz->_cp_tags_bytes + 2002 sz->_cp_refmap_bytes; 2003 sz->_rw_bytes += sz->_cp_bytes + sz->_cp_cache_bytes; 2004} 2005#endif // INCLUDE_SERVICES 2006 2007// Verification 2008 2009void ConstantPool::verify_on(outputStream* st) { 2010 guarantee(is_constantPool(), "object must be constant pool"); 2011 for (int i = 0; i< length(); i++) { 2012 constantTag tag = tag_at(i); 2013 CPSlot entry = slot_at(i); 2014 if (tag.is_klass()) { 2015 if (entry.is_resolved()) { 2016 guarantee(entry.get_klass()->is_klass(), "should be klass"); 2017 } 2018 } else if (tag.is_unresolved_klass()) { 2019 if (entry.is_resolved()) { 2020 guarantee(entry.get_klass()->is_klass(), "should be klass"); 2021 } 2022 } else if (tag.is_symbol()) { 2023 guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count"); 2024 } else if (tag.is_string()) { 2025 guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count"); 2026 } 2027 } 2028 if (cache() != NULL) { 2029 // Note: cache() can be NULL before a class is completely setup or 2030 // in temporary constant pools used during constant pool merging 2031 guarantee(cache()->is_constantPoolCache(), "should be constant pool cache"); 2032 } 2033 if (pool_holder() != NULL) { 2034 // Note: pool_holder() can be NULL in temporary constant pools 2035 // used during constant pool merging 2036 guarantee(pool_holder()->is_klass(), "should be klass"); 2037 } 2038} 2039 2040 2041void SymbolHashMap::add_entry(Symbol* sym, u2 value) { 2042 char *str = sym->as_utf8(); 2043 unsigned int hash = compute_hash(str, sym->utf8_length()); 2044 unsigned int index = hash % table_size(); 2045 2046 // check if already in map 2047 // we prefer the first entry since it is more likely to be what was used in 2048 // the class file 2049 for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) { 2050 assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL"); 2051 if (en->hash() == hash && en->symbol() == sym) { 2052 return; // already there 2053 } 2054 } 2055 2056 SymbolHashMapEntry* entry = new SymbolHashMapEntry(hash, sym, value); 2057 entry->set_next(bucket(index)); 2058 _buckets[index].set_entry(entry); 2059 assert(entry->symbol() != NULL, "SymbolHashMapEntry symbol is NULL"); 2060} 2061 2062SymbolHashMapEntry* SymbolHashMap::find_entry(Symbol* sym) { 2063 assert(sym != NULL, "SymbolHashMap::find_entry - symbol is NULL"); 2064 char *str = sym->as_utf8(); 2065 int len = sym->utf8_length(); 2066 unsigned int hash = SymbolHashMap::compute_hash(str, len); 2067 unsigned int index = hash % table_size(); 2068 for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) { 2069 assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL"); 2070 if (en->hash() == hash && en->symbol() == sym) { 2071 return en; 2072 } 2073 } 2074 return NULL; 2075} 2076