1 /*
   2  * Copyright (c) 1997, 2019, 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_INTERPRETER_LINKRESOLVER_HPP
  26 #define SHARE_INTERPRETER_LINKRESOLVER_HPP
  27 
  28 #include "interpreter/bootstrapInfo.hpp"
  29 #include "oops/method.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   Klass*       _resolved_klass;         // static receiver klass, resolved from a symbolic reference
  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_name;   // Object holding the ResolvedMethodName
  59 
  60   void set_static(Klass* resolved_klass, const methodHandle& resolved_method, TRAPS);
  61   void set_interface(Klass* resolved_klass,
  62                      const methodHandle& resolved_method,
  63                      const methodHandle& selected_method,
  64                      int itable_index, TRAPS);
  65   void set_virtual(Klass* resolved_klass,
  66                    const methodHandle& resolved_method,
  67                    const methodHandle& selected_method,
  68                    int vtable_index, TRAPS);
  69   void set_handle(const methodHandle& resolved_method,
  70                   Handle resolved_appendix, TRAPS);
  71   void set_handle(Klass* resolved_klass,
  72                   const methodHandle& resolved_method,
  73                   Handle resolved_appendix, TRAPS);
  74   void set_common(Klass* resolved_klass,
  75                   const methodHandle& resolved_method,
  76                   const methodHandle& selected_method,
  77                   CallKind kind,
  78                   int index, TRAPS);
  79 
  80   friend class BootstrapInfo;
  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   Method* resolved_method() const                { return _resolved_method(); }
  98   Method* selected_method() const                { return _selected_method(); }
  99   Handle       resolved_appendix() const         { return _resolved_appendix; }
 100   Handle       resolved_method_name() const      { return _resolved_method_name; }
 101   // Materialize a java.lang.invoke.ResolvedMethodName for this resolved_method
 102   void     set_resolved_method_name(TRAPS);
 103 
 104   BasicType    result_type() const               { return selected_method()->result_type(); }
 105   CallKind     call_kind() const                 { return _call_kind; }
 106   int          vtable_index() const {
 107     // Even for interface calls the vtable index could be non-negative.
 108     // See CallInfo::set_interface.
 109     assert(has_vtable_index() || is_statically_bound(), "");
 110     assert(call_kind() == vtable_call || call_kind() == direct_call, "");
 111     // The returned value is < 0 if the call is statically bound.
 112     // But, the returned value may be >= 0 even if the kind is direct_call.
 113     // It is up to the caller to decide which way to go.
 114     return _call_index;
 115   }
 116   int          itable_index() const {
 117     assert(call_kind() == itable_call, "");
 118     // The returned value is always >= 0, a valid itable index.
 119     return _call_index;
 120   }
 121 
 122   // debugging
 123 #ifdef ASSERT
 124   bool         has_vtable_index() const          { return _call_index >= 0 && _call_kind != CallInfo::itable_call; }
 125   bool         is_statically_bound() const       { return _call_index == Method::nonvirtual_vtable_index; }
 126 #endif //ASSERT
 127   void         verify() PRODUCT_RETURN;
 128   void         print()  PRODUCT_RETURN;
 129 };
 130 
 131 
 132 // Condensed information from constant pool to use to resolve the method or field.
 133 //   resolved_klass = specified class (i.e., static receiver class)
 134 //   current_klass  = sending method holder (i.e., class containing the method
 135 //                    containing the call being resolved)
 136 //   current_method = sending method (relevant for field resolution)
 137 class LinkInfo : public StackObj {
 138   Symbol*     _name;            // extracted from JVM_CONSTANT_NameAndType
 139   Symbol*     _signature;
 140   Klass*      _resolved_klass;  // class that the constant pool entry points to
 141   Klass*      _current_klass;   // class that owns the constant pool
 142   methodHandle _current_method;  // sending method
 143   bool        _check_access;
 144   constantTag _tag;
 145 
 146  public:
 147   enum AccessCheck {
 148     needs_access_check,
 149     skip_access_check
 150   };
 151 
 152   LinkInfo(const constantPoolHandle& pool, int index, const methodHandle& current_method, TRAPS);
 153   LinkInfo(const constantPoolHandle& pool, int index, TRAPS);
 154 
 155   // Condensed information from other call sites within the vm.
 156   LinkInfo(Klass* resolved_klass, Symbol* name, Symbol* signature, Klass* current_klass,
 157            AccessCheck check_access = needs_access_check,
 158            constantTag tag = JVM_CONSTANT_Invalid) :
 159     _name(name),
 160     _signature(signature), _resolved_klass(resolved_klass), _current_klass(current_klass), _current_method(methodHandle()),
 161     _check_access(check_access == needs_access_check), _tag(tag) {}
 162 
 163   LinkInfo(Klass* resolved_klass, Symbol* name, Symbol* signature, const methodHandle& current_method,
 164            AccessCheck check_access = needs_access_check,
 165            constantTag tag = JVM_CONSTANT_Invalid) :
 166     _name(name),
 167     _signature(signature), _resolved_klass(resolved_klass), _current_klass(current_method->method_holder()), _current_method(current_method),
 168     _check_access(check_access == needs_access_check), _tag(tag) {}
 169 
 170   // Case where we just find the method and don't check access against the current class
 171   LinkInfo(Klass* resolved_klass, Symbol*name, Symbol* signature) :
 172     _name(name),
 173     _signature(signature), _resolved_klass(resolved_klass), _current_klass(NULL), _current_method(methodHandle()),
 174     _check_access(false), _tag(JVM_CONSTANT_Invalid) {}
 175 
 176   // accessors
 177   Symbol* name() const               { return _name; }
 178   Symbol* signature() const          { return _signature; }
 179   Klass* resolved_klass() const      { return _resolved_klass; }
 180   Klass* current_klass() const       { return _current_klass; }
 181   Method* current_method() const     { return _current_method(); }
 182   constantTag tag() const            { return _tag; }
 183   bool check_access() const          { return _check_access; }
 184 
 185   void         print()  PRODUCT_RETURN;
 186 };
 187 
 188 // Link information for getfield/putfield & getstatic/putstatic bytecodes
 189 // is represented using a fieldDescriptor.
 190 
 191 // The LinkResolver is used to resolve constant-pool references at run-time.
 192 // It does all necessary link-time checks & throws exceptions if necessary.
 193 
 194 class LinkResolver: AllStatic {
 195   friend class klassVtable;
 196   friend class klassItable;
 197 
 198  private:
 199 
 200   static Method* lookup_method_in_klasses(const LinkInfo& link_info,
 201                                           bool checkpolymorphism,
 202                                           bool in_imethod_resolve);
 203   static Method* lookup_method_in_interfaces(const LinkInfo& link_info);
 204 
 205   static Method* lookup_polymorphic_method(const LinkInfo& link_info,
 206                                            Handle *appendix_result_or_null, TRAPS);
 207  JVMCI_ONLY(public:) // Needed for CompilerToVM.resolveMethod()
 208   // Not Linktime so doesn't take LinkInfo
 209   static Method* lookup_instance_method_in_klasses (Klass* klass, Symbol* name, Symbol* signature,
 210                                                     Klass::PrivateLookupMode private_mode, TRAPS);
 211  JVMCI_ONLY(private:)
 212 
 213   // Similar loader constraint checking functions that throw
 214   // LinkageError with descriptive message.
 215   static void check_method_loader_constraints(const LinkInfo& link_info,
 216                                               const methodHandle& resolved_method,
 217                                               const char* method_type, TRAPS);
 218   static void check_field_loader_constraints(Symbol* field, Symbol* sig,
 219                                              Klass* current_klass,
 220                                              Klass* sel_klass, TRAPS);
 221 
 222   static Method* resolve_interface_method(const LinkInfo& link_info, Bytecodes::Code code, TRAPS);
 223   static Method* resolve_method          (const LinkInfo& link_info, Bytecodes::Code code, TRAPS);
 224 
 225   static Method* linktime_resolve_static_method    (const LinkInfo& link_info, TRAPS);
 226   static Method* linktime_resolve_special_method   (const LinkInfo& link_info, TRAPS);
 227   static Method* linktime_resolve_virtual_method   (const LinkInfo& link_info, TRAPS);
 228   static Method* linktime_resolve_interface_method (const LinkInfo& link_info, TRAPS);
 229 
 230   static void runtime_resolve_special_method    (CallInfo& result,
 231                                                  const LinkInfo& link_info,
 232                                                  const methodHandle& resolved_method,
 233                                                  Handle recv, TRAPS);
 234 
 235   static void runtime_resolve_virtual_method    (CallInfo& result,
 236                                                  const methodHandle& resolved_method,
 237                                                  Klass* resolved_klass,
 238                                                  Handle recv,
 239                                                  Klass* recv_klass,
 240                                                  bool check_null_and_abstract, TRAPS);
 241   static void runtime_resolve_interface_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 
 248   static void check_field_accessability(Klass* ref_klass,
 249                                         Klass* resolved_klass,
 250                                         Klass* sel_klass,
 251                                         const fieldDescriptor& fd, TRAPS);
 252   static void check_method_accessability(Klass* ref_klass,
 253                                          Klass* resolved_klass,
 254                                          Klass* sel_klass,
 255                                          const methodHandle& sel_method, TRAPS);
 256 
 257   // runtime resolving from constant pool
 258   static void resolve_invokestatic   (CallInfo& result,
 259                                       const constantPoolHandle& pool, int index, TRAPS);
 260   static void resolve_invokespecial  (CallInfo& result, Handle recv,
 261                                       const constantPoolHandle& pool, int index, TRAPS);
 262   static void resolve_invokevirtual  (CallInfo& result, Handle recv,
 263                                       const constantPoolHandle& pool, int index, TRAPS);
 264   static void resolve_invokeinterface(CallInfo& result, Handle recv,
 265                                       const constantPoolHandle& pool, int index, TRAPS);
 266   static void resolve_invokedynamic  (CallInfo& result,
 267                                       const constantPoolHandle& pool, int index, TRAPS);
 268   static void resolve_invokehandle   (CallInfo& result,
 269                                       const constantPoolHandle& pool, int index, TRAPS);
 270  public:
 271   // constant pool resolving
 272   static void check_klass_accessability(Klass* ref_klass, Klass* sel_klass,
 273                                         bool fold_type_to_class, TRAPS);
 274   // The optional 'fold_type_to_class' means that a derived type (array)
 275   // is first converted to the class it is derived from (element type).
 276   // If this element type is not a class, then the check passes quietly.
 277   // This is usually what is needed, but a few existing uses might break
 278   // if this flag were always turned on.  FIXME: See if it can be, always.
 279   static void check_klass_accessability(Klass* ref_klass, Klass* sel_klass, TRAPS) {
 280     return check_klass_accessability(ref_klass, sel_klass, false, THREAD);
 281   }
 282 
 283   // static resolving calls (will not run any Java code);
 284   // used only from Bytecode_invoke::static_target
 285   static Method* resolve_method_statically(Bytecodes::Code code,
 286                                            const constantPoolHandle& pool,
 287                                            int index, TRAPS);
 288 
 289   static void resolve_field_access(fieldDescriptor& result,
 290                                    const constantPoolHandle& pool,
 291                                    int index,
 292                                    const methodHandle& method,
 293                                    Bytecodes::Code byte, TRAPS);
 294   static void resolve_field(fieldDescriptor& result, const LinkInfo& link_info,
 295                             Bytecodes::Code access_kind,
 296                             bool initialize_class, TRAPS);
 297 
 298   static void resolve_static_call   (CallInfo& result,
 299                                      const LinkInfo& link_info,
 300                                      bool initialize_klass, TRAPS);
 301   static void resolve_special_call  (CallInfo& result,
 302                                      Handle recv,
 303                                      const LinkInfo& link_info,
 304                                      TRAPS);
 305   static void resolve_virtual_call  (CallInfo& result, Handle recv, Klass* recv_klass,
 306                                      const LinkInfo& link_info,
 307                                      bool check_null_and_abstract, TRAPS);
 308   static void resolve_interface_call(CallInfo& result, Handle recv, Klass* recv_klass,
 309                                      const LinkInfo& link_info,
 310                                      bool check_null_and_abstract, TRAPS);
 311   static void resolve_handle_call   (CallInfo& result,
 312                                      const LinkInfo& link_info, TRAPS);
 313   static void resolve_dynamic_call  (CallInfo& result,
 314                                      BootstrapInfo& bootstrap_specifier, TRAPS);
 315 
 316   // same as above for compile-time resolution; but returns null handle instead of throwing
 317   // an exception on error also, does not initialize klass (i.e., no side effects)
 318   static Method* resolve_virtual_call_or_null(Klass* receiver_klass,
 319                                               const LinkInfo& link_info);
 320   static Method* resolve_interface_call_or_null(Klass* receiver_klass,
 321                                                 const LinkInfo& link_info);
 322   static Method* resolve_static_call_or_null(const LinkInfo& link_info);
 323   static Method* resolve_special_call_or_null(const LinkInfo& link_info);
 324 
 325   static int vtable_index_of_interface_method(Klass* klass, const methodHandle& resolved_method);
 326 
 327   // same as above for compile-time resolution; returns vtable_index if current_klass if linked
 328   static int resolve_virtual_vtable_index  (Klass* receiver_klass,
 329                                             const LinkInfo& link_info);
 330 
 331   // static resolving for compiler (does not throw exceptions, returns null handle if unsuccessful)
 332   static Method* linktime_resolve_virtual_method_or_null  (const LinkInfo& link_info);
 333   static Method* linktime_resolve_interface_method_or_null(const LinkInfo& link_info);
 334 
 335   // runtime resolving from constant pool
 336   static void resolve_invoke(CallInfo& result, Handle recv,
 337                              const constantPoolHandle& pool, int index,
 338                              Bytecodes::Code byte, TRAPS);
 339 
 340   // runtime resolving from attached method
 341   static void resolve_invoke(CallInfo& result, Handle& recv,
 342                              const methodHandle& attached_method,
 343                              Bytecodes::Code byte, TRAPS);
 344 
 345  public:
 346   // Only resolved method known.
 347   static void throw_abstract_method_error(const methodHandle& resolved_method, TRAPS) {
 348     throw_abstract_method_error(resolved_method, methodHandle(), NULL, CHECK);
 349   }
 350   // Resolved method and receiver klass know.
 351   static void throw_abstract_method_error(const methodHandle& resolved_method, Klass *recv_klass, TRAPS) {
 352     throw_abstract_method_error(resolved_method, methodHandle(), recv_klass, CHECK);
 353   }
 354   // Selected method is abstract.
 355   static void throw_abstract_method_error(const methodHandle& resolved_method,
 356                                           const methodHandle& selected_method,
 357                                           Klass *recv_klass, TRAPS);
 358 };
 359 #endif // SHARE_INTERPRETER_LINKRESOLVER_HPP