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