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