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   __ verify_oop(recv);
 136   __ load_heap_oop(method_temp, Address(recv, NONZERO(java_lang_invoke_MethodHandle::form_offset_in_bytes())));
 137   __ verify_oop(method_temp);
 138   __ load_heap_oop(method_temp, Address(method_temp, NONZERO(java_lang_invoke_LambdaForm::vmentry_offset_in_bytes())));
 139   __ verify_oop(method_temp);
 140   // the following assumes that a Method* is normally compressed in the vmtarget field:
 141   __ ldr(method_temp, Address(method_temp, NONZERO(java_lang_invoke_MemberName::vmtarget_offset_in_bytes())));
 142 
 143   if (VerifyMethodHandles && !for_compiler_entry) {
 144     // make sure recv is already on stack
 145     __ ldr(temp2, Address(method_temp, Method::const_offset()));
 146     __ load_sized_value(temp2,
 147                         Address(temp2, ConstMethod::size_of_parameters_offset()),
 148                         sizeof(u2), /*is_signed*/ false);
 149     // assert(sizeof(u2) == sizeof(Method::_size_of_parameters), "");
 150     Label L;
 151     __ ldr(rscratch1, __ argument_address(temp2, -1));
 152     __ cmp(recv, rscratch1);
 153     __ br(Assembler::EQ, L);
 154     __ ldr(r0, __ argument_address(temp2, -1));
 155     __ hlt(0);
 156     __ BIND(L);
 157   }
 158 
 159   jump_from_method_handle(_masm, method_temp, temp2, for_compiler_entry);
 160   BLOCK_COMMENT("} jump_to_lambda_form");
 161 }
 162 
 163 // Code generation
 164 address MethodHandles::generate_method_handle_interpreter_entry(MacroAssembler* _masm,
 165                                                                 vmIntrinsics::ID iid) {
 166   const bool not_for_compiler_entry = false;  // this is the interpreter entry
 167   assert(is_signature_polymorphic(iid), "expected invoke iid");
 168   if (iid == vmIntrinsics::_invokeGeneric ||
 169       iid == vmIntrinsics::_compiledLambdaForm) {
 170     // Perhaps surprisingly, the symbolic references visible to Java are not directly used.
 171     // They are linked to Java-generated adapters via MethodHandleNatives.linkMethod.
 172     // They all allow an appendix argument.
 173     __ hlt(0);           // empty stubs make SG sick
 174     return NULL;
 175   }
 176 
 177   // r13: sender SP (must preserve; see prepare_to_jump_from_interpreted)
 178   // rmethod: Method*
 179   // r3: argument locator (parameter slot count, added to rsp)
 180   // r1: used as temp to hold mh or receiver
 181   // r0, r11: garbage temps, blown away
 182   Register argp   = r3;   // argument list ptr, live on error paths
 183   Register temp   = r0;
 184   Register mh     = r1;   // MH receiver; dies quickly and is recycled
 185 
 186   // here's where control starts out:
 187   __ align(CodeEntryAlignment);
 188   address entry_point = __ pc();
 189 
 190   if (VerifyMethodHandles) {
 191     Label L;
 192     BLOCK_COMMENT("verify_intrinsic_id {");
 193     __ ldrb(rscratch1, Address(rmethod, Method::intrinsic_id_offset_in_bytes()));
 194     __ cmp(rscratch1, (int) iid);
 195     __ br(Assembler::EQ, L);
 196     if (iid == vmIntrinsics::_linkToVirtual ||
 197         iid == vmIntrinsics::_linkToSpecial) {
 198       // could do this for all kinds, but would explode assembly code size
 199       trace_method_handle(_masm, "bad Method*::intrinsic_id");
 200     }
 201     __ hlt(0);
 202     __ bind(L);
 203     BLOCK_COMMENT("} verify_intrinsic_id");
 204   }
 205 
 206   // First task:  Find out how big the argument list is.
 207   Address r3_first_arg_addr;
 208   int ref_kind = signature_polymorphic_intrinsic_ref_kind(iid);
 209   assert(ref_kind != 0 || iid == vmIntrinsics::_invokeBasic, "must be _invokeBasic or a linkTo intrinsic");
 210   if (ref_kind == 0 || MethodHandles::ref_kind_has_receiver(ref_kind)) {
 211     __ ldr(argp, Address(rmethod, Method::const_offset()));
 212     __ load_sized_value(argp,
 213                         Address(argp, ConstMethod::size_of_parameters_offset()),
 214                         sizeof(u2), /*is_signed*/ false);
 215     // assert(sizeof(u2) == sizeof(Method::_size_of_parameters), "");
 216     r3_first_arg_addr = __ argument_address(argp, -1);
 217   } else {
 218     DEBUG_ONLY(argp = noreg);
 219   }
 220 
 221   if (!is_signature_polymorphic_static(iid)) {
 222     __ ldr(mh, r3_first_arg_addr);
 223     DEBUG_ONLY(argp = noreg);
 224   }
 225 
 226   // r3_first_arg_addr is live!
 227 
 228   trace_method_handle_interpreter_entry(_masm, iid);
 229   if (iid == vmIntrinsics::_invokeBasic) {
 230     generate_method_handle_dispatch(_masm, iid, mh, noreg, not_for_compiler_entry);
 231 
 232   } else {
 233     // Adjust argument list by popping the trailing MemberName argument.
 234     Register recv = noreg;
 235     if (MethodHandles::ref_kind_has_receiver(ref_kind)) {
 236       // Load the receiver (not the MH; the actual MemberName's receiver) up from the interpreter stack.
 237       __ ldr(recv = r2, r3_first_arg_addr);
 238     }
 239     DEBUG_ONLY(argp = noreg);
 240     Register rmember = rmethod;  // MemberName ptr; incoming method ptr is dead now
 241     __ pop(rmember);             // extract last argument
 242     generate_method_handle_dispatch(_masm, iid, recv, rmember, not_for_compiler_entry);
 243   }
 244 
 245   return entry_point;
 246 }
 247 
 248 
 249 void MethodHandles::generate_method_handle_dispatch(MacroAssembler* _masm,
 250                                                     vmIntrinsics::ID iid,
 251                                                     Register receiver_reg,
 252                                                     Register member_reg,
 253                                                     bool for_compiler_entry) {
 254   assert(is_signature_polymorphic(iid), "expected invoke iid");
 255   // temps used in this code are not used in *either* compiled or interpreted calling sequences
 256   Register temp1 = r10;
 257   Register temp2 = r11;
 258   Register temp3 = r14;  // r13 is live by this point: it contains the sender SP
 259   if (for_compiler_entry) {
 260     assert(receiver_reg == (iid == vmIntrinsics::_linkToStatic ? noreg : j_rarg0), "only valid assignment");
 261     assert_different_registers(temp1,        j_rarg0, j_rarg1, j_rarg2, j_rarg3, j_rarg4, j_rarg5, j_rarg6, j_rarg7);
 262     assert_different_registers(temp2,        j_rarg0, j_rarg1, j_rarg2, j_rarg3, j_rarg4, j_rarg5, j_rarg6, j_rarg7);
 263     assert_different_registers(temp3,        j_rarg0, j_rarg1, j_rarg2, j_rarg3, j_rarg4, j_rarg5, j_rarg6, j_rarg7);
 264   }
 265 
 266   assert_different_registers(temp1, temp2, temp3, receiver_reg);
 267   assert_different_registers(temp1, temp2, temp3, member_reg);
 268 
 269   if (iid == vmIntrinsics::_invokeBasic) {
 270     // indirect through MH.form.vmentry.vmtarget
 271     jump_to_lambda_form(_masm, receiver_reg, rmethod, temp1, for_compiler_entry);
 272 
 273   } else {
 274     // The method is a member invoker used by direct method handles.
 275     if (VerifyMethodHandles) {
 276       // make sure the trailing argument really is a MemberName (caller responsibility)
 277       verify_klass(_masm, member_reg, SystemDictionary::WK_KLASS_ENUM_NAME(java_lang_invoke_MemberName),
 278                    "MemberName required for invokeVirtual etc.");
 279     }
 280 
 281     Address member_clazz(    member_reg, NONZERO(java_lang_invoke_MemberName::clazz_offset_in_bytes()));
 282     Address member_vmindex(  member_reg, NONZERO(java_lang_invoke_MemberName::vmindex_offset_in_bytes()));
 283     Address member_vmtarget( member_reg, NONZERO(java_lang_invoke_MemberName::vmtarget_offset_in_bytes()));
 284 
 285     Register temp1_recv_klass = temp1;
 286     if (iid != vmIntrinsics::_linkToStatic) {
 287       __ verify_oop(receiver_reg);
 288       if (iid == vmIntrinsics::_linkToSpecial) {
 289         // Don't actually load the klass; just null-check the receiver.
 290         __ null_check(receiver_reg);
 291       } else {
 292         // load receiver klass itself
 293         __ null_check(receiver_reg, oopDesc::klass_offset_in_bytes());
 294         __ load_klass(temp1_recv_klass, receiver_reg);
 295         __ verify_klass_ptr(temp1_recv_klass);
 296       }
 297       BLOCK_COMMENT("check_receiver {");
 298       // The receiver for the MemberName must be in receiver_reg.
 299       // Check the receiver against the MemberName.clazz
 300       if (VerifyMethodHandles && iid == vmIntrinsics::_linkToSpecial) {
 301         // Did not load it above...
 302         __ load_klass(temp1_recv_klass, receiver_reg);
 303         __ verify_klass_ptr(temp1_recv_klass);
 304       }
 305       if (VerifyMethodHandles && iid != vmIntrinsics::_linkToInterface) {
 306         Label L_ok;
 307         Register temp2_defc = temp2;
 308         __ load_heap_oop(temp2_defc, member_clazz);
 309         load_klass_from_Class(_masm, temp2_defc);
 310         __ verify_klass_ptr(temp2_defc);
 311         __ check_klass_subtype(temp1_recv_klass, temp2_defc, temp3, L_ok);
 312         // If we get here, the type check failed!
 313         __ hlt(0);
 314         // __ STOP("receiver class disagrees with MemberName.clazz");
 315         __ bind(L_ok);
 316       }
 317       BLOCK_COMMENT("} check_receiver");
 318     }
 319     if (iid == vmIntrinsics::_linkToSpecial ||
 320         iid == vmIntrinsics::_linkToStatic) {
 321       DEBUG_ONLY(temp1_recv_klass = noreg);  // these guys didn't load the recv_klass
 322     }
 323 
 324     // Live registers at this point:
 325     //  member_reg - MemberName that was the trailing argument
 326     //  temp1_recv_klass - klass of stacked receiver, if needed
 327     //  r13 - interpreter linkage (if interpreted)  ??? FIXME
 328     //  r1 ... r0 - compiler arguments (if compiled)
 329 
 330     Label L_incompatible_class_change_error;
 331     switch (iid) {
 332     case vmIntrinsics::_linkToSpecial:
 333       if (VerifyMethodHandles) {
 334         verify_ref_kind(_masm, JVM_REF_invokeSpecial, member_reg, temp3);
 335       }
 336       __ ldr(rmethod, member_vmtarget);
 337       break;
 338 
 339     case vmIntrinsics::_linkToStatic:
 340       if (VerifyMethodHandles) {
 341         verify_ref_kind(_masm, JVM_REF_invokeStatic, member_reg, temp3);
 342       }
 343       __ ldr(rmethod, member_vmtarget);
 344       break;
 345 
 346     case vmIntrinsics::_linkToVirtual:
 347     {
 348       // same as TemplateTable::invokevirtual,
 349       // minus the CP setup and profiling:
 350 
 351       if (VerifyMethodHandles) {
 352         verify_ref_kind(_masm, JVM_REF_invokeVirtual, member_reg, temp3);
 353       }
 354 
 355       // pick out the vtable index from the MemberName, and then we can discard it:
 356       Register temp2_index = temp2;
 357       __ ldr(temp2_index, member_vmindex);
 358 
 359       if (VerifyMethodHandles) {
 360         Label L_index_ok;
 361         __ cmpw(temp2_index, 0U);
 362         __ br(Assembler::GE, L_index_ok);
 363         __ hlt(0);
 364         __ BIND(L_index_ok);
 365       }
 366 
 367       // Note:  The verifier invariants allow us to ignore MemberName.clazz and vmtarget
 368       // at this point.  And VerifyMethodHandles has already checked clazz, if needed.
 369 
 370       // get target Method* & entry point
 371       __ lookup_virtual_method(temp1_recv_klass, temp2_index, rmethod);
 372       break;
 373     }
 374 
 375     case vmIntrinsics::_linkToInterface:
 376     {
 377       // same as TemplateTable::invokeinterface
 378       // (minus the CP setup and profiling, with different argument motion)
 379       if (VerifyMethodHandles) {
 380         verify_ref_kind(_masm, JVM_REF_invokeInterface, member_reg, temp3);
 381       }
 382 
 383       Register temp3_intf = temp3;
 384       __ load_heap_oop(temp3_intf, member_clazz);
 385       load_klass_from_Class(_masm, temp3_intf);
 386       __ verify_klass_ptr(temp3_intf);
 387 
 388       Register rindex = rmethod;
 389       __ ldr(rindex, member_vmindex);
 390       if (VerifyMethodHandles) {
 391         Label L;
 392         __ cmpw(rindex, 0U);
 393         __ br(Assembler::GE, L);
 394         __ hlt(0);
 395         __ bind(L);
 396       }
 397 
 398       // given intf, index, and recv klass, dispatch to the implementation method
 399       __ lookup_interface_method(temp1_recv_klass, temp3_intf,
 400                                  // note: next two args must be the same:
 401                                  rindex, rmethod,
 402                                  temp2,
 403                                  L_incompatible_class_change_error);
 404       break;
 405     }
 406 
 407     default:
 408       fatal(err_msg_res("unexpected intrinsic %d: %s", iid, vmIntrinsics::name_at(iid)));
 409       break;
 410     }
 411 
 412     // live at this point:  rmethod, r13 (if interpreted)
 413 
 414     // After figuring out which concrete method to call, jump into it.
 415     // Note that this works in the interpreter with no data motion.
 416     // But the compiled version will require that r2_recv be shifted out.
 417     __ verify_method_ptr(rmethod);
 418     jump_from_method_handle(_masm, rmethod, temp1, for_compiler_entry);
 419     if (iid == vmIntrinsics::_linkToInterface) {
 420       __ bind(L_incompatible_class_change_error);
 421       __ far_jump(RuntimeAddress(StubRoutines::throw_IncompatibleClassChangeError_entry()));
 422     }
 423   }
 424 }
 425 
 426 #ifndef PRODUCT
 427 void trace_method_handle_stub(const char* adaptername,
 428                               oop mh,
 429                               intptr_t* saved_regs,
 430                               intptr_t* entry_sp) {  }
 431 
 432 // The stub wraps the arguments in a struct on the stack to avoid
 433 // dealing with the different calling conventions for passing 6
 434 // arguments.
 435 struct MethodHandleStubArguments {
 436   const char* adaptername;
 437   oopDesc* mh;
 438   intptr_t* saved_regs;
 439   intptr_t* entry_sp;
 440 };
 441 void trace_method_handle_stub_wrapper(MethodHandleStubArguments* args) {  }
 442 
 443 void MethodHandles::trace_method_handle(MacroAssembler* _masm, const char* adaptername) {  }
 444 #endif //PRODUCT