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); LP64_ONLY( __ push(rscratch1); ) } 86 #define POP { LP64_ONLY( __ 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 if (ref_kind == JVM_REF_invokeVirtual || 116 ref_kind == JVM_REF_invokeSpecial) 117 // could do this for all ref_kinds, but would explode assembly code size 118 trace_method_handle(_masm, buf); 119 __ STOP(buf); 120 } 121 BLOCK_COMMENT("} verify_ref_kind"); 122 __ bind(L); 123 } 124 125 #endif //ASSERT 126 127 void MethodHandles::jump_from_method_handle(MacroAssembler* _masm, Register method, Register temp, 128 bool for_compiler_entry) { 129 assert(method == rbx, "interpreter calling convention"); 130 131 Label L_no_such_method; 132 __ testptr(rbx, rbx); 133 __ jcc(Assembler::zero, L_no_such_method); 134 135 __ verify_method_ptr(method); 136 137 if (!for_compiler_entry && JvmtiExport::can_post_interpreter_events()) { 138 Label run_compiled_code; 139 // JVMTI events, such as single-stepping, are implemented partly by avoiding running 140 // compiled code in threads for which the event is enabled. Check here for 141 // interp_only_mode if these events CAN be enabled. 142 #ifdef _LP64 143 Register rthread = r15_thread; 144 #else 145 Register rthread = temp; 146 __ get_thread(rthread); 147 #endif 148 // interp_only is an int, on little endian it is sufficient to test the byte only 149 // Is a cmpl faster? 150 __ cmpb(Address(rthread, JavaThread::interp_only_mode_offset()), 0); 151 __ jccb(Assembler::zero, run_compiled_code); 152 __ jmp(Address(method, Method::interpreter_entry_offset())); 153 __ BIND(run_compiled_code); 154 } 155 156 // The following jump might pass an inline type argument that was erased to Object as oop to a 157 // callee that expects inline type arguments to be passed as fields. We need to call the compiled 158 // value entry (_code->inline_entry_point() or _adapter->c2i_inline_entry()) which will take care 159 // of translating between the calling conventions. 160 const ByteSize entry_offset = for_compiler_entry ? Method::from_compiled_inline_offset() : 161 Method::from_interpreted_offset(); 162 __ jmp(Address(method, entry_offset)); 163 164 __ bind(L_no_such_method); 165 __ jump(RuntimeAddress(SharedRuntime::throw_AbstractMethodError_entry())); 166 } 167 168 void MethodHandles::jump_to_lambda_form(MacroAssembler* _masm, 169 Register recv, Register method_temp, 170 Register temp2, 171 bool for_compiler_entry) { 172 BLOCK_COMMENT("jump_to_lambda_form {"); 173 // This is the initial entry point of a lazy method handle. 174 // After type checking, it picks up the invoker from the LambdaForm. 175 assert_different_registers(recv, method_temp, temp2); 176 assert(recv != noreg, "required register"); 177 assert(method_temp == rbx, "required register for loading method"); 178 179 // Load the invoker, as MH -> MH.form -> LF.vmentry 180 __ verify_oop(recv); 181 __ load_heap_oop(method_temp, Address(recv, NONZERO(java_lang_invoke_MethodHandle::form_offset())), temp2); 182 __ verify_oop(method_temp); 183 __ load_heap_oop(method_temp, Address(method_temp, NONZERO(java_lang_invoke_LambdaForm::vmentry_offset())), temp2); 184 __ verify_oop(method_temp); 185 __ load_heap_oop(method_temp, Address(method_temp, NONZERO(java_lang_invoke_MemberName::method_offset())), temp2); 186 __ verify_oop(method_temp); 187 __ access_load_at(T_ADDRESS, IN_HEAP, method_temp, 188 Address(method_temp, NONZERO(java_lang_invoke_ResolvedMethodName::vmtarget_offset())), 189 noreg, noreg); 190 191 if (VerifyMethodHandles && !for_compiler_entry) { 192 // make sure recv is already on stack 193 __ movptr(temp2, Address(method_temp, Method::const_offset())); 194 __ load_sized_value(temp2, 195 Address(temp2, ConstMethod::size_of_parameters_offset()), 196 sizeof(u2), /*is_signed*/ false); 197 // assert(sizeof(u2) == sizeof(Method::_size_of_parameters), ""); 198 Label L; 199 __ cmpoop(recv, __ argument_address(temp2, -1)); 200 __ jcc(Assembler::equal, L); 201 __ movptr(rax, __ argument_address(temp2, -1)); 202 __ STOP("receiver not on stack"); 203 __ BIND(L); 204 } 205 206 jump_from_method_handle(_masm, method_temp, temp2, for_compiler_entry); 207 BLOCK_COMMENT("} jump_to_lambda_form"); 208 } 209 210 void MethodHandles::jump_to_native_invoker(MacroAssembler* _masm, Register nep_reg, Register temp_target) { 211 BLOCK_COMMENT("jump_to_native_invoker {"); 212 assert_different_registers(nep_reg, temp_target); 213 assert(nep_reg != noreg, "required register"); 214 215 // Load the invoker, as NEP -> .invoker 216 __ verify_oop(nep_reg); 217 __ access_load_at(T_ADDRESS, IN_HEAP, temp_target, 218 Address(nep_reg, NONZERO(jdk_internal_foreign_abi_NativeEntryPoint::downcall_stub_address_offset_in_bytes())), 219 noreg, noreg); 220 221 __ jmp(temp_target); 222 BLOCK_COMMENT("} jump_to_native_invoker"); 223 } 224 225 226 // Code generation 227 address MethodHandles::generate_method_handle_interpreter_entry(MacroAssembler* _masm, 228 vmIntrinsics::ID iid) { 229 const bool not_for_compiler_entry = false; // this is the interpreter entry 230 assert(is_signature_polymorphic(iid), "expected invoke iid"); 231 if (iid == vmIntrinsics::_invokeGeneric || 232 iid == vmIntrinsics::_compiledLambdaForm) { 233 // Perhaps surprisingly, the symbolic references visible to Java are not directly used. 234 // They are linked to Java-generated adapters via MethodHandleNatives.linkMethod. 235 // They all allow an appendix argument. 236 __ hlt(); // empty stubs make SG sick 237 return nullptr; 238 } 239 240 // No need in interpreter entry for linkToNative for now. 241 // Interpreter calls compiled entry through i2c. 242 if (iid == vmIntrinsics::_linkToNative) { 243 __ hlt(); 244 return nullptr; 245 } 246 247 // rsi/r13: sender SP (must preserve; see prepare_to_jump_from_interpreted) 248 // rbx: Method* 249 // rdx: argument locator (parameter slot count, added to rsp) 250 // rcx: used as temp to hold mh or receiver 251 // rax, rdi: garbage temps, blown away 252 Register rdx_argp = rdx; // argument list ptr, live on error paths 253 Register rax_temp = rax; 254 Register rcx_mh = rcx; // MH receiver; dies quickly and is recycled 255 Register rbx_method = rbx; // eventual target of this invocation 256 257 // here's where control starts out: 258 __ align(CodeEntryAlignment); 259 address entry_point = __ pc(); 260 261 if (VerifyMethodHandles) { 262 assert(Method::intrinsic_id_size_in_bytes() == 2, "assuming Method::_intrinsic_id is u2"); 263 264 Label L; 265 BLOCK_COMMENT("verify_intrinsic_id {"); 266 __ cmpw(Address(rbx_method, Method::intrinsic_id_offset()), (int) iid); 267 __ jcc(Assembler::equal, L); 268 if (iid == vmIntrinsics::_linkToVirtual || 269 iid == vmIntrinsics::_linkToSpecial) { 270 // could do this for all kinds, but would explode assembly code size 271 trace_method_handle(_masm, "bad Method*::intrinsic_id"); 272 } 273 __ STOP("bad Method*::intrinsic_id"); 274 __ bind(L); 275 BLOCK_COMMENT("} verify_intrinsic_id"); 276 } 277 278 // First task: Find out how big the argument list is. 279 Address rdx_first_arg_addr; 280 int ref_kind = signature_polymorphic_intrinsic_ref_kind(iid); 281 assert(ref_kind != 0 || iid == vmIntrinsics::_invokeBasic, "must be _invokeBasic or a linkTo intrinsic"); 282 if (ref_kind == 0 || MethodHandles::ref_kind_has_receiver(ref_kind)) { 283 __ movptr(rdx_argp, Address(rbx_method, Method::const_offset())); 284 __ load_sized_value(rdx_argp, 285 Address(rdx_argp, ConstMethod::size_of_parameters_offset()), 286 sizeof(u2), /*is_signed*/ false); 287 // assert(sizeof(u2) == sizeof(Method::_size_of_parameters), ""); 288 rdx_first_arg_addr = __ argument_address(rdx_argp, -1); 289 } else { 290 DEBUG_ONLY(rdx_argp = noreg); 291 } 292 293 if (!is_signature_polymorphic_static(iid)) { 294 __ movptr(rcx_mh, rdx_first_arg_addr); 295 DEBUG_ONLY(rdx_argp = noreg); 296 } 297 298 // rdx_first_arg_addr is live! 299 300 trace_method_handle_interpreter_entry(_masm, iid); 301 302 if (iid == vmIntrinsics::_invokeBasic) { 303 generate_method_handle_dispatch(_masm, iid, rcx_mh, noreg, not_for_compiler_entry); 304 305 } else { 306 // Adjust argument list by popping the trailing MemberName argument. 307 Register rcx_recv = noreg; 308 if (MethodHandles::ref_kind_has_receiver(ref_kind)) { 309 // Load the receiver (not the MH; the actual MemberName's receiver) up from the interpreter stack. 310 __ movptr(rcx_recv = rcx, rdx_first_arg_addr); 311 } 312 DEBUG_ONLY(rdx_argp = noreg); 313 Register rbx_member = rbx_method; // MemberName ptr; incoming method ptr is dead now 314 __ pop(rax_temp); // return address 315 __ pop(rbx_member); // extract last argument 316 __ push(rax_temp); // re-push return address 317 generate_method_handle_dispatch(_masm, iid, rcx_recv, rbx_member, not_for_compiler_entry); 318 } 319 320 return entry_point; 321 } 322 323 void MethodHandles::generate_method_handle_dispatch(MacroAssembler* _masm, 324 vmIntrinsics::ID iid, 325 Register receiver_reg, 326 Register member_reg, 327 bool for_compiler_entry) { 328 assert(is_signature_polymorphic(iid), "expected invoke iid"); 329 Register rbx_method = rbx; // eventual target of this invocation 330 // temps used in this code are not used in *either* compiled or interpreted calling sequences 331 #ifdef _LP64 332 Register temp1 = rscratch1; 333 Register temp2 = rscratch2; 334 Register temp3 = rax; 335 if (for_compiler_entry) { 336 assert(receiver_reg == (iid == vmIntrinsics::_linkToStatic || iid == vmIntrinsics::_linkToNative ? noreg : j_rarg0), "only valid assignment"); 337 assert_different_registers(temp1, j_rarg0, j_rarg1, j_rarg2, j_rarg3, j_rarg4, j_rarg5); 338 assert_different_registers(temp2, j_rarg0, j_rarg1, j_rarg2, j_rarg3, j_rarg4, j_rarg5); 339 assert_different_registers(temp3, j_rarg0, j_rarg1, j_rarg2, j_rarg3, j_rarg4, j_rarg5); 340 } 341 #else 342 Register temp1 = (for_compiler_entry ? rsi : rdx); 343 Register temp2 = rdi; 344 Register temp3 = rax; 345 if (for_compiler_entry) { 346 assert(receiver_reg == (iid == vmIntrinsics::_linkToStatic || iid == vmIntrinsics::_linkToNative ? noreg : rcx), "only valid assignment"); 347 assert_different_registers(temp1, rcx, rdx); 348 assert_different_registers(temp2, rcx, rdx); 349 assert_different_registers(temp3, rcx, rdx); 350 } 351 #endif 352 else { 353 assert_different_registers(temp1, temp2, temp3, saved_last_sp_register()); // don't trash lastSP 354 } 355 assert_different_registers(temp1, temp2, temp3, receiver_reg); 356 assert_different_registers(temp1, temp2, temp3, member_reg); 357 358 if (iid == vmIntrinsics::_invokeBasic) { 359 // indirect through MH.form.vmentry.vmtarget 360 jump_to_lambda_form(_masm, receiver_reg, rbx_method, temp1, for_compiler_entry); 361 } else if (iid == vmIntrinsics::_linkToNative) { 362 assert(for_compiler_entry, "only compiler entry is supported"); 363 jump_to_native_invoker(_masm, member_reg, temp1); 364 } else { 365 // The method is a member invoker used by direct method handles. 366 if (VerifyMethodHandles) { 367 // make sure the trailing argument really is a MemberName (caller responsibility) 368 verify_klass(_masm, member_reg, VM_CLASS_ID(java_lang_invoke_MemberName), 369 "MemberName required for invokeVirtual etc."); 370 } 371 372 Address member_clazz( member_reg, NONZERO(java_lang_invoke_MemberName::clazz_offset())); 373 Address member_vmindex( member_reg, NONZERO(java_lang_invoke_MemberName::vmindex_offset())); 374 Address member_vmtarget( member_reg, NONZERO(java_lang_invoke_MemberName::method_offset())); 375 Address vmtarget_method( rbx_method, NONZERO(java_lang_invoke_ResolvedMethodName::vmtarget_offset())); 376 377 Register temp1_recv_klass = temp1; 378 if (iid != vmIntrinsics::_linkToStatic) { 379 __ verify_oop(receiver_reg); 380 if (iid == vmIntrinsics::_linkToSpecial) { 381 // Don't actually load the klass; just null-check the receiver. 382 __ null_check(receiver_reg); 383 } else { 384 // load receiver klass itself 385 __ load_klass(temp1_recv_klass, receiver_reg, temp2); 386 __ verify_klass_ptr(temp1_recv_klass); 387 } 388 BLOCK_COMMENT("check_receiver {"); 389 // The receiver for the MemberName must be in receiver_reg. 390 // Check the receiver against the MemberName.clazz 391 if (VerifyMethodHandles && iid == vmIntrinsics::_linkToSpecial) { 392 // Did not load it above... 393 __ load_klass(temp1_recv_klass, receiver_reg, temp2); 394 __ verify_klass_ptr(temp1_recv_klass); 395 } 396 if (VerifyMethodHandles && iid != vmIntrinsics::_linkToInterface) { 397 Label L_ok; 398 Register temp2_defc = temp2; 399 __ load_heap_oop(temp2_defc, member_clazz, temp3); 400 load_klass_from_Class(_masm, temp2_defc); 401 __ verify_klass_ptr(temp2_defc); 402 __ check_klass_subtype(temp1_recv_klass, temp2_defc, temp3, L_ok); 403 // If we get here, the type check failed! 404 __ STOP("receiver class disagrees with MemberName.clazz"); 405 __ bind(L_ok); 406 } 407 BLOCK_COMMENT("} check_receiver"); 408 } 409 if (iid == vmIntrinsics::_linkToSpecial || 410 iid == vmIntrinsics::_linkToStatic) { 411 DEBUG_ONLY(temp1_recv_klass = noreg); // these guys didn't load the recv_klass 412 } 413 414 // Live registers at this point: 415 // member_reg - MemberName that was the trailing argument 416 // temp1_recv_klass - klass of stacked receiver, if needed 417 // rsi/r13 - interpreter linkage (if interpreted) 418 // rcx, rdx, rsi, rdi, r8 - compiler arguments (if compiled) 419 420 Label L_incompatible_class_change_error; 421 switch (iid) { 422 case vmIntrinsics::_linkToSpecial: 423 if (VerifyMethodHandles) { 424 verify_ref_kind(_masm, JVM_REF_invokeSpecial, member_reg, temp3); 425 } 426 __ load_heap_oop(rbx_method, member_vmtarget); 427 __ access_load_at(T_ADDRESS, IN_HEAP, rbx_method, vmtarget_method, noreg, noreg); 428 break; 429 430 case vmIntrinsics::_linkToStatic: 431 if (VerifyMethodHandles) { 432 verify_ref_kind(_masm, JVM_REF_invokeStatic, member_reg, temp3); 433 } 434 __ load_heap_oop(rbx_method, member_vmtarget); 435 __ access_load_at(T_ADDRESS, IN_HEAP, rbx_method, vmtarget_method, noreg, noreg); 436 break; 437 438 case vmIntrinsics::_linkToVirtual: 439 { 440 // same as TemplateTable::invokevirtual, 441 // minus the CP setup and profiling: 442 443 if (VerifyMethodHandles) { 444 verify_ref_kind(_masm, JVM_REF_invokeVirtual, member_reg, temp3); 445 } 446 447 // pick out the vtable index from the MemberName, and then we can discard it: 448 Register temp2_index = temp2; 449 __ access_load_at(T_ADDRESS, IN_HEAP, temp2_index, member_vmindex, noreg, noreg); 450 451 if (VerifyMethodHandles) { 452 Label L_index_ok; 453 __ cmpl(temp2_index, 0); 454 __ jcc(Assembler::greaterEqual, L_index_ok); 455 __ STOP("no virtual index"); 456 __ BIND(L_index_ok); 457 } 458 459 // Note: The verifier invariants allow us to ignore MemberName.clazz and vmtarget 460 // at this point. And VerifyMethodHandles has already checked clazz, if needed. 461 462 // get target Method* & entry point 463 __ lookup_virtual_method(temp1_recv_klass, temp2_index, rbx_method); 464 break; 465 } 466 467 case vmIntrinsics::_linkToInterface: 468 { 469 // same as TemplateTable::invokeinterface 470 // (minus the CP setup and profiling, with different argument motion) 471 if (VerifyMethodHandles) { 472 verify_ref_kind(_masm, JVM_REF_invokeInterface, member_reg, temp3); 473 } 474 475 Register temp3_intf = temp3; 476 __ load_heap_oop(temp3_intf, member_clazz); 477 load_klass_from_Class(_masm, temp3_intf); 478 __ verify_klass_ptr(temp3_intf); 479 480 Register rbx_index = rbx_method; 481 __ access_load_at(T_ADDRESS, IN_HEAP, rbx_index, member_vmindex, noreg, noreg); 482 if (VerifyMethodHandles) { 483 Label L; 484 __ cmpl(rbx_index, 0); 485 __ jcc(Assembler::greaterEqual, L); 486 __ STOP("invalid vtable index for MH.invokeInterface"); 487 __ bind(L); 488 } 489 490 // given intf, index, and recv klass, dispatch to the implementation method 491 __ lookup_interface_method(temp1_recv_klass, temp3_intf, 492 // note: next two args must be the same: 493 rbx_index, rbx_method, 494 temp2, 495 L_incompatible_class_change_error); 496 break; 497 } 498 499 default: 500 fatal("unexpected intrinsic %d: %s", vmIntrinsics::as_int(iid), vmIntrinsics::name_at(iid)); 501 break; 502 } 503 504 // Live at this point: 505 // rbx_method 506 // rsi/r13 (if interpreted) 507 508 // After figuring out which concrete method to call, jump into it. 509 // Note that this works in the interpreter with no data motion. 510 // But the compiled version will require that rcx_recv be shifted out. 511 __ verify_method_ptr(rbx_method); 512 jump_from_method_handle(_masm, rbx_method, temp1, for_compiler_entry); 513 514 if (iid == vmIntrinsics::_linkToInterface) { 515 __ bind(L_incompatible_class_change_error); 516 __ jump(RuntimeAddress(SharedRuntime::throw_IncompatibleClassChangeError_entry())); 517 } 518 } 519 } 520 521 #ifndef PRODUCT 522 void trace_method_handle_stub(const char* adaptername, 523 oopDesc* mh, 524 intptr_t* saved_regs, 525 intptr_t* entry_sp) { 526 // called as a leaf from native code: do not block the JVM! 527 bool has_mh = (strstr(adaptername, "/static") == nullptr && 528 strstr(adaptername, "linkTo") == nullptr); // static linkers don't have MH 529 const char* mh_reg_name = has_mh ? "rcx_mh" : "rcx"; 530 log_info(methodhandles)("MH %s %s=" PTR_FORMAT " sp=" PTR_FORMAT, adaptername, mh_reg_name, p2i(mh), p2i(entry_sp)); 531 532 LogTarget(Trace, methodhandles) lt; 533 if (lt.is_enabled()) { 534 ResourceMark rm; 535 LogStream ls(lt); 536 ls.print_cr("Registers:"); 537 const int saved_regs_count = Register::number_of_registers; 538 for (int i = 0; i < saved_regs_count; i++) { 539 Register r = as_Register(i); 540 // The registers are stored in reverse order on the stack (by pusha). 541 #ifdef AMD64 542 int num_regs = UseAPX ? 32 : 16; 543 assert(Register::available_gp_registers() == num_regs, "sanity"); 544 if (r == rsp) { 545 // rsp is actually not stored by pusha(), compute the old rsp from saved_regs (rsp after pusha): saved_regs + 16 = old rsp 546 ls.print("%3s=" PTR_FORMAT, r->name(), (intptr_t)(&saved_regs[num_regs])); 547 } else { 548 ls.print("%3s=" PTR_FORMAT, r->name(), saved_regs[((saved_regs_count - 1) - i)]); 549 } 550 #else 551 ls.print("%3s=" PTR_FORMAT, r->name(), saved_regs[((saved_regs_count - 1) - i)]); 552 #endif 553 if ((i + 1) % 4 == 0) { 554 ls.cr(); 555 } else { 556 ls.print(", "); 557 } 558 } 559 ls.cr(); 560 561 // Note: We want to allow trace_method_handle from any call site. 562 // While trace_method_handle creates a frame, it may be entered 563 // without a PC on the stack top (e.g. not just after a call). 564 // Walking that frame could lead to failures due to that invalid PC. 565 // => carefully detect that frame when doing the stack walking 566 567 { 568 // dumping last frame with frame::describe 569 570 JavaThread* p = JavaThread::active(); 571 572 // may not be needed by safer and unexpensive here 573 PreserveExceptionMark pem(Thread::current()); 574 FrameValues values; 575 576 frame cur_frame = os::current_frame(); 577 578 if (cur_frame.fp() != nullptr) { // not walkable 579 580 // Robust search of trace_calling_frame (independent of inlining). 581 // Assumes saved_regs comes from a pusha in the trace_calling_frame. 582 // 583 // We have to start the search from cur_frame, because trace_calling_frame may be it. 584 // It is guaranteed that trace_calling_frame is different from the top frame. 585 // But os::current_frame() does NOT return the top frame: it returns the next frame under it (caller's frame). 586 // (Due to inlining and tail call optimizations, caller's frame doesn't necessarily correspond to the immediate 587 // caller in the source code.) 588 assert(cur_frame.sp() < saved_regs, "registers not saved on stack ?"); 589 frame trace_calling_frame = cur_frame; 590 while (trace_calling_frame.fp() < saved_regs) { 591 assert(trace_calling_frame.cb() == nullptr, "not a C frame"); 592 trace_calling_frame = os::get_sender_for_C_frame(&trace_calling_frame); 593 } 594 assert(trace_calling_frame.sp() < saved_regs, "wrong frame"); 595 596 // safely create a frame and call frame::describe 597 intptr_t *dump_sp = trace_calling_frame.sender_sp(); 598 intptr_t *dump_fp = trace_calling_frame.link(); 599 600 if (has_mh) { 601 // The previous definition of walkable may have to be refined 602 // if new call sites cause the next frame constructor to start 603 // failing. Alternatively, frame constructors could be 604 // modified to support the current or future non walkable 605 // frames (but this is more intrusive and is not considered as 606 // part of this RFE, which will instead use a simpler output). 607 frame dump_frame = frame(dump_sp, dump_fp); 608 dump_frame.describe(values, 1); 609 } else { 610 // Stack may not be walkable (invalid PC above FP): 611 // Add descriptions without building a Java frame to avoid issues 612 values.describe(-1, dump_fp, "fp for #1 <not parsed, cannot trust pc>"); 613 values.describe(-1, dump_sp, "sp for #1"); 614 } 615 } 616 values.describe(-1, entry_sp, "raw top of stack"); 617 618 ls.print_cr("Stack layout:"); 619 values.print_on(p, &ls); 620 } 621 if (has_mh && oopDesc::is_oop(mh)) { 622 mh->print_on(&ls); 623 if (java_lang_invoke_MethodHandle::is_instance(mh)) { 624 java_lang_invoke_MethodHandle::form(mh)->print_on(&ls); 625 } 626 } 627 } 628 } 629 630 // The stub wraps the arguments in a struct on the stack to avoid 631 // dealing with the different calling conventions for passing 6 632 // arguments. 633 struct MethodHandleStubArguments { 634 const char* adaptername; 635 oopDesc* mh; 636 intptr_t* saved_regs; 637 intptr_t* entry_sp; 638 }; 639 void trace_method_handle_stub_wrapper(MethodHandleStubArguments* args) { 640 trace_method_handle_stub(args->adaptername, 641 args->mh, 642 args->saved_regs, 643 args->entry_sp); 644 } 645 646 void MethodHandles::trace_method_handle(MacroAssembler* _masm, const char* adaptername) { 647 if (!log_is_enabled(Info, methodhandles)) return; 648 BLOCK_COMMENT(err_msg("trace_method_handle %s {", adaptername)); 649 __ enter(); 650 __ andptr(rsp, -16); // align stack if needed for FPU state 651 __ pusha(); 652 __ mov(rbx, rsp); // for retrieving saved_regs 653 // Note: saved_regs must be in the entered frame for the 654 // robust stack walking implemented in trace_method_handle_stub. 655 656 // save FP result, valid at some call sites (adapter_opt_return_float, ...) 657 __ decrement(rsp, 2 * wordSize); 658 #ifdef _LP64 659 __ movdbl(Address(rsp, 0), xmm0); 660 #else 661 if (UseSSE >= 2) { 662 __ movdbl(Address(rsp, 0), xmm0); 663 } else if (UseSSE == 1) { 664 __ movflt(Address(rsp, 0), xmm0); 665 } else { 666 __ fst_d(Address(rsp, 0)); 667 } 668 #endif // LP64 669 670 // Incoming state: 671 // rcx: method handle 672 // 673 // To avoid calling convention issues, build a record on the stack 674 // and pass the pointer to that instead. 675 __ push(rbp); // entry_sp (with extra align space) 676 __ push(rbx); // pusha saved_regs 677 __ push(rcx); // mh 678 __ push(rcx); // slot for adaptername 679 __ movptr(Address(rsp, 0), (intptr_t) adaptername, rscratch1); 680 __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, trace_method_handle_stub_wrapper), rsp); 681 __ increment(rsp, sizeof(MethodHandleStubArguments)); 682 683 #ifdef _LP64 684 __ movdbl(xmm0, Address(rsp, 0)); 685 #else 686 if (UseSSE >= 2) { 687 __ movdbl(xmm0, Address(rsp, 0)); 688 } else if (UseSSE == 1) { 689 __ movflt(xmm0, Address(rsp, 0)); 690 } else { 691 __ fld_d(Address(rsp, 0)); 692 } 693 #endif // LP64 694 __ increment(rsp, 2 * wordSize); 695 696 __ popa(); 697 __ leave(); 698 BLOCK_COMMENT("} trace_method_handle"); 699 } 700 #endif //PRODUCT