1 /*
  2  * Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2012, 2021 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 #define __ _masm->
 43 
 44 #ifdef PRODUCT
 45 #define BLOCK_COMMENT(str) // nothing
 46 #else
 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 inline 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   __ ld(klass_reg, java_lang_Class::klass_offset(), klass_reg);
 64 }
 65 
 66 #ifdef ASSERT
 67 static int check_nonzero(const char* xname, int x) {
 68   assert(x != 0, "%s should be nonzero", xname);
 69   return x;
 70 }
 71 #define NONZERO(x) check_nonzero(#x, x)
 72 #else //ASSERT
 73 #define NONZERO(x) (x)
 74 #endif //ASSERT
 75 
 76 #ifdef ASSERT
 77 void MethodHandles::verify_klass(MacroAssembler* _masm,
 78                                  Register obj_reg, vmClassID klass_id,
 79                                  Register temp_reg, Register temp2_reg,
 80                                  const char* error_message) {
 81   InstanceKlass** klass_addr = vmClasses::klass_addr_at(klass_id);
 82   Klass* klass = vmClasses::klass_at(klass_id);
 83   Label L_ok, L_bad;
 84   BLOCK_COMMENT("verify_klass {");
 85   __ verify_oop(obj_reg, FILE_AND_LINE);
 86   __ cmpdi(CCR0, obj_reg, 0);
 87   __ beq(CCR0, L_bad);
 88   __ load_klass(temp_reg, obj_reg);
 89   __ load_const_optimized(temp2_reg, (address) klass_addr);
 90   __ ld(temp2_reg, 0, temp2_reg);
 91   __ cmpd(CCR0, temp_reg, temp2_reg);
 92   __ beq(CCR0, L_ok);
 93   __ ld(temp_reg, klass->super_check_offset(), temp_reg);
 94   __ cmpd(CCR0, temp_reg, temp2_reg);
 95   __ beq(CCR0, L_ok);
 96   __ BIND(L_bad);
 97   __ stop(error_message);
 98   __ BIND(L_ok);
 99   BLOCK_COMMENT("} verify_klass");
100 }
101 
102 void MethodHandles::verify_ref_kind(MacroAssembler* _masm, int ref_kind, Register member_reg, Register temp) {
103   Label L;
104   BLOCK_COMMENT("verify_ref_kind {");
105   __ load_sized_value(temp, NONZERO(java_lang_invoke_MemberName::flags_offset()), member_reg,
106                       sizeof(u4), /*is_signed*/ false);
107   // assert(sizeof(u4) == sizeof(java.lang.invoke.MemberName.flags), "");
108   __ srwi( temp, temp, java_lang_invoke_MemberName::MN_REFERENCE_KIND_SHIFT);
109   __ andi(temp, temp, java_lang_invoke_MemberName::MN_REFERENCE_KIND_MASK);
110   __ cmpwi(CCR1, temp, ref_kind);
111   __ beq(CCR1, L);
112   { char* buf = NEW_C_HEAP_ARRAY(char, 100, mtInternal);
113     jio_snprintf(buf, 100, "verify_ref_kind expected %x", 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, buf);
118     __ stop(buf);
119   }
120   BLOCK_COMMENT("} verify_ref_kind");
121   __ BIND(L);
122 }
123 
124 #endif // ASSERT
125 
126 void MethodHandles::jump_from_method_handle(MacroAssembler* _masm, Register method, Register target, Register temp,
127                                             bool for_compiler_entry) {
128   Label L_no_such_method;
129   assert(method == R19_method, "interpreter calling convention");
130   assert_different_registers(method, target, temp);
131 
132   if (!for_compiler_entry && JvmtiExport::can_post_interpreter_events()) {
133     Label run_compiled_code;
134     // JVMTI events, such as single-stepping, are implemented partly by avoiding running
135     // compiled code in threads for which the event is enabled.  Check here for
136     // interp_only_mode if these events CAN be enabled.
137     __ verify_thread();
138     __ lwz(temp, in_bytes(JavaThread::interp_only_mode_offset()), R16_thread);
139     __ cmplwi(CCR0, temp, 0);
140     __ beq(CCR0, run_compiled_code);
141     // Null method test is replicated below in compiled case,
142     // it might be able to address across the verify_thread()
143     __ cmplwi(CCR0, R19_method, 0);
144     __ beq(CCR0, L_no_such_method);
145     __ ld(target, in_bytes(Method::interpreter_entry_offset()), R19_method);
146     __ mtctr(target);
147     __ bctr();
148     __ BIND(run_compiled_code);
149   }
150 
151   // Compiled case, either static or fall-through from runtime conditional
152   __ cmplwi(CCR0, R19_method, 0);
153   __ beq(CCR0, L_no_such_method);
154 
155   const ByteSize entry_offset = for_compiler_entry ? Method::from_compiled_offset() :
156                                                      Method::from_interpreted_offset();
157   __ ld(target, in_bytes(entry_offset), R19_method);
158   __ mtctr(target);
159   __ bctr();
160 
161   __ bind(L_no_such_method);
162   assert(StubRoutines::throw_AbstractMethodError_entry() != NULL, "not yet generated!");
163   __ load_const_optimized(target, StubRoutines::throw_AbstractMethodError_entry());
164   __ mtctr(target);
165   __ bctr();
166 }
167 
168 
169 void MethodHandles::jump_to_lambda_form(MacroAssembler* _masm,
170                                         Register recv, Register method_temp,
171                                         Register temp2, Register temp3,
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, temp3);
177   assert(method_temp == R19_method, "required register for loading method");
178 
179   // Load the invoker, as MH -> MH.form -> LF.vmentry
180   __ verify_oop(recv, FILE_AND_LINE);
181 
182   const MacroAssembler::PreservationLevel preservation_level = for_compiler_entry
183     ? MacroAssembler::PRESERVATION_FRAME_LR_GP_FP_REGS
184     : MacroAssembler::PRESERVATION_FRAME_LR;
185 
186   __ load_heap_oop(method_temp, NONZERO(java_lang_invoke_MethodHandle::form_offset()), recv,
187                    temp2, temp3, preservation_level, IS_NOT_NULL);
188   __ verify_oop(method_temp, FILE_AND_LINE);
189   __ load_heap_oop(method_temp, NONZERO(java_lang_invoke_LambdaForm::vmentry_offset()), method_temp,
190                    temp2, temp3, preservation_level, IS_NOT_NULL);
191   __ verify_oop(method_temp, FILE_AND_LINE);
192   __ load_heap_oop(method_temp, NONZERO(java_lang_invoke_MemberName::method_offset()), method_temp,
193                    temp2, temp3, preservation_level, IS_NOT_NULL);
194   __ verify_oop(method_temp, FILE_AND_LINE);
195   __ ld(method_temp, NONZERO(java_lang_invoke_ResolvedMethodName::vmtarget_offset()), method_temp);
196 
197   if (VerifyMethodHandles && !for_compiler_entry) {
198     // Make sure recv is already on stack.
199     __ ld(temp2, in_bytes(Method::const_offset()), method_temp);
200     __ load_sized_value(temp2, in_bytes(ConstMethod::size_of_parameters_offset()), temp2,
201                         sizeof(u2), /*is_signed*/ false);
202     // assert(sizeof(u2) == sizeof(ConstMethod::_size_of_parameters), "");
203     Label L;
204     __ ld(temp2, __ argument_offset(temp2, temp2, 0), R15_esp);
205     __ cmpd(CCR1, temp2, recv);
206     __ beq(CCR1, L);
207     __ stop("receiver not on stack");
208     __ BIND(L);
209   }
210 
211   jump_from_method_handle(_masm, method_temp, temp2, temp3, for_compiler_entry);
212   BLOCK_COMMENT("} jump_to_lambda_form");
213 }
214 
215 
216 
217 // Code generation
218 address MethodHandles::generate_method_handle_interpreter_entry(MacroAssembler* _masm,
219                                                                 vmIntrinsics::ID iid) {
220   const bool not_for_compiler_entry = false;  // this is the interpreter entry
221   assert(is_signature_polymorphic(iid), "expected invoke iid");
222   if (iid == vmIntrinsics::_invokeGeneric ||
223       iid == vmIntrinsics::_compiledLambdaForm) {
224     // Perhaps surprisingly, the symbolic references visible to Java are not directly used.
225     // They are linked to Java-generated adapters via MethodHandleNatives.linkMethod.
226     // They all allow an appendix argument.
227     __ stop("Should not reach here");           // empty stubs make SG sick
228     return NULL;
229   }
230 
231   // No need in interpreter entry for linkToNative for now.
232   // Interpreter calls compiled entry through i2c.
233   if (iid == vmIntrinsics::_linkToNative) {
234     __ stop("Should not reach here");           // empty stubs make SG sick
235     return NULL;
236   }
237 
238   Register R15_argbase   = R15_esp; // parameter (preserved)
239   Register R30_tmp1      = R30;
240   Register R7_param_size = R7;
241 
242   // here's where control starts out:
243   __ align(CodeEntryAlignment);
244   address entry_point = __ pc();
245 
246   if (VerifyMethodHandles) {
247     assert(Method::intrinsic_id_size_in_bytes() == 2, "assuming Method::_intrinsic_id is u2");
248 
249     Label L;
250     BLOCK_COMMENT("verify_intrinsic_id {");
251     __ load_sized_value(R30_tmp1, Method::intrinsic_id_offset_in_bytes(), R19_method,
252                         sizeof(u2), /*is_signed*/ false);
253     __ cmpwi(CCR1, R30_tmp1, (int) iid);
254     __ beq(CCR1, L);
255     if (iid == vmIntrinsics::_linkToVirtual ||
256         iid == vmIntrinsics::_linkToSpecial) {
257       // could do this for all kinds, but would explode assembly code size
258       trace_method_handle(_masm, "bad Method*:intrinsic_id");
259     }
260     __ stop("bad Method*::intrinsic_id");
261     __ BIND(L);
262     BLOCK_COMMENT("} verify_intrinsic_id");
263   }
264 
265   // First task:  Find out how big the argument list is.
266   int ref_kind = signature_polymorphic_intrinsic_ref_kind(iid);
267   assert(ref_kind != 0 || iid == vmIntrinsics::_invokeBasic, "must be _invokeBasic or a linkTo intrinsic");
268   if (ref_kind == 0 || MethodHandles::ref_kind_has_receiver(ref_kind)) {
269     __ ld(R7_param_size, in_bytes(Method::const_offset()), R19_method);
270     __ load_sized_value(R7_param_size, in_bytes(ConstMethod::size_of_parameters_offset()), R7_param_size,
271                         sizeof(u2), /*is_signed*/ false);
272     // assert(sizeof(u2) == sizeof(ConstMethod::_size_of_parameters), "");
273   } else {
274     DEBUG_ONLY(R7_param_size = noreg);
275   }
276 
277   Register tmp_mh = noreg;
278   if (!is_signature_polymorphic_static(iid)) {
279     __ ld(tmp_mh = R30_tmp1, __ argument_offset(R7_param_size, R7_param_size, 0), R15_argbase);
280     DEBUG_ONLY(R7_param_size = noreg);
281   }
282 
283   if (log_is_enabled(Info, methodhandles)) {
284     if (tmp_mh != noreg) {
285       __ mr(R23_method_handle, tmp_mh);  // make stub happy
286     }
287     trace_method_handle_interpreter_entry(_masm, iid);
288   }
289 
290   if (iid == vmIntrinsics::_invokeBasic) {
291     generate_method_handle_dispatch(_masm, iid, tmp_mh, noreg, not_for_compiler_entry);
292 
293   } else {
294     // Adjust argument list by popping the trailing MemberName argument.
295     Register tmp_recv = noreg;
296     if (MethodHandles::ref_kind_has_receiver(ref_kind)) {
297       // Load the receiver (not the MH; the actual MemberName's receiver) up from the interpreter stack.
298       __ ld(tmp_recv = R30_tmp1, __ argument_offset(R7_param_size, R7_param_size, 0), R15_argbase);
299       DEBUG_ONLY(R7_param_size = noreg);
300     }
301     Register R19_member = R19_method;  // MemberName ptr; incoming method ptr is dead now
302     __ ld(R19_member, RegisterOrConstant((intptr_t)8), R15_argbase);
303     __ add(R15_argbase, Interpreter::stackElementSize, R15_argbase);
304     generate_method_handle_dispatch(_masm, iid, tmp_recv, R19_member, not_for_compiler_entry);
305   }
306 
307   return entry_point;
308 }
309 
310 void MethodHandles::generate_method_handle_dispatch(MacroAssembler* _masm,
311                                                     vmIntrinsics::ID iid,
312                                                     Register receiver_reg,
313                                                     Register member_reg,
314                                                     bool for_compiler_entry) {
315   assert(is_signature_polymorphic(iid), "expected invoke iid");
316   Register temp1 = (for_compiler_entry ? R25_tmp5 : R31); // must be non-volatile due to runtime calls
317   Register temp2 = (for_compiler_entry ? R22_tmp2 : R8);
318   Register temp3 = (for_compiler_entry ? R23_tmp3 : R9);
319   Register temp4 = (for_compiler_entry ? R24_tmp4 : R10);
320   if (receiver_reg != noreg)  assert_different_registers(temp1, temp2, temp3, temp4, receiver_reg);
321   if (member_reg   != noreg)  assert_different_registers(temp1, temp2, temp3, temp4, member_reg);
322 
323   const MacroAssembler::PreservationLevel preservation_level = for_compiler_entry
324     ? MacroAssembler::PRESERVATION_FRAME_LR_GP_FP_REGS
325     : MacroAssembler::PRESERVATION_FRAME_LR;
326 
327   if (iid == vmIntrinsics::_invokeBasic || iid == vmIntrinsics::_linkToNative) {
328     if (iid == vmIntrinsics::_linkToNative) {
329       assert(for_compiler_entry, "only compiler entry is supported");
330     }
331     // indirect through MH.form.vmentry.vmtarget
332     jump_to_lambda_form(_masm, receiver_reg, R19_method, temp1, temp2, for_compiler_entry);
333   } else {
334     // The method is a member invoker used by direct method handles.
335     if (VerifyMethodHandles) {
336       // make sure the trailing argument really is a MemberName (caller responsibility)
337       verify_klass(_masm, member_reg, VM_CLASS_ID(MemberName_klass),
338                    temp1, temp2,
339                    "MemberName required for invokeVirtual etc.");
340     }
341 
342     Register temp1_recv_klass = temp1;
343     if (iid != vmIntrinsics::_linkToStatic) {
344       BLOCK_COMMENT("check_receiver {");
345       __ verify_oop(receiver_reg, FILE_AND_LINE);
346 
347       const int klass_offset = iid == vmIntrinsics::_linkToSpecial
348         ? -1                                  // enforce receiver null check
349         : oopDesc::klass_offset_in_bytes();   // regular null-checking behavior
350 
351       __ null_check_throw(receiver_reg, klass_offset, temp1, Interpreter::throw_NullPointerException_entry());
352 
353       if (iid != vmIntrinsics::_linkToSpecial || VerifyMethodHandles) {
354         __ load_klass(temp1_recv_klass, receiver_reg);
355         __ verify_klass_ptr(temp1_recv_klass);
356       }
357 
358       if (VerifyMethodHandles && iid != vmIntrinsics::_linkToInterface) {
359         Label L_ok;
360         Register temp2_defc = temp2;
361 
362         __ load_heap_oop(temp2_defc, NONZERO(java_lang_invoke_MemberName::clazz_offset()), member_reg,
363                          temp3, temp4, preservation_level, IS_NOT_NULL);
364         load_klass_from_Class(_masm, temp2_defc, temp3, temp4);
365         __ verify_klass_ptr(temp2_defc);
366         __ check_klass_subtype(temp1_recv_klass, temp2_defc, temp3, temp4, L_ok);
367         // If we get here, the type check failed!
368         __ stop("receiver class disagrees with MemberName.clazz");
369         __ BIND(L_ok);
370       }
371       BLOCK_COMMENT("} check_receiver");
372     }
373     if (iid == vmIntrinsics::_linkToSpecial ||
374         iid == vmIntrinsics::_linkToStatic) {
375       DEBUG_ONLY(temp1_recv_klass = noreg);  // these guys didn't load the recv_klass
376     }
377 
378     // Live registers at this point:
379     //  member_reg - MemberName that was the trailing argument
380     //  temp1_recv_klass - klass of stacked receiver, if needed
381 
382     Label L_incompatible_class_change_error;
383     switch (iid) {
384     case vmIntrinsics::_linkToSpecial:
385       if (VerifyMethodHandles) {
386         verify_ref_kind(_masm, JVM_REF_invokeSpecial, member_reg, temp2);
387       }
388       __ load_heap_oop(R19_method, NONZERO(java_lang_invoke_MemberName::method_offset()), member_reg,
389                        temp3, temp4, preservation_level, IS_NOT_NULL);
390       __ ld(R19_method, NONZERO(java_lang_invoke_ResolvedMethodName::vmtarget_offset()), R19_method);
391       break;
392 
393     case vmIntrinsics::_linkToStatic:
394       if (VerifyMethodHandles) {
395         verify_ref_kind(_masm, JVM_REF_invokeStatic, member_reg, temp2);
396       }
397       __ load_heap_oop(R19_method, NONZERO(java_lang_invoke_MemberName::method_offset()), member_reg,
398                        temp3, temp4, preservation_level, IS_NOT_NULL);
399       __ ld(R19_method, NONZERO(java_lang_invoke_ResolvedMethodName::vmtarget_offset()), R19_method);
400       break;
401 
402     case vmIntrinsics::_linkToVirtual:
403     {
404       // same as TemplateTable::invokevirtual,
405       // minus the CP setup and profiling:
406 
407       if (VerifyMethodHandles) {
408         verify_ref_kind(_masm, JVM_REF_invokeVirtual, member_reg, temp2);
409       }
410 
411       // pick out the vtable index from the MemberName, and then we can discard it:
412       Register temp2_index = temp2;
413       __ ld(temp2_index, NONZERO(java_lang_invoke_MemberName::vmindex_offset()), member_reg);
414 
415       if (VerifyMethodHandles) {
416         Label L_index_ok;
417         __ cmpdi(CCR1, temp2_index, 0);
418         __ bge(CCR1, L_index_ok);
419         __ stop("no virtual index");
420         __ BIND(L_index_ok);
421       }
422 
423       // Note:  The verifier invariants allow us to ignore MemberName.clazz and vmtarget
424       // at this point.  And VerifyMethodHandles has already checked clazz, if needed.
425 
426       // get target Method* & entry point
427       __ lookup_virtual_method(temp1_recv_klass, temp2_index, R19_method);
428       break;
429     }
430 
431     case vmIntrinsics::_linkToInterface:
432     {
433       // same as TemplateTable::invokeinterface
434       // (minus the CP setup and profiling, with different argument motion)
435       if (VerifyMethodHandles) {
436         verify_ref_kind(_masm, JVM_REF_invokeInterface, member_reg, temp2);
437       }
438 
439       Register temp2_intf = temp2;
440       __ load_heap_oop(temp2_intf, NONZERO(java_lang_invoke_MemberName::clazz_offset()), member_reg,
441                        temp3, temp4, preservation_level, IS_NOT_NULL);
442       load_klass_from_Class(_masm, temp2_intf, temp3, temp4);
443       __ verify_klass_ptr(temp2_intf);
444 
445       Register vtable_index = R19_method;
446       __ ld(vtable_index, NONZERO(java_lang_invoke_MemberName::vmindex_offset()), member_reg);
447       if (VerifyMethodHandles) {
448         Label L_index_ok;
449         __ cmpdi(CCR1, vtable_index, 0);
450         __ bge(CCR1, L_index_ok);
451         __ stop("invalid vtable index for MH.invokeInterface");
452         __ BIND(L_index_ok);
453       }
454 
455       // given intf, index, and recv klass, dispatch to the implementation method
456       __ lookup_interface_method(temp1_recv_klass, temp2_intf,
457                                  // note: next two args must be the same:
458                                  vtable_index, R19_method,
459                                  temp3, temp4,
460                                  L_incompatible_class_change_error);
461       break;
462     }
463 
464     default:
465       fatal("unexpected intrinsic %d: %s", vmIntrinsics::as_int(iid), vmIntrinsics::name_at(iid));
466       break;
467     }
468 
469     // Live at this point:
470     //   R19_method
471 
472     // After figuring out which concrete method to call, jump into it.
473     // Note that this works in the interpreter with no data motion.
474     // But the compiled version will require that rcx_recv be shifted out.
475     __ verify_method_ptr(R19_method);
476     jump_from_method_handle(_masm, R19_method, temp1, temp2, for_compiler_entry);
477 
478     if (iid == vmIntrinsics::_linkToInterface) {
479       __ BIND(L_incompatible_class_change_error);
480       __ load_const_optimized(temp1, StubRoutines::throw_IncompatibleClassChangeError_entry());
481       __ mtctr(temp1);
482       __ bctr();
483     }
484   }
485 }
486 
487 #ifndef PRODUCT
488 void trace_method_handle_stub(const char* adaptername,
489                               oopDesc* mh,
490                               intptr_t* entry_sp,
491                               intptr_t* saved_regs) {
492 
493   bool has_mh = (strstr(adaptername, "/static") == NULL &&
494                  strstr(adaptername, "linkTo") == NULL);    // static linkers don't have MH
495   const char* mh_reg_name = has_mh ? "R23_method_handle" : "G23";
496   log_info(methodhandles)("MH %s %s=" INTPTR_FORMAT " sp=" INTPTR_FORMAT,
497                 adaptername, mh_reg_name, p2i(mh), p2i(entry_sp));
498 
499   LogTarget(Trace, methodhandles) lt;
500   if (lt.is_enabled()) {
501     ResourceMark rm;
502     LogStream ls(lt);
503     ls.print_cr("Registers:");
504     const int abi_offset = frame::abi_reg_args_size / 8;
505     for (int i = R3->encoding(); i <= R12->encoding(); i++) {
506       Register r = as_Register(i);
507       int count = i - R3->encoding();
508       // The registers are stored in reverse order on the stack (by save_volatile_gprs(R1_SP, abi_reg_args_size)).
509       ls.print("%3s=" PTR_FORMAT, r->name(), saved_regs[abi_offset + count]);
510       if ((count + 1) % 4 == 0) {
511         ls.cr();
512       } else {
513         ls.print(", ");
514       }
515     }
516     ls.cr();
517 
518     {
519       // dumping last frame with frame::describe
520 
521       JavaThread* p = JavaThread::active();
522 
523       // may not be needed by safer and unexpensive here
524       PreserveExceptionMark pem(Thread::current());
525       FrameValues values;
526 
527       // Note: We want to allow trace_method_handle from any call site.
528       // While trace_method_handle creates a frame, it may be entered
529       // without a PC on the stack top (e.g. not just after a call).
530       // Walking that frame could lead to failures due to that invalid PC.
531       // => carefully detect that frame when doing the stack walking
532 
533       // Current C frame
534       frame cur_frame = os::current_frame();
535 
536       // Robust search of trace_calling_frame (independant of inlining).
537       assert(cur_frame.sp() <= saved_regs, "registers not saved on stack ?");
538       frame trace_calling_frame = os::get_sender_for_C_frame(&cur_frame);
539       while (trace_calling_frame.fp() < saved_regs) {
540         trace_calling_frame = os::get_sender_for_C_frame(&trace_calling_frame);
541       }
542 
543       // Safely create a frame and call frame::describe.
544       intptr_t *dump_sp = trace_calling_frame.sender_sp();
545 
546       frame dump_frame = frame(dump_sp);
547       dump_frame.describe(values, 1);
548 
549       values.describe(-1, saved_regs, "raw top of stack");
550 
551       ls.print_cr("Stack layout:");
552       values.print_on(p, &ls);
553     }
554 
555     if (has_mh && oopDesc::is_oop(mh)) {
556       mh->print_on(&ls);
557       if (java_lang_invoke_MethodHandle::is_instance(mh)) {
558         java_lang_invoke_MethodHandle::form(mh)->print_on(&ls);
559       }
560     }
561   }
562 }
563 
564 void MethodHandles::trace_method_handle(MacroAssembler* _masm, const char* adaptername) {
565   if (!log_is_enabled(Info, methodhandles)) return;
566 
567   BLOCK_COMMENT("trace_method_handle {");
568 
569   const Register tmp = R11; // Will be preserved.
570   const int nbytes_save = MacroAssembler::num_volatile_regs * 8;
571   __ save_volatile_gprs(R1_SP, -nbytes_save); // except R0
572   __ save_LR_CR(tmp); // save in old frame
573 
574   __ mr(R5_ARG3, R1_SP);     // saved_sp
575   __ push_frame_reg_args(nbytes_save, tmp);
576 
577   __ load_const_optimized(R3_ARG1, (address)adaptername, tmp);
578   __ mr(R4_ARG2, R23_method_handle);
579   __ mr(R6_ARG4, R1_SP);
580   __ call_VM_leaf(CAST_FROM_FN_PTR(address, trace_method_handle_stub));
581 
582   __ pop_frame();
583   __ restore_LR_CR(tmp);
584   __ restore_volatile_gprs(R1_SP, -nbytes_save); // except R0
585 
586   BLOCK_COMMENT("} trace_method_handle");
587 }
588 #endif // PRODUCT