1 /*
2 * Copyright (c) 2017, 2021, Red Hat, Inc. 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
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 "runtime/threads.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 }
53
54 class ShenandoahVerifyOopClosure : public BasicOopIterateClosure {
55 private:
56 const char* _phase;
57 ShenandoahVerifier::VerifyOptions _options;
58 ShenandoahVerifierStack* _stack;
59 ShenandoahHeap* _heap;
60 MarkBitMap* _map;
61 ShenandoahLivenessData* _ld;
62 void* _interior_loc;
63 oop _loc;
64 ReferenceIterationMode _ref_mode;
65
66 public:
67 ShenandoahVerifyOopClosure(ShenandoahVerifierStack* stack, MarkBitMap* map, ShenandoahLivenessData* ld,
68 const char* phase, ShenandoahVerifier::VerifyOptions options) :
69 _phase(phase),
70 _options(options),
71 _stack(stack),
72 _heap(ShenandoahHeap::heap()),
73 _map(map),
74 _ld(ld),
75 _interior_loc(nullptr),
76 _loc(nullptr) {
77 if (options._verify_marked == ShenandoahVerifier::_verify_marked_complete_except_references ||
78 options._verify_marked == ShenandoahVerifier::_verify_marked_disable) {
79 // Unknown status for Reference.referent field. Do not touch it, it might be dead.
80 // Normally, barriers would prevent us from seeing the dead referents, but verifier
81 // runs with barriers disabled.
82 _ref_mode = DO_FIELDS_EXCEPT_REFERENT;
83 } else {
84 // Otherwise do all fields.
85 _ref_mode = DO_FIELDS;
86 }
87 }
88
89 ReferenceIterationMode reference_iteration_mode() override {
90 return _ref_mode;
91 }
92
93 private:
94 void check(ShenandoahAsserts::SafeLevel level, oop obj, bool test, const char* label) {
95 if (!test) {
96 ShenandoahAsserts::print_failure(level, obj, _interior_loc, _loc, _phase, label, __FILE__, __LINE__);
97 }
98 }
99
100 template <class T>
101 void do_oop_work(T* p) {
102 T o = RawAccess<>::oop_load(p);
103 if (!CompressedOops::is_null(o)) {
104 oop obj = CompressedOops::decode_not_null(o);
105 if (is_instance_ref_klass(obj->klass())) {
106 obj = ShenandoahForwarding::get_forwardee(obj);
107 }
108 // Single threaded verification can use faster non-atomic stack and bitmap
109 // methods.
110 //
111 // For performance reasons, only fully verify non-marked field values.
112 // We are here when the host object for *p is already marked.
113
114 if (_map->par_mark(obj)) {
115 verify_oop_at(p, obj);
116 _stack->push(ShenandoahVerifierTask(obj));
117 }
118 }
119 }
120
121 void verify_oop(oop obj) {
122 // Perform consistency checks with gradually decreasing safety level. This guarantees
123 // that failure report would not try to touch something that was not yet verified to be
124 // safe to process.
125
126 check(ShenandoahAsserts::_safe_unknown, obj, _heap->is_in_reserved(obj),
127 "oop must be in heap bounds");
128 check(ShenandoahAsserts::_safe_unknown, obj, is_object_aligned(obj),
129 "oop must be aligned");
130
131 ShenandoahHeapRegion *obj_reg = _heap->heap_region_containing(obj);
132 Klass* obj_klass = obj->klass_or_null();
133
134 // Verify that obj is not in dead space:
135 {
136 // Do this before touching obj->size()
137 check(ShenandoahAsserts::_safe_unknown, obj, obj_klass != nullptr,
138 "Object klass pointer should not be null");
139 check(ShenandoahAsserts::_safe_unknown, obj, Metaspace::contains(obj_klass),
140 "Object klass pointer must go to metaspace");
151 size_t humongous_end = humongous_start + (obj->size() >> ShenandoahHeapRegion::region_size_words_shift());
152 for (size_t idx = humongous_start + 1; idx < humongous_end; idx++) {
153 check(ShenandoahAsserts::_safe_unknown, obj, _heap->get_region(idx)->is_humongous_continuation(),
154 "Humongous object is in continuation that fits it");
155 }
156 }
157
158 // ------------ obj is safe at this point --------------
159
160 check(ShenandoahAsserts::_safe_oop, obj, obj_reg->is_active(),
161 "Object should be in active region");
162
163 switch (_options._verify_liveness) {
164 case ShenandoahVerifier::_verify_liveness_disable:
165 // skip
166 break;
167 case ShenandoahVerifier::_verify_liveness_complete:
168 Atomic::add(&_ld[obj_reg->index()], (uint) obj->size(), memory_order_relaxed);
169 // fallthrough for fast failure for un-live regions:
170 case ShenandoahVerifier::_verify_liveness_conservative:
171 check(ShenandoahAsserts::_safe_oop, obj, obj_reg->has_live(),
172 "Object must belong to region with live data");
173 break;
174 default:
175 assert(false, "Unhandled liveness verification");
176 }
177 }
178
179 oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj);
180
181 ShenandoahHeapRegion* fwd_reg = nullptr;
182
183 if (obj != fwd) {
184 check(ShenandoahAsserts::_safe_oop, obj, _heap->is_in_reserved(fwd),
185 "Forwardee must be in heap bounds");
186 check(ShenandoahAsserts::_safe_oop, obj, !CompressedOops::is_null(fwd),
187 "Forwardee is set");
188 check(ShenandoahAsserts::_safe_oop, obj, is_object_aligned(fwd),
189 "Forwardee must be aligned");
190
191 // Do this before touching fwd->size()
192 Klass* fwd_klass = fwd->klass_or_null();
202 check(ShenandoahAsserts::_safe_oop, obj, fwd_reg->is_active(),
203 "Forwardee should be in active region");
204
205 // Verify that forwardee is not in the dead space:
206 check(ShenandoahAsserts::_safe_oop, obj, !fwd_reg->is_humongous(),
207 "Should have no humongous forwardees");
208
209 HeapWord *fwd_addr = cast_from_oop<HeapWord *>(fwd);
210 check(ShenandoahAsserts::_safe_oop, obj, fwd_addr < fwd_reg->top(),
211 "Forwardee start should be within the region");
212 check(ShenandoahAsserts::_safe_oop, obj, (fwd_addr + fwd->size()) <= fwd_reg->top(),
213 "Forwardee end should be within the region");
214
215 oop fwd2 = ShenandoahForwarding::get_forwardee_raw_unchecked(fwd);
216 check(ShenandoahAsserts::_safe_oop, obj, (fwd == fwd2),
217 "Double forwarding");
218 } else {
219 fwd_reg = obj_reg;
220 }
221
222 // ------------ obj and fwd are safe at this point --------------
223
224 switch (_options._verify_marked) {
225 case ShenandoahVerifier::_verify_marked_disable:
226 // skip
227 break;
228 case ShenandoahVerifier::_verify_marked_incomplete:
229 check(ShenandoahAsserts::_safe_all, obj, _heap->marking_context()->is_marked(obj),
230 "Must be marked in incomplete bitmap");
231 break;
232 case ShenandoahVerifier::_verify_marked_complete:
233 check(ShenandoahAsserts::_safe_all, obj, _heap->complete_marking_context()->is_marked(obj),
234 "Must be marked in complete bitmap");
235 break;
236 case ShenandoahVerifier::_verify_marked_complete_except_references:
237 check(ShenandoahAsserts::_safe_all, obj, _heap->complete_marking_context()->is_marked(obj),
238 "Must be marked in complete bitmap, except j.l.r.Reference referents");
239 break;
240 default:
241 assert(false, "Unhandled mark verification");
242 }
243
244 switch (_options._verify_forwarded) {
245 case ShenandoahVerifier::_verify_forwarded_disable:
246 // skip
247 break;
248 case ShenandoahVerifier::_verify_forwarded_none: {
249 check(ShenandoahAsserts::_safe_all, obj, (obj == fwd),
250 "Should not be forwarded");
251 break;
252 }
253 case ShenandoahVerifier::_verify_forwarded_allow: {
254 if (obj != fwd) {
255 check(ShenandoahAsserts::_safe_all, obj, obj_reg != fwd_reg,
256 "Forwardee should be in another region");
257 }
299 * Useful when picking up the object at known offset in heap,
300 * but without knowing what objects reference it.
301 * @param obj verified object
302 */
303 void verify_oop_standalone(oop obj) {
304 _interior_loc = nullptr;
305 verify_oop(obj);
306 _interior_loc = nullptr;
307 }
308
309 /**
310 * Verify oop fields from this object.
311 * @param obj host object for verified fields
312 */
313 void verify_oops_from(oop obj) {
314 _loc = obj;
315 obj->oop_iterate(this);
316 _loc = nullptr;
317 }
318
319 virtual void do_oop(oop* p) override { do_oop_work(p); }
320 virtual void do_oop(narrowOop* p) override { do_oop_work(p); }
321 };
322
323 class ShenandoahCalculateRegionStatsClosure : public ShenandoahHeapRegionClosure {
324 private:
325 size_t _used, _committed, _garbage;
326 public:
327 ShenandoahCalculateRegionStatsClosure() : _used(0), _committed(0), _garbage(0) {};
328
329 void heap_region_do(ShenandoahHeapRegion* r) {
330 _used += r->used();
331 _garbage += r->garbage();
332 _committed += r->is_committed() ? ShenandoahHeapRegion::region_size_bytes() : 0;
333 }
334
335 size_t used() { return _used; }
336 size_t committed() { return _committed; }
337 size_t garbage() { return _garbage; }
338 };
339
340 class ShenandoahVerifyHeapRegionClosure : public ShenandoahHeapRegionClosure {
341 private:
342 ShenandoahHeap* _heap;
343 const char* _phase;
344 ShenandoahVerifier::VerifyRegions _regions;
345 public:
346 ShenandoahVerifyHeapRegionClosure(const char* phase, ShenandoahVerifier::VerifyRegions regions) :
347 _heap(ShenandoahHeap::heap()),
348 _phase(phase),
349 _regions(regions) {};
350
351 void print_failure(ShenandoahHeapRegion* r, const char* label) {
352 ResourceMark rm;
353
354 ShenandoahMessageBuffer msg("Shenandoah verification failed; %s: %s\n\n", _phase, label);
355
356 stringStream ss;
357 r->print_on(&ss);
358 msg.append("%s", ss.as_string());
359
360 report_vm_error(__FILE__, __LINE__, msg.buffer());
361 }
362
363 void verify(ShenandoahHeapRegion* r, bool test, const char* msg) {
364 if (!test) {
365 print_failure(r, msg);
366 }
367 }
368
369 void heap_region_do(ShenandoahHeapRegion* r) {
370 switch (_regions) {
371 case ShenandoahVerifier::_verify_regions_disable:
372 break;
373 case ShenandoahVerifier::_verify_regions_notrash:
374 verify(r, !r->is_trash(),
375 "Should not have trash regions");
376 break;
377 case ShenandoahVerifier::_verify_regions_nocset:
378 verify(r, !r->is_cset(),
379 "Should not have cset regions");
380 break;
381 case ShenandoahVerifier::_verify_regions_notrash_nocset:
382 verify(r, !r->is_trash(),
383 "Should not have trash regions");
384 verify(r, !r->is_cset(),
385 "Should not have cset regions");
386 break;
387 default:
388 ShouldNotReachHere();
389 }
401 "Complete TAMS should not be larger than top");
402
403 verify(r, r->get_live_data_bytes() <= r->capacity(),
404 "Live data cannot be larger than capacity");
405
406 verify(r, r->garbage() <= r->capacity(),
407 "Garbage cannot be larger than capacity");
408
409 verify(r, r->used() <= r->capacity(),
410 "Used cannot be larger than capacity");
411
412 verify(r, r->get_shared_allocs() <= r->capacity(),
413 "Shared alloc count should not be larger than capacity");
414
415 verify(r, r->get_tlab_allocs() <= r->capacity(),
416 "TLAB alloc count should not be larger than capacity");
417
418 verify(r, r->get_gclab_allocs() <= r->capacity(),
419 "GCLAB alloc count should not be larger than capacity");
420
421 verify(r, r->get_shared_allocs() + r->get_tlab_allocs() + r->get_gclab_allocs() == r->used(),
422 "Accurate accounting: shared + TLAB + GCLAB = used");
423
424 verify(r, !r->is_empty() || !r->has_live(),
425 "Empty regions should not have live data");
426
427 verify(r, r->is_cset() == _heap->collection_set()->is_in(r),
428 "Transitional: region flags and collection set agree");
429 }
430 };
431
432 class ShenandoahVerifierReachableTask : public WorkerTask {
433 private:
434 const char* _label;
435 ShenandoahVerifier::VerifyOptions _options;
436 ShenandoahHeap* _heap;
437 ShenandoahLivenessData* _ld;
438 MarkBitMap* _bitmap;
439 volatile size_t _processed;
440
441 public:
442 ShenandoahVerifierReachableTask(MarkBitMap* bitmap,
443 ShenandoahLivenessData* ld,
444 const char* label,
445 ShenandoahVerifier::VerifyOptions options) :
446 WorkerTask("Shenandoah Verifier Reachable Objects"),
447 _label(label),
448 _options(options),
449 _heap(ShenandoahHeap::heap()),
450 _ld(ld),
451 _bitmap(bitmap),
452 _processed(0) {};
453
454 size_t processed() {
455 return _processed;
456 }
457
458 virtual void work(uint worker_id) {
459 ResourceMark rm;
460 ShenandoahVerifierStack stack;
461
462 // On level 2, we need to only check the roots once.
463 // On level 3, we want to check the roots, and seed the local stack.
464 // It is a lesser evil to accept multiple root scans at level 3, because
465 // extended parallelism would buy us out.
466 if (((ShenandoahVerifyLevel == 2) && (worker_id == 0))
467 || (ShenandoahVerifyLevel >= 3)) {
468 ShenandoahVerifyOopClosure cl(&stack, _bitmap, _ld,
469 ShenandoahMessageBuffer("%s, Roots", _label),
470 _options);
471 if (_heap->unload_classes()) {
472 ShenandoahRootVerifier::strong_roots_do(&cl);
473 } else {
474 ShenandoahRootVerifier::roots_do(&cl);
475 }
476 }
477
478 size_t processed = 0;
479
480 if (ShenandoahVerifyLevel >= 3) {
481 ShenandoahVerifyOopClosure cl(&stack, _bitmap, _ld,
482 ShenandoahMessageBuffer("%s, Reachable", _label),
483 _options);
484 while (!stack.is_empty()) {
485 processed++;
486 ShenandoahVerifierTask task = stack.pop();
487 cl.verify_oops_from(task.obj());
488 }
489 }
490
491 Atomic::add(&_processed, processed, memory_order_relaxed);
492 }
493 };
494
495 class ShenandoahVerifierMarkedRegionTask : public WorkerTask {
496 private:
497 const char* _label;
498 ShenandoahVerifier::VerifyOptions _options;
499 ShenandoahHeap *_heap;
500 MarkBitMap* _bitmap;
501 ShenandoahLivenessData* _ld;
502 volatile size_t _claimed;
503 volatile size_t _processed;
504
505 public:
506 ShenandoahVerifierMarkedRegionTask(MarkBitMap* bitmap,
507 ShenandoahLivenessData* ld,
508 const char* label,
509 ShenandoahVerifier::VerifyOptions options) :
510 WorkerTask("Shenandoah Verifier Marked Objects"),
511 _label(label),
512 _options(options),
513 _heap(ShenandoahHeap::heap()),
514 _bitmap(bitmap),
515 _ld(ld),
516 _claimed(0),
517 _processed(0) {};
518
519 size_t processed() {
520 return Atomic::load(&_processed);
521 }
522
523 virtual void work(uint worker_id) {
524 ShenandoahVerifierStack stack;
525 ShenandoahVerifyOopClosure cl(&stack, _bitmap, _ld,
526 ShenandoahMessageBuffer("%s, Marked", _label),
527 _options);
528
529 while (true) {
530 size_t v = Atomic::fetch_then_add(&_claimed, 1u, memory_order_relaxed);
531 if (v < _heap->num_regions()) {
532 ShenandoahHeapRegion* r = _heap->get_region(v);
533 if (!r->is_humongous() && !r->is_trash()) {
534 work_regular(r, stack, cl);
535 } else if (r->is_humongous_start()) {
536 work_humongous(r, stack, cl);
537 }
538 } else {
539 break;
540 }
541 }
542 }
543
544 virtual void work_humongous(ShenandoahHeapRegion *r, ShenandoahVerifierStack& stack, ShenandoahVerifyOopClosure& cl) {
545 size_t processed = 0;
546 HeapWord* obj = r->bottom();
547 if (_heap->complete_marking_context()->is_marked(cast_to_oop(obj))) {
548 verify_and_follow(obj, stack, cl, &processed);
549 }
550 Atomic::add(&_processed, processed, memory_order_relaxed);
551 }
552
553 virtual void work_regular(ShenandoahHeapRegion *r, ShenandoahVerifierStack &stack, ShenandoahVerifyOopClosure &cl) {
554 size_t processed = 0;
555 ShenandoahMarkingContext* ctx = _heap->complete_marking_context();
556 HeapWord* tams = ctx->top_at_mark_start(r);
557
558 // Bitmaps, before TAMS
559 if (tams > r->bottom()) {
560 HeapWord* start = r->bottom();
561 HeapWord* addr = ctx->get_next_marked_addr(start, tams);
562
563 while (addr < tams) {
564 verify_and_follow(addr, stack, cl, &processed);
565 addr += 1;
566 if (addr < tams) {
567 addr = ctx->get_next_marked_addr(addr, tams);
568 }
569 }
570 }
571
572 // Size-based, after TAMS
573 {
574 HeapWord* limit = r->top();
575 HeapWord* addr = tams;
594 // everything was already marked, and never touching further:
595 if (!is_instance_ref_klass(obj->klass())) {
596 cl.verify_oops_from(obj);
597 (*processed)++;
598 }
599 while (!stack.is_empty()) {
600 ShenandoahVerifierTask task = stack.pop();
601 cl.verify_oops_from(task.obj());
602 (*processed)++;
603 }
604 }
605 };
606
607 class VerifyThreadGCState : public ThreadClosure {
608 private:
609 const char* const _label;
610 char const _expected;
611
612 public:
613 VerifyThreadGCState(const char* label, char expected) : _label(label), _expected(expected) {}
614 void do_thread(Thread* t) {
615 char actual = ShenandoahThreadLocalData::gc_state(t);
616 if (actual != _expected) {
617 fatal("%s: Thread %s: expected gc-state %d, actual %d", _label, t->name(), _expected, actual);
618 }
619 }
620 };
621
622 void ShenandoahVerifier::verify_at_safepoint(const char *label,
623 VerifyForwarded forwarded, VerifyMarked marked,
624 VerifyCollectionSet cset,
625 VerifyLiveness liveness, VerifyRegions regions,
626 VerifyGCState gcstate) {
627 guarantee(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "only when nothing else happens");
628 guarantee(ShenandoahVerify, "only when enabled, and bitmap is initialized in ShenandoahHeap::initialize");
629
630 ShenandoahHeap::heap()->propagate_gc_state_to_java_threads();
631
632 // Avoid side-effect of changing workers' active thread count, but bypass concurrent/parallel protocol check
633 ShenandoahPushWorkerScope verify_worker_scope(_heap->workers(), _heap->max_workers(), false /*bypass check*/);
634
635 log_info(gc,start)("Verify %s, Level " INTX_FORMAT, label, ShenandoahVerifyLevel);
636
637 // GC state checks
638 {
639 char expected = -1;
640 bool enabled;
641 switch (gcstate) {
642 case _verify_gcstate_disable:
643 enabled = false;
644 break;
645 case _verify_gcstate_forwarded:
646 enabled = true;
647 expected = ShenandoahHeap::HAS_FORWARDED;
648 break;
649 case _verify_gcstate_evacuation:
650 enabled = true;
651 expected = ShenandoahHeap::HAS_FORWARDED | ShenandoahHeap::EVACUATION;
652 if (!_heap->is_stw_gc_in_progress()) {
653 // Only concurrent GC sets this.
654 expected |= ShenandoahHeap::WEAK_ROOTS;
655 }
656 break;
657 case _verify_gcstate_stable:
658 enabled = true;
659 expected = ShenandoahHeap::STABLE;
660 break;
661 case _verify_gcstate_stable_weakroots:
662 enabled = true;
663 expected = ShenandoahHeap::STABLE;
664 if (!_heap->is_stw_gc_in_progress()) {
665 // Only concurrent GC sets this.
666 expected |= ShenandoahHeap::WEAK_ROOTS;
667 }
668 break;
669 default:
670 enabled = false;
671 assert(false, "Unhandled gc-state verification");
672 }
673
674 if (enabled) {
675 char actual = _heap->gc_state();
676 if (actual != expected) {
677 fatal("%s: Global gc-state: expected %d, actual %d", label, expected, actual);
678 }
679
680 VerifyThreadGCState vtgcs(label, expected);
681 Threads::java_threads_do(&vtgcs);
682 }
683 }
684
685 // Deactivate barriers temporarily: Verifier wants plain heap accesses
686 ShenandoahGCStateResetter resetter;
687
688 // Heap size checks
689 {
690 ShenandoahHeapLocker lock(_heap->lock());
691
692 ShenandoahCalculateRegionStatsClosure cl;
693 _heap->heap_region_iterate(&cl);
694 size_t heap_used = _heap->used();
695 guarantee(cl.used() == heap_used,
696 "%s: heap used size must be consistent: heap-used = " SIZE_FORMAT "%s, regions-used = " SIZE_FORMAT "%s",
697 label,
698 byte_size_in_proper_unit(heap_used), proper_unit_for_byte_size(heap_used),
699 byte_size_in_proper_unit(cl.used()), proper_unit_for_byte_size(cl.used()));
700
701 size_t heap_committed = _heap->committed();
702 guarantee(cl.committed() == heap_committed,
703 "%s: heap committed size must be consistent: heap-committed = " SIZE_FORMAT "%s, regions-committed = " SIZE_FORMAT "%s",
704 label,
705 byte_size_in_proper_unit(heap_committed), proper_unit_for_byte_size(heap_committed),
706 byte_size_in_proper_unit(cl.committed()), proper_unit_for_byte_size(cl.committed()));
707 }
708
709 // Internal heap region checks
710 if (ShenandoahVerifyLevel >= 1) {
711 ShenandoahVerifyHeapRegionClosure cl(label, regions);
712 _heap->heap_region_iterate(&cl);
713 }
714
715 OrderAccess::fence();
716
717 if (UseTLAB) {
718 _heap->labs_make_parsable();
719 }
720
721 // Allocate temporary bitmap for storing marking wavefront:
722 _verification_bit_map->clear();
723
724 // Allocate temporary array for storing liveness data
725 ShenandoahLivenessData* ld = NEW_C_HEAP_ARRAY(ShenandoahLivenessData, _heap->num_regions(), mtGC);
726 Copy::fill_to_bytes((void*)ld, _heap->num_regions()*sizeof(ShenandoahLivenessData), 0);
727
728 const VerifyOptions& options = ShenandoahVerifier::VerifyOptions(forwarded, marked, cset, liveness, regions, gcstate);
729
730 // Steps 1-2. Scan root set to get initial reachable set. Finish walking the reachable heap.
731 // This verifies what application can see, since it only cares about reachable objects.
732 size_t count_reachable = 0;
733 if (ShenandoahVerifyLevel >= 2) {
734 ShenandoahVerifierReachableTask task(_verification_bit_map, ld, label, options);
735 _heap->workers()->run_task(&task);
736 count_reachable = task.processed();
737 }
738
739 // Step 3. Walk marked objects. Marked objects might be unreachable. This verifies what collector,
740 // not the application, can see during the region scans. There is no reason to process the objects
741 // that were already verified, e.g. those marked in verification bitmap. There is interaction with TAMS:
742 // before TAMS, we verify the bitmaps, if available; after TAMS, we walk until the top(). It mimics
743 // what marked_object_iterate is doing, without calling into that optimized (and possibly incorrect)
744 // version
745
746 size_t count_marked = 0;
747 if (ShenandoahVerifyLevel >= 4 && (marked == _verify_marked_complete || marked == _verify_marked_complete_except_references)) {
748 guarantee(_heap->marking_context()->is_complete(), "Marking context should be complete");
749 ShenandoahVerifierMarkedRegionTask task(_verification_bit_map, ld, label, options);
750 _heap->workers()->run_task(&task);
751 count_marked = task.processed();
752 } else {
753 guarantee(ShenandoahVerifyLevel < 4 || marked == _verify_marked_incomplete || marked == _verify_marked_disable, "Should be");
754 }
755
756 // Step 4. Verify accumulated liveness data, if needed. Only reliable if verification level includes
757 // marked objects.
758
759 if (ShenandoahVerifyLevel >= 4 && marked == _verify_marked_complete && liveness == _verify_liveness_complete) {
760 for (size_t i = 0; i < _heap->num_regions(); i++) {
761 ShenandoahHeapRegion* r = _heap->get_region(i);
762
763 juint verf_live = 0;
764 if (r->is_humongous()) {
765 // For humongous objects, test if start region is marked live, and if so,
766 // all humongous regions in that chain have live data equal to their "used".
767 juint start_live = Atomic::load(&ld[r->humongous_start_region()->index()]);
768 if (start_live > 0) {
769 verf_live = (juint)(r->used() / HeapWordSize);
770 }
771 } else {
772 verf_live = Atomic::load(&ld[r->index()]);
773 }
774
775 size_t reg_live = r->get_live_data_words();
776 if (reg_live != verf_live) {
777 stringStream ss;
778 r->print_on(&ss);
779 fatal("%s: Live data should match: region-live = " SIZE_FORMAT ", verifier-live = " UINT32_FORMAT "\n%s",
780 label, reg_live, verf_live, ss.freeze());
781 }
782 }
783 }
784
785 log_info(gc)("Verify %s, Level " INTX_FORMAT " (" SIZE_FORMAT " reachable, " SIZE_FORMAT " marked)",
786 label, ShenandoahVerifyLevel, count_reachable, count_marked);
787
788 FREE_C_HEAP_ARRAY(ShenandoahLivenessData, ld);
789 }
790
791 void ShenandoahVerifier::verify_generic(VerifyOption vo) {
792 verify_at_safepoint(
793 "Generic Verification",
794 _verify_forwarded_allow, // conservatively allow forwarded
795 _verify_marked_disable, // do not verify marked: lots ot time wasted checking dead allocations
796 _verify_cset_disable, // cset may be inconsistent
797 _verify_liveness_disable, // no reliable liveness data
798 _verify_regions_disable, // no reliable region data
799 _verify_gcstate_disable // no data about gcstate
800 );
801 }
802
803 void ShenandoahVerifier::verify_before_concmark() {
804 verify_at_safepoint(
805 "Before Mark",
806 _verify_forwarded_none, // UR should have fixed up
807 _verify_marked_disable, // do not verify marked: lots ot time wasted checking dead allocations
808 _verify_cset_none, // UR should have fixed this
809 _verify_liveness_disable, // no reliable liveness data
810 _verify_regions_notrash, // no trash regions
811 _verify_gcstate_stable // there are no forwarded objects
812 );
813 }
814
815 void ShenandoahVerifier::verify_after_concmark() {
816 verify_at_safepoint(
817 "After Mark",
818 _verify_forwarded_none, // no forwarded references
819 _verify_marked_complete_except_references, // bitmaps as precise as we can get, except dangling j.l.r.Refs
820 _verify_cset_none, // no references to cset anymore
821 _verify_liveness_complete, // liveness data must be complete here
822 _verify_regions_disable, // trash regions not yet recycled
823 _verify_gcstate_stable_weakroots // heap is still stable, weakroots are in progress
824 );
825 }
826
827 void ShenandoahVerifier::verify_before_evacuation() {
828 verify_at_safepoint(
829 "Before Evacuation",
830 _verify_forwarded_none, // no forwarded references
831 _verify_marked_complete_except_references, // walk over marked objects too
832 _verify_cset_disable, // non-forwarded references to cset expected
833 _verify_liveness_complete, // liveness data must be complete here
834 _verify_regions_disable, // trash regions not yet recycled
835 _verify_gcstate_stable_weakroots // heap is still stable, weakroots are in progress
836 );
837 }
838
839 void ShenandoahVerifier::verify_during_evacuation() {
840 verify_at_safepoint(
841 "During Evacuation",
842 _verify_forwarded_allow, // some forwarded references are allowed
843 _verify_marked_disable, // walk only roots
844 _verify_cset_disable, // some cset references are not forwarded yet
845 _verify_liveness_disable, // liveness data might be already stale after pre-evacs
846 _verify_regions_disable, // trash regions not yet recycled
847 _verify_gcstate_evacuation // evacuation is in progress
848 );
849 }
850
851 void ShenandoahVerifier::verify_after_evacuation() {
852 verify_at_safepoint(
853 "After Evacuation",
854 _verify_forwarded_allow, // objects are still forwarded
855 _verify_marked_complete, // bitmaps might be stale, but alloc-after-mark should be well
856 _verify_cset_forwarded, // all cset refs are fully forwarded
857 _verify_liveness_disable, // no reliable liveness data anymore
858 _verify_regions_notrash, // trash regions have been recycled already
859 _verify_gcstate_forwarded // evacuation produced some forwarded objects
860 );
861 }
862
863 void ShenandoahVerifier::verify_before_updaterefs() {
864 verify_at_safepoint(
865 "Before Updating References",
866 _verify_forwarded_allow, // forwarded references allowed
867 _verify_marked_complete, // bitmaps might be stale, but alloc-after-mark should be well
868 _verify_cset_forwarded, // all cset refs are fully forwarded
869 _verify_liveness_disable, // no reliable liveness data anymore
870 _verify_regions_notrash, // trash regions have been recycled already
871 _verify_gcstate_forwarded // evacuation should have produced some forwarded objects
872 );
873 }
874
875 void ShenandoahVerifier::verify_after_updaterefs() {
876 verify_at_safepoint(
877 "After Updating References",
878 _verify_forwarded_none, // no forwarded references
879 _verify_marked_complete, // bitmaps might be stale, but alloc-after-mark should be well
880 _verify_cset_none, // no cset references, all updated
881 _verify_liveness_disable, // no reliable liveness data anymore
882 _verify_regions_nocset, // no cset regions, trash regions have appeared
883 _verify_gcstate_stable // update refs had cleaned up forwarded objects
884 );
885 }
886
887 void ShenandoahVerifier::verify_after_degenerated() {
888 verify_at_safepoint(
889 "After Degenerated GC",
890 _verify_forwarded_none, // all objects are non-forwarded
891 _verify_marked_complete, // all objects are marked in complete bitmap
892 _verify_cset_none, // no cset references
893 _verify_liveness_disable, // no reliable liveness data anymore
894 _verify_regions_notrash_nocset, // no trash, no cset
895 _verify_gcstate_stable // degenerated refs had cleaned up forwarded objects
896 );
897 }
898
899 void ShenandoahVerifier::verify_before_fullgc() {
900 verify_at_safepoint(
901 "Before Full GC",
902 _verify_forwarded_allow, // can have forwarded objects
903 _verify_marked_disable, // do not verify marked: lots ot time wasted checking dead allocations
904 _verify_cset_disable, // cset might be foobared
905 _verify_liveness_disable, // no reliable liveness data anymore
906 _verify_regions_disable, // no reliable region data here
907 _verify_gcstate_disable // no reliable gcstate data
908 );
909 }
910
911 void ShenandoahVerifier::verify_after_fullgc() {
912 verify_at_safepoint(
913 "After Full GC",
914 _verify_forwarded_none, // all objects are non-forwarded
915 _verify_marked_complete, // all objects are marked in complete bitmap
916 _verify_cset_none, // no cset references
917 _verify_liveness_disable, // no reliable liveness data anymore
918 _verify_regions_notrash_nocset, // no trash, no cset
919 _verify_gcstate_stable // full gc cleaned up everything
920 );
921 }
922
923 class ShenandoahVerifyNoForwared : public OopClosure {
924 private:
925 template <class T>
926 void do_oop_work(T* p) {
927 T o = RawAccess<>::oop_load(p);
928 if (!CompressedOops::is_null(o)) {
929 oop obj = CompressedOops::decode_not_null(o);
930 oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj);
931 if (obj != fwd) {
932 ShenandoahAsserts::print_failure(ShenandoahAsserts::_safe_all, obj, p, nullptr,
933 "Verify Roots", "Should not be forwarded", __FILE__, __LINE__);
934 }
935 }
936 }
937
938 public:
939 void do_oop(narrowOop* p) { do_oop_work(p); }
940 void do_oop(oop* p) { do_oop_work(p); }
941 };
942
943 class ShenandoahVerifyInToSpaceClosure : public OopClosure {
944 private:
945 template <class T>
946 void do_oop_work(T* p) {
947 T o = RawAccess<>::oop_load(p);
948 if (!CompressedOops::is_null(o)) {
949 oop obj = CompressedOops::decode_not_null(o);
950 ShenandoahHeap* heap = ShenandoahHeap::heap();
951
952 if (!heap->marking_context()->is_marked(obj)) {
953 ShenandoahAsserts::print_failure(ShenandoahAsserts::_safe_all, obj, p, nullptr,
954 "Verify Roots In To-Space", "Should be marked", __FILE__, __LINE__);
955 }
956
957 if (heap->in_collection_set(obj)) {
958 ShenandoahAsserts::print_failure(ShenandoahAsserts::_safe_all, obj, p, nullptr,
959 "Verify Roots In To-Space", "Should not be in collection set", __FILE__, __LINE__);
960 }
961
962 oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj);
963 if (obj != fwd) {
964 ShenandoahAsserts::print_failure(ShenandoahAsserts::_safe_all, obj, p, nullptr,
965 "Verify Roots In To-Space", "Should not be forwarded", __FILE__, __LINE__);
966 }
967 }
968 }
969
970 public:
971 void do_oop(narrowOop* p) { do_oop_work(p); }
972 void do_oop(oop* p) { do_oop_work(p); }
973 };
974
975 void ShenandoahVerifier::verify_roots_in_to_space() {
976 ShenandoahVerifyInToSpaceClosure cl;
977 ShenandoahRootVerifier::roots_do(&cl);
978 }
979
980 void ShenandoahVerifier::verify_roots_no_forwarded() {
981 ShenandoahVerifyNoForwared cl;
982 ShenandoahRootVerifier::roots_do(&cl);
983 }
|
1 /*
2 * Copyright (c) 2017, 2021, Red Hat, Inc. All rights reserved.
3 * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 *
24 */
25
26 #include "precompiled.hpp"
27 #include "gc/shared/tlab_globals.hpp"
28 #include "gc/shenandoah/shenandoahAsserts.hpp"
29 #include "gc/shenandoah/shenandoahForwarding.inline.hpp"
30 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
31 #include "gc/shenandoah/shenandoahGeneration.hpp"
32 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
33 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
34 #include "gc/shenandoah/shenandoahOldGeneration.hpp"
35 #include "gc/shenandoah/shenandoahRootProcessor.hpp"
36 #include "gc/shenandoah/shenandoahScanRemembered.inline.hpp"
37 #include "gc/shenandoah/shenandoahTaskqueue.inline.hpp"
38 #include "gc/shenandoah/shenandoahUtils.hpp"
39 #include "gc/shenandoah/shenandoahVerifier.hpp"
40 #include "gc/shenandoah/shenandoahYoungGeneration.hpp"
41 #include "memory/allocation.hpp"
42 #include "memory/iterator.inline.hpp"
43 #include "memory/resourceArea.hpp"
44 #include "oops/compressedOops.inline.hpp"
45 #include "runtime/atomic.hpp"
46 #include "runtime/orderAccess.hpp"
47 #include "runtime/threads.hpp"
48 #include "utilities/align.hpp"
49
50 // Avoid name collision on verify_oop (defined in macroAssembler_arm.hpp)
51 #ifdef verify_oop
52 #undef verify_oop
53 #endif
54
55 static bool is_instance_ref_klass(Klass* k) {
56 return k->is_instance_klass() && InstanceKlass::cast(k)->reference_type() != REF_NONE;
57 }
58
59 class ShenandoahVerifyOopClosure : public BasicOopIterateClosure {
60 private:
61 const char* _phase;
62 ShenandoahVerifier::VerifyOptions _options;
63 ShenandoahVerifierStack* _stack;
64 ShenandoahHeap* _heap;
65 MarkBitMap* _map;
66 ShenandoahLivenessData* _ld;
67 void* _interior_loc;
68 oop _loc;
69 ReferenceIterationMode _ref_mode;
70 ShenandoahGeneration* _generation;
71
72 public:
73 ShenandoahVerifyOopClosure(ShenandoahVerifierStack* stack, MarkBitMap* map, ShenandoahLivenessData* ld,
74 const char* phase, ShenandoahVerifier::VerifyOptions options) :
75 _phase(phase),
76 _options(options),
77 _stack(stack),
78 _heap(ShenandoahHeap::heap()),
79 _map(map),
80 _ld(ld),
81 _interior_loc(nullptr),
82 _loc(nullptr),
83 _generation(nullptr) {
84 if (options._verify_marked == ShenandoahVerifier::_verify_marked_complete_except_references ||
85 options._verify_marked == ShenandoahVerifier::_verify_marked_complete_satb_empty ||
86 options._verify_marked == ShenandoahVerifier::_verify_marked_disable) {
87 // Unknown status for Reference.referent field. Do not touch it, it might be dead.
88 // Normally, barriers would prevent us from seeing the dead referents, but verifier
89 // runs with barriers disabled.
90 _ref_mode = DO_FIELDS_EXCEPT_REFERENT;
91 } else {
92 // Otherwise do all fields.
93 _ref_mode = DO_FIELDS;
94 }
95
96 if (_heap->mode()->is_generational()) {
97 _generation = _heap->gc_generation();
98 assert(_generation != nullptr, "Expected active generation in this mode");
99 shenandoah_assert_generations_reconciled();
100 }
101 }
102
103 ReferenceIterationMode reference_iteration_mode() override {
104 return _ref_mode;
105 }
106
107 private:
108 void check(ShenandoahAsserts::SafeLevel level, oop obj, bool test, const char* label) {
109 if (!test) {
110 ShenandoahAsserts::print_failure(level, obj, _interior_loc, _loc, _phase, label, __FILE__, __LINE__);
111 }
112 }
113
114 template <class T>
115 void do_oop_work(T* p) {
116 T o = RawAccess<>::oop_load(p);
117 if (!CompressedOops::is_null(o)) {
118 oop obj = CompressedOops::decode_not_null(o);
119 if (is_instance_ref_klass(obj->klass())) {
120 obj = ShenandoahForwarding::get_forwardee(obj);
121 }
122 // Single threaded verification can use faster non-atomic stack and bitmap
123 // methods.
124 //
125 // For performance reasons, only fully verify non-marked field values.
126 // We are here when the host object for *p is already marked.
127 if (in_generation(obj) && _map->par_mark(obj)) {
128 verify_oop_at(p, obj);
129 _stack->push(ShenandoahVerifierTask(obj));
130 }
131 }
132 }
133
134 bool in_generation(oop obj) {
135 if (_generation == nullptr) {
136 return true;
137 }
138
139 ShenandoahHeapRegion* region = _heap->heap_region_containing(obj);
140 return _generation->contains(region);
141 }
142
143 void verify_oop(oop obj) {
144 // Perform consistency checks with gradually decreasing safety level. This guarantees
145 // that failure report would not try to touch something that was not yet verified to be
146 // safe to process.
147
148 check(ShenandoahAsserts::_safe_unknown, obj, _heap->is_in_reserved(obj),
149 "oop must be in heap bounds");
150 check(ShenandoahAsserts::_safe_unknown, obj, is_object_aligned(obj),
151 "oop must be aligned");
152
153 ShenandoahHeapRegion *obj_reg = _heap->heap_region_containing(obj);
154 Klass* obj_klass = obj->klass_or_null();
155
156 // Verify that obj is not in dead space:
157 {
158 // Do this before touching obj->size()
159 check(ShenandoahAsserts::_safe_unknown, obj, obj_klass != nullptr,
160 "Object klass pointer should not be null");
161 check(ShenandoahAsserts::_safe_unknown, obj, Metaspace::contains(obj_klass),
162 "Object klass pointer must go to metaspace");
173 size_t humongous_end = humongous_start + (obj->size() >> ShenandoahHeapRegion::region_size_words_shift());
174 for (size_t idx = humongous_start + 1; idx < humongous_end; idx++) {
175 check(ShenandoahAsserts::_safe_unknown, obj, _heap->get_region(idx)->is_humongous_continuation(),
176 "Humongous object is in continuation that fits it");
177 }
178 }
179
180 // ------------ obj is safe at this point --------------
181
182 check(ShenandoahAsserts::_safe_oop, obj, obj_reg->is_active(),
183 "Object should be in active region");
184
185 switch (_options._verify_liveness) {
186 case ShenandoahVerifier::_verify_liveness_disable:
187 // skip
188 break;
189 case ShenandoahVerifier::_verify_liveness_complete:
190 Atomic::add(&_ld[obj_reg->index()], (uint) obj->size(), memory_order_relaxed);
191 // fallthrough for fast failure for un-live regions:
192 case ShenandoahVerifier::_verify_liveness_conservative:
193 check(ShenandoahAsserts::_safe_oop, obj, obj_reg->has_live() ||
194 (obj_reg->is_old() && _heap->gc_generation()->is_young()),
195 "Object must belong to region with live data");
196 shenandoah_assert_generations_reconciled();
197 break;
198 default:
199 assert(false, "Unhandled liveness verification");
200 }
201 }
202
203 oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj);
204
205 ShenandoahHeapRegion* fwd_reg = nullptr;
206
207 if (obj != fwd) {
208 check(ShenandoahAsserts::_safe_oop, obj, _heap->is_in_reserved(fwd),
209 "Forwardee must be in heap bounds");
210 check(ShenandoahAsserts::_safe_oop, obj, !CompressedOops::is_null(fwd),
211 "Forwardee is set");
212 check(ShenandoahAsserts::_safe_oop, obj, is_object_aligned(fwd),
213 "Forwardee must be aligned");
214
215 // Do this before touching fwd->size()
216 Klass* fwd_klass = fwd->klass_or_null();
226 check(ShenandoahAsserts::_safe_oop, obj, fwd_reg->is_active(),
227 "Forwardee should be in active region");
228
229 // Verify that forwardee is not in the dead space:
230 check(ShenandoahAsserts::_safe_oop, obj, !fwd_reg->is_humongous(),
231 "Should have no humongous forwardees");
232
233 HeapWord *fwd_addr = cast_from_oop<HeapWord *>(fwd);
234 check(ShenandoahAsserts::_safe_oop, obj, fwd_addr < fwd_reg->top(),
235 "Forwardee start should be within the region");
236 check(ShenandoahAsserts::_safe_oop, obj, (fwd_addr + fwd->size()) <= fwd_reg->top(),
237 "Forwardee end should be within the region");
238
239 oop fwd2 = ShenandoahForwarding::get_forwardee_raw_unchecked(fwd);
240 check(ShenandoahAsserts::_safe_oop, obj, (fwd == fwd2),
241 "Double forwarding");
242 } else {
243 fwd_reg = obj_reg;
244 }
245
246 // Do additional checks for special objects: their fields can hold metadata as well.
247 // We want to check class loading/unloading did not corrupt them.
248
249 if (obj_klass == vmClasses::Class_klass()) {
250 Metadata* klass = obj->metadata_field(java_lang_Class::klass_offset());
251 check(ShenandoahAsserts::_safe_oop, obj,
252 klass == nullptr || Metaspace::contains(klass),
253 "Instance class mirror should point to Metaspace");
254
255 Metadata* array_klass = obj->metadata_field(java_lang_Class::array_klass_offset());
256 check(ShenandoahAsserts::_safe_oop, obj,
257 array_klass == nullptr || Metaspace::contains(array_klass),
258 "Array class mirror should point to Metaspace");
259 }
260
261 // ------------ obj and fwd are safe at this point --------------
262 switch (_options._verify_marked) {
263 case ShenandoahVerifier::_verify_marked_disable:
264 // skip
265 break;
266 case ShenandoahVerifier::_verify_marked_incomplete:
267 check(ShenandoahAsserts::_safe_all, obj, _heap->marking_context()->is_marked(obj),
268 "Must be marked in incomplete bitmap");
269 break;
270 case ShenandoahVerifier::_verify_marked_complete:
271 check(ShenandoahAsserts::_safe_all, obj, _heap->gc_generation()->complete_marking_context()->is_marked(obj),
272 "Must be marked in complete bitmap");
273 break;
274 case ShenandoahVerifier::_verify_marked_complete_except_references:
275 case ShenandoahVerifier::_verify_marked_complete_satb_empty:
276 check(ShenandoahAsserts::_safe_all, obj, _heap->gc_generation()->complete_marking_context()->is_marked(obj),
277 "Must be marked in complete bitmap, except j.l.r.Reference referents");
278 break;
279 default:
280 assert(false, "Unhandled mark verification");
281 }
282
283 switch (_options._verify_forwarded) {
284 case ShenandoahVerifier::_verify_forwarded_disable:
285 // skip
286 break;
287 case ShenandoahVerifier::_verify_forwarded_none: {
288 check(ShenandoahAsserts::_safe_all, obj, (obj == fwd),
289 "Should not be forwarded");
290 break;
291 }
292 case ShenandoahVerifier::_verify_forwarded_allow: {
293 if (obj != fwd) {
294 check(ShenandoahAsserts::_safe_all, obj, obj_reg != fwd_reg,
295 "Forwardee should be in another region");
296 }
338 * Useful when picking up the object at known offset in heap,
339 * but without knowing what objects reference it.
340 * @param obj verified object
341 */
342 void verify_oop_standalone(oop obj) {
343 _interior_loc = nullptr;
344 verify_oop(obj);
345 _interior_loc = nullptr;
346 }
347
348 /**
349 * Verify oop fields from this object.
350 * @param obj host object for verified fields
351 */
352 void verify_oops_from(oop obj) {
353 _loc = obj;
354 obj->oop_iterate(this);
355 _loc = nullptr;
356 }
357
358 void do_oop(oop* p) override { do_oop_work(p); }
359 void do_oop(narrowOop* p) override { do_oop_work(p); }
360 };
361
362 // This closure computes the amounts of used, committed, and garbage memory and the number of regions contained within
363 // a subset (e.g. the young generation or old generation) of the total heap.
364 class ShenandoahCalculateRegionStatsClosure : public ShenandoahHeapRegionClosure {
365 private:
366 size_t _used, _committed, _garbage, _regions, _humongous_waste, _trashed_regions;
367 public:
368 ShenandoahCalculateRegionStatsClosure() :
369 _used(0), _committed(0), _garbage(0), _regions(0), _humongous_waste(0), _trashed_regions(0) {};
370
371 void heap_region_do(ShenandoahHeapRegion* r) override {
372 _used += r->used();
373 _garbage += r->garbage();
374 _committed += r->is_committed() ? ShenandoahHeapRegion::region_size_bytes() : 0;
375 if (r->is_humongous()) {
376 _humongous_waste += r->free();
377 }
378 if (r->is_trash()) {
379 _trashed_regions++;
380 }
381 _regions++;
382 log_debug(gc)("ShenandoahCalculateRegionStatsClosure: adding " SIZE_FORMAT " for %s Region " SIZE_FORMAT ", yielding: " SIZE_FORMAT,
383 r->used(), (r->is_humongous() ? "humongous" : "regular"), r->index(), _used);
384 }
385
386 size_t used() const { return _used; }
387 size_t committed() const { return _committed; }
388 size_t garbage() const { return _garbage; }
389 size_t regions() const { return _regions; }
390 size_t waste() const { return _humongous_waste; }
391
392 // span is the total memory affiliated with these stats (some of which is in use and other is available)
393 size_t span() const { return _regions * ShenandoahHeapRegion::region_size_bytes(); }
394 size_t non_trashed_span() const { return (_regions - _trashed_regions) * ShenandoahHeapRegion::region_size_bytes(); }
395 };
396
397 class ShenandoahGenerationStatsClosure : public ShenandoahHeapRegionClosure {
398 public:
399 ShenandoahCalculateRegionStatsClosure old;
400 ShenandoahCalculateRegionStatsClosure young;
401 ShenandoahCalculateRegionStatsClosure global;
402
403 void heap_region_do(ShenandoahHeapRegion* r) override {
404 switch (r->affiliation()) {
405 case FREE:
406 return;
407 case YOUNG_GENERATION:
408 young.heap_region_do(r);
409 global.heap_region_do(r);
410 break;
411 case OLD_GENERATION:
412 old.heap_region_do(r);
413 global.heap_region_do(r);
414 break;
415 default:
416 ShouldNotReachHere();
417 }
418 }
419
420 static void log_usage(ShenandoahGeneration* generation, ShenandoahCalculateRegionStatsClosure& stats) {
421 log_debug(gc)("Safepoint verification: %s verified usage: " SIZE_FORMAT "%s, recorded usage: " SIZE_FORMAT "%s",
422 generation->name(),
423 byte_size_in_proper_unit(generation->used()), proper_unit_for_byte_size(generation->used()),
424 byte_size_in_proper_unit(stats.used()), proper_unit_for_byte_size(stats.used()));
425 }
426
427 static void validate_usage(const bool adjust_for_padding,
428 const char* label, ShenandoahGeneration* generation, ShenandoahCalculateRegionStatsClosure& stats) {
429 ShenandoahHeap* heap = ShenandoahHeap::heap();
430 size_t generation_used = generation->used();
431 size_t generation_used_regions = generation->used_regions();
432 if (adjust_for_padding && (generation->is_young() || generation->is_global())) {
433 size_t pad = heap->old_generation()->get_pad_for_promote_in_place();
434 generation_used += pad;
435 }
436
437 guarantee(stats.used() == generation_used,
438 "%s: generation (%s) used size must be consistent: generation-used: " PROPERFMT ", regions-used: " PROPERFMT,
439 label, generation->name(), PROPERFMTARGS(generation_used), PROPERFMTARGS(stats.used()));
440
441 guarantee(stats.regions() == generation_used_regions,
442 "%s: generation (%s) used regions (" SIZE_FORMAT ") must equal regions that are in use (" SIZE_FORMAT ")",
443 label, generation->name(), generation->used_regions(), stats.regions());
444
445 size_t generation_capacity = generation->max_capacity();
446 guarantee(stats.non_trashed_span() <= generation_capacity,
447 "%s: generation (%s) size spanned by regions (" SIZE_FORMAT ") * region size (" PROPERFMT
448 ") must not exceed current capacity (" PROPERFMT ")",
449 label, generation->name(), stats.regions(), PROPERFMTARGS(ShenandoahHeapRegion::region_size_bytes()),
450 PROPERFMTARGS(generation_capacity));
451
452 size_t humongous_waste = generation->get_humongous_waste();
453 guarantee(stats.waste() == humongous_waste,
454 "%s: generation (%s) humongous waste must be consistent: generation: " PROPERFMT ", regions: " PROPERFMT,
455 label, generation->name(), PROPERFMTARGS(humongous_waste), PROPERFMTARGS(stats.waste()));
456 }
457 };
458
459 class ShenandoahVerifyHeapRegionClosure : public ShenandoahHeapRegionClosure {
460 private:
461 ShenandoahHeap* _heap;
462 const char* _phase;
463 ShenandoahVerifier::VerifyRegions _regions;
464 public:
465 ShenandoahVerifyHeapRegionClosure(const char* phase, ShenandoahVerifier::VerifyRegions regions) :
466 _heap(ShenandoahHeap::heap()),
467 _phase(phase),
468 _regions(regions) {};
469
470 void print_failure(ShenandoahHeapRegion* r, const char* label) {
471 ResourceMark rm;
472
473 ShenandoahMessageBuffer msg("Shenandoah verification failed; %s: %s\n\n", _phase, label);
474
475 stringStream ss;
476 r->print_on(&ss);
477 msg.append("%s", ss.as_string());
478
479 report_vm_error(__FILE__, __LINE__, msg.buffer());
480 }
481
482 void verify(ShenandoahHeapRegion* r, bool test, const char* msg) {
483 if (!test) {
484 print_failure(r, msg);
485 }
486 }
487
488 void heap_region_do(ShenandoahHeapRegion* r) override {
489 switch (_regions) {
490 case ShenandoahVerifier::_verify_regions_disable:
491 break;
492 case ShenandoahVerifier::_verify_regions_notrash:
493 verify(r, !r->is_trash(),
494 "Should not have trash regions");
495 break;
496 case ShenandoahVerifier::_verify_regions_nocset:
497 verify(r, !r->is_cset(),
498 "Should not have cset regions");
499 break;
500 case ShenandoahVerifier::_verify_regions_notrash_nocset:
501 verify(r, !r->is_trash(),
502 "Should not have trash regions");
503 verify(r, !r->is_cset(),
504 "Should not have cset regions");
505 break;
506 default:
507 ShouldNotReachHere();
508 }
520 "Complete TAMS should not be larger than top");
521
522 verify(r, r->get_live_data_bytes() <= r->capacity(),
523 "Live data cannot be larger than capacity");
524
525 verify(r, r->garbage() <= r->capacity(),
526 "Garbage cannot be larger than capacity");
527
528 verify(r, r->used() <= r->capacity(),
529 "Used cannot be larger than capacity");
530
531 verify(r, r->get_shared_allocs() <= r->capacity(),
532 "Shared alloc count should not be larger than capacity");
533
534 verify(r, r->get_tlab_allocs() <= r->capacity(),
535 "TLAB alloc count should not be larger than capacity");
536
537 verify(r, r->get_gclab_allocs() <= r->capacity(),
538 "GCLAB alloc count should not be larger than capacity");
539
540 verify(r, r->get_plab_allocs() <= r->capacity(),
541 "PLAB alloc count should not be larger than capacity");
542
543 verify(r, r->get_shared_allocs() + r->get_tlab_allocs() + r->get_gclab_allocs() + r->get_plab_allocs() == r->used(),
544 "Accurate accounting: shared + TLAB + GCLAB + PLAB = used");
545
546 verify(r, !r->is_empty() || !r->has_live(),
547 "Empty regions should not have live data");
548
549 verify(r, r->is_cset() == _heap->collection_set()->is_in(r),
550 "Transitional: region flags and collection set agree");
551 }
552 };
553
554 class ShenandoahVerifierReachableTask : public WorkerTask {
555 private:
556 const char* _label;
557 ShenandoahVerifier::VerifyOptions _options;
558 ShenandoahHeap* _heap;
559 ShenandoahLivenessData* _ld;
560 MarkBitMap* _bitmap;
561 volatile size_t _processed;
562
563 public:
564 ShenandoahVerifierReachableTask(MarkBitMap* bitmap,
565 ShenandoahLivenessData* ld,
566 const char* label,
567 ShenandoahVerifier::VerifyOptions options) :
568 WorkerTask("Shenandoah Verifier Reachable Objects"),
569 _label(label),
570 _options(options),
571 _heap(ShenandoahHeap::heap()),
572 _ld(ld),
573 _bitmap(bitmap),
574 _processed(0) {};
575
576 size_t processed() const {
577 return _processed;
578 }
579
580 void work(uint worker_id) override {
581 ResourceMark rm;
582 ShenandoahVerifierStack stack;
583
584 // On level 2, we need to only check the roots once.
585 // On level 3, we want to check the roots, and seed the local stack.
586 // It is a lesser evil to accept multiple root scans at level 3, because
587 // extended parallelism would buy us out.
588 if (((ShenandoahVerifyLevel == 2) && (worker_id == 0))
589 || (ShenandoahVerifyLevel >= 3)) {
590 ShenandoahVerifyOopClosure cl(&stack, _bitmap, _ld,
591 ShenandoahMessageBuffer("%s, Roots", _label),
592 _options);
593 if (_heap->unload_classes()) {
594 ShenandoahRootVerifier::strong_roots_do(&cl);
595 } else {
596 ShenandoahRootVerifier::roots_do(&cl);
597 }
598 }
599
600 size_t processed = 0;
601
602 if (ShenandoahVerifyLevel >= 3) {
603 ShenandoahVerifyOopClosure cl(&stack, _bitmap, _ld,
604 ShenandoahMessageBuffer("%s, Reachable", _label),
605 _options);
606 while (!stack.is_empty()) {
607 processed++;
608 ShenandoahVerifierTask task = stack.pop();
609 cl.verify_oops_from(task.obj());
610 }
611 }
612
613 Atomic::add(&_processed, processed, memory_order_relaxed);
614 }
615 };
616
617 class ShenandoahVerifyNoIncompleteSatbBuffers : public ThreadClosure {
618 public:
619 void do_thread(Thread* thread) override {
620 SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(thread);
621 if (!is_empty(queue)) {
622 fatal("All SATB buffers should have been flushed during mark");
623 }
624 }
625 private:
626 bool is_empty(SATBMarkQueue& queue) {
627 return queue.buffer() == nullptr || queue.index() == queue.capacity();
628 }
629 };
630
631 class ShenandoahVerifierMarkedRegionTask : public WorkerTask {
632 private:
633 const char* _label;
634 ShenandoahVerifier::VerifyOptions _options;
635 ShenandoahHeap *_heap;
636 MarkBitMap* _bitmap;
637 ShenandoahLivenessData* _ld;
638 volatile size_t _claimed;
639 volatile size_t _processed;
640 ShenandoahGeneration* _generation;
641
642 public:
643 ShenandoahVerifierMarkedRegionTask(MarkBitMap* bitmap,
644 ShenandoahLivenessData* ld,
645 const char* label,
646 ShenandoahVerifier::VerifyOptions options) :
647 WorkerTask("Shenandoah Verifier Marked Objects"),
648 _label(label),
649 _options(options),
650 _heap(ShenandoahHeap::heap()),
651 _bitmap(bitmap),
652 _ld(ld),
653 _claimed(0),
654 _processed(0),
655 _generation(nullptr) {
656 if (_heap->mode()->is_generational()) {
657 _generation = _heap->gc_generation();
658 assert(_generation != nullptr, "Expected active generation in this mode.");
659 shenandoah_assert_generations_reconciled();
660 }
661 };
662
663 size_t processed() {
664 return Atomic::load(&_processed);
665 }
666
667 void work(uint worker_id) override {
668 if (_options._verify_marked == ShenandoahVerifier::_verify_marked_complete_satb_empty) {
669 ShenandoahVerifyNoIncompleteSatbBuffers verify_satb;
670 Threads::threads_do(&verify_satb);
671 }
672
673 ShenandoahVerifierStack stack;
674 ShenandoahVerifyOopClosure cl(&stack, _bitmap, _ld,
675 ShenandoahMessageBuffer("%s, Marked", _label),
676 _options);
677
678 while (true) {
679 size_t v = Atomic::fetch_then_add(&_claimed, 1u, memory_order_relaxed);
680 if (v < _heap->num_regions()) {
681 ShenandoahHeapRegion* r = _heap->get_region(v);
682 if (!in_generation(r)) {
683 continue;
684 }
685
686 if (!r->is_humongous() && !r->is_trash()) {
687 work_regular(r, stack, cl);
688 } else if (r->is_humongous_start()) {
689 work_humongous(r, stack, cl);
690 }
691 } else {
692 break;
693 }
694 }
695 }
696
697 bool in_generation(ShenandoahHeapRegion* r) {
698 return _generation == nullptr || _generation->contains(r);
699 }
700
701 virtual void work_humongous(ShenandoahHeapRegion *r, ShenandoahVerifierStack& stack, ShenandoahVerifyOopClosure& cl) {
702 size_t processed = 0;
703 HeapWord* obj = r->bottom();
704 if (_heap->gc_generation()->complete_marking_context()->is_marked(cast_to_oop(obj))) {
705 verify_and_follow(obj, stack, cl, &processed);
706 }
707 Atomic::add(&_processed, processed, memory_order_relaxed);
708 }
709
710 virtual void work_regular(ShenandoahHeapRegion *r, ShenandoahVerifierStack &stack, ShenandoahVerifyOopClosure &cl) {
711 size_t processed = 0;
712 ShenandoahMarkingContext* ctx = _heap->gc_generation()->complete_marking_context();
713 HeapWord* tams = ctx->top_at_mark_start(r);
714
715 // Bitmaps, before TAMS
716 if (tams > r->bottom()) {
717 HeapWord* start = r->bottom();
718 HeapWord* addr = ctx->get_next_marked_addr(start, tams);
719
720 while (addr < tams) {
721 verify_and_follow(addr, stack, cl, &processed);
722 addr += 1;
723 if (addr < tams) {
724 addr = ctx->get_next_marked_addr(addr, tams);
725 }
726 }
727 }
728
729 // Size-based, after TAMS
730 {
731 HeapWord* limit = r->top();
732 HeapWord* addr = tams;
751 // everything was already marked, and never touching further:
752 if (!is_instance_ref_klass(obj->klass())) {
753 cl.verify_oops_from(obj);
754 (*processed)++;
755 }
756 while (!stack.is_empty()) {
757 ShenandoahVerifierTask task = stack.pop();
758 cl.verify_oops_from(task.obj());
759 (*processed)++;
760 }
761 }
762 };
763
764 class VerifyThreadGCState : public ThreadClosure {
765 private:
766 const char* const _label;
767 char const _expected;
768
769 public:
770 VerifyThreadGCState(const char* label, char expected) : _label(label), _expected(expected) {}
771 void do_thread(Thread* t) override {
772 char actual = ShenandoahThreadLocalData::gc_state(t);
773 if (!verify_gc_state(actual, _expected)) {
774 fatal("%s: Thread %s: expected gc-state %d, actual %d", _label, t->name(), _expected, actual);
775 }
776 }
777
778 static bool verify_gc_state(char actual, char expected) {
779 // Old generation marking is allowed in all states.
780 if (ShenandoahHeap::heap()->mode()->is_generational()) {
781 return ((actual & ~(ShenandoahHeap::OLD_MARKING | ShenandoahHeap::MARKING)) == expected);
782 } else {
783 assert((actual & ShenandoahHeap::OLD_MARKING) == 0, "Should not mark old in non-generational mode");
784 return (actual == expected);
785 }
786 }
787 };
788
789 void ShenandoahVerifier::verify_at_safepoint(const char* label,
790 VerifyRememberedSet remembered,
791 VerifyForwarded forwarded,
792 VerifyMarked marked,
793 VerifyCollectionSet cset,
794 VerifyLiveness liveness,
795 VerifyRegions regions,
796 VerifySize sizeness,
797 VerifyGCState gcstate) {
798 guarantee(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "only when nothing else happens");
799 guarantee(ShenandoahVerify, "only when enabled, and bitmap is initialized in ShenandoahHeap::initialize");
800
801 ShenandoahHeap::heap()->propagate_gc_state_to_all_threads();
802
803 // Avoid side-effect of changing workers' active thread count, but bypass concurrent/parallel protocol check
804 ShenandoahPushWorkerScope verify_worker_scope(_heap->workers(), _heap->max_workers(), false /*bypass check*/);
805
806 log_info(gc,start)("Verify %s, Level " INTX_FORMAT, label, ShenandoahVerifyLevel);
807
808 // GC state checks
809 {
810 char expected = -1;
811 bool enabled;
812 switch (gcstate) {
813 case _verify_gcstate_disable:
814 enabled = false;
815 break;
816 case _verify_gcstate_forwarded:
817 enabled = true;
818 expected = ShenandoahHeap::HAS_FORWARDED;
819 break;
820 case _verify_gcstate_updating:
821 enabled = true;
822 expected = ShenandoahHeap::HAS_FORWARDED | ShenandoahHeap::UPDATE_REFS;
823 break;
824 case _verify_gcstate_stable:
825 enabled = true;
826 expected = ShenandoahHeap::STABLE;
827 break;
828 case _verify_gcstate_stable_weakroots:
829 enabled = true;
830 expected = ShenandoahHeap::STABLE;
831 if (!_heap->is_stw_gc_in_progress()) {
832 // Only concurrent GC sets this.
833 expected |= ShenandoahHeap::WEAK_ROOTS;
834 }
835 break;
836 default:
837 enabled = false;
838 assert(false, "Unhandled gc-state verification");
839 }
840
841 if (enabled) {
842 char actual = _heap->gc_state();
843
844 bool is_marking = (actual & ShenandoahHeap::MARKING);
845 bool is_marking_young_or_old = (actual & (ShenandoahHeap::YOUNG_MARKING | ShenandoahHeap::OLD_MARKING));
846 assert(is_marking == is_marking_young_or_old, "MARKING iff (YOUNG_MARKING or OLD_MARKING), gc_state is: %x", actual);
847
848 // Old generation marking is allowed in all states.
849 if (!VerifyThreadGCState::verify_gc_state(actual, expected)) {
850 fatal("%s: Global gc-state: expected %d, actual %d", label, expected, actual);
851 }
852
853 VerifyThreadGCState vtgcs(label, expected);
854 Threads::java_threads_do(&vtgcs);
855 }
856 }
857
858 // Deactivate barriers temporarily: Verifier wants plain heap accesses
859 ShenandoahGCStateResetter resetter;
860
861 // Heap size checks
862 {
863 ShenandoahHeapLocker lock(_heap->lock());
864
865 ShenandoahCalculateRegionStatsClosure cl;
866 _heap->heap_region_iterate(&cl);
867 size_t heap_used;
868 if (_heap->mode()->is_generational() && (sizeness == _verify_size_adjusted_for_padding)) {
869 // Prior to evacuation, regular regions that are to be evacuated in place are padded to prevent further allocations
870 heap_used = _heap->used() + _heap->old_generation()->get_pad_for_promote_in_place();
871 } else if (sizeness != _verify_size_disable) {
872 heap_used = _heap->used();
873 }
874 if (sizeness != _verify_size_disable) {
875 guarantee(cl.used() == heap_used,
876 "%s: heap used size must be consistent: heap-used = " SIZE_FORMAT "%s, regions-used = " SIZE_FORMAT "%s",
877 label,
878 byte_size_in_proper_unit(heap_used), proper_unit_for_byte_size(heap_used),
879 byte_size_in_proper_unit(cl.used()), proper_unit_for_byte_size(cl.used()));
880 }
881 size_t heap_committed = _heap->committed();
882 guarantee(cl.committed() == heap_committed,
883 "%s: heap committed size must be consistent: heap-committed = " SIZE_FORMAT "%s, regions-committed = " SIZE_FORMAT "%s",
884 label,
885 byte_size_in_proper_unit(heap_committed), proper_unit_for_byte_size(heap_committed),
886 byte_size_in_proper_unit(cl.committed()), proper_unit_for_byte_size(cl.committed()));
887 }
888
889 log_debug(gc)("Safepoint verification finished heap usage verification");
890
891 ShenandoahGeneration* generation;
892 if (_heap->mode()->is_generational()) {
893 generation = _heap->gc_generation();
894 guarantee(generation != nullptr, "Need to know which generation to verify.");
895 shenandoah_assert_generations_reconciled();
896 } else {
897 generation = nullptr;
898 }
899
900 if (generation != nullptr) {
901 ShenandoahHeapLocker lock(_heap->lock());
902
903 switch (remembered) {
904 case _verify_remembered_disable:
905 break;
906 case _verify_remembered_before_marking:
907 log_debug(gc)("Safepoint verification of remembered set at mark");
908 verify_rem_set_before_mark();
909 break;
910 case _verify_remembered_before_updating_references:
911 log_debug(gc)("Safepoint verification of remembered set at update ref");
912 verify_rem_set_before_update_ref();
913 break;
914 case _verify_remembered_after_full_gc:
915 log_debug(gc)("Safepoint verification of remembered set after full gc");
916 verify_rem_set_after_full_gc();
917 break;
918 default:
919 fatal("Unhandled remembered set verification mode");
920 }
921
922 ShenandoahGenerationStatsClosure cl;
923 _heap->heap_region_iterate(&cl);
924
925 if (LogTarget(Debug, gc)::is_enabled()) {
926 ShenandoahGenerationStatsClosure::log_usage(_heap->old_generation(), cl.old);
927 ShenandoahGenerationStatsClosure::log_usage(_heap->young_generation(), cl.young);
928 ShenandoahGenerationStatsClosure::log_usage(_heap->global_generation(), cl.global);
929 }
930 if (sizeness == _verify_size_adjusted_for_padding) {
931 ShenandoahGenerationStatsClosure::validate_usage(false, label, _heap->old_generation(), cl.old);
932 ShenandoahGenerationStatsClosure::validate_usage(true, label, _heap->young_generation(), cl.young);
933 ShenandoahGenerationStatsClosure::validate_usage(true, label, _heap->global_generation(), cl.global);
934 } else if (sizeness == _verify_size_exact) {
935 ShenandoahGenerationStatsClosure::validate_usage(false, label, _heap->old_generation(), cl.old);
936 ShenandoahGenerationStatsClosure::validate_usage(false, label, _heap->young_generation(), cl.young);
937 ShenandoahGenerationStatsClosure::validate_usage(false, label, _heap->global_generation(), cl.global);
938 }
939 // else: sizeness must equal _verify_size_disable
940 }
941
942 log_debug(gc)("Safepoint verification finished remembered set verification");
943
944 // Internal heap region checks
945 if (ShenandoahVerifyLevel >= 1) {
946 ShenandoahVerifyHeapRegionClosure cl(label, regions);
947 if (generation != nullptr) {
948 generation->heap_region_iterate(&cl);
949 } else {
950 _heap->heap_region_iterate(&cl);
951 }
952 }
953
954 log_debug(gc)("Safepoint verification finished heap region closure verification");
955
956 OrderAccess::fence();
957
958 if (UseTLAB) {
959 _heap->labs_make_parsable();
960 }
961
962 // Allocate temporary bitmap for storing marking wavefront:
963 _verification_bit_map->clear();
964
965 // Allocate temporary array for storing liveness data
966 ShenandoahLivenessData* ld = NEW_C_HEAP_ARRAY(ShenandoahLivenessData, _heap->num_regions(), mtGC);
967 Copy::fill_to_bytes((void*)ld, _heap->num_regions()*sizeof(ShenandoahLivenessData), 0);
968
969 const VerifyOptions& options = ShenandoahVerifier::VerifyOptions(forwarded, marked, cset, liveness, regions, gcstate);
970
971 // Steps 1-2. Scan root set to get initial reachable set. Finish walking the reachable heap.
972 // This verifies what application can see, since it only cares about reachable objects.
973 size_t count_reachable = 0;
974 if (ShenandoahVerifyLevel >= 2) {
975 ShenandoahVerifierReachableTask task(_verification_bit_map, ld, label, options);
976 _heap->workers()->run_task(&task);
977 count_reachable = task.processed();
978 }
979
980 log_debug(gc)("Safepoint verification finished getting initial reachable set");
981
982 // Step 3. Walk marked objects. Marked objects might be unreachable. This verifies what collector,
983 // not the application, can see during the region scans. There is no reason to process the objects
984 // that were already verified, e.g. those marked in verification bitmap. There is interaction with TAMS:
985 // before TAMS, we verify the bitmaps, if available; after TAMS, we walk until the top(). It mimics
986 // what marked_object_iterate is doing, without calling into that optimized (and possibly incorrect)
987 // version
988
989 size_t count_marked = 0;
990 if (ShenandoahVerifyLevel >= 4 &&
991 (marked == _verify_marked_complete ||
992 marked == _verify_marked_complete_except_references ||
993 marked == _verify_marked_complete_satb_empty)) {
994 guarantee(_heap->gc_generation()->is_mark_complete(), "Marking context should be complete");
995 ShenandoahVerifierMarkedRegionTask task(_verification_bit_map, ld, label, options);
996 _heap->workers()->run_task(&task);
997 count_marked = task.processed();
998 } else {
999 guarantee(ShenandoahVerifyLevel < 4 || marked == _verify_marked_incomplete || marked == _verify_marked_disable, "Should be");
1000 }
1001
1002 log_debug(gc)("Safepoint verification finished walking marked objects");
1003
1004 // Step 4. Verify accumulated liveness data, if needed. Only reliable if verification level includes
1005 // marked objects.
1006
1007 if (ShenandoahVerifyLevel >= 4 && marked == _verify_marked_complete && liveness == _verify_liveness_complete) {
1008 for (size_t i = 0; i < _heap->num_regions(); i++) {
1009 ShenandoahHeapRegion* r = _heap->get_region(i);
1010 if (generation != nullptr && !generation->contains(r)) {
1011 continue;
1012 }
1013
1014 juint verf_live = 0;
1015 if (r->is_humongous()) {
1016 // For humongous objects, test if start region is marked live, and if so,
1017 // all humongous regions in that chain have live data equal to their "used".
1018 juint start_live = Atomic::load(&ld[r->humongous_start_region()->index()]);
1019 if (start_live > 0) {
1020 verf_live = (juint)(r->used() / HeapWordSize);
1021 }
1022 } else {
1023 verf_live = Atomic::load(&ld[r->index()]);
1024 }
1025
1026 size_t reg_live = r->get_live_data_words();
1027 if (reg_live != verf_live) {
1028 stringStream ss;
1029 r->print_on(&ss);
1030 fatal("%s: Live data should match: region-live = " SIZE_FORMAT ", verifier-live = " UINT32_FORMAT "\n%s",
1031 label, reg_live, verf_live, ss.freeze());
1032 }
1033 }
1034 }
1035
1036 log_debug(gc)("Safepoint verification finished accumulation of liveness data");
1037
1038
1039 log_info(gc)("Verify %s, Level " INTX_FORMAT " (" SIZE_FORMAT " reachable, " SIZE_FORMAT " marked)",
1040 label, ShenandoahVerifyLevel, count_reachable, count_marked);
1041
1042 FREE_C_HEAP_ARRAY(ShenandoahLivenessData, ld);
1043 }
1044
1045 void ShenandoahVerifier::verify_generic(VerifyOption vo) {
1046 verify_at_safepoint(
1047 "Generic Verification",
1048 _verify_remembered_disable, // do not verify remembered set
1049 _verify_forwarded_allow, // conservatively allow forwarded
1050 _verify_marked_disable, // do not verify marked: lots ot time wasted checking dead allocations
1051 _verify_cset_disable, // cset may be inconsistent
1052 _verify_liveness_disable, // no reliable liveness data
1053 _verify_regions_disable, // no reliable region data
1054 _verify_size_exact, // expect generation and heap sizes to match exactly
1055 _verify_gcstate_disable // no data about gcstate
1056 );
1057 }
1058
1059 void ShenandoahVerifier::verify_before_concmark() {
1060 VerifyRememberedSet verify_remembered_set = _verify_remembered_before_marking;
1061 if (_heap->mode()->is_generational() &&
1062 !_heap->old_generation()->is_mark_complete()) {
1063 // Before marking in generational mode, remembered set can't be verified w/o complete old marking.
1064 verify_remembered_set = _verify_remembered_disable;
1065 }
1066 verify_at_safepoint(
1067 "Before Mark",
1068 verify_remembered_set,
1069 // verify read-only remembered set from bottom() to top()
1070 _verify_forwarded_none, // UR should have fixed up
1071 _verify_marked_disable, // do not verify marked: lots ot time wasted checking dead allocations
1072 _verify_cset_none, // UR should have fixed this
1073 _verify_liveness_disable, // no reliable liveness data
1074 _verify_regions_notrash, // no trash regions
1075 _verify_size_exact, // expect generation and heap sizes to match exactly
1076 _verify_gcstate_stable // there are no forwarded objects
1077 );
1078 }
1079
1080 void ShenandoahVerifier::verify_after_concmark() {
1081 verify_at_safepoint(
1082 "After Mark",
1083 _verify_remembered_disable, // do not verify remembered set
1084 _verify_forwarded_none, // no forwarded references
1085 _verify_marked_complete_satb_empty, // bitmaps as precise as we can get, except dangling j.l.r.Refs
1086 _verify_cset_none, // no references to cset anymore
1087 _verify_liveness_complete, // liveness data must be complete here
1088 _verify_regions_disable, // trash regions not yet recycled
1089 _verify_size_exact, // expect generation and heap sizes to match exactly
1090 _verify_gcstate_stable_weakroots // heap is still stable, weakroots are in progress
1091 );
1092 }
1093
1094 void ShenandoahVerifier::verify_after_concmark_with_promotions() {
1095 verify_at_safepoint(
1096 "After Mark",
1097 _verify_remembered_disable, // do not verify remembered set
1098 _verify_forwarded_none, // no forwarded references
1099 _verify_marked_complete_satb_empty, // bitmaps as precise as we can get, except dangling j.l.r.Refs
1100 _verify_cset_none, // no references to cset anymore
1101 _verify_liveness_complete, // liveness data must be complete here
1102 _verify_regions_disable, // trash regions not yet recycled
1103 _verify_size_adjusted_for_padding, // expect generation and heap sizes to match after adjustments
1104 // for promote in place padding
1105 _verify_gcstate_stable_weakroots // heap is still stable, weakroots are in progress
1106 );
1107 }
1108
1109 void ShenandoahVerifier::verify_before_evacuation() {
1110 verify_at_safepoint(
1111 "Before Evacuation",
1112 _verify_remembered_disable, // do not verify remembered set
1113 _verify_forwarded_none, // no forwarded references
1114 _verify_marked_complete_except_references, // walk over marked objects too
1115 _verify_cset_disable, // non-forwarded references to cset expected
1116 _verify_liveness_complete, // liveness data must be complete here
1117 _verify_regions_disable, // trash regions not yet recycled
1118 _verify_size_adjusted_for_padding, // expect generation and heap sizes to match after adjustments
1119 // for promote in place padding
1120 _verify_gcstate_stable_weakroots // heap is still stable, weakroots are in progress
1121 );
1122 }
1123
1124 void ShenandoahVerifier::verify_before_update_refs() {
1125 VerifyRememberedSet verify_remembered_set = _verify_remembered_before_updating_references;
1126 if (_heap->mode()->is_generational() &&
1127 !_heap->old_generation()->is_mark_complete()) {
1128 verify_remembered_set = _verify_remembered_disable;
1129 }
1130 verify_at_safepoint(
1131 "Before Updating References",
1132 verify_remembered_set, // verify read-write remembered set
1133 _verify_forwarded_allow, // forwarded references allowed
1134 _verify_marked_complete, // bitmaps might be stale, but alloc-after-mark should be well
1135 _verify_cset_forwarded, // all cset refs are fully forwarded
1136 _verify_liveness_disable, // no reliable liveness data anymore
1137 _verify_regions_notrash, // trash regions have been recycled already
1138 _verify_size_exact, // expect generation and heap sizes to match exactly
1139 _verify_gcstate_updating // evacuation should have produced some forwarded objects
1140 );
1141 }
1142
1143 // We have not yet cleanup (reclaimed) the collection set
1144 void ShenandoahVerifier::verify_after_update_refs() {
1145 verify_at_safepoint(
1146 "After Updating References",
1147 _verify_remembered_disable, // do not verify remembered set
1148 _verify_forwarded_none, // no forwarded references
1149 _verify_marked_complete, // bitmaps might be stale, but alloc-after-mark should be well
1150 _verify_cset_none, // no cset references, all updated
1151 _verify_liveness_disable, // no reliable liveness data anymore
1152 _verify_regions_nocset, // no cset regions, trash regions have appeared
1153 _verify_size_exact, // expect generation and heap sizes to match exactly
1154 _verify_gcstate_stable // update refs had cleaned up forwarded objects
1155 );
1156 }
1157
1158 void ShenandoahVerifier::verify_after_degenerated() {
1159 verify_at_safepoint(
1160 "After Degenerated GC",
1161 _verify_remembered_disable, // do not verify remembered set
1162 _verify_forwarded_none, // all objects are non-forwarded
1163 _verify_marked_complete, // all objects are marked in complete bitmap
1164 _verify_cset_none, // no cset references
1165 _verify_liveness_disable, // no reliable liveness data anymore
1166 _verify_regions_notrash_nocset, // no trash, no cset
1167 _verify_size_exact, // expect generation and heap sizes to match exactly
1168 _verify_gcstate_stable // degenerated refs had cleaned up forwarded objects
1169 );
1170 }
1171
1172 void ShenandoahVerifier::verify_before_fullgc() {
1173 verify_at_safepoint(
1174 "Before Full GC",
1175 _verify_remembered_disable, // do not verify remembered set
1176 _verify_forwarded_allow, // can have forwarded objects
1177 _verify_marked_disable, // do not verify marked: lots ot time wasted checking dead allocations
1178 _verify_cset_disable, // cset might be foobared
1179 _verify_liveness_disable, // no reliable liveness data anymore
1180 _verify_regions_disable, // no reliable region data here
1181 _verify_size_disable, // if we degenerate during evacuation, usage not valid: padding and deferred accounting
1182 _verify_gcstate_disable // no reliable gcstate data
1183 );
1184 }
1185
1186 void ShenandoahVerifier::verify_after_fullgc() {
1187 verify_at_safepoint(
1188 "After Full GC",
1189 _verify_remembered_after_full_gc, // verify read-write remembered set
1190 _verify_forwarded_none, // all objects are non-forwarded
1191 _verify_marked_incomplete, // all objects are marked in incomplete bitmap
1192 _verify_cset_none, // no cset references
1193 _verify_liveness_disable, // no reliable liveness data anymore
1194 _verify_regions_notrash_nocset, // no trash, no cset
1195 _verify_size_exact, // expect generation and heap sizes to match exactly
1196 _verify_gcstate_stable // full gc cleaned up everything
1197 );
1198 }
1199
1200 class ShenandoahVerifyNoForwarded : public BasicOopIterateClosure {
1201 private:
1202 template <class T>
1203 void do_oop_work(T* p) {
1204 T o = RawAccess<>::oop_load(p);
1205 if (!CompressedOops::is_null(o)) {
1206 oop obj = CompressedOops::decode_not_null(o);
1207 oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj);
1208 if (obj != fwd) {
1209 ShenandoahAsserts::print_failure(ShenandoahAsserts::_safe_all, obj, p, nullptr,
1210 "Verify Roots", "Should not be forwarded", __FILE__, __LINE__);
1211 }
1212 }
1213 }
1214
1215 public:
1216 void do_oop(narrowOop* p) { do_oop_work(p); }
1217 void do_oop(oop* p) { do_oop_work(p); }
1218 };
1219
1220 class ShenandoahVerifyInToSpaceClosure : public BasicOopIterateClosure {
1221 private:
1222 template <class T>
1223 void do_oop_work(T* p) {
1224 T o = RawAccess<>::oop_load(p);
1225 if (!CompressedOops::is_null(o)) {
1226 oop obj = CompressedOops::decode_not_null(o);
1227 ShenandoahHeap* heap = ShenandoahHeap::heap();
1228
1229 if (!heap->marking_context()->is_marked_or_old(obj)) {
1230 ShenandoahAsserts::print_failure(ShenandoahAsserts::_safe_all, obj, p, nullptr,
1231 "Verify Roots In To-Space", "Should be marked", __FILE__, __LINE__);
1232 }
1233
1234 if (heap->in_collection_set(obj)) {
1235 ShenandoahAsserts::print_failure(ShenandoahAsserts::_safe_all, obj, p, nullptr,
1236 "Verify Roots In To-Space", "Should not be in collection set", __FILE__, __LINE__);
1237 }
1238
1239 oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj);
1240 if (obj != fwd) {
1241 ShenandoahAsserts::print_failure(ShenandoahAsserts::_safe_all, obj, p, nullptr,
1242 "Verify Roots In To-Space", "Should not be forwarded", __FILE__, __LINE__);
1243 }
1244 }
1245 }
1246
1247 public:
1248 void do_oop(narrowOop* p) override { do_oop_work(p); }
1249 void do_oop(oop* p) override { do_oop_work(p); }
1250 };
1251
1252 void ShenandoahVerifier::verify_roots_in_to_space() {
1253 ShenandoahVerifyInToSpaceClosure cl;
1254 ShenandoahRootVerifier::roots_do(&cl);
1255 }
1256
1257 void ShenandoahVerifier::verify_roots_no_forwarded() {
1258 ShenandoahVerifyNoForwarded cl;
1259 ShenandoahRootVerifier::roots_do(&cl);
1260 }
1261
1262 template<typename Scanner>
1263 class ShenandoahVerifyRemSetClosure : public BasicOopIterateClosure {
1264 protected:
1265 ShenandoahGenerationalHeap* const _heap;
1266 Scanner* const _scanner;
1267 const char* _message;
1268
1269 public:
1270 // Argument distinguishes between initial mark or start of update refs verification.
1271 explicit ShenandoahVerifyRemSetClosure(Scanner* scanner, const char* message) :
1272 _heap(ShenandoahGenerationalHeap::heap()),
1273 _scanner(scanner),
1274 _message(message) {}
1275
1276 template<class T>
1277 inline void work(T* p) {
1278 T o = RawAccess<>::oop_load(p);
1279 if (!CompressedOops::is_null(o)) {
1280 oop obj = CompressedOops::decode_not_null(o);
1281 if (_heap->is_in_young(obj) && !_scanner->is_card_dirty((HeapWord*) p)) {
1282 ShenandoahAsserts::print_failure(ShenandoahAsserts::_safe_all, obj, p, nullptr,
1283 _message, "clean card, it should be dirty.", __FILE__, __LINE__);
1284 }
1285 }
1286 }
1287
1288 void do_oop(narrowOop* p) override { work(p); }
1289 void do_oop(oop* p) override { work(p); }
1290 };
1291
1292 template<typename Scanner>
1293 void ShenandoahVerifier::help_verify_region_rem_set(Scanner* scanner, ShenandoahHeapRegion* r,
1294 HeapWord* registration_watermark, const char* message) {
1295 shenandoah_assert_generations_reconciled();
1296 ShenandoahOldGeneration* old_gen = _heap->old_generation();
1297 assert(old_gen->is_mark_complete() || old_gen->is_parsable(), "Sanity");
1298
1299 ShenandoahMarkingContext* ctx = old_gen->is_mark_complete() ? old_gen->complete_marking_context() : nullptr;
1300 ShenandoahVerifyRemSetClosure<Scanner> check_interesting_pointers(scanner, message);
1301 HeapWord* from = r->bottom();
1302 HeapWord* obj_addr = from;
1303 if (r->is_humongous_start()) {
1304 oop obj = cast_to_oop(obj_addr);
1305 if ((ctx == nullptr) || ctx->is_marked(obj)) {
1306 // For humongous objects, the typical object is an array, so the following checks may be overkill
1307 // For regular objects (not object arrays), if the card holding the start of the object is dirty,
1308 // we do not need to verify that cards spanning interesting pointers within this object are dirty.
1309 if (!scanner->is_card_dirty(obj_addr) || obj->is_objArray()) {
1310 obj->oop_iterate(&check_interesting_pointers);
1311 }
1312 // else, object's start is marked dirty and obj is not an objArray, so any interesting pointers are covered
1313 }
1314 // else, this humongous object is not live so no need to verify its internal pointers
1315
1316 if ((obj_addr < registration_watermark) && !scanner->verify_registration(obj_addr, ctx)) {
1317 ShenandoahAsserts::print_failure(ShenandoahAsserts::_safe_all, obj, obj_addr, nullptr, message,
1318 "object not properly registered", __FILE__, __LINE__);
1319 }
1320 } else if (!r->is_humongous()) {
1321 HeapWord* top = r->top();
1322 while (obj_addr < top) {
1323 oop obj = cast_to_oop(obj_addr);
1324 // ctx->is_marked() returns true if mark bit set or if obj above TAMS.
1325 if ((ctx == nullptr) || ctx->is_marked(obj)) {
1326 // For regular objects (not object arrays), if the card holding the start of the object is dirty,
1327 // we do not need to verify that cards spanning interesting pointers within this object are dirty.
1328 if (!scanner->is_card_dirty(obj_addr) || obj->is_objArray()) {
1329 obj->oop_iterate(&check_interesting_pointers);
1330 }
1331 // else, object's start is marked dirty and obj is not an objArray, so any interesting pointers are covered
1332
1333 if ((obj_addr < registration_watermark) && !scanner->verify_registration(obj_addr, ctx)) {
1334 ShenandoahAsserts::print_failure(ShenandoahAsserts::_safe_all, obj, obj_addr, nullptr, message,
1335 "object not properly registered", __FILE__, __LINE__);
1336 }
1337 obj_addr += obj->size();
1338 } else {
1339 // This object is not live so we don't verify dirty cards contained therein
1340 HeapWord* tams = ctx->top_at_mark_start(r);
1341 obj_addr = ctx->get_next_marked_addr(obj_addr, tams);
1342 }
1343 }
1344 }
1345 }
1346
1347 class ShenandoahWriteTableScanner {
1348 private:
1349 ShenandoahScanRemembered* _scanner;
1350 public:
1351 explicit ShenandoahWriteTableScanner(ShenandoahScanRemembered* scanner) : _scanner(scanner) {}
1352
1353 bool is_card_dirty(HeapWord* obj_addr) {
1354 return _scanner->is_write_card_dirty(obj_addr);
1355 }
1356
1357 bool verify_registration(HeapWord* obj_addr, ShenandoahMarkingContext* ctx) {
1358 return _scanner->verify_registration(obj_addr, ctx);
1359 }
1360 };
1361
1362 // Assure that the remember set has a dirty card everywhere there is an interesting pointer.
1363 // This examines the read_card_table between bottom() and top() since all PLABS are retired
1364 // before the safepoint for init_mark. Actually, we retire them before update-references and don't
1365 // restore them until the start of evacuation.
1366 void ShenandoahVerifier::verify_rem_set_before_mark() {
1367 shenandoah_assert_safepoint();
1368 shenandoah_assert_generational();
1369
1370 ShenandoahOldGeneration* old_generation = _heap->old_generation();
1371
1372 log_debug(gc)("Verifying remembered set at %s mark", old_generation->is_doing_mixed_evacuations() ? "mixed" : "young");
1373
1374 ShenandoahScanRemembered* scanner = old_generation->card_scan();
1375 for (size_t i = 0, n = _heap->num_regions(); i < n; ++i) {
1376 ShenandoahHeapRegion* r = _heap->get_region(i);
1377 if (r->is_old() && r->is_active()) {
1378 help_verify_region_rem_set(scanner, r, r->end(), "Verify init-mark remembered set violation");
1379 }
1380 }
1381 }
1382
1383 void ShenandoahVerifier::verify_rem_set_after_full_gc() {
1384 shenandoah_assert_safepoint();
1385 shenandoah_assert_generational();
1386
1387 ShenandoahWriteTableScanner scanner(ShenandoahGenerationalHeap::heap()->old_generation()->card_scan());
1388 for (size_t i = 0, n = _heap->num_regions(); i < n; ++i) {
1389 ShenandoahHeapRegion* r = _heap->get_region(i);
1390 if (r->is_old() && !r->is_cset()) {
1391 help_verify_region_rem_set(&scanner, r, r->top(), "Remembered set violation at end of Full GC");
1392 }
1393 }
1394 }
1395
1396 // Assure that the remember set has a dirty card everywhere there is an interesting pointer. Even though
1397 // the update-references scan of remembered set only examines cards up to update_watermark, the remembered
1398 // set should be valid through top. This examines the write_card_table between bottom() and top() because
1399 // all PLABS are retired immediately before the start of update refs.
1400 void ShenandoahVerifier::verify_rem_set_before_update_ref() {
1401 shenandoah_assert_safepoint();
1402 shenandoah_assert_generational();
1403
1404 ShenandoahWriteTableScanner scanner(_heap->old_generation()->card_scan());
1405 for (size_t i = 0, n = _heap->num_regions(); i < n; ++i) {
1406 ShenandoahHeapRegion* r = _heap->get_region(i);
1407 if (r->is_old() && !r->is_cset()) {
1408 help_verify_region_rem_set(&scanner, r, r->get_update_watermark(), "Remembered set violation at init-update-references");
1409 }
1410 }
1411 }
1412
1413 void ShenandoahVerifier::verify_before_rebuilding_free_set() {
1414 ShenandoahGenerationStatsClosure cl;
1415 _heap->heap_region_iterate(&cl);
1416
1417 ShenandoahGenerationStatsClosure::validate_usage(false, "Before free set rebuild", _heap->old_generation(), cl.old);
1418 ShenandoahGenerationStatsClosure::validate_usage(false, "Before free set rebuild", _heap->young_generation(), cl.young);
1419 ShenandoahGenerationStatsClosure::validate_usage(false, "Before free set rebuild", _heap->global_generation(), cl.global);
1420 }
|