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