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