< prev index next >

src/hotspot/share/gc/shenandoah/shenandoahVerifier.cpp

Print this page

  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "gc/shared/tlab_globals.hpp"
  27 #include "gc/shenandoah/shenandoahAsserts.hpp"
  28 #include "gc/shenandoah/shenandoahForwarding.inline.hpp"
  29 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"

  30 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  31 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"

  32 #include "gc/shenandoah/shenandoahRootProcessor.hpp"
  33 #include "gc/shenandoah/shenandoahTaskqueue.inline.hpp"
  34 #include "gc/shenandoah/shenandoahUtils.hpp"
  35 #include "gc/shenandoah/shenandoahVerifier.hpp"

  36 #include "memory/allocation.hpp"
  37 #include "memory/iterator.inline.hpp"
  38 #include "memory/resourceArea.hpp"
  39 #include "oops/compressedOops.inline.hpp"
  40 #include "runtime/atomic.hpp"
  41 #include "runtime/orderAccess.hpp"
  42 #include "runtime/threads.hpp"
  43 #include "utilities/align.hpp"
  44 
  45 // Avoid name collision on verify_oop (defined in macroAssembler_arm.hpp)
  46 #ifdef verify_oop
  47 #undef verify_oop
  48 #endif
  49 
  50 static bool is_instance_ref_klass(Klass* k) {
  51   return k->is_instance_klass() && InstanceKlass::cast(k)->reference_type() != REF_NONE;
  52 }
  53 
  54 class ShenandoahIgnoreReferenceDiscoverer : public ReferenceDiscoverer {
  55 public:
  56   virtual bool discover_reference(oop obj, ReferenceType type) {
  57     return true;
  58   }
  59 };
  60 
  61 class ShenandoahVerifyOopClosure : public BasicOopIterateClosure {
  62 private:
  63   const char* _phase;
  64   ShenandoahVerifier::VerifyOptions _options;
  65   ShenandoahVerifierStack* _stack;
  66   ShenandoahHeap* _heap;
  67   MarkBitMap* _map;
  68   ShenandoahLivenessData* _ld;
  69   void* _interior_loc;
  70   oop _loc;

  71 
  72 public:
  73   ShenandoahVerifyOopClosure(ShenandoahVerifierStack* stack, MarkBitMap* map, ShenandoahLivenessData* ld,
  74                              const char* phase, ShenandoahVerifier::VerifyOptions options) :
  75     _phase(phase),
  76     _options(options),
  77     _stack(stack),
  78     _heap(ShenandoahHeap::heap()),
  79     _map(map),
  80     _ld(ld),
  81     _interior_loc(NULL),
  82     _loc(NULL) {

  83     if (options._verify_marked == ShenandoahVerifier::_verify_marked_complete_except_references ||
  84         options._verify_marked == ShenandoahVerifier::_verify_marked_disable) {
  85       set_ref_discoverer_internal(new ShenandoahIgnoreReferenceDiscoverer());
  86     }





  87   }
  88 
  89 private:
  90   void check(ShenandoahAsserts::SafeLevel level, oop obj, bool test, const char* label) {
  91     if (!test) {
  92       ShenandoahAsserts::print_failure(level, obj, _interior_loc, _loc, _phase, label, __FILE__, __LINE__);
  93     }
  94   }
  95 
  96   template <class T>
  97   void do_oop_work(T* p) {
  98     T o = RawAccess<>::oop_load(p);
  99     if (!CompressedOops::is_null(o)) {
 100       oop obj = CompressedOops::decode_not_null(o);
 101       if (is_instance_ref_klass(obj->klass())) {
 102         obj = ShenandoahForwarding::get_forwardee(obj);
 103       }
 104       // Single threaded verification can use faster non-atomic stack and bitmap
 105       // methods.
 106       //
 107       // For performance reasons, only fully verify non-marked field values.
 108       // We are here when the host object for *p is already marked.
 109 
 110       if (_map->par_mark(obj)) {
 111         verify_oop_at(p, obj);
 112         _stack->push(ShenandoahVerifierTask(obj));
 113       }
 114     }
 115   }
 116 









 117   void verify_oop(oop obj) {
 118     // Perform consistency checks with gradually decreasing safety level. This guarantees
 119     // that failure report would not try to touch something that was not yet verified to be
 120     // safe to process.
 121 
 122     check(ShenandoahAsserts::_safe_unknown, obj, _heap->is_in(obj),
 123               "oop must be in heap");
 124     check(ShenandoahAsserts::_safe_unknown, obj, is_object_aligned(obj),
 125               "oop must be aligned");
 126 
 127     ShenandoahHeapRegion *obj_reg = _heap->heap_region_containing(obj);
 128     Klass* obj_klass = obj->klass_or_null();
 129 
 130     // Verify that obj is not in dead space:
 131     {
 132       // Do this before touching obj->size()
 133       check(ShenandoahAsserts::_safe_unknown, obj, obj_klass != NULL,
 134              "Object klass pointer should not be NULL");
 135       check(ShenandoahAsserts::_safe_unknown, obj, Metaspace::contains(obj_klass),
 136              "Object klass pointer must go to metaspace");
 137 
 138       HeapWord *obj_addr = cast_from_oop<HeapWord*>(obj);
 139       check(ShenandoahAsserts::_safe_unknown, obj, obj_addr < obj_reg->top(),
 140              "Object start should be within the region");
 141 
 142       if (!obj_reg->is_humongous()) {
 143         check(ShenandoahAsserts::_safe_unknown, obj, (obj_addr + obj->size()) <= obj_reg->top(),
 144                "Object end should be within the region");
 145       } else {
 146         size_t humongous_start = obj_reg->index();
 147         size_t humongous_end = humongous_start + (obj->size() >> ShenandoahHeapRegion::region_size_words_shift());
 148         for (size_t idx = humongous_start + 1; idx < humongous_end; idx++) {
 149           check(ShenandoahAsserts::_safe_unknown, obj, _heap->get_region(idx)->is_humongous_continuation(),
 150                  "Humongous object is in continuation that fits it");
 151         }
 152       }
 153 
 154       // ------------ obj is safe at this point --------------
 155 
 156       check(ShenandoahAsserts::_safe_oop, obj, obj_reg->is_active(),
 157             "Object should be in active region");
 158 
 159       switch (_options._verify_liveness) {
 160         case ShenandoahVerifier::_verify_liveness_disable:
 161           // skip
 162           break;
 163         case ShenandoahVerifier::_verify_liveness_complete:
 164           Atomic::add(&_ld[obj_reg->index()], (uint) obj->size(), memory_order_relaxed);
 165           // fallthrough for fast failure for un-live regions:
 166         case ShenandoahVerifier::_verify_liveness_conservative:
 167           check(ShenandoahAsserts::_safe_oop, obj, obj_reg->has_live(),

 168                    "Object must belong to region with live data");
 169           break;
 170         default:
 171           assert(false, "Unhandled liveness verification");
 172       }
 173     }
 174 
 175     oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj);
 176 
 177     ShenandoahHeapRegion* fwd_reg = NULL;
 178 
 179     if (obj != fwd) {
 180       check(ShenandoahAsserts::_safe_oop, obj, _heap->is_in(fwd),
 181              "Forwardee must be in heap");
 182       check(ShenandoahAsserts::_safe_oop, obj, !CompressedOops::is_null(fwd),
 183              "Forwardee is set");
 184       check(ShenandoahAsserts::_safe_oop, obj, is_object_aligned(fwd),
 185              "Forwardee must be aligned");
 186 
 187       // Do this before touching fwd->size()
 188       Klass* fwd_klass = fwd->klass_or_null();
 189       check(ShenandoahAsserts::_safe_oop, obj, fwd_klass != NULL,
 190              "Forwardee klass pointer should not be NULL");
 191       check(ShenandoahAsserts::_safe_oop, obj, Metaspace::contains(fwd_klass),
 192              "Forwardee klass pointer must go to metaspace");
 193       check(ShenandoahAsserts::_safe_oop, obj, obj_klass == fwd_klass,
 194              "Forwardee klass pointer must go to metaspace");
 195 
 196       fwd_reg = _heap->heap_region_containing(fwd);
 197 
 198       // Verify that forwardee is not in the dead space:
 199       check(ShenandoahAsserts::_safe_oop, obj, !fwd_reg->is_humongous(),
 200              "Should have no humongous forwardees");
 201 
 202       HeapWord *fwd_addr = cast_from_oop<HeapWord *>(fwd);
 203       check(ShenandoahAsserts::_safe_oop, obj, fwd_addr < fwd_reg->top(),
 204              "Forwardee start should be within the region");
 205       check(ShenandoahAsserts::_safe_oop, obj, (fwd_addr + fwd->size()) <= fwd_reg->top(),
 206              "Forwardee end should be within the region");
 207 
 208       oop fwd2 = ShenandoahForwarding::get_forwardee_raw_unchecked(fwd);
 209       check(ShenandoahAsserts::_safe_oop, obj, (fwd == fwd2),
 210              "Double forwarding");
 211     } else {
 212       fwd_reg = obj_reg;
 213     }
 214 
 215     // ------------ obj and fwd are safe at this point --------------
 216 







 217     switch (_options._verify_marked) {
 218       case ShenandoahVerifier::_verify_marked_disable:
 219         // skip
 220         break;
 221       case ShenandoahVerifier::_verify_marked_incomplete:
 222         check(ShenandoahAsserts::_safe_all, obj, _heap->marking_context()->is_marked(obj),
 223                "Must be marked in incomplete bitmap");
 224         break;
 225       case ShenandoahVerifier::_verify_marked_complete:
 226         check(ShenandoahAsserts::_safe_all, obj, _heap->complete_marking_context()->is_marked(obj),
 227                "Must be marked in complete bitmap");
 228         break;
 229       case ShenandoahVerifier::_verify_marked_complete_except_references:
 230         check(ShenandoahAsserts::_safe_all, obj, _heap->complete_marking_context()->is_marked(obj),
 231               "Must be marked in complete bitmap, except j.l.r.Reference referents");
 232         break;
 233       default:
 234         assert(false, "Unhandled mark verification");
 235     }
 236 
 237     switch (_options._verify_forwarded) {
 238       case ShenandoahVerifier::_verify_forwarded_disable:
 239         // skip
 240         break;
 241       case ShenandoahVerifier::_verify_forwarded_none: {
 242         check(ShenandoahAsserts::_safe_all, obj, (obj == fwd),
 243                "Should not be forwarded");
 244         break;
 245       }
 246       case ShenandoahVerifier::_verify_forwarded_allow: {
 247         if (obj != fwd) {
 248           check(ShenandoahAsserts::_safe_all, obj, obj_reg != fwd_reg,
 249                  "Forwardee should be in another region");
 250         }

 304    * @param obj host object for verified fields
 305    */
 306   void verify_oops_from(oop obj) {
 307     _loc = obj;
 308     obj->oop_iterate(this);
 309     _loc = NULL;
 310   }
 311 
 312   virtual void do_oop(oop* p) { do_oop_work(p); }
 313   virtual void do_oop(narrowOop* p) { do_oop_work(p); }
 314 };
 315 
 316 class ShenandoahCalculateRegionStatsClosure : public ShenandoahHeapRegionClosure {
 317 private:
 318   size_t _used, _committed, _garbage;
 319 public:
 320   ShenandoahCalculateRegionStatsClosure() : _used(0), _committed(0), _garbage(0) {};
 321 
 322   void heap_region_do(ShenandoahHeapRegion* r) {
 323     _used += r->used();


 324     _garbage += r->garbage();
 325     _committed += r->is_committed() ? ShenandoahHeapRegion::region_size_bytes() : 0;
 326   }
 327 
 328   size_t used() { return _used; }
 329   size_t committed() { return _committed; }
 330   size_t garbage() { return _garbage; }
 331 };
 332 







































 333 class ShenandoahVerifyHeapRegionClosure : public ShenandoahHeapRegionClosure {
 334 private:
 335   ShenandoahHeap* _heap;
 336   const char* _phase;
 337   ShenandoahVerifier::VerifyRegions _regions;
 338 public:
 339   ShenandoahVerifyHeapRegionClosure(const char* phase, ShenandoahVerifier::VerifyRegions regions) :
 340     _heap(ShenandoahHeap::heap()),
 341     _phase(phase),
 342     _regions(regions) {};
 343 
 344   void print_failure(ShenandoahHeapRegion* r, const char* label) {
 345     ResourceMark rm;
 346 
 347     ShenandoahMessageBuffer msg("Shenandoah verification failed; %s: %s\n\n", _phase, label);
 348 
 349     stringStream ss;
 350     r->print_on(&ss);
 351     msg.append("%s", ss.as_string());
 352 

 394            "Complete TAMS should not be larger than top");
 395 
 396     verify(r, r->get_live_data_bytes() <= r->capacity(),
 397            "Live data cannot be larger than capacity");
 398 
 399     verify(r, r->garbage() <= r->capacity(),
 400            "Garbage cannot be larger than capacity");
 401 
 402     verify(r, r->used() <= r->capacity(),
 403            "Used cannot be larger than capacity");
 404 
 405     verify(r, r->get_shared_allocs() <= r->capacity(),
 406            "Shared alloc count should not be larger than capacity");
 407 
 408     verify(r, r->get_tlab_allocs() <= r->capacity(),
 409            "TLAB alloc count should not be larger than capacity");
 410 
 411     verify(r, r->get_gclab_allocs() <= r->capacity(),
 412            "GCLAB alloc count should not be larger than capacity");
 413 
 414     verify(r, r->get_shared_allocs() + r->get_tlab_allocs() + r->get_gclab_allocs() == r->used(),
 415            "Accurate accounting: shared + TLAB + GCLAB = used");



 416 
 417     verify(r, !r->is_empty() || !r->has_live(),
 418            "Empty regions should not have live data");
 419 
 420     verify(r, r->is_cset() == _heap->collection_set()->is_in(r),
 421            "Transitional: region flags and collection set agree");
 422   }
 423 };
 424 
 425 class ShenandoahVerifierReachableTask : public WorkerTask {
 426 private:
 427   const char* _label;
 428   ShenandoahVerifier::VerifyOptions _options;
 429   ShenandoahHeap* _heap;
 430   ShenandoahLivenessData* _ld;
 431   MarkBitMap* _bitmap;
 432   volatile size_t _processed;
 433 
 434 public:
 435   ShenandoahVerifierReachableTask(MarkBitMap* bitmap,

 472 
 473     if (ShenandoahVerifyLevel >= 3) {
 474       ShenandoahVerifyOopClosure cl(&stack, _bitmap, _ld,
 475                                     ShenandoahMessageBuffer("%s, Reachable", _label),
 476                                     _options);
 477       while (!stack.is_empty()) {
 478         processed++;
 479         ShenandoahVerifierTask task = stack.pop();
 480         cl.verify_oops_from(task.obj());
 481       }
 482     }
 483 
 484     Atomic::add(&_processed, processed, memory_order_relaxed);
 485   }
 486 };
 487 
 488 class ShenandoahVerifierMarkedRegionTask : public WorkerTask {
 489 private:
 490   const char* _label;
 491   ShenandoahVerifier::VerifyOptions _options;
 492   ShenandoahHeap *_heap;
 493   MarkBitMap* _bitmap;
 494   ShenandoahLivenessData* _ld;
 495   volatile size_t _claimed;
 496   volatile size_t _processed;

 497 
 498 public:
 499   ShenandoahVerifierMarkedRegionTask(MarkBitMap* bitmap,
 500                                      ShenandoahLivenessData* ld,
 501                                      const char* label,
 502                                      ShenandoahVerifier::VerifyOptions options) :
 503           WorkerTask("Shenandoah Verifier Marked Objects"),
 504           _label(label),
 505           _options(options),
 506           _heap(ShenandoahHeap::heap()),
 507           _bitmap(bitmap),
 508           _ld(ld),
 509           _claimed(0),
 510           _processed(0) {};






 511 
 512   size_t processed() {
 513     return Atomic::load(&_processed);
 514   }
 515 
 516   virtual void work(uint worker_id) {
 517     ShenandoahVerifierStack stack;
 518     ShenandoahVerifyOopClosure cl(&stack, _bitmap, _ld,
 519                                   ShenandoahMessageBuffer("%s, Marked", _label),
 520                                   _options);
 521 
 522     while (true) {
 523       size_t v = Atomic::fetch_and_add(&_claimed, 1u, memory_order_relaxed);
 524       if (v < _heap->num_regions()) {
 525         ShenandoahHeapRegion* r = _heap->get_region(v);




 526         if (!r->is_humongous() && !r->is_trash()) {
 527           work_regular(r, stack, cl);
 528         } else if (r->is_humongous_start()) {
 529           work_humongous(r, stack, cl);
 530         }
 531       } else {
 532         break;
 533       }
 534     }
 535   }
 536 
 537   virtual void work_humongous(ShenandoahHeapRegion *r, ShenandoahVerifierStack& stack, ShenandoahVerifyOopClosure& cl) {




 538     size_t processed = 0;
 539     HeapWord* obj = r->bottom();
 540     if (_heap->complete_marking_context()->is_marked(cast_to_oop(obj))) {
 541       verify_and_follow(obj, stack, cl, &processed);
 542     }
 543     Atomic::add(&_processed, processed, memory_order_relaxed);
 544   }
 545 
 546   virtual void work_regular(ShenandoahHeapRegion *r, ShenandoahVerifierStack &stack, ShenandoahVerifyOopClosure &cl) {
 547     size_t processed = 0;
 548     ShenandoahMarkingContext* ctx = _heap->complete_marking_context();
 549     HeapWord* tams = ctx->top_at_mark_start(r);
 550 
 551     // Bitmaps, before TAMS
 552     if (tams > r->bottom()) {
 553       HeapWord* start = r->bottom();
 554       HeapWord* addr = ctx->get_next_marked_addr(start, tams);
 555 
 556       while (addr < tams) {
 557         verify_and_follow(addr, stack, cl, &processed);
 558         addr += 1;
 559         if (addr < tams) {
 560           addr = ctx->get_next_marked_addr(addr, tams);
 561         }
 562       }
 563     }
 564 
 565     // Size-based, after TAMS
 566     {
 567       HeapWord* limit = r->top();
 568       HeapWord* addr = tams;
 569 
 570       while (addr < limit) {
 571         verify_and_follow(addr, stack, cl, &processed);
 572         addr += cast_to_oop(addr)->size();
 573       }
 574     }
 575 
 576     Atomic::add(&_processed, processed, memory_order_relaxed);
 577   }
 578 
 579   void verify_and_follow(HeapWord *addr, ShenandoahVerifierStack &stack, ShenandoahVerifyOopClosure &cl, size_t *processed) {
 580     if (!_bitmap->par_mark(addr)) return;
 581 
 582     // Verify the object itself:
 583     oop obj = cast_to_oop(addr);
 584     cl.verify_oop_standalone(obj);
 585 
 586     // Verify everything reachable from that object too, hopefully realizing
 587     // everything was already marked, and never touching further:
 588     if (!is_instance_ref_klass(obj->klass())) {
 589       cl.verify_oops_from(obj);
 590       (*processed)++;
 591     }
 592     while (!stack.is_empty()) {
 593       ShenandoahVerifierTask task = stack.pop();
 594       cl.verify_oops_from(task.obj());
 595       (*processed)++;
 596     }
 597   }
 598 };
 599 
 600 class VerifyThreadGCState : public ThreadClosure {
 601 private:
 602   const char* const _label;
 603          char const _expected;
 604 
 605 public:
 606   VerifyThreadGCState(const char* label, char expected) : _label(label), _expected(expected) {}
 607   void do_thread(Thread* t) {
 608     char actual = ShenandoahThreadLocalData::gc_state(t);
 609     if (actual != _expected) {
 610       fatal("%s: Thread %s: expected gc-state %d, actual %d", _label, t->name(), _expected, actual);
 611     }
 612   }
 613 };
 614 
 615 void ShenandoahVerifier::verify_at_safepoint(const char *label,

 616                                              VerifyForwarded forwarded, VerifyMarked marked,
 617                                              VerifyCollectionSet cset,
 618                                              VerifyLiveness liveness, VerifyRegions regions,
 619                                              VerifyGCState gcstate) {
 620   guarantee(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "only when nothing else happens");
 621   guarantee(ShenandoahVerify, "only when enabled, and bitmap is initialized in ShenandoahHeap::initialize");
 622 
 623   // Avoid side-effect of changing workers' active thread count, but bypass concurrent/parallel protocol check
 624   ShenandoahPushWorkerScope verify_worker_scope(_heap->workers(), _heap->max_workers(), false /*bypass check*/);
 625 
 626   log_info(gc,start)("Verify %s, Level " INTX_FORMAT, label, ShenandoahVerifyLevel);
 627 
 628   // GC state checks
 629   {
 630     char expected = -1;
 631     bool enabled;
 632     switch (gcstate) {
 633       case _verify_gcstate_disable:
 634         enabled = false;
 635         break;
 636       case _verify_gcstate_forwarded:
 637         enabled = true;
 638         expected = ShenandoahHeap::HAS_FORWARDED;
 639         break;
 640       case _verify_gcstate_evacuation:
 641         enabled = true;
 642         expected = ShenandoahHeap::HAS_FORWARDED | ShenandoahHeap::EVACUATION;
 643         if (!_heap->is_stw_gc_in_progress()) {
 644           // Only concurrent GC sets this.
 645           expected |= ShenandoahHeap::WEAK_ROOTS;
 646         }
 647         break;




 648       case _verify_gcstate_stable:
 649         enabled = true;
 650         expected = ShenandoahHeap::STABLE;
 651         break;
 652       case _verify_gcstate_stable_weakroots:
 653         enabled = true;
 654         expected = ShenandoahHeap::STABLE;
 655         if (!_heap->is_stw_gc_in_progress()) {
 656           // Only concurrent GC sets this.
 657           expected |= ShenandoahHeap::WEAK_ROOTS;
 658         }
 659         break;
 660       default:
 661         enabled = false;
 662         assert(false, "Unhandled gc-state verification");
 663     }
 664 
 665     if (enabled) {
 666       char actual = _heap->gc_state();
 667       if (actual != expected) {

 668         fatal("%s: Global gc-state: expected %d, actual %d", label, expected, actual);
 669       }
 670 
 671       VerifyThreadGCState vtgcs(label, expected);
 672       Threads::java_threads_do(&vtgcs);
 673     }
 674   }
 675 
 676   // Deactivate barriers temporarily: Verifier wants plain heap accesses
 677   ShenandoahGCStateResetter resetter;
 678 
 679   // Heap size checks
 680   {
 681     ShenandoahHeapLocker lock(_heap->lock());
 682 
 683     ShenandoahCalculateRegionStatsClosure cl;
 684     _heap->heap_region_iterate(&cl);

 685     size_t heap_used = _heap->used();

 686     guarantee(cl.used() == heap_used,
 687               "%s: heap used size must be consistent: heap-used = " SIZE_FORMAT "%s, regions-used = " SIZE_FORMAT "%s",
 688               label,
 689               byte_size_in_proper_unit(heap_used), proper_unit_for_byte_size(heap_used),
 690               byte_size_in_proper_unit(cl.used()), proper_unit_for_byte_size(cl.used()));
 691 
 692     size_t heap_committed = _heap->committed();
 693     guarantee(cl.committed() == heap_committed,
 694               "%s: heap committed size must be consistent: heap-committed = " SIZE_FORMAT "%s, regions-committed = " SIZE_FORMAT "%s",
 695               label,
 696               byte_size_in_proper_unit(heap_committed), proper_unit_for_byte_size(heap_committed),
 697               byte_size_in_proper_unit(cl.committed()), proper_unit_for_byte_size(cl.committed()));
 698   }
 699 













































 700   // Internal heap region checks
 701   if (ShenandoahVerifyLevel >= 1) {
 702     ShenandoahVerifyHeapRegionClosure cl(label, regions);
 703     _heap->heap_region_iterate(&cl);




 704   }
 705 


 706   OrderAccess::fence();
 707 
 708   if (UseTLAB) {
 709     _heap->labs_make_parsable();
 710   }
 711 
 712   // Allocate temporary bitmap for storing marking wavefront:
 713   _verification_bit_map->clear();
 714 
 715   // Allocate temporary array for storing liveness data
 716   ShenandoahLivenessData* ld = NEW_C_HEAP_ARRAY(ShenandoahLivenessData, _heap->num_regions(), mtGC);
 717   Copy::fill_to_bytes((void*)ld, _heap->num_regions()*sizeof(ShenandoahLivenessData), 0);
 718 
 719   const VerifyOptions& options = ShenandoahVerifier::VerifyOptions(forwarded, marked, cset, liveness, regions, gcstate);
 720 
 721   // Steps 1-2. Scan root set to get initial reachable set. Finish walking the reachable heap.
 722   // This verifies what application can see, since it only cares about reachable objects.
 723   size_t count_reachable = 0;
 724   if (ShenandoahVerifyLevel >= 2) {
 725     ShenandoahVerifierReachableTask task(_verification_bit_map, ld, label, options);
 726     _heap->workers()->run_task(&task);
 727     count_reachable = task.processed();
 728   }
 729 


 730   // Step 3. Walk marked objects. Marked objects might be unreachable. This verifies what collector,
 731   // not the application, can see during the region scans. There is no reason to process the objects
 732   // that were already verified, e.g. those marked in verification bitmap. There is interaction with TAMS:
 733   // before TAMS, we verify the bitmaps, if available; after TAMS, we walk until the top(). It mimics
 734   // what marked_object_iterate is doing, without calling into that optimized (and possibly incorrect)
 735   // version
 736 
 737   size_t count_marked = 0;
 738   if (ShenandoahVerifyLevel >= 4 && (marked == _verify_marked_complete || marked == _verify_marked_complete_except_references)) {
 739     guarantee(_heap->marking_context()->is_complete(), "Marking context should be complete");
 740     ShenandoahVerifierMarkedRegionTask task(_verification_bit_map, ld, label, options);
 741     _heap->workers()->run_task(&task);
 742     count_marked = task.processed();
 743   } else {
 744     guarantee(ShenandoahVerifyLevel < 4 || marked == _verify_marked_incomplete || marked == _verify_marked_disable, "Should be");
 745   }
 746 


 747   // Step 4. Verify accumulated liveness data, if needed. Only reliable if verification level includes
 748   // marked objects.
 749 
 750   if (ShenandoahVerifyLevel >= 4 && marked == _verify_marked_complete && liveness == _verify_liveness_complete) {
 751     for (size_t i = 0; i < _heap->num_regions(); i++) {
 752       ShenandoahHeapRegion* r = _heap->get_region(i);



 753 
 754       juint verf_live = 0;
 755       if (r->is_humongous()) {
 756         // For humongous objects, test if start region is marked live, and if so,
 757         // all humongous regions in that chain have live data equal to their "used".
 758         juint start_live = Atomic::load(&ld[r->humongous_start_region()->index()]);
 759         if (start_live > 0) {
 760           verf_live = (juint)(r->used() / HeapWordSize);
 761         }
 762       } else {
 763         verf_live = Atomic::load(&ld[r->index()]);
 764       }
 765 
 766       size_t reg_live = r->get_live_data_words();
 767       if (reg_live != verf_live) {
 768         ResourceMark rm;
 769         stringStream ss;
 770         r->print_on(&ss);
 771         fatal("%s: Live data should match: region-live = " SIZE_FORMAT ", verifier-live = " UINT32_FORMAT "\n%s",
 772               label, reg_live, verf_live, ss.as_string());
 773       }
 774     }
 775   }
 776 



 777   log_info(gc)("Verify %s, Level " INTX_FORMAT " (" SIZE_FORMAT " reachable, " SIZE_FORMAT " marked)",
 778                label, ShenandoahVerifyLevel, count_reachable, count_marked);
 779 
 780   FREE_C_HEAP_ARRAY(ShenandoahLivenessData, ld);
 781 }
 782 
 783 void ShenandoahVerifier::verify_generic(VerifyOption vo) {
 784   verify_at_safepoint(
 785           "Generic Verification",

 786           _verify_forwarded_allow,     // conservatively allow forwarded
 787           _verify_marked_disable,      // do not verify marked: lots ot time wasted checking dead allocations
 788           _verify_cset_disable,        // cset may be inconsistent
 789           _verify_liveness_disable,    // no reliable liveness data
 790           _verify_regions_disable,     // no reliable region data
 791           _verify_gcstate_disable      // no data about gcstate
 792   );
 793 }
 794 
 795 void ShenandoahVerifier::verify_before_concmark() {
 796     verify_at_safepoint(
 797           "Before Mark",

 798           _verify_forwarded_none,      // UR should have fixed up
 799           _verify_marked_disable,      // do not verify marked: lots ot time wasted checking dead allocations
 800           _verify_cset_none,           // UR should have fixed this
 801           _verify_liveness_disable,    // no reliable liveness data
 802           _verify_regions_notrash,     // no trash regions
 803           _verify_gcstate_stable       // there are no forwarded objects
 804   );
 805 }
 806 
 807 void ShenandoahVerifier::verify_after_concmark() {
 808   verify_at_safepoint(
 809           "After Mark",

 810           _verify_forwarded_none,      // no forwarded references
 811           _verify_marked_complete_except_references, // bitmaps as precise as we can get, except dangling j.l.r.Refs
 812           _verify_cset_none,           // no references to cset anymore
 813           _verify_liveness_complete,   // liveness data must be complete here
 814           _verify_regions_disable,     // trash regions not yet recycled
 815           _verify_gcstate_stable_weakroots  // heap is still stable, weakroots are in progress
 816   );
 817 }
 818 
 819 void ShenandoahVerifier::verify_before_evacuation() {
 820   verify_at_safepoint(
 821           "Before Evacuation",

 822           _verify_forwarded_none,                    // no forwarded references
 823           _verify_marked_complete_except_references, // walk over marked objects too
 824           _verify_cset_disable,                      // non-forwarded references to cset expected
 825           _verify_liveness_complete,                 // liveness data must be complete here
 826           _verify_regions_disable,                   // trash regions not yet recycled
 827           _verify_gcstate_stable_weakroots           // heap is still stable, weakroots are in progress
 828   );
 829 }
 830 
 831 void ShenandoahVerifier::verify_during_evacuation() {
 832   verify_at_safepoint(
 833           "During Evacuation",

 834           _verify_forwarded_allow,    // some forwarded references are allowed
 835           _verify_marked_disable,     // walk only roots
 836           _verify_cset_disable,       // some cset references are not forwarded yet
 837           _verify_liveness_disable,   // liveness data might be already stale after pre-evacs
 838           _verify_regions_disable,    // trash regions not yet recycled
 839           _verify_gcstate_evacuation  // evacuation is in progress
 840   );
 841 }
 842 
 843 void ShenandoahVerifier::verify_after_evacuation() {
 844   verify_at_safepoint(
 845           "After Evacuation",

 846           _verify_forwarded_allow,     // objects are still forwarded
 847           _verify_marked_complete,     // bitmaps might be stale, but alloc-after-mark should be well
 848           _verify_cset_forwarded,      // all cset refs are fully forwarded
 849           _verify_liveness_disable,    // no reliable liveness data anymore
 850           _verify_regions_notrash,     // trash regions have been recycled already
 851           _verify_gcstate_forwarded    // evacuation produced some forwarded objects
 852   );
 853 }
 854 
 855 void ShenandoahVerifier::verify_before_updaterefs() {
 856   verify_at_safepoint(
 857           "Before Updating References",
 858           _verify_forwarded_allow,     // forwarded references allowed
 859           _verify_marked_complete,     // bitmaps might be stale, but alloc-after-mark should be well
 860           _verify_cset_forwarded,      // all cset refs are fully forwarded
 861           _verify_liveness_disable,    // no reliable liveness data anymore
 862           _verify_regions_notrash,     // trash regions have been recycled already
 863           _verify_gcstate_forwarded    // evacuation should have produced some forwarded objects

 864   );
 865 }
 866 
 867 void ShenandoahVerifier::verify_after_updaterefs() {
 868   verify_at_safepoint(
 869           "After Updating References",

 870           _verify_forwarded_none,      // no forwarded references
 871           _verify_marked_complete,     // bitmaps might be stale, but alloc-after-mark should be well
 872           _verify_cset_none,           // no cset references, all updated
 873           _verify_liveness_disable,    // no reliable liveness data anymore
 874           _verify_regions_nocset,      // no cset regions, trash regions have appeared
 875           _verify_gcstate_stable       // update refs had cleaned up forwarded objects
 876   );
 877 }
 878 
 879 void ShenandoahVerifier::verify_after_degenerated() {
 880   verify_at_safepoint(
 881           "After Degenerated GC",

 882           _verify_forwarded_none,      // all objects are non-forwarded
 883           _verify_marked_complete,     // all objects are marked in complete bitmap
 884           _verify_cset_none,           // no cset references
 885           _verify_liveness_disable,    // no reliable liveness data anymore
 886           _verify_regions_notrash_nocset, // no trash, no cset
 887           _verify_gcstate_stable       // degenerated refs had cleaned up forwarded objects
 888   );
 889 }
 890 
 891 void ShenandoahVerifier::verify_before_fullgc() {
 892   verify_at_safepoint(
 893           "Before Full GC",

 894           _verify_forwarded_allow,     // can have forwarded objects
 895           _verify_marked_disable,      // do not verify marked: lots ot time wasted checking dead allocations
 896           _verify_cset_disable,        // cset might be foobared
 897           _verify_liveness_disable,    // no reliable liveness data anymore
 898           _verify_regions_disable,     // no reliable region data here
 899           _verify_gcstate_disable      // no reliable gcstate data
 900   );
 901 }
 902 













 903 void ShenandoahVerifier::verify_after_fullgc() {
 904   verify_at_safepoint(
 905           "After Full GC",

 906           _verify_forwarded_none,      // all objects are non-forwarded
 907           _verify_marked_complete,     // all objects are marked in complete bitmap
 908           _verify_cset_none,           // no cset references
 909           _verify_liveness_disable,    // no reliable liveness data anymore
 910           _verify_regions_notrash_nocset, // no trash, no cset
 911           _verify_gcstate_stable        // full gc cleaned up everything
 912   );
 913 }
 914 
 915 class ShenandoahVerifyNoForwared : public OopClosure {
 916 private:
 917   template <class T>
 918   void do_oop_work(T* p) {
 919     T o = RawAccess<>::oop_load(p);
 920     if (!CompressedOops::is_null(o)) {
 921       oop obj = CompressedOops::decode_not_null(o);
 922       oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj);
 923       if (obj != fwd) {
 924         ShenandoahAsserts::print_failure(ShenandoahAsserts::_safe_all, obj, p, NULL,
 925                                          "Verify Roots", "Should not be forwarded", __FILE__, __LINE__);
 926       }
 927     }
 928   }
 929 
 930 public:
 931   void do_oop(narrowOop* p) { do_oop_work(p); }
 932   void do_oop(oop* p)       { do_oop_work(p); }
 933 };
 934 
 935 class ShenandoahVerifyInToSpaceClosure : public OopClosure {
 936 private:
 937   template <class T>
 938   void do_oop_work(T* p) {
 939     T o = RawAccess<>::oop_load(p);
 940     if (!CompressedOops::is_null(o)) {
 941       oop obj = CompressedOops::decode_not_null(o);
 942       ShenandoahHeap* heap = ShenandoahHeap::heap();
 943 
 944       if (!heap->marking_context()->is_marked(obj)) {
 945         ShenandoahAsserts::print_failure(ShenandoahAsserts::_safe_all, obj, p, NULL,
 946                 "Verify Roots In To-Space", "Should be marked", __FILE__, __LINE__);
 947       }
 948 
 949       if (heap->in_collection_set(obj)) {
 950         ShenandoahAsserts::print_failure(ShenandoahAsserts::_safe_all, obj, p, NULL,
 951                 "Verify Roots In To-Space", "Should not be in collection set", __FILE__, __LINE__);
 952       }
 953 
 954       oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj);
 955       if (obj != fwd) {
 956         ShenandoahAsserts::print_failure(ShenandoahAsserts::_safe_all, obj, p, NULL,
 957                 "Verify Roots In To-Space", "Should not be forwarded", __FILE__, __LINE__);
 958       }
 959     }
 960   }
 961 
 962 public:
 963   void do_oop(narrowOop* p) { do_oop_work(p); }
 964   void do_oop(oop* p)       { do_oop_work(p); }

  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "gc/shared/tlab_globals.hpp"
  27 #include "gc/shenandoah/shenandoahAsserts.hpp"
  28 #include "gc/shenandoah/shenandoahForwarding.inline.hpp"
  29 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
  30 #include "gc/shenandoah/shenandoahGeneration.hpp"
  31 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  32 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
  33 #include "gc/shenandoah/shenandoahOldGeneration.hpp"
  34 #include "gc/shenandoah/shenandoahRootProcessor.hpp"
  35 #include "gc/shenandoah/shenandoahTaskqueue.inline.hpp"
  36 #include "gc/shenandoah/shenandoahUtils.hpp"
  37 #include "gc/shenandoah/shenandoahVerifier.hpp"
  38 #include "gc/shenandoah/shenandoahYoungGeneration.hpp"
  39 #include "memory/allocation.hpp"
  40 #include "memory/iterator.inline.hpp"
  41 #include "memory/resourceArea.hpp"
  42 #include "oops/compressedOops.inline.hpp"
  43 #include "runtime/atomic.hpp"
  44 #include "runtime/orderAccess.hpp"
  45 #include "runtime/threads.hpp"
  46 #include "utilities/align.hpp"
  47 
  48 // Avoid name collision on verify_oop (defined in macroAssembler_arm.hpp)
  49 #ifdef verify_oop
  50 #undef verify_oop
  51 #endif
  52 
  53 static bool is_instance_ref_klass(Klass* k) {
  54   return k->is_instance_klass() && InstanceKlass::cast(k)->reference_type() != REF_NONE;
  55 }
  56 
  57 class ShenandoahIgnoreReferenceDiscoverer : public ReferenceDiscoverer {
  58 public:
  59   virtual bool discover_reference(oop obj, ReferenceType type) {
  60     return true;
  61   }
  62 };
  63 
  64 class ShenandoahVerifyOopClosure : public BasicOopIterateClosure {
  65 private:
  66   const char* _phase;
  67   ShenandoahVerifier::VerifyOptions _options;
  68   ShenandoahVerifierStack* _stack;
  69   ShenandoahHeap* _heap;
  70   MarkBitMap* _map;
  71   ShenandoahLivenessData* _ld;
  72   void* _interior_loc;
  73   oop _loc;
  74   ShenandoahGeneration* _generation;
  75 
  76 public:
  77   ShenandoahVerifyOopClosure(ShenandoahVerifierStack* stack, MarkBitMap* map, ShenandoahLivenessData* ld,
  78                              const char* phase, ShenandoahVerifier::VerifyOptions options) :
  79     _phase(phase),
  80     _options(options),
  81     _stack(stack),
  82     _heap(ShenandoahHeap::heap()),
  83     _map(map),
  84     _ld(ld),
  85     _interior_loc(NULL),
  86     _loc(NULL),
  87     _generation(NULL) {
  88     if (options._verify_marked == ShenandoahVerifier::_verify_marked_complete_except_references ||
  89         options._verify_marked == ShenandoahVerifier::_verify_marked_disable) {
  90       set_ref_discoverer_internal(new ShenandoahIgnoreReferenceDiscoverer());
  91     }
  92 
  93     if (_heap->mode()->is_generational()) {
  94       _generation = _heap->active_generation();
  95       assert(_generation != NULL, "Expected active generation in this mode");
  96     }
  97   }
  98 
  99 private:
 100   void check(ShenandoahAsserts::SafeLevel level, oop obj, bool test, const char* label) {
 101     if (!test) {
 102       ShenandoahAsserts::print_failure(level, obj, _interior_loc, _loc, _phase, label, __FILE__, __LINE__);
 103     }
 104   }
 105 
 106   template <class T>
 107   void do_oop_work(T* p) {
 108     T o = RawAccess<>::oop_load(p);
 109     if (!CompressedOops::is_null(o)) {
 110       oop obj = CompressedOops::decode_not_null(o);
 111       if (is_instance_ref_klass(obj->klass())) {
 112         obj = ShenandoahForwarding::get_forwardee(obj);
 113       }
 114       // Single threaded verification can use faster non-atomic stack and bitmap
 115       // methods.
 116       //
 117       // For performance reasons, only fully verify non-marked field values.
 118       // We are here when the host object for *p is already marked.
 119 
 120       if ( in_generation(obj) && _map->par_mark(obj)) {
 121         verify_oop_at(p, obj);
 122         _stack->push(ShenandoahVerifierTask(obj));
 123       }
 124     }
 125   }
 126 
 127   bool in_generation(oop obj) {
 128     if (_generation == NULL) {
 129       return true;
 130     }
 131 
 132     ShenandoahHeapRegion* region = _heap->heap_region_containing(obj);
 133     return _generation->contains(region);
 134   }
 135 
 136   void verify_oop(oop obj) {
 137     // Perform consistency checks with gradually decreasing safety level. This guarantees
 138     // that failure report would not try to touch something that was not yet verified to be
 139     // safe to process.
 140 
 141     check(ShenandoahAsserts::_safe_unknown, obj, _heap->is_in(obj),
 142               "oop must be in heap");
 143     check(ShenandoahAsserts::_safe_unknown, obj, is_object_aligned(obj),
 144               "oop must be aligned");
 145 
 146     ShenandoahHeapRegion* obj_reg = _heap->heap_region_containing(obj);
 147     Klass* obj_klass = obj->klass_or_null();
 148 
 149     // Verify that obj is not in dead space:
 150     {
 151       // Do this before touching obj->size()
 152       check(ShenandoahAsserts::_safe_unknown, obj, obj_klass != NULL,
 153              "Object klass pointer should not be NULL");
 154       check(ShenandoahAsserts::_safe_unknown, obj, Metaspace::contains(obj_klass),
 155              "Object klass pointer must go to metaspace");
 156 
 157       HeapWord* obj_addr = cast_from_oop<HeapWord*>(obj);
 158       check(ShenandoahAsserts::_safe_unknown, obj, obj_addr < obj_reg->top(),
 159              "Object start should be within the region");
 160 
 161       if (!obj_reg->is_humongous()) {
 162         check(ShenandoahAsserts::_safe_unknown, obj, (obj_addr + obj->size()) <= obj_reg->top(),
 163                "Object end should be within the region");
 164       } else {
 165         size_t humongous_start = obj_reg->index();
 166         size_t humongous_end = humongous_start + (obj->size() >> ShenandoahHeapRegion::region_size_words_shift());
 167         for (size_t idx = humongous_start + 1; idx < humongous_end; idx++) {
 168           check(ShenandoahAsserts::_safe_unknown, obj, _heap->get_region(idx)->is_humongous_continuation(),
 169                  "Humongous object is in continuation that fits it");
 170         }
 171       }
 172 
 173       // ------------ obj is safe at this point --------------
 174 
 175       check(ShenandoahAsserts::_safe_oop, obj, obj_reg->is_active(),
 176             "Object should be in active region");
 177 
 178       switch (_options._verify_liveness) {
 179         case ShenandoahVerifier::_verify_liveness_disable:
 180           // skip
 181           break;
 182         case ShenandoahVerifier::_verify_liveness_complete:
 183           Atomic::add(&_ld[obj_reg->index()], (uint) obj->size(), memory_order_relaxed);
 184           // fallthrough for fast failure for un-live regions:
 185         case ShenandoahVerifier::_verify_liveness_conservative:
 186           check(ShenandoahAsserts::_safe_oop, obj, obj_reg->has_live() ||
 187                 (obj_reg->is_old() && ShenandoahHeap::heap()->is_gc_generation_young()),
 188                    "Object must belong to region with live data");
 189           break;
 190         default:
 191           assert(false, "Unhandled liveness verification");
 192       }
 193     }
 194 
 195     oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj);
 196 
 197     ShenandoahHeapRegion* fwd_reg = NULL;
 198 
 199     if (obj != fwd) {
 200       check(ShenandoahAsserts::_safe_oop, obj, _heap->is_in(fwd),
 201              "Forwardee must be in heap");
 202       check(ShenandoahAsserts::_safe_oop, obj, !CompressedOops::is_null(fwd),
 203              "Forwardee is set");
 204       check(ShenandoahAsserts::_safe_oop, obj, is_object_aligned(fwd),
 205              "Forwardee must be aligned");
 206 
 207       // Do this before touching fwd->size()
 208       Klass* fwd_klass = fwd->klass_or_null();
 209       check(ShenandoahAsserts::_safe_oop, obj, fwd_klass != NULL,
 210              "Forwardee klass pointer should not be NULL");
 211       check(ShenandoahAsserts::_safe_oop, obj, Metaspace::contains(fwd_klass),
 212              "Forwardee klass pointer must go to metaspace");
 213       check(ShenandoahAsserts::_safe_oop, obj, obj_klass == fwd_klass,
 214              "Forwardee klass pointer must go to metaspace");
 215 
 216       fwd_reg = _heap->heap_region_containing(fwd);
 217 
 218       // Verify that forwardee is not in the dead space:
 219       check(ShenandoahAsserts::_safe_oop, obj, !fwd_reg->is_humongous(),
 220              "Should have no humongous forwardees");
 221 
 222       HeapWord* fwd_addr = cast_from_oop<HeapWord* >(fwd);
 223       check(ShenandoahAsserts::_safe_oop, obj, fwd_addr < fwd_reg->top(),
 224              "Forwardee start should be within the region");
 225       check(ShenandoahAsserts::_safe_oop, obj, (fwd_addr + fwd->size()) <= fwd_reg->top(),
 226              "Forwardee end should be within the region");
 227 
 228       oop fwd2 = ShenandoahForwarding::get_forwardee_raw_unchecked(fwd);
 229       check(ShenandoahAsserts::_safe_oop, obj, (fwd == fwd2),
 230              "Double forwarding");
 231     } else {
 232       fwd_reg = obj_reg;
 233     }
 234 
 235     // ------------ obj and fwd are safe at this point --------------
 236     // We allow for marked or old here for two reasons:
 237     //  1. If this is a young collect, old objects wouldn't be marked. We've
 238     //     recently change the verifier traversal to only follow young objects
 239     //     during a young collect so this _shouldn't_ be necessary.
 240     //  2. At present, we do not clear dead objects from the remembered set.
 241     //     Everything in the remembered set is old (ipso facto), so allowing for
 242     //     'marked_or_old' covers the case of stale objects in rset.
 243     // TODO: Just use 'is_marked' here.
 244     switch (_options._verify_marked) {
 245       case ShenandoahVerifier::_verify_marked_disable:
 246         // skip
 247         break;
 248       case ShenandoahVerifier::_verify_marked_incomplete:
 249         check(ShenandoahAsserts::_safe_all, obj, _heap->marking_context()->is_marked_or_old(obj),
 250                "Must be marked in incomplete bitmap");
 251         break;
 252       case ShenandoahVerifier::_verify_marked_complete:
 253         check(ShenandoahAsserts::_safe_all, obj, _heap->complete_marking_context()->is_marked_or_old(obj),
 254                "Must be marked in complete bitmap");
 255         break;
 256       case ShenandoahVerifier::_verify_marked_complete_except_references:
 257         check(ShenandoahAsserts::_safe_all, obj, _heap->complete_marking_context()->is_marked_or_old(obj),
 258               "Must be marked in complete bitmap, except j.l.r.Reference referents");
 259         break;
 260       default:
 261         assert(false, "Unhandled mark verification");
 262     }
 263 
 264     switch (_options._verify_forwarded) {
 265       case ShenandoahVerifier::_verify_forwarded_disable:
 266         // skip
 267         break;
 268       case ShenandoahVerifier::_verify_forwarded_none: {
 269         check(ShenandoahAsserts::_safe_all, obj, (obj == fwd),
 270                "Should not be forwarded");
 271         break;
 272       }
 273       case ShenandoahVerifier::_verify_forwarded_allow: {
 274         if (obj != fwd) {
 275           check(ShenandoahAsserts::_safe_all, obj, obj_reg != fwd_reg,
 276                  "Forwardee should be in another region");
 277         }

 331    * @param obj host object for verified fields
 332    */
 333   void verify_oops_from(oop obj) {
 334     _loc = obj;
 335     obj->oop_iterate(this);
 336     _loc = NULL;
 337   }
 338 
 339   virtual void do_oop(oop* p) { do_oop_work(p); }
 340   virtual void do_oop(narrowOop* p) { do_oop_work(p); }
 341 };
 342 
 343 class ShenandoahCalculateRegionStatsClosure : public ShenandoahHeapRegionClosure {
 344 private:
 345   size_t _used, _committed, _garbage;
 346 public:
 347   ShenandoahCalculateRegionStatsClosure() : _used(0), _committed(0), _garbage(0) {};
 348 
 349   void heap_region_do(ShenandoahHeapRegion* r) {
 350     _used += r->used();
 351     log_debug(gc)("ShenandoahCalculatRegionStatsClosure added " SIZE_FORMAT " for %s Region " SIZE_FORMAT ", yielding: " SIZE_FORMAT,
 352                   r->used(), r->is_humongous()? "humongous": "regular", r->index(), _used);
 353     _garbage += r->garbage();
 354     _committed += r->is_committed() ? ShenandoahHeapRegion::region_size_bytes() : 0;
 355   }
 356 
 357   size_t used() { return _used; }
 358   size_t committed() { return _committed; }
 359   size_t garbage() { return _garbage; }
 360 };
 361 
 362 class ShenandoahGenerationStatsClosure : public ShenandoahHeapRegionClosure {
 363  public:
 364   ShenandoahCalculateRegionStatsClosure old;
 365   ShenandoahCalculateRegionStatsClosure young;
 366   ShenandoahCalculateRegionStatsClosure global;
 367 
 368   void heap_region_do(ShenandoahHeapRegion* r) override {
 369     switch (r->affiliation()) {
 370       default:
 371         ShouldNotReachHere();
 372         return;
 373       case FREE: return;
 374       case YOUNG_GENERATION:
 375         young.heap_region_do(r);
 376         break;
 377       case OLD_GENERATION:
 378         old.heap_region_do(r);
 379         break;
 380     }
 381     global.heap_region_do(r);
 382   }
 383 
 384   static void log_usage(ShenandoahGeneration* generation, ShenandoahCalculateRegionStatsClosure& stats) {
 385     log_debug(gc)("Safepoint verification: %s verified usage: " SIZE_FORMAT "%s, recorded usage: " SIZE_FORMAT "%s",
 386                   generation->name(),
 387                   byte_size_in_proper_unit(generation->used()), proper_unit_for_byte_size(generation->used()),
 388                   byte_size_in_proper_unit(stats.used()), proper_unit_for_byte_size(stats.used()));
 389   }
 390 
 391   static void validate_usage(const char* label, ShenandoahGeneration* generation, ShenandoahCalculateRegionStatsClosure& stats) {
 392     size_t generation_used = generation->used();
 393     guarantee(stats.used() == generation_used,
 394               "%s: generation (%s) used size must be consistent: generation-used = " SIZE_FORMAT "%s, regions-used = " SIZE_FORMAT "%s",
 395               label, generation->name(),
 396               byte_size_in_proper_unit(generation_used), proper_unit_for_byte_size(generation_used),
 397               byte_size_in_proper_unit(stats.used()), proper_unit_for_byte_size(stats.used()));
 398   }
 399 };
 400 
 401 class ShenandoahVerifyHeapRegionClosure : public ShenandoahHeapRegionClosure {
 402 private:
 403   ShenandoahHeap* _heap;
 404   const char* _phase;
 405   ShenandoahVerifier::VerifyRegions _regions;
 406 public:
 407   ShenandoahVerifyHeapRegionClosure(const char* phase, ShenandoahVerifier::VerifyRegions regions) :
 408     _heap(ShenandoahHeap::heap()),
 409     _phase(phase),
 410     _regions(regions) {};
 411 
 412   void print_failure(ShenandoahHeapRegion* r, const char* label) {
 413     ResourceMark rm;
 414 
 415     ShenandoahMessageBuffer msg("Shenandoah verification failed; %s: %s\n\n", _phase, label);
 416 
 417     stringStream ss;
 418     r->print_on(&ss);
 419     msg.append("%s", ss.as_string());
 420 

 462            "Complete TAMS should not be larger than top");
 463 
 464     verify(r, r->get_live_data_bytes() <= r->capacity(),
 465            "Live data cannot be larger than capacity");
 466 
 467     verify(r, r->garbage() <= r->capacity(),
 468            "Garbage cannot be larger than capacity");
 469 
 470     verify(r, r->used() <= r->capacity(),
 471            "Used cannot be larger than capacity");
 472 
 473     verify(r, r->get_shared_allocs() <= r->capacity(),
 474            "Shared alloc count should not be larger than capacity");
 475 
 476     verify(r, r->get_tlab_allocs() <= r->capacity(),
 477            "TLAB alloc count should not be larger than capacity");
 478 
 479     verify(r, r->get_gclab_allocs() <= r->capacity(),
 480            "GCLAB alloc count should not be larger than capacity");
 481 
 482     verify(r, r->get_plab_allocs() <= r->capacity(),
 483            "PLAB alloc count should not be larger than capacity");
 484 
 485     verify(r, r->get_shared_allocs() + r->get_tlab_allocs() + r->get_gclab_allocs() + r->get_plab_allocs() == r->used(),
 486            "Accurate accounting: shared + TLAB + GCLAB + PLAB = used");
 487 
 488     verify(r, !r->is_empty() || !r->has_live(),
 489            "Empty regions should not have live data");
 490 
 491     verify(r, r->is_cset() == _heap->collection_set()->is_in(r),
 492            "Transitional: region flags and collection set agree");
 493   }
 494 };
 495 
 496 class ShenandoahVerifierReachableTask : public WorkerTask {
 497 private:
 498   const char* _label;
 499   ShenandoahVerifier::VerifyOptions _options;
 500   ShenandoahHeap* _heap;
 501   ShenandoahLivenessData* _ld;
 502   MarkBitMap* _bitmap;
 503   volatile size_t _processed;
 504 
 505 public:
 506   ShenandoahVerifierReachableTask(MarkBitMap* bitmap,

 543 
 544     if (ShenandoahVerifyLevel >= 3) {
 545       ShenandoahVerifyOopClosure cl(&stack, _bitmap, _ld,
 546                                     ShenandoahMessageBuffer("%s, Reachable", _label),
 547                                     _options);
 548       while (!stack.is_empty()) {
 549         processed++;
 550         ShenandoahVerifierTask task = stack.pop();
 551         cl.verify_oops_from(task.obj());
 552       }
 553     }
 554 
 555     Atomic::add(&_processed, processed, memory_order_relaxed);
 556   }
 557 };
 558 
 559 class ShenandoahVerifierMarkedRegionTask : public WorkerTask {
 560 private:
 561   const char* _label;
 562   ShenandoahVerifier::VerifyOptions _options;
 563   ShenandoahHeap* _heap;
 564   MarkBitMap* _bitmap;
 565   ShenandoahLivenessData* _ld;
 566   volatile size_t _claimed;
 567   volatile size_t _processed;
 568   ShenandoahGeneration* _generation;
 569 
 570 public:
 571   ShenandoahVerifierMarkedRegionTask(MarkBitMap* bitmap,
 572                                      ShenandoahLivenessData* ld,
 573                                      const char* label,
 574                                      ShenandoahVerifier::VerifyOptions options) :
 575           WorkerTask("Shenandoah Verifier Marked Objects"),
 576           _label(label),
 577           _options(options),
 578           _heap(ShenandoahHeap::heap()),
 579           _bitmap(bitmap),
 580           _ld(ld),
 581           _claimed(0),
 582           _processed(0),
 583           _generation(NULL) {
 584     if (_heap->mode()->is_generational()) {
 585       _generation = _heap->active_generation();
 586       assert(_generation != NULL, "Expected active generation in this mode.");
 587     }
 588   };
 589 
 590   size_t processed() {
 591     return Atomic::load(&_processed);
 592   }
 593 
 594   virtual void work(uint worker_id) {
 595     ShenandoahVerifierStack stack;
 596     ShenandoahVerifyOopClosure cl(&stack, _bitmap, _ld,
 597                                   ShenandoahMessageBuffer("%s, Marked", _label),
 598                                   _options);
 599 
 600     while (true) {
 601       size_t v = Atomic::fetch_and_add(&_claimed, 1u, memory_order_relaxed);
 602       if (v < _heap->num_regions()) {
 603         ShenandoahHeapRegion* r = _heap->get_region(v);
 604         if (!in_generation(r)) {
 605           continue;
 606         }
 607 
 608         if (!r->is_humongous() && !r->is_trash()) {
 609           work_regular(r, stack, cl);
 610         } else if (r->is_humongous_start()) {
 611           work_humongous(r, stack, cl);
 612         }
 613       } else {
 614         break;
 615       }
 616     }
 617   }
 618 
 619   bool in_generation(ShenandoahHeapRegion* r) {
 620     return _generation == NULL || _generation->contains(r);
 621   }
 622 
 623   virtual void work_humongous(ShenandoahHeapRegion* r, ShenandoahVerifierStack& stack, ShenandoahVerifyOopClosure& cl) {
 624     size_t processed = 0;
 625     HeapWord* obj = r->bottom();
 626     if (_heap->complete_marking_context()->is_marked(cast_to_oop(obj))) {
 627       verify_and_follow(obj, stack, cl, &processed);
 628     }
 629     Atomic::add(&_processed, processed, memory_order_relaxed);
 630   }
 631 
 632   virtual void work_regular(ShenandoahHeapRegion* r, ShenandoahVerifierStack &stack, ShenandoahVerifyOopClosure &cl) {
 633     size_t processed = 0;
 634     ShenandoahMarkingContext* ctx = _heap->complete_marking_context();
 635     HeapWord* tams = ctx->top_at_mark_start(r);
 636 
 637     // Bitmaps, before TAMS
 638     if (tams > r->bottom()) {
 639       HeapWord* start = r->bottom();
 640       HeapWord* addr = ctx->get_next_marked_addr(start, tams);
 641 
 642       while (addr < tams) {
 643         verify_and_follow(addr, stack, cl, &processed);
 644         addr += 1;
 645         if (addr < tams) {
 646           addr = ctx->get_next_marked_addr(addr, tams);
 647         }
 648       }
 649     }
 650 
 651     // Size-based, after TAMS
 652     {
 653       HeapWord* limit = r->top();
 654       HeapWord* addr = tams;
 655 
 656       while (addr < limit) {
 657         verify_and_follow(addr, stack, cl, &processed);
 658         addr += cast_to_oop(addr)->size();
 659       }
 660     }
 661 
 662     Atomic::add(&_processed, processed, memory_order_relaxed);
 663   }
 664 
 665   void verify_and_follow(HeapWord* addr, ShenandoahVerifierStack &stack, ShenandoahVerifyOopClosure &cl, size_t* processed) {
 666     if (!_bitmap->par_mark(addr)) return;
 667 
 668     // Verify the object itself:
 669     oop obj = cast_to_oop(addr);
 670     cl.verify_oop_standalone(obj);
 671 
 672     // Verify everything reachable from that object too, hopefully realizing
 673     // everything was already marked, and never touching further:
 674     if (!is_instance_ref_klass(obj->klass())) {
 675       cl.verify_oops_from(obj);
 676       (*processed)++;
 677     }
 678     while (!stack.is_empty()) {
 679       ShenandoahVerifierTask task = stack.pop();
 680       cl.verify_oops_from(task.obj());
 681       (*processed)++;
 682     }
 683   }
 684 };
 685 
 686 class VerifyThreadGCState : public ThreadClosure {
 687 private:
 688   const char* const _label;
 689          char const _expected;
 690 
 691 public:
 692   VerifyThreadGCState(const char* label, char expected) : _label(label), _expected(expected) {}
 693   void do_thread(Thread* t) {
 694     char actual = ShenandoahThreadLocalData::gc_state(t);
 695     if (actual != _expected && !(actual & ShenandoahHeap::OLD_MARKING)) {
 696       fatal("%s: Thread %s: expected gc-state %d, actual %d", _label, t->name(), _expected, actual);
 697     }
 698   }
 699 };
 700 
 701 void ShenandoahVerifier::verify_at_safepoint(const char* label,
 702                                              VerifyRememberedSet remembered,
 703                                              VerifyForwarded forwarded, VerifyMarked marked,
 704                                              VerifyCollectionSet cset,
 705                                              VerifyLiveness liveness, VerifyRegions regions,
 706                                              VerifyGCState gcstate) {
 707   guarantee(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "only when nothing else happens");
 708   guarantee(ShenandoahVerify, "only when enabled, and bitmap is initialized in ShenandoahHeap::initialize");
 709 
 710   // Avoid side-effect of changing workers' active thread count, but bypass concurrent/parallel protocol check
 711   ShenandoahPushWorkerScope verify_worker_scope(_heap->workers(), _heap->max_workers(), false /*bypass check*/);
 712 
 713   log_info(gc,start)("Verify %s, Level " INTX_FORMAT, label, ShenandoahVerifyLevel);
 714 
 715   // GC state checks
 716   {
 717     char expected = -1;
 718     bool enabled;
 719     switch (gcstate) {
 720       case _verify_gcstate_disable:
 721         enabled = false;
 722         break;
 723       case _verify_gcstate_forwarded:
 724         enabled = true;
 725         expected = ShenandoahHeap::HAS_FORWARDED;
 726         break;
 727       case _verify_gcstate_evacuation:
 728         enabled = true;
 729         expected = ShenandoahHeap::HAS_FORWARDED | ShenandoahHeap::EVACUATION;
 730         if (!_heap->is_stw_gc_in_progress()) {
 731           // Only concurrent GC sets this.
 732           expected |= ShenandoahHeap::WEAK_ROOTS;
 733         }
 734         break;
 735       case _verify_gcstate_updating:
 736         enabled = true;
 737         expected = ShenandoahHeap::HAS_FORWARDED | ShenandoahHeap::UPDATEREFS;
 738         break;
 739       case _verify_gcstate_stable:
 740         enabled = true;
 741         expected = ShenandoahHeap::STABLE;
 742         break;
 743       case _verify_gcstate_stable_weakroots:
 744         enabled = true;
 745         expected = ShenandoahHeap::STABLE;
 746         if (!_heap->is_stw_gc_in_progress()) {
 747           // Only concurrent GC sets this.
 748           expected |= ShenandoahHeap::WEAK_ROOTS;
 749         }
 750         break;
 751       default:
 752         enabled = false;
 753         assert(false, "Unhandled gc-state verification");
 754     }
 755 
 756     if (enabled) {
 757       char actual = _heap->gc_state();
 758       // Old generation marking is allowed in all states.
 759       if (actual != expected && !(actual & ShenandoahHeap::OLD_MARKING)) {
 760         fatal("%s: Global gc-state: expected %d, actual %d", label, expected, actual);
 761       }
 762 
 763       VerifyThreadGCState vtgcs(label, expected);
 764       Threads::java_threads_do(&vtgcs);
 765     }
 766   }
 767 
 768   // Deactivate barriers temporarily: Verifier wants plain heap accesses
 769   ShenandoahGCStateResetter resetter;
 770 
 771   // Heap size checks
 772   {
 773     ShenandoahHeapLocker lock(_heap->lock());
 774 
 775     ShenandoahCalculateRegionStatsClosure cl;
 776     _heap->heap_region_iterate(&cl);
 777 
 778     size_t heap_used = _heap->used();
 779 
 780     guarantee(cl.used() == heap_used,
 781               "%s: heap used size must be consistent: heap-used = " SIZE_FORMAT "%s, regions-used = " SIZE_FORMAT "%s",
 782               label,
 783               byte_size_in_proper_unit(heap_used), proper_unit_for_byte_size(heap_used),
 784               byte_size_in_proper_unit(cl.used()), proper_unit_for_byte_size(cl.used()));
 785 
 786     size_t heap_committed = _heap->committed();
 787     guarantee(cl.committed() == heap_committed,
 788               "%s: heap committed size must be consistent: heap-committed = " SIZE_FORMAT "%s, regions-committed = " SIZE_FORMAT "%s",
 789               label,
 790               byte_size_in_proper_unit(heap_committed), proper_unit_for_byte_size(heap_committed),
 791               byte_size_in_proper_unit(cl.committed()), proper_unit_for_byte_size(cl.committed()));
 792   }
 793 
 794   log_debug(gc)("Safepoint verification finished heap usage verification");
 795 
 796   ShenandoahGeneration* generation;
 797   if (_heap->mode()->is_generational()) {
 798     generation = _heap->active_generation();
 799     guarantee(generation != NULL, "Need to know which generation to verify.");
 800   } else {
 801     generation = NULL;
 802   }
 803 
 804   if (generation != NULL) {
 805     ShenandoahHeapLocker lock(_heap->lock());
 806 
 807     if (remembered == _verify_remembered_for_marking) {
 808       log_debug(gc)("Safepoint verification of remembered set at mark");
 809     } else if (remembered == _verify_remembered_for_updating_references) {
 810       log_debug(gc)("Safepoint verification of remembered set at update ref");
 811     } else if (remembered == _verify_remembered_after_full_gc) {
 812       log_debug(gc)("Safepoint verification of remembered set after full gc");
 813     }
 814 
 815     if (remembered == _verify_remembered_for_marking) {
 816       _heap->verify_rem_set_at_mark();
 817     } else if (remembered == _verify_remembered_for_updating_references) {
 818       _heap->verify_rem_set_at_update_ref();
 819     } else if (remembered == _verify_remembered_after_full_gc) {
 820       _heap->verify_rem_set_after_full_gc();
 821     }
 822 
 823     ShenandoahGenerationStatsClosure cl;
 824     _heap->heap_region_iterate(&cl);
 825 
 826     if (LogTarget(Debug, gc)::is_enabled()) {
 827       ShenandoahGenerationStatsClosure::log_usage(_heap->old_generation(), cl.old);
 828       ShenandoahGenerationStatsClosure::log_usage(_heap->young_generation(), cl.young);
 829       ShenandoahGenerationStatsClosure::log_usage(_heap->global_generation(), cl.global);
 830     }
 831 
 832     ShenandoahGenerationStatsClosure::validate_usage(label, _heap->old_generation(), cl.old);
 833     ShenandoahGenerationStatsClosure::validate_usage(label, _heap->young_generation(), cl.young);
 834     ShenandoahGenerationStatsClosure::validate_usage(label, _heap->global_generation(), cl.global);
 835   }
 836 
 837   log_debug(gc)("Safepoint verification finished remembered set verification");
 838 
 839   // Internal heap region checks
 840   if (ShenandoahVerifyLevel >= 1) {
 841     ShenandoahVerifyHeapRegionClosure cl(label, regions);
 842     if (generation != NULL) {
 843       generation->heap_region_iterate(&cl);
 844     } else {
 845       _heap->heap_region_iterate(&cl);
 846     }
 847   }
 848 
 849   log_debug(gc)("Safepoint verification finished heap region closure verification");
 850 
 851   OrderAccess::fence();
 852 
 853   if (UseTLAB) {
 854     _heap->labs_make_parsable();
 855   }
 856 
 857   // Allocate temporary bitmap for storing marking wavefront:
 858   _verification_bit_map->clear();
 859 
 860   // Allocate temporary array for storing liveness data
 861   ShenandoahLivenessData* ld = NEW_C_HEAP_ARRAY(ShenandoahLivenessData, _heap->num_regions(), mtGC);
 862   Copy::fill_to_bytes((void*)ld, _heap->num_regions()*sizeof(ShenandoahLivenessData), 0);
 863 
 864   const VerifyOptions& options = ShenandoahVerifier::VerifyOptions(forwarded, marked, cset, liveness, regions, gcstate);
 865 
 866   // Steps 1-2. Scan root set to get initial reachable set. Finish walking the reachable heap.
 867   // This verifies what application can see, since it only cares about reachable objects.
 868   size_t count_reachable = 0;
 869   if (ShenandoahVerifyLevel >= 2) {
 870     ShenandoahVerifierReachableTask task(_verification_bit_map, ld, label, options);
 871     _heap->workers()->run_task(&task);
 872     count_reachable = task.processed();
 873   }
 874 
 875   log_debug(gc)("Safepoint verification finished getting initial reachable set");
 876 
 877   // Step 3. Walk marked objects. Marked objects might be unreachable. This verifies what collector,
 878   // not the application, can see during the region scans. There is no reason to process the objects
 879   // that were already verified, e.g. those marked in verification bitmap. There is interaction with TAMS:
 880   // before TAMS, we verify the bitmaps, if available; after TAMS, we walk until the top(). It mimics
 881   // what marked_object_iterate is doing, without calling into that optimized (and possibly incorrect)
 882   // version
 883 
 884   size_t count_marked = 0;
 885   if (ShenandoahVerifyLevel >= 4 && (marked == _verify_marked_complete || marked == _verify_marked_complete_except_references)) {
 886     guarantee(_heap->marking_context()->is_complete(), "Marking context should be complete");
 887     ShenandoahVerifierMarkedRegionTask task(_verification_bit_map, ld, label, options);
 888     _heap->workers()->run_task(&task);
 889     count_marked = task.processed();
 890   } else {
 891     guarantee(ShenandoahVerifyLevel < 4 || marked == _verify_marked_incomplete || marked == _verify_marked_disable, "Should be");
 892   }
 893 
 894   log_debug(gc)("Safepoint verification finished walking marked objects");
 895 
 896   // Step 4. Verify accumulated liveness data, if needed. Only reliable if verification level includes
 897   // marked objects.
 898 
 899   if (ShenandoahVerifyLevel >= 4 && marked == _verify_marked_complete && liveness == _verify_liveness_complete) {
 900     for (size_t i = 0; i < _heap->num_regions(); i++) {
 901       ShenandoahHeapRegion* r = _heap->get_region(i);
 902       if (generation != NULL && !generation->contains(r)) {
 903         continue;
 904       }
 905 
 906       juint verf_live = 0;
 907       if (r->is_humongous()) {
 908         // For humongous objects, test if start region is marked live, and if so,
 909         // all humongous regions in that chain have live data equal to their "used".
 910         juint start_live = Atomic::load(&ld[r->humongous_start_region()->index()]);
 911         if (start_live > 0) {
 912           verf_live = (juint)(r->used() / HeapWordSize);
 913         }
 914       } else {
 915         verf_live = Atomic::load(&ld[r->index()]);
 916       }
 917 
 918       size_t reg_live = r->get_live_data_words();
 919       if (reg_live != verf_live) {
 920         ResourceMark rm;
 921         stringStream ss;
 922         r->print_on(&ss);
 923         fatal("%s: Live data should match: region-live = " SIZE_FORMAT ", verifier-live = " UINT32_FORMAT "\n%s",
 924               label, reg_live, verf_live, ss.as_string());
 925       }
 926     }
 927   }
 928 
 929   log_debug(gc)("Safepoint verification finished accumulation of liveness data");
 930 
 931 
 932   log_info(gc)("Verify %s, Level " INTX_FORMAT " (" SIZE_FORMAT " reachable, " SIZE_FORMAT " marked)",
 933                label, ShenandoahVerifyLevel, count_reachable, count_marked);
 934 
 935   FREE_C_HEAP_ARRAY(ShenandoahLivenessData, ld);
 936 }
 937 
 938 void ShenandoahVerifier::verify_generic(VerifyOption vo) {
 939   verify_at_safepoint(
 940           "Generic Verification",
 941           _verify_remembered_disable,  // do not verify remembered set
 942           _verify_forwarded_allow,     // conservatively allow forwarded
 943           _verify_marked_disable,      // do not verify marked: lots ot time wasted checking dead allocations
 944           _verify_cset_disable,        // cset may be inconsistent
 945           _verify_liveness_disable,    // no reliable liveness data
 946           _verify_regions_disable,     // no reliable region data
 947           _verify_gcstate_disable      // no data about gcstate
 948   );
 949 }
 950 
 951 void ShenandoahVerifier::verify_before_concmark() {
 952     verify_at_safepoint(
 953           "Before Mark",
 954           _verify_remembered_for_marking,  // verify read-only remembered set from bottom() to top()
 955           _verify_forwarded_none,      // UR should have fixed up
 956           _verify_marked_disable,      // do not verify marked: lots ot time wasted checking dead allocations
 957           _verify_cset_none,           // UR should have fixed this
 958           _verify_liveness_disable,    // no reliable liveness data
 959           _verify_regions_notrash,     // no trash regions
 960           _verify_gcstate_stable       // there are no forwarded objects
 961   );
 962 }
 963 
 964 void ShenandoahVerifier::verify_after_concmark() {
 965   verify_at_safepoint(
 966           "After Mark",
 967           _verify_remembered_disable,  // do not verify remembered set
 968           _verify_forwarded_none,      // no forwarded references
 969           _verify_marked_complete_except_references, // bitmaps as precise as we can get, except dangling j.l.r.Refs
 970           _verify_cset_none,           // no references to cset anymore
 971           _verify_liveness_complete,   // liveness data must be complete here
 972           _verify_regions_disable,     // trash regions not yet recycled
 973           _verify_gcstate_stable_weakroots  // heap is still stable, weakroots are in progress
 974   );
 975 }
 976 
 977 void ShenandoahVerifier::verify_before_evacuation() {
 978   verify_at_safepoint(
 979           "Before Evacuation",
 980           _verify_remembered_disable,                // do not verify remembered set
 981           _verify_forwarded_none,                    // no forwarded references
 982           _verify_marked_complete_except_references, // walk over marked objects too
 983           _verify_cset_disable,                      // non-forwarded references to cset expected
 984           _verify_liveness_complete,                 // liveness data must be complete here
 985           _verify_regions_disable,                   // trash regions not yet recycled
 986           _verify_gcstate_stable_weakroots           // heap is still stable, weakroots are in progress
 987   );
 988 }
 989 
 990 void ShenandoahVerifier::verify_during_evacuation() {
 991   verify_at_safepoint(
 992           "During Evacuation",
 993           _verify_remembered_disable, // do not verify remembered set
 994           _verify_forwarded_allow,    // some forwarded references are allowed
 995           _verify_marked_disable,     // walk only roots
 996           _verify_cset_disable,       // some cset references are not forwarded yet
 997           _verify_liveness_disable,   // liveness data might be already stale after pre-evacs
 998           _verify_regions_disable,    // trash regions not yet recycled
 999           _verify_gcstate_evacuation  // evacuation is in progress
1000   );
1001 }
1002 
1003 void ShenandoahVerifier::verify_after_evacuation() {
1004   verify_at_safepoint(
1005           "After Evacuation",
1006           _verify_remembered_disable,  // do not verify remembered set
1007           _verify_forwarded_allow,     // objects are still forwarded
1008           _verify_marked_complete,     // bitmaps might be stale, but alloc-after-mark should be well
1009           _verify_cset_forwarded,      // all cset refs are fully forwarded
1010           _verify_liveness_disable,    // no reliable liveness data anymore
1011           _verify_regions_notrash,     // trash regions have been recycled already
1012           _verify_gcstate_forwarded    // evacuation produced some forwarded objects
1013   );
1014 }
1015 
1016 void ShenandoahVerifier::verify_before_updaterefs() {
1017   verify_at_safepoint(
1018           "Before Updating References",
1019           _verify_remembered_for_updating_references,  // verify read-write remembered set
1020           _verify_forwarded_allow,                     // forwarded references allowed
1021           _verify_marked_complete,                     // bitmaps might be stale, but alloc-after-mark should be well
1022           _verify_cset_forwarded,                      // all cset refs are fully forwarded
1023           _verify_liveness_disable,                    // no reliable liveness data anymore
1024           _verify_regions_notrash,                     // trash regions have been recycled already
1025           _verify_gcstate_updating                     // evacuation should have produced some forwarded objects
1026   );
1027 }
1028 
1029 void ShenandoahVerifier::verify_after_updaterefs() {
1030   verify_at_safepoint(
1031           "After Updating References",
1032           _verify_remembered_disable,  // do not verify remembered set
1033           _verify_forwarded_none,      // no forwarded references
1034           _verify_marked_complete,     // bitmaps might be stale, but alloc-after-mark should be well
1035           _verify_cset_none,           // no cset references, all updated
1036           _verify_liveness_disable,    // no reliable liveness data anymore
1037           _verify_regions_nocset,      // no cset regions, trash regions have appeared
1038           _verify_gcstate_stable       // update refs had cleaned up forwarded objects
1039   );
1040 }
1041 
1042 void ShenandoahVerifier::verify_after_degenerated() {
1043   verify_at_safepoint(
1044           "After Degenerated GC",
1045           _verify_remembered_disable,  // do not verify remembered set
1046           _verify_forwarded_none,      // all objects are non-forwarded
1047           _verify_marked_complete,     // all objects are marked in complete bitmap
1048           _verify_cset_none,           // no cset references
1049           _verify_liveness_disable,    // no reliable liveness data anymore
1050           _verify_regions_notrash_nocset, // no trash, no cset
1051           _verify_gcstate_stable       // degenerated refs had cleaned up forwarded objects
1052   );
1053 }
1054 
1055 void ShenandoahVerifier::verify_before_fullgc() {
1056   verify_at_safepoint(
1057           "Before Full GC",
1058           _verify_remembered_disable,  // do not verify remembered set
1059           _verify_forwarded_allow,     // can have forwarded objects
1060           _verify_marked_disable,      // do not verify marked: lots ot time wasted checking dead allocations
1061           _verify_cset_disable,        // cset might be foobared
1062           _verify_liveness_disable,    // no reliable liveness data anymore
1063           _verify_regions_disable,     // no reliable region data here
1064           _verify_gcstate_disable      // no reliable gcstate data
1065   );
1066 }
1067 
1068 void ShenandoahVerifier::verify_after_generational_fullgc() {
1069   verify_at_safepoint(
1070           "After Full Generational GC",
1071           _verify_remembered_after_full_gc,  // verify read-write remembered set
1072           _verify_forwarded_none,      // all objects are non-forwarded
1073           _verify_marked_complete,     // all objects are marked in complete bitmap
1074           _verify_cset_none,           // no cset references
1075           _verify_liveness_disable,    // no reliable liveness data anymore
1076           _verify_regions_notrash_nocset, // no trash, no cset
1077           _verify_gcstate_stable       // full gc cleaned up everything
1078   );
1079 }
1080 
1081 void ShenandoahVerifier::verify_after_fullgc() {
1082   verify_at_safepoint(
1083           "After Full GC",
1084           _verify_remembered_disable,  // do not verify remembered set
1085           _verify_forwarded_none,      // all objects are non-forwarded
1086           _verify_marked_complete,     // all objects are marked in complete bitmap
1087           _verify_cset_none,           // no cset references
1088           _verify_liveness_disable,    // no reliable liveness data anymore
1089           _verify_regions_notrash_nocset, // no trash, no cset
1090           _verify_gcstate_stable        // full gc cleaned up everything
1091   );
1092 }
1093 
1094 class ShenandoahVerifyNoForwared : public BasicOopIterateClosure {
1095 private:
1096   template <class T>
1097   void do_oop_work(T* p) {
1098     T o = RawAccess<>::oop_load(p);
1099     if (!CompressedOops::is_null(o)) {
1100       oop obj = CompressedOops::decode_not_null(o);
1101       oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj);
1102       if (obj != fwd) {
1103         ShenandoahAsserts::print_failure(ShenandoahAsserts::_safe_all, obj, p, NULL,
1104                                          "Verify Roots", "Should not be forwarded", __FILE__, __LINE__);
1105       }
1106     }
1107   }
1108 
1109 public:
1110   void do_oop(narrowOop* p) { do_oop_work(p); }
1111   void do_oop(oop* p)       { do_oop_work(p); }
1112 };
1113 
1114 class ShenandoahVerifyInToSpaceClosure : public BasicOopIterateClosure {
1115 private:
1116   template <class T>
1117   void do_oop_work(T* p) {
1118     T o = RawAccess<>::oop_load(p);
1119     if (!CompressedOops::is_null(o)) {
1120       oop obj = CompressedOops::decode_not_null(o);
1121       ShenandoahHeap* heap = ShenandoahHeap::heap();
1122 
1123       if (!heap->marking_context()->is_marked_or_old(obj)) {
1124         ShenandoahAsserts::print_failure(ShenandoahAsserts::_safe_all, obj, p, NULL,
1125                 "Verify Roots In To-Space", "Should be marked", __FILE__, __LINE__);
1126       }
1127 
1128       if (heap->in_collection_set(obj)) {
1129         ShenandoahAsserts::print_failure(ShenandoahAsserts::_safe_all, obj, p, NULL,
1130                 "Verify Roots In To-Space", "Should not be in collection set", __FILE__, __LINE__);
1131       }
1132 
1133       oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj);
1134       if (obj != fwd) {
1135         ShenandoahAsserts::print_failure(ShenandoahAsserts::_safe_all, obj, p, NULL,
1136                 "Verify Roots In To-Space", "Should not be forwarded", __FILE__, __LINE__);
1137       }
1138     }
1139   }
1140 
1141 public:
1142   void do_oop(narrowOop* p) { do_oop_work(p); }
1143   void do_oop(oop* p)       { do_oop_work(p); }
< prev index next >