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