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.
constant(int value)53 static RegisterOrConstant constant(int value) {
54   return RegisterOrConstant(value);
55 }
56 
load_klass_from_Class(MacroAssembler * _masm,Register klass_reg,Register temp_reg,Register temp2_reg)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
check_nonzero(const char * xname,int x)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
verify_klass(MacroAssembler * _masm,Register obj_reg,vmClassID klass_id,Register temp_reg,Register temp2_reg,const char * error_message)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 
verify_ref_kind(MacroAssembler * _masm,int ref_kind,Register member_reg,Register temp)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 
jump_from_method_handle(MacroAssembler * _masm,Register method,Register target,Register temp,bool for_compiler_entry)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 
jump_to_lambda_form(MacroAssembler * _masm,Register recv,Register method_temp,Register temp2,Register temp3,bool for_compiler_entry)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
generate_method_handle_interpreter_entry(MacroAssembler * _masm,vmIntrinsics::ID iid)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 
generate_method_handle_dispatch(MacroAssembler * _masm,vmIntrinsics::ID iid,Register receiver_reg,Register member_reg,bool for_compiler_entry)351 void MethodHandles::generate_method_handle_dispatch(MacroAssembler* _masm,
352                                                     vmIntrinsics::ID iid,
353                                                     Register receiver_reg,
354                                                     Register member_reg,
355                                                     bool for_compiler_entry) {
356   assert(is_signature_polymorphic(iid), "expected invoke iid");
357 
358   Register temp1 = for_compiler_entry ? Z_R10 : Z_R6;
359   Register temp2 = Z_R12;
360   Register temp3 = Z_R11;
361   Register temp4 = Z_R13;
362 
363   if (for_compiler_entry) {
364     assert(receiver_reg == (iid == vmIntrinsics::_linkToStatic ? noreg : Z_ARG1),
365            "only valid assignment");
366   }
367   if (receiver_reg != noreg) {
368     assert_different_registers(temp1, temp2, temp3, temp4, receiver_reg);
369   }
370   if (member_reg != noreg) {
371     assert_different_registers(temp1, temp2, temp3, temp4, member_reg);
372   }
373   if (!for_compiler_entry) {  // Don't trash last SP.
374     assert_different_registers(temp1, temp2, temp3, temp4, Z_R10);
375   }
376 
377   if (iid == vmIntrinsics::_invokeBasic || iid == vmIntrinsics::_linkToNative) {
378     if (iid == vmIntrinsics::_linkToNative) {
379       assert(for_compiler_entry, "only compiler entry is supported");
380     }
381     __ pc(); // Just for the block comment.
382     // Indirect through MH.form.vmentry.vmtarget.
383     jump_to_lambda_form(_masm, receiver_reg, Z_method, Z_R1, temp3, for_compiler_entry);
384     return;
385   }
386 
387   // The method is a member invoker used by direct method handles.
388   if (VerifyMethodHandles) {
389     // Make sure the trailing argument really is a MemberName (caller responsibility).
390     verify_klass(_masm, member_reg,
391                  VM_CLASS_ID(MemberName_klass),
392                  temp1, temp2,
393                  "MemberName required for invokeVirtual etc.");
394   }
395 
396   Address  member_clazz(   member_reg, NONZERO(java_lang_invoke_MemberName::clazz_offset()));
397   Address  member_vmindex( member_reg, NONZERO(java_lang_invoke_MemberName::vmindex_offset()));
398   Address  member_vmtarget(member_reg, NONZERO(java_lang_invoke_MemberName::method_offset()));
399   Address  vmtarget_method(Z_method, NONZERO(java_lang_invoke_ResolvedMethodName::vmtarget_offset()));
400   Register temp1_recv_klass = temp1;
401 
402   if (iid != vmIntrinsics::_linkToStatic) {
403     __ verify_oop(receiver_reg, FILE_AND_LINE);
404     if (iid == vmIntrinsics::_linkToSpecial) {
405       // Don't actually load the klass; just null-check the receiver.
406       __ null_check(receiver_reg);
407     } else {
408       // Load receiver klass itself.
409       __ null_check(receiver_reg, Z_R0, oopDesc::klass_offset_in_bytes());
410       __ load_klass(temp1_recv_klass, receiver_reg);
411       __ verify_klass_ptr(temp1_recv_klass);
412     }
413     BLOCK_COMMENT("check_receiver {");
414     // The receiver for the MemberName must be in receiver_reg.
415     // Check the receiver against the MemberName.clazz.
416     if (VerifyMethodHandles && iid == vmIntrinsics::_linkToSpecial) {
417       // Did not load it above...
418       __ load_klass(temp1_recv_klass, receiver_reg);
419       __ verify_klass_ptr(temp1_recv_klass);
420     }
421 
422     if (VerifyMethodHandles && iid != vmIntrinsics::_linkToInterface) {
423       NearLabel L_ok;
424       Register temp2_defc = temp2;
425 
426       __ load_heap_oop(temp2_defc, member_clazz,
427                        noreg, noreg, IS_NOT_NULL);
428       load_klass_from_Class(_masm, temp2_defc, temp3, temp4);
429       __ verify_klass_ptr(temp2_defc);
430       __ check_klass_subtype(temp1_recv_klass, temp2_defc, temp3, temp4, L_ok);
431       // If we get here, the type check failed!
432       __ stop("receiver class disagrees with MemberName.clazz");
433       __ bind(L_ok);
434     }
435     BLOCK_COMMENT("} check_receiver");
436   }
437   if (iid == vmIntrinsics::_linkToSpecial || iid == vmIntrinsics::_linkToStatic) {
438     DEBUG_ONLY(temp1_recv_klass = noreg);  // These guys didn't load the recv_klass.
439   }
440 
441   // Live registers at this point:
442   //   member_reg       - MemberName that was the trailing argument.
443   //   temp1_recv_klass - Klass of stacked receiver, if needed.
444   //   Z_R10            - Interpreter linkage if interpreted.
445 
446   bool method_is_live = false;
447 
448   switch (iid) {
449     case vmIntrinsics::_linkToSpecial:
450       if (VerifyMethodHandles) {
451         verify_ref_kind(_masm, JVM_REF_invokeSpecial, member_reg, temp3);
452       }
453       __ load_heap_oop(Z_method, member_vmtarget,
454                        noreg, noreg, IS_NOT_NULL);
455       __ z_lg(Z_method, vmtarget_method);
456       method_is_live = true;
457       break;
458 
459     case vmIntrinsics::_linkToStatic:
460       if (VerifyMethodHandles) {
461         verify_ref_kind(_masm, JVM_REF_invokeStatic, member_reg, temp3);
462       }
463       __ load_heap_oop(Z_method, member_vmtarget,
464                        noreg, noreg, IS_NOT_NULL);
465       __ z_lg(Z_method, vmtarget_method);
466       method_is_live = true;
467       break;
468 
469     case vmIntrinsics::_linkToVirtual: {
470       // Same as TemplateTable::invokevirtual, minus the CP setup and profiling.
471       if (VerifyMethodHandles) {
472         verify_ref_kind(_masm, JVM_REF_invokeVirtual, member_reg, temp3);
473       }
474 
475       // Pick out the vtable index from the MemberName, and then we can discard it.
476       Register temp2_index = temp2;
477       __ z_lg(temp2_index, member_vmindex);
478 
479       if (VerifyMethodHandles) {
480         // if (member_vmindex < 0) stop
481         NearLabel L_index_ok;
482         __ compare32_and_branch(temp2_index, constant(0), Assembler::bcondNotLow, L_index_ok);
483         __ stop("no virtual index");
484         __ BIND(L_index_ok);
485       }
486 
487       // Note: The verifier invariants allow us to ignore MemberName.clazz and vmtarget
488       // at this point. And VerifyMethodHandles has already checked clazz, if needed.
489 
490       // Get target method and entry point.
491       __ lookup_virtual_method(temp1_recv_klass, temp2_index, Z_method);
492       method_is_live = true;
493       break;
494     }
495 
496     case vmIntrinsics::_linkToInterface: {
497       // Same as TemplateTable::invokeinterface, minus the CP setup
498       // and profiling, with different argument motion.
499       if (VerifyMethodHandles) {
500         verify_ref_kind(_masm, JVM_REF_invokeInterface, member_reg, temp3);
501       }
502 
503       Register temp3_intf = temp3;
504 
505       __ load_heap_oop(temp3_intf, member_clazz,
506                        noreg, noreg, IS_NOT_NULL);
507       load_klass_from_Class(_masm, temp3_intf, temp2, temp4);
508 
509       Register Z_index = Z_method;
510 
511       __ z_lg(Z_index, member_vmindex);
512 
513       if (VerifyMethodHandles) {
514         NearLabel L;
515         // if (member_vmindex < 0) stop
516         __ compare32_and_branch(Z_index, constant(0), Assembler::bcondNotLow, L);
517         __ stop("invalid vtable index for MH.invokeInterface");
518         __ bind(L);
519       }
520 
521       // Given interface, index, and recv klass, dispatch to the implementation method.
522       Label L_no_such_interface;
523       __ lookup_interface_method(temp1_recv_klass, temp3_intf,
524                                  // Note: next two args must be the same:
525                                  Z_index, Z_method, temp2,
526                                  L_no_such_interface);
527       jump_from_method_handle(_masm, Z_method, temp2, Z_R0, for_compiler_entry);
528 
529       __ bind(L_no_such_interface);
530 
531       // Throw exception.
532       __ load_const_optimized(Z_R1, StubRoutines::throw_IncompatibleClassChangeError_entry());
533       __ z_br(Z_R1);
534       break;
535     }
536 
537     default:
538       fatal("unexpected intrinsic %d: %s", vmIntrinsics::as_int(iid), vmIntrinsics::name_at(iid));
539       break;
540   }
541 
542   if (method_is_live) {
543     // Live at this point: Z_method, O5_savedSP (if interpreted).
544 
545     // After figuring out which concrete method to call, jump into it.
546     // Note that this works in the interpreter with no data motion.
547     // But the compiled version will require that rcx_recv be shifted out.
548     jump_from_method_handle(_masm, Z_method, temp1, Z_R0, for_compiler_entry);
549   }
550 }
551 
552 #ifndef PRODUCT
trace_method_handle_stub(const char * adaptername,oopDesc * mh,intptr_t * sender_sp,intptr_t * args,intptr_t * tracing_fp)553 void trace_method_handle_stub(const char* adaptername,
554                               oopDesc* mh,
555                               intptr_t* sender_sp,
556                               intptr_t* args,
557                               intptr_t* tracing_fp) {
558   bool has_mh = (strstr(adaptername, "/static") == NULL &&
559                  strstr(adaptername, "linkTo") == NULL);    // Static linkers don't have MH.
560   const char* mh_reg_name = has_mh ? "Z_R4_mh" : "Z_R4";
561   log_info(methodhandles)("MH %s %s=" INTPTR_FORMAT " sender_sp=" INTPTR_FORMAT " args=" INTPTR_FORMAT,
562                           adaptername, mh_reg_name,
563                           p2i(mh), p2i(sender_sp), p2i(args));
564 
565   LogTarget(Trace, methodhandles) lt;
566   if (lt.is_enabled()) {
567     // Dumping last frame with frame::describe.
568     ResourceMark rm;
569     LogStream ls(lt);
570     JavaThread* p = JavaThread::active();
571 
572     // may not be needed by safer and unexpensive here
573     PreserveExceptionMark pem(Thread::current());
574     FrameValues values;
575 
576     // Note: We want to allow trace_method_handle from any call site.
577     // While trace_method_handle creates a frame, it may be entered
578     // without a valid return PC in Z_R14 (e.g. not just after a call).
579     // Walking that frame could lead to failures due to that invalid PC.
580     // => carefully detect that frame when doing the stack walking.
581 
582     // Walk up to the right frame using the "tracing_fp" argument.
583     frame cur_frame = os::current_frame(); // Current C frame.
584 
585     while (cur_frame.fp() != tracing_fp) {
586       cur_frame = os::get_sender_for_C_frame(&cur_frame);
587     }
588 
589     // Safely create a frame and call frame::describe.
590     intptr_t *dump_sp = cur_frame.sender_sp();
591     intptr_t *dump_fp = cur_frame.link();
592 
593     bool walkable = has_mh; // Whether the traced frame shoud be walkable.
594 
595     // The sender for cur_frame is the caller of trace_method_handle.
596     if (walkable) {
597       // The previous definition of walkable may have to be refined
598       // if new call sites cause the next frame constructor to start
599       // failing. Alternatively, frame constructors could be
600       // modified to support the current or future non walkable
601       // frames (but this is more intrusive and is not considered as
602       // part of this RFE, which will instead use a simpler output).
603       frame dump_frame = frame(dump_sp);
604       dump_frame.describe(values, 1);
605     } else {
606       // Robust dump for frames which cannot be constructed from sp/younger_sp
607       // Add descriptions without building a Java frame to avoid issues.
608       values.describe(-1, dump_fp, "fp for #1 <not parsed, cannot trust pc>");
609       values.describe(-1, dump_sp, "sp");
610     }
611 
612     bool has_args = has_mh; // Whether Z_esp is meaningful.
613 
614     // Mark args, if seems valid (may not be valid for some adapters).
615     if (has_args) {
616       if ((args >= dump_sp) && (args < dump_fp)) {
617         values.describe(-1, args, "*Z_esp");
618       }
619     }
620 
621     // Note: the unextended_sp may not be correct.
622     ls.print_cr("  stack layout:");
623     values.print_on(p, &ls);
624     if (has_mh && oopDesc::is_oop(mh)) {
625       mh->print_on(&ls);
626       if (java_lang_invoke_MethodHandle::is_instance(mh)) {
627         java_lang_invoke_MethodHandle::form(mh)->print_on(&ls);
628       }
629     }
630   }
631 }
632 
trace_method_handle(MacroAssembler * _masm,const char * adaptername)633 void MethodHandles::trace_method_handle(MacroAssembler* _masm, const char* adaptername) {
634   if (!log_is_enabled(Info, methodhandles)) { return; }
635 
636   // If arg registers are contiguous, we can use STMG/LMG.
637   assert((Z_ARG5->encoding() - Z_ARG1->encoding() + 1) == RegisterImpl::number_of_arg_registers, "Oops");
638 
639   BLOCK_COMMENT("trace_method_handle {");
640 
641   // Save argument registers (they are used in raise exception stub).
642   // Argument registers have contiguous register numbers -> we can use stmg/lmg.
643   __ z_stmg(Z_ARG1, Z_ARG5, 16, Z_SP);
644 
645   // Setup arguments.
646   __ z_lgr(Z_ARG2, Z_ARG4); // mh, see generate_method_handle_interpreter_entry()
647   __ z_lgr(Z_ARG3, Z_R10);  // sender_sp
648   __ z_lgr(Z_ARG4, Z_esp);
649   __ load_const_optimized(Z_ARG1, (void *)adaptername);
650   __ z_lgr(Z_ARG5, Z_SP);   // tracing_fp
651   __ save_return_pc();      // saves Z_R14
652   __ push_frame_abi160(0);
653   __ call_VM_leaf(CAST_FROM_FN_PTR(address, trace_method_handle_stub));
654   __ pop_frame();
655   __ restore_return_pc();   // restores to Z_R14
656 
657   // Restore argument registers
658   __ z_lmg(Z_ARG1, Z_ARG5, 16, Z_SP);
659   __ zap_from_to(Z_SP, Z_SP, Z_R0, Z_R1, 50, -1);
660   __ zap_from_to(Z_SP, Z_SP, Z_R0, Z_R1, -1, 5);
661 
662   BLOCK_COMMENT("} trace_method_handle");
663 }
664 #endif // !PRODUCT
665