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