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/shenandoahObjectUtils.inline.hpp" 33 #include "gc/shenandoah/shenandoahRootProcessor.hpp" 34 #include "gc/shenandoah/shenandoahTaskqueue.inline.hpp" 35 #include "gc/shenandoah/shenandoahUtils.hpp" 36 #include "gc/shenandoah/shenandoahVerifier.hpp" 37 #include "memory/allocation.hpp" 38 #include "memory/iterator.inline.hpp" 39 #include "memory/resourceArea.hpp" 40 #include "oops/compressedOops.inline.hpp" 41 #include "runtime/atomic.hpp" 42 #include "runtime/orderAccess.hpp" 43 #include "runtime/threads.hpp" 44 #include "utilities/align.hpp" 45 46 // Avoid name collision on verify_oop (defined in macroAssembler_arm.hpp) 47 #ifdef verify_oop 48 #undef verify_oop 49 #endif 50 51 static bool is_instance_ref_klass(Klass* k) { 52 return k->is_instance_klass() && InstanceKlass::cast(k)->reference_type() != REF_NONE; 53 } 54 55 class ShenandoahIgnoreReferenceDiscoverer : public ReferenceDiscoverer { 56 public: 57 virtual bool discover_reference(oop obj, ReferenceType type) { 58 return true; 59 } 60 }; 61 62 class ShenandoahVerifyOopClosure : public BasicOopIterateClosure { 63 private: 64 const char* _phase; 65 ShenandoahVerifier::VerifyOptions _options; 66 ShenandoahVerifierStack* _stack; 67 ShenandoahHeap* _heap; 68 MarkBitMap* _map; 69 ShenandoahLivenessData* _ld; 70 void* _interior_loc; 71 oop _loc; 72 73 public: 74 ShenandoahVerifyOopClosure(ShenandoahVerifierStack* stack, MarkBitMap* map, ShenandoahLivenessData* ld, 75 const char* phase, ShenandoahVerifier::VerifyOptions options) : 76 _phase(phase), 77 _options(options), 78 _stack(stack), 79 _heap(ShenandoahHeap::heap()), 80 _map(map), 81 _ld(ld), 82 _interior_loc(nullptr), 83 _loc(nullptr) { 84 if (options._verify_marked == ShenandoahVerifier::_verify_marked_complete_except_references || 85 options._verify_marked == ShenandoahVerifier::_verify_marked_disable) { 86 set_ref_discoverer_internal(new ShenandoahIgnoreReferenceDiscoverer()); 87 } 88 } 89 90 private: 91 void check(ShenandoahAsserts::SafeLevel level, oop obj, bool test, const char* label) { 92 if (!test) { 93 ShenandoahAsserts::print_failure(level, obj, _interior_loc, _loc, _phase, label, __FILE__, __LINE__); 94 } 95 } 96 97 template <class T> 98 void do_oop_work(T* p) { 99 T o = RawAccess<>::oop_load(p); 100 if (!CompressedOops::is_null(o)) { 101 oop obj = CompressedOops::decode_not_null(o); 102 if (is_instance_ref_klass(ShenandoahObjectUtils::klass(obj))) { 103 obj = ShenandoahForwarding::get_forwardee(obj); 104 } 105 // Single threaded verification can use faster non-atomic stack and bitmap 106 // methods. 107 // 108 // For performance reasons, only fully verify non-marked field values. 109 // We are here when the host object for *p is already marked. 110 111 if (_map->par_mark(obj)) { 112 verify_oop_at(p, obj); 113 _stack->push(ShenandoahVerifierTask(obj)); 114 } 115 } 116 } 117 118 void verify_oop(oop obj) { 119 // Perform consistency checks with gradually decreasing safety level. This guarantees 120 // that failure report would not try to touch something that was not yet verified to be 121 // safe to process. 122 123 check(ShenandoahAsserts::_safe_unknown, obj, _heap->is_in(obj), 124 "oop must be in heap"); 125 check(ShenandoahAsserts::_safe_unknown, obj, is_object_aligned(obj), 126 "oop must be aligned"); 127 128 ShenandoahHeapRegion *obj_reg = _heap->heap_region_containing(obj); 129 Klass* obj_klass = ShenandoahObjectUtils::klass(obj); 130 131 // Verify that obj is not in dead space: 132 { 133 // Do this before touching obj->size() 134 check(ShenandoahAsserts::_safe_unknown, obj, obj_klass != nullptr, 135 "Object klass pointer should not be null"); 136 check(ShenandoahAsserts::_safe_unknown, obj, Metaspace::contains(obj_klass), 137 "Object klass pointer must go to metaspace"); 138 139 HeapWord *obj_addr = cast_from_oop<HeapWord*>(obj); 140 check(ShenandoahAsserts::_safe_unknown, obj, obj_addr < obj_reg->top(), 141 "Object start should be within the region"); 142 143 if (!obj_reg->is_humongous()) { 144 check(ShenandoahAsserts::_safe_unknown, obj, (obj_addr + ShenandoahObjectUtils::size(obj)) <= obj_reg->top(), 145 "Object end should be within the region"); 146 } else { 147 size_t humongous_start = obj_reg->index(); 148 size_t humongous_end = humongous_start + (ShenandoahObjectUtils::size(obj) >> ShenandoahHeapRegion::region_size_words_shift()); 149 for (size_t idx = humongous_start + 1; idx < humongous_end; idx++) { 150 check(ShenandoahAsserts::_safe_unknown, obj, _heap->get_region(idx)->is_humongous_continuation(), 151 "Humongous object is in continuation that fits it"); 152 } 153 } 154 155 // ------------ obj is safe at this point -------------- 156 157 check(ShenandoahAsserts::_safe_oop, obj, obj_reg->is_active(), 158 "Object should be in active region"); 159 160 switch (_options._verify_liveness) { 161 case ShenandoahVerifier::_verify_liveness_disable: 162 // skip 163 break; 164 case ShenandoahVerifier::_verify_liveness_complete: 165 Atomic::add(&_ld[obj_reg->index()], (uint) ShenandoahObjectUtils::size(obj), memory_order_relaxed); 166 // fallthrough for fast failure for un-live regions: 167 case ShenandoahVerifier::_verify_liveness_conservative: 168 check(ShenandoahAsserts::_safe_oop, obj, obj_reg->has_live(), 169 "Object must belong to region with live data"); 170 break; 171 default: 172 assert(false, "Unhandled liveness verification"); 173 } 174 } 175 176 oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj); 177 178 ShenandoahHeapRegion* fwd_reg = nullptr; 179 180 if (obj != fwd) { 181 check(ShenandoahAsserts::_safe_oop, obj, _heap->is_in(fwd), 182 "Forwardee must be in heap"); 183 check(ShenandoahAsserts::_safe_oop, obj, !CompressedOops::is_null(fwd), 184 "Forwardee is set"); 185 check(ShenandoahAsserts::_safe_oop, obj, is_object_aligned(fwd), 186 "Forwardee must be aligned"); 187 188 // Do this before touching fwd->size() 189 Klass* fwd_klass = fwd->klass_or_null(); 190 check(ShenandoahAsserts::_safe_oop, obj, fwd_klass != nullptr, 191 "Forwardee klass pointer should not be null"); 192 check(ShenandoahAsserts::_safe_oop, obj, Metaspace::contains(fwd_klass), 193 "Forwardee klass pointer must go to metaspace"); 194 check(ShenandoahAsserts::_safe_oop, obj, obj_klass == fwd_klass, 195 "Forwardee klass pointer must go to metaspace"); 196 197 fwd_reg = _heap->heap_region_containing(fwd); 198 199 // Verify that forwardee is not in the dead space: 200 check(ShenandoahAsserts::_safe_oop, obj, !fwd_reg->is_humongous(), 201 "Should have no humongous forwardees"); 202 203 HeapWord *fwd_addr = cast_from_oop<HeapWord *>(fwd); 204 check(ShenandoahAsserts::_safe_oop, obj, fwd_addr < fwd_reg->top(), 205 "Forwardee start should be within the region"); 206 check(ShenandoahAsserts::_safe_oop, obj, (fwd_addr + ShenandoahObjectUtils::size(fwd)) <= fwd_reg->top(), 207 "Forwardee end should be within the region"); 208 209 oop fwd2 = ShenandoahForwarding::get_forwardee_raw_unchecked(fwd); 210 check(ShenandoahAsserts::_safe_oop, obj, (fwd == fwd2), 211 "Double forwarding"); 212 } else { 213 fwd_reg = obj_reg; 214 } 215 216 // ------------ obj and fwd are safe at this point -------------- 217 218 switch (_options._verify_marked) { 219 case ShenandoahVerifier::_verify_marked_disable: 220 // skip 221 break; 222 case ShenandoahVerifier::_verify_marked_incomplete: 223 check(ShenandoahAsserts::_safe_all, obj, _heap->marking_context()->is_marked(obj), 224 "Must be marked in incomplete bitmap"); 225 break; 226 case ShenandoahVerifier::_verify_marked_complete: 227 check(ShenandoahAsserts::_safe_all, obj, _heap->complete_marking_context()->is_marked(obj), 228 "Must be marked in complete bitmap"); 229 break; 230 case ShenandoahVerifier::_verify_marked_complete_except_references: 231 check(ShenandoahAsserts::_safe_all, obj, _heap->complete_marking_context()->is_marked(obj), 232 "Must be marked in complete bitmap, except j.l.r.Reference referents"); 233 break; 234 default: 235 assert(false, "Unhandled mark verification"); 236 } 237 238 switch (_options._verify_forwarded) { 239 case ShenandoahVerifier::_verify_forwarded_disable: 240 // skip 241 break; 242 case ShenandoahVerifier::_verify_forwarded_none: { 243 check(ShenandoahAsserts::_safe_all, obj, (obj == fwd), 244 "Should not be forwarded"); 245 break; 246 } 247 case ShenandoahVerifier::_verify_forwarded_allow: { 248 if (obj != fwd) { 249 check(ShenandoahAsserts::_safe_all, obj, obj_reg != fwd_reg, 250 "Forwardee should be in another region"); 251 } 252 break; 253 } 254 default: 255 assert(false, "Unhandled forwarding verification"); 256 } 257 258 switch (_options._verify_cset) { 259 case ShenandoahVerifier::_verify_cset_disable: 260 // skip 261 break; 262 case ShenandoahVerifier::_verify_cset_none: 263 check(ShenandoahAsserts::_safe_all, obj, !_heap->in_collection_set(obj), 264 "Should not have references to collection set"); 265 break; 266 case ShenandoahVerifier::_verify_cset_forwarded: 267 if (_heap->in_collection_set(obj)) { 268 check(ShenandoahAsserts::_safe_all, obj, (obj != fwd), 269 "Object in collection set, should have forwardee"); 270 } 271 break; 272 default: 273 assert(false, "Unhandled cset verification"); 274 } 275 276 } 277 278 public: 279 /** 280 * Verify object with known interior reference. 281 * @param p interior reference where the object is referenced from; can be off-heap 282 * @param obj verified object 283 */ 284 template <class T> 285 void verify_oop_at(T* p, oop obj) { 286 _interior_loc = p; 287 verify_oop(obj); 288 _interior_loc = nullptr; 289 } 290 291 /** 292 * Verify object without known interior reference. 293 * Useful when picking up the object at known offset in heap, 294 * but without knowing what objects reference it. 295 * @param obj verified object 296 */ 297 void verify_oop_standalone(oop obj) { 298 _interior_loc = nullptr; 299 verify_oop(obj); 300 _interior_loc = nullptr; 301 } 302 303 /** 304 * Verify oop fields from this object. 305 * @param obj host object for verified fields 306 */ 307 void verify_oops_from(oop obj) { 308 _loc = obj; 309 Klass* klass = ShenandoahObjectUtils::klass(obj); 310 obj->oop_iterate_backwards(this, klass); 311 _loc = nullptr; 312 } 313 314 virtual void do_oop(oop* p) { do_oop_work(p); } 315 virtual void do_oop(narrowOop* p) { do_oop_work(p); } 316 }; 317 318 class ShenandoahCalculateRegionStatsClosure : public ShenandoahHeapRegionClosure { 319 private: 320 size_t _used, _committed, _garbage; 321 public: 322 ShenandoahCalculateRegionStatsClosure() : _used(0), _committed(0), _garbage(0) {}; 323 324 void heap_region_do(ShenandoahHeapRegion* r) { 325 _used += r->used(); 326 _garbage += r->garbage(); 327 _committed += r->is_committed() ? ShenandoahHeapRegion::region_size_bytes() : 0; 328 } 329 330 size_t used() { return _used; } 331 size_t committed() { return _committed; } 332 size_t garbage() { return _garbage; } 333 }; 334 335 class ShenandoahVerifyHeapRegionClosure : public ShenandoahHeapRegionClosure { 336 private: 337 ShenandoahHeap* _heap; 338 const char* _phase; 339 ShenandoahVerifier::VerifyRegions _regions; 340 public: 341 ShenandoahVerifyHeapRegionClosure(const char* phase, ShenandoahVerifier::VerifyRegions regions) : 342 _heap(ShenandoahHeap::heap()), 343 _phase(phase), 344 _regions(regions) {}; 345 346 void print_failure(ShenandoahHeapRegion* r, const char* label) { 347 ResourceMark rm; 348 349 ShenandoahMessageBuffer msg("Shenandoah verification failed; %s: %s\n\n", _phase, label); 350 351 stringStream ss; 352 r->print_on(&ss); 353 msg.append("%s", ss.as_string()); 354 355 report_vm_error(__FILE__, __LINE__, msg.buffer()); 356 } 357 358 void verify(ShenandoahHeapRegion* r, bool test, const char* msg) { 359 if (!test) { 360 print_failure(r, msg); 361 } 362 } 363 364 void heap_region_do(ShenandoahHeapRegion* r) { 365 switch (_regions) { 366 case ShenandoahVerifier::_verify_regions_disable: 367 break; 368 case ShenandoahVerifier::_verify_regions_notrash: 369 verify(r, !r->is_trash(), 370 "Should not have trash regions"); 371 break; 372 case ShenandoahVerifier::_verify_regions_nocset: 373 verify(r, !r->is_cset(), 374 "Should not have cset regions"); 375 break; 376 case ShenandoahVerifier::_verify_regions_notrash_nocset: 377 verify(r, !r->is_trash(), 378 "Should not have trash regions"); 379 verify(r, !r->is_cset(), 380 "Should not have cset regions"); 381 break; 382 default: 383 ShouldNotReachHere(); 384 } 385 386 verify(r, r->capacity() == ShenandoahHeapRegion::region_size_bytes(), 387 "Capacity should match region size"); 388 389 verify(r, r->bottom() <= r->top(), 390 "Region top should not be less than bottom"); 391 392 verify(r, r->bottom() <= _heap->marking_context()->top_at_mark_start(r), 393 "Region TAMS should not be less than bottom"); 394 395 verify(r, _heap->marking_context()->top_at_mark_start(r) <= r->top(), 396 "Complete TAMS should not be larger than top"); 397 398 verify(r, r->get_live_data_bytes() <= r->capacity(), 399 "Live data cannot be larger than capacity"); 400 401 verify(r, r->garbage() <= r->capacity(), 402 "Garbage cannot be larger than capacity"); 403 404 verify(r, r->used() <= r->capacity(), 405 "Used cannot be larger than capacity"); 406 407 verify(r, r->get_shared_allocs() <= r->capacity(), 408 "Shared alloc count should not be larger than capacity"); 409 410 verify(r, r->get_tlab_allocs() <= r->capacity(), 411 "TLAB alloc count should not be larger than capacity"); 412 413 verify(r, r->get_gclab_allocs() <= r->capacity(), 414 "GCLAB alloc count should not be larger than capacity"); 415 416 verify(r, r->get_shared_allocs() + r->get_tlab_allocs() + r->get_gclab_allocs() == r->used(), 417 "Accurate accounting: shared + TLAB + GCLAB = used"); 418 419 verify(r, !r->is_empty() || !r->has_live(), 420 "Empty regions should not have live data"); 421 422 verify(r, r->is_cset() == _heap->collection_set()->is_in(r), 423 "Transitional: region flags and collection set agree"); 424 } 425 }; 426 427 class ShenandoahVerifierReachableTask : public WorkerTask { 428 private: 429 const char* _label; 430 ShenandoahVerifier::VerifyOptions _options; 431 ShenandoahHeap* _heap; 432 ShenandoahLivenessData* _ld; 433 MarkBitMap* _bitmap; 434 volatile size_t _processed; 435 436 public: 437 ShenandoahVerifierReachableTask(MarkBitMap* bitmap, 438 ShenandoahLivenessData* ld, 439 const char* label, 440 ShenandoahVerifier::VerifyOptions options) : 441 WorkerTask("Shenandoah Verifier Reachable Objects"), 442 _label(label), 443 _options(options), 444 _heap(ShenandoahHeap::heap()), 445 _ld(ld), 446 _bitmap(bitmap), 447 _processed(0) {}; 448 449 size_t processed() { 450 return _processed; 451 } 452 453 virtual void work(uint worker_id) { 454 ResourceMark rm; 455 ShenandoahVerifierStack stack; 456 457 // On level 2, we need to only check the roots once. 458 // On level 3, we want to check the roots, and seed the local stack. 459 // It is a lesser evil to accept multiple root scans at level 3, because 460 // extended parallelism would buy us out. 461 if (((ShenandoahVerifyLevel == 2) && (worker_id == 0)) 462 || (ShenandoahVerifyLevel >= 3)) { 463 ShenandoahVerifyOopClosure cl(&stack, _bitmap, _ld, 464 ShenandoahMessageBuffer("%s, Roots", _label), 465 _options); 466 if (_heap->unload_classes()) { 467 ShenandoahRootVerifier::strong_roots_do(&cl); 468 } else { 469 ShenandoahRootVerifier::roots_do(&cl); 470 } 471 } 472 473 size_t processed = 0; 474 475 if (ShenandoahVerifyLevel >= 3) { 476 ShenandoahVerifyOopClosure cl(&stack, _bitmap, _ld, 477 ShenandoahMessageBuffer("%s, Reachable", _label), 478 _options); 479 while (!stack.is_empty()) { 480 processed++; 481 ShenandoahVerifierTask task = stack.pop(); 482 cl.verify_oops_from(task.obj()); 483 } 484 } 485 486 Atomic::add(&_processed, processed, memory_order_relaxed); 487 } 488 }; 489 490 class ShenandoahVerifierMarkedRegionTask : public WorkerTask { 491 private: 492 const char* _label; 493 ShenandoahVerifier::VerifyOptions _options; 494 ShenandoahHeap *_heap; 495 MarkBitMap* _bitmap; 496 ShenandoahLivenessData* _ld; 497 volatile size_t _claimed; 498 volatile size_t _processed; 499 500 public: 501 ShenandoahVerifierMarkedRegionTask(MarkBitMap* bitmap, 502 ShenandoahLivenessData* ld, 503 const char* label, 504 ShenandoahVerifier::VerifyOptions options) : 505 WorkerTask("Shenandoah Verifier Marked Objects"), 506 _label(label), 507 _options(options), 508 _heap(ShenandoahHeap::heap()), 509 _bitmap(bitmap), 510 _ld(ld), 511 _claimed(0), 512 _processed(0) {}; 513 514 size_t processed() { 515 return Atomic::load(&_processed); 516 } 517 518 virtual void work(uint worker_id) { 519 ShenandoahVerifierStack stack; 520 ShenandoahVerifyOopClosure cl(&stack, _bitmap, _ld, 521 ShenandoahMessageBuffer("%s, Marked", _label), 522 _options); 523 524 while (true) { 525 size_t v = Atomic::fetch_and_add(&_claimed, 1u, memory_order_relaxed); 526 if (v < _heap->num_regions()) { 527 ShenandoahHeapRegion* r = _heap->get_region(v); 528 if (!r->is_humongous() && !r->is_trash()) { 529 work_regular(r, stack, cl); 530 } else if (r->is_humongous_start()) { 531 work_humongous(r, stack, cl); 532 } 533 } else { 534 break; 535 } 536 } 537 } 538 539 virtual void work_humongous(ShenandoahHeapRegion *r, ShenandoahVerifierStack& stack, ShenandoahVerifyOopClosure& cl) { 540 size_t processed = 0; 541 HeapWord* obj = r->bottom(); 542 if (_heap->complete_marking_context()->is_marked(cast_to_oop(obj))) { 543 verify_and_follow(obj, stack, cl, &processed); 544 } 545 Atomic::add(&_processed, processed, memory_order_relaxed); 546 } 547 548 virtual void work_regular(ShenandoahHeapRegion *r, ShenandoahVerifierStack &stack, ShenandoahVerifyOopClosure &cl) { 549 size_t processed = 0; 550 ShenandoahMarkingContext* ctx = _heap->complete_marking_context(); 551 HeapWord* tams = ctx->top_at_mark_start(r); 552 553 // Bitmaps, before TAMS 554 if (tams > r->bottom()) { 555 HeapWord* start = r->bottom(); 556 HeapWord* addr = ctx->get_next_marked_addr(start, tams); 557 558 while (addr < tams) { 559 verify_and_follow(addr, stack, cl, &processed); 560 addr += 1; 561 if (addr < tams) { 562 addr = ctx->get_next_marked_addr(addr, tams); 563 } 564 } 565 } 566 567 // Size-based, after TAMS 568 { 569 HeapWord* limit = r->top(); 570 HeapWord* addr = tams; 571 572 while (addr < limit) { 573 verify_and_follow(addr, stack, cl, &processed); 574 addr += cast_to_oop(addr)->size(); 575 } 576 } 577 578 Atomic::add(&_processed, processed, memory_order_relaxed); 579 } 580 581 void verify_and_follow(HeapWord *addr, ShenandoahVerifierStack &stack, ShenandoahVerifyOopClosure &cl, size_t *processed) { 582 if (!_bitmap->par_mark(addr)) return; 583 584 // Verify the object itself: 585 oop obj = cast_to_oop(addr); 586 cl.verify_oop_standalone(obj); 587 588 // Verify everything reachable from that object too, hopefully realizing 589 // everything was already marked, and never touching further: 590 if (!is_instance_ref_klass(ShenandoahObjectUtils::klass(obj))) { 591 cl.verify_oops_from(obj); 592 (*processed)++; 593 } 594 while (!stack.is_empty()) { 595 ShenandoahVerifierTask task = stack.pop(); 596 cl.verify_oops_from(task.obj()); 597 (*processed)++; 598 } 599 } 600 }; 601 602 class VerifyThreadGCState : public ThreadClosure { 603 private: 604 const char* const _label; 605 char const _expected; 606 607 public: 608 VerifyThreadGCState(const char* label, char expected) : _label(label), _expected(expected) {} 609 void do_thread(Thread* t) { 610 char actual = ShenandoahThreadLocalData::gc_state(t); 611 if (actual != _expected) { 612 fatal("%s: Thread %s: expected gc-state %d, actual %d", _label, t->name(), _expected, actual); 613 } 614 } 615 }; 616 617 void ShenandoahVerifier::verify_at_safepoint(const char *label, 618 VerifyForwarded forwarded, VerifyMarked marked, 619 VerifyCollectionSet cset, 620 VerifyLiveness liveness, VerifyRegions regions, 621 VerifyGCState gcstate) { 622 guarantee(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "only when nothing else happens"); 623 guarantee(ShenandoahVerify, "only when enabled, and bitmap is initialized in ShenandoahHeap::initialize"); 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 }