1 /*
   2  * Copyright (c) 2017, 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/symbolTable.hpp"
  27 #include "classfile/systemDictionary.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 "memory/universe.inline.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/arrayKlass.hpp"
  40 #include "oops/arrayOop.hpp"
  41 #include "oops/valueKlass.hpp"
  42 #include "oops/valueArrayOop.hpp"
  43 #include "oops/verifyOopClosure.hpp"
  44 #include "runtime/handles.inline.hpp"
  45 #include "runtime/orderAccess.inline.hpp"
  46 #include "utilities/macros.hpp"
  47 
  48 #include "oops/valueArrayKlass.hpp"
  49 
  50 // Allocation...
  51 
  52 ValueArrayKlass::ValueArrayKlass(KlassHandle element_klass_handle, Symbol* name) : ArrayKlass(name) {
  53   Klass* k = element_klass_handle();
  54   assert(k->is_value(), "Expected Value");
  55 
  56   set_element_klass(ValueKlass::cast(k));
  57   set_class_loader_data(k->class_loader_data());
  58   set_layout_helper(array_layout_helper(element_klass()));
  59 
  60   assert((1 << Klass::layout_helper_log2_element_size(layout_helper())) >=
  61       element_value_store_size(), "sanity");
  62   assert(is_array_klass(), "sanity");
  63   assert(is_valueArray_klass(), "sanity");
  64 
  65   CMH("tweak name symbol refcnt ?")
  66 #ifndef PRODUCT
  67   if (PrintValueArrayLayout) {
  68     print();
  69   }
  70 #endif
  71 }
  72 
  73 void ValueArrayKlass::set_element_klass(ValueKlass* k) {
  74   _element_klass = k;
  75   _element_value_store_size = k->raw_value_byte_size();
  76 }
  77 
  78 ValueArrayKlass* ValueArrayKlass::allocate_klass(KlassHandle      element_klass,
  79                                                  Symbol*          name,
  80                                                  TRAPS) {
  81   assert(ValueArrayFlatten, "Flatten array not allowed");
  82   assert(ValueKlass::cast(element_klass())->is_atomic() || (!ValueArrayAtomicAccess), "Atomic by-default");
  83 
  84   ClassLoaderData* loader_data = element_klass->class_loader_data();
  85   int size = ArrayKlass::static_size(ValueArrayKlass::header_size());
  86   ValueArrayKlass* vak = new (loader_data, size, THREAD) ValueArrayKlass(element_klass, name);
  87   if (vak == NULL) {
  88     return NULL;
  89   }
  90   loader_data->add_class(vak);
  91   complete_create_array_klass(vak, vak->super(), CHECK_NULL);
  92   return vak;
  93 }
  94 
  95 ValueArrayKlass* ValueArrayKlass::allocate_klass(KlassHandle element_klass, TRAPS) {
  96   Symbol* name = ArrayKlass::create_element_klass_array_name(element_klass, CHECK_NULL);
  97   return allocate_klass(element_klass, name, THREAD);
  98 }
  99 
 100 void ValueArrayKlass::initialize(TRAPS) {
 101   element_klass()->initialize(THREAD);
 102 }
 103 
 104 // Oops allocation...
 105 oop ValueArrayKlass::allocate(int length, bool do_zero, TRAPS) {
 106   if (length < 0) {
 107     THROW_0(vmSymbols::java_lang_NegativeArraySizeException());
 108   }
 109   if (length > max_elements()) {
 110     report_java_out_of_memory("Requested array size exceeds VM limit");
 111     JvmtiExport::post_array_size_exhausted();
 112     THROW_OOP_0(Universe::out_of_memory_error_array_size());
 113   }
 114 
 115   size_t size = valueArrayOopDesc::object_size(layout_helper(), length);
 116   KlassHandle h_k(THREAD, this);
 117   if (do_zero) {
 118     return CollectedHeap::array_allocate(h_k, (int)size, length, CHECK_NULL);
 119   } else {
 120     return CollectedHeap::array_allocate_nozero(h_k, (int)size, length, CHECK_NULL);
 121   }
 122 }
 123 
 124 
 125 oop ValueArrayKlass::multi_allocate(int rank, jint* last_size, TRAPS) {
 126   // For valueArrays this is only called for the last dimension
 127   assert(rank == 1, "just checking");
 128   int length = *last_size;
 129   return allocate(length, true, THREAD);
 130 }
 131 
 132 jint ValueArrayKlass::array_layout_helper(ValueKlass* vk) {
 133   BasicType etype = T_VALUETYPE;
 134   int atag  = _lh_array_tag_type_value;
 135   int esize = upper_log2(vk->raw_value_byte_size());
 136   int hsize = arrayOopDesc::base_offset_in_bytes(etype);
 137 
 138   int lh = (atag       << _lh_array_tag_shift)
 139     |      (1          << _lh_valuetype_bit)
 140     |      (hsize      << _lh_header_size_shift)
 141     |      ((int)etype << _lh_element_type_shift)
 142     |      ((esize)    << _lh_log2_element_size_shift);
 143 
 144   assert(lh < (int)_lh_neutral_value, "must look like an array layout");
 145   assert(layout_helper_is_array(lh), "correct kind");
 146   assert(layout_helper_is_valueArray(lh), "correct kind");


 147   assert(layout_helper_header_size(lh) == hsize, "correct decode");
 148   assert(layout_helper_element_type(lh) == etype, "correct decode");
 149   assert(layout_helper_is_typeArray(lh), "correct kind");
 150   assert(layout_helper_log2_element_size(lh) == esize, "correct decode");
 151   assert((1 << esize) < BytesPerLong || is_size_aligned(hsize, HeapWordsPerLong), "unaligned base");
 152 
 153   return lh;
 154 }
 155 
 156 int ValueArrayKlass::oop_size(oop obj) const {
 157   assert(obj->is_valueArray(),"must be a value array");
 158   valueArrayOop array = valueArrayOop(obj);
 159   return array->object_size();
 160 }
 161 
 162 jint ValueArrayKlass::max_elements() const {
 163   return arrayOopDesc::max_array_length(arrayOopDesc::header_size(T_VALUETYPE), element_byte_size());
 164 }
 165 
 166 oop ValueArrayKlass::protection_domain() const {
 167   return element_klass()->protection_domain();
 168 }
 169 
 170 void ValueArrayKlass::copy_array(arrayOop s, int src_pos,
 171                                  arrayOop d, int dst_pos, int length, TRAPS) {
 172   assert(s->is_valueArray(), "must be value array");
 173 
 174    // Check destination
 175    if (!d->is_valueArray() || element_klass() != ValueArrayKlass::cast(d->klass())->element_klass()) {
 176      THROW(vmSymbols::java_lang_ArrayStoreException());
 177    }
 178 
 179    // Check is all offsets and lengths are non negative
 180    if (src_pos < 0 || dst_pos < 0 || length < 0) {
 181      THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException());
 182    }
 183    // Check if the ranges are valid
 184    if  ( (((unsigned int) length + (unsigned int) src_pos) > (unsigned int) s->length())
 185       || (((unsigned int) length + (unsigned int) dst_pos) > (unsigned int) d->length()) ) {
 186      THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException());
 187    }
 188    // Check zero copy
 189    if (length == 0)
 190      return;
 191 
 192    valueArrayOop sa = valueArrayOop(s);
 193    valueArrayOop da = valueArrayOop(d);
 194    address src = (address) sa->value_at_addr(src_pos, layout_helper());
 195    address dst = (address) da->value_at_addr(dst_pos, layout_helper());
 196    if (contains_oops()) {
 197      int elem_incr = 1 << log2_element_size();
 198      address src_end = src + (length << log2_element_size());
 199      while (src < src_end) {
 200        element_klass()->value_store(src, dst, element_value_store_size(), true, false);
 201        src += elem_incr;
 202        dst += elem_incr;
 203      }
 204    } else {
 205      // we are basically a type array...don't bother limiting element copy
 206      // it would have to be a lot wasted space to be worth value_store() calls, need a setting here ?
 207      Copy::conjoint_memory_atomic(src, dst, (size_t)length << log2_element_size());
 208    }
 209 }
 210 
 211 
 212 Klass* ValueArrayKlass::array_klass_impl(bool or_null, int n, TRAPS) {
 213 
 214   assert(dimension() <= n, "check order of chain");
 215   int dim = dimension();
 216   if (dim == n) return this;
 217 
 218   if (higher_dimension() == NULL) {
 219     if (or_null)  return NULL;
 220 
 221     ResourceMark rm;
 222     JavaThread *jt = (JavaThread *)THREAD;
 223     {
 224       MutexLocker mc(Compile_lock, THREAD);   // for vtables
 225       // Ensure atomic creation of higher dimensions
 226       MutexLocker mu(MultiArray_lock, THREAD);
 227 
 228       // Check if another thread beat us
 229       if (higher_dimension() == NULL) {
 230 
 231         // Create multi-dim klass object and link them together
 232         Klass* k =
 233           ObjArrayKlass::allocate_objArray_klass(class_loader_data(), dim + 1, this, CHECK_NULL);
 234         ObjArrayKlass* ak = ObjArrayKlass::cast(k);
 235         ak->set_lower_dimension(this);
 236         OrderAccess::storestore();
 237         set_higher_dimension(ak);
 238         assert(ak->is_objArray_klass(), "incorrect initialization of ObjArrayKlass");
 239       }
 240     }
 241   } else {
 242     CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops());
 243   }
 244 
 245   ObjArrayKlass *ak = ObjArrayKlass::cast(higher_dimension());
 246   if (or_null) {
 247     return ak->array_klass_or_null(n);
 248   }
 249   return ak->array_klass(n, THREAD);
 250 }
 251 
 252 Klass* ValueArrayKlass::array_klass_impl(bool or_null, TRAPS) {
 253   return array_klass_impl(or_null, dimension() +  1, THREAD);
 254 }
 255 
 256 void ValueArrayKlass::print_on(outputStream* st) const {
 257 #ifndef PRODUCT
 258   assert(!is_objArray_klass(), "Unimplemented");
 259 
 260   st->print("Value Type Array: ");
 261   Klass::print_on(st);
 262 
 263   st->print(" - element klass: ");
 264   element_klass()->print_value_on(st);
 265   st->cr();
 266 
 267   int elem_size = element_byte_size();
 268   st->print(" - element size %i ", elem_size);
 269   st->print("aligned layout size %i", 1 << layout_helper_log2_element_size(layout_helper()));
 270   st->cr();
 271 #endif //PRODUCT
 272 }
 273 
 274 void ValueArrayKlass::print_value_on(outputStream* st) const {
 275   assert(is_klass(), "must be klass");
 276 
 277   element_klass()->print_value_on(st);
 278   st->print("[]");
 279 }
 280 
 281 
 282 #ifndef PRODUCT
 283 void ValueArrayKlass::oop_print_on(oop obj, outputStream* st) {
 284   ArrayKlass::oop_print_on(obj, st);
 285   valueArrayOop va = valueArrayOop(obj);
 286   ValueKlass* vk = element_klass();
 287   int print_len = MIN2((intx) va->length(), MaxElementPrintSize);
 288   for(int index = 0; index < print_len; index++) {
 289     int off = (address) va->value_at_addr(index, layout_helper()) - (address) obj;
 290     st->print_cr(" - Index %3d offset %3d: ", index, off);
 291     oop obj = (oop) ((address)va->value_at_addr(index, layout_helper()) - vk->first_field_offset());
 292     FieldPrinter print_field(st, obj);
 293     vk->do_nonstatic_fields(&print_field);
 294     st->cr();
 295   }
 296   int remaining = va->length() - print_len;
 297   if (remaining > 0) {
 298     st->print_cr(" - <%d more elements, increase MaxElementPrintSize to print>", remaining);
 299   }
 300 }
 301 #endif //PRODUCT
 302 
 303 void ValueArrayKlass::oop_print_value_on(oop obj, outputStream* st) {
 304   assert(obj->is_valueArray(), "must be valueArray");
 305   st->print("a ");
 306   element_klass()->print_value_on(st);
 307   int len = valueArrayOop(obj)->length();
 308   st->print("[%d] ", len);
 309   obj->print_address_on(st);
 310   if (NOT_PRODUCT(PrintOopAddress ||) PrintMiscellaneous && (WizardMode || Verbose)) {
 311     int lh = layout_helper();
 312     st->print("{");
 313     for (int i = 0; i < len; i++) {
 314       if (i > 4) {
 315         st->print("..."); break;
 316       }
 317       st->print(" " INTPTR_FORMAT, (intptr_t)(void*)valueArrayOop(obj)->value_at_addr(i , lh));
 318     }
 319     st->print(" }");
 320   }
 321 }
 322 
 323 // Verification
 324 
 325 void ValueArrayKlass::oop_verify_on(oop obj, outputStream* st) {
 326   ArrayKlass::oop_verify_on(obj, st);
 327   guarantee(obj->is_valueArray(), "must be valueArray");
 328 
 329   if (element_klass()->contains_oops()) {
 330     valueArrayOop va = valueArrayOop(obj);
 331     NoHeaderExtendedOopClosure wrapClosure(&VerifyOopClosure::verify_oop);
 332     va->oop_iterate(&wrapClosure);
 333   }
 334 }
 335 
 336 void ValueArrayKlass::verify_on(outputStream* st) {
 337   ArrayKlass::verify_on(st);
 338   guarantee(element_klass()->is_value(), "should be value type klass");
 339 }
--- EOF ---