1 /* 2 * Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved. 3 * Copyright (c) 2014, Red Hat Inc. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 * 24 */ 25 26 #include "precompiled.hpp" 27 #include "asm/macroAssembler.hpp" 28 #include "classfile/javaClasses.inline.hpp" 29 #include "classfile/vmClasses.hpp" 30 #include "compiler/disassembler.hpp" 31 #include "interpreter/interpreter.hpp" 32 #include "interpreter/interpreterRuntime.hpp" 33 #include "memory/allocation.inline.hpp" 34 #include "prims/jvmtiExport.hpp" 35 #include "prims/methodHandles.hpp" 36 #include "runtime/flags/flagSetting.hpp" 37 #include "runtime/frame.inline.hpp" 38 #include "runtime/stubRoutines.hpp" 39 40 #define __ Disassembler::hook<MacroAssembler>(__FILE__, __LINE__, _masm)-> 41 42 #ifdef PRODUCT 43 #define BLOCK_COMMENT(str) /* nothing */ 44 #else 45 #define BLOCK_COMMENT(str) __ block_comment(str) 46 #endif 47 48 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":") 49 50 void MethodHandles::load_klass_from_Class(MacroAssembler* _masm, Register klass_reg) { 51 if (VerifyMethodHandles) 52 verify_klass(_masm, klass_reg, VM_CLASS_ID(java_lang_Class), 53 "MH argument is a Class"); 54 __ ldr(klass_reg, Address(klass_reg, java_lang_Class::klass_offset())); 55 } 56 57 #ifdef ASSERT 58 static int check_nonzero(const char* xname, int x) { 59 assert(x != 0, "%s should be nonzero", xname); 60 return x; 61 } 62 #define NONZERO(x) check_nonzero(#x, x) 63 #else //ASSERT 64 #define NONZERO(x) (x) 65 #endif //PRODUCT 66 67 #ifdef ASSERT 68 void MethodHandles::verify_klass(MacroAssembler* _masm, 69 Register obj, vmClassID klass_id, 70 const char* error_message) { 71 InstanceKlass** klass_addr = vmClasses::klass_addr_at(klass_id); 72 Klass* klass = vmClasses::klass_at(klass_id); 73 Register temp = rscratch2; 74 Register temp2 = rscratch1; // used by MacroAssembler::cmpptr 75 Label L_ok, L_bad; 76 BLOCK_COMMENT("verify_klass {"); 77 __ verify_oop(obj); 78 __ cbz(obj, L_bad); 79 __ push(RegSet::of(temp, temp2), sp); 80 __ load_klass(temp, obj); 81 __ cmpptr(temp, ExternalAddress((address) klass_addr)); 82 __ br(Assembler::EQ, L_ok); 83 intptr_t super_check_offset = klass->super_check_offset(); 84 __ ldr(temp, Address(temp, super_check_offset)); 85 __ cmpptr(temp, ExternalAddress((address) klass_addr)); 86 __ br(Assembler::EQ, L_ok); 87 __ pop(RegSet::of(temp, temp2), sp); 88 __ bind(L_bad); 89 __ stop(error_message); 90 __ BIND(L_ok); 91 __ pop(RegSet::of(temp, temp2), sp); 92 BLOCK_COMMENT("} verify_klass"); 93 } 94 95 void MethodHandles::verify_ref_kind(MacroAssembler* _masm, int ref_kind, Register member_reg, Register temp) { } 96 97 #endif //ASSERT 98 99 void MethodHandles::jump_from_method_handle(MacroAssembler* _masm, Register method, Register temp, 100 bool for_compiler_entry) { 101 assert(method == rmethod, "interpreter calling convention"); 102 Label L_no_such_method; 103 __ cbz(rmethod, L_no_such_method); 104 __ verify_method_ptr(method); 105 106 if (!for_compiler_entry && JvmtiExport::can_post_interpreter_events()) { 107 Label run_compiled_code; 108 // JVMTI events, such as single-stepping, are implemented partly by avoiding running 109 // compiled code in threads for which the event is enabled. Check here for 110 // interp_only_mode if these events CAN be enabled. 111 112 __ ldrw(rscratch1, Address(rthread, JavaThread::interp_only_mode_offset())); 113 __ cbzw(rscratch1, run_compiled_code); 114 __ ldr(rscratch1, Address(method, Method::interpreter_entry_offset())); 115 __ br(rscratch1); 116 __ BIND(run_compiled_code); 117 } 118 119 // The following jump might pass an inline type argument that was erased to Object as oop to a 120 // callee that expects inline type arguments to be passed as fields. We need to call the compiled 121 // value entry (_code->inline_entry_point() or _adapter->c2i_inline_entry()) which will take care 122 // of translating between the calling conventions. 123 const ByteSize entry_offset = for_compiler_entry ? Method::from_compiled_inline_offset() : 124 Method::from_interpreted_offset(); 125 __ ldr(rscratch1,Address(method, entry_offset)); 126 __ br(rscratch1); 127 __ bind(L_no_such_method); 128 __ far_jump(RuntimeAddress(SharedRuntime::throw_AbstractMethodError_entry())); 129 } 130 131 void MethodHandles::jump_to_lambda_form(MacroAssembler* _masm, 132 Register recv, Register method_temp, 133 Register temp2, 134 bool for_compiler_entry) { 135 BLOCK_COMMENT("jump_to_lambda_form {"); 136 // This is the initial entry point of a lazy method handle. 137 // After type checking, it picks up the invoker from the LambdaForm. 138 assert_different_registers(recv, method_temp, temp2); 139 assert(recv != noreg, "required register"); 140 assert(method_temp == rmethod, "required register for loading method"); 141 142 // Load the invoker, as MH -> MH.form -> LF.vmentry 143 __ verify_oop(recv); 144 __ load_heap_oop(method_temp, Address(recv, NONZERO(java_lang_invoke_MethodHandle::form_offset())), temp2, rscratch2); 145 __ verify_oop(method_temp); 146 __ load_heap_oop(method_temp, Address(method_temp, NONZERO(java_lang_invoke_LambdaForm::vmentry_offset())), temp2, rscratch2); 147 __ verify_oop(method_temp); 148 __ load_heap_oop(method_temp, Address(method_temp, NONZERO(java_lang_invoke_MemberName::method_offset())), temp2, rscratch2); 149 __ verify_oop(method_temp); 150 __ access_load_at(T_ADDRESS, IN_HEAP, method_temp, Address(method_temp, NONZERO(java_lang_invoke_ResolvedMethodName::vmtarget_offset())), noreg, noreg); 151 152 if (VerifyMethodHandles && !for_compiler_entry) { 153 // make sure recv is already on stack 154 __ ldr(temp2, Address(method_temp, Method::const_offset())); 155 __ load_sized_value(temp2, 156 Address(temp2, ConstMethod::size_of_parameters_offset()), 157 sizeof(u2), /*is_signed*/ false); 158 // assert(sizeof(u2) == sizeof(Method::_size_of_parameters), ""); 159 Label L; 160 __ ldr(rscratch1, __ argument_address(temp2, -1)); 161 __ cmpoop(recv, rscratch1); 162 __ br(Assembler::EQ, L); 163 __ ldr(r0, __ argument_address(temp2, -1)); 164 __ hlt(0); 165 __ BIND(L); 166 } 167 168 jump_from_method_handle(_masm, method_temp, temp2, for_compiler_entry); 169 BLOCK_COMMENT("} jump_to_lambda_form"); 170 } 171 172 // Code generation 173 address MethodHandles::generate_method_handle_interpreter_entry(MacroAssembler* _masm, 174 vmIntrinsics::ID iid) { 175 const bool not_for_compiler_entry = false; // this is the interpreter entry 176 assert(is_signature_polymorphic(iid), "expected invoke iid"); 177 if (iid == vmIntrinsics::_invokeGeneric || 178 iid == vmIntrinsics::_compiledLambdaForm) { 179 // Perhaps surprisingly, the symbolic references visible to Java are not directly used. 180 // They are linked to Java-generated adapters via MethodHandleNatives.linkMethod. 181 // They all allow an appendix argument. 182 __ hlt(0); // empty stubs make SG sick 183 return nullptr; 184 } 185 186 // No need in interpreter entry for linkToNative for now. 187 // Interpreter calls compiled entry through i2c. 188 if (iid == vmIntrinsics::_linkToNative) { 189 __ hlt(0); 190 return nullptr; 191 } 192 193 // r19_sender_sp: sender SP (must preserve; see prepare_to_jump_from_interpreted) 194 // rmethod: Method* 195 // r3: argument locator (parameter slot count, added to rsp) 196 // r1: used as temp to hold mh or receiver 197 // r0, r11: garbage temps, blown away 198 Register argp = r3; // argument list ptr, live on error paths 199 Register temp = r0; 200 Register mh = r1; // MH receiver; dies quickly and is recycled 201 202 // here's where control starts out: 203 __ align(CodeEntryAlignment); 204 address entry_point = __ pc(); 205 206 if (VerifyMethodHandles) { 207 assert(Method::intrinsic_id_size_in_bytes() == 2, "assuming Method::_intrinsic_id is u2"); 208 209 Label L; 210 BLOCK_COMMENT("verify_intrinsic_id {"); 211 __ ldrh(rscratch1, Address(rmethod, Method::intrinsic_id_offset())); 212 __ subs(zr, rscratch1, (int) iid); 213 __ br(Assembler::EQ, L); 214 if (iid == vmIntrinsics::_linkToVirtual || 215 iid == vmIntrinsics::_linkToSpecial) { 216 // could do this for all kinds, but would explode assembly code size 217 trace_method_handle(_masm, "bad Method*::intrinsic_id"); 218 } 219 __ hlt(0); 220 __ bind(L); 221 BLOCK_COMMENT("} verify_intrinsic_id"); 222 } 223 224 // First task: Find out how big the argument list is. 225 Address r3_first_arg_addr; 226 int ref_kind = signature_polymorphic_intrinsic_ref_kind(iid); 227 assert(ref_kind != 0 || iid == vmIntrinsics::_invokeBasic, "must be _invokeBasic or a linkTo intrinsic"); 228 if (ref_kind == 0 || MethodHandles::ref_kind_has_receiver(ref_kind)) { 229 __ ldr(argp, Address(rmethod, Method::const_offset())); 230 __ load_sized_value(argp, 231 Address(argp, ConstMethod::size_of_parameters_offset()), 232 sizeof(u2), /*is_signed*/ false); 233 // assert(sizeof(u2) == sizeof(Method::_size_of_parameters), ""); 234 r3_first_arg_addr = __ argument_address(argp, -1); 235 } else { 236 DEBUG_ONLY(argp = noreg); 237 } 238 239 if (!is_signature_polymorphic_static(iid)) { 240 __ ldr(mh, r3_first_arg_addr); 241 DEBUG_ONLY(argp = noreg); 242 } 243 244 // r3_first_arg_addr is live! 245 246 trace_method_handle_interpreter_entry(_masm, iid); 247 if (iid == vmIntrinsics::_invokeBasic) { 248 generate_method_handle_dispatch(_masm, iid, mh, noreg, not_for_compiler_entry); 249 250 } else { 251 // Adjust argument list by popping the trailing MemberName argument. 252 Register recv = noreg; 253 if (MethodHandles::ref_kind_has_receiver(ref_kind)) { 254 // Load the receiver (not the MH; the actual MemberName's receiver) up from the interpreter stack. 255 __ ldr(recv = r2, r3_first_arg_addr); 256 } 257 DEBUG_ONLY(argp = noreg); 258 Register rmember = rmethod; // MemberName ptr; incoming method ptr is dead now 259 __ pop(rmember); // extract last argument 260 generate_method_handle_dispatch(_masm, iid, recv, rmember, not_for_compiler_entry); 261 } 262 263 return entry_point; 264 } 265 266 void MethodHandles::jump_to_native_invoker(MacroAssembler* _masm, Register nep_reg, Register temp_target) { 267 BLOCK_COMMENT("jump_to_native_invoker {"); 268 assert_different_registers(nep_reg, temp_target); 269 assert(nep_reg != noreg, "required register"); 270 271 // Load the invoker, as NEP -> .invoker 272 __ verify_oop(nep_reg); 273 __ access_load_at(T_ADDRESS, IN_HEAP, temp_target, 274 Address(nep_reg, NONZERO(jdk_internal_foreign_abi_NativeEntryPoint::downcall_stub_address_offset_in_bytes())), 275 noreg, noreg); 276 277 __ br(temp_target); 278 BLOCK_COMMENT("} jump_to_native_invoker"); 279 } 280 281 282 void MethodHandles::generate_method_handle_dispatch(MacroAssembler* _masm, 283 vmIntrinsics::ID iid, 284 Register receiver_reg, 285 Register member_reg, 286 bool for_compiler_entry) { 287 assert(is_signature_polymorphic(iid), "expected invoke iid"); 288 // temps used in this code are not used in *either* compiled or interpreted calling sequences 289 Register temp1 = r10; 290 Register temp2 = r11; 291 Register temp3 = r14; 292 if (for_compiler_entry) { 293 assert(receiver_reg == (iid == vmIntrinsics::_linkToStatic || iid == vmIntrinsics::_linkToNative ? noreg : j_rarg0), "only valid assignment"); 294 assert_different_registers(temp1, j_rarg0, j_rarg1, j_rarg2, j_rarg3, j_rarg4, j_rarg5, j_rarg6, j_rarg7); 295 assert_different_registers(temp2, j_rarg0, j_rarg1, j_rarg2, j_rarg3, j_rarg4, j_rarg5, j_rarg6, j_rarg7); 296 assert_different_registers(temp3, j_rarg0, j_rarg1, j_rarg2, j_rarg3, j_rarg4, j_rarg5, j_rarg6, j_rarg7); 297 } 298 299 assert_different_registers(temp1, temp2, temp3, receiver_reg); 300 assert_different_registers(temp1, temp2, temp3, member_reg); 301 302 if (iid == vmIntrinsics::_invokeBasic) { 303 // indirect through MH.form.vmentry.vmtarget 304 jump_to_lambda_form(_masm, receiver_reg, rmethod, temp1, for_compiler_entry); 305 306 } else if (iid == vmIntrinsics::_linkToNative) { 307 assert(for_compiler_entry, "only compiler entry is supported"); 308 jump_to_native_invoker(_masm, member_reg, temp1); 309 } else { 310 // The method is a member invoker used by direct method handles. 311 if (VerifyMethodHandles) { 312 // make sure the trailing argument really is a MemberName (caller responsibility) 313 verify_klass(_masm, member_reg, VM_CLASS_ID(java_lang_invoke_MemberName), 314 "MemberName required for invokeVirtual etc."); 315 } 316 317 Address member_clazz( member_reg, NONZERO(java_lang_invoke_MemberName::clazz_offset())); 318 Address member_vmindex( member_reg, NONZERO(java_lang_invoke_MemberName::vmindex_offset())); 319 Address member_vmtarget( member_reg, NONZERO(java_lang_invoke_MemberName::method_offset())); 320 Address vmtarget_method( rmethod, NONZERO(java_lang_invoke_ResolvedMethodName::vmtarget_offset())); 321 322 Register temp1_recv_klass = temp1; 323 if (iid != vmIntrinsics::_linkToStatic) { 324 __ verify_oop(receiver_reg); 325 if (iid == vmIntrinsics::_linkToSpecial) { 326 // Don't actually load the klass; just null-check the receiver. 327 __ null_check(receiver_reg); 328 } else { 329 // load receiver klass itself 330 __ load_klass(temp1_recv_klass, receiver_reg); 331 __ verify_klass_ptr(temp1_recv_klass); 332 } 333 BLOCK_COMMENT("check_receiver {"); 334 // The receiver for the MemberName must be in receiver_reg. 335 // Check the receiver against the MemberName.clazz 336 if (VerifyMethodHandles && iid == vmIntrinsics::_linkToSpecial) { 337 // Did not load it above... 338 __ load_klass(temp1_recv_klass, receiver_reg); 339 __ verify_klass_ptr(temp1_recv_klass); 340 } 341 if (VerifyMethodHandles && iid != vmIntrinsics::_linkToInterface) { 342 Label L_ok; 343 Register temp2_defc = temp2; 344 __ load_heap_oop(temp2_defc, member_clazz, temp3, rscratch2); 345 load_klass_from_Class(_masm, temp2_defc); 346 __ verify_klass_ptr(temp2_defc); 347 __ check_klass_subtype(temp1_recv_klass, temp2_defc, temp3, L_ok); 348 // If we get here, the type check failed! 349 __ hlt(0); 350 // __ STOP("receiver class disagrees with MemberName.clazz"); 351 __ bind(L_ok); 352 } 353 BLOCK_COMMENT("} check_receiver"); 354 } 355 if (iid == vmIntrinsics::_linkToSpecial || 356 iid == vmIntrinsics::_linkToStatic) { 357 DEBUG_ONLY(temp1_recv_klass = noreg); // these guys didn't load the recv_klass 358 } 359 360 // Live registers at this point: 361 // member_reg - MemberName that was the trailing argument 362 // temp1_recv_klass - klass of stacked receiver, if needed 363 // r19 - interpreter linkage (if interpreted) 364 // r1 ... r0 - compiler arguments (if compiled) 365 366 Label L_incompatible_class_change_error; 367 switch (iid) { 368 case vmIntrinsics::_linkToSpecial: 369 if (VerifyMethodHandles) { 370 verify_ref_kind(_masm, JVM_REF_invokeSpecial, member_reg, temp3); 371 } 372 __ load_heap_oop(rmethod, member_vmtarget, temp3, rscratch2); 373 __ access_load_at(T_ADDRESS, IN_HEAP, rmethod, vmtarget_method, noreg, noreg); 374 break; 375 376 case vmIntrinsics::_linkToStatic: 377 if (VerifyMethodHandles) { 378 verify_ref_kind(_masm, JVM_REF_invokeStatic, member_reg, temp3); 379 } 380 __ load_heap_oop(rmethod, member_vmtarget, temp3, rscratch2); 381 __ access_load_at(T_ADDRESS, IN_HEAP, rmethod, vmtarget_method, noreg, noreg); 382 break; 383 384 case vmIntrinsics::_linkToVirtual: 385 { 386 // same as TemplateTable::invokevirtual, 387 // minus the CP setup and profiling: 388 389 if (VerifyMethodHandles) { 390 verify_ref_kind(_masm, JVM_REF_invokeVirtual, member_reg, temp3); 391 } 392 393 // pick out the vtable index from the MemberName, and then we can discard it: 394 Register temp2_index = temp2; 395 __ access_load_at(T_ADDRESS, IN_HEAP, temp2_index, member_vmindex, noreg, noreg); 396 397 if (VerifyMethodHandles) { 398 Label L_index_ok; 399 __ cmpw(temp2_index, 0U); 400 __ br(Assembler::GE, L_index_ok); 401 __ hlt(0); 402 __ BIND(L_index_ok); 403 } 404 405 // Note: The verifier invariants allow us to ignore MemberName.clazz and vmtarget 406 // at this point. And VerifyMethodHandles has already checked clazz, if needed. 407 408 // get target Method* & entry point 409 __ lookup_virtual_method(temp1_recv_klass, temp2_index, rmethod); 410 break; 411 } 412 413 case vmIntrinsics::_linkToInterface: 414 { 415 // same as TemplateTable::invokeinterface 416 // (minus the CP setup and profiling, with different argument motion) 417 if (VerifyMethodHandles) { 418 verify_ref_kind(_masm, JVM_REF_invokeInterface, member_reg, temp3); 419 } 420 421 Register temp3_intf = temp3; 422 __ load_heap_oop(temp3_intf, member_clazz, temp2, rscratch2); 423 load_klass_from_Class(_masm, temp3_intf); 424 __ verify_klass_ptr(temp3_intf); 425 426 Register rindex = rmethod; 427 __ access_load_at(T_ADDRESS, IN_HEAP, rindex, member_vmindex, noreg, noreg); 428 if (VerifyMethodHandles) { 429 Label L; 430 __ cmpw(rindex, 0U); 431 __ br(Assembler::GE, L); 432 __ hlt(0); 433 __ bind(L); 434 } 435 436 // given intf, index, and recv klass, dispatch to the implementation method 437 __ lookup_interface_method(temp1_recv_klass, temp3_intf, 438 // note: next two args must be the same: 439 rindex, rmethod, 440 temp2, 441 L_incompatible_class_change_error); 442 break; 443 } 444 445 default: 446 fatal("unexpected intrinsic %d: %s", vmIntrinsics::as_int(iid), vmIntrinsics::name_at(iid)); 447 break; 448 } 449 450 // live at this point: rmethod, r19_sender_sp (if interpreted) 451 452 // After figuring out which concrete method to call, jump into it. 453 // Note that this works in the interpreter with no data motion. 454 // But the compiled version will require that r2_recv be shifted out. 455 __ verify_method_ptr(rmethod); 456 jump_from_method_handle(_masm, rmethod, temp1, for_compiler_entry); 457 if (iid == vmIntrinsics::_linkToInterface) { 458 __ bind(L_incompatible_class_change_error); 459 __ far_jump(RuntimeAddress(SharedRuntime::throw_IncompatibleClassChangeError_entry())); 460 } 461 } 462 } 463 464 #ifndef PRODUCT 465 void trace_method_handle_stub(const char* adaptername, 466 oopDesc* mh, 467 intptr_t* saved_regs, 468 intptr_t* entry_sp) { } 469 470 // The stub wraps the arguments in a struct on the stack to avoid 471 // dealing with the different calling conventions for passing 6 472 // arguments. 473 struct MethodHandleStubArguments { 474 const char* adaptername; 475 oopDesc* mh; 476 intptr_t* saved_regs; 477 intptr_t* entry_sp; 478 }; 479 void trace_method_handle_stub_wrapper(MethodHandleStubArguments* args) { } 480 481 void MethodHandles::trace_method_handle(MacroAssembler* _masm, const char* adaptername) { } 482 #endif //PRODUCT