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
25 #include "precompiled.hpp"
26 #include "gc/shared/tlab_globals.hpp"
27 #include "gc/shenandoah/shenandoahAsserts.hpp"
28 #include "gc/shenandoah/shenandoahForwarding.inline.hpp"
29 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
30 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
31 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
32 #include "gc/shenandoah/shenandoahRootProcessor.hpp"
33 #include "gc/shenandoah/shenandoahTaskqueue.inline.hpp"
34 #include "gc/shenandoah/shenandoahUtils.hpp"
35 #include "gc/shenandoah/shenandoahVerifier.hpp"
36 #include "memory/allocation.hpp"
37 #include "memory/iterator.inline.hpp"
38 #include "memory/resourceArea.hpp"
39 #include "oops/compressedOops.inline.hpp"
40 #include "runtime/atomic.hpp"
41 #include "runtime/orderAccess.hpp"
42 #include "utilities/align.hpp"
43
44 // Avoid name collision on verify_oop (defined in macroAssembler_arm.hpp)
45 #ifdef verify_oop
46 #undef verify_oop
47 #endif
48
49 static bool is_instance_ref_klass(Klass* k) {
50 return k->is_instance_klass() && InstanceKlass::cast(k)->reference_type() != REF_NONE;
51 }
80 _interior_loc(NULL),
81 _loc(NULL) {
82 if (options._verify_marked == ShenandoahVerifier::_verify_marked_complete_except_references ||
83 options._verify_marked == ShenandoahVerifier::_verify_marked_disable) {
84 set_ref_discoverer_internal(new ShenandoahIgnoreReferenceDiscoverer());
85 }
86 }
87
88 private:
89 void check(ShenandoahAsserts::SafeLevel level, oop obj, bool test, const char* label) {
90 if (!test) {
91 ShenandoahAsserts::print_failure(level, obj, _interior_loc, _loc, _phase, label, __FILE__, __LINE__);
92 }
93 }
94
95 template <class T>
96 void do_oop_work(T* p) {
97 T o = RawAccess<>::oop_load(p);
98 if (!CompressedOops::is_null(o)) {
99 oop obj = CompressedOops::decode_not_null(o);
100 if (is_instance_ref_klass(obj->klass())) {
101 obj = ShenandoahForwarding::get_forwardee(obj);
102 }
103 // Single threaded verification can use faster non-atomic stack and bitmap
104 // methods.
105 //
106 // For performance reasons, only fully verify non-marked field values.
107 // We are here when the host object for *p is already marked.
108
109 if (_map->par_mark(obj)) {
110 verify_oop_at(p, obj);
111 _stack->push(ShenandoahVerifierTask(obj));
112 }
113 }
114 }
115
116 void verify_oop(oop obj) {
117 // Perform consistency checks with gradually decreasing safety level. This guarantees
118 // that failure report would not try to touch something that was not yet verified to be
119 // safe to process.
120
121 check(ShenandoahAsserts::_safe_unknown, obj, _heap->is_in(obj),
122 "oop must be in heap");
123 check(ShenandoahAsserts::_safe_unknown, obj, is_object_aligned(obj),
124 "oop must be aligned");
125
126 ShenandoahHeapRegion *obj_reg = _heap->heap_region_containing(obj);
127 Klass* obj_klass = obj->klass_or_null();
128
129 // Verify that obj is not in dead space:
130 {
131 // Do this before touching obj->size()
132 check(ShenandoahAsserts::_safe_unknown, obj, obj_klass != NULL,
133 "Object klass pointer should not be NULL");
134 check(ShenandoahAsserts::_safe_unknown, obj, Metaspace::contains(obj_klass),
135 "Object klass pointer must go to metaspace");
136
137 HeapWord *obj_addr = cast_from_oop<HeapWord*>(obj);
138 check(ShenandoahAsserts::_safe_unknown, obj, obj_addr < obj_reg->top(),
139 "Object start should be within the region");
140
141 if (!obj_reg->is_humongous()) {
142 check(ShenandoahAsserts::_safe_unknown, obj, (obj_addr + obj->size()) <= obj_reg->top(),
143 "Object end should be within the region");
144 } else {
145 size_t humongous_start = obj_reg->index();
146 size_t humongous_end = humongous_start + (obj->size() >> ShenandoahHeapRegion::region_size_words_shift());
147 for (size_t idx = humongous_start + 1; idx < humongous_end; idx++) {
148 check(ShenandoahAsserts::_safe_unknown, obj, _heap->get_region(idx)->is_humongous_continuation(),
149 "Humongous object is in continuation that fits it");
150 }
151 }
152
153 // ------------ obj is safe at this point --------------
154
155 check(ShenandoahAsserts::_safe_oop, obj, obj_reg->is_active(),
156 "Object should be in active region");
157
158 switch (_options._verify_liveness) {
159 case ShenandoahVerifier::_verify_liveness_disable:
160 // skip
161 break;
162 case ShenandoahVerifier::_verify_liveness_complete:
163 Atomic::add(&_ld[obj_reg->index()], (uint) obj->size(), memory_order_relaxed);
164 // fallthrough for fast failure for un-live regions:
165 case ShenandoahVerifier::_verify_liveness_conservative:
166 check(ShenandoahAsserts::_safe_oop, obj, obj_reg->has_live(),
167 "Object must belong to region with live data");
168 break;
169 default:
170 assert(false, "Unhandled liveness verification");
171 }
172 }
173
174 oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj);
175
176 ShenandoahHeapRegion* fwd_reg = NULL;
177
178 if (obj != fwd) {
179 check(ShenandoahAsserts::_safe_oop, obj, _heap->is_in(fwd),
180 "Forwardee must be in heap");
181 check(ShenandoahAsserts::_safe_oop, obj, !CompressedOops::is_null(fwd),
182 "Forwardee is set");
183 check(ShenandoahAsserts::_safe_oop, obj, is_object_aligned(fwd),
184 "Forwardee must be aligned");
185
186 // Do this before touching fwd->size()
187 Klass* fwd_klass = fwd->klass_or_null();
188 check(ShenandoahAsserts::_safe_oop, obj, fwd_klass != NULL,
189 "Forwardee klass pointer should not be NULL");
190 check(ShenandoahAsserts::_safe_oop, obj, Metaspace::contains(fwd_klass),
191 "Forwardee klass pointer must go to metaspace");
192 check(ShenandoahAsserts::_safe_oop, obj, obj_klass == fwd_klass,
193 "Forwardee klass pointer must go to metaspace");
194
195 fwd_reg = _heap->heap_region_containing(fwd);
196
197 // Verify that forwardee is not in the dead space:
198 check(ShenandoahAsserts::_safe_oop, obj, !fwd_reg->is_humongous(),
199 "Should have no humongous forwardees");
200
201 HeapWord *fwd_addr = cast_from_oop<HeapWord *>(fwd);
202 check(ShenandoahAsserts::_safe_oop, obj, fwd_addr < fwd_reg->top(),
203 "Forwardee start should be within the region");
204 check(ShenandoahAsserts::_safe_oop, obj, (fwd_addr + fwd->size()) <= fwd_reg->top(),
205 "Forwardee end should be within the region");
206
207 oop fwd2 = ShenandoahForwarding::get_forwardee_raw_unchecked(fwd);
208 check(ShenandoahAsserts::_safe_oop, obj, (fwd == fwd2),
209 "Double forwarding");
210 } else {
211 fwd_reg = obj_reg;
212 }
213
214 // ------------ obj and fwd are safe at this point --------------
215
216 switch (_options._verify_marked) {
217 case ShenandoahVerifier::_verify_marked_disable:
218 // skip
219 break;
220 case ShenandoahVerifier::_verify_marked_incomplete:
221 check(ShenandoahAsserts::_safe_all, obj, _heap->marking_context()->is_marked(obj),
222 "Must be marked in incomplete bitmap");
223 break;
224 case ShenandoahVerifier::_verify_marked_complete:
287 }
288
289 /**
290 * Verify object without known interior reference.
291 * Useful when picking up the object at known offset in heap,
292 * but without knowing what objects reference it.
293 * @param obj verified object
294 */
295 void verify_oop_standalone(oop obj) {
296 _interior_loc = NULL;
297 verify_oop(obj);
298 _interior_loc = NULL;
299 }
300
301 /**
302 * Verify oop fields from this object.
303 * @param obj host object for verified fields
304 */
305 void verify_oops_from(oop obj) {
306 _loc = obj;
307 obj->oop_iterate(this);
308 _loc = NULL;
309 }
310
311 virtual void do_oop(oop* p) { do_oop_work(p); }
312 virtual void do_oop(narrowOop* p) { do_oop_work(p); }
313 };
314
315 class ShenandoahCalculateRegionStatsClosure : public ShenandoahHeapRegionClosure {
316 private:
317 size_t _used, _committed, _garbage;
318 public:
319 ShenandoahCalculateRegionStatsClosure() : _used(0), _committed(0), _garbage(0) {};
320
321 void heap_region_do(ShenandoahHeapRegion* r) {
322 _used += r->used();
323 _garbage += r->garbage();
324 _committed += r->is_committed() ? ShenandoahHeapRegion::region_size_bytes() : 0;
325 }
326
327 size_t used() { return _used; }
567 HeapWord* addr = tams;
568
569 while (addr < limit) {
570 verify_and_follow(addr, stack, cl, &processed);
571 addr += cast_to_oop(addr)->size();
572 }
573 }
574
575 Atomic::add(&_processed, processed, memory_order_relaxed);
576 }
577
578 void verify_and_follow(HeapWord *addr, ShenandoahVerifierStack &stack, ShenandoahVerifyOopClosure &cl, size_t *processed) {
579 if (!_bitmap->par_mark(addr)) return;
580
581 // Verify the object itself:
582 oop obj = cast_to_oop(addr);
583 cl.verify_oop_standalone(obj);
584
585 // Verify everything reachable from that object too, hopefully realizing
586 // everything was already marked, and never touching further:
587 if (!is_instance_ref_klass(obj->klass())) {
588 cl.verify_oops_from(obj);
589 (*processed)++;
590 }
591 while (!stack.is_empty()) {
592 ShenandoahVerifierTask task = stack.pop();
593 cl.verify_oops_from(task.obj());
594 (*processed)++;
595 }
596 }
597 };
598
599 class VerifyThreadGCState : public ThreadClosure {
600 private:
601 const char* const _label;
602 char const _expected;
603
604 public:
605 VerifyThreadGCState(const char* label, char expected) : _label(label), _expected(expected) {}
606 void do_thread(Thread* t) {
607 char actual = ShenandoahThreadLocalData::gc_state(t);
|
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
25 #include "precompiled.hpp"
26 #include "gc/shared/tlab_globals.hpp"
27 #include "gc/shenandoah/shenandoahAsserts.hpp"
28 #include "gc/shenandoah/shenandoahForwarding.inline.hpp"
29 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
30 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
31 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
32 #include "gc/shenandoah/shenandoahObjectUtils.inline.hpp"
33 #include "gc/shenandoah/shenandoahRootProcessor.hpp"
34 #include "gc/shenandoah/shenandoahTaskqueue.inline.hpp"
35 #include "gc/shenandoah/shenandoahUtils.hpp"
36 #include "gc/shenandoah/shenandoahVerifier.hpp"
37 #include "memory/allocation.hpp"
38 #include "memory/iterator.inline.hpp"
39 #include "memory/resourceArea.hpp"
40 #include "oops/compressedOops.inline.hpp"
41 #include "runtime/atomic.hpp"
42 #include "runtime/orderAccess.hpp"
43 #include "utilities/align.hpp"
44
45 // Avoid name collision on verify_oop (defined in macroAssembler_arm.hpp)
46 #ifdef verify_oop
47 #undef verify_oop
48 #endif
49
50 static bool is_instance_ref_klass(Klass* k) {
51 return k->is_instance_klass() && InstanceKlass::cast(k)->reference_type() != REF_NONE;
52 }
81 _interior_loc(NULL),
82 _loc(NULL) {
83 if (options._verify_marked == ShenandoahVerifier::_verify_marked_complete_except_references ||
84 options._verify_marked == ShenandoahVerifier::_verify_marked_disable) {
85 set_ref_discoverer_internal(new ShenandoahIgnoreReferenceDiscoverer());
86 }
87 }
88
89 private:
90 void check(ShenandoahAsserts::SafeLevel level, oop obj, bool test, const char* label) {
91 if (!test) {
92 ShenandoahAsserts::print_failure(level, obj, _interior_loc, _loc, _phase, label, __FILE__, __LINE__);
93 }
94 }
95
96 template <class T>
97 void do_oop_work(T* p) {
98 T o = RawAccess<>::oop_load(p);
99 if (!CompressedOops::is_null(o)) {
100 oop obj = CompressedOops::decode_not_null(o);
101 if (is_instance_ref_klass(ShenandoahObjectUtils::klass(obj))) {
102 obj = ShenandoahForwarding::get_forwardee(obj);
103 }
104 // Single threaded verification can use faster non-atomic stack and bitmap
105 // methods.
106 //
107 // For performance reasons, only fully verify non-marked field values.
108 // We are here when the host object for *p is already marked.
109
110 if (_map->par_mark(obj)) {
111 verify_oop_at(p, obj);
112 _stack->push(ShenandoahVerifierTask(obj));
113 }
114 }
115 }
116
117 void verify_oop(oop obj) {
118 // Perform consistency checks with gradually decreasing safety level. This guarantees
119 // that failure report would not try to touch something that was not yet verified to be
120 // safe to process.
121
122 check(ShenandoahAsserts::_safe_unknown, obj, _heap->is_in(obj),
123 "oop must be in heap");
124 check(ShenandoahAsserts::_safe_unknown, obj, is_object_aligned(obj),
125 "oop must be aligned");
126
127 ShenandoahHeapRegion *obj_reg = _heap->heap_region_containing(obj);
128 Klass* obj_klass = ShenandoahObjectUtils::klass(obj);
129
130 // Verify that obj is not in dead space:
131 {
132 // Do this before touching obj->size()
133 check(ShenandoahAsserts::_safe_unknown, obj, obj_klass != NULL,
134 "Object klass pointer should not be NULL");
135 check(ShenandoahAsserts::_safe_unknown, obj, Metaspace::contains(obj_klass),
136 "Object klass pointer must go to metaspace");
137
138 HeapWord *obj_addr = cast_from_oop<HeapWord*>(obj);
139 check(ShenandoahAsserts::_safe_unknown, obj, obj_addr < obj_reg->top(),
140 "Object start should be within the region");
141
142 if (!obj_reg->is_humongous()) {
143 check(ShenandoahAsserts::_safe_unknown, obj, (obj_addr + ShenandoahObjectUtils::size(obj)) <= obj_reg->top(),
144 "Object end should be within the region");
145 } else {
146 size_t humongous_start = obj_reg->index();
147 size_t humongous_end = humongous_start + (ShenandoahObjectUtils::size(obj) >> ShenandoahHeapRegion::region_size_words_shift());
148 for (size_t idx = humongous_start + 1; idx < humongous_end; idx++) {
149 check(ShenandoahAsserts::_safe_unknown, obj, _heap->get_region(idx)->is_humongous_continuation(),
150 "Humongous object is in continuation that fits it");
151 }
152 }
153
154 // ------------ obj is safe at this point --------------
155
156 check(ShenandoahAsserts::_safe_oop, obj, obj_reg->is_active(),
157 "Object should be in active region");
158
159 switch (_options._verify_liveness) {
160 case ShenandoahVerifier::_verify_liveness_disable:
161 // skip
162 break;
163 case ShenandoahVerifier::_verify_liveness_complete:
164 Atomic::add(&_ld[obj_reg->index()], (uint) ShenandoahObjectUtils::size(obj), memory_order_relaxed);
165 // fallthrough for fast failure for un-live regions:
166 case ShenandoahVerifier::_verify_liveness_conservative:
167 check(ShenandoahAsserts::_safe_oop, obj, obj_reg->has_live(),
168 "Object must belong to region with live data");
169 break;
170 default:
171 assert(false, "Unhandled liveness verification");
172 }
173 }
174
175 oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj);
176
177 ShenandoahHeapRegion* fwd_reg = NULL;
178
179 if (obj != fwd) {
180 check(ShenandoahAsserts::_safe_oop, obj, _heap->is_in(fwd),
181 "Forwardee must be in heap");
182 check(ShenandoahAsserts::_safe_oop, obj, !CompressedOops::is_null(fwd),
183 "Forwardee is set");
184 check(ShenandoahAsserts::_safe_oop, obj, is_object_aligned(fwd),
185 "Forwardee must be aligned");
186
187 // Do this before touching fwd->size()
188 Klass* fwd_klass = fwd->klass_or_null();
189 check(ShenandoahAsserts::_safe_oop, obj, fwd_klass != NULL,
190 "Forwardee klass pointer should not be NULL");
191 check(ShenandoahAsserts::_safe_oop, obj, Metaspace::contains(fwd_klass),
192 "Forwardee klass pointer must go to metaspace");
193 check(ShenandoahAsserts::_safe_oop, obj, obj_klass == fwd_klass,
194 "Forwardee klass pointer must go to metaspace");
195
196 fwd_reg = _heap->heap_region_containing(fwd);
197
198 // Verify that forwardee is not in the dead space:
199 check(ShenandoahAsserts::_safe_oop, obj, !fwd_reg->is_humongous(),
200 "Should have no humongous forwardees");
201
202 HeapWord *fwd_addr = cast_from_oop<HeapWord *>(fwd);
203 check(ShenandoahAsserts::_safe_oop, obj, fwd_addr < fwd_reg->top(),
204 "Forwardee start should be within the region");
205 check(ShenandoahAsserts::_safe_oop, obj, (fwd_addr + ShenandoahObjectUtils::size(fwd)) <= fwd_reg->top(),
206 "Forwardee end should be within the region");
207
208 oop fwd2 = ShenandoahForwarding::get_forwardee_raw_unchecked(fwd);
209 check(ShenandoahAsserts::_safe_oop, obj, (fwd == fwd2),
210 "Double forwarding");
211 } else {
212 fwd_reg = obj_reg;
213 }
214
215 // ------------ obj and fwd are safe at this point --------------
216
217 switch (_options._verify_marked) {
218 case ShenandoahVerifier::_verify_marked_disable:
219 // skip
220 break;
221 case ShenandoahVerifier::_verify_marked_incomplete:
222 check(ShenandoahAsserts::_safe_all, obj, _heap->marking_context()->is_marked(obj),
223 "Must be marked in incomplete bitmap");
224 break;
225 case ShenandoahVerifier::_verify_marked_complete:
288 }
289
290 /**
291 * Verify object without known interior reference.
292 * Useful when picking up the object at known offset in heap,
293 * but without knowing what objects reference it.
294 * @param obj verified object
295 */
296 void verify_oop_standalone(oop obj) {
297 _interior_loc = NULL;
298 verify_oop(obj);
299 _interior_loc = NULL;
300 }
301
302 /**
303 * Verify oop fields from this object.
304 * @param obj host object for verified fields
305 */
306 void verify_oops_from(oop obj) {
307 _loc = obj;
308 Klass* klass = ShenandoahObjectUtils::klass(obj);
309 obj->oop_iterate_backwards(this, klass);
310 _loc = NULL;
311 }
312
313 virtual void do_oop(oop* p) { do_oop_work(p); }
314 virtual void do_oop(narrowOop* p) { do_oop_work(p); }
315 };
316
317 class ShenandoahCalculateRegionStatsClosure : public ShenandoahHeapRegionClosure {
318 private:
319 size_t _used, _committed, _garbage;
320 public:
321 ShenandoahCalculateRegionStatsClosure() : _used(0), _committed(0), _garbage(0) {};
322
323 void heap_region_do(ShenandoahHeapRegion* r) {
324 _used += r->used();
325 _garbage += r->garbage();
326 _committed += r->is_committed() ? ShenandoahHeapRegion::region_size_bytes() : 0;
327 }
328
329 size_t used() { return _used; }
569 HeapWord* addr = tams;
570
571 while (addr < limit) {
572 verify_and_follow(addr, stack, cl, &processed);
573 addr += cast_to_oop(addr)->size();
574 }
575 }
576
577 Atomic::add(&_processed, processed, memory_order_relaxed);
578 }
579
580 void verify_and_follow(HeapWord *addr, ShenandoahVerifierStack &stack, ShenandoahVerifyOopClosure &cl, size_t *processed) {
581 if (!_bitmap->par_mark(addr)) return;
582
583 // Verify the object itself:
584 oop obj = cast_to_oop(addr);
585 cl.verify_oop_standalone(obj);
586
587 // Verify everything reachable from that object too, hopefully realizing
588 // everything was already marked, and never touching further:
589 if (!is_instance_ref_klass(ShenandoahObjectUtils::klass(obj))) {
590 cl.verify_oops_from(obj);
591 (*processed)++;
592 }
593 while (!stack.is_empty()) {
594 ShenandoahVerifierTask task = stack.pop();
595 cl.verify_oops_from(task.obj());
596 (*processed)++;
597 }
598 }
599 };
600
601 class VerifyThreadGCState : public ThreadClosure {
602 private:
603 const char* const _label;
604 char const _expected;
605
606 public:
607 VerifyThreadGCState(const char* label, char expected) : _label(label), _expected(expected) {}
608 void do_thread(Thread* t) {
609 char actual = ShenandoahThreadLocalData::gc_state(t);
|