1 /* 2 * Copyright (c) 1999, 2025, 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 "ci/ciField.hpp" 26 #include "ci/ciInlineKlass.hpp" 27 #include "ci/ciInstance.hpp" 28 #include "ci/ciInstanceKlass.hpp" 29 #include "ci/ciUtilities.inline.hpp" 30 #include "classfile/javaClasses.hpp" 31 #include "classfile/systemDictionary.hpp" 32 #include "classfile/vmClasses.hpp" 33 #include "memory/allocation.hpp" 34 #include "memory/allocation.inline.hpp" 35 #include "memory/resourceArea.hpp" 36 #include "oops/fieldStreams.inline.hpp" 37 #include "oops/inlineKlass.inline.hpp" 38 #include "oops/instanceKlass.inline.hpp" 39 #include "oops/klass.inline.hpp" 40 #include "oops/oop.inline.hpp" 41 #include "runtime/fieldDescriptor.inline.hpp" 42 #include "runtime/handles.inline.hpp" 43 #include "runtime/jniHandles.inline.hpp" 44 45 // ciInstanceKlass 46 // 47 // This class represents a Klass* in the HotSpot virtual machine 48 // whose Klass part in an InstanceKlass. 49 50 51 // ------------------------------------------------------------------ 52 // ciInstanceKlass::ciInstanceKlass 53 // 54 // Loaded instance klass. 55 ciInstanceKlass::ciInstanceKlass(Klass* k) : 56 ciKlass(k) 57 { 58 assert(get_Klass()->is_instance_klass(), "wrong type"); 59 assert(get_instanceKlass()->is_loaded(), "must be at least loaded"); 60 InstanceKlass* ik = get_instanceKlass(); 61 62 AccessFlags access_flags = ik->access_flags(); 63 _flags = ciFlags(access_flags); 64 _has_finalizer = ik->has_finalizer(); 65 _has_subklass = flags().is_final() ? subklass_false : subklass_unknown; 66 _init_state = ik->init_state(); 67 _has_nonstatic_fields = ik->has_nonstatic_fields(); 68 _has_nonstatic_concrete_methods = ik->has_nonstatic_concrete_methods(); 69 _is_hidden = ik->is_hidden(); 70 _is_record = ik->is_record(); 71 _nonstatic_fields = nullptr; // initialized lazily by compute_nonstatic_fields: 72 _has_injected_fields = -1; 73 _implementor = nullptr; // we will fill these lazily 74 _transitive_interfaces = nullptr; 75 76 // Ensure that the metadata wrapped by the ciMetadata is kept alive by GC. 77 // This is primarily useful for metadata which is considered as weak roots 78 // by the GC but need to be strong roots if reachable from a current compilation. 79 // InstanceKlass are created for both weak and strong metadata. Ensuring this metadata 80 // alive covers the cases where there are weak roots without performance cost. 81 oop holder = ik->klass_holder(); 82 if (ik->class_loader_data()->has_class_mirror_holder()) { 83 // Though ciInstanceKlass records class loader oop, it's not enough to keep 84 // non-strong hidden classes alive (loader == nullptr). Klass holder should 85 // be used instead. It is enough to record a ciObject, since cached elements are never removed 86 // during ciObjectFactory lifetime. ciObjectFactory itself is created for 87 // every compilation and lives for the whole duration of the compilation. 88 assert(holder != nullptr, "holder of hidden class is the mirror which is never null"); 89 (void)CURRENT_ENV->get_object(holder); 90 } 91 92 JavaThread *thread = JavaThread::current(); 93 if (ciObjectFactory::is_initialized()) { 94 _loader = JNIHandles::make_local(thread, ik->class_loader()); 95 _is_shared = false; 96 } else { 97 Handle h_loader(thread, ik->class_loader()); 98 _loader = JNIHandles::make_global(h_loader); 99 _is_shared = true; 100 } 101 102 _has_trusted_loader = compute_has_trusted_loader(); 103 104 // Lazy fields get filled in only upon request. 105 _super = nullptr; 106 _java_mirror = nullptr; 107 108 if (is_shared()) { 109 if (k != vmClasses::Object_klass()) { 110 super(); 111 } 112 //compute_nonstatic_fields(); // done outside of constructor 113 } 114 115 _field_cache = nullptr; 116 } 117 118 // Version for unloaded classes: 119 ciInstanceKlass::ciInstanceKlass(ciSymbol* name, 120 jobject loader, 121 BasicType bt) 122 : ciKlass(name, bt) 123 { 124 assert(name->char_at(0) != JVM_SIGNATURE_ARRAY, "not an instance klass"); 125 _init_state = (InstanceKlass::ClassState)0; 126 _has_nonstatic_fields = false; 127 _nonstatic_fields = nullptr; // initialized lazily by compute_nonstatic_fields 128 _has_injected_fields = -1; 129 _is_hidden = false; 130 _is_record = false; 131 _loader = loader; 132 _is_shared = false; 133 _super = nullptr; 134 _java_mirror = nullptr; 135 _field_cache = nullptr; 136 _has_trusted_loader = compute_has_trusted_loader(); 137 } 138 139 140 141 // ------------------------------------------------------------------ 142 // ciInstanceKlass::compute_shared_is_initialized 143 void ciInstanceKlass::compute_shared_init_state() { 144 GUARDED_VM_ENTRY( 145 InstanceKlass* ik = get_instanceKlass(); 146 _init_state = ik->init_state(); 147 ) 148 } 149 150 // ------------------------------------------------------------------ 151 // ciInstanceKlass::compute_shared_has_subklass 152 bool ciInstanceKlass::compute_shared_has_subklass() { 153 GUARDED_VM_ENTRY( 154 InstanceKlass* ik = get_instanceKlass(); 155 _has_subklass = ik->subklass() != nullptr ? subklass_true : subklass_false; 156 return _has_subklass == subklass_true; 157 ) 158 } 159 160 // ------------------------------------------------------------------ 161 // ciInstanceKlass::loader 162 oop ciInstanceKlass::loader() { 163 ASSERT_IN_VM; 164 return JNIHandles::resolve(_loader); 165 } 166 167 // ------------------------------------------------------------------ 168 // ciInstanceKlass::loader_handle 169 jobject ciInstanceKlass::loader_handle() { 170 return _loader; 171 } 172 173 // ------------------------------------------------------------------ 174 // ciInstanceKlass::field_cache 175 // 176 // Get the field cache associated with this klass. 177 ciConstantPoolCache* ciInstanceKlass::field_cache() { 178 if (is_shared()) { 179 return nullptr; 180 } 181 if (_field_cache == nullptr) { 182 assert(!is_java_lang_Object(), "Object has no fields"); 183 Arena* arena = CURRENT_ENV->arena(); 184 _field_cache = new (arena) ciConstantPoolCache(arena, 5); 185 } 186 return _field_cache; 187 } 188 189 // ------------------------------------------------------------------ 190 // ciInstanceKlass::get_canonical_holder 191 // 192 ciInstanceKlass* ciInstanceKlass::get_canonical_holder(int offset) { 193 #ifdef ASSERT 194 if (!(offset >= 0 && offset < layout_helper_size_in_bytes())) { 195 tty->print("*** get_canonical_holder(%d) on ", offset); 196 this->print(); 197 tty->print_cr(" ***"); 198 }; 199 assert(offset >= 0 && offset < layout_helper_size_in_bytes(), "offset must be tame"); 200 #endif 201 202 if (offset < instanceOopDesc::base_offset_in_bytes()) { 203 // All header offsets belong properly to java/lang/Object. 204 return CURRENT_ENV->Object_klass(); 205 } 206 207 ciInstanceKlass* self = this; 208 assert(self->is_loaded(), "must be loaded to access field info"); 209 ciField* field = self->get_field_by_offset(offset, false); 210 if (field != nullptr) { 211 return field->holder(); 212 } else { 213 for (;;) { 214 assert(self->is_loaded(), "must be loaded to have size"); 215 ciInstanceKlass* super = self->super(); 216 if (super == nullptr || 217 super->nof_nonstatic_fields() == 0 || 218 super->layout_helper_size_in_bytes() <= offset) { 219 return self; 220 } else { 221 self = super; // return super->get_canonical_holder(offset) 222 } 223 } 224 } 225 } 226 227 // ------------------------------------------------------------------ 228 // ciInstanceKlass::is_java_lang_Object 229 // 230 // Is this klass java.lang.Object? 231 bool ciInstanceKlass::is_java_lang_Object() const { 232 return equals(CURRENT_ENV->Object_klass()); 233 } 234 235 // ------------------------------------------------------------------ 236 // ciInstanceKlass::uses_default_loader 237 bool ciInstanceKlass::uses_default_loader() const { 238 // Note: We do not need to resolve the handle or enter the VM 239 // in order to test null-ness. 240 return _loader == nullptr; 241 } 242 243 // ------------------------------------------------------------------ 244 245 /** 246 * Return basic type of boxed value for box klass or T_OBJECT if not. 247 */ 248 BasicType ciInstanceKlass::box_klass_type() const { 249 if (uses_default_loader() && is_loaded()) { 250 return vmClasses::box_klass_type(get_Klass()); 251 } else { 252 return T_OBJECT; 253 } 254 } 255 256 /** 257 * Is this boxing klass? 258 */ 259 bool ciInstanceKlass::is_box_klass() const { 260 return is_java_primitive(box_klass_type()); 261 } 262 263 /** 264 * Is this boxed value offset? 265 */ 266 bool ciInstanceKlass::is_boxed_value_offset(int offset) const { 267 BasicType bt = box_klass_type(); 268 return is_java_primitive(bt) && 269 (offset == java_lang_boxing_object::value_offset(bt)); 270 } 271 272 // ------------------------------------------------------------------ 273 // ciInstanceKlass::is_in_package 274 // 275 // Is this klass in the given package? 276 bool ciInstanceKlass::is_in_package(const char* packagename, int len) { 277 // To avoid class loader mischief, this test always rejects application classes. 278 if (!uses_default_loader()) 279 return false; 280 GUARDED_VM_ENTRY( 281 return is_in_package_impl(packagename, len); 282 ) 283 } 284 285 bool ciInstanceKlass::is_in_package_impl(const char* packagename, int len) { 286 ASSERT_IN_VM; 287 288 // If packagename contains trailing '/' exclude it from the 289 // prefix-test since we test for it explicitly. 290 if (packagename[len - 1] == '/') 291 len--; 292 293 if (!name()->starts_with(packagename, len)) 294 return false; 295 296 // Test if the class name is something like "java/lang". 297 if ((len + 1) > name()->utf8_length()) 298 return false; 299 300 // Test for trailing '/' 301 if (name()->char_at(len) != '/') 302 return false; 303 304 // Make sure it's not actually in a subpackage: 305 if (name()->index_of_at(len+1, "/", 1) >= 0) 306 return false; 307 308 return true; 309 } 310 311 // ------------------------------------------------------------------ 312 // ciInstanceKlass::print_impl 313 // 314 // Implementation of the print method. 315 void ciInstanceKlass::print_impl(outputStream* st) { 316 ciKlass::print_impl(st); 317 GUARDED_VM_ENTRY(st->print(" loader=" INTPTR_FORMAT, p2i(loader()));) 318 if (is_loaded()) { 319 st->print(" initialized=%s finalized=%s subklass=%s size=%d flags=", 320 bool_to_str(is_initialized()), 321 bool_to_str(has_finalizer()), 322 bool_to_str(has_subklass()), 323 layout_helper()); 324 325 _flags.print_klass_flags(st); 326 327 if (_super) { 328 st->print(" super="); 329 _super->print_name_on(st); 330 } 331 if (_java_mirror) { 332 st->print(" mirror=PRESENT"); 333 } 334 } 335 } 336 337 // ------------------------------------------------------------------ 338 // ciInstanceKlass::super 339 // 340 // Get the superklass of this klass. 341 ciInstanceKlass* ciInstanceKlass::super() { 342 assert(is_loaded(), "must be loaded"); 343 if (_super == nullptr && !is_java_lang_Object()) { 344 GUARDED_VM_ENTRY( 345 Klass* super_klass = get_instanceKlass()->super(); 346 _super = CURRENT_ENV->get_instance_klass(super_klass); 347 ) 348 } 349 return _super; 350 } 351 352 // ------------------------------------------------------------------ 353 // ciInstanceKlass::java_mirror 354 // 355 // Get the instance of java.lang.Class corresponding to this klass. 356 // Cache it on this->_java_mirror. 357 ciInstance* ciInstanceKlass::java_mirror() { 358 if (is_shared()) { 359 return ciKlass::java_mirror(); 360 } 361 if (_java_mirror == nullptr) { 362 _java_mirror = ciKlass::java_mirror(); 363 } 364 return _java_mirror; 365 } 366 367 // ------------------------------------------------------------------ 368 // ciInstanceKlass::unique_concrete_subklass 369 ciInstanceKlass* ciInstanceKlass::unique_concrete_subklass() { 370 if (!is_loaded()) return nullptr; // No change if class is not loaded 371 if (!is_abstract()) return nullptr; // Only applies to abstract classes. 372 if (!has_subklass()) return nullptr; // Must have at least one subklass. 373 VM_ENTRY_MARK; 374 InstanceKlass* ik = get_instanceKlass(); 375 Klass* up = ik->up_cast_abstract(); 376 assert(up->is_instance_klass(), "must be InstanceKlass"); 377 if (ik == up) { 378 return nullptr; 379 } 380 return CURRENT_THREAD_ENV->get_instance_klass(up); 381 } 382 383 // ------------------------------------------------------------------ 384 // ciInstanceKlass::has_finalizable_subclass 385 bool ciInstanceKlass::has_finalizable_subclass() { 386 if (!is_loaded()) return true; 387 VM_ENTRY_MARK; 388 return Dependencies::find_finalizable_subclass(get_instanceKlass()) != nullptr; 389 } 390 391 // ------------------------------------------------------------------ 392 // ciInstanceKlass::contains_field_offset 393 bool ciInstanceKlass::contains_field_offset(int offset) { 394 VM_ENTRY_MARK; 395 return get_instanceKlass()->contains_field_offset(offset); 396 } 397 398 // ------------------------------------------------------------------ 399 // ciInstanceKlass::get_field_by_offset 400 ciField* ciInstanceKlass::get_field_by_offset(int field_offset, bool is_static) { 401 if (!is_static) { 402 for (int i = 0, len = nof_nonstatic_fields(); i < len; i++) { 403 ciField* field = _nonstatic_fields->at(i); 404 int field_off = field->offset_in_bytes(); 405 if (field_off == field_offset) 406 return field; 407 } 408 return nullptr; 409 } 410 VM_ENTRY_MARK; 411 InstanceKlass* k = get_instanceKlass(); 412 fieldDescriptor fd; 413 if (!k->find_field_from_offset(field_offset, is_static, &fd)) { 414 return nullptr; 415 } 416 ciField* field = new (CURRENT_THREAD_ENV->arena()) ciField(&fd); 417 return field; 418 } 419 420 ciField* ciInstanceKlass::get_non_flat_field_by_offset(int field_offset) { 421 if (super() != nullptr && super()->has_nonstatic_fields()) { 422 ciField* f = super()->get_non_flat_field_by_offset(field_offset); 423 if (f != nullptr) { 424 return f; 425 } 426 } 427 428 VM_ENTRY_MARK; 429 InstanceKlass* k = get_instanceKlass(); 430 Arena* arena = CURRENT_ENV->arena(); 431 for (JavaFieldStream fs(k); !fs.done(); fs.next()) { 432 if (fs.access_flags().is_static()) continue; 433 fieldDescriptor& fd = fs.field_descriptor(); 434 if (fd.offset() == field_offset) { 435 ciField* f = new (arena) ciField(&fd); 436 return f; 437 } 438 } 439 440 return nullptr; 441 } 442 443 // ------------------------------------------------------------------ 444 // ciInstanceKlass::get_field_by_name 445 ciField* ciInstanceKlass::get_field_by_name(ciSymbol* name, ciSymbol* signature, bool is_static) { 446 VM_ENTRY_MARK; 447 InstanceKlass* k = get_instanceKlass(); 448 fieldDescriptor fd; 449 Klass* def = k->find_field(name->get_symbol(), signature->get_symbol(), is_static, &fd); 450 if (def == nullptr) { 451 return nullptr; 452 } 453 ciField* field = new (CURRENT_THREAD_ENV->arena()) ciField(&fd); 454 return field; 455 } 456 457 static int sort_field_by_offset(ciField** a, ciField** b) { 458 return (*a)->offset_in_bytes() - (*b)->offset_in_bytes(); 459 // (no worries about 32-bit overflow...) 460 } 461 462 // ------------------------------------------------------------------ 463 // ciInstanceKlass::compute_nonstatic_fields 464 int ciInstanceKlass::compute_nonstatic_fields() { 465 assert(is_loaded(), "must be loaded"); 466 467 if (_nonstatic_fields != nullptr) 468 return _nonstatic_fields->length(); 469 470 if (!has_nonstatic_fields()) { 471 Arena* arena = CURRENT_ENV->arena(); 472 _nonstatic_fields = new (arena) GrowableArray<ciField*>(arena, 0, 0, nullptr); 473 return 0; 474 } 475 assert(!is_java_lang_Object(), "bootstrap OK"); 476 477 ciInstanceKlass* super = this->super(); 478 GrowableArray<ciField*>* super_fields = nullptr; 479 if (super != nullptr && super->has_nonstatic_fields()) { 480 int super_flen = super->nof_nonstatic_fields(); 481 super_fields = super->_nonstatic_fields; 482 assert(super_flen == 0 || super_fields != nullptr, "first get nof_fields"); 483 } 484 485 GrowableArray<ciField*>* fields = nullptr; 486 GUARDED_VM_ENTRY({ 487 fields = compute_nonstatic_fields_impl(super_fields); 488 }); 489 490 if (fields == nullptr) { 491 // This can happen if this class (java.lang.Class) has invisible fields. 492 if (super_fields != nullptr) { 493 _nonstatic_fields = super_fields; 494 return super_fields->length(); 495 } else { 496 return 0; 497 } 498 } 499 500 _nonstatic_fields = fields; 501 return fields->length(); 502 } 503 504 GrowableArray<ciField*>* ciInstanceKlass::compute_nonstatic_fields_impl(GrowableArray<ciField*>* super_fields, bool is_flat) { 505 ASSERT_IN_VM; 506 Arena* arena = CURRENT_ENV->arena(); 507 int flen = 0; 508 GrowableArray<ciField*>* fields = nullptr; 509 InstanceKlass* k = get_instanceKlass(); 510 for (JavaFieldStream fs(k); !fs.done(); fs.next()) { 511 if (fs.access_flags().is_static()) continue; 512 flen += 1; 513 } 514 515 // allocate the array: 516 if (flen == 0 && !is_inlinetype()) { 517 return nullptr; // return nothing if none are locally declared 518 } 519 if (super_fields != nullptr) { 520 flen += super_fields->length(); 521 } 522 fields = new (arena) GrowableArray<ciField*>(arena, flen, 0, nullptr); 523 if (super_fields != nullptr) { 524 fields->appendAll(super_fields); 525 } 526 527 for (JavaFieldStream fs(k); !fs.done(); fs.next()) { 528 if (fs.access_flags().is_static()) continue; 529 fieldDescriptor& fd = fs.field_descriptor(); 530 if (fd.is_flat() && is_flat) { 531 // Inline type fields are embedded 532 int field_offset = fd.offset(); 533 // Get InlineKlass and adjust number of fields 534 Klass* k = get_instanceKlass()->get_inline_type_field_klass(fd.index()); 535 ciInlineKlass* vk = CURRENT_ENV->get_klass(k)->as_inline_klass(); 536 flen += vk->nof_nonstatic_fields() - 1; 537 // Iterate over fields of the flat inline type and copy them to 'this' 538 for (int i = 0; i < vk->nof_nonstatic_fields(); ++i) { 539 ciField* flat_field = vk->nonstatic_field_at(i); 540 // Adjust offset to account for missing oop header 541 int offset = field_offset + (flat_field->offset_in_bytes() - vk->payload_offset()); 542 // A flat field can be treated as final if the non-flat 543 // field is declared final or the holder klass is an inline type itself. 544 bool is_final = fd.is_final() || is_inlinetype(); 545 ciField* field = new (arena) ciField(flat_field, this, offset, is_final); 546 fields->append(field); 547 } 548 } else { 549 ciField* field = new (arena) ciField(&fd); 550 fields->append(field); 551 } 552 } 553 assert(fields->length() == flen, "sanity"); 554 fields->sort(sort_field_by_offset); 555 return fields; 556 } 557 558 bool ciInstanceKlass::compute_injected_fields_helper() { 559 ASSERT_IN_VM; 560 InstanceKlass* k = get_instanceKlass(); 561 562 for (InternalFieldStream fs(k); !fs.done(); fs.next()) { 563 if (fs.access_flags().is_static()) continue; 564 return true; 565 } 566 return false; 567 } 568 569 void ciInstanceKlass::compute_injected_fields() { 570 assert(is_loaded(), "must be loaded"); 571 572 int has_injected_fields = 0; 573 if (super() != nullptr && super()->has_injected_fields()) { 574 has_injected_fields = 1; 575 } else { 576 GUARDED_VM_ENTRY({ 577 has_injected_fields = compute_injected_fields_helper() ? 1 : 0; 578 }); 579 } 580 // may be concurrently initialized for shared ciInstanceKlass objects 581 assert(_has_injected_fields == -1 || _has_injected_fields == has_injected_fields, "broken concurrent initialization"); 582 _has_injected_fields = has_injected_fields; 583 } 584 585 bool ciInstanceKlass::has_object_fields() const { 586 GUARDED_VM_ENTRY( 587 return get_instanceKlass()->nonstatic_oop_map_size() > 0; 588 ); 589 } 590 591 bool ciInstanceKlass::compute_has_trusted_loader() { 592 ASSERT_IN_VM; 593 oop loader_oop = loader(); 594 if (loader_oop == nullptr) { 595 return true; // bootstrap class loader 596 } 597 return java_lang_ClassLoader::is_trusted_loader(loader_oop); 598 } 599 600 // ------------------------------------------------------------------ 601 // ciInstanceKlass::find_method 602 // 603 // Find a method in this klass. 604 ciMethod* ciInstanceKlass::find_method(ciSymbol* name, ciSymbol* signature) { 605 VM_ENTRY_MARK; 606 InstanceKlass* k = get_instanceKlass(); 607 Symbol* name_sym = name->get_symbol(); 608 Symbol* sig_sym= signature->get_symbol(); 609 610 Method* m = k->find_method(name_sym, sig_sym); 611 if (m == nullptr) return nullptr; 612 613 return CURRENT_THREAD_ENV->get_method(m); 614 } 615 616 // ------------------------------------------------------------------ 617 // ciInstanceKlass::is_leaf_type 618 bool ciInstanceKlass::is_leaf_type() { 619 assert(is_loaded(), "must be loaded"); 620 if (is_shared()) { 621 return is_final(); // approximately correct 622 } else { 623 return !has_subklass() && (nof_implementors() == 0); 624 } 625 } 626 627 // ------------------------------------------------------------------ 628 // ciInstanceKlass::implementor 629 // 630 // Report an implementor of this interface. 631 // Note that there are various races here, since my copy 632 // of _nof_implementors might be out of date with respect 633 // to results returned by InstanceKlass::implementor. 634 // This is OK, since any dependencies we decide to assert 635 // will be checked later under the Compile_lock. 636 ciInstanceKlass* ciInstanceKlass::implementor() { 637 ciInstanceKlass* impl = _implementor; 638 if (impl == nullptr) { 639 if (is_shared()) { 640 impl = this; // assume a well-known interface never has a unique implementor 641 } else { 642 // Go into the VM to fetch the implementor. 643 VM_ENTRY_MARK; 644 InstanceKlass* ik = get_instanceKlass(); 645 Klass* implk = ik->implementor(); 646 if (implk != nullptr) { 647 if (implk == ik) { 648 // More than one implementors. Use 'this' in this case. 649 impl = this; 650 } else { 651 impl = CURRENT_THREAD_ENV->get_instance_klass(implk); 652 } 653 } 654 } 655 // Memoize this result. 656 _implementor = impl; 657 } 658 return impl; 659 } 660 661 bool ciInstanceKlass::can_be_inline_klass(bool is_exact) { 662 if (!EnableValhalla) { 663 return false; 664 } 665 if (!is_loaded() || is_inlinetype()) { 666 // Not loaded or known to be an inline klass 667 return true; 668 } 669 if (!is_exact) { 670 // Not exact, check if this is a valid super for an inline klass 671 VM_ENTRY_MARK; 672 return !get_instanceKlass()->access_flags().is_identity_class() || is_java_lang_Object() ; 673 } 674 return false; 675 } 676 677 // Utility class for printing of the contents of the static fields for 678 // use by compilation replay. It only prints out the information that 679 // could be consumed by the compiler, so for primitive types it prints 680 // out the actual value. For Strings it's the actual string value. 681 // For array types it it's first level array size since that's the 682 // only value which statically unchangeable. For all other reference 683 // types it simply prints out the dynamic type. 684 685 class StaticFieldPrinter : public FieldClosure { 686 protected: 687 outputStream* _out; 688 public: 689 StaticFieldPrinter(outputStream* out) : 690 _out(out) { 691 } 692 void do_field_helper(fieldDescriptor* fd, oop obj, bool is_flat); 693 }; 694 695 class StaticFinalFieldPrinter : public StaticFieldPrinter { 696 const char* _holder; 697 public: 698 StaticFinalFieldPrinter(outputStream* out, const char* holder) : 699 StaticFieldPrinter(out), _holder(holder) { 700 } 701 void do_field(fieldDescriptor* fd) { 702 if (fd->is_final() && !fd->has_initial_value()) { 703 ResourceMark rm; 704 InstanceKlass* holder = fd->field_holder(); 705 oop mirror = holder->java_mirror(); 706 _out->print("staticfield %s %s ", _holder, fd->name()->as_quoted_ascii()); 707 BasicType bt = fd->field_type(); 708 if (bt != T_OBJECT && bt != T_ARRAY) { 709 _out->print("%s ", fd->signature()->as_quoted_ascii()); 710 } 711 do_field_helper(fd, mirror, false); 712 _out->cr(); 713 } 714 } 715 }; 716 717 class InlineTypeFieldPrinter : public StaticFieldPrinter { 718 oop _obj; 719 public: 720 InlineTypeFieldPrinter(outputStream* out, oop obj) : 721 StaticFieldPrinter(out), _obj(obj) { 722 } 723 void do_field(fieldDescriptor* fd) { 724 do_field_helper(fd, _obj, true); 725 _out->print(" "); 726 } 727 }; 728 729 void StaticFieldPrinter::do_field_helper(fieldDescriptor* fd, oop mirror, bool is_flat) { 730 BasicType field_type = fd->field_type(); 731 switch (field_type) { 732 case T_BYTE: _out->print("%d", mirror->byte_field(fd->offset())); break; 733 case T_BOOLEAN: _out->print("%d", mirror->bool_field(fd->offset())); break; 734 case T_SHORT: _out->print("%d", mirror->short_field(fd->offset())); break; 735 case T_CHAR: _out->print("%d", mirror->char_field(fd->offset())); break; 736 case T_INT: _out->print("%d", mirror->int_field(fd->offset())); break; 737 case T_LONG: _out->print(INT64_FORMAT, (int64_t)(mirror->long_field(fd->offset()))); break; 738 case T_FLOAT: { 739 float f = mirror->float_field(fd->offset()); 740 _out->print("%d", *(int*)&f); 741 break; 742 } 743 case T_DOUBLE: { 744 double d = mirror->double_field(fd->offset()); 745 _out->print(INT64_FORMAT, *(int64_t*)&d); 746 break; 747 } 748 case T_ARRAY: // fall-through 749 case T_OBJECT: 750 if (!fd->is_null_free_inline_type()) { 751 _out->print("%s ", fd->signature()->as_quoted_ascii()); 752 oop value = mirror->obj_field_acquire(fd->offset()); 753 if (value == nullptr) { 754 if (field_type == T_ARRAY) { 755 _out->print("%d", -1); 756 } 757 _out->cr(); 758 } else if (value->is_instance()) { 759 assert(field_type == T_OBJECT, ""); 760 if (value->is_a(vmClasses::String_klass())) { 761 const char* ascii_value = java_lang_String::as_quoted_ascii(value); 762 _out->print("\"%s\"", (ascii_value != nullptr) ? ascii_value : ""); 763 } else { 764 const char* klass_name = value->klass()->name()->as_quoted_ascii(); 765 _out->print("%s", klass_name); 766 } 767 } else if (value->is_array()) { 768 typeArrayOop ta = (typeArrayOop)value; 769 _out->print("%d", ta->length()); 770 if (value->is_objArray() || value->is_flatArray()) { 771 objArrayOop oa = (objArrayOop)value; 772 const char* klass_name = value->klass()->name()->as_quoted_ascii(); 773 _out->print(" %s", klass_name); 774 } 775 } else { 776 ShouldNotReachHere(); 777 } 778 break; 779 } else { 780 // handling of null free inline type 781 ResetNoHandleMark rnhm; 782 Thread* THREAD = Thread::current(); 783 SignatureStream ss(fd->signature(), false); 784 Symbol* name = ss.as_symbol(); 785 assert(!HAS_PENDING_EXCEPTION, "can resolve klass?"); 786 InstanceKlass* holder = fd->field_holder(); 787 InstanceKlass* k = SystemDictionary::find_instance_klass(THREAD, name, 788 Handle(THREAD, holder->class_loader())); 789 assert(k != nullptr && !HAS_PENDING_EXCEPTION, "can resolve klass?"); 790 InlineKlass* vk = InlineKlass::cast(k); 791 oop obj; 792 if (is_flat) { 793 int field_offset = fd->offset() - vk->payload_offset(); 794 obj = cast_to_oop(cast_from_oop<address>(mirror) + field_offset); 795 } else { 796 obj = mirror->obj_field_acquire(fd->offset()); 797 } 798 InlineTypeFieldPrinter print_field(_out, obj); 799 vk->do_nonstatic_fields(&print_field); 800 break; 801 } 802 default: 803 ShouldNotReachHere(); 804 } 805 } 806 807 const char *ciInstanceKlass::replay_name() const { 808 return CURRENT_ENV->replay_name(get_instanceKlass()); 809 } 810 811 void ciInstanceKlass::dump_replay_instanceKlass(outputStream* out, InstanceKlass* ik) { 812 if (ik->is_hidden()) { 813 const char *name = CURRENT_ENV->dyno_name(ik); 814 if (name != nullptr) { 815 out->print_cr("instanceKlass %s # %s", name, ik->name()->as_quoted_ascii()); 816 } else { 817 out->print_cr("# instanceKlass %s", ik->name()->as_quoted_ascii()); 818 } 819 } else { 820 out->print_cr("instanceKlass %s", ik->name()->as_quoted_ascii()); 821 } 822 } 823 824 GrowableArray<ciInstanceKlass*>* ciInstanceKlass::transitive_interfaces() const{ 825 if (_transitive_interfaces == nullptr) { 826 const_cast<ciInstanceKlass*>(this)->compute_transitive_interfaces(); 827 } 828 return _transitive_interfaces; 829 } 830 831 void ciInstanceKlass::compute_transitive_interfaces() { 832 GUARDED_VM_ENTRY( 833 InstanceKlass* ik = get_instanceKlass(); 834 Array<InstanceKlass*>* interfaces = ik->transitive_interfaces(); 835 int orig_length = interfaces->length(); 836 Arena* arena = CURRENT_ENV->arena(); 837 int transitive_interfaces_len = orig_length + (is_interface() ? 1 : 0); 838 GrowableArray<ciInstanceKlass*>* transitive_interfaces = new(arena)GrowableArray<ciInstanceKlass*>(arena, transitive_interfaces_len, 839 0, nullptr); 840 for (int i = 0; i < orig_length; i++) { 841 transitive_interfaces->append(CURRENT_ENV->get_instance_klass(interfaces->at(i))); 842 } 843 if (is_interface()) { 844 transitive_interfaces->append(this); 845 } 846 _transitive_interfaces = transitive_interfaces; 847 ); 848 } 849 850 void ciInstanceKlass::dump_replay_data(outputStream* out) { 851 ResourceMark rm; 852 853 InstanceKlass* ik = get_instanceKlass(); 854 ConstantPool* cp = ik->constants(); 855 856 // Try to record related loaded classes 857 Klass* sub = ik->subklass(); 858 while (sub != nullptr) { 859 if (sub->is_instance_klass()) { 860 InstanceKlass *isub = InstanceKlass::cast(sub); 861 dump_replay_instanceKlass(out, isub); 862 } 863 sub = sub->next_sibling(); 864 } 865 866 // Dump out the state of the constant pool tags. During replay the 867 // tags will be validated for things which shouldn't change and 868 // classes will be resolved if the tags indicate that they were 869 // resolved at compile time. 870 const char *name = replay_name(); 871 out->print("ciInstanceKlass %s %d %d %d", name, 872 is_linked(), is_initialized(), cp->length()); 873 for (int index = 1; index < cp->length(); index++) { 874 out->print(" %d", cp->tags()->at(index)); 875 } 876 out->cr(); 877 if (is_initialized()) { 878 // Dump out the static final fields in case the compilation relies 879 // on their value for correct replay. 880 StaticFinalFieldPrinter sffp(out, name); 881 ik->do_local_static_fields(&sffp); 882 } 883 } 884 885 #ifdef ASSERT 886 bool ciInstanceKlass::debug_final_field_at(int offset) { 887 GUARDED_VM_ENTRY( 888 InstanceKlass* ik = get_instanceKlass(); 889 fieldDescriptor fd; 890 if (ik->find_field_from_offset(offset, false, &fd)) { 891 return fd.is_final(); 892 } 893 ); 894 return false; 895 } 896 897 bool ciInstanceKlass::debug_stable_field_at(int offset) { 898 GUARDED_VM_ENTRY( 899 InstanceKlass* ik = get_instanceKlass(); 900 fieldDescriptor fd; 901 if (ik->find_field_from_offset(offset, false, &fd)) { 902 return fd.is_stable(); 903 } 904 ); 905 return false; 906 } 907 #endif