1 /*
  2  * Copyright (c) 2020, 2021, 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_ARCHIVEBUILDER_HPP
 26 #define SHARE_CDS_ARCHIVEBUILDER_HPP
 27 
 28 #include "cds/archiveUtils.hpp"
 29 #include "cds/dumpAllocStats.hpp"
 30 #include "memory/metaspaceClosure.hpp"
 31 #include "oops/array.hpp"
 32 #include "oops/klass.hpp"
 33 #include "runtime/os.hpp"
 34 #include "utilities/bitMap.hpp"
 35 #include "utilities/growableArray.hpp"
 36 #include "utilities/resizeableResourceHash.hpp"
 37 #include "utilities/resourceHash.hpp"
 38 
 39 struct ArchiveHeapOopmapInfo;
 40 class CHeapBitMap;
 41 class FileMapInfo;
 42 class Klass;
 43 class MemRegion;
 44 class Symbol;
 45 
 46 // Metaspace::allocate() requires that all blocks must be aligned with KlassAlignmentInBytes.
 47 // We enforce the same alignment rule in blocks allocated from the shared space.
 48 const int SharedSpaceObjectAlignment = KlassAlignmentInBytes;
 49 
 50 // Overview of CDS archive creation (for both static and dynamic dump):
 51 //
 52 // [1] Load all classes (static dump: from the classlist, dynamic dump: as part of app execution)
 53 // [2] Allocate "output buffer"
 54 // [3] Copy contents of the 2 "core" regions (rw/ro) into the output buffer.
 55 //       - allocate the cpp vtables in rw (static dump only)
 56 //       - memcpy the MetaspaceObjs into rw/ro:
 57 //         dump_rw_region();
 58 //         dump_ro_region();
 59 //       - fix all the pointers in the MetaspaceObjs to point to the copies
 60 //         relocate_metaspaceobj_embedded_pointers()
 61 // [4] Copy symbol table, dictionary, etc, into the ro region
 62 // [5] Relocate all the pointers in rw/ro, so that the archive can be mapped to
 63 //     the "requested" location without runtime relocation. See relocate_to_requested()
 64 class ArchiveBuilder : public StackObj {
 65 protected:
 66   DumpRegion* _current_dump_space;
 67   address _buffer_bottom;                      // for writing the contents of rw/ro regions
 68   address _last_verified_top;
 69   int _num_dump_regions_used;
 70   size_t _other_region_used_bytes;
 71 
 72   // These are the addresses where we will request the static and dynamic archives to be
 73   // mapped at run time. If the request fails (due to ASLR), we will map the archives at
 74   // os-selected addresses.
 75   address _requested_static_archive_bottom;     // This is determined solely by the value of
 76                                                 // SharedBaseAddress during -Xshare:dump.
 77   address _requested_static_archive_top;
 78   address _requested_dynamic_archive_bottom;    // Used only during dynamic dump. It's placed
 79                                                 // immediately above _requested_static_archive_top.
 80   address _requested_dynamic_archive_top;
 81 
 82   // (Used only during dynamic dump) where the static archive is actually mapped. This
 83   // may be different than _requested_static_archive_{bottom,top} due to ASLR
 84   address _mapped_static_archive_bottom;
 85   address _mapped_static_archive_top;
 86 
 87   intx _buffer_to_requested_delta;
 88 
 89   DumpRegion* current_dump_space() const {  return _current_dump_space;  }
 90 
 91 public:
 92   enum FollowMode {
 93     make_a_copy, point_to_it, set_to_null
 94   };
 95 
 96 private:
 97   class SpecialRefInfo {
 98     // We have a "special pointer" of the given _type at _field_offset of _src_obj.
 99     // See MetaspaceClosure::push_special().
100     MetaspaceClosure::SpecialRef _type;
101     address _src_obj;
102     size_t _field_offset;
103     DEBUG_ONLY(size_t _src_obj_size_in_bytes;)
104 
105   public:
106     SpecialRefInfo() {}
107     SpecialRefInfo(MetaspaceClosure::SpecialRef type, address src_obj, size_t field_offset, size_t src_obj_size_in_bytes)
108       : _type(type), _src_obj(src_obj), _field_offset(field_offset) {
109       DEBUG_ONLY(_src_obj_size_in_bytes = src_obj_size_in_bytes);
110     }
111 
112     MetaspaceClosure::SpecialRef type() const { return _type;         }
113     address src_obj()                   const { return _src_obj;      }
114     size_t field_offset()               const { return _field_offset; }
115 
116     DEBUG_ONLY(size_t src_obj_size_in_bytes() const { return _src_obj_size_in_bytes; })
117   };
118 
119   class SourceObjInfo {
120     MetaspaceClosure::Ref* _ref;
121     uintx _ptrmap_start;     // The bit-offset of the start of this object (inclusive)
122     uintx _ptrmap_end;       // The bit-offset of the end   of this object (exclusive)
123     bool _read_only;
124     FollowMode _follow_mode;
125     int _size_in_bytes;
126     MetaspaceObj::Type _msotype;
127     address _dumped_addr;    // Address this->obj(), as used by the dumped archive.
128     address _orig_obj;       // The value of the original object (_ref->obj()) when this
129                              // SourceObjInfo was created. Note that _ref->obj() may change
130                              // later if _ref is relocated.
131 
132   public:
133     SourceObjInfo(MetaspaceClosure::Ref* ref, bool read_only, FollowMode follow_mode) :
134       _ref(ref), _ptrmap_start(0), _ptrmap_end(0), _read_only(read_only), _follow_mode(follow_mode),
135       _size_in_bytes(ref->size() * BytesPerWord), _msotype(ref->msotype()),
136       _orig_obj(ref->obj()) {
137       if (follow_mode == point_to_it) {
138         _dumped_addr = ref->obj();
139       } else {
140         _dumped_addr = NULL;
141       }
142     }
143 
144     bool should_copy() const { return _follow_mode == make_a_copy; }
145     MetaspaceClosure::Ref* ref() const { return  _ref; }
146     void set_dumped_addr(address dumped_addr)  {
147       assert(should_copy(), "must be");
148       assert(_dumped_addr == NULL, "cannot be copied twice");
149       assert(dumped_addr != NULL, "must be a valid copy");
150       _dumped_addr = dumped_addr;
151     }
152     void set_ptrmap_start(uintx v) { _ptrmap_start = v;    }
153     void set_ptrmap_end(uintx v)   { _ptrmap_end = v;      }
154     uintx ptrmap_start()  const    { return _ptrmap_start; } // inclusive
155     uintx ptrmap_end()    const    { return _ptrmap_end;   } // exclusive
156     bool read_only()      const    { return _read_only;    }
157     int size_in_bytes()   const    { return _size_in_bytes; }
158     address orig_obj()    const    { return _orig_obj; }
159     address dumped_addr() const    { return _dumped_addr; }
160     MetaspaceObj::Type msotype() const { return _msotype; }
161 
162     // convenience accessor
163     address obj() const { return ref()->obj(); }
164   };
165 
166   class SourceObjList {
167     uintx _total_bytes;
168     GrowableArray<SourceObjInfo*>* _objs;     // Source objects to be archived
169     CHeapBitMap _ptrmap;                      // Marks the addresses of the pointer fields
170                                               // in the source objects
171   public:
172     SourceObjList();
173     ~SourceObjList();
174 
175     GrowableArray<SourceObjInfo*>* objs() const { return _objs; }
176 
177     void append(MetaspaceClosure::Ref* enclosing_ref, SourceObjInfo* src_info);
178     void remember_embedded_pointer(SourceObjInfo* pointing_obj, MetaspaceClosure::Ref* ref);
179     void relocate(int i, ArchiveBuilder* builder);
180 
181     // convenience accessor
182     SourceObjInfo* at(int i) const { return objs()->at(i); }
183   };
184 
185   class SrcObjTableCleaner {
186   public:
187     bool do_entry(address key, const SourceObjInfo& value) {
188       delete value.ref();
189       return true;
190     }
191   };
192 
193   class CDSMapLogger;
194 
195   static const int INITIAL_TABLE_SIZE = 15889;
196   static const int MAX_TABLE_SIZE     = 1000000;
197 
198   ReservedSpace _shared_rs;
199   VirtualSpace _shared_vs;
200 
201   DumpRegion _rw_region;
202   DumpRegion _ro_region;
203   CHeapBitMap _ptrmap;    // bitmap used by ArchivePtrMarker
204 
205   SourceObjList _rw_src_objs;                 // objs to put in rw region
206   SourceObjList _ro_src_objs;                 // objs to put in ro region
207   ResizeableResourceHashtable<address, SourceObjInfo, ResourceObj::C_HEAP, mtClassShared> _src_obj_table;
208   GrowableArray<Klass*>* _klasses;
209   GrowableArray<Symbol*>* _symbols;
210   GrowableArray<SpecialRefInfo>* _special_refs;
211 
212   // statistics
213   DumpAllocStats _alloc_stats;
214   size_t _total_closed_heap_region_size;
215   size_t _total_open_heap_region_size;
216 
217   void print_region_stats(FileMapInfo *map_info,
218                           GrowableArray<MemRegion>* closed_heap_regions,
219                           GrowableArray<MemRegion>* open_heap_regions);
220   void print_bitmap_region_stats(size_t size, size_t total_size);
221   void print_heap_region_stats(GrowableArray<MemRegion>* regions,
222                                const char *name, size_t total_size);
223 
224   // For global access.
225   static ArchiveBuilder* _current;
226 
227 public:
228   // Use this when you allocate space outside of ArchiveBuilder::dump_{rw,ro}_region.
229   // These are usually for misc tables that are allocated in the RO space.
230   class OtherROAllocMark {
231     char* _oldtop;
232   public:
233     OtherROAllocMark() {
234       _oldtop = _current->_ro_region.top();
235     }
236     ~OtherROAllocMark();
237   };
238 
239 private:
240   bool is_dumping_full_module_graph();
241   FollowMode get_follow_mode(MetaspaceClosure::Ref *ref);
242 
243   void iterate_sorted_roots(MetaspaceClosure* it, bool is_relocating_pointers);
244   void sort_symbols_and_fix_hash();
245   void sort_klasses();
246   static int compare_symbols_by_address(Symbol** a, Symbol** b);
247   static int compare_klass_by_name(Klass** a, Klass** b);
248 
249   void make_shallow_copies(DumpRegion *dump_region, const SourceObjList* src_objs);
250   void make_shallow_copy(DumpRegion *dump_region, SourceObjInfo* src_info);
251 
252   void update_special_refs();
253   void relocate_embedded_pointers(SourceObjList* src_objs);
254 
255   bool is_excluded(Klass* k);
256   void clean_up_src_obj_table();
257 
258 protected:
259   virtual void iterate_roots(MetaspaceClosure* it, bool is_relocating_pointers) = 0;
260 
261   // Conservative estimate for number of bytes needed for:
262   size_t _estimated_metaspaceobj_bytes;   // all archived MetaspaceObj's.
263   size_t _estimated_hashtable_bytes;     // symbol table and dictionaries
264 
265   static const int _total_dump_regions = 2;
266 
267   size_t estimate_archive_size();
268 
269   void start_dump_space(DumpRegion* next);
270   void verify_estimate_size(size_t estimate, const char* which);
271 
272 public:
273   address reserve_buffer();
274 
275   address buffer_bottom()                    const { return _buffer_bottom;                       }
276   address buffer_top()                       const { return (address)current_dump_space()->top(); }
277   address requested_static_archive_bottom()  const { return  _requested_static_archive_bottom;    }
278   address mapped_static_archive_bottom()     const { return  _mapped_static_archive_bottom;       }
279   intx buffer_to_requested_delta()           const { return _buffer_to_requested_delta;           }
280 
281   bool is_in_buffer_space(address p) const {
282     return (buffer_bottom() <= p && p < buffer_top());
283   }
284 
285   template <typename T> bool is_in_requested_static_archive(T p) const {
286     return _requested_static_archive_bottom <= (address)p && (address)p < _requested_static_archive_top;
287   }
288 
289   template <typename T> bool is_in_mapped_static_archive(T p) const {
290     return _mapped_static_archive_bottom <= (address)p && (address)p < _mapped_static_archive_top;
291   }
292 
293   template <typename T> bool is_in_buffer_space(T obj) const {
294     return is_in_buffer_space(address(obj));
295   }
296 
297   template <typename T> T to_requested(T obj) const {
298     assert(is_in_buffer_space(obj), "must be");
299     return (T)(address(obj) + _buffer_to_requested_delta);
300   }
301 
302   static intx get_buffer_to_requested_delta() {
303     return current()->buffer_to_requested_delta();
304   }
305 
306 public:
307   static const uintx MAX_SHARED_DELTA = 0x7FFFFFFF;
308 
309   // The address p points to an object inside the output buffer. When the archive is mapped
310   // at the requested address, what's the offset of this object from _requested_static_archive_bottom?
311   uintx buffer_to_offset(address p) const;
312 
313   // Same as buffer_to_offset, except that the address p points to either (a) an object
314   // inside the output buffer, or (b), an object in the currently mapped static archive.
315   uintx any_to_offset(address p) const;
316 
317   template <typename T>
318   u4 buffer_to_offset_u4(T p) const {
319     uintx offset = buffer_to_offset((address)p);
320     guarantee(offset <= MAX_SHARED_DELTA, "must be 32-bit offset");
321     return (u4)offset;
322   }
323 
324   template <typename T>
325   u4 any_to_offset_u4(T p) const {
326     uintx offset = any_to_offset((address)p);
327     guarantee(offset <= MAX_SHARED_DELTA, "must be 32-bit offset");
328     return (u4)offset;
329   }
330 
331   static void assert_is_vm_thread() PRODUCT_RETURN;
332 
333 public:
334   ArchiveBuilder();
335   ~ArchiveBuilder();
336 
337   void gather_klasses_and_symbols();
338   void gather_source_objs();
339   bool gather_klass_and_symbol(MetaspaceClosure::Ref* ref, bool read_only);
340   bool gather_one_source_obj(MetaspaceClosure::Ref* enclosing_ref, MetaspaceClosure::Ref* ref, bool read_only);
341   void add_special_ref(MetaspaceClosure::SpecialRef type, address src_obj, size_t field_offset, size_t src_obj_size_in_bytes) {
342     _special_refs->append(SpecialRefInfo(type, src_obj, field_offset, src_obj_size_in_bytes));
343   }
344   void remember_embedded_pointer_in_copied_obj(MetaspaceClosure::Ref* enclosing_ref, MetaspaceClosure::Ref* ref);
345 
346   DumpRegion* rw_region() { return &_rw_region; }
347   DumpRegion* ro_region() { return &_ro_region; }
348 
349   static char* rw_region_alloc(size_t num_bytes) {
350     return current()->rw_region()->allocate(num_bytes);
351   }
352   static char* ro_region_alloc(size_t num_bytes) {
353     return current()->ro_region()->allocate(num_bytes);
354   }
355 
356   template <typename T>
357   static Array<T>* new_ro_array(int length) {
358     size_t byte_size = Array<T>::byte_sizeof(length, sizeof(T));
359     Array<T>* array = (Array<T>*)ro_region_alloc(byte_size);
360     array->initialize(length);
361     return array;
362   }
363 
364   template <typename T>
365   static Array<T>* new_rw_array(int length) {
366     size_t byte_size = Array<T>::byte_sizeof(length, sizeof(T));
367     Array<T>* array = (Array<T>*)rw_region_alloc(byte_size);
368     array->initialize(length);
369     return array;
370   }
371 
372   template <typename T>
373   static size_t ro_array_bytesize(int length) {
374     size_t byte_size = Array<T>::byte_sizeof(length, sizeof(T));
375     return align_up(byte_size, SharedSpaceObjectAlignment);
376   }
377 
378   void dump_rw_metadata();
379   void dump_ro_metadata();
380   void relocate_metaspaceobj_embedded_pointers();
381   void relocate_roots();
382   void relocate_vm_classes();
383   void make_klasses_shareable();
384   void relocate_to_requested();
385   void write_archive(FileMapInfo* mapinfo,
386                      GrowableArray<MemRegion>* closed_heap_regions,
387                      GrowableArray<MemRegion>* open_heap_regions,
388                      GrowableArray<ArchiveHeapOopmapInfo>* closed_heap_oopmaps,
389                      GrowableArray<ArchiveHeapOopmapInfo>* open_heap_oopmaps);
390   void write_region(FileMapInfo* mapinfo, int region_idx, DumpRegion* dump_region,
391                     bool read_only,  bool allow_exec);
392 
393   address get_dumped_addr(address src_obj) const;
394 
395   // All klasses and symbols that will be copied into the archive
396   GrowableArray<Klass*>*  klasses() const { return _klasses; }
397   GrowableArray<Symbol*>* symbols() const { return _symbols; }
398 
399   static bool is_active() {
400     return (_current != NULL);
401   }
402 
403   static ArchiveBuilder* current() {
404     assert_is_vm_thread();
405     assert(_current != NULL, "ArchiveBuilder must be active");
406     return _current;
407   }
408 
409   static DumpAllocStats* alloc_stats() {
410     return &(current()->_alloc_stats);
411   }
412 
413   static CompactHashtableStats* symbol_stats() {
414     return alloc_stats()->symbol_stats();
415   }
416 
417   static CompactHashtableStats* string_stats() {
418     return alloc_stats()->string_stats();
419   }
420 
421   void relocate_klass_ptr(oop o);
422 
423   static Klass* get_relocated_klass(Klass* orig_klass) {
424     Klass* klass = (Klass*)current()->get_dumped_addr((address)orig_klass);
425     assert(klass != NULL && klass->is_klass(), "must be");
426     return klass;
427   }
428 
429   static Symbol* get_relocated_symbol(Symbol* orig_symbol) {
430     return (Symbol*)current()->get_dumped_addr((address)orig_symbol);
431   }
432 
433   void print_stats();
434   void report_out_of_space(const char* name, size_t needed_bytes);
435 };
436 
437 #endif // SHARE_CDS_ARCHIVEBUILDER_HPP