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