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