< prev index next >

src/hotspot/share/oops/constantPool.cpp

Print this page

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

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

 268   release_tag_at_put(class_index, JVM_CONSTANT_Class);
 269 }
 270 
 271 #if INCLUDE_CDS_JAVA_HEAP
 272 // Returns the _resolved_reference array after removing unarchivable items from it.
 273 // Returns null if this class is not supported, or _resolved_reference doesn't exist.
 274 objArrayOop ConstantPool::prepare_resolved_references_for_archiving() {
 275   if (_cache == nullptr) {
 276     return nullptr; // nothing to do
 277   }
 278 
 279   InstanceKlass *ik = pool_holder();
 280   if (!(ik->is_shared_boot_class() || ik->is_shared_platform_class() ||
 281         ik->is_shared_app_class())) {
 282     // Archiving resolved references for classes from non-builtin loaders
 283     // is not yet supported.
 284     return nullptr;
 285   }
 286 
 287   objArrayOop rr = resolved_references();

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



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

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

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

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






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





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



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
















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




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




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

 996     case JVM_CONSTANT_Long:
 997     case JVM_CONSTANT_Double:
 998       // these guys trigger OOM at worst
 999       break;
1000     default:
1001       (*status_return) = false;
1002       return nullptr;
1003     }
1004     // from now on there is either success or an OOME
1005     (*status_return) = true;
1006   }
1007 
1008   switch (tag.value()) {
1009 
1010   case JVM_CONSTANT_UnresolvedClass:
1011   case JVM_CONSTANT_Class:
1012     {
1013       assert(cache_index == _no_index_sentinel, "should not have been set");
1014       Klass* resolved = klass_at_impl(this_cp, index, CHECK_NULL);
1015       // ldc wants the java mirror.
1016       result_oop = resolved->java_mirror();


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

1915       case JVM_CONSTANT_Long: {
1916         u8 val = Bytes::get_Java_u8(bytes);
1917         printf("long         " INT64_FORMAT, (int64_t) *(jlong *) &val);
1918         ent_size = 8;
1919         idx++; // Long takes two cpool slots
1920         break;
1921       }
1922       case JVM_CONSTANT_Double: {
1923         u8 val = Bytes::get_Java_u8(bytes);
1924         printf("double       %5.3fd", *(jdouble *)&val);
1925         ent_size = 8;
1926         idx++; // Double takes two cpool slots
1927         break;
1928       }
1929       case JVM_CONSTANT_Class: {
1930         idx1 = Bytes::get_Java_u2(bytes);
1931         printf("class        #%03d", idx1);
1932         ent_size = 2;
1933         break;
1934       }






1935       case JVM_CONSTANT_String: {
1936         idx1 = Bytes::get_Java_u2(bytes);
1937         printf("String       #%03d", idx1);
1938         ent_size = 2;
1939         break;
1940       }
1941       case JVM_CONSTANT_Fieldref: {
1942         idx1 = Bytes::get_Java_u2(bytes);
1943         idx2 = Bytes::get_Java_u2(bytes+2);
1944         printf("Field        #%03d, #%03d", (int) idx1, (int) idx2);
1945         ent_size = 4;
1946         break;
1947       }
1948       case JVM_CONSTANT_Methodref: {
1949         idx1 = Bytes::get_Java_u2(bytes);
1950         idx2 = Bytes::get_Java_u2(bytes+2);
1951         printf("Method       #%03d, #%03d", idx1, idx2);
1952         ent_size = 4;
1953         break;
1954       }

1957         idx2 = Bytes::get_Java_u2(bytes+2);
1958         printf("InterfMethod #%03d, #%03d", idx1, idx2);
1959         ent_size = 4;
1960         break;
1961       }
1962       case JVM_CONSTANT_NameAndType: {
1963         idx1 = Bytes::get_Java_u2(bytes);
1964         idx2 = Bytes::get_Java_u2(bytes+2);
1965         printf("NameAndType  #%03d, #%03d", idx1, idx2);
1966         ent_size = 4;
1967         break;
1968       }
1969       case JVM_CONSTANT_ClassIndex: {
1970         printf("ClassIndex  %s", WARN_MSG);
1971         break;
1972       }
1973       case JVM_CONSTANT_UnresolvedClass: {
1974         printf("UnresolvedClass: %s", WARN_MSG);
1975         break;
1976       }




1977       case JVM_CONSTANT_UnresolvedClassInError: {
1978         printf("UnresolvedClassInErr: %s", WARN_MSG);
1979         break;
1980       }
1981       case JVM_CONSTANT_StringIndex: {
1982         printf("StringIndex: %s", WARN_MSG);
1983         break;
1984       }
1985     }
1986     printf(";\n");
1987     bytes += ent_size;
1988     size  += ent_size;
1989   }
1990   printf("Cpool size: %d\n", size);
1991   fflush(0);
1992   return;
1993 } /* end print_cpool_bytes */
1994 
1995 
1996 // Returns size of constant pool entry.

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

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

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

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

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

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

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

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