1 /*
   2  * Copyright (c) 1997, 2015, 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 #include "utilities/top.hpp"
  30 
  31 // All the necessary definitions for run-time link resolution.
  32 
  33 // CallInfo provides all the information gathered for a particular
  34 // linked call site after resolving it. A link is any reference
  35 // made from within the bytecodes of a method to an object outside of
  36 // that method. If the info is invalid, the link has not been resolved
  37 // successfully.
  38 
  39 class CallInfo : public StackObj {
  40  public:
  41   // Ways that a method call might be selected (or not) based on receiver type.
  42   // Note that an invokevirtual instruction might be linked with no_dispatch,
  43   // and an invokeinterface instruction might be linked with any of the three options
  44   enum CallKind {
  45     direct_call,                        // jump into resolved_method (must be concrete)
  46     vtable_call,                        // select recv.klass.method_at_vtable(index)
  47     itable_call,                        // select recv.klass.method_at_itable(resolved_method.holder, index)
  48     unknown_kind = -1
  49   };
  50  private:
  51   KlassHandle  _resolved_klass;         // static receiver klass, resolved from a symbolic reference
  52   KlassHandle  _selected_klass;         // dynamic receiver class (same as static, or subklass)
  53   methodHandle _resolved_method;        // static target method
  54   methodHandle _selected_method;        // dynamic (actual) target method
  55   CallKind     _call_kind;              // kind of call (static(=bytecode static/special +
  56                                         //               others inferred), vtable, itable)
  57   int          _call_index;             // vtable or itable index of selected class method (if any)
  58   Handle       _resolved_appendix;      // extra argument in constant pool (if CPCE::has_appendix)
  59   Handle       _resolved_method_type;   // MethodType (for invokedynamic and invokehandle call sites)
  60 
  61   void set_static(KlassHandle resolved_klass, const methodHandle& resolved_method, TRAPS);
  62   void set_interface(KlassHandle resolved_klass, KlassHandle selected_klass,
  63                      const methodHandle& resolved_method,
  64                      const methodHandle& selected_method,
  65                      int itable_index, TRAPS);
  66   void set_virtual(KlassHandle resolved_klass, KlassHandle 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_common(KlassHandle resolved_klass, KlassHandle selected_klass,
  73                   const methodHandle& resolved_method,
  74                   const methodHandle& selected_method,
  75                   CallKind kind,
  76                   int index, TRAPS);
  77 
  78   friend class LinkResolver;
  79 
  80  public:
  81   CallInfo() {
  82 #ifndef PRODUCT
  83     _call_kind  = CallInfo::unknown_kind;
  84     _call_index = Method::garbage_vtable_index;
  85 #endif //PRODUCT
  86   }
  87 
  88   // utility to extract an effective CallInfo from a method and an optional receiver limit
  89   // does not queue the method for compilation
  90   CallInfo(Method* resolved_method, Klass* resolved_klass = NULL);
  91 
  92   KlassHandle  resolved_klass() const            { return _resolved_klass; }
  93   KlassHandle  selected_klass() const            { return _selected_klass; }
  94   methodHandle resolved_method() const           { return _resolved_method; }
  95   methodHandle selected_method() const           { return _selected_method; }
  96   Handle       resolved_appendix() const         { return _resolved_appendix; }
  97   Handle       resolved_method_type() const      { return _resolved_method_type; }
  98 
  99   BasicType    result_type() const               { return selected_method()->result_type(); }
 100   CallKind     call_kind() const                 { return _call_kind; }
 101   int          call_index() const                { return _call_index; }
 102   int          vtable_index() const {
 103     // Even for interface calls the vtable index could be non-negative.
 104     // See CallInfo::set_interface.
 105     assert(has_vtable_index() || is_statically_bound(), "");
 106     assert(call_kind() == vtable_call || call_kind() == direct_call, "");
 107     // The returned value is < 0 if the call is statically bound.
 108     // But, the returned value may be >= 0 even if the kind is direct_call.
 109     // It is up to the caller to decide which way to go.
 110     return _call_index;
 111   }
 112   int          itable_index() const {
 113     assert(call_kind() == itable_call, "");
 114     // The returned value is always >= 0, a valid itable index.
 115     return _call_index;
 116   }
 117 
 118   // debugging
 119 #ifdef ASSERT
 120   bool         has_vtable_index() const          { return _call_index >= 0 && _call_kind != CallInfo::itable_call; }
 121   bool         is_statically_bound() const       { return _call_index == Method::nonvirtual_vtable_index; }
 122 #endif //ASSERT
 123   void         verify() PRODUCT_RETURN;
 124   void         print()  PRODUCT_RETURN;
 125 };
 126 
 127 
 128 // Condensed information from constant pool to use to resolve the method or field.
 129 //   resolved_klass = specified class (i.e., static receiver class)
 130 //   current_klass  = sending method holder (i.e., class containing the method
 131 //                    containing the call being resolved)
 132 class LinkInfo : public StackObj {
 133   Symbol*     _name;            // extracted from JVM_CONSTANT_NameAndType
 134   Symbol*     _signature;
 135   KlassHandle _resolved_klass;  // class that the constant pool entry points to
 136   KlassHandle _current_klass;   // class that owns the constant pool
 137   bool        _check_access;
 138   constantTag _tag;
 139  public:
 140   enum AccessCheck {
 141      needs_access_check,
 142      skip_access_check
 143   };
 144 
 145   LinkInfo(const constantPoolHandle& pool, int index, TRAPS);
 146 
 147   // Condensed information from other call sites within the vm.
 148   LinkInfo(KlassHandle resolved_klass, Symbol* name, Symbol* signature, KlassHandle current_klass,
 149            AccessCheck check_access = needs_access_check,
 150            constantTag tag = JVM_CONSTANT_Invalid) :
 151     _resolved_klass(resolved_klass),
 152     _name(name), _signature(signature), _current_klass(current_klass),
 153     _check_access(check_access == needs_access_check && current_klass.not_null()), _tag(tag) {}
 154 
 155   // Case where we just find the method and don't check access against the current class
 156   LinkInfo(KlassHandle resolved_klass, Symbol*name, Symbol* signature) :
 157     _resolved_klass(resolved_klass),
 158     _name(name), _signature(signature), _current_klass(NULL),
 159     _check_access(false), _tag(JVM_CONSTANT_Invalid) {}
 160 
 161   // accessors
 162   Symbol* name() const               { return _name; }
 163   Symbol* signature() const          { return _signature; }
 164   KlassHandle resolved_klass() const { return _resolved_klass; }
 165   KlassHandle current_klass() const  { return _current_klass; }
 166   constantTag tag() const            { return _tag; }
 167   bool check_access() const          { return _check_access; }
 168   char* method_string() const;
 169 
 170   void         print()  PRODUCT_RETURN;
 171 };
 172 
 173 // Link information for getfield/putfield & getstatic/putstatic bytecodes
 174 // is represented using a fieldDescriptor.
 175 
 176 // The LinkResolver is used to resolve constant-pool references at run-time.
 177 // It does all necessary link-time checks & throws exceptions if necessary.
 178 
 179 class LinkResolver: AllStatic {
 180   friend class klassVtable;
 181   friend class klassItable;
 182 
 183  private:
 184 
 185   static methodHandle lookup_method_in_klasses(const LinkInfo& link_info,
 186                                                bool checkpolymorphism,
 187                                                bool in_imethod_resolve, TRAPS);
 188   static methodHandle lookup_method_in_interfaces(const LinkInfo& link_info, TRAPS);
 189   static methodHandle lookup_polymorphic_method(const LinkInfo& link_info,
 190                                                 Handle *appendix_result_or_null,
 191                                                 Handle *method_type_result, TRAPS);
 192  JVMCI_ONLY(public:) // Needed for CompilerToVM.resolveMethod()
 193   // Not Linktime so doesn't take LinkInfo
 194   static methodHandle lookup_instance_method_in_klasses (
 195                                        KlassHandle klass, Symbol* name, Symbol* signature, TRAPS);
 196  JVMCI_ONLY(private:)
 197 
 198   // Similar loader constraint checking functions that throw
 199   // LinkageError with descriptive message.
 200   static void check_method_loader_constraints(const LinkInfo& link_info,
 201                                               const methodHandle& resolved_method,
 202                                               const char* method_type, TRAPS);
 203   static void check_field_loader_constraints(Symbol* field, Symbol* sig,
 204                                              KlassHandle current_klass,
 205                                              KlassHandle sel_klass, TRAPS);
 206 
 207   static methodHandle resolve_interface_method(const LinkInfo& link_info, bool nostatics, TRAPS);
 208   static methodHandle resolve_method          (const LinkInfo& link_info, bool require_methodref, TRAPS);
 209 
 210   static methodHandle linktime_resolve_static_method    (const LinkInfo& link_info, TRAPS);
 211   static methodHandle linktime_resolve_special_method   (const LinkInfo& link_info, TRAPS);
 212   static methodHandle linktime_resolve_virtual_method   (const LinkInfo& link_info, TRAPS);
 213   static methodHandle linktime_resolve_interface_method (const LinkInfo& link_info, TRAPS);
 214 
 215   static void runtime_resolve_special_method    (CallInfo& result,
 216                                                  const methodHandle& resolved_method,
 217                                                  KlassHandle resolved_klass,
 218                                                  KlassHandle current_klass,
 219                                                  bool check_access, TRAPS);
 220   static void runtime_resolve_virtual_method    (CallInfo& result,
 221                                                  const methodHandle& resolved_method,
 222                                                  KlassHandle resolved_klass,
 223                                                  Handle recv,
 224                                                  KlassHandle recv_klass,
 225                                                  bool check_null_and_abstract, TRAPS);
 226   static void runtime_resolve_interface_method  (CallInfo& result,
 227                                                  const methodHandle& resolved_method,
 228                                                  KlassHandle resolved_klass,
 229                                                  Handle recv,
 230                                                  KlassHandle recv_klass,
 231                                                  bool check_null_and_abstract, TRAPS);
 232 
 233   static void check_field_accessability(KlassHandle ref_klass,
 234                                         KlassHandle resolved_klass,
 235                                         KlassHandle sel_klass,
 236                                         const fieldDescriptor& fd, TRAPS);
 237   static void check_method_accessability(KlassHandle ref_klass,
 238                                          KlassHandle resolved_klass,
 239                                          KlassHandle sel_klass,
 240                                          const methodHandle& sel_method, TRAPS);
 241 
 242   // runtime resolving from constant pool
 243   static void resolve_invokestatic   (CallInfo& result,
 244                                       const constantPoolHandle& pool, int index, TRAPS);
 245   static void resolve_invokespecial  (CallInfo& result,
 246                                       const constantPoolHandle& pool, int index, TRAPS);
 247   static void resolve_invokevirtual  (CallInfo& result, Handle recv,
 248                                       const constantPoolHandle& pool, int index, TRAPS);
 249   static void resolve_invokeinterface(CallInfo& result, Handle recv,
 250                                       const constantPoolHandle& pool, int index, TRAPS);
 251   static void resolve_invokedynamic  (CallInfo& result,
 252                                       const constantPoolHandle& pool, int index, TRAPS);
 253   static void resolve_invokehandle   (CallInfo& result,
 254                                       const constantPoolHandle& pool, int index, TRAPS);
 255  public:
 256   // constant pool resolving
 257   static void check_klass_accessability(KlassHandle ref_klass, KlassHandle sel_klass, TRAPS);
 258 
 259   // static resolving calls (will not run any Java code);
 260   // used only from Bytecode_invoke::static_target
 261   static methodHandle resolve_method_statically(Bytecodes::Code code,
 262                                                 const constantPoolHandle& pool,
 263                                                 int index, TRAPS);
 264 
 265   static void resolve_field_access(fieldDescriptor& result,
 266                                    const constantPoolHandle& pool,
 267                                    int index, Bytecodes::Code byte, TRAPS);
 268   static void resolve_field(fieldDescriptor& result, const LinkInfo& link_info,
 269                             Bytecodes::Code access_kind,
 270                             bool initialize_class, TRAPS);
 271 
 272   static void resolve_static_call   (CallInfo& result,
 273                                      const LinkInfo& link_info,
 274                                      bool initialize_klass, TRAPS);
 275   static void resolve_special_call  (CallInfo& result,
 276                                      const LinkInfo& link_info,
 277                                      TRAPS);
 278   static void resolve_virtual_call  (CallInfo& result, Handle recv, KlassHandle recv_klass,
 279                                      const LinkInfo& link_info,
 280                                      bool check_null_and_abstract, TRAPS);
 281   static void resolve_interface_call(CallInfo& result, Handle recv, KlassHandle recv_klass,
 282                                      const LinkInfo& link_info,
 283                                      bool check_null_and_abstract, TRAPS);
 284   static void resolve_handle_call   (CallInfo& result,
 285                                      const LinkInfo& link_info, TRAPS);
 286   static void resolve_dynamic_call  (CallInfo& result, Handle bootstrap_specifier,
 287                                      Symbol* method_name, Symbol* method_signature,
 288                                      KlassHandle current_klass, TRAPS);
 289 
 290   // same as above for compile-time resolution; but returns null handle instead of throwing
 291   // an exception on error also, does not initialize klass (i.e., no side effects)
 292   static methodHandle resolve_virtual_call_or_null  (KlassHandle receiver_klass,
 293                                                      const LinkInfo& link_info);
 294   static methodHandle resolve_interface_call_or_null(KlassHandle receiver_klass,
 295                                                      const LinkInfo& link_info);
 296   static methodHandle resolve_static_call_or_null   (const LinkInfo& link_info);
 297   static methodHandle resolve_special_call_or_null  (const LinkInfo& link_info);
 298 
 299   static int vtable_index_of_interface_method(KlassHandle klass, const methodHandle& resolved_method);
 300 
 301   // same as above for compile-time resolution; returns vtable_index if current_klass if linked
 302   static int resolve_virtual_vtable_index  (KlassHandle receiver_klass,
 303                                             const LinkInfo& link_info);
 304 
 305   // static resolving for compiler (does not throw exceptions, returns null handle if unsuccessful)
 306   static methodHandle linktime_resolve_virtual_method_or_null  (const LinkInfo& link_info);
 307   static methodHandle linktime_resolve_interface_method_or_null(const LinkInfo& link_info);
 308 
 309   // runtime resolving from constant pool
 310   static void resolve_invoke(CallInfo& result, Handle recv,
 311                              const constantPoolHandle& pool, int index,
 312                              Bytecodes::Code byte, TRAPS);
 313  private:
 314   static void trace_method_resolution(const char* prefix, KlassHandle klass,
 315                                       KlassHandle resolved_klass,
 316                                       const methodHandle& method) PRODUCT_RETURN;
 317 };
 318 #endif // SHARE_VM_INTERPRETER_LINKRESOLVER_HPP