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