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