30 #include "classfile/stringTable.hpp"
31 #include "classfile/systemDictionary.hpp"
32 #include "classfile/vmSymbols.hpp"
33 #include "interpreter/linkResolver.hpp"
34 #include "memory/allocation.inline.hpp"
35 #include "memory/heapInspection.hpp"
36 #include "memory/heapShared.hpp"
37 #include "memory/metadataFactory.hpp"
38 #include "memory/metaspaceClosure.hpp"
39 #include "memory/metaspaceShared.hpp"
40 #include "memory/oopFactory.hpp"
41 #include "memory/resourceArea.hpp"
42 #include "oops/array.hpp"
43 #include "oops/constantPool.inline.hpp"
44 #include "oops/cpCache.inline.hpp"
45 #include "oops/instanceKlass.hpp"
46 #include "oops/objArrayKlass.hpp"
47 #include "oops/objArrayOop.inline.hpp"
48 #include "oops/oop.inline.hpp"
49 #include "oops/typeArrayOop.inline.hpp"
50 #include "runtime/fieldType.hpp"
51 #include "runtime/handles.inline.hpp"
52 #include "runtime/init.hpp"
53 #include "runtime/javaCalls.hpp"
54 #include "runtime/signature.hpp"
55 #include "runtime/thread.inline.hpp"
56 #include "runtime/vframe.inline.hpp"
57 #include "utilities/copy.hpp"
58
59 ConstantPool* ConstantPool::allocate(ClassLoaderData* loader_data, int length, TRAPS) {
60 Array<u1>* tags = MetadataFactory::new_array<u1>(loader_data, length, 0, CHECK_NULL);
61 int size = ConstantPool::size(length);
62 return new (loader_data, size, MetaspaceObj::ConstantPoolType, THREAD) ConstantPool(tags);
63 }
64
65 #ifdef ASSERT
66
67 // MetaspaceObj allocation invariant is calloc equivalent memory
68 // simple verification of this here (JVM_CONSTANT_Invalid == 0 )
69 static bool tag_array_is_zero_initialized(Array<u1>* tags) {
193 assert(resolved_klasses() == NULL, "sanity");
194 Array<Klass*>* rk = MetadataFactory::new_array<Klass*>(loader_data, num_klasses, CHECK);
195 set_resolved_klasses(rk);
196 }
197
198 void ConstantPool::initialize_unresolved_klasses(ClassLoaderData* loader_data, TRAPS) {
199 int len = length();
200 int num_klasses = 0;
201 for (int i = 1; i <len; i++) {
202 switch (tag_at(i).value()) {
203 case JVM_CONSTANT_ClassIndex:
204 {
205 const int class_index = klass_index_at(i);
206 unresolved_klass_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 // All of these should have been reverted back to ClassIndex before calling
214 // this function.
215 ShouldNotReachHere();
216 #endif
217 }
218 }
219 allocate_resolved_klasses(loader_data, num_klasses, THREAD);
220 }
221
222 // Unsafe anonymous class support:
223 void ConstantPool::klass_at_put(int class_index, int name_index, int resolved_klass_index, Klass* k, Symbol* name) {
224 assert(is_within_bounds(class_index), "index out of bounds");
225 assert(is_within_bounds(name_index), "index out of bounds");
226 assert((resolved_klass_index & 0xffff0000) == 0, "must be");
227 *int_at_addr(class_index) =
228 build_int_from_shorts((jushort)resolved_klass_index, (jushort)name_index);
229
230 symbol_at_put(name_index, name);
231 name->increment_refcount();
232 Klass** adr = resolved_klasses()->adr_at(resolved_klass_index);
233 OrderAccess::release_store(adr, k);
234
235 // The interpreter assumes when the tag is stored, the klass is resolved
236 // and the Klass* non-NULL, so we need hardware store ordering here.
237 if (k != NULL) {
238 release_tag_at_put(class_index, JVM_CONSTANT_Class);
239 } else {
240 release_tag_at_put(class_index, JVM_CONSTANT_UnresolvedClass);
241 }
242 }
243
244 // Unsafe anonymous class support:
245 void ConstantPool::klass_at_put(int class_index, Klass* k) {
246 assert(k != NULL, "must be valid klass");
247 CPKlassSlot kslot = klass_slot_at(class_index);
248 int resolved_klass_index = kslot.resolved_klass_index();
249 Klass** adr = resolved_klasses()->adr_at(resolved_klass_index);
250 OrderAccess::release_store(adr, k);
251
252 // The interpreter assumes when the tag is stored, the klass is resolved
253 // and the Klass* non-NULL, so we need hardware store ordering here.
254 release_tag_at_put(class_index, JVM_CONSTANT_Class);
255 }
256
257 #if INCLUDE_CDS_JAVA_HEAP
258 // Archive the resolved references
259 void ConstantPool::archive_resolved_references(Thread* THREAD) {
260 if (_cache == NULL) {
425 Symbol* s = vfst.method()->method_holder()->source_file_name();
426 if (s != NULL) {
427 source_file = s->as_C_string();
428 }
429 }
430 }
431 if (k != this_cp->pool_holder()) {
432 // only print something if the classes are different
433 if (source_file != NULL) {
434 log_debug(class, resolve)("%s %s %s:%d",
435 this_cp->pool_holder()->external_name(),
436 k->external_name(), source_file, line_number);
437 } else {
438 log_debug(class, resolve)("%s %s",
439 this_cp->pool_holder()->external_name(),
440 k->external_name());
441 }
442 }
443 }
444
445 Klass* ConstantPool::klass_at_impl(const constantPoolHandle& this_cp, int which,
446 bool save_resolution_error, TRAPS) {
447 assert(THREAD->is_Java_thread(), "must be a Java thread");
448 JavaThread* javaThread = (JavaThread*)THREAD;
449
450 // A resolved constantPool entry will contain a Klass*, otherwise a Symbol*.
451 // It is not safe to rely on the tag bit's here, since we don't have a lock, and
452 // the entry and tag is not updated atomicly.
453 CPKlassSlot kslot = this_cp->klass_slot_at(which);
454 int resolved_klass_index = kslot.resolved_klass_index();
455 int name_index = kslot.name_index();
456 assert(this_cp->tag_at(name_index).is_symbol(), "sanity");
457
458 Klass* klass = this_cp->resolved_klasses()->at(resolved_klass_index);
459 if (klass != NULL) {
460 return klass;
461 }
462
463 // This tag doesn't change back to unresolved class unless at a safepoint.
464 if (this_cp->tag_at(which).is_unresolved_klass_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_NULL);
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
482 Klass* k;
483 {
484 // Turn off the single stepping while doing class resolution
485 JvmtiHideSingleStepping jhss(javaThread);
486 k = SystemDictionary::resolve_or_fail(name, loader, protection_domain, true, THREAD);
487 } // JvmtiHideSingleStepping jhss(javaThread);
488
489 if (!HAS_PENDING_EXCEPTION) {
490 // preserve the resolved klass from unloading
491 mirror_handle = Handle(THREAD, k->java_mirror());
492 // Do access check for klasses
493 verify_constant_pool_resolve(this_cp, k, THREAD);
494 }
495
496 // Failed to resolve class. We must record the errors so that subsequent attempts
497 // to resolve this constant pool entry fail with the same error (JVMS 5.4.3).
498 if (HAS_PENDING_EXCEPTION) {
499 if (save_resolution_error) {
500 save_and_throw_exception(this_cp, which, constantTag(JVM_CONSTANT_UnresolvedClass), CHECK_NULL);
501 // If CHECK_NULL above doesn't return the exception, that means that
502 // some other thread has beaten us and has resolved the class.
503 // To preserve old behavior, we return the resolved class.
504 klass = this_cp->resolved_klasses()->at(resolved_klass_index);
505 assert(klass != NULL, "must be resolved if exception was cleared");
506 return klass;
507 } else {
508 return NULL; // return the pending exception
509 }
510 }
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, 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);
|
30 #include "classfile/stringTable.hpp"
31 #include "classfile/systemDictionary.hpp"
32 #include "classfile/vmSymbols.hpp"
33 #include "interpreter/linkResolver.hpp"
34 #include "memory/allocation.inline.hpp"
35 #include "memory/heapInspection.hpp"
36 #include "memory/heapShared.hpp"
37 #include "memory/metadataFactory.hpp"
38 #include "memory/metaspaceClosure.hpp"
39 #include "memory/metaspaceShared.hpp"
40 #include "memory/oopFactory.hpp"
41 #include "memory/resourceArea.hpp"
42 #include "oops/array.hpp"
43 #include "oops/constantPool.inline.hpp"
44 #include "oops/cpCache.inline.hpp"
45 #include "oops/instanceKlass.hpp"
46 #include "oops/objArrayKlass.hpp"
47 #include "oops/objArrayOop.inline.hpp"
48 #include "oops/oop.inline.hpp"
49 #include "oops/typeArrayOop.inline.hpp"
50 #include "oops/valueArrayKlass.hpp"
51 #include "runtime/fieldType.hpp"
52 #include "runtime/handles.inline.hpp"
53 #include "runtime/init.hpp"
54 #include "runtime/javaCalls.hpp"
55 #include "runtime/signature.hpp"
56 #include "runtime/thread.inline.hpp"
57 #include "runtime/vframe.inline.hpp"
58 #include "utilities/copy.hpp"
59
60 ConstantPool* ConstantPool::allocate(ClassLoaderData* loader_data, int length, TRAPS) {
61 Array<u1>* tags = MetadataFactory::new_array<u1>(loader_data, length, 0, CHECK_NULL);
62 int size = ConstantPool::size(length);
63 return new (loader_data, size, MetaspaceObj::ConstantPoolType, THREAD) ConstantPool(tags);
64 }
65
66 #ifdef ASSERT
67
68 // MetaspaceObj allocation invariant is calloc equivalent memory
69 // simple verification of this here (JVM_CONSTANT_Invalid == 0 )
70 static bool tag_array_is_zero_initialized(Array<u1>* tags) {
194 assert(resolved_klasses() == NULL, "sanity");
195 Array<Klass*>* rk = MetadataFactory::new_array<Klass*>(loader_data, num_klasses, CHECK);
196 set_resolved_klasses(rk);
197 }
198
199 void ConstantPool::initialize_unresolved_klasses(ClassLoaderData* loader_data, TRAPS) {
200 int len = length();
201 int num_klasses = 0;
202 for (int i = 1; i <len; i++) {
203 switch (tag_at(i).value()) {
204 case JVM_CONSTANT_ClassIndex:
205 {
206 const int class_index = klass_index_at(i);
207 unresolved_klass_at_put(i, class_index, num_klasses++);
208 }
209 break;
210 #ifndef PRODUCT
211 case JVM_CONSTANT_Class:
212 case JVM_CONSTANT_UnresolvedClass:
213 case JVM_CONSTANT_UnresolvedClassInError:
214 // All of these should have been reverted back to Unresolved before calling
215 // this function.
216 ShouldNotReachHere();
217 #endif
218 }
219 }
220 allocate_resolved_klasses(loader_data, num_klasses, THREAD);
221 }
222
223 // Unsafe anonymous class support:
224 void ConstantPool::klass_at_put(int class_index, int name_index, int resolved_klass_index, Klass* k, Symbol* name) {
225 assert(is_within_bounds(class_index), "index out of bounds");
226 assert(is_within_bounds(name_index), "index out of bounds");
227 assert((resolved_klass_index & 0xffff0000) == 0, "must be");
228 *int_at_addr(class_index) =
229 build_int_from_shorts((jushort)resolved_klass_index, (jushort)name_index);
230
231 symbol_at_put(name_index, name);
232 name->increment_refcount();
233 Klass** adr = resolved_klasses()->adr_at(resolved_klass_index);
234 OrderAccess::release_store(adr, k);
235
236 // The interpreter assumes when the tag is stored, the klass is resolved
237 // and the Klass* non-NULL, so we need hardware store ordering here.
238 jbyte qdesc_bit = name->is_Q_signature() ? (jbyte)JVM_CONSTANT_QDESC_BIT : 0;
239 if (k != NULL) {
240 release_tag_at_put(class_index, JVM_CONSTANT_Class | qdesc_bit);
241 } else {
242 release_tag_at_put(class_index, JVM_CONSTANT_UnresolvedClass | qdesc_bit);
243 }
244 }
245
246 // Unsafe anonymous class support:
247 void ConstantPool::klass_at_put(int class_index, Klass* k) {
248 assert(k != NULL, "must be valid klass");
249 CPKlassSlot kslot = klass_slot_at(class_index);
250 int resolved_klass_index = kslot.resolved_klass_index();
251 Klass** adr = resolved_klasses()->adr_at(resolved_klass_index);
252 OrderAccess::release_store(adr, k);
253
254 // The interpreter assumes when the tag is stored, the klass is resolved
255 // and the Klass* non-NULL, so we need hardware store ordering here.
256 release_tag_at_put(class_index, JVM_CONSTANT_Class);
257 }
258
259 #if INCLUDE_CDS_JAVA_HEAP
260 // Archive the resolved references
261 void ConstantPool::archive_resolved_references(Thread* THREAD) {
262 if (_cache == NULL) {
427 Symbol* s = vfst.method()->method_holder()->source_file_name();
428 if (s != NULL) {
429 source_file = s->as_C_string();
430 }
431 }
432 }
433 if (k != this_cp->pool_holder()) {
434 // only print something if the classes are different
435 if (source_file != NULL) {
436 log_debug(class, resolve)("%s %s %s:%d",
437 this_cp->pool_holder()->external_name(),
438 k->external_name(), source_file, line_number);
439 } else {
440 log_debug(class, resolve)("%s %s",
441 this_cp->pool_holder()->external_name(),
442 k->external_name());
443 }
444 }
445 }
446
447 void check_is_value_type(Klass* k, TRAPS) {
448 if (!k->is_value()) {
449 THROW(vmSymbols::java_lang_IncompatibleClassChangeError());
450 }
451 }
452
453 Klass* ConstantPool::klass_at_impl(const constantPoolHandle& this_cp, int which,
454 bool save_resolution_error, TRAPS) {
455 assert(THREAD->is_Java_thread(), "must be a Java thread");
456 JavaThread* javaThread = (JavaThread*)THREAD;
457
458 // A resolved constantPool entry will contain a Klass*, otherwise a Symbol*.
459 // It is not safe to rely on the tag bit's here, since we don't have a lock, and
460 // the entry and tag is not updated atomicly.
461 CPKlassSlot kslot = this_cp->klass_slot_at(which);
462 int resolved_klass_index = kslot.resolved_klass_index();
463 int name_index = kslot.name_index();
464 assert(this_cp->tag_at(name_index).is_symbol(), "sanity");
465
466 Klass* klass = this_cp->resolved_klasses()->at(resolved_klass_index);
467 if (klass != NULL) {
468 return klass;
469 }
470
471 // This tag doesn't change back to unresolved class unless at a safepoint.
472 if (this_cp->tag_at(which).is_unresolved_klass_in_error()) {
473 // The original attempt to resolve this constant pool entry failed so find the
474 // class of the original error and throw another error of the same class
475 // (JVMS 5.4.3).
476 // If there is a detail message, pass that detail message to the error.
477 // The JVMS does not strictly require us to duplicate the same detail message,
478 // or any internal exception fields such as cause or stacktrace. But since the
479 // detail message is often a class name or other literal string, we will repeat it
480 // if we can find it in the symbol table.
481 throw_resolution_error(this_cp, which, CHECK_NULL);
482 ShouldNotReachHere();
483 }
484
485 Handle mirror_handle;
486 Symbol* name = this_cp->symbol_at(name_index);
487 bool value_type_signature = false;
488 if (name->is_Q_signature()) {
489 name = name->fundamental_name(THREAD);
490 value_type_signature = true;
491 }
492 Handle loader (THREAD, this_cp->pool_holder()->class_loader());
493 Handle protection_domain (THREAD, this_cp->pool_holder()->protection_domain());
494
495 Klass* k;
496 {
497 // Turn off the single stepping while doing class resolution
498 JvmtiHideSingleStepping jhss(javaThread);
499 k = SystemDictionary::resolve_or_fail(name, loader, protection_domain, true, THREAD);
500 } // JvmtiHideSingleStepping jhss(javaThread);
501 if (value_type_signature) {
502 name->decrement_refcount();
503 }
504
505 if (!HAS_PENDING_EXCEPTION) {
506 // preserve the resolved klass from unloading
507 mirror_handle = Handle(THREAD, k->java_mirror());
508 // Do access check for klasses
509 verify_constant_pool_resolve(this_cp, k, THREAD);
510 }
511
512 if (!HAS_PENDING_EXCEPTION && value_type_signature) {
513 check_is_value_type(k, THREAD);
514 }
515
516 if (!HAS_PENDING_EXCEPTION) {
517 Klass* bottom_klass = NULL;
518 if (k->is_objArray_klass()) {
519 bottom_klass = ObjArrayKlass::cast(k)->bottom_klass();
520 assert(bottom_klass != NULL, "Should be set");
521 assert(bottom_klass->is_instance_klass() || bottom_klass->is_typeArray_klass(), "Sanity check");
522 } else if (k->is_valueArray_klass()) {
523 bottom_klass = ValueArrayKlass::cast(k)->element_klass();
524 assert(bottom_klass != NULL, "Should be set");
525 }
526 }
527
528 // Failed to resolve class. We must record the errors so that subsequent attempts
529 // to resolve this constant pool entry fail with the same error (JVMS 5.4.3).
530 if (HAS_PENDING_EXCEPTION) {
531 if (save_resolution_error) {
532 save_and_throw_exception(this_cp, which, constantTag(JVM_CONSTANT_UnresolvedClass), CHECK_NULL);
533 // If CHECK_NULL above doesn't return the exception, that means that
534 // some other thread has beaten us and has resolved the class.
535 // To preserve old behavior, we return the resolved class.
536 klass = this_cp->resolved_klasses()->at(resolved_klass_index);
537 assert(klass != NULL, "must be resolved if exception was cleared");
538 return klass;
539 } else {
540 return NULL; // return the pending exception
541 }
542 }
543
544 // logging for class+resolve.
545 if (log_is_enabled(Debug, class, resolve)){
546 trace_class_resolution(this_cp, k);
547 }
548 Klass** adr = this_cp->resolved_klasses()->adr_at(resolved_klass_index);
549 OrderAccess::release_store(adr, k);
550 // The interpreter assumes when the tag is stored, the klass is resolved
551 // and the Klass* stored in _resolved_klasses is non-NULL, so we need
552 // hardware store ordering here.
553 jbyte tag = JVM_CONSTANT_Class;
554 if (this_cp->tag_at(which).is_Qdescriptor_klass()) {
555 tag |= JVM_CONSTANT_QDESC_BIT;
556 }
557 this_cp->release_tag_at_put(which, tag);
558 return k;
559 }
560
561
562 // Does not update ConstantPool* - to avoid any exception throwing. Used
563 // by compiler and exception handling. Also used to avoid classloads for
564 // instanceof operations. Returns NULL if the class has not been loaded or
565 // if the verification of constant pool failed
566 Klass* ConstantPool::klass_at_if_loaded(const constantPoolHandle& this_cp, int which) {
567 CPKlassSlot kslot = this_cp->klass_slot_at(which);
568 int resolved_klass_index = kslot.resolved_klass_index();
569 int name_index = kslot.name_index();
570 assert(this_cp->tag_at(name_index).is_symbol(), "sanity");
571
572 Klass* k = this_cp->resolved_klasses()->at(resolved_klass_index);
573 if (k != NULL) {
574 return k;
575 } else {
576 Thread *thread = Thread::current();
577 Symbol* name = this_cp->symbol_at(name_index);
|