1 /* 2 * Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved. 3 * Copyright (c) 2016, 2017 SAP SE. 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 "jvm.h" 28 #include "asm/macroAssembler.inline.hpp" 29 #include "classfile/javaClasses.inline.hpp" 30 #include "classfile/vmClasses.hpp" 31 #include "interpreter/interpreter.hpp" 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/frame.inline.hpp" 39 #include "runtime/stubRoutines.hpp" 40 #include "utilities/macros.hpp" 41 #include "utilities/preserveException.hpp" 42 43 #ifdef PRODUCT 44 #define __ _masm-> 45 #define BLOCK_COMMENT(str) /* nothing */ 46 #else 47 #define __ (Verbose ? (_masm->block_comment(FILE_AND_LINE),_masm):_masm)-> 48 #define BLOCK_COMMENT(str) __ block_comment(str) 49 #endif 50 51 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":") 52 53 // Workaround for C++ overloading nastiness on '0' for RegisterOrConstant. 54 static RegisterOrConstant constant(int value) { 55 return RegisterOrConstant(value); 56 } 57 58 void MethodHandles::load_klass_from_Class(MacroAssembler* _masm, Register klass_reg, 59 Register temp_reg, Register temp2_reg) { 60 if (VerifyMethodHandles) { 61 verify_klass(_masm, klass_reg, VM_CLASS_ID(java_lang_Class), 62 temp_reg, temp2_reg, "MH argument is a Class"); 63 } 64 __ z_lg(klass_reg, Address(klass_reg, java_lang_Class::klass_offset())); 65 } 66 67 68 #ifdef ASSERT 69 static int check_nonzero(const char* xname, int x) { 70 assert(x != 0, "%s should be nonzero", xname); 71 return x; 72 } 73 #define NONZERO(x) check_nonzero(#x, x) 74 #else 75 #define NONZERO(x) (x) 76 #endif 77 78 #ifdef ASSERT 79 void MethodHandles::verify_klass(MacroAssembler* _masm, 80 Register obj_reg, vmClassID klass_id, 81 Register temp_reg, Register temp2_reg, 82 const char* error_message) { 83 84 InstanceKlass** klass_addr = vmClasses::klass_addr_at(klass_id); 85 Klass* klass = vmClasses::klass_at(klass_id); 86 87 assert(temp_reg != Z_R0 && // Is used as base register! 88 temp_reg != noreg && temp2_reg != noreg, "need valid registers!"); 89 90 NearLabel L_ok, L_bad; 91 92 BLOCK_COMMENT("verify_klass {"); 93 94 __ verify_oop(obj_reg, FILE_AND_LINE); 95 __ compareU64_and_branch(obj_reg, (intptr_t)0L, Assembler::bcondEqual, L_bad); 96 __ load_klass(temp_reg, obj_reg); 97 // klass_addr is a klass in allstatic SystemDictionaryHandles. Can't get GCed. 98 __ load_const_optimized(temp2_reg, (address)klass_addr); 99 __ z_lg(temp2_reg, Address(temp2_reg)); 100 __ compareU64_and_branch(temp_reg, temp2_reg, Assembler::bcondEqual, L_ok); 101 102 intptr_t super_check_offset = klass->super_check_offset(); 103 __ z_lg(temp_reg, Address(temp_reg, super_check_offset)); 104 __ compareU64_and_branch(temp_reg, temp2_reg, Assembler::bcondEqual, L_ok); 105 __ BIND(L_bad); 106 __ stop(error_message); 107 __ BIND(L_ok); 108 109 BLOCK_COMMENT("} verify_klass"); 110 } 111 112 void MethodHandles::verify_ref_kind(MacroAssembler* _masm, int ref_kind, 113 Register member_reg, Register temp ) { 114 NearLabel L; 115 BLOCK_COMMENT("verify_ref_kind {"); 116 117 __ z_llgf(temp, 118 Address(member_reg, 119 NONZERO(java_lang_invoke_MemberName::flags_offset()))); 120 __ z_srl(temp, java_lang_invoke_MemberName::MN_REFERENCE_KIND_SHIFT); 121 __ z_nilf(temp, java_lang_invoke_MemberName::MN_REFERENCE_KIND_MASK); 122 __ compare32_and_branch(temp, constant(ref_kind), Assembler::bcondEqual, L); 123 124 { 125 char *buf = NEW_C_HEAP_ARRAY(char, 100, mtInternal); 126 127 jio_snprintf(buf, 100, "verify_ref_kind expected %x", ref_kind); 128 if (ref_kind == JVM_REF_invokeVirtual || ref_kind == JVM_REF_invokeSpecial) { 129 // Could do this for all ref_kinds, but would explode assembly code size. 130 trace_method_handle(_masm, buf); 131 } 132 __ stop(buf); 133 } 134 135 BLOCK_COMMENT("} verify_ref_kind"); 136 137 __ bind(L); 138 } 139 #endif // ASSERT 140 141 void MethodHandles::jump_from_method_handle(MacroAssembler* _masm, Register method, Register target, 142 Register temp, bool for_compiler_entry) { 143 assert(method == Z_method, "interpreter calling convention"); 144 __ verify_method_ptr(method); 145 146 assert(target != method, "don 't you kill the method reg!"); 147 148 Label L_no_such_method; 149 150 if (!for_compiler_entry && JvmtiExport::can_post_interpreter_events()) { 151 // JVMTI events, such as single-stepping, are implemented partly 152 // by avoiding running compiled code in threads for which the 153 // event is enabled. Check here for interp_only_mode if these 154 // events CAN be enabled. 155 __ verify_thread(); 156 157 Label run_compiled_code; 158 159 __ load_and_test_int(temp, Address(Z_thread, JavaThread::interp_only_mode_offset())); 160 __ z_bre(run_compiled_code); 161 162 // Null method test is replicated below in compiled case, 163 // it might be able to address across the verify_thread(). 164 __ z_ltgr(temp, method); 165 __ z_bre(L_no_such_method); 166 167 __ z_lg(target, Address(method, Method::interpreter_entry_offset())); 168 __ z_br(target); 169 170 __ bind(run_compiled_code); 171 } 172 173 // Compiled case, either static or fall-through from runtime conditional. 174 __ z_ltgr(temp, method); 175 __ z_bre(L_no_such_method); 176 177 ByteSize offset = for_compiler_entry ? 178 Method::from_compiled_offset() : Method::from_interpreted_offset(); 179 Address method_from(method, offset); 180 181 __ z_lg(target, method_from); 182 __ z_br(target); 183 184 __ bind(L_no_such_method); 185 assert(StubRoutines::throw_AbstractMethodError_entry() != NULL, "not yet generated!"); 186 __ load_const_optimized(target, StubRoutines::throw_AbstractMethodError_entry()); 187 __ z_br(target); 188 } 189 190 void MethodHandles::jump_to_lambda_form(MacroAssembler* _masm, 191 Register recv, Register method_temp, 192 Register temp2, Register temp3, 193 bool for_compiler_entry) { 194 195 // This is the initial entry point of a lazy method handle. 196 // After type checking, it picks up the invoker from the LambdaForm. 197 assert_different_registers(recv, method_temp, temp2, temp3); 198 assert(method_temp == Z_method, "required register for loading method"); 199 200 BLOCK_COMMENT("jump_to_lambda_form {"); 201 202 // Load the invoker, as MH -> MH.form -> LF.vmentry 203 __ verify_oop(recv, FILE_AND_LINE); 204 __ load_heap_oop(method_temp, 205 Address(recv, 206 NONZERO(java_lang_invoke_MethodHandle::form_offset())), 207 noreg, noreg, IS_NOT_NULL); 208 __ verify_oop(method_temp, FILE_AND_LINE); 209 __ load_heap_oop(method_temp, 210 Address(method_temp, 211 NONZERO(java_lang_invoke_LambdaForm::vmentry_offset())), 212 noreg, noreg, IS_NOT_NULL); 213 __ verify_oop(method_temp, FILE_AND_LINE); 214 __ load_heap_oop(method_temp, 215 Address(method_temp, 216 NONZERO(java_lang_invoke_MemberName::method_offset())), 217 noreg, noreg, IS_NOT_NULL); 218 __ verify_oop(method_temp, FILE_AND_LINE); 219 __ z_lg(method_temp, 220 Address(method_temp, 221 NONZERO(java_lang_invoke_ResolvedMethodName::vmtarget_offset()))); 222 223 if (VerifyMethodHandles && !for_compiler_entry) { 224 // Make sure recv is already on stack. 225 NearLabel L; 226 Address paramSize(temp2, ConstMethod::size_of_parameters_offset()); 227 228 __ z_lg(temp2, Address(method_temp, Method::const_offset())); 229 __ load_sized_value(temp2, paramSize, sizeof(u2), /*is_signed*/ false); 230 // if (temp2 != recv) stop 231 __ z_lg(temp2, __ argument_address(temp2, temp2, 0)); 232 __ compare64_and_branch(temp2, recv, Assembler::bcondEqual, L); 233 __ stop("receiver not on stack"); 234 __ BIND(L); 235 } 236 237 jump_from_method_handle(_masm, method_temp, temp2, Z_R0, for_compiler_entry); 238 239 BLOCK_COMMENT("} jump_to_lambda_form"); 240 } 241 242 // code generation 243 address MethodHandles::generate_method_handle_interpreter_entry(MacroAssembler* _masm, 244 vmIntrinsics::ID iid) { 245 const bool not_for_compiler_entry = false; // This is the interpreter entry. 246 assert(is_signature_polymorphic(iid), "expected invoke iid"); 247 248 if (iid == vmIntrinsics::_invokeGeneric || iid == vmIntrinsics::_compiledLambdaForm) { 249 // Perhaps surprisingly, the symbolic references visible to Java 250 // are not directly used. They are linked to Java-generated 251 // adapters via MethodHandleNatives.linkMethod. They all allow an 252 // appendix argument. 253 __ should_not_reach_here(); // Empty stubs make SG sick. 254 return NULL; 255 } 256 257 // No need in interpreter entry for linkToNative for now. 258 // Interpreter calls compiled entry through i2c. 259 if (iid == vmIntrinsics::_linkToNative) { 260 __ should_not_reach_here(); // Empty stubs make SG sick. 261 return NULL; 262 } 263 264 // Z_R10: sender SP (must preserve; see prepare_to_jump_from_interprted) 265 // Z_method: method 266 // Z_ARG1 (Gargs): incoming argument list (must preserve) 267 Register Z_R4_param_size = Z_R4; // size of parameters 268 address code_start = __ pc(); 269 270 // Here is where control starts out: 271 __ align(CodeEntryAlignment); 272 273 address entry_point = __ pc(); 274 275 if (VerifyMethodHandles) { 276 Label L; 277 BLOCK_COMMENT("verify_intrinsic_id {"); 278 279 // Supplement to 8139891: _intrinsic_id exceeded 1-byte size limit. 280 if (Method::intrinsic_id_size_in_bytes() == 1) { 281 __ z_cli(Address(Z_method, Method::intrinsic_id_offset_in_bytes()), (int)iid); 282 } else { 283 assert(Method::intrinsic_id_size_in_bytes() == 2, "size error: check Method::_intrinsic_id"); 284 __ z_lh(Z_R0_scratch, Address(Z_method, Method::intrinsic_id_offset_in_bytes())); 285 __ z_chi(Z_R0_scratch, (int)iid); 286 } 287 __ z_bre(L); 288 289 if (iid == vmIntrinsics::_linkToVirtual || iid == vmIntrinsics::_linkToSpecial) { 290 // Could do this for all kinds, but would explode assembly code size. 291 trace_method_handle(_masm, "bad Method::intrinsic_id"); 292 } 293 294 __ stop("bad Method::intrinsic_id"); 295 __ bind(L); 296 297 BLOCK_COMMENT("} verify_intrinsic_id"); 298 } 299 300 // First task: Find out how big the argument list is. 301 Address Z_R4_first_arg_addr; 302 int ref_kind = signature_polymorphic_intrinsic_ref_kind(iid); 303 304 assert(ref_kind != 0 || iid == vmIntrinsics::_invokeBasic, 305 "must be _invokeBasic or a linkTo intrinsic"); 306 307 if (ref_kind == 0 || MethodHandles::ref_kind_has_receiver(ref_kind)) { 308 Address paramSize(Z_R1_scratch, ConstMethod::size_of_parameters_offset()); 309 310 __ z_lg(Z_R1_scratch, Address(Z_method, Method::const_offset())); 311 __ load_sized_value(Z_R4_param_size, paramSize, sizeof(u2), /*is_signed*/ false); 312 Z_R4_first_arg_addr = __ argument_address(Z_R4_param_size, Z_R4_param_size, 0); 313 } else { 314 DEBUG_ONLY(Z_R4_param_size = noreg); 315 } 316 317 Register Z_mh = noreg; 318 if (!is_signature_polymorphic_static(iid)) { 319 Z_mh = Z_ARG4; 320 __ z_lg(Z_mh, Z_R4_first_arg_addr); 321 DEBUG_ONLY(Z_R4_param_size = noreg); 322 } 323 324 // Z_R4_first_arg_addr is live! 325 326 trace_method_handle_interpreter_entry(_masm, iid); 327 328 if (iid == vmIntrinsics::_invokeBasic) { 329 __ pc(); // just for the block comment 330 generate_method_handle_dispatch(_masm, iid, Z_mh, noreg, not_for_compiler_entry); 331 } else { 332 // Adjust argument list by popping the trailing MemberName argument. 333 Register Z_recv = noreg; 334 335 if (MethodHandles::ref_kind_has_receiver(ref_kind)) { 336 // Load the receiver (not the MH; the actual MemberName's receiver) 337 // up from the interpreter stack. 338 __ z_lg(Z_recv = Z_R5, Z_R4_first_arg_addr); 339 DEBUG_ONLY(Z_R4_param_size = noreg); 340 } 341 342 Register Z_member = Z_method; // MemberName ptr; incoming method ptr is dead now 343 344 __ z_lg(Z_member, __ argument_address(constant(1))); 345 __ add2reg(Z_esp, Interpreter::stackElementSize); 346 generate_method_handle_dispatch(_masm, iid, Z_recv, Z_member, not_for_compiler_entry); 347 } 348 349 return entry_point; 350 } 351 352 void MethodHandles::generate_method_handle_dispatch(MacroAssembler* _masm, 353 vmIntrinsics::ID iid, 354 Register receiver_reg, 355 Register member_reg, 356 bool for_compiler_entry) { 357 assert(is_signature_polymorphic(iid), "expected invoke iid"); 358 359 Register temp1 = for_compiler_entry ? Z_R10 : Z_R6; 360 Register temp2 = Z_R12; 361 Register temp3 = Z_R11; 362 Register temp4 = Z_R13; 363 364 if (for_compiler_entry) { 365 assert(receiver_reg == (iid == vmIntrinsics::_linkToStatic ? noreg : Z_ARG1), 366 "only valid assignment"); 367 } 368 if (receiver_reg != noreg) { 369 assert_different_registers(temp1, temp2, temp3, temp4, receiver_reg); 370 } 371 if (member_reg != noreg) { 372 assert_different_registers(temp1, temp2, temp3, temp4, member_reg); 373 } 374 if (!for_compiler_entry) { // Don't trash last SP. 375 assert_different_registers(temp1, temp2, temp3, temp4, Z_R10); 376 } 377 378 if (iid == vmIntrinsics::_invokeBasic || iid == vmIntrinsics::_linkToNative) { 379 if (iid == vmIntrinsics::_linkToNative) { 380 assert(for_compiler_entry, "only compiler entry is supported"); 381 } 382 __ pc(); // Just for the block comment. 383 // Indirect through MH.form.vmentry.vmtarget. 384 jump_to_lambda_form(_masm, receiver_reg, Z_method, Z_R1, temp3, for_compiler_entry); 385 return; 386 } 387 388 // The method is a member invoker used by direct method handles. 389 if (VerifyMethodHandles) { 390 // Make sure the trailing argument really is a MemberName (caller responsibility). 391 verify_klass(_masm, member_reg, 392 VM_CLASS_ID(MemberName_klass), 393 temp1, temp2, 394 "MemberName required for invokeVirtual etc."); 395 } 396 397 Address member_clazz( member_reg, NONZERO(java_lang_invoke_MemberName::clazz_offset())); 398 Address member_vmindex( member_reg, NONZERO(java_lang_invoke_MemberName::vmindex_offset())); 399 Address member_vmtarget(member_reg, NONZERO(java_lang_invoke_MemberName::method_offset())); 400 Address vmtarget_method(Z_method, NONZERO(java_lang_invoke_ResolvedMethodName::vmtarget_offset())); 401 Register temp1_recv_klass = temp1; 402 403 if (iid != vmIntrinsics::_linkToStatic) { 404 __ verify_oop(receiver_reg, FILE_AND_LINE); 405 if (iid == vmIntrinsics::_linkToSpecial) { 406 // Don't actually load the klass; just null-check the receiver. 407 __ null_check(receiver_reg); 408 } else { 409 // Load receiver klass itself. 410 __ null_check(receiver_reg, Z_R0, oopDesc::klass_offset_in_bytes()); 411 __ load_klass(temp1_recv_klass, receiver_reg); 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); 420 __ verify_klass_ptr(temp1_recv_klass); 421 } 422 423 if (VerifyMethodHandles && iid != vmIntrinsics::_linkToInterface) { 424 NearLabel L_ok; 425 Register temp2_defc = temp2; 426 427 __ load_heap_oop(temp2_defc, member_clazz, 428 noreg, noreg, IS_NOT_NULL); 429 load_klass_from_Class(_masm, temp2_defc, temp3, temp4); 430 __ verify_klass_ptr(temp2_defc); 431 __ check_klass_subtype(temp1_recv_klass, temp2_defc, temp3, temp4, L_ok); 432 // If we get here, the type check failed! 433 __ stop("receiver class disagrees with MemberName.clazz"); 434 __ bind(L_ok); 435 } 436 BLOCK_COMMENT("} check_receiver"); 437 } 438 if (iid == vmIntrinsics::_linkToSpecial || iid == vmIntrinsics::_linkToStatic) { 439 DEBUG_ONLY(temp1_recv_klass = noreg); // These guys didn't load the recv_klass. 440 } 441 442 // Live registers at this point: 443 // member_reg - MemberName that was the trailing argument. 444 // temp1_recv_klass - Klass of stacked receiver, if needed. 445 // Z_R10 - Interpreter linkage if interpreted. 446 447 bool method_is_live = false; 448 449 switch (iid) { 450 case vmIntrinsics::_linkToSpecial: 451 if (VerifyMethodHandles) { 452 verify_ref_kind(_masm, JVM_REF_invokeSpecial, member_reg, temp3); 453 } 454 __ load_heap_oop(Z_method, member_vmtarget, 455 noreg, noreg, IS_NOT_NULL); 456 __ z_lg(Z_method, vmtarget_method); 457 method_is_live = true; 458 break; 459 460 case vmIntrinsics::_linkToStatic: 461 if (VerifyMethodHandles) { 462 verify_ref_kind(_masm, JVM_REF_invokeStatic, member_reg, temp3); 463 } 464 __ load_heap_oop(Z_method, member_vmtarget, 465 noreg, noreg, IS_NOT_NULL); 466 __ z_lg(Z_method, vmtarget_method); 467 method_is_live = true; 468 break; 469 470 case vmIntrinsics::_linkToVirtual: { 471 // Same as TemplateTable::invokevirtual, minus the CP setup and profiling. 472 if (VerifyMethodHandles) { 473 verify_ref_kind(_masm, JVM_REF_invokeVirtual, member_reg, temp3); 474 } 475 476 // Pick out the vtable index from the MemberName, and then we can discard it. 477 Register temp2_index = temp2; 478 __ z_lg(temp2_index, member_vmindex); 479 480 if (VerifyMethodHandles) { 481 // if (member_vmindex < 0) stop 482 NearLabel L_index_ok; 483 __ compare32_and_branch(temp2_index, constant(0), Assembler::bcondNotLow, L_index_ok); 484 __ stop("no virtual index"); 485 __ BIND(L_index_ok); 486 } 487 488 // Note: The verifier invariants allow us to ignore MemberName.clazz and vmtarget 489 // at this point. And VerifyMethodHandles has already checked clazz, if needed. 490 491 // Get target method and entry point. 492 __ lookup_virtual_method(temp1_recv_klass, temp2_index, Z_method); 493 method_is_live = true; 494 break; 495 } 496 497 case vmIntrinsics::_linkToInterface: { 498 // Same as TemplateTable::invokeinterface, minus the CP setup 499 // and profiling, with different argument motion. 500 if (VerifyMethodHandles) { 501 verify_ref_kind(_masm, JVM_REF_invokeInterface, member_reg, temp3); 502 } 503 504 Register temp3_intf = temp3; 505 506 __ load_heap_oop(temp3_intf, member_clazz, 507 noreg, noreg, IS_NOT_NULL); 508 load_klass_from_Class(_masm, temp3_intf, temp2, temp4); 509 510 Register Z_index = Z_method; 511 512 __ z_lg(Z_index, member_vmindex); 513 514 if (VerifyMethodHandles) { 515 NearLabel L; 516 // if (member_vmindex < 0) stop 517 __ compare32_and_branch(Z_index, constant(0), Assembler::bcondNotLow, L); 518 __ stop("invalid vtable index for MH.invokeInterface"); 519 __ bind(L); 520 } 521 522 // Given interface, index, and recv klass, dispatch to the implementation method. 523 Label L_no_such_interface; 524 __ lookup_interface_method(temp1_recv_klass, temp3_intf, 525 // Note: next two args must be the same: 526 Z_index, Z_method, temp2, 527 L_no_such_interface); 528 jump_from_method_handle(_masm, Z_method, temp2, Z_R0, for_compiler_entry); 529 530 __ bind(L_no_such_interface); 531 532 // Throw exception. 533 __ load_const_optimized(Z_R1, StubRoutines::throw_IncompatibleClassChangeError_entry()); 534 __ z_br(Z_R1); 535 break; 536 } 537 538 default: 539 fatal("unexpected intrinsic %d: %s", vmIntrinsics::as_int(iid), vmIntrinsics::name_at(iid)); 540 break; 541 } 542 543 if (method_is_live) { 544 // Live at this point: Z_method, O5_savedSP (if interpreted). 545 546 // After figuring out which concrete method to call, jump into it. 547 // Note that this works in the interpreter with no data motion. 548 // But the compiled version will require that rcx_recv be shifted out. 549 jump_from_method_handle(_masm, Z_method, temp1, Z_R0, for_compiler_entry); 550 } 551 } 552 553 #ifndef PRODUCT 554 void trace_method_handle_stub(const char* adaptername, 555 oopDesc* mh, 556 intptr_t* sender_sp, 557 intptr_t* args, 558 intptr_t* tracing_fp) { 559 bool has_mh = (strstr(adaptername, "/static") == NULL && 560 strstr(adaptername, "linkTo") == NULL); // Static linkers don't have MH. 561 const char* mh_reg_name = has_mh ? "Z_R4_mh" : "Z_R4"; 562 log_info(methodhandles)("MH %s %s=" INTPTR_FORMAT " sender_sp=" INTPTR_FORMAT " args=" INTPTR_FORMAT, 563 adaptername, mh_reg_name, 564 p2i(mh), p2i(sender_sp), p2i(args)); 565 566 LogTarget(Trace, methodhandles) lt; 567 if (lt.is_enabled()) { 568 // Dumping last frame with frame::describe. 569 ResourceMark rm; 570 LogStream ls(lt); 571 JavaThread* p = JavaThread::active(); 572 573 // may not be needed by safer and unexpensive here 574 PreserveExceptionMark pem(Thread::current()); 575 FrameValues values; 576 577 // Note: We want to allow trace_method_handle from any call site. 578 // While trace_method_handle creates a frame, it may be entered 579 // without a valid return PC in Z_R14 (e.g. not just after a call). 580 // Walking that frame could lead to failures due to that invalid PC. 581 // => carefully detect that frame when doing the stack walking. 582 583 // Walk up to the right frame using the "tracing_fp" argument. 584 frame cur_frame = os::current_frame(); // Current C frame. 585 586 while (cur_frame.fp() != tracing_fp) { 587 cur_frame = os::get_sender_for_C_frame(&cur_frame); 588 } 589 590 // Safely create a frame and call frame::describe. 591 intptr_t *dump_sp = cur_frame.sender_sp(); 592 intptr_t *dump_fp = cur_frame.link(); 593 594 bool walkable = has_mh; // Whether the traced frame should be walkable. 595 596 // The sender for cur_frame is the caller of trace_method_handle. 597 if (walkable) { 598 // The previous definition of walkable may have to be refined 599 // if new call sites cause the next frame constructor to start 600 // failing. Alternatively, frame constructors could be 601 // modified to support the current or future non walkable 602 // frames (but this is more intrusive and is not considered as 603 // part of this RFE, which will instead use a simpler output). 604 frame dump_frame = frame(dump_sp); 605 dump_frame.describe(values, 1); 606 } else { 607 // Robust dump for frames which cannot be constructed from sp/younger_sp 608 // Add descriptions without building a Java frame to avoid issues. 609 values.describe(-1, dump_fp, "fp for #1 <not parsed, cannot trust pc>"); 610 values.describe(-1, dump_sp, "sp"); 611 } 612 613 bool has_args = has_mh; // Whether Z_esp is meaningful. 614 615 // Mark args, if seems valid (may not be valid for some adapters). 616 if (has_args) { 617 if ((args >= dump_sp) && (args < dump_fp)) { 618 values.describe(-1, args, "*Z_esp"); 619 } 620 } 621 622 // Note: the unextended_sp may not be correct. 623 ls.print_cr(" stack layout:"); 624 values.print_on(p, &ls); 625 if (has_mh && oopDesc::is_oop(mh)) { 626 mh->print_on(&ls); 627 if (java_lang_invoke_MethodHandle::is_instance(mh)) { 628 java_lang_invoke_MethodHandle::form(mh)->print_on(&ls); 629 } 630 } 631 } 632 } 633 634 void MethodHandles::trace_method_handle(MacroAssembler* _masm, const char* adaptername) { 635 if (!log_is_enabled(Info, methodhandles)) { return; } 636 637 // If arg registers are contiguous, we can use STMG/LMG. 638 assert((Z_ARG5->encoding() - Z_ARG1->encoding() + 1) == RegisterImpl::number_of_arg_registers, "Oops"); 639 640 BLOCK_COMMENT("trace_method_handle {"); 641 642 // Save argument registers (they are used in raise exception stub). 643 // Argument registers have contiguous register numbers -> we can use stmg/lmg. 644 __ z_stmg(Z_ARG1, Z_ARG5, 16, Z_SP); 645 646 // Setup arguments. 647 __ z_lgr(Z_ARG2, Z_ARG4); // mh, see generate_method_handle_interpreter_entry() 648 __ z_lgr(Z_ARG3, Z_R10); // sender_sp 649 __ z_lgr(Z_ARG4, Z_esp); 650 __ load_const_optimized(Z_ARG1, (void *)adaptername); 651 __ z_lgr(Z_ARG5, Z_SP); // tracing_fp 652 __ save_return_pc(); // saves Z_R14 653 __ push_frame_abi160(0); 654 __ call_VM_leaf(CAST_FROM_FN_PTR(address, trace_method_handle_stub)); 655 __ pop_frame(); 656 __ restore_return_pc(); // restores to Z_R14 657 658 // Restore argument registers 659 __ z_lmg(Z_ARG1, Z_ARG5, 16, Z_SP); 660 __ zap_from_to(Z_SP, Z_SP, Z_R0, Z_R1, 50, -1); 661 __ zap_from_to(Z_SP, Z_SP, Z_R0, Z_R1, -1, 5); 662 663 BLOCK_COMMENT("} trace_method_handle"); 664 } 665 #endif // !PRODUCT