1 /*
2 * Copyright (c) 1997, 2021, 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:
klassVtable(Klass * klass,void * base,int length)54 klassVtable(Klass* klass, void* base, int length) : _klass(klass) {
55 _tableOffset = (address)base - (address)klass; _length = length;
56 }
57
58 // accessors
table() const59 vtableEntry* table() const { return (vtableEntry*)(address(_klass) + _tableOffset); }
klass() const60 Klass* klass() const { return _klass; }
length() const61 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 static void print_statistics() PRODUCT_RETURN;
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 redefintion
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
size()185 static int size() { return sizeof(vtableEntry) / wordSize; }
size_in_bytes()186 static int size_in_bytes() { return sizeof(vtableEntry); }
187
method_offset_in_bytes()188 static int method_offset_in_bytes() { return offset_of(vtableEntry, _method); }
method() const189 Method* method() const { return _method; }
method_addr()190 Method** method_addr() { return &_method; }
191
192 private:
193 Method* _method;
set(Method * method)194 void set(Method* method) { assert(method != NULL, "use clear"); _method = method; }
clear()195 void clear() { _method = NULL; }
196 void print() PRODUCT_RETURN;
197 void verify(klassVtable* vt, outputStream* st);
198
199 friend class klassVtable;
200 };
201
202
method_at(int i) const203 inline Method* klassVtable::method_at(int i) const {
204 assert(i >= 0 && i < _length, "index out of bounds");
205 assert(table()[i].method() != NULL, "should not be null");
206 assert(((Metadata*)table()[i].method())->is_method(), "should be method");
207 return table()[i].method();
208 }
209
unchecked_method_at(int i) const210 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:
interface_klass() const224 InstanceKlass* interface_klass() const { return _interface; }
interface_klass_addr()225 InstanceKlass**interface_klass_addr() { return &_interface; }
offset() const226 int offset() const { return _offset; }
227
method_entry(Klass * k,int offset)228 static itableMethodEntry* method_entry(Klass* k, int offset) { return (itableMethodEntry*)(((address)k) + offset); }
first_method_entry(Klass * k)229 itableMethodEntry* first_method_entry(Klass* k) { return method_entry(k, _offset); }
230
initialize(InstanceKlass * interf,int offset)231 void initialize(InstanceKlass* interf, int offset) { _interface = interf; _offset = offset; }
232
233 // Static size and offset accessors
size()234 static int size() { return sizeof(itableOffsetEntry) / wordSize; } // size in words
interface_offset_in_bytes()235 static int interface_offset_in_bytes() { return offset_of(itableOffsetEntry, _interface); }
offset_offset_in_bytes()236 static int offset_offset_in_bytes() { return offset_of(itableOffsetEntry, _offset); }
237
238 friend class klassItable;
239 };
240
241
242 class itableMethodEntry {
243 private:
244 Method* _method;
245
246 public:
method() const247 Method* method() const { return _method; }
method_addr()248 Method**method_addr() { return &_method; }
249
clear()250 void clear() { _method = NULL; }
251
252 void initialize(InstanceKlass* klass, Method* method);
253
254 // Static size and offset accessors
size()255 static int size() { return sizeof(itableMethodEntry) / wordSize; } // size in words
method_offset_in_bytes()256 static int method_offset_in_bytes() { return 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
offset_entry(int i)292 itableOffsetEntry* offset_entry(int i) { assert(0 <= i && i <= _size_offset_table, "index out of bounds");
293 return &((itableOffsetEntry*)vtable_start())[i]; }
294
method_entry(int i)295 itableMethodEntry* method_entry(int i) { assert(0 <= i && i <= _size_method_table, "index out of bounds");
296 return &((itableMethodEntry*)method_start())[i]; }
297
size_offset_table()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 = NULL);
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 // Debugging/Statistics
323 static void print_statistics() PRODUCT_RETURN;
324 private:
vtable_start() const325 intptr_t* vtable_start() const { return ((intptr_t*)_klass) + _table_offset; }
method_start() const326 intptr_t* method_start() const { return vtable_start() + _size_offset_table * itableOffsetEntry::size(); }
327
328 // Helper methods
calc_itable_size(int num_interfaces,int num_methods)329 static int calc_itable_size(int num_interfaces, int num_methods) { return (num_interfaces * itableOffsetEntry::size()) + (num_methods * itableMethodEntry::size()); }
330
331 // Statistics
332 NOT_PRODUCT(static int _total_classes;) // Total no. of classes with itables
333 NOT_PRODUCT(static size_t _total_size;) // Total no. of bytes used for itables
334
335 static void update_stats(int size) PRODUCT_RETURN NOT_PRODUCT({ _total_classes++; _total_size += size; })
336 };
337
338 #endif // SHARE_OOPS_KLASSVTABLE_HPP
339