1 /*
  2  * Copyright (c) 1999, 2021, 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/ciInlineKlass.hpp"
 28 #include "ci/ciInstance.hpp"
 29 #include "ci/ciInstanceKlass.hpp"
 30 #include "ci/ciUtilities.inline.hpp"
 31 #include "classfile/javaClasses.hpp"
 32 #include "classfile/systemDictionary.hpp"
 33 #include "classfile/vmClasses.hpp"
 34 #include "memory/allocation.hpp"
 35 #include "memory/allocation.inline.hpp"
 36 #include "memory/resourceArea.hpp"
 37 #include "oops/instanceKlass.inline.hpp"
 38 #include "oops/klass.inline.hpp"
 39 #include "oops/oop.inline.hpp"
 40 #include "oops/fieldStreams.inline.hpp"
 41 #include "oops/inlineKlass.inline.hpp"
 42 #include "runtime/fieldDescriptor.inline.hpp"
 43 #include "runtime/handles.inline.hpp"
 44 #include "runtime/jniHandles.inline.hpp"
 45 
 46 // ciInstanceKlass
 47 //
 48 // This class represents a Klass* in the HotSpot virtual machine
 49 // whose Klass part in an InstanceKlass.
 50 
 51 
 52 // ------------------------------------------------------------------
 53 // ciInstanceKlass::ciInstanceKlass
 54 //
 55 // Loaded instance klass.
 56 ciInstanceKlass::ciInstanceKlass(Klass* k) :
 57   ciKlass(k)
 58 {
 59   assert(get_Klass()->is_instance_klass(), "wrong type");
 60   assert(get_instanceKlass()->is_loaded(), "must be at least loaded");
 61   InstanceKlass* ik = get_instanceKlass();
 62 
 63   AccessFlags access_flags = ik->access_flags();
 64   _flags = ciFlags(access_flags);
 65   _has_finalizer = access_flags.has_finalizer();
 66   _has_subklass = flags().is_final() ? subklass_false : subklass_unknown;
 67   _init_state = ik->init_state();
 68   _has_nonstatic_fields = ik->has_nonstatic_fields();
 69   _has_nonstatic_concrete_methods = ik->has_nonstatic_concrete_methods();
 70   _is_hidden = ik->is_hidden();
 71   _is_record = ik->is_record();
 72   _nonstatic_fields = NULL; // initialized lazily by compute_nonstatic_fields:
 73   _has_injected_fields = -1;
 74   _implementor = NULL; // we will fill these lazily
 75 
 76   // Ensure that the metadata wrapped by the ciMetadata is kept alive by GC.
 77   // This is primarily useful for metadata which is considered as weak roots
 78   // by the GC but need to be strong roots if reachable from a current compilation.
 79   // InstanceKlass are created for both weak and strong metadata.  Ensuring this metadata
 80   // alive covers the cases where there are weak roots without performance cost.
 81   oop holder = ik->klass_holder();
 82   if (ik->class_loader_data()->has_class_mirror_holder()) {
 83     // Though ciInstanceKlass records class loader oop, it's not enough to keep
 84     // non-strong hidden classes alive (loader == NULL). Klass holder should
 85     // be used instead. It is enough to record a ciObject, since cached elements are never removed
 86     // during ciObjectFactory lifetime. ciObjectFactory itself is created for
 87     // every compilation and lives for the whole duration of the compilation.
 88     assert(holder != NULL, "holder of hidden class is the mirror which is never null");
 89     (void)CURRENT_ENV->get_object(holder);
 90   }
 91 
 92   JavaThread *thread = JavaThread::current();
 93   if (ciObjectFactory::is_initialized()) {
 94     _loader = JNIHandles::make_local(thread, ik->class_loader());
 95     _protection_domain = JNIHandles::make_local(thread,
 96                                                 ik->protection_domain());
 97     _is_shared = false;
 98   } else {
 99     Handle h_loader(thread, ik->class_loader());
100     Handle h_protection_domain(thread, ik->protection_domain());
101     _loader = JNIHandles::make_global(h_loader);
102     _protection_domain = JNIHandles::make_global(h_protection_domain);
103     _is_shared = true;
104   }
105 
106   // Lazy fields get filled in only upon request.
107   _super  = NULL;
108   _java_mirror = NULL;
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 = NULL;
118 }
119 
120 // Version for unloaded classes:
121 ciInstanceKlass::ciInstanceKlass(ciSymbol* name,
122                                  jobject loader, jobject protection_domain,
123                                  BasicType bt)
124   : ciKlass(name, bt)
125 {
126   assert(name->char_at(0) != JVM_SIGNATURE_ARRAY, "not an instance klass");
127   _init_state = (InstanceKlass::ClassState)0;
128   _has_nonstatic_fields = false;
129   _nonstatic_fields = NULL;            // initialized lazily by compute_nonstatic_fields
130   _has_injected_fields = -1;
131   _is_hidden = false;
132   _is_record = false;
133   _loader = loader;
134   _protection_domain = protection_domain;
135   _is_shared = false;
136   _super = NULL;
137   _java_mirror = NULL;
138   _field_cache = NULL;
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() != NULL ? 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 NULL;
195   }
196   if (_field_cache == NULL) {
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 != NULL) {
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 == NULL ||
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 == NULL;
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 static bool is_klass_initialized(Symbol* klass_name) {
288   VM_ENTRY_MARK;
289   InstanceKlass* ik = SystemDictionary::find_instance_klass(klass_name, Handle(), Handle());
290   return ik != nullptr && ik->is_initialized();
291 }
292 
293 bool ciInstanceKlass::is_box_cache_valid() const {
294   BasicType box_type = box_klass_type();
295 
296   if (box_type != T_OBJECT) {
297     switch(box_type) {
298       case T_INT:     return is_klass_initialized(java_lang_Integer_IntegerCache::symbol());
299       case T_CHAR:    return is_klass_initialized(java_lang_Character_CharacterCache::symbol());
300       case T_SHORT:   return is_klass_initialized(java_lang_Short_ShortCache::symbol());
301       case T_BYTE:    return is_klass_initialized(java_lang_Byte_ByteCache::symbol());
302       case T_LONG:    return is_klass_initialized(java_lang_Long_LongCache::symbol());
303       case T_BOOLEAN:
304       case T_FLOAT:
305       case T_DOUBLE:  return true;
306       default:;
307     }
308   }
309   return false;
310 }
311 
312 // ------------------------------------------------------------------
313 // ciInstanceKlass::is_in_package
314 //
315 // Is this klass in the given package?
316 bool ciInstanceKlass::is_in_package(const char* packagename, int len) {
317   // To avoid class loader mischief, this test always rejects application classes.
318   if (!uses_default_loader())
319     return false;
320   GUARDED_VM_ENTRY(
321     return is_in_package_impl(packagename, len);
322   )
323 }
324 
325 bool ciInstanceKlass::is_in_package_impl(const char* packagename, int len) {
326   ASSERT_IN_VM;
327 
328   // If packagename contains trailing '/' exclude it from the
329   // prefix-test since we test for it explicitly.
330   if (packagename[len - 1] == '/')
331     len--;
332 
333   if (!name()->starts_with(packagename, len))
334     return false;
335 
336   // Test if the class name is something like "java/lang".
337   if ((len + 1) > name()->utf8_length())
338     return false;
339 
340   // Test for trailing '/'
341   if (name()->char_at(len) != '/')
342     return false;
343 
344   // Make sure it's not actually in a subpackage:
345   if (name()->index_of_at(len+1, "/", 1) >= 0)
346     return false;
347 
348   return true;
349 }
350 
351 // ------------------------------------------------------------------
352 // ciInstanceKlass::print_impl
353 //
354 // Implementation of the print method.
355 void ciInstanceKlass::print_impl(outputStream* st) {
356   ciKlass::print_impl(st);
357   GUARDED_VM_ENTRY(st->print(" loader=" INTPTR_FORMAT, p2i(loader()));)
358   if (is_loaded()) {
359     st->print(" loaded=true initialized=%s finalized=%s subklass=%s size=%d flags=",
360               bool_to_str(is_initialized()),
361               bool_to_str(has_finalizer()),
362               bool_to_str(has_subklass()),
363               layout_helper());
364 
365     _flags.print_klass_flags(st);
366 
367     if (_super) {
368       st->print(" super=");
369       _super->print_name_on(st);
370     }
371     if (_java_mirror) {
372       st->print(" mirror=PRESENT");
373     }
374   } else {
375     st->print(" loaded=false");
376   }
377 }
378 
379 // ------------------------------------------------------------------
380 // ciInstanceKlass::super
381 //
382 // Get the superklass of this klass.
383 ciInstanceKlass* ciInstanceKlass::super() {
384   assert(is_loaded(), "must be loaded");
385   if (_super == NULL && !is_java_lang_Object()) {
386     GUARDED_VM_ENTRY(
387       Klass* super_klass = get_instanceKlass()->super();
388       _super = CURRENT_ENV->get_instance_klass(super_klass);
389     )
390   }
391   return _super;
392 }
393 
394 // ------------------------------------------------------------------
395 // ciInstanceKlass::java_mirror
396 //
397 // Get the instance of java.lang.Class corresponding to this klass.
398 // Cache it on this->_java_mirror.
399 ciInstance* ciInstanceKlass::java_mirror() {
400   if (is_shared()) {
401     return ciKlass::java_mirror();
402   }
403   if (_java_mirror == NULL) {
404     _java_mirror = ciKlass::java_mirror();
405   }
406   return _java_mirror;
407 }
408 
409 // ------------------------------------------------------------------
410 // ciInstanceKlass::unique_concrete_subklass
411 ciInstanceKlass* ciInstanceKlass::unique_concrete_subklass() {
412   if (!is_loaded())     return NULL; // No change if class is not loaded
413   if (!is_abstract())   return NULL; // Only applies to abstract classes.
414   if (!has_subklass())  return NULL; // Must have at least one subklass.
415   VM_ENTRY_MARK;
416   InstanceKlass* ik = get_instanceKlass();
417   Klass* up = ik->up_cast_abstract();
418   assert(up->is_instance_klass(), "must be InstanceKlass");
419   if (ik == up) {
420     return NULL;
421   }
422   return CURRENT_THREAD_ENV->get_instance_klass(up);
423 }
424 
425 // ------------------------------------------------------------------
426 // ciInstanceKlass::has_finalizable_subclass
427 bool ciInstanceKlass::has_finalizable_subclass() {
428   if (!is_loaded())     return true;
429   VM_ENTRY_MARK;
430   return Dependencies::find_finalizable_subclass(get_instanceKlass()) != NULL;
431 }
432 
433 // ------------------------------------------------------------------
434 // ciInstanceKlass::contains_field_offset
435 bool ciInstanceKlass::contains_field_offset(int offset) {
436   VM_ENTRY_MARK;
437   return get_instanceKlass()->contains_field_offset(offset);
438 }
439 
440 // ------------------------------------------------------------------
441 // ciInstanceKlass::get_field_by_offset
442 ciField* ciInstanceKlass::get_field_by_offset(int field_offset, bool is_static) {
443   if (!is_static) {
444     for (int i = 0, len = nof_nonstatic_fields(); i < len; i++) {
445       ciField* field = _nonstatic_fields->at(i);
446       int  field_off = field->offset_in_bytes();
447       if (field_off == field_offset)
448         return field;
449       if (field_off > field_offset)
450         break;
451       // could do binary search or check bins, but probably not worth it
452     }
453     return NULL;
454   }
455   VM_ENTRY_MARK;
456   InstanceKlass* k = get_instanceKlass();
457   fieldDescriptor fd;
458   if (!k->find_field_from_offset(field_offset, is_static, &fd)) {
459     return NULL;
460   }
461   ciField* field = new (CURRENT_THREAD_ENV->arena()) ciField(&fd);
462   return field;
463 }
464 
465 ciField* ciInstanceKlass::get_non_flattened_field_by_offset(int field_offset) {
466   if (super() != NULL && super()->has_nonstatic_fields()) {
467     ciField* f = super()->get_non_flattened_field_by_offset(field_offset);
468     if (f != NULL) {
469       return f;
470     }
471   }
472 
473   VM_ENTRY_MARK;
474   InstanceKlass* k = get_instanceKlass();
475   Arena* arena = CURRENT_ENV->arena();
476   for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
477     if (fs.access_flags().is_static())  continue;
478     fieldDescriptor& fd = fs.field_descriptor();
479     if (fd.offset() == field_offset) {
480       ciField* f = new (arena) ciField(&fd);
481       return f;
482     }
483   }
484 
485   return NULL;
486 }
487 
488 // ------------------------------------------------------------------
489 // ciInstanceKlass::get_field_by_name
490 ciField* ciInstanceKlass::get_field_by_name(ciSymbol* name, ciSymbol* signature, bool is_static) {
491   VM_ENTRY_MARK;
492   InstanceKlass* k = get_instanceKlass();
493   fieldDescriptor fd;
494   Klass* def = k->find_field(name->get_symbol(), signature->get_symbol(), is_static, &fd);
495   if (def == NULL) {
496     return NULL;
497   }
498   ciField* field = new (CURRENT_THREAD_ENV->arena()) ciField(&fd);
499   return field;
500 }
501 
502 
503 static int sort_field_by_offset(ciField** a, ciField** b) {
504   return (*a)->offset_in_bytes() - (*b)->offset_in_bytes();
505   // (no worries about 32-bit overflow...)
506 }
507 
508 // ------------------------------------------------------------------
509 // ciInstanceKlass::compute_nonstatic_fields
510 int ciInstanceKlass::compute_nonstatic_fields() {
511   assert(is_loaded(), "must be loaded");
512 
513   if (_nonstatic_fields != NULL)
514     return _nonstatic_fields->length();
515 
516   if (!has_nonstatic_fields()) {
517     Arena* arena = CURRENT_ENV->arena();
518     _nonstatic_fields = new (arena) GrowableArray<ciField*>(arena, 0, 0, NULL);
519     return 0;
520   }
521   assert(!is_java_lang_Object(), "bootstrap OK");
522 
523   ciInstanceKlass* super = this->super();
524   GrowableArray<ciField*>* super_fields = NULL;
525   if (super != NULL && super->has_nonstatic_fields()) {
526     int super_flen   = super->nof_nonstatic_fields();
527     super_fields = super->_nonstatic_fields;
528     assert(super_flen == 0 || super_fields != NULL, "first get nof_fields");
529   }
530 
531   GrowableArray<ciField*>* fields = NULL;
532   GUARDED_VM_ENTRY({
533       fields = compute_nonstatic_fields_impl(super_fields);
534     });
535 
536   if (fields == NULL) {
537     // This can happen if this class (java.lang.Class) has invisible fields.
538     if (super_fields != NULL) {
539       _nonstatic_fields = super_fields;
540       return super_fields->length();
541     } else {
542       return 0;
543     }
544   }
545 
546   _nonstatic_fields = fields;
547   return fields->length();
548 }
549 
550 GrowableArray<ciField*>* ciInstanceKlass::compute_nonstatic_fields_impl(GrowableArray<ciField*>* super_fields, bool flatten) {
551   ASSERT_IN_VM;
552   Arena* arena = CURRENT_ENV->arena();
553   int flen = 0;
554   GrowableArray<ciField*>* fields = NULL;
555   InstanceKlass* k = get_instanceKlass();
556   for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
557     if (fs.access_flags().is_static())  continue;
558     flen += 1;
559   }
560 
561   // allocate the array:
562   if (flen == 0) {
563     return NULL;  // return nothing if none are locally declared
564   }
565   if (super_fields != NULL) {
566     flen += super_fields->length();
567   }
568 
569   fields = new (arena) GrowableArray<ciField*>(arena, flen, 0, NULL);
570   if (super_fields != NULL) {
571     fields->appendAll(super_fields);
572   }
573 
574   for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
575     if (fs.access_flags().is_static())  continue;
576     fieldDescriptor& fd = fs.field_descriptor();
577     if (fd.is_inlined() && flatten) {
578       // Inline type fields are embedded
579       int field_offset = fd.offset();
580       // Get InlineKlass and adjust number of fields
581       Klass* k = get_instanceKlass()->get_inline_type_field_klass(fd.index());
582       ciInlineKlass* vk = CURRENT_ENV->get_klass(k)->as_inline_klass();
583       flen += vk->nof_nonstatic_fields() - 1;
584       // Iterate over fields of the flattened inline type and copy them to 'this'
585       for (int i = 0; i < vk->nof_nonstatic_fields(); ++i) {
586         ciField* flattened_field = vk->nonstatic_field_at(i);
587         // Adjust offset to account for missing oop header
588         int offset = field_offset + (flattened_field->offset() - vk->first_field_offset());
589         // A flattened field can be treated as final if the non-flattened
590         // field is declared final or the holder klass is an inline type itself.
591         bool is_final = fd.is_final() || is_inlinetype();
592         ciField* field = new (arena) ciField(flattened_field, this, offset, is_final);
593         fields->append(field);
594       }
595     } else {
596       ciField* field = new (arena) ciField(&fd);
597       fields->append(field);
598     }
599   }
600   assert(fields->length() == flen, "sanity");
601   // Now sort them by offset, ascending.
602   // (In principle, they could mix with superclass fields.)
603   fields->sort(sort_field_by_offset);
604   return fields;
605 }
606 
607 bool ciInstanceKlass::compute_injected_fields_helper() {
608   ASSERT_IN_VM;
609   InstanceKlass* k = get_instanceKlass();
610 
611   for (InternalFieldStream fs(k); !fs.done(); fs.next()) {
612     if (fs.access_flags().is_static())  continue;
613     return true;
614   }
615   return false;
616 }
617 
618 void ciInstanceKlass::compute_injected_fields() {
619   assert(is_loaded(), "must be loaded");
620 
621   int has_injected_fields = 0;
622   if (super() != NULL && super()->has_injected_fields()) {
623     has_injected_fields = 1;
624   } else {
625     GUARDED_VM_ENTRY({
626         has_injected_fields = compute_injected_fields_helper() ? 1 : 0;
627       });
628   }
629   // may be concurrently initialized for shared ciInstanceKlass objects
630   assert(_has_injected_fields == -1 || _has_injected_fields == has_injected_fields, "broken concurrent initialization");
631   _has_injected_fields = has_injected_fields;
632 }
633 
634 bool ciInstanceKlass::has_object_fields() const {
635   GUARDED_VM_ENTRY(
636       return get_instanceKlass()->nonstatic_oop_map_size() > 0;
637     );
638 }
639 
640 // ------------------------------------------------------------------
641 // ciInstanceKlass::find_method
642 //
643 // Find a method in this klass.
644 ciMethod* ciInstanceKlass::find_method(ciSymbol* name, ciSymbol* signature) {
645   VM_ENTRY_MARK;
646   InstanceKlass* k = get_instanceKlass();
647   Symbol* name_sym = name->get_symbol();
648   Symbol* sig_sym= signature->get_symbol();
649 
650   Method* m = k->find_method(name_sym, sig_sym);
651   if (m == NULL)  return NULL;
652 
653   return CURRENT_THREAD_ENV->get_method(m);
654 }
655 
656 // ------------------------------------------------------------------
657 // ciInstanceKlass::is_leaf_type
658 bool ciInstanceKlass::is_leaf_type() {
659   assert(is_loaded(), "must be loaded");
660   if (is_shared()) {
661     return is_final();  // approximately correct
662   } else {
663     return !has_subklass() && (nof_implementors() == 0);
664   }
665 }
666 
667 // ------------------------------------------------------------------
668 // ciInstanceKlass::implementor
669 //
670 // Report an implementor of this interface.
671 // Note that there are various races here, since my copy
672 // of _nof_implementors might be out of date with respect
673 // to results returned by InstanceKlass::implementor.
674 // This is OK, since any dependencies we decide to assert
675 // will be checked later under the Compile_lock.
676 ciInstanceKlass* ciInstanceKlass::implementor() {
677   ciInstanceKlass* impl = _implementor;
678   if (impl == NULL) {
679     // Go into the VM to fetch the implementor.
680     {
681       VM_ENTRY_MARK;
682       MutexLocker ml(Compile_lock);
683       Klass* k = get_instanceKlass()->implementor();
684       if (k != NULL) {
685         if (k == get_instanceKlass()) {
686           // More than one implementors. Use 'this' in this case.
687           impl = this;
688         } else {
689           impl = CURRENT_THREAD_ENV->get_instance_klass(k);
690         }
691       }
692     }
693     // Memoize this result.
694     if (!is_shared()) {
695       _implementor = impl;
696     }
697   }
698   return impl;
699 }
700 
701 bool ciInstanceKlass::can_be_inline_klass(bool is_exact) {
702   if (!EnableValhalla) {
703     return false;
704   }
705   if (!is_loaded() || is_inlinetype()) {
706     // Not loaded or known to be an inline klass
707     return true;
708   }
709   if (!is_exact) {
710     // Not exact, check if this is a valid super for an inline klass
711     VM_ENTRY_MARK;
712     return !get_instanceKlass()->invalid_inline_super();
713   }
714   return false;
715 }
716 
717 // Utility class for printing of the contents of the static fields for
718 // use by compilation replay.  It only prints out the information that
719 // could be consumed by the compiler, so for primitive types it prints
720 // out the actual value.  For Strings it's the actual string value.
721 // For array types it it's first level array size since that's the
722 // only value which statically unchangeable.  For all other reference
723 // types it simply prints out the dynamic type.
724 
725 class StaticFieldPrinter : public FieldClosure {
726 protected:
727   outputStream* _out;
728 public:
729   StaticFieldPrinter(outputStream* out) :
730     _out(out) {
731   }
732   void do_field_helper(fieldDescriptor* fd, oop obj, bool flattened);
733 };
734 
735 class StaticFinalFieldPrinter : public StaticFieldPrinter {
736   const char*   _holder;
737  public:
738   StaticFinalFieldPrinter(outputStream* out, const char* holder) :
739     StaticFieldPrinter(out), _holder(holder) {
740   }
741   void do_field(fieldDescriptor* fd) {
742     if (fd->is_final() && !fd->has_initial_value()) {
743       ResourceMark rm;
744       InstanceKlass* holder = fd->field_holder();
745       oop mirror = holder->java_mirror();
746       _out->print("staticfield %s %s ", _holder, fd->name()->as_quoted_ascii());
747       BasicType bt = fd->field_type();
748       if (bt != T_OBJECT && bt != T_ARRAY) {
749         _out->print("%s ", fd->signature()->as_quoted_ascii());
750       }
751       do_field_helper(fd, mirror, false);
752       _out->cr();
753     }
754   }
755 };
756 
757 class InlineTypeFieldPrinter : public StaticFieldPrinter {
758   oop _obj;
759 public:
760   InlineTypeFieldPrinter(outputStream* out, oop obj) :
761     StaticFieldPrinter(out), _obj(obj) {
762   }
763   void do_field(fieldDescriptor* fd) {
764     do_field_helper(fd, _obj, true);
765     _out->print(" ");
766   }
767 };
768 
769 void StaticFieldPrinter::do_field_helper(fieldDescriptor* fd, oop mirror, bool flattened) {
770   BasicType bt = fd->field_type();
771   switch (bt) {
772     case T_BYTE:    _out->print("%d", mirror->byte_field(fd->offset()));   break;
773     case T_BOOLEAN: _out->print("%d", mirror->bool_field(fd->offset()));   break;
774     case T_SHORT:   _out->print("%d", mirror->short_field(fd->offset()));  break;
775     case T_CHAR:    _out->print("%d", mirror->char_field(fd->offset()));   break;
776     case T_INT:     _out->print("%d", mirror->int_field(fd->offset()));    break;
777     case T_LONG:    _out->print(INT64_FORMAT, (int64_t)(mirror->long_field(fd->offset())));   break;
778     case T_FLOAT: {
779       float f = mirror->float_field(fd->offset());
780       _out->print("%d", *(int*)&f);
781       break;
782     }
783     case T_DOUBLE: {
784       double d = mirror->double_field(fd->offset());
785       _out->print(INT64_FORMAT, *(int64_t*)&d);
786       break;
787     }
788     case T_ARRAY:  // fall-through
789     case T_OBJECT: {
790       _out->print("%s ", fd->signature()->as_quoted_ascii());
791       oop value =  mirror->obj_field_acquire(fd->offset());
792       if (value == NULL) {
793         _out->print_cr("null");
794       } else if (value->is_instance()) {
795         assert(fd->field_type() == T_OBJECT, "");
796         if (value->is_a(vmClasses::String_klass())) {
797           const char* ascii_value = java_lang_String::as_quoted_ascii(value);
798           _out->print("\"%s\"", (ascii_value != NULL) ? ascii_value : "");
799          } else {
800           const char* klass_name  = value->klass()->name()->as_quoted_ascii();
801           _out->print("%s", klass_name);
802         }
803       } else if (value->is_array()) {
804         typeArrayOop ta = (typeArrayOop)value;
805         _out->print("%d", ta->length());
806         if (value->is_objArray() || value->is_flatArray()) {
807           objArrayOop oa = (objArrayOop)value;
808           const char* klass_name  = value->klass()->name()->as_quoted_ascii();
809           _out->print(" %s", klass_name);
810         }
811       } else {
812         ShouldNotReachHere();
813       }
814       break;
815     }
816     case T_PRIMITIVE_OBJECT: {
817       ResetNoHandleMark rnhm;
818       Thread* THREAD = Thread::current();
819       SignatureStream ss(fd->signature(), false);
820       Symbol* name = ss.as_symbol();
821       assert(!HAS_PENDING_EXCEPTION, "can resolve klass?");
822       InstanceKlass* holder = fd->field_holder();
823       InstanceKlass* k = SystemDictionary::find_instance_klass(name,
824                                                                Handle(THREAD, holder->class_loader()),
825                                                                Handle(THREAD, holder->protection_domain()));
826       assert(k != NULL && !HAS_PENDING_EXCEPTION, "can resolve klass?");
827       InlineKlass* vk = InlineKlass::cast(k);
828       oop obj;
829       if (flattened) {
830         int field_offset = fd->offset() - vk->first_field_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 != NULL) {
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 void ciInstanceKlass::dump_replay_data(outputStream* out) {
862   ResourceMark rm;
863 
864   InstanceKlass* ik = get_instanceKlass();
865   ConstantPool*  cp = ik->constants();
866 
867   // Try to record related loaded classes
868   Klass* sub = ik->subklass();
869   while (sub != NULL) {
870     if (sub->is_instance_klass()) {
871       InstanceKlass *isub = InstanceKlass::cast(sub);
872       dump_replay_instanceKlass(out, isub);
873     }
874     sub = sub->next_sibling();
875   }
876 
877   // Dump out the state of the constant pool tags.  During replay the
878   // tags will be validated for things which shouldn't change and
879   // classes will be resolved if the tags indicate that they were
880   // resolved at compile time.
881   const char *name = replay_name();
882   out->print("ciInstanceKlass %s %d %d %d", name,
883              is_linked(), is_initialized(), cp->length());
884   for (int index = 1; index < cp->length(); index++) {
885     out->print(" %d", cp->tags()->at(index));
886   }
887   out->cr();
888   if (is_initialized()) {
889     //  Dump out the static final fields in case the compilation relies
890     //  on their value for correct replay.
891     StaticFinalFieldPrinter sffp(out, name);
892     ik->do_local_static_fields(&sffp);
893   }
894 }
895 
896 #ifdef ASSERT
897 bool ciInstanceKlass::debug_final_field_at(int offset) {
898   GUARDED_VM_ENTRY(
899     InstanceKlass* ik = get_instanceKlass();
900     fieldDescriptor fd;
901     if (ik->find_field_from_offset(offset, false, &fd)) {
902       return fd.is_final();
903     }
904   );
905   return false;
906 }
907 
908 bool ciInstanceKlass::debug_stable_field_at(int offset) {
909   GUARDED_VM_ENTRY(
910     InstanceKlass* ik = get_instanceKlass();
911     fieldDescriptor fd;
912     if (ik->find_field_from_offset(offset, false, &fd)) {
913       return fd.is_stable();
914     }
915   );
916   return false;
917 }
918 #endif