1 /*
  2  * Copyright (c) 2019, 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_ARCHIVEUTILS_HPP
 26 #define SHARE_CDS_ARCHIVEUTILS_HPP
 27 
 28 #include "cds/cds_globals.hpp"
 29 #include "cds/serializeClosure.hpp"
 30 #include "logging/log.hpp"
 31 #include "memory/metaspace.hpp"
 32 #include "memory/virtualspace.hpp"
 33 #include "utilities/bitMap.hpp"
 34 #include "utilities/exceptions.hpp"
 35 #include "utilities/macros.hpp"
 36 #include "runtime/nonJavaThread.hpp"
 37 #include "runtime/semaphore.hpp"
 38 
 39 class BootstrapInfo;
 40 class ReservedSpace;
 41 class VirtualSpace;
 42 
 43 template<class E> class Array;
 44 template<class E> class GrowableArray;
 45 
 46 // ArchivePtrMarker is used to mark the location of pointers embedded in a CDS archive. E.g., when an
 47 // InstanceKlass k is dumped, we mark the location of the k->_name pointer by effectively calling
 48 // mark_pointer(/*ptr_loc=*/&k->_name). It's required that (_prt_base <= ptr_loc < _ptr_end). _ptr_base is
 49 // fixed, but _ptr_end can be expanded as more objects are dumped.
 50 class ArchivePtrMarker : AllStatic {
 51   static CHeapBitMap*  _ptrmap;
 52   static CHeapBitMap*  _rw_ptrmap;
 53   static CHeapBitMap*  _ro_ptrmap;
 54   static CHeapBitMap*  _ac_ptrmap;
 55   static VirtualSpace* _vs;
 56 
 57   // Once _ptrmap is compacted, we don't allow bit marking anymore. This is to
 58   // avoid unintentional copy operations after the bitmap has been finalized and written.
 59   static bool         _compacted;
 60 
 61   static address* ptr_base() { return (address*)_vs->low();  } // committed lower bound (inclusive)
 62   static address* ptr_end()  { return (address*)_vs->high(); } // committed upper bound (exclusive)
 63 
 64 public:
 65   static void initialize(CHeapBitMap* ptrmap, VirtualSpace* vs);
 66   static void initialize_rw_ro_ac_maps(CHeapBitMap* rw_ptrmap, CHeapBitMap* ro_ptrmap, CHeapBitMap* ac_ptrmap);
 67   static void mark_pointer(address* ptr_loc);
 68   static void clear_pointer(address* ptr_loc);
 69   static void compact(address relocatable_base, address relocatable_end);
 70   static void compact(size_t max_non_null_offset);
 71 
 72   template <typename T>
 73   static void mark_pointer(T* ptr_loc) {
 74     mark_pointer((address*)ptr_loc);
 75   }
 76 
 77   template <typename T>
 78   static void set_and_mark_pointer(T* ptr_loc, T ptr_value) {
 79     *ptr_loc = ptr_value;
 80     mark_pointer(ptr_loc);
 81   }
 82 
 83   static CHeapBitMap* ptrmap() {
 84     return _ptrmap;
 85   }
 86 
 87   static CHeapBitMap* rw_ptrmap() {
 88     return _rw_ptrmap;
 89   }
 90 
 91   static CHeapBitMap* ro_ptrmap() {
 92     return _ro_ptrmap;
 93   }
 94 
 95   static CHeapBitMap* ac_ptrmap() {
 96     return _ac_ptrmap;
 97   }
 98 
 99   static void reset_map_and_vs() {
100     _ptrmap = nullptr;
101     _rw_ptrmap = nullptr;
102     _ro_ptrmap = nullptr;
103     _ac_ptrmap = nullptr;
104     _vs = nullptr;
105   }
106 };
107 
108 // SharedDataRelocator is used to shift pointers in the CDS archive.
109 //
110 // The CDS archive is basically a contiguous block of memory (divided into several regions)
111 // that contains multiple objects. The objects may contain direct pointers that point to other objects
112 // within the archive (e.g., InstanceKlass::_name points to a Symbol in the archive). During dumping, we
113 // built a bitmap that marks the locations of all these pointers (using ArchivePtrMarker, see comments above).
114 //
115 // The contents of the archive assumes that it's mapped at the default SharedBaseAddress (e.g. 0x800000000).
116 // If the archive ends up being mapped at a different address (e.g. 0x810000000), SharedDataRelocator
117 // is used to shift each marked pointer by a delta (0x10000000 in this example), so that it points to
118 // the actually mapped location of the target object.
119 class SharedDataRelocator: public BitMapClosure {
120   // for all (address** p), where (is_marked(p) && _patch_base <= p && p < _patch_end) { *p += delta; }
121 
122   // Patch all pointers within this region that are marked.
123   address* _patch_base;
124   address* _patch_end;
125 
126   // Before patching, all pointers must point to this region.
127   address _valid_old_base;
128   address _valid_old_end;
129 
130   // After patching, all pointers must point to this region.
131   address _valid_new_base;
132   address _valid_new_end;
133 
134   // How much to relocate for each pointer.
135   intx _delta;
136 
137  public:
138   SharedDataRelocator(address* patch_base, address* patch_end,
139                       address valid_old_base, address valid_old_end,
140                       address valid_new_base, address valid_new_end, intx delta) :
141     _patch_base(patch_base), _patch_end(patch_end),
142     _valid_old_base(valid_old_base), _valid_old_end(valid_old_end),
143     _valid_new_base(valid_new_base), _valid_new_end(valid_new_end),
144     _delta(delta) {
145     log_debug(aot, reloc)("SharedDataRelocator::_patch_base     = " PTR_FORMAT, p2i(_patch_base));
146     log_debug(aot, reloc)("SharedDataRelocator::_patch_end      = " PTR_FORMAT, p2i(_patch_end));
147     log_debug(aot, reloc)("SharedDataRelocator::_valid_old_base = " PTR_FORMAT, p2i(_valid_old_base));
148     log_debug(aot, reloc)("SharedDataRelocator::_valid_old_end  = " PTR_FORMAT, p2i(_valid_old_end));
149     log_debug(aot, reloc)("SharedDataRelocator::_valid_new_base = " PTR_FORMAT, p2i(_valid_new_base));
150     log_debug(aot, reloc)("SharedDataRelocator::_valid_new_end  = " PTR_FORMAT, p2i(_valid_new_end));
151   }
152 
153   bool do_bit(size_t offset);
154 };
155 
156 class DumpRegion {
157 private:
158   const char* _name;
159   char* _base;
160   char* _top;
161   char* _end;
162   uintx _max_delta;
163   bool _is_packed;
164   ReservedSpace* _rs;
165   VirtualSpace* _vs;
166 
167   void commit_to(char* newtop);
168 
169 public:
170   DumpRegion(const char* name, uintx max_delta = 0)
171     : _name(name), _base(nullptr), _top(nullptr), _end(nullptr),
172       _max_delta(max_delta), _is_packed(false),
173       _rs(nullptr), _vs(nullptr) {}
174 
175   char* expand_top_to(char* newtop);
176   char* allocate(size_t num_bytes, size_t alignment = 0);
177 
178   void append_intptr_t(intptr_t n, bool need_to_mark = false) NOT_CDS_RETURN;
179 
180   char* base()      const { return _base;        }
181   char* top()       const { return _top;         }
182   char* end()       const { return _end;         }
183   size_t reserved() const { return _end - _base; }
184   size_t used()     const { return _top - _base; }
185   bool is_packed()  const { return _is_packed;   }
186   bool is_allocatable() const {
187     return !is_packed() && _base != nullptr;
188   }
189   bool is_empty()   const { return _base == _top; }
190 
191   void print(size_t total_bytes) const;
192   void print_out_of_space_msg(const char* failing_region, size_t needed_bytes);
193 
194   void init(ReservedSpace* rs, VirtualSpace* vs);
195 
196   void pack(DumpRegion* next = nullptr);
197 
198   bool contains(char* p) {
199     return base() <= p && p < top();
200   }
201 };
202 
203 // Closure for serializing initialization data out to a data area to be
204 // written to the shared file.
205 
206 class WriteClosure : public SerializeClosure {
207 private:
208   DumpRegion* _dump_region;
209 
210 public:
211   WriteClosure(DumpRegion* r) {
212     _dump_region = r;
213   }
214 
215   void do_ptr(void** p);
216 
217   void do_u4(u4* p) {
218     _dump_region->append_intptr_t((intptr_t)(*p));
219   }
220 
221   void do_int(int* p) {
222     _dump_region->append_intptr_t((intptr_t)(*p));
223   }
224 
225   void do_bool(bool *p) {
226     _dump_region->append_intptr_t((intptr_t)(*p));
227   }
228 
229   void do_tag(int tag) {
230     _dump_region->append_intptr_t((intptr_t)tag);
231   }
232 
233   char* region_top() {
234     return _dump_region->top();
235   }
236 
237   bool reading() const { return false; }
238 };
239 
240 // Closure for serializing initialization data in from a data area
241 // (ptr_array) read from the shared file.
242 
243 class ReadClosure : public SerializeClosure {
244 private:
245   intptr_t** _ptr_array;
246   intptr_t _base_address;
247   inline intptr_t nextPtr() {
248     return *(*_ptr_array)++;
249   }
250 
251 public:
252   ReadClosure(intptr_t** ptr_array, intptr_t base_address) :
253     _ptr_array(ptr_array), _base_address(base_address) {}
254 
255   void do_ptr(void** p);
256   void do_u4(u4* p);
257   void do_int(int* p);
258   void do_bool(bool *p);
259   void do_tag(int tag);
260   bool reading() const { return true; }
261   char* region_top() { return nullptr; }
262 };
263 
264 class ArchiveUtils {
265   template <typename T> static Array<T>* archive_non_ptr_array(GrowableArray<T>* tmp_array);
266   template <typename T> static Array<T>* archive_ptr_array(GrowableArray<T>* tmp_array);
267 
268 public:
269   static const uintx MAX_SHARED_DELTA = 0x7FFFFFFF;
270   static void log_to_classlist(BootstrapInfo* bootstrap_specifier, TRAPS) NOT_CDS_RETURN;
271   static bool has_aot_initialized_mirror(InstanceKlass* src_ik);
272 
273   template <typename T, ENABLE_IF(!std::is_pointer<T>::value)>
274   static Array<T>* archive_array(GrowableArray<T>* tmp_array) {
275     return archive_non_ptr_array(tmp_array);
276   }
277 
278   template <typename T, ENABLE_IF(std::is_pointer<T>::value)>
279   static Array<T>* archive_array(GrowableArray<T>* tmp_array) {
280     return archive_ptr_array(tmp_array);
281   }
282 
283   static const char* builtin_loader_name_or_null(oop loader); // "boot", "platform", "app", or nullptr
284   static const char* builtin_loader_name(oop loader); // "boot", "platform", or "app". Asserts if not a built-in-loader.
285 
286   static bool builtin_loader_from_type(const char* loader_type, oop* value_ret);
287   static oop builtin_loader_from_type(int loader_type);
288 
289   // The following functions translate between a u4 offset and an address in the
290   // the range of the mapped CDS archive (e.g., Metaspace::is_in_shared_metaspace()).
291   // Since the first 16 bytes in this range are dummy data (see ArchiveBuilder::reserve_buffer()),
292   // we know that offset 0 never represents a valid object. As a result, an offset of 0
293   // is used to encode a nullptr.
294   //
295   // Use the "archived_address_or_null" variants if a nullptr may be encoded.
296 
297   // offset must represent an object of type T in the mapped shared space. Return
298   // a direct pointer to this object.
299   template <typename T> T static offset_to_archived_address(u4 offset) {
300     assert(offset != 0, "sanity");
301     T p = (T)(SharedBaseAddress + offset);
302     assert(Metaspace::is_in_shared_metaspace(p), "must be");
303     return p;
304   }
305 
306   template <typename T> T static offset_to_archived_address_or_null(u4 offset) {
307     if (offset == 0) {
308       return nullptr;
309     } else {
310       return offset_to_archived_address<T>(offset);
311     }
312   }
313 
314   // p must be an archived object. Get its offset from SharedBaseAddress
315   template <typename T> static u4 archived_address_to_offset(T p) {
316     uintx pn = (uintx)p;
317     uintx base = (uintx)SharedBaseAddress;
318     assert(Metaspace::is_in_shared_metaspace(p), "must be");
319     assert(pn > base, "sanity"); // No valid object is stored at 0 offset from SharedBaseAddress
320     uintx offset = pn - base;
321     assert(offset <= MAX_SHARED_DELTA, "range check");
322     return static_cast<u4>(offset);
323   }
324 
325   template <typename T> static u4 archived_address_or_null_to_offset(T p) {
326     if (p == nullptr) {
327       return 0;
328     } else {
329       return archived_address_to_offset<T>(p);
330     }
331   }
332 };
333 
334 class HeapRootSegments {
335 private:
336   size_t _base_offset;
337   size_t _count;
338   int _roots_count;
339   int _max_size_in_bytes;
340   int _max_size_in_elems;
341 
342 public:
343   size_t base_offset() { return _base_offset; }
344   size_t count() { return _count; }
345   int roots_count() { return _roots_count; }
346   int max_size_in_bytes() { return _max_size_in_bytes; }
347   int max_size_in_elems() { return _max_size_in_elems; }
348 
349   size_t size_in_bytes(size_t seg_idx);
350   int size_in_elems(size_t seg_idx);
351   size_t segment_offset(size_t seg_idx);
352 
353   // Trivial copy assignments are allowed to copy the entire object representation.
354   // We also inline this class into archive header. Therefore, it is important to make
355   // sure any gaps in object representation are initialized to zeroes. This is why
356   // constructors memset before doing field assignments.
357   HeapRootSegments() {
358     memset(this, 0, sizeof(*this));
359   }
360   HeapRootSegments(size_t base_offset, int roots_count, int max_size_in_bytes, int max_size_in_elems) {
361     memset(this, 0, sizeof(*this));
362     _base_offset = base_offset;
363     _count = (roots_count + max_size_in_elems - 1) / max_size_in_elems;
364     _roots_count = roots_count;
365     _max_size_in_bytes = max_size_in_bytes;
366     _max_size_in_elems = max_size_in_elems;
367   }
368 
369   // This class is trivially copyable and assignable.
370   HeapRootSegments(const HeapRootSegments&) = default;
371   HeapRootSegments& operator=(const HeapRootSegments&) = default;
372 };
373 
374 class ArchiveWorkers;
375 
376 // A task to be worked on by worker threads
377 class ArchiveWorkerTask : public CHeapObj<mtInternal> {
378   friend class ArchiveWorkers;
379 private:
380   const char* _name;
381   int _max_chunks;
382   volatile int _chunk;
383 
384   void run();
385 
386   void configure_max_chunks(int max_chunks);
387 
388 public:
389   ArchiveWorkerTask(const char* name) :
390       _name(name), _max_chunks(0), _chunk(0) {}
391   const char* name() const { return _name; }
392   virtual void work(int chunk, int max_chunks) = 0;
393 };
394 
395 class ArchiveWorkerThread : public NamedThread {
396   friend class ArchiveWorkers;
397 private:
398   ArchiveWorkers* const _pool;
399 
400   void post_run() override;
401 
402 public:
403   ArchiveWorkerThread(ArchiveWorkers* pool);
404   const char* type_name() const override { return "Archive Worker Thread"; }
405   void run() override;
406 };
407 
408 // Special archive workers. The goal for this implementation is to startup fast,
409 // distribute spiky workloads efficiently, and shutdown immediately after use.
410 // This makes the implementation quite different from the normal GC worker pool.
411 class ArchiveWorkers : public StackObj {
412   friend class ArchiveWorkerThread;
413 private:
414   // Target number of chunks per worker. This should be large enough to even
415   // out work imbalance, and small enough to keep bookkeeping overheads low.
416   static constexpr int CHUNKS_PER_WORKER = 4;
417   static int max_workers();
418 
419   Semaphore _end_semaphore;
420 
421   int _num_workers;
422   int _started_workers;
423   int _finish_tokens;
424 
425   typedef enum { UNUSED, WORKING, SHUTDOWN } State;
426   volatile State _state;
427 
428   ArchiveWorkerTask* _task;
429 
430   void run_as_worker();
431   void start_worker_if_needed();
432 
433   void run_task_single(ArchiveWorkerTask* task);
434   void run_task_multi(ArchiveWorkerTask* task);
435 
436   bool is_parallel();
437 
438 public:
439   ArchiveWorkers();
440   ~ArchiveWorkers();
441   void run_task(ArchiveWorkerTask* task);
442 };
443 
444 #endif // SHARE_CDS_ARCHIVEUTILS_HPP