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 // 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 // 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_method_type_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_method_type(); 596 } 597 598 oop ConstantPool::method_type_at_if_loaded(const constantPoolHandle& cpool, int which) { 599 if (cpool->cache() == NULL) return NULL; // nothing to load yet 600 int cache_index = decode_cpcache_index(which, true); 601 ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index); 602 return e->method_type_if_resolved(cpool); 603 } 604 605 606 Symbol* ConstantPool::impl_name_ref_at(int which, bool uncached) { 607 int name_index = name_ref_index_at(impl_name_and_type_ref_index_at(which, uncached)); 608 return symbol_at(name_index); 609 } 610 611 612 Symbol* ConstantPool::impl_signature_ref_at(int which, bool uncached) { 613 int signature_index = signature_ref_index_at(impl_name_and_type_ref_index_at(which, uncached)); 614 return symbol_at(signature_index); 615 } 616 617 int ConstantPool::impl_name_and_type_ref_index_at(int which, bool uncached) { 618 int i = which; 619 if (!uncached && cache() != NULL) { 620 if (ConstantPool::is_invokedynamic_index(which)) { 621 // Invokedynamic index is index into the constant pool cache 622 int pool_index = invokedynamic_cp_cache_entry_at(which)->constant_pool_index(); 623 pool_index = invoke_dynamic_name_and_type_ref_index_at(pool_index); 624 assert(tag_at(pool_index).is_name_and_type(), ""); 625 return pool_index; 626 } 627 // change byte-ordering and go via cache 628 i = remap_instruction_operand_from_cache(which); 629 } else { 630 if (tag_at(which).is_invoke_dynamic() || 631 tag_at(which).is_dynamic_constant() || 632 tag_at(which).is_dynamic_constant_in_error()) { 633 int pool_index = invoke_dynamic_name_and_type_ref_index_at(which); 634 assert(tag_at(pool_index).is_name_and_type(), ""); 635 return pool_index; 636 } 637 } 638 assert(tag_at(i).is_field_or_method(), "Corrupted constant pool"); 639 assert(!tag_at(i).is_invoke_dynamic() && 640 !tag_at(i).is_dynamic_constant() && 641 !tag_at(i).is_dynamic_constant_in_error(), "Must be handled above"); 642 jint ref_index = *int_at_addr(i); 643 return extract_high_short_from_int(ref_index); 644 } 645 646 constantTag ConstantPool::impl_tag_ref_at(int which, bool uncached) { 647 int pool_index = which; 648 if (!uncached && cache() != NULL) { 649 if (ConstantPool::is_invokedynamic_index(which)) { 650 // Invokedynamic index is index into resolved_references 651 pool_index = invokedynamic_cp_cache_entry_at(which)->constant_pool_index(); 652 } else { 653 // change byte-ordering and go via cache 654 pool_index = remap_instruction_operand_from_cache(which); 655 } 656 } 657 return tag_at(pool_index); 658 } 659 660 int ConstantPool::impl_klass_ref_index_at(int which, bool uncached) { 661 guarantee(!ConstantPool::is_invokedynamic_index(which), 662 "an invokedynamic instruction does not have a klass"); 663 int i = which; 664 if (!uncached && cache() != NULL) { 665 // change byte-ordering and go via cache 666 i = remap_instruction_operand_from_cache(which); 667 } 668 assert(tag_at(i).is_field_or_method(), "Corrupted constant pool"); 669 jint ref_index = *int_at_addr(i); 670 return extract_low_short_from_int(ref_index); 671 } 672 673 674 675 int ConstantPool::remap_instruction_operand_from_cache(int operand) { 676 int cpc_index = operand; 677 DEBUG_ONLY(cpc_index -= CPCACHE_INDEX_TAG); 678 assert((int)(u2)cpc_index == cpc_index, "clean u2"); 679 int member_index = cache()->entry_at(cpc_index)->constant_pool_index(); 680 return member_index; 681 } 682 683 684 void ConstantPool::verify_constant_pool_resolve(const constantPoolHandle& this_cp, Klass* k, TRAPS) { 685 if (!(k->is_instance_klass() || k->is_objArray_klass())) { 686 return; // short cut, typeArray klass is always accessible 687 } 688 Klass* holder = this_cp->pool_holder(); 689 bool fold_type_to_class = true; 690 LinkResolver::check_klass_accessability(holder, k, fold_type_to_class, CHECK); 691 } 692 693 694 int ConstantPool::name_ref_index_at(int which_nt) { 695 jint ref_index = name_and_type_at(which_nt); 696 return extract_low_short_from_int(ref_index); 697 } 698 699 700 int ConstantPool::signature_ref_index_at(int which_nt) { 701 jint ref_index = name_and_type_at(which_nt); 702 return extract_high_short_from_int(ref_index); 703 } 704 705 706 Klass* ConstantPool::klass_ref_at(int which, TRAPS) { 707 return klass_at(klass_ref_index_at(which), THREAD); 708 } 709 710 Symbol* ConstantPool::klass_name_at(int which) const { 711 return symbol_at(klass_slot_at(which).name_index()); 712 } 713 714 Symbol* ConstantPool::klass_ref_at_noresolve(int which) { 715 jint ref_index = klass_ref_index_at(which); 716 return klass_at_noresolve(ref_index); 717 } 718 719 Symbol* ConstantPool::uncached_klass_ref_at_noresolve(int which) { 720 jint ref_index = uncached_klass_ref_index_at(which); 721 return klass_at_noresolve(ref_index); 722 } 723 724 char* ConstantPool::string_at_noresolve(int which) { 725 return unresolved_string_at(which)->as_C_string(); 726 } 727 728 BasicType ConstantPool::basic_type_for_signature_at(int which) const { 729 return FieldType::basic_type(symbol_at(which)); 730 } 731 732 733 void ConstantPool::resolve_string_constants_impl(const constantPoolHandle& this_cp, TRAPS) { 734 for (int index = 1; index < this_cp->length(); index++) { // Index 0 is unused 735 if (this_cp->tag_at(index).is_string()) { 736 this_cp->string_at(index, CHECK); 737 } 738 } 739 } 740 741 Symbol* ConstantPool::exception_message(const constantPoolHandle& this_cp, int which, constantTag tag, oop pending_exception) { 742 // Dig out the detailed message to reuse if possible 743 Symbol* message = java_lang_Throwable::detail_message(pending_exception); 744 if (message != NULL) { 745 return message; 746 } 747 748 // Return specific message for the tag 749 switch (tag.value()) { 750 case JVM_CONSTANT_UnresolvedClass: 751 // return the class name in the error message 752 message = this_cp->klass_name_at(which); 753 break; 754 case JVM_CONSTANT_MethodHandle: 755 // return the method handle name in the error message 756 message = this_cp->method_handle_name_ref_at(which); 757 break; 758 case JVM_CONSTANT_MethodType: 759 // return the method type signature in the error message 760 message = this_cp->method_type_signature_at(which); 761 break; 762 default: 763 ShouldNotReachHere(); 764 } 765 766 return message; 767 } 768 769 void ConstantPool::throw_resolution_error(const constantPoolHandle& this_cp, int which, TRAPS) { 770 Symbol* message = NULL; 771 Symbol* error = SystemDictionary::find_resolution_error(this_cp, which, &message); 772 assert(error != NULL && message != NULL, "checking"); 773 CLEAR_PENDING_EXCEPTION; 774 ResourceMark rm; 775 THROW_MSG(error, message->as_C_string()); 776 } 777 778 // If resolution for Class, Dynamic constant, MethodHandle or MethodType fails, save the 779 // exception in the resolution error table, so that the same exception is thrown again. 780 void ConstantPool::save_and_throw_exception(const constantPoolHandle& this_cp, int which, 781 constantTag tag, TRAPS) { 782 Symbol* error = PENDING_EXCEPTION->klass()->name(); 783 784 int error_tag = tag.error_value(); 785 786 if (!PENDING_EXCEPTION-> 787 is_a(SystemDictionary::LinkageError_klass())) { 788 // Just throw the exception and don't prevent these classes from 789 // being loaded due to virtual machine errors like StackOverflow 790 // and OutOfMemoryError, etc, or if the thread was hit by stop() 791 // Needs clarification to section 5.4.3 of the VM spec (see 6308271) 792 } else if (this_cp->tag_at(which).value() != error_tag) { 793 Symbol* message = exception_message(this_cp, which, tag, PENDING_EXCEPTION); 794 SystemDictionary::add_resolution_error(this_cp, which, error, message); 795 // CAS in the tag. If a thread beat us to registering this error that's fine. 796 // If another thread resolved the reference, this is a race condition. This 797 // thread may have had a security manager or something temporary. 798 // This doesn't deterministically get an error. So why do we save this? 799 // We save this because jvmti can add classes to the bootclass path after 800 // this error, so it needs to get the same error if the error is first. 801 jbyte old_tag = Atomic::cmpxchg((jbyte)error_tag, 802 (jbyte*)this_cp->tag_addr_at(which), (jbyte)tag.value()); 803 if (old_tag != error_tag && old_tag != tag.value()) { 804 // MethodHandles and MethodType doesn't change to resolved version. 805 assert(this_cp->tag_at(which).is_klass(), "Wrong tag value"); 806 // Forget the exception and use the resolved class. 807 CLEAR_PENDING_EXCEPTION; 808 } 809 } else { 810 // some other thread put this in error state 811 throw_resolution_error(this_cp, which, CHECK); 812 } 813 } 814 815 constantTag ConstantPool::constant_tag_at(int which) { 816 constantTag tag = tag_at(which); 817 if (tag.is_dynamic_constant() || 818 tag.is_dynamic_constant_in_error()) { 819 // have to look at the signature for this one 820 Symbol* constant_type = uncached_signature_ref_at(which); 821 return constantTag::ofBasicType(FieldType::basic_type(constant_type)); 822 } 823 return tag; 824 } 825 826 BasicType ConstantPool::basic_type_for_constant_at(int which) { 827 constantTag tag = tag_at(which); 828 if (tag.is_dynamic_constant() || 829 tag.is_dynamic_constant_in_error()) { 830 // have to look at the signature for this one 831 Symbol* constant_type = uncached_signature_ref_at(which); 832 return FieldType::basic_type(constant_type); 833 } 834 return tag.basic_type(); 835 } 836 837 // Called to resolve constants in the constant pool and return an oop. 838 // Some constant pool entries cache their resolved oop. This is also 839 // called to create oops from constants to use in arguments for invokedynamic 840 oop ConstantPool::resolve_constant_at_impl(const constantPoolHandle& this_cp, 841 int index, int cache_index, 842 bool* status_return, TRAPS) { 843 oop result_oop = NULL; 844 Handle throw_exception; 845 846 if (cache_index == _possible_index_sentinel) { 847 // It is possible that this constant is one which is cached in the objects. 848 // We'll do a linear search. This should be OK because this usage is rare. 849 // FIXME: If bootstrap specifiers stress this code, consider putting in 850 // a reverse index. Binary search over a short array should do it. 851 assert(index > 0, "valid index"); 852 cache_index = this_cp->cp_to_object_index(index); 853 } 854 assert(cache_index == _no_index_sentinel || cache_index >= 0, ""); 855 assert(index == _no_index_sentinel || index >= 0, ""); 856 857 if (cache_index >= 0) { 858 result_oop = this_cp->resolved_references()->obj_at(cache_index); 859 if (result_oop != NULL) { 860 if (oopDesc::equals(result_oop, Universe::the_null_sentinel())) { 861 DEBUG_ONLY(int temp_index = (index >= 0 ? index : this_cp->object_to_cp_index(cache_index))); 862 assert(this_cp->tag_at(temp_index).is_dynamic_constant(), "only condy uses the null sentinel"); 863 result_oop = NULL; 864 } 865 if (status_return != NULL) (*status_return) = true; 866 return result_oop; 867 // That was easy... 868 } 869 index = this_cp->object_to_cp_index(cache_index); 870 } 871 872 jvalue prim_value; // temp used only in a few cases below 873 874 constantTag tag = this_cp->tag_at(index); 875 876 if (status_return != NULL) { 877 // don't trigger resolution if the constant might need it 878 switch (tag.value()) { 879 case JVM_CONSTANT_Class: 880 { 881 CPKlassSlot kslot = this_cp->klass_slot_at(index); 882 int resolved_klass_index = kslot.resolved_klass_index(); 883 if (this_cp->resolved_klasses()->at(resolved_klass_index) == NULL) { 884 (*status_return) = false; 885 return NULL; 886 } 887 // the klass is waiting in the CP; go get it 888 break; 889 } 890 case JVM_CONSTANT_String: 891 case JVM_CONSTANT_Integer: 892 case JVM_CONSTANT_Float: 893 case JVM_CONSTANT_Long: 894 case JVM_CONSTANT_Double: 895 // these guys trigger OOM at worst 896 break; 897 default: 898 (*status_return) = false; 899 return NULL; 900 } 901 // from now on there is either success or an OOME 902 (*status_return) = true; 903 } 904 905 switch (tag.value()) { 906 907 case JVM_CONSTANT_UnresolvedClass: 908 case JVM_CONSTANT_UnresolvedClassInError: 909 case JVM_CONSTANT_Class: 910 { 911 assert(cache_index == _no_index_sentinel, "should not have been set"); 912 Klass* resolved = klass_at_impl(this_cp, index, true, CHECK_NULL); 913 // ldc wants the java mirror. 914 result_oop = resolved->java_mirror(); 915 break; 916 } 917 918 case JVM_CONSTANT_Dynamic: 919 { 920 Klass* current_klass = this_cp->pool_holder(); 921 Symbol* constant_name = this_cp->uncached_name_ref_at(index); 922 Symbol* constant_type = this_cp->uncached_signature_ref_at(index); 923 924 // The initial step in resolving an unresolved symbolic reference to a 925 // dynamically-computed constant is to resolve the symbolic reference to a 926 // method handle which will be the bootstrap method for the dynamically-computed 927 // constant. If resolution of the java.lang.invoke.MethodHandle for the bootstrap 928 // method fails, then a MethodHandleInError is stored at the corresponding 929 // bootstrap method's CP index for the CONSTANT_MethodHandle_info. No need to 930 // set a DynamicConstantInError here since any subsequent use of this 931 // bootstrap method will encounter the resolution of MethodHandleInError. 932 oop bsm_info = this_cp->resolve_bootstrap_specifier_at(index, THREAD); 933 Exceptions::wrap_dynamic_exception(CHECK_NULL); 934 assert(bsm_info != NULL, ""); 935 // FIXME: Cache this once per BootstrapMethods entry, not once per CONSTANT_Dynamic. 936 Handle bootstrap_specifier = Handle(THREAD, bsm_info); 937 938 // Resolve the Dynamically-Computed constant to invoke the BSM in order to obtain the resulting oop. 939 Handle value = SystemDictionary::link_dynamic_constant(current_klass, 940 index, 941 bootstrap_specifier, 942 constant_name, 943 constant_type, 944 THREAD); 945 result_oop = value(); 946 Exceptions::wrap_dynamic_exception(THREAD); 947 if (HAS_PENDING_EXCEPTION) { 948 // Resolution failure of the dynamically-computed constant, save_and_throw_exception 949 // will check for a LinkageError and store a DynamicConstantInError. 950 save_and_throw_exception(this_cp, index, tag, CHECK_NULL); 951 } 952 BasicType type = FieldType::basic_type(constant_type); 953 if (!is_reference_type(type)) { 954 // Make sure the primitive value is properly boxed. 955 // This is a JDK responsibility. 956 const char* fail = NULL; 957 if (result_oop == NULL) { 958 fail = "null result instead of box"; 959 } else if (!is_java_primitive(type)) { 960 // FIXME: support value types via unboxing 961 fail = "can only handle references and primitives"; 962 } else if (!java_lang_boxing_object::is_instance(result_oop, type)) { 963 fail = "primitive is not properly boxed"; 964 } 965 if (fail != NULL) { 966 // Since this exception is not a LinkageError, throw exception 967 // but do not save a DynamicInError resolution result. 968 // See section 5.4.3 of the VM spec. 969 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), fail); 970 } 971 } 972 break; 973 } 974 975 case JVM_CONSTANT_String: 976 assert(cache_index != _no_index_sentinel, "should have been set"); 977 if (this_cp->is_pseudo_string_at(index)) { 978 result_oop = this_cp->pseudo_string_at(index, cache_index); 979 break; 980 } 981 result_oop = string_at_impl(this_cp, index, cache_index, CHECK_NULL); 982 break; 983 984 case JVM_CONSTANT_DynamicInError: 985 case JVM_CONSTANT_MethodHandleInError: 986 case JVM_CONSTANT_MethodTypeInError: 987 { 988 throw_resolution_error(this_cp, index, CHECK_NULL); 989 break; 990 } 991 992 case JVM_CONSTANT_MethodHandle: 993 { 994 int ref_kind = this_cp->method_handle_ref_kind_at(index); 995 int callee_index = this_cp->method_handle_klass_index_at(index); 996 Symbol* name = this_cp->method_handle_name_ref_at(index); 997 Symbol* signature = this_cp->method_handle_signature_ref_at(index); 998 constantTag m_tag = this_cp->tag_at(this_cp->method_handle_index_at(index)); 999 { ResourceMark rm(THREAD); 1000 log_debug(class, resolve)("resolve JVM_CONSTANT_MethodHandle:%d [%d/%d/%d] %s.%s", 1001 ref_kind, index, this_cp->method_handle_index_at(index), 1002 callee_index, name->as_C_string(), signature->as_C_string()); 1003 } 1004 1005 Klass* callee = klass_at_impl(this_cp, callee_index, true, CHECK_NULL); 1006 1007 // Check constant pool method consistency 1008 if ((callee->is_interface() && m_tag.is_method()) || 1009 ((!callee->is_interface() && m_tag.is_interface_method()))) { 1010 ResourceMark rm(THREAD); 1011 char buf[400]; 1012 jio_snprintf(buf, sizeof(buf), 1013 "Inconsistent constant pool data in classfile for class %s. " 1014 "Method %s%s at index %d is %s and should be %s", 1015 callee->name()->as_C_string(), name->as_C_string(), signature->as_C_string(), index, 1016 callee->is_interface() ? "CONSTANT_MethodRef" : "CONSTANT_InterfaceMethodRef", 1017 callee->is_interface() ? "CONSTANT_InterfaceMethodRef" : "CONSTANT_MethodRef"); 1018 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 1019 } 1020 1021 Klass* klass = this_cp->pool_holder(); 1022 Handle value = SystemDictionary::link_method_handle_constant(klass, ref_kind, 1023 callee, name, signature, 1024 THREAD); 1025 result_oop = value(); 1026 if (HAS_PENDING_EXCEPTION) { 1027 save_and_throw_exception(this_cp, index, tag, CHECK_NULL); 1028 } 1029 break; 1030 } 1031 1032 case JVM_CONSTANT_MethodType: 1033 { 1034 Symbol* signature = this_cp->method_type_signature_at(index); 1035 { ResourceMark rm(THREAD); 1036 log_debug(class, resolve)("resolve JVM_CONSTANT_MethodType [%d/%d] %s", 1037 index, this_cp->method_type_index_at(index), 1038 signature->as_C_string()); 1039 } 1040 Klass* klass = this_cp->pool_holder(); 1041 Handle value = SystemDictionary::find_method_handle_type(signature, klass, THREAD); 1042 result_oop = value(); 1043 if (HAS_PENDING_EXCEPTION) { 1044 save_and_throw_exception(this_cp, index, tag, CHECK_NULL); 1045 } 1046 break; 1047 } 1048 1049 case JVM_CONSTANT_Integer: 1050 assert(cache_index == _no_index_sentinel, "should not have been set"); 1051 prim_value.i = this_cp->int_at(index); 1052 result_oop = java_lang_boxing_object::create(T_INT, &prim_value, CHECK_NULL); 1053 break; 1054 1055 case JVM_CONSTANT_Float: 1056 assert(cache_index == _no_index_sentinel, "should not have been set"); 1057 prim_value.f = this_cp->float_at(index); 1058 result_oop = java_lang_boxing_object::create(T_FLOAT, &prim_value, CHECK_NULL); 1059 break; 1060 1061 case JVM_CONSTANT_Long: 1062 assert(cache_index == _no_index_sentinel, "should not have been set"); 1063 prim_value.j = this_cp->long_at(index); 1064 result_oop = java_lang_boxing_object::create(T_LONG, &prim_value, CHECK_NULL); 1065 break; 1066 1067 case JVM_CONSTANT_Double: 1068 assert(cache_index == _no_index_sentinel, "should not have been set"); 1069 prim_value.d = this_cp->double_at(index); 1070 result_oop = java_lang_boxing_object::create(T_DOUBLE, &prim_value, CHECK_NULL); 1071 break; 1072 1073 default: 1074 DEBUG_ONLY( tty->print_cr("*** %p: tag at CP[%d/%d] = %d", 1075 this_cp(), index, cache_index, tag.value())); 1076 assert(false, "unexpected constant tag"); 1077 break; 1078 } 1079 1080 if (cache_index >= 0) { 1081 // Benign race condition: resolved_references may already be filled in. 1082 // The important thing here is that all threads pick up the same result. 1083 // It doesn't matter which racing thread wins, as long as only one 1084 // result is used by all threads, and all future queries. 1085 oop new_result = (result_oop == NULL ? Universe::the_null_sentinel() : result_oop); 1086 oop old_result = this_cp->resolved_references() 1087 ->atomic_compare_exchange_oop(cache_index, new_result, NULL); 1088 if (old_result == NULL) { 1089 return result_oop; // was installed 1090 } else { 1091 // Return the winning thread's result. This can be different than 1092 // the result here for MethodHandles. 1093 if (oopDesc::equals(old_result, Universe::the_null_sentinel())) 1094 old_result = NULL; 1095 return old_result; 1096 } 1097 } else { 1098 assert(!oopDesc::equals(result_oop, Universe::the_null_sentinel()), ""); 1099 return result_oop; 1100 } 1101 } 1102 1103 oop ConstantPool::uncached_string_at(int which, TRAPS) { 1104 Symbol* sym = unresolved_string_at(which); 1105 oop str = StringTable::intern(sym, CHECK_(NULL)); 1106 assert(java_lang_String::is_instance(str), "must be string"); 1107 return str; 1108 } 1109 1110 1111 oop ConstantPool::resolve_bootstrap_specifier_at_impl(const constantPoolHandle& this_cp, int index, TRAPS) { 1112 assert((this_cp->tag_at(index).is_invoke_dynamic() || 1113 this_cp->tag_at(index).is_dynamic_constant()), "Corrupted constant pool"); 1114 Handle bsm; 1115 int argc; 1116 { 1117 // JVM_CONSTANT_InvokeDynamic is an ordered pair of [bootm, name&mtype], plus optional arguments 1118 // JVM_CONSTANT_Dynamic is an ordered pair of [bootm, name&ftype], plus optional arguments 1119 // In both cases, the bootm, being a JVM_CONSTANT_MethodHandle, has its own cache entry. 1120 // It is accompanied by the optional arguments. 1121 int bsm_index = this_cp->invoke_dynamic_bootstrap_method_ref_index_at(index); 1122 oop bsm_oop = this_cp->resolve_possibly_cached_constant_at(bsm_index, CHECK_NULL); 1123 if (!java_lang_invoke_MethodHandle::is_instance(bsm_oop)) { 1124 THROW_MSG_NULL(vmSymbols::java_lang_LinkageError(), "BSM not an MethodHandle"); 1125 } 1126 1127 // Extract the optional static arguments. 1128 argc = this_cp->invoke_dynamic_argument_count_at(index); 1129 1130 // if there are no static arguments, return the bsm by itself: 1131 if (argc == 0 && UseBootstrapCallInfo < 2) return bsm_oop; 1132 1133 bsm = Handle(THREAD, bsm_oop); 1134 } 1135 1136 // We are going to return an ordered pair of {bsm, info}, using a 2-array. 1137 objArrayHandle info; 1138 { 1139 objArrayOop info_oop = oopFactory::new_objArray(SystemDictionary::Object_klass(), 2, CHECK_NULL); 1140 info = objArrayHandle(THREAD, info_oop); 1141 } 1142 1143 info->obj_at_put(0, bsm()); 1144 1145 bool use_BSCI; 1146 switch (UseBootstrapCallInfo) { 1147 default: use_BSCI = true; break; // stress mode 1148 case 0: use_BSCI = false; break; // stress mode 1149 case 1: // normal mode 1150 // If we were to support an alternative mode of BSM invocation, 1151 // we'd convert to pull mode here if the BSM could be a candidate 1152 // for that alternative mode. We can't easily test for things 1153 // like varargs here, but we can get away with approximate testing, 1154 // since the JDK runtime will make up the difference either way. 1155 // For now, exercise the pull-mode path if the BSM is of arity 2, 1156 // or if there is a potential condy loop (see below). 1157 oop mt_oop = java_lang_invoke_MethodHandle::type(bsm()); 1158 use_BSCI = (java_lang_invoke_MethodType::ptype_count(mt_oop) == 2); 1159 break; 1160 } 1161 1162 // Here's a reason to use BSCI even if it wasn't requested: 1163 // If a condy uses a condy argument, we want to avoid infinite 1164 // recursion (condy loops) in the C code. It's OK in Java, 1165 // because Java has stack overflow checking, so we punt 1166 // potentially cyclic cases from C to Java. 1167 if (!use_BSCI && this_cp->tag_at(index).is_dynamic_constant()) { 1168 bool found_unresolved_condy = false; 1169 for (int i = 0; i < argc; i++) { 1170 int arg_index = this_cp->invoke_dynamic_argument_index_at(index, i); 1171 if (this_cp->tag_at(arg_index).is_dynamic_constant()) { 1172 // potential recursion point condy -> condy 1173 bool found_it = false; 1174 this_cp->find_cached_constant_at(arg_index, found_it, CHECK_NULL); 1175 if (!found_it) { found_unresolved_condy = true; break; } 1176 } 1177 } 1178 if (found_unresolved_condy) 1179 use_BSCI = true; 1180 } 1181 1182 const int SMALL_ARITY = 5; 1183 if (use_BSCI && argc <= SMALL_ARITY && UseBootstrapCallInfo <= 2) { 1184 // If there are only a few arguments, and none of them need linking, 1185 // push them, instead of asking the JDK runtime to turn around and 1186 // pull them, saving a JVM/JDK transition in some simple cases. 1187 bool all_resolved = true; 1188 for (int i = 0; i < argc; i++) { 1189 bool found_it = false; 1190 int arg_index = this_cp->invoke_dynamic_argument_index_at(index, i); 1191 this_cp->find_cached_constant_at(arg_index, found_it, CHECK_NULL); 1192 if (!found_it) { all_resolved = false; break; } 1193 } 1194 if (all_resolved) 1195 use_BSCI = false; 1196 } 1197 1198 if (!use_BSCI) { 1199 // return {bsm, {arg...}}; resolution of arguments is done immediately, before JDK code is called 1200 objArrayOop args_oop = oopFactory::new_objArray(SystemDictionary::Object_klass(), argc, CHECK_NULL); 1201 info->obj_at_put(1, args_oop); // may overwrite with args[0] below 1202 objArrayHandle args(THREAD, args_oop); 1203 copy_bootstrap_arguments_at_impl(this_cp, index, 0, argc, args, 0, true, Handle(), CHECK_NULL); 1204 if (argc == 1) { 1205 // try to discard the singleton array 1206 oop arg_oop = args->obj_at(0); 1207 if (arg_oop != NULL && !arg_oop->is_array()) { 1208 // JVM treats arrays and nulls specially in this position, 1209 // but other things are just single arguments 1210 info->obj_at_put(1, arg_oop); 1211 } 1212 } 1213 } else { 1214 // return {bsm, {arg_count, pool_index}}; JDK code must pull the arguments as needed 1215 typeArrayOop ints_oop = oopFactory::new_typeArray(T_INT, 2, CHECK_NULL); 1216 ints_oop->int_at_put(0, argc); 1217 ints_oop->int_at_put(1, index); 1218 info->obj_at_put(1, ints_oop); 1219 } 1220 return info(); 1221 } 1222 1223 void ConstantPool::copy_bootstrap_arguments_at_impl(const constantPoolHandle& this_cp, int index, 1224 int start_arg, int end_arg, 1225 objArrayHandle info, int pos, 1226 bool must_resolve, Handle if_not_available, 1227 TRAPS) { 1228 int argc; 1229 int limit = pos + end_arg - start_arg; 1230 // checks: index in range [0..this_cp->length), 1231 // tag at index, start..end in range [0..argc], 1232 // info array non-null, pos..limit in [0..info.length] 1233 if ((0 >= index || index >= this_cp->length()) || 1234 !(this_cp->tag_at(index).is_invoke_dynamic() || 1235 this_cp->tag_at(index).is_dynamic_constant()) || 1236 (0 > start_arg || start_arg > end_arg) || 1237 (end_arg > (argc = this_cp->invoke_dynamic_argument_count_at(index))) || 1238 (0 > pos || pos > limit) || 1239 (info.is_null() || limit > info->length())) { 1240 // An index or something else went wrong; throw an error. 1241 // Since this is an internal API, we don't expect this, 1242 // so we don't bother to craft a nice message. 1243 THROW_MSG(vmSymbols::java_lang_LinkageError(), "bad BSM argument access"); 1244 } 1245 // now we can loop safely 1246 int info_i = pos; 1247 for (int i = start_arg; i < end_arg; i++) { 1248 int arg_index = this_cp->invoke_dynamic_argument_index_at(index, i); 1249 oop arg_oop; 1250 if (must_resolve) { 1251 arg_oop = this_cp->resolve_possibly_cached_constant_at(arg_index, CHECK); 1252 } else { 1253 bool found_it = false; 1254 arg_oop = this_cp->find_cached_constant_at(arg_index, found_it, CHECK); 1255 if (!found_it) arg_oop = if_not_available(); 1256 } 1257 info->obj_at_put(info_i++, arg_oop); 1258 } 1259 } 1260 1261 oop ConstantPool::string_at_impl(const constantPoolHandle& this_cp, int which, int obj_index, TRAPS) { 1262 // If the string has already been interned, this entry will be non-null 1263 oop str = this_cp->resolved_references()->obj_at(obj_index); 1264 assert(!oopDesc::equals(str, Universe::the_null_sentinel()), ""); 1265 if (str != NULL) return str; 1266 Symbol* sym = this_cp->unresolved_string_at(which); 1267 str = StringTable::intern(sym, CHECK_(NULL)); 1268 this_cp->string_at_put(which, obj_index, str); 1269 assert(java_lang_String::is_instance(str), "must be string"); 1270 return str; 1271 } 1272 1273 1274 bool ConstantPool::klass_name_at_matches(const InstanceKlass* k, int which) { 1275 // Names are interned, so we can compare Symbol*s directly 1276 Symbol* cp_name = klass_name_at(which); 1277 return (cp_name == k->name()); 1278 } 1279 1280 1281 // Iterate over symbols and decrement ones which are Symbol*s 1282 // This is done during GC. 1283 // Only decrement the UTF8 symbols. Strings point to 1284 // these symbols but didn't increment the reference count. 1285 void ConstantPool::unreference_symbols() { 1286 for (int index = 1; index < length(); index++) { // Index 0 is unused 1287 constantTag tag = tag_at(index); 1288 if (tag.is_symbol()) { 1289 symbol_at(index)->decrement_refcount(); 1290 } 1291 } 1292 } 1293 1294 1295 // Compare this constant pool's entry at index1 to the constant pool 1296 // cp2's entry at index2. 1297 bool ConstantPool::compare_entry_to(int index1, const constantPoolHandle& cp2, 1298 int index2, TRAPS) { 1299 1300 // The error tags are equivalent to non-error tags when comparing 1301 jbyte t1 = tag_at(index1).non_error_value(); 1302 jbyte t2 = cp2->tag_at(index2).non_error_value(); 1303 1304 if (t1 != t2) { 1305 // Not the same entry type so there is nothing else to check. Note 1306 // that this style of checking will consider resolved/unresolved 1307 // class pairs as different. 1308 // From the ConstantPool* API point of view, this is correct 1309 // behavior. See VM_RedefineClasses::merge_constant_pools() to see how this 1310 // plays out in the context of ConstantPool* merging. 1311 return false; 1312 } 1313 1314 switch (t1) { 1315 case JVM_CONSTANT_Class: 1316 { 1317 Klass* k1 = klass_at(index1, CHECK_false); 1318 Klass* k2 = cp2->klass_at(index2, CHECK_false); 1319 if (k1 == k2) { 1320 return true; 1321 } 1322 } break; 1323 1324 case JVM_CONSTANT_ClassIndex: 1325 { 1326 int recur1 = klass_index_at(index1); 1327 int recur2 = cp2->klass_index_at(index2); 1328 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false); 1329 if (match) { 1330 return true; 1331 } 1332 } break; 1333 1334 case JVM_CONSTANT_Double: 1335 { 1336 jdouble d1 = double_at(index1); 1337 jdouble d2 = cp2->double_at(index2); 1338 if (d1 == d2) { 1339 return true; 1340 } 1341 } break; 1342 1343 case JVM_CONSTANT_Fieldref: 1344 case JVM_CONSTANT_InterfaceMethodref: 1345 case JVM_CONSTANT_Methodref: 1346 { 1347 int recur1 = uncached_klass_ref_index_at(index1); 1348 int recur2 = cp2->uncached_klass_ref_index_at(index2); 1349 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false); 1350 if (match) { 1351 recur1 = uncached_name_and_type_ref_index_at(index1); 1352 recur2 = cp2->uncached_name_and_type_ref_index_at(index2); 1353 match = compare_entry_to(recur1, cp2, recur2, CHECK_false); 1354 if (match) { 1355 return true; 1356 } 1357 } 1358 } break; 1359 1360 case JVM_CONSTANT_Float: 1361 { 1362 jfloat f1 = float_at(index1); 1363 jfloat f2 = cp2->float_at(index2); 1364 if (f1 == f2) { 1365 return true; 1366 } 1367 } break; 1368 1369 case JVM_CONSTANT_Integer: 1370 { 1371 jint i1 = int_at(index1); 1372 jint i2 = cp2->int_at(index2); 1373 if (i1 == i2) { 1374 return true; 1375 } 1376 } break; 1377 1378 case JVM_CONSTANT_Long: 1379 { 1380 jlong l1 = long_at(index1); 1381 jlong l2 = cp2->long_at(index2); 1382 if (l1 == l2) { 1383 return true; 1384 } 1385 } break; 1386 1387 case JVM_CONSTANT_NameAndType: 1388 { 1389 int recur1 = name_ref_index_at(index1); 1390 int recur2 = cp2->name_ref_index_at(index2); 1391 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false); 1392 if (match) { 1393 recur1 = signature_ref_index_at(index1); 1394 recur2 = cp2->signature_ref_index_at(index2); 1395 match = compare_entry_to(recur1, cp2, recur2, CHECK_false); 1396 if (match) { 1397 return true; 1398 } 1399 } 1400 } break; 1401 1402 case JVM_CONSTANT_StringIndex: 1403 { 1404 int recur1 = string_index_at(index1); 1405 int recur2 = cp2->string_index_at(index2); 1406 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false); 1407 if (match) { 1408 return true; 1409 } 1410 } break; 1411 1412 case JVM_CONSTANT_UnresolvedClass: 1413 { 1414 Symbol* k1 = klass_name_at(index1); 1415 Symbol* k2 = cp2->klass_name_at(index2); 1416 if (k1 == k2) { 1417 return true; 1418 } 1419 } break; 1420 1421 case JVM_CONSTANT_MethodType: 1422 { 1423 int k1 = method_type_index_at(index1); 1424 int k2 = cp2->method_type_index_at(index2); 1425 bool match = compare_entry_to(k1, cp2, k2, CHECK_false); 1426 if (match) { 1427 return true; 1428 } 1429 } break; 1430 1431 case JVM_CONSTANT_MethodHandle: 1432 { 1433 int k1 = method_handle_ref_kind_at(index1); 1434 int k2 = cp2->method_handle_ref_kind_at(index2); 1435 if (k1 == k2) { 1436 int i1 = method_handle_index_at(index1); 1437 int i2 = cp2->method_handle_index_at(index2); 1438 bool match = compare_entry_to(i1, cp2, i2, CHECK_false); 1439 if (match) { 1440 return true; 1441 } 1442 } 1443 } break; 1444 1445 case JVM_CONSTANT_Dynamic: 1446 { 1447 int k1 = invoke_dynamic_name_and_type_ref_index_at(index1); 1448 int k2 = cp2->invoke_dynamic_name_and_type_ref_index_at(index2); 1449 int i1 = invoke_dynamic_bootstrap_specifier_index(index1); 1450 int i2 = cp2->invoke_dynamic_bootstrap_specifier_index(index2); 1451 // separate statements and variables because CHECK_false is used 1452 bool match_entry = compare_entry_to(k1, cp2, k2, CHECK_false); 1453 bool match_operand = compare_operand_to(i1, cp2, i2, CHECK_false); 1454 return (match_entry && match_operand); 1455 } break; 1456 1457 case JVM_CONSTANT_InvokeDynamic: 1458 { 1459 int k1 = invoke_dynamic_name_and_type_ref_index_at(index1); 1460 int k2 = cp2->invoke_dynamic_name_and_type_ref_index_at(index2); 1461 int i1 = invoke_dynamic_bootstrap_specifier_index(index1); 1462 int i2 = cp2->invoke_dynamic_bootstrap_specifier_index(index2); 1463 // separate statements and variables because CHECK_false is used 1464 bool match_entry = compare_entry_to(k1, cp2, k2, CHECK_false); 1465 bool match_operand = compare_operand_to(i1, cp2, i2, CHECK_false); 1466 return (match_entry && match_operand); 1467 } break; 1468 1469 case JVM_CONSTANT_String: 1470 { 1471 Symbol* s1 = unresolved_string_at(index1); 1472 Symbol* s2 = cp2->unresolved_string_at(index2); 1473 if (s1 == s2) { 1474 return true; 1475 } 1476 } break; 1477 1478 case JVM_CONSTANT_Utf8: 1479 { 1480 Symbol* s1 = symbol_at(index1); 1481 Symbol* s2 = cp2->symbol_at(index2); 1482 if (s1 == s2) { 1483 return true; 1484 } 1485 } break; 1486 1487 // Invalid is used as the tag for the second constant pool entry 1488 // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should 1489 // not be seen by itself. 1490 case JVM_CONSTANT_Invalid: // fall through 1491 1492 default: 1493 ShouldNotReachHere(); 1494 break; 1495 } 1496 1497 return false; 1498 } // end compare_entry_to() 1499 1500 1501 // Resize the operands array with delta_len and delta_size. 1502 // Used in RedefineClasses for CP merge. 1503 void ConstantPool::resize_operands(int delta_len, int delta_size, TRAPS) { 1504 int old_len = operand_array_length(operands()); 1505 int new_len = old_len + delta_len; 1506 int min_len = (delta_len > 0) ? old_len : new_len; 1507 1508 int old_size = operands()->length(); 1509 int new_size = old_size + delta_size; 1510 int min_size = (delta_size > 0) ? old_size : new_size; 1511 1512 ClassLoaderData* loader_data = pool_holder()->class_loader_data(); 1513 Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, new_size, CHECK); 1514 1515 // Set index in the resized array for existing elements only 1516 for (int idx = 0; idx < min_len; idx++) { 1517 int offset = operand_offset_at(idx); // offset in original array 1518 operand_offset_at_put(new_ops, idx, offset + 2*delta_len); // offset in resized array 1519 } 1520 // Copy the bootstrap specifiers only 1521 Copy::conjoint_memory_atomic(operands()->adr_at(2*old_len), 1522 new_ops->adr_at(2*new_len), 1523 (min_size - 2*min_len) * sizeof(u2)); 1524 // Explicitly deallocate old operands array. 1525 // Note, it is not needed for 7u backport. 1526 if ( operands() != NULL) { // the safety check 1527 MetadataFactory::free_array<u2>(loader_data, operands()); 1528 } 1529 set_operands(new_ops); 1530 } // end resize_operands() 1531 1532 1533 // Extend the operands array with the length and size of the ext_cp operands. 1534 // Used in RedefineClasses for CP merge. 1535 void ConstantPool::extend_operands(const constantPoolHandle& ext_cp, TRAPS) { 1536 int delta_len = operand_array_length(ext_cp->operands()); 1537 if (delta_len == 0) { 1538 return; // nothing to do 1539 } 1540 int delta_size = ext_cp->operands()->length(); 1541 1542 assert(delta_len > 0 && delta_size > 0, "extended operands array must be bigger"); 1543 1544 if (operand_array_length(operands()) == 0) { 1545 ClassLoaderData* loader_data = pool_holder()->class_loader_data(); 1546 Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, delta_size, CHECK); 1547 // The first element index defines the offset of second part 1548 operand_offset_at_put(new_ops, 0, 2*delta_len); // offset in new array 1549 set_operands(new_ops); 1550 } else { 1551 resize_operands(delta_len, delta_size, CHECK); 1552 } 1553 1554 } // end extend_operands() 1555 1556 1557 // Shrink the operands array to a smaller array with new_len length. 1558 // Used in RedefineClasses for CP merge. 1559 void ConstantPool::shrink_operands(int new_len, TRAPS) { 1560 int old_len = operand_array_length(operands()); 1561 if (new_len == old_len) { 1562 return; // nothing to do 1563 } 1564 assert(new_len < old_len, "shrunken operands array must be smaller"); 1565 1566 int free_base = operand_next_offset_at(new_len - 1); 1567 int delta_len = new_len - old_len; 1568 int delta_size = 2*delta_len + free_base - operands()->length(); 1569 1570 resize_operands(delta_len, delta_size, CHECK); 1571 1572 } // end shrink_operands() 1573 1574 1575 void ConstantPool::copy_operands(const constantPoolHandle& from_cp, 1576 const constantPoolHandle& to_cp, 1577 TRAPS) { 1578 1579 int from_oplen = operand_array_length(from_cp->operands()); 1580 int old_oplen = operand_array_length(to_cp->operands()); 1581 if (from_oplen != 0) { 1582 ClassLoaderData* loader_data = to_cp->pool_holder()->class_loader_data(); 1583 // append my operands to the target's operands array 1584 if (old_oplen == 0) { 1585 // Can't just reuse from_cp's operand list because of deallocation issues 1586 int len = from_cp->operands()->length(); 1587 Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, len, CHECK); 1588 Copy::conjoint_memory_atomic( 1589 from_cp->operands()->adr_at(0), new_ops->adr_at(0), len * sizeof(u2)); 1590 to_cp->set_operands(new_ops); 1591 } else { 1592 int old_len = to_cp->operands()->length(); 1593 int from_len = from_cp->operands()->length(); 1594 int old_off = old_oplen * sizeof(u2); 1595 int from_off = from_oplen * sizeof(u2); 1596 // Use the metaspace for the destination constant pool 1597 Array<u2>* new_operands = MetadataFactory::new_array<u2>(loader_data, old_len + from_len, CHECK); 1598 int fillp = 0, len = 0; 1599 // first part of dest 1600 Copy::conjoint_memory_atomic(to_cp->operands()->adr_at(0), 1601 new_operands->adr_at(fillp), 1602 (len = old_off) * sizeof(u2)); 1603 fillp += len; 1604 // first part of src 1605 Copy::conjoint_memory_atomic(from_cp->operands()->adr_at(0), 1606 new_operands->adr_at(fillp), 1607 (len = from_off) * sizeof(u2)); 1608 fillp += len; 1609 // second part of dest 1610 Copy::conjoint_memory_atomic(to_cp->operands()->adr_at(old_off), 1611 new_operands->adr_at(fillp), 1612 (len = old_len - old_off) * sizeof(u2)); 1613 fillp += len; 1614 // second part of src 1615 Copy::conjoint_memory_atomic(from_cp->operands()->adr_at(from_off), 1616 new_operands->adr_at(fillp), 1617 (len = from_len - from_off) * sizeof(u2)); 1618 fillp += len; 1619 assert(fillp == new_operands->length(), ""); 1620 1621 // Adjust indexes in the first part of the copied operands array. 1622 for (int j = 0; j < from_oplen; j++) { 1623 int offset = operand_offset_at(new_operands, old_oplen + j); 1624 assert(offset == operand_offset_at(from_cp->operands(), j), "correct copy"); 1625 offset += old_len; // every new tuple is preceded by old_len extra u2's 1626 operand_offset_at_put(new_operands, old_oplen + j, offset); 1627 } 1628 1629 // replace target operands array with combined array 1630 to_cp->set_operands(new_operands); 1631 } 1632 } 1633 } // end copy_operands() 1634 1635 1636 // Copy this constant pool's entries at start_i to end_i (inclusive) 1637 // to the constant pool to_cp's entries starting at to_i. A total of 1638 // (end_i - start_i) + 1 entries are copied. 1639 void ConstantPool::copy_cp_to_impl(const constantPoolHandle& from_cp, int start_i, int end_i, 1640 const constantPoolHandle& to_cp, int to_i, TRAPS) { 1641 1642 1643 int dest_i = to_i; // leave original alone for debug purposes 1644 1645 for (int src_i = start_i; src_i <= end_i; /* see loop bottom */ ) { 1646 copy_entry_to(from_cp, src_i, to_cp, dest_i, CHECK); 1647 1648 switch (from_cp->tag_at(src_i).value()) { 1649 case JVM_CONSTANT_Double: 1650 case JVM_CONSTANT_Long: 1651 // double and long take two constant pool entries 1652 src_i += 2; 1653 dest_i += 2; 1654 break; 1655 1656 default: 1657 // all others take one constant pool entry 1658 src_i++; 1659 dest_i++; 1660 break; 1661 } 1662 } 1663 copy_operands(from_cp, to_cp, CHECK); 1664 1665 } // end copy_cp_to_impl() 1666 1667 1668 // Copy this constant pool's entry at from_i to the constant pool 1669 // to_cp's entry at to_i. 1670 void ConstantPool::copy_entry_to(const constantPoolHandle& from_cp, int from_i, 1671 const constantPoolHandle& to_cp, int to_i, 1672 TRAPS) { 1673 1674 int tag = from_cp->tag_at(from_i).value(); 1675 switch (tag) { 1676 case JVM_CONSTANT_ClassIndex: 1677 { 1678 jint ki = from_cp->klass_index_at(from_i); 1679 to_cp->klass_index_at_put(to_i, ki); 1680 } break; 1681 1682 case JVM_CONSTANT_Double: 1683 { 1684 jdouble d = from_cp->double_at(from_i); 1685 to_cp->double_at_put(to_i, d); 1686 // double takes two constant pool entries so init second entry's tag 1687 to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid); 1688 } break; 1689 1690 case JVM_CONSTANT_Fieldref: 1691 { 1692 int class_index = from_cp->uncached_klass_ref_index_at(from_i); 1693 int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i); 1694 to_cp->field_at_put(to_i, class_index, name_and_type_index); 1695 } break; 1696 1697 case JVM_CONSTANT_Float: 1698 { 1699 jfloat f = from_cp->float_at(from_i); 1700 to_cp->float_at_put(to_i, f); 1701 } break; 1702 1703 case JVM_CONSTANT_Integer: 1704 { 1705 jint i = from_cp->int_at(from_i); 1706 to_cp->int_at_put(to_i, i); 1707 } break; 1708 1709 case JVM_CONSTANT_InterfaceMethodref: 1710 { 1711 int class_index = from_cp->uncached_klass_ref_index_at(from_i); 1712 int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i); 1713 to_cp->interface_method_at_put(to_i, class_index, name_and_type_index); 1714 } break; 1715 1716 case JVM_CONSTANT_Long: 1717 { 1718 jlong l = from_cp->long_at(from_i); 1719 to_cp->long_at_put(to_i, l); 1720 // long takes two constant pool entries so init second entry's tag 1721 to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid); 1722 } break; 1723 1724 case JVM_CONSTANT_Methodref: 1725 { 1726 int class_index = from_cp->uncached_klass_ref_index_at(from_i); 1727 int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i); 1728 to_cp->method_at_put(to_i, class_index, name_and_type_index); 1729 } break; 1730 1731 case JVM_CONSTANT_NameAndType: 1732 { 1733 int name_ref_index = from_cp->name_ref_index_at(from_i); 1734 int signature_ref_index = from_cp->signature_ref_index_at(from_i); 1735 to_cp->name_and_type_at_put(to_i, name_ref_index, signature_ref_index); 1736 } break; 1737 1738 case JVM_CONSTANT_StringIndex: 1739 { 1740 jint si = from_cp->string_index_at(from_i); 1741 to_cp->string_index_at_put(to_i, si); 1742 } break; 1743 1744 case JVM_CONSTANT_Class: 1745 case JVM_CONSTANT_UnresolvedClass: 1746 case JVM_CONSTANT_UnresolvedClassInError: 1747 { 1748 // Revert to JVM_CONSTANT_ClassIndex 1749 int name_index = from_cp->klass_slot_at(from_i).name_index(); 1750 assert(from_cp->tag_at(name_index).is_symbol(), "sanity"); 1751 to_cp->klass_index_at_put(to_i, name_index); 1752 } break; 1753 1754 case JVM_CONSTANT_String: 1755 { 1756 Symbol* s = from_cp->unresolved_string_at(from_i); 1757 to_cp->unresolved_string_at_put(to_i, s); 1758 } break; 1759 1760 case JVM_CONSTANT_Utf8: 1761 { 1762 Symbol* s = from_cp->symbol_at(from_i); 1763 // Need to increase refcount, the old one will be thrown away and deferenced 1764 s->increment_refcount(); 1765 to_cp->symbol_at_put(to_i, s); 1766 } break; 1767 1768 case JVM_CONSTANT_MethodType: 1769 case JVM_CONSTANT_MethodTypeInError: 1770 { 1771 jint k = from_cp->method_type_index_at(from_i); 1772 to_cp->method_type_index_at_put(to_i, k); 1773 } break; 1774 1775 case JVM_CONSTANT_MethodHandle: 1776 case JVM_CONSTANT_MethodHandleInError: 1777 { 1778 int k1 = from_cp->method_handle_ref_kind_at(from_i); 1779 int k2 = from_cp->method_handle_index_at(from_i); 1780 to_cp->method_handle_index_at_put(to_i, k1, k2); 1781 } break; 1782 1783 case JVM_CONSTANT_Dynamic: 1784 case JVM_CONSTANT_DynamicInError: 1785 { 1786 int k1 = from_cp->invoke_dynamic_bootstrap_specifier_index(from_i); 1787 int k2 = from_cp->invoke_dynamic_name_and_type_ref_index_at(from_i); 1788 k1 += operand_array_length(to_cp->operands()); // to_cp might already have operands 1789 to_cp->dynamic_constant_at_put(to_i, k1, k2); 1790 } break; 1791 1792 case JVM_CONSTANT_InvokeDynamic: 1793 { 1794 int k1 = from_cp->invoke_dynamic_bootstrap_specifier_index(from_i); 1795 int k2 = from_cp->invoke_dynamic_name_and_type_ref_index_at(from_i); 1796 k1 += operand_array_length(to_cp->operands()); // to_cp might already have operands 1797 to_cp->invoke_dynamic_at_put(to_i, k1, k2); 1798 } break; 1799 1800 // Invalid is used as the tag for the second constant pool entry 1801 // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should 1802 // not be seen by itself. 1803 case JVM_CONSTANT_Invalid: // fall through 1804 1805 default: 1806 { 1807 ShouldNotReachHere(); 1808 } break; 1809 } 1810 } // end copy_entry_to() 1811 1812 // Search constant pool search_cp for an entry that matches this 1813 // constant pool's entry at pattern_i. Returns the index of a 1814 // matching entry or zero (0) if there is no matching entry. 1815 int ConstantPool::find_matching_entry(int pattern_i, 1816 const constantPoolHandle& search_cp, TRAPS) { 1817 1818 // index zero (0) is not used 1819 for (int i = 1; i < search_cp->length(); i++) { 1820 bool found = compare_entry_to(pattern_i, search_cp, i, CHECK_0); 1821 if (found) { 1822 return i; 1823 } 1824 } 1825 1826 return 0; // entry not found; return unused index zero (0) 1827 } // end find_matching_entry() 1828 1829 1830 // Compare this constant pool's bootstrap specifier at idx1 to the constant pool 1831 // cp2's bootstrap specifier at idx2. 1832 bool ConstantPool::compare_operand_to(int idx1, const constantPoolHandle& cp2, int idx2, TRAPS) { 1833 int k1 = operand_bootstrap_method_ref_index_at(idx1); 1834 int k2 = cp2->operand_bootstrap_method_ref_index_at(idx2); 1835 bool match = compare_entry_to(k1, cp2, k2, CHECK_false); 1836 1837 if (!match) { 1838 return false; 1839 } 1840 int argc = operand_argument_count_at(idx1); 1841 if (argc == cp2->operand_argument_count_at(idx2)) { 1842 for (int j = 0; j < argc; j++) { 1843 k1 = operand_argument_index_at(idx1, j); 1844 k2 = cp2->operand_argument_index_at(idx2, j); 1845 match = compare_entry_to(k1, cp2, k2, CHECK_false); 1846 if (!match) { 1847 return false; 1848 } 1849 } 1850 return true; // got through loop; all elements equal 1851 } 1852 return false; 1853 } // end compare_operand_to() 1854 1855 // Search constant pool search_cp for a bootstrap specifier that matches 1856 // this constant pool's bootstrap specifier at pattern_i index. 1857 // Return the index of a matching bootstrap specifier or (-1) if there is no match. 1858 int ConstantPool::find_matching_operand(int pattern_i, 1859 const constantPoolHandle& search_cp, int search_len, TRAPS) { 1860 for (int i = 0; i < search_len; i++) { 1861 bool found = compare_operand_to(pattern_i, search_cp, i, CHECK_(-1)); 1862 if (found) { 1863 return i; 1864 } 1865 } 1866 return -1; // bootstrap specifier not found; return unused index (-1) 1867 } // end find_matching_operand() 1868 1869 1870 #ifndef PRODUCT 1871 1872 const char* ConstantPool::printable_name_at(int which) { 1873 1874 constantTag tag = tag_at(which); 1875 1876 if (tag.is_string()) { 1877 return string_at_noresolve(which); 1878 } else if (tag.is_klass() || tag.is_unresolved_klass()) { 1879 return klass_name_at(which)->as_C_string(); 1880 } else if (tag.is_symbol()) { 1881 return symbol_at(which)->as_C_string(); 1882 } 1883 return ""; 1884 } 1885 1886 #endif // PRODUCT 1887 1888 1889 // JVMTI GetConstantPool support 1890 1891 // For debugging of constant pool 1892 const bool debug_cpool = false; 1893 1894 #define DBG(code) do { if (debug_cpool) { (code); } } while(0) 1895 1896 static void print_cpool_bytes(jint cnt, u1 *bytes) { 1897 const char* WARN_MSG = "Must not be such entry!"; 1898 jint size = 0; 1899 u2 idx1, idx2; 1900 1901 for (jint idx = 1; idx < cnt; idx++) { 1902 jint ent_size = 0; 1903 u1 tag = *bytes++; 1904 size++; // count tag 1905 1906 printf("const #%03d, tag: %02d ", idx, tag); 1907 switch(tag) { 1908 case JVM_CONSTANT_Invalid: { 1909 printf("Invalid"); 1910 break; 1911 } 1912 case JVM_CONSTANT_Unicode: { 1913 printf("Unicode %s", WARN_MSG); 1914 break; 1915 } 1916 case JVM_CONSTANT_Utf8: { 1917 u2 len = Bytes::get_Java_u2(bytes); 1918 char str[128]; 1919 if (len > 127) { 1920 len = 127; 1921 } 1922 strncpy(str, (char *) (bytes+2), len); 1923 str[len] = '\0'; 1924 printf("Utf8 \"%s\"", str); 1925 ent_size = 2 + len; 1926 break; 1927 } 1928 case JVM_CONSTANT_Integer: { 1929 u4 val = Bytes::get_Java_u4(bytes); 1930 printf("int %d", *(int *) &val); 1931 ent_size = 4; 1932 break; 1933 } 1934 case JVM_CONSTANT_Float: { 1935 u4 val = Bytes::get_Java_u4(bytes); 1936 printf("float %5.3ff", *(float *) &val); 1937 ent_size = 4; 1938 break; 1939 } 1940 case JVM_CONSTANT_Long: { 1941 u8 val = Bytes::get_Java_u8(bytes); 1942 printf("long " INT64_FORMAT, (int64_t) *(jlong *) &val); 1943 ent_size = 8; 1944 idx++; // Long takes two cpool slots 1945 break; 1946 } 1947 case JVM_CONSTANT_Double: { 1948 u8 val = Bytes::get_Java_u8(bytes); 1949 printf("double %5.3fd", *(jdouble *)&val); 1950 ent_size = 8; 1951 idx++; // Double takes two cpool slots 1952 break; 1953 } 1954 case JVM_CONSTANT_Class: { 1955 idx1 = Bytes::get_Java_u2(bytes); 1956 printf("class #%03d", idx1); 1957 ent_size = 2; 1958 break; 1959 } 1960 case JVM_CONSTANT_String: { 1961 idx1 = Bytes::get_Java_u2(bytes); 1962 printf("String #%03d", idx1); 1963 ent_size = 2; 1964 break; 1965 } 1966 case JVM_CONSTANT_Fieldref: { 1967 idx1 = Bytes::get_Java_u2(bytes); 1968 idx2 = Bytes::get_Java_u2(bytes+2); 1969 printf("Field #%03d, #%03d", (int) idx1, (int) idx2); 1970 ent_size = 4; 1971 break; 1972 } 1973 case JVM_CONSTANT_Methodref: { 1974 idx1 = Bytes::get_Java_u2(bytes); 1975 idx2 = Bytes::get_Java_u2(bytes+2); 1976 printf("Method #%03d, #%03d", idx1, idx2); 1977 ent_size = 4; 1978 break; 1979 } 1980 case JVM_CONSTANT_InterfaceMethodref: { 1981 idx1 = Bytes::get_Java_u2(bytes); 1982 idx2 = Bytes::get_Java_u2(bytes+2); 1983 printf("InterfMethod #%03d, #%03d", idx1, idx2); 1984 ent_size = 4; 1985 break; 1986 } 1987 case JVM_CONSTANT_NameAndType: { 1988 idx1 = Bytes::get_Java_u2(bytes); 1989 idx2 = Bytes::get_Java_u2(bytes+2); 1990 printf("NameAndType #%03d, #%03d", idx1, idx2); 1991 ent_size = 4; 1992 break; 1993 } 1994 case JVM_CONSTANT_ClassIndex: { 1995 printf("ClassIndex %s", WARN_MSG); 1996 break; 1997 } 1998 case JVM_CONSTANT_UnresolvedClass: { 1999 printf("UnresolvedClass: %s", WARN_MSG); 2000 break; 2001 } 2002 case JVM_CONSTANT_UnresolvedClassInError: { 2003 printf("UnresolvedClassInErr: %s", WARN_MSG); 2004 break; 2005 } 2006 case JVM_CONSTANT_StringIndex: { 2007 printf("StringIndex: %s", WARN_MSG); 2008 break; 2009 } 2010 } 2011 printf(";\n"); 2012 bytes += ent_size; 2013 size += ent_size; 2014 } 2015 printf("Cpool size: %d\n", size); 2016 fflush(0); 2017 return; 2018 } /* end print_cpool_bytes */ 2019 2020 2021 // Returns size of constant pool entry. 2022 jint ConstantPool::cpool_entry_size(jint idx) { 2023 switch(tag_at(idx).value()) { 2024 case JVM_CONSTANT_Invalid: 2025 case JVM_CONSTANT_Unicode: 2026 return 1; 2027 2028 case JVM_CONSTANT_Utf8: 2029 return 3 + symbol_at(idx)->utf8_length(); 2030 2031 case JVM_CONSTANT_Class: 2032 case JVM_CONSTANT_String: 2033 case JVM_CONSTANT_ClassIndex: 2034 case JVM_CONSTANT_UnresolvedClass: 2035 case JVM_CONSTANT_UnresolvedClassInError: 2036 case JVM_CONSTANT_StringIndex: 2037 case JVM_CONSTANT_MethodType: 2038 case JVM_CONSTANT_MethodTypeInError: 2039 return 3; 2040 2041 case JVM_CONSTANT_MethodHandle: 2042 case JVM_CONSTANT_MethodHandleInError: 2043 return 4; //tag, ref_kind, ref_index 2044 2045 case JVM_CONSTANT_Integer: 2046 case JVM_CONSTANT_Float: 2047 case JVM_CONSTANT_Fieldref: 2048 case JVM_CONSTANT_Methodref: 2049 case JVM_CONSTANT_InterfaceMethodref: 2050 case JVM_CONSTANT_NameAndType: 2051 return 5; 2052 2053 case JVM_CONSTANT_Dynamic: 2054 case JVM_CONSTANT_DynamicInError: 2055 case JVM_CONSTANT_InvokeDynamic: 2056 // u1 tag, u2 bsm, u2 nt 2057 return 5; 2058 2059 case JVM_CONSTANT_Long: 2060 case JVM_CONSTANT_Double: 2061 return 9; 2062 } 2063 assert(false, "cpool_entry_size: Invalid constant pool entry tag"); 2064 return 1; 2065 } /* end cpool_entry_size */ 2066 2067 2068 // SymbolHashMap is used to find a constant pool index from a string. 2069 // This function fills in SymbolHashMaps, one for utf8s and one for 2070 // class names, returns size of the cpool raw bytes. 2071 jint ConstantPool::hash_entries_to(SymbolHashMap *symmap, 2072 SymbolHashMap *classmap) { 2073 jint size = 0; 2074 2075 for (u2 idx = 1; idx < length(); idx++) { 2076 u2 tag = tag_at(idx).value(); 2077 size += cpool_entry_size(idx); 2078 2079 switch(tag) { 2080 case JVM_CONSTANT_Utf8: { 2081 Symbol* sym = symbol_at(idx); 2082 symmap->add_entry(sym, idx); 2083 DBG(printf("adding symbol entry %s = %d\n", sym->as_utf8(), idx)); 2084 break; 2085 } 2086 case JVM_CONSTANT_Class: 2087 case JVM_CONSTANT_UnresolvedClass: 2088 case JVM_CONSTANT_UnresolvedClassInError: { 2089 Symbol* sym = klass_name_at(idx); 2090 classmap->add_entry(sym, idx); 2091 DBG(printf("adding class entry %s = %d\n", sym->as_utf8(), idx)); 2092 break; 2093 } 2094 case JVM_CONSTANT_Long: 2095 case JVM_CONSTANT_Double: { 2096 idx++; // Both Long and Double take two cpool slots 2097 break; 2098 } 2099 } 2100 } 2101 return size; 2102 } /* end hash_utf8_entries_to */ 2103 2104 2105 // Copy cpool bytes. 2106 // Returns: 2107 // 0, in case of OutOfMemoryError 2108 // -1, in case of internal error 2109 // > 0, count of the raw cpool bytes that have been copied 2110 int ConstantPool::copy_cpool_bytes(int cpool_size, 2111 SymbolHashMap* tbl, 2112 unsigned char *bytes) { 2113 u2 idx1, idx2; 2114 jint size = 0; 2115 jint cnt = length(); 2116 unsigned char *start_bytes = bytes; 2117 2118 for (jint idx = 1; idx < cnt; idx++) { 2119 u1 tag = tag_at(idx).value(); 2120 jint ent_size = cpool_entry_size(idx); 2121 2122 assert(size + ent_size <= cpool_size, "Size mismatch"); 2123 2124 *bytes = tag; 2125 DBG(printf("#%03hd tag=%03hd, ", (short)idx, (short)tag)); 2126 switch(tag) { 2127 case JVM_CONSTANT_Invalid: { 2128 DBG(printf("JVM_CONSTANT_Invalid")); 2129 break; 2130 } 2131 case JVM_CONSTANT_Unicode: { 2132 assert(false, "Wrong constant pool tag: JVM_CONSTANT_Unicode"); 2133 DBG(printf("JVM_CONSTANT_Unicode")); 2134 break; 2135 } 2136 case JVM_CONSTANT_Utf8: { 2137 Symbol* sym = symbol_at(idx); 2138 char* str = sym->as_utf8(); 2139 // Warning! It's crashing on x86 with len = sym->utf8_length() 2140 int len = (int) strlen(str); 2141 Bytes::put_Java_u2((address) (bytes+1), (u2) len); 2142 for (int i = 0; i < len; i++) { 2143 bytes[3+i] = (u1) str[i]; 2144 } 2145 DBG(printf("JVM_CONSTANT_Utf8: %s ", str)); 2146 break; 2147 } 2148 case JVM_CONSTANT_Integer: { 2149 jint val = int_at(idx); 2150 Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val); 2151 break; 2152 } 2153 case JVM_CONSTANT_Float: { 2154 jfloat val = float_at(idx); 2155 Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val); 2156 break; 2157 } 2158 case JVM_CONSTANT_Long: { 2159 jlong val = long_at(idx); 2160 Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val); 2161 idx++; // Long takes two cpool slots 2162 break; 2163 } 2164 case JVM_CONSTANT_Double: { 2165 jdouble val = double_at(idx); 2166 Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val); 2167 idx++; // Double takes two cpool slots 2168 break; 2169 } 2170 case JVM_CONSTANT_Class: 2171 case JVM_CONSTANT_UnresolvedClass: 2172 case JVM_CONSTANT_UnresolvedClassInError: { 2173 *bytes = JVM_CONSTANT_Class; 2174 Symbol* sym = klass_name_at(idx); 2175 idx1 = tbl->symbol_to_value(sym); 2176 assert(idx1 != 0, "Have not found a hashtable entry"); 2177 Bytes::put_Java_u2((address) (bytes+1), idx1); 2178 DBG(printf("JVM_CONSTANT_Class: idx=#%03hd, %s", idx1, sym->as_utf8())); 2179 break; 2180 } 2181 case JVM_CONSTANT_String: { 2182 *bytes = JVM_CONSTANT_String; 2183 Symbol* sym = unresolved_string_at(idx); 2184 idx1 = tbl->symbol_to_value(sym); 2185 assert(idx1 != 0, "Have not found a hashtable entry"); 2186 Bytes::put_Java_u2((address) (bytes+1), idx1); 2187 DBG(printf("JVM_CONSTANT_String: idx=#%03hd, %s", idx1, sym->as_utf8())); 2188 break; 2189 } 2190 case JVM_CONSTANT_Fieldref: 2191 case JVM_CONSTANT_Methodref: 2192 case JVM_CONSTANT_InterfaceMethodref: { 2193 idx1 = uncached_klass_ref_index_at(idx); 2194 idx2 = uncached_name_and_type_ref_index_at(idx); 2195 Bytes::put_Java_u2((address) (bytes+1), idx1); 2196 Bytes::put_Java_u2((address) (bytes+3), idx2); 2197 DBG(printf("JVM_CONSTANT_Methodref: %hd %hd", idx1, idx2)); 2198 break; 2199 } 2200 case JVM_CONSTANT_NameAndType: { 2201 idx1 = name_ref_index_at(idx); 2202 idx2 = signature_ref_index_at(idx); 2203 Bytes::put_Java_u2((address) (bytes+1), idx1); 2204 Bytes::put_Java_u2((address) (bytes+3), idx2); 2205 DBG(printf("JVM_CONSTANT_NameAndType: %hd %hd", idx1, idx2)); 2206 break; 2207 } 2208 case JVM_CONSTANT_ClassIndex: { 2209 *bytes = JVM_CONSTANT_Class; 2210 idx1 = klass_index_at(idx); 2211 Bytes::put_Java_u2((address) (bytes+1), idx1); 2212 DBG(printf("JVM_CONSTANT_ClassIndex: %hd", idx1)); 2213 break; 2214 } 2215 case JVM_CONSTANT_StringIndex: { 2216 *bytes = JVM_CONSTANT_String; 2217 idx1 = string_index_at(idx); 2218 Bytes::put_Java_u2((address) (bytes+1), idx1); 2219 DBG(printf("JVM_CONSTANT_StringIndex: %hd", idx1)); 2220 break; 2221 } 2222 case JVM_CONSTANT_MethodHandle: 2223 case JVM_CONSTANT_MethodHandleInError: { 2224 *bytes = JVM_CONSTANT_MethodHandle; 2225 int kind = method_handle_ref_kind_at(idx); 2226 idx1 = method_handle_index_at(idx); 2227 *(bytes+1) = (unsigned char) kind; 2228 Bytes::put_Java_u2((address) (bytes+2), idx1); 2229 DBG(printf("JVM_CONSTANT_MethodHandle: %d %hd", kind, idx1)); 2230 break; 2231 } 2232 case JVM_CONSTANT_MethodType: 2233 case JVM_CONSTANT_MethodTypeInError: { 2234 *bytes = JVM_CONSTANT_MethodType; 2235 idx1 = method_type_index_at(idx); 2236 Bytes::put_Java_u2((address) (bytes+1), idx1); 2237 DBG(printf("JVM_CONSTANT_MethodType: %hd", idx1)); 2238 break; 2239 } 2240 case JVM_CONSTANT_Dynamic: 2241 case JVM_CONSTANT_DynamicInError: { 2242 *bytes = tag; 2243 idx1 = extract_low_short_from_int(*int_at_addr(idx)); 2244 idx2 = extract_high_short_from_int(*int_at_addr(idx)); 2245 assert(idx2 == invoke_dynamic_name_and_type_ref_index_at(idx), "correct half of u4"); 2246 Bytes::put_Java_u2((address) (bytes+1), idx1); 2247 Bytes::put_Java_u2((address) (bytes+3), idx2); 2248 DBG(printf("JVM_CONSTANT_Dynamic: %hd %hd", idx1, idx2)); 2249 break; 2250 } 2251 case JVM_CONSTANT_InvokeDynamic: { 2252 *bytes = tag; 2253 idx1 = extract_low_short_from_int(*int_at_addr(idx)); 2254 idx2 = extract_high_short_from_int(*int_at_addr(idx)); 2255 assert(idx2 == invoke_dynamic_name_and_type_ref_index_at(idx), "correct half of u4"); 2256 Bytes::put_Java_u2((address) (bytes+1), idx1); 2257 Bytes::put_Java_u2((address) (bytes+3), idx2); 2258 DBG(printf("JVM_CONSTANT_InvokeDynamic: %hd %hd", idx1, idx2)); 2259 break; 2260 } 2261 } 2262 DBG(printf("\n")); 2263 bytes += ent_size; 2264 size += ent_size; 2265 } 2266 assert(size == cpool_size, "Size mismatch"); 2267 2268 // Keep temorarily for debugging until it's stable. 2269 DBG(print_cpool_bytes(cnt, start_bytes)); 2270 return (int)(bytes - start_bytes); 2271 } /* end copy_cpool_bytes */ 2272 2273 #undef DBG 2274 2275 2276 void ConstantPool::set_on_stack(const bool value) { 2277 if (value) { 2278 // Only record if it's not already set. 2279 if (!on_stack()) { 2280 assert(!is_shared(), "should always be set for shared constant pools"); 2281 _flags |= _on_stack; 2282 MetadataOnStackMark::record(this); 2283 } 2284 } else { 2285 // Clearing is done single-threadedly. 2286 if (!is_shared()) { 2287 _flags &= ~_on_stack; 2288 } 2289 } 2290 } 2291 2292 // JSR 292 support for patching constant pool oops after the class is linked and 2293 // the oop array for resolved references are created. 2294 // We can't do this during classfile parsing, which is how the other indexes are 2295 // patched. The other patches are applied early for some error checking 2296 // so only defer the pseudo_strings. 2297 void ConstantPool::patch_resolved_references(GrowableArray<Handle>* cp_patches) { 2298 for (int index = 1; index < cp_patches->length(); index++) { // Index 0 is unused 2299 Handle patch = cp_patches->at(index); 2300 if (patch.not_null()) { 2301 assert (tag_at(index).is_string(), "should only be string left"); 2302 // Patching a string means pre-resolving it. 2303 // The spelling in the constant pool is ignored. 2304 // The constant reference may be any object whatever. 2305 // If it is not a real interned string, the constant is referred 2306 // to as a "pseudo-string", and must be presented to the CP 2307 // explicitly, because it may require scavenging. 2308 int obj_index = cp_to_object_index(index); 2309 pseudo_string_at_put(index, obj_index, patch()); 2310 DEBUG_ONLY(cp_patches->at_put(index, Handle());) 2311 } 2312 } 2313 #ifdef ASSERT 2314 // Ensure that all the patches have been used. 2315 for (int index = 0; index < cp_patches->length(); index++) { 2316 assert(cp_patches->at(index).is_null(), 2317 "Unused constant pool patch at %d in class file %s", 2318 index, 2319 pool_holder()->external_name()); 2320 } 2321 #endif // ASSERT 2322 } 2323 2324 #ifndef PRODUCT 2325 2326 // CompileTheWorld support. Preload all classes loaded references in the passed in constantpool 2327 void ConstantPool::preload_and_initialize_all_classes(ConstantPool* obj, TRAPS) { 2328 guarantee(obj->is_constantPool(), "object must be constant pool"); 2329 constantPoolHandle cp(THREAD, (ConstantPool*)obj); 2330 guarantee(cp->pool_holder() != NULL, "must be fully loaded"); 2331 2332 for (int i = 0; i< cp->length(); i++) { 2333 if (cp->tag_at(i).is_unresolved_klass()) { 2334 // This will force loading of the class 2335 Klass* klass = cp->klass_at(i, CHECK); 2336 if (klass->is_instance_klass()) { 2337 // Force initialization of class 2338 InstanceKlass::cast(klass)->initialize(CHECK); 2339 } 2340 } 2341 } 2342 } 2343 2344 #endif 2345 2346 2347 // Printing 2348 2349 void ConstantPool::print_on(outputStream* st) const { 2350 assert(is_constantPool(), "must be constantPool"); 2351 st->print_cr("%s", internal_name()); 2352 if (flags() != 0) { 2353 st->print(" - flags: 0x%x", flags()); 2354 if (has_preresolution()) st->print(" has_preresolution"); 2355 if (on_stack()) st->print(" on_stack"); 2356 st->cr(); 2357 } 2358 if (pool_holder() != NULL) { 2359 st->print_cr(" - holder: " INTPTR_FORMAT, p2i(pool_holder())); 2360 } 2361 st->print_cr(" - cache: " INTPTR_FORMAT, p2i(cache())); 2362 st->print_cr(" - resolved_references: " INTPTR_FORMAT, p2i(resolved_references())); 2363 st->print_cr(" - reference_map: " INTPTR_FORMAT, p2i(reference_map())); 2364 st->print_cr(" - resolved_klasses: " INTPTR_FORMAT, p2i(resolved_klasses())); 2365 2366 for (int index = 1; index < length(); index++) { // Index 0 is unused 2367 ((ConstantPool*)this)->print_entry_on(index, st); 2368 switch (tag_at(index).value()) { 2369 case JVM_CONSTANT_Long : 2370 case JVM_CONSTANT_Double : 2371 index++; // Skip entry following eigth-byte constant 2372 } 2373 2374 } 2375 st->cr(); 2376 } 2377 2378 // Print one constant pool entry 2379 void ConstantPool::print_entry_on(const int index, outputStream* st) { 2380 EXCEPTION_MARK; 2381 st->print(" - %3d : ", index); 2382 tag_at(index).print_on(st); 2383 st->print(" : "); 2384 switch (tag_at(index).value()) { 2385 case JVM_CONSTANT_Class : 2386 { Klass* k = klass_at(index, CATCH); 2387 guarantee(k != NULL, "need klass"); 2388 k->print_value_on(st); 2389 st->print(" {" PTR_FORMAT "}", p2i(k)); 2390 } 2391 break; 2392 case JVM_CONSTANT_Fieldref : 2393 case JVM_CONSTANT_Methodref : 2394 case JVM_CONSTANT_InterfaceMethodref : 2395 st->print("klass_index=%d", uncached_klass_ref_index_at(index)); 2396 st->print(" name_and_type_index=%d", uncached_name_and_type_ref_index_at(index)); 2397 break; 2398 case JVM_CONSTANT_String : 2399 if (is_pseudo_string_at(index)) { 2400 oop anObj = pseudo_string_at(index); 2401 anObj->print_value_on(st); 2402 st->print(" {" PTR_FORMAT "}", p2i(anObj)); 2403 } else { 2404 unresolved_string_at(index)->print_value_on(st); 2405 } 2406 break; 2407 case JVM_CONSTANT_Integer : 2408 st->print("%d", int_at(index)); 2409 break; 2410 case JVM_CONSTANT_Float : 2411 st->print("%f", float_at(index)); 2412 break; 2413 case JVM_CONSTANT_Long : 2414 st->print_jlong(long_at(index)); 2415 break; 2416 case JVM_CONSTANT_Double : 2417 st->print("%lf", double_at(index)); 2418 break; 2419 case JVM_CONSTANT_NameAndType : 2420 st->print("name_index=%d", name_ref_index_at(index)); 2421 st->print(" signature_index=%d", signature_ref_index_at(index)); 2422 break; 2423 case JVM_CONSTANT_Utf8 : 2424 symbol_at(index)->print_value_on(st); 2425 break; 2426 case JVM_CONSTANT_ClassIndex: { 2427 int name_index = *int_at_addr(index); 2428 st->print("klass_index=%d ", name_index); 2429 symbol_at(name_index)->print_value_on(st); 2430 } 2431 break; 2432 case JVM_CONSTANT_UnresolvedClass : // fall-through 2433 case JVM_CONSTANT_UnresolvedClassInError: { 2434 CPKlassSlot kslot = klass_slot_at(index); 2435 int resolved_klass_index = kslot.resolved_klass_index(); 2436 int name_index = kslot.name_index(); 2437 assert(tag_at(name_index).is_symbol(), "sanity"); 2438 2439 Klass* klass = resolved_klasses()->at(resolved_klass_index); 2440 if (klass != NULL) { 2441 klass->print_value_on(st); 2442 } else { 2443 symbol_at(name_index)->print_value_on(st); 2444 } 2445 } 2446 break; 2447 case JVM_CONSTANT_MethodHandle : 2448 case JVM_CONSTANT_MethodHandleInError : 2449 st->print("ref_kind=%d", method_handle_ref_kind_at(index)); 2450 st->print(" ref_index=%d", method_handle_index_at(index)); 2451 break; 2452 case JVM_CONSTANT_MethodType : 2453 case JVM_CONSTANT_MethodTypeInError : 2454 st->print("signature_index=%d", method_type_index_at(index)); 2455 break; 2456 case JVM_CONSTANT_Dynamic : 2457 case JVM_CONSTANT_DynamicInError : 2458 { 2459 st->print("bootstrap_method_index=%d", invoke_dynamic_bootstrap_method_ref_index_at(index)); 2460 st->print(" type_index=%d", invoke_dynamic_name_and_type_ref_index_at(index)); 2461 int argc = invoke_dynamic_argument_count_at(index); 2462 if (argc > 0) { 2463 for (int arg_i = 0; arg_i < argc; arg_i++) { 2464 int arg = invoke_dynamic_argument_index_at(index, arg_i); 2465 st->print((arg_i == 0 ? " arguments={%d" : ", %d"), arg); 2466 } 2467 st->print("}"); 2468 } 2469 } 2470 break; 2471 case JVM_CONSTANT_InvokeDynamic : 2472 { 2473 st->print("bootstrap_method_index=%d", invoke_dynamic_bootstrap_method_ref_index_at(index)); 2474 st->print(" name_and_type_index=%d", invoke_dynamic_name_and_type_ref_index_at(index)); 2475 int argc = invoke_dynamic_argument_count_at(index); 2476 if (argc > 0) { 2477 for (int arg_i = 0; arg_i < argc; arg_i++) { 2478 int arg = invoke_dynamic_argument_index_at(index, arg_i); 2479 st->print((arg_i == 0 ? " arguments={%d" : ", %d"), arg); 2480 } 2481 st->print("}"); 2482 } 2483 } 2484 break; 2485 default: 2486 ShouldNotReachHere(); 2487 break; 2488 } 2489 st->cr(); 2490 } 2491 2492 void ConstantPool::print_value_on(outputStream* st) const { 2493 assert(is_constantPool(), "must be constantPool"); 2494 st->print("constant pool [%d]", length()); 2495 if (has_preresolution()) st->print("/preresolution"); 2496 if (operands() != NULL) st->print("/operands[%d]", operands()->length()); 2497 print_address_on(st); 2498 if (pool_holder() != NULL) { 2499 st->print(" for "); 2500 pool_holder()->print_value_on(st); 2501 bool extra = (pool_holder()->constants() != this); 2502 if (extra) st->print(" (extra)"); 2503 } 2504 if (cache() != NULL) { 2505 st->print(" cache=" PTR_FORMAT, p2i(cache())); 2506 } 2507 } 2508 2509 #if INCLUDE_SERVICES 2510 // Size Statistics 2511 void ConstantPool::collect_statistics(KlassSizeStats *sz) const { 2512 sz->_cp_all_bytes += (sz->_cp_bytes = sz->count(this)); 2513 sz->_cp_all_bytes += (sz->_cp_tags_bytes = sz->count_array(tags())); 2514 sz->_cp_all_bytes += (sz->_cp_cache_bytes = sz->count(cache())); 2515 sz->_cp_all_bytes += (sz->_cp_operands_bytes = sz->count_array(operands())); 2516 sz->_cp_all_bytes += (sz->_cp_refmap_bytes = sz->count_array(reference_map())); 2517 2518 sz->_ro_bytes += sz->_cp_operands_bytes + sz->_cp_tags_bytes + 2519 sz->_cp_refmap_bytes; 2520 sz->_rw_bytes += sz->_cp_bytes + sz->_cp_cache_bytes; 2521 } 2522 #endif // INCLUDE_SERVICES 2523 2524 // Verification 2525 2526 void ConstantPool::verify_on(outputStream* st) { 2527 guarantee(is_constantPool(), "object must be constant pool"); 2528 for (int i = 0; i< length(); i++) { 2529 constantTag tag = tag_at(i); 2530 if (tag.is_klass() || tag.is_unresolved_klass()) { 2531 guarantee(klass_name_at(i)->refcount() != 0, "should have nonzero reference count"); 2532 } else if (tag.is_symbol()) { 2533 CPSlot entry = slot_at(i); 2534 guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count"); 2535 } else if (tag.is_string()) { 2536 CPSlot entry = slot_at(i); 2537 guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count"); 2538 } 2539 } 2540 if (cache() != NULL) { 2541 // Note: cache() can be NULL before a class is completely setup or 2542 // in temporary constant pools used during constant pool merging 2543 guarantee(cache()->is_constantPoolCache(), "should be constant pool cache"); 2544 } 2545 if (pool_holder() != NULL) { 2546 // Note: pool_holder() can be NULL in temporary constant pools 2547 // used during constant pool merging 2548 guarantee(pool_holder()->is_klass(), "should be klass"); 2549 } 2550 } 2551 2552 2553 SymbolHashMap::~SymbolHashMap() { 2554 SymbolHashMapEntry* next; 2555 for (int i = 0; i < _table_size; i++) { 2556 for (SymbolHashMapEntry* cur = bucket(i); cur != NULL; cur = next) { 2557 next = cur->next(); 2558 delete(cur); 2559 } 2560 } 2561 FREE_C_HEAP_ARRAY(SymbolHashMapBucket, _buckets); 2562 } 2563 2564 void SymbolHashMap::add_entry(Symbol* sym, u2 value) { 2565 char *str = sym->as_utf8(); 2566 unsigned int hash = compute_hash(str, sym->utf8_length()); 2567 unsigned int index = hash % table_size(); 2568 2569 // check if already in map 2570 // we prefer the first entry since it is more likely to be what was used in 2571 // the class file 2572 for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) { 2573 assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL"); 2574 if (en->hash() == hash && en->symbol() == sym) { 2575 return; // already there 2576 } 2577 } 2578 2579 SymbolHashMapEntry* entry = new SymbolHashMapEntry(hash, sym, value); 2580 entry->set_next(bucket(index)); 2581 _buckets[index].set_entry(entry); 2582 assert(entry->symbol() != NULL, "SymbolHashMapEntry symbol is NULL"); 2583 } 2584 2585 SymbolHashMapEntry* SymbolHashMap::find_entry(Symbol* sym) { 2586 assert(sym != NULL, "SymbolHashMap::find_entry - symbol is NULL"); 2587 char *str = sym->as_utf8(); 2588 int len = sym->utf8_length(); 2589 unsigned int hash = SymbolHashMap::compute_hash(str, len); 2590 unsigned int index = hash % table_size(); 2591 for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) { 2592 assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL"); 2593 if (en->hash() == hash && en->symbol() == sym) { 2594 return en; 2595 } 2596 } 2597 return NULL; 2598 } 2599 2600 void SymbolHashMap::initialize_table(int table_size) { 2601 _table_size = table_size; 2602 _buckets = NEW_C_HEAP_ARRAY(SymbolHashMapBucket, table_size, mtSymbol); 2603 for (int index = 0; index < table_size; index++) { 2604 _buckets[index].clear(); 2605 } 2606 }