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