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