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