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 const ByteSize entry_offset = for_compiler_entry ? Method::from_compiled_offset() : 120 Method::from_interpreted_offset(); 121 __ ldr(rscratch1,Address(method, entry_offset)); 122 __ br(rscratch1); 123 __ bind(L_no_such_method); 124 __ far_jump(RuntimeAddress(SharedRuntime::throw_AbstractMethodError_entry())); 125 } 126 127 void MethodHandles::jump_to_lambda_form(MacroAssembler* _masm, 128 Register recv, Register method_temp, 129 Register temp2, 130 bool for_compiler_entry) { 131 BLOCK_COMMENT("jump_to_lambda_form {"); 132 // This is the initial entry point of a lazy method handle. 133 // After type checking, it picks up the invoker from the LambdaForm. 134 assert_different_registers(recv, method_temp, temp2); 135 assert(recv != noreg, "required register"); 136 assert(method_temp == rmethod, "required register for loading method"); 137 138 // Load the invoker, as MH -> MH.form -> LF.vmentry 139 __ verify_oop(recv); 140 __ load_heap_oop(method_temp, Address(recv, NONZERO(java_lang_invoke_MethodHandle::form_offset())), temp2, rscratch2); 141 __ verify_oop(method_temp); 142 __ load_heap_oop(method_temp, Address(method_temp, NONZERO(java_lang_invoke_LambdaForm::vmentry_offset())), temp2, rscratch2); 143 __ verify_oop(method_temp); 144 __ load_heap_oop(method_temp, Address(method_temp, NONZERO(java_lang_invoke_MemberName::method_offset())), temp2, rscratch2); 145 __ verify_oop(method_temp); 146 __ access_load_at(T_ADDRESS, IN_HEAP, method_temp, Address(method_temp, NONZERO(java_lang_invoke_ResolvedMethodName::vmtarget_offset())), noreg, noreg); 147 148 if (VerifyMethodHandles && !for_compiler_entry) { 149 // make sure recv is already on stack 150 __ ldr(temp2, Address(method_temp, Method::const_offset())); 151 __ load_sized_value(temp2, 152 Address(temp2, ConstMethod::size_of_parameters_offset()), 153 sizeof(u2), /*is_signed*/ false); 154 // assert(sizeof(u2) == sizeof(Method::_size_of_parameters), ""); 155 Label L; 156 __ ldr(rscratch1, __ argument_address(temp2, -1)); 157 __ cmpoop(recv, rscratch1); 158 __ br(Assembler::EQ, L); 159 __ ldr(r0, __ argument_address(temp2, -1)); 160 __ hlt(0); 161 __ BIND(L); 162 } 163 164 jump_from_method_handle(_masm, method_temp, temp2, for_compiler_entry); 165 BLOCK_COMMENT("} jump_to_lambda_form"); 166 } 167 168 // Code generation 169 address MethodHandles::generate_method_handle_interpreter_entry(MacroAssembler* _masm, 170 vmIntrinsics::ID iid) { 171 const bool not_for_compiler_entry = false; // this is the interpreter entry 172 assert(is_signature_polymorphic(iid), "expected invoke iid"); 173 if (iid == vmIntrinsics::_invokeGeneric || 174 iid == vmIntrinsics::_compiledLambdaForm) { 175 // Perhaps surprisingly, the symbolic references visible to Java are not directly used. 176 // They are linked to Java-generated adapters via MethodHandleNatives.linkMethod. 177 // They all allow an appendix argument. 178 __ hlt(0); // empty stubs make SG sick 179 return nullptr; 180 } 181 182 // No need in interpreter entry for linkToNative for now. 183 // Interpreter calls compiled entry through i2c. 184 if (iid == vmIntrinsics::_linkToNative) { 185 __ hlt(0); 186 return nullptr; 187 } 188 189 // r19_sender_sp: sender SP (must preserve; see prepare_to_jump_from_interpreted) 190 // rmethod: Method* 191 // r3: argument locator (parameter slot count, added to rsp) 192 // r1: used as temp to hold mh or receiver 193 // r0, r11: garbage temps, blown away 194 Register argp = r3; // argument list ptr, live on error paths 195 Register temp = r0; 196 Register mh = r1; // MH receiver; dies quickly and is recycled 197 198 // here's where control starts out: 199 __ align(CodeEntryAlignment); 200 address entry_point = __ pc(); 201 202 if (VerifyMethodHandles) { 203 assert(Method::intrinsic_id_size_in_bytes() == 2, "assuming Method::_intrinsic_id is u2"); 204 205 Label L; 206 BLOCK_COMMENT("verify_intrinsic_id {"); 207 __ ldrh(rscratch1, Address(rmethod, Method::intrinsic_id_offset())); 208 __ subs(zr, rscratch1, (int) iid); 209 __ br(Assembler::EQ, L); 210 if (iid == vmIntrinsics::_linkToVirtual || 211 iid == vmIntrinsics::_linkToSpecial) { 212 // could do this for all kinds, but would explode assembly code size 213 trace_method_handle(_masm, "bad Method*::intrinsic_id"); 214 } 215 __ hlt(0); 216 __ bind(L); 217 BLOCK_COMMENT("} verify_intrinsic_id"); 218 } 219 220 // First task: Find out how big the argument list is. 221 Address r3_first_arg_addr; 222 int ref_kind = signature_polymorphic_intrinsic_ref_kind(iid); 223 assert(ref_kind != 0 || iid == vmIntrinsics::_invokeBasic, "must be _invokeBasic or a linkTo intrinsic"); 224 if (ref_kind == 0 || MethodHandles::ref_kind_has_receiver(ref_kind)) { 225 __ ldr(argp, Address(rmethod, Method::const_offset())); 226 __ load_sized_value(argp, 227 Address(argp, ConstMethod::size_of_parameters_offset()), 228 sizeof(u2), /*is_signed*/ false); 229 // assert(sizeof(u2) == sizeof(Method::_size_of_parameters), ""); 230 r3_first_arg_addr = __ argument_address(argp, -1); 231 } else { 232 DEBUG_ONLY(argp = noreg); 233 } 234 235 if (!is_signature_polymorphic_static(iid)) { 236 __ ldr(mh, r3_first_arg_addr); 237 DEBUG_ONLY(argp = noreg); 238 } 239 240 // r3_first_arg_addr is live! 241 242 trace_method_handle_interpreter_entry(_masm, iid); 243 if (iid == vmIntrinsics::_invokeBasic) { 244 generate_method_handle_dispatch(_masm, iid, mh, noreg, not_for_compiler_entry); 245 246 } else { 247 // Adjust argument list by popping the trailing MemberName argument. 248 Register recv = noreg; 249 if (MethodHandles::ref_kind_has_receiver(ref_kind)) { 250 // Load the receiver (not the MH; the actual MemberName's receiver) up from the interpreter stack. 251 __ ldr(recv = r2, r3_first_arg_addr); 252 } 253 DEBUG_ONLY(argp = noreg); 254 Register rmember = rmethod; // MemberName ptr; incoming method ptr is dead now 255 __ pop(rmember); // extract last argument 256 generate_method_handle_dispatch(_masm, iid, recv, rmember, not_for_compiler_entry); 257 } 258 259 return entry_point; 260 } 261 262 void MethodHandles::jump_to_native_invoker(MacroAssembler* _masm, Register nep_reg, Register temp_target) { 263 BLOCK_COMMENT("jump_to_native_invoker {"); 264 assert_different_registers(nep_reg, temp_target); 265 assert(nep_reg != noreg, "required register"); 266 267 // Load the invoker, as NEP -> .invoker 268 __ verify_oop(nep_reg); 269 __ access_load_at(T_ADDRESS, IN_HEAP, temp_target, 270 Address(nep_reg, NONZERO(jdk_internal_foreign_abi_NativeEntryPoint::downcall_stub_address_offset_in_bytes())), 271 noreg, noreg); 272 273 __ br(temp_target); 274 BLOCK_COMMENT("} jump_to_native_invoker"); 275 } 276 277 278 void MethodHandles::generate_method_handle_dispatch(MacroAssembler* _masm, 279 vmIntrinsics::ID iid, 280 Register receiver_reg, 281 Register member_reg, 282 bool for_compiler_entry) { 283 assert(is_signature_polymorphic(iid), "expected invoke iid"); 284 // temps used in this code are not used in *either* compiled or interpreted calling sequences 285 Register temp1 = r10; 286 Register temp2 = r11; 287 Register temp3 = r14; 288 if (for_compiler_entry) { 289 assert(receiver_reg == (iid == vmIntrinsics::_linkToStatic || iid == vmIntrinsics::_linkToNative ? noreg : j_rarg0), "only valid assignment"); 290 assert_different_registers(temp1, j_rarg0, j_rarg1, j_rarg2, j_rarg3, j_rarg4, j_rarg5, j_rarg6, j_rarg7); 291 assert_different_registers(temp2, j_rarg0, j_rarg1, j_rarg2, j_rarg3, j_rarg4, j_rarg5, j_rarg6, j_rarg7); 292 assert_different_registers(temp3, j_rarg0, j_rarg1, j_rarg2, j_rarg3, j_rarg4, j_rarg5, j_rarg6, j_rarg7); 293 } 294 295 assert_different_registers(temp1, temp2, temp3, receiver_reg); 296 assert_different_registers(temp1, temp2, temp3, member_reg); 297 298 if (iid == vmIntrinsics::_invokeBasic) { 299 // indirect through MH.form.vmentry.vmtarget 300 jump_to_lambda_form(_masm, receiver_reg, rmethod, temp1, for_compiler_entry); 301 302 } else if (iid == vmIntrinsics::_linkToNative) { 303 assert(for_compiler_entry, "only compiler entry is supported"); 304 jump_to_native_invoker(_masm, member_reg, temp1); 305 } else { 306 // The method is a member invoker used by direct method handles. 307 if (VerifyMethodHandles) { 308 // make sure the trailing argument really is a MemberName (caller responsibility) 309 verify_klass(_masm, member_reg, VM_CLASS_ID(java_lang_invoke_MemberName), 310 "MemberName required for invokeVirtual etc."); 311 } 312 313 Address member_clazz( member_reg, NONZERO(java_lang_invoke_MemberName::clazz_offset())); 314 Address member_vmindex( member_reg, NONZERO(java_lang_invoke_MemberName::vmindex_offset())); 315 Address member_vmtarget( member_reg, NONZERO(java_lang_invoke_MemberName::method_offset())); 316 Address vmtarget_method( rmethod, NONZERO(java_lang_invoke_ResolvedMethodName::vmtarget_offset())); 317 318 Register temp1_recv_klass = temp1; 319 if (iid != vmIntrinsics::_linkToStatic) { 320 __ verify_oop(receiver_reg); 321 if (iid == vmIntrinsics::_linkToSpecial) { 322 // Don't actually load the klass; just null-check the receiver. 323 __ null_check(receiver_reg); 324 } else { 325 // load receiver klass itself 326 __ load_klass(temp1_recv_klass, receiver_reg); 327 __ verify_klass_ptr(temp1_recv_klass); 328 } 329 BLOCK_COMMENT("check_receiver {"); 330 // The receiver for the MemberName must be in receiver_reg. 331 // Check the receiver against the MemberName.clazz 332 if (VerifyMethodHandles && iid == vmIntrinsics::_linkToSpecial) { 333 // Did not load it above... 334 __ load_klass(temp1_recv_klass, receiver_reg); 335 __ verify_klass_ptr(temp1_recv_klass); 336 } 337 if (VerifyMethodHandles && iid != vmIntrinsics::_linkToInterface) { 338 Label L_ok; 339 Register temp2_defc = temp2; 340 __ load_heap_oop(temp2_defc, member_clazz, temp3, rscratch2); 341 load_klass_from_Class(_masm, temp2_defc); 342 __ verify_klass_ptr(temp2_defc); 343 __ check_klass_subtype(temp1_recv_klass, temp2_defc, temp3, L_ok); 344 // If we get here, the type check failed! 345 __ hlt(0); 346 // __ STOP("receiver class disagrees with MemberName.clazz"); 347 __ bind(L_ok); 348 } 349 BLOCK_COMMENT("} check_receiver"); 350 } 351 if (iid == vmIntrinsics::_linkToSpecial || 352 iid == vmIntrinsics::_linkToStatic) { 353 DEBUG_ONLY(temp1_recv_klass = noreg); // these guys didn't load the recv_klass 354 } 355 356 // Live registers at this point: 357 // member_reg - MemberName that was the trailing argument 358 // temp1_recv_klass - klass of stacked receiver, if needed 359 // r19 - interpreter linkage (if interpreted) 360 // r1 ... r0 - compiler arguments (if compiled) 361 362 Label L_incompatible_class_change_error; 363 switch (iid) { 364 case vmIntrinsics::_linkToSpecial: 365 if (VerifyMethodHandles) { 366 verify_ref_kind(_masm, JVM_REF_invokeSpecial, member_reg, temp3); 367 } 368 __ load_heap_oop(rmethod, member_vmtarget, temp3, rscratch2); 369 __ access_load_at(T_ADDRESS, IN_HEAP, rmethod, vmtarget_method, noreg, noreg); 370 break; 371 372 case vmIntrinsics::_linkToStatic: 373 if (VerifyMethodHandles) { 374 verify_ref_kind(_masm, JVM_REF_invokeStatic, member_reg, temp3); 375 } 376 __ load_heap_oop(rmethod, member_vmtarget, temp3, rscratch2); 377 __ access_load_at(T_ADDRESS, IN_HEAP, rmethod, vmtarget_method, noreg, noreg); 378 break; 379 380 case vmIntrinsics::_linkToVirtual: 381 { 382 // same as TemplateTable::invokevirtual, 383 // minus the CP setup and profiling: 384 385 if (VerifyMethodHandles) { 386 verify_ref_kind(_masm, JVM_REF_invokeVirtual, member_reg, temp3); 387 } 388 389 // pick out the vtable index from the MemberName, and then we can discard it: 390 Register temp2_index = temp2; 391 __ access_load_at(T_ADDRESS, IN_HEAP, temp2_index, member_vmindex, noreg, noreg); 392 393 if (VerifyMethodHandles) { 394 Label L_index_ok; 395 __ cmpw(temp2_index, 0U); 396 __ br(Assembler::GE, L_index_ok); 397 __ hlt(0); 398 __ BIND(L_index_ok); 399 } 400 401 // Note: The verifier invariants allow us to ignore MemberName.clazz and vmtarget 402 // at this point. And VerifyMethodHandles has already checked clazz, if needed. 403 404 // get target Method* & entry point 405 __ lookup_virtual_method(temp1_recv_klass, temp2_index, rmethod); 406 break; 407 } 408 409 case vmIntrinsics::_linkToInterface: 410 { 411 // same as TemplateTable::invokeinterface 412 // (minus the CP setup and profiling, with different argument motion) 413 if (VerifyMethodHandles) { 414 verify_ref_kind(_masm, JVM_REF_invokeInterface, member_reg, temp3); 415 } 416 417 Register temp3_intf = temp3; 418 __ load_heap_oop(temp3_intf, member_clazz, temp2, rscratch2); 419 load_klass_from_Class(_masm, temp3_intf); 420 __ verify_klass_ptr(temp3_intf); 421 422 Register rindex = rmethod; 423 __ access_load_at(T_ADDRESS, IN_HEAP, rindex, member_vmindex, noreg, noreg); 424 if (VerifyMethodHandles) { 425 Label L; 426 __ cmpw(rindex, 0U); 427 __ br(Assembler::GE, L); 428 __ hlt(0); 429 __ bind(L); 430 } 431 432 // given intf, index, and recv klass, dispatch to the implementation method 433 __ lookup_interface_method(temp1_recv_klass, temp3_intf, 434 // note: next two args must be the same: 435 rindex, rmethod, 436 temp2, 437 L_incompatible_class_change_error); 438 break; 439 } 440 441 default: 442 fatal("unexpected intrinsic %d: %s", vmIntrinsics::as_int(iid), vmIntrinsics::name_at(iid)); 443 break; 444 } 445 446 // live at this point: rmethod, r19_sender_sp (if interpreted) 447 448 // After figuring out which concrete method to call, jump into it. 449 // Note that this works in the interpreter with no data motion. 450 // But the compiled version will require that r2_recv be shifted out. 451 __ verify_method_ptr(rmethod); 452 jump_from_method_handle(_masm, rmethod, temp1, for_compiler_entry); 453 if (iid == vmIntrinsics::_linkToInterface) { 454 __ bind(L_incompatible_class_change_error); 455 __ far_jump(RuntimeAddress(SharedRuntime::throw_IncompatibleClassChangeError_entry())); 456 } 457 } 458 } 459 460 #ifndef PRODUCT 461 void trace_method_handle_stub(const char* adaptername, 462 oopDesc* mh, 463 intptr_t* saved_regs, 464 intptr_t* entry_sp) { } 465 466 // The stub wraps the arguments in a struct on the stack to avoid 467 // dealing with the different calling conventions for passing 6 468 // arguments. 469 struct MethodHandleStubArguments { 470 const char* adaptername; 471 oopDesc* mh; 472 intptr_t* saved_regs; 473 intptr_t* entry_sp; 474 }; 475 void trace_method_handle_stub_wrapper(MethodHandleStubArguments* args) { } 476 477 void MethodHandles::trace_method_handle(MacroAssembler* _masm, const char* adaptername) { } 478 #endif //PRODUCT