< prev index next >

src/hotspot/share/oops/constantPool.cpp

Print this page

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

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

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

 250   release_tag_at_put(class_index, JVM_CONSTANT_Class);
 251 }
 252 
 253 #if INCLUDE_CDS_JAVA_HEAP
 254 // Archive the resolved references
 255 void ConstantPool::archive_resolved_references() {
 256   if (_cache == NULL) {
 257     return; // nothing to do
 258   }
 259 
 260   InstanceKlass *ik = pool_holder();
 261   if (!(ik->is_shared_boot_class() || ik->is_shared_platform_class() ||
 262         ik->is_shared_app_class())) {
 263     // Archiving resolved references for classes from non-builtin loaders
 264     // is not yet supported.
 265     return;
 266   }
 267 
 268   objArrayOop rr = resolved_references();
 269   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()->remove_unshareable_info();
 426   }
 427 }
 428 
 429 int ConstantPool::cp_to_object_index(int cp_index) {
 430   // this is harder don't do this so much.
 431   int i = reference_map()->find(cp_index);
 432   // We might not find the index for jsr292 call.
 433   return (i < 0) ? _no_index_sentinel : i;
 434 }
 435 
 436 void ConstantPool::string_at_put(int which, int obj_index, oop str) {
 437   resolved_references()->obj_at_put(obj_index, str);
 438 }
 439 

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






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





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



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
















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




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




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

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


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

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






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

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




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

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

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

 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()->remove_unshareable_info();
 429   }
 430 }
 431 
 432 int ConstantPool::cp_to_object_index(int cp_index) {
 433   // this is harder don't do this so much.
 434   int i = reference_map()->find(cp_index);
 435   // We might not find the index for jsr292 call.
 436   return (i < 0) ? _no_index_sentinel : i;
 437 }
 438 
 439 void ConstantPool::string_at_put(int which, int obj_index, oop str) {
 440   resolved_references()->obj_at_put(obj_index, str);
 441 }
 442 

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

1003     case JVM_CONSTANT_Long:
1004     case JVM_CONSTANT_Double:
1005       // these guys trigger OOM at worst
1006       break;
1007     default:
1008       (*status_return) = false;
1009       return NULL;
1010     }
1011     // from now on there is either success or an OOME
1012     (*status_return) = true;
1013   }
1014 
1015   switch (tag.value()) {
1016 
1017   case JVM_CONSTANT_UnresolvedClass:
1018   case JVM_CONSTANT_Class:
1019     {
1020       assert(cache_index == _no_index_sentinel, "should not have been set");
1021       Klass* resolved = klass_at_impl(this_cp, index, CHECK_NULL);
1022       // ldc wants the java mirror.
1023       result_oop = tag.is_Qdescriptor_klass()
1024                       ? InlineKlass::cast(resolved)->val_mirror()
1025                       : resolved->java_mirror();
1026       break;
1027     }
1028 
1029   case JVM_CONSTANT_Dynamic:
1030     {
1031       // Resolve the Dynamically-Computed constant to invoke the BSM in order to obtain the resulting oop.
1032       BootstrapInfo bootstrap_specifier(this_cp, index);
1033 
1034       // The initial step in resolving an unresolved symbolic reference to a
1035       // dynamically-computed constant is to resolve the symbolic reference to a
1036       // method handle which will be the bootstrap method for the dynamically-computed
1037       // constant. If resolution of the java.lang.invoke.MethodHandle for the bootstrap
1038       // method fails, then a MethodHandleInError is stored at the corresponding
1039       // bootstrap method's CP index for the CONSTANT_MethodHandle_info. No need to
1040       // set a DynamicConstantInError here since any subsequent use of this
1041       // bootstrap method will encounter the resolution of MethodHandleInError.
1042       // Both the first, (resolution of the BSM and its static arguments), and the second tasks,
1043       // (invocation of the BSM), of JVMS Section 5.4.3.6 occur within invoke_bootstrap_method()
1044       // for the bootstrap_specifier created above.
1045       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_Class | JVM_CONSTANT_QDescBit): {
1944         idx1 = Bytes::get_Java_u2(bytes);
1945         printf("qclass        #%03d", idx1);
1946         ent_size = 2;
1947         break;
1948       }
1949       case JVM_CONSTANT_String: {
1950         idx1 = Bytes::get_Java_u2(bytes);
1951         printf("String       #%03d", idx1);
1952         ent_size = 2;
1953         break;
1954       }
1955       case JVM_CONSTANT_Fieldref: {
1956         idx1 = Bytes::get_Java_u2(bytes);
1957         idx2 = Bytes::get_Java_u2(bytes+2);
1958         printf("Field        #%03d, #%03d", (int) idx1, (int) idx2);
1959         ent_size = 4;
1960         break;
1961       }
1962       case JVM_CONSTANT_Methodref: {
1963         idx1 = Bytes::get_Java_u2(bytes);
1964         idx2 = Bytes::get_Java_u2(bytes+2);
1965         printf("Method       #%03d, #%03d", idx1, idx2);
1966         ent_size = 4;
1967         break;
1968       }

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