8246458: Shenandoah: TestAllocObjects.java test fail with -XX:+ShenandoahVerify

Reviewed-by: shade
diff --git a/src/hotspot/share/gc/shenandoah/shenandoahHeap.cpp b/src/hotspot/share/gc/shenandoah/shenandoahHeap.cpp
index aa56ee1..5611a9e 100644
--- a/src/hotspot/share/gc/shenandoah/shenandoahHeap.cpp
+++ b/src/hotspot/share/gc/shenandoah/shenandoahHeap.cpp
@@ -1589,17 +1589,20 @@
       }
 
       if (ShenandoahVerify) {
-        ShenandoahRootVerifier::RootTypes types = ShenandoahRootVerifier::None;
-        if (ShenandoahConcurrentRoots::should_do_concurrent_roots()) {
-          types = ShenandoahRootVerifier::combine(ShenandoahRootVerifier::JNIHandleRoots, ShenandoahRootVerifier::WeakRoots);
-          types = ShenandoahRootVerifier::combine(types, ShenandoahRootVerifier::CLDGRoots);
-          types = ShenandoahRootVerifier::combine(types, ShenandoahRootVerifier::StringDedupRoots);
-        }
+        // If OOM while evacuating/updating of roots, there is no guarantee of their consistencies
+        if (!cancelled_gc()) {
+          ShenandoahRootVerifier::RootTypes types = ShenandoahRootVerifier::None;
+          if (ShenandoahConcurrentRoots::should_do_concurrent_roots()) {
+            types = ShenandoahRootVerifier::combine(ShenandoahRootVerifier::JNIHandleRoots, ShenandoahRootVerifier::WeakRoots);
+            types = ShenandoahRootVerifier::combine(types, ShenandoahRootVerifier::CLDGRoots);
+            types = ShenandoahRootVerifier::combine(types, ShenandoahRootVerifier::StringDedupRoots);
+          }
 
-        if (ShenandoahConcurrentRoots::should_do_concurrent_class_unloading()) {
-          types = ShenandoahRootVerifier::combine(types, ShenandoahRootVerifier::CodeRoots);
+          if (ShenandoahConcurrentRoots::should_do_concurrent_class_unloading()) {
+            types = ShenandoahRootVerifier::combine(types, ShenandoahRootVerifier::CodeRoots);
+          }
+          verifier()->verify_roots_no_forwarded_except(types);
         }
-        verifier()->verify_roots_no_forwarded_except(types);
         verifier()->verify_during_evacuation();
       }
     } else {