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