1 /* 2 * Copyright (c) 1997, 2022, 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 void check_constraints(GrowableArray<InstanceKlass*>* supers, TRAPS); 52 53 public: 54 klassVtable(Klass* klass, void* base, int length) : _klass(klass) { 55 _tableOffset = (address)base - (address)klass; _length = length; 56 } 57 58 // accessors 59 vtableEntry* table() const { return (vtableEntry*)(address(_klass) + _tableOffset); } 60 Klass* klass() const { return _klass; } 61 int length() const { return _length; } 62 inline Method* method_at(int i) const; 63 inline Method* unchecked_method_at(int i) const; 64 65 // searching; all methods return -1 if not found 66 int index_of_miranda(Symbol* name, Symbol* signature); 67 68 // initialize vtable of a new klass 69 void initialize_vtable(GrowableArray<InstanceKlass*>* supers = NULL); 70 void initialize_vtable_and_check_constraints(TRAPS); 71 72 // computes vtable length (in words) and the number of miranda methods 73 static void compute_vtable_size_and_num_mirandas(int* vtable_length, 74 int* num_new_mirandas, 75 GrowableArray<Method*>* all_mirandas, 76 const Klass* super, 77 Array<Method*>* methods, 78 AccessFlags class_flags, 79 u2 major_version, 80 Handle classloader, 81 Symbol* classname, 82 Array<InstanceKlass*>* local_interfaces); 83 84 #if INCLUDE_JVMTI 85 // RedefineClasses() API support: 86 // If any entry of this vtable points to any of old_methods, 87 // replace it with the corresponding new_method. 88 // trace_name_printed is set to true if the current call has 89 // printed the klass name so that other routines in the adjust_* 90 // group don't print the klass name. 91 bool adjust_default_method(int vtable_index, Method* old_method, Method* new_method); 92 void adjust_method_entries(bool* trace_name_printed); 93 bool check_no_old_or_obsolete_entries(); 94 void dump_vtable(); 95 #endif // INCLUDE_JVMTI 96 97 // Debugging code 98 void print() PRODUCT_RETURN; 99 void verify(outputStream* st, bool force = false); 100 101 protected: 102 friend class vtableEntry; 103 104 public: 105 // Transitive overridng rules for class files < JDK1_7 use the older JVMS rules. 106 // Overriding is determined as we create the vtable, so we use the class file version 107 // of the class whose vtable we are calculating. 108 enum { VTABLE_TRANSITIVE_OVERRIDE_VERSION = 51 } ; 109 110 private: 111 void copy_vtable_to(vtableEntry* start); 112 int initialize_from_super(Klass* super); 113 void put_method_at(Method* m, int index); 114 static bool needs_new_vtable_entry(Method* m, 115 const Klass* super, 116 Handle classloader, 117 Symbol* classname, 118 AccessFlags access_flags, 119 u2 major_version); 120 121 bool update_inherited_vtable(Thread* current, 122 const methodHandle& target_method, 123 int super_vtable_len, 124 int default_index, 125 GrowableArray<InstanceKlass*>* supers); 126 InstanceKlass* find_transitive_override(InstanceKlass* initialsuper, 127 const methodHandle& target_method, int vtable_index, 128 Handle target_loader, Symbol* target_classname); 129 130 // support for miranda methods 131 bool is_miranda_entry_at(int i); 132 int fill_in_mirandas(Thread* current, int initialized); 133 static bool is_miranda(Method* m, Array<Method*>* class_methods, 134 Array<Method*>* default_methods, const Klass* super, 135 bool is_interface); 136 static void add_new_mirandas_to_lists( 137 GrowableArray<Method*>* new_mirandas, 138 GrowableArray<Method*>* all_mirandas, 139 Array<Method*>* current_interface_methods, 140 Array<Method*>* class_methods, 141 Array<Method*>* default_methods, 142 const Klass* super, 143 bool is_interface); 144 static void get_mirandas( 145 GrowableArray<Method*>* new_mirandas, 146 GrowableArray<Method*>* all_mirandas, 147 const Klass* super, 148 Array<Method*>* class_methods, 149 Array<Method*>* default_methods, 150 Array<InstanceKlass*>* local_interfaces, 151 bool is_interface); 152 void verify_against(outputStream* st, klassVtable* vt, int index); 153 inline InstanceKlass* ik() const; 154 // When loading a class from CDS archive at run time, and no class redefinition 155 // has happened, it is expected that the class's itable/vtables are 156 // laid out exactly the same way as they had been during dump time. 157 // Therefore, in klassVtable::initialize_[iv]table, we do not layout the 158 // tables again. Instead, we only rerun the process to create/check 159 // the class loader constraints. In non-product builds, we add asserts to 160 // guarantee that the table's layout would be the same as at dump time. 161 // 162 // If JVMTI redefines any class, the read-only shared memory are remapped 163 // as read-write. A shared class' vtable/itable are re-initialized and 164 // might have different layout due to class redefinition of the shared class 165 // or its super types. 166 bool is_preinitialized_vtable(); 167 }; 168 169 170 // private helper class for klassVtable 171 // description of entry points: 172 // destination is interpreted: 173 // from_compiled_code_entry_point -> c2iadapter 174 // from_interpreter_entry_point -> interpreter entry point 175 // destination is compiled: 176 // from_compiled_code_entry_point -> nmethod entry point 177 // from_interpreter_entry_point -> i2cadapter 178 class vtableEntry { 179 friend class VMStructs; 180 friend class JVMCIVMStructs; 181 182 public: 183 // size in words 184 static int size() { return sizeof(vtableEntry) / wordSize; } 185 static int size_in_bytes() { return sizeof(vtableEntry); } 186 187 static int method_offset_in_bytes() { return offset_of(vtableEntry, _method); } 188 Method* method() const { return _method; } 189 Method** method_addr() { return &_method; } 190 191 private: 192 Method* _method; 193 void set(Method* method) { assert(method != NULL, "use clear"); _method = method; } 194 void clear() { _method = NULL; } 195 void print() PRODUCT_RETURN; 196 void verify(klassVtable* vt, outputStream* st); 197 198 friend class klassVtable; 199 }; 200 201 202 inline Method* klassVtable::method_at(int i) const { 203 assert(i >= 0 && i < _length, "index out of bounds"); 204 assert(table()[i].method() != NULL, "should not be null"); 205 assert(((Metadata*)table()[i].method())->is_method(), "should be method"); 206 return table()[i].method(); 207 } 208 209 inline Method* klassVtable::unchecked_method_at(int i) const { 210 assert(i >= 0 && i < _length, "index out of bounds"); 211 return table()[i].method(); 212 } 213 214 // -------------------------------------------------------------------------------- 215 class klassItable; 216 class itableMethodEntry; 217 218 class itableOffsetEntry { 219 private: 220 InstanceKlass* _interface; 221 int _offset; 222 public: 223 InstanceKlass* interface_klass() const { return _interface; } 224 InstanceKlass**interface_klass_addr() { return &_interface; } 225 int offset() const { return _offset; } 226 227 static itableMethodEntry* method_entry(Klass* k, int offset) { return (itableMethodEntry*)(((address)k) + offset); } 228 itableMethodEntry* first_method_entry(Klass* k) { return method_entry(k, _offset); } 229 230 void initialize(InstanceKlass* interf, int offset) { _interface = interf; _offset = offset; } 231 232 // Static size and offset accessors 233 static int size() { return sizeof(itableOffsetEntry) / wordSize; } // size in words 234 static int interface_offset_in_bytes() { return offset_of(itableOffsetEntry, _interface); } 235 static int offset_offset_in_bytes() { return offset_of(itableOffsetEntry, _offset); } 236 237 friend class klassItable; 238 }; 239 240 241 class itableMethodEntry { 242 private: 243 Method* _method; 244 245 public: 246 Method* method() const { return _method; } 247 Method**method_addr() { return &_method; } 248 249 void clear() { _method = NULL; } 250 251 void initialize(InstanceKlass* klass, Method* method); 252 253 // Static size and offset accessors 254 static int size() { return sizeof(itableMethodEntry) / wordSize; } // size in words 255 static int method_offset_in_bytes() { return offset_of(itableMethodEntry, _method); } 256 257 friend class klassItable; 258 }; 259 260 // 261 // Format of an itable 262 // 263 // ---- offset table --- 264 // Klass* of interface 1 \ 265 // offset to vtable from start of oop / offset table entry 266 // ... 267 // Klass* of interface n \ 268 // offset to vtable from start of oop / offset table entry 269 // --- vtable for interface 1 --- 270 // Method* \ 271 // compiler entry point / method table entry 272 // ... 273 // Method* \ 274 // compiler entry point / method table entry 275 // -- vtable for interface 2 --- 276 // ... 277 // 278 class klassItable { 279 private: 280 InstanceKlass* _klass; // my klass 281 int _table_offset; // offset of start of itable data within klass (in words) 282 int _size_offset_table; // size of offset table (in itableOffset entries) 283 int _size_method_table; // size of methodtable (in itableMethodEntry entries) 284 285 void initialize_itable_for_interface(int method_table_offset, InstanceKlass* interf_h, 286 GrowableArray<Method*>* supers, int start_offset); 287 void check_constraints(GrowableArray<Method*>* supers, TRAPS); 288 public: 289 klassItable(InstanceKlass* klass); 290 291 itableOffsetEntry* offset_entry(int i) { assert(0 <= i && i <= _size_offset_table, "index out of bounds"); 292 return &((itableOffsetEntry*)vtable_start())[i]; } 293 294 itableMethodEntry* method_entry(int i) { assert(0 <= i && i <= _size_method_table, "index out of bounds"); 295 return &((itableMethodEntry*)method_start())[i]; } 296 297 int size_offset_table() { return _size_offset_table; } 298 299 // Initialization 300 void initialize_itable_and_check_constraints(TRAPS); 301 void initialize_itable(GrowableArray<Method*>* supers = NULL); 302 303 #if INCLUDE_JVMTI 304 // RedefineClasses() API support: 305 // if any entry of this itable points to any of old_methods, 306 // replace it with the corresponding new_method. 307 // trace_name_printed is set to true if the current call has 308 // printed the klass name so that other routines in the adjust_* 309 // group don't print the klass name. 310 void adjust_method_entries(bool* trace_name_printed); 311 bool check_no_old_or_obsolete_entries(); 312 void dump_itable(); 313 #endif // INCLUDE_JVMTI 314 315 // Setup of itable 316 static int assign_itable_indices_for_interface(InstanceKlass* klass); 317 static int method_count_for_interface(InstanceKlass* klass); 318 static int compute_itable_size(Array<InstanceKlass*>* transitive_interfaces); 319 static void setup_itable_offset_table(InstanceKlass* klass); 320 321 private: 322 intptr_t* vtable_start() const { return ((intptr_t*)_klass) + _table_offset; } 323 intptr_t* method_start() const { return vtable_start() + _size_offset_table * itableOffsetEntry::size(); } 324 325 // Helper methods 326 static int calc_itable_size(int num_interfaces, int num_methods) { return (num_interfaces * itableOffsetEntry::size()) + (num_methods * itableMethodEntry::size()); } 327 328 }; 329 330 #endif // SHARE_OOPS_KLASSVTABLE_HPP