1 /*
  2  * Copyright (c) 1997, 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 "classfile/vmSymbols.hpp"
 26 #include "memory/resourceArea.hpp"
 27 #include "oops/annotations.hpp"
 28 #include "oops/constantPool.hpp"
 29 #include "oops/fieldStreams.inline.hpp"
 30 #include "oops/inlineKlass.inline.hpp"
 31 #include "oops/instanceKlass.hpp"
 32 #include "oops/klass.inline.hpp"
 33 #include "oops/oop.inline.hpp"
 34 #include "runtime/fieldDescriptor.inline.hpp"
 35 #include "runtime/handles.inline.hpp"
 36 #include "runtime/signature.hpp"
 37 
 38 Symbol* fieldDescriptor::generic_signature() const {
 39   if (!has_generic_signature()) {
 40     return nullptr;
 41   }
 42   return _cp->symbol_at(_fieldinfo.generic_signature_index());
 43 }
 44 
 45 bool fieldDescriptor::is_trusted_final() const {
 46   InstanceKlass* ik = field_holder();
 47   return is_final() && (is_static() || ik->is_hidden() || ik->is_record() || ik->is_inline_klass()
 48                         || (ik->is_abstract() && !ik->is_identity_class() && !ik->is_interface()));
 49 }
 50 
 51 bool fieldDescriptor::is_mutable_static_final() const {
 52   InstanceKlass* ik = field_holder();
 53   // write protected fields (JLS 17.5.4)
 54   if (is_final() && is_static() && ik == vmClasses::System_klass() &&
 55       (offset() == java_lang_System::in_offset() || offset() == java_lang_System::out_offset() || offset() == java_lang_System::err_offset())) {
 56    return true;
 57   }
 58   return false;
 59 }
 60 
 61 AnnotationArray* fieldDescriptor::annotations() const {
 62   InstanceKlass* ik = field_holder();
 63   Array<AnnotationArray*>* md = ik->fields_annotations();
 64   if (md == nullptr)
 65     return nullptr;
 66   return md->at(index());
 67 }
 68 
 69 AnnotationArray* fieldDescriptor::type_annotations() const {
 70   InstanceKlass* ik = field_holder();
 71   Array<AnnotationArray*>* type_annos = ik->fields_type_annotations();
 72   if (type_annos == nullptr)
 73     return nullptr;
 74   return type_annos->at(index());
 75 }
 76 
 77 constantTag fieldDescriptor::initial_value_tag() const {
 78   return constants()->tag_at(initial_value_index());
 79 }
 80 
 81 jint fieldDescriptor::int_initial_value() const {
 82   return constants()->int_at(initial_value_index());
 83 }
 84 
 85 jlong fieldDescriptor::long_initial_value() const {
 86   return constants()->long_at(initial_value_index());
 87 }
 88 
 89 jfloat fieldDescriptor::float_initial_value() const {
 90   return constants()->float_at(initial_value_index());
 91 }
 92 
 93 jdouble fieldDescriptor::double_initial_value() const {
 94   return constants()->double_at(initial_value_index());
 95 }
 96 
 97 oop fieldDescriptor::string_initial_value(TRAPS) const {
 98   return constants()->uncached_string_at(initial_value_index(), THREAD);
 99 }
100 
101 void fieldDescriptor::reinitialize(InstanceKlass* ik, const FieldInfo& fieldinfo) {
102   if (_cp.is_null() || field_holder() != ik) {
103     _cp = constantPoolHandle(Thread::current(), ik->constants());
104     // _cp should now reference ik's constant pool; i.e., ik is now field_holder.
105     // If the class is a scratch class, the constant pool points to the original class,
106     // but that's ok because of constant pool merging.
107     assert(field_holder() == ik || ik->is_scratch_class(), "must be already initialized to this class");
108   }
109   _fieldinfo = fieldinfo;
110   guarantee(_fieldinfo.name_index() != 0 && _fieldinfo.signature_index() != 0, "bad constant pool index for fieldDescriptor");
111 }
112 
113 void fieldDescriptor::print_on(outputStream* st, int base_offset) const {
114   access_flags().print_on(st);
115   if (field_flags().is_injected()) st->print("injected ");
116   name()->print_value_on(st);
117   st->print(" ");
118   signature()->print_value_on(st);
119   st->print(" @%d ", offset() + base_offset);
120   if (WizardMode && has_initial_value()) {
121     st->print("(initval ");
122     constantTag t = initial_value_tag();
123     if (t.is_int()) {
124       st->print("int %d)", int_initial_value());
125     } else if (t.is_long()){
126       st->print_jlong(long_initial_value());
127     } else if (t.is_float()){
128       st->print("float %f)", float_initial_value());
129     } else if (t.is_double()){
130       st->print("double %lf)", double_initial_value());
131     }
132   }
133 }
134 
135 void fieldDescriptor::print() const { print_on(tty); }
136 
137 void fieldDescriptor::print_on_for(outputStream* st, oop obj, int indent, int base_offset) {
138   BasicType ft = field_type();
139   print_on(st, base_offset);
140   st->print(" ");
141   jint as_int = 0;
142   switch (ft) {
143     case T_BYTE:
144       st->print("%d", obj->byte_field(offset()));
145       break;
146     case T_CHAR:
147       {
148         jchar c = obj->char_field(offset());
149         st->print("%c %d", isprint(c) ? c : ' ', c);
150       }
151       break;
152     case T_DOUBLE:
153       st->print("%lf", obj->double_field(offset()));
154       break;
155     case T_FLOAT:
156       st->print("%f", obj->float_field(offset()));
157       break;
158     case T_INT:
159       st->print("%d", obj->int_field(offset()));
160       break;
161     case T_LONG:
162       st->print_jlong(obj->long_field(offset()));
163       break;
164     case T_SHORT:
165       st->print("%d", obj->short_field(offset()));
166       break;
167     case T_BOOLEAN:
168       st->print("%s", obj->bool_field(offset()) ? "true" : "false");
169       break;
170     case T_ARRAY:
171     case T_OBJECT:
172       if (is_flat()) { // only some inline types can be flat
173         InlineKlass* vk = InlineKlass::cast(field_holder()->get_inline_type_field_klass(index()));
174         st->print("Flat inline type field '%s':", vk->name()->as_C_string());
175         if (!is_null_free_inline_type()) {
176           assert(has_null_marker(), "should have null marker");
177           InlineLayoutInfo* li = field_holder()->inline_layout_info_adr(index());
178           int nm_offset = li->null_marker_offset();
179           if (obj->byte_field_acquire(nm_offset) == 0) {
180             st->print(" null");
181             return;
182           }
183         }
184         st->cr();
185         // Print fields of flat field (recursively)
186         int field_offset = offset() - vk->payload_offset();
187         obj = cast_to_oop(cast_from_oop<address>(obj) + field_offset);
188         FieldPrinter print_field(st, obj, indent + 1, base_offset + field_offset );
189         vk->do_nonstatic_fields(&print_field);
190         if (this->field_flags().has_null_marker()) {
191           for (int i = 0; i < indent + 1; i++) st->print("  ");
192           st->print_cr(" - [null_marker] @%d %s",
193                     vk->null_marker_offset() - base_offset + field_offset,
194                     obj->bool_field(vk->null_marker_offset()) ? "Field marked as non-null" : "Field marked as null");
195         }
196         return; // Do not print underlying representation
197       }
198       // Not flat inline type field, fall through
199       if (obj->obj_field(offset()) != nullptr) {
200         obj->obj_field(offset())->print_value_on(st);
201       } else {
202         st->print("null");
203       }
204       break;
205     default:
206       ShouldNotReachHere();
207       break;
208   }
209 
210   // Print a hint as to the underlying integer representation.
211   if (is_reference_type(ft)) {
212 #ifdef _LP64
213     if (UseCompressedOops) {
214       st->print(" (" INT32_FORMAT_X_0 ")", obj->int_field(offset()));
215     } else {
216       st->print(" (" INT64_FORMAT_X_0 ")", (int64_t)obj->long_field(offset()));
217     }
218 #else
219     st->print(" (" INT32_FORMAT_X_0 ")", obj->int_field(offset()));
220 #endif
221   } else { // Primitives
222     switch (ft) {
223       case T_LONG:    st->print(" (" INT64_FORMAT_X_0 ")", (int64_t)obj->long_field(offset())); break;
224       case T_DOUBLE:  st->print(" (" INT64_FORMAT_X_0 ")", (int64_t)obj->long_field(offset())); break;
225       case T_BYTE:    st->print(" (" INT8_FORMAT_X_0  ")", obj->byte_field(offset()));          break;
226       case T_CHAR:    st->print(" (" INT16_FORMAT_X_0 ")", obj->char_field(offset()));          break;
227       case T_FLOAT:   st->print(" (" INT32_FORMAT_X_0 ")", obj->int_field(offset()));           break;
228       case T_INT:     st->print(" (" INT32_FORMAT_X_0 ")", obj->int_field(offset()));           break;
229       case T_SHORT:   st->print(" (" INT16_FORMAT_X_0 ")", obj->short_field(offset()));         break;
230       case T_BOOLEAN: st->print(" (" INT8_FORMAT_X_0  ")", obj->bool_field(offset()));          break;
231     default:
232       ShouldNotReachHere();
233       break;
234     }
235   }
236 }