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