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];
70 public:
71 static int num_slots(int vtable_size) {
72 return 1 + vtable_size; // Need to add the space occupied by _vtable_size;
73 }
74 int vtable_size() { return int(uintx(_vtable_size)); }
75 void set_vtable_size(int n) { _vtable_size = intptr_t(n); }
76 intptr_t* cloned_vtable() { return &_cloned_vtable[0]; }
77 void zero() { memset(_cloned_vtable, 0, sizeof(intptr_t) * vtable_size()); }
78 // Returns the address of the next CppVtableInfo that can be placed immediately after this CppVtableInfo
79 static size_t byte_size(int vtable_size) {
80 CppVtableInfo i;
81 return pointer_delta(&i._cloned_vtable[vtable_size], &i, sizeof(u1));
82 }
83 };
84
85 static inline intptr_t* vtable_of(const Metadata* m) {
179 ArchivePtrMarker::mark_pointer(&_index[c##_Kind]);
180
181 #define INITIALIZE_VTABLE(c) \
182 CppVtableCloner<c>::initialize(#c, _index[c##_Kind]);
183
184 #define INIT_ORIG_CPP_VTPTRS(c) \
185 CppVtableCloner<c>::init_orig_cpp_vtptr(c##_Kind);
186
187 #define DECLARE_CLONED_VTABLE_KIND(c) c ## _Kind,
188
189 enum ClonedVtableKind {
190 // E.g., ConstantPool_Kind == 0, InstanceKlass_Kind == 1, etc.
191 CPP_VTABLE_TYPES_DO(DECLARE_CLONED_VTABLE_KIND)
192 _num_cloned_vtable_kinds
193 };
194
195 // This is a map of all the original vtptrs. E.g., for
196 // ConstantPool *cp = new (...) ConstantPool(...) ; // a dynamically allocated constant pool
197 // the following holds true:
198 // _orig_cpp_vtptrs[ConstantPool_Kind] == ((intptr_t**)cp)[0]
199 static intptr_t* _orig_cpp_vtptrs[_num_cloned_vtable_kinds];
200 static bool _orig_cpp_vtptrs_inited = false;
201
202 template <class T>
203 void CppVtableCloner<T>::init_orig_cpp_vtptr(int kind) {
204 assert(kind < _num_cloned_vtable_kinds, "sanity");
205 T tmp; // Allocate temporary dummy metadata object to get to the original vtable.
206 intptr_t* srcvtable = vtable_of(&tmp);
207 _orig_cpp_vtptrs[kind] = srcvtable;
208 }
209
210 // This is the index of all the cloned vtables. E.g., for
211 // ConstantPool* cp = ....; // an archived constant pool
212 // InstanceKlass* ik = ....;// an archived class
213 // the following holds true:
214 // _index[ConstantPool_Kind]->cloned_vtable() == ((intptr_t**)cp)[0]
215 // _index[InstanceKlass_Kind]->cloned_vtable() == ((intptr_t**)ik)[0]
216 static CppVtableInfo* _index[_num_cloned_vtable_kinds];
217
218 // Vtables are all fixed offsets from ArchiveBuilder::current()->mapped_base()
219 // E.g. ConstantPool is at offset 0x58. We can archive these offsets in the
220 // RO region and use them to alculate their location at runtime without storing
221 // the pointers in the RW region
222 char* CppVtables::_vtables_serialized_base = nullptr;
223
224 void CppVtables::dumptime_init(ArchiveBuilder* builder) {
225 assert(CDSConfig::is_dumping_static_archive(), "cpp tables are only dumped into static archive");
226
227 CPP_VTABLE_TYPES_DO(ALLOCATE_AND_INITIALIZE_VTABLE);
228
229 size_t cpp_tables_size = builder->rw_region()->top() - builder->rw_region()->base();
230 builder->alloc_stats()->record_cpp_vtables((int)cpp_tables_size);
231 }
232
233 void CppVtables::serialize(SerializeClosure* soc) {
234 if (!soc->reading()) {
235 _vtables_serialized_base = (char*)ArchiveBuilder::current()->buffer_top();
236 }
237 for (int i = 0; i < _num_cloned_vtable_kinds; i++) {
238 soc->do_ptr(&_index[i]);
239 }
240 if (soc->reading()) {
241 CPP_VTABLE_TYPES_DO(INITIALIZE_VTABLE);
242 }
243 }
244
245 intptr_t* CppVtables::get_archived_vtable(MetaspaceObj::Type msotype, address obj) {
246 if (!_orig_cpp_vtptrs_inited) {
247 CPP_VTABLE_TYPES_DO(INIT_ORIG_CPP_VTPTRS);
248 _orig_cpp_vtptrs_inited = true;
249 }
250
251 assert(CDSConfig::is_dumping_archive(), "sanity");
252 int kind = -1;
253 switch (msotype) {
254 case MetaspaceObj::SymbolType:
255 case MetaspaceObj::TypeArrayU1Type:
256 case MetaspaceObj::TypeArrayU2Type:
257 case MetaspaceObj::TypeArrayU4Type:
258 case MetaspaceObj::TypeArrayU8Type:
259 case MetaspaceObj::TypeArrayOtherType:
260 case MetaspaceObj::ConstMethodType:
261 case MetaspaceObj::ConstantPoolCacheType:
262 case MetaspaceObj::AnnotationsType:
263 case MetaspaceObj::MethodCountersType:
264 case MetaspaceObj::SharedClassPathEntryType:
265 case MetaspaceObj::RecordComponentType:
266 // These have no vtables.
267 break;
268 case MetaspaceObj::MethodDataType:
269 // We don't archive MethodData <-- should have been removed in removed_unsharable_info
270 ShouldNotReachHere();
271 break;
272 default:
273 for (kind = 0; kind < _num_cloned_vtable_kinds; kind ++) {
274 if (vtable_of((Metadata*)obj) == _orig_cpp_vtptrs[kind]) {
275 break;
276 }
277 }
278 if (kind >= _num_cloned_vtable_kinds) {
279 fatal("Cannot find C++ vtable for " INTPTR_FORMAT " -- you probably added"
280 " a new subtype of Klass or MetaData without updating CPP_VTABLE_TYPES_DO or the cases in this 'switch' statement",
281 p2i(obj));
282 }
283 }
284
285 if (kind >= 0) {
286 assert(kind < _num_cloned_vtable_kinds, "must be");
287 return _index[kind]->cloned_vtable();
288 } else {
289 return nullptr;
290 }
291 }
292
293 void CppVtables::zero_archived_vtables() {
294 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];
77 public:
78 static int num_slots(int vtable_size) {
79 return 1 + vtable_size; // Need to add the space occupied by _vtable_size;
80 }
81 int vtable_size() { return int(uintx(_vtable_size)); }
82 void set_vtable_size(int n) { _vtable_size = intptr_t(n); }
83 intptr_t* cloned_vtable() { return &_cloned_vtable[0]; }
84 void zero() { memset(_cloned_vtable, 0, sizeof(intptr_t) * vtable_size()); }
85 // Returns the address of the next CppVtableInfo that can be placed immediately after this CppVtableInfo
86 static size_t byte_size(int vtable_size) {
87 CppVtableInfo i;
88 return pointer_delta(&i._cloned_vtable[vtable_size], &i, sizeof(u1));
89 }
90 };
91
92 static inline intptr_t* vtable_of(const Metadata* m) {
186 ArchivePtrMarker::mark_pointer(&_index[c##_Kind]);
187
188 #define INITIALIZE_VTABLE(c) \
189 CppVtableCloner<c>::initialize(#c, _index[c##_Kind]);
190
191 #define INIT_ORIG_CPP_VTPTRS(c) \
192 CppVtableCloner<c>::init_orig_cpp_vtptr(c##_Kind);
193
194 #define DECLARE_CLONED_VTABLE_KIND(c) c ## _Kind,
195
196 enum ClonedVtableKind {
197 // E.g., ConstantPool_Kind == 0, InstanceKlass_Kind == 1, etc.
198 CPP_VTABLE_TYPES_DO(DECLARE_CLONED_VTABLE_KIND)
199 _num_cloned_vtable_kinds
200 };
201
202 // This is a map of all the original vtptrs. E.g., for
203 // ConstantPool *cp = new (...) ConstantPool(...) ; // a dynamically allocated constant pool
204 // the following holds true:
205 // _orig_cpp_vtptrs[ConstantPool_Kind] == ((intptr_t**)cp)[0]
206 static intptr_t* _orig_cpp_vtptrs[_num_cloned_vtable_kinds]; // vtptrs set by the C++ compiler
207 static intptr_t* _archived_cpp_vtptrs[_num_cloned_vtable_kinds]; // vtptrs used in the static archive
208 static bool _orig_cpp_vtptrs_inited = false;
209
210 template <class T>
211 void CppVtableCloner<T>::init_orig_cpp_vtptr(int kind) {
212 assert(kind < _num_cloned_vtable_kinds, "sanity");
213 T tmp; // Allocate temporary dummy metadata object to get to the original vtable.
214 intptr_t* srcvtable = vtable_of(&tmp);
215 _orig_cpp_vtptrs[kind] = srcvtable;
216 }
217
218 // This is the index of all the cloned vtables. E.g., for
219 // ConstantPool* cp = ....; // an archived constant pool
220 // InstanceKlass* ik = ....;// an archived class
221 // the following holds true:
222 // _index[ConstantPool_Kind]->cloned_vtable() == ((intptr_t**)cp)[0]
223 // _index[InstanceKlass_Kind]->cloned_vtable() == ((intptr_t**)ik)[0]
224 static CppVtableInfo* _index[_num_cloned_vtable_kinds];
225
226 // Vtables are all fixed offsets from ArchiveBuilder::current()->mapped_base()
227 // E.g. ConstantPool is at offset 0x58. We can archive these offsets in the
228 // RO region and use them to alculate their location at runtime without storing
229 // the pointers in the RW region
230 char* CppVtables::_vtables_serialized_base = nullptr;
231
232 void CppVtables::dumptime_init(ArchiveBuilder* builder) {
233 assert(CDSConfig::is_dumping_static_archive(), "cpp tables are only dumped into static archive");
234
235 CPP_VTABLE_TYPES_DO(ALLOCATE_AND_INITIALIZE_VTABLE);
236
237 if (!CDSConfig::is_dumping_final_static_archive()) {
238 for (int kind = 0; kind < _num_cloned_vtable_kinds; kind++) {
239 _archived_cpp_vtptrs[kind] = _index[kind]->cloned_vtable();
240 }
241 }
242
243 size_t cpp_tables_size = builder->rw_region()->top() - builder->rw_region()->base();
244 builder->alloc_stats()->record_cpp_vtables((int)cpp_tables_size);
245 }
246
247 void CppVtables::serialize(SerializeClosure* soc) {
248 if (!soc->reading()) {
249 _vtables_serialized_base = (char*)ArchiveBuilder::current()->buffer_top();
250 }
251 for (int i = 0; i < _num_cloned_vtable_kinds; i++) {
252 soc->do_ptr(&_index[i]);
253 }
254 if (soc->reading()) {
255 CPP_VTABLE_TYPES_DO(INITIALIZE_VTABLE);
256 }
257
258 if (soc->writing() && CDSConfig::is_dumping_final_static_archive()) {
259 memset(_archived_cpp_vtptrs, 0, sizeof(_archived_cpp_vtptrs));
260 }
261
262 for (int kind = 0; kind < _num_cloned_vtable_kinds; kind++) {
263 soc->do_ptr(&_archived_cpp_vtptrs[kind]);
264 }
265 }
266
267 intptr_t* CppVtables::get_archived_vtable(MetaspaceObj::Type msotype, address obj) {
268 if (!_orig_cpp_vtptrs_inited) {
269 CPP_VTABLE_TYPES_DO(INIT_ORIG_CPP_VTPTRS);
270 _orig_cpp_vtptrs_inited = true;
271 }
272
273 assert(CDSConfig::is_dumping_archive(), "sanity");
274 int kind = -1;
275 switch (msotype) {
276 case MetaspaceObj::SymbolType:
277 case MetaspaceObj::TypeArrayU1Type:
278 case MetaspaceObj::TypeArrayU2Type:
279 case MetaspaceObj::TypeArrayU4Type:
280 case MetaspaceObj::TypeArrayU8Type:
281 case MetaspaceObj::TypeArrayOtherType:
282 case MetaspaceObj::ConstMethodType:
283 case MetaspaceObj::ConstantPoolCacheType:
284 case MetaspaceObj::AnnotationsType:
285 case MetaspaceObj::SharedClassPathEntryType:
286 case MetaspaceObj::RecordComponentType:
287 // These have no vtables.
288 break;
289 default:
290 for (kind = 0; kind < _num_cloned_vtable_kinds; kind ++) {
291 if (vtable_of((Metadata*)obj) == _orig_cpp_vtptrs[kind] ||
292 vtable_of((Metadata*)obj) == _archived_cpp_vtptrs[kind]) {
293 break;
294 }
295 }
296 if (kind >= _num_cloned_vtable_kinds) {
297 fatal("Cannot find C++ vtable for " INTPTR_FORMAT " -- you probably added"
298 " a new subtype of Klass or MetaData without updating CPP_VTABLE_TYPES_DO or the cases in this 'switch' statement",
299 p2i(obj));
300 }
301 }
302
303 if (kind >= 0) {
304 assert(kind < _num_cloned_vtable_kinds, "must be");
305 return _index[kind]->cloned_vtable();
306 } else {
307 return nullptr;
308 }
309 }
310
311 void CppVtables::zero_archived_vtables() {
312 assert(CDSConfig::is_dumping_static_archive(), "cpp tables are only dumped into static archive");
|