1 /*
   2  * Copyright (c) 2019, Red Hat, Inc. All rights reserved.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 
  25 #include "precompiled.hpp"
  26 
  27 
  28 #include "classfile/classLoaderDataGraph.hpp"
  29 #include "classfile/systemDictionary.hpp"
  30 #include "code/codeCache.hpp"
  31 #include "gc/shenandoah/shenandoahHeap.hpp"
  32 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
  33 #include "gc/shenandoah/shenandoahRootVerifier.hpp"
  34 #include "gc/shenandoah/shenandoahStringDedup.hpp"
  35 #include "gc/shared/weakProcessor.inline.hpp"
  36 #include "memory/universe.hpp"
  37 #include "runtime/thread.hpp"
  38 #include "services/management.hpp"
  39 #include "utilities/debug.hpp"
  40 
  41 // Check for overflow of number of root types.
  42 STATIC_ASSERT((static_cast<uint>(ShenandoahRootVerifier::AllRoots) + 1) > static_cast<uint>(ShenandoahRootVerifier::AllRoots));
  43 
  44 ShenandoahRootVerifier::ShenandoahRootVerifier() : _types(AllRoots) {
  45 }
  46 
  47 void ShenandoahRootVerifier::excludes(RootTypes types) {
  48   _types = static_cast<ShenandoahRootVerifier::RootTypes>(static_cast<uint>(_types) & (~static_cast<uint>(types)));
  49 }
  50 
  51 bool ShenandoahRootVerifier::verify(RootTypes type) const {
  52   return (_types & type) != 0;
  53 }
  54 
  55 void ShenandoahRootVerifier::oops_do(OopClosure* oops) {
  56   CodeBlobToOopClosure blobs(oops, !CodeBlobToOopClosure::FixRelocations);
  57   if (verify(CodeRoots)) {
  58     CodeCache::blobs_do(&blobs);
  59   }
  60 
  61   if (verify(CLDGRoots)) {
  62     CLDToOopClosure clds(oops, ClassLoaderData::_claim_none);
  63     ClassLoaderDataGraph::cld_do(&clds);
  64   }
  65 
  66   if (verify(SerialRoots)) {
  67     Universe::oops_do(oops);
  68     Management::oops_do(oops);
  69     JvmtiExport::oops_do(oops);
  70     JNIHandles::oops_do(oops);
  71     ObjectSynchronizer::oops_do(oops);
  72     SystemDictionary::oops_do(oops);
  73   }
  74 
  75   if (verify(WeakRoots)) {
  76     AlwaysTrueClosure always_true;
  77     WeakProcessor::weak_oops_do(&always_true, oops);
  78   }
  79 
  80   if (ShenandoahStringDedup::is_enabled() && verify(StringDedupRoots)) {
  81     ShenandoahStringDedup::oops_do_slow(oops);
  82   }
  83 
  84   if (verify(ThreadRoots)) {
  85     // Do thread roots the last. This allows verification code to find
  86     // any broken objects from those special roots first, not the accidental
  87     // dangling reference from the thread root.
  88     Threads::possibly_parallel_oops_do(false, oops, &blobs);
  89   }
  90 }
  91 
  92 void ShenandoahRootVerifier::roots_do(OopClosure* oops) {
  93   CodeBlobToOopClosure blobs(oops, !CodeBlobToOopClosure::FixRelocations);
  94   CodeCache::blobs_do(&blobs);
  95 
  96   CLDToOopClosure clds(oops, ClassLoaderData::_claim_none);
  97   ClassLoaderDataGraph::cld_do(&clds);
  98 
  99   Universe::oops_do(oops);
 100   Management::oops_do(oops);
 101   JvmtiExport::oops_do(oops);
 102   JNIHandles::oops_do(oops);
 103   ObjectSynchronizer::oops_do(oops);
 104   SystemDictionary::oops_do(oops);
 105 
 106   AlwaysTrueClosure always_true;
 107   WeakProcessor::weak_oops_do(&always_true, oops);
 108 
 109   if (ShenandoahStringDedup::is_enabled()) {
 110     ShenandoahStringDedup::oops_do_slow(oops);
 111   }
 112 
 113   // Do thread roots the last. This allows verification code to find
 114   // any broken objects from those special roots first, not the accidental
 115   // dangling reference from the thread root.
 116   Threads::possibly_parallel_oops_do(false, oops, &blobs);
 117 }
 118 
 119 void ShenandoahRootVerifier::strong_roots_do(OopClosure* oops) {
 120   CodeBlobToOopClosure blobs(oops, !CodeBlobToOopClosure::FixRelocations);
 121 
 122   CLDToOopClosure clds(oops, ClassLoaderData::_claim_none);
 123   ClassLoaderDataGraph::roots_cld_do(&clds, NULL);
 124 
 125   Universe::oops_do(oops);
 126   Management::oops_do(oops);
 127   JvmtiExport::oops_do(oops);
 128   JNIHandles::oops_do(oops);
 129   ObjectSynchronizer::oops_do(oops);
 130   SystemDictionary::oops_do(oops);
 131 
 132   // Do thread roots the last. This allows verification code to find
 133   // any broken objects from those special roots first, not the accidental
 134   // dangling reference from the thread root.
 135   Threads::possibly_parallel_oops_do(false, oops, &blobs);
 136 }