1 /* 2 * Copyright (c) 1997, 2015, 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.inline.hpp" 30 #include "gc/shared/specialized_oop_closures.hpp" 31 #include "memory/iterator.inline.hpp" 32 #include "memory/metadataFactory.hpp" 33 #include "memory/oopFactory.hpp" 34 #include "memory/resourceArea.hpp" 35 #include "memory/universe.inline.hpp" 36 #include "oops/instanceKlass.hpp" 37 #include "oops/klass.inline.hpp" 38 #include "oops/objArrayKlass.inline.hpp" 39 #include "oops/objArrayOop.inline.hpp" 40 #include "oops/oop.inline.hpp" 41 #include "oops/symbol.hpp" 42 #include "runtime/handles.inline.hpp" 43 #include "runtime/mutexLocker.hpp" 44 #include "runtime/orderAccess.inline.hpp" 45 #include "utilities/copy.hpp" 46 #include "utilities/macros.hpp" 47 48 ObjArrayKlass* ObjArrayKlass::allocate(ClassLoaderData* loader_data, int n, KlassHandle klass_handle, Symbol* name, TRAPS) { 49 assert(ObjArrayKlass::header_size() <= InstanceKlass::header_size(), 50 "array klasses must be same size as InstanceKlass"); 51 52 int size = ArrayKlass::static_size(ObjArrayKlass::header_size()); 53 54 return new (loader_data, size, THREAD) ObjArrayKlass(n, klass_handle, name); 55 } 56 57 Klass* ObjArrayKlass::allocate_objArray_klass(ClassLoaderData* loader_data, 58 int n, KlassHandle element_klass, TRAPS) { 59 60 // Eagerly allocate the direct array supertype. 61 KlassHandle super_klass = KlassHandle(); 62 if (!Universe::is_bootstrapping() || SystemDictionary::Object_klass_loaded()) { 63 KlassHandle element_super (THREAD, element_klass->super()); 64 if (element_super.not_null()) { 65 // The element type has a direct super. E.g., String[] has direct super of Object[]. 66 super_klass = KlassHandle(THREAD, element_super->array_klass_or_null()); 67 bool supers_exist = super_klass.not_null(); 68 // Also, see if the element has secondary supertypes. 69 // We need an array type for each. 70 Array<Klass*>* element_supers = element_klass->secondary_supers(); 71 for( int i = element_supers->length()-1; i >= 0; i-- ) { 72 Klass* elem_super = element_supers->at(i); 73 if (elem_super->array_klass_or_null() == NULL) { 74 supers_exist = false; 75 break; 76 } 77 } 78 if (!supers_exist) { 79 // Oops. Not allocated yet. Back out, allocate it, and retry. 80 KlassHandle ek; 81 { 82 MutexUnlocker mu(MultiArray_lock); 83 MutexUnlocker mc(Compile_lock); // for vtables 84 Klass* sk = element_super->array_klass(CHECK_0); 85 super_klass = KlassHandle(THREAD, sk); 86 for( int i = element_supers->length()-1; i >= 0; i-- ) { 87 KlassHandle elem_super (THREAD, element_supers->at(i)); 88 elem_super->array_klass(CHECK_0); 89 } 90 // Now retry from the beginning 91 Klass* klass_oop = element_klass->array_klass(n, CHECK_0); 92 // Create a handle because the enclosing brace, when locking 93 // can cause a gc. Better to have this function return a Handle. 94 ek = KlassHandle(THREAD, klass_oop); 95 } // re-lock 96 return ek(); 97 } 98 } else { 99 // The element type is already Object. Object[] has direct super of Object. 100 super_klass = KlassHandle(THREAD, SystemDictionary::Object_klass()); 101 } 102 } 103 104 // Create type name for klass. 105 Symbol* name = ArrayKlass::create_element_klass_array_name(element_klass, CHECK_NULL); 106 107 // Initialize instance variables 108 ObjArrayKlass* oak = ObjArrayKlass::allocate(loader_data, n, element_klass, name, CHECK_0); 109 110 // Add all classes to our internal class loader list here, 111 // including classes in the bootstrap (NULL) class loader. 112 // GC walks these as strong roots. 113 loader_data->add_class(oak); 114 115 // Call complete_create_array_klass after all instance variables has been initialized. 116 ArrayKlass::complete_create_array_klass(oak, super_klass, CHECK_0); 117 118 return oak; 119 } 120 121 ObjArrayKlass::ObjArrayKlass(int n, KlassHandle element_klass, Symbol* name) : ArrayKlass(name) { 122 this->set_dimension(n); 123 this->set_element_klass(element_klass()); 124 // decrement refcount because object arrays are not explicitly freed. The 125 // InstanceKlass array_name() keeps the name counted while the klass is 126 // loaded. 127 name->decrement_refcount(); 128 129 Klass* bk; 130 if (element_klass->is_objArray_klass()) { 131 bk = ObjArrayKlass::cast(element_klass())->bottom_klass(); 132 } else { 133 bk = element_klass(); 134 } 135 assert(bk != NULL && (bk->is_instance_klass() || 136 bk->is_typeArray_klass() || 137 bk->is_valueArray_klass()), "invalid bottom klass"); 138 this->set_bottom_klass(bk); 139 this->set_class_loader_data(bk->class_loader_data()); 140 141 this->set_layout_helper(array_layout_helper(T_OBJECT)); 142 assert(this->is_array_klass(), "sanity"); 143 assert(this->is_objArray_klass(), "sanity"); 144 } 145 146 int ObjArrayKlass::oop_size(oop obj) const { 147 assert(obj->is_objArray(), "must be object array"); 148 return objArrayOop(obj)->object_size(); 149 } 150 151 objArrayOop ObjArrayKlass::allocate(int length, TRAPS) { 152 if (length >= 0) { 153 if (length <= arrayOopDesc::max_array_length(T_OBJECT)) { 154 int size = objArrayOopDesc::object_size(length); 155 KlassHandle h_k(THREAD, this); 156 return (objArrayOop)CollectedHeap::array_allocate(h_k, size, length, THREAD); 157 } else { 158 report_java_out_of_memory("Requested array size exceeds VM limit"); 159 JvmtiExport::post_array_size_exhausted(); 160 THROW_OOP_0(Universe::out_of_memory_error_array_size()); 161 } 162 } else { 163 THROW_0(vmSymbols::java_lang_NegativeArraySizeException()); 164 } 165 } 166 167 static int multi_alloc_counter = 0; 168 169 oop ObjArrayKlass::multi_allocate(int rank, jint* sizes, TRAPS) { 170 int length = *sizes; 171 if (rank == 1) { // last dim may be valueArray 172 return oopFactory::new_array(element_klass(), length, CHECK_NULL); 173 } 174 guarantee(rank > 1, "Rank below 1"); 175 176 // Call to lower_dimension uses this pointer, so most be called before a 177 // possible GC 178 KlassHandle h_lower_dimension(THREAD, lower_dimension()); 179 // If length < 0 allocate will throw an exception. 180 objArrayOop array = allocate(length, CHECK_NULL); 181 objArrayHandle h_array (THREAD, array); 182 if (length != 0) { 183 for (int index = 0; index < length; index++) { 184 ArrayKlass* ak = ArrayKlass::cast(h_lower_dimension()); 185 oop sub_array = ak->multi_allocate(rank-1, &sizes[1], CHECK_NULL); 186 h_array->obj_at_put(index, sub_array); 187 } 188 } else { 189 // Since this array dimension has zero length, nothing will be 190 // allocated, however the lower dimension values must be checked 191 // for illegal values. 192 for (int i = 0; i < rank - 1; ++i) { 193 sizes += 1; 194 if (*sizes < 0) { 195 THROW_0(vmSymbols::java_lang_NegativeArraySizeException()); 196 } 197 } 198 } 199 return h_array(); 200 } 201 202 // Either oop or narrowOop depending on UseCompressedOops. 203 template <class T> void ObjArrayKlass::do_copy(arrayOop s, T* src, 204 arrayOop d, T* dst, int length, TRAPS) { 205 206 BarrierSet* bs = Universe::heap()->barrier_set(); 207 // For performance reasons, we assume we are that the write barrier we 208 // are using has optimized modes for arrays of references. At least one 209 // of the asserts below will fail if this is not the case. 210 assert(bs->has_write_ref_array_opt(), "Barrier set must have ref array opt"); 211 assert(bs->has_write_ref_array_pre_opt(), "For pre-barrier as well."); 212 213 if (s == d) { 214 // since source and destination are equal we do not need conversion checks. 215 assert(length > 0, "sanity check"); 216 bs->write_ref_array_pre(dst, length); 217 Copy::conjoint_oops_atomic(src, dst, length); 218 } else { 219 // We have to make sure all elements conform to the destination array 220 Klass* bound = ObjArrayKlass::cast(d->klass())->element_klass(); 221 Klass* stype = ObjArrayKlass::cast(s->klass())->element_klass(); 222 if (stype == bound || stype->is_subtype_of(bound)) { 223 // elements are guaranteed to be subtypes, so no check necessary 224 bs->write_ref_array_pre(dst, length); 225 Copy::conjoint_oops_atomic(src, dst, length); 226 } else { 227 // slow case: need individual subtype checks 228 // note: don't use obj_at_put below because it includes a redundant store check 229 T* from = src; 230 T* end = from + length; 231 for (T* p = dst; from < end; from++, p++) { 232 // XXX this is going to be slow. 233 T element = *from; 234 // even slower now 235 bool element_is_null = oopDesc::is_null(element); 236 oop new_val = element_is_null ? oop(NULL) 237 : oopDesc::decode_heap_oop_not_null(element); 238 if (element_is_null || 239 (new_val->klass())->is_subtype_of(bound)) { 240 bs->write_ref_field_pre(p, new_val); 241 *p = element; 242 } else { 243 // We must do a barrier to cover the partial copy. 244 const size_t pd = pointer_delta(p, dst, (size_t)heapOopSize); 245 // pointer delta is scaled to number of elements (length field in 246 // objArrayOop) which we assume is 32 bit. 247 assert(pd == (size_t)(int)pd, "length field overflow"); 248 bs->write_ref_array((HeapWord*)dst, pd); 249 THROW(vmSymbols::java_lang_ArrayStoreException()); 250 return; 251 } 252 } 253 } 254 } 255 bs->write_ref_array((HeapWord*)dst, length); 256 } 257 258 void ObjArrayKlass::copy_array(arrayOop s, int src_pos, arrayOop d, 259 int dst_pos, int length, TRAPS) { 260 assert(s->is_objArray(), "must be obj array"); 261 262 if (!d->is_objArray()) { 263 THROW(vmSymbols::java_lang_ArrayStoreException()); 264 } 265 266 // Check is all offsets and lengths are non negative 267 if (src_pos < 0 || dst_pos < 0 || length < 0) { 268 THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException()); 269 } 270 // Check if the ranges are valid 271 if ( (((unsigned int) length + (unsigned int) src_pos) > (unsigned int) s->length()) 272 || (((unsigned int) length + (unsigned int) dst_pos) > (unsigned int) d->length()) ) { 273 THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException()); 274 } 275 276 // Special case. Boundary cases must be checked first 277 // This allows the following call: copy_array(s, s.length(), d.length(), 0). 278 // This is correct, since the position is supposed to be an 'in between point', i.e., s.length(), 279 // points to the right of the last element. 280 if (length==0) { 281 return; 282 } 283 if (UseCompressedOops) { 284 narrowOop* const src = objArrayOop(s)->obj_at_addr<narrowOop>(src_pos); 285 narrowOop* const dst = objArrayOop(d)->obj_at_addr<narrowOop>(dst_pos); 286 do_copy<narrowOop>(s, src, d, dst, length, CHECK); 287 } else { 288 oop* const src = objArrayOop(s)->obj_at_addr<oop>(src_pos); 289 oop* const dst = objArrayOop(d)->obj_at_addr<oop>(dst_pos); 290 do_copy<oop> (s, src, d, dst, length, CHECK); 291 } 292 } 293 294 Klass* ObjArrayKlass::array_klass_impl(bool or_null, int n, TRAPS) { 295 296 assert(dimension() <= n, "check order of chain"); 297 int dim = dimension(); 298 if (dim == n) return this; 299 300 if (higher_dimension() == NULL) { 301 if (or_null) return NULL; 302 303 ResourceMark rm; 304 JavaThread *jt = (JavaThread *)THREAD; 305 { 306 MutexLocker mc(Compile_lock, THREAD); // for vtables 307 // Ensure atomic creation of higher dimensions 308 MutexLocker mu(MultiArray_lock, THREAD); 309 310 // Check if another thread beat us 311 if (higher_dimension() == NULL) { 312 313 // Create multi-dim klass object and link them together 314 Klass* k = 315 ObjArrayKlass::allocate_objArray_klass(class_loader_data(), dim + 1, this, CHECK_NULL); 316 ObjArrayKlass* ak = ObjArrayKlass::cast(k); 317 ak->set_lower_dimension(this); 318 OrderAccess::storestore(); 319 set_higher_dimension(ak); 320 assert(ak->is_objArray_klass(), "incorrect initialization of ObjArrayKlass"); 321 } 322 } 323 } else { 324 CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops()); 325 } 326 327 ObjArrayKlass *ak = ObjArrayKlass::cast(higher_dimension()); 328 if (or_null) { 329 return ak->array_klass_or_null(n); 330 } 331 return ak->array_klass(n, THREAD); 332 } 333 334 Klass* ObjArrayKlass::array_klass_impl(bool or_null, TRAPS) { 335 return array_klass_impl(or_null, dimension() + 1, THREAD); 336 } 337 338 bool ObjArrayKlass::can_be_primary_super_slow() const { 339 if (!bottom_klass()->can_be_primary_super()) 340 // array of interfaces 341 return false; 342 else 343 return Klass::can_be_primary_super_slow(); 344 } 345 346 GrowableArray<Klass*>* ObjArrayKlass::compute_secondary_supers(int num_extra_slots) { 347 // interfaces = { cloneable_klass, serializable_klass, elemSuper[], ... }; 348 Array<Klass*>* elem_supers = element_klass()->secondary_supers(); 349 int num_elem_supers = elem_supers == NULL ? 0 : elem_supers->length(); 350 int num_secondaries = num_extra_slots + 2 + num_elem_supers; 351 if (num_secondaries == 2) { 352 // Must share this for correct bootstrapping! 353 if (EnableExtraSuper) { 354 set_secondary_supers(Universe::the_empty_klass_array()); 355 } 356 else { 357 set_secondary_supers(Universe::the_array_interfaces_array()); 358 } 359 return NULL; 360 } else { 361 int len = num_elem_supers; 362 if (!EnableExtraSuper) { 363 len += 2; 364 } 365 GrowableArray<Klass*>* secondaries = new GrowableArray<Klass*>(len); 366 if (!EnableExtraSuper) { 367 secondaries->push(SystemDictionary::Cloneable_klass()); 368 secondaries->push(SystemDictionary::Serializable_klass()); 369 } 370 for (int i = 0; i < num_elem_supers; i++) { 371 Klass* elem_super = (Klass*) elem_supers->at(i); 372 Klass* array_super = elem_super->array_klass_or_null(); 373 assert(array_super != NULL, "must already have been created"); 374 secondaries->push(array_super); 375 } 376 return secondaries; 377 } 378 } 379 380 bool ObjArrayKlass::compute_is_subtype_of(Klass* k) { 381 if (!k->is_objArray_klass()) 382 return ArrayKlass::compute_is_subtype_of(k); 383 384 ObjArrayKlass* oak = ObjArrayKlass::cast(k); 385 return element_klass()->is_subtype_of(oak->element_klass()); 386 } 387 388 void ObjArrayKlass::initialize(TRAPS) { 389 bottom_klass()->initialize(THREAD); // dispatches to either InstanceKlass or TypeArrayKlass 390 } 391 392 // JVM support 393 394 jint ObjArrayKlass::compute_modifier_flags(TRAPS) const { 395 // The modifier for an objectArray is the same as its element 396 if (element_klass() == NULL) { 397 assert(Universe::is_bootstrapping(), "partial objArray only at startup"); 398 return JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC; 399 } 400 // Return the flags of the bottom element type. 401 jint element_flags = bottom_klass()->compute_modifier_flags(CHECK_0); 402 403 return (element_flags & (JVM_ACC_PUBLIC | JVM_ACC_PRIVATE | JVM_ACC_PROTECTED)) 404 | (JVM_ACC_ABSTRACT | JVM_ACC_FINAL); 405 } 406 407 408 // Printing 409 410 void ObjArrayKlass::print_on(outputStream* st) const { 411 #ifndef PRODUCT 412 Klass::print_on(st); 413 st->print(" - element klass: "); 414 element_klass()->print_value_on(st); 415 st->cr(); 416 #endif //PRODUCT 417 } 418 419 void ObjArrayKlass::print_value_on(outputStream* st) const { 420 assert(is_klass(), "must be klass"); 421 422 element_klass()->print_value_on(st); 423 st->print("[]"); 424 } 425 426 #ifndef PRODUCT 427 428 void ObjArrayKlass::oop_print_on(oop obj, outputStream* st) { 429 ArrayKlass::oop_print_on(obj, st); 430 assert(obj->is_objArray(), "must be objArray"); 431 objArrayOop oa = objArrayOop(obj); 432 int print_len = MIN2((intx) oa->length(), MaxElementPrintSize); 433 for(int index = 0; index < print_len; index++) { 434 st->print(" - %3d : ", index); 435 oa->obj_at(index)->print_value_on(st); 436 st->cr(); 437 } 438 int remaining = oa->length() - print_len; 439 if (remaining > 0) { 440 st->print_cr(" - <%d more elements, increase MaxElementPrintSize to print>", remaining); 441 } 442 } 443 444 #endif //PRODUCT 445 446 static int max_objArray_print_length = 4; 447 448 void ObjArrayKlass::oop_print_value_on(oop obj, outputStream* st) { 449 assert(obj->is_objArray(), "must be objArray"); 450 st->print("a "); 451 element_klass()->print_value_on(st); 452 int len = objArrayOop(obj)->length(); 453 st->print("[%d] ", len); 454 obj->print_address_on(st); 455 if (NOT_PRODUCT(PrintOopAddress ||) PrintMiscellaneous && (WizardMode || Verbose)) { 456 st->print("{"); 457 for (int i = 0; i < len; i++) { 458 if (i > max_objArray_print_length) { 459 st->print("..."); break; 460 } 461 st->print(" " INTPTR_FORMAT, (intptr_t)(void*)objArrayOop(obj)->obj_at(i)); 462 } 463 st->print(" }"); 464 } 465 } 466 467 const char* ObjArrayKlass::internal_name() const { 468 return external_name(); 469 } 470 471 472 // Verification 473 474 void ObjArrayKlass::verify_on(outputStream* st) { 475 ArrayKlass::verify_on(st); 476 guarantee(element_klass()->is_klass(), "should be klass"); 477 guarantee(bottom_klass()->is_klass(), "should be klass"); 478 Klass* bk = bottom_klass(); 479 guarantee(bk->is_instance_klass() || bk->is_typeArray_klass(), "invalid bottom klass"); 480 } 481 482 void ObjArrayKlass::oop_verify_on(oop obj, outputStream* st) { 483 ArrayKlass::oop_verify_on(obj, st); 484 guarantee(obj->is_objArray(), "must be objArray"); 485 objArrayOop oa = objArrayOop(obj); 486 for(int index = 0; index < oa->length(); index++) { 487 guarantee(oa->obj_at(index)->is_oop_or_null(), "should be oop"); 488 } 489 }