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