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