< 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/checkedCast.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);

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

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

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



 399       break;
 400     case JVM_CONSTANT_MethodHandleInError:
 401       tag_at_put(cp_index, JVM_CONSTANT_MethodHandle);
 402       break;
 403     case JVM_CONSTANT_MethodTypeInError:
 404       tag_at_put(cp_index, JVM_CONSTANT_MethodType);
 405       break;
 406     case JVM_CONSTANT_DynamicInError:
 407       tag_at_put(cp_index, JVM_CONSTANT_Dynamic);
 408       break;
 409     case JVM_CONSTANT_Class:
 410       archived = maybe_archive_resolved_klass_at(cp_index);
 411       ArchiveBuilder::alloc_stats()->record_klass_cp_entry(archived);
 412       break;
 413     }
 414   }
 415 
 416   if (cache() != nullptr) {
 417     // cache() is null if this class is not yet linked.
 418     cache()->remove_unshareable_info();

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

 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(checked_cast<u2>(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 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 = nullptr;

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





 538   Handle loader (THREAD, this_cp->pool_holder()->class_loader());
 539   Handle protection_domain (THREAD, this_cp->pool_holder()->protection_domain());
 540 
 541   Klass* k;
 542   {
 543     // Turn off the single stepping while doing class resolution
 544     JvmtiHideSingleStepping jhss(javaThread);
 545     k = SystemDictionary::resolve_or_fail(name, loader, protection_domain, true, THREAD);
 546   } //  JvmtiHideSingleStepping jhss(javaThread);



 547 
 548   if (!HAS_PENDING_EXCEPTION) {
 549     // preserve the resolved klass from unloading
 550     mirror_handle = Handle(THREAD, k->java_mirror());
 551     // Do access check for klasses
 552     verify_constant_pool_resolve(this_cp, k, THREAD);
 553   }
 554 
















 555   // Failed to resolve class. We must record the errors so that subsequent attempts
 556   // to resolve this constant pool entry fail with the same error (JVMS 5.4.3).
 557   if (HAS_PENDING_EXCEPTION) {
 558     save_and_throw_exception(this_cp, cp_index, constantTag(JVM_CONSTANT_UnresolvedClass), CHECK_NULL);




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




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

 678   if (cpool->cache() == nullptr)  return false;  // nothing to load yet
 679   int cache_index = decode_cpcache_index(which, true);
 680   if (is_invokedynamic_index(which)) {
 681     return cpool->resolved_indy_entry_at(cache_index)->has_local_signature();
 682   } else {
 683     ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
 684     return e->has_local_signature();
 685   }
 686 }
 687 
 688 // Translate index, which could be CPCache index or Indy index, to a constant pool index
 689 int ConstantPool::to_cp_index(int index, Bytecodes::Code code) {
 690   assert(cache() != nullptr, "'index' is a rewritten index so this class must have been rewritten");
 691   switch(code) {
 692     case Bytecodes::_invokedynamic:
 693       return invokedynamic_bootstrap_ref_index_at(index);
 694     case Bytecodes::_getfield:
 695     case Bytecodes::_getstatic:
 696     case Bytecodes::_putfield:
 697     case Bytecodes::_putstatic:

 698       return resolved_field_entry_at(index)->constant_pool_index();
 699     case Bytecodes::_invokeinterface:
 700     case Bytecodes::_invokehandle:
 701     case Bytecodes::_invokespecial:
 702     case Bytecodes::_invokestatic:
 703     case Bytecodes::_invokevirtual:
 704       // TODO: handle resolved method entries with new structure
 705     default:
 706       // change byte-ordering and go via cache
 707       return remap_instruction_operand_from_cache(index);
 708   }
 709 }
 710 
 711 u2 ConstantPool::uncached_name_and_type_ref_index_at(int cp_index)  {
 712   if (tag_at(cp_index).has_bootstrap()) {
 713     u2 pool_index = bootstrap_name_and_type_ref_index_at(cp_index);
 714     assert(tag_at(pool_index).is_name_and_type(), "");
 715     return pool_index;
 716   }
 717   assert(tag_at(cp_index).is_field_or_method(), "Corrupted constant pool");

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


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

1923       case JVM_CONSTANT_Long: {
1924         u8 val = Bytes::get_Java_u8(bytes);
1925         printf("long         " INT64_FORMAT, (int64_t) *(jlong *) &val);
1926         ent_size = 8;
1927         idx++; // Long takes two cpool slots
1928         break;
1929       }
1930       case JVM_CONSTANT_Double: {
1931         u8 val = Bytes::get_Java_u8(bytes);
1932         printf("double       %5.3fd", *(jdouble *)&val);
1933         ent_size = 8;
1934         idx++; // Double takes two cpool slots
1935         break;
1936       }
1937       case JVM_CONSTANT_Class: {
1938         idx1 = Bytes::get_Java_u2(bytes);
1939         printf("class        #%03d", idx1);
1940         ent_size = 2;
1941         break;
1942       }






1943       case JVM_CONSTANT_String: {
1944         idx1 = Bytes::get_Java_u2(bytes);
1945         printf("String       #%03d", idx1);
1946         ent_size = 2;
1947         break;
1948       }
1949       case JVM_CONSTANT_Fieldref: {
1950         idx1 = Bytes::get_Java_u2(bytes);
1951         idx2 = Bytes::get_Java_u2(bytes+2);
1952         printf("Field        #%03d, #%03d", (int) idx1, (int) idx2);
1953         ent_size = 4;
1954         break;
1955       }
1956       case JVM_CONSTANT_Methodref: {
1957         idx1 = Bytes::get_Java_u2(bytes);
1958         idx2 = Bytes::get_Java_u2(bytes+2);
1959         printf("Method       #%03d, #%03d", idx1, idx2);
1960         ent_size = 4;
1961         break;
1962       }

1965         idx2 = Bytes::get_Java_u2(bytes+2);
1966         printf("InterfMethod #%03d, #%03d", idx1, idx2);
1967         ent_size = 4;
1968         break;
1969       }
1970       case JVM_CONSTANT_NameAndType: {
1971         idx1 = Bytes::get_Java_u2(bytes);
1972         idx2 = Bytes::get_Java_u2(bytes+2);
1973         printf("NameAndType  #%03d, #%03d", idx1, idx2);
1974         ent_size = 4;
1975         break;
1976       }
1977       case JVM_CONSTANT_ClassIndex: {
1978         printf("ClassIndex  %s", WARN_MSG);
1979         break;
1980       }
1981       case JVM_CONSTANT_UnresolvedClass: {
1982         printf("UnresolvedClass: %s", WARN_MSG);
1983         break;
1984       }




1985       case JVM_CONSTANT_UnresolvedClassInError: {
1986         printf("UnresolvedClassInErr: %s", WARN_MSG);
1987         break;
1988       }
1989       case JVM_CONSTANT_StringIndex: {
1990         printf("StringIndex: %s", WARN_MSG);
1991         break;
1992       }
1993     }
1994     printf(";\n");
1995     bytes += ent_size;
1996     size  += ent_size;
1997   }
1998   printf("Cpool size: %d\n", size);
1999   fflush(0);
2000   return;
2001 } /* end print_cpool_bytes */
2002 
2003 
2004 // 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/checkedCast.hpp"
  69 #include "utilities/copy.hpp"
  70 
  71 ConstantPool* ConstantPool::allocate(ClassLoaderData* loader_data, int length, TRAPS) {
  72   Array<u1>* tags = MetadataFactory::new_array<u1>(loader_data, length, 0, CHECK_NULL);
  73   int size = ConstantPool::size(length);

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

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

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

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

 722   if (cpool->cache() == nullptr)  return false;  // nothing to load yet
 723   int cache_index = decode_cpcache_index(which, true);
 724   if (is_invokedynamic_index(which)) {
 725     return cpool->resolved_indy_entry_at(cache_index)->has_local_signature();
 726   } else {
 727     ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
 728     return e->has_local_signature();
 729   }
 730 }
 731 
 732 // Translate index, which could be CPCache index or Indy index, to a constant pool index
 733 int ConstantPool::to_cp_index(int index, Bytecodes::Code code) {
 734   assert(cache() != nullptr, "'index' is a rewritten index so this class must have been rewritten");
 735   switch(code) {
 736     case Bytecodes::_invokedynamic:
 737       return invokedynamic_bootstrap_ref_index_at(index);
 738     case Bytecodes::_getfield:
 739     case Bytecodes::_getstatic:
 740     case Bytecodes::_putfield:
 741     case Bytecodes::_putstatic:
 742     case Bytecodes::_withfield:
 743       return resolved_field_entry_at(index)->constant_pool_index();
 744     case Bytecodes::_invokeinterface:
 745     case Bytecodes::_invokehandle:
 746     case Bytecodes::_invokespecial:
 747     case Bytecodes::_invokestatic:
 748     case Bytecodes::_invokevirtual:
 749       // TODO: handle resolved method entries with new structure
 750     default:
 751       // change byte-ordering and go via cache
 752       return remap_instruction_operand_from_cache(index);
 753   }
 754 }
 755 
 756 u2 ConstantPool::uncached_name_and_type_ref_index_at(int cp_index)  {
 757   if (tag_at(cp_index).has_bootstrap()) {
 758     u2 pool_index = bootstrap_name_and_type_ref_index_at(cp_index);
 759     assert(tag_at(pool_index).is_name_and_type(), "");
 760     return pool_index;
 761   }
 762   assert(tag_at(cp_index).is_field_or_method(), "Corrupted constant pool");

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

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

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