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