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