1 /* 2 * Copyright (c) 1997, 2024, 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 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "cds/cdsConfig.hpp" 27 #include "cds/metaspaceShared.hpp" 28 #include "classfile/javaClasses.hpp" 29 #include "classfile/moduleEntry.hpp" 30 #include "classfile/symbolTable.hpp" 31 #include "classfile/vmClasses.hpp" 32 #include "classfile/vmSymbols.hpp" 33 #include "gc/shared/collectedHeap.inline.hpp" 34 #include "jvmtifiles/jvmti.h" 35 #include "memory/metaspaceClosure.hpp" 36 #include "memory/resourceArea.hpp" 37 #include "memory/universe.hpp" 38 #include "oops/arrayKlass.inline.hpp" 39 #include "oops/arrayOop.hpp" 40 #include "oops/instanceKlass.hpp" 41 #include "oops/klass.inline.hpp" 42 #include "oops/objArrayKlass.hpp" 43 #include "oops/objArrayOop.hpp" 44 #include "oops/oop.inline.hpp" 45 #include "runtime/handles.inline.hpp" 46 47 void* ArrayKlass::operator new(size_t size, ClassLoaderData* loader_data, size_t word_size, TRAPS) throw() { 48 return Metaspace::allocate(loader_data, word_size, MetaspaceObj::ClassType, true, THREAD); 49 } 50 51 ArrayKlass::ArrayKlass() { 52 assert(CDSConfig::is_dumping_static_archive() || CDSConfig::is_using_archive(), "only for CDS"); 53 } 54 55 int ArrayKlass::static_size(int header_size) { 56 // size of an array klass object 57 assert(header_size <= InstanceKlass::header_size(), "bad header size"); 58 // If this assert fails, see comments in base_create_array_klass. 59 header_size = InstanceKlass::header_size(); 60 int vtable_len = Universe::base_vtable_size(); 61 int size = header_size + vtable_len; 62 return align_metadata_size(size); 63 } 64 65 66 InstanceKlass* ArrayKlass::java_super() const { 67 if (super() == nullptr) return nullptr; // bootstrap case 68 // Array klasses have primary supertypes which are not reported to Java. 69 // Example super chain: String[][] -> Object[][] -> Object[] -> Object 70 return vmClasses::Object_klass(); 71 } 72 73 74 oop ArrayKlass::multi_allocate(int rank, jint* sizes, TRAPS) { 75 ShouldNotReachHere(); 76 return nullptr; 77 } 78 79 // find field according to JVM spec 5.4.3.2, returns the klass in which the field is defined 80 Klass* ArrayKlass::find_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const { 81 // There are no fields in an array klass but look to the super class (Object) 82 assert(super(), "super klass must be present"); 83 return super()->find_field(name, sig, fd); 84 } 85 86 Method* ArrayKlass::uncached_lookup_method(const Symbol* name, 87 const Symbol* signature, 88 OverpassLookupMode overpass_mode, 89 PrivateLookupMode private_mode) const { 90 // There are no methods in an array klass but the super class (Object) has some 91 assert(super(), "super klass must be present"); 92 // Always ignore overpass methods in superclasses, although technically the 93 // super klass of an array, (j.l.Object) should not have 94 // any overpass methods present. 95 return super()->uncached_lookup_method(name, signature, OverpassLookupMode::skip, private_mode); 96 } 97 98 ArrayKlass::ArrayKlass(Symbol* name, KlassKind kind) : 99 Klass(kind), 100 _dimension(1), 101 _higher_dimension(nullptr), 102 _lower_dimension(nullptr) { 103 // Arrays don't add any new methods, so their vtable is the same size as 104 // the vtable of klass Object. 105 set_vtable_length(Universe::base_vtable_size()); 106 set_name(name); 107 set_super(Universe::is_bootstrapping() ? nullptr : vmClasses::Object_klass()); 108 set_layout_helper(Klass::_lh_neutral_value); 109 set_is_cloneable(); // All arrays are considered to be cloneable (See JLS 20.1.5) 110 JFR_ONLY(INIT_ID(this);) 111 log_array_class_load(this); 112 } 113 114 Symbol* ArrayKlass::create_element_klass_array_name(Klass* element_klass, TRAPS) { 115 ResourceMark rm(THREAD); 116 Symbol* name = nullptr; 117 char *name_str = element_klass->name()->as_C_string(); 118 int len = element_klass->name()->utf8_length(); 119 char *new_str = NEW_RESOURCE_ARRAY(char, len + 4); 120 int idx = 0; 121 new_str[idx++] = JVM_SIGNATURE_ARRAY; 122 if (element_klass->is_instance_klass()) { // it could be an array or simple type 123 new_str[idx++] = JVM_SIGNATURE_CLASS; 124 } 125 memcpy(&new_str[idx], name_str, len * sizeof(char)); 126 idx += len; 127 if (element_klass->is_instance_klass()) { 128 new_str[idx++] = JVM_SIGNATURE_ENDCLASS; 129 } 130 new_str[idx++] = '\0'; 131 return SymbolTable::new_symbol(new_str); 132 } 133 134 // Initialization of vtables and mirror object is done separately from base_create_array_klass, 135 // since a GC can happen. At this point all instance variables of the ArrayKlass must be setup. 136 void ArrayKlass::complete_create_array_klass(ArrayKlass* k, Klass* super_klass, ModuleEntry* module_entry, TRAPS) { 137 k->initialize_supers(super_klass, nullptr, CHECK); 138 k->vtable().initialize_vtable(); 139 140 // During bootstrapping, before java.base is defined, the module_entry may not be present yet. 141 // These classes will be put on a fixup list and their module fields will be patched once 142 // java.base is defined. 143 assert((module_entry != nullptr) || ((module_entry == nullptr) && !ModuleEntryTable::javabase_defined()), 144 "module entry not available post " JAVA_BASE_NAME " definition"); 145 oop module = (module_entry != nullptr) ? module_entry->module() : (oop)nullptr; 146 java_lang_Class::create_mirror(k, Handle(THREAD, k->class_loader()), Handle(THREAD, module), Handle(), Handle(), CHECK); 147 } 148 149 ArrayKlass* ArrayKlass::array_klass(int n, TRAPS) { 150 151 assert(dimension() <= n, "check order of chain"); 152 int dim = dimension(); 153 if (dim == n) return this; 154 155 // lock-free read needs acquire semantics 156 if (higher_dimension_acquire() == nullptr) { 157 158 // Ensure atomic creation of higher dimensions 159 RecursiveLocker rl(MultiArray_lock, THREAD); 160 161 if (higher_dimension() == nullptr) { 162 // Create multi-dim klass object and link them together 163 ObjArrayKlass* ak = 164 ObjArrayKlass::allocate_objArray_klass(class_loader_data(), dim + 1, this, false, CHECK_NULL); 165 // use 'release' to pair with lock-free load 166 release_set_higher_dimension(ak); 167 assert(ak->lower_dimension() == this, "lower dimension mismatch"); 168 } 169 } 170 171 ObjArrayKlass* ak = higher_dimension(); 172 assert(ak != nullptr, "should be set"); 173 THREAD->check_possible_safepoint(); 174 return ak->array_klass(n, THREAD); 175 } 176 177 ArrayKlass* ArrayKlass::array_klass_or_null(int n) { 178 179 assert(dimension() <= n, "check order of chain"); 180 int dim = dimension(); 181 if (dim == n) return this; 182 183 // lock-free read needs acquire semantics 184 if (higher_dimension_acquire() == nullptr) { 185 return nullptr; 186 } 187 188 ObjArrayKlass *ak = higher_dimension(); 189 return ak->array_klass_or_null(n); 190 } 191 192 ArrayKlass* ArrayKlass::array_klass(TRAPS) { 193 return array_klass(dimension() + 1, THREAD); 194 } 195 196 ArrayKlass* ArrayKlass::array_klass_or_null() { 197 return array_klass_or_null(dimension() + 1); 198 } 199 200 201 GrowableArray<Klass*>* ArrayKlass::compute_secondary_supers(int num_extra_slots, 202 Array<InstanceKlass*>* transitive_interfaces) { 203 // interfaces = { cloneable_klass, serializable_klass }; 204 assert(num_extra_slots == 0, "sanity of primitive array type"); 205 assert(transitive_interfaces == nullptr, "sanity"); 206 // Must share this for correct bootstrapping! 207 set_secondary_supers(Universe::the_array_interfaces_array(), 208 Universe::the_array_interfaces_bitmap()); 209 return nullptr; 210 } 211 212 objArrayOop ArrayKlass::allocate_arrayArray(int n, int length, TRAPS) { 213 check_array_allocation_length(length, arrayOopDesc::max_array_length(T_ARRAY), CHECK_NULL); 214 size_t size = objArrayOopDesc::object_size(length); 215 ArrayKlass* ak = array_klass(n + dimension(), CHECK_NULL); 216 objArrayOop o = (objArrayOop)Universe::heap()->array_allocate(ak, size, length, 217 /* do_zero */ true, CHECK_NULL); 218 // initialization to null not necessary, area already cleared 219 return o; 220 } 221 222 oop ArrayKlass::component_mirror() const { 223 return java_lang_Class::component_mirror(java_mirror()); 224 } 225 226 jint ArrayKlass::compute_modifier_flags() const { 227 int identity_flag = (Arguments::enable_preview()) ? JVM_ACC_IDENTITY : 0; 228 229 return JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC 230 | identity_flag; 231 } 232 233 // JVMTI support 234 235 jint ArrayKlass::jvmti_class_status() const { 236 return JVMTI_CLASS_STATUS_ARRAY; 237 } 238 239 void ArrayKlass::metaspace_pointers_do(MetaspaceClosure* it) { 240 Klass::metaspace_pointers_do(it); 241 242 ResourceMark rm; 243 log_trace(cds)("Iter(ArrayKlass): %p (%s)", this, external_name()); 244 245 // need to cast away volatile 246 it->push((Klass**)&_higher_dimension); 247 it->push((Klass**)&_lower_dimension); 248 } 249 250 #if INCLUDE_CDS 251 void ArrayKlass::remove_unshareable_info() { 252 Klass::remove_unshareable_info(); 253 if (_higher_dimension != nullptr) { 254 ArrayKlass *ak = higher_dimension(); 255 ak->remove_unshareable_info(); 256 } 257 } 258 259 void ArrayKlass::remove_java_mirror() { 260 Klass::remove_java_mirror(); 261 if (_higher_dimension != nullptr) { 262 ArrayKlass *ak = higher_dimension(); 263 ak->remove_java_mirror(); 264 } 265 } 266 267 void ArrayKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS) { 268 Klass::restore_unshareable_info(loader_data, protection_domain, CHECK); 269 // Klass recreates the component mirror also 270 271 if (_higher_dimension != nullptr) { 272 ArrayKlass *ak = higher_dimension(); 273 log_array_class_load(ak); 274 ak->restore_unshareable_info(loader_data, protection_domain, CHECK); 275 } 276 } 277 278 void ArrayKlass::cds_print_value_on(outputStream* st) const { 279 assert(is_klass(), "must be klass"); 280 st->print(" - array: %s", internal_name()); 281 if (_higher_dimension != nullptr) { 282 ArrayKlass* ak = higher_dimension(); 283 st->cr(); 284 ak->cds_print_value_on(st); 285 } 286 } 287 #endif // INCLUDE_CDS 288 289 void ArrayKlass::log_array_class_load(Klass* k) { 290 LogTarget(Debug, class, load, array) lt; 291 if (lt.is_enabled()) { 292 LogStream ls(lt); 293 ResourceMark rm; 294 ls.print("%s", k->name()->as_klass_external_name()); 295 if (MetaspaceShared::is_shared_dynamic((void*)k)) { 296 ls.print(" source: shared objects file (top)"); 297 } else if (MetaspaceShared::is_shared_static((void*)k)) { 298 ls.print(" source: shared objects file"); 299 } 300 ls.cr(); 301 } 302 } 303 304 // Printing 305 306 void ArrayKlass::print_on(outputStream* st) const { 307 assert(is_klass(), "must be klass"); 308 Klass::print_on(st); 309 } 310 311 void ArrayKlass::print_value_on(outputStream* st) const { 312 assert(is_klass(), "must be klass"); 313 for(int index = 0; index < dimension(); index++) { 314 st->print("[]"); 315 } 316 } 317 318 void ArrayKlass::oop_print_on(oop obj, outputStream* st) { 319 assert(obj->is_array(), "must be array"); 320 Klass::oop_print_on(obj, st); 321 st->print_cr(" - length: %d", arrayOop(obj)->length()); 322 } 323 324 325 // Verification 326 327 void ArrayKlass::verify_on(outputStream* st) { 328 Klass::verify_on(st); 329 } 330 331 void ArrayKlass::oop_verify_on(oop obj, outputStream* st) { 332 guarantee(obj->is_array(), "must be array"); 333 arrayOop a = arrayOop(obj); 334 guarantee(a->length() >= 0, "array with negative length?"); 335 }