1 /* 2 * Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "asm/macroAssembler.hpp" 26 #include "classfile/vmClasses.hpp" 27 #include "compiler/disassembler.hpp" 28 #include "classfile/javaClasses.inline.hpp" 29 #include "interpreter/interpreter.hpp" 30 #include "interpreter/interpreterRuntime.hpp" 31 #include "jvm.h" 32 #include "logging/log.hpp" 33 #include "logging/logStream.hpp" 34 #include "memory/allocation.inline.hpp" 35 #include "memory/resourceArea.hpp" 36 #include "prims/jvmtiExport.hpp" 37 #include "prims/methodHandles.hpp" 38 #include "runtime/flags/flagSetting.hpp" 39 #include "runtime/frame.inline.hpp" 40 #include "runtime/stubRoutines.hpp" 41 #include "utilities/formatBuffer.hpp" 42 #include "utilities/preserveException.hpp" 43 44 #define __ Disassembler::hook<MacroAssembler>(__FILE__, __LINE__, _masm)-> 45 46 #ifdef PRODUCT 47 #define BLOCK_COMMENT(str) /* nothing */ 48 #define STOP(error) stop(error) 49 #else 50 #define BLOCK_COMMENT(str) __ block_comment(str) 51 #define STOP(error) block_comment(error); __ stop(error) 52 #endif 53 54 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":") 55 56 void MethodHandles::load_klass_from_Class(MacroAssembler* _masm, Register klass_reg) { 57 if (VerifyMethodHandles) 58 verify_klass(_masm, klass_reg, VM_CLASS_ID(java_lang_Class), 59 "MH argument is a Class"); 60 __ movptr(klass_reg, Address(klass_reg, java_lang_Class::klass_offset())); 61 } 62 63 #ifdef ASSERT 64 static int check_nonzero(const char* xname, int x) { 65 assert(x != 0, "%s should be nonzero", xname); 66 return x; 67 } 68 #define NONZERO(x) check_nonzero(#x, x) 69 #else //ASSERT 70 #define NONZERO(x) (x) 71 #endif //ASSERT 72 73 #ifdef ASSERT 74 void MethodHandles::verify_klass(MacroAssembler* _masm, 75 Register obj, vmClassID klass_id, 76 const char* error_message) { 77 InstanceKlass** klass_addr = vmClasses::klass_addr_at(klass_id); 78 Klass* klass = vmClasses::klass_at(klass_id); 79 Register temp = rdi; 80 Label L_ok, L_bad; 81 BLOCK_COMMENT("verify_klass {"); 82 __ verify_oop(obj); 83 __ testptr(obj, obj); 84 __ jcc(Assembler::zero, L_bad); 85 #define PUSH { __ push(temp); __ push(rscratch1); } 86 #define POP { __ pop(rscratch1); __ pop(temp); } 87 PUSH; 88 __ load_klass(temp, obj, rscratch1); 89 __ cmpptr(temp, ExternalAddress((address) klass_addr), rscratch1); 90 __ jcc(Assembler::equal, L_ok); 91 intptr_t super_check_offset = klass->super_check_offset(); 92 __ movptr(temp, Address(temp, super_check_offset)); 93 __ cmpptr(temp, ExternalAddress((address) klass_addr), rscratch1); 94 __ jcc(Assembler::equal, L_ok); 95 POP; 96 __ bind(L_bad); 97 __ STOP(error_message); 98 __ BIND(L_ok); 99 POP; 100 BLOCK_COMMENT("} verify_klass"); 101 #undef POP 102 #undef PUSH 103 } 104 105 void MethodHandles::verify_ref_kind(MacroAssembler* _masm, int ref_kind, Register member_reg, Register temp) { 106 Label L; 107 BLOCK_COMMENT("verify_ref_kind {"); 108 __ movl(temp, Address(member_reg, NONZERO(java_lang_invoke_MemberName::flags_offset()))); 109 __ shrl(temp, java_lang_invoke_MemberName::MN_REFERENCE_KIND_SHIFT); 110 __ andl(temp, java_lang_invoke_MemberName::MN_REFERENCE_KIND_MASK); 111 __ cmpl(temp, ref_kind); 112 __ jcc(Assembler::equal, L); 113 { //char* buf = NEW_C_HEAP_ARRAY(char, 100, mtInternal); 114 //jio_snprintf(buf, 100, "verify_ref_kind expected %x", ref_kind); 115 const char* buf = "verify_ref_kind"; // avoid allocating new strings 116 if (ref_kind == JVM_REF_invokeVirtual || 117 ref_kind == JVM_REF_invokeSpecial) { 118 // could do this for all ref_kinds, but would explode assembly code size 119 trace_method_handle(_masm, buf); 120 } 121 __ STOP(buf); 122 } 123 BLOCK_COMMENT("} verify_ref_kind"); 124 __ bind(L); 125 } 126 127 void MethodHandles::verify_method(MacroAssembler* _masm, Register method, Register temp, vmIntrinsics::ID iid) { 128 BLOCK_COMMENT("verify_method {"); 129 __ verify_method_ptr(method); 130 if (VerifyMethodHandles) { 131 Label L_ok; 132 assert_different_registers(method, temp); 133 134 const Register method_holder = temp; 135 __ load_method_holder(method_holder, method); 136 __ push(method_holder); // keep holder around for diagnostic purposes 137 138 switch (iid) { 139 case vmIntrinsicID::_invokeBasic: 140 // Require compiled LambdaForm class to be fully initialized. 141 __ cmpb(Address(method_holder, InstanceKlass::init_state_offset()), InstanceKlass::fully_initialized); 142 __ jccb(Assembler::equal, L_ok); 143 break; 144 145 case vmIntrinsicID::_linkToStatic: 146 __ clinit_barrier(method_holder, &L_ok); 147 break; 148 149 case vmIntrinsicID::_linkToVirtual: 150 case vmIntrinsicID::_linkToSpecial: 151 case vmIntrinsicID::_linkToInterface: 152 // Class initialization check is too strong here. Just ensure that initialization has been initiated. 153 __ cmpb(Address(method_holder, InstanceKlass::init_state_offset()), InstanceKlass::being_initialized); 154 __ jcc(Assembler::greaterEqual, L_ok); 155 156 // init_state check failed, but it may be an abstract interface method 157 __ load_unsigned_short(temp, Address(method, Method::access_flags_offset())); 158 __ testl(temp, JVM_ACC_ABSTRACT); 159 __ jccb(Assembler::notZero, L_ok); 160 break; 161 162 default: 163 fatal("unexpected intrinsic %d: %s", vmIntrinsics::as_int(iid), vmIntrinsics::name_at(iid)); 164 } 165 166 // clinit check failed for a concrete method 167 __ STOP("Method holder klass is not initialized"); 168 169 __ BIND(L_ok); 170 __ pop(method_holder); // restore stack layout 171 } 172 BLOCK_COMMENT("} verify_method"); 173 } 174 #endif // ASSERT 175 176 void MethodHandles::jump_from_method_handle(MacroAssembler* _masm, Register method, Register temp, 177 bool for_compiler_entry, vmIntrinsics::ID iid) { 178 assert(method == rbx, "interpreter calling convention"); 179 180 Label L_no_such_method; 181 __ testptr(rbx, rbx); 182 __ jcc(Assembler::zero, L_no_such_method); 183 184 verify_method(_masm, method, temp, iid); 185 186 if (!for_compiler_entry && JvmtiExport::can_post_interpreter_events()) { 187 Label run_compiled_code; 188 // JVMTI events, such as single-stepping, are implemented partly by avoiding running 189 // compiled code in threads for which the event is enabled. Check here for 190 // interp_only_mode if these events CAN be enabled. 191 // interp_only is an int, on little endian it is sufficient to test the byte only 192 // Is a cmpl faster? 193 __ cmpb(Address(r15_thread, JavaThread::interp_only_mode_offset()), 0); 194 __ jccb(Assembler::zero, run_compiled_code); 195 __ jmp(Address(method, Method::interpreter_entry_offset())); 196 __ BIND(run_compiled_code); 197 } 198 199 const ByteSize entry_offset = for_compiler_entry ? Method::from_compiled_offset() : 200 Method::from_interpreted_offset(); 201 __ jmp(Address(method, entry_offset)); 202 203 __ bind(L_no_such_method); 204 __ jump(RuntimeAddress(SharedRuntime::throw_AbstractMethodError_entry())); 205 } 206 207 void MethodHandles::jump_to_lambda_form(MacroAssembler* _masm, 208 Register recv, Register method_temp, 209 Register temp2, 210 bool for_compiler_entry) { 211 BLOCK_COMMENT("jump_to_lambda_form {"); 212 // This is the initial entry point of a lazy method handle. 213 // After type checking, it picks up the invoker from the LambdaForm. 214 assert_different_registers(recv, method_temp, temp2); 215 assert(recv != noreg, "required register"); 216 assert(method_temp == rbx, "required register for loading method"); 217 218 // Load the invoker, as MH -> MH.form -> LF.vmentry 219 __ verify_oop(recv); 220 __ load_heap_oop(method_temp, Address(recv, NONZERO(java_lang_invoke_MethodHandle::form_offset())), temp2); 221 __ verify_oop(method_temp); 222 __ load_heap_oop(method_temp, Address(method_temp, NONZERO(java_lang_invoke_LambdaForm::vmentry_offset())), temp2); 223 __ verify_oop(method_temp); 224 __ load_heap_oop(method_temp, Address(method_temp, NONZERO(java_lang_invoke_MemberName::method_offset())), temp2); 225 __ verify_oop(method_temp); 226 __ access_load_at(T_ADDRESS, IN_HEAP, method_temp, 227 Address(method_temp, NONZERO(java_lang_invoke_ResolvedMethodName::vmtarget_offset())), 228 noreg); 229 230 if (VerifyMethodHandles && !for_compiler_entry) { 231 // make sure recv is already on stack 232 __ movptr(temp2, Address(method_temp, Method::const_offset())); 233 __ load_sized_value(temp2, 234 Address(temp2, ConstMethod::size_of_parameters_offset()), 235 sizeof(u2), /*is_signed*/ false); 236 // assert(sizeof(u2) == sizeof(Method::_size_of_parameters), ""); 237 Label L; 238 __ cmpoop(recv, __ argument_address(temp2, -1)); 239 __ jcc(Assembler::equal, L); 240 __ movptr(rax, __ argument_address(temp2, -1)); 241 __ STOP("receiver not on stack"); 242 __ BIND(L); 243 } 244 245 jump_from_method_handle(_masm, method_temp, temp2, for_compiler_entry, vmIntrinsics::_invokeBasic); 246 BLOCK_COMMENT("} jump_to_lambda_form"); 247 } 248 249 void MethodHandles::jump_to_native_invoker(MacroAssembler* _masm, Register nep_reg, Register temp_target) { 250 BLOCK_COMMENT("jump_to_native_invoker {"); 251 assert_different_registers(nep_reg, temp_target); 252 assert(nep_reg != noreg, "required register"); 253 254 // Load the invoker, as NEP -> .invoker 255 __ verify_oop(nep_reg); 256 __ access_load_at(T_ADDRESS, IN_HEAP, temp_target, 257 Address(nep_reg, NONZERO(jdk_internal_foreign_abi_NativeEntryPoint::downcall_stub_address_offset_in_bytes())), 258 noreg); 259 260 __ jmp(temp_target); 261 BLOCK_COMMENT("} jump_to_native_invoker"); 262 } 263 264 265 // Code generation 266 address MethodHandles::generate_method_handle_interpreter_entry(MacroAssembler* _masm, 267 vmIntrinsics::ID iid) { 268 const bool not_for_compiler_entry = false; // this is the interpreter entry 269 assert(is_signature_polymorphic(iid), "expected invoke iid"); 270 if (iid == vmIntrinsics::_invokeGeneric || 271 iid == vmIntrinsics::_compiledLambdaForm) { 272 // Perhaps surprisingly, the symbolic references visible to Java are not directly used. 273 // They are linked to Java-generated adapters via MethodHandleNatives.linkMethod. 274 // They all allow an appendix argument. 275 __ hlt(); // empty stubs make SG sick 276 return nullptr; 277 } 278 279 // No need in interpreter entry for linkToNative for now. 280 // Interpreter calls compiled entry through i2c. 281 if (iid == vmIntrinsics::_linkToNative) { 282 __ hlt(); 283 return nullptr; 284 } 285 286 // rsi/r13: sender SP (must preserve; see prepare_to_jump_from_interpreted) 287 // rbx: Method* 288 // rdx: argument locator (parameter slot count, added to rsp) 289 // rcx: used as temp to hold mh or receiver 290 // rax, rdi: garbage temps, blown away 291 Register rdx_argp = rdx; // argument list ptr, live on error paths 292 Register rax_temp = rax; 293 Register rcx_mh = rcx; // MH receiver; dies quickly and is recycled 294 Register rbx_method = rbx; // eventual target of this invocation 295 296 // here's where control starts out: 297 __ align(CodeEntryAlignment); 298 address entry_point = __ pc(); 299 300 if (VerifyMethodHandles) { 301 assert(Method::intrinsic_id_size_in_bytes() == 2, "assuming Method::_intrinsic_id is u2"); 302 303 Label L; 304 BLOCK_COMMENT("verify_intrinsic_id {"); 305 __ cmpw(Address(rbx_method, Method::intrinsic_id_offset()), (int) iid); 306 __ jcc(Assembler::equal, L); 307 if (iid == vmIntrinsics::_linkToVirtual || 308 iid == vmIntrinsics::_linkToSpecial) { 309 // could do this for all kinds, but would explode assembly code size 310 trace_method_handle(_masm, "bad Method*::intrinsic_id"); 311 } 312 __ STOP("bad Method*::intrinsic_id"); 313 __ bind(L); 314 BLOCK_COMMENT("} verify_intrinsic_id"); 315 } 316 317 // First task: Find out how big the argument list is. 318 Address rdx_first_arg_addr; 319 int ref_kind = signature_polymorphic_intrinsic_ref_kind(iid); 320 assert(ref_kind != 0 || iid == vmIntrinsics::_invokeBasic, "must be _invokeBasic or a linkTo intrinsic"); 321 if (ref_kind == 0 || MethodHandles::ref_kind_has_receiver(ref_kind)) { 322 __ movptr(rdx_argp, Address(rbx_method, Method::const_offset())); 323 __ load_sized_value(rdx_argp, 324 Address(rdx_argp, ConstMethod::size_of_parameters_offset()), 325 sizeof(u2), /*is_signed*/ false); 326 // assert(sizeof(u2) == sizeof(Method::_size_of_parameters), ""); 327 rdx_first_arg_addr = __ argument_address(rdx_argp, -1); 328 } else { 329 DEBUG_ONLY(rdx_argp = noreg); 330 } 331 332 if (!is_signature_polymorphic_static(iid)) { 333 __ movptr(rcx_mh, rdx_first_arg_addr); 334 DEBUG_ONLY(rdx_argp = noreg); 335 } 336 337 // rdx_first_arg_addr is live! 338 339 trace_method_handle_interpreter_entry(_masm, iid); 340 341 if (iid == vmIntrinsics::_invokeBasic) { 342 generate_method_handle_dispatch(_masm, iid, rcx_mh, noreg, not_for_compiler_entry); 343 344 } else { 345 // Adjust argument list by popping the trailing MemberName argument. 346 Register rcx_recv = noreg; 347 if (MethodHandles::ref_kind_has_receiver(ref_kind)) { 348 // Load the receiver (not the MH; the actual MemberName's receiver) up from the interpreter stack. 349 __ movptr(rcx_recv = rcx, rdx_first_arg_addr); 350 } 351 DEBUG_ONLY(rdx_argp = noreg); 352 Register rbx_member = rbx_method; // MemberName ptr; incoming method ptr is dead now 353 __ pop(rax_temp); // return address 354 __ pop(rbx_member); // extract last argument 355 __ push(rax_temp); // re-push return address 356 generate_method_handle_dispatch(_masm, iid, rcx_recv, rbx_member, not_for_compiler_entry); 357 } 358 359 return entry_point; 360 } 361 362 void MethodHandles::generate_method_handle_dispatch(MacroAssembler* _masm, 363 vmIntrinsics::ID iid, 364 Register receiver_reg, 365 Register member_reg, 366 bool for_compiler_entry) { 367 assert(is_signature_polymorphic(iid), "expected invoke iid"); 368 Register rbx_method = rbx; // eventual target of this invocation 369 // temps used in this code are not used in *either* compiled or interpreted calling sequences 370 Register temp1 = rscratch1; 371 Register temp2 = rscratch2; 372 Register temp3 = rax; 373 if (for_compiler_entry) { 374 assert(receiver_reg == (iid == vmIntrinsics::_linkToStatic || iid == vmIntrinsics::_linkToNative ? noreg : j_rarg0), "only valid assignment"); 375 assert_different_registers(temp1, j_rarg0, j_rarg1, j_rarg2, j_rarg3, j_rarg4, j_rarg5); 376 assert_different_registers(temp2, j_rarg0, j_rarg1, j_rarg2, j_rarg3, j_rarg4, j_rarg5); 377 assert_different_registers(temp3, j_rarg0, j_rarg1, j_rarg2, j_rarg3, j_rarg4, j_rarg5); 378 } else { 379 assert_different_registers(temp1, temp2, temp3, saved_last_sp_register()); // don't trash lastSP 380 } 381 assert_different_registers(temp1, temp2, temp3, receiver_reg); 382 assert_different_registers(temp1, temp2, temp3, member_reg); 383 384 if (iid == vmIntrinsics::_invokeBasic) { 385 // indirect through MH.form.vmentry.vmtarget 386 jump_to_lambda_form(_masm, receiver_reg, rbx_method, temp1, for_compiler_entry); 387 } else if (iid == vmIntrinsics::_linkToNative) { 388 assert(for_compiler_entry, "only compiler entry is supported"); 389 jump_to_native_invoker(_masm, member_reg, temp1); 390 } else { 391 // The method is a member invoker used by direct method handles. 392 if (VerifyMethodHandles) { 393 // make sure the trailing argument really is a MemberName (caller responsibility) 394 verify_klass(_masm, member_reg, VM_CLASS_ID(java_lang_invoke_MemberName), 395 "MemberName required for invokeVirtual etc."); 396 } 397 398 Address member_clazz( member_reg, NONZERO(java_lang_invoke_MemberName::clazz_offset())); 399 Address member_vmindex( member_reg, NONZERO(java_lang_invoke_MemberName::vmindex_offset())); 400 Address member_vmtarget( member_reg, NONZERO(java_lang_invoke_MemberName::method_offset())); 401 Address vmtarget_method( rbx_method, NONZERO(java_lang_invoke_ResolvedMethodName::vmtarget_offset())); 402 403 Register temp1_recv_klass = temp1; 404 if (iid != vmIntrinsics::_linkToStatic) { 405 __ verify_oop(receiver_reg); 406 if (iid == vmIntrinsics::_linkToSpecial) { 407 // Don't actually load the klass; just null-check the receiver. 408 __ null_check(receiver_reg); 409 } else { 410 // load receiver klass itself 411 __ load_klass(temp1_recv_klass, receiver_reg, temp2); 412 __ verify_klass_ptr(temp1_recv_klass); 413 } 414 BLOCK_COMMENT("check_receiver {"); 415 // The receiver for the MemberName must be in receiver_reg. 416 // Check the receiver against the MemberName.clazz 417 if (VerifyMethodHandles && iid == vmIntrinsics::_linkToSpecial) { 418 // Did not load it above... 419 __ load_klass(temp1_recv_klass, receiver_reg, temp2); 420 __ verify_klass_ptr(temp1_recv_klass); 421 } 422 if (VerifyMethodHandles && iid != vmIntrinsics::_linkToInterface) { 423 Label L_ok; 424 Register temp2_defc = temp2; 425 __ load_heap_oop(temp2_defc, member_clazz, temp3); 426 load_klass_from_Class(_masm, temp2_defc); 427 __ verify_klass_ptr(temp2_defc); 428 __ check_klass_subtype(temp1_recv_klass, temp2_defc, temp3, L_ok); 429 // If we get here, the type check failed! 430 __ STOP("receiver class disagrees with MemberName.clazz"); 431 __ bind(L_ok); 432 } 433 BLOCK_COMMENT("} check_receiver"); 434 } 435 if (iid == vmIntrinsics::_linkToSpecial || 436 iid == vmIntrinsics::_linkToStatic) { 437 DEBUG_ONLY(temp1_recv_klass = noreg); // these guys didn't load the recv_klass 438 } 439 440 // Live registers at this point: 441 // member_reg - MemberName that was the trailing argument 442 // temp1_recv_klass - klass of stacked receiver, if needed 443 // rsi/r13 - interpreter linkage (if interpreted) 444 // rcx, rdx, rsi, rdi, r8 - compiler arguments (if compiled) 445 446 Label L_incompatible_class_change_error; 447 switch (iid) { 448 case vmIntrinsics::_linkToSpecial: 449 if (VerifyMethodHandles) { 450 verify_ref_kind(_masm, JVM_REF_invokeSpecial, member_reg, temp3); 451 } 452 __ load_heap_oop(rbx_method, member_vmtarget); 453 __ access_load_at(T_ADDRESS, IN_HEAP, rbx_method, vmtarget_method, noreg); 454 break; 455 456 case vmIntrinsics::_linkToStatic: 457 if (VerifyMethodHandles) { 458 verify_ref_kind(_masm, JVM_REF_invokeStatic, member_reg, temp3); 459 } 460 __ load_heap_oop(rbx_method, member_vmtarget); 461 __ access_load_at(T_ADDRESS, IN_HEAP, rbx_method, vmtarget_method, noreg); 462 break; 463 464 case vmIntrinsics::_linkToVirtual: 465 { 466 // same as TemplateTable::invokevirtual, 467 // minus the CP setup and profiling: 468 469 if (VerifyMethodHandles) { 470 verify_ref_kind(_masm, JVM_REF_invokeVirtual, member_reg, temp3); 471 } 472 473 // pick out the vtable index from the MemberName, and then we can discard it: 474 Register temp2_index = temp2; 475 __ access_load_at(T_ADDRESS, IN_HEAP, temp2_index, member_vmindex, noreg); 476 477 if (VerifyMethodHandles) { 478 Label L_index_ok; 479 __ cmpl(temp2_index, 0); 480 __ jcc(Assembler::greaterEqual, L_index_ok); 481 __ STOP("no virtual index"); 482 __ BIND(L_index_ok); 483 } 484 485 // Note: The verifier invariants allow us to ignore MemberName.clazz and vmtarget 486 // at this point. And VerifyMethodHandles has already checked clazz, if needed. 487 488 // get target Method* & entry point 489 __ lookup_virtual_method(temp1_recv_klass, temp2_index, rbx_method); 490 break; 491 } 492 493 case vmIntrinsics::_linkToInterface: 494 { 495 // same as TemplateTable::invokeinterface 496 // (minus the CP setup and profiling, with different argument motion) 497 if (VerifyMethodHandles) { 498 verify_ref_kind(_masm, JVM_REF_invokeInterface, member_reg, temp3); 499 } 500 501 Register temp3_intf = temp3; 502 __ load_heap_oop(temp3_intf, member_clazz); 503 load_klass_from_Class(_masm, temp3_intf); 504 __ verify_klass_ptr(temp3_intf); 505 506 Register rbx_index = rbx_method; 507 __ access_load_at(T_ADDRESS, IN_HEAP, rbx_index, member_vmindex, noreg); 508 if (VerifyMethodHandles) { 509 Label L; 510 __ cmpl(rbx_index, 0); 511 __ jcc(Assembler::greaterEqual, L); 512 __ STOP("invalid vtable index for MH.invokeInterface"); 513 __ bind(L); 514 } 515 516 // given intf, index, and recv klass, dispatch to the implementation method 517 __ lookup_interface_method(temp1_recv_klass, temp3_intf, 518 // note: next two args must be the same: 519 rbx_index, rbx_method, 520 temp2, 521 L_incompatible_class_change_error); 522 break; 523 } 524 525 default: 526 fatal("unexpected intrinsic %d: %s", vmIntrinsics::as_int(iid), vmIntrinsics::name_at(iid)); 527 break; 528 } 529 530 // Live at this point: 531 // rbx_method 532 // rsi/r13 (if interpreted) 533 534 // After figuring out which concrete method to call, jump into it. 535 // Note that this works in the interpreter with no data motion. 536 // But the compiled version will require that rcx_recv be shifted out. 537 jump_from_method_handle(_masm, rbx_method, temp1, for_compiler_entry, iid); 538 539 if (iid == vmIntrinsics::_linkToInterface) { 540 __ bind(L_incompatible_class_change_error); 541 __ jump(RuntimeAddress(SharedRuntime::throw_IncompatibleClassChangeError_entry())); 542 } 543 } 544 } 545 546 #ifndef PRODUCT 547 void trace_method_handle_stub(const char* adaptername, 548 oopDesc* mh, 549 intptr_t* saved_regs, 550 intptr_t* entry_sp) { 551 // called as a leaf from native code: do not block the JVM! 552 bool has_mh = (strstr(adaptername, "/static") == nullptr && 553 strstr(adaptername, "linkTo") == nullptr); // static linkers don't have MH 554 const char* mh_reg_name = has_mh ? "rcx_mh" : "rcx"; 555 log_info(methodhandles)("MH %s %s=" PTR_FORMAT " sp=" PTR_FORMAT, adaptername, mh_reg_name, p2i(mh), p2i(entry_sp)); 556 557 LogTarget(Trace, methodhandles) lt; 558 if (lt.is_enabled()) { 559 ResourceMark rm; 560 LogStream ls(lt); 561 ls.print_cr("Registers:"); 562 const int saved_regs_count = Register::number_of_registers; 563 for (int i = 0; i < saved_regs_count; i++) { 564 Register r = as_Register(i); 565 // The registers are stored in reverse order on the stack (by pusha). 566 #ifdef AMD64 567 int num_regs = UseAPX ? 32 : 16; 568 assert(Register::available_gp_registers() == num_regs, "sanity"); 569 if (r == rsp) { 570 // rsp is actually not stored by pusha(), compute the old rsp from saved_regs (rsp after pusha): saved_regs + 16 = old rsp 571 ls.print("%3s=" PTR_FORMAT, r->name(), (intptr_t)(&saved_regs[num_regs])); 572 } else { 573 ls.print("%3s=" PTR_FORMAT, r->name(), saved_regs[((saved_regs_count - 1) - i)]); 574 } 575 #else 576 ls.print("%3s=" PTR_FORMAT, r->name(), saved_regs[((saved_regs_count - 1) - i)]); 577 #endif 578 if ((i + 1) % 4 == 0) { 579 ls.cr(); 580 } else { 581 ls.print(", "); 582 } 583 } 584 ls.cr(); 585 586 // Note: We want to allow trace_method_handle from any call site. 587 // While trace_method_handle creates a frame, it may be entered 588 // without a PC on the stack top (e.g. not just after a call). 589 // Walking that frame could lead to failures due to that invalid PC. 590 // => carefully detect that frame when doing the stack walking 591 592 { 593 // dumping last frame with frame::describe 594 595 JavaThread* p = JavaThread::active(); 596 597 // may not be needed by safer and unexpensive here 598 PreserveExceptionMark pem(Thread::current()); 599 FrameValues values; 600 601 frame cur_frame = os::current_frame(); 602 603 if (cur_frame.fp() != nullptr) { // not walkable 604 605 // Robust search of trace_calling_frame (independent of inlining). 606 // Assumes saved_regs comes from a pusha in the trace_calling_frame. 607 // 608 // We have to start the search from cur_frame, because trace_calling_frame may be it. 609 // It is guaranteed that trace_calling_frame is different from the top frame. 610 // But os::current_frame() does NOT return the top frame: it returns the next frame under it (caller's frame). 611 // (Due to inlining and tail call optimizations, caller's frame doesn't necessarily correspond to the immediate 612 // caller in the source code.) 613 assert(cur_frame.sp() < saved_regs, "registers not saved on stack ?"); 614 frame trace_calling_frame = cur_frame; 615 while (trace_calling_frame.fp() < saved_regs) { 616 assert(trace_calling_frame.cb() == nullptr, "not a C frame"); 617 trace_calling_frame = os::get_sender_for_C_frame(&trace_calling_frame); 618 } 619 assert(trace_calling_frame.sp() < saved_regs, "wrong frame"); 620 621 // safely create a frame and call frame::describe 622 intptr_t *dump_sp = trace_calling_frame.sender_sp(); 623 intptr_t *dump_fp = trace_calling_frame.link(); 624 625 if (has_mh) { 626 // The previous definition of walkable may have to be refined 627 // if new call sites cause the next frame constructor to start 628 // failing. Alternatively, frame constructors could be 629 // modified to support the current or future non walkable 630 // frames (but this is more intrusive and is not considered as 631 // part of this RFE, which will instead use a simpler output). 632 frame dump_frame = frame(dump_sp, dump_fp); 633 dump_frame.describe(values, 1); 634 } else { 635 // Stack may not be walkable (invalid PC above FP): 636 // Add descriptions without building a Java frame to avoid issues 637 values.describe(-1, dump_fp, "fp for #1 <not parsed, cannot trust pc>"); 638 values.describe(-1, dump_sp, "sp for #1"); 639 } 640 } 641 values.describe(-1, entry_sp, "raw top of stack"); 642 643 ls.print_cr("Stack layout:"); 644 values.print_on(p, &ls); 645 } 646 if (has_mh && oopDesc::is_oop(mh)) { 647 mh->print_on(&ls); 648 if (java_lang_invoke_MethodHandle::is_instance(mh)) { 649 java_lang_invoke_MethodHandle::form(mh)->print_on(&ls); 650 } 651 } 652 } 653 } 654 655 // The stub wraps the arguments in a struct on the stack to avoid 656 // dealing with the different calling conventions for passing 6 657 // arguments. 658 struct MethodHandleStubArguments { 659 const char* adaptername; 660 oopDesc* mh; 661 intptr_t* saved_regs; 662 intptr_t* entry_sp; 663 }; 664 void trace_method_handle_stub_wrapper(MethodHandleStubArguments* args) { 665 trace_method_handle_stub(args->adaptername, 666 args->mh, 667 args->saved_regs, 668 args->entry_sp); 669 } 670 671 void MethodHandles::trace_method_handle(MacroAssembler* _masm, const char* adaptername) { 672 if (!log_is_enabled(Info, methodhandles)) return; 673 BLOCK_COMMENT(err_msg("trace_method_handle %s {", adaptername)); 674 __ enter(); 675 __ andptr(rsp, -16); // align stack if needed for FPU state 676 __ pusha(); 677 __ mov(rbx, rsp); // for retrieving saved_regs 678 // Note: saved_regs must be in the entered frame for the 679 // robust stack walking implemented in trace_method_handle_stub. 680 681 // save FP result, valid at some call sites (adapter_opt_return_float, ...) 682 __ decrement(rsp, 2 * wordSize); 683 __ movdbl(Address(rsp, 0), xmm0); 684 685 // Incoming state: 686 // rcx: method handle 687 // 688 // To avoid calling convention issues, build a record on the stack 689 // and pass the pointer to that instead. 690 __ push(rbp); // entry_sp (with extra align space) 691 __ push(rbx); // pusha saved_regs 692 __ push(rcx); // mh 693 __ push(rcx); // slot for adaptername 694 __ movptr(Address(rsp, 0), (intptr_t) adaptername, rscratch1); 695 __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, trace_method_handle_stub_wrapper), rsp); 696 __ increment(rsp, sizeof(MethodHandleStubArguments)); 697 698 __ movdbl(xmm0, Address(rsp, 0)); 699 __ increment(rsp, 2 * wordSize); 700 701 __ popa(); 702 __ leave(); 703 BLOCK_COMMENT("} trace_method_handle"); 704 } 705 #endif //PRODUCT