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 "oops/oopsHierarchy.hpp"
  29 #include "memory/allocation.hpp"
  30 #include "utilities/stack.hpp"
  31 
  32 class ShenandoahHeap;
  33 
  34 class ShenandoahVerifierTask {
  35 public:
  36   ShenandoahVerifierTask(oop o = NULL, int idx = 0): _obj(o) { }
  37   ShenandoahVerifierTask(oop o, size_t idx): _obj(o) { }
  38   ShenandoahVerifierTask(const ShenandoahVerifierTask& t): _obj(t._obj) { }
  39 
  40   ShenandoahVerifierTask& operator =(const ShenandoahVerifierTask& t) {
  41     _obj = t._obj;
  42     return *this;
  43   }
  44   volatile ShenandoahVerifierTask&
  45   operator =(const volatile ShenandoahVerifierTask& t) volatile {
  46     (void)const_cast<oop&>(_obj = t._obj);
  47     return *this;
  48   }
  49 
  50   inline oop obj()  const { return _obj; }
  51 
  52 private:
  53   oop _obj;
  54 };
  55 
  56 typedef Stack<ShenandoahVerifierTask, mtGC> ShenandoahVerifierStack;
  57 typedef volatile jint ShenandoahLivenessData;
  58 
  59 class ShenandoahVerifier : public CHeapObj<mtGC> {
  60 private:
  61   ShenandoahHeap* _heap;
  62   MarkBitMap* _verification_bit_map;
  63 public:
  64   typedef enum {
  65     // Disable marked objects verification.
  66     _verify_marked_disable,
  67 
  68     // Objects should be marked in "next" bitmap.
  69     _verify_marked_incomplete,
  70 
  71     // Objects should be marked in "complete" bitmap.
  72     _verify_marked_complete
  73   } VerifyMarked;
  74 
  75   typedef enum {
  76     // Disable forwarded objects verification.
  77     _verify_forwarded_disable,
  78 
  79     // Objects should not have forwardees.
  80     _verify_forwarded_none,
  81 
  82     // Objects may have forwardees.
  83     _verify_forwarded_allow
  84   } VerifyForwarded;
  85 
  86   typedef enum {
  87     // Disable collection set verification.
  88     _verify_cset_disable,
  89 
  90     // Should have no references to cset.
  91     _verify_cset_none,
  92 
  93     // May have references to cset, all should be forwarded.
  94     // Note: Allowing non-forwarded references to cset is equivalent
  95     // to _verify_cset_disable.
  96     _verify_cset_forwarded
  97   } VerifyCollectionSet;
  98 
  99   typedef enum {
 100     // Disable liveness verification
 101     _verify_liveness_disable,
 102 
 103     // All objects should belong to live regions
 104     _verify_liveness_conservative,
 105 
 106     // All objects should belong to live regions,
 107     // and liveness data should be accurate
 108     _verify_liveness_complete
 109   } VerifyLiveness;
 110 
 111   typedef enum {
 112     // Disable region verification
 113     _verify_regions_disable,
 114 
 115     // No trash regions allowed
 116     _verify_regions_notrash,
 117 
 118     // No collection set regions allowed
 119     _verify_regions_nocset,
 120 
 121     // No trash and no cset regions allowed
 122     _verify_regions_notrash_nocset
 123   } VerifyRegions;
 124 
 125   typedef enum {
 126     // Disable gc-state verification
 127     _verify_gcstate_disable,
 128 
 129     // Nothing is in progress, no forwarded objects
 130     _verify_gcstate_stable,
 131 
 132     // Nothing is in progress, some objects are forwarded
 133     _verify_gcstate_forwarded,
 134 
 135     // Evacuation is in progress, some objects are forwarded
 136     _verify_gcstate_evacuation
 137   } VerifyGCState;
 138 
 139   struct VerifyOptions {
 140     VerifyForwarded     _verify_forwarded;
 141     VerifyMarked        _verify_marked;
 142     VerifyCollectionSet _verify_cset;
 143     VerifyLiveness      _verify_liveness;
 144     VerifyRegions       _verify_regions;
 145     VerifyGCState       _verify_gcstate;
 146 
 147     VerifyOptions(VerifyForwarded verify_forwarded,
 148                   VerifyMarked verify_marked,
 149                   VerifyCollectionSet verify_collection_set,
 150                   VerifyLiveness verify_liveness,
 151                   VerifyRegions verify_regions,
 152                   VerifyGCState verify_gcstate) :
 153             _verify_forwarded(verify_forwarded), _verify_marked(verify_marked),
 154             _verify_cset(verify_collection_set),
 155             _verify_liveness(verify_liveness), _verify_regions(verify_regions),
 156             _verify_gcstate(verify_gcstate) {}
 157   };
 158 
 159 private:
 160   void verify_at_safepoint(const char *label,
 161                            VerifyForwarded forwarded,
 162                            VerifyMarked marked,
 163                            VerifyCollectionSet cset,
 164                            VerifyLiveness liveness,
 165                            VerifyRegions regions,
 166                            VerifyGCState gcstate);
 167 
 168 public:
 169   ShenandoahVerifier(ShenandoahHeap* heap, MarkBitMap* verification_bitmap) :
 170           _heap(heap), _verification_bit_map(verification_bitmap) {};
 171 
 172   void verify_before_concmark();
 173   void verify_after_concmark();
 174   void verify_before_evacuation();
 175   void verify_during_evacuation();
 176   void verify_after_evacuation();
 177   void verify_before_updaterefs();
 178   void verify_after_updaterefs();
 179   void verify_before_fullgc();
 180   void verify_after_fullgc();
 181   void verify_before_traversal();
 182   void verify_after_traversal();
 183   void verify_after_degenerated();
 184   void verify_generic(VerifyOption option);
 185 };
 186 
 187 #endif // SHARE_VM_GC_SHENANDOAH_SHENANDOAHVERIFIER_HPP