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