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 ciField* ciInstanceKlass::get_non_flattened_field_by_offset(int field_offset) {
 425   if (super() != NULL && super()->has_nonstatic_fields()) {
 426     ciField* f = super()->get_non_flattened_field_by_offset(field_offset);
 427     if (f != NULL) {
 428       return f;
 429     }
 430   }
 431 
 432   VM_ENTRY_MARK;
 433   InstanceKlass* k = get_instanceKlass();
 434   Arena* arena = CURRENT_ENV->arena();
 435   for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
 436     if (fs.access_flags().is_static())  continue;
 437     fieldDescriptor& fd = fs.field_descriptor();
 438     if (fd.offset() == field_offset) {
 439       ciField* f = new (arena) ciField(&fd);
 440       return f;
 441     }
 442   }
 443 
 444   return NULL;
 445 }
 446 
 447 // ------------------------------------------------------------------
 448 // ciInstanceKlass::get_field_by_name
 449 ciField* ciInstanceKlass::get_field_by_name(ciSymbol* name, ciSymbol* signature, bool is_static) {
 450   VM_ENTRY_MARK;
 451   InstanceKlass* k = get_instanceKlass();
 452   fieldDescriptor fd;
 453   Klass* def = k->find_field(name->get_symbol(), signature->get_symbol(), is_static, &fd);
 454   if (def == NULL) {
 455     return NULL;
 456   }
 457   ciField* field = new (CURRENT_THREAD_ENV->arena()) ciField(&fd);
 458   return field;
 459 }
 460 
 461 
 462 static int sort_field_by_offset(ciField** a, ciField** b) {
 463   return (*a)->offset_in_bytes() - (*b)->offset_in_bytes();
 464   // (no worries about 32-bit overflow...)
 465 }
 466 
 467 // ------------------------------------------------------------------
 468 // ciInstanceKlass::compute_nonstatic_fields
 469 int ciInstanceKlass::compute_nonstatic_fields() {
 470   assert(is_loaded(), "must be loaded");
 471 
 472   if (_nonstatic_fields != NULL)
 473     return _nonstatic_fields->length();
 474 
 475   if (!has_nonstatic_fields()) {
 476     Arena* arena = CURRENT_ENV->arena();
 477     _nonstatic_fields = new (arena) GrowableArray<ciField*>(arena, 0, 0, NULL);
 478     return 0;
 479   }
 480   assert(!is_java_lang_Object(), "bootstrap OK");
 481 
 482   // Size in bytes of my fields, including inherited fields.
 483   int fsize = nonstatic_field_size() * heapOopSize;
 484 
 485   ciInstanceKlass* super = this->super();
 486   GrowableArray<ciField*>* super_fields = NULL;
 487   if (super != NULL && super->has_nonstatic_fields()) {
 488     int super_fsize  = super->nonstatic_field_size() * heapOopSize;
 489     int super_flen   = super->nof_nonstatic_fields();
 490     super_fields = super->_nonstatic_fields;
 491     assert(super_flen == 0 || super_fields != NULL, "first get nof_fields");
 492     // See if I am no larger than my super; if so, I can use his fields.
 493     if (fsize == super_fsize) {
 494       _nonstatic_fields = super_fields;
 495       return super_fields->length();
 496     }
 497   }
 498 
 499   GrowableArray<ciField*>* fields = NULL;
 500   GUARDED_VM_ENTRY({
 501       fields = compute_nonstatic_fields_impl(super_fields);
 502     });
 503 
 504   if (fields == NULL) {
 505     // This can happen if this class (java.lang.Class) has invisible fields.
 506     if (super_fields != NULL) {
 507       _nonstatic_fields = super_fields;
 508       return super_fields->length();
 509     } else {
 510       return 0;
 511     }
 512   }
 513 
 514   _nonstatic_fields = fields;
 515   return fields->length();
 516 }
 517 
 518 GrowableArray<ciField*>* ciInstanceKlass::compute_nonstatic_fields_impl(GrowableArray<ciField*>* super_fields, bool flatten) {
 519   ASSERT_IN_VM;
 520   Arena* arena = CURRENT_ENV->arena();
 521   int flen = 0;
 522   GrowableArray<ciField*>* fields = NULL;
 523   InstanceKlass* k = get_instanceKlass();
 524   for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
 525     if (fs.access_flags().is_static())  continue;
 526     flen += 1;
 527   }
 528 
 529   // allocate the array:
 530   if (flen == 0) {
 531     return NULL;  // return nothing if none are locally declared
 532   }
 533   if (super_fields != NULL) {
 534     flen += super_fields->length();
 535   }
 536 
 537   fields = new (arena) GrowableArray<ciField*>(arena, flen, 0, NULL);
 538   if (super_fields != NULL) {
 539     fields->appendAll(super_fields);
 540   }
 541 
 542   for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
 543     if (fs.access_flags().is_static())  continue;
 544     fieldDescriptor& fd = fs.field_descriptor();
 545     if (fd.is_flattened() && flatten) {
 546       // Value type fields are embedded
 547       int field_offset = fd.offset();
 548       // Get ValueKlass and adjust number of fields
 549       Klass* k = get_instanceKlass()->get_value_field_klass(fd.index());
 550       ciValueKlass* vk = CURRENT_ENV->get_klass(k)->as_value_klass();
 551       flen += vk->nof_nonstatic_fields() - 1;
 552       // Iterate over fields of the flattened value type and copy them to 'this'
 553       for (int i = 0; i < vk->nof_nonstatic_fields(); ++i) {
 554         ciField* flattened_field = vk->nonstatic_field_at(i);
 555         // Adjust offset to account for missing oop header
 556         int offset = field_offset + (flattened_field->offset() - vk->first_field_offset());
 557         // A flattened field can be treated as final if the non-flattened
 558         // field is declared final or the holder klass is a value type itself.
 559         bool is_final = fd.is_final() || is_valuetype();
 560         ciField* field = new (arena) ciField(flattened_field, this, offset, is_final);
 561         fields->append(field);
 562       }
 563     } else {
 564       ciField* field = new (arena) ciField(&fd);
 565       fields->append(field);
 566     }
 567   }
 568   assert(fields->length() == flen, "sanity");
 569   // Now sort them by offset, ascending.
 570   // (In principle, they could mix with superclass fields.)
 571   fields->sort(sort_field_by_offset);
 572   return fields;
 573 }
 574 
 575 bool ciInstanceKlass::compute_injected_fields_helper() {
 576   ASSERT_IN_VM;
 577   InstanceKlass* k = get_instanceKlass();
 578 
 579   for (InternalFieldStream fs(k); !fs.done(); fs.next()) {
 580     if (fs.access_flags().is_static())  continue;
 581     return true;
 582   }
 583   return false;
 584 }
 585 
 586 void ciInstanceKlass::compute_injected_fields() {
 587   assert(is_loaded(), "must be loaded");
 588 
 589   int has_injected_fields = 0;
 590   if (super() != NULL && super()->has_injected_fields()) {
 591     has_injected_fields = 1;
 592   } else {
 593     GUARDED_VM_ENTRY({
 594         has_injected_fields = compute_injected_fields_helper() ? 1 : 0;
 595       });
 596   }
 597   // may be concurrently initialized for shared ciInstanceKlass objects
 598   assert(_has_injected_fields == -1 || _has_injected_fields == has_injected_fields, "broken concurrent initialization");
 599   _has_injected_fields = has_injected_fields;
 600 }
 601 
 602 bool ciInstanceKlass::has_object_fields() const {
 603   GUARDED_VM_ENTRY(
 604       return get_instanceKlass()->nonstatic_oop_map_size() > 0;
 605     );
 606 }
 607 
 608 // ------------------------------------------------------------------
 609 // ciInstanceKlass::find_method
 610 //
 611 // Find a method in this klass.
 612 ciMethod* ciInstanceKlass::find_method(ciSymbol* name, ciSymbol* signature) {
 613   VM_ENTRY_MARK;
 614   InstanceKlass* k = get_instanceKlass();
 615   Symbol* name_sym = name->get_symbol();
 616   Symbol* sig_sym= signature->get_symbol();
 617 
 618   Method* m = k->find_method(name_sym, sig_sym);
 619   if (m == NULL)  return NULL;
 620 
 621   return CURRENT_THREAD_ENV->get_method(m);
 622 }
 623 
 624 // ------------------------------------------------------------------
 625 // ciInstanceKlass::is_leaf_type
 626 bool ciInstanceKlass::is_leaf_type() {
 627   assert(is_loaded(), "must be loaded");
 628   if (is_shared()) {
 629     return is_final();  // approximately correct
 630   } else {
 631     return !has_subklass() && (nof_implementors() == 0);
 632   }
 633 }
 634 
 635 // ------------------------------------------------------------------
 636 // ciInstanceKlass::implementor
 637 //
 638 // Report an implementor of this interface.
 639 // Note that there are various races here, since my copy
 640 // of _nof_implementors might be out of date with respect
 641 // to results returned by InstanceKlass::implementor.
 642 // This is OK, since any dependencies we decide to assert
 643 // will be checked later under the Compile_lock.
 644 ciInstanceKlass* ciInstanceKlass::implementor() {
 645   ciInstanceKlass* impl = _implementor;
 646   if (impl == NULL) {
 647     // Go into the VM to fetch the implementor.
 648     {
 649       VM_ENTRY_MARK;
 650       MutexLocker ml(Compile_lock);
 651       Klass* k = get_instanceKlass()->implementor();
 652       if (k != NULL) {
 653         if (k == get_instanceKlass()) {
 654           // More than one implementors. Use 'this' in this case.
 655           impl = this;
 656         } else {
 657           impl = CURRENT_THREAD_ENV->get_instance_klass(k);
 658         }
 659       }
 660     }
 661     // Memoize this result.
 662     if (!is_shared()) {
 663       _implementor = impl;
 664     }
 665   }
 666   return impl;
 667 }
 668 
 669 ciInstanceKlass* ciInstanceKlass::unsafe_anonymous_host() {
 670   assert(is_loaded(), "must be loaded");
 671   if (is_unsafe_anonymous()) {
 672     VM_ENTRY_MARK
 673     Klass* unsafe_anonymous_host = get_instanceKlass()->unsafe_anonymous_host();
 674     return CURRENT_ENV->get_instance_klass(unsafe_anonymous_host);
 675   }
 676   return NULL;
 677 }
 678 
 679 ciInstanceKlass* ciInstanceKlass::vcc_klass() {
 680   return NULL;
 681 }
 682 
 683 // Utility class for printing of the contents of the static fields for
 684 // use by compilation replay.  It only prints out the information that
 685 // could be consumed by the compiler, so for primitive types it prints
 686 // out the actual value.  For Strings it's the actual string value.
 687 // For array types it it's first level array size since that's the
 688 // only value which statically unchangeable.  For all other reference
 689 // types it simply prints out the dynamic type.
 690 
 691 class StaticFieldPrinter : public FieldClosure {
 692 protected:
 693   outputStream* _out;
 694 public:
 695   StaticFieldPrinter(outputStream* out) :
 696     _out(out) {
 697   }
 698   void do_field_helper(fieldDescriptor* fd, oop obj, bool flattened);
 699 };
 700 
 701 class StaticFinalFieldPrinter : public StaticFieldPrinter {
 702   const char*   _holder;
 703  public:
 704   StaticFinalFieldPrinter(outputStream* out, const char* holder) :
 705     StaticFieldPrinter(out), _holder(holder) {
 706   }
 707   void do_field(fieldDescriptor* fd) {
 708     if (fd->is_final() && !fd->has_initial_value()) {
 709       ResourceMark rm;
 710       InstanceKlass* holder = fd->field_holder();
 711       oop mirror = holder->java_mirror();
 712       _out->print("staticfield %s %s ", _holder, fd->name()->as_quoted_ascii());
 713       BasicType bt = fd->field_type();
 714       if (bt != T_OBJECT && bt != T_ARRAY) {
 715         _out->print("%s ", fd->signature()->as_quoted_ascii());
 716       }
 717       do_field_helper(fd, mirror, false);
 718       _out->cr();
 719     }
 720   }
 721 };
 722 
 723 class ValueTypeFieldPrinter : public StaticFieldPrinter {
 724   oop _obj;
 725 public:
 726   ValueTypeFieldPrinter(outputStream* out, oop obj) :
 727     StaticFieldPrinter(out), _obj(obj) {
 728   }
 729   void do_field(fieldDescriptor* fd) {
 730     do_field_helper(fd, _obj, true);
 731     _out->print(" ");
 732   }
 733 };
 734 
 735 void StaticFieldPrinter::do_field_helper(fieldDescriptor* fd, oop mirror, bool flattened) {
 736   BasicType bt = fd->field_type();
 737   switch (bt) {
 738     case T_BYTE:    _out->print("%d", mirror->byte_field(fd->offset()));   break;
 739     case T_BOOLEAN: _out->print("%d", mirror->bool_field(fd->offset()));   break;
 740     case T_SHORT:   _out->print("%d", mirror->short_field(fd->offset()));  break;
 741     case T_CHAR:    _out->print("%d", mirror->char_field(fd->offset()));   break;
 742     case T_INT:     _out->print("%d", mirror->int_field(fd->offset()));    break;
 743     case T_LONG:    _out->print(INT64_FORMAT, (int64_t)(mirror->long_field(fd->offset())));   break;
 744     case T_FLOAT: {
 745       float f = mirror->float_field(fd->offset());
 746       _out->print("%d", *(int*)&f);
 747       break;
 748     }
 749     case T_DOUBLE: {
 750       double d = mirror->double_field(fd->offset());
 751       _out->print(INT64_FORMAT, *(int64_t*)&d);
 752       break;
 753     }
 754     case T_ARRAY:  // fall-through
 755     case T_OBJECT: {
 756       oop value =  mirror->obj_field_acquire(fd->offset());
 757       if (value == NULL) {
 758         _out->print_cr("null");
 759       } else if (value->is_instance()) {
 760         assert(fd->field_type() == T_OBJECT, "");
 761         if (value->is_a(SystemDictionary::String_klass())) {
 762           const char* ascii_value = java_lang_String::as_quoted_ascii(value);
 763           _out->print("\"%s\"", (ascii_value != NULL) ? ascii_value : "");
 764          } else {
 765           const char* klass_name  = value->klass()->name()->as_quoted_ascii();
 766           _out->print_cr("%s", klass_name);
 767         }
 768       } else if (value->is_array()) {
 769         typeArrayOop ta = (typeArrayOop)value;
 770         _out->print("%d", ta->length());
 771         if (value->is_objArray() || value->is_valueArray()) {
 772           objArrayOop oa = (objArrayOop)value;
 773           const char* klass_name  = value->klass()->name()->as_quoted_ascii();
 774           _out->print(" %s", klass_name);
 775         }
 776         _out->cr();
 777       } else {
 778         ShouldNotReachHere();
 779       }
 780       break;
 781     }
 782     case T_VALUETYPE: {
 783       ResetNoHandleMark rnhm;
 784       Thread* THREAD = Thread::current();
 785       SignatureStream ss(fd->signature(), false);
 786       Symbol* name = ss.as_symbol(THREAD);
 787       assert(!HAS_PENDING_EXCEPTION, "can resolve klass?");
 788       InstanceKlass* holder = fd->field_holder();
 789       Klass* k = SystemDictionary::find(name, Handle(THREAD, holder->class_loader()),
 790                                         Handle(THREAD, holder->protection_domain()), THREAD);
 791       assert(k != NULL && !HAS_PENDING_EXCEPTION, "can resolve klass?");
 792       ValueKlass* vk = ValueKlass::cast(k);
 793       oop obj;
 794       if (flattened) {
 795         int field_offset = fd->offset() - vk->first_field_offset();
 796         obj = (oop)((address)mirror + field_offset);
 797       } else {
 798         obj =  mirror->obj_field_acquire(fd->offset());
 799       }
 800       ValueTypeFieldPrinter print_field(_out, obj);
 801       vk->do_nonstatic_fields(&print_field);
 802       break;
 803     }
 804     default:
 805       ShouldNotReachHere();
 806   }
 807 }
 808 
 809 
 810 void ciInstanceKlass::dump_replay_data(outputStream* out) {
 811   ResourceMark rm;
 812 
 813   InstanceKlass* ik = get_instanceKlass();
 814   ConstantPool*  cp = ik->constants();
 815 
 816   // Try to record related loaded classes
 817   Klass* sub = ik->subklass();
 818   while (sub != NULL) {
 819     if (sub->is_instance_klass()) {
 820       out->print_cr("instanceKlass %s", sub->name()->as_quoted_ascii());
 821     }
 822     sub = sub->next_sibling();
 823   }
 824 
 825   // Dump out the state of the constant pool tags.  During replay the
 826   // tags will be validated for things which shouldn't change and
 827   // classes will be resolved if the tags indicate that they were
 828   // resolved at compile time.
 829   out->print("ciInstanceKlass %s %d %d %d", ik->name()->as_quoted_ascii(),
 830              is_linked(), is_initialized(), cp->length());
 831   for (int index = 1; index < cp->length(); index++) {
 832     out->print(" %d", cp->tags()->at(index));
 833   }
 834   out->cr();
 835   if (is_initialized()) {
 836     //  Dump out the static final fields in case the compilation relies
 837     //  on their value for correct replay.
 838     StaticFinalFieldPrinter sffp(out, ik->name()->as_quoted_ascii());
 839     ik->do_local_static_fields(&sffp);
 840   }
 841 }
 842 
 843 #ifdef ASSERT
 844 bool ciInstanceKlass::debug_final_field_at(int offset) {
 845   GUARDED_VM_ENTRY(
 846     InstanceKlass* ik = get_instanceKlass();
 847     fieldDescriptor fd;
 848     if (ik->find_field_from_offset(offset, false, &fd)) {
 849       return fd.is_final();
 850     }
 851   );
 852   return false;
 853 }
 854 
 855 bool ciInstanceKlass::debug_stable_field_at(int offset) {
 856   GUARDED_VM_ENTRY(
 857     InstanceKlass* ik = get_instanceKlass();
 858     fieldDescriptor fd;
 859     if (ik->find_field_from_offset(offset, false, &fd)) {
 860       return fd.is_stable();
 861     }
 862   );
 863   return false;
 864 }
 865 #endif