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 }
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");
|
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/methodCounters.hpp"
37 #include "oops/methodData.hpp"
38 #include "oops/trainingData.hpp"
39 #include "oops/objArrayKlass.hpp"
40 #include "oops/typeArrayKlass.hpp"
41 #include "runtime/arguments.hpp"
42 #include "utilities/globalDefinitions.hpp"
43
44 // Objects of the Metadata types (such as Klass and ConstantPool) have C++ vtables.
45 // (In GCC this is the field <Type>::_vptr, i.e., first word in the object.)
46 //
47 // Addresses of the vtables and the methods may be different across JVM runs,
48 // if libjvm.so is dynamically loaded at a different base address.
49 //
50 // To ensure that the Metadata objects in the CDS archive always have the correct vtable:
51 //
52 // + at dump time: we redirect the _vptr to point to our own vtables inside
53 // the CDS image
54 // + at run time: we clone the actual contents of the vtables from libjvm.so
55 // into our own tables.
56
57 // Currently, the archive contains ONLY the following types of objects that have C++ vtables.
58 #define CPP_VTABLE_TYPES_DO(f) \
59 f(ConstantPool) \
60 f(InstanceKlass) \
61 f(InstanceClassLoaderKlass) \
62 f(InstanceMirrorKlass) \
63 f(InstanceRefKlass) \
64 f(InstanceStackChunkKlass) \
65 f(Method) \
66 f(MethodData) \
67 f(MethodCounters) \
68 f(ObjArrayKlass) \
69 f(TypeArrayKlass) \
70 f(KlassTrainingData) \
71 f(MethodTrainingData) \
72 f(CompileTrainingData)
73
74 class CppVtableInfo {
75 intptr_t _vtable_size;
76 intptr_t _cloned_vtable[1]; // Pseudo flexible array member.
77 static size_t cloned_vtable_offset() { return offset_of(CppVtableInfo, _cloned_vtable); }
78 public:
79 int vtable_size() { return int(uintx(_vtable_size)); }
80 void set_vtable_size(int n) { _vtable_size = intptr_t(n); }
81 // Using _cloned_vtable[i] for i > 0 causes undefined behavior. We use address calculation instead.
82 intptr_t* cloned_vtable() { return (intptr_t*)((char*)this + cloned_vtable_offset()); }
83 void zero() { memset(cloned_vtable(), 0, sizeof(intptr_t) * vtable_size()); }
84 // Returns the address of the next CppVtableInfo that can be placed immediately after this CppVtableInfo
85 static size_t byte_size(int vtable_size) {
86 return cloned_vtable_offset() + (sizeof(intptr_t) * vtable_size);
87 }
88 };
89
90 static inline intptr_t* vtable_of(const Metadata* m) {
91 return *((intptr_t**)m);
92 }
184 ArchivePtrMarker::mark_pointer(&_index[c##_Kind]);
185
186 #define INITIALIZE_VTABLE(c) \
187 CppVtableCloner<c>::initialize(#c, _index[c##_Kind]);
188
189 #define INIT_ORIG_CPP_VTPTRS(c) \
190 CppVtableCloner<c>::init_orig_cpp_vtptr(c##_Kind);
191
192 #define DECLARE_CLONED_VTABLE_KIND(c) c ## _Kind,
193
194 enum ClonedVtableKind {
195 // E.g., ConstantPool_Kind == 0, InstanceKlass_Kind == 1, etc.
196 CPP_VTABLE_TYPES_DO(DECLARE_CLONED_VTABLE_KIND)
197 _num_cloned_vtable_kinds
198 };
199
200 // This is a map of all the original vtptrs. E.g., for
201 // ConstantPool *cp = new (...) ConstantPool(...) ; // a dynamically allocated constant pool
202 // the following holds true:
203 // _orig_cpp_vtptrs[ConstantPool_Kind] == ((intptr_t**)cp)[0]
204 static intptr_t* _orig_cpp_vtptrs[_num_cloned_vtable_kinds]; // vtptrs set by the C++ compiler
205 static intptr_t* _archived_cpp_vtptrs[_num_cloned_vtable_kinds]; // vtptrs used in the static archive
206 static bool _orig_cpp_vtptrs_inited = false;
207
208 template <class T>
209 void CppVtableCloner<T>::init_orig_cpp_vtptr(int kind) {
210 assert(kind < _num_cloned_vtable_kinds, "sanity");
211 T tmp; // Allocate temporary dummy metadata object to get to the original vtable.
212 intptr_t* srcvtable = vtable_of(&tmp);
213 _orig_cpp_vtptrs[kind] = srcvtable;
214 }
215
216 // This is the index of all the cloned vtables. E.g., for
217 // ConstantPool* cp = ....; // an archived constant pool
218 // InstanceKlass* ik = ....;// an archived class
219 // the following holds true:
220 // _index[ConstantPool_Kind]->cloned_vtable() == ((intptr_t**)cp)[0]
221 // _index[InstanceKlass_Kind]->cloned_vtable() == ((intptr_t**)ik)[0]
222 static CppVtableInfo* _index[_num_cloned_vtable_kinds];
223
224 // Vtables are all fixed offsets from ArchiveBuilder::current()->mapped_base()
225 // E.g. ConstantPool is at offset 0x58. We can archive these offsets in the
226 // RO region and use them to alculate their location at runtime without storing
227 // the pointers in the RW region
228 char* CppVtables::_vtables_serialized_base = nullptr;
229
230 void CppVtables::dumptime_init(ArchiveBuilder* builder) {
231 assert(CDSConfig::is_dumping_static_archive(), "cpp tables are only dumped into static archive");
232
233 CPP_VTABLE_TYPES_DO(ALLOCATE_AND_INITIALIZE_VTABLE);
234
235 if (!CDSConfig::is_dumping_final_static_archive()) {
236 for (int kind = 0; kind < _num_cloned_vtable_kinds; kind++) {
237 _archived_cpp_vtptrs[kind] = _index[kind]->cloned_vtable();
238 }
239 }
240
241 size_t cpp_tables_size = builder->rw_region()->top() - builder->rw_region()->base();
242 builder->alloc_stats()->record_cpp_vtables((int)cpp_tables_size);
243 }
244
245 void CppVtables::serialize(SerializeClosure* soc) {
246 if (!soc->reading()) {
247 _vtables_serialized_base = (char*)ArchiveBuilder::current()->buffer_top();
248 }
249 for (int i = 0; i < _num_cloned_vtable_kinds; i++) {
250 soc->do_ptr(&_index[i]);
251 }
252 if (soc->reading()) {
253 CPP_VTABLE_TYPES_DO(INITIALIZE_VTABLE);
254 }
255
256 if (soc->writing() && CDSConfig::is_dumping_final_static_archive()) {
257 memset(_archived_cpp_vtptrs, 0, sizeof(_archived_cpp_vtptrs));
258 }
259
260 for (int kind = 0; kind < _num_cloned_vtable_kinds; kind++) {
261 soc->do_ptr(&_archived_cpp_vtptrs[kind]);
262 }
263 }
264
265 intptr_t* CppVtables::get_archived_vtable(MetaspaceObj::Type msotype, address obj) {
266 if (!_orig_cpp_vtptrs_inited) {
267 CPP_VTABLE_TYPES_DO(INIT_ORIG_CPP_VTPTRS);
268 _orig_cpp_vtptrs_inited = true;
269 }
270
271 assert(CDSConfig::is_dumping_archive(), "sanity");
272 int kind = -1;
273 switch (msotype) {
274 case MetaspaceObj::SymbolType:
275 case MetaspaceObj::TypeArrayU1Type:
276 case MetaspaceObj::TypeArrayU2Type:
277 case MetaspaceObj::TypeArrayU4Type:
278 case MetaspaceObj::TypeArrayU8Type:
279 case MetaspaceObj::TypeArrayOtherType:
280 case MetaspaceObj::ConstMethodType:
281 case MetaspaceObj::ConstantPoolCacheType:
282 case MetaspaceObj::AnnotationsType:
283 case MetaspaceObj::SharedClassPathEntryType:
284 case MetaspaceObj::RecordComponentType:
285 // These have no vtables.
286 break;
287 default:
288 for (kind = 0; kind < _num_cloned_vtable_kinds; kind ++) {
289 if (vtable_of((Metadata*)obj) == _orig_cpp_vtptrs[kind] ||
290 vtable_of((Metadata*)obj) == _archived_cpp_vtptrs[kind]) {
291 break;
292 }
293 }
294 if (kind >= _num_cloned_vtable_kinds) {
295 fatal("Cannot find C++ vtable for " INTPTR_FORMAT " -- you probably added"
296 " a new subtype of Klass or MetaData without updating CPP_VTABLE_TYPES_DO or the cases in this 'switch' statement",
297 p2i(obj));
298 }
299 }
300
301 if (kind >= 0) {
302 assert(kind < _num_cloned_vtable_kinds, "must be");
303 return _index[kind]->cloned_vtable();
304 } else {
305 return nullptr;
306 }
307 }
308
309 void CppVtables::zero_archived_vtables() {
310 assert(CDSConfig::is_dumping_static_archive(), "cpp tables are only dumped into static archive");
|