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_OOPS_KLASSVTABLE_HPP
  26 #define SHARE_OOPS_KLASSVTABLE_HPP
  27 
  28 #include "oops/oopsHierarchy.hpp"
  29 #include "runtime/handles.hpp"
  30 #include "utilities/growableArray.hpp"
  31 
  32 // A klassVtable abstracts the variable-length vtable that is embedded in InstanceKlass
  33 // and ArrayKlass.  klassVtable objects are used just as convenient transient accessors to the vtable,
  34 // not to actually hold the vtable data.
  35 // Note: the klassVtable should not be accessed before the class has been verified
  36 // (until that point, the vtable is uninitialized).
  37 
  38 // Currently a klassVtable contains a direct reference to the vtable data, and is therefore
  39 // not preserved across GCs.
  40 
  41 class vtableEntry;
  42 
  43 class klassVtable {
  44   Klass*       _klass;            // my klass
  45   int          _tableOffset;      // offset of start of vtable data within klass
  46   int          _length;           // length of vtable (number of entries)
  47 #ifndef PRODUCT
  48   int          _verify_count;     // to make verify faster
  49 #endif
  50 
  51   // Ordering important, so greater_than (>) can be used as an merge operator.
  52   enum AccessType {
  53     acc_private         = 0,
  54     acc_package_private = 1,
  55     acc_publicprotected = 2
  56   };
  57 
  58  public:
  59   klassVtable(Klass* klass, void* base, int length) : _klass(klass) {
  60     _tableOffset = (address)base - (address)klass; _length = length;
  61   }
  62 
  63   // accessors
  64   vtableEntry* table() const      { return (vtableEntry*)(address(_klass) + _tableOffset); }
  65   Klass* klass() const            { return _klass;  }
  66   int length() const              { return _length; }
  67   inline Method* method_at(int i) const;
  68   inline Method* unchecked_method_at(int i) const;
  69   inline Method** adr_method_at(int i) const;
  70 
  71   // searching; all methods return -1 if not found
  72   int index_of(Method* m) const                         { return index_of(m, _length); }
  73   int index_of_miranda(Symbol* name, Symbol* signature);
  74 
  75   void initialize_vtable(bool checkconstraints, TRAPS);   // initialize vtable of a new klass
  76 
  77   // CDS/RedefineClasses support - clear vtables so they can be reinitialized
  78   // at dump time.  Clearing gives us an easy way to tell if the vtable has
  79   // already been reinitialized at dump time (see dump.cpp).  Vtables can
  80   // be initialized at run time by RedefineClasses so dumping the right order
  81   // is necessary.
  82   void clear_vtable();
  83   bool is_initialized();
  84 
  85   // computes vtable length (in words) and the number of miranda methods
  86   static void compute_vtable_size_and_num_mirandas(int* vtable_length,
  87                                                    int* num_new_mirandas,
  88                                                    GrowableArray<Method*>* all_mirandas,
  89                                                    const Klass* super,
  90                                                    Array<Method*>* methods,
  91                                                    AccessFlags class_flags,
  92                                                    bool is_class_sealed,
  93                                                    u2 major_version,
  94                                                    Handle classloader,
  95                                                    Symbol* classname,
  96                                                    Array<InstanceKlass*>* local_interfaces,
  97                                                    TRAPS);
  98 
  99 #if INCLUDE_JVMTI
 100   // RedefineClasses() API support:
 101   // If any entry of this vtable points to any of old_methods,
 102   // replace it with the corresponding new_method.
 103   // trace_name_printed is set to true if the current call has
 104   // printed the klass name so that other routines in the adjust_*
 105   // group don't print the klass name.
 106   bool adjust_default_method(int vtable_index, Method* old_method, Method* new_method);
 107   void adjust_method_entries(bool* trace_name_printed);
 108   bool check_no_old_or_obsolete_entries();
 109   void dump_vtable();
 110 #endif // INCLUDE_JVMTI
 111 
 112   // Debugging code
 113   void print()                                              PRODUCT_RETURN;
 114   void verify(outputStream* st, bool force = false);
 115   static void print_statistics()                            PRODUCT_RETURN;
 116 
 117  protected:
 118   friend class vtableEntry;
 119 
 120  public:
 121   // Transitive overridng rules for class files < JDK1_7 use the older JVMS rules.
 122   // Overriding is determined as we create the vtable, so we use the class file version
 123   // of the class whose vtable we are calculating.
 124   enum { VTABLE_TRANSITIVE_OVERRIDE_VERSION = 51 } ;
 125 
 126  private:
 127   void copy_vtable_to(vtableEntry* start);
 128   int  initialize_from_super(Klass* super);
 129   int  index_of(Method* m, int len) const; // same as index_of, but search only up to len
 130   void put_method_at(Method* m, int index);
 131   static bool needs_new_vtable_entry(const methodHandle& m,
 132                                      const Klass* super,
 133                                      Handle classloader,
 134                                      Symbol* classname,
 135                                      AccessFlags access_flags,
 136                                      bool is_sealed,
 137                                      u2 major_version,
 138                                      TRAPS);
 139 
 140   bool update_inherited_vtable(InstanceKlass* klass, const methodHandle& target_method, int super_vtable_len, int default_index, bool checkconstraints, TRAPS);
 141  InstanceKlass* find_transitive_override(InstanceKlass* initialsuper, const methodHandle& target_method, int vtable_index,
 142                                          Handle target_loader, Symbol* target_classname, Thread* THREAD);
 143 
 144   // support for miranda methods
 145   bool is_miranda_entry_at(int i);
 146   int fill_in_mirandas(int initialized, TRAPS);
 147   static bool is_miranda(Method* m, Array<Method*>* class_methods,
 148                          Array<Method*>* default_methods, const Klass* super,
 149                          bool is_interface);
 150   static void add_new_mirandas_to_lists(
 151       GrowableArray<Method*>* new_mirandas,
 152       GrowableArray<Method*>* all_mirandas,
 153       Array<Method*>* current_interface_methods,
 154       Array<Method*>* class_methods,
 155       Array<Method*>* default_methods,
 156       const Klass* super,
 157       bool is_interface);
 158   static void get_mirandas(
 159       GrowableArray<Method*>* new_mirandas,
 160       GrowableArray<Method*>* all_mirandas,
 161       const Klass* super,
 162       Array<Method*>* class_methods,
 163       Array<Method*>* default_methods,
 164       Array<InstanceKlass*>* local_interfaces,
 165       bool is_interface);
 166   void verify_against(outputStream* st, klassVtable* vt, int index);
 167   inline InstanceKlass* ik() const;
 168   // When loading a class from CDS archive at run time, and no class redefintion
 169   // has happened, it is expected that the class's itable/vtables are
 170   // laid out exactly the same way as they had been during dump time.
 171   // Therefore, in klassVtable::initialize_[iv]table, we do not layout the
 172   // tables again. Instead, we only rerun the process to create/check
 173   // the class loader constraints. In non-product builds, we add asserts to
 174   // guarantee that the table's layout would be the same as at dump time.
 175   //
 176   // If JVMTI redefines any class, the read-only shared memory are remapped
 177   // as read-write. A shared class' vtable/itable are re-initialized and
 178   // might have different layout due to class redefinition of the shared class
 179   // or its super types.
 180   bool is_preinitialized_vtable();
 181 };
 182 
 183 
 184 // private helper class for klassVtable
 185 // description of entry points:
 186 //    destination is interpreted:
 187 //      from_compiled_code_entry_point -> c2iadapter
 188 //      from_interpreter_entry_point   -> interpreter entry point
 189 //    destination is compiled:
 190 //      from_compiled_code_entry_point -> nmethod entry point
 191 //      from_interpreter_entry_point   -> i2cadapter
 192 class vtableEntry {
 193   friend class VMStructs;
 194   friend class JVMCIVMStructs;
 195 
 196  public:
 197   // size in words
 198   static int size()          { return sizeof(vtableEntry) / wordSize; }
 199   static int size_in_bytes() { return sizeof(vtableEntry); }
 200 
 201   static int method_offset_in_bytes() { return offset_of(vtableEntry, _method); }
 202   Method* method() const    { return _method; }
 203   Method** method_addr()    { return &_method; }
 204 
 205  private:
 206   Method* _method;
 207   void set(Method* method)  { assert(method != NULL, "use clear"); _method = method; }
 208   void clear()                { _method = NULL; }
 209   void print()                                        PRODUCT_RETURN;
 210   void verify(klassVtable* vt, outputStream* st);
 211 
 212   friend class klassVtable;
 213 };
 214 
 215 
 216 inline Method* klassVtable::method_at(int i) const {
 217   assert(i >= 0 && i < _length, "index out of bounds");
 218   assert(table()[i].method() != NULL, "should not be null");
 219   assert(((Metadata*)table()[i].method())->is_method(), "should be method");
 220   return table()[i].method();
 221 }
 222 
 223 inline Method* klassVtable::unchecked_method_at(int i) const {
 224   assert(i >= 0 && i < _length, "index out of bounds");
 225   return table()[i].method();
 226 }
 227 
 228 inline Method** klassVtable::adr_method_at(int i) const {
 229   // Allow one past the last entry to be referenced; useful for loop bounds.
 230   assert(i >= 0 && i <= _length, "index out of bounds");
 231   return (Method**)(address(table() + i) + vtableEntry::method_offset_in_bytes());
 232 }
 233 
 234 // --------------------------------------------------------------------------------
 235 class klassItable;
 236 class itableMethodEntry;
 237 
 238 class itableOffsetEntry {
 239  private:
 240   InstanceKlass* _interface;
 241   int      _offset;
 242  public:
 243   InstanceKlass* interface_klass() const { return _interface; }
 244   InstanceKlass**interface_klass_addr()  { return &_interface; }
 245   int      offset() const          { return _offset; }
 246 
 247   static itableMethodEntry* method_entry(Klass* k, int offset) { return (itableMethodEntry*)(((address)k) + offset); }
 248   itableMethodEntry* first_method_entry(Klass* k)              { return method_entry(k, _offset); }
 249 
 250   void initialize(InstanceKlass* interf, int offset) { _interface = interf; _offset = offset; }
 251 
 252   // Static size and offset accessors
 253   static int size()                       { return sizeof(itableOffsetEntry) / wordSize; }    // size in words
 254   static int interface_offset_in_bytes()  { return offset_of(itableOffsetEntry, _interface); }
 255   static int offset_offset_in_bytes()     { return offset_of(itableOffsetEntry, _offset); }
 256 
 257   friend class klassItable;
 258 };
 259 
 260 
 261 class itableMethodEntry {
 262  private:
 263   Method* _method;
 264 
 265  public:
 266   Method* method() const { return _method; }
 267   Method**method_addr() { return &_method; }
 268 
 269   void clear()             { _method = NULL; }
 270 
 271   void initialize(Method* method);
 272 
 273   // Static size and offset accessors
 274   static int size()                         { return sizeof(itableMethodEntry) / wordSize; }  // size in words
 275   static int method_offset_in_bytes()       { return offset_of(itableMethodEntry, _method); }
 276 
 277   friend class klassItable;
 278 };
 279 
 280 //
 281 // Format of an itable
 282 //
 283 //    ---- offset table ---
 284 //    Klass* of interface 1             \
 285 //    offset to vtable from start of oop  / offset table entry
 286 //    ...
 287 //    Klass* of interface n             \
 288 //    offset to vtable from start of oop  / offset table entry
 289 //    --- vtable for interface 1 ---
 290 //    Method*                             \
 291 //    compiler entry point                / method table entry
 292 //    ...
 293 //    Method*                             \
 294 //    compiler entry point                / method table entry
 295 //    -- vtable for interface 2 ---
 296 //    ...
 297 //
 298 class klassItable {
 299  private:
 300   InstanceKlass*       _klass;             // my klass
 301   int                  _table_offset;      // offset of start of itable data within klass (in words)
 302   int                  _size_offset_table; // size of offset table (in itableOffset entries)
 303   int                  _size_method_table; // size of methodtable (in itableMethodEntry entries)
 304 
 305   void initialize_itable_for_interface(int method_table_offset, InstanceKlass* interf_h, bool checkconstraints, TRAPS);
 306  public:
 307   klassItable(InstanceKlass* klass);
 308 
 309   itableOffsetEntry* offset_entry(int i) { assert(0 <= i && i <= _size_offset_table, "index out of bounds");
 310                                            return &((itableOffsetEntry*)vtable_start())[i]; }
 311 
 312   itableMethodEntry* method_entry(int i) { assert(0 <= i && i <= _size_method_table, "index out of bounds");
 313                                            return &((itableMethodEntry*)method_start())[i]; }
 314 
 315   int size_offset_table()                { return _size_offset_table; }
 316 
 317   // Initialization
 318   void initialize_itable(bool checkconstraints, TRAPS);
 319 
 320 #if INCLUDE_JVMTI
 321   // RedefineClasses() API support:
 322   // if any entry of this itable points to any of old_methods,
 323   // replace it with the corresponding new_method.
 324   // trace_name_printed is set to true if the current call has
 325   // printed the klass name so that other routines in the adjust_*
 326   // group don't print the klass name.
 327   void adjust_method_entries(bool* trace_name_printed);
 328   bool check_no_old_or_obsolete_entries();
 329   void dump_itable();
 330 #endif // INCLUDE_JVMTI
 331 
 332   // Setup of itable
 333   static int assign_itable_indices_for_interface(InstanceKlass* klass, TRAPS);
 334   static int method_count_for_interface(InstanceKlass* klass);
 335   static int compute_itable_size(Array<InstanceKlass*>* transitive_interfaces);
 336   static void setup_itable_offset_table(InstanceKlass* klass);
 337 
 338   // Resolving of method to index
 339   static Method* method_for_itable_index(InstanceKlass* klass, int itable_index);
 340 
 341   // Debugging/Statistics
 342   static void print_statistics() PRODUCT_RETURN;
 343  private:
 344   intptr_t* vtable_start() const { return ((intptr_t*)_klass) + _table_offset; }
 345   intptr_t* method_start() const { return vtable_start() + _size_offset_table * itableOffsetEntry::size(); }
 346 
 347   // Helper methods
 348   static int  calc_itable_size(int num_interfaces, int num_methods) { return (num_interfaces * itableOffsetEntry::size()) + (num_methods * itableMethodEntry::size()); }
 349 
 350   // Statistics
 351   NOT_PRODUCT(static int  _total_classes;)   // Total no. of classes with itables
 352   NOT_PRODUCT(static long _total_size;)      // Total no. of bytes used for itables
 353 
 354   static void update_stats(int size) PRODUCT_RETURN NOT_PRODUCT({ _total_classes++; _total_size += size; })
 355 };
 356 
 357 #endif // SHARE_OOPS_KLASSVTABLE_HPP