1 /* 2 * Copyright (c) 2017, 2019, Red Hat, Inc. All rights reserved. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. 7 * 8 * This code is distributed in the hope that it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11 * version 2 for more details (a copy is included in the LICENSE file that 12 * accompanied this code). 13 * 14 * You should have received a copy of the GNU General Public License version 15 * 2 along with this work; if not, write to the Free Software Foundation, 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 17 * 18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 19 * or visit www.oracle.com if you need additional information or have any 20 * questions. 21 * 22 */ 23 24 #include "precompiled.hpp" 25 26 #include "gc_implementation/shenandoah/shenandoahAsserts.hpp" 27 #include "gc_implementation/shenandoah/shenandoahForwarding.hpp" 28 #include "gc_implementation/shenandoah/shenandoahHeap.inline.hpp" 29 #include "gc_implementation/shenandoah/shenandoahHeapRegion.inline.hpp" 30 #include "gc_implementation/shenandoah/shenandoahRootProcessor.hpp" 31 #include "gc_implementation/shenandoah/shenandoahUtils.hpp" 32 #include "gc_implementation/shenandoah/shenandoahVerifier.hpp" 33 #include "gc_implementation/shenandoah/shenandoahWorkGroup.hpp" 34 #include "memory/allocation.hpp" 35 #include "memory/resourceArea.hpp" 36 37 // Avoid name collision on verify_oop (defined in macroAssembler_arm.hpp) 38 #ifdef verify_oop 39 #undef verify_oop 40 #endif 41 42 class ShenandoahVerifyOopClosure : public ExtendedOopClosure { 43 private: 44 const char* _phase; 45 ShenandoahVerifier::VerifyOptions _options; 46 ShenandoahVerifierStack* _stack; 47 ShenandoahHeap* _heap; 48 MarkBitMap* _map; 49 ShenandoahLivenessData* _ld; 50 void* _interior_loc; 51 oop _loc; 52 53 public: 54 ShenandoahVerifyOopClosure(ShenandoahVerifierStack* stack, MarkBitMap* map, ShenandoahLivenessData* ld, 55 const char* phase, ShenandoahVerifier::VerifyOptions options) : 56 _phase(phase), 57 _options(options), 58 _stack(stack), 59 _heap(ShenandoahHeap::heap()), 60 _map(map), 61 _ld(ld), 62 _interior_loc(NULL), 63 _loc(NULL) { } 64 65 private: 66 void verify(ShenandoahAsserts::SafeLevel level, oop obj, bool test, const char* label) { 67 if (!test) { 68 ShenandoahAsserts::print_failure(level, obj, _interior_loc, _loc, _phase, label, __FILE__, __LINE__); 69 } 70 } 71 72 template <class T> 73 void do_oop_work(T* p) { 74 T o = oopDesc::load_heap_oop(p); 75 if (!oopDesc::is_null(o)) { 76 oop obj = oopDesc::decode_heap_oop_not_null(o); 77 78 // Single threaded verification can use faster non-atomic stack and bitmap 79 // methods. 80 // 81 // For performance reasons, only fully verify non-marked field values. 82 // We are here when the host object for *p is already marked. 83 84 HeapWord* addr = (HeapWord*) obj; 85 if (_map->parMark(addr)) { 86 verify_oop_at(p, obj); 87 _stack->push(ShenandoahVerifierTask(obj)); 88 } 89 } 90 } 91 92 void verify_oop(oop obj) { 93 // Perform consistency checks with gradually decreasing safety level. This guarantees 94 // that failure report would not try to touch something that was not yet verified to be 95 // safe to process. 96 97 verify(ShenandoahAsserts::_safe_unknown, obj, _heap->is_in(obj), 98 "oop must be in heap"); 99 verify(ShenandoahAsserts::_safe_unknown, obj, check_obj_alignment(obj), 100 "oop must be aligned"); 101 102 ShenandoahHeapRegion *obj_reg = _heap->heap_region_containing(obj); 103 Klass* obj_klass = obj->klass_or_null(); 104 105 // Verify that obj is not in dead space: 106 { 107 // Do this before touching obj->size() 108 verify(ShenandoahAsserts::_safe_unknown, obj, obj_klass != NULL, 109 "Object klass pointer should not be NULL"); 110 verify(ShenandoahAsserts::_safe_unknown, obj, Metaspace::contains(obj_klass), 111 "Object klass pointer must go to metaspace"); 112 113 HeapWord *obj_addr = (HeapWord *) obj; 114 verify(ShenandoahAsserts::_safe_unknown, obj, obj_addr < obj_reg->top(), 115 "Object start should be within the region"); 116 117 if (!obj_reg->is_humongous()) { 118 verify(ShenandoahAsserts::_safe_unknown, obj, (obj_addr + obj->size()) <= obj_reg->top(), 119 "Object end should be within the region"); 120 } else { 121 size_t humongous_start = obj_reg->index(); 122 size_t humongous_end = humongous_start + (obj->size() >> ShenandoahHeapRegion::region_size_words_shift()); 123 for (size_t idx = humongous_start + 1; idx < humongous_end; idx++) { 124 verify(ShenandoahAsserts::_safe_unknown, obj, _heap->get_region(idx)->is_humongous_continuation(), 125 "Humongous object is in continuation that fits it"); 126 } 127 } 128 129 // ------------ obj is safe at this point -------------- 130 131 verify(ShenandoahAsserts::_safe_oop, obj, obj_reg->is_active(), 132 "Object should be in active region"); 133 134 switch (_options._verify_liveness) { 135 case ShenandoahVerifier::_verify_liveness_disable: 136 // skip 137 break; 138 case ShenandoahVerifier::_verify_liveness_complete: 139 Atomic::add((uint) obj->size(), &_ld[obj_reg->index()]); 140 // fallthrough for fast failure for un-live regions: 141 case ShenandoahVerifier::_verify_liveness_conservative: 142 verify(ShenandoahAsserts::_safe_oop, obj, obj_reg->has_live(), 143 "Object must belong to region with live data"); 144 break; 145 default: 146 assert(false, "Unhandled liveness verification"); 147 } 148 } 149 150 oop fwd = (oop) ShenandoahForwarding::get_forwardee_raw_unchecked(obj); 151 152 ShenandoahHeapRegion* fwd_reg = NULL; 153 154 if (obj != fwd) { 155 verify(ShenandoahAsserts::_safe_oop, obj, _heap->is_in(fwd), 156 "Forwardee must be in heap"); 157 verify(ShenandoahAsserts::_safe_oop, obj, !oopDesc::is_null(fwd), 158 "Forwardee is set"); 159 verify(ShenandoahAsserts::_safe_oop, obj, check_obj_alignment(fwd), 160 "Forwardee must be aligned"); 161 162 // Do this before touching fwd->size() 163 Klass* fwd_klass = fwd->klass_or_null(); 164 verify(ShenandoahAsserts::_safe_oop, obj, fwd_klass != NULL, 165 "Forwardee klass pointer should not be NULL"); 166 verify(ShenandoahAsserts::_safe_oop, obj, Metaspace::contains(fwd_klass), 167 "Forwardee klass pointer must go to metaspace"); 168 verify(ShenandoahAsserts::_safe_oop, obj, obj_klass == fwd_klass, 169 "Forwardee klass pointer must go to metaspace"); 170 171 fwd_reg = _heap->heap_region_containing(fwd); 172 173 // Verify that forwardee is not in the dead space: 174 verify(ShenandoahAsserts::_safe_oop, obj, !fwd_reg->is_humongous(), 175 "Should have no humongous forwardees"); 176 177 HeapWord *fwd_addr = (HeapWord *) fwd; 178 verify(ShenandoahAsserts::_safe_oop, obj, fwd_addr < fwd_reg->top(), 179 "Forwardee start should be within the region"); 180 verify(ShenandoahAsserts::_safe_oop, obj, (fwd_addr + fwd->size()) <= fwd_reg->top(), 181 "Forwardee end should be within the region"); 182 183 oop fwd2 = (oop) ShenandoahForwarding::get_forwardee_raw_unchecked(fwd); 184 verify(ShenandoahAsserts::_safe_oop, obj, fwd == fwd2, 185 "Double forwarding"); 186 } else { 187 fwd_reg = obj_reg; 188 } 189 190 // ------------ obj and fwd are safe at this point -------------- 191 192 switch (_options._verify_marked) { 193 case ShenandoahVerifier::_verify_marked_disable: 194 // skip 195 break; 196 case ShenandoahVerifier::_verify_marked_incomplete: 197 verify(ShenandoahAsserts::_safe_all, obj, _heap->marking_context()->is_marked(obj), 198 "Must be marked in incomplete bitmap"); 199 case ShenandoahVerifier::_verify_marked_complete: 200 verify(ShenandoahAsserts::_safe_all, obj, _heap->complete_marking_context()->is_marked(obj), 201 "Must be marked in complete bitmap"); 202 break; 203 default: 204 assert(false, "Unhandled mark verification"); 205 } 206 207 switch (_options._verify_forwarded) { 208 case ShenandoahVerifier::_verify_forwarded_disable: 209 // skip 210 break; 211 case ShenandoahVerifier::_verify_forwarded_none: { 212 verify(ShenandoahAsserts::_safe_all, obj, obj == fwd, 213 "Should not be forwarded"); 214 break; 215 } 216 case ShenandoahVerifier::_verify_forwarded_allow: { 217 if (obj != fwd) { 218 verify(ShenandoahAsserts::_safe_all, obj, obj_reg != fwd_reg, 219 "Forwardee should be in another region"); 220 } 221 break; 222 } 223 default: 224 assert(false, "Unhandled forwarding verification"); 225 } 226 227 switch (_options._verify_cset) { 228 case ShenandoahVerifier::_verify_cset_disable: 229 // skip 230 break; 231 case ShenandoahVerifier::_verify_cset_none: 232 verify(ShenandoahAsserts::_safe_all, obj, !_heap->in_collection_set(obj), 233 "Should not have references to collection set"); 234 break; 235 case ShenandoahVerifier::_verify_cset_forwarded: 236 if (_heap->in_collection_set(obj)) { 237 verify(ShenandoahAsserts::_safe_all, obj, obj != fwd, 238 "Object in collection set, should have forwardee"); 239 } 240 break; 241 default: 242 assert(false, "Unhandled cset verification"); 243 } 244 } 245 246 public: 247 /** 248 * Verify object with known interior reference. 249 * @param p interior reference where the object is referenced from; can be off-heap 250 * @param obj verified object 251 */ 252 template <class T> 253 void verify_oop_at(T* p, oop obj) { 254 _interior_loc = p; 255 verify_oop(obj); 256 _interior_loc = NULL; 257 } 258 259 /** 260 * Verify object without known interior reference. 261 * Useful when picking up the object at known offset in heap, 262 * but without knowing what objects reference it. 263 * @param obj verified object 264 */ 265 void verify_oop_standalone(oop obj) { 266 _interior_loc = NULL; 267 verify_oop(obj); 268 _interior_loc = NULL; 269 } 270 271 /** 272 * Verify oop fields from this object. 273 * @param obj host object for verified fields 274 */ 275 void verify_oops_from(oop obj) { 276 _loc = obj; 277 obj->oop_iterate(this); 278 _loc = NULL; 279 } 280 281 void do_oop(oop* p) { do_oop_work(p); } 282 void do_oop(narrowOop* p) { do_oop_work(p); } 283 }; 284 285 class ShenandoahCalculateRegionStatsClosure : public ShenandoahHeapRegionClosure { 286 private: 287 size_t _used, _committed, _garbage; 288 public: 289 ShenandoahCalculateRegionStatsClosure() : _used(0), _committed(0), _garbage(0) {}; 290 291 void heap_region_do(ShenandoahHeapRegion* r) { 292 _used += r->used(); 293 _garbage += r->garbage(); 294 _committed += r->is_committed() ? ShenandoahHeapRegion::region_size_bytes() : 0; 295 } 296 297 size_t used() { return _used; } 298 size_t committed() { return _committed; } 299 size_t garbage() { return _garbage; } 300 }; 301 302 class ShenandoahVerifyHeapRegionClosure : public ShenandoahHeapRegionClosure { 303 private: 304 ShenandoahHeap* _heap; 305 const char* _phase; 306 ShenandoahVerifier::VerifyRegions _regions; 307 public: 308 ShenandoahVerifyHeapRegionClosure(const char* phase, ShenandoahVerifier::VerifyRegions regions) : 309 _heap(ShenandoahHeap::heap()), 310 _phase(phase), 311 _regions(regions) {}; 312 313 void print_failure(ShenandoahHeapRegion* r, const char* label) { 314 ResourceMark rm; 315 316 ShenandoahMessageBuffer msg("Shenandoah verification failed; %s: %s\n\n", _phase, label); 317 318 stringStream ss; 319 r->print_on(&ss); 320 msg.append("%s", ss.as_string()); 321 322 report_vm_error(__FILE__, __LINE__, msg.buffer()); 323 } 324 325 void verify(ShenandoahHeapRegion* r, bool test, const char* msg) { 326 if (!test) { 327 print_failure(r, msg); 328 } 329 } 330 331 void heap_region_do(ShenandoahHeapRegion* r) { 332 switch (_regions) { 333 case ShenandoahVerifier::_verify_regions_disable: 334 break; 335 case ShenandoahVerifier::_verify_regions_notrash: 336 verify(r, !r->is_trash(), 337 "Should not have trash regions"); 338 break; 339 case ShenandoahVerifier::_verify_regions_nocset: 340 verify(r, !r->is_cset(), 341 "Should not have cset regions"); 342 break; 343 case ShenandoahVerifier::_verify_regions_notrash_nocset: 344 verify(r, !r->is_trash(), 345 "Should not have trash regions"); 346 verify(r, !r->is_cset(), 347 "Should not have cset regions"); 348 break; 349 default: 350 ShouldNotReachHere(); 351 } 352 353 verify(r, r->capacity() == ShenandoahHeapRegion::region_size_bytes(), 354 "Capacity should match region size"); 355 356 verify(r, r->bottom() <= r->top(), 357 "Region top should not be less than bottom"); 358 359 verify(r, r->bottom() <= _heap->marking_context()->top_at_mark_start(r), 360 "Region TAMS should not be less than bottom"); 361 362 verify(r, _heap->marking_context()->top_at_mark_start(r) <= r->top(), 363 "Complete TAMS should not be larger than top"); 364 365 verify(r, r->get_live_data_bytes() <= r->capacity(), 366 "Live data cannot be larger than capacity"); 367 368 verify(r, r->garbage() <= r->capacity(), 369 "Garbage cannot be larger than capacity"); 370 371 verify(r, r->used() <= r->capacity(), 372 "Used cannot be larger than capacity"); 373 374 verify(r, r->get_shared_allocs() <= r->capacity(), 375 "Shared alloc count should not be larger than capacity"); 376 377 verify(r, r->get_tlab_allocs() <= r->capacity(), 378 "TLAB alloc count should not be larger than capacity"); 379 380 verify(r, r->get_gclab_allocs() <= r->capacity(), 381 "GCLAB alloc count should not be larger than capacity"); 382 383 verify(r, r->get_shared_allocs() + r->get_tlab_allocs() + r->get_gclab_allocs() == r->used(), 384 "Accurate accounting: shared + TLAB + GCLAB = used"); 385 386 verify(r, !r->is_empty() || !r->has_live(), 387 "Empty regions should not have live data"); 388 389 verify(r, r->is_cset() == _heap->collection_set()->is_in(r), 390 "Transitional: region flags and collection set agree"); 391 } 392 }; 393 394 class ShenandoahVerifierReachableTask : public AbstractGangTask { 395 private: 396 const char* _label; 397 ShenandoahRootVerifier* _verifier; 398 ShenandoahVerifier::VerifyOptions _options; 399 ShenandoahHeap* _heap; 400 ShenandoahLivenessData* _ld; 401 MarkBitMap* _bitmap; 402 volatile jlong _processed; 403 404 public: 405 ShenandoahVerifierReachableTask(MarkBitMap* bitmap, 406 ShenandoahLivenessData* ld, 407 ShenandoahRootVerifier* verifier, 408 const char* label, 409 ShenandoahVerifier::VerifyOptions options) : 410 AbstractGangTask("Shenandoah Parallel Verifier Reachable Task"), 411 _label(label), 412 _verifier(verifier), 413 _options(options), 414 _heap(ShenandoahHeap::heap()), 415 _ld(ld), 416 _bitmap(bitmap), 417 _processed(0) {}; 418 419 size_t processed() { 420 return (size_t) _processed; 421 } 422 423 virtual void work(uint worker_id) { 424 ResourceMark rm; 425 ShenandoahVerifierStack stack; 426 427 // On level 2, we need to only check the roots once. 428 // On level 3, we want to check the roots, and seed the local stack. 429 // It is a lesser evil to accept multiple root scans at level 3, because 430 // extended parallelism would buy us out. 431 if (((ShenandoahVerifyLevel == 2) && (worker_id == 0)) 432 || (ShenandoahVerifyLevel >= 3)) { 433 ShenandoahVerifyOopClosure cl(&stack, _bitmap, _ld, 434 ShenandoahMessageBuffer("Shenandoah verification failed; %s, Roots", _label), 435 _options); 436 if (_heap->unload_classes()) { 437 _verifier->strong_roots_do(&cl); 438 } else { 439 _verifier->roots_do(&cl); 440 } 441 } 442 443 jlong processed = 0; 444 445 if (ShenandoahVerifyLevel >= 3) { 446 ShenandoahVerifyOopClosure cl(&stack, _bitmap, _ld, 447 ShenandoahMessageBuffer("Shenandoah verification failed; %s, Reachable", _label), 448 _options); 449 while (!stack.is_empty()) { 450 processed++; 451 ShenandoahVerifierTask task = stack.pop(); 452 cl.verify_oops_from(task.obj()); 453 } 454 } 455 456 Atomic::add(processed, &_processed); 457 } 458 }; 459 460 class ShenandoahVerifierMarkedRegionTask : public AbstractGangTask { 461 private: 462 const char* _label; 463 ShenandoahVerifier::VerifyOptions _options; 464 ShenandoahHeap *_heap; 465 ShenandoahLivenessData* _ld; 466 MarkBitMap* _bitmap; 467 volatile jint _claimed; 468 volatile jlong _processed; 469 470 public: 471 ShenandoahVerifierMarkedRegionTask(MarkBitMap* bitmap, 472 ShenandoahLivenessData* ld, 473 const char* label, 474 ShenandoahVerifier::VerifyOptions options) : 475 AbstractGangTask("Shenandoah Parallel Verifier Marked Region"), 476 _label(label), 477 _options(options), 478 _heap(ShenandoahHeap::heap()), 479 _bitmap(bitmap), 480 _ld(ld), 481 _claimed(0), 482 _processed(0) {}; 483 484 size_t processed() { 485 return (size_t) _processed; 486 } 487 488 virtual void work(uint worker_id) { 489 ShenandoahVerifierStack stack; 490 ShenandoahVerifyOopClosure cl(&stack, _bitmap, _ld, 491 ShenandoahMessageBuffer("Shenandoah verification failed; %s, Marked", _label), 492 _options); 493 assert((size_t)max_jint >= _heap->num_regions(), "Too many regions"); 494 while (true) { 495 size_t v = (size_t) (Atomic::add(1, &_claimed) - 1); 496 if (v < _heap->num_regions()) { 497 ShenandoahHeapRegion* r = _heap->get_region(v); 498 if (!r->is_humongous() && !r->is_trash()) { 499 work_regular(r, stack, cl); 500 } else if (r->is_humongous_start()) { 501 work_humongous(r, stack, cl); 502 } 503 } else { 504 break; 505 } 506 } 507 } 508 509 virtual void work_humongous(ShenandoahHeapRegion *r, ShenandoahVerifierStack& stack, ShenandoahVerifyOopClosure& cl) { 510 jlong processed = 0; 511 HeapWord* obj = r->bottom(); 512 if (_heap->complete_marking_context()->is_marked((oop)obj)) { 513 verify_and_follow(obj, stack, cl, &processed); 514 } 515 Atomic::add(processed, &_processed); 516 } 517 518 virtual void work_regular(ShenandoahHeapRegion *r, ShenandoahVerifierStack &stack, ShenandoahVerifyOopClosure &cl) { 519 jlong processed = 0; 520 MarkBitMap* mark_bit_map = _heap->complete_marking_context()->mark_bit_map(); 521 HeapWord* tams = _heap->complete_marking_context()->top_at_mark_start(r); 522 523 // Bitmaps, before TAMS 524 if (tams > r->bottom()) { 525 HeapWord* start = r->bottom(); 526 HeapWord* addr = mark_bit_map->getNextMarkedWordAddress(start, tams); 527 528 while (addr < tams) { 529 verify_and_follow(addr, stack, cl, &processed); 530 addr += 1; 531 if (addr < tams) { 532 addr = mark_bit_map->getNextMarkedWordAddress(addr, tams); 533 } 534 } 535 } 536 537 // Size-based, after TAMS 538 { 539 HeapWord* limit = r->top(); 540 HeapWord* addr = tams; 541 542 while (addr < limit) { 543 verify_and_follow(addr, stack, cl, &processed); 544 addr += oop(addr)->size(); 545 } 546 } 547 548 Atomic::add(processed, &_processed); 549 } 550 551 void verify_and_follow(HeapWord *addr, ShenandoahVerifierStack &stack, ShenandoahVerifyOopClosure &cl, jlong *processed) { 552 if (!_bitmap->parMark(addr)) return; 553 554 // Verify the object itself: 555 oop obj = oop(addr); 556 cl.verify_oop_standalone(obj); 557 558 // Verify everything reachable from that object too, hopefully realizing 559 // everything was already marked, and never touching further: 560 cl.verify_oops_from(obj); 561 (*processed)++; 562 563 while (!stack.is_empty()) { 564 ShenandoahVerifierTask task = stack.pop(); 565 cl.verify_oops_from(task.obj()); 566 (*processed)++; 567 } 568 } 569 }; 570 571 class VerifyThreadGCState : public ThreadClosure { 572 private: 573 const char* const _label; 574 char const _expected; 575 576 public: 577 VerifyThreadGCState(const char* label, char expected) : _label(label), _expected(expected) {} 578 void do_thread(Thread* t) { 579 assert(t->is_Java_thread(), "sanity"); 580 char actual = ((JavaThread*)t)->gc_state(); 581 if (actual != _expected) { 582 fatal(err_msg("%s: Thread %s: expected gc-state %d, actual %d", _label, t->name(), _expected, actual)); 583 } 584 } 585 }; 586 587 class ShenandoahGCStateResetter : public StackObj { 588 private: 589 ShenandoahHeap* const _heap; 590 char _gc_state; 591 592 public: 593 ShenandoahGCStateResetter() : _heap(ShenandoahHeap::heap()) { 594 _gc_state = _heap->gc_state(); 595 _heap->_gc_state.clear(); 596 } 597 598 ~ShenandoahGCStateResetter() { 599 _heap->_gc_state.set(_gc_state); 600 assert(_heap->gc_state() == _gc_state, "Should be restored"); 601 } 602 }; 603 604 void ShenandoahVerifier::verify_at_safepoint(const char *label, 605 VerifyForwarded forwarded, VerifyMarked marked, 606 VerifyCollectionSet cset, 607 VerifyLiveness liveness, VerifyRegions regions, 608 VerifyGCState gcstate) { 609 guarantee(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "only when nothing else happens"); 610 guarantee(ShenandoahVerify, "only when enabled, and bitmap is initialized in ShenandoahHeap::initialize"); 611 612 // Avoid side-effect of changing workers' active thread count, but bypass concurrent/parallel protocol check 613 ShenandoahPushWorkerScope verify_worker_scope(_heap->workers(), _heap->max_workers(), false /*bypass check*/); 614 615 log_info(gc,start)("Verify %s, Level " INTX_FORMAT, label, ShenandoahVerifyLevel); 616 617 // GC state checks 618 { 619 char expected = -1; 620 bool enabled; 621 switch (gcstate) { 622 case _verify_gcstate_disable: 623 enabled = false; 624 break; 625 case _verify_gcstate_forwarded: 626 enabled = true; 627 expected = ShenandoahHeap::HAS_FORWARDED; 628 break; 629 case _verify_gcstate_evacuation: 630 enabled = true; 631 expected = ShenandoahHeap::HAS_FORWARDED | ShenandoahHeap::EVACUATION; 632 break; 633 case _verify_gcstate_stable: 634 enabled = true; 635 expected = ShenandoahHeap::STABLE; 636 break; 637 default: 638 enabled = false; 639 assert(false, "Unhandled gc-state verification"); 640 } 641 642 if (enabled) { 643 char actual = _heap->gc_state(); 644 if (actual != expected) { 645 fatal(err_msg("%s: Global gc-state: expected %d, actual %d", label, expected, actual)); 646 } 647 648 VerifyThreadGCState vtgcs(label, expected); 649 Threads::java_threads_do(&vtgcs); 650 } 651 } 652 653 // Deactivate barriers temporarily: Verifier wants plain heap accesses 654 ShenandoahGCStateResetter resetter; 655 656 // Heap size checks 657 { 658 ShenandoahHeapLocker lock(_heap->lock()); 659 660 ShenandoahCalculateRegionStatsClosure cl; 661 _heap->heap_region_iterate(&cl); 662 size_t heap_used = _heap->used(); 663 guarantee(cl.used() == heap_used, 664 err_msg("%s: heap used size must be consistent: heap-used = " SIZE_FORMAT "%s, regions-used = " SIZE_FORMAT "%s", 665 label, 666 byte_size_in_proper_unit(heap_used), proper_unit_for_byte_size(heap_used), 667 byte_size_in_proper_unit(cl.used()), proper_unit_for_byte_size(cl.used()))); 668 669 size_t heap_committed = _heap->committed(); 670 guarantee(cl.committed() == heap_committed, 671 err_msg("%s: heap committed size must be consistent: heap-committed = " SIZE_FORMAT "%s, regions-committed = " SIZE_FORMAT "%s", 672 label, 673 byte_size_in_proper_unit(heap_committed), proper_unit_for_byte_size(heap_committed), 674 byte_size_in_proper_unit(cl.committed()), proper_unit_for_byte_size(cl.committed()))); 675 } 676 677 // Internal heap region checks 678 if (ShenandoahVerifyLevel >= 1) { 679 ShenandoahVerifyHeapRegionClosure cl(label, regions); 680 _heap->heap_region_iterate(&cl); 681 } 682 683 OrderAccess::fence(); 684 _heap->make_parsable(false); 685 686 // Allocate temporary bitmap for storing marking wavefront: 687 _verification_bit_map->clear(); 688 689 // Allocate temporary array for storing liveness data 690 ShenandoahLivenessData* ld = NEW_C_HEAP_ARRAY(ShenandoahLivenessData, _heap->num_regions(), mtGC); 691 Copy::fill_to_bytes((void*)ld, _heap->num_regions()*sizeof(ShenandoahLivenessData), 0); 692 693 const VerifyOptions& options = ShenandoahVerifier::VerifyOptions(forwarded, marked, cset, liveness, regions, gcstate); 694 695 // Steps 1-2. Scan root set to get initial reachable set. Finish walking the reachable heap. 696 // This verifies what application can see, since it only cares about reachable objects. 697 size_t count_reachable = 0; 698 if (ShenandoahVerifyLevel >= 2) { 699 ShenandoahRootVerifier verifier; 700 701 ShenandoahVerifierReachableTask task(_verification_bit_map, ld, &verifier, label, options); 702 _heap->workers()->run_task(&task); 703 count_reachable = task.processed(); 704 } 705 706 // Step 3. Walk marked objects. Marked objects might be unreachable. This verifies what collector, 707 // not the application, can see during the region scans. There is no reason to process the objects 708 // that were already verified, e.g. those marked in verification bitmap. There is interaction with TAMS: 709 // before TAMS, we verify the bitmaps, if available; after TAMS, we walk until the top(). It mimics 710 // what marked_object_iterate is doing, without calling into that optimized (and possibly incorrect) 711 // version 712 713 size_t count_marked = 0; 714 if (ShenandoahVerifyLevel >= 4 && marked == _verify_marked_complete) { 715 guarantee(_heap->marking_context()->is_complete(), "Marking context should be complete"); 716 ShenandoahVerifierMarkedRegionTask task(_verification_bit_map, ld, label, options); 717 _heap->workers()->run_task(&task); 718 count_marked = task.processed(); 719 } else { 720 guarantee(ShenandoahVerifyLevel < 4 || marked == _verify_marked_incomplete || marked == _verify_marked_disable, "Should be"); 721 } 722 723 // Step 4. Verify accumulated liveness data, if needed. Only reliable if verification level includes 724 // marked objects. 725 726 if (ShenandoahVerifyLevel >= 4 && marked == _verify_marked_complete && liveness == _verify_liveness_complete) { 727 for (size_t i = 0; i < _heap->num_regions(); i++) { 728 ShenandoahHeapRegion* r = _heap->get_region(i); 729 730 jint verf_live = 0; 731 if (r->is_humongous()) { 732 // For humongous objects, test if start region is marked live, and if so, 733 // all humongous regions in that chain have live data equal to their "used". 734 jint start_live = OrderAccess::load_acquire(&ld[r->humongous_start_region()->index()]); 735 if (start_live > 0) { 736 verf_live = (jint)(r->used() / HeapWordSize); 737 } 738 } else { 739 verf_live = OrderAccess::load_acquire(&ld[r->index()]); 740 } 741 742 size_t reg_live = r->get_live_data_words(); 743 if (reg_live != (size_t)verf_live) { 744 ResourceMark rm; 745 stringStream ss; 746 r->print_on(&ss); 747 fatal(err_msg("%s: Live data should match: region-live = " SIZE_FORMAT ", verifier-live = " INT32_FORMAT "\n%s", 748 label, reg_live, verf_live, ss.as_string())); 749 } 750 } 751 } 752 753 log_info(gc)("Verify %s, Level " INTX_FORMAT " (" SIZE_FORMAT " reachable, " SIZE_FORMAT " marked)", 754 label, ShenandoahVerifyLevel, count_reachable, count_marked); 755 756 FREE_C_HEAP_ARRAY(ShenandoahLivenessData, ld, mtGC); 757 } 758 759 void ShenandoahVerifier::verify_generic(VerifyOption vo) { 760 verify_at_safepoint( 761 "Generic Verification", 762 _verify_forwarded_allow, // conservatively allow forwarded 763 _verify_marked_disable, // do not verify marked: lots ot time wasted checking dead allocations 764 _verify_cset_disable, // cset may be inconsistent 765 _verify_liveness_disable, // no reliable liveness data 766 _verify_regions_disable, // no reliable region data 767 _verify_gcstate_disable // no data about gcstate 768 ); 769 } 770 771 void ShenandoahVerifier::verify_before_concmark() { 772 verify_at_safepoint( 773 "Before Mark", 774 _verify_forwarded_none, // UR should have fixed up 775 _verify_marked_disable, // do not verify marked: lots ot time wasted checking dead allocations 776 _verify_cset_none, // UR should have fixed this 777 _verify_liveness_disable, // no reliable liveness data 778 _verify_regions_notrash, // no trash regions 779 _verify_gcstate_stable // there are no forwarded objects 780 ); 781 } 782 783 void ShenandoahVerifier::verify_after_concmark() { 784 verify_at_safepoint( 785 "After Mark", 786 _verify_forwarded_none, // no forwarded references 787 _verify_marked_complete, // bitmaps as precise as we can get 788 _verify_cset_none, // no references to cset anymore 789 _verify_liveness_complete, // liveness data must be complete here 790 _verify_regions_disable, // trash regions not yet recycled 791 _verify_gcstate_stable // mark should have stabilized the heap 792 ); 793 } 794 795 void ShenandoahVerifier::verify_before_evacuation() { 796 verify_at_safepoint( 797 "Before Evacuation", 798 _verify_forwarded_none, // no forwarded references 799 _verify_marked_complete, // walk over marked objects too 800 _verify_cset_disable, // non-forwarded references to cset expected 801 _verify_liveness_complete, // liveness data must be complete here 802 _verify_regions_disable, // trash regions not yet recycled 803 _verify_gcstate_stable // mark should have stabilized the heap 804 ); 805 } 806 807 void ShenandoahVerifier::verify_during_evacuation() { 808 verify_at_safepoint( 809 "During Evacuation", 810 _verify_forwarded_allow, // some forwarded references are allowed 811 _verify_marked_disable, // walk only roots 812 _verify_cset_disable, // some cset references are not forwarded yet 813 _verify_liveness_disable, // liveness data might be already stale after pre-evacs 814 _verify_regions_disable, // trash regions not yet recycled 815 _verify_gcstate_evacuation // evacuation is in progress 816 ); 817 } 818 819 void ShenandoahVerifier::verify_after_evacuation() { 820 verify_at_safepoint( 821 "After Evacuation", 822 _verify_forwarded_allow, // objects are still forwarded 823 _verify_marked_complete, // bitmaps might be stale, but alloc-after-mark should be well 824 _verify_cset_forwarded, // all cset refs are fully forwarded 825 _verify_liveness_disable, // no reliable liveness data anymore 826 _verify_regions_notrash, // trash regions have been recycled already 827 _verify_gcstate_forwarded // evacuation produced some forwarded objects 828 ); 829 } 830 831 void ShenandoahVerifier::verify_before_updaterefs() { 832 verify_at_safepoint( 833 "Before Updating References", 834 _verify_forwarded_allow, // forwarded references allowed 835 _verify_marked_complete, // bitmaps might be stale, but alloc-after-mark should be well 836 _verify_cset_forwarded, // all cset refs are fully forwarded 837 _verify_liveness_disable, // no reliable liveness data anymore 838 _verify_regions_notrash, // trash regions have been recycled already 839 _verify_gcstate_forwarded // evacuation should have produced some forwarded objects 840 ); 841 } 842 843 void ShenandoahVerifier::verify_after_updaterefs() { 844 verify_at_safepoint( 845 "After Updating References", 846 _verify_forwarded_none, // no forwarded references 847 _verify_marked_complete, // bitmaps might be stale, but alloc-after-mark should be well 848 _verify_cset_none, // no cset references, all updated 849 _verify_liveness_disable, // no reliable liveness data anymore 850 _verify_regions_nocset, // no cset regions, trash regions have appeared 851 _verify_gcstate_stable // update refs had cleaned up forwarded objects 852 ); 853 } 854 855 void ShenandoahVerifier::verify_before_fullgc() { 856 verify_at_safepoint( 857 "Before Full GC", 858 _verify_forwarded_allow, // can have forwarded objects 859 _verify_marked_disable, // do not verify marked: lots ot time wasted checking dead allocations 860 _verify_cset_disable, // cset might be foobared 861 _verify_liveness_disable, // no reliable liveness data anymore 862 _verify_regions_disable, // no reliable region data here 863 _verify_gcstate_disable // no reliable gcstate data 864 ); 865 } 866 867 void ShenandoahVerifier::verify_after_fullgc() { 868 verify_at_safepoint( 869 "After Full GC", 870 _verify_forwarded_none, // all objects are non-forwarded 871 _verify_marked_complete, // all objects are marked in complete bitmap 872 _verify_cset_none, // no cset references 873 _verify_liveness_disable, // no reliable liveness data anymore 874 _verify_regions_notrash_nocset, // no trash, no cset 875 _verify_gcstate_stable // degenerated refs had cleaned up forwarded objects 876 ); 877 } 878 void ShenandoahVerifier::verify_after_degenerated() { 879 verify_at_safepoint( 880 "After Degenerated GC", 881 _verify_forwarded_none, // all objects are non-forwarded 882 _verify_marked_complete, // all objects are marked in complete bitmap 883 _verify_cset_none, // no cset references 884 _verify_liveness_disable, // no reliable liveness data anymore 885 _verify_regions_notrash_nocset, // no trash, no cset 886 _verify_gcstate_stable // full gc cleaned up everything 887 ); 888 } 889 890 class ShenandoahVerifyNoForwared : public OopClosure { 891 private: 892 template <class T> 893 void do_oop_work(T* p) { 894 T o = oopDesc::load_heap_oop(p); 895 if (!oopDesc::is_null(o)) { 896 oop obj = oopDesc::decode_heap_oop_not_null(o); 897 oop fwd = (oop) ShenandoahForwarding::get_forwardee_raw_unchecked(obj); 898 if (obj != fwd) { 899 ShenandoahAsserts::print_failure(ShenandoahAsserts::_safe_all, obj, p, NULL, 900 "Verify Roots", "Should not be forwarded", __FILE__, __LINE__); 901 } 902 } 903 } 904 905 public: 906 void do_oop(narrowOop* p) { do_oop_work(p); } 907 void do_oop(oop* p) { do_oop_work(p); } 908 }; 909 910 class ShenandoahVerifyInToSpaceClosure : public OopClosure { 911 private: 912 template <class T> 913 void do_oop_work(T* p) { 914 T o = oopDesc::load_heap_oop(p); 915 if (!oopDesc::is_null(o)) { 916 oop obj = oopDesc::decode_heap_oop_not_null(o); 917 ShenandoahHeap* heap = ShenandoahHeap::heap(); 918 919 if (!heap->marking_context()->is_marked(obj)) { 920 ShenandoahAsserts::print_failure(ShenandoahAsserts::_safe_all, obj, p, NULL, 921 "Verify Roots In To-Space", "Should be marked", __FILE__, __LINE__); 922 } 923 924 if (heap->in_collection_set(obj)) { 925 ShenandoahAsserts::print_failure(ShenandoahAsserts::_safe_all, obj, p, NULL, 926 "Verify Roots In To-Space", "Should not be in collection set", __FILE__, __LINE__); 927 } 928 929 oop fwd = (oop) ShenandoahForwarding::get_forwardee_raw_unchecked(obj); 930 if (obj != fwd) { 931 ShenandoahAsserts::print_failure(ShenandoahAsserts::_safe_all, obj, p, NULL, 932 "Verify Roots In To-Space", "Should not be forwarded", __FILE__, __LINE__); 933 } 934 } 935 } 936 937 public: 938 void do_oop(narrowOop* p) { do_oop_work(p); } 939 void do_oop(oop* p) { do_oop_work(p); } 940 }; 941 942 void ShenandoahVerifier::verify_roots_in_to_space() { 943 ShenandoahRootVerifier verifier; 944 ShenandoahVerifyInToSpaceClosure cl; 945 verifier.oops_do(&cl); 946 } 947 948 void ShenandoahVerifier::verify_roots_no_forwarded() { 949 guarantee(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "only when nothing else happens"); 950 ShenandoahRootVerifier verifier; 951 ShenandoahVerifyNoForwared cl; 952 verifier.oops_do(&cl); 953 } 954 955 void ShenandoahVerifier::verify_roots_no_forwarded_except(ShenandoahRootVerifier::RootTypes types) { 956 guarantee(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "only when nothing else happens"); 957 ShenandoahRootVerifier verifier; 958 verifier.excludes(types); 959 ShenandoahVerifyNoForwared cl; 960 verifier.oops_do(&cl); 961 } 962