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