< 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();

 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_UnresolvedClassInError:
 980   case JVM_CONSTANT_Class:
 981     {
 982       assert(cache_index == _no_index_sentinel, "should not have been set");
 983       Klass* resolved = klass_at_impl(this_cp, index, CHECK_NULL);
 984       // ldc wants the java mirror.
 985       result_oop = resolved->java_mirror();


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

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






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

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




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

2096       case JVM_CONSTANT_Float: {
2097         jfloat val = float_at(idx);
2098         Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val);
2099         break;
2100       }
2101       case JVM_CONSTANT_Long: {
2102         jlong val = long_at(idx);
2103         Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
2104         idx++;             // Long takes two cpool slots
2105         break;
2106       }
2107       case JVM_CONSTANT_Double: {
2108         jdouble val = double_at(idx);
2109         Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
2110         idx++;             // Double takes two cpool slots
2111         break;
2112       }
2113       case JVM_CONSTANT_Class:
2114       case JVM_CONSTANT_UnresolvedClass:
2115       case JVM_CONSTANT_UnresolvedClassInError: {

2116         *bytes = JVM_CONSTANT_Class;
2117         Symbol* sym = klass_name_at(idx);
2118         idx1 = tbl->symbol_to_value(sym);
2119         assert(idx1 != 0, "Have not found a hashtable entry");
2120         Bytes::put_Java_u2((address) (bytes+1), idx1);
2121         DBG(printf("JVM_CONSTANT_Class: idx=#%03hd, %s", idx1, sym->as_utf8()));
2122         break;
2123       }
2124       case JVM_CONSTANT_String: {
2125         *bytes = JVM_CONSTANT_String;
2126         Symbol* sym = unresolved_string_at(idx);
2127         idx1 = tbl->symbol_to_value(sym);
2128         assert(idx1 != 0, "Have not found a hashtable entry");
2129         Bytes::put_Java_u2((address) (bytes+1), idx1);
2130         DBG(printf("JVM_CONSTANT_String: idx=#%03hd, %s", idx1, sym->as_utf8()));
2131         break;
2132       }
2133       case JVM_CONSTANT_Fieldref:
2134       case JVM_CONSTANT_Methodref:
2135       case JVM_CONSTANT_InterfaceMethodref: {

  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();

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_UnresolvedClassInError:
1021   case JVM_CONSTANT_Class:
1022     {
1023       assert(cache_index == _no_index_sentinel, "should not have been set");
1024       Klass* resolved = klass_at_impl(this_cp, index, CHECK_NULL);
1025       // ldc wants the java mirror.
1026       result_oop = tag.is_Qdescriptor_klass()
1027                       ? InlineKlass::cast(resolved)->val_mirror()
1028                       : resolved->java_mirror();
1029       break;
1030     }
1031 
1032   case JVM_CONSTANT_Dynamic:
1033     {
1034       // Resolve the Dynamically-Computed constant to invoke the BSM in order to obtain the resulting oop.
1035       BootstrapInfo bootstrap_specifier(this_cp, index);
1036 
1037       // The initial step in resolving an unresolved symbolic reference to a
1038       // dynamically-computed constant is to resolve the symbolic reference to a
1039       // method handle which will be the bootstrap method for the dynamically-computed
1040       // constant. If resolution of the java.lang.invoke.MethodHandle for the bootstrap
1041       // method fails, then a MethodHandleInError is stored at the corresponding
1042       // bootstrap method's CP index for the CONSTANT_MethodHandle_info. No need to
1043       // set a DynamicConstantInError here since any subsequent use of this
1044       // bootstrap method will encounter the resolution of MethodHandleInError.
1045       // Both the first, (resolution of the BSM and its static arguments), and the second tasks,
1046       // (invocation of the BSM), of JVMS Section 5.4.3.6 occur within invoke_bootstrap_method()
1047       // for the bootstrap_specifier created above.
1048       SystemDictionary::invoke_bootstrap_method(bootstrap_specifier, THREAD);

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

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

2149       case JVM_CONSTANT_Float: {
2150         jfloat val = float_at(idx);
2151         Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val);
2152         break;
2153       }
2154       case JVM_CONSTANT_Long: {
2155         jlong val = long_at(idx);
2156         Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
2157         idx++;             // Long takes two cpool slots
2158         break;
2159       }
2160       case JVM_CONSTANT_Double: {
2161         jdouble val = double_at(idx);
2162         Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
2163         idx++;             // Double takes two cpool slots
2164         break;
2165       }
2166       case JVM_CONSTANT_Class:
2167       case JVM_CONSTANT_UnresolvedClass:
2168       case JVM_CONSTANT_UnresolvedClassInError: {
2169         assert(!tag_at(idx).is_Qdescriptor_klass(), "Failed to encode QDesc");
2170         *bytes = JVM_CONSTANT_Class;
2171         Symbol* sym = klass_name_at(idx);
2172         idx1 = tbl->symbol_to_value(sym);
2173         assert(idx1 != 0, "Have not found a hashtable entry");
2174         Bytes::put_Java_u2((address) (bytes+1), idx1);
2175         DBG(printf("JVM_CONSTANT_Class: idx=#%03hd, %s", idx1, sym->as_utf8()));
2176         break;
2177       }
2178       case JVM_CONSTANT_String: {
2179         *bytes = JVM_CONSTANT_String;
2180         Symbol* sym = unresolved_string_at(idx);
2181         idx1 = tbl->symbol_to_value(sym);
2182         assert(idx1 != 0, "Have not found a hashtable entry");
2183         Bytes::put_Java_u2((address) (bytes+1), idx1);
2184         DBG(printf("JVM_CONSTANT_String: idx=#%03hd, %s", idx1, sym->as_utf8()));
2185         break;
2186       }
2187       case JVM_CONSTANT_Fieldref:
2188       case JVM_CONSTANT_Methodref:
2189       case JVM_CONSTANT_InterfaceMethodref: {
< prev index next >