1 /*
  2  * Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2016, 2017 SAP SE. All rights reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #include "precompiled.hpp"
 27 #include "jvm.h"
 28 #include "asm/macroAssembler.inline.hpp"
 29 #include "classfile/javaClasses.inline.hpp"
 30 #include "classfile/vmClasses.hpp"
 31 #include "interpreter/interpreter.hpp"
 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/frame.inline.hpp"
 39 #include "runtime/stubRoutines.hpp"
 40 #include "utilities/preserveException.hpp"
 41 
 42 #ifdef PRODUCT
 43 #define __ _masm->
 44 #define BLOCK_COMMENT(str) /* nothing */
 45 #else
 46 #define __ (Verbose ? (_masm->block_comment(FILE_AND_LINE),_masm):_masm)->
 47 #define BLOCK_COMMENT(str) __ block_comment(str)
 48 #endif
 49 
 50 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
 51 
 52 // Workaround for C++ overloading nastiness on '0' for RegisterOrConstant.
 53 static RegisterOrConstant constant(int value) {
 54   return RegisterOrConstant(value);
 55 }
 56 
 57 void MethodHandles::load_klass_from_Class(MacroAssembler* _masm, Register klass_reg,
 58                                           Register temp_reg, Register temp2_reg) {
 59   if (VerifyMethodHandles) {
 60     verify_klass(_masm, klass_reg, VM_CLASS_ID(java_lang_Class),
 61                  temp_reg, temp2_reg, "MH argument is a Class");
 62   }
 63   __ z_lg(klass_reg, Address(klass_reg, java_lang_Class::klass_offset()));
 64 }
 65 
 66 
 67 #ifdef ASSERT
 68 static int check_nonzero(const char* xname, int x) {
 69   assert(x != 0, "%s should be nonzero", xname);
 70   return x;
 71 }
 72 #define NONZERO(x) check_nonzero(#x, x)
 73 #else
 74 #define NONZERO(x) (x)
 75 #endif
 76 
 77 #ifdef ASSERT
 78 void MethodHandles::verify_klass(MacroAssembler* _masm,
 79                                  Register obj_reg, vmClassID klass_id,
 80                                  Register temp_reg, Register temp2_reg,
 81                                  const char* error_message) {
 82 
 83   InstanceKlass** klass_addr = vmClasses::klass_addr_at(klass_id);
 84   Klass* klass = vmClasses::klass_at(klass_id);
 85 
 86   assert(temp_reg != Z_R0 && // Is used as base register!
 87          temp_reg != noreg && temp2_reg != noreg, "need valid registers!");
 88 
 89   NearLabel L_ok, L_bad;
 90 
 91   BLOCK_COMMENT("verify_klass {");
 92 
 93   __ verify_oop(obj_reg, FILE_AND_LINE);
 94   __ compareU64_and_branch(obj_reg, (intptr_t)0L, Assembler::bcondEqual, L_bad);
 95   __ load_klass(temp_reg, obj_reg);
 96   // klass_addr is a klass in allstatic SystemDictionaryHandles. Can't get GCed.
 97   __ load_const_optimized(temp2_reg, (address)klass_addr);
 98   __ z_lg(temp2_reg, Address(temp2_reg));
 99   __ compareU64_and_branch(temp_reg, temp2_reg, Assembler::bcondEqual, L_ok);
100 
101   intptr_t super_check_offset = klass->super_check_offset();
102   __ z_lg(temp_reg, Address(temp_reg, super_check_offset));
103   __ compareU64_and_branch(temp_reg, temp2_reg, Assembler::bcondEqual, L_ok);
104   __ BIND(L_bad);
105   __ stop(error_message);
106   __ BIND(L_ok);
107 
108   BLOCK_COMMENT("} verify_klass");
109 }
110 
111 void MethodHandles::verify_ref_kind(MacroAssembler* _masm, int ref_kind,
112                                     Register member_reg, Register temp  ) {
113   NearLabel L;
114   BLOCK_COMMENT("verify_ref_kind {");
115 
116   __ z_llgf(temp,
117             Address(member_reg,
118                     NONZERO(java_lang_invoke_MemberName::flags_offset())));
119   __ z_srl(temp,  java_lang_invoke_MemberName::MN_REFERENCE_KIND_SHIFT);
120   __ z_nilf(temp, java_lang_invoke_MemberName::MN_REFERENCE_KIND_MASK);
121   __ compare32_and_branch(temp, constant(ref_kind), Assembler::bcondEqual, L);
122 
123   {
124     char *buf = NEW_C_HEAP_ARRAY(char, 100, mtInternal);
125 
126     jio_snprintf(buf, 100, "verify_ref_kind expected %x", ref_kind);
127     if (ref_kind == JVM_REF_invokeVirtual || ref_kind == JVM_REF_invokeSpecial) {
128       // Could do this for all ref_kinds, but would explode assembly code size.
129       trace_method_handle(_masm, buf);
130     }
131     __ stop(buf);
132   }
133 
134   BLOCK_COMMENT("} verify_ref_kind");
135 
136   __ bind(L);
137 }
138 #endif // ASSERT
139 
140 void MethodHandles::jump_from_method_handle(MacroAssembler* _masm, Register method, Register target,
141                                             Register temp, bool for_compiler_entry) {
142   assert(method == Z_method, "interpreter calling convention");
143   __ verify_method_ptr(method);
144 
145   assert(target != method, "don 't you kill the method reg!");
146 
147   Label L_no_such_method;
148 
149   if (!for_compiler_entry && JvmtiExport::can_post_interpreter_events()) {
150     // JVMTI events, such as single-stepping, are implemented partly
151     // by avoiding running compiled code in threads for which the
152     // event is enabled. Check here for interp_only_mode if these
153     // events CAN be enabled.
154     __ verify_thread();
155 
156     Label run_compiled_code;
157 
158     __ load_and_test_int(temp, Address(Z_thread, JavaThread::interp_only_mode_offset()));
159     __ z_bre(run_compiled_code);
160 
161     // Null method test is replicated below in compiled case,
162     // it might be able to address across the verify_thread().
163     __ z_ltgr(temp, method);
164     __ z_bre(L_no_such_method);
165 
166     __ z_lg(target, Address(method, Method::interpreter_entry_offset()));
167     __ z_br(target);
168 
169     __ bind(run_compiled_code);
170   }
171 
172   // Compiled case, either static or fall-through from runtime conditional.
173   __ z_ltgr(temp, method);
174   __ z_bre(L_no_such_method);
175 
176   ByteSize offset = for_compiler_entry ?
177                        Method::from_compiled_offset() : Method::from_interpreted_offset();
178   Address method_from(method, offset);
179 
180   __ z_lg(target, method_from);
181   __ z_br(target);
182 
183   __ bind(L_no_such_method);
184   assert(StubRoutines::throw_AbstractMethodError_entry() != NULL, "not yet generated!");
185   __ load_const_optimized(target, StubRoutines::throw_AbstractMethodError_entry());
186   __ z_br(target);
187 }
188 
189 void MethodHandles::jump_to_lambda_form(MacroAssembler* _masm,
190                                         Register recv, Register method_temp,
191                                         Register temp2, Register temp3,
192                                         bool for_compiler_entry) {
193 
194   // This is the initial entry point of a lazy method handle.
195   // After type checking, it picks up the invoker from the LambdaForm.
196   assert_different_registers(recv, method_temp, temp2, temp3);
197   assert(method_temp == Z_method, "required register for loading method");
198 
199   BLOCK_COMMENT("jump_to_lambda_form {");
200 
201   // Load the invoker, as MH -> MH.form -> LF.vmentry
202   __ verify_oop(recv, FILE_AND_LINE);
203   __ load_heap_oop(method_temp,
204                    Address(recv,
205                            NONZERO(java_lang_invoke_MethodHandle::form_offset())),
206                    noreg, noreg, IS_NOT_NULL);
207   __ verify_oop(method_temp, FILE_AND_LINE);
208   __ load_heap_oop(method_temp,
209                    Address(method_temp,
210                            NONZERO(java_lang_invoke_LambdaForm::vmentry_offset())),
211                    noreg, noreg, IS_NOT_NULL);
212   __ verify_oop(method_temp, FILE_AND_LINE);
213   __ load_heap_oop(method_temp,
214                    Address(method_temp,
215                            NONZERO(java_lang_invoke_MemberName::method_offset())),
216                    noreg, noreg, IS_NOT_NULL);
217   __ verify_oop(method_temp, FILE_AND_LINE);
218   __ z_lg(method_temp,
219           Address(method_temp,
220                   NONZERO(java_lang_invoke_ResolvedMethodName::vmtarget_offset())));
221 
222   if (VerifyMethodHandles && !for_compiler_entry) {
223     // Make sure recv is already on stack.
224     NearLabel L;
225     Address paramSize(temp2, ConstMethod::size_of_parameters_offset());
226 
227     __ z_lg(temp2, Address(method_temp, Method::const_offset()));
228     __ load_sized_value(temp2, paramSize, sizeof(u2), /*is_signed*/ false);
229     // if (temp2 != recv) stop
230     __ z_lg(temp2, __ argument_address(temp2, temp2, 0));
231     __ compare64_and_branch(temp2, recv, Assembler::bcondEqual, L);
232     __ stop("receiver not on stack");
233     __ BIND(L);
234   }
235 
236   jump_from_method_handle(_masm, method_temp, temp2, Z_R0, for_compiler_entry);
237 
238   BLOCK_COMMENT("} jump_to_lambda_form");
239 }
240 
241 // code generation
242 address MethodHandles::generate_method_handle_interpreter_entry(MacroAssembler* _masm,
243                                                                 vmIntrinsics::ID iid) {
244   const bool not_for_compiler_entry = false;  // This is the interpreter entry.
245   assert(is_signature_polymorphic(iid), "expected invoke iid");
246 
247   if (iid == vmIntrinsics::_invokeGeneric || iid == vmIntrinsics::_compiledLambdaForm) {
248     // Perhaps surprisingly, the symbolic references visible to Java
249     // are not directly used. They are linked to Java-generated
250     // adapters via MethodHandleNatives.linkMethod. They all allow an
251     // appendix argument.
252     __ should_not_reach_here();           // Empty stubs make SG sick.
253     return NULL;
254   }
255 
256   // No need in interpreter entry for linkToNative for now.
257   // Interpreter calls compiled entry through i2c.
258   if (iid == vmIntrinsics::_linkToNative) {
259     __ should_not_reach_here();           // Empty stubs make SG sick.
260     return NULL;
261   }
262 
263   // Z_R10: sender SP (must preserve; see prepare_to_jump_from_interprted)
264   // Z_method: method
265   // Z_ARG1 (Gargs): incoming argument list (must preserve)
266   Register Z_R4_param_size = Z_R4;   // size of parameters
267   address code_start = __ pc();
268 
269   // Here is where control starts out:
270   __ align(CodeEntryAlignment);
271 
272   address entry_point = __ pc();
273 
274   if (VerifyMethodHandles) {
275     Label L;
276     BLOCK_COMMENT("verify_intrinsic_id {");
277 
278     // Supplement to 8139891: _intrinsic_id exceeded 1-byte size limit.
279     if (Method::intrinsic_id_size_in_bytes() == 1) {
280       __ z_cli(Address(Z_method, Method::intrinsic_id_offset_in_bytes()), (int)iid);
281     } else {
282       assert(Method::intrinsic_id_size_in_bytes() == 2, "size error: check Method::_intrinsic_id");
283       __ z_lh(Z_R0_scratch, Address(Z_method, Method::intrinsic_id_offset_in_bytes()));
284       __ z_chi(Z_R0_scratch, (int)iid);
285     }
286     __ z_bre(L);
287 
288     if (iid == vmIntrinsics::_linkToVirtual || iid == vmIntrinsics::_linkToSpecial) {
289       // Could do this for all kinds, but would explode assembly code size.
290       trace_method_handle(_masm, "bad Method::intrinsic_id");
291     }
292 
293     __ stop("bad Method::intrinsic_id");
294     __ bind(L);
295 
296     BLOCK_COMMENT("} verify_intrinsic_id");
297   }
298 
299   // First task: Find out how big the argument list is.
300   Address Z_R4_first_arg_addr;
301   int ref_kind = signature_polymorphic_intrinsic_ref_kind(iid);
302 
303   assert(ref_kind != 0 || iid == vmIntrinsics::_invokeBasic,
304          "must be _invokeBasic or a linkTo intrinsic");
305 
306   if (ref_kind == 0 || MethodHandles::ref_kind_has_receiver(ref_kind)) {
307      Address paramSize(Z_R1_scratch, ConstMethod::size_of_parameters_offset());
308 
309     __ z_lg(Z_R1_scratch, Address(Z_method, Method::const_offset()));
310     __ load_sized_value(Z_R4_param_size, paramSize, sizeof(u2), /*is_signed*/ false);
311     Z_R4_first_arg_addr = __ argument_address(Z_R4_param_size, Z_R4_param_size, 0);
312   } else {
313     DEBUG_ONLY(Z_R4_param_size = noreg);
314   }
315 
316   Register Z_mh = noreg;
317   if (!is_signature_polymorphic_static(iid)) {
318     Z_mh = Z_ARG4;
319     __ z_lg(Z_mh, Z_R4_first_arg_addr);
320     DEBUG_ONLY(Z_R4_param_size = noreg);
321   }
322 
323   // Z_R4_first_arg_addr is live!
324 
325   trace_method_handle_interpreter_entry(_masm, iid);
326 
327   if (iid == vmIntrinsics::_invokeBasic) {
328     __ pc(); // just for the block comment
329     generate_method_handle_dispatch(_masm, iid, Z_mh, noreg, not_for_compiler_entry);
330   } else {
331     // Adjust argument list by popping the trailing MemberName argument.
332     Register Z_recv = noreg;
333 
334     if (MethodHandles::ref_kind_has_receiver(ref_kind)) {
335       // Load the receiver (not the MH; the actual MemberName's receiver)
336       // up from the interpreter stack.
337       __ z_lg(Z_recv = Z_R5, Z_R4_first_arg_addr);
338       DEBUG_ONLY(Z_R4_param_size = noreg);
339     }
340 
341     Register Z_member = Z_method;  // MemberName ptr; incoming method ptr is dead now
342 
343     __ z_lg(Z_member, __ argument_address(constant(1)));
344     __ add2reg(Z_esp, Interpreter::stackElementSize);
345     generate_method_handle_dispatch(_masm, iid, Z_recv, Z_member, not_for_compiler_entry);
346   }
347 
348   return entry_point;
349 }
350 
351 void MethodHandles::jump_to_native_invoker(MacroAssembler* _masm, Register nep_reg, Register temp_target) {
352   BLOCK_COMMENT("jump_to_native_invoker {");
353   __ should_not_reach_here();
354   BLOCK_COMMENT("} jump_to_native_invoker");
355 }
356 
357 void MethodHandles::generate_method_handle_dispatch(MacroAssembler* _masm,
358                                                     vmIntrinsics::ID iid,
359                                                     Register receiver_reg,
360                                                     Register member_reg,
361                                                     bool for_compiler_entry) {
362   assert(is_signature_polymorphic(iid), "expected invoke iid");
363 
364   Register temp1 = for_compiler_entry ? Z_R10 : Z_R6;
365   Register temp2 = Z_R12;
366   Register temp3 = Z_R11;
367   Register temp4 = Z_R13;
368 
369   if (for_compiler_entry) {
370     assert(receiver_reg == (iid == vmIntrinsics::_linkToStatic || iid == vmIntrinsics::_linkToNative ? noreg : Z_ARG1),
371            "only valid assignment");
372   }
373   if (receiver_reg != noreg) {
374     assert_different_registers(temp1, temp2, temp3, temp4, receiver_reg);
375   }
376   if (member_reg != noreg) {
377     assert_different_registers(temp1, temp2, temp3, temp4, member_reg);
378   }
379   if (!for_compiler_entry) {  // Don't trash last SP.
380     assert_different_registers(temp1, temp2, temp3, temp4, Z_R10);
381   }
382 
383   if (iid == vmIntrinsics::_invokeBasic) {
384     __ pc(); // Just for the block comment.
385     // Indirect through MH.form.vmentry.vmtarget.
386     jump_to_lambda_form(_masm, receiver_reg, Z_method, Z_R1, temp3, for_compiler_entry);
387     return;
388   } else if (iid == vmIntrinsics::_linkToNative) {
389     assert(for_compiler_entry, "only compiler entry is supported");
390     jump_to_native_invoker(_masm, member_reg, temp1);
391   }
392 
393   // The method is a member invoker used by direct method handles.
394   if (VerifyMethodHandles) {
395     // Make sure the trailing argument really is a MemberName (caller responsibility).
396     verify_klass(_masm, member_reg,
397                  VM_CLASS_ID(MemberName_klass),
398                  temp1, temp2,
399                  "MemberName required for invokeVirtual etc.");
400   }
401 
402   Address  member_clazz(   member_reg, NONZERO(java_lang_invoke_MemberName::clazz_offset()));
403   Address  member_vmindex( member_reg, NONZERO(java_lang_invoke_MemberName::vmindex_offset()));
404   Address  member_vmtarget(member_reg, NONZERO(java_lang_invoke_MemberName::method_offset()));
405   Address  vmtarget_method(Z_method, NONZERO(java_lang_invoke_ResolvedMethodName::vmtarget_offset()));
406   Register temp1_recv_klass = temp1;
407 
408   if (iid != vmIntrinsics::_linkToStatic) {
409     __ verify_oop(receiver_reg, FILE_AND_LINE);
410     if (iid == vmIntrinsics::_linkToSpecial) {
411       // Don't actually load the klass; just null-check the receiver.
412       __ null_check(receiver_reg);
413     } else {
414       // Load receiver klass itself.
415       __ null_check(receiver_reg, Z_R0, oopDesc::klass_offset_in_bytes());
416       __ load_klass(temp1_recv_klass, receiver_reg);
417       __ verify_klass_ptr(temp1_recv_klass);
418     }
419     BLOCK_COMMENT("check_receiver {");
420     // The receiver for the MemberName must be in receiver_reg.
421     // Check the receiver against the MemberName.clazz.
422     if (VerifyMethodHandles && iid == vmIntrinsics::_linkToSpecial) {
423       // Did not load it above...
424       __ load_klass(temp1_recv_klass, receiver_reg);
425       __ verify_klass_ptr(temp1_recv_klass);
426     }
427 
428     if (VerifyMethodHandles && iid != vmIntrinsics::_linkToInterface) {
429       NearLabel L_ok;
430       Register temp2_defc = temp2;
431 
432       __ load_heap_oop(temp2_defc, member_clazz,
433                        noreg, noreg, IS_NOT_NULL);
434       load_klass_from_Class(_masm, temp2_defc, temp3, temp4);
435       __ verify_klass_ptr(temp2_defc);
436       __ check_klass_subtype(temp1_recv_klass, temp2_defc, temp3, temp4, L_ok);
437       // If we get here, the type check failed!
438       __ stop("receiver class disagrees with MemberName.clazz");
439       __ bind(L_ok);
440     }
441     BLOCK_COMMENT("} check_receiver");
442   }
443   if (iid == vmIntrinsics::_linkToSpecial || iid == vmIntrinsics::_linkToStatic) {
444     DEBUG_ONLY(temp1_recv_klass = noreg);  // These guys didn't load the recv_klass.
445   }
446 
447   // Live registers at this point:
448   //   member_reg       - MemberName that was the trailing argument.
449   //   temp1_recv_klass - Klass of stacked receiver, if needed.
450   //   Z_R10            - Interpreter linkage if interpreted.
451 
452   bool method_is_live = false;
453 
454   switch (iid) {
455     case vmIntrinsics::_linkToSpecial:
456       if (VerifyMethodHandles) {
457         verify_ref_kind(_masm, JVM_REF_invokeSpecial, member_reg, temp3);
458       }
459       __ load_heap_oop(Z_method, member_vmtarget,
460                        noreg, noreg, IS_NOT_NULL);
461       __ z_lg(Z_method, vmtarget_method);
462       method_is_live = true;
463       break;
464 
465     case vmIntrinsics::_linkToStatic:
466       if (VerifyMethodHandles) {
467         verify_ref_kind(_masm, JVM_REF_invokeStatic, member_reg, temp3);
468       }
469       __ load_heap_oop(Z_method, member_vmtarget,
470                        noreg, noreg, IS_NOT_NULL);
471       __ z_lg(Z_method, vmtarget_method);
472       method_is_live = true;
473       break;
474 
475     case vmIntrinsics::_linkToVirtual: {
476       // Same as TemplateTable::invokevirtual, minus the CP setup and profiling.
477       if (VerifyMethodHandles) {
478         verify_ref_kind(_masm, JVM_REF_invokeVirtual, member_reg, temp3);
479       }
480 
481       // Pick out the vtable index from the MemberName, and then we can discard it.
482       Register temp2_index = temp2;
483       __ z_lg(temp2_index, member_vmindex);
484 
485       if (VerifyMethodHandles) {
486         // if (member_vmindex < 0) stop
487         NearLabel L_index_ok;
488         __ compare32_and_branch(temp2_index, constant(0), Assembler::bcondNotLow, L_index_ok);
489         __ stop("no virtual index");
490         __ BIND(L_index_ok);
491       }
492 
493       // Note: The verifier invariants allow us to ignore MemberName.clazz and vmtarget
494       // at this point. And VerifyMethodHandles has already checked clazz, if needed.
495 
496       // Get target method and entry point.
497       __ lookup_virtual_method(temp1_recv_klass, temp2_index, Z_method);
498       method_is_live = true;
499       break;
500     }
501 
502     case vmIntrinsics::_linkToInterface: {
503       // Same as TemplateTable::invokeinterface, minus the CP setup
504       // and profiling, with different argument motion.
505       if (VerifyMethodHandles) {
506         verify_ref_kind(_masm, JVM_REF_invokeInterface, member_reg, temp3);
507       }
508 
509       Register temp3_intf = temp3;
510 
511       __ load_heap_oop(temp3_intf, member_clazz,
512                        noreg, noreg, IS_NOT_NULL);
513       load_klass_from_Class(_masm, temp3_intf, temp2, temp4);
514 
515       Register Z_index = Z_method;
516 
517       __ z_lg(Z_index, member_vmindex);
518 
519       if (VerifyMethodHandles) {
520         NearLabel L;
521         // if (member_vmindex < 0) stop
522         __ compare32_and_branch(Z_index, constant(0), Assembler::bcondNotLow, L);
523         __ stop("invalid vtable index for MH.invokeInterface");
524         __ bind(L);
525       }
526 
527       // Given interface, index, and recv klass, dispatch to the implementation method.
528       Label L_no_such_interface;
529       __ lookup_interface_method(temp1_recv_klass, temp3_intf,
530                                  // Note: next two args must be the same:
531                                  Z_index, Z_method, temp2,
532                                  L_no_such_interface);
533       jump_from_method_handle(_masm, Z_method, temp2, Z_R0, for_compiler_entry);
534 
535       __ bind(L_no_such_interface);
536 
537       // Throw exception.
538       __ load_const_optimized(Z_R1, StubRoutines::throw_IncompatibleClassChangeError_entry());
539       __ z_br(Z_R1);
540       break;
541     }
542 
543     default:
544       fatal("unexpected intrinsic %d: %s", vmIntrinsics::as_int(iid), vmIntrinsics::name_at(iid));
545       break;
546   }
547 
548   if (method_is_live) {
549     // Live at this point: Z_method, O5_savedSP (if interpreted).
550 
551     // After figuring out which concrete method to call, jump into it.
552     // Note that this works in the interpreter with no data motion.
553     // But the compiled version will require that rcx_recv be shifted out.
554     jump_from_method_handle(_masm, Z_method, temp1, Z_R0, for_compiler_entry);
555   }
556 }
557 
558 #ifndef PRODUCT
559 void trace_method_handle_stub(const char* adaptername,
560                               oopDesc* mh,
561                               intptr_t* sender_sp,
562                               intptr_t* args,
563                               intptr_t* tracing_fp) {
564   bool has_mh = (strstr(adaptername, "/static") == NULL &&
565                  strstr(adaptername, "linkTo") == NULL);    // Static linkers don't have MH.
566   const char* mh_reg_name = has_mh ? "Z_R4_mh" : "Z_R4";
567   log_info(methodhandles)("MH %s %s=" INTPTR_FORMAT " sender_sp=" INTPTR_FORMAT " args=" INTPTR_FORMAT,
568                           adaptername, mh_reg_name,
569                           p2i(mh), p2i(sender_sp), p2i(args));
570 
571   LogTarget(Trace, methodhandles) lt;
572   if (lt.is_enabled()) {
573     // Dumping last frame with frame::describe.
574     ResourceMark rm;
575     LogStream ls(lt);
576     JavaThread* p = JavaThread::active();
577 
578     // may not be needed by safer and unexpensive here
579     PreserveExceptionMark pem(Thread::current());
580     FrameValues values;
581 
582     // Note: We want to allow trace_method_handle from any call site.
583     // While trace_method_handle creates a frame, it may be entered
584     // without a valid return PC in Z_R14 (e.g. not just after a call).
585     // Walking that frame could lead to failures due to that invalid PC.
586     // => carefully detect that frame when doing the stack walking.
587 
588     // Walk up to the right frame using the "tracing_fp" argument.
589     frame cur_frame = os::current_frame(); // Current C frame.
590 
591     while (cur_frame.fp() != tracing_fp) {
592       cur_frame = os::get_sender_for_C_frame(&cur_frame);
593     }
594 
595     // Safely create a frame and call frame::describe.
596     intptr_t *dump_sp = cur_frame.sender_sp();
597     intptr_t *dump_fp = cur_frame.link();
598 
599     bool walkable = has_mh; // Whether the traced frame shoud be walkable.
600 
601     // The sender for cur_frame is the caller of trace_method_handle.
602     if (walkable) {
603       // The previous definition of walkable may have to be refined
604       // if new call sites cause the next frame constructor to start
605       // failing. Alternatively, frame constructors could be
606       // modified to support the current or future non walkable
607       // frames (but this is more intrusive and is not considered as
608       // part of this RFE, which will instead use a simpler output).
609       frame dump_frame = frame(dump_sp);
610       dump_frame.describe(values, 1);
611     } else {
612       // Robust dump for frames which cannot be constructed from sp/younger_sp
613       // Add descriptions without building a Java frame to avoid issues.
614       values.describe(-1, dump_fp, "fp for #1 <not parsed, cannot trust pc>");
615       values.describe(-1, dump_sp, "sp");
616     }
617 
618     bool has_args = has_mh; // Whether Z_esp is meaningful.
619 
620     // Mark args, if seems valid (may not be valid for some adapters).
621     if (has_args) {
622       if ((args >= dump_sp) && (args < dump_fp)) {
623         values.describe(-1, args, "*Z_esp");
624       }
625     }
626 
627     // Note: the unextended_sp may not be correct.
628     ls.print_cr("  stack layout:");
629     values.print_on(p, &ls);
630     if (has_mh && oopDesc::is_oop(mh)) {
631       mh->print_on(&ls);
632       if (java_lang_invoke_MethodHandle::is_instance(mh)) {
633         java_lang_invoke_MethodHandle::form(mh)->print_on(&ls);
634       }
635     }
636   }
637 }
638 
639 void MethodHandles::trace_method_handle(MacroAssembler* _masm, const char* adaptername) {
640   if (!log_is_enabled(Info, methodhandles)) { return; }
641 
642   // If arg registers are contiguous, we can use STMG/LMG.
643   assert((Z_ARG5->encoding() - Z_ARG1->encoding() + 1) == RegisterImpl::number_of_arg_registers, "Oops");
644 
645   BLOCK_COMMENT("trace_method_handle {");
646 
647   // Save argument registers (they are used in raise exception stub).
648   // Argument registers have contiguous register numbers -> we can use stmg/lmg.
649   __ z_stmg(Z_ARG1, Z_ARG5, 16, Z_SP);
650 
651   // Setup arguments.
652   __ z_lgr(Z_ARG2, Z_ARG4); // mh, see generate_method_handle_interpreter_entry()
653   __ z_lgr(Z_ARG3, Z_R10);  // sender_sp
654   __ z_lgr(Z_ARG4, Z_esp);
655   __ load_const_optimized(Z_ARG1, (void *)adaptername);
656   __ z_lgr(Z_ARG5, Z_SP);   // tracing_fp
657   __ save_return_pc();      // saves Z_R14
658   __ push_frame_abi160(0);
659   __ call_VM_leaf(CAST_FROM_FN_PTR(address, trace_method_handle_stub));
660   __ pop_frame();
661   __ restore_return_pc();   // restores to Z_R14
662 
663   // Restore argument registers
664   __ z_lmg(Z_ARG1, Z_ARG5, 16, Z_SP);
665   __ zap_from_to(Z_SP, Z_SP, Z_R0, Z_R1, 50, -1);
666   __ zap_from_to(Z_SP, Z_SP, Z_R0, Z_R1, -1, 5);
667 
668   BLOCK_COMMENT("} trace_method_handle");
669 }
670 #endif // !PRODUCT