1 /*
2 * Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
253 tty->print_cr("Error: find_field called on a klass oop."
254 " Likely error: reflection method does not correctly"
255 " wrap return value in a mirror object.");
256 #endif
257 ShouldNotReachHere();
258 return nullptr;
259 }
260
261 Method* Klass::uncached_lookup_method(const Symbol* name, const Symbol* signature,
262 OverpassLookupMode overpass_mode,
263 PrivateLookupMode private_mode) const {
264 #ifdef ASSERT
265 tty->print_cr("Error: uncached_lookup_method called on a klass oop."
266 " Likely error: reflection method does not correctly"
267 " wrap return value in a mirror object.");
268 #endif
269 ShouldNotReachHere();
270 return nullptr;
271 }
272
273 static markWord make_prototype(const Klass* kls) {
274 markWord prototype = markWord::prototype();
275 #ifdef _LP64
276 if (UseCompactObjectHeaders) {
277 // With compact object headers, the narrow Klass ID is part of the mark word.
278 // We therefore seed the mark word with the narrow Klass ID.
279 precond(CompressedKlassPointers::is_encodable(kls));
280 const narrowKlass nk = CompressedKlassPointers::encode(const_cast<Klass*>(kls));
281 prototype = prototype.set_narrow_klass(nk);
282 }
283 #endif
284 return prototype;
285 }
286
287 void* Klass::operator new(size_t size, ClassLoaderData* loader_data, size_t word_size, TRAPS) throw() {
288 return Metaspace::allocate(loader_data, word_size, MetaspaceObj::ClassType, THREAD);
289 }
290
291 Klass::Klass() : _kind(UnknownKlassKind) {
292 assert(CDSConfig::is_dumping_static_archive() || CDSConfig::is_using_archive(), "only for cds");
293 }
294
295 // "Normal" instantiation is preceded by a MetaspaceObj allocation
296 // which zeros out memory - calloc equivalent.
297 // The constructor is also used from CppVtableCloner,
298 // which doesn't zero out the memory before calling the constructor.
299 Klass::Klass(KlassKind kind) : _kind(kind),
300 _prototype_header(make_prototype(this)),
301 _shared_class_path_index(-1) {
302 CDS_ONLY(_aot_class_flags = 0;)
303 CDS_JAVA_HEAP_ONLY(_archived_mirror_index = -1;)
304 _primary_supers[0] = this;
305 set_super_check_offset(in_bytes(primary_supers_offset()));
306 }
307
308 jint Klass::array_layout_helper(BasicType etype) {
309 assert(etype >= T_BOOLEAN && etype <= T_OBJECT, "valid etype");
310 // Note that T_ARRAY is not allowed here.
311 int hsize = arrayOopDesc::base_offset_in_bytes(etype);
312 int esize = type2aelembytes(etype);
313 bool isobj = (etype == T_OBJECT);
314 int tag = isobj ? _lh_array_tag_obj_value : _lh_array_tag_type_value;
315 int lh = array_layout_helper(tag, hsize, etype, exact_log2(esize));
316
317 assert(lh < (int)_lh_neutral_value, "must look like an array layout");
318 assert(layout_helper_is_array(lh), "correct kind");
319 assert(layout_helper_is_objArray(lh) == isobj, "correct kind");
320 assert(layout_helper_is_typeArray(lh) == !isobj, "correct kind");
321 assert(layout_helper_header_size(lh) == hsize, "correct decode");
322 assert(layout_helper_element_type(lh) == etype, "correct decode");
323 assert(1 << layout_helper_log2_element_size(lh) == esize, "correct decode");
324
325 return lh;
326 }
327
328 int Klass::modifier_flags() const {
329 int mods = java_lang_Class::modifiers(java_mirror());
330 assert(mods == compute_modifier_flags(), "should be same");
331 return mods;
332 }
333
334 bool Klass::can_be_primary_super_slow() const {
335 if (super() == nullptr)
336 return true;
337 else if (super()->super_depth() >= primary_super_limit()-1)
338 return false;
339 else
590 }
591 if( i < secondaries->length() )
592 continue; // It's a dup, don't put it in
593 primaries->push(p);
594 }
595 // Combine the two arrays into a metadata object to pack the array.
596 uintx bitmap = 0;
597 Array<Klass*>* s2 = pack_secondary_supers(class_loader_data(), primaries, secondaries, bitmap, CHECK);
598 set_secondary_supers(s2, bitmap);
599 }
600 }
601
602 GrowableArray<Klass*>* Klass::compute_secondary_supers(int num_extra_slots,
603 Array<InstanceKlass*>* transitive_interfaces) {
604 assert(num_extra_slots == 0, "override for complex klasses");
605 assert(transitive_interfaces == nullptr, "sanity");
606 set_secondary_supers(Universe::the_empty_klass_array(), Universe::the_empty_klass_bitmap());
607 return nullptr;
608 }
609
610
611 // subklass links. Used by the compiler (and vtable initialization)
612 // May be cleaned concurrently, so must use the Compile_lock.
613 Klass* Klass::subklass() const {
614 // Need load_acquire on the _subklass, because it races with inserts that
615 // publishes freshly initialized data.
616 for (Klass* chain = AtomicAccess::load_acquire(&_subklass);
617 chain != nullptr;
618 // Do not need load_acquire on _next_sibling, because inserts never
619 // create _next_sibling edges to dead data.
620 chain = AtomicAccess::load(&chain->_next_sibling))
621 {
622 if (chain->is_loader_alive()) {
623 return chain;
624 }
625 }
626 return nullptr;
627 }
628
629 Klass* Klass::next_sibling(bool log) const {
630 // Do not need load_acquire on _next_sibling, because inserts never
1012 void Klass::print_on(outputStream* st) const {
1013 ResourceMark rm;
1014 // print title
1015 st->print("%s", internal_name());
1016 print_address_on(st);
1017 st->cr();
1018 }
1019
1020 #define BULLET " - "
1021
1022 // Caller needs ResourceMark
1023 void Klass::oop_print_on(oop obj, outputStream* st) {
1024 // print title
1025 st->print_cr("%s ", internal_name());
1026 obj->print_address_on(st);
1027
1028 if (WizardMode) {
1029 // print header
1030 obj->mark().print_on(st);
1031 st->cr();
1032 if (UseCompactObjectHeaders) {
1033 st->print(BULLET"prototype_header: " INTPTR_FORMAT, _prototype_header.value());
1034 st->cr();
1035 }
1036 }
1037
1038 // print class
1039 st->print(BULLET"klass: ");
1040 obj->klass()->print_value_on(st);
1041 st->print(BULLET"flags: "); _misc_flags.print_on(st); st->cr();
1042 st->cr();
1043 }
1044
1045 void Klass::oop_print_value_on(oop obj, outputStream* st) {
1046 // print title
1047 ResourceMark rm; // Cannot print in debug mode without this
1048 st->print("%s", internal_name());
1049 obj->print_address_on(st);
1050 }
1051
1052 // Verification
1053
1054 void Klass::verify_on(outputStream* st) {
1055
1073 Klass* ko = secondary_super_cache();
1074 guarantee(ko->is_klass(), "should be klass");
1075 }
1076 for ( uint i = 0; i < primary_super_limit(); i++ ) {
1077 Klass* ko = _primary_supers[i];
1078 if (ko != nullptr) {
1079 guarantee(ko->is_klass(), "should be klass");
1080 }
1081 }
1082
1083 if (java_mirror_no_keepalive() != nullptr) {
1084 guarantee(java_lang_Class::is_instance(java_mirror_no_keepalive()), "should be instance");
1085 }
1086 }
1087
1088 void Klass::oop_verify_on(oop obj, outputStream* st) {
1089 guarantee(oopDesc::is_oop(obj), "should be oop");
1090 guarantee(obj->klass()->is_klass(), "klass field is not a klass");
1091 }
1092
1093 // Note: this function is called with an address that may or may not be a Klass.
1094 // The point is not to assert it is but to check if it could be.
1095 bool Klass::is_valid(Klass* k) {
1096 if (!is_aligned(k, sizeof(MetaWord))) return false;
1097 if ((size_t)k < os::min_page_size()) return false;
1098
1099 if (!os::is_readable_range(k, k + 1)) return false;
1100 if (!Metaspace::contains(k)) return false;
1101
1102 if (!Symbol::is_valid(k->name())) return false;
1103 return ClassLoaderDataGraph::is_valid(k->class_loader_data());
1104 }
1105
1106 Method* Klass::method_at_vtable(int index) {
1107 #ifndef PRODUCT
1108 assert(index >= 0, "valid vtable index");
1109 if (DebugVtables) {
1110 verify_vtable_index(index);
1111 }
1112 #endif
|
1 /*
2 * Copyright (c) 1997, 2026, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
253 tty->print_cr("Error: find_field called on a klass oop."
254 " Likely error: reflection method does not correctly"
255 " wrap return value in a mirror object.");
256 #endif
257 ShouldNotReachHere();
258 return nullptr;
259 }
260
261 Method* Klass::uncached_lookup_method(const Symbol* name, const Symbol* signature,
262 OverpassLookupMode overpass_mode,
263 PrivateLookupMode private_mode) const {
264 #ifdef ASSERT
265 tty->print_cr("Error: uncached_lookup_method called on a klass oop."
266 " Likely error: reflection method does not correctly"
267 " wrap return value in a mirror object.");
268 #endif
269 ShouldNotReachHere();
270 return nullptr;
271 }
272
273 void* Klass::operator new(size_t size, ClassLoaderData* loader_data, size_t word_size, TRAPS) throw() {
274 return Metaspace::allocate(loader_data, word_size, MetaspaceObj::ClassType, THREAD);
275 }
276
277 Klass::Klass() : _kind(UnknownKlassKind) {
278 assert(CDSConfig::is_dumping_static_archive() || CDSConfig::is_using_archive(), "only for cds");
279 }
280
281 // "Normal" instantiation is preceded by a MetaspaceObj allocation
282 // which zeros out memory - calloc equivalent.
283 // The constructor is also used from CppVtableCloner,
284 // which doesn't zero out the memory before calling the constructor.
285 Klass::Klass(KlassKind kind, markWord prototype_header) : _kind(kind),
286 _shared_class_path_index(-1) {
287 set_prototype_header(make_prototype_header(this, prototype_header));
288 CDS_ONLY(_aot_class_flags = 0;)
289 CDS_JAVA_HEAP_ONLY(_archived_mirror_index = -1;)
290 _primary_supers[0] = this;
291 set_super_check_offset(in_bytes(primary_supers_offset()));
292 }
293
294 jint Klass::array_layout_helper(BasicType etype) {
295 assert(etype >= T_BOOLEAN && etype <= T_OBJECT, "valid etype");
296 // Note that T_ARRAY is not allowed here.
297 int hsize = arrayOopDesc::base_offset_in_bytes(etype);
298 int esize = type2aelembytes(etype);
299 bool isobj = (etype == T_OBJECT);
300 int tag = isobj ? _lh_array_tag_ref_value : _lh_array_tag_type_value;
301 int lh = array_layout_helper(tag, false, hsize, etype, exact_log2(esize));
302
303 assert(lh < (int)_lh_neutral_value, "must look like an array layout");
304 assert(layout_helper_is_array(lh), "correct kind");
305 assert(layout_helper_is_refArray(lh) == isobj, "correct kind");
306 assert(layout_helper_is_typeArray(lh) == !isobj, "correct kind");
307 assert(layout_helper_header_size(lh) == hsize, "correct decode");
308 assert(layout_helper_element_type(lh) == etype, "correct decode");
309 assert(1 << layout_helper_log2_element_size(lh) == esize, "correct decode");
310
311 return lh;
312 }
313
314 int Klass::modifier_flags() const {
315 int mods = java_lang_Class::modifiers(java_mirror());
316 assert(mods == compute_modifier_flags(), "should be same");
317 return mods;
318 }
319
320 bool Klass::can_be_primary_super_slow() const {
321 if (super() == nullptr)
322 return true;
323 else if (super()->super_depth() >= primary_super_limit()-1)
324 return false;
325 else
576 }
577 if( i < secondaries->length() )
578 continue; // It's a dup, don't put it in
579 primaries->push(p);
580 }
581 // Combine the two arrays into a metadata object to pack the array.
582 uintx bitmap = 0;
583 Array<Klass*>* s2 = pack_secondary_supers(class_loader_data(), primaries, secondaries, bitmap, CHECK);
584 set_secondary_supers(s2, bitmap);
585 }
586 }
587
588 GrowableArray<Klass*>* Klass::compute_secondary_supers(int num_extra_slots,
589 Array<InstanceKlass*>* transitive_interfaces) {
590 assert(num_extra_slots == 0, "override for complex klasses");
591 assert(transitive_interfaces == nullptr, "sanity");
592 set_secondary_supers(Universe::the_empty_klass_array(), Universe::the_empty_klass_bitmap());
593 return nullptr;
594 }
595
596 // subklass links. Used by the compiler (and vtable initialization)
597 // May be cleaned concurrently, so must use the Compile_lock.
598 Klass* Klass::subklass() const {
599 // Need load_acquire on the _subklass, because it races with inserts that
600 // publishes freshly initialized data.
601 for (Klass* chain = AtomicAccess::load_acquire(&_subklass);
602 chain != nullptr;
603 // Do not need load_acquire on _next_sibling, because inserts never
604 // create _next_sibling edges to dead data.
605 chain = AtomicAccess::load(&chain->_next_sibling))
606 {
607 if (chain->is_loader_alive()) {
608 return chain;
609 }
610 }
611 return nullptr;
612 }
613
614 Klass* Klass::next_sibling(bool log) const {
615 // Do not need load_acquire on _next_sibling, because inserts never
997 void Klass::print_on(outputStream* st) const {
998 ResourceMark rm;
999 // print title
1000 st->print("%s", internal_name());
1001 print_address_on(st);
1002 st->cr();
1003 }
1004
1005 #define BULLET " - "
1006
1007 // Caller needs ResourceMark
1008 void Klass::oop_print_on(oop obj, outputStream* st) {
1009 // print title
1010 st->print_cr("%s ", internal_name());
1011 obj->print_address_on(st);
1012
1013 if (WizardMode) {
1014 // print header
1015 obj->mark().print_on(st);
1016 st->cr();
1017 st->print(BULLET"prototype_header: " INTPTR_FORMAT, _prototype_header.value());
1018 st->cr();
1019 }
1020
1021 // print class
1022 st->print(BULLET"klass: ");
1023 obj->klass()->print_value_on(st);
1024 st->print(BULLET"flags: "); _misc_flags.print_on(st); st->cr();
1025 st->cr();
1026 }
1027
1028 void Klass::oop_print_value_on(oop obj, outputStream* st) {
1029 // print title
1030 ResourceMark rm; // Cannot print in debug mode without this
1031 st->print("%s", internal_name());
1032 obj->print_address_on(st);
1033 }
1034
1035 // Verification
1036
1037 void Klass::verify_on(outputStream* st) {
1038
1056 Klass* ko = secondary_super_cache();
1057 guarantee(ko->is_klass(), "should be klass");
1058 }
1059 for ( uint i = 0; i < primary_super_limit(); i++ ) {
1060 Klass* ko = _primary_supers[i];
1061 if (ko != nullptr) {
1062 guarantee(ko->is_klass(), "should be klass");
1063 }
1064 }
1065
1066 if (java_mirror_no_keepalive() != nullptr) {
1067 guarantee(java_lang_Class::is_instance(java_mirror_no_keepalive()), "should be instance");
1068 }
1069 }
1070
1071 void Klass::oop_verify_on(oop obj, outputStream* st) {
1072 guarantee(oopDesc::is_oop(obj), "should be oop");
1073 guarantee(obj->klass()->is_klass(), "klass field is not a klass");
1074 }
1075
1076 #ifdef ASSERT
1077 void Klass::validate_array_description(const ArrayDescription& ad) {
1078 if (is_identity_class() || is_array_klass() || is_interface() ||
1079 (is_instance_klass() && InstanceKlass::cast(this)->access_flags().is_abstract())) {
1080 assert(ad._layout_kind == LayoutKind::REFERENCE, "Cannot support flattening");
1081 assert(ad._kind == KlassKind::RefArrayKlassKind, "Must be a reference array");
1082 } else {
1083 assert(is_inline_klass(), "Must be");
1084 InlineKlass* ik = InlineKlass::cast(this);
1085 switch(ad._layout_kind) {
1086 case LayoutKind::BUFFERED:
1087 fatal("Invalid layout for an array");
1088 break;
1089 case LayoutKind::NULL_FREE_ATOMIC_FLAT:
1090 assert(ik->has_null_free_atomic_layout(), "Sanity check");
1091 break;
1092 case LayoutKind::NULL_FREE_NON_ATOMIC_FLAT:
1093 assert(ik->has_null_free_non_atomic_layout(), "Sanity check");
1094 break;
1095 case LayoutKind::NULLABLE_ATOMIC_FLAT:
1096 assert(ik->has_nullable_atomic_layout(), "Sanity check");
1097 break;
1098 case LayoutKind::NULLABLE_NON_ATOMIC_FLAT:
1099 assert(ik->has_nullable_non_atomic_layout(), "Sanity check)");
1100 break;
1101 case LayoutKind::REFERENCE:
1102 break;
1103 default:
1104 ShouldNotReachHere();
1105 }
1106 }
1107 }
1108 #endif // ASSERT
1109
1110 // Note: this function is called with an address that may or may not be a Klass.
1111 // The point is not to assert it is but to check if it could be.
1112 bool Klass::is_valid(Klass* k) {
1113 if (!is_aligned(k, sizeof(MetaWord))) return false;
1114 if ((size_t)k < os::min_page_size()) return false;
1115
1116 if (!os::is_readable_range(k, k + 1)) return false;
1117 if (!Metaspace::contains(k)) return false;
1118
1119 if (!Symbol::is_valid(k->name())) return false;
1120 return ClassLoaderDataGraph::is_valid(k->class_loader_data());
1121 }
1122
1123 Method* Klass::method_at_vtable(int index) {
1124 #ifndef PRODUCT
1125 assert(index >= 0, "valid vtable index");
1126 if (DebugVtables) {
1127 verify_vtable_index(index);
1128 }
1129 #endif
|