Determining the size of C++ vtables

Ioi Lam ioi.lam at oracle.com
Fri Feb 24 03:47:03 UTC 2017


Hi,

I am working on https://bugs.openjdk.java.net/browse/JDK-8005165 (Remove
CPU-dependent code in self-patching vtables), I need a way find out the size
of a C++ vtable. I ended up doing this:


// Objects of the Metadata types (such as Klass and ConstantPool) have 
C++ vtables.
// (In GCC this is the field <Type>::_vptr, i.e., first word in the object.)
//
// Addresses of the vtables and the methods may be different across JVM 
runs,
// if libjvm.so is dynamically loaded at a different base address.
//
// To ensure that the Metadata objects in the CDS archive always have 
the correct vtable:
//
// + at dump time:  we redirect the _vptr to point to our own vtables inside
//                  the CDS image
// + at run time:   we clone the actual contents of the vtables from 
libjvm.so
//                  into our own tables.
//
// To determine the size of the vtable for each type, we use the following
// trick by declaring 2 subclasses:
//
//   class CppVtabTesterA: public InstanceKlass {
//          virtual int   last_virtual_method() {return 1;}
//   };
//   class CppVtabTesterB: public InstanceKlass {
//          virtual void* last_virtual_method() {return NULL};
//   };
//
// CppVtabTesterA and CppVtabTesterB's vtables have the following 
properties:
// - Their size (N+1) is exactly one more than the size of 
InstanceKlass's vtable (N)
// - The first N entries have are exactly the same as in InstanceKlass's 
vtable.
// - Their last entry is different.
//
// So to determine the value of N, we just walk CppVtabTesterA and 
CppVtabTesterB's tables
// and find the first entry that's different


Could anyone comment if this is acceptable? I know it's not 100% 
portable (C++ doesn't
specify where to find the vtable, or what's inside), but my assumptions 
is the same as
the existing code. I.e., _vptr is a pointer located at offset 0 of the 
object, and it
points to a one-dimensional array.

So at least it's not any worse than before?

Thanks
- Ioi



More information about the hotspot-dev mailing list