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