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_GC_SHENANDOAH_SHENANDOAHVERIFIER_HPP
  25 #define SHARE_GC_SHENANDOAH_SHENANDOAHVERIFIER_HPP
  26 
  27 #include "gc/shared/markBitMap.hpp"
  28 #include "memory/allocation.hpp"
  29 #include "oops/oopsHierarchy.hpp"
  30 #include "utilities/stack.hpp"
  31 
  32 class ShenandoahHeap;
  33 
  34 #ifdef _WINDOWS
  35 #pragma warning( disable : 4522 )
  36 #endif
  37 
  38 class ShenandoahVerifierTask {
  39 public:
  40   ShenandoahVerifierTask(oop o = NULL, int idx = 0): _obj(o) { }
  41   ShenandoahVerifierTask(oop o, size_t idx): _obj(o) { }
  42   ShenandoahVerifierTask(const ShenandoahVerifierTask& t): _obj(t._obj) { }
  43 
  44   ShenandoahVerifierTask& operator =(const ShenandoahVerifierTask& t) {
  45     _obj = t._obj;
  46     return *this;
  47   }
  48   volatile ShenandoahVerifierTask&
  49   operator =(const volatile ShenandoahVerifierTask& t) volatile {
  50     (void)const_cast<oop&>(_obj = t._obj);
  51     return *this;
  52   }
  53 
  54   inline oop obj()  const { return _obj; }
  55 
  56 private:
  57   oop _obj;
  58 };
  59 
  60 typedef Stack<ShenandoahVerifierTask, mtGC> ShenandoahVerifierStack;
  61 typedef volatile juint ShenandoahLivenessData;
  62 
  63 class ShenandoahVerifier : public CHeapObj<mtGC> {
  64 private:
  65   ShenandoahHeap* _heap;
  66   MarkBitMap* _verification_bit_map;
  67 public:
  68   typedef enum {
  69     // Disable marked objects verification.
  70     _verify_marked_disable,
  71 
  72     // Objects should be marked in "next" bitmap.
  73     _verify_marked_incomplete,
  74 
  75     // Objects should be marked in "complete" bitmap.
  76     _verify_marked_complete,
  77   } VerifyMarked;
  78 
  79   typedef enum {
  80     // Disable forwarded objects verification.
  81     _verify_forwarded_disable,
  82 
  83     // Objects should not have forwardees.
  84     _verify_forwarded_none,
  85 
  86     // Objects may have forwardees.
  87     _verify_forwarded_allow,
  88   } VerifyForwarded;
  89 
  90   typedef enum {
  91     // Disable collection set verification.
  92     _verify_cset_disable,
  93 
  94     // Should have no references to cset.
  95     _verify_cset_none,
  96 
  97     // May have references to cset, all should be forwarded.
  98     // Note: Allowing non-forwarded references to cset is equivalent
  99     // to _verify_cset_disable.
 100     _verify_cset_forwarded,
 101   } VerifyCollectionSet;
 102 
 103   typedef enum {
 104     // Disable liveness verification
 105     _verify_liveness_disable,
 106 
 107     // All objects should belong to live regions
 108     _verify_liveness_conservative,
 109 
 110     // All objects should belong to live regions,
 111     // and liveness data should be accurate
 112     _verify_liveness_complete,
 113   } VerifyLiveness;
 114 
 115   typedef enum {
 116     // Disable region verification
 117     _verify_regions_disable,
 118 
 119     // No trash regions allowed
 120     _verify_regions_notrash,
 121 
 122     // No collection set regions allowed
 123     _verify_regions_nocset,
 124 
 125     // No trash and no cset regions allowed
 126     _verify_regions_notrash_nocset,
 127   } VerifyRegions;
 128 
 129   typedef enum {
 130     // Disable gc-state verification
 131     _verify_gcstate_disable,
 132 
 133     // Nothing is in progress, no forwarded objects
 134     _verify_gcstate_stable,
 135 
 136     // Nothing is in progress, some objects are forwarded
 137     _verify_gcstate_forwarded,
 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_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_GC_SHENANDOAH_SHENANDOAHVERIFIER_HPP