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