< prev index next >

src/hotspot/share/oops/constantPool.cpp

Print this page

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

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

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

 252   release_tag_at_put(class_index, JVM_CONSTANT_Class);
 253 }
 254 
 255 #if INCLUDE_CDS_JAVA_HEAP
 256 // Archive the resolved references
 257 void ConstantPool::archive_resolved_references() {
 258   if (_cache == NULL) {
 259     return; // nothing to do
 260   }
 261 
 262   InstanceKlass *ik = pool_holder();
 263   if (!(ik->is_shared_boot_class() || ik->is_shared_platform_class() ||
 264         ik->is_shared_app_class())) {
 265     // Archiving resolved references for classes from non-builtin loaders
 266     // is not yet supported.
 267     return;
 268   }
 269 
 270   objArrayOop rr = resolved_references();
 271   Array<u2>* ref_map = reference_map();

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

 393     if (tag_at(index).is_unresolved_klass_in_error()) {
 394       tag_at_put(index, JVM_CONSTANT_UnresolvedClass);
 395     } else if (tag_at(index).is_method_handle_in_error()) {
 396       tag_at_put(index, JVM_CONSTANT_MethodHandle);
 397     } else if (tag_at(index).is_method_type_in_error()) {
 398       tag_at_put(index, JVM_CONSTANT_MethodType);
 399     } else if (tag_at(index).is_dynamic_constant_in_error()) {
 400       tag_at_put(index, JVM_CONSTANT_Dynamic);
 401     }
 402     if (tag_at(index).is_klass()) {
 403       // This class was resolved as a side effect of executing Java code
 404       // during dump time. We need to restore it back to an UnresolvedClass,
 405       // so that the proper class loading and initialization can happen
 406       // at runtime.
 407       bool clear_it = true;
 408       if (pool_holder()->is_hidden() && index == pool_holder()->this_class_index()) {
 409         // All references to a hidden class's own field/methods are through this
 410         // index. We cannot clear it. See comments in ClassFileParser::fill_instance_klass.
 411         clear_it = false;
 412       }
 413       if (clear_it) {
 414         CPKlassSlot kslot = klass_slot_at(index);
 415         int resolved_klass_index = kslot.resolved_klass_index();
 416         int name_index = kslot.name_index();
 417         assert(tag_at(name_index).is_symbol(), "sanity");
 418         resolved_klasses()->at_put(resolved_klass_index, NULL);
 419         tag_at_put(index, JVM_CONSTANT_UnresolvedClass);
 420         assert(klass_name_at(index) == symbol_at(name_index), "sanity");
 421       }
 422     }
 423   }
 424   if (cache() != NULL) {
 425     // cache() is NULL if this class is not yet linked.
 426     cache()->remove_unshareable_info();
 427   }
 428 }
 429 #endif // INCLUDE_CDS
 430 
 431 int ConstantPool::cp_to_object_index(int cp_index) {
 432   // this is harder don't do this so much.
 433   int i = reference_map()->find(cp_index);
 434   // We might not find the index for jsr292 call.
 435   return (i < 0) ? _no_index_sentinel : i;
 436 }
 437 
 438 void ConstantPool::string_at_put(int which, int obj_index, oop str) {
 439   resolved_references()->obj_at_put(obj_index, str);

 451       Symbol* s = vfst.method()->method_holder()->source_file_name();
 452       if (s != NULL) {
 453         source_file = s->as_C_string();
 454       }
 455     }
 456   }
 457   if (k != this_cp->pool_holder()) {
 458     // only print something if the classes are different
 459     if (source_file != NULL) {
 460       log_debug(class, resolve)("%s %s %s:%d",
 461                  this_cp->pool_holder()->external_name(),
 462                  k->external_name(), source_file, line_number);
 463     } else {
 464       log_debug(class, resolve)("%s %s",
 465                  this_cp->pool_holder()->external_name(),
 466                  k->external_name());
 467     }
 468   }
 469 }
 470 






 471 Klass* ConstantPool::klass_at_impl(const constantPoolHandle& this_cp, int which,
 472                                    TRAPS) {
 473   JavaThread* javaThread = THREAD;
 474 
 475   // A resolved constantPool entry will contain a Klass*, otherwise a Symbol*.
 476   // It is not safe to rely on the tag bit's here, since we don't have a lock, and
 477   // the entry and tag is not updated atomically.
 478   CPKlassSlot kslot = this_cp->klass_slot_at(which);
 479   int resolved_klass_index = kslot.resolved_klass_index();
 480   int name_index = kslot.name_index();
 481   assert(this_cp->tag_at(name_index).is_symbol(), "sanity");
 482 
 483   // The tag must be JVM_CONSTANT_Class in order to read the correct value from
 484   // the unresolved_klasses() array.
 485   if (this_cp->tag_at(which).is_klass()) {
 486     Klass* klass = this_cp->resolved_klasses()->at(resolved_klass_index);
 487     if (klass != NULL) {
 488       return klass;
 489     }
 490   }
 491 
 492   // This tag doesn't change back to unresolved class unless at a safepoint.
 493   if (this_cp->tag_at(which).is_unresolved_klass_in_error()) {
 494     // The original attempt to resolve this constant pool entry failed so find the
 495     // class of the original error and throw another error of the same class
 496     // (JVMS 5.4.3).
 497     // If there is a detail message, pass that detail message to the error.
 498     // The JVMS does not strictly require us to duplicate the same detail message,
 499     // or any internal exception fields such as cause or stacktrace.  But since the
 500     // detail message is often a class name or other literal string, we will repeat it
 501     // if we can find it in the symbol table.
 502     throw_resolution_error(this_cp, which, CHECK_NULL);
 503     ShouldNotReachHere();
 504   }
 505 
 506   Handle mirror_handle;
 507   Symbol* name = this_cp->symbol_at(name_index);





 508   Handle loader (THREAD, this_cp->pool_holder()->class_loader());
 509   Handle protection_domain (THREAD, this_cp->pool_holder()->protection_domain());
 510 
 511   Klass* k;
 512   {
 513     // Turn off the single stepping while doing class resolution
 514     JvmtiHideSingleStepping jhss(javaThread);
 515     k = SystemDictionary::resolve_or_fail(name, loader, protection_domain, true, THREAD);
 516   } //  JvmtiHideSingleStepping jhss(javaThread);



 517 
 518   if (!HAS_PENDING_EXCEPTION) {
 519     // preserve the resolved klass from unloading
 520     mirror_handle = Handle(THREAD, k->java_mirror());
 521     // Do access check for klasses
 522     verify_constant_pool_resolve(this_cp, k, THREAD);
 523   }
 524 
















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




 529     // If CHECK_NULL above doesn't return the exception, that means that
 530     // some other thread has beaten us and has resolved the class.
 531     // To preserve old behavior, we return the resolved class.
 532     Klass* klass = this_cp->resolved_klasses()->at(resolved_klass_index);
 533     assert(klass != NULL, "must be resolved if exception was cleared");
 534     return klass;
 535   }
 536 
 537   // logging for class+resolve.
 538   if (log_is_enabled(Debug, class, resolve)){
 539     trace_class_resolution(this_cp, k);
 540   }
 541 
 542   Klass** adr = this_cp->resolved_klasses()->adr_at(resolved_klass_index);
 543   Atomic::release_store(adr, k);
 544   // The interpreter assumes when the tag is stored, the klass is resolved
 545   // and the Klass* stored in _resolved_klasses is non-NULL, so we need
 546   // hardware store ordering here.




 547   // We also need to CAS to not overwrite an error from a racing thread.
 548 
 549   jbyte old_tag = Atomic::cmpxchg((jbyte*)this_cp->tag_addr_at(which),
 550                                   (jbyte)JVM_CONSTANT_UnresolvedClass,
 551                                   (jbyte)JVM_CONSTANT_Class);
 552 
 553   // We need to recheck exceptions from racing thread and return the same.
 554   if (old_tag == JVM_CONSTANT_UnresolvedClassInError) {
 555     // Remove klass.
 556     this_cp->resolved_klasses()->at_put(resolved_klass_index, NULL);
 557     throw_resolution_error(this_cp, which, CHECK_NULL);
 558   }
 559 
 560   return k;
 561 }
 562 
 563 
 564 // Does not update ConstantPool* - to avoid any exception throwing. Used
 565 // by compiler and exception handling.  Also used to avoid classloads for
 566 // instanceof operations. Returns NULL if the class has not been loaded or
 567 // if the verification of constant pool failed
 568 Klass* ConstantPool::klass_at_if_loaded(const constantPoolHandle& this_cp, int which) {
 569   CPKlassSlot kslot = this_cp->klass_slot_at(which);
 570   int resolved_klass_index = kslot.resolved_klass_index();
 571   int name_index = kslot.name_index();

 964     case JVM_CONSTANT_Long:
 965     case JVM_CONSTANT_Double:
 966       // these guys trigger OOM at worst
 967       break;
 968     default:
 969       (*status_return) = false;
 970       return NULL;
 971     }
 972     // from now on there is either success or an OOME
 973     (*status_return) = true;
 974   }
 975 
 976   switch (tag.value()) {
 977 
 978   case JVM_CONSTANT_UnresolvedClass:
 979   case JVM_CONSTANT_Class:
 980     {
 981       assert(cache_index == _no_index_sentinel, "should not have been set");
 982       Klass* resolved = klass_at_impl(this_cp, index, CHECK_NULL);
 983       // ldc wants the java mirror.
 984       result_oop = resolved->java_mirror();


 985       break;
 986     }
 987 
 988   case JVM_CONSTANT_Dynamic:
 989     {
 990       // Resolve the Dynamically-Computed constant to invoke the BSM in order to obtain the resulting oop.
 991       BootstrapInfo bootstrap_specifier(this_cp, index);
 992 
 993       // The initial step in resolving an unresolved symbolic reference to a
 994       // dynamically-computed constant is to resolve the symbolic reference to a
 995       // method handle which will be the bootstrap method for the dynamically-computed
 996       // constant. If resolution of the java.lang.invoke.MethodHandle for the bootstrap
 997       // method fails, then a MethodHandleInError is stored at the corresponding
 998       // bootstrap method's CP index for the CONSTANT_MethodHandle_info. No need to
 999       // set a DynamicConstantInError here since any subsequent use of this
1000       // bootstrap method will encounter the resolution of MethodHandleInError.
1001       // Both the first, (resolution of the BSM and its static arguments), and the second tasks,
1002       // (invocation of the BSM), of JVMS Section 5.4.3.6 occur within invoke_bootstrap_method()
1003       // for the bootstrap_specifier created above.
1004       SystemDictionary::invoke_bootstrap_method(bootstrap_specifier, THREAD);

1882       case JVM_CONSTANT_Long: {
1883         u8 val = Bytes::get_Java_u8(bytes);
1884         printf("long         " INT64_FORMAT, (int64_t) *(jlong *) &val);
1885         ent_size = 8;
1886         idx++; // Long takes two cpool slots
1887         break;
1888       }
1889       case JVM_CONSTANT_Double: {
1890         u8 val = Bytes::get_Java_u8(bytes);
1891         printf("double       %5.3fd", *(jdouble *)&val);
1892         ent_size = 8;
1893         idx++; // Double takes two cpool slots
1894         break;
1895       }
1896       case JVM_CONSTANT_Class: {
1897         idx1 = Bytes::get_Java_u2(bytes);
1898         printf("class        #%03d", idx1);
1899         ent_size = 2;
1900         break;
1901       }






1902       case JVM_CONSTANT_String: {
1903         idx1 = Bytes::get_Java_u2(bytes);
1904         printf("String       #%03d", idx1);
1905         ent_size = 2;
1906         break;
1907       }
1908       case JVM_CONSTANT_Fieldref: {
1909         idx1 = Bytes::get_Java_u2(bytes);
1910         idx2 = Bytes::get_Java_u2(bytes+2);
1911         printf("Field        #%03d, #%03d", (int) idx1, (int) idx2);
1912         ent_size = 4;
1913         break;
1914       }
1915       case JVM_CONSTANT_Methodref: {
1916         idx1 = Bytes::get_Java_u2(bytes);
1917         idx2 = Bytes::get_Java_u2(bytes+2);
1918         printf("Method       #%03d, #%03d", idx1, idx2);
1919         ent_size = 4;
1920         break;
1921       }

1924         idx2 = Bytes::get_Java_u2(bytes+2);
1925         printf("InterfMethod #%03d, #%03d", idx1, idx2);
1926         ent_size = 4;
1927         break;
1928       }
1929       case JVM_CONSTANT_NameAndType: {
1930         idx1 = Bytes::get_Java_u2(bytes);
1931         idx2 = Bytes::get_Java_u2(bytes+2);
1932         printf("NameAndType  #%03d, #%03d", idx1, idx2);
1933         ent_size = 4;
1934         break;
1935       }
1936       case JVM_CONSTANT_ClassIndex: {
1937         printf("ClassIndex  %s", WARN_MSG);
1938         break;
1939       }
1940       case JVM_CONSTANT_UnresolvedClass: {
1941         printf("UnresolvedClass: %s", WARN_MSG);
1942         break;
1943       }




1944       case JVM_CONSTANT_UnresolvedClassInError: {
1945         printf("UnresolvedClassInErr: %s", WARN_MSG);
1946         break;
1947       }
1948       case JVM_CONSTANT_StringIndex: {
1949         printf("StringIndex: %s", WARN_MSG);
1950         break;
1951       }
1952     }
1953     printf(";\n");
1954     bytes += ent_size;
1955     size  += ent_size;
1956   }
1957   printf("Cpool size: %d\n", size);
1958   fflush(0);
1959   return;
1960 } /* end print_cpool_bytes */
1961 
1962 
1963 // Returns size of constant pool entry.

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

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

 375   // Shared ConstantPools are in the RO region, so the _flags cannot be modified.
 376   // The _on_stack flag is used to prevent ConstantPools from deallocation during
 377   // class redefinition. Since shared ConstantPools cannot be deallocated anyway,
 378   // we always set _on_stack to true to avoid having to change _flags during runtime.
 379   _flags |= (_on_stack | _is_shared);
 380 
 381   if (!_pool_holder->is_linked() && !_pool_holder->verified_at_dump_time()) {
 382     return;
 383   }
 384   // Resolved references are not in the shared archive.
 385   // Save the length for restoration.  It is not necessarily the same length
 386   // as reference_map.length() if invokedynamic is saved. It is needed when
 387   // re-creating the resolved reference array if archived heap data cannot be map
 388   // at runtime.
 389   set_resolved_reference_length(
 390     resolved_references() != NULL ? resolved_references()->length() : 0);
 391   set_resolved_references(OopHandle());
 392 
 393   int num_klasses = 0;
 394   for (int index = 1; index < length(); index++) { // Index 0 is unused
 395     jbyte qdesc_bit = tag_at(index).is_Qdescriptor_klass() ? (jbyte) JVM_CONSTANT_QDescBit : 0;
 396     if (tag_at(index).is_unresolved_klass_in_error()) {
 397       tag_at_put(index, JVM_CONSTANT_UnresolvedClass | qdesc_bit);
 398     } else if (tag_at(index).is_method_handle_in_error()) {
 399       tag_at_put(index, JVM_CONSTANT_MethodHandle);
 400     } else if (tag_at(index).is_method_type_in_error()) {
 401       tag_at_put(index, JVM_CONSTANT_MethodType);
 402     } else if (tag_at(index).is_dynamic_constant_in_error()) {
 403       tag_at_put(index, JVM_CONSTANT_Dynamic);
 404     }
 405     if (tag_at(index).is_klass()) {
 406       // This class was resolved as a side effect of executing Java code
 407       // during dump time. We need to restore it back to an UnresolvedClass,
 408       // so that the proper class loading and initialization can happen
 409       // at runtime.
 410       bool clear_it = true;
 411       if (pool_holder()->is_hidden() && index == pool_holder()->this_class_index()) {
 412         // All references to a hidden class's own field/methods are through this
 413         // index. We cannot clear it. See comments in ClassFileParser::fill_instance_klass.
 414         clear_it = false;
 415       }
 416       if (clear_it) {
 417         CPKlassSlot kslot = klass_slot_at(index);
 418         int resolved_klass_index = kslot.resolved_klass_index();
 419         int name_index = kslot.name_index();
 420         assert(tag_at(name_index).is_symbol(), "sanity");
 421         resolved_klasses()->at_put(resolved_klass_index, NULL);
 422         tag_at_put(index, JVM_CONSTANT_UnresolvedClass | qdesc_bit);
 423         assert(klass_name_at(index) == symbol_at(name_index), "sanity");
 424       }
 425     }
 426   }
 427   if (cache() != NULL) {
 428     // cache() is NULL if this class is not yet linked.
 429     cache()->remove_unshareable_info();
 430   }
 431 }
 432 #endif // INCLUDE_CDS
 433 
 434 int ConstantPool::cp_to_object_index(int cp_index) {
 435   // this is harder don't do this so much.
 436   int i = reference_map()->find(cp_index);
 437   // We might not find the index for jsr292 call.
 438   return (i < 0) ? _no_index_sentinel : i;
 439 }
 440 
 441 void ConstantPool::string_at_put(int which, int obj_index, oop str) {
 442   resolved_references()->obj_at_put(obj_index, str);

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

1005     case JVM_CONSTANT_Long:
1006     case JVM_CONSTANT_Double:
1007       // these guys trigger OOM at worst
1008       break;
1009     default:
1010       (*status_return) = false;
1011       return NULL;
1012     }
1013     // from now on there is either success or an OOME
1014     (*status_return) = true;
1015   }
1016 
1017   switch (tag.value()) {
1018 
1019   case JVM_CONSTANT_UnresolvedClass:
1020   case JVM_CONSTANT_Class:
1021     {
1022       assert(cache_index == _no_index_sentinel, "should not have been set");
1023       Klass* resolved = klass_at_impl(this_cp, index, CHECK_NULL);
1024       // ldc wants the java mirror.
1025       result_oop = tag.is_Qdescriptor_klass()
1026                       ? InlineKlass::cast(resolved)->val_mirror()
1027                       : resolved->java_mirror();
1028       break;
1029     }
1030 
1031   case JVM_CONSTANT_Dynamic:
1032     {
1033       // Resolve the Dynamically-Computed constant to invoke the BSM in order to obtain the resulting oop.
1034       BootstrapInfo bootstrap_specifier(this_cp, index);
1035 
1036       // The initial step in resolving an unresolved symbolic reference to a
1037       // dynamically-computed constant is to resolve the symbolic reference to a
1038       // method handle which will be the bootstrap method for the dynamically-computed
1039       // constant. If resolution of the java.lang.invoke.MethodHandle for the bootstrap
1040       // method fails, then a MethodHandleInError is stored at the corresponding
1041       // bootstrap method's CP index for the CONSTANT_MethodHandle_info. No need to
1042       // set a DynamicConstantInError here since any subsequent use of this
1043       // bootstrap method will encounter the resolution of MethodHandleInError.
1044       // Both the first, (resolution of the BSM and its static arguments), and the second tasks,
1045       // (invocation of the BSM), of JVMS Section 5.4.3.6 occur within invoke_bootstrap_method()
1046       // for the bootstrap_specifier created above.
1047       SystemDictionary::invoke_bootstrap_method(bootstrap_specifier, THREAD);

1925       case JVM_CONSTANT_Long: {
1926         u8 val = Bytes::get_Java_u8(bytes);
1927         printf("long         " INT64_FORMAT, (int64_t) *(jlong *) &val);
1928         ent_size = 8;
1929         idx++; // Long takes two cpool slots
1930         break;
1931       }
1932       case JVM_CONSTANT_Double: {
1933         u8 val = Bytes::get_Java_u8(bytes);
1934         printf("double       %5.3fd", *(jdouble *)&val);
1935         ent_size = 8;
1936         idx++; // Double takes two cpool slots
1937         break;
1938       }
1939       case JVM_CONSTANT_Class: {
1940         idx1 = Bytes::get_Java_u2(bytes);
1941         printf("class        #%03d", idx1);
1942         ent_size = 2;
1943         break;
1944       }
1945       case (JVM_CONSTANT_Class | JVM_CONSTANT_QDescBit): {
1946         idx1 = Bytes::get_Java_u2(bytes);
1947         printf("qclass        #%03d", idx1);
1948         ent_size = 2;
1949         break;
1950       }
1951       case JVM_CONSTANT_String: {
1952         idx1 = Bytes::get_Java_u2(bytes);
1953         printf("String       #%03d", idx1);
1954         ent_size = 2;
1955         break;
1956       }
1957       case JVM_CONSTANT_Fieldref: {
1958         idx1 = Bytes::get_Java_u2(bytes);
1959         idx2 = Bytes::get_Java_u2(bytes+2);
1960         printf("Field        #%03d, #%03d", (int) idx1, (int) idx2);
1961         ent_size = 4;
1962         break;
1963       }
1964       case JVM_CONSTANT_Methodref: {
1965         idx1 = Bytes::get_Java_u2(bytes);
1966         idx2 = Bytes::get_Java_u2(bytes+2);
1967         printf("Method       #%03d, #%03d", idx1, idx2);
1968         ent_size = 4;
1969         break;
1970       }

1973         idx2 = Bytes::get_Java_u2(bytes+2);
1974         printf("InterfMethod #%03d, #%03d", idx1, idx2);
1975         ent_size = 4;
1976         break;
1977       }
1978       case JVM_CONSTANT_NameAndType: {
1979         idx1 = Bytes::get_Java_u2(bytes);
1980         idx2 = Bytes::get_Java_u2(bytes+2);
1981         printf("NameAndType  #%03d, #%03d", idx1, idx2);
1982         ent_size = 4;
1983         break;
1984       }
1985       case JVM_CONSTANT_ClassIndex: {
1986         printf("ClassIndex  %s", WARN_MSG);
1987         break;
1988       }
1989       case JVM_CONSTANT_UnresolvedClass: {
1990         printf("UnresolvedClass: %s", WARN_MSG);
1991         break;
1992       }
1993       case (JVM_CONSTANT_UnresolvedClass | JVM_CONSTANT_QDescBit): {
1994         printf("UnresolvedQClass: %s", WARN_MSG);
1995         break;
1996       }
1997       case JVM_CONSTANT_UnresolvedClassInError: {
1998         printf("UnresolvedClassInErr: %s", WARN_MSG);
1999         break;
2000       }
2001       case JVM_CONSTANT_StringIndex: {
2002         printf("StringIndex: %s", WARN_MSG);
2003         break;
2004       }
2005     }
2006     printf(";\n");
2007     bytes += ent_size;
2008     size  += ent_size;
2009   }
2010   printf("Cpool size: %d\n", size);
2011   fflush(0);
2012   return;
2013 } /* end print_cpool_bytes */
2014 
2015 
2016 // Returns size of constant pool entry.
< prev index next >