1 /*
 2  * Copyright (c) 2022, 2025, Oracle and/or its affiliates. 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 #ifndef SHARE_CDS_CDSHEAPVERIFIER_HPP
26 #define SHARE_CDS_CDSHEAPVERIFIER_HPP
27 
28 #include "cds/heapShared.hpp"
29 #include "memory/iterator.hpp"
30 #include "oops/oopHandle.hpp"
31 #include "utilities/growableArray.hpp"
32 #include "utilities/resourceHash.hpp"
33 
34 class InstanceKlass;
35 class Symbol;
36 
37 #if INCLUDE_CDS_JAVA_HEAP
38 
39 class CDSHeapVerifier : public KlassClosure {
40   class CheckStaticFields;
41   class TraceFields;
42 
43   int _archived_objs;
44   int _problems;
45 
46   struct StaticFieldInfo {
47     InstanceKlass* _holder;
48     Symbol* _name;
49   };
50 
51   ResourceHashtable<oop, StaticFieldInfo,
52       15889, // prime number
53       AnyObj::C_HEAP,
54       mtClassShared,
55       HeapShared::oop_hash> _table;
56 
57   GrowableArray<const char**> _exclusions;
58 
59   void add_exclusion(const char** excl) {
60     _exclusions.append(excl);
61   }
62   void add_static_obj_field(InstanceKlass* ik, oop field, Symbol* name);
63 
64   const char** find_exclusion(InstanceKlass* ik) {
65     for (int i = 0; i < _exclusions.length(); i++) {
66       const char** excl = _exclusions.at(i);
67       if (ik->name()->equals(excl[0])) {
68         return &excl[1];
69       }
70     }
71     return nullptr;
72   }
73   static int trace_to_root(outputStream* st, oop orig_obj, oop orig_field, HeapShared::CachedOopInfo* p);
74 
75   CDSHeapVerifier();
76   ~CDSHeapVerifier();
77 
78 public:
79 
80   // Overrides KlassClosure::do_klass()
81   virtual void do_klass(Klass* k);
82 
83   // For ResourceHashtable::iterate()
84   inline bool do_entry(OopHandle& orig_obj, HeapShared::CachedOopInfo& value);
85 
86   static void verify();
87 
88   static void trace_to_root(outputStream* st, oop orig_obj);
89 };
90 
91 #endif // INCLUDE_CDS_JAVA_HEAP
92 #endif // SHARE_CDS_CDSHEAPVERIFIER_HPP