< prev index next >

src/hotspot/share/oops/constantPool.cpp

Print this page

  32 #include "classfile/metadataOnStackMark.hpp"
  33 #include "classfile/stringTable.hpp"
  34 #include "classfile/systemDictionary.hpp"
  35 #include "classfile/vmClasses.hpp"
  36 #include "classfile/vmSymbols.hpp"
  37 #include "code/codeCache.hpp"
  38 #include "interpreter/bootstrapInfo.hpp"
  39 #include "interpreter/linkResolver.hpp"
  40 #include "jvm.h"
  41 #include "logging/log.hpp"
  42 #include "logging/logStream.hpp"
  43 #include "memory/allocation.inline.hpp"
  44 #include "memory/metadataFactory.hpp"
  45 #include "memory/metaspaceClosure.hpp"
  46 #include "memory/oopFactory.hpp"
  47 #include "memory/resourceArea.hpp"
  48 #include "memory/universe.hpp"
  49 #include "oops/array.hpp"
  50 #include "oops/constantPool.inline.hpp"
  51 #include "oops/cpCache.inline.hpp"

  52 #include "oops/instanceKlass.hpp"
  53 #include "oops/klass.inline.hpp"
  54 #include "oops/objArrayKlass.hpp"
  55 #include "oops/objArrayOop.inline.hpp"
  56 #include "oops/oop.inline.hpp"
  57 #include "oops/typeArrayOop.inline.hpp"
  58 #include "prims/jvmtiExport.hpp"
  59 #include "runtime/atomic.hpp"
  60 #include "runtime/handles.inline.hpp"
  61 #include "runtime/init.hpp"
  62 #include "runtime/javaCalls.hpp"
  63 #include "runtime/javaThread.hpp"
  64 #include "runtime/signature.hpp"
  65 #include "runtime/vframe.inline.hpp"
  66 #include "utilities/copy.hpp"
  67 
  68 ConstantPool* ConstantPool::allocate(ClassLoaderData* loader_data, int length, TRAPS) {
  69   Array<u1>* tags = MetadataFactory::new_array<u1>(loader_data, length, 0, CHECK_NULL);
  70   int size = ConstantPool::size(length);
  71   return new (loader_data, size, MetaspaceObj::ConstantPoolType, THREAD) ConstantPool(tags);

 227   assert(resolved_klasses() == NULL, "sanity");
 228   Array<Klass*>* rk = MetadataFactory::new_array<Klass*>(loader_data, num_klasses, CHECK);
 229   set_resolved_klasses(rk);
 230 }
 231 
 232 void ConstantPool::initialize_unresolved_klasses(ClassLoaderData* loader_data, TRAPS) {
 233   int len = length();
 234   int num_klasses = 0;
 235   for (int i = 1; i <len; i++) {
 236     switch (tag_at(i).value()) {
 237     case JVM_CONSTANT_ClassIndex:
 238       {
 239         const int class_index = klass_index_at(i);
 240         unresolved_klass_at_put(i, class_index, num_klasses++);
 241       }
 242       break;
 243 #ifndef PRODUCT
 244     case JVM_CONSTANT_Class:
 245     case JVM_CONSTANT_UnresolvedClass:
 246     case JVM_CONSTANT_UnresolvedClassInError:
 247       // All of these should have been reverted back to ClassIndex before calling
 248       // this function.
 249       ShouldNotReachHere();
 250 #endif
 251     }
 252   }
 253   allocate_resolved_klasses(loader_data, num_klasses, THREAD);
 254 }
 255 
 256 // Hidden class support:
 257 void ConstantPool::klass_at_put(int class_index, Klass* k) {
 258   assert(k != NULL, "must be valid klass");
 259   CPKlassSlot kslot = klass_slot_at(class_index);
 260   int resolved_klass_index = kslot.resolved_klass_index();
 261   Klass** adr = resolved_klasses()->adr_at(resolved_klass_index);
 262   Atomic::release_store(adr, k);
 263 
 264   // The interpreter assumes when the tag is stored, the klass is resolved
 265   // and the Klass* non-NULL, so we need hardware store ordering here.

 266   release_tag_at_put(class_index, JVM_CONSTANT_Class);
 267 }
 268 
 269 #if INCLUDE_CDS_JAVA_HEAP
 270 // Archive the resolved references
 271 void ConstantPool::archive_resolved_references() {
 272   if (_cache == NULL) {
 273     return; // nothing to do
 274   }
 275 
 276   InstanceKlass *ik = pool_holder();
 277   if (!(ik->is_shared_boot_class() || ik->is_shared_platform_class() ||
 278         ik->is_shared_app_class())) {
 279     // Archiving resolved references for classes from non-builtin loaders
 280     // is not yet supported.
 281     return;
 282   }
 283 
 284   objArrayOop rr = resolved_references();
 285   Array<u2>* ref_map = reference_map();

 372   // class redefinition. Since shared ConstantPools cannot be deallocated anyway,
 373   // we always set _on_stack to true to avoid having to change _flags during runtime.
 374   _flags |= (_on_stack | _is_shared);
 375 
 376   if (!_pool_holder->is_linked() && !_pool_holder->verified_at_dump_time()) {
 377     return;
 378   }
 379   // Resolved references are not in the shared archive.
 380   // Save the length for restoration.  It is not necessarily the same length
 381   // as reference_map.length() if invokedynamic is saved. It is needed when
 382   // re-creating the resolved reference array if archived heap data cannot be map
 383   // at runtime.
 384   set_resolved_reference_length(
 385     resolved_references() != NULL ? resolved_references()->length() : 0);
 386   set_resolved_references(OopHandle());
 387 
 388   bool archived = false;
 389   for (int index = 1; index < length(); index++) { // Index 0 is unused
 390     switch (tag_at(index).value()) {
 391     case JVM_CONSTANT_UnresolvedClassInError:
 392       tag_at_put(index, JVM_CONSTANT_UnresolvedClass);



 393       break;
 394     case JVM_CONSTANT_MethodHandleInError:
 395       tag_at_put(index, JVM_CONSTANT_MethodHandle);
 396       break;
 397     case JVM_CONSTANT_MethodTypeInError:
 398       tag_at_put(index, JVM_CONSTANT_MethodType);
 399       break;
 400     case JVM_CONSTANT_DynamicInError:
 401       tag_at_put(index, JVM_CONSTANT_Dynamic);
 402       break;
 403     case JVM_CONSTANT_Class:
 404       archived = maybe_archive_resolved_klass_at(index);
 405       ArchiveBuilder::alloc_stats()->record_klass_cp_entry(archived);
 406       break;
 407     }
 408   }
 409 
 410   if (cache() != NULL) {
 411     // cache() is NULL if this class is not yet linked.
 412     cache()->remove_unshareable_info();

 428   int resolved_klass_index = kslot.resolved_klass_index();
 429   Klass* k = resolved_klasses()->at(resolved_klass_index);
 430   // k could be NULL if the referenced class has been excluded via
 431   // SystemDictionaryShared::is_excluded_class().
 432 
 433   if (k != NULL) {
 434     ConstantPool* src_cp = ArchiveBuilder::current()->get_source_addr(this);
 435     if (ClassPrelinker::can_archive_resolved_klass(src_cp, cp_index)) {
 436       if (log_is_enabled(Debug, cds, resolve)) {
 437         ResourceMark rm;
 438         log_debug(cds, resolve)("Resolved klass CP entry [%d]: %s => %s", cp_index,
 439                                 pool_holder()->external_name(), k->external_name());
 440       }
 441       return true;
 442     }
 443   }
 444 
 445   // This referenced class cannot be archived. Revert the tag to UnresolvedClass,
 446   // so that the proper class loading and initialization can happen at runtime.
 447   resolved_klasses()->at_put(resolved_klass_index, NULL);
 448   tag_at_put(cp_index, JVM_CONSTANT_UnresolvedClass);

 449   return false;
 450 }
 451 #endif // INCLUDE_CDS
 452 
 453 int ConstantPool::cp_to_object_index(int cp_index) {
 454   // this is harder don't do this so much.
 455   int i = reference_map()->find(cp_index);
 456   // We might not find the index for jsr292 call.
 457   return (i < 0) ? _no_index_sentinel : i;
 458 }
 459 
 460 void ConstantPool::string_at_put(int which, int obj_index, oop str) {
 461   oop result = set_resolved_reference_at(obj_index, str);
 462   assert(result == nullptr || result == str, "Only set once or to the same string.");
 463 }
 464 
 465 void ConstantPool::trace_class_resolution(const constantPoolHandle& this_cp, Klass* k) {
 466   ResourceMark rm;
 467   int line_number = -1;
 468   const char * source_file = NULL;

 474       Symbol* s = vfst.method()->method_holder()->source_file_name();
 475       if (s != NULL) {
 476         source_file = s->as_C_string();
 477       }
 478     }
 479   }
 480   if (k != this_cp->pool_holder()) {
 481     // only print something if the classes are different
 482     if (source_file != NULL) {
 483       log_debug(class, resolve)("%s %s %s:%d",
 484                  this_cp->pool_holder()->external_name(),
 485                  k->external_name(), source_file, line_number);
 486     } else {
 487       log_debug(class, resolve)("%s %s",
 488                  this_cp->pool_holder()->external_name(),
 489                  k->external_name());
 490     }
 491   }
 492 }
 493 






 494 Klass* ConstantPool::klass_at_impl(const constantPoolHandle& this_cp, int which,
 495                                    TRAPS) {
 496   JavaThread* javaThread = THREAD;
 497 
 498   // A resolved constantPool entry will contain a Klass*, otherwise a Symbol*.
 499   // It is not safe to rely on the tag bit's here, since we don't have a lock, and
 500   // the entry and tag is not updated atomically.
 501   CPKlassSlot kslot = this_cp->klass_slot_at(which);
 502   int resolved_klass_index = kslot.resolved_klass_index();
 503   int name_index = kslot.name_index();
 504   assert(this_cp->tag_at(name_index).is_symbol(), "sanity");
 505 
 506   // The tag must be JVM_CONSTANT_Class in order to read the correct value from
 507   // the unresolved_klasses() array.
 508   if (this_cp->tag_at(which).is_klass()) {
 509     Klass* klass = this_cp->resolved_klasses()->at(resolved_klass_index);
 510     if (klass != NULL) {
 511       return klass;
 512     }
 513   }
 514 
 515   // This tag doesn't change back to unresolved class unless at a safepoint.
 516   if (this_cp->tag_at(which).is_unresolved_klass_in_error()) {
 517     // The original attempt to resolve this constant pool entry failed so find the
 518     // class of the original error and throw another error of the same class
 519     // (JVMS 5.4.3).
 520     // If there is a detail message, pass that detail message to the error.
 521     // The JVMS does not strictly require us to duplicate the same detail message,
 522     // or any internal exception fields such as cause or stacktrace.  But since the
 523     // detail message is often a class name or other literal string, we will repeat it
 524     // if we can find it in the symbol table.
 525     throw_resolution_error(this_cp, which, CHECK_NULL);
 526     ShouldNotReachHere();
 527   }
 528 
 529   Handle mirror_handle;
 530   Symbol* name = this_cp->symbol_at(name_index);





 531   Handle loader (THREAD, this_cp->pool_holder()->class_loader());
 532   Handle protection_domain (THREAD, this_cp->pool_holder()->protection_domain());
 533 
 534   Klass* k;
 535   {
 536     // Turn off the single stepping while doing class resolution
 537     JvmtiHideSingleStepping jhss(javaThread);
 538     k = SystemDictionary::resolve_or_fail(name, loader, protection_domain, true, THREAD);
 539   } //  JvmtiHideSingleStepping jhss(javaThread);



 540 
 541   if (!HAS_PENDING_EXCEPTION) {
 542     // preserve the resolved klass from unloading
 543     mirror_handle = Handle(THREAD, k->java_mirror());
 544     // Do access check for klasses
 545     verify_constant_pool_resolve(this_cp, k, THREAD);
 546   }
 547 
















 548   // Failed to resolve class. We must record the errors so that subsequent attempts
 549   // to resolve this constant pool entry fail with the same error (JVMS 5.4.3).
 550   if (HAS_PENDING_EXCEPTION) {
 551     save_and_throw_exception(this_cp, which, constantTag(JVM_CONSTANT_UnresolvedClass), CHECK_NULL);




 552     // If CHECK_NULL above doesn't return the exception, that means that
 553     // some other thread has beaten us and has resolved the class.
 554     // To preserve old behavior, we return the resolved class.
 555     Klass* klass = this_cp->resolved_klasses()->at(resolved_klass_index);
 556     assert(klass != NULL, "must be resolved if exception was cleared");
 557     return klass;
 558   }
 559 
 560   // logging for class+resolve.
 561   if (log_is_enabled(Debug, class, resolve)){
 562     trace_class_resolution(this_cp, k);
 563   }
 564 
 565   Klass** adr = this_cp->resolved_klasses()->adr_at(resolved_klass_index);
 566   Atomic::release_store(adr, k);
 567   // The interpreter assumes when the tag is stored, the klass is resolved
 568   // and the Klass* stored in _resolved_klasses is non-NULL, so we need
 569   // hardware store ordering here.




 570   // We also need to CAS to not overwrite an error from a racing thread.
 571 
 572   jbyte old_tag = Atomic::cmpxchg((jbyte*)this_cp->tag_addr_at(which),
 573                                   (jbyte)JVM_CONSTANT_UnresolvedClass,
 574                                   (jbyte)JVM_CONSTANT_Class);
 575 
 576   // We need to recheck exceptions from racing thread and return the same.
 577   if (old_tag == JVM_CONSTANT_UnresolvedClassInError) {
 578     // Remove klass.
 579     this_cp->resolved_klasses()->at_put(resolved_klass_index, NULL);
 580     throw_resolution_error(this_cp, which, CHECK_NULL);
 581   }
 582 
 583   return k;
 584 }
 585 
 586 
 587 // Does not update ConstantPool* - to avoid any exception throwing. Used
 588 // by compiler and exception handling.  Also used to avoid classloads for
 589 // instanceof operations. Returns NULL if the class has not been loaded or
 590 // if the verification of constant pool failed
 591 Klass* ConstantPool::klass_at_if_loaded(const constantPoolHandle& this_cp, int which) {
 592   CPKlassSlot kslot = this_cp->klass_slot_at(which);
 593   int resolved_klass_index = kslot.resolved_klass_index();
 594   int name_index = kslot.name_index();

 987     case JVM_CONSTANT_Long:
 988     case JVM_CONSTANT_Double:
 989       // these guys trigger OOM at worst
 990       break;
 991     default:
 992       (*status_return) = false;
 993       return NULL;
 994     }
 995     // from now on there is either success or an OOME
 996     (*status_return) = true;
 997   }
 998 
 999   switch (tag.value()) {
1000 
1001   case JVM_CONSTANT_UnresolvedClass:
1002   case JVM_CONSTANT_Class:
1003     {
1004       assert(cache_index == _no_index_sentinel, "should not have been set");
1005       Klass* resolved = klass_at_impl(this_cp, index, CHECK_NULL);
1006       // ldc wants the java mirror.
1007       result_oop = resolved->java_mirror();


1008       break;
1009     }
1010 
1011   case JVM_CONSTANT_Dynamic:
1012     {
1013       // Resolve the Dynamically-Computed constant to invoke the BSM in order to obtain the resulting oop.
1014       BootstrapInfo bootstrap_specifier(this_cp, index);
1015 
1016       // The initial step in resolving an unresolved symbolic reference to a
1017       // dynamically-computed constant is to resolve the symbolic reference to a
1018       // method handle which will be the bootstrap method for the dynamically-computed
1019       // constant. If resolution of the java.lang.invoke.MethodHandle for the bootstrap
1020       // method fails, then a MethodHandleInError is stored at the corresponding
1021       // bootstrap method's CP index for the CONSTANT_MethodHandle_info. No need to
1022       // set a DynamicConstantInError here since any subsequent use of this
1023       // bootstrap method will encounter the resolution of MethodHandleInError.
1024       // Both the first, (resolution of the BSM and its static arguments), and the second tasks,
1025       // (invocation of the BSM), of JVMS Section 5.4.3.6 occur within invoke_bootstrap_method()
1026       // for the bootstrap_specifier created above.
1027       SystemDictionary::invoke_bootstrap_method(bootstrap_specifier, THREAD);

1904       case JVM_CONSTANT_Long: {
1905         u8 val = Bytes::get_Java_u8(bytes);
1906         printf("long         " INT64_FORMAT, (int64_t) *(jlong *) &val);
1907         ent_size = 8;
1908         idx++; // Long takes two cpool slots
1909         break;
1910       }
1911       case JVM_CONSTANT_Double: {
1912         u8 val = Bytes::get_Java_u8(bytes);
1913         printf("double       %5.3fd", *(jdouble *)&val);
1914         ent_size = 8;
1915         idx++; // Double takes two cpool slots
1916         break;
1917       }
1918       case JVM_CONSTANT_Class: {
1919         idx1 = Bytes::get_Java_u2(bytes);
1920         printf("class        #%03d", idx1);
1921         ent_size = 2;
1922         break;
1923       }






1924       case JVM_CONSTANT_String: {
1925         idx1 = Bytes::get_Java_u2(bytes);
1926         printf("String       #%03d", idx1);
1927         ent_size = 2;
1928         break;
1929       }
1930       case JVM_CONSTANT_Fieldref: {
1931         idx1 = Bytes::get_Java_u2(bytes);
1932         idx2 = Bytes::get_Java_u2(bytes+2);
1933         printf("Field        #%03d, #%03d", (int) idx1, (int) idx2);
1934         ent_size = 4;
1935         break;
1936       }
1937       case JVM_CONSTANT_Methodref: {
1938         idx1 = Bytes::get_Java_u2(bytes);
1939         idx2 = Bytes::get_Java_u2(bytes+2);
1940         printf("Method       #%03d, #%03d", idx1, idx2);
1941         ent_size = 4;
1942         break;
1943       }

1946         idx2 = Bytes::get_Java_u2(bytes+2);
1947         printf("InterfMethod #%03d, #%03d", idx1, idx2);
1948         ent_size = 4;
1949         break;
1950       }
1951       case JVM_CONSTANT_NameAndType: {
1952         idx1 = Bytes::get_Java_u2(bytes);
1953         idx2 = Bytes::get_Java_u2(bytes+2);
1954         printf("NameAndType  #%03d, #%03d", idx1, idx2);
1955         ent_size = 4;
1956         break;
1957       }
1958       case JVM_CONSTANT_ClassIndex: {
1959         printf("ClassIndex  %s", WARN_MSG);
1960         break;
1961       }
1962       case JVM_CONSTANT_UnresolvedClass: {
1963         printf("UnresolvedClass: %s", WARN_MSG);
1964         break;
1965       }




1966       case JVM_CONSTANT_UnresolvedClassInError: {
1967         printf("UnresolvedClassInErr: %s", WARN_MSG);
1968         break;
1969       }
1970       case JVM_CONSTANT_StringIndex: {
1971         printf("StringIndex: %s", WARN_MSG);
1972         break;
1973       }
1974     }
1975     printf(";\n");
1976     bytes += ent_size;
1977     size  += ent_size;
1978   }
1979   printf("Cpool size: %d\n", size);
1980   fflush(0);
1981   return;
1982 } /* end print_cpool_bytes */
1983 
1984 
1985 // Returns size of constant pool entry.

  32 #include "classfile/metadataOnStackMark.hpp"
  33 #include "classfile/stringTable.hpp"
  34 #include "classfile/systemDictionary.hpp"
  35 #include "classfile/vmClasses.hpp"
  36 #include "classfile/vmSymbols.hpp"
  37 #include "code/codeCache.hpp"
  38 #include "interpreter/bootstrapInfo.hpp"
  39 #include "interpreter/linkResolver.hpp"
  40 #include "jvm.h"
  41 #include "logging/log.hpp"
  42 #include "logging/logStream.hpp"
  43 #include "memory/allocation.inline.hpp"
  44 #include "memory/metadataFactory.hpp"
  45 #include "memory/metaspaceClosure.hpp"
  46 #include "memory/oopFactory.hpp"
  47 #include "memory/resourceArea.hpp"
  48 #include "memory/universe.hpp"
  49 #include "oops/array.hpp"
  50 #include "oops/constantPool.inline.hpp"
  51 #include "oops/cpCache.inline.hpp"
  52 #include "oops/flatArrayKlass.hpp"
  53 #include "oops/instanceKlass.hpp"
  54 #include "oops/klass.inline.hpp"
  55 #include "oops/objArrayKlass.hpp"
  56 #include "oops/objArrayOop.inline.hpp"
  57 #include "oops/oop.inline.hpp"
  58 #include "oops/typeArrayOop.inline.hpp"
  59 #include "prims/jvmtiExport.hpp"
  60 #include "runtime/atomic.hpp"
  61 #include "runtime/handles.inline.hpp"
  62 #include "runtime/init.hpp"
  63 #include "runtime/javaCalls.hpp"
  64 #include "runtime/javaThread.hpp"
  65 #include "runtime/signature.hpp"
  66 #include "runtime/vframe.inline.hpp"
  67 #include "utilities/copy.hpp"
  68 
  69 ConstantPool* ConstantPool::allocate(ClassLoaderData* loader_data, int length, TRAPS) {
  70   Array<u1>* tags = MetadataFactory::new_array<u1>(loader_data, length, 0, CHECK_NULL);
  71   int size = ConstantPool::size(length);
  72   return new (loader_data, size, MetaspaceObj::ConstantPoolType, THREAD) ConstantPool(tags);

 228   assert(resolved_klasses() == NULL, "sanity");
 229   Array<Klass*>* rk = MetadataFactory::new_array<Klass*>(loader_data, num_klasses, CHECK);
 230   set_resolved_klasses(rk);
 231 }
 232 
 233 void ConstantPool::initialize_unresolved_klasses(ClassLoaderData* loader_data, TRAPS) {
 234   int len = length();
 235   int num_klasses = 0;
 236   for (int i = 1; i <len; i++) {
 237     switch (tag_at(i).value()) {
 238     case JVM_CONSTANT_ClassIndex:
 239       {
 240         const int class_index = klass_index_at(i);
 241         unresolved_klass_at_put(i, class_index, num_klasses++);
 242       }
 243       break;
 244 #ifndef PRODUCT
 245     case JVM_CONSTANT_Class:
 246     case JVM_CONSTANT_UnresolvedClass:
 247     case JVM_CONSTANT_UnresolvedClassInError:
 248       // All of these should have been reverted back to Unresolved before calling
 249       // this function.
 250       ShouldNotReachHere();
 251 #endif
 252     }
 253   }
 254   allocate_resolved_klasses(loader_data, num_klasses, THREAD);
 255 }
 256 
 257 // Hidden class support:
 258 void ConstantPool::klass_at_put(int class_index, Klass* k) {
 259   assert(k != NULL, "must be valid klass");
 260   CPKlassSlot kslot = klass_slot_at(class_index);
 261   int resolved_klass_index = kslot.resolved_klass_index();
 262   Klass** adr = resolved_klasses()->adr_at(resolved_klass_index);
 263   Atomic::release_store(adr, k);
 264 
 265   // The interpreter assumes when the tag is stored, the klass is resolved
 266   // and the Klass* non-NULL, so we need hardware store ordering here.
 267   assert(!k->name()->is_Q_signature(), "Q-type without JVM_CONSTANT_QDescBit");
 268   release_tag_at_put(class_index, JVM_CONSTANT_Class);
 269 }
 270 
 271 #if INCLUDE_CDS_JAVA_HEAP
 272 // Archive the resolved references
 273 void ConstantPool::archive_resolved_references() {
 274   if (_cache == NULL) {
 275     return; // nothing to do
 276   }
 277 
 278   InstanceKlass *ik = pool_holder();
 279   if (!(ik->is_shared_boot_class() || ik->is_shared_platform_class() ||
 280         ik->is_shared_app_class())) {
 281     // Archiving resolved references for classes from non-builtin loaders
 282     // is not yet supported.
 283     return;
 284   }
 285 
 286   objArrayOop rr = resolved_references();
 287   Array<u2>* ref_map = reference_map();

 374   // class redefinition. Since shared ConstantPools cannot be deallocated anyway,
 375   // we always set _on_stack to true to avoid having to change _flags during runtime.
 376   _flags |= (_on_stack | _is_shared);
 377 
 378   if (!_pool_holder->is_linked() && !_pool_holder->verified_at_dump_time()) {
 379     return;
 380   }
 381   // Resolved references are not in the shared archive.
 382   // Save the length for restoration.  It is not necessarily the same length
 383   // as reference_map.length() if invokedynamic is saved. It is needed when
 384   // re-creating the resolved reference array if archived heap data cannot be map
 385   // at runtime.
 386   set_resolved_reference_length(
 387     resolved_references() != NULL ? resolved_references()->length() : 0);
 388   set_resolved_references(OopHandle());
 389 
 390   bool archived = false;
 391   for (int index = 1; index < length(); index++) { // Index 0 is unused
 392     switch (tag_at(index).value()) {
 393     case JVM_CONSTANT_UnresolvedClassInError:
 394       {
 395         jbyte qdesc_bit = tag_at(index).is_Qdescriptor_klass() ? (jbyte) JVM_CONSTANT_QDescBit : 0;
 396         tag_at_put(index, JVM_CONSTANT_UnresolvedClass | qdesc_bit);
 397       }
 398       break;
 399     case JVM_CONSTANT_MethodHandleInError:
 400       tag_at_put(index, JVM_CONSTANT_MethodHandle);
 401       break;
 402     case JVM_CONSTANT_MethodTypeInError:
 403       tag_at_put(index, JVM_CONSTANT_MethodType);
 404       break;
 405     case JVM_CONSTANT_DynamicInError:
 406       tag_at_put(index, JVM_CONSTANT_Dynamic);
 407       break;
 408     case JVM_CONSTANT_Class:
 409       archived = maybe_archive_resolved_klass_at(index);
 410       ArchiveBuilder::alloc_stats()->record_klass_cp_entry(archived);
 411       break;
 412     }
 413   }
 414 
 415   if (cache() != NULL) {
 416     // cache() is NULL if this class is not yet linked.
 417     cache()->remove_unshareable_info();

 433   int resolved_klass_index = kslot.resolved_klass_index();
 434   Klass* k = resolved_klasses()->at(resolved_klass_index);
 435   // k could be NULL if the referenced class has been excluded via
 436   // SystemDictionaryShared::is_excluded_class().
 437 
 438   if (k != NULL) {
 439     ConstantPool* src_cp = ArchiveBuilder::current()->get_source_addr(this);
 440     if (ClassPrelinker::can_archive_resolved_klass(src_cp, cp_index)) {
 441       if (log_is_enabled(Debug, cds, resolve)) {
 442         ResourceMark rm;
 443         log_debug(cds, resolve)("Resolved klass CP entry [%d]: %s => %s", cp_index,
 444                                 pool_holder()->external_name(), k->external_name());
 445       }
 446       return true;
 447     }
 448   }
 449 
 450   // This referenced class cannot be archived. Revert the tag to UnresolvedClass,
 451   // so that the proper class loading and initialization can happen at runtime.
 452   resolved_klasses()->at_put(resolved_klass_index, NULL);
 453   jbyte qdesc_bit = tag_at(cp_index).is_Qdescriptor_klass() ? (jbyte) JVM_CONSTANT_QDescBit : 0;
 454   tag_at_put(cp_index, JVM_CONSTANT_UnresolvedClass | qdesc_bit);
 455   return false;
 456 }
 457 #endif // INCLUDE_CDS
 458 
 459 int ConstantPool::cp_to_object_index(int cp_index) {
 460   // this is harder don't do this so much.
 461   int i = reference_map()->find(cp_index);
 462   // We might not find the index for jsr292 call.
 463   return (i < 0) ? _no_index_sentinel : i;
 464 }
 465 
 466 void ConstantPool::string_at_put(int which, int obj_index, oop str) {
 467   oop result = set_resolved_reference_at(obj_index, str);
 468   assert(result == nullptr || result == str, "Only set once or to the same string.");
 469 }
 470 
 471 void ConstantPool::trace_class_resolution(const constantPoolHandle& this_cp, Klass* k) {
 472   ResourceMark rm;
 473   int line_number = -1;
 474   const char * source_file = NULL;

 480       Symbol* s = vfst.method()->method_holder()->source_file_name();
 481       if (s != NULL) {
 482         source_file = s->as_C_string();
 483       }
 484     }
 485   }
 486   if (k != this_cp->pool_holder()) {
 487     // only print something if the classes are different
 488     if (source_file != NULL) {
 489       log_debug(class, resolve)("%s %s %s:%d",
 490                  this_cp->pool_holder()->external_name(),
 491                  k->external_name(), source_file, line_number);
 492     } else {
 493       log_debug(class, resolve)("%s %s",
 494                  this_cp->pool_holder()->external_name(),
 495                  k->external_name());
 496     }
 497   }
 498 }
 499 
 500 void check_is_inline_type(Klass* k, TRAPS) {
 501   if (!k->is_inline_klass()) {
 502     THROW(vmSymbols::java_lang_IncompatibleClassChangeError());
 503   }
 504 }
 505 
 506 Klass* ConstantPool::klass_at_impl(const constantPoolHandle& this_cp, int which,
 507                                    TRAPS) {
 508   JavaThread* javaThread = THREAD;
 509 
 510   // A resolved constantPool entry will contain a Klass*, otherwise a Symbol*.
 511   // It is not safe to rely on the tag bit's here, since we don't have a lock, and
 512   // the entry and tag is not updated atomically.
 513   CPKlassSlot kslot = this_cp->klass_slot_at(which);
 514   int resolved_klass_index = kslot.resolved_klass_index();
 515   int name_index = kslot.name_index();
 516   assert(this_cp->tag_at(name_index).is_symbol(), "sanity");
 517 
 518   // The tag must be JVM_CONSTANT_Class in order to read the correct value from
 519   // the unresolved_klasses() array.
 520   if (this_cp->tag_at(which).is_klass()) {
 521     Klass* klass = this_cp->resolved_klasses()->at(resolved_klass_index);
 522     if (klass != NULL) {
 523       return klass;
 524     }
 525   }
 526 
 527   // This tag doesn't change back to unresolved class unless at a safepoint.
 528   if (this_cp->tag_at(which).is_unresolved_klass_in_error()) {
 529     // The original attempt to resolve this constant pool entry failed so find the
 530     // class of the original error and throw another error of the same class
 531     // (JVMS 5.4.3).
 532     // If there is a detail message, pass that detail message to the error.
 533     // The JVMS does not strictly require us to duplicate the same detail message,
 534     // or any internal exception fields such as cause or stacktrace.  But since the
 535     // detail message is often a class name or other literal string, we will repeat it
 536     // if we can find it in the symbol table.
 537     throw_resolution_error(this_cp, which, CHECK_NULL);
 538     ShouldNotReachHere();
 539   }
 540 
 541   Handle mirror_handle;
 542   Symbol* name = this_cp->symbol_at(name_index);
 543   bool inline_type_signature = false;
 544   if (name->is_Q_signature()) {
 545     name = name->fundamental_name(THREAD);
 546     inline_type_signature = true;
 547   }
 548   Handle loader (THREAD, this_cp->pool_holder()->class_loader());
 549   Handle protection_domain (THREAD, this_cp->pool_holder()->protection_domain());
 550 
 551   Klass* k;
 552   {
 553     // Turn off the single stepping while doing class resolution
 554     JvmtiHideSingleStepping jhss(javaThread);
 555     k = SystemDictionary::resolve_or_fail(name, loader, protection_domain, true, THREAD);
 556   } //  JvmtiHideSingleStepping jhss(javaThread);
 557   if (inline_type_signature) {
 558     name->decrement_refcount();
 559   }
 560 
 561   if (!HAS_PENDING_EXCEPTION) {
 562     // preserve the resolved klass from unloading
 563     mirror_handle = Handle(THREAD, k->java_mirror());
 564     // Do access check for klasses
 565     verify_constant_pool_resolve(this_cp, k, THREAD);
 566   }
 567 
 568   if (!HAS_PENDING_EXCEPTION && inline_type_signature) {
 569     check_is_inline_type(k, THREAD);
 570   }
 571 
 572   if (!HAS_PENDING_EXCEPTION) {
 573     Klass* bottom_klass = NULL;
 574     if (k->is_objArray_klass()) {
 575       bottom_klass = ObjArrayKlass::cast(k)->bottom_klass();
 576       assert(bottom_klass != NULL, "Should be set");
 577       assert(bottom_klass->is_instance_klass() || bottom_klass->is_typeArray_klass(), "Sanity check");
 578     } else if (k->is_flatArray_klass()) {
 579       bottom_klass = FlatArrayKlass::cast(k)->element_klass();
 580       assert(bottom_klass != NULL, "Should be set");
 581     }
 582   }
 583 
 584   // Failed to resolve class. We must record the errors so that subsequent attempts
 585   // to resolve this constant pool entry fail with the same error (JVMS 5.4.3).
 586   if (HAS_PENDING_EXCEPTION) {
 587     jbyte tag = JVM_CONSTANT_UnresolvedClass;
 588     if (this_cp->tag_at(which).is_Qdescriptor_klass()) {
 589       tag |= JVM_CONSTANT_QDescBit;
 590     }
 591     save_and_throw_exception(this_cp, which, constantTag(tag), CHECK_NULL);
 592     // If CHECK_NULL above doesn't return the exception, that means that
 593     // some other thread has beaten us and has resolved the class.
 594     // To preserve old behavior, we return the resolved class.
 595     Klass* klass = this_cp->resolved_klasses()->at(resolved_klass_index);
 596     assert(klass != NULL, "must be resolved if exception was cleared");
 597     return klass;
 598   }
 599 
 600   // logging for class+resolve.
 601   if (log_is_enabled(Debug, class, resolve)){
 602     trace_class_resolution(this_cp, k);
 603   }
 604 
 605   Klass** adr = this_cp->resolved_klasses()->adr_at(resolved_klass_index);
 606   Atomic::release_store(adr, k);
 607   // The interpreter assumes when the tag is stored, the klass is resolved
 608   // and the Klass* stored in _resolved_klasses is non-NULL, so we need
 609   // hardware store ordering here.
 610   jbyte tag = JVM_CONSTANT_Class;
 611   if (this_cp->tag_at(which).is_Qdescriptor_klass()) {
 612     tag |= JVM_CONSTANT_QDescBit;
 613   }
 614   // We also need to CAS to not overwrite an error from a racing thread.
 615 
 616   jbyte old_tag = Atomic::cmpxchg((jbyte*)this_cp->tag_addr_at(which),
 617                                   (jbyte)JVM_CONSTANT_UnresolvedClass,
 618                                   tag);
 619 
 620   // We need to recheck exceptions from racing thread and return the same.
 621   if (old_tag == JVM_CONSTANT_UnresolvedClassInError) {
 622     // Remove klass.
 623     this_cp->resolved_klasses()->at_put(resolved_klass_index, NULL);
 624     throw_resolution_error(this_cp, which, CHECK_NULL);
 625   }
 626 
 627   return k;
 628 }
 629 
 630 
 631 // Does not update ConstantPool* - to avoid any exception throwing. Used
 632 // by compiler and exception handling.  Also used to avoid classloads for
 633 // instanceof operations. Returns NULL if the class has not been loaded or
 634 // if the verification of constant pool failed
 635 Klass* ConstantPool::klass_at_if_loaded(const constantPoolHandle& this_cp, int which) {
 636   CPKlassSlot kslot = this_cp->klass_slot_at(which);
 637   int resolved_klass_index = kslot.resolved_klass_index();
 638   int name_index = kslot.name_index();

1031     case JVM_CONSTANT_Long:
1032     case JVM_CONSTANT_Double:
1033       // these guys trigger OOM at worst
1034       break;
1035     default:
1036       (*status_return) = false;
1037       return NULL;
1038     }
1039     // from now on there is either success or an OOME
1040     (*status_return) = true;
1041   }
1042 
1043   switch (tag.value()) {
1044 
1045   case JVM_CONSTANT_UnresolvedClass:
1046   case JVM_CONSTANT_Class:
1047     {
1048       assert(cache_index == _no_index_sentinel, "should not have been set");
1049       Klass* resolved = klass_at_impl(this_cp, index, CHECK_NULL);
1050       // ldc wants the java mirror.
1051       result_oop = tag.is_Qdescriptor_klass()
1052                       ? InlineKlass::cast(resolved)->val_mirror()
1053                       : resolved->java_mirror();
1054       break;
1055     }
1056 
1057   case JVM_CONSTANT_Dynamic:
1058     {
1059       // Resolve the Dynamically-Computed constant to invoke the BSM in order to obtain the resulting oop.
1060       BootstrapInfo bootstrap_specifier(this_cp, index);
1061 
1062       // The initial step in resolving an unresolved symbolic reference to a
1063       // dynamically-computed constant is to resolve the symbolic reference to a
1064       // method handle which will be the bootstrap method for the dynamically-computed
1065       // constant. If resolution of the java.lang.invoke.MethodHandle for the bootstrap
1066       // method fails, then a MethodHandleInError is stored at the corresponding
1067       // bootstrap method's CP index for the CONSTANT_MethodHandle_info. No need to
1068       // set a DynamicConstantInError here since any subsequent use of this
1069       // bootstrap method will encounter the resolution of MethodHandleInError.
1070       // Both the first, (resolution of the BSM and its static arguments), and the second tasks,
1071       // (invocation of the BSM), of JVMS Section 5.4.3.6 occur within invoke_bootstrap_method()
1072       // for the bootstrap_specifier created above.
1073       SystemDictionary::invoke_bootstrap_method(bootstrap_specifier, THREAD);

1950       case JVM_CONSTANT_Long: {
1951         u8 val = Bytes::get_Java_u8(bytes);
1952         printf("long         " INT64_FORMAT, (int64_t) *(jlong *) &val);
1953         ent_size = 8;
1954         idx++; // Long takes two cpool slots
1955         break;
1956       }
1957       case JVM_CONSTANT_Double: {
1958         u8 val = Bytes::get_Java_u8(bytes);
1959         printf("double       %5.3fd", *(jdouble *)&val);
1960         ent_size = 8;
1961         idx++; // Double takes two cpool slots
1962         break;
1963       }
1964       case JVM_CONSTANT_Class: {
1965         idx1 = Bytes::get_Java_u2(bytes);
1966         printf("class        #%03d", idx1);
1967         ent_size = 2;
1968         break;
1969       }
1970       case (JVM_CONSTANT_Class | JVM_CONSTANT_QDescBit): {
1971         idx1 = Bytes::get_Java_u2(bytes);
1972         printf("qclass        #%03d", idx1);
1973         ent_size = 2;
1974         break;
1975       }
1976       case JVM_CONSTANT_String: {
1977         idx1 = Bytes::get_Java_u2(bytes);
1978         printf("String       #%03d", idx1);
1979         ent_size = 2;
1980         break;
1981       }
1982       case JVM_CONSTANT_Fieldref: {
1983         idx1 = Bytes::get_Java_u2(bytes);
1984         idx2 = Bytes::get_Java_u2(bytes+2);
1985         printf("Field        #%03d, #%03d", (int) idx1, (int) idx2);
1986         ent_size = 4;
1987         break;
1988       }
1989       case JVM_CONSTANT_Methodref: {
1990         idx1 = Bytes::get_Java_u2(bytes);
1991         idx2 = Bytes::get_Java_u2(bytes+2);
1992         printf("Method       #%03d, #%03d", idx1, idx2);
1993         ent_size = 4;
1994         break;
1995       }

1998         idx2 = Bytes::get_Java_u2(bytes+2);
1999         printf("InterfMethod #%03d, #%03d", idx1, idx2);
2000         ent_size = 4;
2001         break;
2002       }
2003       case JVM_CONSTANT_NameAndType: {
2004         idx1 = Bytes::get_Java_u2(bytes);
2005         idx2 = Bytes::get_Java_u2(bytes+2);
2006         printf("NameAndType  #%03d, #%03d", idx1, idx2);
2007         ent_size = 4;
2008         break;
2009       }
2010       case JVM_CONSTANT_ClassIndex: {
2011         printf("ClassIndex  %s", WARN_MSG);
2012         break;
2013       }
2014       case JVM_CONSTANT_UnresolvedClass: {
2015         printf("UnresolvedClass: %s", WARN_MSG);
2016         break;
2017       }
2018       case (JVM_CONSTANT_UnresolvedClass | JVM_CONSTANT_QDescBit): {
2019         printf("UnresolvedQClass: %s", WARN_MSG);
2020         break;
2021       }
2022       case JVM_CONSTANT_UnresolvedClassInError: {
2023         printf("UnresolvedClassInErr: %s", WARN_MSG);
2024         break;
2025       }
2026       case JVM_CONSTANT_StringIndex: {
2027         printf("StringIndex: %s", WARN_MSG);
2028         break;
2029       }
2030     }
2031     printf(";\n");
2032     bytes += ent_size;
2033     size  += ent_size;
2034   }
2035   printf("Cpool size: %d\n", size);
2036   fflush(0);
2037   return;
2038 } /* end print_cpool_bytes */
2039 
2040 
2041 // Returns size of constant pool entry.
< prev index next >