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