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