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