22 * |
22 * |
23 */ |
23 */ |
24 |
24 |
25 #include "precompiled.hpp" |
25 #include "precompiled.hpp" |
26 #include "classfile/classLoaderData.inline.hpp" |
26 #include "classfile/classLoaderData.inline.hpp" |
|
27 #include "classfile/classLoaderDataGraph.inline.hpp" |
27 #include "classfile/dictionary.hpp" |
28 #include "classfile/dictionary.hpp" |
28 #include "classfile/javaClasses.hpp" |
29 #include "classfile/javaClasses.hpp" |
|
30 #include "classfile/moduleEntry.hpp" |
29 #include "classfile/systemDictionary.hpp" |
31 #include "classfile/systemDictionary.hpp" |
30 #include "classfile/vmSymbols.hpp" |
32 #include "classfile/vmSymbols.hpp" |
31 #include "gc/shared/collectedHeap.inline.hpp" |
33 #include "gc/shared/collectedHeap.inline.hpp" |
32 #include "logging/log.hpp" |
34 #include "logging/log.hpp" |
33 #include "memory/heapInspection.hpp" |
35 #include "memory/heapInspection.hpp" |
|
36 #include "memory/heapShared.hpp" |
34 #include "memory/metadataFactory.hpp" |
37 #include "memory/metadataFactory.hpp" |
35 #include "memory/metaspaceClosure.hpp" |
38 #include "memory/metaspaceClosure.hpp" |
36 #include "memory/metaspaceShared.hpp" |
39 #include "memory/metaspaceShared.hpp" |
37 #include "memory/oopFactory.hpp" |
40 #include "memory/oopFactory.hpp" |
38 #include "memory/resourceArea.hpp" |
41 #include "memory/resourceArea.hpp" |
|
42 #include "memory/universe.hpp" |
39 #include "oops/compressedOops.inline.hpp" |
43 #include "oops/compressedOops.inline.hpp" |
40 #include "oops/instanceKlass.hpp" |
44 #include "oops/instanceKlass.hpp" |
41 #include "oops/klass.inline.hpp" |
45 #include "oops/klass.inline.hpp" |
42 #include "oops/oop.inline.hpp" |
46 #include "oops/oop.inline.hpp" |
43 #include "oops/oopHandle.inline.hpp" |
47 #include "oops/oopHandle.inline.hpp" |
190 // which doesn't zero out the memory before calling the constructor. |
189 // which doesn't zero out the memory before calling the constructor. |
191 // Need to set the _java_mirror field explicitly to not hit an assert that the field |
190 // Need to set the _java_mirror field explicitly to not hit an assert that the field |
192 // should be NULL before setting it. |
191 // should be NULL before setting it. |
193 Klass::Klass(KlassID id) : _id(id), |
192 Klass::Klass(KlassID id) : _id(id), |
194 _java_mirror(NULL), |
193 _java_mirror(NULL), |
195 _prototype_header(markOopDesc::prototype()), |
194 _prototype_header(markWord::prototype()), |
196 _shared_class_path_index(-1) { |
195 _shared_class_path_index(-1) { |
197 CDS_ONLY(_shared_class_flags = 0;) |
196 CDS_ONLY(_shared_class_flags = 0;) |
198 CDS_JAVA_HEAP_ONLY(_archived_mirror = 0;) |
197 CDS_JAVA_HEAP_ONLY(_archived_mirror = 0;) |
199 _primary_supers[0] = this; |
198 _primary_supers[0] = this; |
200 set_super_check_offset(in_bytes(primary_supers_offset())); |
199 set_super_check_offset(in_bytes(primary_supers_offset())); |
354 set_secondary_supers(Universe::the_empty_klass_array()); |
348 set_secondary_supers(Universe::the_empty_klass_array()); |
355 return NULL; |
349 return NULL; |
356 } |
350 } |
357 |
351 |
358 |
352 |
|
353 // superklass links |
359 InstanceKlass* Klass::superklass() const { |
354 InstanceKlass* Klass::superklass() const { |
360 assert(super() == NULL || super()->is_instance_klass(), "must be instance klass"); |
355 assert(super() == NULL || super()->is_instance_klass(), "must be instance klass"); |
361 return _super == NULL ? NULL : InstanceKlass::cast(_super); |
356 return _super == NULL ? NULL : InstanceKlass::cast(_super); |
362 } |
357 } |
363 |
358 |
|
359 // subklass links. Used by the compiler (and vtable initialization) |
|
360 // May be cleaned concurrently, so must use the Compile_lock. |
|
361 // The log parameter is for clean_weak_klass_links to report unlinked classes. |
|
362 Klass* Klass::subklass(bool log) const { |
|
363 // Need load_acquire on the _subklass, because it races with inserts that |
|
364 // publishes freshly initialized data. |
|
365 for (Klass* chain = OrderAccess::load_acquire(&_subklass); |
|
366 chain != NULL; |
|
367 // Do not need load_acquire on _next_sibling, because inserts never |
|
368 // create _next_sibling edges to dead data. |
|
369 chain = Atomic::load(&chain->_next_sibling)) |
|
370 { |
|
371 if (chain->is_loader_alive()) { |
|
372 return chain; |
|
373 } else if (log) { |
|
374 if (log_is_enabled(Trace, class, unload)) { |
|
375 ResourceMark rm; |
|
376 log_trace(class, unload)("unlinking class (subclass): %s", chain->external_name()); |
|
377 } |
|
378 } |
|
379 } |
|
380 return NULL; |
|
381 } |
|
382 |
|
383 Klass* Klass::next_sibling(bool log) const { |
|
384 // Do not need load_acquire on _next_sibling, because inserts never |
|
385 // create _next_sibling edges to dead data. |
|
386 for (Klass* chain = Atomic::load(&_next_sibling); |
|
387 chain != NULL; |
|
388 chain = Atomic::load(&chain->_next_sibling)) { |
|
389 // Only return alive klass, there may be stale klass |
|
390 // in this chain if cleaned concurrently. |
|
391 if (chain->is_loader_alive()) { |
|
392 return chain; |
|
393 } else if (log) { |
|
394 if (log_is_enabled(Trace, class, unload)) { |
|
395 ResourceMark rm; |
|
396 log_trace(class, unload)("unlinking class (sibling): %s", chain->external_name()); |
|
397 } |
|
398 } |
|
399 } |
|
400 return NULL; |
|
401 } |
|
402 |
364 void Klass::set_subklass(Klass* s) { |
403 void Klass::set_subklass(Klass* s) { |
365 assert(s != this, "sanity check"); |
404 assert(s != this, "sanity check"); |
366 _subklass = s; |
405 OrderAccess::release_store(&_subklass, s); |
367 } |
406 } |
368 |
407 |
369 void Klass::set_next_sibling(Klass* s) { |
408 void Klass::set_next_sibling(Klass* s) { |
370 assert(s != this, "sanity check"); |
409 assert(s != this, "sanity check"); |
371 _next_sibling = s; |
410 // Does not need release semantics. If used by cleanup, it will link to |
|
411 // already safely published data, and if used by inserts, will be published |
|
412 // safely using cmpxchg. |
|
413 Atomic::store(s, &_next_sibling); |
372 } |
414 } |
373 |
415 |
374 void Klass::append_to_sibling_list() { |
416 void Klass::append_to_sibling_list() { |
|
417 assert_locked_or_safepoint(Compile_lock); |
375 debug_only(verify();) |
418 debug_only(verify();) |
376 // add ourselves to superklass' subklass list |
419 // add ourselves to superklass' subklass list |
377 InstanceKlass* super = superklass(); |
420 InstanceKlass* super = superklass(); |
378 if (super == NULL) return; // special case: class Object |
421 if (super == NULL) return; // special case: class Object |
379 assert((!super->is_interface() // interfaces cannot be supers |
422 assert((!super->is_interface() // interfaces cannot be supers |
380 && (super->superklass() == NULL || !is_interface())), |
423 && (super->superklass() == NULL || !is_interface())), |
381 "an interface can only be a subklass of Object"); |
424 "an interface can only be a subklass of Object"); |
382 Klass* prev_first_subklass = super->subklass(); |
425 |
383 if (prev_first_subklass != NULL) { |
426 // Make sure there is no stale subklass head |
384 // set our sibling to be the superklass' previous first subklass |
427 super->clean_subklass(); |
385 set_next_sibling(prev_first_subklass); |
428 |
386 } |
429 for (;;) { |
387 // make ourselves the superklass' first subklass |
430 Klass* prev_first_subklass = OrderAccess::load_acquire(&_super->_subklass); |
388 super->set_subklass(this); |
431 if (prev_first_subklass != NULL) { |
|
432 // set our sibling to be the superklass' previous first subklass |
|
433 assert(prev_first_subklass->is_loader_alive(), "May not attach not alive klasses"); |
|
434 set_next_sibling(prev_first_subklass); |
|
435 } |
|
436 // Note that the prev_first_subklass is always alive, meaning no sibling_next links |
|
437 // are ever created to not alive klasses. This is an important invariant of the lock-free |
|
438 // cleaning protocol, that allows us to safely unlink dead klasses from the sibling list. |
|
439 if (Atomic::cmpxchg(this, &super->_subklass, prev_first_subklass) == prev_first_subklass) { |
|
440 return; |
|
441 } |
|
442 } |
389 debug_only(verify();) |
443 debug_only(verify();) |
390 } |
444 } |
391 |
445 |
392 oop Klass::holder_phantom() const { |
446 void Klass::clean_subklass() { |
393 return class_loader_data()->holder_phantom(); |
447 for (;;) { |
|
448 // Need load_acquire, due to contending with concurrent inserts |
|
449 Klass* subklass = OrderAccess::load_acquire(&_subklass); |
|
450 if (subklass == NULL || subklass->is_loader_alive()) { |
|
451 return; |
|
452 } |
|
453 // Try to fix _subklass until it points at something not dead. |
|
454 Atomic::cmpxchg(subklass->next_sibling(), &_subklass, subklass); |
|
455 } |
394 } |
456 } |
395 |
457 |
396 void Klass::clean_weak_klass_links(bool unloading_occurred, bool clean_alive_klasses) { |
458 void Klass::clean_weak_klass_links(bool unloading_occurred, bool clean_alive_klasses) { |
397 if (!ClassUnloading || !unloading_occurred) { |
459 if (!ClassUnloading || !unloading_occurred) { |
398 return; |
460 return; |
406 Klass* current = stack.pop(); |
468 Klass* current = stack.pop(); |
407 |
469 |
408 assert(current->is_loader_alive(), "just checking, this should be live"); |
470 assert(current->is_loader_alive(), "just checking, this should be live"); |
409 |
471 |
410 // Find and set the first alive subklass |
472 // Find and set the first alive subklass |
411 Klass* sub = current->subklass(); |
473 Klass* sub = current->subklass(true); |
412 while (sub != NULL && !sub->is_loader_alive()) { |
474 current->clean_subklass(); |
413 #ifndef PRODUCT |
|
414 if (log_is_enabled(Trace, class, unload)) { |
|
415 ResourceMark rm; |
|
416 log_trace(class, unload)("unlinking class (subclass): %s", sub->external_name()); |
|
417 } |
|
418 #endif |
|
419 sub = sub->next_sibling(); |
|
420 } |
|
421 current->set_subklass(sub); |
|
422 if (sub != NULL) { |
475 if (sub != NULL) { |
423 stack.push(sub); |
476 stack.push(sub); |
424 } |
477 } |
425 |
478 |
426 // Find and set the first alive sibling |
479 // Find and set the first alive sibling |
427 Klass* sibling = current->next_sibling(); |
480 Klass* sibling = current->next_sibling(true); |
428 while (sibling != NULL && !sibling->is_loader_alive()) { |
|
429 if (log_is_enabled(Trace, class, unload)) { |
|
430 ResourceMark rm; |
|
431 log_trace(class, unload)("[Unlinking class (sibling) %s]", sibling->external_name()); |
|
432 } |
|
433 sibling = sibling->next_sibling(); |
|
434 } |
|
435 current->set_next_sibling(sibling); |
481 current->set_next_sibling(sibling); |
436 if (sibling != NULL) { |
482 if (sibling != NULL) { |
437 stack.push(sibling); |
483 stack.push(sibling); |
438 } |
484 } |
439 |
485 |
462 it->push(&_secondary_supers); |
508 it->push(&_secondary_supers); |
463 for (int i = 0; i < _primary_super_limit; i++) { |
509 for (int i = 0; i < _primary_super_limit; i++) { |
464 it->push(&_primary_supers[i]); |
510 it->push(&_primary_supers[i]); |
465 } |
511 } |
466 it->push(&_super); |
512 it->push(&_super); |
467 it->push(&_subklass); |
513 it->push((Klass**)&_subklass); |
468 it->push(&_next_sibling); |
514 it->push((Klass**)&_next_sibling); |
469 it->push(&_next_link); |
515 it->push(&_next_link); |
470 |
516 |
471 vtableEntry* vt = start_of_vtable(); |
517 vtableEntry* vt = start_of_vtable(); |
472 for (int i=0; i<vtable_length(); i++) { |
518 for (int i=0; i<vtable_length(); i++) { |
473 it->push(vt[i].method_addr()); |
519 it->push(vt[i].method_addr()); |
474 } |
520 } |
475 } |
521 } |
476 |
522 |
477 void Klass::remove_unshareable_info() { |
523 void Klass::remove_unshareable_info() { |
478 assert (DumpSharedSpaces, "only called for DumpSharedSpaces"); |
524 assert (Arguments::is_dumping_archive(), |
|
525 "only called during CDS dump time"); |
479 JFR_ONLY(REMOVE_ID(this);) |
526 JFR_ONLY(REMOVE_ID(this);) |
480 if (log_is_enabled(Trace, cds, unshareable)) { |
527 if (log_is_enabled(Trace, cds, unshareable)) { |
481 ResourceMark rm; |
528 ResourceMark rm; |
482 log_trace(cds, unshareable)("remove: %s", external_name()); |
529 log_trace(cds, unshareable)("remove: %s", external_name()); |
483 } |
530 } |
608 Klass* Klass::array_klass_impl(bool or_null, TRAPS) { |
661 Klass* Klass::array_klass_impl(bool or_null, TRAPS) { |
609 fatal("array_klass should be dispatched to InstanceKlass, ObjArrayKlass or TypeArrayKlass"); |
662 fatal("array_klass should be dispatched to InstanceKlass, ObjArrayKlass or TypeArrayKlass"); |
610 return NULL; |
663 return NULL; |
611 } |
664 } |
612 |
665 |
613 oop Klass::class_loader() const { return class_loader_data()->class_loader(); } |
666 void Klass::check_array_allocation_length(int length, int max_length, TRAPS) { |
|
667 if (length > max_length) { |
|
668 if (!THREAD->in_retryable_allocation()) { |
|
669 report_java_out_of_memory("Requested array size exceeds VM limit"); |
|
670 JvmtiExport::post_array_size_exhausted(); |
|
671 THROW_OOP(Universe::out_of_memory_error_array_size()); |
|
672 } else { |
|
673 THROW_OOP(Universe::out_of_memory_error_retry()); |
|
674 } |
|
675 } else if (length < 0) { |
|
676 THROW_MSG(vmSymbols::java_lang_NegativeArraySizeException(), err_msg("%d", length)); |
|
677 } |
|
678 } |
614 |
679 |
615 // In product mode, this function doesn't have virtual function calls so |
680 // In product mode, this function doesn't have virtual function calls so |
616 // there might be some performance advantage to handling InstanceKlass here. |
681 // there might be some performance advantage to handling InstanceKlass here. |
617 const char* Klass::external_name() const { |
682 const char* Klass::external_name() const { |
618 if (is_instance_klass()) { |
683 if (is_instance_klass()) { |
728 if (ko != NULL) { |
797 if (ko != NULL) { |
729 guarantee(ko->is_klass(), "should be klass"); |
798 guarantee(ko->is_klass(), "should be klass"); |
730 } |
799 } |
731 } |
800 } |
732 |
801 |
733 if (java_mirror() != NULL) { |
802 if (java_mirror_no_keepalive() != NULL) { |
734 guarantee(oopDesc::is_oop(java_mirror()), "should be instance"); |
803 guarantee(oopDesc::is_oop(java_mirror_no_keepalive()), "should be instance"); |
735 } |
804 } |
736 } |
805 } |
737 |
806 |
738 void Klass::oop_verify_on(oop obj, outputStream* st) { |
807 void Klass::oop_verify_on(oop obj, outputStream* st) { |
739 guarantee(oopDesc::is_oop(obj), "should be oop"); |
808 guarantee(oopDesc::is_oop(obj), "should be oop"); |
740 guarantee(obj->klass()->is_klass(), "klass field is not a klass"); |
809 guarantee(obj->klass()->is_klass(), "klass field is not a klass"); |
741 } |
810 } |
742 |
811 |
743 klassVtable Klass::vtable() const { |
812 bool Klass::is_valid(Klass* k) { |
744 return klassVtable(const_cast<Klass*>(this), start_of_vtable(), vtable_length() / vtableEntry::size()); |
813 if (!is_aligned(k, sizeof(MetaWord))) return false; |
745 } |
814 if ((size_t)k < os::min_page_size()) return false; |
746 |
815 |
747 vtableEntry* Klass::start_of_vtable() const { |
816 if (!os::is_readable_range(k, k + 1)) return false; |
748 return (vtableEntry*) ((address)this + in_bytes(vtable_start_offset())); |
817 if (!Metaspace::contains(k)) return false; |
|
818 |
|
819 if (!Symbol::is_valid(k->name())) return false; |
|
820 return ClassLoaderDataGraph::is_valid(k->class_loader_data()); |
749 } |
821 } |
750 |
822 |
751 Method* Klass::method_at_vtable(int index) { |
823 Method* Klass::method_at_vtable(int index) { |
752 #ifndef PRODUCT |
824 #ifndef PRODUCT |
753 assert(index >= 0, "valid vtable index"); |
825 assert(index >= 0, "valid vtable index"); |
860 const char* parent_loader_phrase = ""; |
929 const char* parent_loader_phrase = ""; |
861 const char* parent_loader_name_and_id = ""; |
930 const char* parent_loader_name_and_id = ""; |
862 if (include_parent_loader && |
931 if (include_parent_loader && |
863 !cld->is_builtin_class_loader_data()) { |
932 !cld->is_builtin_class_loader_data()) { |
864 oop parent_loader = java_lang_ClassLoader::parent(class_loader()); |
933 oop parent_loader = java_lang_ClassLoader::parent(class_loader()); |
865 ClassLoaderData *parent_cld = ClassLoaderData::class_loader_data(parent_loader); |
934 ClassLoaderData *parent_cld = ClassLoaderData::class_loader_data_or_null(parent_loader); |
866 assert(parent_cld != NULL, "parent's class loader data should not be null"); |
935 // The parent loader's ClassLoaderData could be null if it is |
867 parent_loader_name_and_id = parent_cld->loader_name_and_id(); |
936 // a delegating class loader that has never defined a class. |
|
937 // In this case the loader's name must be obtained via the parent loader's oop. |
|
938 if (parent_cld == NULL) { |
|
939 oop cl_name_and_id = java_lang_ClassLoader::nameAndId(parent_loader); |
|
940 if (cl_name_and_id != NULL) { |
|
941 parent_loader_name_and_id = java_lang_String::as_utf8_string(cl_name_and_id); |
|
942 } |
|
943 } else { |
|
944 parent_loader_name_and_id = parent_cld->loader_name_and_id(); |
|
945 } |
868 parent_loader_phrase = ", parent loader "; |
946 parent_loader_phrase = ", parent loader "; |
869 len += strlen(parent_loader_phrase) + strlen(parent_loader_name_and_id); |
947 len += strlen(parent_loader_phrase) + strlen(parent_loader_name_and_id); |
870 } |
948 } |
871 |
949 |
872 // Start to construct final full class description string |
950 // Start to construct final full class description string |