1 /* 2 * Copyright (c) 1997, 2023, 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 "classfile/moduleEntry.hpp" 27 #include "classfile/packageEntry.hpp" 28 #include "classfile/symbolTable.hpp" 29 #include "classfile/vmSymbols.hpp" 30 #include "gc/shared/collectedHeap.hpp" 31 #include "gc/shared/collectedHeap.inline.hpp" 32 #include "memory/metadataFactory.hpp" 33 #include "memory/resourceArea.hpp" 34 #include "memory/universe.hpp" 35 #include "oops/arrayKlass.inline.hpp" 36 #include "oops/instanceKlass.hpp" 37 #include "oops/klass.inline.hpp" 38 #include "oops/objArrayKlass.hpp" 39 #include "oops/oop.inline.hpp" 40 #include "oops/typeArrayKlass.inline.hpp" 41 #include "oops/typeArrayOop.inline.hpp" 42 #include "runtime/handles.inline.hpp" 43 #include "utilities/macros.hpp" 44 45 TypeArrayKlass* TypeArrayKlass::create_klass(BasicType type, 46 const char* name_str, TRAPS) { 47 Symbol* sym = nullptr; 48 if (name_str != nullptr) { 49 sym = SymbolTable::new_permanent_symbol(name_str); 50 } 51 52 ClassLoaderData* null_loader_data = ClassLoaderData::the_null_class_loader_data(); 53 54 TypeArrayKlass* ak = TypeArrayKlass::allocate(null_loader_data, type, sym, CHECK_NULL); 55 56 // Call complete_create_array_klass after all instance variables have been initialized. 57 complete_create_array_klass(ak, ak->super(), ModuleEntryTable::javabase_moduleEntry(), CHECK_NULL); 58 59 // Add all classes to our internal class loader list here, 60 // including classes in the bootstrap (null) class loader. 61 // Do this step after creating the mirror so that if the 62 // mirror creation fails, loaded_classes_do() doesn't find 63 // an array class without a mirror. 64 null_loader_data->add_class(ak); 65 JFR_ONLY(ASSIGN_PRIMITIVE_CLASS_ID(ak);) 66 return ak; 67 } 68 69 TypeArrayKlass* TypeArrayKlass::allocate(ClassLoaderData* loader_data, BasicType type, Symbol* name, TRAPS) { 70 assert(TypeArrayKlass::header_size() <= InstanceKlass::header_size(), 71 "array klasses must be same size as InstanceKlass"); 72 73 int size = ArrayKlass::static_size(TypeArrayKlass::header_size()); 74 75 return new (loader_data, size, THREAD) TypeArrayKlass(type, name); 76 } 77 78 TypeArrayKlass::TypeArrayKlass(BasicType type, Symbol* name) : ArrayKlass(name, Kind) { 79 set_layout_helper(array_layout_helper(type)); 80 assert(is_array_klass(), "sanity"); 81 assert(is_typeArray_klass(), "sanity"); 82 83 set_max_length(arrayOopDesc::max_array_length(type)); 84 assert(size() >= TypeArrayKlass::header_size(), "bad size"); 85 86 set_class_loader_data(ClassLoaderData::the_null_class_loader_data()); 87 } 88 89 typeArrayOop TypeArrayKlass::allocate_common(int length, bool do_zero, TRAPS) { 90 assert(log2_element_size() >= 0, "bad scale"); 91 check_array_allocation_length(length, max_length(), CHECK_NULL); 92 size_t size = typeArrayOopDesc::object_size(layout_helper(), length); 93 return (typeArrayOop)Universe::heap()->array_allocate(this, size, length, 94 do_zero, CHECK_NULL); 95 } 96 97 oop TypeArrayKlass::multi_allocate(int rank, jint* last_size, TRAPS) { 98 // For typeArrays this is only called for the last dimension 99 assert(rank == 1, "just checking"); 100 int length = *last_size; 101 return allocate(length, THREAD); 102 } 103 104 105 void TypeArrayKlass::copy_array(arrayOop s, int src_pos, arrayOop d, int dst_pos, int length, TRAPS) { 106 assert(s->is_typeArray(), "must be type array"); 107 108 // Check destination type. 109 if (!d->is_typeArray()) { 110 ResourceMark rm(THREAD); 111 stringStream ss; 112 if (d->is_objArray()) { 113 ss.print("arraycopy: type mismatch: can not copy %s[] into object array[]", 114 type2name_tab[ArrayKlass::cast(s->klass())->element_type()]); 115 } else { 116 ss.print("arraycopy: destination type %s is not an array", d->klass()->external_name()); 117 } 118 THROW_MSG(vmSymbols::java_lang_ArrayStoreException(), ss.as_string()); 119 } 120 if (element_type() != TypeArrayKlass::cast(d->klass())->element_type()) { 121 ResourceMark rm(THREAD); 122 stringStream ss; 123 ss.print("arraycopy: type mismatch: can not copy %s[] into %s[]", 124 type2name_tab[ArrayKlass::cast(s->klass())->element_type()], 125 type2name_tab[ArrayKlass::cast(d->klass())->element_type()]); 126 THROW_MSG(vmSymbols::java_lang_ArrayStoreException(), ss.as_string()); 127 } 128 129 // Check if all offsets and lengths are non negative. 130 if (src_pos < 0 || dst_pos < 0 || length < 0) { 131 // Pass specific exception reason. 132 ResourceMark rm(THREAD); 133 stringStream ss; 134 if (src_pos < 0) { 135 ss.print("arraycopy: source index %d out of bounds for %s[%d]", 136 src_pos, type2name_tab[ArrayKlass::cast(s->klass())->element_type()], s->length()); 137 } else if (dst_pos < 0) { 138 ss.print("arraycopy: destination index %d out of bounds for %s[%d]", 139 dst_pos, type2name_tab[ArrayKlass::cast(d->klass())->element_type()], d->length()); 140 } else { 141 ss.print("arraycopy: length %d is negative", length); 142 } 143 THROW_MSG(vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), ss.as_string()); 144 } 145 // Check if the ranges are valid 146 if ((((unsigned int) length + (unsigned int) src_pos) > (unsigned int) s->length()) || 147 (((unsigned int) length + (unsigned int) dst_pos) > (unsigned int) d->length())) { 148 // Pass specific exception reason. 149 ResourceMark rm(THREAD); 150 stringStream ss; 151 if (((unsigned int) length + (unsigned int) src_pos) > (unsigned int) s->length()) { 152 ss.print("arraycopy: last source index %u out of bounds for %s[%d]", 153 (unsigned int) length + (unsigned int) src_pos, 154 type2name_tab[ArrayKlass::cast(s->klass())->element_type()], s->length()); 155 } else { 156 ss.print("arraycopy: last destination index %u out of bounds for %s[%d]", 157 (unsigned int) length + (unsigned int) dst_pos, 158 type2name_tab[ArrayKlass::cast(d->klass())->element_type()], d->length()); 159 } 160 THROW_MSG(vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), ss.as_string()); 161 } 162 // Check zero copy 163 if (length == 0) 164 return; 165 166 // This is an attempt to make the copy_array fast. 167 int l2es = log2_element_size(); 168 size_t src_offset = arrayOopDesc::base_offset_in_bytes(element_type()) + ((size_t)src_pos << l2es); 169 size_t dst_offset = arrayOopDesc::base_offset_in_bytes(element_type()) + ((size_t)dst_pos << l2es); 170 ArrayAccess<ARRAYCOPY_ATOMIC>::arraycopy<void>(s, src_offset, d, dst_offset, (size_t)length << l2es); 171 } 172 173 // create a klass of array holding typeArrays 174 Klass* TypeArrayKlass::array_klass(int n, TRAPS) { 175 int dim = dimension(); 176 assert(dim <= n, "check order of chain"); 177 if (dim == n) 178 return this; 179 180 // lock-free read needs acquire semantics 181 if (higher_dimension_acquire() == nullptr) { 182 183 ResourceMark rm; 184 JavaThread *jt = THREAD; 185 { 186 // Atomic create higher dimension and link into list 187 MutexLocker mu(THREAD, MultiArray_lock); 188 189 if (higher_dimension() == nullptr) { 190 Klass* oak = ObjArrayKlass::allocate_objArray_klass( 191 class_loader_data(), dim + 1, this, CHECK_NULL); 192 ObjArrayKlass* h_ak = ObjArrayKlass::cast(oak); 193 h_ak->set_lower_dimension(this); 194 // use 'release' to pair with lock-free load 195 release_set_higher_dimension(h_ak); 196 assert(h_ak->is_objArray_klass(), "incorrect initialization of ObjArrayKlass"); 197 } 198 } 199 } 200 201 ObjArrayKlass* h_ak = ObjArrayKlass::cast(higher_dimension()); 202 THREAD->check_possible_safepoint(); 203 return h_ak->array_klass(n, THREAD); 204 } 205 206 // return existing klass of array holding typeArrays 207 Klass* TypeArrayKlass::array_klass_or_null(int n) { 208 int dim = dimension(); 209 assert(dim <= n, "check order of chain"); 210 if (dim == n) 211 return this; 212 213 // lock-free read needs acquire semantics 214 if (higher_dimension_acquire() == nullptr) { 215 return nullptr; 216 } 217 218 ObjArrayKlass* h_ak = ObjArrayKlass::cast(higher_dimension()); 219 return h_ak->array_klass_or_null(n); 220 } 221 222 Klass* TypeArrayKlass::array_klass(TRAPS) { 223 return array_klass(dimension() + 1, THREAD); 224 } 225 226 Klass* TypeArrayKlass::array_klass_or_null() { 227 return array_klass_or_null(dimension() + 1); 228 } 229 230 size_t TypeArrayKlass::oop_size(oop obj) const { 231 // In this assert, we cannot safely access the Klass* with compact headers. 232 assert(UseCompactObjectHeaders || obj->is_typeArray(),"must be a type array"); 233 typeArrayOop t = typeArrayOop(obj); 234 return t->object_size(this); 235 } 236 237 void TypeArrayKlass::initialize(TRAPS) { 238 // Nothing to do. Having this function is handy since objArrayKlasses can be 239 // initialized by calling initialize on their bottom_klass, see ObjArrayKlass::initialize 240 } 241 242 const char* TypeArrayKlass::external_name(BasicType type) { 243 switch (type) { 244 case T_BOOLEAN: return "[Z"; 245 case T_CHAR: return "[C"; 246 case T_FLOAT: return "[F"; 247 case T_DOUBLE: return "[D"; 248 case T_BYTE: return "[B"; 249 case T_SHORT: return "[S"; 250 case T_INT: return "[I"; 251 case T_LONG: return "[J"; 252 default: ShouldNotReachHere(); 253 } 254 return nullptr; 255 } 256 257 258 // Printing 259 260 void TypeArrayKlass::print_on(outputStream* st) const { 261 #ifndef PRODUCT 262 assert(is_klass(), "must be klass"); 263 print_value_on(st); 264 Klass::print_on(st); 265 #endif //PRODUCT 266 } 267 268 void TypeArrayKlass::print_value_on(outputStream* st) const { 269 assert(is_klass(), "must be klass"); 270 st->print("{type array "); 271 BasicType bt = element_type(); 272 if (bt == T_BOOLEAN) { 273 st->print("bool"); 274 } else { 275 st->print("%s", type2name_tab[bt]); 276 } 277 st->print("}"); 278 } 279 280 #ifndef PRODUCT 281 282 static void print_boolean_array(typeArrayOop ta, int print_len, outputStream* st) { 283 for (int index = 0; index < print_len; index++) { 284 st->print_cr(" - %3d: %s", index, (ta->bool_at(index) == 0) ? "false" : "true"); 285 } 286 } 287 288 289 static void print_char_array(typeArrayOop ta, int print_len, outputStream* st) { 290 for (int index = 0; index < print_len; index++) { 291 jchar c = ta->char_at(index); 292 st->print_cr(" - %3d: %x %c", index, c, isprint(c) ? c : ' '); 293 } 294 } 295 296 297 static void print_float_array(typeArrayOop ta, int print_len, outputStream* st) { 298 for (int index = 0; index < print_len; index++) { 299 st->print_cr(" - %3d: %g", index, ta->float_at(index)); 300 } 301 } 302 303 304 static void print_double_array(typeArrayOop ta, int print_len, outputStream* st) { 305 for (int index = 0; index < print_len; index++) { 306 st->print_cr(" - %3d: %g", index, ta->double_at(index)); 307 } 308 } 309 310 311 static void print_byte_array(typeArrayOop ta, int print_len, outputStream* st) { 312 for (int index = 0; index < print_len; index++) { 313 jbyte c = ta->byte_at(index); 314 st->print_cr(" - %3d: %x %c", index, c, isprint(c) ? c : ' '); 315 } 316 } 317 318 319 static void print_short_array(typeArrayOop ta, int print_len, outputStream* st) { 320 for (int index = 0; index < print_len; index++) { 321 int v = ta->ushort_at(index); 322 st->print_cr(" - %3d: 0x%x\t %d", index, v, v); 323 } 324 } 325 326 327 static void print_int_array(typeArrayOop ta, int print_len, outputStream* st) { 328 for (int index = 0; index < print_len; index++) { 329 jint v = ta->int_at(index); 330 st->print_cr(" - %3d: 0x%x %d", index, v, v); 331 } 332 } 333 334 335 static void print_long_array(typeArrayOop ta, int print_len, outputStream* st) { 336 for (int index = 0; index < print_len; index++) { 337 jlong v = ta->long_at(index); 338 st->print_cr(" - %3d: 0x%x 0x%x", index, high(v), low(v)); 339 } 340 } 341 342 343 void TypeArrayKlass::oop_print_on(oop obj, outputStream* st) { 344 ArrayKlass::oop_print_on(obj, st); 345 typeArrayOop ta = typeArrayOop(obj); 346 int print_len = MIN2((intx) ta->length(), MaxElementPrintSize); 347 switch (element_type()) { 348 case T_BOOLEAN: print_boolean_array(ta, print_len, st); break; 349 case T_CHAR: print_char_array(ta, print_len, st); break; 350 case T_FLOAT: print_float_array(ta, print_len, st); break; 351 case T_DOUBLE: print_double_array(ta, print_len, st); break; 352 case T_BYTE: print_byte_array(ta, print_len, st); break; 353 case T_SHORT: print_short_array(ta, print_len, st); break; 354 case T_INT: print_int_array(ta, print_len, st); break; 355 case T_LONG: print_long_array(ta, print_len, st); break; 356 default: ShouldNotReachHere(); 357 } 358 int remaining = ta->length() - print_len; 359 if (remaining > 0) { 360 st->print_cr(" - <%d more elements, increase MaxElementPrintSize to print>", remaining); 361 } 362 } 363 364 #endif // PRODUCT 365 366 const char* TypeArrayKlass::internal_name() const { 367 return Klass::external_name(); 368 } 369 370 // A TypeArrayKlass is an array of a primitive type, its defining module is java.base 371 ModuleEntry* TypeArrayKlass::module() const { 372 return ModuleEntryTable::javabase_moduleEntry(); 373 } 374 375 PackageEntry* TypeArrayKlass::package() const { 376 return nullptr; 377 }