< 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 "utilities/align.hpp"
  43 
  44 // Avoid name collision on verify_oop (defined in macroAssembler_arm.hpp)
  45 #ifdef verify_oop
  46 #undef verify_oop
  47 #endif
  48 
  49 static bool is_instance_ref_klass(Klass* k) {
  50   return k->is_instance_klass() && InstanceKlass::cast(k)->reference_type() != REF_NONE;
  51 }
  52 
  53 class ShenandoahIgnoreReferenceDiscoverer : public ReferenceDiscoverer {
  54 public:
  55   virtual bool discover_reference(oop obj, ReferenceType type) {
  56     return true;
  57   }
  58 };
  59 
  60 class ShenandoahVerifyOopClosure : public BasicOopIterateClosure {
  61 private:
  62   const char* _phase;
  63   ShenandoahVerifier::VerifyOptions _options;
  64   ShenandoahVerifierStack* _stack;
  65   ShenandoahHeap* _heap;
  66   MarkBitMap* _map;
  67   ShenandoahLivenessData* _ld;
  68   void* _interior_loc;
  69   oop _loc;

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

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





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









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

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







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

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


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







































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

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



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

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

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






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




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




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

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




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

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

 684     size_t heap_used = _heap->used();

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













































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




 703   }
 704 


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


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


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



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



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

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

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

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

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

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

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

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

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

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

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













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

 905           _verify_forwarded_none,      // all objects are non-forwarded
 906           _verify_marked_complete,     // all objects are marked in complete bitmap
 907           _verify_cset_none,           // no cset references
 908           _verify_liveness_disable,    // no reliable liveness data anymore
 909           _verify_regions_notrash_nocset, // no trash, no cset
 910           _verify_gcstate_stable        // full gc cleaned up everything
 911   );
 912 }
 913 
 914 class ShenandoahVerifyNoForwared : public OopClosure {
 915 private:
 916   template <class T>
 917   void do_oop_work(T* p) {
 918     T o = RawAccess<>::oop_load(p);
 919     if (!CompressedOops::is_null(o)) {
 920       oop obj = CompressedOops::decode_not_null(o);
 921       oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj);
 922       if (obj != fwd) {
 923         ShenandoahAsserts::print_failure(ShenandoahAsserts::_safe_all, obj, p, NULL,
 924                                          "Verify Roots", "Should not be forwarded", __FILE__, __LINE__);
 925       }
 926     }
 927   }
 928 
 929 public:
 930   void do_oop(narrowOop* p) { do_oop_work(p); }
 931   void do_oop(oop* p)       { do_oop_work(p); }
 932 };
 933 
 934 class ShenandoahVerifyInToSpaceClosure : public OopClosure {
 935 private:
 936   template <class T>
 937   void do_oop_work(T* p) {
 938     T o = RawAccess<>::oop_load(p);
 939     if (!CompressedOops::is_null(o)) {
 940       oop obj = CompressedOops::decode_not_null(o);
 941       ShenandoahHeap* heap = ShenandoahHeap::heap();
 942 
 943       if (!heap->marking_context()->is_marked(obj)) {
 944         ShenandoahAsserts::print_failure(ShenandoahAsserts::_safe_all, obj, p, NULL,
 945                 "Verify Roots In To-Space", "Should be marked", __FILE__, __LINE__);
 946       }
 947 
 948       if (heap->in_collection_set(obj)) {
 949         ShenandoahAsserts::print_failure(ShenandoahAsserts::_safe_all, obj, p, NULL,
 950                 "Verify Roots In To-Space", "Should not be in collection set", __FILE__, __LINE__);
 951       }
 952 
 953       oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj);
 954       if (obj != fwd) {
 955         ShenandoahAsserts::print_failure(ShenandoahAsserts::_safe_all, obj, p, NULL,
 956                 "Verify Roots In To-Space", "Should not be forwarded", __FILE__, __LINE__);
 957       }
 958     }
 959   }
 960 
 961 public:
 962   void do_oop(narrowOop* p) { do_oop_work(p); }
 963   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 "utilities/align.hpp"
  46 
  47 // Avoid name collision on verify_oop (defined in macroAssembler_arm.hpp)
  48 #ifdef verify_oop
  49 #undef verify_oop
  50 #endif
  51 
  52 static bool is_instance_ref_klass(Klass* k) {
  53   return k->is_instance_klass() && InstanceKlass::cast(k)->reference_type() != REF_NONE;
  54 }
  55 
  56 class ShenandoahIgnoreReferenceDiscoverer : public ReferenceDiscoverer {
  57 public:
  58   virtual bool discover_reference(oop obj, ReferenceType type) {
  59     return true;
  60   }
  61 };
  62 
  63 class ShenandoahVerifyOopClosure : public BasicOopIterateClosure {
  64 private:
  65   const char* _phase;
  66   ShenandoahVerifier::VerifyOptions _options;
  67   ShenandoahVerifierStack* _stack;
  68   ShenandoahHeap* _heap;
  69   MarkBitMap* _map;
  70   ShenandoahLivenessData* _ld;
  71   void* _interior_loc;
  72   oop _loc;
  73   ShenandoahGeneration* _generation;
  74 
  75 public:
  76   ShenandoahVerifyOopClosure(ShenandoahVerifierStack* stack, MarkBitMap* map, ShenandoahLivenessData* ld,
  77                              const char* phase, ShenandoahVerifier::VerifyOptions options) :
  78     _phase(phase),
  79     _options(options),
  80     _stack(stack),
  81     _heap(ShenandoahHeap::heap()),
  82     _map(map),
  83     _ld(ld),
  84     _interior_loc(NULL),
  85     _loc(NULL),
  86     _generation(NULL) {
  87     if (options._verify_marked == ShenandoahVerifier::_verify_marked_complete_except_references ||
  88         options._verify_marked == ShenandoahVerifier::_verify_marked_disable) {
  89       set_ref_discoverer_internal(new ShenandoahIgnoreReferenceDiscoverer());
  90     }
  91 
  92     if (_heap->mode()->is_generational()) {
  93       _generation = _heap->active_generation();
  94       assert(_generation != NULL, "Expected active generation in this mode");
  95     }
  96   }
  97 
  98 private:
  99   void check(ShenandoahAsserts::SafeLevel level, oop obj, bool test, const char* label) {
 100     if (!test) {
 101       ShenandoahAsserts::print_failure(level, obj, _interior_loc, _loc, _phase, label, __FILE__, __LINE__);
 102     }
 103   }
 104 
 105   template <class T>
 106   void do_oop_work(T* p) {
 107     T o = RawAccess<>::oop_load(p);
 108     if (!CompressedOops::is_null(o)) {
 109       oop obj = CompressedOops::decode_not_null(o);
 110       if (is_instance_ref_klass(obj->klass())) {
 111         obj = ShenandoahForwarding::get_forwardee(obj);
 112       }
 113       // Single threaded verification can use faster non-atomic stack and bitmap
 114       // methods.
 115       //
 116       // For performance reasons, only fully verify non-marked field values.
 117       // We are here when the host object for *p is already marked.
 118 
 119       if ( in_generation(obj) && _map->par_mark(obj)) {
 120         verify_oop_at(p, obj);
 121         _stack->push(ShenandoahVerifierTask(obj));
 122       }
 123     }
 124   }
 125 
 126   bool in_generation(oop obj) {
 127     if (_generation == NULL) {
 128       return true;
 129     }
 130 
 131     ShenandoahHeapRegion* region = _heap->heap_region_containing(obj);
 132     return _generation->contains(region);
 133   }
 134 
 135   void verify_oop(oop obj) {
 136     // Perform consistency checks with gradually decreasing safety level. This guarantees
 137     // that failure report would not try to touch something that was not yet verified to be
 138     // safe to process.
 139 
 140     check(ShenandoahAsserts::_safe_unknown, obj, _heap->is_in(obj),
 141               "oop must be in heap");
 142     check(ShenandoahAsserts::_safe_unknown, obj, is_object_aligned(obj),
 143               "oop must be aligned");
 144 
 145     ShenandoahHeapRegion* obj_reg = _heap->heap_region_containing(obj);
 146     Klass* obj_klass = obj->klass_or_null();
 147 
 148     // Verify that obj is not in dead space:
 149     {
 150       // Do this before touching obj->size()
 151       check(ShenandoahAsserts::_safe_unknown, obj, obj_klass != NULL,
 152              "Object klass pointer should not be NULL");
 153       check(ShenandoahAsserts::_safe_unknown, obj, Metaspace::contains(obj_klass),
 154              "Object klass pointer must go to metaspace");
 155 
 156       HeapWord* obj_addr = cast_from_oop<HeapWord*>(obj);
 157       check(ShenandoahAsserts::_safe_unknown, obj, obj_addr < obj_reg->top(),
 158              "Object start should be within the region");
 159 
 160       if (!obj_reg->is_humongous()) {
 161         check(ShenandoahAsserts::_safe_unknown, obj, (obj_addr + obj->size()) <= obj_reg->top(),
 162                "Object end should be within the region");
 163       } else {
 164         size_t humongous_start = obj_reg->index();
 165         size_t humongous_end = humongous_start + (obj->size() >> ShenandoahHeapRegion::region_size_words_shift());
 166         for (size_t idx = humongous_start + 1; idx < humongous_end; idx++) {
 167           check(ShenandoahAsserts::_safe_unknown, obj, _heap->get_region(idx)->is_humongous_continuation(),
 168                  "Humongous object is in continuation that fits it");
 169         }
 170       }
 171 
 172       // ------------ obj is safe at this point --------------
 173 
 174       check(ShenandoahAsserts::_safe_oop, obj, obj_reg->is_active(),
 175             "Object should be in active region");
 176 
 177       switch (_options._verify_liveness) {
 178         case ShenandoahVerifier::_verify_liveness_disable:
 179           // skip
 180           break;
 181         case ShenandoahVerifier::_verify_liveness_complete:
 182           Atomic::add(&_ld[obj_reg->index()], (uint) obj->size(), memory_order_relaxed);
 183           // fallthrough for fast failure for un-live regions:
 184         case ShenandoahVerifier::_verify_liveness_conservative:
 185           check(ShenandoahAsserts::_safe_oop, obj, obj_reg->has_live() ||
 186                 (obj_reg->is_old() && ShenandoahHeap::heap()->is_gc_generation_young()),
 187                    "Object must belong to region with live data");
 188           break;
 189         default:
 190           assert(false, "Unhandled liveness verification");
 191       }
 192     }
 193 
 194     oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj);
 195 
 196     ShenandoahHeapRegion* fwd_reg = NULL;
 197 
 198     if (obj != fwd) {
 199       check(ShenandoahAsserts::_safe_oop, obj, _heap->is_in(fwd),
 200              "Forwardee must be in heap");
 201       check(ShenandoahAsserts::_safe_oop, obj, !CompressedOops::is_null(fwd),
 202              "Forwardee is set");
 203       check(ShenandoahAsserts::_safe_oop, obj, is_object_aligned(fwd),
 204              "Forwardee must be aligned");
 205 
 206       // Do this before touching fwd->size()
 207       Klass* fwd_klass = fwd->klass_or_null();
 208       check(ShenandoahAsserts::_safe_oop, obj, fwd_klass != NULL,
 209              "Forwardee klass pointer should not be NULL");
 210       check(ShenandoahAsserts::_safe_oop, obj, Metaspace::contains(fwd_klass),
 211              "Forwardee klass pointer must go to metaspace");
 212       check(ShenandoahAsserts::_safe_oop, obj, obj_klass == fwd_klass,
 213              "Forwardee klass pointer must go to metaspace");
 214 
 215       fwd_reg = _heap->heap_region_containing(fwd);
 216 
 217       // Verify that forwardee is not in the dead space:
 218       check(ShenandoahAsserts::_safe_oop, obj, !fwd_reg->is_humongous(),
 219              "Should have no humongous forwardees");
 220 
 221       HeapWord* fwd_addr = cast_from_oop<HeapWord* >(fwd);
 222       check(ShenandoahAsserts::_safe_oop, obj, fwd_addr < fwd_reg->top(),
 223              "Forwardee start should be within the region");
 224       check(ShenandoahAsserts::_safe_oop, obj, (fwd_addr + fwd->size()) <= fwd_reg->top(),
 225              "Forwardee end should be within the region");
 226 
 227       oop fwd2 = ShenandoahForwarding::get_forwardee_raw_unchecked(fwd);
 228       check(ShenandoahAsserts::_safe_oop, obj, (fwd == fwd2),
 229              "Double forwarding");
 230     } else {
 231       fwd_reg = obj_reg;
 232     }
 233 
 234     // ------------ obj and fwd are safe at this point --------------
 235     // We allow for marked or old here for two reasons:
 236     //  1. If this is a young collect, old objects wouldn't be marked. We've
 237     //     recently change the verifier traversal to only follow young objects
 238     //     during a young collect so this _shouldn't_ be necessary.
 239     //  2. At present, we do not clear dead objects from the remembered set.
 240     //     Everything in the remembered set is old (ipso facto), so allowing for
 241     //     'marked_or_old' covers the case of stale objects in rset.
 242     // TODO: Just use 'is_marked' here.
 243     switch (_options._verify_marked) {
 244       case ShenandoahVerifier::_verify_marked_disable:
 245         // skip
 246         break;
 247       case ShenandoahVerifier::_verify_marked_incomplete:
 248         check(ShenandoahAsserts::_safe_all, obj, _heap->marking_context()->is_marked_or_old(obj),
 249                "Must be marked in incomplete bitmap");
 250         break;
 251       case ShenandoahVerifier::_verify_marked_complete:
 252         check(ShenandoahAsserts::_safe_all, obj, _heap->complete_marking_context()->is_marked_or_old(obj),
 253                "Must be marked in complete bitmap");
 254         break;
 255       case ShenandoahVerifier::_verify_marked_complete_except_references:
 256         check(ShenandoahAsserts::_safe_all, obj, _heap->complete_marking_context()->is_marked_or_old(obj),
 257               "Must be marked in complete bitmap, except j.l.r.Reference referents");
 258         break;
 259       default:
 260         assert(false, "Unhandled mark verification");
 261     }
 262 
 263     switch (_options._verify_forwarded) {
 264       case ShenandoahVerifier::_verify_forwarded_disable:
 265         // skip
 266         break;
 267       case ShenandoahVerifier::_verify_forwarded_none: {
 268         check(ShenandoahAsserts::_safe_all, obj, (obj == fwd),
 269                "Should not be forwarded");
 270         break;
 271       }
 272       case ShenandoahVerifier::_verify_forwarded_allow: {
 273         if (obj != fwd) {
 274           check(ShenandoahAsserts::_safe_all, obj, obj_reg != fwd_reg,
 275                  "Forwardee should be in another region");
 276         }

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

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

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