1 /*
2 * Copyright (c) 2023, 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 "cds/archiveBuilder.hpp"
26 #include "oops/method.hpp"
27 #include "oops/resolvedMethodEntry.hpp"
28
29 bool ResolvedMethodEntry::check_no_old_or_obsolete_entry() {
30 // return false if m refers to a non-deleted old or obsolete method
31 if (_method != nullptr) {
32 assert(_method->is_valid() && _method->is_method(), "m is a valid method");
33 return !_method->is_old() && !_method->is_obsolete(); // old is always set for old and obsolete
34 } else {
35 return true;
36 }
37 }
38
39 void ResolvedMethodEntry::reset_entry() {
40 if (has_resolved_references_index()) {
41 u2 saved_resolved_references_index = _entry_specific._resolved_references_index;
42 u2 saved_cpool_index = _cpool_index;
43 memset(this, 0, sizeof(*this));
44 set_resolved_references_index(saved_resolved_references_index);
45 _cpool_index = saved_cpool_index;
46 } else {
47 u2 saved_cpool_index = _cpool_index;
48 memset(this, 0, sizeof(*this));
49 _cpool_index = saved_cpool_index;
50 }
51 }
52
53 #if INCLUDE_CDS
54 void ResolvedMethodEntry::remove_unshareable_info() {
55 reset_entry();
56 }
57
58 void ResolvedMethodEntry::mark_and_relocate(ConstantPool* src_cp) {
59 if (_method == nullptr) {
60 assert(bytecode2() == Bytecodes::_invokevirtual, "");
61 } else {
62 ArchiveBuilder::current()->mark_and_relocate_to_buffered_addr(&_method);
63 }
64 if (bytecode1() == Bytecodes::_invokeinterface) {
65 ArchiveBuilder::current()->mark_and_relocate_to_buffered_addr(&_entry_specific._interface_klass);
66 }
67 #if 0
68 // OLD CODE ... some of it may need to be salvaged.
69 Bytecodes::Code invoke_code = bytecode_1();
70 if (invoke_code != (Bytecodes::Code)0) {
71 Metadata* f1 = f1_ord();
72 if (f1 != nullptr) {
73 ArchiveBuilder::current()->mark_and_relocate_to_buffered_addr(&_f1);
74 switch (invoke_code) {
75 case Bytecodes::_invokeinterface:
76 assert(0, "not implemented");
77 //assert(f1->is_klass(), "");
78 //ArchiveBuilder::current()->mark_and_relocate_to_buffered_addr(&_f2); // f2 is interface method
79 return false;
80 case Bytecodes::_invokestatic:
81 // For safety, we support invokestatic only for invoking methods in MethodHandle.
82 // FIXME -- further restrict it to linkToStatic(), etc?
83 assert(bytecode_2() == (Bytecodes::Code)0, "must be");
84 assert(f1->is_method(), "");
85 assert(f1_as_method()->method_holder()->name()->equals("java/lang/invoke/MethodHandle") ||
86 f1_as_method()->method_holder()->name()->equals("java/lang/invoke/MethodHandleNatives"), "sanity");
87 return true;
88 case Bytecodes::_invokespecial:
89 assert(f1->is_method(), "must be");
90 // Also need to work on bytecode_2() below.
91 break;
92 case Bytecodes::_invokehandle:
93 assert(bytecode_2() == (Bytecodes::Code)0, "must be");
94 assert(f1->is_method(), "");
95 return true;
96 default:
97 ShouldNotReachHere();
98 break;
99 }
100 }
101 }
102
103 // TODO test case: can invokespecial and invokevirtual share the same CP?
104 invoke_code = bytecode_2();
105 if (invoke_code != (Bytecodes::Code)0) {
106 assert(invoke_code == Bytecodes::_invokevirtual, "must be");
107 if (is_vfinal()) {
108 // f2 is vfinal method
109 ArchiveBuilder::current()->mark_and_relocate_to_buffered_addr(&_f2); // f2 is final method
110 } else {
111 // f2 is vtable index, no need to mark
112 if (DynamicDumpSharedSpaces) {
113 // InstanceKlass::methods() is has been resorted, so we need to
114 // update the vtable_index.
115 int holder_index = src_cp->uncached_klass_ref_index_at(constant_pool_index());
116 Klass* src_klass = src_cp->resolved_klass_at(holder_index);
117 Method* src_m = src_klass->method_at_vtable(f2_as_index());
118 if (!ArchiveBuilder::current()->is_in_mapped_static_archive(src_m->method_holder()) &&
119 !ArchiveBuilder::current()->is_in_mapped_static_archive(src_m)) {
120 Klass* buffered_klass = ArchiveBuilder::current()->get_buffered_addr(src_klass);
121 Method* buffered_m = ArchiveBuilder::current()->get_buffered_addr(src_m);
122 int vtable_index;
123 if (src_m->method_holder()->is_interface()) { // default or miranda method
124 assert(src_m->vtable_index() < 0, "must be");
125 assert(buffered_klass->is_instance_klass(), "must be");
126 vtable_index = InstanceKlass::cast(buffered_klass)->vtable_index_of_interface_method(buffered_m);
127 assert(vtable_index >= 0, "must be");
128 } else {
129 vtable_index = buffered_m->vtable_index();
130 assert(vtable_index >= 0, "must be");
131 }
132 if (_f2 != vtable_index) {
133 log_trace(cds, resolve)("vtable_index changed %d => %d", (int)_f2, vtable_index);
134 _f2 = vtable_index;
135 }
136 }
137 }
138 }
139 }
140
141 #endif
142 }
143 #endif
144
145 void ResolvedMethodEntry::print_on(outputStream* st) const {
146 st->print_cr("Method Entry:");
147
148 if (method() != nullptr) {
149 st->print_cr(" - Method: " INTPTR_FORMAT " %s", p2i(method()), method()->external_name());
150 } else {
151 st->print_cr("- Method: null");
152 }
153 // Some fields are mutually exclusive and are only used by certain invoke codes
154 if (bytecode1() == Bytecodes::_invokeinterface && interface_klass() != nullptr) {
155 st->print_cr(" - Klass: " INTPTR_FORMAT " %s", p2i(interface_klass()), interface_klass()->external_name());
156 } else {
157 st->print_cr("- Klass: null");
158 }
159 if (bytecode1() == Bytecodes::_invokehandle) {
160 st->print_cr(" - Resolved References Index: %d", resolved_references_index());
161 } else {
162 st->print_cr(" - Resolved References Index: none");
163 }
164 if (bytecode2() == Bytecodes::_invokevirtual) {
165 #ifdef ASSERT
166 if (_has_table_index) {
167 st->print_cr(" - Table Index: %d", table_index());
168 }
169 #else
170 st->print_cr(" - Table Index: %d", table_index());
171 #endif
172 } else {
173 st->print_cr(" - Table Index: none");
174 }
175 st->print_cr(" - CP Index: %d", constant_pool_index());
176 st->print_cr(" - TOS: %s", type2name(as_BasicType((TosState)tos_state())));
177 st->print_cr(" - Number of Parameters: %d", number_of_parameters());
178 st->print_cr(" - Is Virtual Final: %d", is_vfinal());
179 st->print_cr(" - Is Final: %d", is_final());
180 st->print_cr(" - Is Forced Virtual: %d", is_forced_virtual());
181 st->print_cr(" - Has Appendix: %d", has_appendix());
182 st->print_cr(" - Has Local Signature: %d", has_local_signature());
183 st->print_cr(" - Bytecode 1: %s", Bytecodes::name((Bytecodes::Code)bytecode1()));
184 st->print_cr(" - Bytecode 2: %s", Bytecodes::name((Bytecodes::Code)bytecode2()));
185 }