1 /*
 2  * Copyright (c) 2023, 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 "cds/archiveBuilder.hpp"
26 #include "cppstdlib/type_traits.hpp"
27 #include "oops/instanceKlass.hpp"
28 #include "oops/instanceOop.hpp"
29 #include "oops/resolvedFieldEntry.hpp"
30 #include "runtime/fieldDescriptor.inline.hpp"
31 #include "utilities/checkedCast.hpp"
32 #include "utilities/globalDefinitions.hpp"
33 
34 static_assert(std::is_trivially_copyable_v<ResolvedFieldEntry>);
35 
36 // Detect inadvertently introduced trailing padding.
37 class ResolvedFieldEntryWithExtra : public ResolvedFieldEntry {
38   u1 _extra_field;
39 };
40 static_assert(sizeof(ResolvedFieldEntryWithExtra) > sizeof(ResolvedFieldEntry));
41 
42 void ResolvedFieldEntry::fill_in(const fieldDescriptor& info, u1 tos_state, u1 get_code, u1 put_code) {
43   set_flags(info.access_flags().is_final(), info.access_flags().is_volatile());
44   _field_holder = info.field_holder();
45   _field_offset = info.offset();
46   _field_index = checked_cast<u2>(info.index());
47   _tos_state = tos_state;
48 
49   // These must be set after the other fields
50   set_bytecode(&_get_code, get_code);
51   set_bytecode(&_put_code, put_code);
52   assert_is_valid();
53 }
54 
55 void ResolvedFieldEntry::print_on(outputStream* st) const {
56   st->print_cr("Field Entry:");
57 
58   if (field_holder() != nullptr) {
59     st->print_cr(" - Holder: " INTPTR_FORMAT " %s", p2i(field_holder()), field_holder()->external_name());
60   } else {
61     st->print_cr("- Holder: null");
62   }
63   st->print_cr(" - Offset: %d", field_offset());
64   st->print_cr(" - Field Index: %d", field_index());
65   st->print_cr(" - CP Index: %d", constant_pool_index());
66   st->print_cr(" - TOS: %s", type2name(as_BasicType((TosState)tos_state())));
67   st->print_cr(" - Is Final: %d", is_final());
68   st->print_cr(" - Is Volatile: %d", is_volatile());
69   st->print_cr(" - Get Bytecode: %s", Bytecodes::name((Bytecodes::Code)get_code()));
70   st->print_cr(" - Put Bytecode: %s", Bytecodes::name((Bytecodes::Code)put_code()));
71 }
72 
73 #ifdef ASSERT
74 void ResolvedFieldEntry::assert_is_valid() const {
75   assert(field_holder()->is_instance_klass(), "should be instanceKlass");
76   assert(field_offset() >= instanceOopDesc::base_offset_in_bytes(),
77          "field offset out of range %d >= %d", field_offset(), instanceOopDesc::base_offset_in_bytes());
78   assert(as_BasicType((TosState)tos_state()) != T_ILLEGAL, "tos_state is ILLEGAL");
79   assert(_flags < (1 << (max_flag_shift + 1)), "flags are too large %d", _flags);
80 
81   // Read each bytecode once.
82   volatile Bytecodes::Code g = (Bytecodes::Code)get_code();
83   assert(g == 0 || g == Bytecodes::_getstatic || g == Bytecodes::_getfield, "invalid get bytecode %d", g);
84 
85   volatile Bytecodes::Code p = (Bytecodes::Code)put_code();
86   assert(p == 0 || p == Bytecodes::_putstatic || p == Bytecodes::_putfield, "invalid put bytecode %d", p);
87 }
88 #endif
89 
90 #if INCLUDE_CDS
91 void ResolvedFieldEntry::remove_unshareable_info() {
92   *this = ResolvedFieldEntry(_cpool_index);
93 }
94 
95 void ResolvedFieldEntry::mark_and_relocate() {
96   ArchiveBuilder::current()->mark_and_relocate_to_buffered_addr(&_field_holder);
97 }
98 #endif