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