1 /*
   2  * Copyright (c) 2017, 2018, Red Hat, Inc. All rights reserved.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #ifndef SHARE_VM_GC_SHENANDOAH_SHENANDOAHVERIFIER_HPP
  25 #define SHARE_VM_GC_SHENANDOAH_SHENANDOAHVERIFIER_HPP
  26 
  27 #include "gc_implementation/shared/markBitMap.hpp"
  28 #include "gc_implementation/shenandoah/shenandoahRootVerifier.hpp"
  29 #include "oops/oopsHierarchy.hpp"
  30 #include "memory/allocation.hpp"
  31 #include "utilities/stack.hpp"
  32 
  33 class ShenandoahHeap;
  34 
  35 class ShenandoahVerifierTask {
  36 public:
  37   ShenandoahVerifierTask(oop o = NULL, int idx = 0): _obj(o) { }
  38   ShenandoahVerifierTask(oop o, size_t idx): _obj(o) { }
  39   ShenandoahVerifierTask(const ShenandoahVerifierTask& t): _obj(t._obj) { }
  40 
  41   ShenandoahVerifierTask& operator =(const ShenandoahVerifierTask& t) {
  42     _obj = t._obj;
  43     return *this;
  44   }
  45   volatile ShenandoahVerifierTask&
  46   operator =(const volatile ShenandoahVerifierTask& t) volatile {
  47     (void)const_cast<oop&>(_obj = t._obj);
  48     return *this;
  49   }
  50 
  51   inline oop obj()  const { return _obj; }
  52 
  53 private:
  54   oop _obj;
  55 };
  56 
  57 typedef Stack<ShenandoahVerifierTask, mtGC> ShenandoahVerifierStack;
  58 typedef volatile jint ShenandoahLivenessData;
  59 
  60 class ShenandoahVerifier : public CHeapObj<mtGC> {
  61 private:
  62   ShenandoahHeap* _heap;
  63   MarkBitMap* _verification_bit_map;
  64 public:
  65   typedef enum {
  66     // Disable marked objects verification.
  67     _verify_marked_disable,
  68 
  69     // Objects should be marked in "next" bitmap.
  70     _verify_marked_incomplete,
  71 
  72     // Objects should be marked in "complete" bitmap.
  73     _verify_marked_complete
  74   } VerifyMarked;
  75 
  76   typedef enum {
  77     // Disable forwarded objects verification.
  78     _verify_forwarded_disable,
  79 
  80     // Objects should not have forwardees.
  81     _verify_forwarded_none,
  82 
  83     // Objects may have forwardees.
  84     _verify_forwarded_allow
  85   } VerifyForwarded;
  86 
  87   typedef enum {
  88     // Disable collection set verification.
  89     _verify_cset_disable,
  90 
  91     // Should have no references to cset.
  92     _verify_cset_none,
  93 
  94     // May have references to cset, all should be forwarded.
  95     // Note: Allowing non-forwarded references to cset is equivalent
  96     // to _verify_cset_disable.
  97     _verify_cset_forwarded
  98   } VerifyCollectionSet;
  99 
 100   typedef enum {
 101     // Disable liveness verification
 102     _verify_liveness_disable,
 103 
 104     // All objects should belong to live regions
 105     _verify_liveness_conservative,
 106 
 107     // All objects should belong to live regions,
 108     // and liveness data should be accurate
 109     _verify_liveness_complete
 110   } VerifyLiveness;
 111 
 112   typedef enum {
 113     // Disable region verification
 114     _verify_regions_disable,
 115 
 116     // No trash regions allowed
 117     _verify_regions_notrash,
 118 
 119     // No collection set regions allowed
 120     _verify_regions_nocset,
 121 
 122     // No trash and no cset regions allowed
 123     _verify_regions_notrash_nocset
 124   } VerifyRegions;
 125 
 126   typedef enum {
 127     // Disable gc-state verification
 128     _verify_gcstate_disable,
 129 
 130     // Nothing is in progress, no forwarded objects
 131     _verify_gcstate_stable,
 132 
 133     // Nothing is in progress, some objects are forwarded
 134     _verify_gcstate_forwarded,
 135 
 136     // Evacuation is in progress, some objects are forwarded
 137     _verify_gcstate_evacuation
 138   } VerifyGCState;
 139 
 140   struct VerifyOptions {
 141     VerifyForwarded     _verify_forwarded;
 142     VerifyMarked        _verify_marked;
 143     VerifyCollectionSet _verify_cset;
 144     VerifyLiveness      _verify_liveness;
 145     VerifyRegions       _verify_regions;
 146     VerifyGCState       _verify_gcstate;
 147 
 148     VerifyOptions(VerifyForwarded verify_forwarded,
 149                   VerifyMarked verify_marked,
 150                   VerifyCollectionSet verify_collection_set,
 151                   VerifyLiveness verify_liveness,
 152                   VerifyRegions verify_regions,
 153                   VerifyGCState verify_gcstate) :
 154             _verify_forwarded(verify_forwarded), _verify_marked(verify_marked),
 155             _verify_cset(verify_collection_set),
 156             _verify_liveness(verify_liveness), _verify_regions(verify_regions),
 157             _verify_gcstate(verify_gcstate) {}
 158   };
 159 
 160 private:
 161   void verify_at_safepoint(const char *label,
 162                            VerifyForwarded forwarded,
 163                            VerifyMarked marked,
 164                            VerifyCollectionSet cset,
 165                            VerifyLiveness liveness,
 166                            VerifyRegions regions,
 167                            VerifyGCState gcstate);
 168 
 169 public:
 170   ShenandoahVerifier(ShenandoahHeap* heap, MarkBitMap* verification_bitmap) :
 171           _heap(heap), _verification_bit_map(verification_bitmap) {};
 172 
 173   void verify_before_concmark();
 174   void verify_after_concmark();
 175   void verify_before_evacuation();
 176   void verify_during_evacuation();
 177   void verify_after_evacuation();
 178   void verify_before_updaterefs();
 179   void verify_after_updaterefs();
 180   void verify_before_fullgc();
 181   void verify_after_fullgc();
 182   void verify_after_degenerated();
 183   void verify_generic(VerifyOption option);
 184 
 185   // Roots should only contain to-space oops
 186   void verify_roots_in_to_space();
 187   void verify_roots_no_forwarded();
 188   void verify_roots_no_forwarded_except(ShenandoahRootVerifier::RootTypes types);
 189 };
 190 
 191 #endif // SHARE_VM_GC_SHENANDOAH_SHENANDOAHVERIFIER_HPP