1 /*
   2  * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright 2009, 2010, 2011 Red Hat, Inc.
   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 "interpreter/cppInterpreterGenerator.hpp"
  28 #include "interpreter/interpreter.hpp"
  29 #include "interpreter/interpreterRuntime.hpp"
  30 #include "memory/allocation.inline.hpp"
  31 #include "memory/resourceArea.hpp"
  32 #include "oops/method.inline.hpp"
  33 #include "oops/oop.inline.hpp"
  34 #include "runtime/frame.inline.hpp"
  35 #include "prims/methodHandles.hpp"
  36 
  37 void MethodHandles::invoke_target(Method* method, TRAPS) {
  38 
  39   JavaThread *thread = (JavaThread *) THREAD;
  40   ZeroStack *stack = thread->zero_stack();
  41   InterpreterFrame *frame = thread->top_zero_frame()->as_interpreter_frame();
  42   interpreterState istate = frame->interpreter_state();
  43 
  44   // Trim back the stack to put the parameters at the top
  45   stack->set_sp(istate->stack() + 1);
  46 
  47   Interpreter::invoke_method(method, method->from_interpreted_entry(), THREAD);
  48 
  49   // Convert the result
  50   istate->set_stack(stack->sp() - 1);
  51 
  52 }
  53 
  54 oop MethodHandles::popFromStack(TRAPS) {
  55 
  56   JavaThread *thread = (JavaThread *) THREAD;
  57   InterpreterFrame *frame = thread->top_zero_frame()->as_interpreter_frame();
  58   interpreterState istate = frame->interpreter_state();
  59   intptr_t* topOfStack = istate->stack();
  60 
  61   oop top = STACK_OBJECT(-1);
  62   MORE_STACK(-1);
  63   istate->set_stack(topOfStack);
  64 
  65   return top;
  66 
  67 }
  68 
  69 void MethodHandles::throw_AME(Klass* rcvr, Method* interface_method, TRAPS) {
  70 
  71   JavaThread *thread = (JavaThread *) THREAD;
  72   // Set up the frame anchor if it isn't already
  73   bool has_last_Java_frame = thread->has_last_Java_frame();
  74   if (!has_last_Java_frame) {
  75     intptr_t *sp = thread->zero_stack()->sp();
  76     ZeroFrame *frame = thread->top_zero_frame();
  77     while (frame) {
  78       if (frame->is_interpreter_frame()) {
  79         interpreterState istate =
  80           frame->as_interpreter_frame()->interpreter_state();
  81         if (istate->self_link() == istate)
  82           break;
  83       }
  84 
  85       sp = ((intptr_t *) frame) + 1;
  86       frame = frame->next();
  87     }
  88 
  89     assert(frame != NULL, "must be");
  90     thread->set_last_Java_frame(frame, sp);
  91   }
  92   InterpreterRuntime::throw_AbstractMethodErrorVerbose(thread, rcvr, interface_method);
  93   // Reset the frame anchor if necessary
  94   if (!has_last_Java_frame) {
  95     thread->reset_last_Java_frame();
  96   }
  97 
  98 }
  99 
 100 int MethodHandles::method_handle_entry_invokeBasic(Method* method, intptr_t UNUSED, TRAPS) {
 101 
 102   JavaThread *thread = (JavaThread *) THREAD;
 103   InterpreterFrame *frame = thread->top_zero_frame()->as_interpreter_frame();
 104   interpreterState istate = frame->interpreter_state();
 105   intptr_t* topOfStack = istate->stack();
 106 
 107   // 'this' is a MethodHandle. We resolve the target method by accessing this.form.vmentry.vmtarget.
 108   int numArgs = method->size_of_parameters();
 109   oop lform1 = java_lang_invoke_MethodHandle::form(STACK_OBJECT(-numArgs)); // this.form
 110   oop vmEntry1 = java_lang_invoke_LambdaForm::vmentry(lform1);
 111   Method* vmtarget = (Method*) java_lang_invoke_MemberName::vmtarget(vmEntry1);
 112 
 113   invoke_target(vmtarget, THREAD);
 114 
 115   // No deoptimized frames on the stack
 116   return 0;
 117 }
 118 
 119 int MethodHandles::method_handle_entry_linkToStaticOrSpecial(Method* method, intptr_t UNUSED, TRAPS) {
 120 
 121   // Pop appendix argument from stack. This is a MemberName which we resolve to the
 122   // target method.
 123   oop vmentry = popFromStack(THREAD);
 124 
 125   Method* vmtarget = (Method*) java_lang_invoke_MemberName::vmtarget(vmentry);
 126 
 127   invoke_target(vmtarget, THREAD);
 128 
 129   return 0;
 130 }
 131 
 132 int MethodHandles::method_handle_entry_linkToInterface(Method* method, intptr_t UNUSED, TRAPS) {
 133   JavaThread *thread = (JavaThread *) THREAD;
 134   InterpreterFrame *frame = thread->top_zero_frame()->as_interpreter_frame();
 135   interpreterState istate = frame->interpreter_state();
 136 
 137   // Pop appendix argument from stack. This is a MemberName which we resolve to the
 138   // target method.
 139   oop vmentry = popFromStack(THREAD);
 140   intptr_t* topOfStack = istate->stack();
 141 
 142   // Resolve target method by looking up in the receiver object's itable.
 143   Klass* clazz = java_lang_Class::as_Klass(java_lang_invoke_MemberName::clazz(vmentry));
 144   intptr_t vmindex = java_lang_invoke_MemberName::vmindex(vmentry);
 145   Method* target = (Method*) java_lang_invoke_MemberName::vmtarget(vmentry);
 146 
 147   int numArgs = target->size_of_parameters();
 148   oop recv = STACK_OBJECT(-numArgs);
 149 
 150   InstanceKlass* klass_part = InstanceKlass::cast(recv->klass());
 151   itableOffsetEntry* ki = (itableOffsetEntry*) klass_part->start_of_itable();
 152   int i;
 153   for ( i = 0 ; i < klass_part->itable_length() ; i++, ki++ ) {
 154     if (ki->interface_klass() == clazz) break;
 155   }
 156 
 157   itableMethodEntry* im = ki->first_method_entry(recv->klass());
 158   Method* vmtarget = im[vmindex].method();
 159   // Check that the vmtarget entry is non-null.  A null entry means
 160   // that the method no longer exists (got deleted) or is private.
 161   // Private class methods can never be an implementation of an
 162   // interface method. In those cases, throw AME.
 163   if (vmtarget != NULL) {
 164     invoke_target(vmtarget, THREAD);
 165   } else {
 166     throw_AME(recv->klass(), target, THREAD);
 167   }
 168 
 169   return 0;
 170 }
 171 
 172 int MethodHandles::method_handle_entry_linkToVirtual(Method* method, intptr_t UNUSED, TRAPS) {
 173   JavaThread *thread = (JavaThread *) THREAD;
 174 
 175   InterpreterFrame *frame = thread->top_zero_frame()->as_interpreter_frame();
 176   interpreterState istate = frame->interpreter_state();
 177 
 178   // Pop appendix argument from stack. This is a MemberName which we resolve to the
 179   // target method.
 180   oop vmentry = popFromStack(THREAD);
 181   intptr_t* topOfStack = istate->stack();
 182 
 183   // Resolve target method by looking up in the receiver object's vtable.
 184   intptr_t vmindex = java_lang_invoke_MemberName::vmindex(vmentry);
 185   Method* target = (Method*) java_lang_invoke_MemberName::vmtarget(vmentry);
 186   int numArgs = target->size_of_parameters();
 187   oop recv = STACK_OBJECT(-numArgs);
 188   Klass* clazz = recv->klass();
 189   Klass* klass_part = InstanceKlass::cast(clazz);
 190   ResourceMark rm(THREAD);
 191   klassVtable vtable = klass_part->vtable();
 192   Method* vmtarget = vtable.method_at(vmindex);
 193 
 194   invoke_target(vmtarget, THREAD);
 195 
 196   return 0;
 197 }
 198 
 199 int MethodHandles::method_handle_entry_invalid(Method* method, intptr_t UNUSED, TRAPS) {
 200   ShouldNotReachHere();
 201   return 0;
 202 }
 203 
 204 address MethodHandles::generate_method_handle_interpreter_entry(MacroAssembler* masm,
 205                                                                 vmIntrinsics::ID iid) {
 206   switch (iid) {
 207   case vmIntrinsics::_invokeGeneric:
 208   case vmIntrinsics::_compiledLambdaForm:
 209     // Perhaps surprisingly, the symbolic references visible to Java are not directly used.
 210     // They are linked to Java-generated adapters via MethodHandleNatives.linkMethod.
 211     // They all allow an appendix argument.
 212     return CppInterpreterGenerator::generate_entry_impl(masm, (address) MethodHandles::method_handle_entry_invalid);
 213   case vmIntrinsics::_invokeBasic:
 214     return CppInterpreterGenerator::generate_entry_impl(masm, (address) MethodHandles::method_handle_entry_invokeBasic);
 215   case vmIntrinsics::_linkToStatic:
 216   case vmIntrinsics::_linkToSpecial:
 217     return CppInterpreterGenerator::generate_entry_impl(masm, (address) MethodHandles::method_handle_entry_linkToStaticOrSpecial);
 218   case vmIntrinsics::_linkToInterface:
 219     return CppInterpreterGenerator::generate_entry_impl(masm, (address) MethodHandles::method_handle_entry_linkToInterface);
 220   case vmIntrinsics::_linkToVirtual:
 221     return CppInterpreterGenerator::generate_entry_impl(masm, (address) MethodHandles::method_handle_entry_linkToVirtual);
 222   default:
 223     ShouldNotReachHere();
 224     return NULL;
 225   }
 226 }
 227 
 228 #ifndef PRODUCT
 229 void MethodHandles::trace_method_handle(MacroAssembler* _masm, const char* adaptername) {
 230   // This is just a stub.
 231 }
 232 #endif //PRODUCT