< prev index next >

src/share/vm/oops/constantPool.cpp

Print this page


  30 #include "classfile/systemDictionary.hpp"
  31 #include "classfile/vmSymbols.hpp"
  32 #include "interpreter/linkResolver.hpp"
  33 #include "memory/heapInspection.hpp"
  34 #include "memory/metadataFactory.hpp"
  35 #include "memory/oopFactory.hpp"
  36 #include "memory/resourceArea.hpp"
  37 #include "oops/constantPool.hpp"
  38 #include "oops/instanceKlass.hpp"
  39 #include "oops/objArrayKlass.hpp"
  40 #include "oops/objArrayOop.inline.hpp"
  41 #include "oops/oop.inline.hpp"
  42 #include "runtime/fieldType.hpp"
  43 #include "runtime/init.hpp"
  44 #include "runtime/javaCalls.hpp"
  45 #include "runtime/signature.hpp"
  46 #include "runtime/vframe.hpp"
  47 #include "utilities/copy.hpp"
  48 
  49 ConstantPool* ConstantPool::allocate(ClassLoaderData* loader_data, int length, TRAPS) {
  50   // Tags are RW but comment below applies to tags also.
  51   Array<u1>* tags = MetadataFactory::new_writeable_array<u1>(loader_data, length, 0, CHECK_NULL);
  52 
  53   int size = ConstantPool::size(length);
  54 
  55   // CDS considerations:
  56   // Allocate read-write but may be able to move to read-only at dumping time
  57   // if all the klasses are resolved.  The only other field that is writable is
  58   // the resolved_references array, which is recreated at startup time.
  59   // But that could be moved to InstanceKlass (although a pain to access from
  60   // assembly code).  Maybe it could be moved to the cpCache which is RW.
  61   return new (loader_data, size, false, MetaspaceObj::ConstantPoolType, THREAD) ConstantPool(tags);
  62 }
  63 
  64 #ifdef ASSERT
  65 
  66 // MetaspaceObj allocation invariant is calloc equivalent memory
  67 // simple verification of this here (JVM_CONSTANT_Invalid == 0 )
  68 static bool tag_array_is_zero_initialized(Array<u1>* tags) {
  69   assert(tags != NULL, "invariant");
  70   const int length = tags->length();
  71   for (int index = 0; index < length; ++index) {
  72     if (JVM_CONSTANT_Invalid != tags->at(index)) {
  73       return false;
  74     }
  75   }
  76   return true;
  77 }
  78 
  79 #endif
  80 
  81 ConstantPool::ConstantPool(Array<u1>* tags) :
  82   _tags(tags),
  83   _length(tags->length()),
  84   _flags(0) {
  85 
  86     assert(_tags != NULL, "invariant");
  87     assert(tags->length() == _length, "invariant");
  88     assert(tag_array_is_zero_initialized(tags), "invariant");
  89     assert(0 == _flags, "invariant");
  90     assert(0 == version(), "invariant");
  91     assert(NULL == _pool_holder, "invariant");
  92 }
  93 
  94 void ConstantPool::deallocate_contents(ClassLoaderData* loader_data) {
  95   MetadataFactory::free_metadata(loader_data, cache());
  96   set_cache(NULL);
  97   MetadataFactory::free_array<u2>(loader_data, reference_map());
  98   set_reference_map(NULL);






  99 
 100   MetadataFactory::free_array<jushort>(loader_data, operands());
 101   set_operands(NULL);
 102 
 103   release_C_heap_structures();
 104 
 105   // free tag array
 106   MetadataFactory::free_array<u1>(loader_data, tags());
 107   set_tags(NULL);
 108 }
 109 
 110 void ConstantPool::release_C_heap_structures() {
 111   // walk constant pool and decrement symbol reference counts
 112   unreference_symbols();
 113 }
 114 
 115 objArrayOop ConstantPool::resolved_references() const {
 116   return (objArrayOop)JNIHandles::resolve(_resolved_references);
 117 }
 118 
 119 // Create resolved_references array and mapping array for original cp indexes
 120 // The ldc bytecode was rewritten to have the resolved reference array index so need a way
 121 // to map it back for resolving and some unlikely miscellaneous uses.
 122 // The objects created by invokedynamic are appended to this list.
 123 void ConstantPool::initialize_resolved_references(ClassLoaderData* loader_data,
 124                                                   const intStack& reference_map,
 125                                                   int constant_pool_map_length,
 126                                                   TRAPS) {
 127   // Initialized the resolved object cache.
 128   int map_length = reference_map.length();
 129   if (map_length > 0) {
 130     // Only need mapping back to constant pool entries.  The map isn't used for
 131     // invokedynamic resolved_reference entries.  For invokedynamic entries,
 132     // the constant pool cache index has the mapping back to both the constant
 133     // pool and to the resolved reference index.
 134     if (constant_pool_map_length > 0) {
 135       Array<u2>* om = MetadataFactory::new_array<u2>(loader_data, constant_pool_map_length, CHECK);
 136 
 137       for (int i = 0; i < constant_pool_map_length; i++) {
 138         int x = reference_map.at(i);
 139         assert(x == (int)(jushort) x, "klass index is too big");
 140         om->at_put(i, (jushort)x);
 141       }
 142       set_reference_map(om);
 143     }
 144 
 145     // Create Java array for holding resolved strings, methodHandles,
 146     // methodTypes, invokedynamic and invokehandle appendix objects, etc.
 147     objArrayOop stom = oopFactory::new_objArray(SystemDictionary::Object_klass(), map_length, CHECK);
 148     Handle refs_handle (THREAD, (oop)stom);  // must handleize.
 149     set_resolved_references(loader_data->add_handle(refs_handle));
 150   }
 151 }
 152 






































































 153 // CDS support. Create a new resolved_references array.
 154 void ConstantPool::restore_unshareable_info(TRAPS) {
 155   assert(is_constantPool(), "ensure C++ vtable is restored");


 156 
 157   // Only create the new resolved references array if it hasn't been attempted before
 158   if (resolved_references() != NULL) return;
 159 
 160   // restore the C++ vtable from the shared archive
 161   restore_vtable();
 162 
 163   if (SystemDictionary::Object_klass_loaded()) {
 164     // Recreate the object array and add to ClassLoaderData.
 165     int map_length = resolved_reference_length();
 166     if (map_length > 0) {
 167       objArrayOop stom = oopFactory::new_objArray(SystemDictionary::Object_klass(), map_length, CHECK);
 168       Handle refs_handle (THREAD, (oop)stom);  // must handleize.
 169 
 170       ClassLoaderData* loader_data = pool_holder()->class_loader_data();
 171       set_resolved_references(loader_data->add_handle(refs_handle));
 172     }
 173   }
 174 }
 175 
 176 void ConstantPool::remove_unshareable_info() {
 177   // Resolved references are not in the shared archive.
 178   // Save the length for restoration.  It is not necessarily the same length
 179   // as reference_map.length() if invokedynamic is saved.
 180   set_resolved_reference_length(
 181     resolved_references() != NULL ? resolved_references()->length() : 0);
 182   set_resolved_references(NULL);






 183 }
 184 
 185 int ConstantPool::cp_to_object_index(int cp_index) {
 186   // this is harder don't do this so much.
 187   int i = reference_map()->find(cp_index);
 188   // We might not find the index for jsr292 call.
 189   return (i < 0) ? _no_index_sentinel : i;
 190 }
 191 
 192 void ConstantPool::string_at_put(int which, int obj_index, oop str) {
 193   resolved_references()->obj_at_put(obj_index, str);
 194 }
 195 
 196 void ConstantPool::trace_class_resolution(const constantPoolHandle& this_cp, Klass* k) {
 197   ResourceMark rm;
 198   int line_number = -1;
 199   const char * source_file = NULL;
 200   if (JavaThread::current()->has_last_Java_frame()) {
 201     // try to identify the method which called this function.
 202     vframeStream vfst(JavaThread::current());


 212     // only print something if the classes are different
 213     if (source_file != NULL) {
 214       log_debug(class, resolve)("%s %s %s:%d",
 215                  this_cp->pool_holder()->external_name(),
 216                  k->external_name(), source_file, line_number);
 217     } else {
 218       log_debug(class, resolve)("%s %s",
 219                  this_cp->pool_holder()->external_name(),
 220                  k->external_name());
 221     }
 222   }
 223 }
 224 
 225 Klass* ConstantPool::klass_at_impl(const constantPoolHandle& this_cp, int which,
 226                                    bool save_resolution_error, TRAPS) {
 227   assert(THREAD->is_Java_thread(), "must be a Java thread");
 228 
 229   // A resolved constantPool entry will contain a Klass*, otherwise a Symbol*.
 230   // It is not safe to rely on the tag bit's here, since we don't have a lock, and
 231   // the entry and tag is not updated atomicly.
 232   CPSlot entry = this_cp->slot_at(which);
 233   if (entry.is_resolved()) {
 234     assert(entry.get_klass()->is_klass(), "must be");
 235     // Already resolved - return entry.
 236     return entry.get_klass();



 237   }
 238 
 239   // This tag doesn't change back to unresolved class unless at a safepoint.
 240   if (this_cp->tag_at(which).is_unresolved_klass_in_error()) {
 241     // The original attempt to resolve this constant pool entry failed so find the
 242     // class of the original error and throw another error of the same class
 243     // (JVMS 5.4.3).
 244     // If there is a detail message, pass that detail message to the error.
 245     // The JVMS does not strictly require us to duplicate the same detail message,
 246     // or any internal exception fields such as cause or stacktrace.  But since the
 247     // detail message is often a class name or other literal string, we will repeat it
 248     // if we can find it in the symbol table.
 249     throw_resolution_error(this_cp, which, CHECK_0);
 250     ShouldNotReachHere();
 251   }
 252 
 253   Handle mirror_handle;
 254   Symbol* name = entry.get_symbol();
 255   Handle loader (THREAD, this_cp->pool_holder()->class_loader());
 256   Handle protection_domain (THREAD, this_cp->pool_holder()->protection_domain());
 257   Klass* k = SystemDictionary::resolve_or_fail(name, loader, protection_domain, true, THREAD);
 258   if (!HAS_PENDING_EXCEPTION) {
 259     // preserve the resolved klass from unloading
 260     mirror_handle = Handle(THREAD, k->java_mirror());
 261     // Do access check for klasses
 262     verify_constant_pool_resolve(this_cp, k, THREAD);
 263   }
 264 
 265   // Failed to resolve class. We must record the errors so that subsequent attempts
 266   // to resolve this constant pool entry fail with the same error (JVMS 5.4.3).
 267   if (HAS_PENDING_EXCEPTION) {
 268     if (save_resolution_error) {
 269       save_and_throw_exception(this_cp, which, constantTag(JVM_CONSTANT_UnresolvedClass), CHECK_NULL);
 270       // If CHECK_NULL above doesn't return the exception, that means that
 271       // some other thread has beaten us and has resolved the class.
 272       // To preserve old behavior, we return the resolved class.
 273       entry = this_cp->resolved_klass_at(which);
 274       assert(entry.is_resolved(), "must be resolved if exception was cleared");
 275       assert(entry.get_klass()->is_klass(), "must be resolved to a klass");
 276       return entry.get_klass();
 277     } else {
 278       return NULL;  // return the pending exception
 279     }
 280   }
 281 
 282   // Make this class loader depend upon the class loader owning the class reference
 283   ClassLoaderData* this_key = this_cp->pool_holder()->class_loader_data();
 284   this_key->record_dependency(k, CHECK_NULL); // Can throw OOM
 285 
 286   // logging for class+resolve.
 287   if (log_is_enabled(Debug, class, resolve)){
 288     trace_class_resolution(this_cp, k);
 289   }
 290   this_cp->klass_at_put(which, k);
 291   entry = this_cp->resolved_klass_at(which);
 292   assert(entry.is_resolved() && entry.get_klass()->is_klass(), "must be resolved at this point");
 293   return entry.get_klass();



 294 }
 295 
 296 
 297 // Does not update ConstantPool* - to avoid any exception throwing. Used
 298 // by compiler and exception handling.  Also used to avoid classloads for
 299 // instanceof operations. Returns NULL if the class has not been loaded or
 300 // if the verification of constant pool failed
 301 Klass* ConstantPool::klass_at_if_loaded(const constantPoolHandle& this_cp, int which) {
 302   CPSlot entry = this_cp->slot_at(which);
 303   if (entry.is_resolved()) {
 304     assert(entry.get_klass()->is_klass(), "must be");
 305     return entry.get_klass();




 306   } else {
 307     assert(entry.is_unresolved(), "must be either symbol or klass");
 308     Thread *thread = Thread::current();
 309     Symbol* name = entry.get_symbol();
 310     oop loader = this_cp->pool_holder()->class_loader();
 311     oop protection_domain = this_cp->pool_holder()->protection_domain();
 312     Handle h_prot (thread, protection_domain);
 313     Handle h_loader (thread, loader);
 314     Klass* k = SystemDictionary::find(name, h_loader, h_prot, thread);
 315 
 316     if (k != NULL) {
 317       // Make sure that resolving is legal
 318       EXCEPTION_MARK;
 319       // return NULL if verification fails
 320       verify_constant_pool_resolve(this_cp, k, THREAD);
 321       if (HAS_PENDING_EXCEPTION) {
 322         CLEAR_PENDING_EXCEPTION;
 323         return NULL;
 324       }
 325       return k;
 326     } else {
 327       return k;
 328     }
 329   }


 467   }
 468 }
 469 
 470 
 471 int ConstantPool::name_ref_index_at(int which_nt) {
 472   jint ref_index = name_and_type_at(which_nt);
 473   return extract_low_short_from_int(ref_index);
 474 }
 475 
 476 
 477 int ConstantPool::signature_ref_index_at(int which_nt) {
 478   jint ref_index = name_and_type_at(which_nt);
 479   return extract_high_short_from_int(ref_index);
 480 }
 481 
 482 
 483 Klass* ConstantPool::klass_ref_at(int which, TRAPS) {
 484   return klass_at(klass_ref_index_at(which), THREAD);
 485 }
 486 
 487 
 488 Symbol* ConstantPool::klass_name_at(int which) const {
 489   assert(tag_at(which).is_unresolved_klass() || tag_at(which).is_klass(),
 490          "Corrupted constant pool");
 491   // A resolved constantPool entry will contain a Klass*, otherwise a Symbol*.
 492   // It is not safe to rely on the tag bit's here, since we don't have a lock, and the entry and
 493   // tag is not updated atomicly.
 494   CPSlot entry = slot_at(which);
 495   if (entry.is_resolved()) {
 496     // Already resolved - return entry's name.
 497     assert(entry.get_klass()->is_klass(), "must be");
 498     return entry.get_klass()->name();
 499   } else {
 500     assert(entry.is_unresolved(), "must be either symbol or klass");
 501     return entry.get_symbol();
 502   }
 503 }
 504 
 505 Symbol* ConstantPool::klass_ref_at_noresolve(int which) {
 506   jint ref_index = klass_ref_index_at(which);
 507   return klass_at_noresolve(ref_index);
 508 }
 509 
 510 Symbol* ConstantPool::uncached_klass_ref_at_noresolve(int which) {
 511   jint ref_index = uncached_klass_ref_index_at(which);
 512   return klass_at_noresolve(ref_index);
 513 }
 514 
 515 char* ConstantPool::string_at_noresolve(int which) {
 516   return unresolved_string_at(which)->as_C_string();
 517 }
 518 
 519 BasicType ConstantPool::basic_type_for_signature_at(int which) const {
 520   return FieldType::basic_type(symbol_at(which));
 521 }
 522 


 833   // If the string has already been interned, this entry will be non-null
 834   oop str = this_cp->resolved_references()->obj_at(obj_index);
 835   if (str != NULL) return str;
 836   Symbol* sym = this_cp->unresolved_string_at(which);
 837   str = StringTable::intern(sym, CHECK_(NULL));
 838   this_cp->string_at_put(which, obj_index, str);
 839   assert(java_lang_String::is_instance(str), "must be string");
 840   return str;
 841 }
 842 
 843 
 844 bool ConstantPool::klass_name_at_matches(const InstanceKlass* k, int which) {
 845   // Names are interned, so we can compare Symbol*s directly
 846   Symbol* cp_name = klass_name_at(which);
 847   return (cp_name == k->name());
 848 }
 849 
 850 
 851 // Iterate over symbols and decrement ones which are Symbol*s
 852 // This is done during GC.
 853 // Only decrement the UTF8 symbols. Unresolved classes and strings point to
 854 // these symbols but didn't increment the reference count.
 855 void ConstantPool::unreference_symbols() {
 856   for (int index = 1; index < length(); index++) { // Index 0 is unused
 857     constantTag tag = tag_at(index);
 858     if (tag.is_symbol()) {
 859       symbol_at(index)->decrement_refcount();
 860     }
 861   }
 862 }
 863 
 864 
 865 // Compare this constant pool's entry at index1 to the constant pool
 866 // cp2's entry at index2.
 867 bool ConstantPool::compare_entry_to(int index1, const constantPoolHandle& cp2,
 868        int index2, TRAPS) {
 869 
 870   // The error tags are equivalent to non-error tags when comparing
 871   jbyte t1 = tag_at(index1).non_error_value();
 872   jbyte t2 = cp2->tag_at(index2).non_error_value();
 873 


1214     default:
1215       // all others take one constant pool entry
1216       src_i++;
1217       dest_i++;
1218       break;
1219     }
1220   }
1221   copy_operands(from_cp, to_cp, CHECK);
1222 
1223 } // end copy_cp_to_impl()
1224 
1225 
1226 // Copy this constant pool's entry at from_i to the constant pool
1227 // to_cp's entry at to_i.
1228 void ConstantPool::copy_entry_to(const constantPoolHandle& from_cp, int from_i,
1229                                         const constantPoolHandle& to_cp, int to_i,
1230                                         TRAPS) {
1231 
1232   int tag = from_cp->tag_at(from_i).value();
1233   switch (tag) {
1234   case JVM_CONSTANT_Class:
1235   {
1236     Klass* k = from_cp->klass_at(from_i, CHECK);
1237     to_cp->klass_at_put(to_i, k);
1238   } break;
1239 
1240   case JVM_CONSTANT_ClassIndex:
1241   {
1242     jint ki = from_cp->klass_index_at(from_i);
1243     to_cp->klass_index_at_put(to_i, ki);
1244   } break;
1245 
1246   case JVM_CONSTANT_Double:
1247   {
1248     jdouble d = from_cp->double_at(from_i);
1249     to_cp->double_at_put(to_i, d);
1250     // double takes two constant pool entries so init second entry's tag
1251     to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
1252   } break;
1253 
1254   case JVM_CONSTANT_Fieldref:
1255   {
1256     int class_index = from_cp->uncached_klass_ref_index_at(from_i);
1257     int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
1258     to_cp->field_at_put(to_i, class_index, name_and_type_index);
1259   } break;


1288   case JVM_CONSTANT_Methodref:
1289   {
1290     int class_index = from_cp->uncached_klass_ref_index_at(from_i);
1291     int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
1292     to_cp->method_at_put(to_i, class_index, name_and_type_index);
1293   } break;
1294 
1295   case JVM_CONSTANT_NameAndType:
1296   {
1297     int name_ref_index = from_cp->name_ref_index_at(from_i);
1298     int signature_ref_index = from_cp->signature_ref_index_at(from_i);
1299     to_cp->name_and_type_at_put(to_i, name_ref_index, signature_ref_index);
1300   } break;
1301 
1302   case JVM_CONSTANT_StringIndex:
1303   {
1304     jint si = from_cp->string_index_at(from_i);
1305     to_cp->string_index_at_put(to_i, si);
1306   } break;
1307 

1308   case JVM_CONSTANT_UnresolvedClass:
1309   case JVM_CONSTANT_UnresolvedClassInError:
1310   {
1311     // Can be resolved after checking tag, so check the slot first.
1312     CPSlot entry = from_cp->slot_at(from_i);
1313     if (entry.is_resolved()) {
1314       assert(entry.get_klass()->is_klass(), "must be");
1315       // Already resolved
1316       to_cp->klass_at_put(to_i, entry.get_klass());
1317     } else {
1318       to_cp->unresolved_klass_at_put(to_i, entry.get_symbol());
1319     }
1320   } break;
1321 
1322   case JVM_CONSTANT_String:
1323   {
1324     Symbol* s = from_cp->unresolved_string_at(from_i);
1325     to_cp->unresolved_string_at_put(to_i, s);
1326   } break;
1327 
1328   case JVM_CONSTANT_Utf8:
1329   {
1330     Symbol* s = from_cp->symbol_at(from_i);
1331     // Need to increase refcount, the old one will be thrown away and deferenced
1332     s->increment_refcount();
1333     to_cp->symbol_at_put(to_i, s);
1334   } break;
1335 
1336   case JVM_CONSTANT_MethodType:
1337   case JVM_CONSTANT_MethodTypeInError:
1338   {
1339     jint k = from_cp->method_type_index_at(from_i);


1351   case JVM_CONSTANT_InvokeDynamic:
1352   {
1353     int k1 = from_cp->invoke_dynamic_bootstrap_specifier_index(from_i);
1354     int k2 = from_cp->invoke_dynamic_name_and_type_ref_index_at(from_i);
1355     k1 += operand_array_length(to_cp->operands());  // to_cp might already have operands
1356     to_cp->invoke_dynamic_at_put(to_i, k1, k2);
1357   } break;
1358 
1359   // Invalid is used as the tag for the second constant pool entry
1360   // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
1361   // not be seen by itself.
1362   case JVM_CONSTANT_Invalid: // fall through
1363 
1364   default:
1365   {
1366     ShouldNotReachHere();
1367   } break;
1368   }
1369 } // end copy_entry_to()
1370 
1371 
1372 // Search constant pool search_cp for an entry that matches this
1373 // constant pool's entry at pattern_i. Returns the index of a
1374 // matching entry or zero (0) if there is no matching entry.
1375 int ConstantPool::find_matching_entry(int pattern_i,
1376       const constantPoolHandle& search_cp, TRAPS) {
1377 
1378   // index zero (0) is not used
1379   for (int i = 1; i < search_cp->length(); i++) {
1380     bool found = compare_entry_to(pattern_i, search_cp, i, CHECK_0);
1381     if (found) {
1382       return i;
1383     }
1384   }
1385 
1386   return 0;  // entry not found; return unused index zero (0)
1387 } // end find_matching_entry()
1388 
1389 
1390 // Compare this constant pool's bootstrap specifier at idx1 to the constant pool
1391 // cp2's bootstrap specifier at idx2.


1807       }
1808     }
1809     DBG(printf("\n"));
1810     bytes += ent_size;
1811     size  += ent_size;
1812   }
1813   assert(size == cpool_size, "Size mismatch");
1814 
1815   // Keep temorarily for debugging until it's stable.
1816   DBG(print_cpool_bytes(cnt, start_bytes));
1817   return (int)(bytes - start_bytes);
1818 } /* end copy_cpool_bytes */
1819 
1820 #undef DBG
1821 
1822 
1823 void ConstantPool::set_on_stack(const bool value) {
1824   if (value) {
1825     // Only record if it's not already set.
1826     if (!on_stack()) {

1827       _flags |= _on_stack;
1828       MetadataOnStackMark::record(this);
1829     }
1830   } else {
1831     // Clearing is done single-threadedly.

1832     _flags &= ~_on_stack;
1833   }

1834 }
1835 
1836 // JSR 292 support for patching constant pool oops after the class is linked and
1837 // the oop array for resolved references are created.
1838 // We can't do this during classfile parsing, which is how the other indexes are
1839 // patched.  The other patches are applied early for some error checking
1840 // so only defer the pseudo_strings.
1841 void ConstantPool::patch_resolved_references(GrowableArray<Handle>* cp_patches) {
1842   for (int index = 1; index < cp_patches->length(); index++) { // Index 0 is unused
1843     Handle patch = cp_patches->at(index);
1844     if (patch.not_null()) {
1845       assert (tag_at(index).is_string(), "should only be string left");
1846       // Patching a string means pre-resolving it.
1847       // The spelling in the constant pool is ignored.
1848       // The constant reference may be any object whatever.
1849       // If it is not a real interned string, the constant is referred
1850       // to as a "pseudo-string", and must be presented to the CP
1851       // explicitly, because it may require scavenging.
1852       int obj_index = cp_to_object_index(index);
1853       pseudo_string_at_put(index, obj_index, patch());


1888 #endif
1889 
1890 
1891 // Printing
1892 
1893 void ConstantPool::print_on(outputStream* st) const {
1894   assert(is_constantPool(), "must be constantPool");
1895   st->print_cr("%s", internal_name());
1896   if (flags() != 0) {
1897     st->print(" - flags: 0x%x", flags());
1898     if (has_preresolution()) st->print(" has_preresolution");
1899     if (on_stack()) st->print(" on_stack");
1900     st->cr();
1901   }
1902   if (pool_holder() != NULL) {
1903     st->print_cr(" - holder: " INTPTR_FORMAT, p2i(pool_holder()));
1904   }
1905   st->print_cr(" - cache: " INTPTR_FORMAT, p2i(cache()));
1906   st->print_cr(" - resolved_references: " INTPTR_FORMAT, p2i(resolved_references()));
1907   st->print_cr(" - reference_map: " INTPTR_FORMAT, p2i(reference_map()));

1908 
1909   for (int index = 1; index < length(); index++) {      // Index 0 is unused
1910     ((ConstantPool*)this)->print_entry_on(index, st);
1911     switch (tag_at(index).value()) {
1912       case JVM_CONSTANT_Long :
1913       case JVM_CONSTANT_Double :
1914         index++;   // Skip entry following eigth-byte constant
1915     }
1916 
1917   }
1918   st->cr();
1919 }
1920 
1921 // Print one constant pool entry
1922 void ConstantPool::print_entry_on(const int index, outputStream* st) {
1923   EXCEPTION_MARK;
1924   st->print(" - %3d : ", index);
1925   tag_at(index).print_on(st);
1926   st->print(" : ");
1927   switch (tag_at(index).value()) {


1949       break;
1950     case JVM_CONSTANT_Integer :
1951       st->print("%d", int_at(index));
1952       break;
1953     case JVM_CONSTANT_Float :
1954       st->print("%f", float_at(index));
1955       break;
1956     case JVM_CONSTANT_Long :
1957       st->print_jlong(long_at(index));
1958       break;
1959     case JVM_CONSTANT_Double :
1960       st->print("%lf", double_at(index));
1961       break;
1962     case JVM_CONSTANT_NameAndType :
1963       st->print("name_index=%d", name_ref_index_at(index));
1964       st->print(" signature_index=%d", signature_ref_index_at(index));
1965       break;
1966     case JVM_CONSTANT_Utf8 :
1967       symbol_at(index)->print_value_on(st);
1968       break;






1969     case JVM_CONSTANT_UnresolvedClass :               // fall-through
1970     case JVM_CONSTANT_UnresolvedClassInError: {
1971       CPSlot entry = slot_at(index);
1972       if (entry.is_resolved()) {
1973         entry.get_klass()->print_value_on(st);





1974       } else {
1975         entry.get_symbol()->print_value_on(st);
1976       }
1977       }
1978       break;
1979     case JVM_CONSTANT_MethodHandle :
1980     case JVM_CONSTANT_MethodHandleInError :
1981       st->print("ref_kind=%d", method_handle_ref_kind_at(index));
1982       st->print(" ref_index=%d", method_handle_index_at(index));
1983       break;
1984     case JVM_CONSTANT_MethodType :
1985     case JVM_CONSTANT_MethodTypeInError :
1986       st->print("signature_index=%d", method_type_index_at(index));
1987       break;
1988     case JVM_CONSTANT_InvokeDynamic :
1989       {
1990         st->print("bootstrap_method_index=%d", invoke_dynamic_bootstrap_method_ref_index_at(index));
1991         st->print(" name_and_type_index=%d", invoke_dynamic_name_and_type_ref_index_at(index));
1992         int argc = invoke_dynamic_argument_count_at(index);
1993         if (argc > 0) {
1994           for (int arg_i = 0; arg_i < argc; arg_i++) {
1995             int arg = invoke_dynamic_argument_index_at(index, arg_i);


2027 // Size Statistics
2028 void ConstantPool::collect_statistics(KlassSizeStats *sz) const {
2029   sz->_cp_all_bytes += (sz->_cp_bytes          = sz->count(this));
2030   sz->_cp_all_bytes += (sz->_cp_tags_bytes     = sz->count_array(tags()));
2031   sz->_cp_all_bytes += (sz->_cp_cache_bytes    = sz->count(cache()));
2032   sz->_cp_all_bytes += (sz->_cp_operands_bytes = sz->count_array(operands()));
2033   sz->_cp_all_bytes += (sz->_cp_refmap_bytes   = sz->count_array(reference_map()));
2034 
2035   sz->_ro_bytes += sz->_cp_operands_bytes + sz->_cp_tags_bytes +
2036                    sz->_cp_refmap_bytes;
2037   sz->_rw_bytes += sz->_cp_bytes + sz->_cp_cache_bytes;
2038 }
2039 #endif // INCLUDE_SERVICES
2040 
2041 // Verification
2042 
2043 void ConstantPool::verify_on(outputStream* st) {
2044   guarantee(is_constantPool(), "object must be constant pool");
2045   for (int i = 0; i< length();  i++) {
2046     constantTag tag = tag_at(i);
2047     CPSlot entry = slot_at(i);
2048     if (tag.is_klass()) {
2049       if (entry.is_resolved()) {
2050         guarantee(entry.get_klass()->is_klass(),    "should be klass");
2051       }
2052     } else if (tag.is_unresolved_klass()) {
2053       if (entry.is_resolved()) {
2054         guarantee(entry.get_klass()->is_klass(),    "should be klass");
2055       }
2056     } else if (tag.is_symbol()) {

2057       guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count");
2058     } else if (tag.is_string()) {

2059       guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count");
2060     }
2061   }
2062   if (cache() != NULL) {
2063     // Note: cache() can be NULL before a class is completely setup or
2064     // in temporary constant pools used during constant pool merging
2065     guarantee(cache()->is_constantPoolCache(), "should be constant pool cache");
2066   }
2067   if (pool_holder() != NULL) {
2068     // Note: pool_holder() can be NULL in temporary constant pools
2069     // used during constant pool merging
2070     guarantee(pool_holder()->is_klass(),    "should be klass");
2071   }
2072 }
2073 
2074 
2075 void SymbolHashMap::add_entry(Symbol* sym, u2 value) {
2076   char *str = sym->as_utf8();
2077   unsigned int hash = compute_hash(str, sym->utf8_length());
2078   unsigned int index = hash % table_size();




  30 #include "classfile/systemDictionary.hpp"
  31 #include "classfile/vmSymbols.hpp"
  32 #include "interpreter/linkResolver.hpp"
  33 #include "memory/heapInspection.hpp"
  34 #include "memory/metadataFactory.hpp"
  35 #include "memory/oopFactory.hpp"
  36 #include "memory/resourceArea.hpp"
  37 #include "oops/constantPool.hpp"
  38 #include "oops/instanceKlass.hpp"
  39 #include "oops/objArrayKlass.hpp"
  40 #include "oops/objArrayOop.inline.hpp"
  41 #include "oops/oop.inline.hpp"
  42 #include "runtime/fieldType.hpp"
  43 #include "runtime/init.hpp"
  44 #include "runtime/javaCalls.hpp"
  45 #include "runtime/signature.hpp"
  46 #include "runtime/vframe.hpp"
  47 #include "utilities/copy.hpp"
  48 
  49 ConstantPool* ConstantPool::allocate(ClassLoaderData* loader_data, int length, TRAPS) {

  50   Array<u1>* tags = MetadataFactory::new_writeable_array<u1>(loader_data, length, 0, CHECK_NULL);

  51   int size = ConstantPool::size(length);
  52   return new (loader_data, size, true, MetaspaceObj::ConstantPoolType, THREAD) ConstantPool(tags);







  53 }
  54 
  55 #ifdef ASSERT
  56 
  57 // MetaspaceObj allocation invariant is calloc equivalent memory
  58 // simple verification of this here (JVM_CONSTANT_Invalid == 0 )
  59 static bool tag_array_is_zero_initialized(Array<u1>* tags) {
  60   assert(tags != NULL, "invariant");
  61   const int length = tags->length();
  62   for (int index = 0; index < length; ++index) {
  63     if (JVM_CONSTANT_Invalid != tags->at(index)) {
  64       return false;
  65     }
  66   }
  67   return true;
  68 }
  69 
  70 #endif
  71 
  72 ConstantPool::ConstantPool(Array<u1>* tags) :
  73   _tags(tags),
  74   _length(tags->length()) {

  75 
  76     assert(_tags != NULL, "invariant");
  77     assert(tags->length() == _length, "invariant");
  78     assert(tag_array_is_zero_initialized(tags), "invariant");
  79     assert(0 == flags(), "invariant");
  80     assert(0 == version(), "invariant");
  81     assert(NULL == _pool_holder, "invariant");
  82 }
  83 
  84 void ConstantPool::deallocate_contents(ClassLoaderData* loader_data) {
  85   if (cache() != NULL) {

  86     MetadataFactory::free_array<u2>(loader_data, reference_map());
  87     set_reference_map(NULL);
  88     MetadataFactory::free_metadata(loader_data, cache());
  89     set_cache(NULL);
  90   }
  91 
  92   MetadataFactory::free_array<Klass*>(loader_data, resolved_klasses());
  93   set_resolved_klasses(NULL);
  94 
  95   MetadataFactory::free_array<jushort>(loader_data, operands());
  96   set_operands(NULL);
  97 
  98   release_C_heap_structures();
  99 
 100   // free tag array
 101   MetadataFactory::free_array<u1>(loader_data, tags());
 102   set_tags(NULL);
 103 }
 104 
 105 void ConstantPool::release_C_heap_structures() {
 106   // walk constant pool and decrement symbol reference counts
 107   unreference_symbols();
 108 }
 109 
 110 objArrayOop ConstantPool::resolved_references() const {
 111   return (objArrayOop)JNIHandles::resolve(_cache->resolved_references());
 112 }
 113 
 114 // Create resolved_references array and mapping array for original cp indexes
 115 // The ldc bytecode was rewritten to have the resolved reference array index so need a way
 116 // to map it back for resolving and some unlikely miscellaneous uses.
 117 // The objects created by invokedynamic are appended to this list.
 118 void ConstantPool::initialize_resolved_references(ClassLoaderData* loader_data,
 119                                                   const intStack& reference_map,
 120                                                   int constant_pool_map_length,
 121                                                   TRAPS) {
 122   // Initialized the resolved object cache.
 123   int map_length = reference_map.length();
 124   if (map_length > 0) {
 125     // Only need mapping back to constant pool entries.  The map isn't used for
 126     // invokedynamic resolved_reference entries.  For invokedynamic entries,
 127     // the constant pool cache index has the mapping back to both the constant
 128     // pool and to the resolved reference index.
 129     if (constant_pool_map_length > 0) {
 130       Array<u2>* om = MetadataFactory::new_array<u2>(loader_data, constant_pool_map_length, CHECK);
 131 
 132       for (int i = 0; i < constant_pool_map_length; i++) {
 133         int x = reference_map.at(i);
 134         assert(x == (int)(jushort) x, "klass index is too big");
 135         om->at_put(i, (jushort)x);
 136       }
 137       set_reference_map(om);
 138     }
 139 
 140     // Create Java array for holding resolved strings, methodHandles,
 141     // methodTypes, invokedynamic and invokehandle appendix objects, etc.
 142     objArrayOop stom = oopFactory::new_objArray(SystemDictionary::Object_klass(), map_length, CHECK);
 143     Handle refs_handle (THREAD, (oop)stom);  // must handleize.
 144     set_resolved_references(loader_data->add_handle(refs_handle));
 145   }
 146 }
 147 
 148 void ConstantPool::allocate_resolved_klasses(ClassLoaderData* loader_data, int num_klasses, TRAPS) {
 149   // A ConstantPool can't possibly have 0xffff valid class entries,
 150   // because entry #0 must be CONSTANT_Invalid, and each class entry must refer to a UTF8
 151   // entry for the class's name. So at most we will have 0xfffe class entries.
 152   // This allows us to use 0xffff (ConstantPool::_temp_resolved_klass_index) to indicate
 153   // UnresolvedKlass entries that are temporarily created during class redefinition.
 154   assert(num_klasses < CPKlassSlot::_temp_resolved_klass_index, "sanity");
 155   assert(resolved_klasses() == NULL, "sanity");
 156   Array<Klass*>* rk = MetadataFactory::new_writeable_array<Klass*>(loader_data, num_klasses, CHECK);
 157   set_resolved_klasses(rk);
 158 }
 159 
 160 void ConstantPool::initialize_unresolved_klasses(ClassLoaderData* loader_data, TRAPS) {
 161   int len = length();
 162   int num_klasses = 0;
 163   for (int i = 1; i <len; i++) {
 164     switch (tag_at(i).value()) {
 165     case JVM_CONSTANT_ClassIndex:
 166       {
 167         const int class_index = klass_index_at(i);
 168         unresolved_klass_at_put(i, class_index, num_klasses++);
 169       }
 170       break;
 171 #ifndef PRODUCT
 172     case JVM_CONSTANT_Class:
 173     case JVM_CONSTANT_UnresolvedClass:
 174     case JVM_CONSTANT_UnresolvedClassInError:
 175       // All of these should have been reverted back to ClassIndex before calling
 176       // this function.
 177       ShouldNotReachHere();
 178 #endif
 179     }
 180   }
 181   allocate_resolved_klasses(loader_data, num_klasses, THREAD);
 182 }
 183 
 184 // Anonymous class support:
 185 void ConstantPool::klass_at_put(int class_index, int name_index, int resolved_klass_index, Klass* k, Symbol* name) {
 186   assert(is_within_bounds(class_index), "index out of bounds");
 187   assert(is_within_bounds(name_index), "index out of bounds");
 188   assert((resolved_klass_index & 0xffff0000) == 0, "must be");
 189   *int_at_addr(class_index) =
 190     build_int_from_shorts((jushort)resolved_klass_index, (jushort)name_index);
 191 
 192   symbol_at_put(name_index, name);
 193   name->increment_refcount();
 194   Klass** adr = resolved_klasses()->adr_at(resolved_klass_index);
 195   OrderAccess::release_store_ptr((Klass* volatile *)adr, k);
 196 
 197   // The interpreter assumes when the tag is stored, the klass is resolved
 198   // and the Klass* non-NULL, so we need hardware store ordering here.
 199   if (k != NULL) {
 200     release_tag_at_put(class_index, JVM_CONSTANT_Class);
 201   } else {
 202     release_tag_at_put(class_index, JVM_CONSTANT_UnresolvedClass);
 203   }
 204 }
 205 
 206 // Anonymous class support:
 207 void ConstantPool::klass_at_put(int class_index, Klass* k) {
 208   CPKlassSlot kslot = klass_slot_at(class_index);
 209   int resolved_klass_index = kslot.resolved_klass_index();
 210   int name_index = kslot.name_index();
 211   Symbol* name = symbol_at(name_index);
 212   guarantee(name == k->name(), "Invalid class name for anonymous");
 213 
 214   Klass** adr = resolved_klasses()->adr_at(resolved_klass_index);
 215   OrderAccess::release_store_ptr((Klass* volatile *)adr, k);
 216 }
 217 
 218 // CDS support. Create a new resolved_references array.
 219 void ConstantPool::restore_unshareable_info(TRAPS) {
 220   assert(is_constantPool(), "ensure C++ vtable is restored");
 221   assert(on_stack(), "should always be set for shared constant pools");
 222   assert(is_shared(), "should always be set for shared constant pools");
 223 
 224   // Only create the new resolved references array if it hasn't been attempted before
 225   if (resolved_references() != NULL) return;
 226 
 227   // restore the C++ vtable from the shared archive
 228   restore_vtable();
 229 
 230   if (SystemDictionary::Object_klass_loaded()) {
 231     // Recreate the object array and add to ClassLoaderData.
 232     int map_length = resolved_reference_length();
 233     if (map_length > 0) {
 234       objArrayOop stom = oopFactory::new_objArray(SystemDictionary::Object_klass(), map_length, CHECK);
 235       Handle refs_handle (THREAD, (oop)stom);  // must handleize.
 236 
 237       ClassLoaderData* loader_data = pool_holder()->class_loader_data();
 238       set_resolved_references(loader_data->add_handle(refs_handle));
 239     }
 240   }
 241 }
 242 
 243 void ConstantPool::remove_unshareable_info() {
 244   // Resolved references are not in the shared archive.
 245   // Save the length for restoration.  It is not necessarily the same length
 246   // as reference_map.length() if invokedynamic is saved.
 247   set_resolved_reference_length(
 248     resolved_references() != NULL ? resolved_references()->length() : 0);
 249   set_resolved_references(NULL);
 250 
 251   // Shared ConstantPools are in the RO region, so the _flags cannot be modified.
 252   // The _on_stack flag is used to prevent ConstantPools from deallocation during
 253   // class redefinition. Since shared ConstantPools cannot be deallocated anyway,
 254   // we always set _on_stack to true to avoid having to change _flags during runtime.
 255   _flags |= (_on_stack | _is_shared);
 256 }
 257 
 258 int ConstantPool::cp_to_object_index(int cp_index) {
 259   // this is harder don't do this so much.
 260   int i = reference_map()->find(cp_index);
 261   // We might not find the index for jsr292 call.
 262   return (i < 0) ? _no_index_sentinel : i;
 263 }
 264 
 265 void ConstantPool::string_at_put(int which, int obj_index, oop str) {
 266   resolved_references()->obj_at_put(obj_index, str);
 267 }
 268 
 269 void ConstantPool::trace_class_resolution(const constantPoolHandle& this_cp, Klass* k) {
 270   ResourceMark rm;
 271   int line_number = -1;
 272   const char * source_file = NULL;
 273   if (JavaThread::current()->has_last_Java_frame()) {
 274     // try to identify the method which called this function.
 275     vframeStream vfst(JavaThread::current());


 285     // only print something if the classes are different
 286     if (source_file != NULL) {
 287       log_debug(class, resolve)("%s %s %s:%d",
 288                  this_cp->pool_holder()->external_name(),
 289                  k->external_name(), source_file, line_number);
 290     } else {
 291       log_debug(class, resolve)("%s %s",
 292                  this_cp->pool_holder()->external_name(),
 293                  k->external_name());
 294     }
 295   }
 296 }
 297 
 298 Klass* ConstantPool::klass_at_impl(const constantPoolHandle& this_cp, int which,
 299                                    bool save_resolution_error, TRAPS) {
 300   assert(THREAD->is_Java_thread(), "must be a Java thread");
 301 
 302   // A resolved constantPool entry will contain a Klass*, otherwise a Symbol*.
 303   // It is not safe to rely on the tag bit's here, since we don't have a lock, and
 304   // the entry and tag is not updated atomicly.
 305   CPKlassSlot kslot = this_cp->klass_slot_at(which);
 306   int resolved_klass_index = kslot.resolved_klass_index();
 307   int name_index = kslot.name_index();
 308   assert(this_cp->tag_at(name_index).is_symbol(), "sanity");
 309 
 310   Klass* klass = this_cp->resolved_klasses()->at(resolved_klass_index);
 311   if (klass != NULL) {
 312     return klass;
 313   }
 314 
 315   // This tag doesn't change back to unresolved class unless at a safepoint.
 316   if (this_cp->tag_at(which).is_unresolved_klass_in_error()) {
 317     // The original attempt to resolve this constant pool entry failed so find the
 318     // class of the original error and throw another error of the same class
 319     // (JVMS 5.4.3).
 320     // If there is a detail message, pass that detail message to the error.
 321     // The JVMS does not strictly require us to duplicate the same detail message,
 322     // or any internal exception fields such as cause or stacktrace.  But since the
 323     // detail message is often a class name or other literal string, we will repeat it
 324     // if we can find it in the symbol table.
 325     throw_resolution_error(this_cp, which, CHECK_0);
 326     ShouldNotReachHere();
 327   }
 328 
 329   Handle mirror_handle;
 330   Symbol* name = this_cp->symbol_at(name_index);
 331   Handle loader (THREAD, this_cp->pool_holder()->class_loader());
 332   Handle protection_domain (THREAD, this_cp->pool_holder()->protection_domain());
 333   Klass* k = SystemDictionary::resolve_or_fail(name, loader, protection_domain, true, THREAD);
 334   if (!HAS_PENDING_EXCEPTION) {
 335     // preserve the resolved klass from unloading
 336     mirror_handle = Handle(THREAD, k->java_mirror());
 337     // Do access check for klasses
 338     verify_constant_pool_resolve(this_cp, k, THREAD);
 339   }
 340 
 341   // Failed to resolve class. We must record the errors so that subsequent attempts
 342   // to resolve this constant pool entry fail with the same error (JVMS 5.4.3).
 343   if (HAS_PENDING_EXCEPTION) {
 344     if (save_resolution_error) {
 345       save_and_throw_exception(this_cp, which, constantTag(JVM_CONSTANT_UnresolvedClass), CHECK_NULL);
 346       // If CHECK_NULL above doesn't return the exception, that means that
 347       // some other thread has beaten us and has resolved the class.
 348       // To preserve old behavior, we return the resolved class.
 349       klass = this_cp->resolved_klasses()->at(resolved_klass_index);
 350       assert(klass != NULL, "must be resolved if exception was cleared");
 351       return klass;

 352     } else {
 353       return NULL;  // return the pending exception
 354     }
 355   }
 356 
 357   // Make this class loader depend upon the class loader owning the class reference
 358   ClassLoaderData* this_key = this_cp->pool_holder()->class_loader_data();
 359   this_key->record_dependency(k, CHECK_NULL); // Can throw OOM
 360 
 361   // logging for class+resolve.
 362   if (log_is_enabled(Debug, class, resolve)){
 363     trace_class_resolution(this_cp, k);
 364   }
 365   Klass** adr = this_cp->resolved_klasses()->adr_at(resolved_klass_index);
 366   OrderAccess::release_store_ptr((Klass* volatile *)adr, k);
 367   // The interpreter assumes when the tag is stored, the klass is resolved
 368   // and the Klass* stored in _resolved_klasses is non-NULL, so we need
 369   // hardware store ordering here.
 370   this_cp->release_tag_at_put(which, JVM_CONSTANT_Class);
 371   return k;
 372 }
 373 
 374 
 375 // Does not update ConstantPool* - to avoid any exception throwing. Used
 376 // by compiler and exception handling.  Also used to avoid classloads for
 377 // instanceof operations. Returns NULL if the class has not been loaded or
 378 // if the verification of constant pool failed
 379 Klass* ConstantPool::klass_at_if_loaded(const constantPoolHandle& this_cp, int which) {
 380   CPKlassSlot kslot = this_cp->klass_slot_at(which);
 381   int resolved_klass_index = kslot.resolved_klass_index();
 382   int name_index = kslot.name_index();
 383   assert(this_cp->tag_at(name_index).is_symbol(), "sanity");
 384 
 385   Klass* k = this_cp->resolved_klasses()->at(resolved_klass_index);
 386   if (k != NULL) {
 387     return k;
 388   } else {

 389     Thread *thread = Thread::current();
 390     Symbol* name = this_cp->symbol_at(name_index);
 391     oop loader = this_cp->pool_holder()->class_loader();
 392     oop protection_domain = this_cp->pool_holder()->protection_domain();
 393     Handle h_prot (thread, protection_domain);
 394     Handle h_loader (thread, loader);
 395     Klass* k = SystemDictionary::find(name, h_loader, h_prot, thread);
 396 
 397     if (k != NULL) {
 398       // Make sure that resolving is legal
 399       EXCEPTION_MARK;
 400       // return NULL if verification fails
 401       verify_constant_pool_resolve(this_cp, k, THREAD);
 402       if (HAS_PENDING_EXCEPTION) {
 403         CLEAR_PENDING_EXCEPTION;
 404         return NULL;
 405       }
 406       return k;
 407     } else {
 408       return k;
 409     }
 410   }


 548   }
 549 }
 550 
 551 
 552 int ConstantPool::name_ref_index_at(int which_nt) {
 553   jint ref_index = name_and_type_at(which_nt);
 554   return extract_low_short_from_int(ref_index);
 555 }
 556 
 557 
 558 int ConstantPool::signature_ref_index_at(int which_nt) {
 559   jint ref_index = name_and_type_at(which_nt);
 560   return extract_high_short_from_int(ref_index);
 561 }
 562 
 563 
 564 Klass* ConstantPool::klass_ref_at(int which, TRAPS) {
 565   return klass_at(klass_ref_index_at(which), THREAD);
 566 }
 567 

 568 Symbol* ConstantPool::klass_name_at(int which) const {
 569   return symbol_at(klass_slot_at(which).name_index());













 570 }
 571 
 572 Symbol* ConstantPool::klass_ref_at_noresolve(int which) {
 573   jint ref_index = klass_ref_index_at(which);
 574   return klass_at_noresolve(ref_index);
 575 }
 576 
 577 Symbol* ConstantPool::uncached_klass_ref_at_noresolve(int which) {
 578   jint ref_index = uncached_klass_ref_index_at(which);
 579   return klass_at_noresolve(ref_index);
 580 }
 581 
 582 char* ConstantPool::string_at_noresolve(int which) {
 583   return unresolved_string_at(which)->as_C_string();
 584 }
 585 
 586 BasicType ConstantPool::basic_type_for_signature_at(int which) const {
 587   return FieldType::basic_type(symbol_at(which));
 588 }
 589 


 900   // If the string has already been interned, this entry will be non-null
 901   oop str = this_cp->resolved_references()->obj_at(obj_index);
 902   if (str != NULL) return str;
 903   Symbol* sym = this_cp->unresolved_string_at(which);
 904   str = StringTable::intern(sym, CHECK_(NULL));
 905   this_cp->string_at_put(which, obj_index, str);
 906   assert(java_lang_String::is_instance(str), "must be string");
 907   return str;
 908 }
 909 
 910 
 911 bool ConstantPool::klass_name_at_matches(const InstanceKlass* k, int which) {
 912   // Names are interned, so we can compare Symbol*s directly
 913   Symbol* cp_name = klass_name_at(which);
 914   return (cp_name == k->name());
 915 }
 916 
 917 
 918 // Iterate over symbols and decrement ones which are Symbol*s
 919 // This is done during GC.
 920 // Only decrement the UTF8 symbols. Strings point to
 921 // these symbols but didn't increment the reference count.
 922 void ConstantPool::unreference_symbols() {
 923   for (int index = 1; index < length(); index++) { // Index 0 is unused
 924     constantTag tag = tag_at(index);
 925     if (tag.is_symbol()) {
 926       symbol_at(index)->decrement_refcount();
 927     }
 928   }
 929 }
 930 
 931 
 932 // Compare this constant pool's entry at index1 to the constant pool
 933 // cp2's entry at index2.
 934 bool ConstantPool::compare_entry_to(int index1, const constantPoolHandle& cp2,
 935        int index2, TRAPS) {
 936 
 937   // The error tags are equivalent to non-error tags when comparing
 938   jbyte t1 = tag_at(index1).non_error_value();
 939   jbyte t2 = cp2->tag_at(index2).non_error_value();
 940 


1281     default:
1282       // all others take one constant pool entry
1283       src_i++;
1284       dest_i++;
1285       break;
1286     }
1287   }
1288   copy_operands(from_cp, to_cp, CHECK);
1289 
1290 } // end copy_cp_to_impl()
1291 
1292 
1293 // Copy this constant pool's entry at from_i to the constant pool
1294 // to_cp's entry at to_i.
1295 void ConstantPool::copy_entry_to(const constantPoolHandle& from_cp, int from_i,
1296                                         const constantPoolHandle& to_cp, int to_i,
1297                                         TRAPS) {
1298 
1299   int tag = from_cp->tag_at(from_i).value();
1300   switch (tag) {






1301   case JVM_CONSTANT_ClassIndex:
1302   {
1303     jint ki = from_cp->klass_index_at(from_i);
1304     to_cp->klass_index_at_put(to_i, ki);
1305   } break;
1306 
1307   case JVM_CONSTANT_Double:
1308   {
1309     jdouble d = from_cp->double_at(from_i);
1310     to_cp->double_at_put(to_i, d);
1311     // double takes two constant pool entries so init second entry's tag
1312     to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
1313   } break;
1314 
1315   case JVM_CONSTANT_Fieldref:
1316   {
1317     int class_index = from_cp->uncached_klass_ref_index_at(from_i);
1318     int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
1319     to_cp->field_at_put(to_i, class_index, name_and_type_index);
1320   } break;


1349   case JVM_CONSTANT_Methodref:
1350   {
1351     int class_index = from_cp->uncached_klass_ref_index_at(from_i);
1352     int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
1353     to_cp->method_at_put(to_i, class_index, name_and_type_index);
1354   } break;
1355 
1356   case JVM_CONSTANT_NameAndType:
1357   {
1358     int name_ref_index = from_cp->name_ref_index_at(from_i);
1359     int signature_ref_index = from_cp->signature_ref_index_at(from_i);
1360     to_cp->name_and_type_at_put(to_i, name_ref_index, signature_ref_index);
1361   } break;
1362 
1363   case JVM_CONSTANT_StringIndex:
1364   {
1365     jint si = from_cp->string_index_at(from_i);
1366     to_cp->string_index_at_put(to_i, si);
1367   } break;
1368 
1369   case JVM_CONSTANT_Class:
1370   case JVM_CONSTANT_UnresolvedClass:
1371   case JVM_CONSTANT_UnresolvedClassInError:
1372   {
1373     // Revert to JVM_CONSTANT_ClassIndex
1374     int name_index = from_cp->klass_slot_at(from_i).name_index();
1375     assert(from_cp->tag_at(name_index).is_symbol(), "sanity");
1376     to_cp->klass_index_at_put(to_i, name_index);





1377   } break;
1378 
1379   case JVM_CONSTANT_String:
1380   {
1381     Symbol* s = from_cp->unresolved_string_at(from_i);
1382     to_cp->unresolved_string_at_put(to_i, s);
1383   } break;
1384 
1385   case JVM_CONSTANT_Utf8:
1386   {
1387     Symbol* s = from_cp->symbol_at(from_i);
1388     // Need to increase refcount, the old one will be thrown away and deferenced
1389     s->increment_refcount();
1390     to_cp->symbol_at_put(to_i, s);
1391   } break;
1392 
1393   case JVM_CONSTANT_MethodType:
1394   case JVM_CONSTANT_MethodTypeInError:
1395   {
1396     jint k = from_cp->method_type_index_at(from_i);


1408   case JVM_CONSTANT_InvokeDynamic:
1409   {
1410     int k1 = from_cp->invoke_dynamic_bootstrap_specifier_index(from_i);
1411     int k2 = from_cp->invoke_dynamic_name_and_type_ref_index_at(from_i);
1412     k1 += operand_array_length(to_cp->operands());  // to_cp might already have operands
1413     to_cp->invoke_dynamic_at_put(to_i, k1, k2);
1414   } break;
1415 
1416   // Invalid is used as the tag for the second constant pool entry
1417   // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
1418   // not be seen by itself.
1419   case JVM_CONSTANT_Invalid: // fall through
1420 
1421   default:
1422   {
1423     ShouldNotReachHere();
1424   } break;
1425   }
1426 } // end copy_entry_to()
1427 

1428 // Search constant pool search_cp for an entry that matches this
1429 // constant pool's entry at pattern_i. Returns the index of a
1430 // matching entry or zero (0) if there is no matching entry.
1431 int ConstantPool::find_matching_entry(int pattern_i,
1432       const constantPoolHandle& search_cp, TRAPS) {
1433 
1434   // index zero (0) is not used
1435   for (int i = 1; i < search_cp->length(); i++) {
1436     bool found = compare_entry_to(pattern_i, search_cp, i, CHECK_0);
1437     if (found) {
1438       return i;
1439     }
1440   }
1441 
1442   return 0;  // entry not found; return unused index zero (0)
1443 } // end find_matching_entry()
1444 
1445 
1446 // Compare this constant pool's bootstrap specifier at idx1 to the constant pool
1447 // cp2's bootstrap specifier at idx2.


1863       }
1864     }
1865     DBG(printf("\n"));
1866     bytes += ent_size;
1867     size  += ent_size;
1868   }
1869   assert(size == cpool_size, "Size mismatch");
1870 
1871   // Keep temorarily for debugging until it's stable.
1872   DBG(print_cpool_bytes(cnt, start_bytes));
1873   return (int)(bytes - start_bytes);
1874 } /* end copy_cpool_bytes */
1875 
1876 #undef DBG
1877 
1878 
1879 void ConstantPool::set_on_stack(const bool value) {
1880   if (value) {
1881     // Only record if it's not already set.
1882     if (!on_stack()) {
1883       assert(!is_shared(), "should always be set for shared constant pools");
1884       _flags |= _on_stack;
1885       MetadataOnStackMark::record(this);
1886     }
1887   } else {
1888     // Clearing is done single-threadedly.
1889     if (!is_shared()) {
1890       _flags &= ~_on_stack;
1891     }
1892   }
1893 }
1894 
1895 // JSR 292 support for patching constant pool oops after the class is linked and
1896 // the oop array for resolved references are created.
1897 // We can't do this during classfile parsing, which is how the other indexes are
1898 // patched.  The other patches are applied early for some error checking
1899 // so only defer the pseudo_strings.
1900 void ConstantPool::patch_resolved_references(GrowableArray<Handle>* cp_patches) {
1901   for (int index = 1; index < cp_patches->length(); index++) { // Index 0 is unused
1902     Handle patch = cp_patches->at(index);
1903     if (patch.not_null()) {
1904       assert (tag_at(index).is_string(), "should only be string left");
1905       // Patching a string means pre-resolving it.
1906       // The spelling in the constant pool is ignored.
1907       // The constant reference may be any object whatever.
1908       // If it is not a real interned string, the constant is referred
1909       // to as a "pseudo-string", and must be presented to the CP
1910       // explicitly, because it may require scavenging.
1911       int obj_index = cp_to_object_index(index);
1912       pseudo_string_at_put(index, obj_index, patch());


1947 #endif
1948 
1949 
1950 // Printing
1951 
1952 void ConstantPool::print_on(outputStream* st) const {
1953   assert(is_constantPool(), "must be constantPool");
1954   st->print_cr("%s", internal_name());
1955   if (flags() != 0) {
1956     st->print(" - flags: 0x%x", flags());
1957     if (has_preresolution()) st->print(" has_preresolution");
1958     if (on_stack()) st->print(" on_stack");
1959     st->cr();
1960   }
1961   if (pool_holder() != NULL) {
1962     st->print_cr(" - holder: " INTPTR_FORMAT, p2i(pool_holder()));
1963   }
1964   st->print_cr(" - cache: " INTPTR_FORMAT, p2i(cache()));
1965   st->print_cr(" - resolved_references: " INTPTR_FORMAT, p2i(resolved_references()));
1966   st->print_cr(" - reference_map: " INTPTR_FORMAT, p2i(reference_map()));
1967   st->print_cr(" - resolved_klasses: " INTPTR_FORMAT, p2i(resolved_klasses()));
1968 
1969   for (int index = 1; index < length(); index++) {      // Index 0 is unused
1970     ((ConstantPool*)this)->print_entry_on(index, st);
1971     switch (tag_at(index).value()) {
1972       case JVM_CONSTANT_Long :
1973       case JVM_CONSTANT_Double :
1974         index++;   // Skip entry following eigth-byte constant
1975     }
1976 
1977   }
1978   st->cr();
1979 }
1980 
1981 // Print one constant pool entry
1982 void ConstantPool::print_entry_on(const int index, outputStream* st) {
1983   EXCEPTION_MARK;
1984   st->print(" - %3d : ", index);
1985   tag_at(index).print_on(st);
1986   st->print(" : ");
1987   switch (tag_at(index).value()) {


2009       break;
2010     case JVM_CONSTANT_Integer :
2011       st->print("%d", int_at(index));
2012       break;
2013     case JVM_CONSTANT_Float :
2014       st->print("%f", float_at(index));
2015       break;
2016     case JVM_CONSTANT_Long :
2017       st->print_jlong(long_at(index));
2018       break;
2019     case JVM_CONSTANT_Double :
2020       st->print("%lf", double_at(index));
2021       break;
2022     case JVM_CONSTANT_NameAndType :
2023       st->print("name_index=%d", name_ref_index_at(index));
2024       st->print(" signature_index=%d", signature_ref_index_at(index));
2025       break;
2026     case JVM_CONSTANT_Utf8 :
2027       symbol_at(index)->print_value_on(st);
2028       break;
2029     case JVM_CONSTANT_ClassIndex: {
2030         int name_index = *int_at_addr(index);
2031         st->print("klass_index=%d ", name_index);
2032         symbol_at(name_index)->print_value_on(st);
2033       }
2034       break;
2035     case JVM_CONSTANT_UnresolvedClass :               // fall-through
2036     case JVM_CONSTANT_UnresolvedClassInError: {
2037         CPKlassSlot kslot = klass_slot_at(index);
2038         int resolved_klass_index = kslot.resolved_klass_index();
2039         int name_index = kslot.name_index();
2040         assert(tag_at(name_index).is_symbol(), "sanity");
2041 
2042         Klass* klass = resolved_klasses()->at(resolved_klass_index);
2043         if (klass != NULL) {
2044           klass->print_value_on(st);
2045         } else {
2046           symbol_at(name_index)->print_value_on(st);
2047         }
2048       }
2049       break;
2050     case JVM_CONSTANT_MethodHandle :
2051     case JVM_CONSTANT_MethodHandleInError :
2052       st->print("ref_kind=%d", method_handle_ref_kind_at(index));
2053       st->print(" ref_index=%d", method_handle_index_at(index));
2054       break;
2055     case JVM_CONSTANT_MethodType :
2056     case JVM_CONSTANT_MethodTypeInError :
2057       st->print("signature_index=%d", method_type_index_at(index));
2058       break;
2059     case JVM_CONSTANT_InvokeDynamic :
2060       {
2061         st->print("bootstrap_method_index=%d", invoke_dynamic_bootstrap_method_ref_index_at(index));
2062         st->print(" name_and_type_index=%d", invoke_dynamic_name_and_type_ref_index_at(index));
2063         int argc = invoke_dynamic_argument_count_at(index);
2064         if (argc > 0) {
2065           for (int arg_i = 0; arg_i < argc; arg_i++) {
2066             int arg = invoke_dynamic_argument_index_at(index, arg_i);


2098 // Size Statistics
2099 void ConstantPool::collect_statistics(KlassSizeStats *sz) const {
2100   sz->_cp_all_bytes += (sz->_cp_bytes          = sz->count(this));
2101   sz->_cp_all_bytes += (sz->_cp_tags_bytes     = sz->count_array(tags()));
2102   sz->_cp_all_bytes += (sz->_cp_cache_bytes    = sz->count(cache()));
2103   sz->_cp_all_bytes += (sz->_cp_operands_bytes = sz->count_array(operands()));
2104   sz->_cp_all_bytes += (sz->_cp_refmap_bytes   = sz->count_array(reference_map()));
2105 
2106   sz->_ro_bytes += sz->_cp_operands_bytes + sz->_cp_tags_bytes +
2107                    sz->_cp_refmap_bytes;
2108   sz->_rw_bytes += sz->_cp_bytes + sz->_cp_cache_bytes;
2109 }
2110 #endif // INCLUDE_SERVICES
2111 
2112 // Verification
2113 
2114 void ConstantPool::verify_on(outputStream* st) {
2115   guarantee(is_constantPool(), "object must be constant pool");
2116   for (int i = 0; i< length();  i++) {
2117     constantTag tag = tag_at(i);
2118     if (tag.is_klass() || tag.is_unresolved_klass()) {
2119       guarantee(klass_name_at(i)->refcount() != 0, "should have nonzero reference count");







2120     } else if (tag.is_symbol()) {
2121       CPSlot entry = slot_at(i);
2122       guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count");
2123     } else if (tag.is_string()) {
2124       CPSlot entry = slot_at(i);
2125       guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count");
2126     }
2127   }
2128   if (cache() != NULL) {
2129     // Note: cache() can be NULL before a class is completely setup or
2130     // in temporary constant pools used during constant pool merging
2131     guarantee(cache()->is_constantPoolCache(), "should be constant pool cache");
2132   }
2133   if (pool_holder() != NULL) {
2134     // Note: pool_holder() can be NULL in temporary constant pools
2135     // used during constant pool merging
2136     guarantee(pool_holder()->is_klass(),    "should be klass");
2137   }
2138 }
2139 
2140 
2141 void SymbolHashMap::add_entry(Symbol* sym, u2 value) {
2142   char *str = sym->as_utf8();
2143   unsigned int hash = compute_hash(str, sym->utf8_length());
2144   unsigned int index = hash % table_size();


< prev index next >