< prev index next >

src/hotspot/share/cds/cppVtables.cpp

Print this page

 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 CppVtableInfo** CppVtables::_index = nullptr;
217 
218 char* CppVtables::dumptime_init(ArchiveBuilder* builder) {
219   assert(CDSConfig::is_dumping_static_archive(), "cpp tables are only dumped into static archive");
220   size_t vtptrs_bytes = _num_cloned_vtable_kinds * sizeof(CppVtableInfo*);
221   _index = (CppVtableInfo**)builder->rw_region()->allocate(vtptrs_bytes);
222 
223   CPP_VTABLE_TYPES_DO(ALLOCATE_AND_INITIALIZE_VTABLE);
224 






225   size_t cpp_tables_size = builder->rw_region()->top() - builder->rw_region()->base();
226   builder->alloc_stats()->record_cpp_vtables((int)cpp_tables_size);
227 
228   return (char*)_index;
229 }
230 
231 void CppVtables::serialize(SerializeClosure* soc) {
232   soc->do_ptr(&_index);
233   if (soc->reading()) {
234     CPP_VTABLE_TYPES_DO(INITIALIZE_VTABLE);
235   }








236 }
237 
238 intptr_t* CppVtables::get_archived_vtable(MetaspaceObj::Type msotype, address obj) {
239   if (!_orig_cpp_vtptrs_inited) {
240     CPP_VTABLE_TYPES_DO(INIT_ORIG_CPP_VTPTRS);
241     _orig_cpp_vtptrs_inited = true;
242   }
243 
244   assert(CDSConfig::is_dumping_archive(), "sanity");
245   int kind = -1;
246   switch (msotype) {
247   case MetaspaceObj::SymbolType:
248   case MetaspaceObj::TypeArrayU1Type:
249   case MetaspaceObj::TypeArrayU2Type:
250   case MetaspaceObj::TypeArrayU4Type:
251   case MetaspaceObj::TypeArrayU8Type:
252   case MetaspaceObj::TypeArrayOtherType:
253   case MetaspaceObj::ConstMethodType:
254   case MetaspaceObj::ConstantPoolCacheType:
255   case MetaspaceObj::AnnotationsType:
256   case MetaspaceObj::MethodCountersType:
257   case MetaspaceObj::SharedClassPathEntryType:
258   case MetaspaceObj::RecordComponentType:
259     // These have no vtables.
260     break;
261   case MetaspaceObj::MethodDataType:
262     // We don't archive MethodData <-- should have been removed in removed_unsharable_info
263     ShouldNotReachHere();
264     break;
265   default:
266     for (kind = 0; kind < _num_cloned_vtable_kinds; kind ++) {
267       if (vtable_of((Metadata*)obj) == _orig_cpp_vtptrs[kind]) {

268         break;
269       }
270     }
271     if (kind >= _num_cloned_vtable_kinds) {
272       fatal("Cannot find C++ vtable for " INTPTR_FORMAT " -- you probably added"
273             " a new subtype of Klass or MetaData without updating CPP_VTABLE_TYPES_DO or the cases in this 'switch' statement",
274             p2i(obj));
275     }
276   }
277 
278   if (kind >= 0) {
279     assert(kind < _num_cloned_vtable_kinds, "must be");
280     return _index[kind]->cloned_vtable();
281   } else {
282     return nullptr;
283   }
284 }
285 
286 void CppVtables::zero_archived_vtables() {
287   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 CppVtableInfo** CppVtables::_index = nullptr;
225 
226 char* CppVtables::dumptime_init(ArchiveBuilder* builder) {
227   assert(CDSConfig::is_dumping_static_archive(), "cpp tables are only dumped into static archive");
228   size_t vtptrs_bytes = _num_cloned_vtable_kinds * sizeof(CppVtableInfo*);
229   _index = (CppVtableInfo**)builder->rw_region()->allocate(vtptrs_bytes);
230 
231   CPP_VTABLE_TYPES_DO(ALLOCATE_AND_INITIALIZE_VTABLE);
232 
233   if (!CDSConfig::is_dumping_final_static_archive()) {
234     for (int kind = 0; kind < _num_cloned_vtable_kinds; kind++) {
235       _archived_cpp_vtptrs[kind] = _index[kind]->cloned_vtable();
236     }
237   }
238 
239   size_t cpp_tables_size = builder->rw_region()->top() - builder->rw_region()->base();
240   builder->alloc_stats()->record_cpp_vtables((int)cpp_tables_size);
241 
242   return (char*)_index;
243 }
244 
245 void CppVtables::serialize(SerializeClosure* soc) {
246   soc->do_ptr(&_index);
247   if (soc->reading()) {
248     CPP_VTABLE_TYPES_DO(INITIALIZE_VTABLE);
249   }
250 
251   if (soc->writing() && CDSConfig::is_dumping_final_static_archive()) {
252     memset(_archived_cpp_vtptrs, 0, sizeof(_archived_cpp_vtptrs));
253   }
254 
255   for (int kind = 0; kind < _num_cloned_vtable_kinds; kind++) {
256     soc->do_ptr(&_archived_cpp_vtptrs[kind]);
257   }
258 }
259 
260 intptr_t* CppVtables::get_archived_vtable(MetaspaceObj::Type msotype, address obj) {
261   if (!_orig_cpp_vtptrs_inited) {
262     CPP_VTABLE_TYPES_DO(INIT_ORIG_CPP_VTPTRS);
263     _orig_cpp_vtptrs_inited = true;
264   }
265 
266   assert(CDSConfig::is_dumping_archive(), "sanity");
267   int kind = -1;
268   switch (msotype) {
269   case MetaspaceObj::SymbolType:
270   case MetaspaceObj::TypeArrayU1Type:
271   case MetaspaceObj::TypeArrayU2Type:
272   case MetaspaceObj::TypeArrayU4Type:
273   case MetaspaceObj::TypeArrayU8Type:
274   case MetaspaceObj::TypeArrayOtherType:
275   case MetaspaceObj::ConstMethodType:
276   case MetaspaceObj::ConstantPoolCacheType:
277   case MetaspaceObj::AnnotationsType:

278   case MetaspaceObj::SharedClassPathEntryType:
279   case MetaspaceObj::RecordComponentType:
280     // These have no vtables.
281     break;




282   default:
283     for (kind = 0; kind < _num_cloned_vtable_kinds; kind ++) {
284       if (vtable_of((Metadata*)obj) == _orig_cpp_vtptrs[kind] ||
285           vtable_of((Metadata*)obj) == _archived_cpp_vtptrs[kind]) {
286         break;
287       }
288     }
289     if (kind >= _num_cloned_vtable_kinds) {
290       fatal("Cannot find C++ vtable for " INTPTR_FORMAT " -- you probably added"
291             " a new subtype of Klass or MetaData without updating CPP_VTABLE_TYPES_DO or the cases in this 'switch' statement",
292             p2i(obj));
293     }
294   }
295 
296   if (kind >= 0) {
297     assert(kind < _num_cloned_vtable_kinds, "must be");
298     return _index[kind]->cloned_vtable();
299   } else {
300     return nullptr;
301   }
302 }
303 
304 void CppVtables::zero_archived_vtables() {
305   assert(CDSConfig::is_dumping_static_archive(), "cpp tables are only dumped into static archive");
< prev index next >