1 /*
  2  * Copyright (c) 2018, 2020, 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_MEMORY_HEAPSHARED_HPP
 26 #define SHARE_MEMORY_HEAPSHARED_HPP
 27 
 28 #include "classfile/compactHashtable.hpp"
 29 #include "classfile/javaClasses.hpp"
 30 #include "classfile/systemDictionary.hpp"
 31 #include "memory/allocation.hpp"
 32 #include "memory/metaspaceShared.hpp"
 33 #include "oops/compressedOops.hpp"
 34 #include "oops/objArrayKlass.hpp"
 35 #include "oops/oop.hpp"
 36 #include "oops/typeArrayKlass.hpp"
 37 #include "utilities/bitMap.hpp"
 38 #include "utilities/growableArray.hpp"
 39 #include "utilities/resourceHash.hpp"
 40 
 41 #if INCLUDE_CDS_JAVA_HEAP
 42 class DumpedInternedStrings;
 43 
 44 struct ArchivableStaticFieldInfo {
 45   const char* klass_name;
 46   const char* field_name;
 47   InstanceKlass* klass;
 48   int offset;
 49   BasicType type;
 50 };
 51 
 52 // A dump time sub-graph info for Klass _k. It includes the entry points
 53 // (static fields in _k's mirror) of the archived sub-graphs reachable
 54 // from _k's mirror. It also contains a list of Klasses of the objects
 55 // within the sub-graphs.
 56 class KlassSubGraphInfo: public CHeapObj<mtClass> {
 57  private:
 58   // The class that contains the static field(s) as the entry point(s)
 59   // of archived object sub-graph(s).
 60   Klass* _k;
 61   // A list of classes need to be loaded and initialized before the archived
 62   // object sub-graphs can be accessed at runtime.
 63   GrowableArray<Klass*>* _subgraph_object_klasses;
 64   // A list of _k's static fields as the entry points of archived sub-graphs.
 65   // For each entry field, it is a tuple of field_offset, field_value and
 66   // is_closed_archive flag.
 67   GrowableArray<juint>*  _subgraph_entry_fields;
 68 
 69   bool _is_full_module_graph;
 70  public:
 71   KlassSubGraphInfo(Klass* k, bool is_full_module_graph) :
 72     _k(k),  _subgraph_object_klasses(NULL),
 73     _subgraph_entry_fields(NULL),
 74     _is_full_module_graph(is_full_module_graph) {}
 75   ~KlassSubGraphInfo() {
 76     if (_subgraph_object_klasses != NULL) {
 77       delete _subgraph_object_klasses;
 78     }
 79     if (_subgraph_entry_fields != NULL) {
 80       delete _subgraph_entry_fields;
 81     }
 82   };
 83 
 84   Klass* klass()            { return _k; }
 85   GrowableArray<Klass*>* subgraph_object_klasses() {
 86     return _subgraph_object_klasses;
 87   }
 88   GrowableArray<juint>*  subgraph_entry_fields() {
 89     return _subgraph_entry_fields;
 90   }
 91   void add_subgraph_entry_field(int static_field_offset, oop v,
 92                                 bool is_closed_archive);
 93   void add_subgraph_object_klass(Klass *orig_k, Klass *relocated_k);
 94   int num_subgraph_object_klasses() {
 95     return _subgraph_object_klasses == NULL ? 0 :
 96            _subgraph_object_klasses->length();
 97   }
 98   bool is_full_module_graph() const { return _is_full_module_graph; }
 99 };
100 
101 // An archived record of object sub-graphs reachable from static
102 // fields within _k's mirror. The record is reloaded from the archive
103 // at runtime.
104 class ArchivedKlassSubGraphInfoRecord {
105  private:
106   Klass* _k;
107   bool _is_full_module_graph;
108 
109   // contains pairs of field offset and value for each subgraph entry field
110   Array<juint>* _entry_field_records;
111 
112   // klasses of objects in archived sub-graphs referenced from the entry points
113   // (static fields) in the containing class
114   Array<Klass*>* _subgraph_object_klasses;
115  public:
116   ArchivedKlassSubGraphInfoRecord() :
117     _k(NULL), _entry_field_records(NULL), _subgraph_object_klasses(NULL) {}
118   void init(KlassSubGraphInfo* info);
119   Klass* klass() const { return _k; }
120   Array<juint>*  entry_field_records() const { return _entry_field_records; }
121   Array<Klass*>* subgraph_object_klasses() const { return _subgraph_object_klasses; }
122   bool is_full_module_graph() const { return _is_full_module_graph; }
123 };
124 #endif // INCLUDE_CDS_JAVA_HEAP
125 
126 class HeapShared: AllStatic {
127   friend class VerifySharedOopClosure;
128  private:
129 
130 #if INCLUDE_CDS_JAVA_HEAP
131   static bool _closed_archive_heap_region_mapped;
132   static bool _open_archive_heap_region_mapped;
133   static bool _archive_heap_region_fixed;
134   static DumpedInternedStrings *_dumped_interned_strings;
135 
136 public:
137   static bool oop_equals(oop const& p1, oop const& p2) {
138     return p1 == p2;
139   }
140   static unsigned oop_hash(oop const& p);
141   static unsigned string_oop_hash(oop const& string) {
142     return java_lang_String::hash_code(string);
143   }
144 
145 private:
146   typedef ResourceHashtable<oop, oop,
147       HeapShared::oop_hash,
148       HeapShared::oop_equals,
149       15889, // prime number
150       ResourceObj::C_HEAP> ArchivedObjectCache;
151   static ArchivedObjectCache* _archived_object_cache;
152 
153   static bool klass_equals(Klass* const& p1, Klass* const& p2) {
154     return primitive_equals<Klass*>(p1, p2);
155   }
156 
157   static unsigned klass_hash(Klass* const& klass) {
158     // Generate deterministic hashcode even if SharedBaseAddress is changed due to ASLR.
159     return primitive_hash<address>(address(klass) - SharedBaseAddress);
160   }
161 
162   class DumpTimeKlassSubGraphInfoTable
163     : public ResourceHashtable<Klass*, KlassSubGraphInfo,
164                                HeapShared::klass_hash,
165                                HeapShared::klass_equals,
166                                137, // prime number
167                                ResourceObj::C_HEAP> {
168   public:
169     int _count;
170   };
171 
172 public: // solaris compiler wants this for RunTimeKlassSubGraphInfoTable
173   inline static bool record_equals_compact_hashtable_entry(
174        const ArchivedKlassSubGraphInfoRecord* value, const Klass* key, int len_unused) {
175     return (value->klass() == key);
176   }
177 
178 private:
179   typedef OffsetCompactHashtable<
180     const Klass*,
181     const ArchivedKlassSubGraphInfoRecord*,
182     record_equals_compact_hashtable_entry
183     > RunTimeKlassSubGraphInfoTable;
184 
185   static DumpTimeKlassSubGraphInfoTable* _dump_time_subgraph_info_table;
186   static RunTimeKlassSubGraphInfoTable _run_time_subgraph_info_table;
187 
188   static void check_closed_archive_heap_region_object(InstanceKlass* k,
189                                                       Thread* THREAD);
190 
191   static void archive_object_subgraphs(ArchivableStaticFieldInfo fields[],
192                                        int num,
193                                        bool is_closed_archive,
194                                        bool is_full_module_graph,
195                                        Thread* THREAD);
196 
197   // Archive object sub-graph starting from the given static field
198   // in Klass k's mirror.
199   static void archive_reachable_objects_from_static_field(
200     InstanceKlass* k, const char* klass_name,
201     int field_offset, const char* field_name,
202     bool is_closed_archive, TRAPS);
203 
204   static void verify_subgraph_from_static_field(
205     InstanceKlass* k, int field_offset) PRODUCT_RETURN;
206   static void verify_reachable_objects_from(oop obj, bool is_archived) PRODUCT_RETURN;
207   static void verify_subgraph_from(oop orig_obj) PRODUCT_RETURN;
208 
209   static KlassSubGraphInfo* init_subgraph_info(Klass *k, bool is_full_module_graph);
210   static KlassSubGraphInfo* get_subgraph_info(Klass *k);
211 
212   static void init_subgraph_entry_fields(ArchivableStaticFieldInfo fields[],
213                                          int num, Thread* THREAD);
214 
215   // Used by decode_from_archive
216   static address _narrow_oop_base;
217   static int     _narrow_oop_shift;
218 
219   typedef ResourceHashtable<oop, bool,
220       HeapShared::oop_hash,
221       HeapShared::oop_equals,
222       15889, // prime number
223       ResourceObj::C_HEAP> SeenObjectsTable;
224 
225   static SeenObjectsTable *_seen_objects_table;
226 
227   static void init_seen_objects_table() {
228     assert(_seen_objects_table == NULL, "must be");
229     _seen_objects_table = new (ResourceObj::C_HEAP, mtClass)SeenObjectsTable();
230   }
231   static void delete_seen_objects_table() {
232     assert(_seen_objects_table != NULL, "must be");
233     delete _seen_objects_table;
234     _seen_objects_table = NULL;
235   }
236 
237   // Statistics (for one round of start_recording_subgraph ... done_recording_subgraph)
238   static int _num_new_walked_objs;
239   static int _num_new_archived_objs;
240   static int _num_old_recorded_klasses;
241 
242   // Statistics (for all archived subgraphs)
243   static int _num_total_subgraph_recordings;
244   static int _num_total_walked_objs;
245   static int _num_total_archived_objs;
246   static int _num_total_recorded_klasses;
247   static int _num_total_verifications;
248 
249   static void start_recording_subgraph(InstanceKlass *k, const char* klass_name,
250                                        bool is_full_module_graph);
251   static void done_recording_subgraph(InstanceKlass *k, const char* klass_name);
252 
253   static bool has_been_seen_during_subgraph_recording(oop obj);
254   static void set_has_been_seen_during_subgraph_recording(oop obj);
255 
256   static void check_module_oop(oop orig_module_obj);
257 
258  public:
259   static void reset_archived_object_states(TRAPS);
260   static void create_archived_object_cache() {
261     _archived_object_cache =
262       new (ResourceObj::C_HEAP, mtClass)ArchivedObjectCache();
263   }
264   static void destroy_archived_object_cache() {
265     delete _archived_object_cache;
266     _archived_object_cache = NULL;
267   }
268   static ArchivedObjectCache* archived_object_cache() {
269     return _archived_object_cache;
270   }
271 
272   static oop find_archived_heap_object(oop obj);
273   static oop archive_heap_object(oop obj, Thread* THREAD);
274   static oop materialize_archived_object(narrowOop v);
275 
276   static void archive_klass_objects(Thread* THREAD);
277 
278   static void set_archive_heap_region_fixed() {
279     _archive_heap_region_fixed = true;
280   }
281   static bool archive_heap_region_fixed() {
282     return _archive_heap_region_fixed;
283   }
284 
285   static void archive_java_heap_objects(GrowableArray<MemRegion> *closed,
286                                         GrowableArray<MemRegion> *open);
287   static void copy_closed_archive_heap_objects(GrowableArray<MemRegion> * closed_archive);
288   static void copy_open_archive_heap_objects(GrowableArray<MemRegion> * open_archive);
289 
290   static oop archive_reachable_objects_from(int level,
291                                             KlassSubGraphInfo* subgraph_info,
292                                             oop orig_obj,
293                                             bool is_closed_archive,
294                                             TRAPS);
295 
296   static ResourceBitMap calculate_oopmap(MemRegion region);
297   static void add_to_dumped_interned_strings(oop string);
298 #endif // INCLUDE_CDS_JAVA_HEAP
299 
300  public:
301   static void run_full_gc_in_vm_thread() NOT_CDS_JAVA_HEAP_RETURN;
302 
303   static bool is_heap_object_archiving_allowed() {
304     CDS_JAVA_HEAP_ONLY(return (UseG1GC && UseCompressedOops && UseCompressedClassPointers);)
305     NOT_CDS_JAVA_HEAP(return false;)
306   }
307 
308   static bool is_heap_region(int idx) {
309     CDS_JAVA_HEAP_ONLY(return (idx >= MetaspaceShared::first_closed_archive_heap_region &&
310                                idx <= MetaspaceShared::last_open_archive_heap_region));
311     NOT_CDS_JAVA_HEAP_RETURN_(false);
312   }
313 
314   static void set_closed_archive_heap_region_mapped() {
315     CDS_JAVA_HEAP_ONLY(_closed_archive_heap_region_mapped = true);
316     NOT_CDS_JAVA_HEAP_RETURN;
317   }
318   static bool closed_archive_heap_region_mapped() {
319     CDS_JAVA_HEAP_ONLY(return _closed_archive_heap_region_mapped);
320     NOT_CDS_JAVA_HEAP_RETURN_(false);
321   }
322   static void set_open_archive_heap_region_mapped() {
323     CDS_JAVA_HEAP_ONLY(_open_archive_heap_region_mapped = true);
324     NOT_CDS_JAVA_HEAP_RETURN;
325   }
326   static bool open_archive_heap_region_mapped() {
327     CDS_JAVA_HEAP_ONLY(return _open_archive_heap_region_mapped);
328     NOT_CDS_JAVA_HEAP_RETURN_(false);
329   }
330 
331   static void fixup_mapped_heap_regions() NOT_CDS_JAVA_HEAP_RETURN;
332 
333   inline static bool is_archived_object(oop p) NOT_CDS_JAVA_HEAP_RETURN_(false);
334 
335   static void initialize_from_archived_subgraph(Klass* k, TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
336 
337   // NarrowOops stored in the CDS archive may use a different encoding scheme
338   // than CompressedOops::{base,shift} -- see FileMapInfo::map_heap_regions_impl.
339   // To decode them, do not use CompressedOops::decode_not_null. Use this
340   // function instead.
341   inline static oop decode_from_archive(narrowOop v) NOT_CDS_JAVA_HEAP_RETURN_(NULL);
342 
343   static void init_narrow_oop_decoding(address base, int shift) NOT_CDS_JAVA_HEAP_RETURN;
344 
345   static void patch_archived_heap_embedded_pointers(MemRegion mem, address  oopmap,
346                                                     size_t oopmap_in_bits) NOT_CDS_JAVA_HEAP_RETURN;
347 
348   static void init_for_dumping(Thread* THREAD) NOT_CDS_JAVA_HEAP_RETURN;
349   static void init_subgraph_entry_fields(Thread* THREAD) NOT_CDS_JAVA_HEAP_RETURN;
350   static void write_subgraph_info_table() NOT_CDS_JAVA_HEAP_RETURN;
351   static void serialize_subgraph_info_table_header(SerializeClosure* soc) NOT_CDS_JAVA_HEAP_RETURN;
352 };
353 
354 #if INCLUDE_CDS_JAVA_HEAP
355 class DumpedInternedStrings :
356   public ResourceHashtable<oop, bool,
357                            HeapShared::string_oop_hash,
358                            HeapShared::oop_equals,
359                            15889, // prime number
360                            ResourceObj::C_HEAP>
361 {};
362 #endif
363 
364 #endif // SHARE_MEMORY_HEAPSHARED_HPP