1 /*
  2  * Copyright (c) 2020, 2024, 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/metaspace.hpp"
 31 #include "memory/metaspaceClosure.hpp"
 32 #include "oops/array.hpp"
 33 #include "oops/klass.hpp"
 34 #include "runtime/os.hpp"
 35 #include "utilities/bitMap.hpp"
 36 #include "utilities/growableArray.hpp"
 37 #include "utilities/resizeableResourceHash.hpp"
 38 #include "utilities/resourceHash.hpp"
 39 
 40 class ArchiveHeapInfo;
 41 class CHeapBitMap;
 42 class FileMapInfo;
 43 class Klass;
 44 class MemRegion;
 45 class Symbol;
 46 
 47 // The minimum alignment for non-Klass objects inside the CDS archive. Klass objects need
 48 // to follow CompressedKlassPointers::klass_alignment_in_bytes().
 49 constexpr size_t SharedSpaceObjectAlignment = Metaspace::min_allocation_alignment_bytes;
 50 
 51 // Overview of CDS archive creation (for both static and dynamic dump):
 52 //
 53 // [1] Load all classes (static dump: from the classlist, dynamic dump: as part of app execution)
 54 // [2] Allocate "output buffer"
 55 // [3] Copy contents of the 2 "core" regions (rw/ro) into the output buffer.
 56 //       - allocate the cpp vtables in rw (static dump only)
 57 //       - memcpy the MetaspaceObjs into rw/ro:
 58 //         dump_rw_region();
 59 //         dump_ro_region();
 60 //       - fix all the pointers in the MetaspaceObjs to point to the copies
 61 //         relocate_metaspaceobj_embedded_pointers()
 62 // [4] Copy symbol table, dictionary, etc, into the ro region
 63 // [5] Relocate all the pointers in rw/ro, so that the archive can be mapped to
 64 //     the "requested" location without runtime relocation. See relocate_to_requested()
 65 //
 66 // "source" vs "buffered" vs "requested"
 67 //
 68 // The ArchiveBuilder deals with three types of addresses.
 69 //
 70 // "source":    These are the addresses of objects created in step [1] above. They are the actual
 71 //              InstanceKlass*, Method*, etc, of the Java classes that are loaded for executing
 72 //              Java bytecodes in the JVM process that's dumping the CDS archive.
 73 //
 74 //              It may be necessary to contiue Java execution after ArchiveBuilder is finished.
 75 //              Therefore, we don't modify any of the "source" objects.
 76 //
 77 // "buffered":  The "source" objects that are deemed archivable are copied into a temporary buffer.
 78 //              Objects in the buffer are modified in steps [2, 3, 4] (e.g., unshareable info is
 79 //              removed, pointers are relocated, etc) to prepare them to be loaded at runtime.
 80 //
 81 // "requested": These are the addreses where the "buffered" objects should be loaded at runtime.
 82 //              When the "buffered" objects are written into the archive file, their addresses
 83 //              are adjusted in step [5] such that the lowest of these objects would be mapped
 84 //              at SharedBaseAddress.
 85 //
 86 // Translation between "source" and "buffered" addresses is done with two hashtables:
 87 //     _src_obj_table          : "source"   -> "buffered"
 88 //     _buffered_to_src_table  : "buffered" -> "source"
 89 //
 90 // Translation between "buffered" and "requested" addresses is done with a simple shift:
 91 //    buffered_address + _buffer_to_requested_delta == requested_address
 92 //
 93 class ArchiveBuilder : public StackObj {
 94 protected:
 95   DumpRegion* _current_dump_space;
 96   address _buffer_bottom;                      // for writing the contents of rw/ro regions
 97   address _last_verified_top;
 98   int _num_dump_regions_used;
 99   size_t _other_region_used_bytes;
100 
101   // These are the addresses where we will request the static and dynamic archives to be
102   // mapped at run time. If the request fails (due to ASLR), we will map the archives at
103   // os-selected addresses.
104   address _requested_static_archive_bottom;     // This is determined solely by the value of
105                                                 // SharedBaseAddress during -Xshare:dump.
106   address _requested_static_archive_top;
107   address _requested_dynamic_archive_bottom;    // Used only during dynamic dump. It's placed
108                                                 // immediately above _requested_static_archive_top.
109   address _requested_dynamic_archive_top;
110 
111   // (Used only during dynamic dump) where the static archive is actually mapped. This
112   // may be different than _requested_static_archive_{bottom,top} due to ASLR
113   address _mapped_static_archive_bottom;
114   address _mapped_static_archive_top;
115 
116   intx _buffer_to_requested_delta;
117 
118   DumpRegion* current_dump_space() const {  return _current_dump_space;  }
119 
120 public:
121   enum FollowMode {
122     make_a_copy, point_to_it, set_to_null
123   };
124 
125 private:
126   class SourceObjInfo {
127     uintx _ptrmap_start;     // The bit-offset of the start of this object (inclusive)
128     uintx _ptrmap_end;       // The bit-offset of the end   of this object (exclusive)
129     bool _read_only;
130     FollowMode _follow_mode;
131     int _size_in_bytes;
132     MetaspaceObj::Type _msotype;
133     address _source_addr;    // The source object to be copied.
134     address _buffered_addr;  // The copy of this object insider the buffer.
135   public:
136     SourceObjInfo(MetaspaceClosure::Ref* ref, bool read_only, FollowMode follow_mode) :
137       _ptrmap_start(0), _ptrmap_end(0), _read_only(read_only), _follow_mode(follow_mode),
138       _size_in_bytes(ref->size() * BytesPerWord), _msotype(ref->msotype()),
139       _source_addr(ref->obj()) {
140       if (follow_mode == point_to_it) {
141         _buffered_addr = ref->obj();
142       } else {
143         _buffered_addr = nullptr;
144       }
145     }
146 
147     // This constructor is only used for regenerated objects (created by LambdaFormInvokers, etc).
148     //   src = address of a Method or InstanceKlass that has been regenerated.
149     //   renegerated_obj_info = info for the regenerated version of src.
150     SourceObjInfo(address src, SourceObjInfo* renegerated_obj_info) :
151       _ptrmap_start(0), _ptrmap_end(0), _read_only(false),
152       _follow_mode(renegerated_obj_info->_follow_mode),
153       _size_in_bytes(0), _msotype(renegerated_obj_info->_msotype),
154       _source_addr(src),  _buffered_addr(renegerated_obj_info->_buffered_addr) {}
155 
156     bool should_copy() const { return _follow_mode == make_a_copy; }
157     void set_buffered_addr(address addr)  {
158       assert(should_copy(), "must be");
159       assert(_buffered_addr == nullptr, "cannot be copied twice");
160       assert(addr != nullptr, "must be a valid copy");
161       _buffered_addr = addr;
162     }
163     void set_ptrmap_start(uintx v) { _ptrmap_start = v;    }
164     void set_ptrmap_end(uintx v)   { _ptrmap_end = v;      }
165     uintx ptrmap_start()  const    { return _ptrmap_start; } // inclusive
166     uintx ptrmap_end()    const    { return _ptrmap_end;   } // exclusive
167     bool read_only()      const    { return _read_only;    }
168     int size_in_bytes()   const    { return _size_in_bytes; }
169     address source_addr() const    { return _source_addr; }
170     address buffered_addr() const  {
171       if (_follow_mode != set_to_null) {
172         assert(_buffered_addr != nullptr, "must be initialized");
173       }
174       return _buffered_addr;
175     }
176     MetaspaceObj::Type msotype() const { return _msotype; }
177   };
178 
179   class SourceObjList {
180     uintx _total_bytes;
181     GrowableArray<SourceObjInfo*>* _objs;     // Source objects to be archived
182     CHeapBitMap _ptrmap;                      // Marks the addresses of the pointer fields
183                                               // in the source objects
184   public:
185     SourceObjList();
186     ~SourceObjList();
187 
188     GrowableArray<SourceObjInfo*>* objs() const { return _objs; }
189 
190     void append(SourceObjInfo* src_info);
191     void remember_embedded_pointer(SourceObjInfo* pointing_obj, MetaspaceClosure::Ref* ref);
192     void relocate(int i, ArchiveBuilder* builder);
193 
194     // convenience accessor
195     SourceObjInfo* at(int i) const { return objs()->at(i); }
196   };
197 
198   class CDSMapLogger;
199 
200   static const int INITIAL_TABLE_SIZE = 15889;
201   static const int MAX_TABLE_SIZE     = 1000000;
202 
203   ReservedSpace _shared_rs;
204   VirtualSpace _shared_vs;
205 
206   DumpRegion _rw_region;
207   DumpRegion _ro_region;
208 
209   // Combined bitmap to track pointers in both RW and RO regions. This is updated
210   // as objects are copied into RW and RO.
211   CHeapBitMap _ptrmap;
212 
213   // _ptrmap is split into these two bitmaps which are written into the archive.
214   CHeapBitMap _rw_ptrmap;   // marks pointers in the RW region
215   CHeapBitMap _ro_ptrmap;   // marks pointers in the RO region
216 
217   SourceObjList _rw_src_objs;                 // objs to put in rw region
218   SourceObjList _ro_src_objs;                 // objs to put in ro region
219   ResizeableResourceHashtable<address, SourceObjInfo, AnyObj::C_HEAP, mtClassShared> _src_obj_table;
220   ResizeableResourceHashtable<address, address, AnyObj::C_HEAP, mtClassShared> _buffered_to_src_table;
221   GrowableArray<Klass*>* _klasses;
222   GrowableArray<Symbol*>* _symbols;
223 
224   // statistics
225   DumpAllocStats _alloc_stats;
226   size_t _total_heap_region_size;
227 
228   void print_region_stats(FileMapInfo *map_info, ArchiveHeapInfo* heap_info);
229   void print_bitmap_region_stats(size_t size, size_t total_size);
230   void print_heap_region_stats(ArchiveHeapInfo* heap_info, size_t total_size);
231 
232   // For global access.
233   static ArchiveBuilder* _current;
234 
235 public:
236   // Use this when you allocate space outside of ArchiveBuilder::dump_{rw,ro}_region.
237   // These are usually for misc tables that are allocated in the RO space.
238   class OtherROAllocMark {
239     char* _oldtop;
240   public:
241     OtherROAllocMark() {
242       _oldtop = _current->_ro_region.top();
243     }
244     ~OtherROAllocMark();
245   };
246 
247 private:
248   FollowMode get_follow_mode(MetaspaceClosure::Ref *ref);
249 
250   void iterate_sorted_roots(MetaspaceClosure* it);
251   void sort_klasses();
252   static int compare_symbols_by_address(Symbol** a, Symbol** b);
253   static int compare_klass_by_name(Klass** a, Klass** b);
254 
255   void make_shallow_copies(DumpRegion *dump_region, const SourceObjList* src_objs);
256   void make_shallow_copy(DumpRegion *dump_region, SourceObjInfo* src_info);
257 
258   void relocate_embedded_pointers(SourceObjList* src_objs);
259 
260   bool is_excluded(Klass* k);
261   void clean_up_src_obj_table();
262 
263 protected:
264   virtual void iterate_roots(MetaspaceClosure* it) = 0;
265 
266   // Conservative estimate for number of bytes needed for:
267   size_t _estimated_metaspaceobj_bytes;   // all archived MetaspaceObj's.
268   size_t _estimated_hashtable_bytes;     // symbol table and dictionaries
269 
270   static const int _total_dump_regions = 2;
271 
272   size_t estimate_archive_size();
273 
274   void start_dump_space(DumpRegion* next);
275   void verify_estimate_size(size_t estimate, const char* which);
276 
277 public:
278   address reserve_buffer();
279 
280   address buffer_bottom()                    const { return _buffer_bottom;                       }
281   address buffer_top()                       const { return (address)current_dump_space()->top(); }
282   address requested_static_archive_bottom()  const { return  _requested_static_archive_bottom;    }
283   address mapped_static_archive_bottom()     const { return  _mapped_static_archive_bottom;       }
284   intx buffer_to_requested_delta()           const { return _buffer_to_requested_delta;           }
285 
286   bool is_in_buffer_space(address p) const {
287     return (buffer_bottom() <= p && p < buffer_top());
288   }
289 
290   template <typename T> bool is_in_requested_static_archive(T p) const {
291     return _requested_static_archive_bottom <= (address)p && (address)p < _requested_static_archive_top;
292   }
293 
294   template <typename T> bool is_in_mapped_static_archive(T p) const {
295     return _mapped_static_archive_bottom <= (address)p && (address)p < _mapped_static_archive_top;
296   }
297 
298   template <typename T> bool is_in_buffer_space(T obj) const {
299     return is_in_buffer_space(address(obj));
300   }
301 
302   template <typename T> T to_requested(T obj) const {
303     assert(is_in_buffer_space(obj), "must be");
304     return (T)(address(obj) + _buffer_to_requested_delta);
305   }
306 
307   static intx get_buffer_to_requested_delta() {
308     return current()->buffer_to_requested_delta();
309   }
310 
311   inline static u4 to_offset_u4(uintx offset) {
312     guarantee(offset <= MAX_SHARED_DELTA, "must be 32-bit offset " INTPTR_FORMAT, offset);
313     return (u4)offset;
314   }
315 
316 public:
317   static const uintx MAX_SHARED_DELTA = 0x7FFFFFFF;
318 
319   // The address p points to an object inside the output buffer. When the archive is mapped
320   // at the requested address, what's the offset of this object from _requested_static_archive_bottom?
321   uintx buffer_to_offset(address p) const;
322 
323   // Same as buffer_to_offset, except that the address p points to either (a) an object
324   // inside the output buffer, or (b), an object in the currently mapped static archive.
325   uintx any_to_offset(address p) const;
326 
327   template <typename T>
328   u4 buffer_to_offset_u4(T p) const {
329     uintx offset = buffer_to_offset((address)p);
330     return to_offset_u4(offset);
331   }
332 
333   template <typename T>
334   u4 any_to_offset_u4(T p) const {
335     uintx offset = any_to_offset((address)p);
336     return to_offset_u4(offset);
337   }
338 
339   static void assert_is_vm_thread() PRODUCT_RETURN;
340 
341 public:
342   ArchiveBuilder();
343   ~ArchiveBuilder();
344 
345   void gather_klasses_and_symbols();
346   void gather_source_objs();
347   bool gather_klass_and_symbol(MetaspaceClosure::Ref* ref, bool read_only);
348   bool gather_one_source_obj(MetaspaceClosure::Ref* ref, bool read_only);
349   void remember_embedded_pointer_in_enclosing_obj(MetaspaceClosure::Ref* ref);
350   static void serialize_dynamic_archivable_items(SerializeClosure* soc);
351 
352   DumpRegion* rw_region() { return &_rw_region; }
353   DumpRegion* ro_region() { return &_ro_region; }
354 
355   static char* rw_region_alloc(size_t num_bytes) {
356     return current()->rw_region()->allocate(num_bytes);
357   }
358   static char* ro_region_alloc(size_t num_bytes) {
359     return current()->ro_region()->allocate(num_bytes);
360   }
361 
362   template <typename T>
363   static Array<T>* new_ro_array(int length) {
364     size_t byte_size = Array<T>::byte_sizeof(length, sizeof(T));
365     Array<T>* array = (Array<T>*)ro_region_alloc(byte_size);
366     array->initialize(length);
367     return array;
368   }
369 
370   template <typename T>
371   static Array<T>* new_rw_array(int length) {
372     size_t byte_size = Array<T>::byte_sizeof(length, sizeof(T));
373     Array<T>* array = (Array<T>*)rw_region_alloc(byte_size);
374     array->initialize(length);
375     return array;
376   }
377 
378   template <typename T>
379   static size_t ro_array_bytesize(int length) {
380     size_t byte_size = Array<T>::byte_sizeof(length, sizeof(T));
381     return align_up(byte_size, SharedSpaceObjectAlignment);
382   }
383 
384   char* ro_strdup(const char* s);
385 
386   void dump_rw_metadata();
387   void dump_ro_metadata();
388   void relocate_metaspaceobj_embedded_pointers();
389   void record_regenerated_object(address orig_src_obj, address regen_src_obj);
390   void make_klasses_shareable();
391   void relocate_to_requested();
392   void write_archive(FileMapInfo* mapinfo, ArchiveHeapInfo* heap_info);
393   void write_region(FileMapInfo* mapinfo, int region_idx, DumpRegion* dump_region,
394                     bool read_only,  bool allow_exec);
395 
396   void write_pointer_in_buffer(address* ptr_location, address src_addr);
397   template <typename T> void write_pointer_in_buffer(T* ptr_location, T src_addr) {
398     write_pointer_in_buffer((address*)ptr_location, (address)src_addr);
399   }
400 
401   address get_buffered_addr(address src_addr) const;
402   template <typename T> T get_buffered_addr(T src_addr) const {
403     return (T)get_buffered_addr((address)src_addr);
404   }
405 
406   address get_source_addr(address buffered_addr) const;
407   template <typename T> T get_source_addr(T buffered_addr) const {
408     return (T)get_source_addr((address)buffered_addr);
409   }
410 
411   // All klasses and symbols that will be copied into the archive
412   GrowableArray<Klass*>*  klasses() const { return _klasses; }
413   GrowableArray<Symbol*>* symbols() const { return _symbols; }
414 
415   static bool is_active() {
416     return (_current != nullptr);
417   }
418 
419   static ArchiveBuilder* current() {
420     assert_is_vm_thread();
421     assert(_current != nullptr, "ArchiveBuilder must be active");
422     return _current;
423   }
424 
425   static DumpAllocStats* alloc_stats() {
426     return &(current()->_alloc_stats);
427   }
428 
429   static CompactHashtableStats* symbol_stats() {
430     return alloc_stats()->symbol_stats();
431   }
432 
433   static CompactHashtableStats* string_stats() {
434     return alloc_stats()->string_stats();
435   }
436 
437   narrowKlass get_requested_narrow_klass(Klass* k);
438 
439   static Klass* get_buffered_klass(Klass* src_klass) {
440     Klass* klass = (Klass*)current()->get_buffered_addr((address)src_klass);
441     assert(klass != nullptr && klass->is_klass(), "must be");
442     return klass;
443   }
444 
445   static Symbol* get_buffered_symbol(Symbol* src_symbol) {
446     return (Symbol*)current()->get_buffered_addr((address)src_symbol);
447   }
448 
449   void print_stats();
450   void report_out_of_space(const char* name, size_t needed_bytes);
451 
452 #ifdef _LP64
453   // Archived heap object headers (and soon, with Lilliput, markword prototypes) carry pre-computed
454   // narrow Klass ids calculated with the following scheme:
455   // 1) the encoding base must be the mapping start address.
456   // 2) shift must be large enough to result in an encoding range that covers the runtime Klass range.
457   //    That Klass range is defined by CDS archive size and runtime class space size. Luckily, the maximum
458   //    size can be predicted: archive size is assumed to be <1G, class space size capped at 3G, and at
459   //    runtime we put both regions adjacent to each other. Therefore, runtime Klass range size < 4G.
460   // The value of this precomputed shift depends on the class pointer mode at dump time.
461   // Legacy Mode:
462   //    Since nKlass itself is 32 bit, our encoding range len is 4G, and since we set the base directly
463   //    at mapping start, these 4G are enough. Therefore, we don't need to shift at all (shift=0).
464   // TinyClassPointer Mode:
465   //    To cover the 4G, we need the highest possible shift value. That may change in the future, if
466   //    we decide to correct the pre-calculated narrow Klass IDs at load time.
467   static int precomputed_narrow_klass_shift();
468 #endif // _LP64
469 
470 };
471 
472 #endif // SHARE_CDS_ARCHIVEBUILDER_HPP