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