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