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 "asm/macroAssembler.hpp"
26 #include "classfile/vmClasses.hpp"
27 #include "compiler/disassembler.hpp"
28 #include "classfile/javaClasses.inline.hpp"
29 #include "interpreter/interpreter.hpp"
30 #include "interpreter/interpreterRuntime.hpp"
31 #include "jvm.h"
32 #include "logging/log.hpp"
33 #include "logging/logStream.hpp"
34 #include "memory/allocation.inline.hpp"
35 #include "memory/resourceArea.hpp"
36 #include "prims/jvmtiExport.hpp"
37 #include "prims/methodHandles.hpp"
38 #include "runtime/flags/flagSetting.hpp"
39 #include "runtime/frame.inline.hpp"
40 #include "runtime/stubRoutines.hpp"
41 #include "utilities/formatBuffer.hpp"
42 #include "utilities/preserveException.hpp"
43
44 #define __ Disassembler::hook<MacroAssembler>(__FILE__, __LINE__, _masm)->
45
46 #ifdef PRODUCT
47 #define BLOCK_COMMENT(str) /* nothing */
48 #define STOP(error) stop(error)
49 #else
50 #define BLOCK_COMMENT(str) __ block_comment(str)
51 #define STOP(error) block_comment(error); __ stop(error)
52 #endif
53
54 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
55
56 void MethodHandles::load_klass_from_Class(MacroAssembler* _masm, Register klass_reg) {
57 if (VerifyMethodHandles)
58 verify_klass(_masm, klass_reg, VM_CLASS_ID(java_lang_Class),
59 "MH argument is a Class");
60 __ movptr(klass_reg, Address(klass_reg, java_lang_Class::klass_offset()));
61 }
62
63 #ifdef ASSERT
64 static int check_nonzero(const char* xname, int x) {
65 assert(x != 0, "%s should be nonzero", xname);
66 return x;
67 }
68 #define NONZERO(x) check_nonzero(#x, x)
69 #else //ASSERT
70 #define NONZERO(x) (x)
71 #endif //ASSERT
72
73 #ifdef ASSERT
74 void MethodHandles::verify_klass(MacroAssembler* _masm,
75 Register obj, vmClassID klass_id,
76 const char* error_message) {
77 InstanceKlass** klass_addr = vmClasses::klass_addr_at(klass_id);
78 Klass* klass = vmClasses::klass_at(klass_id);
79 Register temp = rdi;
80 Label L_ok, L_bad;
81 BLOCK_COMMENT("verify_klass {");
82 __ verify_oop(obj);
83 __ testptr(obj, obj);
84 __ jcc(Assembler::zero, L_bad);
85 #define PUSH { __ push(temp); __ push(rscratch1); }
86 #define POP { __ pop(rscratch1); __ pop(temp); }
87 PUSH;
88 __ load_klass(temp, obj, rscratch1);
89 __ cmpptr(temp, ExternalAddress((address) klass_addr), rscratch1);
90 __ jcc(Assembler::equal, L_ok);
91 intptr_t super_check_offset = klass->super_check_offset();
92 __ movptr(temp, Address(temp, super_check_offset));
93 __ cmpptr(temp, ExternalAddress((address) klass_addr), rscratch1);
94 __ jcc(Assembler::equal, L_ok);
95 POP;
96 __ bind(L_bad);
97 __ STOP(error_message);
98 __ BIND(L_ok);
99 POP;
100 BLOCK_COMMENT("} verify_klass");
101 #undef POP
102 #undef PUSH
103 }
104
105 void MethodHandles::verify_ref_kind(MacroAssembler* _masm, int ref_kind, Register member_reg, Register temp) {
106 Label L;
107 BLOCK_COMMENT("verify_ref_kind {");
108 __ movl(temp, Address(member_reg, NONZERO(java_lang_invoke_MemberName::flags_offset())));
109 __ shrl(temp, java_lang_invoke_MemberName::MN_REFERENCE_KIND_SHIFT);
110 __ andl(temp, java_lang_invoke_MemberName::MN_REFERENCE_KIND_MASK);
111 __ cmpl(temp, ref_kind);
112 __ jcc(Assembler::equal, L);
113 const char* msg = ref_kind_to_verify_msg(ref_kind);
114 if (ref_kind == JVM_REF_invokeVirtual ||
115 ref_kind == JVM_REF_invokeSpecial) {
116 // could do this for all ref_kinds, but would explode assembly code size
117 trace_method_handle(_masm, msg);
118 }
119 __ STOP(msg);
120 BLOCK_COMMENT("} verify_ref_kind");
121 __ bind(L);
122 }
123
124 void MethodHandles::verify_method(MacroAssembler* _masm, Register method, Register temp, vmIntrinsics::ID iid) {
125 BLOCK_COMMENT("verify_method {");
126 __ verify_method_ptr(method);
127 if (VerifyMethodHandles) {
128 Label L_ok;
129 assert_different_registers(method, temp);
130
131 const Register method_holder = temp;
132 __ load_method_holder(method_holder, method);
133 __ push_ppx(method_holder); // keep holder around for diagnostic purposes
134
135 switch (iid) {
136 case vmIntrinsicID::_invokeBasic:
137 // Require compiled LambdaForm class to be fully initialized.
138 __ cmpb(Address(method_holder, InstanceKlass::init_state_offset()), InstanceKlass::fully_initialized);
139 __ jcc(Assembler::equal, L_ok);
140 break;
141
142 case vmIntrinsicID::_linkToStatic:
143 __ clinit_barrier(method_holder, &L_ok);
144 break;
145
146 case vmIntrinsicID::_linkToVirtual:
147 case vmIntrinsicID::_linkToSpecial:
148 case vmIntrinsicID::_linkToInterface:
149 // Class initialization check is too strong here. Just ensure that initialization has been initiated.
150 __ cmpb(Address(method_holder, InstanceKlass::init_state_offset()), InstanceKlass::being_initialized);
151 __ jcc(Assembler::greaterEqual, L_ok);
152
153 // init_state check failed, but it may be an abstract interface method
154 __ load_unsigned_short(temp, Address(method, Method::access_flags_offset()));
155 __ testl(temp, JVM_ACC_ABSTRACT);
156 __ jcc(Assembler::notZero, L_ok);
157 break;
158
159 default:
160 fatal("unexpected intrinsic %d: %s", vmIntrinsics::as_int(iid), vmIntrinsics::name_at(iid));
161 }
162
163 // clinit check failed for a concrete method
164 __ STOP("Method holder klass is not initialized");
165
166 __ BIND(L_ok);
167 __ pop_ppx(method_holder); // restore stack layout
168 }
169 BLOCK_COMMENT("} verify_method");
170 }
171 #endif // ASSERT
172
173 void MethodHandles::jump_from_method_handle(MacroAssembler* _masm, Register method, Register temp,
174 bool for_compiler_entry, vmIntrinsics::ID iid) {
175 assert(method == rbx, "interpreter calling convention");
176
177 Label L_no_such_method;
178 __ testptr(rbx, rbx);
179 __ jcc(Assembler::zero, L_no_such_method);
180
181 verify_method(_masm, method, temp, iid);
182
183 if (!for_compiler_entry && JvmtiExport::can_post_interpreter_events()) {
184 Label run_compiled_code;
185 // JVMTI events, such as single-stepping, are implemented partly by avoiding running
186 // compiled code in threads for which the event is enabled. Check here for
187 // interp_only_mode if these events CAN be enabled.
188 // interp_only is an int, on little endian it is sufficient to test the byte only
189 // Is a cmpl faster?
190 __ cmpb(Address(r15_thread, JavaThread::interp_only_mode_offset()), 0);
191 __ jccb(Assembler::zero, run_compiled_code);
192 __ jmp(Address(method, Method::interpreter_entry_offset()));
193 __ BIND(run_compiled_code);
194 }
195
196 const ByteSize entry_offset = for_compiler_entry ? Method::from_compiled_offset() :
197 Method::from_interpreted_offset();
198 __ jmp(Address(method, entry_offset));
199
200 __ bind(L_no_such_method);
201 __ jump(RuntimeAddress(SharedRuntime::throw_AbstractMethodError_entry()));
202 }
203
204 void MethodHandles::jump_to_lambda_form(MacroAssembler* _masm,
205 Register recv, Register method_temp,
206 Register temp2,
207 bool for_compiler_entry) {
208 BLOCK_COMMENT("jump_to_lambda_form {");
209 // This is the initial entry point of a lazy method handle.
210 // After type checking, it picks up the invoker from the LambdaForm.
211 assert_different_registers(recv, method_temp, temp2);
212 assert(recv != noreg, "required register");
213 assert(method_temp == rbx, "required register for loading method");
214
215 // Load the invoker, as MH -> MH.form -> LF.vmentry
216 __ verify_oop(recv);
217 __ load_heap_oop(method_temp, Address(recv, NONZERO(java_lang_invoke_MethodHandle::form_offset())), temp2);
218 __ verify_oop(method_temp);
219 __ load_heap_oop(method_temp, Address(method_temp, NONZERO(java_lang_invoke_LambdaForm::vmentry_offset())), temp2);
220 __ verify_oop(method_temp);
221 __ load_heap_oop(method_temp, Address(method_temp, NONZERO(java_lang_invoke_MemberName::method_offset())), temp2);
222 __ verify_oop(method_temp);
223 __ access_load_at(T_ADDRESS, IN_HEAP, method_temp,
224 Address(method_temp, NONZERO(java_lang_invoke_ResolvedMethodName::vmtarget_offset())),
225 noreg);
226
227 if (VerifyMethodHandles && !for_compiler_entry) {
228 // make sure recv is already on stack
229 __ movptr(temp2, Address(method_temp, Method::const_offset()));
230 __ load_sized_value(temp2,
231 Address(temp2, ConstMethod::size_of_parameters_offset()),
232 sizeof(u2), /*is_signed*/ false);
233 // assert(sizeof(u2) == sizeof(Method::_size_of_parameters), "");
234 Label L;
235 __ cmpoop(recv, __ argument_address(temp2, -1));
236 __ jcc(Assembler::equal, L);
237 __ movptr(rax, __ argument_address(temp2, -1));
238 __ STOP("receiver not on stack");
239 __ BIND(L);
240 }
241
242 jump_from_method_handle(_masm, method_temp, temp2, for_compiler_entry, vmIntrinsics::_invokeBasic);
243 BLOCK_COMMENT("} jump_to_lambda_form");
244 }
245
246 void MethodHandles::jump_to_native_invoker(MacroAssembler* _masm, Register nep_reg, Register temp_target) {
247 BLOCK_COMMENT("jump_to_native_invoker {");
248 assert_different_registers(nep_reg, temp_target);
249 assert(nep_reg != noreg, "required register");
250
251 // Load the invoker, as NEP -> .invoker
252 __ verify_oop(nep_reg);
253 __ access_load_at(T_ADDRESS, IN_HEAP, temp_target,
254 Address(nep_reg, NONZERO(jdk_internal_foreign_abi_NativeEntryPoint::downcall_stub_address_offset_in_bytes())),
255 noreg);
256
257 __ jmp(temp_target);
258 BLOCK_COMMENT("} jump_to_native_invoker");
259 }
260
261
262 // Code generation
263 address MethodHandles::generate_method_handle_interpreter_entry(MacroAssembler* _masm,
264 vmIntrinsics::ID iid) {
265 const bool not_for_compiler_entry = false; // this is the interpreter entry
266 assert(is_signature_polymorphic(iid), "expected invoke iid");
267 if (iid == vmIntrinsics::_invokeGeneric ||
268 iid == vmIntrinsics::_compiledLambdaForm) {
269 // Perhaps surprisingly, the symbolic references visible to Java are not directly used.
270 // They are linked to Java-generated adapters via MethodHandleNatives.linkMethod.
271 // They all allow an appendix argument.
272 __ hlt(); // empty stubs make SG sick
273 return nullptr;
274 }
275
276 // No need in interpreter entry for linkToNative for now.
277 // Interpreter calls compiled entry through i2c.
278 if (iid == vmIntrinsics::_linkToNative) {
279 __ hlt();
280 return nullptr;
281 }
282
283 // rsi/r13: sender SP (must preserve; see prepare_to_jump_from_interpreted)
284 // rbx: Method*
285 // rdx: argument locator (parameter slot count, added to rsp)
286 // rcx: used as temp to hold mh or receiver
287 // rax, rdi: garbage temps, blown away
288 Register rdx_argp = rdx; // argument list ptr, live on error paths
289 Register rax_temp = rax;
290 Register rcx_mh = rcx; // MH receiver; dies quickly and is recycled
291 Register rbx_method = rbx; // eventual target of this invocation
292
293 // here's where control starts out:
294 __ align(CodeEntryAlignment);
295 address entry_point = __ pc();
296
297 if (VerifyMethodHandles) {
298 assert(Method::intrinsic_id_size_in_bytes() == 2, "assuming Method::_intrinsic_id is u2");
299
300 Label L;
301 BLOCK_COMMENT("verify_intrinsic_id {");
302 __ cmpw(Address(rbx_method, Method::intrinsic_id_offset()), (int) iid);
303 __ jcc(Assembler::equal, L);
304 if (iid == vmIntrinsics::_linkToVirtual ||
305 iid == vmIntrinsics::_linkToSpecial) {
306 // could do this for all kinds, but would explode assembly code size
307 trace_method_handle(_masm, "bad Method*::intrinsic_id");
308 }
309 __ STOP("bad Method*::intrinsic_id");
310 __ bind(L);
311 BLOCK_COMMENT("} verify_intrinsic_id");
312 }
313
314 // First task: Find out how big the argument list is.
315 Address rdx_first_arg_addr;
316 int ref_kind = signature_polymorphic_intrinsic_ref_kind(iid);
317 assert(ref_kind != 0 || iid == vmIntrinsics::_invokeBasic, "must be _invokeBasic or a linkTo intrinsic");
318 if (ref_kind == 0 || MethodHandles::ref_kind_has_receiver(ref_kind)) {
319 __ movptr(rdx_argp, Address(rbx_method, Method::const_offset()));
320 __ load_sized_value(rdx_argp,
321 Address(rdx_argp, ConstMethod::size_of_parameters_offset()),
322 sizeof(u2), /*is_signed*/ false);
323 // assert(sizeof(u2) == sizeof(Method::_size_of_parameters), "");
324 rdx_first_arg_addr = __ argument_address(rdx_argp, -1);
325 } else {
326 DEBUG_ONLY(rdx_argp = noreg);
327 }
328
329 if (!is_signature_polymorphic_static(iid)) {
330 __ movptr(rcx_mh, rdx_first_arg_addr);
331 DEBUG_ONLY(rdx_argp = noreg);
332 }
333
334 // rdx_first_arg_addr is live!
335
336 trace_method_handle_interpreter_entry(_masm, iid);
337
338 if (iid == vmIntrinsics::_invokeBasic) {
339 generate_method_handle_dispatch(_masm, iid, rcx_mh, noreg, not_for_compiler_entry);
340
341 } else {
342 // Adjust argument list by popping the trailing MemberName argument.
343 Register rcx_recv = noreg;
344 if (MethodHandles::ref_kind_has_receiver(ref_kind)) {
345 // Load the receiver (not the MH; the actual MemberName's receiver) up from the interpreter stack.
346 __ movptr(rcx_recv = rcx, rdx_first_arg_addr);
347 }
348 DEBUG_ONLY(rdx_argp = noreg);
349 Register rbx_member = rbx_method; // MemberName ptr; incoming method ptr is dead now
350 __ pop(rax_temp); // return address
351 __ pop(rbx_member); // extract last argument
352 __ push(rax_temp); // re-push return address
353 generate_method_handle_dispatch(_masm, iid, rcx_recv, rbx_member, not_for_compiler_entry);
354 }
355
356 return entry_point;
357 }
358
359 void MethodHandles::generate_method_handle_dispatch(MacroAssembler* _masm,
360 vmIntrinsics::ID iid,
361 Register receiver_reg,
362 Register member_reg,
363 bool for_compiler_entry) {
364 assert(is_signature_polymorphic(iid), "expected invoke iid");
365 Register rbx_method = rbx; // eventual target of this invocation
366 // temps used in this code are not used in *either* compiled or interpreted calling sequences
367 Register temp1 = rscratch1;
368 Register temp2 = rscratch2;
369 Register temp3 = rax;
370 if (for_compiler_entry) {
371 assert(receiver_reg == (iid == vmIntrinsics::_linkToStatic || iid == vmIntrinsics::_linkToNative ? noreg : j_rarg0), "only valid assignment");
372 assert_different_registers(temp1, j_rarg0, j_rarg1, j_rarg2, j_rarg3, j_rarg4, j_rarg5);
373 assert_different_registers(temp2, j_rarg0, j_rarg1, j_rarg2, j_rarg3, j_rarg4, j_rarg5);
374 assert_different_registers(temp3, j_rarg0, j_rarg1, j_rarg2, j_rarg3, j_rarg4, j_rarg5);
375 } else {
376 assert_different_registers(temp1, temp2, temp3, saved_last_sp_register()); // don't trash lastSP
377 }
378 assert_different_registers(temp1, temp2, temp3, receiver_reg);
379 assert_different_registers(temp1, temp2, temp3, member_reg);
380
381 if (iid == vmIntrinsics::_invokeBasic) {
382 // indirect through MH.form.vmentry.vmtarget
383 jump_to_lambda_form(_masm, receiver_reg, rbx_method, temp1, for_compiler_entry);
384 } else if (iid == vmIntrinsics::_linkToNative) {
385 assert(for_compiler_entry, "only compiler entry is supported");
386 jump_to_native_invoker(_masm, member_reg, temp1);
387 } else {
388 // The method is a member invoker used by direct method handles.
389 if (VerifyMethodHandles) {
390 // make sure the trailing argument really is a MemberName (caller responsibility)
391 verify_klass(_masm, member_reg, VM_CLASS_ID(java_lang_invoke_MemberName),
392 "MemberName required for invokeVirtual etc.");
393 }
394
395 Address member_clazz( member_reg, NONZERO(java_lang_invoke_MemberName::clazz_offset()));
396 Address member_vmindex( member_reg, NONZERO(java_lang_invoke_MemberName::vmindex_offset()));
397 Address member_vmtarget( member_reg, NONZERO(java_lang_invoke_MemberName::method_offset()));
398 Address vmtarget_method( rbx_method, NONZERO(java_lang_invoke_ResolvedMethodName::vmtarget_offset()));
399
400 Register temp1_recv_klass = temp1;
401 if (iid != vmIntrinsics::_linkToStatic) {
402 __ verify_oop(receiver_reg);
403 if (iid == vmIntrinsics::_linkToSpecial) {
404 // Don't actually load the klass; just null-check the receiver.
405 __ null_check(receiver_reg);
406 } else {
407 // load receiver klass itself
408 __ load_klass(temp1_recv_klass, receiver_reg, temp2);
409 __ verify_klass_ptr(temp1_recv_klass);
410 }
411 BLOCK_COMMENT("check_receiver {");
412 // The receiver for the MemberName must be in receiver_reg.
413 // Check the receiver against the MemberName.clazz
414 if (VerifyMethodHandles && iid == vmIntrinsics::_linkToSpecial) {
415 // Did not load it above...
416 __ load_klass(temp1_recv_klass, receiver_reg, temp2);
417 __ verify_klass_ptr(temp1_recv_klass);
418 }
419 if (VerifyMethodHandles && iid != vmIntrinsics::_linkToInterface) {
420 Label L_ok;
421 Register temp2_defc = temp2;
422 __ load_heap_oop(temp2_defc, member_clazz, temp3);
423 load_klass_from_Class(_masm, temp2_defc);
424 __ verify_klass_ptr(temp2_defc);
425 __ check_klass_subtype(temp1_recv_klass, temp2_defc, temp3, L_ok);
426 // If we get here, the type check failed!
427 __ STOP("receiver class disagrees with MemberName.clazz");
428 __ bind(L_ok);
429 }
430 BLOCK_COMMENT("} check_receiver");
431 }
432 if (iid == vmIntrinsics::_linkToSpecial ||
433 iid == vmIntrinsics::_linkToStatic) {
434 DEBUG_ONLY(temp1_recv_klass = noreg); // these guys didn't load the recv_klass
435 }
436
437 // Live registers at this point:
438 // member_reg - MemberName that was the trailing argument
439 // temp1_recv_klass - klass of stacked receiver, if needed
440 // rsi/r13 - interpreter linkage (if interpreted)
441 // rcx, rdx, rsi, rdi, r8 - compiler arguments (if compiled)
442
443 Label L_incompatible_class_change_error;
444 switch (iid) {
445 case vmIntrinsics::_linkToSpecial:
446 if (VerifyMethodHandles) {
447 verify_ref_kind(_masm, JVM_REF_invokeSpecial, member_reg, temp3);
448 }
449 __ load_heap_oop(rbx_method, member_vmtarget);
450 __ access_load_at(T_ADDRESS, IN_HEAP, rbx_method, vmtarget_method, noreg);
451 break;
452
453 case vmIntrinsics::_linkToStatic:
454 if (VerifyMethodHandles) {
455 verify_ref_kind(_masm, JVM_REF_invokeStatic, member_reg, temp3);
456 }
457 __ load_heap_oop(rbx_method, member_vmtarget);
458 __ access_load_at(T_ADDRESS, IN_HEAP, rbx_method, vmtarget_method, noreg);
459 break;
460
461 case vmIntrinsics::_linkToVirtual:
462 {
463 // same as TemplateTable::invokevirtual,
464 // minus the CP setup and profiling:
465
466 if (VerifyMethodHandles) {
467 verify_ref_kind(_masm, JVM_REF_invokeVirtual, member_reg, temp3);
468 }
469
470 // pick out the vtable index from the MemberName, and then we can discard it:
471 Register temp2_index = temp2;
472 __ access_load_at(T_ADDRESS, IN_HEAP, temp2_index, member_vmindex, noreg);
473
474 if (VerifyMethodHandles) {
475 Label L_index_ok;
476 __ cmpl(temp2_index, 0);
477 __ jcc(Assembler::greaterEqual, L_index_ok);
478 __ STOP("no virtual index");
479 __ BIND(L_index_ok);
480 }
481
482 // Note: The verifier invariants allow us to ignore MemberName.clazz and vmtarget
483 // at this point. And VerifyMethodHandles has already checked clazz, if needed.
484
485 // get target Method* & entry point
486 __ lookup_virtual_method(temp1_recv_klass, temp2_index, rbx_method);
487 break;
488 }
489
490 case vmIntrinsics::_linkToInterface:
491 {
492 // same as TemplateTable::invokeinterface
493 // (minus the CP setup and profiling, with different argument motion)
494 if (VerifyMethodHandles) {
495 verify_ref_kind(_masm, JVM_REF_invokeInterface, member_reg, temp3);
496 }
497
498 Register temp3_intf = temp3;
499 __ load_heap_oop(temp3_intf, member_clazz);
500 load_klass_from_Class(_masm, temp3_intf);
501 __ verify_klass_ptr(temp3_intf);
502
503 Register rbx_index = rbx_method;
504 __ access_load_at(T_ADDRESS, IN_HEAP, rbx_index, member_vmindex, noreg);
505 if (VerifyMethodHandles) {
506 Label L;
507 __ cmpl(rbx_index, 0);
508 __ jcc(Assembler::greaterEqual, L);
509 __ STOP("invalid vtable index for MH.invokeInterface");
510 __ bind(L);
511 }
512
513 // given intf, index, and recv klass, dispatch to the implementation method
514 __ lookup_interface_method(temp1_recv_klass, temp3_intf,
515 // note: next two args must be the same:
516 rbx_index, rbx_method,
517 temp2,
518 L_incompatible_class_change_error);
519 break;
520 }
521
522 default:
523 fatal("unexpected intrinsic %d: %s", vmIntrinsics::as_int(iid), vmIntrinsics::name_at(iid));
524 break;
525 }
526
527 // Live at this point:
528 // rbx_method
529 // rsi/r13 (if interpreted)
530
531 // After figuring out which concrete method to call, jump into it.
532 // Note that this works in the interpreter with no data motion.
533 // But the compiled version will require that rcx_recv be shifted out.
534 jump_from_method_handle(_masm, rbx_method, temp1, for_compiler_entry, iid);
535
536 if (iid == vmIntrinsics::_linkToInterface) {
537 __ bind(L_incompatible_class_change_error);
538 __ jump(RuntimeAddress(SharedRuntime::throw_IncompatibleClassChangeError_entry()));
539 }
540 }
541 }
542
543 #ifndef PRODUCT
544 void trace_method_handle_stub(const char* adaptername,
545 oopDesc* mh,
546 intptr_t* saved_regs,
547 intptr_t* entry_sp) {
548 // called as a leaf from native code: do not block the JVM!
549 bool has_mh = (strstr(adaptername, "/static") == nullptr &&
550 strstr(adaptername, "linkTo") == nullptr); // static linkers don't have MH
551 const char* mh_reg_name = has_mh ? "rcx_mh" : "rcx";
552 log_info(methodhandles)("MH %s %s=" PTR_FORMAT " sp=" PTR_FORMAT, adaptername, mh_reg_name, p2i(mh), p2i(entry_sp));
553
554 LogTarget(Trace, methodhandles) lt;
555 if (lt.is_enabled()) {
556 ResourceMark rm;
557 LogStream ls(lt);
558 ls.print_cr("Registers:");
559 const int saved_regs_count = Register::number_of_registers;
560 for (int i = 0; i < saved_regs_count; i++) {
561 Register r = as_Register(i);
562 // The registers are stored in reverse order on the stack (by pusha).
563 int num_regs = UseAPX ? 32 : 16;
564 assert(Register::available_gp_registers() == num_regs, "sanity");
565 if (r == rsp) {
566 // rsp is actually not stored by pusha(), compute the old rsp from saved_regs (rsp after pusha): saved_regs + 16 = old rsp
567 ls.print("%3s=" PTR_FORMAT, r->name(), (intptr_t)(&saved_regs[num_regs]));
568 } else {
569 ls.print("%3s=" PTR_FORMAT, r->name(), saved_regs[((saved_regs_count - 1) - i)]);
570 }
571 if ((i + 1) % 4 == 0) {
572 ls.cr();
573 } else {
574 ls.print(", ");
575 }
576 }
577 ls.cr();
578
579 // Note: We want to allow trace_method_handle from any call site.
580 // While trace_method_handle creates a frame, it may be entered
581 // without a PC on the stack top (e.g. not just after a call).
582 // Walking that frame could lead to failures due to that invalid PC.
583 // => carefully detect that frame when doing the stack walking
584
585 {
586 // dumping last frame with frame::describe
587
588 JavaThread* p = JavaThread::active();
589
590 // may not be needed by safer and unexpensive here
591 PreserveExceptionMark pem(Thread::current());
592 FrameValues values;
593
594 frame cur_frame = os::current_frame();
595
596 if (cur_frame.fp() != nullptr) { // not walkable
597
598 // Robust search of trace_calling_frame (independent of inlining).
599 // Assumes saved_regs comes from a pusha in the trace_calling_frame.
600 //
601 // We have to start the search from cur_frame, because trace_calling_frame may be it.
602 // It is guaranteed that trace_calling_frame is different from the top frame.
603 // But os::current_frame() does NOT return the top frame: it returns the next frame under it (caller's frame).
604 // (Due to inlining and tail call optimizations, caller's frame doesn't necessarily correspond to the immediate
605 // caller in the source code.)
606 assert(cur_frame.sp() < saved_regs, "registers not saved on stack ?");
607 frame trace_calling_frame = cur_frame;
608 while (trace_calling_frame.fp() < saved_regs) {
609 assert(trace_calling_frame.cb() == nullptr, "not a C frame");
610 trace_calling_frame = os::get_sender_for_C_frame(&trace_calling_frame);
611 }
612 assert(trace_calling_frame.sp() < saved_regs, "wrong frame");
613
614 // safely create a frame and call frame::describe
615 intptr_t *dump_sp = trace_calling_frame.sender_sp();
616 intptr_t *dump_fp = trace_calling_frame.link();
617
618 if (has_mh) {
619 // The previous definition of walkable may have to be refined
620 // if new call sites cause the next frame constructor to start
621 // failing. Alternatively, frame constructors could be
622 // modified to support the current or future non walkable
623 // frames (but this is more intrusive and is not considered as
624 // part of this RFE, which will instead use a simpler output).
625 frame dump_frame = frame(dump_sp, dump_fp);
626 dump_frame.describe(values, 1);
627 } else {
628 // Stack may not be walkable (invalid PC above FP):
629 // Add descriptions without building a Java frame to avoid issues
630 values.describe(-1, dump_fp, "fp for #1 <not parsed, cannot trust pc>");
631 values.describe(-1, dump_sp, "sp for #1");
632 }
633 }
634 values.describe(-1, entry_sp, "raw top of stack");
635
636 ls.print_cr("Stack layout:");
637 values.print_on(p, &ls);
638 }
639 if (has_mh && oopDesc::is_oop(mh)) {
640 mh->print_on(&ls);
641 if (java_lang_invoke_MethodHandle::is_instance(mh)) {
642 java_lang_invoke_MethodHandle::form(mh)->print_on(&ls);
643 }
644 }
645 }
646 }
647
648 // The stub wraps the arguments in a struct on the stack to avoid
649 // dealing with the different calling conventions for passing 6
650 // arguments.
651 struct MethodHandleStubArguments {
652 const char* adaptername;
653 oopDesc* mh;
654 intptr_t* saved_regs;
655 intptr_t* entry_sp;
656 };
657 void trace_method_handle_stub_wrapper(MethodHandleStubArguments* args) {
658 trace_method_handle_stub(args->adaptername,
659 args->mh,
660 args->saved_regs,
661 args->entry_sp);
662 }
663
664 void MethodHandles::trace_method_handle(MacroAssembler* _masm, const char* adaptername) {
665 if (!log_is_enabled(Info, methodhandles)) return;
666 BLOCK_COMMENT(err_msg("trace_method_handle %s {", adaptername));
667 __ enter();
668 __ andptr(rsp, -16); // align stack if needed for FPU state
669 __ pusha();
670 __ mov(rbx, rsp); // for retrieving saved_regs
671 // Note: saved_regs must be in the entered frame for the
672 // robust stack walking implemented in trace_method_handle_stub.
673
674 // save FP result, valid at some call sites (adapter_opt_return_float, ...)
675 __ decrement(rsp, 2 * wordSize);
676 __ movdbl(Address(rsp, 0), xmm0);
677
678 // Incoming state:
679 // rcx: method handle
680 //
681 // To avoid calling convention issues, build a record on the stack
682 // and pass the pointer to that instead.
683 __ push(rbp); // entry_sp (with extra align space)
684 __ push(rbx); // pusha saved_regs
685 __ push(rcx); // mh
686 __ push(rcx); // slot for adaptername
687 __ movptr(Address(rsp, 0), (intptr_t) adaptername, rscratch1);
688 __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, trace_method_handle_stub_wrapper), rsp);
689 __ increment(rsp, sizeof(MethodHandleStubArguments));
690
691 __ movdbl(xmm0, Address(rsp, 0));
692 __ increment(rsp, 2 * wordSize);
693
694 __ popa();
695 __ leave();
696 BLOCK_COMMENT("} trace_method_handle");
697 }
698 #endif //PRODUCT