1 /*
   2  * Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  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 #ifndef SHARE_MEMORY_METASPACECLOSURE_HPP
  26 #define SHARE_MEMORY_METASPACECLOSURE_HPP
  27 
  28 #include "logging/log.hpp"
  29 #include "memory/allocation.hpp"
  30 #include "oops/array.hpp"
  31 #include "utilities/globalDefinitions.hpp"
  32 #include "utilities/growableArray.hpp"
  33 #include "utilities/hashtable.inline.hpp"
  34 
  35 // The metadata hierarchy is separate from the oop hierarchy
  36   class MetaspaceObj;        // no C++ vtable
  37 //class   Array;             // no C++ vtable
  38   class   Annotations;       // no C++ vtable
  39   class   ConstantPoolCache; // no C++ vtable
  40   class   ConstMethod;       // no C++ vtable
  41   class   MethodCounters;    // no C++ vtable
  42   class   Symbol;            // no C++ vtable
  43   class   Metadata;          // has C++ vtable (so do all subclasses)
  44   class     ConstantPool;
  45   class     MethodData;
  46   class     Method;
  47   class     Klass;
  48   class       InstanceKlass;
  49   class         InstanceMirrorKlass;
  50   class         InstanceClassLoaderKlass;
  51   class         InstanceRefKlass;
  52   class       ArrayKlass;
  53   class         ObjArrayKlass;
  54   class         TypeArrayKlass;
  55 
  56 // class MetaspaceClosure --
  57 //
  58 // This class is used for iterating the objects in the HotSpot Metaspaces. It
  59 // provides an API to walk all the reachable objects starting from a set of
  60 // root references (such as all Klass'es in the SystemDictionary).
  61 //
  62 // Currently it is used for compacting the CDS archive by eliminate temporary
  63 // objects allocated during archive creation time. See ArchiveCompactor in
  64 // metaspaceShared.cpp for an example.
  65 //
  66 // To support MetaspaceClosure, each subclass of MetaspaceObj must provide
  67 // a method of the type void metaspace_pointers_do(MetaspaceClosure*). This method
  68 // should call MetaspaceClosure::push() on every pointer fields of this
  69 // class that points to a MetaspaceObj. See Annotations::metaspace_pointers_do()
  70 // for an example.
  71 class MetaspaceClosure {
  72 public:
  73   enum Writability {
  74     _writable,
  75     _not_writable,
  76     _default
  77   };
  78 
  79   enum SpecialRef {
  80     _method_entry_ref
  81   };
  82 
  83   // class MetaspaceClosure::Ref --
  84   //
  85   // MetaspaceClosure can be viewed as a very simple type of copying garbage
  86   // collector. For it to function properly, it requires each subclass of
  87   // MetaspaceObj to provide two methods:
  88   //
  89   //  size_t size();                                 -- to determine how much data to copy
  90   //  void metaspace_pointers_do(MetaspaceClosure*); -- to locate all the embedded pointers
  91   //
  92   // Calling these methods would be trivial if these two were virtual methods.
  93   // However, to save space, MetaspaceObj has NO vtable. The vtable is introduced
  94   // only in the Metadata class.
  95   //
  96   // To work around the lack of a vtable, we use Ref class with templates
  97   // (see ObjectRef, PrimitiveArrayRef and PointerArrayRef)
  98   // so that we can statically discover the type of a object. The use of Ref
  99   // depends on the fact that:
 100   //
 101   // [1] We don't use polymorphic pointers for MetaspaceObj's that are not subclasses
 102   //     of Metadata. I.e., we don't do this:
 103   //     class Klass {
 104   //         MetaspaceObj *_obj;
 105   //         Array<int>* foo() { return (Array<int>*)_obj; }
 106   //         Symbol*     bar() { return (Symbol*)    _obj; }
 107   //
 108   // [2] All Array<T> dimensions are statically declared.
 109   class Ref : public CHeapObj<mtInternal> {
 110     Writability _writability;
 111     bool _keep_after_pushing;
 112     Ref* _next;
 113     void* _user_data;
 114     NONCOPYABLE(Ref);
 115 
 116   protected:
 117     virtual void** mpp() const = 0;
 118     Ref(Writability w) : _writability(w), _keep_after_pushing(false), _next(NULL), _user_data(NULL) {}
 119   public:
 120     virtual bool not_null() const = 0;
 121     virtual int size() const = 0;
 122     virtual void metaspace_pointers_do(MetaspaceClosure *it) const = 0;
 123     virtual void metaspace_pointers_do_at(MetaspaceClosure *it, address new_loc) const = 0;
 124     virtual MetaspaceObj::Type msotype() const = 0;
 125     virtual bool is_read_only_by_default() const = 0;
 126     virtual ~Ref() {}
 127 
 128     address obj() const {
 129       // In some rare cases (see CPSlot in constantPool.hpp) we store some flags in the lowest
 130       // 2 bits of a MetaspaceObj pointer. Unmask these when manipulating the pointer.
 131       uintx p = (uintx)*mpp();
 132       return (address)(p & (~FLAG_MASK));
 133     }
 134 
 135     address* addr() const {
 136       return (address*)mpp();
 137     }
 138 
 139     void update(address new_loc) const;
 140 
 141     Writability writability() const { return _writability; };
 142     void set_keep_after_pushing()   { _keep_after_pushing = true; }
 143     bool keep_after_pushing()       { return _keep_after_pushing; }
 144     void set_user_data(void* data)  { _user_data = data; }
 145     void* user_data()               { return _user_data; }
 146     void set_next(Ref* n)           { _next = n; }
 147     Ref* next() const               { return _next; }
 148 
 149   private:
 150     static const uintx FLAG_MASK = 0x03;
 151 
 152     int flag_bits() const {
 153       uintx p = (uintx)*mpp();
 154       return (int)(p & FLAG_MASK);
 155     }
 156   };
 157 
 158 private:
 159   // -------------------------------------------------- ObjectRef
 160   template <class T> class ObjectRef : public Ref {
 161     T** _mpp;
 162     T* dereference() const {
 163       return *_mpp;
 164     }
 165   protected:
 166     virtual void** mpp() const {
 167       return (void**)_mpp;
 168     }
 169 
 170   public:
 171     ObjectRef(T** mpp, Writability w) : Ref(w), _mpp(mpp) {}
 172 
 173     virtual bool is_read_only_by_default() const { return T::is_read_only_by_default(); }
 174     virtual bool not_null()                const { return dereference() != NULL; }
 175     virtual int size()                     const { return dereference()->size(); }
 176     virtual MetaspaceObj::Type msotype()   const { return dereference()->type(); }
 177 
 178     virtual void metaspace_pointers_do(MetaspaceClosure *it) const {
 179       dereference()->metaspace_pointers_do(it);
 180     }
 181     virtual void metaspace_pointers_do_at(MetaspaceClosure *it, address new_loc) const {
 182       ((T*)new_loc)->metaspace_pointers_do(it);
 183     }
 184   };
 185 
 186   // -------------------------------------------------- PrimitiveArrayRef
 187   template <class T> class PrimitiveArrayRef : public Ref {
 188     Array<T>** _mpp;
 189     Array<T>* dereference() const {
 190       return *_mpp;
 191     }
 192   protected:
 193     virtual void** mpp() const {
 194       return (void**)_mpp;
 195     }
 196 
 197   public:
 198     PrimitiveArrayRef(Array<T>** mpp, Writability w) : Ref(w), _mpp(mpp) {}
 199 
 200     // all Arrays are read-only by default
 201     virtual bool is_read_only_by_default() const { return true; }
 202     virtual bool not_null()                const { return dereference() != NULL;  }
 203     virtual int size()                     const { return dereference()->size(); }
 204     virtual MetaspaceObj::Type msotype()   const { return MetaspaceObj::array_type(sizeof(T)); }
 205 
 206     virtual void metaspace_pointers_do(MetaspaceClosure *it) const {
 207       Array<T>* array = dereference();
 208       log_trace(cds)("Iter(PrimitiveArray): %p [%d]", array, array->length());
 209     }
 210     virtual void metaspace_pointers_do_at(MetaspaceClosure *it, address new_loc) const {
 211       Array<T>* array = (Array<T>*)new_loc;
 212       log_trace(cds)("Iter(PrimitiveArray): %p [%d]", array, array->length());
 213     }
 214   };
 215 
 216   // -------------------------------------------------- PointerArrayRef
 217   template <class T> class PointerArrayRef : public Ref {
 218     Array<T*>** _mpp;
 219     Array<T*>* dereference() const {
 220       return *_mpp;
 221     }
 222   protected:
 223     virtual void** mpp() const {
 224       return (void**)_mpp;
 225     }
 226 
 227   public:
 228     PointerArrayRef(Array<T*>** mpp, Writability w) : Ref(w), _mpp(mpp) {}
 229 
 230     // all Arrays are read-only by default
 231     virtual bool is_read_only_by_default() const { return true; }
 232     virtual bool not_null()                const { return dereference() != NULL; }
 233     virtual int size()                     const { return dereference()->size(); }
 234     virtual MetaspaceObj::Type msotype()   const { return MetaspaceObj::array_type(sizeof(T*)); }
 235 
 236     virtual void metaspace_pointers_do(MetaspaceClosure *it) const {
 237       metaspace_pointers_do_at_impl(it, dereference());
 238     }
 239     virtual void metaspace_pointers_do_at(MetaspaceClosure *it, address new_loc) const {
 240       metaspace_pointers_do_at_impl(it, (Array<T*>*)new_loc);
 241     }
 242   private:
 243     void metaspace_pointers_do_at_impl(MetaspaceClosure *it, Array<T*>* array) const {
 244       log_trace(cds)("Iter(ObjectArray): %p [%d]", array, array->length());
 245       for (int i = 0; i < array->length(); i++) {
 246         T** mpp = array->adr_at(i);
 247         it->push(mpp);
 248       }
 249     }
 250   };
 251 
 252   // Normally, chains of references like a->b->c->d are iterated recursively. However,
 253   // if recursion is too deep, we save the Refs in _pending_refs, and push them later in
 254   // MetaspaceClosure::finish(). This avoids overflowing the C stack.
 255   static const int MAX_NEST_LEVEL = 5;
 256   Ref* _pending_refs;
 257   int _nest_level;
 258   Ref* _enclosing_ref;
 259 
 260   void push_impl(Ref* ref);
 261   void do_push(Ref* ref);
 262 
 263 public:
 264   MetaspaceClosure(): _pending_refs(NULL), _nest_level(0), _enclosing_ref(NULL) {}
 265   ~MetaspaceClosure();
 266 
 267   void finish();
 268 
 269   // enclosing_ref() is used to compute the offset of a field in a C++ class. For example
 270   // class Foo { intx scala; Bar* ptr; }
 271   //    Foo *f = 0x100;
 272   // when the f->ptr field is iterated with do_ref() on 64-bit platforms, we will have
 273   //    do_ref(Ref* r) {
 274   //       r->addr() == 0x108;                // == &f->ptr;
 275   //       enclosing_ref()->obj() == 0x100;   // == foo
 276   // So we know that we are iterating upon a field at offset 8 of the object at 0x100.
 277   //
 278   // Note that if we have stack overflow, do_pending_ref(r) will be called first and
 279   // do_ref(r) will be called later, for the same r. In this case, enclosing_ref() is valid only
 280   // when do_pending_ref(r) is called, and will return NULL when do_ref(r) is called.
 281   Ref* enclosing_ref() const {
 282     return _enclosing_ref;
 283   }
 284 
 285   // This is called when a reference is placed in _pending_refs. Override this
 286   // function if you're using enclosing_ref(). See notes above.
 287   virtual void do_pending_ref(Ref* ref) {}
 288 
 289   // returns true if we want to keep iterating the pointers embedded inside <ref>
 290   virtual bool do_ref(Ref* ref, bool read_only) = 0;
 291 
 292   // When you do:
 293   //     void MyType::metaspace_pointers_do(MetaspaceClosure* it) {
 294   //       it->push(_my_field)
 295   //     }
 296   //
 297   // C++ will try to match the "most specific" template function. This one will
 298   // will be matched if possible (if mpp is an Array<> of any pointer type).
 299   template <typename T> void push(Array<T*>** mpp, Writability w = _default) {
 300     push_impl(new PointerArrayRef<T>(mpp, w));
 301   }
 302 
 303   // If the above function doesn't match (mpp is an Array<>, but T is not a pointer type), then
 304   // this is the second choice.
 305   template <typename T> void push(Array<T>** mpp, Writability w = _default) {
 306     push_impl(new PrimitiveArrayRef<T>(mpp, w));
 307   }
 308 
 309   // If the above function doesn't match (mpp is not an Array<> type), then
 310   // this will be matched by default.
 311   template <class T> void push(T** mpp, Writability w = _default) {
 312     push_impl(new ObjectRef<T>(mpp, w));
 313   }
 314 
 315   template <class T> void push_method_entry(T** mpp, intptr_t* p) {
 316     Ref* ref = new ObjectRef<T>(mpp, _default);
 317     push_special(_method_entry_ref, ref, (intptr_t*)p);
 318     if (!ref->keep_after_pushing()) {
 319       delete ref;
 320     }
 321   }
 322 
 323   // This is for tagging special pointers that are not a reference to MetaspaceObj. It's currently
 324   // used to mark the method entry points in Method/ConstMethod.
 325   virtual void push_special(SpecialRef type, Ref* obj, intptr_t* p) {
 326     assert(type == _method_entry_ref, "only special type allowed for now");
 327   }
 328 };
 329 
 330 // This is a special MetaspaceClosure that visits each unique MetaspaceObj once.
 331 class UniqueMetaspaceClosure : public MetaspaceClosure {
 332   static const int INITIAL_TABLE_SIZE = 15889;
 333   static const int MAX_TABLE_SIZE     = 1000000;
 334 
 335   // Do not override. Returns true if we are discovering ref->obj() for the first time.
 336   virtual bool do_ref(Ref* ref, bool read_only);
 337 
 338 public:
 339   // Gets called the first time we discover an object.
 340   virtual bool do_unique_ref(Ref* ref, bool read_only) = 0;
 341   UniqueMetaspaceClosure() : _has_been_visited(INITIAL_TABLE_SIZE) {}
 342 
 343 private:
 344   KVHashtable<address, bool, mtInternal> _has_been_visited;
 345 };
 346 
 347 #endif // SHARE_MEMORY_METASPACECLOSURE_HPP