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