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