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