1 /*
   2  * Copyright (c) 2017, 2021, Red Hat, Inc. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  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         }
 278         break;
 279       }
 280       default:
 281         assert(false, "Unhandled forwarding verification");
 282     }
 283 
 284     switch (_options._verify_cset) {
 285       case ShenandoahVerifier::_verify_cset_disable:
 286         // skip
 287         break;
 288       case ShenandoahVerifier::_verify_cset_none:
 289         check(ShenandoahAsserts::_safe_all, obj, !_heap->in_collection_set(obj),
 290                "Should not have references to collection set");
 291         break;
 292       case ShenandoahVerifier::_verify_cset_forwarded:
 293         if (_heap->in_collection_set(obj)) {
 294           check(ShenandoahAsserts::_safe_all, obj, (obj != fwd),
 295                  "Object in collection set, should have forwardee");
 296         }
 297         break;
 298       default:
 299         assert(false, "Unhandled cset verification");
 300     }
 301 
 302   }
 303 
 304 public:
 305   /**
 306    * Verify object with known interior reference.
 307    * @param p interior reference where the object is referenced from; can be off-heap
 308    * @param obj verified object
 309    */
 310   template <class T>
 311   void verify_oop_at(T* p, oop obj) {
 312     _interior_loc = p;
 313     verify_oop(obj);
 314     _interior_loc = NULL;
 315   }
 316 
 317   /**
 318    * Verify object without known interior reference.
 319    * Useful when picking up the object at known offset in heap,
 320    * but without knowing what objects reference it.
 321    * @param obj verified object
 322    */
 323   void verify_oop_standalone(oop obj) {
 324     _interior_loc = NULL;
 325     verify_oop(obj);
 326     _interior_loc = NULL;
 327   }
 328 
 329   /**
 330    * Verify oop fields from this object.
 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 // This closure computes the amounts of used, committed, and garbage memory and the number of regions contained within
 344 // a subset (e.g. the young generation or old generation) of the total heap.
 345 class ShenandoahCalculateRegionStatsClosure : public ShenandoahHeapRegionClosure {
 346 private:
 347   size_t _used, _committed, _garbage, _regions;
 348 public:
 349   ShenandoahCalculateRegionStatsClosure() : _used(0), _committed(0), _garbage(0), _regions(0) {};
 350 
 351   void heap_region_do(ShenandoahHeapRegion* r) {
 352     _used += r->used();
 353     log_debug(gc)("ShenandoahCalculateRegionStatsClosure added " SIZE_FORMAT " for %s Region " SIZE_FORMAT ", yielding: " SIZE_FORMAT,
 354                   r->used(), r->is_humongous()? "humongous": "regular", r->index(), _used);
 355     _garbage += r->garbage();
 356     _committed += r->is_committed() ? ShenandoahHeapRegion::region_size_bytes() : 0;
 357     _regions++;
 358   }
 359 
 360   size_t used() { return _used; }
 361   size_t committed() { return _committed; }
 362   size_t garbage() { return _garbage; }
 363   size_t regions() { return _regions; }
 364 
 365   // span is the total memory affiliated with these stats (some of which is in use and other is available)
 366   size_t span() { return _regions * ShenandoahHeapRegion::region_size_bytes(); }
 367 };
 368 
 369 class ShenandoahGenerationStatsClosure : public ShenandoahHeapRegionClosure {
 370  public:
 371   ShenandoahCalculateRegionStatsClosure old;
 372   ShenandoahCalculateRegionStatsClosure young;
 373   ShenandoahCalculateRegionStatsClosure global;
 374 
 375   void heap_region_do(ShenandoahHeapRegion* r) override {
 376     switch (r->affiliation()) {
 377       default:
 378         ShouldNotReachHere();
 379         return;
 380       case FREE: return;
 381       case YOUNG_GENERATION:
 382         young.heap_region_do(r);
 383         break;
 384       case OLD_GENERATION:
 385         old.heap_region_do(r);
 386         break;
 387     }
 388     global.heap_region_do(r);
 389   }
 390 
 391   static void log_usage(ShenandoahGeneration* generation, ShenandoahCalculateRegionStatsClosure& stats) {
 392     log_debug(gc)("Safepoint verification: %s verified usage: " SIZE_FORMAT "%s, recorded usage: " SIZE_FORMAT "%s",
 393                   generation->name(),
 394                   byte_size_in_proper_unit(generation->used()), proper_unit_for_byte_size(generation->used()),
 395                   byte_size_in_proper_unit(stats.used()), proper_unit_for_byte_size(stats.used()));
 396   }
 397 
 398   static void validate_usage(const char* label, ShenandoahGeneration* generation, ShenandoahCalculateRegionStatsClosure& stats) {
 399     size_t generation_used = generation->used();
 400     guarantee(stats.used() == generation_used,
 401               "%s: generation (%s) used size must be consistent: generation-used = " SIZE_FORMAT "%s, regions-used = " SIZE_FORMAT "%s",
 402               label, generation->name(),
 403               byte_size_in_proper_unit(generation_used), proper_unit_for_byte_size(generation_used),
 404               byte_size_in_proper_unit(stats.used()), proper_unit_for_byte_size(stats.used()));
 405 
 406     guarantee(stats.regions() == generation->used_regions(),
 407               "%s: generation (%s) used regions (" SIZE_FORMAT ") must equal regions that are in use (" SIZE_FORMAT ")",
 408               label, generation->name(), generation->used_regions(), stats.regions());
 409 
 410     size_t capacity = generation->adjusted_capacity();
 411     guarantee(stats.span() <= capacity,
 412               "%s: generation (%s) size spanned by regions (" SIZE_FORMAT ") must not exceed current capacity (" SIZE_FORMAT "%s)",
 413               label, generation->name(), stats.regions(),
 414               byte_size_in_proper_unit(capacity), proper_unit_for_byte_size(capacity));
 415 
 416   }
 417 };
 418 
 419 class ShenandoahVerifyHeapRegionClosure : public ShenandoahHeapRegionClosure {
 420 private:
 421   ShenandoahHeap* _heap;
 422   const char* _phase;
 423   ShenandoahVerifier::VerifyRegions _regions;
 424 public:
 425   ShenandoahVerifyHeapRegionClosure(const char* phase, ShenandoahVerifier::VerifyRegions regions) :
 426     _heap(ShenandoahHeap::heap()),
 427     _phase(phase),
 428     _regions(regions) {};
 429 
 430   void print_failure(ShenandoahHeapRegion* r, const char* label) {
 431     ResourceMark rm;
 432 
 433     ShenandoahMessageBuffer msg("Shenandoah verification failed; %s: %s\n\n", _phase, label);
 434 
 435     stringStream ss;
 436     r->print_on(&ss);
 437     msg.append("%s", ss.as_string());
 438 
 439     report_vm_error(__FILE__, __LINE__, msg.buffer());
 440   }
 441 
 442   void verify(ShenandoahHeapRegion* r, bool test, const char* msg) {
 443     if (!test) {
 444       print_failure(r, msg);
 445     }
 446   }
 447 
 448   void heap_region_do(ShenandoahHeapRegion* r) {
 449     switch (_regions) {
 450       case ShenandoahVerifier::_verify_regions_disable:
 451         break;
 452       case ShenandoahVerifier::_verify_regions_notrash:
 453         verify(r, !r->is_trash(),
 454                "Should not have trash regions");
 455         break;
 456       case ShenandoahVerifier::_verify_regions_nocset:
 457         verify(r, !r->is_cset(),
 458                "Should not have cset regions");
 459         break;
 460       case ShenandoahVerifier::_verify_regions_notrash_nocset:
 461         verify(r, !r->is_trash(),
 462                "Should not have trash regions");
 463         verify(r, !r->is_cset(),
 464                "Should not have cset regions");
 465         break;
 466       default:
 467         ShouldNotReachHere();
 468     }
 469 
 470     verify(r, r->capacity() == ShenandoahHeapRegion::region_size_bytes(),
 471            "Capacity should match region size");
 472 
 473     verify(r, r->bottom() <= r->top(),
 474            "Region top should not be less than bottom");
 475 
 476     verify(r, r->bottom() <= _heap->marking_context()->top_at_mark_start(r),
 477            "Region TAMS should not be less than bottom");
 478 
 479     verify(r, _heap->marking_context()->top_at_mark_start(r) <= r->top(),
 480            "Complete TAMS should not be larger than top");
 481 
 482     verify(r, r->get_live_data_bytes() <= r->capacity(),
 483            "Live data cannot be larger than capacity");
 484 
 485     verify(r, r->garbage() <= r->capacity(),
 486            "Garbage cannot be larger than capacity");
 487 
 488     verify(r, r->used() <= r->capacity(),
 489            "Used cannot be larger than capacity");
 490 
 491     verify(r, r->get_shared_allocs() <= r->capacity(),
 492            "Shared alloc count should not be larger than capacity");
 493 
 494     verify(r, r->get_tlab_allocs() <= r->capacity(),
 495            "TLAB alloc count should not be larger than capacity");
 496 
 497     verify(r, r->get_gclab_allocs() <= r->capacity(),
 498            "GCLAB alloc count should not be larger than capacity");
 499 
 500     verify(r, r->get_plab_allocs() <= r->capacity(),
 501            "PLAB alloc count should not be larger than capacity");
 502 
 503     verify(r, r->get_shared_allocs() + r->get_tlab_allocs() + r->get_gclab_allocs() + r->get_plab_allocs() == r->used(),
 504            "Accurate accounting: shared + TLAB + GCLAB + PLAB = used");
 505 
 506     verify(r, !r->is_empty() || !r->has_live(),
 507            "Empty regions should not have live data");
 508 
 509     verify(r, r->is_cset() == _heap->collection_set()->is_in(r),
 510            "Transitional: region flags and collection set agree");
 511   }
 512 };
 513 
 514 class ShenandoahVerifierReachableTask : public WorkerTask {
 515 private:
 516   const char* _label;
 517   ShenandoahVerifier::VerifyOptions _options;
 518   ShenandoahHeap* _heap;
 519   ShenandoahLivenessData* _ld;
 520   MarkBitMap* _bitmap;
 521   volatile size_t _processed;
 522 
 523 public:
 524   ShenandoahVerifierReachableTask(MarkBitMap* bitmap,
 525                                   ShenandoahLivenessData* ld,
 526                                   const char* label,
 527                                   ShenandoahVerifier::VerifyOptions options) :
 528     WorkerTask("Shenandoah Verifier Reachable Objects"),
 529     _label(label),
 530     _options(options),
 531     _heap(ShenandoahHeap::heap()),
 532     _ld(ld),
 533     _bitmap(bitmap),
 534     _processed(0) {};
 535 
 536   size_t processed() {
 537     return _processed;
 538   }
 539 
 540   virtual void work(uint worker_id) {
 541     ResourceMark rm;
 542     ShenandoahVerifierStack stack;
 543 
 544     // On level 2, we need to only check the roots once.
 545     // On level 3, we want to check the roots, and seed the local stack.
 546     // It is a lesser evil to accept multiple root scans at level 3, because
 547     // extended parallelism would buy us out.
 548     if (((ShenandoahVerifyLevel == 2) && (worker_id == 0))
 549         || (ShenandoahVerifyLevel >= 3)) {
 550         ShenandoahVerifyOopClosure cl(&stack, _bitmap, _ld,
 551                                       ShenandoahMessageBuffer("%s, Roots", _label),
 552                                       _options);
 553         if (_heap->unload_classes()) {
 554           ShenandoahRootVerifier::strong_roots_do(&cl);
 555         } else {
 556           ShenandoahRootVerifier::roots_do(&cl);
 557         }
 558     }
 559 
 560     size_t processed = 0;
 561 
 562     if (ShenandoahVerifyLevel >= 3) {
 563       ShenandoahVerifyOopClosure cl(&stack, _bitmap, _ld,
 564                                     ShenandoahMessageBuffer("%s, Reachable", _label),
 565                                     _options);
 566       while (!stack.is_empty()) {
 567         processed++;
 568         ShenandoahVerifierTask task = stack.pop();
 569         cl.verify_oops_from(task.obj());
 570       }
 571     }
 572 
 573     Atomic::add(&_processed, processed, memory_order_relaxed);
 574   }
 575 };
 576 
 577 class ShenandoahVerifierMarkedRegionTask : public WorkerTask {
 578 private:
 579   const char* _label;
 580   ShenandoahVerifier::VerifyOptions _options;
 581   ShenandoahHeap* _heap;
 582   MarkBitMap* _bitmap;
 583   ShenandoahLivenessData* _ld;
 584   volatile size_t _claimed;
 585   volatile size_t _processed;
 586   ShenandoahGeneration* _generation;
 587 
 588 public:
 589   ShenandoahVerifierMarkedRegionTask(MarkBitMap* bitmap,
 590                                      ShenandoahLivenessData* ld,
 591                                      const char* label,
 592                                      ShenandoahVerifier::VerifyOptions options) :
 593           WorkerTask("Shenandoah Verifier Marked Objects"),
 594           _label(label),
 595           _options(options),
 596           _heap(ShenandoahHeap::heap()),
 597           _bitmap(bitmap),
 598           _ld(ld),
 599           _claimed(0),
 600           _processed(0),
 601           _generation(NULL) {
 602     if (_heap->mode()->is_generational()) {
 603       _generation = _heap->active_generation();
 604       assert(_generation != NULL, "Expected active generation in this mode.");
 605     }
 606   };
 607 
 608   size_t processed() {
 609     return Atomic::load(&_processed);
 610   }
 611 
 612   virtual void work(uint worker_id) {
 613     ShenandoahVerifierStack stack;
 614     ShenandoahVerifyOopClosure cl(&stack, _bitmap, _ld,
 615                                   ShenandoahMessageBuffer("%s, Marked", _label),
 616                                   _options);
 617 
 618     while (true) {
 619       size_t v = Atomic::fetch_and_add(&_claimed, 1u, memory_order_relaxed);
 620       if (v < _heap->num_regions()) {
 621         ShenandoahHeapRegion* r = _heap->get_region(v);
 622         if (!in_generation(r)) {
 623           continue;
 624         }
 625 
 626         if (!r->is_humongous() && !r->is_trash()) {
 627           work_regular(r, stack, cl);
 628         } else if (r->is_humongous_start()) {
 629           work_humongous(r, stack, cl);
 630         }
 631       } else {
 632         break;
 633       }
 634     }
 635   }
 636 
 637   bool in_generation(ShenandoahHeapRegion* r) {
 638     return _generation == NULL || _generation->contains(r);
 639   }
 640 
 641   virtual void work_humongous(ShenandoahHeapRegion* r, ShenandoahVerifierStack& stack, ShenandoahVerifyOopClosure& cl) {
 642     size_t processed = 0;
 643     HeapWord* obj = r->bottom();
 644     if (_heap->complete_marking_context()->is_marked(cast_to_oop(obj))) {
 645       verify_and_follow(obj, stack, cl, &processed);
 646     }
 647     Atomic::add(&_processed, processed, memory_order_relaxed);
 648   }
 649 
 650   virtual void work_regular(ShenandoahHeapRegion* r, ShenandoahVerifierStack &stack, ShenandoahVerifyOopClosure &cl) {
 651     size_t processed = 0;
 652     ShenandoahMarkingContext* ctx = _heap->complete_marking_context();
 653     HeapWord* tams = ctx->top_at_mark_start(r);
 654 
 655     // Bitmaps, before TAMS
 656     if (tams > r->bottom()) {
 657       HeapWord* start = r->bottom();
 658       HeapWord* addr = ctx->get_next_marked_addr(start, tams);
 659 
 660       while (addr < tams) {
 661         verify_and_follow(addr, stack, cl, &processed);
 662         addr += 1;
 663         if (addr < tams) {
 664           addr = ctx->get_next_marked_addr(addr, tams);
 665         }
 666       }
 667     }
 668 
 669     // Size-based, after TAMS
 670     {
 671       HeapWord* limit = r->top();
 672       HeapWord* addr = tams;
 673 
 674       while (addr < limit) {
 675         verify_and_follow(addr, stack, cl, &processed);
 676         addr += cast_to_oop(addr)->size();
 677       }
 678     }
 679 
 680     Atomic::add(&_processed, processed, memory_order_relaxed);
 681   }
 682 
 683   void verify_and_follow(HeapWord* addr, ShenandoahVerifierStack &stack, ShenandoahVerifyOopClosure &cl, size_t* processed) {
 684     if (!_bitmap->par_mark(addr)) return;
 685 
 686     // Verify the object itself:
 687     oop obj = cast_to_oop(addr);
 688     cl.verify_oop_standalone(obj);
 689 
 690     // Verify everything reachable from that object too, hopefully realizing
 691     // everything was already marked, and never touching further:
 692     if (!is_instance_ref_klass(obj->klass())) {
 693       cl.verify_oops_from(obj);
 694       (*processed)++;
 695     }
 696     while (!stack.is_empty()) {
 697       ShenandoahVerifierTask task = stack.pop();
 698       cl.verify_oops_from(task.obj());
 699       (*processed)++;
 700     }
 701   }
 702 };
 703 
 704 class VerifyThreadGCState : public ThreadClosure {
 705 private:
 706   const char* const _label;
 707          char const _expected;
 708 
 709 public:
 710   VerifyThreadGCState(const char* label, char expected) : _label(label), _expected(expected) {}
 711   void do_thread(Thread* t) {
 712     char actual = ShenandoahThreadLocalData::gc_state(t);
 713     if (actual != _expected && !(actual & ShenandoahHeap::OLD_MARKING)) {
 714       fatal("%s: Thread %s: expected gc-state %d, actual %d", _label, t->name(), _expected, actual);
 715     }
 716   }
 717 };
 718 
 719 void ShenandoahVerifier::verify_at_safepoint(const char* label,
 720                                              VerifyRememberedSet remembered,
 721                                              VerifyForwarded forwarded, VerifyMarked marked,
 722                                              VerifyCollectionSet cset,
 723                                              VerifyLiveness liveness, VerifyRegions regions,
 724                                              VerifyGCState gcstate) {
 725   guarantee(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "only when nothing else happens");
 726   guarantee(ShenandoahVerify, "only when enabled, and bitmap is initialized in ShenandoahHeap::initialize");
 727 
 728   // Avoid side-effect of changing workers' active thread count, but bypass concurrent/parallel protocol check
 729   ShenandoahPushWorkerScope verify_worker_scope(_heap->workers(), _heap->max_workers(), false /*bypass check*/);
 730 
 731   log_info(gc,start)("Verify %s, Level " INTX_FORMAT, label, ShenandoahVerifyLevel);
 732 
 733   // GC state checks
 734   {
 735     char expected = -1;
 736     bool enabled;
 737     switch (gcstate) {
 738       case _verify_gcstate_disable:
 739         enabled = false;
 740         break;
 741       case _verify_gcstate_forwarded:
 742         enabled = true;
 743         expected = ShenandoahHeap::HAS_FORWARDED;
 744         break;
 745       case _verify_gcstate_evacuation:
 746         enabled = true;
 747         expected = ShenandoahHeap::HAS_FORWARDED | ShenandoahHeap::EVACUATION;
 748         if (!_heap->is_stw_gc_in_progress()) {
 749           // Only concurrent GC sets this.
 750           expected |= ShenandoahHeap::WEAK_ROOTS;
 751         }
 752         break;
 753       case _verify_gcstate_updating:
 754         enabled = true;
 755         expected = ShenandoahHeap::HAS_FORWARDED | ShenandoahHeap::UPDATEREFS;
 756         break;
 757       case _verify_gcstate_stable:
 758         enabled = true;
 759         expected = ShenandoahHeap::STABLE;
 760         break;
 761       case _verify_gcstate_stable_weakroots:
 762         enabled = true;
 763         expected = ShenandoahHeap::STABLE;
 764         if (!_heap->is_stw_gc_in_progress()) {
 765           // Only concurrent GC sets this.
 766           expected |= ShenandoahHeap::WEAK_ROOTS;
 767         }
 768         break;
 769       default:
 770         enabled = false;
 771         assert(false, "Unhandled gc-state verification");
 772     }
 773 
 774     if (enabled) {
 775       char actual = _heap->gc_state();
 776       // Old generation marking is allowed in all states.
 777       if (actual != expected && !(actual & ShenandoahHeap::OLD_MARKING)) {
 778         fatal("%s: Global gc-state: expected %d, actual %d", label, expected, actual);
 779       }
 780 
 781       VerifyThreadGCState vtgcs(label, expected);
 782       Threads::java_threads_do(&vtgcs);
 783     }
 784   }
 785 
 786   // Deactivate barriers temporarily: Verifier wants plain heap accesses
 787   ShenandoahGCStateResetter resetter;
 788 
 789   // Heap size checks
 790   {
 791     ShenandoahHeapLocker lock(_heap->lock());
 792 
 793     ShenandoahCalculateRegionStatsClosure cl;
 794     _heap->heap_region_iterate(&cl);
 795     size_t heap_used = _heap->used();
 796     guarantee(cl.used() == heap_used,
 797               "%s: heap used size must be consistent: heap-used = " SIZE_FORMAT "%s, regions-used = " SIZE_FORMAT "%s",
 798               label,
 799               byte_size_in_proper_unit(heap_used), proper_unit_for_byte_size(heap_used),
 800               byte_size_in_proper_unit(cl.used()), proper_unit_for_byte_size(cl.used()));
 801 
 802     size_t heap_committed = _heap->committed();
 803     guarantee(cl.committed() == heap_committed,
 804               "%s: heap committed size must be consistent: heap-committed = " SIZE_FORMAT "%s, regions-committed = " SIZE_FORMAT "%s",
 805               label,
 806               byte_size_in_proper_unit(heap_committed), proper_unit_for_byte_size(heap_committed),
 807               byte_size_in_proper_unit(cl.committed()), proper_unit_for_byte_size(cl.committed()));
 808   }
 809 
 810   log_debug(gc)("Safepoint verification finished heap usage verification");
 811 
 812   ShenandoahGeneration* generation;
 813   if (_heap->mode()->is_generational()) {
 814     generation = _heap->active_generation();
 815     guarantee(generation != NULL, "Need to know which generation to verify.");
 816   } else {
 817     generation = NULL;
 818   }
 819 
 820   if (generation != NULL) {
 821     ShenandoahHeapLocker lock(_heap->lock());
 822 
 823     if (remembered == _verify_remembered_for_marking) {
 824       log_debug(gc)("Safepoint verification of remembered set at mark");
 825     } else if (remembered == _verify_remembered_for_updating_references) {
 826       log_debug(gc)("Safepoint verification of remembered set at update ref");
 827     } else if (remembered == _verify_remembered_after_full_gc) {
 828       log_debug(gc)("Safepoint verification of remembered set after full gc");
 829     }
 830 
 831     if (remembered == _verify_remembered_for_marking) {
 832       _heap->verify_rem_set_at_mark();
 833     } else if (remembered == _verify_remembered_for_updating_references) {
 834       _heap->verify_rem_set_at_update_ref();
 835     } else if (remembered == _verify_remembered_after_full_gc) {
 836       _heap->verify_rem_set_after_full_gc();
 837     }
 838 
 839     ShenandoahGenerationStatsClosure cl;
 840     _heap->heap_region_iterate(&cl);
 841 
 842     if (LogTarget(Debug, gc)::is_enabled()) {
 843       ShenandoahGenerationStatsClosure::log_usage(_heap->old_generation(), cl.old);
 844       ShenandoahGenerationStatsClosure::log_usage(_heap->young_generation(), cl.young);
 845       ShenandoahGenerationStatsClosure::log_usage(_heap->global_generation(), cl.global);
 846     }
 847 
 848     ShenandoahGenerationStatsClosure::validate_usage(label, _heap->old_generation(), cl.old);
 849     ShenandoahGenerationStatsClosure::validate_usage(label, _heap->young_generation(), cl.young);
 850     ShenandoahGenerationStatsClosure::validate_usage(label, _heap->global_generation(), cl.global);
 851   }
 852 
 853   log_debug(gc)("Safepoint verification finished remembered set verification");
 854 
 855   // Internal heap region checks
 856   if (ShenandoahVerifyLevel >= 1) {
 857     ShenandoahVerifyHeapRegionClosure cl(label, regions);
 858     if (generation != NULL) {
 859       generation->heap_region_iterate(&cl);
 860     } else {
 861       _heap->heap_region_iterate(&cl);
 862     }
 863   }
 864 
 865   log_debug(gc)("Safepoint verification finished heap region closure verification");
 866 
 867   OrderAccess::fence();
 868 
 869   if (UseTLAB) {
 870     _heap->labs_make_parsable();
 871   }
 872 
 873   // Allocate temporary bitmap for storing marking wavefront:
 874   _verification_bit_map->clear();
 875 
 876   // Allocate temporary array for storing liveness data
 877   ShenandoahLivenessData* ld = NEW_C_HEAP_ARRAY(ShenandoahLivenessData, _heap->num_regions(), mtGC);
 878   Copy::fill_to_bytes((void*)ld, _heap->num_regions()*sizeof(ShenandoahLivenessData), 0);
 879 
 880   const VerifyOptions& options = ShenandoahVerifier::VerifyOptions(forwarded, marked, cset, liveness, regions, gcstate);
 881 
 882   // Steps 1-2. Scan root set to get initial reachable set. Finish walking the reachable heap.
 883   // This verifies what application can see, since it only cares about reachable objects.
 884   size_t count_reachable = 0;
 885   if (ShenandoahVerifyLevel >= 2) {
 886     ShenandoahVerifierReachableTask task(_verification_bit_map, ld, label, options);
 887     _heap->workers()->run_task(&task);
 888     count_reachable = task.processed();
 889   }
 890 
 891   log_debug(gc)("Safepoint verification finished getting initial reachable set");
 892 
 893   // Step 3. Walk marked objects. Marked objects might be unreachable. This verifies what collector,
 894   // not the application, can see during the region scans. There is no reason to process the objects
 895   // that were already verified, e.g. those marked in verification bitmap. There is interaction with TAMS:
 896   // before TAMS, we verify the bitmaps, if available; after TAMS, we walk until the top(). It mimics
 897   // what marked_object_iterate is doing, without calling into that optimized (and possibly incorrect)
 898   // version
 899 
 900   size_t count_marked = 0;
 901   if (ShenandoahVerifyLevel >= 4 && (marked == _verify_marked_complete || marked == _verify_marked_complete_except_references)) {
 902     guarantee(_heap->marking_context()->is_complete(), "Marking context should be complete");
 903     ShenandoahVerifierMarkedRegionTask task(_verification_bit_map, ld, label, options);
 904     _heap->workers()->run_task(&task);
 905     count_marked = task.processed();
 906   } else {
 907     guarantee(ShenandoahVerifyLevel < 4 || marked == _verify_marked_incomplete || marked == _verify_marked_disable, "Should be");
 908   }
 909 
 910   log_debug(gc)("Safepoint verification finished walking marked objects");
 911 
 912   // Step 4. Verify accumulated liveness data, if needed. Only reliable if verification level includes
 913   // marked objects.
 914 
 915   if (ShenandoahVerifyLevel >= 4 && marked == _verify_marked_complete && liveness == _verify_liveness_complete) {
 916     for (size_t i = 0; i < _heap->num_regions(); i++) {
 917       ShenandoahHeapRegion* r = _heap->get_region(i);
 918       if (generation != NULL && !generation->contains(r)) {
 919         continue;
 920       }
 921 
 922       juint verf_live = 0;
 923       if (r->is_humongous()) {
 924         // For humongous objects, test if start region is marked live, and if so,
 925         // all humongous regions in that chain have live data equal to their "used".
 926         juint start_live = Atomic::load(&ld[r->humongous_start_region()->index()]);
 927         if (start_live > 0) {
 928           verf_live = (juint)(r->used() / HeapWordSize);
 929         }
 930       } else {
 931         verf_live = Atomic::load(&ld[r->index()]);
 932       }
 933 
 934       size_t reg_live = r->get_live_data_words();
 935       if (reg_live != verf_live) {
 936         stringStream ss;
 937         r->print_on(&ss);
 938         fatal("%s: Live data should match: region-live = " SIZE_FORMAT ", verifier-live = " UINT32_FORMAT "\n%s",
 939               label, reg_live, verf_live, ss.freeze());
 940       }
 941     }
 942   }
 943 
 944   log_debug(gc)("Safepoint verification finished accumulation of liveness data");
 945 
 946 
 947   log_info(gc)("Verify %s, Level " INTX_FORMAT " (" SIZE_FORMAT " reachable, " SIZE_FORMAT " marked)",
 948                label, ShenandoahVerifyLevel, count_reachable, count_marked);
 949 
 950   FREE_C_HEAP_ARRAY(ShenandoahLivenessData, ld);
 951 }
 952 
 953 void ShenandoahVerifier::verify_generic(VerifyOption vo) {
 954   verify_at_safepoint(
 955           "Generic Verification",
 956           _verify_remembered_disable,  // do not verify remembered set
 957           _verify_forwarded_allow,     // conservatively allow forwarded
 958           _verify_marked_disable,      // do not verify marked: lots ot time wasted checking dead allocations
 959           _verify_cset_disable,        // cset may be inconsistent
 960           _verify_liveness_disable,    // no reliable liveness data
 961           _verify_regions_disable,     // no reliable region data
 962           _verify_gcstate_disable      // no data about gcstate
 963   );
 964 }
 965 
 966 void ShenandoahVerifier::verify_before_concmark() {
 967     verify_at_safepoint(
 968           "Before Mark",
 969           _verify_remembered_for_marking,  // verify read-only remembered set from bottom() to top()
 970           _verify_forwarded_none,      // UR should have fixed up
 971           _verify_marked_disable,      // do not verify marked: lots ot time wasted checking dead allocations
 972           _verify_cset_none,           // UR should have fixed this
 973           _verify_liveness_disable,    // no reliable liveness data
 974           _verify_regions_notrash,     // no trash regions
 975           _verify_gcstate_stable       // there are no forwarded objects
 976   );
 977 }
 978 
 979 void ShenandoahVerifier::verify_after_concmark() {
 980   verify_at_safepoint(
 981           "After Mark",
 982           _verify_remembered_disable,  // do not verify remembered set
 983           _verify_forwarded_none,      // no forwarded references
 984           _verify_marked_complete_except_references, // bitmaps as precise as we can get, except dangling j.l.r.Refs
 985           _verify_cset_none,           // no references to cset anymore
 986           _verify_liveness_complete,   // liveness data must be complete here
 987           _verify_regions_disable,     // trash regions not yet recycled
 988           _verify_gcstate_stable_weakroots  // heap is still stable, weakroots are in progress
 989   );
 990 }
 991 
 992 void ShenandoahVerifier::verify_before_evacuation() {
 993   verify_at_safepoint(
 994           "Before Evacuation",
 995           _verify_remembered_disable,                // do not verify remembered set
 996           _verify_forwarded_none,                    // no forwarded references
 997           _verify_marked_complete_except_references, // walk over marked objects too
 998           _verify_cset_disable,                      // non-forwarded references to cset expected
 999           _verify_liveness_complete,                 // liveness data must be complete here
1000           _verify_regions_disable,                   // trash regions not yet recycled
1001           _verify_gcstate_stable_weakroots           // heap is still stable, weakroots are in progress
1002   );
1003 }
1004 
1005 void ShenandoahVerifier::verify_during_evacuation() {
1006   verify_at_safepoint(
1007           "During Evacuation",
1008           _verify_remembered_disable, // do not verify remembered set
1009           _verify_forwarded_allow,    // some forwarded references are allowed
1010           _verify_marked_disable,     // walk only roots
1011           _verify_cset_disable,       // some cset references are not forwarded yet
1012           _verify_liveness_disable,   // liveness data might be already stale after pre-evacs
1013           _verify_regions_disable,    // trash regions not yet recycled
1014           _verify_gcstate_evacuation  // evacuation is in progress
1015   );
1016 }
1017 
1018 void ShenandoahVerifier::verify_after_evacuation() {
1019   verify_at_safepoint(
1020           "After Evacuation",
1021           _verify_remembered_disable,  // do not verify remembered set
1022           _verify_forwarded_allow,     // objects are still forwarded
1023           _verify_marked_complete,     // bitmaps might be stale, but alloc-after-mark should be well
1024           _verify_cset_forwarded,      // all cset refs are fully forwarded
1025           _verify_liveness_disable,    // no reliable liveness data anymore
1026           _verify_regions_notrash,     // trash regions have been recycled already
1027           _verify_gcstate_forwarded    // evacuation produced some forwarded objects
1028   );
1029 }
1030 
1031 void ShenandoahVerifier::verify_before_updaterefs() {
1032   verify_at_safepoint(
1033           "Before Updating References",
1034           _verify_remembered_for_updating_references,  // verify read-write remembered set
1035           _verify_forwarded_allow,                     // forwarded references allowed
1036           _verify_marked_complete,                     // bitmaps might be stale, but alloc-after-mark should be well
1037           _verify_cset_forwarded,                      // all cset refs are fully forwarded
1038           _verify_liveness_disable,                    // no reliable liveness data anymore
1039           _verify_regions_notrash,                     // trash regions have been recycled already
1040           _verify_gcstate_updating                     // evacuation should have produced some forwarded objects
1041   );
1042 }
1043 
1044 void ShenandoahVerifier::verify_after_updaterefs() {
1045   verify_at_safepoint(
1046           "After Updating References",
1047           _verify_remembered_disable,  // do not verify remembered set
1048           _verify_forwarded_none,      // no forwarded references
1049           _verify_marked_complete,     // bitmaps might be stale, but alloc-after-mark should be well
1050           _verify_cset_none,           // no cset references, all updated
1051           _verify_liveness_disable,    // no reliable liveness data anymore
1052           _verify_regions_nocset,      // no cset regions, trash regions have appeared
1053           _verify_gcstate_stable       // update refs had cleaned up forwarded objects
1054   );
1055 }
1056 
1057 void ShenandoahVerifier::verify_after_degenerated() {
1058   verify_at_safepoint(
1059           "After Degenerated GC",
1060           _verify_remembered_disable,  // do not verify remembered set
1061           _verify_forwarded_none,      // all objects are non-forwarded
1062           _verify_marked_complete,     // all objects are marked in complete bitmap
1063           _verify_cset_none,           // no cset references
1064           _verify_liveness_disable,    // no reliable liveness data anymore
1065           _verify_regions_notrash_nocset, // no trash, no cset
1066           _verify_gcstate_stable       // degenerated refs had cleaned up forwarded objects
1067   );
1068 }
1069 
1070 void ShenandoahVerifier::verify_before_fullgc() {
1071   verify_at_safepoint(
1072           "Before Full GC",
1073           _verify_remembered_disable,  // do not verify remembered set
1074           _verify_forwarded_allow,     // can have forwarded objects
1075           _verify_marked_disable,      // do not verify marked: lots ot time wasted checking dead allocations
1076           _verify_cset_disable,        // cset might be foobared
1077           _verify_liveness_disable,    // no reliable liveness data anymore
1078           _verify_regions_disable,     // no reliable region data here
1079           _verify_gcstate_disable      // no reliable gcstate data
1080   );
1081 }
1082 
1083 void ShenandoahVerifier::verify_after_generational_fullgc() {
1084   verify_at_safepoint(
1085           "After Full Generational GC",
1086           _verify_remembered_after_full_gc,  // verify read-write remembered set
1087           _verify_forwarded_none,      // all objects are non-forwarded
1088           _verify_marked_complete,     // all objects are marked in complete bitmap
1089           _verify_cset_none,           // no cset references
1090           _verify_liveness_disable,    // no reliable liveness data anymore
1091           _verify_regions_notrash_nocset, // no trash, no cset
1092           _verify_gcstate_stable       // full gc cleaned up everything
1093   );
1094 }
1095 
1096 void ShenandoahVerifier::verify_after_fullgc() {
1097   verify_at_safepoint(
1098           "After Full GC",
1099           _verify_remembered_disable,  // do not verify remembered set
1100           _verify_forwarded_none,      // all objects are non-forwarded
1101           _verify_marked_complete,     // all objects are marked in complete bitmap
1102           _verify_cset_none,           // no cset references
1103           _verify_liveness_disable,    // no reliable liveness data anymore
1104           _verify_regions_notrash_nocset, // no trash, no cset
1105           _verify_gcstate_stable        // full gc cleaned up everything
1106   );
1107 }
1108 
1109 class ShenandoahVerifyNoForwared : public BasicOopIterateClosure {
1110 private:
1111   template <class T>
1112   void do_oop_work(T* p) {
1113     T o = RawAccess<>::oop_load(p);
1114     if (!CompressedOops::is_null(o)) {
1115       oop obj = CompressedOops::decode_not_null(o);
1116       oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj);
1117       if (obj != fwd) {
1118         ShenandoahAsserts::print_failure(ShenandoahAsserts::_safe_all, obj, p, NULL,
1119                                          "Verify Roots", "Should not be forwarded", __FILE__, __LINE__);
1120       }
1121     }
1122   }
1123 
1124 public:
1125   void do_oop(narrowOop* p) { do_oop_work(p); }
1126   void do_oop(oop* p)       { do_oop_work(p); }
1127 };
1128 
1129 class ShenandoahVerifyInToSpaceClosure : public BasicOopIterateClosure {
1130 private:
1131   template <class T>
1132   void do_oop_work(T* p) {
1133     T o = RawAccess<>::oop_load(p);
1134     if (!CompressedOops::is_null(o)) {
1135       oop obj = CompressedOops::decode_not_null(o);
1136       ShenandoahHeap* heap = ShenandoahHeap::heap();
1137 
1138       if (!heap->marking_context()->is_marked_or_old(obj)) {
1139         ShenandoahAsserts::print_failure(ShenandoahAsserts::_safe_all, obj, p, NULL,
1140                 "Verify Roots In To-Space", "Should be marked", __FILE__, __LINE__);
1141       }
1142 
1143       if (heap->in_collection_set(obj)) {
1144         ShenandoahAsserts::print_failure(ShenandoahAsserts::_safe_all, obj, p, NULL,
1145                 "Verify Roots In To-Space", "Should not be in collection set", __FILE__, __LINE__);
1146       }
1147 
1148       oop fwd = ShenandoahForwarding::get_forwardee_raw_unchecked(obj);
1149       if (obj != fwd) {
1150         ShenandoahAsserts::print_failure(ShenandoahAsserts::_safe_all, obj, p, NULL,
1151                 "Verify Roots In To-Space", "Should not be forwarded", __FILE__, __LINE__);
1152       }
1153     }
1154   }
1155 
1156 public:
1157   void do_oop(narrowOop* p) { do_oop_work(p); }
1158   void do_oop(oop* p)       { do_oop_work(p); }
1159 };
1160 
1161 void ShenandoahVerifier::verify_roots_in_to_space() {
1162   ShenandoahVerifyInToSpaceClosure cl;
1163   ShenandoahRootVerifier::roots_do(&cl);
1164 }
1165 
1166 void ShenandoahVerifier::verify_roots_no_forwarded() {
1167   ShenandoahVerifyNoForwared cl;
1168   ShenandoahRootVerifier::roots_do(&cl);
1169 }