36 #include "classfile/systemDictionary.hpp"
37 #include "classfile/systemDictionaryShared.hpp"
38 #include "classfile/vmClasses.hpp"
39 #include "classfile/vmSymbols.hpp"
40 #include "code/codeCache.hpp"
41 #include "interpreter/bootstrapInfo.hpp"
42 #include "interpreter/linkResolver.hpp"
43 #include "jvm.h"
44 #include "logging/log.hpp"
45 #include "logging/logStream.hpp"
46 #include "memory/allocation.inline.hpp"
47 #include "memory/metadataFactory.hpp"
48 #include "memory/metaspaceClosure.hpp"
49 #include "memory/oopFactory.hpp"
50 #include "memory/resourceArea.hpp"
51 #include "memory/universe.hpp"
52 #include "oops/array.hpp"
53 #include "oops/constantPool.inline.hpp"
54 #include "oops/cpCache.inline.hpp"
55 #include "oops/fieldStreams.inline.hpp"
56 #include "oops/instanceKlass.hpp"
57 #include "oops/klass.inline.hpp"
58 #include "oops/objArrayKlass.hpp"
59 #include "oops/objArrayOop.inline.hpp"
60 #include "oops/oop.inline.hpp"
61 #include "oops/typeArrayOop.inline.hpp"
62 #include "prims/jvmtiExport.hpp"
63 #include "runtime/atomic.hpp"
64 #include "runtime/fieldDescriptor.inline.hpp"
65 #include "runtime/handles.inline.hpp"
66 #include "runtime/init.hpp"
67 #include "runtime/javaCalls.hpp"
68 #include "runtime/javaThread.inline.hpp"
69 #include "runtime/perfData.hpp"
70 #include "runtime/signature.hpp"
71 #include "runtime/vframe.inline.hpp"
72 #include "utilities/checkedCast.hpp"
73 #include "utilities/copy.hpp"
74
75 ConstantPool* ConstantPool::allocate(ClassLoaderData* loader_data, int length, TRAPS) {
245 assert(resolved_klasses() == nullptr, "sanity");
246 Array<Klass*>* rk = MetadataFactory::new_array<Klass*>(loader_data, num_klasses, CHECK);
247 set_resolved_klasses(rk);
248 }
249
250 void ConstantPool::initialize_unresolved_klasses(ClassLoaderData* loader_data, TRAPS) {
251 int len = length();
252 int num_klasses = 0;
253 for (int i = 1; i <len; i++) {
254 switch (tag_at(i).value()) {
255 case JVM_CONSTANT_ClassIndex:
256 {
257 const int class_index = klass_index_at(i);
258 unresolved_klass_at_put(i, class_index, num_klasses++);
259 }
260 break;
261 #ifndef PRODUCT
262 case JVM_CONSTANT_Class:
263 case JVM_CONSTANT_UnresolvedClass:
264 case JVM_CONSTANT_UnresolvedClassInError:
265 // All of these should have been reverted back to ClassIndex before calling
266 // this function.
267 ShouldNotReachHere();
268 #endif
269 }
270 }
271 allocate_resolved_klasses(loader_data, num_klasses, THREAD);
272 }
273
274 // Hidden class support:
275 void ConstantPool::klass_at_put(int class_index, Klass* k) {
276 assert(k != nullptr, "must be valid klass");
277 CPKlassSlot kslot = klass_slot_at(class_index);
278 int resolved_klass_index = kslot.resolved_klass_index();
279 Klass** adr = resolved_klasses()->adr_at(resolved_klass_index);
280 Atomic::release_store(adr, k);
281
282 // The interpreter assumes when the tag is stored, the klass is resolved
283 // and the Klass* non-null, so we need hardware store ordering here.
284 release_tag_at_put(class_index, JVM_CONSTANT_Class);
285 }
602 Symbol* s = vfst.method()->method_holder()->source_file_name();
603 if (s != nullptr) {
604 source_file = s->as_C_string();
605 }
606 }
607 }
608 if (k != this_cp->pool_holder()) {
609 // only print something if the classes are different
610 if (source_file != nullptr) {
611 log_debug(class, resolve)("%s %s %s:%d",
612 this_cp->pool_holder()->external_name(),
613 k->external_name(), source_file, line_number);
614 } else {
615 log_debug(class, resolve)("%s %s",
616 this_cp->pool_holder()->external_name(),
617 k->external_name());
618 }
619 }
620 }
621
622 Klass* ConstantPool::klass_at_impl(const constantPoolHandle& this_cp, int cp_index,
623 TRAPS) {
624 JavaThread* javaThread = THREAD;
625
626 // A resolved constantPool entry will contain a Klass*, otherwise a Symbol*.
627 // It is not safe to rely on the tag bit's here, since we don't have a lock, and
628 // the entry and tag is not updated atomically.
629 CPKlassSlot kslot = this_cp->klass_slot_at(cp_index);
630 int resolved_klass_index = kslot.resolved_klass_index();
631 int name_index = kslot.name_index();
632 assert(this_cp->tag_at(name_index).is_symbol(), "sanity");
633
634 // The tag must be JVM_CONSTANT_Class in order to read the correct value from
635 // the unresolved_klasses() array.
636 if (this_cp->tag_at(cp_index).is_klass()) {
637 Klass* klass = this_cp->resolved_klasses()->at(resolved_klass_index);
638 assert(klass != nullptr, "must be resolved");
639 return klass;
640 }
641
642 // This tag doesn't change back to unresolved class unless at a safepoint.
643 if (this_cp->tag_at(cp_index).is_unresolved_klass_in_error()) {
644 // The original attempt to resolve this constant pool entry failed so find the
645 // class of the original error and throw another error of the same class
646 // (JVMS 5.4.3).
647 // If there is a detail message, pass that detail message to the error.
648 // The JVMS does not strictly require us to duplicate the same detail message,
649 // or any internal exception fields such as cause or stacktrace. But since the
650 // detail message is often a class name or other literal string, we will repeat it
651 // if we can find it in the symbol table.
652 throw_resolution_error(this_cp, cp_index, CHECK_NULL);
653 ShouldNotReachHere();
654 }
655
656 HandleMark hm(THREAD);
657 Handle mirror_handle;
658 Symbol* name = this_cp->symbol_at(name_index);
659 Handle loader (THREAD, this_cp->pool_holder()->class_loader());
660
661 Klass* k;
662 {
663 // Turn off the single stepping while doing class resolution
664 JvmtiHideSingleStepping jhss(javaThread);
665 k = SystemDictionary::resolve_or_fail(name, loader, true, THREAD);
666 } // JvmtiHideSingleStepping jhss(javaThread);
667
668 if (!HAS_PENDING_EXCEPTION) {
669 // preserve the resolved klass from unloading
670 mirror_handle = Handle(THREAD, k->java_mirror());
671 // Do access check for klasses
672 verify_constant_pool_resolve(this_cp, k, THREAD);
673 }
674
675 // Failed to resolve class. We must record the errors so that subsequent attempts
676 // to resolve this constant pool entry fail with the same error (JVMS 5.4.3).
677 if (HAS_PENDING_EXCEPTION) {
678 save_and_throw_exception(this_cp, cp_index, constantTag(JVM_CONSTANT_UnresolvedClass), CHECK_NULL);
679 // If CHECK_NULL above doesn't return the exception, that means that
680 // some other thread has beaten us and has resolved the class.
681 // To preserve old behavior, we return the resolved class.
682 Klass* klass = this_cp->resolved_klasses()->at(resolved_klass_index);
683 assert(klass != nullptr, "must be resolved if exception was cleared");
684 return klass;
685 }
686
687 // logging for class+resolve.
688 if (log_is_enabled(Debug, class, resolve)){
689 trace_class_resolution(this_cp, k);
690 }
691
692 // The interpreter assumes when the tag is stored, the klass is resolved
693 // and the Klass* stored in _resolved_klasses is non-null, so we need
694 // hardware store ordering here.
|
36 #include "classfile/systemDictionary.hpp"
37 #include "classfile/systemDictionaryShared.hpp"
38 #include "classfile/vmClasses.hpp"
39 #include "classfile/vmSymbols.hpp"
40 #include "code/codeCache.hpp"
41 #include "interpreter/bootstrapInfo.hpp"
42 #include "interpreter/linkResolver.hpp"
43 #include "jvm.h"
44 #include "logging/log.hpp"
45 #include "logging/logStream.hpp"
46 #include "memory/allocation.inline.hpp"
47 #include "memory/metadataFactory.hpp"
48 #include "memory/metaspaceClosure.hpp"
49 #include "memory/oopFactory.hpp"
50 #include "memory/resourceArea.hpp"
51 #include "memory/universe.hpp"
52 #include "oops/array.hpp"
53 #include "oops/constantPool.inline.hpp"
54 #include "oops/cpCache.inline.hpp"
55 #include "oops/fieldStreams.inline.hpp"
56 #include "oops/flatArrayKlass.hpp"
57 #include "oops/instanceKlass.hpp"
58 #include "oops/klass.inline.hpp"
59 #include "oops/objArrayKlass.hpp"
60 #include "oops/objArrayOop.inline.hpp"
61 #include "oops/oop.inline.hpp"
62 #include "oops/typeArrayOop.inline.hpp"
63 #include "prims/jvmtiExport.hpp"
64 #include "runtime/atomic.hpp"
65 #include "runtime/fieldDescriptor.inline.hpp"
66 #include "runtime/handles.inline.hpp"
67 #include "runtime/init.hpp"
68 #include "runtime/javaCalls.hpp"
69 #include "runtime/javaThread.inline.hpp"
70 #include "runtime/perfData.hpp"
71 #include "runtime/signature.hpp"
72 #include "runtime/vframe.inline.hpp"
73 #include "utilities/checkedCast.hpp"
74 #include "utilities/copy.hpp"
75
76 ConstantPool* ConstantPool::allocate(ClassLoaderData* loader_data, int length, TRAPS) {
246 assert(resolved_klasses() == nullptr, "sanity");
247 Array<Klass*>* rk = MetadataFactory::new_array<Klass*>(loader_data, num_klasses, CHECK);
248 set_resolved_klasses(rk);
249 }
250
251 void ConstantPool::initialize_unresolved_klasses(ClassLoaderData* loader_data, TRAPS) {
252 int len = length();
253 int num_klasses = 0;
254 for (int i = 1; i <len; i++) {
255 switch (tag_at(i).value()) {
256 case JVM_CONSTANT_ClassIndex:
257 {
258 const int class_index = klass_index_at(i);
259 unresolved_klass_at_put(i, class_index, num_klasses++);
260 }
261 break;
262 #ifndef PRODUCT
263 case JVM_CONSTANT_Class:
264 case JVM_CONSTANT_UnresolvedClass:
265 case JVM_CONSTANT_UnresolvedClassInError:
266 // All of these should have been reverted back to Unresolved before calling
267 // this function.
268 ShouldNotReachHere();
269 #endif
270 }
271 }
272 allocate_resolved_klasses(loader_data, num_klasses, THREAD);
273 }
274
275 // Hidden class support:
276 void ConstantPool::klass_at_put(int class_index, Klass* k) {
277 assert(k != nullptr, "must be valid klass");
278 CPKlassSlot kslot = klass_slot_at(class_index);
279 int resolved_klass_index = kslot.resolved_klass_index();
280 Klass** adr = resolved_klasses()->adr_at(resolved_klass_index);
281 Atomic::release_store(adr, k);
282
283 // The interpreter assumes when the tag is stored, the klass is resolved
284 // and the Klass* non-null, so we need hardware store ordering here.
285 release_tag_at_put(class_index, JVM_CONSTANT_Class);
286 }
603 Symbol* s = vfst.method()->method_holder()->source_file_name();
604 if (s != nullptr) {
605 source_file = s->as_C_string();
606 }
607 }
608 }
609 if (k != this_cp->pool_holder()) {
610 // only print something if the classes are different
611 if (source_file != nullptr) {
612 log_debug(class, resolve)("%s %s %s:%d",
613 this_cp->pool_holder()->external_name(),
614 k->external_name(), source_file, line_number);
615 } else {
616 log_debug(class, resolve)("%s %s",
617 this_cp->pool_holder()->external_name(),
618 k->external_name());
619 }
620 }
621 }
622
623 void check_is_inline_type(Klass* k, TRAPS) {
624 if (!k->is_inline_klass()) {
625 THROW(vmSymbols::java_lang_IncompatibleClassChangeError());
626 }
627 }
628
629 Klass* ConstantPool::klass_at_impl(const constantPoolHandle& this_cp, int cp_index,
630 TRAPS) {
631 JavaThread* javaThread = THREAD;
632
633 // A resolved constantPool entry will contain a Klass*, otherwise a Symbol*.
634 // It is not safe to rely on the tag bit's here, since we don't have a lock, and
635 // the entry and tag is not updated atomically.
636 CPKlassSlot kslot = this_cp->klass_slot_at(cp_index);
637 int resolved_klass_index = kslot.resolved_klass_index();
638 int name_index = kslot.name_index();
639 assert(this_cp->tag_at(name_index).is_symbol(), "sanity");
640
641 // The tag must be JVM_CONSTANT_Class in order to read the correct value from
642 // the unresolved_klasses() array.
643 if (this_cp->tag_at(cp_index).is_klass()) {
644 Klass* klass = this_cp->resolved_klasses()->at(resolved_klass_index);
645 assert(klass != nullptr, "must be resolved");
646 return klass;
647 }
648
649 // This tag doesn't change back to unresolved class unless at a safepoint.
650 if (this_cp->tag_at(cp_index).is_unresolved_klass_in_error()) {
651 // The original attempt to resolve this constant pool entry failed so find the
652 // class of the original error and throw another error of the same class
653 // (JVMS 5.4.3).
654 // If there is a detail message, pass that detail message to the error.
655 // The JVMS does not strictly require us to duplicate the same detail message,
656 // or any internal exception fields such as cause or stacktrace. But since the
657 // detail message is often a class name or other literal string, we will repeat it
658 // if we can find it in the symbol table.
659 throw_resolution_error(this_cp, cp_index, CHECK_NULL);
660 ShouldNotReachHere();
661 }
662
663 HandleMark hm(THREAD);
664 Handle mirror_handle;
665 Symbol* name = this_cp->symbol_at(name_index);
666 bool inline_type_signature = false;
667 Handle loader (THREAD, this_cp->pool_holder()->class_loader());
668
669 Klass* k;
670 {
671 // Turn off the single stepping while doing class resolution
672 JvmtiHideSingleStepping jhss(javaThread);
673 k = SystemDictionary::resolve_or_fail(name, loader, true, THREAD);
674 } // JvmtiHideSingleStepping jhss(javaThread);
675 if (inline_type_signature) {
676 name->decrement_refcount();
677 }
678
679 if (!HAS_PENDING_EXCEPTION) {
680 // preserve the resolved klass from unloading
681 mirror_handle = Handle(THREAD, k->java_mirror());
682 // Do access check for klasses
683 verify_constant_pool_resolve(this_cp, k, THREAD);
684 }
685
686 if (!HAS_PENDING_EXCEPTION && inline_type_signature) {
687 check_is_inline_type(k, THREAD);
688 }
689
690 if (!HAS_PENDING_EXCEPTION) {
691 Klass* bottom_klass = nullptr;
692 if (k->is_objArray_klass()) {
693 bottom_klass = ObjArrayKlass::cast(k)->bottom_klass();
694 assert(bottom_klass != nullptr, "Should be set");
695 assert(bottom_klass->is_instance_klass() || bottom_klass->is_typeArray_klass(), "Sanity check");
696 } else if (k->is_flatArray_klass()) {
697 bottom_klass = FlatArrayKlass::cast(k)->element_klass();
698 assert(bottom_klass != nullptr, "Should be set");
699 }
700 }
701
702 // Failed to resolve class. We must record the errors so that subsequent attempts
703 // to resolve this constant pool entry fail with the same error (JVMS 5.4.3).
704 if (HAS_PENDING_EXCEPTION) {
705 save_and_throw_exception(this_cp, cp_index, constantTag(JVM_CONSTANT_UnresolvedClass), CHECK_NULL);
706 // If CHECK_NULL above doesn't return the exception, that means that
707 // some other thread has beaten us and has resolved the class.
708 // To preserve old behavior, we return the resolved class.
709 Klass* klass = this_cp->resolved_klasses()->at(resolved_klass_index);
710 assert(klass != nullptr, "must be resolved if exception was cleared");
711 return klass;
712 }
713
714 // logging for class+resolve.
715 if (log_is_enabled(Debug, class, resolve)){
716 trace_class_resolution(this_cp, k);
717 }
718
719 // The interpreter assumes when the tag is stored, the klass is resolved
720 // and the Klass* stored in _resolved_klasses is non-null, so we need
721 // hardware store ordering here.
|