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 }
637 Symbol* s = vfst.method()->method_holder()->source_file_name();
638 if (s != nullptr) {
639 source_file = s->as_C_string();
640 }
641 }
642 }
643 if (k != this_cp->pool_holder()) {
644 // only print something if the classes are different
645 if (source_file != nullptr) {
646 log_debug(class, resolve)("%s %s %s:%d",
647 this_cp->pool_holder()->external_name(),
648 k->external_name(), source_file, line_number);
649 } else {
650 log_debug(class, resolve)("%s %s",
651 this_cp->pool_holder()->external_name(),
652 k->external_name());
653 }
654 }
655 }
656
657 Klass* ConstantPool::klass_at_impl(const constantPoolHandle& this_cp, int cp_index,
658 TRAPS) {
659 JavaThread* javaThread = THREAD;
660
661 // A resolved constantPool entry will contain a Klass*, otherwise a Symbol*.
662 // It is not safe to rely on the tag bit's here, since we don't have a lock, and
663 // the entry and tag is not updated atomically.
664 CPKlassSlot kslot = this_cp->klass_slot_at(cp_index);
665 int resolved_klass_index = kslot.resolved_klass_index();
666 int name_index = kslot.name_index();
667 assert(this_cp->tag_at(name_index).is_symbol(), "sanity");
668
669 // The tag must be JVM_CONSTANT_Class in order to read the correct value from
670 // the unresolved_klasses() array.
671 if (this_cp->tag_at(cp_index).is_klass()) {
672 Klass* klass = this_cp->resolved_klasses()->at(resolved_klass_index);
673 assert(klass != nullptr, "must be resolved");
674 return klass;
675 }
676
677 // This tag doesn't change back to unresolved class unless at a safepoint.
678 if (this_cp->tag_at(cp_index).is_unresolved_klass_in_error()) {
679 // The original attempt to resolve this constant pool entry failed so find the
680 // class of the original error and throw another error of the same class
681 // (JVMS 5.4.3).
682 // If there is a detail message, pass that detail message to the error.
683 // The JVMS does not strictly require us to duplicate the same detail message,
684 // or any internal exception fields such as cause or stacktrace. But since the
685 // detail message is often a class name or other literal string, we will repeat it
686 // if we can find it in the symbol table.
687 throw_resolution_error(this_cp, cp_index, CHECK_NULL);
688 ShouldNotReachHere();
689 }
690
691 HandleMark hm(THREAD);
692 Handle mirror_handle;
693 Symbol* name = this_cp->symbol_at(name_index);
694 Handle loader (THREAD, this_cp->pool_holder()->class_loader());
695
696 Klass* k;
697 {
698 // Turn off the single stepping while doing class resolution
699 JvmtiHideSingleStepping jhss(javaThread);
700 k = SystemDictionary::resolve_or_fail(name, loader, true, THREAD);
701 } // JvmtiHideSingleStepping jhss(javaThread);
702
703 if (!HAS_PENDING_EXCEPTION) {
704 // preserve the resolved klass from unloading
705 mirror_handle = Handle(THREAD, k->java_mirror());
706 // Do access check for klasses
707 verify_constant_pool_resolve(this_cp, k, THREAD);
708 }
709
710 // Failed to resolve class. We must record the errors so that subsequent attempts
711 // to resolve this constant pool entry fail with the same error (JVMS 5.4.3).
712 if (HAS_PENDING_EXCEPTION) {
713 save_and_throw_exception(this_cp, cp_index, constantTag(JVM_CONSTANT_UnresolvedClass), CHECK_NULL);
714 // If CHECK_NULL above doesn't return the exception, that means that
715 // some other thread has beaten us and has resolved the class.
716 // To preserve old behavior, we return the resolved class.
717 Klass* klass = this_cp->resolved_klasses()->at(resolved_klass_index);
718 assert(klass != nullptr, "must be resolved if exception was cleared");
719 return klass;
720 }
721
722 // logging for class+resolve.
723 if (log_is_enabled(Debug, class, resolve)){
724 trace_class_resolution(this_cp, k);
725 }
726
727 // The interpreter assumes when the tag is stored, the klass is resolved
728 // and the Klass* stored in _resolved_klasses is non-null, so we need
729 // 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 }
638 Symbol* s = vfst.method()->method_holder()->source_file_name();
639 if (s != nullptr) {
640 source_file = s->as_C_string();
641 }
642 }
643 }
644 if (k != this_cp->pool_holder()) {
645 // only print something if the classes are different
646 if (source_file != nullptr) {
647 log_debug(class, resolve)("%s %s %s:%d",
648 this_cp->pool_holder()->external_name(),
649 k->external_name(), source_file, line_number);
650 } else {
651 log_debug(class, resolve)("%s %s",
652 this_cp->pool_holder()->external_name(),
653 k->external_name());
654 }
655 }
656 }
657
658 void check_is_inline_type(Klass* k, TRAPS) {
659 if (!k->is_inline_klass()) {
660 THROW(vmSymbols::java_lang_IncompatibleClassChangeError());
661 }
662 }
663
664 Klass* ConstantPool::klass_at_impl(const constantPoolHandle& this_cp, int cp_index,
665 TRAPS) {
666 JavaThread* javaThread = THREAD;
667
668 // A resolved constantPool entry will contain a Klass*, otherwise a Symbol*.
669 // It is not safe to rely on the tag bit's here, since we don't have a lock, and
670 // the entry and tag is not updated atomically.
671 CPKlassSlot kslot = this_cp->klass_slot_at(cp_index);
672 int resolved_klass_index = kslot.resolved_klass_index();
673 int name_index = kslot.name_index();
674 assert(this_cp->tag_at(name_index).is_symbol(), "sanity");
675
676 // The tag must be JVM_CONSTANT_Class in order to read the correct value from
677 // the unresolved_klasses() array.
678 if (this_cp->tag_at(cp_index).is_klass()) {
679 Klass* klass = this_cp->resolved_klasses()->at(resolved_klass_index);
680 assert(klass != nullptr, "must be resolved");
681 return klass;
682 }
683
684 // This tag doesn't change back to unresolved class unless at a safepoint.
685 if (this_cp->tag_at(cp_index).is_unresolved_klass_in_error()) {
686 // The original attempt to resolve this constant pool entry failed so find the
687 // class of the original error and throw another error of the same class
688 // (JVMS 5.4.3).
689 // If there is a detail message, pass that detail message to the error.
690 // The JVMS does not strictly require us to duplicate the same detail message,
691 // or any internal exception fields such as cause or stacktrace. But since the
692 // detail message is often a class name or other literal string, we will repeat it
693 // if we can find it in the symbol table.
694 throw_resolution_error(this_cp, cp_index, CHECK_NULL);
695 ShouldNotReachHere();
696 }
697
698 HandleMark hm(THREAD);
699 Handle mirror_handle;
700 Symbol* name = this_cp->symbol_at(name_index);
701 bool inline_type_signature = false;
702 Handle loader (THREAD, this_cp->pool_holder()->class_loader());
703
704 Klass* k;
705 {
706 // Turn off the single stepping while doing class resolution
707 JvmtiHideSingleStepping jhss(javaThread);
708 k = SystemDictionary::resolve_or_fail(name, loader, true, THREAD);
709 } // JvmtiHideSingleStepping jhss(javaThread);
710 if (inline_type_signature) {
711 name->decrement_refcount();
712 }
713
714 if (!HAS_PENDING_EXCEPTION) {
715 // preserve the resolved klass from unloading
716 mirror_handle = Handle(THREAD, k->java_mirror());
717 // Do access check for klasses
718 verify_constant_pool_resolve(this_cp, k, THREAD);
719 }
720
721 if (!HAS_PENDING_EXCEPTION && inline_type_signature) {
722 check_is_inline_type(k, THREAD);
723 }
724
725 if (!HAS_PENDING_EXCEPTION) {
726 Klass* bottom_klass = nullptr;
727 if (k->is_objArray_klass()) {
728 bottom_klass = ObjArrayKlass::cast(k)->bottom_klass();
729 assert(bottom_klass != nullptr, "Should be set");
730 assert(bottom_klass->is_instance_klass() || bottom_klass->is_typeArray_klass(), "Sanity check");
731 } else if (k->is_flatArray_klass()) {
732 bottom_klass = FlatArrayKlass::cast(k)->element_klass();
733 assert(bottom_klass != nullptr, "Should be set");
734 }
735 }
736
737 // Failed to resolve class. We must record the errors so that subsequent attempts
738 // to resolve this constant pool entry fail with the same error (JVMS 5.4.3).
739 if (HAS_PENDING_EXCEPTION) {
740 save_and_throw_exception(this_cp, cp_index, constantTag(JVM_CONSTANT_UnresolvedClass), CHECK_NULL);
741 // If CHECK_NULL above doesn't return the exception, that means that
742 // some other thread has beaten us and has resolved the class.
743 // To preserve old behavior, we return the resolved class.
744 Klass* klass = this_cp->resolved_klasses()->at(resolved_klass_index);
745 assert(klass != nullptr, "must be resolved if exception was cleared");
746 return klass;
747 }
748
749 // logging for class+resolve.
750 if (log_is_enabled(Debug, class, resolve)){
751 trace_class_resolution(this_cp, k);
752 }
753
754 // The interpreter assumes when the tag is stored, the klass is resolved
755 // and the Klass* stored in _resolved_klasses is non-null, so we need
756 // hardware store ordering here.
|