1 /*
  2  * Copyright (c) 2017, 2021, 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   enum SpecialRef {
 82     _method_entry_ref
 83   };
 84 
 85   // class MetaspaceClosure::Ref --
 86   //
 87   // MetaspaceClosure can be viewed as a very simple type of copying garbage
 88   // collector. For it to function properly, it requires each subclass of
 89   // MetaspaceObj to provide two methods:
 90   //
 91   //  size_t size();                                 -- to determine how much data to copy
 92   //  void metaspace_pointers_do(MetaspaceClosure*); -- to locate all the embedded pointers
 93   //
 94   // Calling these methods would be trivial if these two were virtual methods.
 95   // However, to save space, MetaspaceObj has NO vtable. The vtable is introduced
 96   // only in the Metadata class.
 97   //
 98   // To work around the lack of a vtable, we use the Ref class with templates
 99   // (see MSORef, OtherArrayRef, MSOArrayRef, and MSOPointerArrayRef)
100   // so that we can statically discover the type of a object. The use of Ref
101   // depends on the fact that:
102   //
103   // [1] We don't use polymorphic pointers for MetaspaceObj's that are not subclasses
104   //     of Metadata. I.e., we don't do this:
105   //     class Klass {
106   //         MetaspaceObj *_obj;
107   //         Array<int>* foo() { return (Array<int>*)_obj; }
108   //         Symbol*     bar() { return (Symbol*)    _obj; }
109   //
110   // [2] All Array<T> dimensions are statically declared.
111   class Ref : public CHeapObj<mtMetaspace> {
112     Writability _writability;
113     bool _keep_after_pushing;
114     Ref* _next;
115     void* _user_data;
116     NONCOPYABLE(Ref);
117 
118   protected:
119     virtual void** mpp() const = 0;
120     Ref(Writability w) : _writability(w), _keep_after_pushing(false), _next(NULL), _user_data(NULL) {}
121   public:
122     virtual bool not_null() const = 0;
123     virtual int size() const = 0;
124     virtual void metaspace_pointers_do(MetaspaceClosure *it) const = 0;
125     virtual void metaspace_pointers_do_at(MetaspaceClosure *it, address new_loc) const = 0;
126     virtual MetaspaceObj::Type msotype() const = 0;
127     virtual bool is_read_only_by_default() const = 0;
128     virtual ~Ref() {}
129 
130     address obj() const {
131       return *addr();
132     }
133 
134     address* addr() const {
135       return (address*)mpp();
136     }
137 
138     void update(address new_loc) const;
139 
140     Writability writability() const { return _writability; };
141     void set_keep_after_pushing()   { _keep_after_pushing = true; }
142     bool keep_after_pushing()       { return _keep_after_pushing; }
143     void set_user_data(void* data)  { _user_data = data; }
144     void* user_data()               { return _user_data; }
145     void set_next(Ref* n)           { _next = n; }
146     Ref* next() const               { return _next; }
147   };
148 
149 private:
150   // MSORef -- iterate an instance of MetaspaceObj
151   template <class T> class MSORef : public Ref {
152     T** _mpp;
153     T* dereference() const {
154       return *_mpp;
155     }
156   protected:
157     virtual void** mpp() const {
158       return (void**)_mpp;
159     }
160 
161   public:
162     MSORef(T** mpp, Writability w) : Ref(w), _mpp(mpp) {}
163 
164     virtual bool is_read_only_by_default() const { return T::is_read_only_by_default(); }
165     virtual bool not_null()                const { return dereference() != NULL; }
166     virtual int size()                     const { return dereference()->size(); }
167     virtual MetaspaceObj::Type msotype()   const { return dereference()->type(); }
168 
169     virtual void metaspace_pointers_do(MetaspaceClosure *it) const {
170       dereference()->metaspace_pointers_do(it);
171     }
172     virtual void metaspace_pointers_do_at(MetaspaceClosure *it, address new_loc) const {
173       ((T*)new_loc)->metaspace_pointers_do(it);
174     }
175   };
176 
177   // abstract base class for MSOArrayRef, MSOPointerArrayRef and OtherArrayRef
178   template <class T> class ArrayRef : public Ref {
179     Array<T>** _mpp;
180   protected:
181     Array<T>* dereference() const {
182       return *_mpp;
183     }
184     virtual void** mpp() const {
185       return (void**)_mpp;
186     }
187 
188     ArrayRef(Array<T>** mpp, Writability w) : Ref(w), _mpp(mpp) {}
189 
190     // all Arrays are read-only by default
191     virtual bool is_read_only_by_default() const { return true; }
192     virtual bool not_null()                const { return dereference() != NULL;  }
193     virtual int size()                     const { return dereference()->size(); }
194     virtual MetaspaceObj::Type msotype()   const { return MetaspaceObj::array_type(sizeof(T)); }
195   };
196 
197   // OtherArrayRef -- iterate an instance of Array<T>, where T is NOT a subtype of MetaspaceObj.
198   // T can be a primitive type, such as int, or a structure. However, we do not scan
199   // the fields inside T, so you should not embed any pointers inside T.
200   template <class T> class OtherArrayRef : public ArrayRef<T> {
201   public:
202     OtherArrayRef(Array<T>** mpp, Writability w) : ArrayRef<T>(mpp, w) {}
203 
204     virtual void metaspace_pointers_do(MetaspaceClosure *it) const {
205       Array<T>* array = ArrayRef<T>::dereference();
206       log_trace(cds)("Iter(OtherArray): %p [%d]", array, array->length());
207     }
208     virtual void metaspace_pointers_do_at(MetaspaceClosure *it, address new_loc) const {
209       Array<T>* array = (Array<T>*)new_loc;
210       log_trace(cds)("Iter(OtherArray): %p [%d]", array, array->length());
211     }
212   };
213 
214   // MSOArrayRef -- iterate an instance of Array<T>, where T is a subtype of MetaspaceObj.
215   // We recursively call T::metaspace_pointers_do() for each element in this array.
216   template <class T> class MSOArrayRef : public ArrayRef<T> {
217   public:
218     MSOArrayRef(Array<T>** mpp, Writability w) : ArrayRef<T>(mpp, w) {}
219 
220     virtual void metaspace_pointers_do(MetaspaceClosure *it) const {
221       metaspace_pointers_do_at_impl(it, ArrayRef<T>::dereference());
222     }
223     virtual void metaspace_pointers_do_at(MetaspaceClosure *it, address new_loc) const {
224       metaspace_pointers_do_at_impl(it, (Array<T>*)new_loc);
225     }
226   private:
227     void metaspace_pointers_do_at_impl(MetaspaceClosure *it, Array<T>* array) const {
228       log_trace(cds)("Iter(MSOArray): %p [%d]", array, array->length());
229       for (int i = 0; i < array->length(); i++) {
230         T* elm = array->adr_at(i);
231         elm->metaspace_pointers_do(it);
232       }
233     }
234   };
235 
236   // MSOPointerArrayRef -- iterate an instance of Array<T*>, where T is a subtype of MetaspaceObj.
237   // We recursively call MetaspaceClosure::push() for each pointer in this array.
238   template <class T> class MSOPointerArrayRef : public ArrayRef<T*> {
239   public:
240     MSOPointerArrayRef(Array<T*>** mpp, Writability w) : ArrayRef<T*>(mpp, w) {}
241 
242     virtual void metaspace_pointers_do(MetaspaceClosure *it) const {
243       metaspace_pointers_do_at_impl(it, ArrayRef<T*>::dereference());
244     }
245     virtual void metaspace_pointers_do_at(MetaspaceClosure *it, address new_loc) const {
246       metaspace_pointers_do_at_impl(it, (Array<T*>*)new_loc);
247     }
248   private:
249     void metaspace_pointers_do_at_impl(MetaspaceClosure *it, Array<T*>* array) const {
250       log_trace(cds)("Iter(MSOPointerArray): %p [%d]", array, array->length());
251       for (int i = 0; i < array->length(); i++) {
252         T** mpp = array->adr_at(i);
253         it->push(mpp);
254       }
255     }
256   };
257 
258   // Normally, chains of references like a->b->c->d are iterated recursively. However,
259   // if recursion is too deep, we save the Refs in _pending_refs, and push them later in
260   // MetaspaceClosure::finish(). This avoids overflowing the C stack.
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(NULL), _nest_level(0), _enclosing_ref(NULL) {}
271   ~MetaspaceClosure();
272 
273   void finish();
274 
275   // enclosing_ref() is used to compute the offset of a field in a C++ class. For example
276   // class Foo { intx scala; Bar* ptr; }
277   //    Foo *f = 0x100;
278   // when the f->ptr field is iterated with do_ref() on 64-bit platforms, we will have
279   //    do_ref(Ref* r) {
280   //       r->addr() == 0x108;                // == &f->ptr;
281   //       enclosing_ref()->obj() == 0x100;   // == foo
282   // So we know that we are iterating upon a field at offset 8 of the object at 0x100.
283   //
284   // Note that if we have stack overflow, do_pending_ref(r) will be called first and
285   // do_ref(r) will be called later, for the same r. In this case, enclosing_ref() is valid only
286   // when do_pending_ref(r) is called, and will return NULL when do_ref(r) is called.
287   Ref* enclosing_ref() const {
288     return _enclosing_ref;
289   }
290 
291   // This is called when a reference is placed in _pending_refs. Override this
292   // function if you're using enclosing_ref(). See notes above.
293   virtual void do_pending_ref(Ref* ref) {}
294 
295   // returns true if we want to keep iterating the pointers embedded inside <ref>
296   virtual bool do_ref(Ref* ref, bool read_only) = 0;
297 
298 private:
299   template <class REF_TYPE, typename T>
300   void push_with_ref(T** mpp, Writability w) {
301     push_impl(new REF_TYPE(mpp, w));
302   }
303 
304 public:
305   // When MetaspaceClosure::push(...) is called, pick the correct Ref subtype to handle it:
306   //
307   // MetaspaceClosure*      it = ...;
308   // Klass*                 o  = ...;  it->push(&o);     => MSORef
309   // Array<int>*            a1 = ...;  it->push(&a1);    => OtherArrayRef
310   // Array<Annotation>*     a2 = ...;  it->push(&a2);    => MSOArrayRef
311   // Array<Klass*>*         a3 = ...;  it->push(&a3);    => MSOPointerArrayRef
312   // Array<Array<Klass*>*>* a4 = ...;  it->push(&a4);    => MSOPointerArrayRef
313   // Array<Annotation*>*    a5 = ...;  it->push(&a5);    => MSOPointerArrayRef
314   //
315   // Note that the following will fail to compile (to prevent you from adding new fields
316   // into the MetaspaceObj subtypes that cannot be properly copied by CDS):
317   //
318   // Hashtable*             h  = ...;  it->push(&h);     => Hashtable is not a subclass of MetaspaceObj
319   // Array<Hashtable*>*     a6 = ...;  it->push(&a6);    => Hashtable is not a subclass of MetaspaceObj
320   // Array<int*>*           a7 = ...;  it->push(&a7);    => int       is not a subclass of MetaspaceObj
321 
322   template <typename T>
323   void push(T** mpp, Writability w = _default) {
324     static_assert(std::is_base_of<MetaspaceObj, T>::value, "Do not push pointers of arbitrary types");
325     push_with_ref<MSORef<T>>(mpp, w);
326   }
327 
328   template <typename T, ENABLE_IF(!std::is_base_of<MetaspaceObj, T>::value)>
329   void push(Array<T>** mpp, Writability w = _default) {
330     push_with_ref<OtherArrayRef<T>>(mpp, w);
331   }
332 
333   template <typename T, ENABLE_IF(std::is_base_of<MetaspaceObj, T>::value)>
334   void push(Array<T>** mpp, Writability w = _default) {
335     push_with_ref<MSOArrayRef<T>>(mpp, w);
336   }
337 
338   template <typename T>
339   void push(Array<T*>** mpp, Writability w = _default) {
340     static_assert(std::is_base_of<MetaspaceObj, T>::value, "Do not push Arrays of arbitrary pointer types");
341     push_with_ref<MSOPointerArrayRef<T>>(mpp, w);
342   }
343 
344 #if 0
345   // Enable this block if you're changing the push(...) methods, to test for types that should be
346   // disallowed. Each of the following "push" calls should result in a compile-time error.
347   void test_disallowed_types(MetaspaceClosure* it) {
348     Hashtable<bool, mtInternal>* h  = NULL;
349     it->push(&h);
350 
351     Array<Hashtable<bool, mtInternal>*>* a6 = NULL;
352     it->push(&a6);
353 
354     Array<int*>* a7 = NULL;
355     it->push(&a7);
356   }
357 #endif
358 
359   template <class T> void push_method_entry(T** mpp, intptr_t* p) {
360     Ref* ref = new MSORef<T>(mpp, _default);
361     push_special(_method_entry_ref, ref, (intptr_t*)p);
362     if (!ref->keep_after_pushing()) {
363       delete ref;
364     }
365   }
366 
367   // This is for tagging special pointers that are not a reference to MetaspaceObj. It's currently
368   // used to mark the method entry points in Method/ConstMethod.
369   virtual void push_special(SpecialRef type, Ref* obj, intptr_t* p) {
370     assert(type == _method_entry_ref, "only special type allowed for now");
371   }
372 };
373 
374 // This is a special MetaspaceClosure that visits each unique MetaspaceObj once.
375 class UniqueMetaspaceClosure : public MetaspaceClosure {
376   static const int INITIAL_TABLE_SIZE = 15889;
377   static const int MAX_TABLE_SIZE     = 1000000;
378 
379   // Do not override. Returns true if we are discovering ref->obj() for the first time.
380   virtual bool do_ref(Ref* ref, bool read_only);
381 
382 public:
383   // Gets called the first time we discover an object.
384   virtual bool do_unique_ref(Ref* ref, bool read_only) = 0;
385   UniqueMetaspaceClosure() : _has_been_visited(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE) {}
386 
387 private:
388   ResizeableResourceHashtable<address, bool, ResourceObj::C_HEAP,
389                               mtClassShared> _has_been_visited;
390 };
391 
392 #endif // SHARE_MEMORY_METASPACECLOSURE_HPP