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