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