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