1 /*
  2  * Copyright (c) 2017, 2023, 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 "metaprogramming/enableIf.hpp"
 31 #include "oops/array.hpp"
 32 #include "utilities/globalDefinitions.hpp"
 33 #include "utilities/growableArray.hpp"
 34 #include "utilities/macros.hpp"
 35 #include "utilities/resizeableResourceHash.hpp"
 36 #include <type_traits>
 37 
 38 // The metadata hierarchy is separate from the oop hierarchy
 39   class MetaspaceObj;        // no C++ vtable
 40 //class   Array;             // no C++ vtable
 41   class   Annotations;       // no C++ vtable
 42   class   ConstantPoolCache; // no C++ vtable
 43   class   ConstMethod;       // no C++ vtable
 44   class   MethodCounters;    // no C++ vtable
 45   class   Symbol;            // no C++ vtable
 46   class   Metadata;          // has C++ vtable (so do all subclasses)
 47   class     ConstantPool;
 48   class     MethodData;
 49   class     Method;
 50   class     Klass;
 51   class       InstanceKlass;
 52   class         InstanceMirrorKlass;
 53   class         InstanceClassLoaderKlass;
 54   class         InstanceRefKlass;
 55   class       ArrayKlass;
 56   class         ObjArrayKlass;
 57   class         TypeArrayKlass;
 58 
 59 // class MetaspaceClosure --
 60 //
 61 // This class is used for iterating the objects in the HotSpot Metaspaces. It
 62 // provides an API to walk all the reachable objects starting from a set of
 63 // root references (such as all Klass'es in the SystemDictionary).
 64 //
 65 // Currently it is used for compacting the CDS archive by eliminate temporary
 66 // objects allocated during archive creation time. See ArchiveBuilder for an example.
 67 //
 68 // To support MetaspaceClosure, each subclass of MetaspaceObj must provide
 69 // a method of the type void metaspace_pointers_do(MetaspaceClosure*). This method
 70 // should call MetaspaceClosure::push() on every pointer fields of this
 71 // class that points to a MetaspaceObj. See Annotations::metaspace_pointers_do()
 72 // for an example.
 73 class MetaspaceClosure {
 74 public:
 75   enum Writability {
 76     _writable,
 77     _not_writable,
 78     _default
 79   };
 80 
 81   // class MetaspaceClosure::Ref --
 82   //
 83   // MetaspaceClosure can be viewed as a very simple type of copying garbage
 84   // collector. For it to function properly, it requires each subclass of
 85   // MetaspaceObj to provide two methods:
 86   //
 87   //  size_t size();                                 -- to determine how much data to copy
 88   //  void metaspace_pointers_do(MetaspaceClosure*); -- to locate all the embedded pointers
 89   //
 90   // Calling these methods would be trivial if these two were virtual methods.
 91   // However, to save space, MetaspaceObj has NO vtable. The vtable is introduced
 92   // only in the Metadata class.
 93   //
 94   // To work around the lack of a vtable, we use the Ref class with templates
 95   // (see MSORef, OtherArrayRef, MSOArrayRef, and MSOPointerArrayRef)
 96   // so that we can statically discover the type of a object. The use of Ref
 97   // depends on the fact that:
 98   //
 99   // [1] We don't use polymorphic pointers for MetaspaceObj's that are not subclasses
100   //     of Metadata. I.e., we don't do this:
101   //     class Klass {
102   //         MetaspaceObj *_obj;
103   //         Array<int>* foo() { return (Array<int>*)_obj; }
104   //         Symbol*     bar() { return (Symbol*)    _obj; }
105   //
106   // [2] All Array<T> dimensions are statically declared.
107   class Ref : public CHeapObj<mtMetaspace> {
108     Writability _writability;
109     address _enclosing_obj;
110     Ref* _next;
111     NONCOPYABLE(Ref);
112 
113   protected:
114     virtual void** mpp() const = 0;
115     Ref(Writability w) : _writability(w), _enclosing_obj(nullptr), _next(nullptr) {}
116   public:
117     virtual bool not_null() const = 0;
118     virtual int size() const = 0;
119     virtual void metaspace_pointers_do(MetaspaceClosure *it) const = 0;
120     virtual void metaspace_pointers_do_at(MetaspaceClosure *it, address new_loc) const = 0;
121     virtual MetaspaceObj::Type msotype() const = 0;
122     virtual bool is_read_only_by_default() const = 0;
123     virtual ~Ref() {}
124 
125     address obj() const {
126       return *addr();
127     }
128 
129     address* addr() const {
130       return (address*)mpp();
131     }
132 
133     // See comments in ArchiveBuilder::remember_embedded_pointer_in_enclosing_obj()
134     address enclosing_obj() const {
135       return _enclosing_obj;
136     }
137     void set_enclosing_obj(address obj) {
138       _enclosing_obj = obj;
139     }
140 
141     Writability writability() const { return _writability; };
142     void set_next(Ref* n)           { _next = n; }
143     Ref* next() const               { return _next; }
144   };
145 
146 private:
147   // MSORef -- iterate an instance of MetaspaceObj
148   template <class T> class MSORef : public Ref {
149     T** _mpp;
150     T* dereference() const {
151       return *_mpp;
152     }
153   protected:
154     virtual void** mpp() const {
155       return (void**)_mpp;
156     }
157 
158   public:
159     MSORef(T** mpp, Writability w) : Ref(w), _mpp(mpp) {}
160 
161     virtual bool is_read_only_by_default() const { return T::is_read_only_by_default(); }
162     virtual bool not_null()                const { return dereference() != nullptr; }
163     virtual int size()                     const { return dereference()->size(); }
164     virtual MetaspaceObj::Type msotype()   const { return dereference()->type(); }
165 
166     virtual void metaspace_pointers_do(MetaspaceClosure *it) const {
167       dereference()->metaspace_pointers_do(it);
168     }
169     virtual void metaspace_pointers_do_at(MetaspaceClosure *it, address new_loc) const {
170       ((T*)new_loc)->metaspace_pointers_do(it);
171     }
172   };
173 
174   // abstract base class for MSOArrayRef, MSOPointerArrayRef and OtherArrayRef
175   template <class T> class ArrayRef : public Ref {
176     Array<T>** _mpp;
177   protected:
178     Array<T>* dereference() const {
179       return *_mpp;
180     }
181     virtual void** mpp() const {
182       return (void**)_mpp;
183     }
184 
185     ArrayRef(Array<T>** mpp, Writability w) : Ref(w), _mpp(mpp) {}
186 
187     // all Arrays are read-only by default
188     virtual bool is_read_only_by_default() const { return true; }
189     virtual bool not_null()                const { return dereference() != nullptr;  }
190     virtual int size()                     const { return dereference()->size(); }
191     virtual MetaspaceObj::Type msotype()   const { return MetaspaceObj::array_type(sizeof(T)); }
192   };
193 
194   // OtherArrayRef -- iterate an instance of Array<T>, where T is NOT a subtype of MetaspaceObj.
195   // T can be a primitive type, such as int, or a structure. However, we do not scan
196   // the fields inside T, so you should not embed any pointers inside T.
197   template <class T> class OtherArrayRef : public ArrayRef<T> {
198   public:
199     OtherArrayRef(Array<T>** mpp, Writability w) : ArrayRef<T>(mpp, w) {}
200 
201     virtual void metaspace_pointers_do(MetaspaceClosure *it) const {
202       Array<T>* array = ArrayRef<T>::dereference();
203       log_trace(cds)("Iter(OtherArray): %p [%d]", array, array->length());
204     }
205     virtual void metaspace_pointers_do_at(MetaspaceClosure *it, address new_loc) const {
206       Array<T>* array = (Array<T>*)new_loc;
207       log_trace(cds)("Iter(OtherArray): %p [%d]", array, array->length());
208     }
209   };
210 
211   // MSOArrayRef -- iterate an instance of Array<T>, where T is a subtype of MetaspaceObj.
212   // We recursively call T::metaspace_pointers_do() for each element in this array.
213   template <class T> class MSOArrayRef : public ArrayRef<T> {
214   public:
215     MSOArrayRef(Array<T>** mpp, Writability w) : ArrayRef<T>(mpp, w) {}
216 
217     virtual void metaspace_pointers_do(MetaspaceClosure *it) const {
218       metaspace_pointers_do_at_impl(it, ArrayRef<T>::dereference());
219     }
220     virtual void metaspace_pointers_do_at(MetaspaceClosure *it, address new_loc) const {
221       metaspace_pointers_do_at_impl(it, (Array<T>*)new_loc);
222     }
223   private:
224     void metaspace_pointers_do_at_impl(MetaspaceClosure *it, Array<T>* array) const {
225       log_trace(cds)("Iter(MSOArray): %p [%d]", array, array->length());
226       for (int i = 0; i < array->length(); i++) {
227         T* elm = array->adr_at(i);
228         elm->metaspace_pointers_do(it);
229       }
230     }
231   };
232 
233   // MSOPointerArrayRef -- iterate an instance of Array<T*>, where T is a subtype of MetaspaceObj.
234   // We recursively call MetaspaceClosure::push() for each pointer in this array.
235   template <class T> class MSOPointerArrayRef : public ArrayRef<T*> {
236   public:
237     MSOPointerArrayRef(Array<T*>** mpp, Writability w) : ArrayRef<T*>(mpp, w) {}
238 
239     virtual void metaspace_pointers_do(MetaspaceClosure *it) const {
240       metaspace_pointers_do_at_impl(it, ArrayRef<T*>::dereference());
241     }
242     virtual void metaspace_pointers_do_at(MetaspaceClosure *it, address new_loc) const {
243       metaspace_pointers_do_at_impl(it, (Array<T*>*)new_loc);
244     }
245   private:
246     void metaspace_pointers_do_at_impl(MetaspaceClosure *it, Array<T*>* array) const {
247       log_trace(cds)("Iter(MSOPointerArray): %p [%d]", array, array->length());
248       for (int i = 0; i < array->length(); i++) {
249         T** mpp = array->adr_at(i);
250         it->push(mpp);
251       }
252     }
253   };
254 
255   // Normally, chains of references like a->b->c->d are iterated recursively. However,
256   // if recursion is too deep, we save the Refs in _pending_refs, and push them later in
257   // MetaspaceClosure::finish(). This avoids overflowing the C stack.
258   //
259   // When we are visting d, the _enclosing_ref is c,
260   // When we are visting c, the _enclosing_ref is b, ... and so on.
261   static const int MAX_NEST_LEVEL = 5;
262   Ref* _pending_refs;
263   int _nest_level;
264   Ref* _enclosing_ref;
265 
266   void push_impl(Ref* ref);
267   void do_push(Ref* ref);
268 
269 public:
270   MetaspaceClosure(): _pending_refs(nullptr), _nest_level(0), _enclosing_ref(nullptr) {}
271   ~MetaspaceClosure();
272 
273   void finish();
274 
275   // returns true if we want to keep iterating the pointers embedded inside <ref>
276   virtual bool do_ref(Ref* ref, bool read_only) = 0;
277 
278 private:
279   template <class REF_TYPE, typename T>
280   void push_with_ref(T** mpp, Writability w) {
281     // We cannot make stack allocation because the Ref may need to be saved in
282     // _pending_refs to avoid overflowing the C call stack
283     push_impl(new REF_TYPE(mpp, w));
284   }
285 
286 public:
287   // When MetaspaceClosure::push(...) is called, pick the correct Ref subtype to handle it:
288   //
289   // MetaspaceClosure*      it = ...;
290   // Klass*                 o  = ...;  it->push(&o);     => MSORef
291   // Array<int>*            a1 = ...;  it->push(&a1);    => OtherArrayRef
292   // Array<Annotation>*     a2 = ...;  it->push(&a2);    => MSOArrayRef
293   // Array<Klass*>*         a3 = ...;  it->push(&a3);    => MSOPointerArrayRef
294   // Array<Array<Klass*>*>* a4 = ...;  it->push(&a4);    => MSOPointerArrayRef
295   // Array<Annotation*>*    a5 = ...;  it->push(&a5);    => MSOPointerArrayRef
296   //
297   // Note that the following will fail to compile (to prevent you from adding new fields
298   // into the MetaspaceObj subtypes that cannot be properly copied by CDS):
299   //
300   // MemoryPool*            p  = ...;  it->push(&p);     => MemoryPool is not a subclass of MetaspaceObj
301   // Array<MemoryPool*>*    a6 = ...;  it->push(&a6);    => MemoryPool is not a subclass of MetaspaceObj
302   // Array<int*>*           a7 = ...;  it->push(&a7);    => int       is not a subclass of MetaspaceObj
303 
304   template <typename T>
305   void push(T** mpp, Writability w = _default) {
306     static_assert(std::is_base_of<MetaspaceObj, T>::value, "Do not push pointers of arbitrary types");
307     push_with_ref<MSORef<T>>(mpp, w);
308   }
309 
310   template <typename T, ENABLE_IF(!std::is_base_of<MetaspaceObj, T>::value)>
311   void push(Array<T>** mpp, Writability w = _default) {
312     push_with_ref<OtherArrayRef<T>>(mpp, w);
313   }
314 
315   template <typename T, ENABLE_IF(std::is_base_of<MetaspaceObj, T>::value)>
316   void push(Array<T>** mpp, Writability w = _default) {
317     push_with_ref<MSOArrayRef<T>>(mpp, w);
318   }
319 
320   template <typename T>
321   void push(Array<T*>** mpp, Writability w = _default) {
322     static_assert(std::is_base_of<MetaspaceObj, T>::value, "Do not push Arrays of arbitrary pointer types");
323     push_with_ref<MSOPointerArrayRef<T>>(mpp, w);
324   }
325 };
326 
327 // This is a special MetaspaceClosure that visits each unique MetaspaceObj once.
328 class UniqueMetaspaceClosure : public MetaspaceClosure {
329   static const int INITIAL_TABLE_SIZE = 15889;
330   static const int MAX_TABLE_SIZE     = 1000000;
331 
332   // Do not override. Returns true if we are discovering ref->obj() for the first time.
333   virtual bool do_ref(Ref* ref, bool read_only);
334 
335 public:
336   // Gets called the first time we discover an object.
337   virtual bool do_unique_ref(Ref* ref, bool read_only) = 0;
338   UniqueMetaspaceClosure() : _has_been_visited(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE) {}
339 
340 private:
341   ResizeableResourceHashtable<address, bool, AnyObj::C_HEAP,
342                               mtClassShared> _has_been_visited;
343 };
344 
345 #endif // SHARE_MEMORY_METASPACECLOSURE_HPP