src/share/vm/oops/klassVtable.hpp

Thu, 04 Apr 2019 17:56:29 +0800

author
aoqi
date
Thu, 04 Apr 2019 17:56:29 +0800
changeset 9572
624a0741915c
parent 8604
04d83ba48607
permissions
-rw-r--r--

Merge

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

mercurial