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