1 /*
2 * Copyright (c) 1998, 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 *
194 if (do_resolve) {
195 method_entry->set_bytecode1(invoke_code);
196 }
197 } else if (byte_no == 2) {
198 if (change_to_virtual) {
199 assert(invoke_code == Bytecodes::_invokeinterface, "");
200 // NOTE: THIS IS A HACK - BE VERY CAREFUL!!!
201 //
202 // Workaround for the case where we encounter an invokeinterface, but we
203 // should really have an _invokevirtual since the resolved method is a
204 // virtual method in java.lang.Object. This is a corner case in the spec
205 // but is presumably legal. javac does not generate this code.
206 //
207 // We do not set bytecode_1() to _invokeinterface, because that is the
208 // bytecode # used by the interpreter to see if it is resolved. In this
209 // case, the method gets reresolved with caller for each interface call
210 // because the actual selected method may not be public.
211 //
212 // We set bytecode_2() to _invokevirtual.
213 // See also interpreterRuntime.cpp. (8/25/2000)
214 } else {
215 assert(invoke_code == Bytecodes::_invokevirtual ||
216 (invoke_code == Bytecodes::_invokeinterface &&
217 ((method->is_private() ||
218 (method->is_final() && method->method_holder() == vmClasses::Object_klass())))),
219 "unexpected invocation mode");
220 if (invoke_code == Bytecodes::_invokeinterface &&
221 (method->is_private() || method->is_final())) {
222 // We set bytecode_1() to _invokeinterface, because that is the
223 // bytecode # used by the interpreter to see if it is resolved.
224 // We set bytecode_2() to _invokevirtual.
225 method_entry->set_bytecode1(invoke_code);
226 }
227 }
228 // set up for invokevirtual, even if linking for invokeinterface also:
229 method_entry->set_bytecode2(Bytecodes::_invokevirtual);
230 } else {
231 ShouldNotReachHere();
232 }
233 }
234
235 void ConstantPoolCache::set_direct_call(Bytecodes::Code invoke_code, int method_index, const methodHandle& method,
236 bool sender_is_interface) {
237 int index = Method::nonvirtual_vtable_index;
238 // index < 0; FIXME: inline and customize set_direct_or_vtable_call
239 set_direct_or_vtable_call(invoke_code, method_index, method, index, sender_is_interface);
240 }
241
242 void ConstantPoolCache::set_vtable_call(Bytecodes::Code invoke_code, int method_index, const methodHandle& method, int index) {
243 // either the method is a miranda or its holder should accept the given index
244 assert(method->method_holder()->is_interface() || method->method_holder()->verify_vtable_index(index), "");
245 // index >= 0; FIXME: inline and customize set_direct_or_vtable_call
246 set_direct_or_vtable_call(invoke_code, method_index, method, index, false);
247 }
248
249 void ConstantPoolCache::set_itable_call(Bytecodes::Code invoke_code,
259 method_entry->set_klass(static_cast<InstanceKlass*>(referenced_klass));
260 method_entry->set_method(method());
261 method_entry->fill_in((u1)as_TosState(method->result_type()), (u2)method()->size_of_parameters());
262 method_entry->set_bytecode1(Bytecodes::_invokeinterface);
263 }
264
265 ResolvedMethodEntry* ConstantPoolCache::set_method_handle(int method_index, const CallInfo &call_info) {
266 // NOTE: This method entry can be the subject of data races.
267 // There are three words to update: flags, refs[appendix_index], method (in that order).
268 // Writers must store all other values before method.
269 // Readers must test the method first for non-null before reading other fields.
270 // Competing writers must acquire exclusive access via a lock.
271 // A losing writer waits on the lock until the winner writes the method and leaves
272 // the lock, so that when the losing writer returns, he can use the linked
273 // cache entry.
274
275 // Lock fields to write
276 Bytecodes::Code invoke_code = Bytecodes::_invokehandle;
277
278 JavaThread* current = JavaThread::current();
279 objArrayHandle resolved_references(current, constant_pool()->resolved_references());
280 // Use the resolved_references() lock for this cpCache entry.
281 // resolved_references are created for all classes with Invokedynamic, MethodHandle
282 // or MethodType constant pool cache entries.
283 assert(resolved_references() != nullptr,
284 "a resolved_references array should have been created for this class");
285 ObjectLocker ol(resolved_references, current);
286
287 ResolvedMethodEntry* method_entry = resolved_method_entry_at(method_index);
288 if (method_entry->is_resolved(invoke_code)) {
289 return method_entry;
290 }
291
292 Method* adapter = call_info.resolved_method();
293 const Handle appendix = call_info.resolved_appendix();
294 const bool has_appendix = appendix.not_null();
295
296 // Write the flags.
297 // MHs are always sig-poly and have a local signature.
298 method_entry->fill_in((u1)as_TosState(adapter->result_type()), (u2)adapter->size_of_parameters());
299 method_entry->set_flags(((has_appendix ? 1 : 0) << ResolvedMethodEntry::has_appendix_shift ) |
740 }
741 ResourceMark rm(THREAD);
742 Symbol* error = PENDING_EXCEPTION->klass()->name();
743 const char* message = java_lang_Throwable::message_as_utf8(PENDING_EXCEPTION);
744
745 int encoded_index = ResolutionErrorTable::encode_indy_index(index);
746 SystemDictionary::add_resolution_error(cpool, encoded_index, error, message);
747 resolved_indy_entry_at(index)->set_resolution_failed();
748 return true;
749 }
750
751 oop ConstantPoolCache::set_dynamic_call(const CallInfo &call_info, int index) {
752 ResourceMark rm;
753
754 // Use the resolved_references() lock for this cpCache entry.
755 // resolved_references are created for all classes with Invokedynamic, MethodHandle
756 // or MethodType constant pool cache entries.
757 JavaThread* current = JavaThread::current();
758 constantPoolHandle cp(current, constant_pool());
759
760 objArrayHandle resolved_references(current, cp->resolved_references());
761 assert(resolved_references() != nullptr,
762 "a resolved_references array should have been created for this class");
763 ObjectLocker ol(resolved_references, current);
764 assert(index >= 0, "Indy index must be positive at this point");
765
766 if (resolved_indy_entry_at(index)->method() != nullptr) {
767 return cp->resolved_reference_from_indy(index);
768 }
769
770 if (resolved_indy_entry_at(index)->resolution_failed()) {
771 // Before we got here, another thread got a LinkageError exception during
772 // resolution. Ignore our success and throw their exception.
773 guarantee(index >= 0, "Invalid indy index");
774 int encoded_index = ResolutionErrorTable::encode_indy_index(index);
775 ConstantPool::throw_resolution_error(cp, encoded_index, current);
776 return nullptr;
777 }
778
779 Method* adapter = call_info.resolved_method();
780 const Handle appendix = call_info.resolved_appendix();
|
1 /*
2 * Copyright (c) 1998, 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 *
194 if (do_resolve) {
195 method_entry->set_bytecode1(invoke_code);
196 }
197 } else if (byte_no == 2) {
198 if (change_to_virtual) {
199 assert(invoke_code == Bytecodes::_invokeinterface, "");
200 // NOTE: THIS IS A HACK - BE VERY CAREFUL!!!
201 //
202 // Workaround for the case where we encounter an invokeinterface, but we
203 // should really have an _invokevirtual since the resolved method is a
204 // virtual method in java.lang.Object. This is a corner case in the spec
205 // but is presumably legal. javac does not generate this code.
206 //
207 // We do not set bytecode_1() to _invokeinterface, because that is the
208 // bytecode # used by the interpreter to see if it is resolved. In this
209 // case, the method gets reresolved with caller for each interface call
210 // because the actual selected method may not be public.
211 //
212 // We set bytecode_2() to _invokevirtual.
213 // See also interpreterRuntime.cpp. (8/25/2000)
214 invoke_code = Bytecodes::_invokevirtual;
215 } else {
216 assert(invoke_code == Bytecodes::_invokevirtual ||
217 (invoke_code == Bytecodes::_invokeinterface &&
218 ((method->is_private() ||
219 (method->is_final() && method->method_holder() == vmClasses::Object_klass())))),
220 "unexpected invocation mode");
221 if (invoke_code == Bytecodes::_invokeinterface &&
222 (method->is_private() || method->is_final())) {
223 // We set bytecode_1() to _invokeinterface, because that is the
224 // bytecode # used by the interpreter to see if it is resolved.
225 // We set bytecode_2() to _invokevirtual.
226 method_entry->set_bytecode1(invoke_code);
227 }
228 }
229 // set up for invokevirtual, even if linking for invokeinterface also:
230 method_entry->set_bytecode2(invoke_code);
231 } else {
232 ShouldNotReachHere();
233 }
234 }
235
236 void ConstantPoolCache::set_direct_call(Bytecodes::Code invoke_code, int method_index, const methodHandle& method,
237 bool sender_is_interface) {
238 int index = Method::nonvirtual_vtable_index;
239 // index < 0; FIXME: inline and customize set_direct_or_vtable_call
240 set_direct_or_vtable_call(invoke_code, method_index, method, index, sender_is_interface);
241 }
242
243 void ConstantPoolCache::set_vtable_call(Bytecodes::Code invoke_code, int method_index, const methodHandle& method, int index) {
244 // either the method is a miranda or its holder should accept the given index
245 assert(method->method_holder()->is_interface() || method->method_holder()->verify_vtable_index(index), "");
246 // index >= 0; FIXME: inline and customize set_direct_or_vtable_call
247 set_direct_or_vtable_call(invoke_code, method_index, method, index, false);
248 }
249
250 void ConstantPoolCache::set_itable_call(Bytecodes::Code invoke_code,
260 method_entry->set_klass(static_cast<InstanceKlass*>(referenced_klass));
261 method_entry->set_method(method());
262 method_entry->fill_in((u1)as_TosState(method->result_type()), (u2)method()->size_of_parameters());
263 method_entry->set_bytecode1(Bytecodes::_invokeinterface);
264 }
265
266 ResolvedMethodEntry* ConstantPoolCache::set_method_handle(int method_index, const CallInfo &call_info) {
267 // NOTE: This method entry can be the subject of data races.
268 // There are three words to update: flags, refs[appendix_index], method (in that order).
269 // Writers must store all other values before method.
270 // Readers must test the method first for non-null before reading other fields.
271 // Competing writers must acquire exclusive access via a lock.
272 // A losing writer waits on the lock until the winner writes the method and leaves
273 // the lock, so that when the losing writer returns, he can use the linked
274 // cache entry.
275
276 // Lock fields to write
277 Bytecodes::Code invoke_code = Bytecodes::_invokehandle;
278
279 JavaThread* current = JavaThread::current();
280 refArrayHandle resolved_references(current, constant_pool()->resolved_references());
281 // Use the resolved_references() lock for this cpCache entry.
282 // resolved_references are created for all classes with Invokedynamic, MethodHandle
283 // or MethodType constant pool cache entries.
284 assert(resolved_references() != nullptr,
285 "a resolved_references array should have been created for this class");
286 ObjectLocker ol(resolved_references, current);
287
288 ResolvedMethodEntry* method_entry = resolved_method_entry_at(method_index);
289 if (method_entry->is_resolved(invoke_code)) {
290 return method_entry;
291 }
292
293 Method* adapter = call_info.resolved_method();
294 const Handle appendix = call_info.resolved_appendix();
295 const bool has_appendix = appendix.not_null();
296
297 // Write the flags.
298 // MHs are always sig-poly and have a local signature.
299 method_entry->fill_in((u1)as_TosState(adapter->result_type()), (u2)adapter->size_of_parameters());
300 method_entry->set_flags(((has_appendix ? 1 : 0) << ResolvedMethodEntry::has_appendix_shift ) |
741 }
742 ResourceMark rm(THREAD);
743 Symbol* error = PENDING_EXCEPTION->klass()->name();
744 const char* message = java_lang_Throwable::message_as_utf8(PENDING_EXCEPTION);
745
746 int encoded_index = ResolutionErrorTable::encode_indy_index(index);
747 SystemDictionary::add_resolution_error(cpool, encoded_index, error, message);
748 resolved_indy_entry_at(index)->set_resolution_failed();
749 return true;
750 }
751
752 oop ConstantPoolCache::set_dynamic_call(const CallInfo &call_info, int index) {
753 ResourceMark rm;
754
755 // Use the resolved_references() lock for this cpCache entry.
756 // resolved_references are created for all classes with Invokedynamic, MethodHandle
757 // or MethodType constant pool cache entries.
758 JavaThread* current = JavaThread::current();
759 constantPoolHandle cp(current, constant_pool());
760
761 refArrayHandle resolved_references(current, cp->resolved_references());
762 assert(resolved_references() != nullptr,
763 "a resolved_references array should have been created for this class");
764 ObjectLocker ol(resolved_references, current);
765 assert(index >= 0, "Indy index must be positive at this point");
766
767 if (resolved_indy_entry_at(index)->method() != nullptr) {
768 return cp->resolved_reference_from_indy(index);
769 }
770
771 if (resolved_indy_entry_at(index)->resolution_failed()) {
772 // Before we got here, another thread got a LinkageError exception during
773 // resolution. Ignore our success and throw their exception.
774 guarantee(index >= 0, "Invalid indy index");
775 int encoded_index = ResolutionErrorTable::encode_indy_index(index);
776 ConstantPool::throw_resolution_error(cp, encoded_index, current);
777 return nullptr;
778 }
779
780 Method* adapter = call_info.resolved_method();
781 const Handle appendix = call_info.resolved_appendix();
|