1 /*
  2  * Copyright (c) 2020, 2026, 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 #include "cds/aotGrowableArray.hpp"
 26 #include "cds/aotMetaspace.hpp"
 27 #include "cds/archiveBuilder.hpp"
 28 #include "cds/archiveUtils.hpp"
 29 #include "cds/cdsConfig.hpp"
 30 #include "cds/cppVtables.hpp"
 31 #include "logging/log.hpp"
 32 #include "memory/resourceArea.hpp"
 33 #include "oops/flatArrayKlass.hpp"
 34 #include "oops/inlineKlass.hpp"
 35 #include "oops/instanceClassLoaderKlass.hpp"
 36 #include "oops/instanceKlass.inline.hpp"
 37 #include "oops/instanceMirrorKlass.hpp"
 38 #include "oops/instanceRefKlass.hpp"
 39 #include "oops/instanceStackChunkKlass.hpp"
 40 #include "oops/methodCounters.hpp"
 41 #include "oops/methodData.hpp"
 42 #include "oops/objArrayKlass.hpp"
 43 #include "oops/refArrayKlass.hpp"
 44 #include "oops/trainingData.hpp"
 45 #include "oops/typeArrayKlass.hpp"
 46 #include "runtime/arguments.hpp"
 47 #include "utilities/globalDefinitions.hpp"
 48 
 49 // Objects of the Metadata types (such as Klass and ConstantPool) have C++ vtables.
 50 // (In GCC this is the field <Type>::_vptr, i.e., first word in the object.)
 51 //
 52 // Addresses of the vtables and the methods may be different across JVM runs,
 53 // if libjvm.so is dynamically loaded at a different base address.
 54 //
 55 // To ensure that the Metadata objects in the CDS archive always have the correct vtable:
 56 //
 57 // + at dump time:  we redirect the _vptr to point to our own vtables inside
 58 //                  the CDS image
 59 // + at run time:   we clone the actual contents of the vtables from libjvm.so
 60 //                  into our own tables.
 61 
 62 
 63 #ifndef PRODUCT
 64 
 65 // AOTGrowableArray has a vtable only when in non-product builds (due to
 66 // the virtual printing functions in AnyObj).
 67 
 68 using GrowableArray_ModuleEntry_ptr = AOTGrowableArray<ModuleEntry*>;
 69 
 70 #define DEBUG_CPP_VTABLE_TYPES_DO(f) \
 71   f(GrowableArray_ModuleEntry_ptr) \
 72 
 73 #endif
 74 
 75 // Currently, the archive contains ONLY the following types of objects that have C++ vtables.
 76 // NOTE: this table must be in-sync with sun.jvm.hotspot.memory.FileMapInfo::populateMetadataTypeArray().
 77 #define CPP_VTABLE_TYPES_DO(f) \
 78   f(ConstantPool) \
 79   f(InstanceKlass) \
 80   f(InstanceClassLoaderKlass) \
 81   f(InstanceMirrorKlass) \
 82   f(InstanceRefKlass) \
 83   f(InstanceStackChunkKlass) \
 84   f(Method) \
 85   f(MethodData) \
 86   f(MethodCounters) \
 87   f(TypeArrayKlass) \
 88   f(ObjArrayKlass) \
 89   f(RefArrayKlass) \
 90   f(FlatArrayKlass) \
 91   f(InlineKlass) \
 92   f(KlassTrainingData) \
 93   f(MethodTrainingData) \
 94   f(CompileTrainingData) \
 95   NOT_PRODUCT(DEBUG_CPP_VTABLE_TYPES_DO(f))
 96 
 97 class CppVtableInfo {
 98   intptr_t _vtable_size;
 99   intptr_t _cloned_vtable[1]; // Pseudo flexible array member.
100   static size_t cloned_vtable_offset() { return offset_of(CppVtableInfo, _cloned_vtable); }
101 public:
102   int vtable_size()           { return int(uintx(_vtable_size)); }
103   void set_vtable_size(int n) { _vtable_size = intptr_t(n); }
104   // Using _cloned_vtable[i] for i > 0 causes undefined behavior. We use address calculation instead.
105   intptr_t* cloned_vtable()   { return (intptr_t*)((char*)this + cloned_vtable_offset()); }
106   void zero()                 { memset(cloned_vtable(), 0, sizeof(intptr_t) * vtable_size()); }
107   // Returns the address of the next CppVtableInfo that can be placed immediately after this CppVtableInfo
108   static size_t byte_size(int vtable_size) {
109     return cloned_vtable_offset() + (sizeof(intptr_t) * vtable_size);
110   }
111 };
112 
113 static inline intptr_t* vtable_of(const void* m) {
114   return *((intptr_t**)m);
115 }
116 
117 template <class T> class CppVtableCloner {
118   static int get_vtable_length(const char* name);
119 
120 public:
121   // Allocate a clone of the vtable of T from the shared metaspace;
122   // Initialize the contents of this clone.
123   static CppVtableInfo* allocate_and_initialize(const char* name);
124 
125   // Copy the contents of the vtable of T into info->_cloned_vtable;
126   static void initialize(const char* name, CppVtableInfo* info);
127 
128   static void init_orig_cpp_vtptr(int kind);
129 };
130 
131 template <class T>
132 CppVtableInfo* CppVtableCloner<T>::allocate_and_initialize(const char* name) {
133   int n = get_vtable_length(name);
134   CppVtableInfo* info =
135       (CppVtableInfo*)ArchiveBuilder::current()->rw_region()->allocate(CppVtableInfo::byte_size(n));
136   info->set_vtable_size(n);
137   initialize(name, info);
138   return info;
139 }
140 
141 template <class T>
142 void CppVtableCloner<T>::initialize(const char* name, CppVtableInfo* info) {
143   ResourceMark rm;
144   T tmp; // Allocate temporary dummy metadata object to get to the original vtable.
145   int n = info->vtable_size();
146   intptr_t* srcvtable = vtable_of(&tmp);
147   intptr_t* dstvtable = info->cloned_vtable();
148 
149   // We already checked (and, if necessary, adjusted n) when the vtables were allocated, so we are
150   // safe to do memcpy.
151   log_debug(aot, vtables)("Copying %3d vtable entries for %s", n, name);
152   memcpy(dstvtable, srcvtable, sizeof(intptr_t) * n);
153 }
154 
155 // To determine the size of the vtable for each type, we use the following
156 // trick by declaring 2 subclasses:
157 //
158 //   class CppVtableTesterA: public InstanceKlass {virtual int   last_virtual_method() {return 1;}    };
159 //   class CppVtableTesterB: public InstanceKlass {virtual void* last_virtual_method() {return nullptr}; };
160 //
161 // CppVtableTesterA and CppVtableTesterB's vtables have the following properties:
162 // - Their size (N+1) is exactly one more than the size of InstanceKlass's vtable (N)
163 // - The first N entries have are exactly the same as in InstanceKlass's vtable.
164 // - Their last entry is different.
165 //
166 // So to determine the value of N, we just walk CppVtableTesterA and CppVtableTesterB's tables
167 // and find the first entry that's different.
168 //
169 // This works on all C++ compilers supported by Oracle, but you may need to tweak it for more
170 // esoteric compilers.
171 
172 template <class T> class CppVtableTesterB: public T {
173 public:
174   virtual int last_virtual_method() {return 1;}
175 };
176 
177 template <class T> class CppVtableTesterA : public T {
178 public:
179   virtual void* last_virtual_method() {
180     // Make this different than CppVtableTesterB::last_virtual_method so the C++
181     // compiler/linker won't alias the two functions.
182     return nullptr;
183   }
184 };
185 
186 template <class T>
187 int CppVtableCloner<T>::get_vtable_length(const char* name) {
188   CppVtableTesterA<T> a;
189   CppVtableTesterB<T> b;
190 
191   intptr_t* avtable = vtable_of(&a);
192   intptr_t* bvtable = vtable_of(&b);
193 
194   // Start at slot 1, because slot 0 may be RTTI (on Solaris/Sparc)
195   int vtable_len = 1;
196   for (; ; vtable_len++) {
197     if (avtable[vtable_len] != bvtable[vtable_len]) {
198       break;
199     }
200   }
201   log_debug(aot, vtables)("Found   %3d vtable entries for %s", vtable_len, name);
202 
203   return vtable_len;
204 }
205 
206 #define ALLOCATE_AND_INITIALIZE_VTABLE(c) \
207   _index[c##_Kind] = CppVtableCloner<c>::allocate_and_initialize(#c); \
208   ArchivePtrMarker::mark_pointer(&_index[c##_Kind]);
209 
210 #define INITIALIZE_VTABLE(c) \
211   CppVtableCloner<c>::initialize(#c, _index[c##_Kind]);
212 
213 #define INIT_ORIG_CPP_VTPTRS(c) \
214   CppVtableCloner<c>::init_orig_cpp_vtptr(c##_Kind);
215 
216 #define DECLARE_CLONED_VTABLE_KIND(c) c ## _Kind,
217 
218 enum ClonedVtableKind {
219   // E.g., ConstantPool_Kind == 0, InstanceKlass_Kind == 1, etc.
220   CPP_VTABLE_TYPES_DO(DECLARE_CLONED_VTABLE_KIND)
221   _num_cloned_vtable_kinds
222 };
223 
224 // _orig_cpp_vtptrs and _archived_cpp_vtptrs are used for type checking in
225 // CppVtables::get_archived_vtable().
226 //
227 // _orig_cpp_vtptrs is a map of all the original vtptrs. E.g., for
228 //     ConstantPool *cp = new (...) ConstantPool(...) ; // a dynamically allocated constant pool
229 // the following holds true:
230 //     _orig_cpp_vtptrs[ConstantPool_Kind] == ((intptr_t**)cp)[0]
231 //
232 // _archived_cpp_vtptrs is a map of all the vptprs used by classes in a preimage. E.g., for
233 //    InstanceKlass* k = a class loaded from the preimage;
234 //    ConstantPool* cp = k->constants();
235 // the following holds true:
236 //     _archived_cpp_vtptrs[ConstantPool_Kind] == ((intptr_t**)cp)[0]
237 static bool _orig_cpp_vtptrs_inited = false;
238 static intptr_t* _orig_cpp_vtptrs[_num_cloned_vtable_kinds];
239 static intptr_t* _archived_cpp_vtptrs[_num_cloned_vtable_kinds];
240 
241 template <class T>
242 void CppVtableCloner<T>::init_orig_cpp_vtptr(int kind) {
243   assert(kind < _num_cloned_vtable_kinds, "sanity");
244   T tmp; // Allocate temporary dummy metadata object to get to the original vtable.
245   intptr_t* srcvtable = vtable_of(&tmp);
246   _orig_cpp_vtptrs[kind] = srcvtable;
247 }
248 
249 // This is the index of all the cloned vtables. E.g., for
250 //     ConstantPool* cp = ....; // an archived constant pool
251 //     InstanceKlass* ik = ....;// an archived class
252 // the following holds true:
253 //     _index[ConstantPool_Kind]->cloned_vtable()  == ((intptr_t**)cp)[0]
254 //     _index[InstanceKlass_Kind]->cloned_vtable() == ((intptr_t**)ik)[0]
255 static CppVtableInfo* _index[_num_cloned_vtable_kinds];
256 
257 // This marks the location in the archive where _index[0] is stored. This location
258 // will be stored as FileMapHeader::_cloned_vtables_offset into the archive header.
259 // Serviceability Agent uses this information to determine the vtables of
260 // archived Metadata objects.
261 char* CppVtables::_vtables_serialized_base = nullptr;
262 
263 void CppVtables::dumptime_init(ArchiveBuilder* builder) {
264   assert(CDSConfig::is_dumping_static_archive(), "cpp tables are only dumped into static archive");
265 
266   if (CDSConfig::is_dumping_final_static_archive()) {
267     // When dumping final archive, _index[kind] at this point is in the preimage.
268     // Remember these vtable pointers in _archived_cpp_vtptrs, as _index[kind] will now be rewritten
269     // to point to the runtime vtable data.
270     for (int i = 0; i < _num_cloned_vtable_kinds; i++) {
271       assert(_index[i] != nullptr, "must have been restored by CppVtables::serialize()");
272       _archived_cpp_vtptrs[i] = _index[i]->cloned_vtable();
273     }
274   } else {
275     memset(_archived_cpp_vtptrs, 0, sizeof(_archived_cpp_vtptrs));
276   }
277 
278   CPP_VTABLE_TYPES_DO(ALLOCATE_AND_INITIALIZE_VTABLE);
279 
280   size_t cpp_tables_size = builder->rw_region()->top() - builder->rw_region()->base();
281   builder->alloc_stats()->record_cpp_vtables((int)cpp_tables_size);
282 }
283 
284 void CppVtables::serialize(SerializeClosure* soc) {
285   if (!soc->reading()) {
286     _vtables_serialized_base = (char*)ArchiveBuilder::current()->buffer_top();
287   }
288   for (int i = 0; i < _num_cloned_vtable_kinds; i++) {
289     soc->do_ptr(&_index[i]);
290   }
291   if (soc->reading()) {
292     CPP_VTABLE_TYPES_DO(INITIALIZE_VTABLE);
293   }
294 }
295 
296 intptr_t* CppVtables::get_archived_vtable(MetaspaceClosureType type, address obj) {
297   if (!_orig_cpp_vtptrs_inited) {
298     CPP_VTABLE_TYPES_DO(INIT_ORIG_CPP_VTPTRS);
299     _orig_cpp_vtptrs_inited = true;
300   }
301 
302   assert(CDSConfig::is_dumping_archive(), "sanity");
303   int kind = -1;
304   switch (type) {
305   case MetaspaceClosureType::SymbolType:
306   case MetaspaceClosureType::TypeArrayU1Type:
307   case MetaspaceClosureType::TypeArrayU2Type:
308   case MetaspaceClosureType::TypeArrayU4Type:
309   case MetaspaceClosureType::TypeArrayU8Type:
310   case MetaspaceClosureType::TypeArrayOtherType:
311   case MetaspaceClosureType::CArrayType:
312   case MetaspaceClosureType::ConstMethodType:
313   case MetaspaceClosureType::ConstantPoolCacheType:
314   case MetaspaceClosureType::AnnotationsType:
315   case MetaspaceClosureType::ModuleEntryType:
316   case MetaspaceClosureType::PackageEntryType:
317   case MetaspaceClosureType::RecordComponentType:
318   case MetaspaceClosureType::AdapterHandlerEntryType:
319   case MetaspaceClosureType::AdapterFingerPrintType:
320   PRODUCT_ONLY(case MetaspaceClosureType::GrowableArrayType:)
321     // These have no vtables.
322     break;
323   default:
324     for (kind = 0; kind < _num_cloned_vtable_kinds; kind ++) {
325       if (vtable_of((Metadata*)obj) == _orig_cpp_vtptrs[kind] ||
326           vtable_of((Metadata*)obj) == _archived_cpp_vtptrs[kind]) {
327         break;
328       }
329     }
330     if (kind >= _num_cloned_vtable_kinds) {
331       fatal("Cannot find C++ vtable for " INTPTR_FORMAT " -- you probably added"
332             " a new subtype of Klass or MetaData without updating CPP_VTABLE_TYPES_DO or the cases in this 'switch' statement",
333             p2i(obj));
334     }
335   }
336 
337   if (kind >= 0) {
338     assert(kind < _num_cloned_vtable_kinds, "must be");
339     return _index[kind]->cloned_vtable();
340   } else {
341     return nullptr;
342   }
343 }
344 
345 void CppVtables::zero_archived_vtables() {
346   assert(CDSConfig::is_dumping_static_archive(), "cpp tables are only dumped into static archive");
347   for (int kind = 0; kind < _num_cloned_vtable_kinds; kind ++) {
348     _index[kind]->zero();
349   }
350 }
351 
352 bool CppVtables::is_valid_shared_method(const Method* m) {
353   assert(AOTMetaspace::in_aot_cache(m), "must be");
354   return vtable_of(m) == _index[Method_Kind]->cloned_vtable() ||
355          vtable_of(m) == _archived_cpp_vtptrs[Method_Kind];
356 }