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 "classfile/classLoaderDataGraph.hpp"
27 #include "gc/z/zAddress.hpp"
28 #include "gc/z/zHeap.inline.hpp"
29 #include "gc/z/zOop.hpp"
30 #include "gc/z/zResurrection.hpp"
31 #include "gc/z/zRootsIterator.hpp"
32 #include "gc/z/zStat.hpp"
33 #include "gc/z/zVerify.hpp"
34 #include "memory/allocation.hpp"
35 #include "memory/iterator.inline.hpp"
36 #include "oops/oop.inline.hpp"
37 
38 #define BAD_OOP_REPORT(addr)                                                \
39     "Bad oop " PTR_FORMAT " found at " PTR_FORMAT ", expected " PTR_FORMAT, \
40     addr, p2i(p), ZAddress::good(addr)
41 
42 class ZVerifyRootsClosure : public ZRootsIteratorClosure {
43 public:
do_oop(oop * p)44   virtual void do_oop(oop* p) {
45     uintptr_t value = ZOop::to_address(*p);
46 
47     if (value == 0) {
48       return;
49     }
50 
51     guarantee(!ZAddress::is_finalizable(value), BAD_OOP_REPORT(value));
52     guarantee(ZAddress::is_good(value), BAD_OOP_REPORT(value));
53     guarantee(oopDesc::is_oop(ZOop::from_address(value)), BAD_OOP_REPORT(value));
54   }
do_oop(narrowOop *)55   virtual void do_oop(narrowOop*) { ShouldNotReachHere(); }
56 };
57 
58 template <bool VisitReferents>
59 class ZVerifyOopClosure : public ClaimMetadataVisitingOopIterateClosure, public ZRootsIteratorClosure  {
60 public:
ZVerifyOopClosure()61   ZVerifyOopClosure() :
62       ClaimMetadataVisitingOopIterateClosure(ClassLoaderData::_claim_other) {}
63 
64   virtual void do_oop(oop* p);
do_oop(narrowOop * p)65   virtual void do_oop(narrowOop* p) { ShouldNotReachHere(); }
66 
reference_iteration_mode()67   virtual ReferenceIterationMode reference_iteration_mode() {
68     return VisitReferents ? DO_FIELDS : DO_FIELDS_EXCEPT_REFERENT;
69   }
70 
71 #ifdef ASSERT
72   // Verification handled by the closure itself
should_verify_oops()73   virtual bool should_verify_oops() {
74     return false;
75   }
76 #endif
77 };
78 
79 class ZVerifyObjectClosure : public ObjectClosure {
80 private:
81   bool _visit_referents;
82 
83 public:
ZVerifyObjectClosure(bool visit_referents)84   ZVerifyObjectClosure(bool visit_referents) : _visit_referents(visit_referents) {}
85   virtual void do_object(oop o);
86 };
87 
88 template <typename RootsIterator>
roots_impl()89 void ZVerify::roots_impl() {
90   if (ZVerifyRoots) {
91     ZVerifyRootsClosure cl;
92     RootsIterator iter;
93     iter.oops_do(&cl);
94   }
95 }
96 
roots_strong()97 void ZVerify::roots_strong() {
98   roots_impl<ZRootsIterator>();
99 }
100 
101 class ZVerifyConcurrentRootsIterator : public ZConcurrentRootsIterator {
102 public:
ZVerifyConcurrentRootsIterator()103   ZVerifyConcurrentRootsIterator()
104       : ZConcurrentRootsIterator(ClassLoaderData::_claim_none) {}
105 };
106 
roots_concurrent()107 void ZVerify::roots_concurrent() {
108   roots_impl<ZVerifyConcurrentRootsIterator>();
109 }
110 
roots_weak()111 void ZVerify::roots_weak() {
112   assert(!ZResurrection::is_blocked(), "Invalid phase");
113 
114   roots_impl<ZWeakRootsIterator>();
115 }
116 
roots(bool verify_weaks)117 void ZVerify::roots(bool verify_weaks) {
118   roots_strong();
119   roots_concurrent();
120   if (verify_weaks) {
121     roots_weak();
122     roots_concurrent_weak();
123   }
124 }
125 
objects(bool verify_weaks)126 void ZVerify::objects(bool verify_weaks) {
127   if (ZVerifyObjects) {
128     ZVerifyObjectClosure cl(verify_weaks);
129     ZHeap::heap()->object_iterate(&cl, verify_weaks);
130   }
131 }
132 
roots_concurrent_weak()133 void ZVerify::roots_concurrent_weak() {
134   assert(!ZResurrection::is_blocked(), "Invalid phase");
135 
136   roots_impl<ZConcurrentWeakRootsIterator>();
137 }
138 
roots_and_objects(bool verify_weaks)139 void ZVerify::roots_and_objects(bool verify_weaks) {
140   ZStatTimerDisable  _disable;
141 
142   roots(verify_weaks);
143   objects(verify_weaks);
144 }
145 
after_mark()146 void ZVerify::after_mark() {
147   // Only verify strong roots and references.
148   roots_and_objects(false /* verify_weaks */);
149 }
150 
after_weak_processing()151 void ZVerify::after_weak_processing() {
152   // Also verify weaks - all should have been processed at this point.
153   roots_and_objects(true /* verify_weaks */);
154 }
155 
156 template <bool VisitReferents>
do_oop(oop * p)157 void ZVerifyOopClosure<VisitReferents>::do_oop(oop* p) {
158   guarantee(SafepointSynchronize::is_at_safepoint(), "Must be at a safepoint");
159   guarantee(ZGlobalPhase == ZPhaseMarkCompleted, "Invalid phase");
160   guarantee(!ZResurrection::is_blocked(), "Invalid phase");
161 
162   const oop o = RawAccess<>::oop_load(p);
163   if (o == NULL) {
164     return;
165   }
166 
167   const uintptr_t addr = ZOop::to_address(o);
168   if (VisitReferents) {
169     guarantee(ZAddress::is_good(addr) || ZAddress::is_finalizable_good(addr), BAD_OOP_REPORT(addr));
170   } else {
171     // Should not encounter finalizable oops through strong-only paths. Assumes only strong roots are visited.
172     guarantee(ZAddress::is_good(addr), BAD_OOP_REPORT(addr));
173   }
174 
175   const uintptr_t good_addr = ZAddress::good(addr);
176   guarantee(oopDesc::is_oop(ZOop::from_address(good_addr)), BAD_OOP_REPORT(addr));
177 }
178 
do_object(oop o)179 void ZVerifyObjectClosure::do_object(oop o) {
180   if (_visit_referents) {
181     ZVerifyOopClosure<true /* VisitReferents */> cl;
182     o->oop_iterate((OopIterateClosure*)&cl);
183   } else {
184     ZVerifyOopClosure<false /* VisitReferents */> cl;
185     o->oop_iterate(&cl);
186   }
187 }
188