Path: blob/master/src/hotspot/share/cds/cppVtables.cpp
41144 views
/*1* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "cds/archiveUtils.hpp"26#include "cds/archiveBuilder.hpp"27#include "cds/cppVtables.hpp"28#include "cds/metaspaceShared.hpp"29#include "logging/log.hpp"30#include "oops/instanceClassLoaderKlass.hpp"31#include "oops/instanceMirrorKlass.hpp"32#include "oops/instanceRefKlass.hpp"33#include "oops/methodData.hpp"34#include "oops/objArrayKlass.hpp"35#include "oops/typeArrayKlass.hpp"36#include "runtime/arguments.hpp"37#include "utilities/globalDefinitions.hpp"3839// Objects of the Metadata types (such as Klass and ConstantPool) have C++ vtables.40// (In GCC this is the field <Type>::_vptr, i.e., first word in the object.)41//42// Addresses of the vtables and the methods may be different across JVM runs,43// if libjvm.so is dynamically loaded at a different base address.44//45// To ensure that the Metadata objects in the CDS archive always have the correct vtable:46//47// + at dump time: we redirect the _vptr to point to our own vtables inside48// the CDS image49// + at run time: we clone the actual contents of the vtables from libjvm.so50// into our own tables.5152// Currently, the archive contains ONLY the following types of objects that have C++ vtables.53#define CPP_VTABLE_TYPES_DO(f) \54f(ConstantPool) \55f(InstanceKlass) \56f(InstanceClassLoaderKlass) \57f(InstanceMirrorKlass) \58f(InstanceRefKlass) \59f(Method) \60f(ObjArrayKlass) \61f(TypeArrayKlass)6263class CppVtableInfo {64intptr_t _vtable_size;65intptr_t _cloned_vtable[1];66public:67static int num_slots(int vtable_size) {68return 1 + vtable_size; // Need to add the space occupied by _vtable_size;69}70int vtable_size() { return int(uintx(_vtable_size)); }71void set_vtable_size(int n) { _vtable_size = intptr_t(n); }72intptr_t* cloned_vtable() { return &_cloned_vtable[0]; }73void zero() { memset(_cloned_vtable, 0, sizeof(intptr_t) * vtable_size()); }74// Returns the address of the next CppVtableInfo that can be placed immediately after this CppVtableInfo75static size_t byte_size(int vtable_size) {76CppVtableInfo i;77return pointer_delta(&i._cloned_vtable[vtable_size], &i, sizeof(u1));78}79};8081static inline intptr_t* vtable_of(const Metadata* m) {82return *((intptr_t**)m);83}8485template <class T> class CppVtableCloner {86static int get_vtable_length(const char* name);8788public:89// Allocate a clone of the vtable of T from the shared metaspace;90// Initialize the contents of this clone.91static CppVtableInfo* allocate_and_initialize(const char* name);9293// Copy the contents of the vtable of T into info->_cloned_vtable;94static void initialize(const char* name, CppVtableInfo* info);9596static void init_orig_cpp_vtptr(int kind);97};9899template <class T>100CppVtableInfo* CppVtableCloner<T>::allocate_and_initialize(const char* name) {101int n = get_vtable_length(name);102CppVtableInfo* info =103(CppVtableInfo*)ArchiveBuilder::current()->rw_region()->allocate(CppVtableInfo::byte_size(n));104info->set_vtable_size(n);105initialize(name, info);106return info;107}108109template <class T>110void CppVtableCloner<T>::initialize(const char* name, CppVtableInfo* info) {111T tmp; // Allocate temporary dummy metadata object to get to the original vtable.112int n = info->vtable_size();113intptr_t* srcvtable = vtable_of(&tmp);114intptr_t* dstvtable = info->cloned_vtable();115116// We already checked (and, if necessary, adjusted n) when the vtables were allocated, so we are117// safe to do memcpy.118log_debug(cds, vtables)("Copying %3d vtable entries for %s", n, name);119memcpy(dstvtable, srcvtable, sizeof(intptr_t) * n);120}121122// To determine the size of the vtable for each type, we use the following123// trick by declaring 2 subclasses:124//125// class CppVtableTesterA: public InstanceKlass {virtual int last_virtual_method() {return 1;} };126// class CppVtableTesterB: public InstanceKlass {virtual void* last_virtual_method() {return NULL}; };127//128// CppVtableTesterA and CppVtableTesterB's vtables have the following properties:129// - Their size (N+1) is exactly one more than the size of InstanceKlass's vtable (N)130// - The first N entries have are exactly the same as in InstanceKlass's vtable.131// - Their last entry is different.132//133// So to determine the value of N, we just walk CppVtableTesterA and CppVtableTesterB's tables134// and find the first entry that's different.135//136// This works on all C++ compilers supported by Oracle, but you may need to tweak it for more137// esoteric compilers.138139template <class T> class CppVtableTesterB: public T {140public:141virtual int last_virtual_method() {return 1;}142};143144template <class T> class CppVtableTesterA : public T {145public:146virtual void* last_virtual_method() {147// Make this different than CppVtableTesterB::last_virtual_method so the C++148// compiler/linker won't alias the two functions.149return NULL;150}151};152153template <class T>154int CppVtableCloner<T>::get_vtable_length(const char* name) {155CppVtableTesterA<T> a;156CppVtableTesterB<T> b;157158intptr_t* avtable = vtable_of(&a);159intptr_t* bvtable = vtable_of(&b);160161// Start at slot 1, because slot 0 may be RTTI (on Solaris/Sparc)162int vtable_len = 1;163for (; ; vtable_len++) {164if (avtable[vtable_len] != bvtable[vtable_len]) {165break;166}167}168log_debug(cds, vtables)("Found %3d vtable entries for %s", vtable_len, name);169170return vtable_len;171}172173#define ALLOCATE_AND_INITIALIZE_VTABLE(c) \174_index[c##_Kind] = CppVtableCloner<c>::allocate_and_initialize(#c); \175ArchivePtrMarker::mark_pointer(&_index[c##_Kind]);176177#define INITIALIZE_VTABLE(c) \178CppVtableCloner<c>::initialize(#c, _index[c##_Kind]);179180#define INIT_ORIG_CPP_VTPTRS(c) \181CppVtableCloner<c>::init_orig_cpp_vtptr(c##_Kind);182183#define DECLARE_CLONED_VTABLE_KIND(c) c ## _Kind,184185enum ClonedVtableKind {186// E.g., ConstantPool_Kind == 0, InstanceKlass_Kind == 1, etc.187CPP_VTABLE_TYPES_DO(DECLARE_CLONED_VTABLE_KIND)188_num_cloned_vtable_kinds189};190191// This is a map of all the original vtptrs. E.g., for192// ConstantPool *cp = new (...) ConstantPool(...) ; // a dynamically allocated constant pool193// the following holds true:194// _orig_cpp_vtptrs[ConstantPool_Kind] == ((intptr_t**)cp)[0]195static intptr_t* _orig_cpp_vtptrs[_num_cloned_vtable_kinds];196static bool _orig_cpp_vtptrs_inited = false;197198template <class T>199void CppVtableCloner<T>::init_orig_cpp_vtptr(int kind) {200assert(kind < _num_cloned_vtable_kinds, "sanity");201T tmp; // Allocate temporary dummy metadata object to get to the original vtable.202intptr_t* srcvtable = vtable_of(&tmp);203_orig_cpp_vtptrs[kind] = srcvtable;204}205206// This is the index of all the cloned vtables. E.g., for207// ConstantPool* cp = ....; // an archived constant pool208// InstanceKlass* ik = ....;// an archived class209// the following holds true:210// _index[ConstantPool_Kind]->cloned_vtable() == ((intptr_t**)cp)[0]211// _index[InstanceKlass_Kind]->cloned_vtable() == ((intptr_t**)ik)[0]212CppVtableInfo** CppVtables::_index = NULL;213214char* CppVtables::dumptime_init(ArchiveBuilder* builder) {215assert(DumpSharedSpaces, "must");216size_t vtptrs_bytes = _num_cloned_vtable_kinds * sizeof(CppVtableInfo*);217_index = (CppVtableInfo**)builder->rw_region()->allocate(vtptrs_bytes);218219CPP_VTABLE_TYPES_DO(ALLOCATE_AND_INITIALIZE_VTABLE);220221size_t cpp_tables_size = builder->rw_region()->top() - builder->rw_region()->base();222builder->alloc_stats()->record_cpp_vtables((int)cpp_tables_size);223224return (char*)_index;225}226227void CppVtables::serialize(SerializeClosure* soc) {228soc->do_ptr((void**)&_index);229if (soc->reading()) {230CPP_VTABLE_TYPES_DO(INITIALIZE_VTABLE);231}232}233234intptr_t* CppVtables::get_archived_vtable(MetaspaceObj::Type msotype, address obj) {235if (!_orig_cpp_vtptrs_inited) {236CPP_VTABLE_TYPES_DO(INIT_ORIG_CPP_VTPTRS);237_orig_cpp_vtptrs_inited = true;238}239240Arguments::assert_is_dumping_archive();241int kind = -1;242switch (msotype) {243case MetaspaceObj::SymbolType:244case MetaspaceObj::TypeArrayU1Type:245case MetaspaceObj::TypeArrayU2Type:246case MetaspaceObj::TypeArrayU4Type:247case MetaspaceObj::TypeArrayU8Type:248case MetaspaceObj::TypeArrayOtherType:249case MetaspaceObj::ConstMethodType:250case MetaspaceObj::ConstantPoolCacheType:251case MetaspaceObj::AnnotationsType:252case MetaspaceObj::MethodCountersType:253case MetaspaceObj::RecordComponentType:254// These have no vtables.255break;256case MetaspaceObj::MethodDataType:257// We don't archive MethodData <-- should have been removed in removed_unsharable_info258ShouldNotReachHere();259break;260default:261for (kind = 0; kind < _num_cloned_vtable_kinds; kind ++) {262if (vtable_of((Metadata*)obj) == _orig_cpp_vtptrs[kind]) {263break;264}265}266if (kind >= _num_cloned_vtable_kinds) {267fatal("Cannot find C++ vtable for " INTPTR_FORMAT " -- you probably added"268" a new subtype of Klass or MetaData without updating CPP_VTABLE_TYPES_DO",269p2i(obj));270}271}272273if (kind >= 0) {274assert(kind < _num_cloned_vtable_kinds, "must be");275return _index[kind]->cloned_vtable();276} else {277return NULL;278}279}280281void CppVtables::zero_archived_vtables() {282assert(DumpSharedSpaces, "dump-time only");283for (int kind = 0; kind < _num_cloned_vtable_kinds; kind ++) {284_index[kind]->zero();285}286}287288bool CppVtables::is_valid_shared_method(const Method* m) {289assert(MetaspaceShared::is_in_shared_metaspace(m), "must be");290return vtable_of(m) == _index[Method_Kind]->cloned_vtable();291}292293294