src/hotspot/share/interpreter/linkResolver.hpp
changeset 47216 71c04702a3d5
parent 46727 6e4a84748e2c
child 47401 98e960939ef2
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  *
       
    23  */
       
    24 
       
    25 #ifndef SHARE_VM_INTERPRETER_LINKRESOLVER_HPP
       
    26 #define SHARE_VM_INTERPRETER_LINKRESOLVER_HPP
       
    27 
       
    28 #include "oops/method.hpp"
       
    29 
       
    30 // All the necessary definitions for run-time link resolution.
       
    31 
       
    32 // CallInfo provides all the information gathered for a particular
       
    33 // linked call site after resolving it. A link is any reference
       
    34 // made from within the bytecodes of a method to an object outside of
       
    35 // that method. If the info is invalid, the link has not been resolved
       
    36 // successfully.
       
    37 
       
    38 class CallInfo : public StackObj {
       
    39  public:
       
    40   // Ways that a method call might be selected (or not) based on receiver type.
       
    41   // Note that an invokevirtual instruction might be linked with no_dispatch,
       
    42   // and an invokeinterface instruction might be linked with any of the three options
       
    43   enum CallKind {
       
    44     direct_call,                        // jump into resolved_method (must be concrete)
       
    45     vtable_call,                        // select recv.klass.method_at_vtable(index)
       
    46     itable_call,                        // select recv.klass.method_at_itable(resolved_method.holder, index)
       
    47     unknown_kind = -1
       
    48   };
       
    49  private:
       
    50   Klass*       _resolved_klass;         // static receiver klass, resolved from a symbolic reference
       
    51   Klass*       _selected_klass;         // dynamic receiver class (same as static, or subklass)
       
    52   methodHandle _resolved_method;        // static target method
       
    53   methodHandle _selected_method;        // dynamic (actual) target method
       
    54   CallKind     _call_kind;              // kind of call (static(=bytecode static/special +
       
    55                                         //               others inferred), vtable, itable)
       
    56   int          _call_index;             // vtable or itable index of selected class method (if any)
       
    57   Handle       _resolved_appendix;      // extra argument in constant pool (if CPCE::has_appendix)
       
    58   Handle       _resolved_method_type;   // MethodType (for invokedynamic and invokehandle call sites)
       
    59   Handle       _resolved_method_name;   // Object holding the ResolvedMethodName
       
    60 
       
    61   void set_static(Klass* resolved_klass, const methodHandle& resolved_method, TRAPS);
       
    62   void set_interface(Klass* resolved_klass, Klass* selected_klass,
       
    63                      const methodHandle& resolved_method,
       
    64                      const methodHandle& selected_method,
       
    65                      int itable_index, TRAPS);
       
    66   void set_virtual(Klass* resolved_klass, Klass* selected_klass,
       
    67                    const methodHandle& resolved_method,
       
    68                    const methodHandle& selected_method,
       
    69                    int vtable_index, TRAPS);
       
    70   void set_handle(const methodHandle& resolved_method,
       
    71                   Handle resolved_appendix, Handle resolved_method_type, TRAPS);
       
    72   void set_handle(Klass* resolved_klass,
       
    73                   const methodHandle& resolved_method,
       
    74                   Handle resolved_appendix, Handle resolved_method_type, TRAPS);
       
    75   void set_common(Klass* resolved_klass, Klass* selected_klass,
       
    76                   const methodHandle& resolved_method,
       
    77                   const methodHandle& selected_method,
       
    78                   CallKind kind,
       
    79                   int index, TRAPS);
       
    80 
       
    81   friend class LinkResolver;
       
    82 
       
    83  public:
       
    84   CallInfo() {
       
    85 #ifndef PRODUCT
       
    86     _call_kind  = CallInfo::unknown_kind;
       
    87     _call_index = Method::garbage_vtable_index;
       
    88 #endif //PRODUCT
       
    89   }
       
    90 
       
    91   // utility to extract an effective CallInfo from a method and an optional receiver limit
       
    92   // does not queue the method for compilation.  This also creates a ResolvedMethodName
       
    93   // object for the resolved_method.
       
    94   CallInfo(Method* resolved_method, Klass* resolved_klass, TRAPS);
       
    95 
       
    96   Klass*  resolved_klass() const                 { return _resolved_klass; }
       
    97   Klass*  selected_klass() const                 { return _selected_klass; }
       
    98   methodHandle resolved_method() const           { return _resolved_method; }
       
    99   methodHandle selected_method() const           { return _selected_method; }
       
   100   Handle       resolved_appendix() const         { return _resolved_appendix; }
       
   101   Handle       resolved_method_type() const      { return _resolved_method_type; }
       
   102   Handle       resolved_method_name() const      { return _resolved_method_name; }
       
   103   // Materialize a java.lang.invoke.ResolvedMethodName for this resolved_method
       
   104   void     set_resolved_method_name(TRAPS);
       
   105 
       
   106   BasicType    result_type() const               { return selected_method()->result_type(); }
       
   107   CallKind     call_kind() const                 { return _call_kind; }
       
   108   int          call_index() const                { return _call_index; }
       
   109   int          vtable_index() const {
       
   110     // Even for interface calls the vtable index could be non-negative.
       
   111     // See CallInfo::set_interface.
       
   112     assert(has_vtable_index() || is_statically_bound(), "");
       
   113     assert(call_kind() == vtable_call || call_kind() == direct_call, "");
       
   114     // The returned value is < 0 if the call is statically bound.
       
   115     // But, the returned value may be >= 0 even if the kind is direct_call.
       
   116     // It is up to the caller to decide which way to go.
       
   117     return _call_index;
       
   118   }
       
   119   int          itable_index() const {
       
   120     assert(call_kind() == itable_call, "");
       
   121     // The returned value is always >= 0, a valid itable index.
       
   122     return _call_index;
       
   123   }
       
   124 
       
   125   // debugging
       
   126 #ifdef ASSERT
       
   127   bool         has_vtable_index() const          { return _call_index >= 0 && _call_kind != CallInfo::itable_call; }
       
   128   bool         is_statically_bound() const       { return _call_index == Method::nonvirtual_vtable_index; }
       
   129 #endif //ASSERT
       
   130   void         verify() PRODUCT_RETURN;
       
   131   void         print()  PRODUCT_RETURN;
       
   132 };
       
   133 
       
   134 
       
   135 // Condensed information from constant pool to use to resolve the method or field.
       
   136 //   resolved_klass = specified class (i.e., static receiver class)
       
   137 //   current_klass  = sending method holder (i.e., class containing the method
       
   138 //                    containing the call being resolved)
       
   139 //   current_method = sending method (relevant for field resolution)
       
   140 class LinkInfo : public StackObj {
       
   141   Symbol*     _name;            // extracted from JVM_CONSTANT_NameAndType
       
   142   Symbol*     _signature;
       
   143   Klass*      _resolved_klass;  // class that the constant pool entry points to
       
   144   Klass*      _current_klass;   // class that owns the constant pool
       
   145   methodHandle _current_method;  // sending method
       
   146   bool        _check_access;
       
   147   constantTag _tag;
       
   148 
       
   149  public:
       
   150   enum AccessCheck {
       
   151     needs_access_check,
       
   152     skip_access_check
       
   153   };
       
   154 
       
   155   LinkInfo(const constantPoolHandle& pool, int index, const methodHandle& current_method, TRAPS);
       
   156   LinkInfo(const constantPoolHandle& pool, int index, TRAPS);
       
   157 
       
   158   // Condensed information from other call sites within the vm.
       
   159   LinkInfo(Klass* resolved_klass, Symbol* name, Symbol* signature, Klass* current_klass,
       
   160            AccessCheck check_access = needs_access_check,
       
   161            constantTag tag = JVM_CONSTANT_Invalid) :
       
   162     _resolved_klass(resolved_klass),
       
   163     _name(name), _signature(signature), _current_klass(current_klass), _current_method(methodHandle()),
       
   164     _check_access(check_access == needs_access_check), _tag(tag) {}
       
   165 
       
   166   LinkInfo(Klass* resolved_klass, Symbol* name, Symbol* signature, const methodHandle& current_method,
       
   167            AccessCheck check_access = needs_access_check,
       
   168            constantTag tag = JVM_CONSTANT_Invalid) :
       
   169     _resolved_klass(resolved_klass),
       
   170     _name(name), _signature(signature), _current_klass(current_method->method_holder()), _current_method(current_method),
       
   171     _check_access(check_access == needs_access_check), _tag(tag) {}
       
   172 
       
   173   // Case where we just find the method and don't check access against the current class
       
   174   LinkInfo(Klass* resolved_klass, Symbol*name, Symbol* signature) :
       
   175     _resolved_klass(resolved_klass),
       
   176     _name(name), _signature(signature), _current_klass(NULL), _current_method(methodHandle()),
       
   177     _check_access(false), _tag(JVM_CONSTANT_Invalid) {}
       
   178 
       
   179   // accessors
       
   180   Symbol* name() const               { return _name; }
       
   181   Symbol* signature() const          { return _signature; }
       
   182   Klass* resolved_klass() const      { return _resolved_klass; }
       
   183   Klass* current_klass() const       { return _current_klass; }
       
   184   methodHandle current_method() const { return _current_method; }
       
   185   constantTag tag() const            { return _tag; }
       
   186   bool check_access() const          { return _check_access; }
       
   187   char* method_string() const;
       
   188 
       
   189   void         print()  PRODUCT_RETURN;
       
   190 };
       
   191 
       
   192 // Link information for getfield/putfield & getstatic/putstatic bytecodes
       
   193 // is represented using a fieldDescriptor.
       
   194 
       
   195 // The LinkResolver is used to resolve constant-pool references at run-time.
       
   196 // It does all necessary link-time checks & throws exceptions if necessary.
       
   197 
       
   198 class LinkResolver: AllStatic {
       
   199   friend class klassVtable;
       
   200   friend class klassItable;
       
   201 
       
   202  private:
       
   203 
       
   204   static Method* lookup_method_in_klasses(const LinkInfo& link_info,
       
   205                                           bool checkpolymorphism,
       
   206                                           bool in_imethod_resolve);
       
   207   static Method* lookup_method_in_interfaces(const LinkInfo& link_info);
       
   208 
       
   209   static methodHandle lookup_polymorphic_method(const LinkInfo& link_info,
       
   210                                                 Handle *appendix_result_or_null,
       
   211                                                 Handle *method_type_result, TRAPS);
       
   212  JVMCI_ONLY(public:) // Needed for CompilerToVM.resolveMethod()
       
   213   // Not Linktime so doesn't take LinkInfo
       
   214   static methodHandle lookup_instance_method_in_klasses (
       
   215                                        Klass* klass, Symbol* name, Symbol* signature, TRAPS);
       
   216  JVMCI_ONLY(private:)
       
   217 
       
   218   // Similar loader constraint checking functions that throw
       
   219   // LinkageError with descriptive message.
       
   220   static void check_method_loader_constraints(const LinkInfo& link_info,
       
   221                                               const methodHandle& resolved_method,
       
   222                                               const char* method_type, TRAPS);
       
   223   static void check_field_loader_constraints(Symbol* field, Symbol* sig,
       
   224                                              Klass* current_klass,
       
   225                                              Klass* sel_klass, TRAPS);
       
   226 
       
   227   static methodHandle resolve_interface_method(const LinkInfo& link_info, Bytecodes::Code code, TRAPS);
       
   228   static methodHandle resolve_method          (const LinkInfo& link_info, Bytecodes::Code code, TRAPS);
       
   229 
       
   230   static methodHandle linktime_resolve_static_method    (const LinkInfo& link_info, TRAPS);
       
   231   static methodHandle linktime_resolve_special_method   (const LinkInfo& link_info, TRAPS);
       
   232   static methodHandle linktime_resolve_virtual_method   (const LinkInfo& link_info, TRAPS);
       
   233   static methodHandle linktime_resolve_interface_method (const LinkInfo& link_info, TRAPS);
       
   234 
       
   235   static void runtime_resolve_special_method    (CallInfo& result,
       
   236                                                  const methodHandle& resolved_method,
       
   237                                                  Klass* resolved_klass,
       
   238                                                  Klass* current_klass,
       
   239                                                  Handle recv,
       
   240                                                  bool check_access, TRAPS);
       
   241   static void runtime_resolve_virtual_method    (CallInfo& result,
       
   242                                                  const methodHandle& resolved_method,
       
   243                                                  Klass* resolved_klass,
       
   244                                                  Handle recv,
       
   245                                                  Klass* recv_klass,
       
   246                                                  bool check_null_and_abstract, TRAPS);
       
   247   static void runtime_resolve_interface_method  (CallInfo& result,
       
   248                                                  const methodHandle& resolved_method,
       
   249                                                  Klass* resolved_klass,
       
   250                                                  Handle recv,
       
   251                                                  Klass* recv_klass,
       
   252                                                  bool check_null_and_abstract, TRAPS);
       
   253 
       
   254   static void check_field_accessability(Klass* ref_klass,
       
   255                                         Klass* resolved_klass,
       
   256                                         Klass* sel_klass,
       
   257                                         const fieldDescriptor& fd, TRAPS);
       
   258   static void check_method_accessability(Klass* ref_klass,
       
   259                                          Klass* resolved_klass,
       
   260                                          Klass* sel_klass,
       
   261                                          const methodHandle& sel_method, TRAPS);
       
   262 
       
   263   // runtime resolving from constant pool
       
   264   static void resolve_invokestatic   (CallInfo& result,
       
   265                                       const constantPoolHandle& pool, int index, TRAPS);
       
   266   static void resolve_invokespecial  (CallInfo& result, Handle recv,
       
   267                                       const constantPoolHandle& pool, int index, TRAPS);
       
   268   static void resolve_invokevirtual  (CallInfo& result, Handle recv,
       
   269                                       const constantPoolHandle& pool, int index, TRAPS);
       
   270   static void resolve_invokeinterface(CallInfo& result, Handle recv,
       
   271                                       const constantPoolHandle& pool, int index, TRAPS);
       
   272   static void resolve_invokedynamic  (CallInfo& result,
       
   273                                       const constantPoolHandle& pool, int index, TRAPS);
       
   274   static void resolve_invokehandle   (CallInfo& result,
       
   275                                       const constantPoolHandle& pool, int index, TRAPS);
       
   276  public:
       
   277   // constant pool resolving
       
   278   static void check_klass_accessability(Klass* ref_klass, Klass* sel_klass, TRAPS);
       
   279 
       
   280   // static resolving calls (will not run any Java code);
       
   281   // used only from Bytecode_invoke::static_target
       
   282   static methodHandle resolve_method_statically(Bytecodes::Code code,
       
   283                                                 const constantPoolHandle& pool,
       
   284                                                 int index, TRAPS);
       
   285 
       
   286   static void resolve_field_access(fieldDescriptor& result,
       
   287                                    const constantPoolHandle& pool,
       
   288                                    int index,
       
   289                                    const methodHandle& method,
       
   290                                    Bytecodes::Code byte, TRAPS);
       
   291   static void resolve_field(fieldDescriptor& result, const LinkInfo& link_info,
       
   292                             Bytecodes::Code access_kind,
       
   293                             bool initialize_class, TRAPS);
       
   294 
       
   295   static void resolve_static_call   (CallInfo& result,
       
   296                                      const LinkInfo& link_info,
       
   297                                      bool initialize_klass, TRAPS);
       
   298   static void resolve_special_call  (CallInfo& result,
       
   299                                      Handle recv,
       
   300                                      const LinkInfo& link_info,
       
   301                                      TRAPS);
       
   302   static void resolve_virtual_call  (CallInfo& result, Handle recv, Klass* recv_klass,
       
   303                                      const LinkInfo& link_info,
       
   304                                      bool check_null_and_abstract, TRAPS);
       
   305   static void resolve_interface_call(CallInfo& result, Handle recv, Klass* recv_klass,
       
   306                                      const LinkInfo& link_info,
       
   307                                      bool check_null_and_abstract, TRAPS);
       
   308   static void resolve_handle_call   (CallInfo& result,
       
   309                                      const LinkInfo& link_info, TRAPS);
       
   310   static void resolve_dynamic_call  (CallInfo& result, Handle bootstrap_specifier,
       
   311                                      Symbol* method_name, Symbol* method_signature,
       
   312                                      Klass* current_klass, TRAPS);
       
   313 
       
   314   // same as above for compile-time resolution; but returns null handle instead of throwing
       
   315   // an exception on error also, does not initialize klass (i.e., no side effects)
       
   316   static methodHandle resolve_virtual_call_or_null  (Klass* receiver_klass,
       
   317                                                      const LinkInfo& link_info);
       
   318   static methodHandle resolve_interface_call_or_null(Klass* receiver_klass,
       
   319                                                      const LinkInfo& link_info);
       
   320   static methodHandle resolve_static_call_or_null   (const LinkInfo& link_info);
       
   321   static methodHandle resolve_special_call_or_null  (const LinkInfo& link_info);
       
   322 
       
   323   static int vtable_index_of_interface_method(Klass* klass, const methodHandle& resolved_method);
       
   324 
       
   325   // same as above for compile-time resolution; returns vtable_index if current_klass if linked
       
   326   static int resolve_virtual_vtable_index  (Klass* receiver_klass,
       
   327                                             const LinkInfo& link_info);
       
   328 
       
   329   // static resolving for compiler (does not throw exceptions, returns null handle if unsuccessful)
       
   330   static methodHandle linktime_resolve_virtual_method_or_null  (const LinkInfo& link_info);
       
   331   static methodHandle linktime_resolve_interface_method_or_null(const LinkInfo& link_info);
       
   332 
       
   333   // runtime resolving from constant pool
       
   334   static void resolve_invoke(CallInfo& result, Handle recv,
       
   335                              const constantPoolHandle& pool, int index,
       
   336                              Bytecodes::Code byte, TRAPS);
       
   337 
       
   338   // runtime resolving from attached method
       
   339   static void resolve_invoke(CallInfo& result, Handle& recv,
       
   340                              const methodHandle& attached_method,
       
   341                              Bytecodes::Code byte, TRAPS);
       
   342 };
       
   343 #endif // SHARE_VM_INTERPRETER_LINKRESOLVER_HPP