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  *
 23  */
 24 
 25 #include "classfile/moduleEntry.hpp"
 26 #include "classfile/packageEntry.hpp"
 27 #include "classfile/symbolTable.hpp"
 28 #include "classfile/vmSymbols.hpp"
 29 #include "gc/shared/collectedHeap.hpp"
 30 #include "gc/shared/collectedHeap.inline.hpp"
 31 #include "memory/metadataFactory.hpp"
 32 #include "memory/resourceArea.hpp"
 33 #include "memory/universe.hpp"
 34 #include "oops/arrayKlass.hpp"
 35 #include "oops/instanceKlass.hpp"
 36 #include "oops/klass.inline.hpp"
 37 #include "oops/objArrayKlass.hpp"
 38 #include "oops/oop.inline.hpp"
 39 #include "oops/typeArrayKlass.inline.hpp"
 40 #include "oops/typeArrayOop.inline.hpp"
 41 #include "runtime/arguments.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_klass(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_klass(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 u2 TypeArrayKlass::compute_modifier_flags() const {
 79   u2 identity_flag = (Arguments::is_valhalla_enabled()) ? JVM_ACC_IDENTITY : 0;
 80 
 81   return JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC
 82                     | identity_flag;
 83 }
 84 
 85 TypeArrayKlass::TypeArrayKlass(BasicType type, Symbol* name)
 86     : ArrayKlass(1, name, Kind, ArrayProperties::Default()) {
 87   set_layout_helper(array_layout_helper(type));
 88   assert(is_array_klass(), "sanity");
 89   assert(is_typeArray_klass(), "sanity");
 90 
 91   set_max_length(arrayOopDesc::max_array_length(type));
 92   assert(size() >= TypeArrayKlass::header_size(), "bad size");
 93 
 94   set_class_loader_data(ClassLoaderData::the_null_class_loader_data());
 95 }
 96 
 97 typeArrayOop TypeArrayKlass::allocate_common(int length, bool do_zero, TRAPS) {
 98   assert(log2_element_size() >= 0, "bad scale");
 99   check_array_allocation_length(length, max_length(), CHECK_NULL);
100   size_t size = typeArrayOopDesc::object_size(layout_helper(), length);
101   return (typeArrayOop)Universe::heap()->array_allocate(this, size, length,
102                                                         do_zero, CHECK_NULL);
103 }
104 
105 oop TypeArrayKlass::multi_allocate(int rank, jint* last_size, TRAPS) {
106   assert(rank == 1, "just checking");
107   int length = *last_size;
108   return allocate_instance(length, THREAD);
109 }
110 
111 
112 void TypeArrayKlass::copy_array(arrayOop s, int src_pos, arrayOop d, int dst_pos, int length, TRAPS) {
113   assert(s->is_typeArray(), "must be type array");
114 
115   // Check destination type.
116   if (!d->is_typeArray()) {
117     ResourceMark rm(THREAD);
118     stringStream ss;
119     if (d->is_objArray()) {
120       ss.print("arraycopy: type mismatch: can not copy %s[] into object array[]",
121                type2name_tab[ArrayKlass::cast(s->klass())->element_type()]);
122     } else {
123       ss.print("arraycopy: destination type %s is not an array", d->klass()->external_name());
124     }
125     THROW_MSG(vmSymbols::java_lang_ArrayStoreException(), ss.as_string());
126   }
127   if (element_type() != TypeArrayKlass::cast(d->klass())->element_type()) {
128     ResourceMark rm(THREAD);
129     stringStream ss;
130     ss.print("arraycopy: type mismatch: can not copy %s[] into %s[]",
131              type2name_tab[ArrayKlass::cast(s->klass())->element_type()],
132              type2name_tab[ArrayKlass::cast(d->klass())->element_type()]);
133     THROW_MSG(vmSymbols::java_lang_ArrayStoreException(), ss.as_string());
134   }
135 
136   // Check if all offsets and lengths are non negative.
137   if (src_pos < 0 || dst_pos < 0 || length < 0) {
138     // Pass specific exception reason.
139     ResourceMark rm(THREAD);
140     stringStream ss;
141     if (src_pos < 0) {
142       ss.print("arraycopy: source index %d out of bounds for %s[%d]",
143                src_pos, type2name_tab[ArrayKlass::cast(s->klass())->element_type()], s->length());
144     } else if (dst_pos < 0) {
145       ss.print("arraycopy: destination index %d out of bounds for %s[%d]",
146                dst_pos, type2name_tab[ArrayKlass::cast(d->klass())->element_type()], d->length());
147     } else {
148       ss.print("arraycopy: length %d is negative", length);
149     }
150     THROW_MSG(vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), ss.as_string());
151   }
152   // Check if the ranges are valid
153   if ((((unsigned int) length + (unsigned int) src_pos) > (unsigned int) s->length()) ||
154       (((unsigned int) length + (unsigned int) dst_pos) > (unsigned int) d->length())) {
155     // Pass specific exception reason.
156     ResourceMark rm(THREAD);
157     stringStream ss;
158     if (((unsigned int) length + (unsigned int) src_pos) > (unsigned int) s->length()) {
159       ss.print("arraycopy: last source index %u out of bounds for %s[%d]",
160                (unsigned int) length + (unsigned int) src_pos,
161                type2name_tab[ArrayKlass::cast(s->klass())->element_type()], s->length());
162     } else {
163       ss.print("arraycopy: last destination index %u out of bounds for %s[%d]",
164                (unsigned int) length + (unsigned int) dst_pos,
165                type2name_tab[ArrayKlass::cast(d->klass())->element_type()], d->length());
166     }
167     THROW_MSG(vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), ss.as_string());
168   }
169   // Check zero copy
170   if (length == 0)
171     return;
172 
173   // This is an attempt to make the copy_array fast.
174   int l2es = log2_element_size();
175   size_t src_offset = arrayOopDesc::base_offset_in_bytes(element_type()) + ((size_t)src_pos << l2es);
176   size_t dst_offset = arrayOopDesc::base_offset_in_bytes(element_type()) + ((size_t)dst_pos << l2es);
177   ArrayAccess<ARRAYCOPY_ATOMIC>::arraycopy<void>(s, src_offset, d, dst_offset, (size_t)length << l2es);
178 }
179 
180 size_t TypeArrayKlass::oop_size(oop obj) const {
181   // In this assert, we cannot safely access the Klass* with compact headers.
182   assert(UseCompactObjectHeaders || obj->is_typeArray(),"must be a type array");
183   typeArrayOop t = typeArrayOop(obj);
184   return t->object_size(this);
185 }
186 
187 void TypeArrayKlass::initialize(TRAPS) {
188   // Nothing to do. Having this function is handy since objArrayKlasses can be
189   // initialized by calling initialize on their bottom_klass, see ObjArrayKlass::initialize
190 }
191 
192 const char* TypeArrayKlass::external_name(BasicType type) {
193   switch (type) {
194     case T_BOOLEAN: return "[Z";
195     case T_CHAR:    return "[C";
196     case T_FLOAT:   return "[F";
197     case T_DOUBLE:  return "[D";
198     case T_BYTE:    return "[B";
199     case T_SHORT:   return "[S";
200     case T_INT:     return "[I";
201     case T_LONG:    return "[J";
202     default: ShouldNotReachHere();
203   }
204   return nullptr;
205 }
206 
207 
208 // Printing
209 
210 void TypeArrayKlass::print_on(outputStream* st) const {
211 #ifndef PRODUCT
212   assert(is_klass(), "must be klass");
213   print_value_on(st);
214   Klass::print_on(st);
215 #endif //PRODUCT
216 }
217 
218 void TypeArrayKlass::print_value_on(outputStream* st) const {
219   assert(is_klass(), "must be klass");
220   st->print("{type array ");
221   BasicType bt = element_type();
222   if (bt == T_BOOLEAN) {
223     st->print("bool");
224   } else {
225     st->print("%s", type2name_tab[bt]);
226   }
227   st->print("}");
228 }
229 
230 static void print_boolean_array(typeArrayOop ta, int print_len, outputStream* st) {
231   for (int index = 0; index < print_len; index++) {
232     st->print_cr(" - %3d: %s", index, (ta->bool_at(index) == 0) ? "false" : "true");
233   }
234 }
235 
236 
237 static void print_char_array(typeArrayOop ta, int print_len, outputStream* st) {
238   for (int index = 0; index < print_len; index++) {
239     jchar c = ta->char_at(index);
240     st->print_cr(" - %3d: %x %c", index, c, isprint(c) ? c : ' ');
241   }
242 }
243 
244 
245 static void print_float_array(typeArrayOop ta, int print_len, outputStream* st) {
246   for (int index = 0; index < print_len; index++) {
247     st->print_cr(" - %3d: %g", index, ta->float_at(index));
248   }
249 }
250 
251 
252 static void print_double_array(typeArrayOop ta, int print_len, outputStream* st) {
253   for (int index = 0; index < print_len; index++) {
254     st->print_cr(" - %3d: %g", index, ta->double_at(index));
255   }
256 }
257 
258 
259 static void print_byte_array(typeArrayOop ta, int print_len, outputStream* st) {
260   for (int index = 0; index < print_len; index++) {
261     jbyte c = ta->byte_at(index);
262     st->print_cr(" - %3d: %x %c", index, c, isprint(c) ? c : ' ');
263   }
264 }
265 
266 
267 static void print_short_array(typeArrayOop ta, int print_len, outputStream* st) {
268   for (int index = 0; index < print_len; index++) {
269     int v = ta->ushort_at(index);
270     st->print_cr(" - %3d: 0x%x\t %d", index, v, v);
271   }
272 }
273 
274 
275 static void print_int_array(typeArrayOop ta, int print_len, outputStream* st) {
276   for (int index = 0; index < print_len; index++) {
277     jint v = ta->int_at(index);
278     st->print_cr(" - %3d: 0x%x %d", index, v, v);
279   }
280 }
281 
282 
283 static void print_long_array(typeArrayOop ta, int print_len, outputStream* st) {
284   for (int index = 0; index < print_len; index++) {
285     jlong v = ta->long_at(index);
286     st->print_cr(" - %3d: 0x%x 0x%x", index, high(v), low(v));
287   }
288 }
289 
290 
291 void TypeArrayKlass::oop_print_on(oop obj, outputStream* st) {
292   ArrayKlass::oop_print_on(obj, st);
293   oop_print_elements_on(typeArrayOop(obj), st);
294 }
295 
296 void TypeArrayKlass::oop_print_elements_on(typeArrayOop ta, outputStream* st) {
297   int print_len = MIN2(ta->length(), MaxElementPrintSize);
298   switch (element_type()) {
299     case T_BOOLEAN: print_boolean_array(ta, print_len, st); break;
300     case T_CHAR:    print_char_array(ta, print_len, st);    break;
301     case T_FLOAT:   print_float_array(ta, print_len, st);   break;
302     case T_DOUBLE:  print_double_array(ta, print_len, st);  break;
303     case T_BYTE:    print_byte_array(ta, print_len, st);    break;
304     case T_SHORT:   print_short_array(ta, print_len, st);   break;
305     case T_INT:     print_int_array(ta, print_len, st);     break;
306     case T_LONG:    print_long_array(ta, print_len, st);    break;
307     default: ShouldNotReachHere();
308   }
309   int remaining = ta->length() - print_len;
310   if (remaining > 0) {
311     st->print_cr(" - <%d more elements, increase MaxElementPrintSize to print>", remaining);
312   }
313 }
314 
315 const char* TypeArrayKlass::internal_name() const {
316   return Klass::external_name();
317 }
318 
319 // A TypeArrayKlass is an array of a primitive type, its defining module is java.base
320 ModuleEntry* TypeArrayKlass::module() const {
321   return ModuleEntryTable::javabase_moduleEntry();
322 }
323 
324 PackageEntry* TypeArrayKlass::package() const {
325   return nullptr;
326 }