1 /* 2 * Copyright (c) 2017, 2020, 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 27 #include "gc/shenandoah/shenandoahAsserts.hpp" 28 #include "gc/shenandoah/shenandoahConcurrentRoots.hpp" 29 #include "gc/shenandoah/shenandoahForwarding.inline.hpp" 30 #include "gc/shenandoah/shenandoahPhaseTimings.hpp" 31 #include "gc/shenandoah/shenandoahHeap.inline.hpp" 32 #include "gc/shenandoah/shenandoahRootProcessor.hpp" 33 #include "gc/shenandoah/shenandoahTaskqueue.inline.hpp" 34 #include "gc/shenandoah/shenandoahUtils.hpp" 35 #include "gc/shenandoah/shenandoahVerifier.hpp" 36 #include "memory/allocation.hpp" 37 #include "memory/iterator.inline.hpp" 38 #include "memory/resourceArea.hpp" 39 #include "oops/compressedOops.inline.hpp" 40 #include "runtime/atomic.hpp" 41 #include "runtime/orderAccess.hpp" 42 #include "utilities/align.hpp" 43 44 // Avoid name collision on verify_oop (defined in macroAssembler_arm.hpp) 45 #ifdef verify_oop 46 #undef verify_oop 47 #endif 48 49 class ShenandoahVerifyOopClosure : public BasicOopIterateClosure { 50 private: 51 const char* _phase; 52 ShenandoahVerifier::VerifyOptions _options; 53 ShenandoahVerifierStack* _stack; 54 ShenandoahHeap* _heap; 55 MarkBitMap* _map; 56 ShenandoahLivenessData* _ld; 57 void* _interior_loc; 58 oop _loc; 59 60 public: 61 ShenandoahVerifyOopClosure(ShenandoahVerifierStack* stack, MarkBitMap* map, ShenandoahLivenessData* ld, 62 const char* phase, ShenandoahVerifier::VerifyOptions options) : 63 _phase(phase), 64 _options(options), 65 _stack(stack), 66 _heap(ShenandoahHeap::heap()), 67 _map(map), 68 _ld(ld), 69 _interior_loc(NULL), 70 _loc(NULL) { } 71 72 private: 73 void check(ShenandoahAsserts::SafeLevel level, oop obj, bool test, const char* label) { 74 if (!test) { 75 ShenandoahAsserts::print_failure(level, obj, _interior_loc, _loc, _phase, label, __FILE__, __LINE__); 76 } 77 } 78 79 template <class T> 80 void do_oop_work(T* p) { 81 T o = RawAccess<>::oop_load(p); 82 if (!CompressedOops::is_null(o)) { 83 oop obj = CompressedOops::decode_not_null(o); 84 85 // Single threaded verification can use faster non-atomic stack and bitmap 86 // methods. 87 // 88 // For performance reasons, only fully verify non-marked field values. 89 // We are here when the host object for *p is already marked. 90 91 HeapWord* addr = cast_from_oop<HeapWord*>(obj); 92 if (_map->par_mark(addr)) { 93 verify_oop_at(p, obj); 94 _stack->push(ShenandoahVerifierTask(obj)); 95 } 96 } 97 } 98 99 void verify_oop(oop obj) { 100 // Perform consistency checks with gradually decreasing safety level. This guarantees 101 // that failure report would not try to touch something that was not yet verified to be 102 // safe to process. 103 104 check(ShenandoahAsserts::_safe_unknown, obj, _heap->is_in(obj), 105 "oop must be in heap"); 106 check(ShenandoahAsserts::_safe_unknown, obj, is_object_aligned(obj), 107 "oop must be aligned"); 108 109 ShenandoahHeapRegion *obj_reg = _heap->heap_region_containing(obj); 110 Klass* obj_klass = obj->klass_or_null(); 111 112 // Verify that obj is not in dead space: 113 { 114 // Do this before touching obj->size() 115 check(ShenandoahAsserts::_safe_unknown, obj, obj_klass != NULL, 116 "Object klass pointer should not be NULL"); 117 check(ShenandoahAsserts::_safe_unknown, obj, Metaspace::contains(obj_klass), 118 "Object klass pointer must go to metaspace"); 119 120 HeapWord *obj_addr = cast_from_oop<HeapWord*>(obj); 121 check(ShenandoahAsserts::_safe_unknown, obj, obj_addr < obj_reg->top(), 122 "Object start should be within the region"); 123 124 if (!obj_reg->is_humongous()) { 125 check(ShenandoahAsserts::_safe_unknown, obj, (obj_addr + obj->size()) <= obj_reg->top(), 126 "Object end should be within the region"); 127 } else { 128 size_t humongous_start = obj_reg->region_number(); 129 size_t humongous_end = humongous_start + (obj->size() >> ShenandoahHeapRegion::region_size_words_shift()); 130 for (size_t idx = humongous_start + 1; idx < humongous_end; idx++) { 131 check(ShenandoahAsserts::_safe_unknown, obj, _heap->get_region(idx)->is_humongous_continuation(), 132 "Humongous object is in continuation that fits it"); 133 } 134 } 135 136 // ------------ obj is safe at this point -------------- 137 138 check(ShenandoahAsserts::_safe_oop, obj, obj_reg->is_active(), 139 "Object should be in active region"); 140 141 switch (_options._verify_liveness) { 142 case ShenandoahVerifier::_verify_liveness_disable: 143 // skip 144 break; 145 case ShenandoahVerifier::_verify_liveness_complete: 146 Atomic::add(&_ld[obj_reg->region_number()], (uint) obj->size()); 147 // fallthrough for fast failure for un-live regions: 148 case ShenandoahVerifier::_verify_liveness_conservative: 149 check(ShenandoahAsserts::_safe_oop, obj, obj_reg->has_live(), 150 "Object must belong to region with live data"); 151 break; 152 default: 153 assert(false, "Unhandled liveness verification"); 154 } 155 } 156 157 oop fwd = (oop) ShenandoahForwarding::get_forwardee_raw_unchecked(obj); 158 159 ShenandoahHeapRegion* fwd_reg = NULL; 160 161 if (obj != fwd) { 162 check(ShenandoahAsserts::_safe_oop, obj, _heap->is_in(fwd), 163 "Forwardee must be in heap"); 164 check(ShenandoahAsserts::_safe_oop, obj, !CompressedOops::is_null(fwd), 165 "Forwardee is set"); 166 check(ShenandoahAsserts::_safe_oop, obj, is_object_aligned(fwd), 167 "Forwardee must be aligned"); 168 169 // Do this before touching fwd->size() 170 Klass* fwd_klass = fwd->klass_or_null(); 171 check(ShenandoahAsserts::_safe_oop, obj, fwd_klass != NULL, 172 "Forwardee klass pointer should not be NULL"); 173 check(ShenandoahAsserts::_safe_oop, obj, Metaspace::contains(fwd_klass), 174 "Forwardee klass pointer must go to metaspace"); 175 check(ShenandoahAsserts::_safe_oop, obj, obj_klass == fwd_klass, 176 "Forwardee klass pointer must go to metaspace"); 177 178 fwd_reg = _heap->heap_region_containing(fwd); 179 180 // Verify that forwardee is not in the dead space: 181 check(ShenandoahAsserts::_safe_oop, obj, !fwd_reg->is_humongous(), 182 "Should have no humongous forwardees"); 183 184 HeapWord *fwd_addr = cast_from_oop<HeapWord *>(fwd); 185 check(ShenandoahAsserts::_safe_oop, obj, fwd_addr < fwd_reg->top(), 186 "Forwardee start should be within the region"); 187 check(ShenandoahAsserts::_safe_oop, obj, (fwd_addr + fwd->size()) <= fwd_reg->top(), 188 "Forwardee end should be within the region"); 189 190 oop fwd2 = (oop) ShenandoahForwarding::get_forwardee_raw_unchecked(fwd); 191 check(ShenandoahAsserts::_safe_oop, obj, (fwd == fwd2), 192 "Double forwarding"); 193 } else { 194 fwd_reg = obj_reg; 195 } 196 197 // ------------ obj and fwd are safe at this point -------------- 198 199 switch (_options._verify_marked) { 200 case ShenandoahVerifier::_verify_marked_disable: 201 // skip 202 break; 203 case ShenandoahVerifier::_verify_marked_incomplete: 204 check(ShenandoahAsserts::_safe_all, obj, _heap->marking_context()->is_marked(obj), 205 "Must be marked in incomplete bitmap"); 206 break; 207 case ShenandoahVerifier::_verify_marked_complete: 208 check(ShenandoahAsserts::_safe_all, obj, _heap->complete_marking_context()->is_marked(obj), 209 "Must be marked in complete bitmap"); 210 break; 211 default: 212 assert(false, "Unhandled mark verification"); 213 } 214 215 switch (_options._verify_forwarded) { 216 case ShenandoahVerifier::_verify_forwarded_disable: 217 // skip 218 break; 219 case ShenandoahVerifier::_verify_forwarded_none: { 220 check(ShenandoahAsserts::_safe_all, obj, (obj == fwd), 221 "Should not be forwarded"); 222 break; 223 } 224 case ShenandoahVerifier::_verify_forwarded_allow: { 225 if (obj != fwd) { 226 check(ShenandoahAsserts::_safe_all, obj, obj_reg != fwd_reg, 227 "Forwardee should be in another region"); 228 } 229 break; 230 } 231 default: 232 assert(false, "Unhandled forwarding verification"); 233 } 234 235 switch (_options._verify_cset) { 236 case ShenandoahVerifier::_verify_cset_disable: 237 // skip 238 break; 239 case ShenandoahVerifier::_verify_cset_none: 240 check(ShenandoahAsserts::_safe_all, obj, !_heap->in_collection_set(obj), 241 "Should not have references to collection set"); 242 break; 243 case ShenandoahVerifier::_verify_cset_forwarded: 244 if (_heap->in_collection_set(obj)) { 245 check(ShenandoahAsserts::_safe_all, obj, (obj != fwd), 246 "Object in collection set, should have forwardee"); 247 } 248 break; 249 default: 250 assert(false, "Unhandled cset verification"); 251 } 252 253 } 254 255 public: 256 /** 257 * Verify object with known interior reference. 258 * @param p interior reference where the object is referenced from; can be off-heap 259 * @param obj verified object 260 */ 261 template <class T> 262 void verify_oop_at(T* p, oop obj) { 263 _interior_loc = p; 264 verify_oop(obj); 265 _interior_loc = NULL; 266 } 267 268 /** 269 * Verify object without known interior reference. 270 * Useful when picking up the object at known offset in heap, 271 * but without knowing what objects reference it. 272 * @param obj verified object 273 */ 274 void verify_oop_standalone(oop obj) { 275 _interior_loc = NULL; 276 verify_oop(obj); 277 _interior_loc = NULL; 278 } 279 280 /** 281 * Verify oop fields from this object. 282 * @param obj host object for verified fields 283 */ 284 void verify_oops_from(oop obj) { 285 _loc = obj; 286 obj->oop_iterate(this); 287 _loc = NULL; 288 } 289 290 virtual void do_oop(oop* p) { do_oop_work(p); } 291 virtual void do_oop(narrowOop* p) { do_oop_work(p); } 292 }; 293 294 class ShenandoahCalculateRegionStatsClosure : public ShenandoahHeapRegionClosure { 295 private: 296 size_t _used, _committed, _garbage; 297 public: 298 ShenandoahCalculateRegionStatsClosure() : _used(0), _committed(0), _garbage(0) {}; 299 300 void heap_region_do(ShenandoahHeapRegion* r) { 301 _used += r->used(); 302 _garbage += r->garbage(); 303 _committed += r->is_committed() ? ShenandoahHeapRegion::region_size_bytes() : 0; 304 } 305 306 size_t used() { return _used; } 307 size_t committed() { return _committed; } 308 size_t garbage() { return _garbage; } 309 }; 310 311 class ShenandoahVerifyHeapRegionClosure : public ShenandoahHeapRegionClosure { 312 private: 313 ShenandoahHeap* _heap; 314 const char* _phase; 315 ShenandoahVerifier::VerifyRegions _regions; 316 public: 317 ShenandoahVerifyHeapRegionClosure(const char* phase, ShenandoahVerifier::VerifyRegions regions) : 318 _heap(ShenandoahHeap::heap()), 319 _phase(phase), 320 _regions(regions) {}; 321 322 void print_failure(ShenandoahHeapRegion* r, const char* label) { 323 ResourceMark rm; 324 325 ShenandoahMessageBuffer msg("Shenandoah verification failed; %s: %s\n\n", _phase, label); 326 327 stringStream ss; 328 r->print_on(&ss); 329 msg.append("%s", ss.as_string()); 330 331 report_vm_error(__FILE__, __LINE__, msg.buffer()); 332 } 333 334 void verify(ShenandoahHeapRegion* r, bool test, const char* msg) { 335 if (!test) { 336 print_failure(r, msg); 337 } 338 } 339 340 void heap_region_do(ShenandoahHeapRegion* r) { 341 switch (_regions) { 342 case ShenandoahVerifier::_verify_regions_disable: 343 break; 344 case ShenandoahVerifier::_verify_regions_notrash: 345 verify(r, !r->is_trash(), 346 "Should not have trash regions"); 347 break; 348 case ShenandoahVerifier::_verify_regions_nocset: 349 verify(r, !r->is_cset(), 350 "Should not have cset regions"); 351 break; 352 case ShenandoahVerifier::_verify_regions_notrash_nocset: 353 verify(r, !r->is_trash(), 354 "Should not have trash regions"); 355 verify(r, !r->is_cset(), 356 "Should not have cset regions"); 357 break; 358 default: 359 ShouldNotReachHere(); 360 } 361 362 verify(r, r->capacity() == ShenandoahHeapRegion::region_size_bytes(), 363 "Capacity should match region size"); 364 365 verify(r, r->bottom() <= r->top(), 366 "Region top should not be less than bottom"); 367 368 verify(r, r->bottom() <= _heap->marking_context()->top_at_mark_start(r), 369 "Region TAMS should not be less than bottom"); 370 371 verify(r, _heap->marking_context()->top_at_mark_start(r) <= r->top(), 372 "Complete TAMS should not be larger than top"); 373 374 verify(r, r->get_live_data_bytes() <= r->capacity(), 375 "Live data cannot be larger than capacity"); 376 377 verify(r, r->garbage() <= r->capacity(), 378 "Garbage cannot be larger than capacity"); 379 380 verify(r, r->used() <= r->capacity(), 381 "Used cannot be larger than capacity"); 382 383 verify(r, r->get_shared_allocs() <= r->capacity(), 384 "Shared alloc count should not be larger than capacity"); 385 386 verify(r, r->get_tlab_allocs() <= r->capacity(), 387 "TLAB alloc count should not be larger than capacity"); 388 389 verify(r, r->get_gclab_allocs() <= r->capacity(), 390 "GCLAB alloc count should not be larger than capacity"); 391 392 verify(r, r->get_shared_allocs() + r->get_tlab_allocs() + r->get_gclab_allocs() == r->used(), 393 "Accurate accounting: shared + TLAB + GCLAB = used"); 394 395 verify(r, !r->is_empty() || !r->has_live(), 396 "Empty regions should not have live data"); 397 398 verify(r, r->is_cset() == _heap->collection_set()->is_in(r), 399 "Transitional: region flags and collection set agree"); 400 401 verify(r, r->is_empty() || r->seqnum_first_alloc() != 0, 402 "Non-empty regions should have first seqnum set"); 403 404 verify(r, r->is_empty() || (r->seqnum_first_alloc_mutator() != 0 || r->seqnum_first_alloc_gc() != 0), 405 "Non-empty regions should have first seqnum set to either GC or mutator"); 406 407 verify(r, r->is_empty() || r->seqnum_last_alloc() != 0, 408 "Non-empty regions should have last seqnum set"); 409 410 verify(r, r->is_empty() || (r->seqnum_last_alloc_mutator() != 0 || r->seqnum_last_alloc_gc() != 0), 411 "Non-empty regions should have last seqnum set to either GC or mutator"); 412 413 verify(r, r->seqnum_first_alloc() <= r->seqnum_last_alloc(), 414 "First seqnum should not be greater than last timestamp"); 415 416 verify(r, r->seqnum_first_alloc_mutator() <= r->seqnum_last_alloc_mutator(), 417 "First mutator seqnum should not be greater than last seqnum"); 418 419 verify(r, r->seqnum_first_alloc_gc() <= r->seqnum_last_alloc_gc(), 420 "First GC seqnum should not be greater than last seqnum"); 421 } 422 }; 423 424 class ShenandoahVerifierReachableTask : public AbstractGangTask { 425 private: 426 const char* _label; 427 ShenandoahRootVerifier* _verifier; 428 ShenandoahVerifier::VerifyOptions _options; 429 ShenandoahHeap* _heap; 430 ShenandoahLivenessData* _ld; 431 MarkBitMap* _bitmap; 432 volatile size_t _processed; 433 434 public: 435 ShenandoahVerifierReachableTask(MarkBitMap* bitmap, 436 ShenandoahLivenessData* ld, 437 ShenandoahRootVerifier* verifier, 438 const char* label, 439 ShenandoahVerifier::VerifyOptions options) : 440 AbstractGangTask("Shenandoah Parallel Verifier Reachable Task"), 441 _label(label), 442 _verifier(verifier), 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 _verifier->strong_roots_do(&cl); 468 } else { 469 _verifier->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); 487 } 488 }; 489 490 class ShenandoahVerifierMarkedRegionTask : public AbstractGangTask { 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 AbstractGangTask("Shenandoah Parallel Verifier Marked Region"), 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 _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); 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((oop)obj)) { 543 verify_and_follow(obj, stack, cl, &processed); 544 } 545 Atomic::add(&_processed, processed); 546 } 547 548 virtual void work_regular(ShenandoahHeapRegion *r, ShenandoahVerifierStack &stack, ShenandoahVerifyOopClosure &cl) { 549 size_t processed = 0; 550 MarkBitMap* mark_bit_map = _heap->complete_marking_context()->mark_bit_map(); 551 HeapWord* tams = _heap->complete_marking_context()->top_at_mark_start(r); 552 553 // Bitmaps, before TAMS 554 if (tams > r->bottom()) { 555 HeapWord* start = r->bottom(); 556 HeapWord* addr = mark_bit_map->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 = mark_bit_map->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 += oop(addr)->size(); 575 } 576 } 577 578 Atomic::add(&_processed, processed); 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 = 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 cl.verify_oops_from(obj); 591 (*processed)++; 592 593 while (!stack.is_empty()) { 594 ShenandoahVerifierTask task = stack.pop(); 595 cl.verify_oops_from(task.obj()); 596 (*processed)++; 597 } 598 } 599 }; 600 601 class VerifyThreadGCState : public ThreadClosure { 602 private: 603 const char* _label; 604 char _expected; 605 606 public: 607 VerifyThreadGCState(const char* label, char expected) : _expected(expected) {} 608 void do_thread(Thread* t) { 609 char actual = ShenandoahThreadLocalData::gc_state(t); 610 if (actual != _expected) { 611 fatal("%s: Thread %s: expected gc-state %d, actual %d", _label, t->name(), _expected, actual); 612 } 613 } 614 }; 615 616 class ShenandoahGCStateResetter : public StackObj { 617 private: 618 ShenandoahHeap* const _heap; 619 char _gc_state; 620 621 public: 622 ShenandoahGCStateResetter() : _heap(ShenandoahHeap::heap()) { 623 _gc_state = _heap->gc_state(); 624 _heap->_gc_state.clear(); 625 } 626 627 ~ShenandoahGCStateResetter() { 628 _heap->_gc_state.set(_gc_state); 629 assert(_heap->gc_state() == _gc_state, "Should be restored"); 630 } 631 }; 632 633 void ShenandoahVerifier::verify_at_safepoint(const char *label, 634 VerifyForwarded forwarded, VerifyMarked marked, 635 VerifyCollectionSet cset, 636 VerifyLiveness liveness, VerifyRegions regions, 637 VerifyGCState gcstate, 638 VerifyWeakRoots weak_roots) { 639 guarantee(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "only when nothing else happens"); 640 guarantee(ShenandoahVerify, "only when enabled, and bitmap is initialized in ShenandoahHeap::initialize"); 641 642 // Avoid side-effect of changing workers' active thread count, but bypass concurrent/parallel protocol check 643 ShenandoahPushWorkerScope verify_worker_scope(_heap->workers(), _heap->max_workers(), false /*bypass check*/); 644 645 log_info(gc,start)("Verify %s, Level " INTX_FORMAT, label, ShenandoahVerifyLevel); 646 647 // GC state checks 648 { 649 char expected = -1; 650 bool enabled; 651 switch (gcstate) { 652 case _verify_gcstate_disable: 653 enabled = false; 654 break; 655 case _verify_gcstate_forwarded: 656 enabled = true; 657 expected = ShenandoahHeap::HAS_FORWARDED; 658 break; 659 case _verify_gcstate_evacuation: 660 enabled = true; 661 expected = ShenandoahHeap::HAS_FORWARDED | ShenandoahHeap::EVACUATION; 662 break; 663 case _verify_gcstate_stable: 664 enabled = true; 665 expected = ShenandoahHeap::STABLE; 666 break; 667 default: 668 enabled = false; 669 assert(false, "Unhandled gc-state verification"); 670 } 671 672 if (enabled) { 673 char actual = _heap->gc_state(); 674 if (actual != expected) { 675 fatal("%s: Global gc-state: expected %d, actual %d", label, expected, actual); 676 } 677 678 VerifyThreadGCState vtgcs(label, expected); 679 Threads::java_threads_do(&vtgcs); 680 } 681 } 682 683 // Deactivate barriers temporarily: Verifier wants plain heap accesses 684 ShenandoahGCStateResetter resetter; 685 686 // Heap size checks 687 { 688 ShenandoahHeapLocker lock(_heap->lock()); 689 690 ShenandoahCalculateRegionStatsClosure cl; 691 _heap->heap_region_iterate(&cl); 692 size_t heap_used = _heap->used(); 693 guarantee(cl.used() == heap_used, 694 "%s: heap used size must be consistent: heap-used = " SIZE_FORMAT "%s, regions-used = " SIZE_FORMAT "%s", 695 label, 696 byte_size_in_proper_unit(heap_used), proper_unit_for_byte_size(heap_used), 697 byte_size_in_proper_unit(cl.used()), proper_unit_for_byte_size(cl.used())); 698 699 size_t heap_committed = _heap->committed(); 700 guarantee(cl.committed() == heap_committed, 701 "%s: heap committed size must be consistent: heap-committed = " SIZE_FORMAT "%s, regions-committed = " SIZE_FORMAT "%s", 702 label, 703 byte_size_in_proper_unit(heap_committed), proper_unit_for_byte_size(heap_committed), 704 byte_size_in_proper_unit(cl.committed()), proper_unit_for_byte_size(cl.committed())); 705 } 706 707 // Internal heap region checks 708 if (ShenandoahVerifyLevel >= 1) { 709 ShenandoahVerifyHeapRegionClosure cl(label, regions); 710 _heap->heap_region_iterate(&cl); 711 } 712 713 OrderAccess::fence(); 714 _heap->make_parsable(false); 715 716 // Allocate temporary bitmap for storing marking wavefront: 717 _verification_bit_map->clear(); 718 719 // Allocate temporary array for storing liveness data 720 ShenandoahLivenessData* ld = NEW_C_HEAP_ARRAY(ShenandoahLivenessData, _heap->num_regions(), mtGC); 721 Copy::fill_to_bytes((void*)ld, _heap->num_regions()*sizeof(ShenandoahLivenessData), 0); 722 723 const VerifyOptions& options = ShenandoahVerifier::VerifyOptions(forwarded, marked, cset, liveness, regions, gcstate); 724 725 // Steps 1-2. Scan root set to get initial reachable set. Finish walking the reachable heap. 726 // This verifies what application can see, since it only cares about reachable objects. 727 size_t count_reachable = 0; 728 if (ShenandoahVerifyLevel >= 2) { 729 ShenandoahRootVerifier verifier; 730 switch (weak_roots) { 731 case _verify_serial_weak_roots: 732 verifier.excludes(ShenandoahRootVerifier::ConcurrentWeakRoots); 733 break; 734 case _verify_concurrent_weak_roots: 735 verifier.excludes(ShenandoahRootVerifier::SerialWeakRoots); 736 break; 737 case _verify_all_weak_roots: 738 break; 739 default: 740 ShouldNotReachHere(); 741 } 742 743 ShenandoahVerifierReachableTask task(_verification_bit_map, ld, &verifier, label, options); 744 _heap->workers()->run_task(&task); 745 count_reachable = task.processed(); 746 } 747 748 // Step 3. Walk marked objects. Marked objects might be unreachable. This verifies what collector, 749 // not the application, can see during the region scans. There is no reason to process the objects 750 // that were already verified, e.g. those marked in verification bitmap. There is interaction with TAMS: 751 // before TAMS, we verify the bitmaps, if available; after TAMS, we walk until the top(). It mimics 752 // what marked_object_iterate is doing, without calling into that optimized (and possibly incorrect) 753 // version 754 755 size_t count_marked = 0; 756 if (ShenandoahVerifyLevel >= 4 && marked == _verify_marked_complete) { 757 guarantee(_heap->marking_context()->is_complete(), "Marking context should be complete"); 758 ShenandoahVerifierMarkedRegionTask task(_verification_bit_map, ld, label, options); 759 _heap->workers()->run_task(&task); 760 count_marked = task.processed(); 761 } else { 762 guarantee(ShenandoahVerifyLevel < 4 || marked == _verify_marked_incomplete || marked == _verify_marked_disable, "Should be"); 763 } 764 765 // Step 4. Verify accumulated liveness data, if needed. Only reliable if verification level includes 766 // marked objects. 767 768 if (ShenandoahVerifyLevel >= 4 && marked == _verify_marked_complete && liveness == _verify_liveness_complete) { 769 for (size_t i = 0; i < _heap->num_regions(); i++) { 770 ShenandoahHeapRegion* r = _heap->get_region(i); 771 772 juint verf_live = 0; 773 if (r->is_humongous()) { 774 // For humongous objects, test if start region is marked live, and if so, 775 // all humongous regions in that chain have live data equal to their "used". 776 juint start_live = Atomic::load_acquire(&ld[r->humongous_start_region()->region_number()]); 777 if (start_live > 0) { 778 verf_live = (juint)(r->used() / HeapWordSize); 779 } 780 } else { 781 verf_live = Atomic::load_acquire(&ld[r->region_number()]); 782 } 783 784 size_t reg_live = r->get_live_data_words(); 785 if (reg_live != verf_live) { 786 ResourceMark rm; 787 stringStream ss; 788 r->print_on(&ss); 789 fatal("%s: Live data should match: region-live = " SIZE_FORMAT ", verifier-live = " UINT32_FORMAT "\n%s", 790 label, reg_live, verf_live, ss.as_string()); 791 } 792 } 793 } 794 795 log_info(gc)("Verify %s, Level " INTX_FORMAT " (" SIZE_FORMAT " reachable, " SIZE_FORMAT " marked)", 796 label, ShenandoahVerifyLevel, count_reachable, count_marked); 797 798 FREE_C_HEAP_ARRAY(ShenandoahLivenessData, ld); 799 } 800 801 void ShenandoahVerifier::verify_generic(VerifyOption vo) { 802 verify_at_safepoint( 803 "Generic Verification", 804 _verify_forwarded_allow, // conservatively allow forwarded 805 _verify_marked_disable, // do not verify marked: lots ot time wasted checking dead allocations 806 _verify_cset_disable, // cset may be inconsistent 807 _verify_liveness_disable, // no reliable liveness data 808 _verify_regions_disable, // no reliable region data 809 _verify_gcstate_disable, // no data about gcstate 810 _verify_all_weak_roots 811 ); 812 } 813 814 void ShenandoahVerifier::verify_before_concmark() { 815 if (_heap->has_forwarded_objects()) { 816 verify_at_safepoint( 817 "Before Mark", 818 _verify_forwarded_allow, // may have forwarded references 819 _verify_marked_disable, // do not verify marked: lots ot time wasted checking dead allocations 820 _verify_cset_forwarded, // allow forwarded references to cset 821 _verify_liveness_disable, // no reliable liveness data 822 _verify_regions_notrash, // no trash regions 823 _verify_gcstate_forwarded, // there are forwarded objects 824 _verify_all_weak_roots 825 ); 826 } else { 827 verify_at_safepoint( 828 "Before Mark", 829 _verify_forwarded_none, // UR should have fixed up 830 _verify_marked_disable, // do not verify marked: lots ot time wasted checking dead allocations 831 _verify_cset_none, // UR should have fixed this 832 _verify_liveness_disable, // no reliable liveness data 833 _verify_regions_notrash, // no trash regions 834 _verify_gcstate_stable, // there are no forwarded objects 835 _verify_all_weak_roots 836 ); 837 } 838 } 839 840 void ShenandoahVerifier::verify_after_concmark() { 841 verify_at_safepoint( 842 "After Mark", 843 _verify_forwarded_none, // no forwarded references 844 _verify_marked_complete, // bitmaps as precise as we can get 845 _verify_cset_none, // no references to cset anymore 846 _verify_liveness_complete, // liveness data must be complete here 847 _verify_regions_disable, // trash regions not yet recycled 848 _verify_gcstate_stable, // mark should have stabilized the heap 849 _verify_all_weak_roots 850 ); 851 } 852 853 void ShenandoahVerifier::verify_before_evacuation() { 854 // Concurrent weak roots are evacuated during concurrent phase 855 VerifyWeakRoots verify_weak_roots = ShenandoahConcurrentRoots::should_do_concurrent_class_unloading() ? 856 _verify_serial_weak_roots : 857 _verify_all_weak_roots; 858 859 verify_at_safepoint( 860 "Before Evacuation", 861 _verify_forwarded_none, // no forwarded references 862 _verify_marked_complete, // walk over marked objects too 863 _verify_cset_disable, // non-forwarded references to cset expected 864 _verify_liveness_complete, // liveness data must be complete here 865 _verify_regions_disable, // trash regions not yet recycled 866 _verify_gcstate_stable, // mark should have stabilized the heap 867 verify_weak_roots 868 ); 869 } 870 871 void ShenandoahVerifier::verify_during_evacuation() { 872 // Concurrent weak roots are evacuated during concurrent phase 873 VerifyWeakRoots verify_weak_roots = ShenandoahConcurrentRoots::should_do_concurrent_class_unloading() ? 874 _verify_serial_weak_roots : 875 _verify_all_weak_roots; 876 877 verify_at_safepoint( 878 "During Evacuation", 879 _verify_forwarded_allow, // some forwarded references are allowed 880 _verify_marked_disable, // walk only roots 881 _verify_cset_disable, // some cset references are not forwarded yet 882 _verify_liveness_disable, // liveness data might be already stale after pre-evacs 883 _verify_regions_disable, // trash regions not yet recycled 884 _verify_gcstate_evacuation, // evacuation is in progress 885 verify_weak_roots 886 ); 887 } 888 889 void ShenandoahVerifier::verify_after_evacuation() { 890 verify_at_safepoint( 891 "After Evacuation", 892 _verify_forwarded_allow, // objects are still forwarded 893 _verify_marked_complete, // bitmaps might be stale, but alloc-after-mark should be well 894 _verify_cset_forwarded, // all cset refs are fully forwarded 895 _verify_liveness_disable, // no reliable liveness data anymore 896 _verify_regions_notrash, // trash regions have been recycled already 897 _verify_gcstate_forwarded, // evacuation produced some forwarded objects 898 _verify_all_weak_roots 899 ); 900 } 901 902 void ShenandoahVerifier::verify_before_updaterefs() { 903 verify_at_safepoint( 904 "Before Updating References", 905 _verify_forwarded_allow, // forwarded references allowed 906 _verify_marked_complete, // bitmaps might be stale, but alloc-after-mark should be well 907 _verify_cset_forwarded, // all cset refs are fully forwarded 908 _verify_liveness_disable, // no reliable liveness data anymore 909 _verify_regions_notrash, // trash regions have been recycled already 910 _verify_gcstate_forwarded, // evacuation should have produced some forwarded objects 911 _verify_all_weak_roots 912 ); 913 } 914 915 void ShenandoahVerifier::verify_after_updaterefs() { 916 verify_at_safepoint( 917 "After Updating References", 918 _verify_forwarded_none, // no forwarded references 919 _verify_marked_complete, // bitmaps might be stale, but alloc-after-mark should be well 920 _verify_cset_none, // no cset references, all updated 921 _verify_liveness_disable, // no reliable liveness data anymore 922 _verify_regions_nocset, // no cset regions, trash regions have appeared 923 _verify_gcstate_stable, // update refs had cleaned up forwarded objects 924 _verify_all_weak_roots 925 ); 926 } 927 928 void ShenandoahVerifier::verify_after_degenerated() { 929 verify_at_safepoint( 930 "After Degenerated GC", 931 _verify_forwarded_none, // all objects are non-forwarded 932 _verify_marked_complete, // all objects are marked in complete bitmap 933 _verify_cset_none, // no cset references 934 _verify_liveness_disable, // no reliable liveness data anymore 935 _verify_regions_notrash_nocset, // no trash, no cset 936 _verify_gcstate_stable, // degenerated refs had cleaned up forwarded objects 937 _verify_all_weak_roots 938 ); 939 } 940 941 void ShenandoahVerifier::verify_before_traversal() { 942 verify_at_safepoint( 943 "Before Traversal", 944 _verify_forwarded_none, // cannot have forwarded objects 945 _verify_marked_disable, // bitmaps are not relevant before traversal 946 _verify_cset_none, // no cset references before traversal 947 _verify_liveness_disable, // no reliable liveness data anymore 948 _verify_regions_notrash_nocset, // no trash and no cset regions 949 _verify_gcstate_stable, // nothing forwarded before traversal 950 _verify_all_weak_roots 951 ); 952 } 953 954 void ShenandoahVerifier::verify_after_traversal() { 955 verify_at_safepoint( 956 "After Traversal", 957 _verify_forwarded_none, // cannot have forwarded objects 958 _verify_marked_complete, // should have complete marking after traversal 959 _verify_cset_none, // no cset references left after traversal 960 _verify_liveness_disable, // liveness data is not collected for new allocations 961 _verify_regions_nocset, // no cset regions, trash regions allowed 962 _verify_gcstate_stable, // nothing forwarded after traversal 963 _verify_all_weak_roots 964 ); 965 } 966 967 void ShenandoahVerifier::verify_before_fullgc() { 968 verify_at_safepoint( 969 "Before Full GC", 970 _verify_forwarded_allow, // can have forwarded objects 971 _verify_marked_disable, // do not verify marked: lots ot time wasted checking dead allocations 972 _verify_cset_disable, // cset might be foobared 973 _verify_liveness_disable, // no reliable liveness data anymore 974 _verify_regions_disable, // no reliable region data here 975 _verify_gcstate_disable, // no reliable gcstate data 976 _verify_all_weak_roots 977 ); 978 } 979 980 void ShenandoahVerifier::verify_after_fullgc() { 981 verify_at_safepoint( 982 "After Full GC", 983 _verify_forwarded_none, // all objects are non-forwarded 984 _verify_marked_complete, // all objects are marked in complete bitmap 985 _verify_cset_none, // no cset references 986 _verify_liveness_disable, // no reliable liveness data anymore 987 _verify_regions_notrash_nocset, // no trash, no cset 988 _verify_gcstate_stable, // full gc cleaned up everything 989 _verify_all_weak_roots 990 ); 991 } 992 993 class ShenandoahVerifyNoForwared : public OopClosure { 994 private: 995 template <class T> 996 void do_oop_work(T* p) { 997 T o = RawAccess<>::oop_load(p); 998 if (!CompressedOops::is_null(o)) { 999 oop obj = CompressedOops::decode_not_null(o); 1000 oop fwd = (oop) ShenandoahForwarding::get_forwardee_raw_unchecked(obj); 1001 if (obj != fwd) { 1002 ShenandoahAsserts::print_failure(ShenandoahAsserts::_safe_all, obj, p, NULL, 1003 "Verify Roots", "Should not be forwarded", __FILE__, __LINE__); 1004 } 1005 } 1006 } 1007 1008 public: 1009 void do_oop(narrowOop* p) { do_oop_work(p); } 1010 void do_oop(oop* p) { do_oop_work(p); } 1011 }; 1012 1013 class ShenandoahVerifyInToSpaceClosure : public OopClosure { 1014 private: 1015 template <class T> 1016 void do_oop_work(T* p) { 1017 T o = RawAccess<>::oop_load(p); 1018 if (!CompressedOops::is_null(o)) { 1019 oop obj = CompressedOops::decode_not_null(o); 1020 ShenandoahHeap* heap = ShenandoahHeap::heap_no_check(); 1021 1022 if (!heap->marking_context()->is_marked(obj)) { 1023 ShenandoahAsserts::print_failure(ShenandoahAsserts::_safe_all, obj, p, NULL, 1024 "Verify Roots In To-Space", "Should be marked", __FILE__, __LINE__); 1025 } 1026 1027 if (heap->in_collection_set(obj)) { 1028 ShenandoahAsserts::print_failure(ShenandoahAsserts::_safe_all, obj, p, NULL, 1029 "Verify Roots In To-Space", "Should not be in collection set", __FILE__, __LINE__); 1030 } 1031 1032 oop fwd = (oop) ShenandoahForwarding::get_forwardee_raw_unchecked(obj); 1033 if (obj != fwd) { 1034 ShenandoahAsserts::print_failure(ShenandoahAsserts::_safe_all, obj, p, NULL, 1035 "Verify Roots In To-Space", "Should not be forwarded", __FILE__, __LINE__); 1036 } 1037 } 1038 } 1039 1040 public: 1041 void do_oop(narrowOop* p) { do_oop_work(p); } 1042 void do_oop(oop* p) { do_oop_work(p); } 1043 }; 1044 1045 void ShenandoahVerifier::verify_roots_in_to_space() { 1046 ShenandoahRootVerifier verifier; 1047 ShenandoahVerifyInToSpaceClosure cl; 1048 verifier.oops_do(&cl); 1049 } 1050 1051 void ShenandoahVerifier::verify_roots_in_to_space_except(ShenandoahRootVerifier::RootTypes types) { 1052 ShenandoahRootVerifier verifier; 1053 verifier.excludes(types); 1054 ShenandoahVerifyInToSpaceClosure cl; 1055 verifier.oops_do(&cl); 1056 } 1057 1058 void ShenandoahVerifier::verify_roots_no_forwarded() { 1059 ShenandoahRootVerifier verifier; 1060 ShenandoahVerifyNoForwared cl; 1061 verifier.oops_do(&cl); 1062 } 1063 1064 void ShenandoahVerifier::verify_roots_no_forwarded_except(ShenandoahRootVerifier::RootTypes types) { 1065 ShenandoahRootVerifier verifier; 1066 verifier.excludes(types); 1067 ShenandoahVerifyNoForwared cl; 1068 verifier.oops_do(&cl); 1069 }