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