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