183 // entry for the class's name. So at most we will have 0xfffe class entries.
184 // This allows us to use 0xffff (ConstantPool::_temp_resolved_klass_index) to indicate
185 // UnresolvedKlass entries that are temporarily created during class redefinition.
186 assert(num_klasses < CPKlassSlot::_temp_resolved_klass_index, "sanity");
187 assert(resolved_klasses() == NULL, "sanity");
188 Array<Klass*>* rk = MetadataFactory::new_array<Klass*>(loader_data, num_klasses, CHECK);
189 set_resolved_klasses(rk);
190 }
191
192 void ConstantPool::initialize_unresolved_klasses(ClassLoaderData* loader_data, TRAPS) {
193 int len = length();
194 int num_klasses = 0;
195 for (int i = 1; i <len; i++) {
196 switch (tag_at(i).value()) {
197 case JVM_CONSTANT_ClassIndex:
198 {
199 const int class_index = klass_index_at(i);
200 unresolved_klass_at_put(i, class_index, num_klasses++);
201 }
202 break;
203 case JVM_CONSTANT_ValueIndex:
204 {
205 const int class_index = value_type_index_at(i);
206 unresolved_value_type_at_put(i, class_index, num_klasses++);
207 }
208 break;
209 #ifndef PRODUCT
210 case JVM_CONSTANT_Class:
211 case JVM_CONSTANT_UnresolvedClass:
212 case JVM_CONSTANT_UnresolvedClassInError:
213 case JVM_CONSTANT_Value:
214 case JVM_CONSTANT_UnresolvedValue:
215 case JVM_CONSTANT_UnresolvedValueInError:
216 // All of these should have been reverted back to Unresolved before calling
217 // this function.
218 ShouldNotReachHere();
219 #endif
220 }
221 }
222 allocate_resolved_klasses(loader_data, num_klasses, THREAD);
223 }
224
225 // Anonymous class support:
226 void ConstantPool::klass_at_put(int class_index, int name_index, int resolved_klass_index, Klass* k, Symbol* name) {
227 assert(is_within_bounds(class_index), "index out of bounds");
228 assert(is_within_bounds(name_index), "index out of bounds");
229 assert((resolved_klass_index & 0xffff0000) == 0, "must be");
230 *int_at_addr(class_index) =
231 build_int_from_shorts((jushort)resolved_klass_index, (jushort)name_index);
232
233 symbol_at_put(name_index, name);
234 name->increment_refcount();
235 Klass** adr = resolved_klasses()->adr_at(resolved_klass_index);
236 OrderAccess::release_store(adr, k);
237
238 // The interpreter assumes when the tag is stored, the klass is resolved
239 // and the Klass* non-NULL, so we need hardware store ordering here.
240 if (k != NULL) {
241 release_tag_at_put(class_index, (k->is_value() ? (jbyte)JVM_CONSTANT_Value : JVM_CONSTANT_Class));
242 } else {
243 release_tag_at_put(class_index, (tag_at(class_index).is_value_type_or_reference() ?
244 JVM_CONSTANT_UnresolvedValue : JVM_CONSTANT_UnresolvedClass));
245 }
246 }
247
248 // Anonymous class support:
249 void ConstantPool::klass_at_put(int class_index, Klass* k) {
250 assert(k != NULL, "must be valid klass");
251 CPKlassSlot kslot = klass_slot_at(class_index);
252 int resolved_klass_index = kslot.resolved_klass_index();
253 Klass** adr = resolved_klasses()->adr_at(resolved_klass_index);
254 OrderAccess::release_store(adr, k);
255
256 // The interpreter assumes when the tag is stored, the klass is resolved
257 // and the Klass* non-NULL, so we need hardware store ordering here.
258 release_tag_at_put(class_index, (k->is_value() ? (jbyte)JVM_CONSTANT_Value : JVM_CONSTANT_Class));
259 }
260
261 #if INCLUDE_CDS_JAVA_HEAP
262 // Archive the resolved references
263 void ConstantPool::archive_resolved_references(Thread* THREAD) {
264 if (_cache == NULL) {
265 return; // nothing to do
266 }
267
268 InstanceKlass *ik = pool_holder();
269 if (!(ik->is_shared_boot_class() || ik->is_shared_platform_class() ||
270 ik->is_shared_app_class())) {
271 // Archiving resolved references for classes from non-builtin loaders
272 // is not yet supported.
273 set_resolved_references(NULL);
274 return;
275 }
276
277 objArrayOop rr = resolved_references();
278 Array<u2>* ref_map = reference_map();
437 log_debug(class, resolve)("%s %s",
438 this_cp->pool_holder()->external_name(),
439 k->external_name());
440 }
441 }
442 }
443
444 Klass* ConstantPool::klass_at_impl(const constantPoolHandle& this_cp, int which,
445 bool save_resolution_error, TRAPS) {
446 assert(THREAD->is_Java_thread(), "must be a Java thread");
447
448 // A resolved constantPool entry will contain a Klass*, otherwise a Symbol*.
449 // It is not safe to rely on the tag bit's here, since we don't have a lock, and
450 // the entry and tag is not updated atomicly.
451 CPKlassSlot kslot = this_cp->klass_slot_at(which);
452 int resolved_klass_index = kslot.resolved_klass_index();
453 int name_index = kslot.name_index();
454 assert(this_cp->tag_at(name_index).is_symbol(), "sanity");
455
456 Klass* klass = this_cp->resolved_klasses()->at(resolved_klass_index);
457
458 if (klass != NULL) {
459 return klass;
460 }
461
462 // This tag doesn't change back to unresolved class unless at a safepoint.
463 if (this_cp->tag_at(which).is_unresolved_klass_in_error() ||
464 this_cp->tag_at(which).is_unresolved_value_type_in_error()) {
465 // The original attempt to resolve this constant pool entry failed so find the
466 // class of the original error and throw another error of the same class
467 // (JVMS 5.4.3).
468 // If there is a detail message, pass that detail message to the error.
469 // The JVMS does not strictly require us to duplicate the same detail message,
470 // or any internal exception fields such as cause or stacktrace. But since the
471 // detail message is often a class name or other literal string, we will repeat it
472 // if we can find it in the symbol table.
473 throw_resolution_error(this_cp, which, CHECK_0);
474 ShouldNotReachHere();
475 }
476
477 Handle mirror_handle;
478 Symbol* name = this_cp->symbol_at(name_index);
479 Handle loader (THREAD, this_cp->pool_holder()->class_loader());
480 Handle protection_domain (THREAD, this_cp->pool_holder()->protection_domain());
481 Klass* k = SystemDictionary::resolve_or_fail(name, loader, protection_domain, true, THREAD);
482 if (!HAS_PENDING_EXCEPTION) {
483 // preserve the resolved klass from unloading
484 mirror_handle = Handle(THREAD, k->java_mirror());
485 // Do access check for klasses
486 verify_constant_pool_resolve(this_cp, k, THREAD);
487 }
488
489 // Failed to resolve class. We must record the errors so that subsequent attempts
490 // to resolve this constant pool entry fail with the same error (JVMS 5.4.3).
491 if (HAS_PENDING_EXCEPTION) {
492 if (save_resolution_error) {
493 bool is_value_type_tag = this_cp->tag_at(which).is_value_type_or_reference();
494 save_and_throw_exception(this_cp, which,
495 constantTag((is_value_type_tag ? JVM_CONSTANT_UnresolvedValue : JVM_CONSTANT_UnresolvedClass)),
496 CHECK_NULL);
497 // If CHECK_NULL above doesn't return the exception, that means that
498 // some other thread has beaten us and has resolved the class.
499 // To preserve old behavior, we return the resolved class.
500 klass = this_cp->resolved_klasses()->at(resolved_klass_index);
501 assert(klass != NULL, "must be resolved if exception was cleared");
502 return klass;
503 } else {
504 return NULL; // return the pending exception
505 }
506 }
507
508 // Make this class loader depend upon the class loader owning the class reference
509 ClassLoaderData* this_key = this_cp->pool_holder()->class_loader_data();
510 this_key->record_dependency(k, CHECK_NULL); // Can throw OOM
511
512 // logging for class+resolve.
513 if (log_is_enabled(Debug, class, resolve)){
514 trace_class_resolution(this_cp, k);
515 }
516 Klass** adr = this_cp->resolved_klasses()->adr_at(resolved_klass_index);
517 OrderAccess::release_store(adr, k);
518 // The interpreter assumes when the tag is stored, the klass is resolved
519 // and the Klass* stored in _resolved_klasses is non-NULL, so we need
520 // hardware store ordering here.
521 this_cp->release_tag_at_put(which, (k->is_value() ? (jbyte)JVM_CONSTANT_Value : JVM_CONSTANT_Class));
522 return k;
523 }
524
525
526 // Does not update ConstantPool* - to avoid any exception throwing. Used
527 // by compiler and exception handling. Also used to avoid classloads for
528 // instanceof operations. Returns NULL if the class has not been loaded or
529 // if the verification of constant pool failed
530 Klass* ConstantPool::klass_at_if_loaded(const constantPoolHandle& this_cp, int which) {
531 CPKlassSlot kslot = this_cp->klass_slot_at(which);
532 int resolved_klass_index = kslot.resolved_klass_index();
533 int name_index = kslot.name_index();
534 assert(this_cp->tag_at(name_index).is_symbol(), "sanity");
535
536 Klass* k = this_cp->resolved_klasses()->at(resolved_klass_index);
537 if (k != NULL) {
538 return k;
539 } else {
540 Thread *thread = Thread::current();
541 Symbol* name = this_cp->symbol_at(name_index);
739
740
741 void ConstantPool::resolve_string_constants_impl(const constantPoolHandle& this_cp, TRAPS) {
742 for (int index = 1; index < this_cp->length(); index++) { // Index 0 is unused
743 if (this_cp->tag_at(index).is_string()) {
744 this_cp->string_at(index, CHECK);
745 }
746 }
747 }
748
749 Symbol* ConstantPool::exception_message(const constantPoolHandle& this_cp, int which, constantTag tag, oop pending_exception) {
750 // Dig out the detailed message to reuse if possible
751 Symbol* message = java_lang_Throwable::detail_message(pending_exception);
752 if (message != NULL) {
753 return message;
754 }
755
756 // Return specific message for the tag
757 switch (tag.value()) {
758 case JVM_CONSTANT_UnresolvedClass:
759 case JVM_CONSTANT_UnresolvedValue:
760 // return the class name in the error message
761 message = this_cp->klass_name_at(which);
762 break;
763 case JVM_CONSTANT_MethodHandle:
764 // return the method handle name in the error message
765 message = this_cp->method_handle_name_ref_at(which);
766 break;
767 case JVM_CONSTANT_MethodType:
768 // return the method type signature in the error message
769 message = this_cp->method_type_signature_at(which);
770 break;
771 default:
772 ShouldNotReachHere();
773 }
774
775 return message;
776 }
777
778 void ConstantPool::throw_resolution_error(const constantPoolHandle& this_cp, int which, TRAPS) {
779 Symbol* message = NULL;
794
795 if (!PENDING_EXCEPTION->
796 is_a(SystemDictionary::LinkageError_klass())) {
797 // Just throw the exception and don't prevent these classes from
798 // being loaded due to virtual machine errors like StackOverflow
799 // and OutOfMemoryError, etc, or if the thread was hit by stop()
800 // Needs clarification to section 5.4.3 of the VM spec (see 6308271)
801 } else if (this_cp->tag_at(which).value() != error_tag) {
802 Symbol* message = exception_message(this_cp, which, tag, PENDING_EXCEPTION);
803 SystemDictionary::add_resolution_error(this_cp, which, error, message);
804 // CAS in the tag. If a thread beat us to registering this error that's fine.
805 // If another thread resolved the reference, this is a race condition. This
806 // thread may have had a security manager or something temporary.
807 // This doesn't deterministically get an error. So why do we save this?
808 // We save this because jvmti can add classes to the bootclass path after
809 // this error, so it needs to get the same error if the error is first.
810 jbyte old_tag = Atomic::cmpxchg((jbyte)error_tag,
811 (jbyte*)this_cp->tag_addr_at(which), (jbyte)tag.value());
812 if (old_tag != error_tag && old_tag != tag.value()) {
813 // MethodHandles and MethodType doesn't change to resolved version.
814 assert(this_cp->tag_at(which).is_klass() || this_cp->tag_at(which).is_value_type(), "Wrong tag value");
815 // Forget the exception and use the resolved class.
816 CLEAR_PENDING_EXCEPTION;
817 }
818 } else {
819 // some other thread put this in error state
820 throw_resolution_error(this_cp, which, CHECK);
821 }
822 }
823
824 BasicType ConstantPool::basic_type_for_constant_at(int which) {
825 constantTag tag = tag_at(which);
826 if (tag.is_dynamic_constant() ||
827 tag.is_dynamic_constant_in_error()) {
828 // have to look at the signature for this one
829 Symbol* constant_type = uncached_signature_ref_at(which);
830 return FieldType::basic_type(constant_type);
831 }
832 return tag.basic_type();
833 }
834
888 case JVM_CONSTANT_String:
889 case JVM_CONSTANT_Integer:
890 case JVM_CONSTANT_Float:
891 case JVM_CONSTANT_Long:
892 case JVM_CONSTANT_Double:
893 // these guys trigger OOM at worst
894 break;
895 default:
896 (*status_return) = false;
897 return NULL;
898 }
899 // from now on there is either success or an OOME
900 (*status_return) = true;
901 }
902
903 switch (tag.value()) {
904
905 case JVM_CONSTANT_UnresolvedClass:
906 case JVM_CONSTANT_UnresolvedClassInError:
907 case JVM_CONSTANT_Class:
908 case JVM_CONSTANT_UnresolvedValue:
909 case JVM_CONSTANT_UnresolvedValueInError:
910 case JVM_CONSTANT_Value:
911 {
912 assert(cache_index == _no_index_sentinel, "should not have been set");
913 Klass* resolved = klass_at_impl(this_cp, index, true, CHECK_NULL);
914 // ldc wants the java mirror.
915 result_oop = resolved->java_mirror();
916 break;
917 }
918
919 case JVM_CONSTANT_Dynamic:
920 {
921 Klass* current_klass = this_cp->pool_holder();
922 Symbol* constant_name = this_cp->uncached_name_ref_at(index);
923 Symbol* constant_type = this_cp->uncached_signature_ref_at(index);
924
925 // The initial step in resolving an unresolved symbolic reference to a
926 // dynamically-computed constant is to resolve the symbolic reference to a
927 // method handle which will be the bootstrap method for the dynamically-computed
928 // constant. If resolution of the java.lang.invoke.MethodHandle for the bootstrap
929 // method fails, then a MethodHandleInError is stored at the corresponding
930 // bootstrap method's CP index for the CONSTANT_MethodHandle_info. No need to
1297 // cp2's entry at index2.
1298 bool ConstantPool::compare_entry_to(int index1, const constantPoolHandle& cp2,
1299 int index2, TRAPS) {
1300
1301 // The error tags are equivalent to non-error tags when comparing
1302 jbyte t1 = tag_at(index1).non_error_value();
1303 jbyte t2 = cp2->tag_at(index2).non_error_value();
1304
1305 if (t1 != t2) {
1306 // Not the same entry type so there is nothing else to check. Note
1307 // that this style of checking will consider resolved/unresolved
1308 // class pairs as different.
1309 // From the ConstantPool* API point of view, this is correct
1310 // behavior. See VM_RedefineClasses::merge_constant_pools() to see how this
1311 // plays out in the context of ConstantPool* merging.
1312 return false;
1313 }
1314
1315 switch (t1) {
1316 case JVM_CONSTANT_Class:
1317 case JVM_CONSTANT_Value:
1318 {
1319 Klass* k1 = klass_at(index1, CHECK_false);
1320 Klass* k2 = cp2->klass_at(index2, CHECK_false);
1321 if (k1 == k2) {
1322 return true;
1323 }
1324 } break;
1325
1326 case JVM_CONSTANT_ClassIndex:
1327 {
1328 int recur1 = klass_index_at(index1);
1329 int recur2 = cp2->klass_index_at(index2);
1330 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
1331 if (match) {
1332 return true;
1333 }
1334 } break;
1335
1336 case JVM_CONSTANT_ValueIndex:
1337 {
1338 int recur1 = value_type_index_at(index1);
1339 int recur2 = cp2->value_type_index_at(index2);
1340 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
1341 if (match) {
1342 return true;
1343 }
1344 } break;
1345
1346 case JVM_CONSTANT_Double:
1347 {
1348 jdouble d1 = double_at(index1);
1349 jdouble d2 = cp2->double_at(index2);
1350 if (d1 == d2) {
1351 return true;
1352 }
1353 } break;
1354
1355 case JVM_CONSTANT_Fieldref:
1356 case JVM_CONSTANT_InterfaceMethodref:
1357 case JVM_CONSTANT_Methodref:
1358 {
1359 int recur1 = uncached_klass_ref_index_at(index1);
1360 int recur2 = cp2->uncached_klass_ref_index_at(index2);
1361 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
1362 if (match) {
1363 recur1 = uncached_name_and_type_ref_index_at(index1);
1364 recur2 = cp2->uncached_name_and_type_ref_index_at(index2);
1365 match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
1405 recur1 = signature_ref_index_at(index1);
1406 recur2 = cp2->signature_ref_index_at(index2);
1407 match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
1408 if (match) {
1409 return true;
1410 }
1411 }
1412 } break;
1413
1414 case JVM_CONSTANT_StringIndex:
1415 {
1416 int recur1 = string_index_at(index1);
1417 int recur2 = cp2->string_index_at(index2);
1418 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
1419 if (match) {
1420 return true;
1421 }
1422 } break;
1423
1424 case JVM_CONSTANT_UnresolvedClass:
1425 case JVM_CONSTANT_UnresolvedValue:
1426 {
1427 Symbol* k1 = klass_name_at(index1);
1428 Symbol* k2 = cp2->klass_name_at(index2);
1429 if (k1 == k2) {
1430 return true;
1431 }
1432 } break;
1433
1434 case JVM_CONSTANT_MethodType:
1435 {
1436 int k1 = method_type_index_at(index1);
1437 int k2 = cp2->method_type_index_at(index2);
1438 bool match = compare_entry_to(k1, cp2, k2, CHECK_false);
1439 if (match) {
1440 return true;
1441 }
1442 } break;
1443
1444 case JVM_CONSTANT_MethodHandle:
1445 {
1675 }
1676 copy_operands(from_cp, to_cp, CHECK);
1677
1678 } // end copy_cp_to_impl()
1679
1680
1681 // Copy this constant pool's entry at from_i to the constant pool
1682 // to_cp's entry at to_i.
1683 void ConstantPool::copy_entry_to(const constantPoolHandle& from_cp, int from_i,
1684 const constantPoolHandle& to_cp, int to_i,
1685 TRAPS) {
1686
1687 int tag = from_cp->tag_at(from_i).value();
1688 switch (tag) {
1689 case JVM_CONSTANT_ClassIndex:
1690 {
1691 jint ki = from_cp->klass_index_at(from_i);
1692 to_cp->klass_index_at_put(to_i, ki);
1693 } break;
1694
1695 case JVM_CONSTANT_ValueIndex:
1696 {
1697 jint ki = from_cp->klass_index_at(from_i);
1698 to_cp->klass_index_at_put(to_i, ki);
1699 } break;
1700
1701 case JVM_CONSTANT_Double:
1702 {
1703 jdouble d = from_cp->double_at(from_i);
1704 to_cp->double_at_put(to_i, d);
1705 // double takes two constant pool entries so init second entry's tag
1706 to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
1707 } break;
1708
1709 case JVM_CONSTANT_Fieldref:
1710 {
1711 int class_index = from_cp->uncached_klass_ref_index_at(from_i);
1712 int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
1713 to_cp->field_at_put(to_i, class_index, name_and_type_index);
1714 } break;
1715
1716 case JVM_CONSTANT_Float:
1717 {
1718 jfloat f = from_cp->float_at(from_i);
1719 to_cp->float_at_put(to_i, f);
1720 } break;
1753 int signature_ref_index = from_cp->signature_ref_index_at(from_i);
1754 to_cp->name_and_type_at_put(to_i, name_ref_index, signature_ref_index);
1755 } break;
1756
1757 case JVM_CONSTANT_StringIndex:
1758 {
1759 jint si = from_cp->string_index_at(from_i);
1760 to_cp->string_index_at_put(to_i, si);
1761 } break;
1762
1763 case JVM_CONSTANT_Class:
1764 case JVM_CONSTANT_UnresolvedClass:
1765 case JVM_CONSTANT_UnresolvedClassInError:
1766 {
1767 // Revert to JVM_CONSTANT_ClassIndex
1768 int name_index = from_cp->klass_slot_at(from_i).name_index();
1769 assert(from_cp->tag_at(name_index).is_symbol(), "sanity");
1770 to_cp->klass_index_at_put(to_i, name_index);
1771 } break;
1772
1773 case JVM_CONSTANT_Value:
1774 case JVM_CONSTANT_UnresolvedValue:
1775 case JVM_CONSTANT_UnresolvedValueInError:
1776 {
1777 // Revert to JVM_CONSTANT_ValueIndex
1778 int name_index = from_cp->klass_slot_at(from_i).name_index();
1779 assert(from_cp->tag_at(name_index).is_symbol(), "sanity");
1780 to_cp->value_type_index_at_put(to_i, name_index);
1781 } break;
1782
1783 case JVM_CONSTANT_String:
1784 {
1785 Symbol* s = from_cp->unresolved_string_at(from_i);
1786 to_cp->unresolved_string_at_put(to_i, s);
1787 } break;
1788
1789 case JVM_CONSTANT_Utf8:
1790 {
1791 Symbol* s = from_cp->symbol_at(from_i);
1792 // Need to increase refcount, the old one will be thrown away and deferenced
1793 s->increment_refcount();
1794 to_cp->symbol_at_put(to_i, s);
1795 } break;
1796
1797 case JVM_CONSTANT_MethodType:
1798 case JVM_CONSTANT_MethodTypeInError:
1799 {
1800 jint k = from_cp->method_type_index_at(from_i);
1801 to_cp->method_type_index_at_put(to_i, k);
1802 } break;
1887 int ConstantPool::find_matching_operand(int pattern_i,
1888 const constantPoolHandle& search_cp, int search_len, TRAPS) {
1889 for (int i = 0; i < search_len; i++) {
1890 bool found = compare_operand_to(pattern_i, search_cp, i, CHECK_(-1));
1891 if (found) {
1892 return i;
1893 }
1894 }
1895 return -1; // bootstrap specifier not found; return unused index (-1)
1896 } // end find_matching_operand()
1897
1898
1899 #ifndef PRODUCT
1900
1901 const char* ConstantPool::printable_name_at(int which) {
1902
1903 constantTag tag = tag_at(which);
1904
1905 if (tag.is_string()) {
1906 return string_at_noresolve(which);
1907 } else if (tag.is_klass() || tag.is_unresolved_klass() ||
1908 tag.is_value_type() || tag.is_unresolved_value_type()) {
1909 return klass_name_at(which)->as_C_string();
1910 } else if (tag.is_symbol()) {
1911 return symbol_at(which)->as_C_string();
1912 }
1913 return "";
1914 }
1915
1916 #endif // PRODUCT
1917
1918
1919 // JVMTI GetConstantPool support
1920
1921 // For debugging of constant pool
1922 const bool debug_cpool = false;
1923
1924 #define DBG(code) do { if (debug_cpool) { (code); } } while(0)
1925
1926 static void print_cpool_bytes(jint cnt, u1 *bytes) {
1927 const char* WARN_MSG = "Must not be such entry!";
1928 jint size = 0;
1970 case JVM_CONSTANT_Long: {
1971 u8 val = Bytes::get_Java_u8(bytes);
1972 printf("long " INT64_FORMAT, (int64_t) *(jlong *) &val);
1973 ent_size = 8;
1974 idx++; // Long takes two cpool slots
1975 break;
1976 }
1977 case JVM_CONSTANT_Double: {
1978 u8 val = Bytes::get_Java_u8(bytes);
1979 printf("double %5.3fd", *(jdouble *)&val);
1980 ent_size = 8;
1981 idx++; // Double takes two cpool slots
1982 break;
1983 }
1984 case JVM_CONSTANT_Class: {
1985 idx1 = Bytes::get_Java_u2(bytes);
1986 printf("class #%03d", idx1);
1987 ent_size = 2;
1988 break;
1989 }
1990 case JVM_CONSTANT_Value: {
1991 idx1 = Bytes::get_Java_u2(bytes);
1992 printf("class #%03d", idx1);
1993 ent_size = 2;
1994 break;
1995 }
1996 case JVM_CONSTANT_String: {
1997 idx1 = Bytes::get_Java_u2(bytes);
1998 printf("String #%03d", idx1);
1999 ent_size = 2;
2000 break;
2001 }
2002 case JVM_CONSTANT_Fieldref: {
2003 idx1 = Bytes::get_Java_u2(bytes);
2004 idx2 = Bytes::get_Java_u2(bytes+2);
2005 printf("Field #%03d, #%03d", (int) idx1, (int) idx2);
2006 ent_size = 4;
2007 break;
2008 }
2009 case JVM_CONSTANT_Methodref: {
2010 idx1 = Bytes::get_Java_u2(bytes);
2011 idx2 = Bytes::get_Java_u2(bytes+2);
2012 printf("Method #%03d, #%03d", idx1, idx2);
2013 ent_size = 4;
2014 break;
2015 }
2022 }
2023 case JVM_CONSTANT_NameAndType: {
2024 idx1 = Bytes::get_Java_u2(bytes);
2025 idx2 = Bytes::get_Java_u2(bytes+2);
2026 printf("NameAndType #%03d, #%03d", idx1, idx2);
2027 ent_size = 4;
2028 break;
2029 }
2030 case JVM_CONSTANT_ClassIndex: {
2031 printf("ClassIndex %s", WARN_MSG);
2032 break;
2033 }
2034 case JVM_CONSTANT_UnresolvedClass: {
2035 printf("UnresolvedClass: %s", WARN_MSG);
2036 break;
2037 }
2038 case JVM_CONSTANT_UnresolvedClassInError: {
2039 printf("UnresolvedClassInErr: %s", WARN_MSG);
2040 break;
2041 }
2042 case JVM_CONSTANT_ValueIndex: {
2043 printf("ValueIndex %s", WARN_MSG);
2044 break;
2045 }
2046 case JVM_CONSTANT_UnresolvedValue: {
2047 printf("UnresolvedValue: %s", WARN_MSG);
2048 break;
2049 }
2050 case JVM_CONSTANT_UnresolvedValueInError: {
2051 printf("UnresolvedValueInErr: %s", WARN_MSG);
2052 break;
2053 }
2054 case JVM_CONSTANT_StringIndex: {
2055 printf("StringIndex: %s", WARN_MSG);
2056 break;
2057 }
2058 }
2059 printf(";\n");
2060 bytes += ent_size;
2061 size += ent_size;
2062 }
2063 printf("Cpool size: %d\n", size);
2064 fflush(0);
2065 return;
2066 } /* end print_cpool_bytes */
2067
2068
2069 // Returns size of constant pool entry.
2070 jint ConstantPool::cpool_entry_size(jint idx) {
2071 switch(tag_at(idx).value()) {
2072 case JVM_CONSTANT_Invalid:
2073 case JVM_CONSTANT_Unicode:
2074 return 1;
2075
2076 case JVM_CONSTANT_Utf8:
2077 return 3 + symbol_at(idx)->utf8_length();
2078
2079 case JVM_CONSTANT_Class:
2080 case JVM_CONSTANT_String:
2081 case JVM_CONSTANT_ClassIndex:
2082 case JVM_CONSTANT_UnresolvedClass:
2083 case JVM_CONSTANT_UnresolvedClassInError:
2084 case JVM_CONSTANT_Value:
2085 case JVM_CONSTANT_ValueIndex:
2086 case JVM_CONSTANT_UnresolvedValue:
2087 case JVM_CONSTANT_UnresolvedValueInError:
2088 case JVM_CONSTANT_StringIndex:
2089 case JVM_CONSTANT_MethodType:
2090 case JVM_CONSTANT_MethodTypeInError:
2091 return 3;
2092
2093 case JVM_CONSTANT_MethodHandle:
2094 case JVM_CONSTANT_MethodHandleInError:
2095 return 4; //tag, ref_kind, ref_index
2096
2097 case JVM_CONSTANT_Integer:
2098 case JVM_CONSTANT_Float:
2099 case JVM_CONSTANT_Fieldref:
2100 case JVM_CONSTANT_Methodref:
2101 case JVM_CONSTANT_InterfaceMethodref:
2102 case JVM_CONSTANT_NameAndType:
2103 return 5;
2104
2105 case JVM_CONSTANT_Dynamic:
2106 case JVM_CONSTANT_DynamicInError:
2107 case JVM_CONSTANT_InvokeDynamic:
2126
2127 for (u2 idx = 1; idx < length(); idx++) {
2128 u2 tag = tag_at(idx).value();
2129 size += cpool_entry_size(idx);
2130
2131 switch(tag) {
2132 case JVM_CONSTANT_Utf8: {
2133 Symbol* sym = symbol_at(idx);
2134 symmap->add_entry(sym, idx);
2135 DBG(printf("adding symbol entry %s = %d\n", sym->as_utf8(), idx));
2136 break;
2137 }
2138 case JVM_CONSTANT_Class:
2139 case JVM_CONSTANT_UnresolvedClass:
2140 case JVM_CONSTANT_UnresolvedClassInError: {
2141 Symbol* sym = klass_name_at(idx);
2142 classmap->add_entry(sym, idx);
2143 DBG(printf("adding class entry %s = %d\n", sym->as_utf8(), idx));
2144 break;
2145 }
2146 case JVM_CONSTANT_Value:
2147 case JVM_CONSTANT_UnresolvedValue:
2148 case JVM_CONSTANT_UnresolvedValueInError: {
2149 Symbol* sym = klass_name_at(idx);
2150 classmap->add_entry(sym, idx);
2151 DBG(printf("adding value type entry %s = %d\n", sym->as_utf8(), idx));
2152 break;
2153 }
2154 case JVM_CONSTANT_Long:
2155 case JVM_CONSTANT_Double: {
2156 idx++; // Both Long and Double take two cpool slots
2157 break;
2158 }
2159 }
2160 }
2161 return size;
2162 } /* end hash_utf8_entries_to */
2163
2164
2165 // Copy cpool bytes.
2166 // Returns:
2167 // 0, in case of OutOfMemoryError
2168 // -1, in case of internal error
2169 // > 0, count of the raw cpool bytes that have been copied
2170 int ConstantPool::copy_cpool_bytes(int cpool_size,
2171 SymbolHashMap* tbl,
2172 unsigned char *bytes) {
2173 u2 idx1, idx2;
2221 idx++; // Long takes two cpool slots
2222 break;
2223 }
2224 case JVM_CONSTANT_Double: {
2225 jdouble val = double_at(idx);
2226 Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
2227 idx++; // Double takes two cpool slots
2228 break;
2229 }
2230 case JVM_CONSTANT_Class:
2231 case JVM_CONSTANT_UnresolvedClass:
2232 case JVM_CONSTANT_UnresolvedClassInError: {
2233 *bytes = JVM_CONSTANT_Class;
2234 Symbol* sym = klass_name_at(idx);
2235 idx1 = tbl->symbol_to_value(sym);
2236 assert(idx1 != 0, "Have not found a hashtable entry");
2237 Bytes::put_Java_u2((address) (bytes+1), idx1);
2238 DBG(printf("JVM_CONSTANT_Class: idx=#%03hd, %s", idx1, sym->as_utf8()));
2239 break;
2240 }
2241 case JVM_CONSTANT_Value:
2242 case JVM_CONSTANT_UnresolvedValue:
2243 case JVM_CONSTANT_UnresolvedValueInError: {
2244 *bytes = JVM_CONSTANT_Value;
2245 Symbol* sym = klass_name_at(idx);
2246 idx1 = tbl->symbol_to_value(sym);
2247 assert(idx1 != 0, "Have not found a hashtable entry");
2248 Bytes::put_Java_u2((address) (bytes+1), idx1);
2249 DBG(printf("JVM_CONSTANT_Value: idx=#%03hd, %s", idx1, sym->as_utf8()));
2250 break;
2251 }
2252 case JVM_CONSTANT_String: {
2253 *bytes = JVM_CONSTANT_String;
2254 Symbol* sym = unresolved_string_at(idx);
2255 idx1 = tbl->symbol_to_value(sym);
2256 assert(idx1 != 0, "Have not found a hashtable entry");
2257 Bytes::put_Java_u2((address) (bytes+1), idx1);
2258 DBG(printf("JVM_CONSTANT_String: idx=#%03hd, %s", idx1, sym->as_utf8()));
2259 break;
2260 }
2261 case JVM_CONSTANT_Fieldref:
2262 case JVM_CONSTANT_Methodref:
2263 case JVM_CONSTANT_InterfaceMethodref: {
2264 idx1 = uncached_klass_ref_index_at(idx);
2265 idx2 = uncached_name_and_type_ref_index_at(idx);
2266 Bytes::put_Java_u2((address) (bytes+1), idx1);
2267 Bytes::put_Java_u2((address) (bytes+3), idx2);
2268 DBG(printf("JVM_CONSTANT_Methodref: %hd %hd", idx1, idx2));
2269 break;
2270 }
2271 case JVM_CONSTANT_NameAndType: {
2272 idx1 = name_ref_index_at(idx);
2273 idx2 = signature_ref_index_at(idx);
2274 Bytes::put_Java_u2((address) (bytes+1), idx1);
2275 Bytes::put_Java_u2((address) (bytes+3), idx2);
2276 DBG(printf("JVM_CONSTANT_NameAndType: %hd %hd", idx1, idx2));
2277 break;
2278 }
2279 case JVM_CONSTANT_ClassIndex: {
2280 *bytes = JVM_CONSTANT_Class;
2281 idx1 = klass_index_at(idx);
2282 Bytes::put_Java_u2((address) (bytes+1), idx1);
2283 DBG(printf("JVM_CONSTANT_ClassIndex: %hd", idx1));
2284 break;
2285 }
2286 case JVM_CONSTANT_ValueIndex: {
2287 *bytes = JVM_CONSTANT_Value;
2288 idx1 = value_type_index_at(idx);
2289 Bytes::put_Java_u2((address) (bytes+1), idx1);
2290 DBG(printf("JVM_CONSTANT_ValueIndex: %hd", idx1));
2291 break;
2292 }
2293 case JVM_CONSTANT_StringIndex: {
2294 *bytes = JVM_CONSTANT_String;
2295 idx1 = string_index_at(idx);
2296 Bytes::put_Java_u2((address) (bytes+1), idx1);
2297 DBG(printf("JVM_CONSTANT_StringIndex: %hd", idx1));
2298 break;
2299 }
2300 case JVM_CONSTANT_MethodHandle:
2301 case JVM_CONSTANT_MethodHandleInError: {
2302 *bytes = JVM_CONSTANT_MethodHandle;
2303 int kind = method_handle_ref_kind_at(idx);
2304 idx1 = method_handle_index_at(idx);
2305 *(bytes+1) = (unsigned char) kind;
2306 Bytes::put_Java_u2((address) (bytes+2), idx1);
2307 DBG(printf("JVM_CONSTANT_MethodHandle: %d %hd", kind, idx1));
2308 break;
2309 }
2310 case JVM_CONSTANT_MethodType:
2311 case JVM_CONSTANT_MethodTypeInError: {
2312 *bytes = JVM_CONSTANT_MethodType;
2391 #ifdef ASSERT
2392 // Ensure that all the patches have been used.
2393 for (int index = 0; index < cp_patches->length(); index++) {
2394 assert(cp_patches->at(index).is_null(),
2395 "Unused constant pool patch at %d in class file %s",
2396 index,
2397 pool_holder()->external_name());
2398 }
2399 #endif // ASSERT
2400 }
2401
2402 #ifndef PRODUCT
2403
2404 // CompileTheWorld support. Preload all classes loaded references in the passed in constantpool
2405 void ConstantPool::preload_and_initialize_all_classes(ConstantPool* obj, TRAPS) {
2406 guarantee(obj->is_constantPool(), "object must be constant pool");
2407 constantPoolHandle cp(THREAD, (ConstantPool*)obj);
2408 guarantee(cp->pool_holder() != NULL, "must be fully loaded");
2409
2410 for (int i = 0; i< cp->length(); i++) {
2411 if (cp->tag_at(i).is_unresolved_klass() ||
2412 cp->tag_at(i).is_unresolved_value_type()) {
2413 // This will force loading of the class
2414 Klass* klass = cp->klass_at(i, CHECK);
2415 if (klass->is_instance_klass()) {
2416 // Force initialization of class
2417 InstanceKlass::cast(klass)->initialize(CHECK);
2418 }
2419 }
2420 }
2421 }
2422
2423 #endif
2424
2425
2426 // Printing
2427
2428 void ConstantPool::print_on(outputStream* st) const {
2429 assert(is_constantPool(), "must be constantPool");
2430 st->print_cr("%s", internal_name());
2431 if (flags() != 0) {
2432 st->print(" - flags: 0x%x", flags());
2451 }
2452
2453 }
2454 st->cr();
2455 }
2456
2457 // Print one constant pool entry
2458 void ConstantPool::print_entry_on(const int index, outputStream* st) {
2459 EXCEPTION_MARK;
2460 st->print(" - %3d : ", index);
2461 tag_at(index).print_on(st);
2462 st->print(" : ");
2463 switch (tag_at(index).value()) {
2464 case JVM_CONSTANT_Class :
2465 { Klass* k = klass_at(index, CATCH);
2466 guarantee(k != NULL, "need klass");
2467 k->print_value_on(st);
2468 st->print(" {" PTR_FORMAT "}", p2i(k));
2469 }
2470 break;
2471 case JVM_CONSTANT_Value :
2472 { Klass* k = klass_at(index, CATCH);
2473 guarantee(k != NULL, "need klass");
2474 k->print_value_on(st);
2475 st->print(" {" PTR_FORMAT "}", p2i(k));
2476 }
2477 break;
2478 case JVM_CONSTANT_Fieldref :
2479 case JVM_CONSTANT_Methodref :
2480 case JVM_CONSTANT_InterfaceMethodref :
2481 st->print("klass_index=%d", uncached_klass_ref_index_at(index));
2482 st->print(" name_and_type_index=%d", uncached_name_and_type_ref_index_at(index));
2483 break;
2484 case JVM_CONSTANT_String :
2485 if (is_pseudo_string_at(index)) {
2486 oop anObj = pseudo_string_at(index);
2487 anObj->print_value_on(st);
2488 st->print(" {" PTR_FORMAT "}", p2i(anObj));
2489 } else {
2490 unresolved_string_at(index)->print_value_on(st);
2491 }
2492 break;
2493 case JVM_CONSTANT_Integer :
2494 st->print("%d", int_at(index));
2495 break;
2496 case JVM_CONSTANT_Float :
2497 st->print("%f", float_at(index));
2498 break;
2499 case JVM_CONSTANT_Long :
2500 st->print_jlong(long_at(index));
2501 break;
2502 case JVM_CONSTANT_Double :
2503 st->print("%lf", double_at(index));
2504 break;
2505 case JVM_CONSTANT_NameAndType :
2506 st->print("name_index=%d", name_ref_index_at(index));
2507 st->print(" signature_index=%d", signature_ref_index_at(index));
2508 break;
2509 case JVM_CONSTANT_Utf8 :
2510 symbol_at(index)->print_value_on(st);
2511 break;
2512 case JVM_CONSTANT_ClassIndex:
2513 case JVM_CONSTANT_ValueIndex: {
2514 int name_index = *int_at_addr(index);
2515 st->print("klass_index=%d ", name_index);
2516 symbol_at(name_index)->print_value_on(st);
2517 }
2518 break;
2519 case JVM_CONSTANT_UnresolvedClass : // fall-through
2520 case JVM_CONSTANT_UnresolvedClassInError :
2521 case JVM_CONSTANT_UnresolvedValue :
2522 case JVM_CONSTANT_UnresolvedValueInError : {
2523 CPKlassSlot kslot = klass_slot_at(index);
2524 int resolved_klass_index = kslot.resolved_klass_index();
2525 int name_index = kslot.name_index();
2526 assert(tag_at(name_index).is_symbol(), "sanity");
2527
2528 Klass* klass = resolved_klasses()->at(resolved_klass_index);
2529 if (klass != NULL) {
2530 klass->print_value_on(st);
2531 } else {
2532 symbol_at(name_index)->print_value_on(st);
2533 }
2534 }
2535 break;
2536 case JVM_CONSTANT_MethodHandle :
2537 case JVM_CONSTANT_MethodHandleInError :
2538 st->print("ref_kind=%d", method_handle_ref_kind_at(index));
2539 st->print(" ref_index=%d", method_handle_index_at(index));
2540 break;
2541 case JVM_CONSTANT_MethodType :
2542 case JVM_CONSTANT_MethodTypeInError :
2599 // Size Statistics
2600 void ConstantPool::collect_statistics(KlassSizeStats *sz) const {
2601 sz->_cp_all_bytes += (sz->_cp_bytes = sz->count(this));
2602 sz->_cp_all_bytes += (sz->_cp_tags_bytes = sz->count_array(tags()));
2603 sz->_cp_all_bytes += (sz->_cp_cache_bytes = sz->count(cache()));
2604 sz->_cp_all_bytes += (sz->_cp_operands_bytes = sz->count_array(operands()));
2605 sz->_cp_all_bytes += (sz->_cp_refmap_bytes = sz->count_array(reference_map()));
2606
2607 sz->_ro_bytes += sz->_cp_operands_bytes + sz->_cp_tags_bytes +
2608 sz->_cp_refmap_bytes;
2609 sz->_rw_bytes += sz->_cp_bytes + sz->_cp_cache_bytes;
2610 }
2611 #endif // INCLUDE_SERVICES
2612
2613 // Verification
2614
2615 void ConstantPool::verify_on(outputStream* st) {
2616 guarantee(is_constantPool(), "object must be constant pool");
2617 for (int i = 0; i< length(); i++) {
2618 constantTag tag = tag_at(i);
2619 if (tag.is_klass() || tag.is_unresolved_klass() ||
2620 tag.is_value_type() || tag.is_unresolved_value_type()) {
2621 guarantee(klass_name_at(i)->refcount() != 0, "should have nonzero reference count");
2622 } else if (tag.is_symbol()) {
2623 CPSlot entry = slot_at(i);
2624 guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count");
2625 } else if (tag.is_string()) {
2626 CPSlot entry = slot_at(i);
2627 guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count");
2628 }
2629 }
2630 if (cache() != NULL) {
2631 // Note: cache() can be NULL before a class is completely setup or
2632 // in temporary constant pools used during constant pool merging
2633 guarantee(cache()->is_constantPoolCache(), "should be constant pool cache");
2634 }
2635 if (pool_holder() != NULL) {
2636 // Note: pool_holder() can be NULL in temporary constant pools
2637 // used during constant pool merging
2638 guarantee(pool_holder()->is_klass(), "should be klass");
2639 }
2640 }
|
183 // entry for the class's name. So at most we will have 0xfffe class entries.
184 // This allows us to use 0xffff (ConstantPool::_temp_resolved_klass_index) to indicate
185 // UnresolvedKlass entries that are temporarily created during class redefinition.
186 assert(num_klasses < CPKlassSlot::_temp_resolved_klass_index, "sanity");
187 assert(resolved_klasses() == NULL, "sanity");
188 Array<Klass*>* rk = MetadataFactory::new_array<Klass*>(loader_data, num_klasses, CHECK);
189 set_resolved_klasses(rk);
190 }
191
192 void ConstantPool::initialize_unresolved_klasses(ClassLoaderData* loader_data, TRAPS) {
193 int len = length();
194 int num_klasses = 0;
195 for (int i = 1; i <len; i++) {
196 switch (tag_at(i).value()) {
197 case JVM_CONSTANT_ClassIndex:
198 {
199 const int class_index = klass_index_at(i);
200 unresolved_klass_at_put(i, class_index, num_klasses++);
201 }
202 break;
203 #ifndef PRODUCT
204 case JVM_CONSTANT_Class:
205 case JVM_CONSTANT_UnresolvedClass:
206 case JVM_CONSTANT_UnresolvedClassInError:
207 // All of these should have been reverted back to Unresolved before calling
208 // this function.
209 ShouldNotReachHere();
210 #endif
211 }
212 }
213 allocate_resolved_klasses(loader_data, num_klasses, THREAD);
214 }
215
216 // Anonymous class support:
217 void ConstantPool::klass_at_put(int class_index, int name_index, int resolved_klass_index, Klass* k, Symbol* name) {
218 assert(is_within_bounds(class_index), "index out of bounds");
219 assert(is_within_bounds(name_index), "index out of bounds");
220 assert((resolved_klass_index & 0xffff0000) == 0, "must be");
221 *int_at_addr(class_index) =
222 build_int_from_shorts((jushort)resolved_klass_index, (jushort)name_index);
223
224 symbol_at_put(name_index, name);
225 name->increment_refcount();
226 Klass** adr = resolved_klasses()->adr_at(resolved_klass_index);
227 OrderAccess::release_store(adr, k);
228
229 // The interpreter assumes when the tag is stored, the klass is resolved
230 // and the Klass* non-NULL, so we need hardware store ordering here.
231 if (k != NULL) {
232 release_tag_at_put(class_index, JVM_CONSTANT_Class);
233 } else {
234 release_tag_at_put(class_index, JVM_CONSTANT_UnresolvedClass);
235 }
236 }
237
238 // Anonymous class support:
239 void ConstantPool::klass_at_put(int class_index, Klass* k) {
240 assert(k != NULL, "must be valid klass");
241 CPKlassSlot kslot = klass_slot_at(class_index);
242 int resolved_klass_index = kslot.resolved_klass_index();
243 Klass** adr = resolved_klasses()->adr_at(resolved_klass_index);
244 OrderAccess::release_store(adr, k);
245
246 // The interpreter assumes when the tag is stored, the klass is resolved
247 // and the Klass* non-NULL, so we need hardware store ordering here.
248 release_tag_at_put(class_index, JVM_CONSTANT_Class);
249 }
250
251 #if INCLUDE_CDS_JAVA_HEAP
252 // Archive the resolved references
253 void ConstantPool::archive_resolved_references(Thread* THREAD) {
254 if (_cache == NULL) {
255 return; // nothing to do
256 }
257
258 InstanceKlass *ik = pool_holder();
259 if (!(ik->is_shared_boot_class() || ik->is_shared_platform_class() ||
260 ik->is_shared_app_class())) {
261 // Archiving resolved references for classes from non-builtin loaders
262 // is not yet supported.
263 set_resolved_references(NULL);
264 return;
265 }
266
267 objArrayOop rr = resolved_references();
268 Array<u2>* ref_map = reference_map();
427 log_debug(class, resolve)("%s %s",
428 this_cp->pool_holder()->external_name(),
429 k->external_name());
430 }
431 }
432 }
433
434 Klass* ConstantPool::klass_at_impl(const constantPoolHandle& this_cp, int which,
435 bool save_resolution_error, TRAPS) {
436 assert(THREAD->is_Java_thread(), "must be a Java thread");
437
438 // A resolved constantPool entry will contain a Klass*, otherwise a Symbol*.
439 // It is not safe to rely on the tag bit's here, since we don't have a lock, and
440 // the entry and tag is not updated atomicly.
441 CPKlassSlot kslot = this_cp->klass_slot_at(which);
442 int resolved_klass_index = kslot.resolved_klass_index();
443 int name_index = kslot.name_index();
444 assert(this_cp->tag_at(name_index).is_symbol(), "sanity");
445
446 Klass* klass = this_cp->resolved_klasses()->at(resolved_klass_index);
447 if (klass != NULL) {
448 return klass;
449 }
450
451 // This tag doesn't change back to unresolved class unless at a safepoint.
452 if (this_cp->tag_at(which).is_unresolved_klass_in_error()) {
453 // The original attempt to resolve this constant pool entry failed so find the
454 // class of the original error and throw another error of the same class
455 // (JVMS 5.4.3).
456 // If there is a detail message, pass that detail message to the error.
457 // The JVMS does not strictly require us to duplicate the same detail message,
458 // or any internal exception fields such as cause or stacktrace. But since the
459 // detail message is often a class name or other literal string, we will repeat it
460 // if we can find it in the symbol table.
461 throw_resolution_error(this_cp, which, CHECK_0);
462 ShouldNotReachHere();
463 }
464
465 Handle mirror_handle;
466 Symbol* name = this_cp->symbol_at(name_index);
467 Handle loader (THREAD, this_cp->pool_holder()->class_loader());
468 Handle protection_domain (THREAD, this_cp->pool_holder()->protection_domain());
469 Klass* k = SystemDictionary::resolve_or_fail(name, loader, protection_domain, true, THREAD);
470 if (!HAS_PENDING_EXCEPTION) {
471 // preserve the resolved klass from unloading
472 mirror_handle = Handle(THREAD, k->java_mirror());
473 // Do access check for klasses
474 verify_constant_pool_resolve(this_cp, k, THREAD);
475 }
476
477 // Failed to resolve class. We must record the errors so that subsequent attempts
478 // to resolve this constant pool entry fail with the same error (JVMS 5.4.3).
479 if (HAS_PENDING_EXCEPTION) {
480 if (save_resolution_error) {
481 save_and_throw_exception(this_cp, which, constantTag(JVM_CONSTANT_UnresolvedClass), CHECK_NULL);
482 // If CHECK_NULL above doesn't return the exception, that means that
483 // some other thread has beaten us and has resolved the class.
484 // To preserve old behavior, we return the resolved class.
485 klass = this_cp->resolved_klasses()->at(resolved_klass_index);
486 assert(klass != NULL, "must be resolved if exception was cleared");
487 return klass;
488 } else {
489 return NULL; // return the pending exception
490 }
491 }
492
493 // Make this class loader depend upon the class loader owning the class reference
494 ClassLoaderData* this_key = this_cp->pool_holder()->class_loader_data();
495 this_key->record_dependency(k, CHECK_NULL); // Can throw OOM
496
497 // logging for class+resolve.
498 if (log_is_enabled(Debug, class, resolve)){
499 trace_class_resolution(this_cp, k);
500 }
501 Klass** adr = this_cp->resolved_klasses()->adr_at(resolved_klass_index);
502 OrderAccess::release_store(adr, k);
503 // The interpreter assumes when the tag is stored, the klass is resolved
504 // and the Klass* stored in _resolved_klasses is non-NULL, so we need
505 // hardware store ordering here.
506 this_cp->release_tag_at_put(which, JVM_CONSTANT_Class);
507 return k;
508 }
509
510
511 // Does not update ConstantPool* - to avoid any exception throwing. Used
512 // by compiler and exception handling. Also used to avoid classloads for
513 // instanceof operations. Returns NULL if the class has not been loaded or
514 // if the verification of constant pool failed
515 Klass* ConstantPool::klass_at_if_loaded(const constantPoolHandle& this_cp, int which) {
516 CPKlassSlot kslot = this_cp->klass_slot_at(which);
517 int resolved_klass_index = kslot.resolved_klass_index();
518 int name_index = kslot.name_index();
519 assert(this_cp->tag_at(name_index).is_symbol(), "sanity");
520
521 Klass* k = this_cp->resolved_klasses()->at(resolved_klass_index);
522 if (k != NULL) {
523 return k;
524 } else {
525 Thread *thread = Thread::current();
526 Symbol* name = this_cp->symbol_at(name_index);
724
725
726 void ConstantPool::resolve_string_constants_impl(const constantPoolHandle& this_cp, TRAPS) {
727 for (int index = 1; index < this_cp->length(); index++) { // Index 0 is unused
728 if (this_cp->tag_at(index).is_string()) {
729 this_cp->string_at(index, CHECK);
730 }
731 }
732 }
733
734 Symbol* ConstantPool::exception_message(const constantPoolHandle& this_cp, int which, constantTag tag, oop pending_exception) {
735 // Dig out the detailed message to reuse if possible
736 Symbol* message = java_lang_Throwable::detail_message(pending_exception);
737 if (message != NULL) {
738 return message;
739 }
740
741 // Return specific message for the tag
742 switch (tag.value()) {
743 case JVM_CONSTANT_UnresolvedClass:
744 // return the class name in the error message
745 message = this_cp->klass_name_at(which);
746 break;
747 case JVM_CONSTANT_MethodHandle:
748 // return the method handle name in the error message
749 message = this_cp->method_handle_name_ref_at(which);
750 break;
751 case JVM_CONSTANT_MethodType:
752 // return the method type signature in the error message
753 message = this_cp->method_type_signature_at(which);
754 break;
755 default:
756 ShouldNotReachHere();
757 }
758
759 return message;
760 }
761
762 void ConstantPool::throw_resolution_error(const constantPoolHandle& this_cp, int which, TRAPS) {
763 Symbol* message = NULL;
778
779 if (!PENDING_EXCEPTION->
780 is_a(SystemDictionary::LinkageError_klass())) {
781 // Just throw the exception and don't prevent these classes from
782 // being loaded due to virtual machine errors like StackOverflow
783 // and OutOfMemoryError, etc, or if the thread was hit by stop()
784 // Needs clarification to section 5.4.3 of the VM spec (see 6308271)
785 } else if (this_cp->tag_at(which).value() != error_tag) {
786 Symbol* message = exception_message(this_cp, which, tag, PENDING_EXCEPTION);
787 SystemDictionary::add_resolution_error(this_cp, which, error, message);
788 // CAS in the tag. If a thread beat us to registering this error that's fine.
789 // If another thread resolved the reference, this is a race condition. This
790 // thread may have had a security manager or something temporary.
791 // This doesn't deterministically get an error. So why do we save this?
792 // We save this because jvmti can add classes to the bootclass path after
793 // this error, so it needs to get the same error if the error is first.
794 jbyte old_tag = Atomic::cmpxchg((jbyte)error_tag,
795 (jbyte*)this_cp->tag_addr_at(which), (jbyte)tag.value());
796 if (old_tag != error_tag && old_tag != tag.value()) {
797 // MethodHandles and MethodType doesn't change to resolved version.
798 assert(this_cp->tag_at(which).is_klass(), "Wrong tag value");
799 // Forget the exception and use the resolved class.
800 CLEAR_PENDING_EXCEPTION;
801 }
802 } else {
803 // some other thread put this in error state
804 throw_resolution_error(this_cp, which, CHECK);
805 }
806 }
807
808 BasicType ConstantPool::basic_type_for_constant_at(int which) {
809 constantTag tag = tag_at(which);
810 if (tag.is_dynamic_constant() ||
811 tag.is_dynamic_constant_in_error()) {
812 // have to look at the signature for this one
813 Symbol* constant_type = uncached_signature_ref_at(which);
814 return FieldType::basic_type(constant_type);
815 }
816 return tag.basic_type();
817 }
818
872 case JVM_CONSTANT_String:
873 case JVM_CONSTANT_Integer:
874 case JVM_CONSTANT_Float:
875 case JVM_CONSTANT_Long:
876 case JVM_CONSTANT_Double:
877 // these guys trigger OOM at worst
878 break;
879 default:
880 (*status_return) = false;
881 return NULL;
882 }
883 // from now on there is either success or an OOME
884 (*status_return) = true;
885 }
886
887 switch (tag.value()) {
888
889 case JVM_CONSTANT_UnresolvedClass:
890 case JVM_CONSTANT_UnresolvedClassInError:
891 case JVM_CONSTANT_Class:
892 {
893 assert(cache_index == _no_index_sentinel, "should not have been set");
894 Klass* resolved = klass_at_impl(this_cp, index, true, CHECK_NULL);
895 // ldc wants the java mirror.
896 result_oop = resolved->java_mirror();
897 break;
898 }
899
900 case JVM_CONSTANT_Dynamic:
901 {
902 Klass* current_klass = this_cp->pool_holder();
903 Symbol* constant_name = this_cp->uncached_name_ref_at(index);
904 Symbol* constant_type = this_cp->uncached_signature_ref_at(index);
905
906 // The initial step in resolving an unresolved symbolic reference to a
907 // dynamically-computed constant is to resolve the symbolic reference to a
908 // method handle which will be the bootstrap method for the dynamically-computed
909 // constant. If resolution of the java.lang.invoke.MethodHandle for the bootstrap
910 // method fails, then a MethodHandleInError is stored at the corresponding
911 // bootstrap method's CP index for the CONSTANT_MethodHandle_info. No need to
1278 // cp2's entry at index2.
1279 bool ConstantPool::compare_entry_to(int index1, const constantPoolHandle& cp2,
1280 int index2, TRAPS) {
1281
1282 // The error tags are equivalent to non-error tags when comparing
1283 jbyte t1 = tag_at(index1).non_error_value();
1284 jbyte t2 = cp2->tag_at(index2).non_error_value();
1285
1286 if (t1 != t2) {
1287 // Not the same entry type so there is nothing else to check. Note
1288 // that this style of checking will consider resolved/unresolved
1289 // class pairs as different.
1290 // From the ConstantPool* API point of view, this is correct
1291 // behavior. See VM_RedefineClasses::merge_constant_pools() to see how this
1292 // plays out in the context of ConstantPool* merging.
1293 return false;
1294 }
1295
1296 switch (t1) {
1297 case JVM_CONSTANT_Class:
1298 {
1299 Klass* k1 = klass_at(index1, CHECK_false);
1300 Klass* k2 = cp2->klass_at(index2, CHECK_false);
1301 if (k1 == k2) {
1302 return true;
1303 }
1304 } break;
1305
1306 case JVM_CONSTANT_ClassIndex:
1307 {
1308 int recur1 = klass_index_at(index1);
1309 int recur2 = cp2->klass_index_at(index2);
1310 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
1311 if (match) {
1312 return true;
1313 }
1314 } break;
1315
1316 case JVM_CONSTANT_Double:
1317 {
1318 jdouble d1 = double_at(index1);
1319 jdouble d2 = cp2->double_at(index2);
1320 if (d1 == d2) {
1321 return true;
1322 }
1323 } break;
1324
1325 case JVM_CONSTANT_Fieldref:
1326 case JVM_CONSTANT_InterfaceMethodref:
1327 case JVM_CONSTANT_Methodref:
1328 {
1329 int recur1 = uncached_klass_ref_index_at(index1);
1330 int recur2 = cp2->uncached_klass_ref_index_at(index2);
1331 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
1332 if (match) {
1333 recur1 = uncached_name_and_type_ref_index_at(index1);
1334 recur2 = cp2->uncached_name_and_type_ref_index_at(index2);
1335 match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
1375 recur1 = signature_ref_index_at(index1);
1376 recur2 = cp2->signature_ref_index_at(index2);
1377 match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
1378 if (match) {
1379 return true;
1380 }
1381 }
1382 } break;
1383
1384 case JVM_CONSTANT_StringIndex:
1385 {
1386 int recur1 = string_index_at(index1);
1387 int recur2 = cp2->string_index_at(index2);
1388 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
1389 if (match) {
1390 return true;
1391 }
1392 } break;
1393
1394 case JVM_CONSTANT_UnresolvedClass:
1395 {
1396 Symbol* k1 = klass_name_at(index1);
1397 Symbol* k2 = cp2->klass_name_at(index2);
1398 if (k1 == k2) {
1399 return true;
1400 }
1401 } break;
1402
1403 case JVM_CONSTANT_MethodType:
1404 {
1405 int k1 = method_type_index_at(index1);
1406 int k2 = cp2->method_type_index_at(index2);
1407 bool match = compare_entry_to(k1, cp2, k2, CHECK_false);
1408 if (match) {
1409 return true;
1410 }
1411 } break;
1412
1413 case JVM_CONSTANT_MethodHandle:
1414 {
1644 }
1645 copy_operands(from_cp, to_cp, CHECK);
1646
1647 } // end copy_cp_to_impl()
1648
1649
1650 // Copy this constant pool's entry at from_i to the constant pool
1651 // to_cp's entry at to_i.
1652 void ConstantPool::copy_entry_to(const constantPoolHandle& from_cp, int from_i,
1653 const constantPoolHandle& to_cp, int to_i,
1654 TRAPS) {
1655
1656 int tag = from_cp->tag_at(from_i).value();
1657 switch (tag) {
1658 case JVM_CONSTANT_ClassIndex:
1659 {
1660 jint ki = from_cp->klass_index_at(from_i);
1661 to_cp->klass_index_at_put(to_i, ki);
1662 } break;
1663
1664 case JVM_CONSTANT_Double:
1665 {
1666 jdouble d = from_cp->double_at(from_i);
1667 to_cp->double_at_put(to_i, d);
1668 // double takes two constant pool entries so init second entry's tag
1669 to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
1670 } break;
1671
1672 case JVM_CONSTANT_Fieldref:
1673 {
1674 int class_index = from_cp->uncached_klass_ref_index_at(from_i);
1675 int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
1676 to_cp->field_at_put(to_i, class_index, name_and_type_index);
1677 } break;
1678
1679 case JVM_CONSTANT_Float:
1680 {
1681 jfloat f = from_cp->float_at(from_i);
1682 to_cp->float_at_put(to_i, f);
1683 } break;
1716 int signature_ref_index = from_cp->signature_ref_index_at(from_i);
1717 to_cp->name_and_type_at_put(to_i, name_ref_index, signature_ref_index);
1718 } break;
1719
1720 case JVM_CONSTANT_StringIndex:
1721 {
1722 jint si = from_cp->string_index_at(from_i);
1723 to_cp->string_index_at_put(to_i, si);
1724 } break;
1725
1726 case JVM_CONSTANT_Class:
1727 case JVM_CONSTANT_UnresolvedClass:
1728 case JVM_CONSTANT_UnresolvedClassInError:
1729 {
1730 // Revert to JVM_CONSTANT_ClassIndex
1731 int name_index = from_cp->klass_slot_at(from_i).name_index();
1732 assert(from_cp->tag_at(name_index).is_symbol(), "sanity");
1733 to_cp->klass_index_at_put(to_i, name_index);
1734 } break;
1735
1736 case JVM_CONSTANT_String:
1737 {
1738 Symbol* s = from_cp->unresolved_string_at(from_i);
1739 to_cp->unresolved_string_at_put(to_i, s);
1740 } break;
1741
1742 case JVM_CONSTANT_Utf8:
1743 {
1744 Symbol* s = from_cp->symbol_at(from_i);
1745 // Need to increase refcount, the old one will be thrown away and deferenced
1746 s->increment_refcount();
1747 to_cp->symbol_at_put(to_i, s);
1748 } break;
1749
1750 case JVM_CONSTANT_MethodType:
1751 case JVM_CONSTANT_MethodTypeInError:
1752 {
1753 jint k = from_cp->method_type_index_at(from_i);
1754 to_cp->method_type_index_at_put(to_i, k);
1755 } break;
1840 int ConstantPool::find_matching_operand(int pattern_i,
1841 const constantPoolHandle& search_cp, int search_len, TRAPS) {
1842 for (int i = 0; i < search_len; i++) {
1843 bool found = compare_operand_to(pattern_i, search_cp, i, CHECK_(-1));
1844 if (found) {
1845 return i;
1846 }
1847 }
1848 return -1; // bootstrap specifier not found; return unused index (-1)
1849 } // end find_matching_operand()
1850
1851
1852 #ifndef PRODUCT
1853
1854 const char* ConstantPool::printable_name_at(int which) {
1855
1856 constantTag tag = tag_at(which);
1857
1858 if (tag.is_string()) {
1859 return string_at_noresolve(which);
1860 } else if (tag.is_klass() || tag.is_unresolved_klass()) {
1861 return klass_name_at(which)->as_C_string();
1862 } else if (tag.is_symbol()) {
1863 return symbol_at(which)->as_C_string();
1864 }
1865 return "";
1866 }
1867
1868 #endif // PRODUCT
1869
1870
1871 // JVMTI GetConstantPool support
1872
1873 // For debugging of constant pool
1874 const bool debug_cpool = false;
1875
1876 #define DBG(code) do { if (debug_cpool) { (code); } } while(0)
1877
1878 static void print_cpool_bytes(jint cnt, u1 *bytes) {
1879 const char* WARN_MSG = "Must not be such entry!";
1880 jint size = 0;
1922 case JVM_CONSTANT_Long: {
1923 u8 val = Bytes::get_Java_u8(bytes);
1924 printf("long " INT64_FORMAT, (int64_t) *(jlong *) &val);
1925 ent_size = 8;
1926 idx++; // Long takes two cpool slots
1927 break;
1928 }
1929 case JVM_CONSTANT_Double: {
1930 u8 val = Bytes::get_Java_u8(bytes);
1931 printf("double %5.3fd", *(jdouble *)&val);
1932 ent_size = 8;
1933 idx++; // Double takes two cpool slots
1934 break;
1935 }
1936 case JVM_CONSTANT_Class: {
1937 idx1 = Bytes::get_Java_u2(bytes);
1938 printf("class #%03d", idx1);
1939 ent_size = 2;
1940 break;
1941 }
1942 case JVM_CONSTANT_String: {
1943 idx1 = Bytes::get_Java_u2(bytes);
1944 printf("String #%03d", idx1);
1945 ent_size = 2;
1946 break;
1947 }
1948 case JVM_CONSTANT_Fieldref: {
1949 idx1 = Bytes::get_Java_u2(bytes);
1950 idx2 = Bytes::get_Java_u2(bytes+2);
1951 printf("Field #%03d, #%03d", (int) idx1, (int) idx2);
1952 ent_size = 4;
1953 break;
1954 }
1955 case JVM_CONSTANT_Methodref: {
1956 idx1 = Bytes::get_Java_u2(bytes);
1957 idx2 = Bytes::get_Java_u2(bytes+2);
1958 printf("Method #%03d, #%03d", idx1, idx2);
1959 ent_size = 4;
1960 break;
1961 }
1968 }
1969 case JVM_CONSTANT_NameAndType: {
1970 idx1 = Bytes::get_Java_u2(bytes);
1971 idx2 = Bytes::get_Java_u2(bytes+2);
1972 printf("NameAndType #%03d, #%03d", idx1, idx2);
1973 ent_size = 4;
1974 break;
1975 }
1976 case JVM_CONSTANT_ClassIndex: {
1977 printf("ClassIndex %s", WARN_MSG);
1978 break;
1979 }
1980 case JVM_CONSTANT_UnresolvedClass: {
1981 printf("UnresolvedClass: %s", WARN_MSG);
1982 break;
1983 }
1984 case JVM_CONSTANT_UnresolvedClassInError: {
1985 printf("UnresolvedClassInErr: %s", WARN_MSG);
1986 break;
1987 }
1988 case JVM_CONSTANT_StringIndex: {
1989 printf("StringIndex: %s", WARN_MSG);
1990 break;
1991 }
1992 }
1993 printf(";\n");
1994 bytes += ent_size;
1995 size += ent_size;
1996 }
1997 printf("Cpool size: %d\n", size);
1998 fflush(0);
1999 return;
2000 } /* end print_cpool_bytes */
2001
2002
2003 // Returns size of constant pool entry.
2004 jint ConstantPool::cpool_entry_size(jint idx) {
2005 switch(tag_at(idx).value()) {
2006 case JVM_CONSTANT_Invalid:
2007 case JVM_CONSTANT_Unicode:
2008 return 1;
2009
2010 case JVM_CONSTANT_Utf8:
2011 return 3 + symbol_at(idx)->utf8_length();
2012
2013 case JVM_CONSTANT_Class:
2014 case JVM_CONSTANT_String:
2015 case JVM_CONSTANT_ClassIndex:
2016 case JVM_CONSTANT_UnresolvedClass:
2017 case JVM_CONSTANT_UnresolvedClassInError:
2018 case JVM_CONSTANT_StringIndex:
2019 case JVM_CONSTANT_MethodType:
2020 case JVM_CONSTANT_MethodTypeInError:
2021 return 3;
2022
2023 case JVM_CONSTANT_MethodHandle:
2024 case JVM_CONSTANT_MethodHandleInError:
2025 return 4; //tag, ref_kind, ref_index
2026
2027 case JVM_CONSTANT_Integer:
2028 case JVM_CONSTANT_Float:
2029 case JVM_CONSTANT_Fieldref:
2030 case JVM_CONSTANT_Methodref:
2031 case JVM_CONSTANT_InterfaceMethodref:
2032 case JVM_CONSTANT_NameAndType:
2033 return 5;
2034
2035 case JVM_CONSTANT_Dynamic:
2036 case JVM_CONSTANT_DynamicInError:
2037 case JVM_CONSTANT_InvokeDynamic:
2056
2057 for (u2 idx = 1; idx < length(); idx++) {
2058 u2 tag = tag_at(idx).value();
2059 size += cpool_entry_size(idx);
2060
2061 switch(tag) {
2062 case JVM_CONSTANT_Utf8: {
2063 Symbol* sym = symbol_at(idx);
2064 symmap->add_entry(sym, idx);
2065 DBG(printf("adding symbol entry %s = %d\n", sym->as_utf8(), idx));
2066 break;
2067 }
2068 case JVM_CONSTANT_Class:
2069 case JVM_CONSTANT_UnresolvedClass:
2070 case JVM_CONSTANT_UnresolvedClassInError: {
2071 Symbol* sym = klass_name_at(idx);
2072 classmap->add_entry(sym, idx);
2073 DBG(printf("adding class entry %s = %d\n", sym->as_utf8(), idx));
2074 break;
2075 }
2076 case JVM_CONSTANT_Long:
2077 case JVM_CONSTANT_Double: {
2078 idx++; // Both Long and Double take two cpool slots
2079 break;
2080 }
2081 }
2082 }
2083 return size;
2084 } /* end hash_utf8_entries_to */
2085
2086
2087 // Copy cpool bytes.
2088 // Returns:
2089 // 0, in case of OutOfMemoryError
2090 // -1, in case of internal error
2091 // > 0, count of the raw cpool bytes that have been copied
2092 int ConstantPool::copy_cpool_bytes(int cpool_size,
2093 SymbolHashMap* tbl,
2094 unsigned char *bytes) {
2095 u2 idx1, idx2;
2143 idx++; // Long takes two cpool slots
2144 break;
2145 }
2146 case JVM_CONSTANT_Double: {
2147 jdouble val = double_at(idx);
2148 Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
2149 idx++; // Double takes two cpool slots
2150 break;
2151 }
2152 case JVM_CONSTANT_Class:
2153 case JVM_CONSTANT_UnresolvedClass:
2154 case JVM_CONSTANT_UnresolvedClassInError: {
2155 *bytes = JVM_CONSTANT_Class;
2156 Symbol* sym = klass_name_at(idx);
2157 idx1 = tbl->symbol_to_value(sym);
2158 assert(idx1 != 0, "Have not found a hashtable entry");
2159 Bytes::put_Java_u2((address) (bytes+1), idx1);
2160 DBG(printf("JVM_CONSTANT_Class: idx=#%03hd, %s", idx1, sym->as_utf8()));
2161 break;
2162 }
2163 case JVM_CONSTANT_String: {
2164 *bytes = JVM_CONSTANT_String;
2165 Symbol* sym = unresolved_string_at(idx);
2166 idx1 = tbl->symbol_to_value(sym);
2167 assert(idx1 != 0, "Have not found a hashtable entry");
2168 Bytes::put_Java_u2((address) (bytes+1), idx1);
2169 DBG(printf("JVM_CONSTANT_String: idx=#%03hd, %s", idx1, sym->as_utf8()));
2170 break;
2171 }
2172 case JVM_CONSTANT_Fieldref:
2173 case JVM_CONSTANT_Methodref:
2174 case JVM_CONSTANT_InterfaceMethodref: {
2175 idx1 = uncached_klass_ref_index_at(idx);
2176 idx2 = uncached_name_and_type_ref_index_at(idx);
2177 Bytes::put_Java_u2((address) (bytes+1), idx1);
2178 Bytes::put_Java_u2((address) (bytes+3), idx2);
2179 DBG(printf("JVM_CONSTANT_Methodref: %hd %hd", idx1, idx2));
2180 break;
2181 }
2182 case JVM_CONSTANT_NameAndType: {
2183 idx1 = name_ref_index_at(idx);
2184 idx2 = signature_ref_index_at(idx);
2185 Bytes::put_Java_u2((address) (bytes+1), idx1);
2186 Bytes::put_Java_u2((address) (bytes+3), idx2);
2187 DBG(printf("JVM_CONSTANT_NameAndType: %hd %hd", idx1, idx2));
2188 break;
2189 }
2190 case JVM_CONSTANT_ClassIndex: {
2191 *bytes = JVM_CONSTANT_Class;
2192 idx1 = klass_index_at(idx);
2193 Bytes::put_Java_u2((address) (bytes+1), idx1);
2194 DBG(printf("JVM_CONSTANT_ClassIndex: %hd", idx1));
2195 break;
2196 }
2197 case JVM_CONSTANT_StringIndex: {
2198 *bytes = JVM_CONSTANT_String;
2199 idx1 = string_index_at(idx);
2200 Bytes::put_Java_u2((address) (bytes+1), idx1);
2201 DBG(printf("JVM_CONSTANT_StringIndex: %hd", idx1));
2202 break;
2203 }
2204 case JVM_CONSTANT_MethodHandle:
2205 case JVM_CONSTANT_MethodHandleInError: {
2206 *bytes = JVM_CONSTANT_MethodHandle;
2207 int kind = method_handle_ref_kind_at(idx);
2208 idx1 = method_handle_index_at(idx);
2209 *(bytes+1) = (unsigned char) kind;
2210 Bytes::put_Java_u2((address) (bytes+2), idx1);
2211 DBG(printf("JVM_CONSTANT_MethodHandle: %d %hd", kind, idx1));
2212 break;
2213 }
2214 case JVM_CONSTANT_MethodType:
2215 case JVM_CONSTANT_MethodTypeInError: {
2216 *bytes = JVM_CONSTANT_MethodType;
2295 #ifdef ASSERT
2296 // Ensure that all the patches have been used.
2297 for (int index = 0; index < cp_patches->length(); index++) {
2298 assert(cp_patches->at(index).is_null(),
2299 "Unused constant pool patch at %d in class file %s",
2300 index,
2301 pool_holder()->external_name());
2302 }
2303 #endif // ASSERT
2304 }
2305
2306 #ifndef PRODUCT
2307
2308 // CompileTheWorld support. Preload all classes loaded references in the passed in constantpool
2309 void ConstantPool::preload_and_initialize_all_classes(ConstantPool* obj, TRAPS) {
2310 guarantee(obj->is_constantPool(), "object must be constant pool");
2311 constantPoolHandle cp(THREAD, (ConstantPool*)obj);
2312 guarantee(cp->pool_holder() != NULL, "must be fully loaded");
2313
2314 for (int i = 0; i< cp->length(); i++) {
2315 if (cp->tag_at(i).is_unresolved_klass()) {
2316 // This will force loading of the class
2317 Klass* klass = cp->klass_at(i, CHECK);
2318 if (klass->is_instance_klass()) {
2319 // Force initialization of class
2320 InstanceKlass::cast(klass)->initialize(CHECK);
2321 }
2322 }
2323 }
2324 }
2325
2326 #endif
2327
2328
2329 // Printing
2330
2331 void ConstantPool::print_on(outputStream* st) const {
2332 assert(is_constantPool(), "must be constantPool");
2333 st->print_cr("%s", internal_name());
2334 if (flags() != 0) {
2335 st->print(" - flags: 0x%x", flags());
2354 }
2355
2356 }
2357 st->cr();
2358 }
2359
2360 // Print one constant pool entry
2361 void ConstantPool::print_entry_on(const int index, outputStream* st) {
2362 EXCEPTION_MARK;
2363 st->print(" - %3d : ", index);
2364 tag_at(index).print_on(st);
2365 st->print(" : ");
2366 switch (tag_at(index).value()) {
2367 case JVM_CONSTANT_Class :
2368 { Klass* k = klass_at(index, CATCH);
2369 guarantee(k != NULL, "need klass");
2370 k->print_value_on(st);
2371 st->print(" {" PTR_FORMAT "}", p2i(k));
2372 }
2373 break;
2374 case JVM_CONSTANT_Fieldref :
2375 case JVM_CONSTANT_Methodref :
2376 case JVM_CONSTANT_InterfaceMethodref :
2377 st->print("klass_index=%d", uncached_klass_ref_index_at(index));
2378 st->print(" name_and_type_index=%d", uncached_name_and_type_ref_index_at(index));
2379 break;
2380 case JVM_CONSTANT_String :
2381 if (is_pseudo_string_at(index)) {
2382 oop anObj = pseudo_string_at(index);
2383 anObj->print_value_on(st);
2384 st->print(" {" PTR_FORMAT "}", p2i(anObj));
2385 } else {
2386 unresolved_string_at(index)->print_value_on(st);
2387 }
2388 break;
2389 case JVM_CONSTANT_Integer :
2390 st->print("%d", int_at(index));
2391 break;
2392 case JVM_CONSTANT_Float :
2393 st->print("%f", float_at(index));
2394 break;
2395 case JVM_CONSTANT_Long :
2396 st->print_jlong(long_at(index));
2397 break;
2398 case JVM_CONSTANT_Double :
2399 st->print("%lf", double_at(index));
2400 break;
2401 case JVM_CONSTANT_NameAndType :
2402 st->print("name_index=%d", name_ref_index_at(index));
2403 st->print(" signature_index=%d", signature_ref_index_at(index));
2404 break;
2405 case JVM_CONSTANT_Utf8 :
2406 symbol_at(index)->print_value_on(st);
2407 break;
2408 case JVM_CONSTANT_ClassIndex: {
2409 int name_index = *int_at_addr(index);
2410 st->print("klass_index=%d ", name_index);
2411 symbol_at(name_index)->print_value_on(st);
2412 }
2413 break;
2414 case JVM_CONSTANT_UnresolvedClass : // fall-through
2415 case JVM_CONSTANT_UnresolvedClassInError: {
2416 CPKlassSlot kslot = klass_slot_at(index);
2417 int resolved_klass_index = kslot.resolved_klass_index();
2418 int name_index = kslot.name_index();
2419 assert(tag_at(name_index).is_symbol(), "sanity");
2420
2421 Klass* klass = resolved_klasses()->at(resolved_klass_index);
2422 if (klass != NULL) {
2423 klass->print_value_on(st);
2424 } else {
2425 symbol_at(name_index)->print_value_on(st);
2426 }
2427 }
2428 break;
2429 case JVM_CONSTANT_MethodHandle :
2430 case JVM_CONSTANT_MethodHandleInError :
2431 st->print("ref_kind=%d", method_handle_ref_kind_at(index));
2432 st->print(" ref_index=%d", method_handle_index_at(index));
2433 break;
2434 case JVM_CONSTANT_MethodType :
2435 case JVM_CONSTANT_MethodTypeInError :
2492 // Size Statistics
2493 void ConstantPool::collect_statistics(KlassSizeStats *sz) const {
2494 sz->_cp_all_bytes += (sz->_cp_bytes = sz->count(this));
2495 sz->_cp_all_bytes += (sz->_cp_tags_bytes = sz->count_array(tags()));
2496 sz->_cp_all_bytes += (sz->_cp_cache_bytes = sz->count(cache()));
2497 sz->_cp_all_bytes += (sz->_cp_operands_bytes = sz->count_array(operands()));
2498 sz->_cp_all_bytes += (sz->_cp_refmap_bytes = sz->count_array(reference_map()));
2499
2500 sz->_ro_bytes += sz->_cp_operands_bytes + sz->_cp_tags_bytes +
2501 sz->_cp_refmap_bytes;
2502 sz->_rw_bytes += sz->_cp_bytes + sz->_cp_cache_bytes;
2503 }
2504 #endif // INCLUDE_SERVICES
2505
2506 // Verification
2507
2508 void ConstantPool::verify_on(outputStream* st) {
2509 guarantee(is_constantPool(), "object must be constant pool");
2510 for (int i = 0; i< length(); i++) {
2511 constantTag tag = tag_at(i);
2512 if (tag.is_klass() || tag.is_unresolved_klass()) {
2513 guarantee(klass_name_at(i)->refcount() != 0, "should have nonzero reference count");
2514 } else if (tag.is_symbol()) {
2515 CPSlot entry = slot_at(i);
2516 guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count");
2517 } else if (tag.is_string()) {
2518 CPSlot entry = slot_at(i);
2519 guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count");
2520 }
2521 }
2522 if (cache() != NULL) {
2523 // Note: cache() can be NULL before a class is completely setup or
2524 // in temporary constant pools used during constant pool merging
2525 guarantee(cache()->is_constantPoolCache(), "should be constant pool cache");
2526 }
2527 if (pool_holder() != NULL) {
2528 // Note: pool_holder() can be NULL in temporary constant pools
2529 // used during constant pool merging
2530 guarantee(pool_holder()->is_klass(), "should be klass");
2531 }
2532 }
|