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