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