1 /*
   2  * Copyright (c) 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 #include "precompiled.hpp"
  25 #include "classfile/classLoaderData.hpp"
  26 #include "gc/z/zAddress.hpp"
  27 #include "gc/z/zHeap.inline.hpp"
  28 #include "gc/z/zOop.hpp"
  29 #include "gc/z/zResurrection.hpp"
  30 #include "gc/z/zRootsIterator.hpp"
  31 #include "gc/z/zStat.hpp"
  32 #include "gc/z/zVerify.hpp"
  33 #include "memory/iterator.inline.hpp"
  34 #include "oops/oop.hpp"
  35 
  36 #define ZBAD_OOP_ARG(o, p)   "Bad oop " PTR_FORMAT " found at " PTR_FORMAT, p2i(o), p2i(p)
  37 
  38 static void verify_oop(oop* p) {
  39   const oop o = RawAccess<>::oop_load(p);
  40   if (o != NULL) {
  41     const uintptr_t addr = ZOop::to_address(o);
  42     guarantee(ZAddress::is_good(addr), ZBAD_OOP_ARG(o, p));
  43     guarantee(oopDesc::is_oop(ZOop::from_address(addr)), ZBAD_OOP_ARG(o, p));
  44   }
  45 }
  46 
  47 static void verify_possibly_weak_oop(oop* p) {
  48   const oop o = RawAccess<>::oop_load(p);
  49   if (o != NULL) {
  50     const uintptr_t addr = ZOop::to_address(o);
  51     guarantee(ZAddress::is_good(addr) || ZAddress::is_finalizable_good(addr), ZBAD_OOP_ARG(o, p));
  52     guarantee(oopDesc::is_oop(ZOop::from_address(ZAddress::good(addr))), ZBAD_OOP_ARG(o, p));
  53   }
  54 }
  55 
  56 class ZVerifyRootClosure : public ZRootsIteratorClosure {
  57 public:
  58   virtual void do_oop(oop* p) {
  59     verify_oop(p);
  60   }
  61 
  62   virtual void do_oop(narrowOop*) {
  63     ShouldNotReachHere();
  64   }
  65 };
  66 
  67 class ZVerifyOopClosure : public ClaimMetadataVisitingOopIterateClosure, public ZRootsIteratorClosure  {
  68 private:
  69   const bool _verify_weaks;
  70 
  71 public:
  72   ZVerifyOopClosure(bool verify_weaks) :
  73       ClaimMetadataVisitingOopIterateClosure(ClassLoaderData::_claim_other),
  74       _verify_weaks(verify_weaks) {}
  75 
  76   virtual void do_oop(oop* p) {
  77     if (_verify_weaks) {
  78       verify_possibly_weak_oop(p);
  79     } else {
  80       // We should never encounter finalizable oops through strong
  81       // paths. This assumes we have only visited strong roots.
  82       verify_oop(p);
  83     }
  84   }
  85 
  86   virtual void do_oop(narrowOop* p) {
  87     ShouldNotReachHere();
  88   }
  89 
  90   virtual ReferenceIterationMode reference_iteration_mode() {
  91     return _verify_weaks ? DO_FIELDS : DO_FIELDS_EXCEPT_REFERENT;
  92   }
  93 
  94 #ifdef ASSERT
  95   // Verification handled by the closure itself
  96   virtual bool should_verify_oops() {
  97     return false;
  98   }
  99 #endif
 100 };
 101 
 102 template <typename RootsIterator>
 103 void ZVerify::roots() {
 104   assert(SafepointSynchronize::is_at_safepoint(), "Must be at a safepoint");
 105   assert(!ZResurrection::is_blocked(), "Invalid phase");
 106 
 107   if (ZVerifyRoots) {
 108     ZVerifyRootClosure cl;
 109     RootsIterator iter;
 110     iter.oops_do(&cl);
 111   }
 112 }
 113 
 114 void ZVerify::roots_strong() {
 115   roots<ZRootsIterator>();
 116 }
 117 
 118 void ZVerify::roots_weak() {
 119   roots<ZWeakRootsIterator>();
 120 }
 121 
 122 void ZVerify::roots_concurrent_strong() {
 123   roots<ZConcurrentRootsIteratorClaimNone>();
 124 }
 125 
 126 void ZVerify::roots_concurrent_weak() {
 127   roots<ZConcurrentWeakRootsIterator>();
 128 }
 129 
 130 void ZVerify::roots(bool verify_weaks) {
 131   roots_strong();
 132   roots_concurrent_strong();
 133   if (verify_weaks) {
 134     roots_weak();
 135     roots_concurrent_weak();
 136   }
 137 }
 138 
 139 void ZVerify::objects(bool verify_weaks) {
 140   assert(SafepointSynchronize::is_at_safepoint(), "Must be at a safepoint");
 141   assert(ZGlobalPhase == ZPhaseMarkCompleted, "Invalid phase");
 142   assert(!ZResurrection::is_blocked(), "Invalid phase");
 143 
 144   if (ZVerifyObjects) {
 145     ZVerifyOopClosure cl(verify_weaks);
 146     ObjectToOopClosure object_cl(&cl);
 147     ZHeap::heap()->object_iterate(&object_cl, verify_weaks);
 148   }
 149 }
 150 
 151 void ZVerify::roots_and_objects(bool verify_weaks) {
 152   roots(verify_weaks);
 153   objects(verify_weaks);
 154 }
 155 
 156 void ZVerify::before_zoperation() {
 157   // Verify strong roots
 158   ZStatTimerDisable _disable;
 159   roots_strong();
 160 }
 161 
 162 void ZVerify::after_mark() {
 163   // Verify all strong roots and strong references
 164   ZStatTimerDisable _disable;
 165   roots_and_objects(false /* verify_weaks */);
 166 }
 167 
 168 void ZVerify::after_weak_processing() {
 169   // Verify all roots and all references
 170   ZStatTimerDisable _disable;
 171   roots_and_objects(true /* verify_weaks */);
 172 }