1 /* 2 * Copyright (c) 2017, 2019, 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 "gc/shared/barrierSet.hpp" 27 #include "gc/shared/collectedHeap.inline.hpp" 28 #include "gc/shared/gcLocker.inline.hpp" 29 #include "interpreter/interpreter.hpp" 30 #include "logging/log.hpp" 31 #include "memory/metaspaceClosure.hpp" 32 #include "memory/metadataFactory.hpp" 33 #include "oops/access.hpp" 34 #include "oops/compressedOops.inline.hpp" 35 #include "oops/fieldStreams.inline.hpp" 36 #include "oops/instanceKlass.inline.hpp" 37 #include "oops/method.hpp" 38 #include "oops/oop.inline.hpp" 39 #include "oops/objArrayKlass.hpp" 40 #include "oops/valueKlass.inline.hpp" 41 #include "oops/valueArrayKlass.hpp" 42 #include "runtime/fieldDescriptor.inline.hpp" 43 #include "runtime/handles.inline.hpp" 44 #include "runtime/safepointVerifiers.hpp" 45 #include "runtime/sharedRuntime.hpp" 46 #include "runtime/signature.hpp" 47 #include "runtime/thread.inline.hpp" 48 #include "utilities/copy.hpp" 49 50 // Constructor 51 ValueKlass::ValueKlass(const ClassFileParser& parser) 52 : InstanceKlass(parser, InstanceKlass::_misc_kind_value_type, InstanceKlass::ID) { 53 _adr_valueklass_fixed_block = valueklass_static_block(); 54 // Addresses used for value type calling convention 55 *((Array<SigEntry>**)adr_extended_sig()) = NULL; 56 *((Array<VMRegPair>**)adr_return_regs()) = NULL; 57 *((address*)adr_pack_handler()) = NULL; 58 *((address*)adr_pack_handler_jobject()) = NULL; 59 *((address*)adr_unpack_handler()) = NULL; 60 assert(pack_handler() == NULL, "pack handler not null"); 61 *((int*)adr_default_value_offset()) = 0; 62 *((Klass**)adr_value_array_klass()) = NULL; 63 set_prototype_header(markWord::always_locked_prototype()); 64 } 65 66 oop ValueKlass::default_value() { 67 oop val = java_mirror()->obj_field_acquire(default_value_offset()); 68 assert(oopDesc::is_oop(val), "Sanity check"); 69 assert(val->is_value(), "Sanity check"); 70 assert(val->klass() == this, "sanity check"); 71 return val; 72 } 73 74 int ValueKlass::first_field_offset_old() { 75 #ifdef ASSERT 76 int first_offset = INT_MAX; 77 for (AllFieldStream fs(this); !fs.done(); fs.next()) { 78 if (fs.offset() < first_offset) first_offset= fs.offset(); 79 } 80 #endif 81 int base_offset = instanceOopDesc::base_offset_in_bytes(); 82 // The first field of value types is aligned on a long boundary 83 base_offset = align_up(base_offset, BytesPerLong); 84 assert(base_offset == first_offset, "inconsistent offsets"); 85 return base_offset; 86 } 87 88 int ValueKlass::raw_value_byte_size() { 89 int heapOopAlignedSize = nonstatic_field_size() << LogBytesPerHeapOop; 90 // If bigger than 64 bits or needs oop alignment, then use jlong aligned 91 // which for values should be jlong aligned, asserts in raw_field_copy otherwise 92 if (heapOopAlignedSize >= longSize || contains_oops()) { 93 return heapOopAlignedSize; 94 } 95 // Small primitives... 96 // If a few small basic type fields, return the actual size, i.e. 97 // 1 byte = 1 98 // 2 byte = 2 99 // 3 byte = 4, because pow2 needed for element stores 100 int first_offset = first_field_offset(); 101 int last_offset = 0; // find the last offset, add basic type size 102 int last_tsz = 0; 103 for (AllFieldStream fs(this); !fs.done(); fs.next()) { 104 if (fs.access_flags().is_static()) { 105 continue; 106 } else if (fs.offset() > last_offset) { 107 BasicType type = Signature::basic_type(fs.signature()); 108 if (is_java_primitive(type)) { 109 last_tsz = type2aelembytes(type); 110 } else if (type == T_VALUETYPE) { 111 // Not just primitives. Layout aligns embedded value, so use jlong aligned it is 112 return heapOopAlignedSize; 113 } else { 114 guarantee(0, "Unknown type %d", type); 115 } 116 assert(last_tsz != 0, "Invariant"); 117 last_offset = fs.offset(); 118 } 119 } 120 // Assumes VT with no fields are meaningless and illegal 121 last_offset += last_tsz; 122 assert(last_offset > first_offset && last_tsz, "Invariant"); 123 return 1 << upper_log2(last_offset - first_offset); 124 } 125 126 instanceOop ValueKlass::allocate_instance(TRAPS) { 127 int size = size_helper(); // Query before forming handle. 128 129 instanceOop oop = (instanceOop)Universe::heap()->obj_allocate(this, size, CHECK_NULL); 130 assert(oop->mark().is_always_locked(), "Unlocked value type"); 131 return oop; 132 } 133 134 instanceOop ValueKlass::allocate_instance_buffer(TRAPS) { 135 int size = size_helper(); // Query before forming handle. 136 137 instanceOop oop = (instanceOop)Universe::heap()->obj_buffer_allocate(this, size, CHECK_NULL); 138 assert(oop->mark().is_always_locked(), "Unlocked value type"); 139 return oop; 140 } 141 142 int ValueKlass::nonstatic_oop_count() { 143 int oops = 0; 144 int map_count = nonstatic_oop_map_count(); 145 OopMapBlock* block = start_of_nonstatic_oop_maps(); 146 OopMapBlock* end = block + map_count; 147 while (block != end) { 148 oops += block->count(); 149 block++; 150 } 151 return oops; 152 } 153 154 oop ValueKlass::read_flattened_field(oop obj, int offset, TRAPS) { 155 oop res = NULL; 156 this->initialize(CHECK_NULL); // will throw an exception if in error state 157 if (is_empty_value()) { 158 res = (instanceOop)default_value(); 159 } else { 160 Handle obj_h(THREAD, obj); 161 res = allocate_instance_buffer(CHECK_NULL); 162 value_copy_payload_to_new_oop(((char*)(oopDesc*)obj_h()) + offset, res); 163 } 164 assert(res != NULL, "Must be set in one of two paths above"); 165 return res; 166 } 167 168 void ValueKlass::write_flattened_field(oop obj, int offset, oop value, TRAPS) { 169 if (value == NULL) { 170 THROW(vmSymbols::java_lang_NullPointerException()); 171 } 172 if (!is_empty_value()) { 173 value_copy_oop_to_payload(value, ((char*)(oopDesc*)obj) + offset); 174 } 175 } 176 177 // Arrays of... 178 179 bool ValueKlass::flatten_array() { 180 if (!ValueArrayFlatten) { 181 return false; 182 } 183 184 int elem_bytes = raw_value_byte_size(); 185 // Too big 186 if ((ValueArrayElemMaxFlatSize >= 0) && (elem_bytes > ValueArrayElemMaxFlatSize)) { 187 return false; 188 } 189 // Too many embedded oops 190 if ((ValueArrayElemMaxFlatOops >= 0) && (nonstatic_oop_count() > ValueArrayElemMaxFlatOops)) { 191 return false; 192 } 193 194 // Declared atomic but not naturally atomic. 195 if (is_declared_atomic() && !is_naturally_atomic()) { 196 return false; 197 } 198 199 return true; 200 } 201 202 void ValueKlass::remove_unshareable_info() { 203 InstanceKlass::remove_unshareable_info(); 204 205 *((Array<SigEntry>**)adr_extended_sig()) = NULL; 206 *((Array<VMRegPair>**)adr_return_regs()) = NULL; 207 *((address*)adr_pack_handler()) = NULL; 208 *((address*)adr_pack_handler_jobject()) = NULL; 209 *((address*)adr_unpack_handler()) = NULL; 210 assert(pack_handler() == NULL, "pack handler not null"); 211 *((Klass**)adr_value_array_klass()) = NULL; 212 } 213 214 void ValueKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS) { 215 InstanceKlass::restore_unshareable_info(loader_data, protection_domain, CHECK); 216 oop val = allocate_instance(CHECK); 217 set_default_value(val); 218 } 219 220 221 Klass* ValueKlass::array_klass_impl(ArrayStorageProperties storage_props, bool or_null, int n, TRAPS) { 222 if (storage_props.is_null_free()) { 223 return value_array_klass(storage_props, or_null, n, THREAD); 224 } else { 225 return InstanceKlass::array_klass_impl(storage_props, or_null, n, THREAD); 226 } 227 } 228 229 Klass* ValueKlass::array_klass_impl(ArrayStorageProperties storage_props, bool or_null, TRAPS) { 230 return array_klass_impl(storage_props, or_null, 1, THREAD); 231 } 232 233 Klass* ValueKlass::value_array_klass(ArrayStorageProperties storage_props, bool or_null, int rank, TRAPS) { 234 Klass* vak = acquire_value_array_klass(); 235 if (vak == NULL) { 236 if (or_null) return NULL; 237 ResourceMark rm; 238 { 239 // Atomic creation of array_klasses 240 MutexLocker ma(THREAD, MultiArray_lock); 241 if (get_value_array_klass() == NULL) { 242 vak = allocate_value_array_klass(CHECK_NULL); 243 Atomic::release_store((Klass**)adr_value_array_klass(), vak); 244 } 245 } 246 } 247 if (!vak->is_valueArray_klass()) { 248 storage_props.clear_flattened(); 249 } 250 if (or_null) { 251 return vak->array_klass_or_null(storage_props, rank); 252 } 253 return vak->array_klass(storage_props, rank, THREAD); 254 } 255 256 Klass* ValueKlass::allocate_value_array_klass(TRAPS) { 257 if (flatten_array() && (is_naturally_atomic() || (!ValueArrayAtomicAccess))) { 258 return ValueArrayKlass::allocate_klass(ArrayStorageProperties::flattened_and_null_free, this, THREAD); 259 } 260 return ObjArrayKlass::allocate_objArray_klass(ArrayStorageProperties::null_free, 1, this, THREAD); 261 } 262 263 void ValueKlass::array_klasses_do(void f(Klass* k)) { 264 InstanceKlass::array_klasses_do(f); 265 if (get_value_array_klass() != NULL) 266 ArrayKlass::cast(get_value_array_klass())->array_klasses_do(f); 267 } 268 269 // Value type arguments are not passed by reference, instead each 270 // field of the value type is passed as an argument. This helper 271 // function collects the fields of the value types (including embedded 272 // value type's fields) in a list. Included with the field's type is 273 // the offset of each field in the value type: i2c and c2i adapters 274 // need that to load or store fields. Finally, the list of fields is 275 // sorted in order of increasing offsets: the adapters and the 276 // compiled code need to agree upon the order of fields. 277 // 278 // The list of basic types that is returned starts with a T_VALUETYPE 279 // and ends with an extra T_VOID. T_VALUETYPE/T_VOID pairs are used as 280 // delimiters. Every entry between the two is a field of the value 281 // type. If there's an embedded value type in the list, it also starts 282 // with a T_VALUETYPE and ends with a T_VOID. This is so we can 283 // generate a unique fingerprint for the method's adapters and we can 284 // generate the list of basic types from the interpreter point of view 285 // (value types passed as reference: iterate on the list until a 286 // T_VALUETYPE, drop everything until and including the closing 287 // T_VOID) or the compiler point of view (each field of the value 288 // types is an argument: drop all T_VALUETYPE/T_VOID from the list). 289 int ValueKlass::collect_fields(GrowableArray<SigEntry>* sig, int base_off) { 290 int count = 0; 291 SigEntry::add_entry(sig, T_VALUETYPE, base_off); 292 for (AllFieldStream fs(this); !fs.done(); fs.next()) { 293 if (fs.access_flags().is_static()) continue; 294 int offset = base_off + fs.offset() - (base_off > 0 ? first_field_offset() : 0); 295 if (fs.is_flattened()) { 296 // Resolve klass of flattened value type field and recursively collect fields 297 Klass* vk = get_value_field_klass(fs.index()); 298 count += ValueKlass::cast(vk)->collect_fields(sig, offset); 299 } else { 300 BasicType bt = Signature::basic_type(fs.signature()); 301 if (bt == T_VALUETYPE) { 302 bt = T_OBJECT; 303 } 304 SigEntry::add_entry(sig, bt, offset); 305 count += type2size[bt]; 306 } 307 } 308 int offset = base_off + size_helper()*HeapWordSize - (base_off > 0 ? first_field_offset() : 0); 309 SigEntry::add_entry(sig, T_VOID, offset); 310 if (base_off == 0) { 311 sig->sort(SigEntry::compare); 312 } 313 assert(sig->at(0)._bt == T_VALUETYPE && sig->at(sig->length()-1)._bt == T_VOID, "broken structure"); 314 return count; 315 } 316 317 void ValueKlass::initialize_calling_convention(TRAPS) { 318 // Because the pack and unpack handler addresses need to be loadable from generated code, 319 // they are stored at a fixed offset in the klass metadata. Since value type klasses do 320 // not have a vtable, the vtable offset is used to store these addresses. 321 if (is_scalarizable() && (ValueTypeReturnedAsFields || ValueTypePassFieldsAsArgs)) { 322 ResourceMark rm; 323 GrowableArray<SigEntry> sig_vk; 324 int nb_fields = collect_fields(&sig_vk); 325 Array<SigEntry>* extended_sig = MetadataFactory::new_array<SigEntry>(class_loader_data(), sig_vk.length(), CHECK); 326 *((Array<SigEntry>**)adr_extended_sig()) = extended_sig; 327 for (int i = 0; i < sig_vk.length(); i++) { 328 extended_sig->at_put(i, sig_vk.at(i)); 329 } 330 331 if (ValueTypeReturnedAsFields) { 332 nb_fields++; 333 BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, nb_fields); 334 sig_bt[0] = T_METADATA; 335 SigEntry::fill_sig_bt(&sig_vk, sig_bt+1); 336 VMRegPair* regs = NEW_RESOURCE_ARRAY(VMRegPair, nb_fields); 337 int total = SharedRuntime::java_return_convention(sig_bt, regs, nb_fields); 338 339 if (total > 0) { 340 Array<VMRegPair>* return_regs = MetadataFactory::new_array<VMRegPair>(class_loader_data(), nb_fields, CHECK); 341 *((Array<VMRegPair>**)adr_return_regs()) = return_regs; 342 for (int i = 0; i < nb_fields; i++) { 343 return_regs->at_put(i, regs[i]); 344 } 345 346 BufferedValueTypeBlob* buffered_blob = SharedRuntime::generate_buffered_value_type_adapter(this); 347 *((address*)adr_pack_handler()) = buffered_blob->pack_fields(); 348 *((address*)adr_pack_handler_jobject()) = buffered_blob->pack_fields_jobject(); 349 *((address*)adr_unpack_handler()) = buffered_blob->unpack_fields(); 350 assert(CodeCache::find_blob(pack_handler()) == buffered_blob, "lost track of blob"); 351 } 352 } 353 } 354 } 355 356 void ValueKlass::deallocate_contents(ClassLoaderData* loader_data) { 357 if (extended_sig() != NULL) { 358 MetadataFactory::free_array<SigEntry>(loader_data, extended_sig()); 359 } 360 if (return_regs() != NULL) { 361 MetadataFactory::free_array<VMRegPair>(loader_data, return_regs()); 362 } 363 cleanup_blobs(); 364 InstanceKlass::deallocate_contents(loader_data); 365 } 366 367 void ValueKlass::cleanup(ValueKlass* ik) { 368 ik->cleanup_blobs(); 369 } 370 371 void ValueKlass::cleanup_blobs() { 372 if (pack_handler() != NULL) { 373 CodeBlob* buffered_blob = CodeCache::find_blob(pack_handler()); 374 assert(buffered_blob->is_buffered_value_type_blob(), "bad blob type"); 375 BufferBlob::free((BufferBlob*)buffered_blob); 376 *((address*)adr_pack_handler()) = NULL; 377 *((address*)adr_pack_handler_jobject()) = NULL; 378 *((address*)adr_unpack_handler()) = NULL; 379 } 380 } 381 382 // Can this value type be scalarized? 383 bool ValueKlass::is_scalarizable() const { 384 return ScalarizeValueTypes; 385 } 386 387 // Can this value type be returned as multiple values? 388 bool ValueKlass::can_be_returned_as_fields() const { 389 return return_regs() != NULL; 390 } 391 392 // Create handles for all oop fields returned in registers that are going to be live across a safepoint 393 void ValueKlass::save_oop_fields(const RegisterMap& reg_map, GrowableArray<Handle>& handles) const { 394 Thread* thread = Thread::current(); 395 const Array<SigEntry>* sig_vk = extended_sig(); 396 const Array<VMRegPair>* regs = return_regs(); 397 int j = 1; 398 399 for (int i = 0; i < sig_vk->length(); i++) { 400 BasicType bt = sig_vk->at(i)._bt; 401 if (bt == T_OBJECT || bt == T_ARRAY) { 402 VMRegPair pair = regs->at(j); 403 address loc = reg_map.location(pair.first()); 404 oop v = *(oop*)loc; 405 assert(v == NULL || oopDesc::is_oop(v), "not an oop?"); 406 assert(Universe::heap()->is_in_or_null(v), "must be heap pointer"); 407 handles.push(Handle(thread, v)); 408 } 409 if (bt == T_VALUETYPE) { 410 continue; 411 } 412 if (bt == T_VOID && 413 sig_vk->at(i-1)._bt != T_LONG && 414 sig_vk->at(i-1)._bt != T_DOUBLE) { 415 continue; 416 } 417 j++; 418 } 419 assert(j == regs->length(), "missed a field?"); 420 } 421 422 // Update oop fields in registers from handles after a safepoint 423 void ValueKlass::restore_oop_results(RegisterMap& reg_map, GrowableArray<Handle>& handles) const { 424 assert(ValueTypeReturnedAsFields, "inconsistent"); 425 const Array<SigEntry>* sig_vk = extended_sig(); 426 const Array<VMRegPair>* regs = return_regs(); 427 assert(regs != NULL, "inconsistent"); 428 429 int j = 1; 430 for (int i = 0, k = 0; i < sig_vk->length(); i++) { 431 BasicType bt = sig_vk->at(i)._bt; 432 if (bt == T_OBJECT || bt == T_ARRAY) { 433 VMRegPair pair = regs->at(j); 434 address loc = reg_map.location(pair.first()); 435 *(oop*)loc = handles.at(k++)(); 436 } 437 if (bt == T_VALUETYPE) { 438 continue; 439 } 440 if (bt == T_VOID && 441 sig_vk->at(i-1)._bt != T_LONG && 442 sig_vk->at(i-1)._bt != T_DOUBLE) { 443 continue; 444 } 445 j++; 446 } 447 assert(j == regs->length(), "missed a field?"); 448 } 449 450 // Fields are in registers. Create an instance of the value type and 451 // initialize it with the values of the fields. 452 oop ValueKlass::realloc_result(const RegisterMap& reg_map, const GrowableArray<Handle>& handles, TRAPS) { 453 oop new_vt = allocate_instance(CHECK_NULL); 454 const Array<SigEntry>* sig_vk = extended_sig(); 455 const Array<VMRegPair>* regs = return_regs(); 456 457 int j = 1; 458 int k = 0; 459 for (int i = 0; i < sig_vk->length(); i++) { 460 BasicType bt = sig_vk->at(i)._bt; 461 if (bt == T_VALUETYPE) { 462 continue; 463 } 464 if (bt == T_VOID) { 465 if (sig_vk->at(i-1)._bt == T_LONG || 466 sig_vk->at(i-1)._bt == T_DOUBLE) { 467 j++; 468 } 469 continue; 470 } 471 int off = sig_vk->at(i)._offset; 472 assert(off > 0, "offset in object should be positive"); 473 VMRegPair pair = regs->at(j); 474 address loc = reg_map.location(pair.first()); 475 switch(bt) { 476 case T_BOOLEAN: { 477 new_vt->bool_field_put(off, *(jboolean*)loc); 478 break; 479 } 480 case T_CHAR: { 481 new_vt->char_field_put(off, *(jchar*)loc); 482 break; 483 } 484 case T_BYTE: { 485 new_vt->byte_field_put(off, *(jbyte*)loc); 486 break; 487 } 488 case T_SHORT: { 489 new_vt->short_field_put(off, *(jshort*)loc); 490 break; 491 } 492 case T_INT: { 493 new_vt->int_field_put(off, *(jint*)loc); 494 break; 495 } 496 case T_LONG: { 497 #ifdef _LP64 498 new_vt->double_field_put(off, *(jdouble*)loc); 499 #else 500 Unimplemented(); 501 #endif 502 break; 503 } 504 case T_OBJECT: 505 case T_ARRAY: { 506 Handle handle = handles.at(k++); 507 new_vt->obj_field_put(off, handle()); 508 break; 509 } 510 case T_FLOAT: { 511 new_vt->float_field_put(off, *(jfloat*)loc); 512 break; 513 } 514 case T_DOUBLE: { 515 new_vt->double_field_put(off, *(jdouble*)loc); 516 break; 517 } 518 default: 519 ShouldNotReachHere(); 520 } 521 *(intptr_t*)loc = 0xDEAD; 522 j++; 523 } 524 assert(j == regs->length(), "missed a field?"); 525 assert(k == handles.length(), "missed an oop?"); 526 return new_vt; 527 } 528 529 // Check the return register for a ValueKlass oop 530 ValueKlass* ValueKlass::returned_value_klass(const RegisterMap& map) { 531 BasicType bt = T_METADATA; 532 VMRegPair pair; 533 int nb = SharedRuntime::java_return_convention(&bt, &pair, 1); 534 assert(nb == 1, "broken"); 535 536 address loc = map.location(pair.first()); 537 intptr_t ptr = *(intptr_t*)loc; 538 if (is_set_nth_bit(ptr, 0)) { 539 // Oop is tagged, must be a ValueKlass oop 540 clear_nth_bit(ptr, 0); 541 assert(Metaspace::contains((void*)ptr), "should be klass"); 542 ValueKlass* vk = (ValueKlass*)ptr; 543 assert(vk->can_be_returned_as_fields(), "must be able to return as fields"); 544 return vk; 545 } 546 #ifdef ASSERT 547 // Oop is not tagged, must be a valid oop 548 if (VerifyOops) { 549 oopDesc::verify(oop((HeapWord*)ptr)); 550 } 551 #endif 552 return NULL; 553 } 554 555 void ValueKlass::verify_on(outputStream* st) { 556 InstanceKlass::verify_on(st); 557 guarantee(prototype_header().is_always_locked(), "Prototype header is not always locked"); 558 } 559 560 void ValueKlass::oop_verify_on(oop obj, outputStream* st) { 561 InstanceKlass::oop_verify_on(obj, st); 562 guarantee(obj->mark().is_always_locked(), "Header is not always locked"); 563 } 564 565 void ValueKlass::metaspace_pointers_do(MetaspaceClosure* it) { 566 InstanceKlass::metaspace_pointers_do(it); 567 568 ValueKlass* this_ptr = this; 569 it->push_internal_pointer(&this_ptr, (intptr_t*)&_adr_valueklass_fixed_block); 570 }