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