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