1 /*
  2  * Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2014, Red Hat Inc. All rights reserved.
  4  * Copyright (c) 2020, 2021, Huawei Technologies Co., Ltd. All rights reserved.
  5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  6  *
  7  * This code is free software; you can redistribute it and/or modify it
  8  * under the terms of the GNU General Public License version 2 only, as
  9  * published by the Free Software Foundation.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  *
 25  */
 26 
 27 #include "precompiled.hpp"
 28 #include "asm/macroAssembler.hpp"
 29 #include "classfile/javaClasses.inline.hpp"
 30 #include "classfile/vmClasses.hpp"
 31 #include "interpreter/interpreter.hpp"
 32 #include "interpreter/interpreterRuntime.hpp"
 33 #include "memory/allocation.inline.hpp"
 34 #include "prims/jvmtiExport.hpp"
 35 #include "prims/methodHandles.hpp"
 36 #include "runtime/flags/flagSetting.hpp"
 37 #include "runtime/frame.inline.hpp"
 38 #include "runtime/stubRoutines.hpp"
 39 
 40 #define __ _masm->
 41 
 42 #ifdef PRODUCT
 43 #define BLOCK_COMMENT(str) /* nothing */
 44 #else
 45 #define BLOCK_COMMENT(str) __ block_comment(str)
 46 #endif
 47 
 48 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
 49 
 50 void MethodHandles::load_klass_from_Class(MacroAssembler* _masm, Register klass_reg) {
 51   assert_cond(_masm != NULL);
 52   if (VerifyMethodHandles) {
 53     verify_klass(_masm, klass_reg, VM_CLASS_ID(java_lang_Class),
 54                  "MH argument is a Class");
 55   }
 56   __ ld(klass_reg, Address(klass_reg, java_lang_Class::klass_offset()));
 57 }
 58 
 59 #ifdef ASSERT
 60 static int check_nonzero(const char* xname, int x) {
 61   assert(x != 0, "%s should be nonzero", xname);
 62   return x;
 63 }
 64 #define NONZERO(x) check_nonzero(#x, x)
 65 #else //ASSERT
 66 #define NONZERO(x) (x)
 67 #endif //PRODUCT
 68 
 69 #ifdef ASSERT
 70 void MethodHandles::verify_klass(MacroAssembler* _masm,
 71                                  Register obj, vmClassID klass_id,
 72                                  const char* error_message) {
 73   assert_cond(_masm != NULL);
 74   InstanceKlass** klass_addr = vmClasses::klass_addr_at(klass_id);
 75   Klass* klass = vmClasses::klass_at(klass_id);
 76   Register temp = t1;
 77   Register temp2 = t0; // used by MacroAssembler::cmpptr
 78   Label L_ok, L_bad;
 79   BLOCK_COMMENT("verify_klass {");
 80   __ verify_oop(obj);
 81   __ beqz(obj, L_bad);
 82   __ push_reg(RegSet::of(temp, temp2), sp);
 83   __ load_klass(temp, obj);
 84   __ cmpptr(temp, ExternalAddress((address) klass_addr), L_ok);
 85   intptr_t super_check_offset = klass->super_check_offset();
 86   __ ld(temp, Address(temp, super_check_offset));
 87   __ cmpptr(temp, ExternalAddress((address) klass_addr), L_ok);
 88   __ pop_reg(RegSet::of(temp, temp2), sp);
 89   __ bind(L_bad);
 90   __ stop(error_message);
 91   __ BIND(L_ok);
 92   __ pop_reg(RegSet::of(temp, temp2), sp);
 93   BLOCK_COMMENT("} verify_klass");
 94 }
 95 
 96 void MethodHandles::verify_ref_kind(MacroAssembler* _masm, int ref_kind, Register member_reg, Register temp) {  }
 97 
 98 #endif //ASSERT
 99 
100 void MethodHandles::jump_from_method_handle(MacroAssembler* _masm, Register method, Register temp,
101                                             bool for_compiler_entry) {
102   assert_cond(_masm != NULL);
103   assert(method == xmethod, "interpreter calling convention");
104   Label L_no_such_method;
105   __ beqz(xmethod, L_no_such_method);
106   __ verify_method_ptr(method);
107 
108   if (!for_compiler_entry && JvmtiExport::can_post_interpreter_events()) {
109     Label run_compiled_code;
110     // JVMTI events, such as single-stepping, are implemented partly by avoiding running
111     // compiled code in threads for which the event is enabled.  Check here for
112     // interp_only_mode if these events CAN be enabled.
113 
114     __ lwu(t0, Address(xthread, JavaThread::interp_only_mode_offset()));
115     __ beqz(t0, run_compiled_code);
116     __ ld(t0, Address(method, Method::interpreter_entry_offset()));
117     __ jr(t0);
118     __ BIND(run_compiled_code);
119   }
120 
121   const ByteSize entry_offset = for_compiler_entry ? Method::from_compiled_offset() :
122                                                      Method::from_interpreted_offset();
123   __ ld(t0,Address(method, entry_offset));
124   __ jr(t0);
125   __ bind(L_no_such_method);
126   __ far_jump(RuntimeAddress(StubRoutines::throw_AbstractMethodError_entry()));
127 }
128 
129 void MethodHandles::jump_to_lambda_form(MacroAssembler* _masm,
130                                         Register recv, Register method_temp,
131                                         Register temp2,
132                                         bool for_compiler_entry) {
133   assert_cond(_masm != NULL);
134   BLOCK_COMMENT("jump_to_lambda_form {");
135   // This is the initial entry point of a lazy method handle.
136   // After type checking, it picks up the invoker from the LambdaForm.
137   assert_different_registers(recv, method_temp, temp2);
138   assert(recv != noreg, "required register");
139   assert(method_temp == xmethod, "required register for loading method");
140 
141   // Load the invoker, as MH -> MH.form -> LF.vmentry
142   __ verify_oop(recv);
143   __ load_heap_oop(method_temp, Address(recv, NONZERO(java_lang_invoke_MethodHandle::form_offset())), temp2);
144   __ verify_oop(method_temp);
145   __ load_heap_oop(method_temp, Address(method_temp, NONZERO(java_lang_invoke_LambdaForm::vmentry_offset())), temp2);
146   __ verify_oop(method_temp);
147   __ load_heap_oop(method_temp, Address(method_temp, NONZERO(java_lang_invoke_MemberName::method_offset())), temp2);
148   __ verify_oop(method_temp);
149   __ access_load_at(T_ADDRESS, IN_HEAP, method_temp, Address(method_temp, NONZERO(java_lang_invoke_ResolvedMethodName::vmtarget_offset())), noreg, noreg);
150 
151   if (VerifyMethodHandles && !for_compiler_entry) {
152     // make sure recv is already on stack
153     __ ld(temp2, Address(method_temp, Method::const_offset()));
154     __ load_sized_value(temp2,
155                         Address(temp2, ConstMethod::size_of_parameters_offset()),
156                         sizeof(u2), /*is_signed*/ false);
157     Label L;
158     __ ld(t0, __ argument_address(temp2, -1));
159     __ beq(recv, t0, L);
160     __ ld(x10, __ argument_address(temp2, -1));
161     __ ebreak();
162     __ BIND(L);
163   }
164 
165   jump_from_method_handle(_masm, method_temp, temp2, for_compiler_entry);
166   BLOCK_COMMENT("} jump_to_lambda_form");
167 }
168 
169 // Code generation
170 address MethodHandles::generate_method_handle_interpreter_entry(MacroAssembler* _masm,
171                                                                 vmIntrinsics::ID iid) {
172   assert_cond(_masm != NULL);
173   const bool not_for_compiler_entry = false;  // this is the interpreter entry
174   assert(is_signature_polymorphic(iid), "expected invoke iid");
175   if (iid == vmIntrinsics::_invokeGeneric ||
176       iid == vmIntrinsics::_compiledLambdaForm) {
177     // Perhaps surprisingly, the symbolic references visible to Java are not directly used.
178     // They are linked to Java-generated adapters via MethodHandleNatives.linkMethod.
179     // They all allow an appendix argument.
180     __ ebreak();           // empty stubs make SG sick
181     return NULL;
182   }
183 
184   // No need in interpreter entry for linkToNative for now.
185   // Interpreter calls compiled entry through i2c.
186   if (iid == vmIntrinsics::_linkToNative) {
187     __ ebreak();
188     return NULL;
189   }
190 
191   // x30: sender SP (must preserve; see prepare_to_jump_from_interpreted)
192   // xmethod: Method*
193   // x13: argument locator (parameter slot count, added to rsp)
194   // x11: used as temp to hold mh or receiver
195   // x10, x29: garbage temps, blown away
196   Register argp   = x13;   // argument list ptr, live on error paths
197   Register mh     = x11;   // MH receiver; dies quickly and is recycled
198 
199   // here's where control starts out:
200   __ align(CodeEntryAlignment);
201   address entry_point = __ pc();
202 
203   if (VerifyMethodHandles) {
204     assert(Method::intrinsic_id_size_in_bytes() == 2, "assuming Method::_intrinsic_id is u2");
205 
206     Label L;
207     BLOCK_COMMENT("verify_intrinsic_id {");
208     __ lhu(t0, Address(xmethod, Method::intrinsic_id_offset_in_bytes()));
209     __ mv(t1, (int) iid);
210     __ beq(t0, t1, L);
211     if (iid == vmIntrinsics::_linkToVirtual ||
212         iid == vmIntrinsics::_linkToSpecial) {
213       // could do this for all kinds, but would explode assembly code size
214       trace_method_handle(_masm, "bad Method*::intrinsic_id");
215     }
216     __ ebreak();
217     __ bind(L);
218     BLOCK_COMMENT("} verify_intrinsic_id");
219   }
220 
221   // First task:  Find out how big the argument list is.
222   Address x13_first_arg_addr;
223   int ref_kind = signature_polymorphic_intrinsic_ref_kind(iid);
224   assert(ref_kind != 0 || iid == vmIntrinsics::_invokeBasic, "must be _invokeBasic or a linkTo intrinsic");
225   if (ref_kind == 0 || MethodHandles::ref_kind_has_receiver(ref_kind)) {
226     __ ld(argp, Address(xmethod, Method::const_offset()));
227     __ load_sized_value(argp,
228                         Address(argp, ConstMethod::size_of_parameters_offset()),
229                         sizeof(u2), /*is_signed*/ false);
230     x13_first_arg_addr = __ argument_address(argp, -1);
231   } else {
232     DEBUG_ONLY(argp = noreg);
233   }
234 
235   if (!is_signature_polymorphic_static(iid)) {
236     __ ld(mh, x13_first_arg_addr);
237     DEBUG_ONLY(argp = noreg);
238   }
239 
240   // x13_first_arg_addr is live!
241 
242   trace_method_handle_interpreter_entry(_masm, iid);
243   if (iid == vmIntrinsics::_invokeBasic) {
244     generate_method_handle_dispatch(_masm, iid, mh, noreg, not_for_compiler_entry);
245 
246   } else {
247     // Adjust argument list by popping the trailing MemberName argument.
248     Register recv = noreg;
249     if (MethodHandles::ref_kind_has_receiver(ref_kind)) {
250       // Load the receiver (not the MH; the actual MemberName's receiver) up from the interpreter stack.
251       __ ld(recv = x12, x13_first_arg_addr);
252     }
253     DEBUG_ONLY(argp = noreg);
254     Register xmember = xmethod;  // MemberName ptr; incoming method ptr is dead now
255     __ pop_reg(xmember);             // extract last argument
256     generate_method_handle_dispatch(_masm, iid, recv, xmember, not_for_compiler_entry);
257   }
258 
259   return entry_point;
260 }
261 
262 
263 void MethodHandles::generate_method_handle_dispatch(MacroAssembler* _masm,
264                                                     vmIntrinsics::ID iid,
265                                                     Register receiver_reg,
266                                                     Register member_reg,
267                                                     bool for_compiler_entry) {
268   assert_cond(_masm != NULL);
269   assert(is_signature_polymorphic(iid), "expected invoke iid");
270   // temps used in this code are not used in *either* compiled or interpreted calling sequences
271   Register temp1 = x7;
272   Register temp2 = x28;
273   Register temp3 = x29;  // x30 is live by this point: it contains the sender SP
274   if (for_compiler_entry) {
275     assert(receiver_reg == (iid == vmIntrinsics::_linkToStatic ? noreg : j_rarg0), "only valid assignment");
276     assert_different_registers(temp1, j_rarg0, j_rarg1, j_rarg2, j_rarg3, j_rarg4, j_rarg5, j_rarg6, j_rarg7);
277     assert_different_registers(temp2, j_rarg0, j_rarg1, j_rarg2, j_rarg3, j_rarg4, j_rarg5, j_rarg6, j_rarg7);
278     assert_different_registers(temp3, j_rarg0, j_rarg1, j_rarg2, j_rarg3, j_rarg4, j_rarg5, j_rarg6, j_rarg7);
279   }
280 
281   assert_different_registers(temp1, temp2, temp3, receiver_reg);
282   assert_different_registers(temp1, temp2, temp3, member_reg);
283 
284   if (iid == vmIntrinsics::_invokeBasic || iid == vmIntrinsics::_linkToNative) {
285     if (iid == vmIntrinsics::_linkToNative) {
286       assert(for_compiler_entry, "only compiler entry is supported");
287     }
288     // indirect through MH.form.vmentry.vmtarget
289     jump_to_lambda_form(_masm, receiver_reg, xmethod, temp1, for_compiler_entry);
290   } else {
291     // The method is a member invoker used by direct method handles.
292     if (VerifyMethodHandles) {
293       // make sure the trailing argument really is a MemberName (caller responsibility)
294       verify_klass(_masm, member_reg, VM_CLASS_ID(java_lang_invoke_MemberName),
295                    "MemberName required for invokeVirtual etc.");
296     }
297 
298     Address member_clazz(    member_reg, NONZERO(java_lang_invoke_MemberName::clazz_offset()));
299     Address member_vmindex(  member_reg, NONZERO(java_lang_invoke_MemberName::vmindex_offset()));
300     Address member_vmtarget( member_reg, NONZERO(java_lang_invoke_MemberName::method_offset()));
301     Address vmtarget_method( xmethod, NONZERO(java_lang_invoke_ResolvedMethodName::vmtarget_offset()));
302 
303     Register temp1_recv_klass = temp1;
304     if (iid != vmIntrinsics::_linkToStatic) {
305       __ verify_oop(receiver_reg);
306       if (iid == vmIntrinsics::_linkToSpecial) {
307         // Don't actually load the klass; just null-check the receiver.
308         __ null_check(receiver_reg);
309       } else {
310         // load receiver klass itself
311         __ null_check(receiver_reg, oopDesc::klass_offset_in_bytes());
312         __ load_klass(temp1_recv_klass, receiver_reg);
313         __ verify_klass_ptr(temp1_recv_klass);
314       }
315       BLOCK_COMMENT("check_receiver {");
316       // The receiver for the MemberName must be in receiver_reg.
317       // Check the receiver against the MemberName.clazz
318       if (VerifyMethodHandles && iid == vmIntrinsics::_linkToSpecial) {
319         // Did not load it above...
320         __ load_klass(temp1_recv_klass, receiver_reg);
321         __ verify_klass_ptr(temp1_recv_klass);
322       }
323       if (VerifyMethodHandles && iid != vmIntrinsics::_linkToInterface) {
324         Label L_ok;
325         Register temp2_defc = temp2;
326         __ load_heap_oop(temp2_defc, member_clazz, temp3);
327         load_klass_from_Class(_masm, temp2_defc);
328         __ verify_klass_ptr(temp2_defc);
329         __ check_klass_subtype(temp1_recv_klass, temp2_defc, temp3, L_ok);
330         // If we get here, the type check failed!
331         __ ebreak();
332         __ bind(L_ok);
333       }
334       BLOCK_COMMENT("} check_receiver");
335     }
336     if (iid == vmIntrinsics::_linkToSpecial ||
337         iid == vmIntrinsics::_linkToStatic) {
338       DEBUG_ONLY(temp1_recv_klass = noreg);  // these guys didn't load the recv_klass
339     }
340 
341     // Live registers at this point:
342     //  member_reg - MemberName that was the trailing argument
343     //  temp1_recv_klass - klass of stacked receiver, if needed
344     //  x30 - interpreter linkage (if interpreted)
345     //  x11 ... x10 - compiler arguments (if compiled)
346 
347     Label L_incompatible_class_change_error;
348     switch (iid) {
349       case vmIntrinsics::_linkToSpecial:
350         if (VerifyMethodHandles) {
351           verify_ref_kind(_masm, JVM_REF_invokeSpecial, member_reg, temp3);
352         }
353         __ load_heap_oop(xmethod, member_vmtarget);
354         __ access_load_at(T_ADDRESS, IN_HEAP, xmethod, vmtarget_method, noreg, noreg);
355         break;
356 
357       case vmIntrinsics::_linkToStatic:
358         if (VerifyMethodHandles) {
359           verify_ref_kind(_masm, JVM_REF_invokeStatic, member_reg, temp3);
360         }
361         __ load_heap_oop(xmethod, member_vmtarget);
362         __ access_load_at(T_ADDRESS, IN_HEAP, xmethod, vmtarget_method, noreg, noreg);
363         break;
364 
365       case vmIntrinsics::_linkToVirtual:
366       {
367         // same as TemplateTable::invokevirtual,
368         // minus the CP setup and profiling:
369 
370         if (VerifyMethodHandles) {
371           verify_ref_kind(_masm, JVM_REF_invokeVirtual, member_reg, temp3);
372         }
373 
374         // pick out the vtable index from the MemberName, and then we can discard it:
375         Register temp2_index = temp2;
376         __ access_load_at(T_ADDRESS, IN_HEAP, temp2_index, member_vmindex, noreg, noreg);
377 
378         if (VerifyMethodHandles) {
379           Label L_index_ok;
380           __ bgez(temp2_index, L_index_ok);
381           __ ebreak();
382           __ BIND(L_index_ok);
383         }
384 
385         // Note:  The verifier invariants allow us to ignore MemberName.clazz and vmtarget
386         // at this point.  And VerifyMethodHandles has already checked clazz, if needed.
387 
388         // get target Method* & entry point
389         __ lookup_virtual_method(temp1_recv_klass, temp2_index, xmethod);
390         break;
391       }
392 
393       case vmIntrinsics::_linkToInterface:
394       {
395         // same as TemplateTable::invokeinterface
396         // (minus the CP setup and profiling, with different argument motion)
397         if (VerifyMethodHandles) {
398           verify_ref_kind(_masm, JVM_REF_invokeInterface, member_reg, temp3);
399         }
400 
401         Register temp3_intf = temp3;
402         __ load_heap_oop(temp3_intf, member_clazz);
403         load_klass_from_Class(_masm, temp3_intf);
404         __ verify_klass_ptr(temp3_intf);
405 
406         Register rindex = xmethod;
407         __ access_load_at(T_ADDRESS, IN_HEAP, rindex, member_vmindex, noreg, noreg);
408         if (VerifyMethodHandles) {
409           Label L;
410           __ bgez(rindex, L);
411           __ ebreak();
412           __ bind(L);
413         }
414 
415         // given intf, index, and recv klass, dispatch to the implementation method
416         __ lookup_interface_method(temp1_recv_klass, temp3_intf,
417                                    // note: next two args must be the same:
418                                    rindex, xmethod,
419                                    temp2,
420                                    L_incompatible_class_change_error);
421         break;
422       }
423 
424       default:
425         fatal("unexpected intrinsic %d: %s", vmIntrinsics::as_int(iid), vmIntrinsics::name_at(iid));
426         break;
427     }
428 
429     // live at this point:  xmethod, x30 (if interpreted)
430 
431     // After figuring out which concrete method to call, jump into it.
432     // Note that this works in the interpreter with no data motion.
433     // But the compiled version will require that r2_recv be shifted out.
434     __ verify_method_ptr(xmethod);
435     jump_from_method_handle(_masm, xmethod, temp1, for_compiler_entry);
436     if (iid == vmIntrinsics::_linkToInterface) {
437       __ bind(L_incompatible_class_change_error);
438       __ far_jump(RuntimeAddress(StubRoutines::throw_IncompatibleClassChangeError_entry()));
439     }
440   }
441 
442 }
443 
444 #ifndef PRODUCT
445 void trace_method_handle_stub(const char* adaptername,
446                               oopDesc* mh,
447                               intptr_t* saved_regs,
448                               intptr_t* entry_sp) {  }
449 
450 // The stub wraps the arguments in a struct on the stack to avoid
451 // dealing with the different calling conventions for passing 6
452 // arguments.
453 struct MethodHandleStubArguments {
454   const char* adaptername;
455   oopDesc* mh;
456   intptr_t* saved_regs;
457   intptr_t* entry_sp;
458 };
459 void trace_method_handle_stub_wrapper(MethodHandleStubArguments* args) {  }
460 
461 void MethodHandles::trace_method_handle(MacroAssembler* _masm, const char* adaptername) {  }
462 #endif //PRODUCT