1 /*
  2  * Copyright (c) 1997, 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_ITERATOR_HPP
 26 #define SHARE_MEMORY_ITERATOR_HPP
 27 
 28 #include "memory/allocation.hpp"
 29 #include "memory/memRegion.hpp"
 30 #include "oops/oopsHierarchy.hpp"
 31 #include "utilities/bitMap.hpp"
 32 
 33 class CodeBlob;
 34 class nmethod;
 35 class ReferenceDiscoverer;
 36 class DataLayout;
 37 class KlassClosure;
 38 class ClassLoaderData;
 39 class Symbol;
 40 class Metadata;
 41 class Thread;
 42 
 43 // The following classes are C++ `closures` for iterating over objects, roots and spaces
 44 
 45 class Closure : public StackObj { };
 46 
 47 // Thread iterator
 48 class ThreadClosure {
 49  public:
 50   virtual void do_thread(Thread* thread) = 0;
 51 };
 52 
 53 // OopClosure is used for iterating through references to Java objects.
 54 class OopClosure : public Closure {
 55  public:
 56   virtual void do_oop(oop* o) = 0;
 57   virtual void do_oop(narrowOop* o) = 0;
 58 };
 59 
 60 class DoNothingClosure : public OopClosure {
 61  public:
 62   virtual void do_oop(oop* p)       {}
 63   virtual void do_oop(narrowOop* p) {}
 64 };
 65 extern DoNothingClosure do_nothing_cl;
 66 
 67 // OopIterateClosure adds extra code to be run during oop iterations.
 68 // This is needed by the GC and is extracted to a separate type to not
 69 // pollute the OopClosure interface.
 70 class OopIterateClosure : public OopClosure {
 71  private:
 72   ReferenceDiscoverer* _ref_discoverer;
 73 
 74  protected:
 75   OopIterateClosure(ReferenceDiscoverer* rd) : _ref_discoverer(rd) { }
 76   OopIterateClosure() : _ref_discoverer(NULL) { }
 77   ~OopIterateClosure() { }
 78 
 79   void set_ref_discoverer_internal(ReferenceDiscoverer* rd) { _ref_discoverer = rd; }
 80 
 81  public:
 82   ReferenceDiscoverer* ref_discoverer() const { return _ref_discoverer; }
 83 
 84   // Iteration of InstanceRefKlasses differ depending on the closure,
 85   // the below enum describes the different alternatives.
 86   enum ReferenceIterationMode {
 87     DO_DISCOVERY,                // Apply closure and discover references
 88     DO_DISCOVERED_AND_DISCOVERY, // Apply closure to discovered field and do discovery
 89     DO_FIELDS,                   // Apply closure to all fields
 90     DO_FIELDS_EXCEPT_REFERENT    // Apply closure to all fields except the referent field
 91   };
 92 
 93   // The default iteration mode is to do discovery.
 94   virtual ReferenceIterationMode reference_iteration_mode() { return DO_DISCOVERY; }
 95 
 96   // If the do_metadata functions return "true",
 97   // we invoke the following when running oop_iterate():
 98   //
 99   // 1) do_klass on the header klass pointer.
100   // 2) do_klass on the klass pointer in the mirrors.
101   // 3) do_cld   on the class loader data in class loaders.
102 
103   virtual bool do_metadata() = 0;
104   virtual void do_klass(Klass* k) = 0;
105   virtual void do_cld(ClassLoaderData* cld) = 0;
106   virtual void do_method(Method* m) = 0;
107   virtual void do_nmethod(nmethod* nm) = 0;
108 };
109 
110 // An OopIterateClosure that can be used when there's no need to visit the Metadata.
111 class BasicOopIterateClosure : public OopIterateClosure {
112 public:
113   BasicOopIterateClosure(ReferenceDiscoverer* rd = NULL) : OopIterateClosure(rd) {}
114 
115   virtual bool do_metadata() { return false; }
116   virtual void do_klass(Klass* k) { ShouldNotReachHere(); }
117   virtual void do_cld(ClassLoaderData* cld) { ShouldNotReachHere(); }
118   virtual void do_method(Method* m) { ShouldNotReachHere(); }
119   virtual void do_nmethod(nmethod* nm) { ShouldNotReachHere(); }
120 };
121 
122 enum class derived_pointer : intptr_t;
123 class DerivedOopClosure : public Closure {
124  public:
125   enum { SkipNull = true };
126   virtual void do_derived_oop(oop* base, derived_pointer* derived) = 0;
127 };
128 
129 class KlassClosure : public Closure {
130  public:
131   virtual void do_klass(Klass* k) = 0;
132 };
133 
134 class CLDClosure : public Closure {
135  public:
136   virtual void do_cld(ClassLoaderData* cld) = 0;
137 };
138 
139 class MetadataClosure : public Closure {
140  public:
141   virtual void do_metadata(Metadata* md) = 0;
142 };
143 
144 
145 class CLDToOopClosure : public CLDClosure {
146   OopClosure*       _oop_closure;
147   int               _cld_claim;
148 
149  public:
150   CLDToOopClosure(OopClosure* oop_closure,
151                   int cld_claim) :
152       _oop_closure(oop_closure),
153       _cld_claim(cld_claim) {}
154 
155   void do_cld(ClassLoaderData* cld);
156 };
157 
158 template <int claim>
159 class ClaimingCLDToOopClosure : public CLDToOopClosure {
160 public:
161   ClaimingCLDToOopClosure(OopClosure* cl) : CLDToOopClosure(cl, claim) {}
162 };
163 
164 class ClaimMetadataVisitingOopIterateClosure : public OopIterateClosure {
165  protected:
166   const int _claim;
167 
168  public:
169   ClaimMetadataVisitingOopIterateClosure(int claim, ReferenceDiscoverer* rd = NULL) :
170       OopIterateClosure(rd),
171       _claim(claim) { }
172 
173   virtual bool do_metadata() { return true; }
174   virtual void do_klass(Klass* k);
175   virtual void do_cld(ClassLoaderData* cld);
176   virtual void do_method(Method* m);
177   virtual void do_nmethod(nmethod* nm);
178 };
179 
180 // The base class for all concurrent marking closures,
181 // that participates in class unloading.
182 // It's used to proxy through the metadata to the oops defined in them.
183 class MetadataVisitingOopIterateClosure: public ClaimMetadataVisitingOopIterateClosure {
184  public:
185   MetadataVisitingOopIterateClosure(ReferenceDiscoverer* rd = NULL);
186 };
187 
188 // ObjectClosure is used for iterating through an object space
189 
190 class ObjectClosure : public Closure {
191  public:
192   // Called for each object.
193   virtual void do_object(oop obj) = 0;
194 };
195 
196 
197 class BoolObjectClosure : public Closure {
198  public:
199   virtual bool do_object_b(oop obj) = 0;
200 };
201 
202 class AlwaysTrueClosure: public BoolObjectClosure {
203  public:
204   bool do_object_b(oop p) { return true; }
205 };
206 
207 class AlwaysFalseClosure : public BoolObjectClosure {
208  public:
209   bool do_object_b(oop p) { return false; }
210 };
211 
212 // Applies an oop closure to all ref fields in objects iterated over in an
213 // object iteration.
214 class ObjectToOopClosure: public ObjectClosure {
215   OopIterateClosure* _cl;
216 public:
217   void do_object(oop obj);
218   ObjectToOopClosure(OopIterateClosure* cl) : _cl(cl) {}
219 };
220 
221 // SpaceClosure is used for iterating over spaces
222 
223 class Space;
224 class CompactibleSpace;
225 
226 class SpaceClosure : public StackObj {
227  public:
228   // Called for each space
229   virtual void do_space(Space* s) = 0;
230 };
231 
232 class CompactibleSpaceClosure : public StackObj {
233  public:
234   // Called for each compactible space
235   virtual void do_space(CompactibleSpace* s) = 0;
236 };
237 
238 
239 // CodeBlobClosure is used for iterating through code blobs
240 // in the code cache or on thread stacks
241 
242 class CodeBlobClosure : public Closure {
243  public:
244   // Called for each code blob.
245   virtual void do_code_blob(CodeBlob* cb) = 0;
246 };
247 
248 // Applies an oop closure to all ref fields in code blobs
249 // iterated over in an object iteration.
250 class CodeBlobToOopClosure : public CodeBlobClosure {
251  protected:
252   OopClosure* _cl;
253   bool _fix_relocations;
254   void do_nmethod(nmethod* nm);
255  public:
256   // If fix_relocations(), then cl must copy objects to their new location immediately to avoid
257   // patching nmethods with the old locations.
258   CodeBlobToOopClosure(OopClosure* cl, bool fix_relocations) : _cl(cl), _fix_relocations(fix_relocations) {}
259   virtual void do_code_blob(CodeBlob* cb);
260 
261   bool fix_relocations() const { return _fix_relocations; }
262   const static bool FixRelocations = true;
263 };
264 
265 class MarkingCodeBlobClosure : public CodeBlobToOopClosure {
266   bool _keepalive_nmethods;
267 
268  public:
269   MarkingCodeBlobClosure(OopClosure* cl, bool fix_relocations, bool keepalive_nmethods) :
270       CodeBlobToOopClosure(cl, fix_relocations),
271       _keepalive_nmethods(keepalive_nmethods) {}
272   // Called for each code blob, but at most once per unique blob.
273 
274   virtual void do_code_blob(CodeBlob* cb);
275 };
276 
277 class NMethodClosure : public Closure {
278  public:
279   virtual void do_nmethod(nmethod* n) = 0;
280 };
281 
282 class CodeBlobToNMethodClosure : public CodeBlobClosure {
283   NMethodClosure* const _nm_cl;
284 
285  public:
286   CodeBlobToNMethodClosure(NMethodClosure* nm_cl) : _nm_cl(nm_cl) {}
287 
288   virtual void do_code_blob(CodeBlob* cb);
289 };
290 
291 // MonitorClosure is used for iterating over monitors in the monitors cache
292 
293 class ObjectMonitor;
294 
295 class MonitorClosure : public StackObj {
296  public:
297   // called for each monitor in cache
298   virtual void do_monitor(ObjectMonitor* m) = 0;
299 };
300 
301 // A closure that is applied without any arguments.
302 class VoidClosure : public StackObj {
303  public:
304   virtual void do_void() = 0;
305 };
306 
307 
308 // YieldClosure is intended for use by iteration loops
309 // to incrementalize their work, allowing interleaving
310 // of an interruptable task so as to allow other
311 // threads to run (which may not otherwise be able to access
312 // exclusive resources, for instance). Additionally, the
313 // closure also allows for aborting an ongoing iteration
314 // by means of checking the return value from the polling
315 // call.
316 class YieldClosure : public StackObj {
317 public:
318  virtual bool should_return() = 0;
319 
320  // Yield on a fine-grain level. The check in case of not yielding should be very fast.
321  virtual bool should_return_fine_grain() { return false; }
322 };
323 
324 // Abstract closure for serializing data (read or write).
325 
326 class SerializeClosure : public Closure {
327 public:
328   // Return bool indicating whether closure implements read or write.
329   virtual bool reading() const = 0;
330 
331   // Read/write the void pointer pointed to by p.
332   virtual void do_ptr(void** p) = 0;
333 
334   // Read/write the 32-bit unsigned integer pointed to by p.
335   virtual void do_u4(u4* p) = 0;
336 
337   // Read/write the bool pointed to by p.
338   virtual void do_bool(bool* p) = 0;
339 
340   // Read/write the region specified.
341   virtual void do_region(u_char* start, size_t size) = 0;
342 
343   // Check/write the tag.  If reading, then compare the tag against
344   // the passed in value and fail is they don't match.  This allows
345   // for verification that sections of the serialized data are of the
346   // correct length.
347   virtual void do_tag(int tag) = 0;
348 
349   // Read/write the oop
350   virtual void do_oop(oop* o) = 0;
351 
352   bool writing() {
353     return !reading();
354   }
355 };
356 
357 class SymbolClosure : public StackObj {
358  public:
359   virtual void do_symbol(Symbol**) = 0;
360 };
361 
362 template <typename E>
363 class CompareClosure : public Closure {
364 public:
365     virtual int do_compare(const E&, const E&) = 0;
366 };
367 
368 // Dispatches to the non-virtual functions if OopClosureType has
369 // a concrete implementation, otherwise a virtual call is taken.
370 class Devirtualizer {
371  public:
372   template <typename OopClosureType, typename T> static void do_oop(OopClosureType* closure, T* p);
373   template <typename OopClosureType>             static void do_klass(OopClosureType* closure, Klass* k);
374   template <typename OopClosureType>             static void do_cld(OopClosureType* closure, ClassLoaderData* cld);
375   template <typename OopClosureType>             static bool do_metadata(OopClosureType* closure);
376   template <typename DerivedOopClosureType>      static void do_derived_oop(DerivedOopClosureType* closure, oop* base, derived_pointer* derived);
377   template <typename BitMapClosureType>          static bool do_bit(BitMapClosureType* closure, BitMap::idx_t index);
378 };
379 
380 class OopIteratorClosureDispatch {
381  public:
382   template <typename OopClosureType> static void oop_oop_iterate(OopClosureType* cl, oop obj, Klass* klass);
383   template <typename OopClosureType> static void oop_oop_iterate(OopClosureType* cl, oop obj, Klass* klass, MemRegion mr);
384   template <typename OopClosureType> static void oop_oop_iterate_backwards(OopClosureType* cl, oop obj, Klass* klass);
385 };
386 
387 #endif // SHARE_MEMORY_ITERATOR_HPP