34 #include "classfile/systemDictionary.hpp"
35 #include "classfile/systemDictionaryShared.hpp"
36 #include "classfile/vmClasses.hpp"
37 #include "classfile/vmSymbols.hpp"
38 #include "code/codeCache.hpp"
39 #include "interpreter/bootstrapInfo.hpp"
40 #include "interpreter/linkResolver.hpp"
41 #include "jvm.h"
42 #include "logging/log.hpp"
43 #include "logging/logStream.hpp"
44 #include "memory/allocation.inline.hpp"
45 #include "memory/metadataFactory.hpp"
46 #include "memory/metaspaceClosure.hpp"
47 #include "memory/oopFactory.hpp"
48 #include "memory/resourceArea.hpp"
49 #include "memory/universe.hpp"
50 #include "oops/array.hpp"
51 #include "oops/constantPool.inline.hpp"
52 #include "oops/cpCache.inline.hpp"
53 #include "oops/fieldStreams.inline.hpp"
54 #include "oops/instanceKlass.hpp"
55 #include "oops/klass.inline.hpp"
56 #include "oops/objArrayKlass.hpp"
57 #include "oops/objArrayOop.inline.hpp"
58 #include "oops/oop.inline.hpp"
59 #include "oops/typeArrayOop.inline.hpp"
60 #include "prims/jvmtiExport.hpp"
61 #include "runtime/atomicAccess.hpp"
62 #include "runtime/fieldDescriptor.inline.hpp"
63 #include "runtime/handles.inline.hpp"
64 #include "runtime/init.hpp"
65 #include "runtime/javaCalls.hpp"
66 #include "runtime/javaThread.inline.hpp"
67 #include "runtime/perfData.hpp"
68 #include "runtime/signature.hpp"
69 #include "runtime/vframe.inline.hpp"
70 #include "utilities/checkedCast.hpp"
71 #include "utilities/copy.hpp"
72
73 ConstantPool* ConstantPool::allocate(ClassLoaderData* loader_data, int length, TRAPS) {
74 Array<u1>* tags = MetadataFactory::new_array<u1>(loader_data, length, 0, CHECK_NULL);
75 int size = ConstantPool::size(length);
76 return new (loader_data, size, MetaspaceObj::ConstantPoolType, THREAD) ConstantPool(tags);
77 }
78
172
173 // Called from outside constant pool resolution where a resolved_reference array
174 // may not be present.
175 objArrayOop ConstantPool::resolved_references_or_null() const {
176 if (_cache == nullptr) {
177 return nullptr;
178 } else {
179 return _cache->resolved_references();
180 }
181 }
182
183 oop ConstantPool::resolved_reference_at(int index) const {
184 oop result = resolved_references()->obj_at(index);
185 assert(oopDesc::is_oop_or_null(result), "Must be oop");
186 return result;
187 }
188
189 // Use a CAS for multithreaded access
190 oop ConstantPool::set_resolved_reference_at(int index, oop new_result) {
191 assert(oopDesc::is_oop_or_null(new_result), "Must be oop");
192 return resolved_references()->replace_if_null(index, new_result);
193 }
194
195 // Create resolved_references array and mapping array for original cp indexes
196 // The ldc bytecode was rewritten to have the resolved reference array index so need a way
197 // to map it back for resolving and some unlikely miscellaneous uses.
198 // The objects created by invokedynamic are appended to this list.
199 void ConstantPool::initialize_resolved_references(ClassLoaderData* loader_data,
200 const intStack& reference_map,
201 int constant_pool_map_length,
202 TRAPS) {
203 // Initialized the resolved object cache.
204 int map_length = reference_map.length();
205 if (map_length > 0) {
206 // Only need mapping back to constant pool entries. The map isn't used for
207 // invokedynamic resolved_reference entries. For invokedynamic entries,
208 // the constant pool cache index has the mapping back to both the constant
209 // pool and to the resolved reference index.
210 if (constant_pool_map_length > 0) {
211 Array<u2>* om = MetadataFactory::new_array<u2>(loader_data, constant_pool_map_length, CHECK);
212
243 assert(resolved_klasses() == nullptr, "sanity");
244 Array<Klass*>* rk = MetadataFactory::new_array<Klass*>(loader_data, num_klasses, CHECK);
245 set_resolved_klasses(rk);
246 }
247
248 void ConstantPool::initialize_unresolved_klasses(ClassLoaderData* loader_data, TRAPS) {
249 int len = length();
250 int num_klasses = 0;
251 for (int i = 1; i <len; i++) {
252 switch (tag_at(i).value()) {
253 case JVM_CONSTANT_ClassIndex:
254 {
255 const int class_index = klass_index_at(i);
256 unresolved_klass_at_put(i, class_index, num_klasses++);
257 }
258 break;
259 #ifndef PRODUCT
260 case JVM_CONSTANT_Class:
261 case JVM_CONSTANT_UnresolvedClass:
262 case JVM_CONSTANT_UnresolvedClassInError:
263 // All of these should have been reverted back to ClassIndex before calling
264 // this function.
265 ShouldNotReachHere();
266 #endif
267 }
268 }
269 allocate_resolved_klasses(loader_data, num_klasses, THREAD);
270 }
271
272 // Hidden class support:
273 void ConstantPool::klass_at_put(int class_index, Klass* k) {
274 assert(k != nullptr, "must be valid klass");
275 CPKlassSlot kslot = klass_slot_at(class_index);
276 int resolved_klass_index = kslot.resolved_klass_index();
277 Klass** adr = resolved_klasses()->adr_at(resolved_klass_index);
278 AtomicAccess::release_store(adr, k);
279
280 // The interpreter assumes when the tag is stored, the klass is resolved
281 // and the Klass* non-null, so we need hardware store ordering here.
282 release_tag_at_put(class_index, JVM_CONSTANT_Class);
283 }
459 if (update_resolved_reference && cache() != nullptr) {
460 set_resolved_reference_length(
461 resolved_references() != nullptr ? resolved_references()->length() : 0);
462 set_resolved_references(OopHandle());
463 }
464 remove_unshareable_entries();
465 }
466
467 static const char* get_type(Klass* k) {
468 const char* type;
469 Klass* src_k;
470 if (ArchiveBuilder::is_active() && ArchiveBuilder::current()->is_in_buffer_space(k)) {
471 src_k = ArchiveBuilder::current()->get_source_addr(k);
472 } else {
473 src_k = k;
474 }
475
476 if (src_k->is_objArray_klass()) {
477 src_k = ObjArrayKlass::cast(src_k)->bottom_klass();
478 assert(!src_k->is_objArray_klass(), "sanity");
479 }
480
481 if (src_k->is_typeArray_klass()) {
482 type = "prim";
483 } else {
484 InstanceKlass* src_ik = InstanceKlass::cast(src_k);
485 if (src_ik->defined_by_boot_loader()) {
486 return "boot";
487 } else if (src_ik->defined_by_platform_loader()) {
488 return "plat";
489 } else if (src_ik->defined_by_app_loader()) {
490 return "app";
491 } else {
492 return "unreg";
493 }
494 }
495
496 return type;
497 }
498
658
659 HandleMark hm(THREAD);
660 Handle mirror_handle;
661 Symbol* name = this_cp->symbol_at(name_index);
662 Handle loader (THREAD, this_cp->pool_holder()->class_loader());
663
664 Klass* k;
665 {
666 // Turn off the single stepping while doing class resolution
667 JvmtiHideSingleStepping jhss(javaThread);
668 k = SystemDictionary::resolve_or_fail(name, loader, true, THREAD);
669 } // JvmtiHideSingleStepping jhss(javaThread);
670
671 if (!HAS_PENDING_EXCEPTION) {
672 // preserve the resolved klass from unloading
673 mirror_handle = Handle(THREAD, k->java_mirror());
674 // Do access check for klasses
675 verify_constant_pool_resolve(this_cp, k, THREAD);
676 }
677
678 // Failed to resolve class. We must record the errors so that subsequent attempts
679 // to resolve this constant pool entry fail with the same error (JVMS 5.4.3).
680 if (HAS_PENDING_EXCEPTION) {
681 save_and_throw_exception(this_cp, cp_index, constantTag(JVM_CONSTANT_UnresolvedClass), CHECK_NULL);
682 // If CHECK_NULL above doesn't return the exception, that means that
683 // some other thread has beaten us and has resolved the class.
684 // To preserve old behavior, we return the resolved class.
685 Klass* klass = this_cp->resolved_klasses()->at(resolved_klass_index);
686 assert(klass != nullptr, "must be resolved if exception was cleared");
687 return klass;
688 }
689
690 // logging for class+resolve.
691 if (log_is_enabled(Debug, class, resolve)){
692 trace_class_resolution(this_cp, k);
693 }
694
695 // The interpreter assumes when the tag is stored, the klass is resolved
696 // and the Klass* stored in _resolved_klasses is non-null, so we need
697 // hardware store ordering here.
|
34 #include "classfile/systemDictionary.hpp"
35 #include "classfile/systemDictionaryShared.hpp"
36 #include "classfile/vmClasses.hpp"
37 #include "classfile/vmSymbols.hpp"
38 #include "code/codeCache.hpp"
39 #include "interpreter/bootstrapInfo.hpp"
40 #include "interpreter/linkResolver.hpp"
41 #include "jvm.h"
42 #include "logging/log.hpp"
43 #include "logging/logStream.hpp"
44 #include "memory/allocation.inline.hpp"
45 #include "memory/metadataFactory.hpp"
46 #include "memory/metaspaceClosure.hpp"
47 #include "memory/oopFactory.hpp"
48 #include "memory/resourceArea.hpp"
49 #include "memory/universe.hpp"
50 #include "oops/array.hpp"
51 #include "oops/constantPool.inline.hpp"
52 #include "oops/cpCache.inline.hpp"
53 #include "oops/fieldStreams.inline.hpp"
54 #include "oops/flatArrayKlass.hpp"
55 #include "oops/instanceKlass.hpp"
56 #include "oops/klass.inline.hpp"
57 #include "oops/objArrayKlass.hpp"
58 #include "oops/objArrayOop.inline.hpp"
59 #include "oops/oop.inline.hpp"
60 #include "oops/refArrayOop.hpp"
61 #include "oops/typeArrayOop.inline.hpp"
62 #include "prims/jvmtiExport.hpp"
63 #include "runtime/atomicAccess.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) {
76 Array<u1>* tags = MetadataFactory::new_array<u1>(loader_data, length, 0, CHECK_NULL);
77 int size = ConstantPool::size(length);
78 return new (loader_data, size, MetaspaceObj::ConstantPoolType, THREAD) ConstantPool(tags);
79 }
80
174
175 // Called from outside constant pool resolution where a resolved_reference array
176 // may not be present.
177 objArrayOop ConstantPool::resolved_references_or_null() const {
178 if (_cache == nullptr) {
179 return nullptr;
180 } else {
181 return _cache->resolved_references();
182 }
183 }
184
185 oop ConstantPool::resolved_reference_at(int index) const {
186 oop result = resolved_references()->obj_at(index);
187 assert(oopDesc::is_oop_or_null(result), "Must be oop");
188 return result;
189 }
190
191 // Use a CAS for multithreaded access
192 oop ConstantPool::set_resolved_reference_at(int index, oop new_result) {
193 assert(oopDesc::is_oop_or_null(new_result), "Must be oop");
194 return refArrayOopDesc::cast(resolved_references())->replace_if_null(index, new_result);
195 }
196
197 // Create resolved_references array and mapping array for original cp indexes
198 // The ldc bytecode was rewritten to have the resolved reference array index so need a way
199 // to map it back for resolving and some unlikely miscellaneous uses.
200 // The objects created by invokedynamic are appended to this list.
201 void ConstantPool::initialize_resolved_references(ClassLoaderData* loader_data,
202 const intStack& reference_map,
203 int constant_pool_map_length,
204 TRAPS) {
205 // Initialized the resolved object cache.
206 int map_length = reference_map.length();
207 if (map_length > 0) {
208 // Only need mapping back to constant pool entries. The map isn't used for
209 // invokedynamic resolved_reference entries. For invokedynamic entries,
210 // the constant pool cache index has the mapping back to both the constant
211 // pool and to the resolved reference index.
212 if (constant_pool_map_length > 0) {
213 Array<u2>* om = MetadataFactory::new_array<u2>(loader_data, constant_pool_map_length, CHECK);
214
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 Unresolved 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 AtomicAccess::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 }
461 if (update_resolved_reference && cache() != nullptr) {
462 set_resolved_reference_length(
463 resolved_references() != nullptr ? resolved_references()->length() : 0);
464 set_resolved_references(OopHandle());
465 }
466 remove_unshareable_entries();
467 }
468
469 static const char* get_type(Klass* k) {
470 const char* type;
471 Klass* src_k;
472 if (ArchiveBuilder::is_active() && ArchiveBuilder::current()->is_in_buffer_space(k)) {
473 src_k = ArchiveBuilder::current()->get_source_addr(k);
474 } else {
475 src_k = k;
476 }
477
478 if (src_k->is_objArray_klass()) {
479 src_k = ObjArrayKlass::cast(src_k)->bottom_klass();
480 assert(!src_k->is_objArray_klass(), "sanity");
481 assert(src_k->is_instance_klass() || src_k->is_typeArray_klass(), "Sanity check");
482 }
483
484 if (src_k->is_typeArray_klass()) {
485 type = "prim";
486 } else {
487 InstanceKlass* src_ik = InstanceKlass::cast(src_k);
488 if (src_ik->defined_by_boot_loader()) {
489 return "boot";
490 } else if (src_ik->defined_by_platform_loader()) {
491 return "plat";
492 } else if (src_ik->defined_by_app_loader()) {
493 return "app";
494 } else {
495 return "unreg";
496 }
497 }
498
499 return type;
500 }
501
661
662 HandleMark hm(THREAD);
663 Handle mirror_handle;
664 Symbol* name = this_cp->symbol_at(name_index);
665 Handle loader (THREAD, this_cp->pool_holder()->class_loader());
666
667 Klass* k;
668 {
669 // Turn off the single stepping while doing class resolution
670 JvmtiHideSingleStepping jhss(javaThread);
671 k = SystemDictionary::resolve_or_fail(name, loader, true, THREAD);
672 } // JvmtiHideSingleStepping jhss(javaThread);
673
674 if (!HAS_PENDING_EXCEPTION) {
675 // preserve the resolved klass from unloading
676 mirror_handle = Handle(THREAD, k->java_mirror());
677 // Do access check for klasses
678 verify_constant_pool_resolve(this_cp, k, THREAD);
679 }
680
681 #ifdef DEBUG
682 if (!HAS_PENDING_EXCEPTION && k->is_objArray_klass()) {
683 Klass* bottom_klass = ObjArrayKlass::cast(k)->bottom_klass();
684 assert(bottom_klass != nullptr, "Should be set");
685 assert(bottom_klass->is_instance_klass() || bottom_klass->is_typeArray_klass(), "Sanity check");
686 }
687 #endif
688
689 // Failed to resolve class. We must record the errors so that subsequent attempts
690 // to resolve this constant pool entry fail with the same error (JVMS 5.4.3).
691 if (HAS_PENDING_EXCEPTION) {
692 save_and_throw_exception(this_cp, cp_index, constantTag(JVM_CONSTANT_UnresolvedClass), CHECK_NULL);
693 // If CHECK_NULL above doesn't return the exception, that means that
694 // some other thread has beaten us and has resolved the class.
695 // To preserve old behavior, we return the resolved class.
696 Klass* klass = this_cp->resolved_klasses()->at(resolved_klass_index);
697 assert(klass != nullptr, "must be resolved if exception was cleared");
698 return klass;
699 }
700
701 // logging for class+resolve.
702 if (log_is_enabled(Debug, class, resolve)){
703 trace_class_resolution(this_cp, k);
704 }
705
706 // The interpreter assumes when the tag is stored, the klass is resolved
707 // and the Klass* stored in _resolved_klasses is non-null, so we need
708 // hardware store ordering here.
|