< prev index next >

src/hotspot/share/cds/cppVtables.cpp

Print this page

 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/instanceClassLoaderKlass.hpp"
 32 #include "oops/instanceMirrorKlass.hpp"
 33 #include "oops/instanceRefKlass.hpp"
 34 #include "oops/instanceStackChunkKlass.hpp"

 35 #include "oops/methodData.hpp"

 36 #include "oops/objArrayKlass.hpp"
 37 #include "oops/typeArrayKlass.hpp"
 38 #include "runtime/arguments.hpp"
 39 #include "utilities/globalDefinitions.hpp"
 40 
 41 // Objects of the Metadata types (such as Klass and ConstantPool) have C++ vtables.
 42 // (In GCC this is the field <Type>::_vptr, i.e., first word in the object.)
 43 //
 44 // Addresses of the vtables and the methods may be different across JVM runs,
 45 // if libjvm.so is dynamically loaded at a different base address.
 46 //
 47 // To ensure that the Metadata objects in the CDS archive always have the correct vtable:
 48 //
 49 // + at dump time:  we redirect the _vptr to point to our own vtables inside
 50 //                  the CDS image
 51 // + at run time:   we clone the actual contents of the vtables from libjvm.so
 52 //                  into our own tables.
 53 
 54 // Currently, the archive contains ONLY the following types of objects that have C++ vtables.
 55 #define CPP_VTABLE_TYPES_DO(f) \
 56   f(ConstantPool) \
 57   f(InstanceKlass) \
 58   f(InstanceClassLoaderKlass) \
 59   f(InstanceMirrorKlass) \
 60   f(InstanceRefKlass) \
 61   f(InstanceStackChunkKlass) \
 62   f(Method) \


 63   f(ObjArrayKlass) \
 64   f(TypeArrayKlass)



 65 
 66 class CppVtableInfo {
 67   intptr_t _vtable_size;
 68   intptr_t _cloned_vtable[1]; // Pseudo flexible array member.
 69   static size_t cloned_vtable_offset() { return offset_of(CppVtableInfo, _cloned_vtable); }
 70 public:
 71   int vtable_size()           { return int(uintx(_vtable_size)); }
 72   void set_vtable_size(int n) { _vtable_size = intptr_t(n); }
 73   // Using _cloned_vtable[i] for i > 0 causes undefined behavior. We use address calculation instead.
 74   intptr_t* cloned_vtable()   { return (intptr_t*)((char*)this + cloned_vtable_offset()); }
 75   void zero()                 { memset(cloned_vtable(), 0, sizeof(intptr_t) * vtable_size()); }
 76   // Returns the address of the next CppVtableInfo that can be placed immediately after this CppVtableInfo
 77   static size_t byte_size(int vtable_size) {
 78     return cloned_vtable_offset() + (sizeof(intptr_t) * vtable_size);
 79   }
 80 };
 81 
 82 static inline intptr_t* vtable_of(const Metadata* m) {
 83   return *((intptr_t**)m);
 84 }

262 }
263 
264 intptr_t* CppVtables::get_archived_vtable(MetaspaceObj::Type msotype, address obj) {
265   if (!_orig_cpp_vtptrs_inited) {
266     CPP_VTABLE_TYPES_DO(INIT_ORIG_CPP_VTPTRS);
267     _orig_cpp_vtptrs_inited = true;
268   }
269 
270   assert(CDSConfig::is_dumping_archive(), "sanity");
271   int kind = -1;
272   switch (msotype) {
273   case MetaspaceObj::SymbolType:
274   case MetaspaceObj::TypeArrayU1Type:
275   case MetaspaceObj::TypeArrayU2Type:
276   case MetaspaceObj::TypeArrayU4Type:
277   case MetaspaceObj::TypeArrayU8Type:
278   case MetaspaceObj::TypeArrayOtherType:
279   case MetaspaceObj::ConstMethodType:
280   case MetaspaceObj::ConstantPoolCacheType:
281   case MetaspaceObj::AnnotationsType:
282   case MetaspaceObj::MethodCountersType:
283   case MetaspaceObj::RecordComponentType:


284     // These have no vtables.
285     break;
286   case MetaspaceObj::MethodDataType:
287     // We don't archive MethodData <-- should have been removed in removed_unsharable_info
288     ShouldNotReachHere();
289     break;
290   default:
291     for (kind = 0; kind < _num_cloned_vtable_kinds; kind ++) {
292       if (vtable_of((Metadata*)obj) == _orig_cpp_vtptrs[kind] ||
293           vtable_of((Metadata*)obj) == _archived_cpp_vtptrs[kind]) {
294         break;
295       }
296     }
297     if (kind >= _num_cloned_vtable_kinds) {
298       fatal("Cannot find C++ vtable for " INTPTR_FORMAT " -- you probably added"
299             " a new subtype of Klass or MetaData without updating CPP_VTABLE_TYPES_DO or the cases in this 'switch' statement",
300             p2i(obj));
301     }
302   }
303 
304   if (kind >= 0) {
305     assert(kind < _num_cloned_vtable_kinds, "must be");
306     return _index[kind]->cloned_vtable();
307   } else {
308     return nullptr;
309   }

 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/instanceClassLoaderKlass.hpp"
 32 #include "oops/instanceMirrorKlass.hpp"
 33 #include "oops/instanceRefKlass.hpp"
 34 #include "oops/instanceStackChunkKlass.hpp"
 35 #include "oops/methodCounters.hpp"
 36 #include "oops/methodData.hpp"
 37 #include "oops/trainingData.hpp"
 38 #include "oops/objArrayKlass.hpp"
 39 #include "oops/typeArrayKlass.hpp"
 40 #include "runtime/arguments.hpp"
 41 #include "utilities/globalDefinitions.hpp"
 42 
 43 // Objects of the Metadata types (such as Klass and ConstantPool) have C++ vtables.
 44 // (In GCC this is the field <Type>::_vptr, i.e., first word in the object.)
 45 //
 46 // Addresses of the vtables and the methods may be different across JVM runs,
 47 // if libjvm.so is dynamically loaded at a different base address.
 48 //
 49 // To ensure that the Metadata objects in the CDS archive always have the correct vtable:
 50 //
 51 // + at dump time:  we redirect the _vptr to point to our own vtables inside
 52 //                  the CDS image
 53 // + at run time:   we clone the actual contents of the vtables from libjvm.so
 54 //                  into our own tables.
 55 
 56 // Currently, the archive contains ONLY the following types of objects that have C++ vtables.
 57 #define CPP_VTABLE_TYPES_DO(f) \
 58   f(ConstantPool) \
 59   f(InstanceKlass) \
 60   f(InstanceClassLoaderKlass) \
 61   f(InstanceMirrorKlass) \
 62   f(InstanceRefKlass) \
 63   f(InstanceStackChunkKlass) \
 64   f(Method) \
 65   f(MethodData)                \
 66   f(MethodCounters)            \
 67   f(ObjArrayKlass) \
 68   f(TypeArrayKlass)            \
 69   f(KlassTrainingData)         \
 70   f(MethodTrainingData)        \
 71   f(CompileTrainingData)
 72 
 73 class CppVtableInfo {
 74   intptr_t _vtable_size;
 75   intptr_t _cloned_vtable[1]; // Pseudo flexible array member.
 76   static size_t cloned_vtable_offset() { return offset_of(CppVtableInfo, _cloned_vtable); }
 77 public:
 78   int vtable_size()           { return int(uintx(_vtable_size)); }
 79   void set_vtable_size(int n) { _vtable_size = intptr_t(n); }
 80   // Using _cloned_vtable[i] for i > 0 causes undefined behavior. We use address calculation instead.
 81   intptr_t* cloned_vtable()   { return (intptr_t*)((char*)this + cloned_vtable_offset()); }
 82   void zero()                 { memset(cloned_vtable(), 0, sizeof(intptr_t) * vtable_size()); }
 83   // Returns the address of the next CppVtableInfo that can be placed immediately after this CppVtableInfo
 84   static size_t byte_size(int vtable_size) {
 85     return cloned_vtable_offset() + (sizeof(intptr_t) * vtable_size);
 86   }
 87 };
 88 
 89 static inline intptr_t* vtable_of(const Metadata* m) {
 90   return *((intptr_t**)m);
 91 }

269 }
270 
271 intptr_t* CppVtables::get_archived_vtable(MetaspaceObj::Type msotype, address obj) {
272   if (!_orig_cpp_vtptrs_inited) {
273     CPP_VTABLE_TYPES_DO(INIT_ORIG_CPP_VTPTRS);
274     _orig_cpp_vtptrs_inited = true;
275   }
276 
277   assert(CDSConfig::is_dumping_archive(), "sanity");
278   int kind = -1;
279   switch (msotype) {
280   case MetaspaceObj::SymbolType:
281   case MetaspaceObj::TypeArrayU1Type:
282   case MetaspaceObj::TypeArrayU2Type:
283   case MetaspaceObj::TypeArrayU4Type:
284   case MetaspaceObj::TypeArrayU8Type:
285   case MetaspaceObj::TypeArrayOtherType:
286   case MetaspaceObj::ConstMethodType:
287   case MetaspaceObj::ConstantPoolCacheType:
288   case MetaspaceObj::AnnotationsType:

289   case MetaspaceObj::RecordComponentType:
290   case MetaspaceObj::AdapterHandlerEntryType:
291   case MetaspaceObj::AdapterFingerPrintType:
292     // These have no vtables.
293     break;




294   default:
295     for (kind = 0; kind < _num_cloned_vtable_kinds; kind ++) {
296       if (vtable_of((Metadata*)obj) == _orig_cpp_vtptrs[kind] ||
297           vtable_of((Metadata*)obj) == _archived_cpp_vtptrs[kind]) {
298         break;
299       }
300     }
301     if (kind >= _num_cloned_vtable_kinds) {
302       fatal("Cannot find C++ vtable for " INTPTR_FORMAT " -- you probably added"
303             " a new subtype of Klass or MetaData without updating CPP_VTABLE_TYPES_DO or the cases in this 'switch' statement",
304             p2i(obj));
305     }
306   }
307 
308   if (kind >= 0) {
309     assert(kind < _num_cloned_vtable_kinds, "must be");
310     return _index[kind]->cloned_vtable();
311   } else {
312     return nullptr;
313   }
< prev index next >