1 /*
   2  * Copyright (c) 2017, Red Hat, Inc. and/or its affiliates.
   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 "memory/allocation.hpp"
  28 #include "utilities/stack.hpp"
  29 #include "gc/shared/markBitMap.hpp"
  30 
  31 class Thread;
  32 class ShenandoahHeapRegionSet;
  33 class ShenandoahHeap;
  34 class ShenandoahVerifyOopClosure;
  35 
  36 #ifdef _WINDOWS
  37 #pragma warning( disable : 4522 )
  38 #endif
  39 
  40 class ShenandoahVerifierTask {
  41 public:
  42   ShenandoahVerifierTask(oop o = NULL, int idx = 0): _obj(o) { }
  43   ShenandoahVerifierTask(oop o, size_t idx): _obj(o) { }
  44   ShenandoahVerifierTask(const ShenandoahVerifierTask& t): _obj(t._obj) { }
  45 
  46   ShenandoahVerifierTask& operator =(const ShenandoahVerifierTask& t) {
  47     _obj = t._obj;
  48     return *this;
  49   }
  50   volatile ShenandoahVerifierTask&
  51   operator =(const volatile ShenandoahVerifierTask& t) volatile {
  52     (void)const_cast<oop&>(_obj = t._obj);
  53     return *this;
  54   }
  55 
  56   inline oop obj()  const { return _obj; }
  57 
  58   DEBUG_ONLY(bool is_valid() const); // Tasks to be pushed/popped must be valid.
  59 
  60 private:
  61   oop _obj;
  62 };
  63 
  64 typedef FormatBuffer<8192> ShenandoahMessageBuffer;
  65 typedef Stack<ShenandoahVerifierTask, mtGC> ShenandoahVerifierStack;
  66 typedef volatile juint ShenandoahLivenessData;
  67 
  68 class ShenandoahVerifier : public CHeapObj<mtGC> {
  69 private:
  70   ShenandoahHeap* _heap;
  71   MarkBitMap* _verification_bit_map;
  72 public:
  73   typedef enum {
  74     // Disable matrix verification completely
  75     _verify_matrix_disable,
  76 
  77     // Conservative matrix verification: all connected objects should have matrix
  78     // connections. The verification is conservative, because it allows matrix
  79     // connection that do not have actual heap connections.
  80     _verify_matrix_conservative,
  81 
  82     // Precise matrix verification: all connected objects should have matrix connections,
  83     // *and* every matrix connection should have at least a pair a connected objects.
  84     // TODO: implement this, if needed
  85     _verify_matrix_precise,
  86   } VerifyMatrix;
  87 
  88   typedef enum {
  89     // Disable marked objects verification.
  90     _verify_marked_disable,
  91 
  92     // Objects should be marked
  93     _verify_marked,
  94 
  95   } VerifyMarked;
  96 
  97   typedef enum {
  98     // Disable forwarded objects verification.
  99     _verify_forwarded_disable,
 100 
 101     // Objects should not have forwardees.
 102     _verify_forwarded_none,
 103 
 104     // Objects may have forwardees.
 105     _verify_forwarded_allow,
 106   } VerifyForwarded;
 107 
 108   typedef enum {
 109     // Disable collection set verification.
 110     _verify_cset_disable,
 111 
 112     // Should have no references to cset.
 113     _verify_cset_none,
 114 
 115     // May have references to cset, all should be forwarded.
 116     // Note: Allowing non-forwarded references to cset is equivalent
 117     // to _verify_cset_disable.
 118     _verify_cset_forwarded,
 119   } VerifyCollectionSet;
 120 
 121   typedef enum {
 122     // Disable liveness verification
 123     _verify_liveness_disable,
 124 
 125     // All objects should belong to live regions
 126     _verify_liveness_conservative,
 127 
 128     // All objects should belong to live regions,
 129     // and liveness data should be accurate
 130     _verify_liveness_complete,
 131   } VerifyLiveness;
 132 
 133   typedef enum {
 134     // Disable region verification
 135     _verify_regions_disable,
 136 
 137     // No trash regions allowed
 138     _verify_regions_notrash,
 139 
 140     // No collection set regions allowed
 141     _verify_regions_nocset,
 142 
 143     // No trash and no cset regions allowed
 144     _verify_regions_notrash_nocset,
 145   } VerifyRegions;
 146 
 147   struct VerifyOptions {
 148     VerifyForwarded     _verify_forwarded;
 149     VerifyMarked        _verify_marked;
 150     VerifyMatrix        _verify_matrix;
 151     VerifyCollectionSet _verify_cset;
 152     VerifyLiveness      _verify_liveness;
 153     VerifyRegions       _verify_regions;
 154 
 155     VerifyOptions(VerifyForwarded verify_forwarded,
 156                   VerifyMarked verify_marked,
 157                   VerifyMatrix verify_matrix,
 158                   VerifyCollectionSet verify_collection_set,
 159                   VerifyLiveness verify_liveness,
 160                   VerifyRegions verify_regions) :
 161             _verify_forwarded(verify_forwarded), _verify_marked(verify_marked),
 162             _verify_matrix(verify_matrix), _verify_cset(verify_collection_set),
 163             _verify_liveness(verify_liveness), _verify_regions(verify_regions) {}
 164   };
 165 
 166 private:
 167   void verify_at_safepoint(const char *label,
 168                            VerifyForwarded forwarded,
 169                            VerifyMarked marked,
 170                            VerifyMatrix matrix,
 171                            VerifyCollectionSet cset,
 172                            VerifyLiveness liveness,
 173                            VerifyRegions regions);
 174 
 175 public:
 176   ShenandoahVerifier(ShenandoahHeap* heap, MarkBitMap* verification_bitmap) :
 177           _heap(heap), _verification_bit_map(verification_bitmap) {};
 178 
 179   void verify_before_concmark();
 180   void verify_after_concmark();
 181   void verify_before_evacuation();
 182   void verify_after_evacuation();
 183   void verify_before_updaterefs();
 184   void verify_after_updaterefs();
 185   void verify_before_fullgc();
 186   void verify_after_fullgc();
 187   void verify_before_partial();
 188   void verify_after_partial();
 189   void verify_generic(VerifyOption option);
 190 
 191   static void verify_oop(oop obj);
 192   static void verify_oop_fwdptr(oop obj, oop new_fwd);
 193 };
 194 
 195 #endif // SHARE_VM_GC_SHENANDOAH_SHENANDOAHVERIFIER_HPP