1 /*
  2  * Copyright (c) 2019, 2023, 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/serializeClosure.hpp"
 29 #include "logging/log.hpp"
 30 #include "memory/virtualspace.hpp"
 31 #include "utilities/bitMap.hpp"
 32 #include "utilities/exceptions.hpp"
 33 #include "utilities/macros.hpp"
 34 
 35 class BootstrapInfo;
 36 class ReservedSpace;
 37 class VirtualSpace;
 38 
 39 template<class E> class Array;
 40 template<class E> class GrowableArray;
 41 
 42 // ArchivePtrMarker is used to mark the location of pointers embedded in a CDS archive. E.g., when an
 43 // InstanceKlass k is dumped, we mark the location of the k->_name pointer by effectively calling
 44 // mark_pointer(/*ptr_loc=*/&k->_name). It's required that (_prt_base <= ptr_loc < _ptr_end). _ptr_base is
 45 // fixed, but _ptr_end can be expanded as more objects are dumped.
 46 class ArchivePtrMarker : AllStatic {
 47   static CHeapBitMap*  _ptrmap;
 48   static VirtualSpace* _vs;
 49 
 50   // Once _ptrmap is compacted, we don't allow bit marking anymore. This is to
 51   // avoid unintentional copy operations after the bitmap has been finalized and written.
 52   static bool         _compacted;
 53 
 54   static address* ptr_base() { return (address*)_vs->low();  } // committed lower bound (inclusive)
 55   static address* ptr_end()  { return (address*)_vs->high(); } // committed upper bound (exclusive)
 56 
 57 public:
 58   static void initialize(CHeapBitMap* ptrmap, VirtualSpace* vs);
 59   static void mark_pointer(address* ptr_loc);
 60   static void clear_pointer(address* ptr_loc);
 61   static void compact(address relocatable_base, address relocatable_end);
 62   static void compact(size_t max_non_null_offset);
 63 
 64   template <typename T>
 65   static void mark_pointer(T* ptr_loc) {
 66     mark_pointer((address*)ptr_loc);
 67   }
 68 
 69   template <typename T>
 70   static void set_and_mark_pointer(T* ptr_loc, T ptr_value) {
 71     *ptr_loc = ptr_value;
 72     mark_pointer(ptr_loc);
 73   }
 74 
 75   static CHeapBitMap* ptrmap() {
 76     return _ptrmap;
 77   }
 78 
 79   static void reset_map_and_vs() {
 80     _ptrmap = nullptr;
 81     _vs = nullptr;
 82   }
 83 };
 84 
 85 // SharedDataRelocator is used to shift pointers in the CDS archive.
 86 //
 87 // The CDS archive is basically a contiguous block of memory (divided into several regions)
 88 // that contains multiple objects. The objects may contain direct pointers that point to other objects
 89 // within the archive (e.g., InstanceKlass::_name points to a Symbol in the archive). During dumping, we
 90 // built a bitmap that marks the locations of all these pointers (using ArchivePtrMarker, see comments above).
 91 //
 92 // The contents of the archive assumes that it’s mapped at the default SharedBaseAddress (e.g. 0x800000000).
 93 // If the archive ends up being mapped at a different address (e.g. 0x810000000), SharedDataRelocator
 94 // is used to shift each marked pointer by a delta (0x10000000 in this example), so that it points to
 95 // the actually mapped location of the target object.
 96 class SharedDataRelocator: public BitMapClosure {
 97   // for all (address** p), where (is_marked(p) && _patch_base <= p && p < _patch_end) { *p += delta; }
 98 
 99   // Patch all pointers within this region that are marked.
100   address* _patch_base;
101   address* _patch_end;
102 
103   // Before patching, all pointers must point to this region.
104   address _valid_old_base;
105   address _valid_old_end;
106 
107   // After patching, all pointers must point to this region.
108   address _valid_new_base;
109   address _valid_new_end;
110 
111   // How much to relocate for each pointer.
112   intx _delta;
113 
114  public:
115   SharedDataRelocator(address* patch_base, address* patch_end,
116                       address valid_old_base, address valid_old_end,
117                       address valid_new_base, address valid_new_end, intx delta) :
118     _patch_base(patch_base), _patch_end(patch_end),
119     _valid_old_base(valid_old_base), _valid_old_end(valid_old_end),
120     _valid_new_base(valid_new_base), _valid_new_end(valid_new_end),
121     _delta(delta) {
122     log_debug(cds, reloc)("SharedDataRelocator::_patch_base     = " PTR_FORMAT, p2i(_patch_base));
123     log_debug(cds, reloc)("SharedDataRelocator::_patch_end      = " PTR_FORMAT, p2i(_patch_end));
124     log_debug(cds, reloc)("SharedDataRelocator::_valid_old_base = " PTR_FORMAT, p2i(_valid_old_base));
125     log_debug(cds, reloc)("SharedDataRelocator::_valid_old_end  = " PTR_FORMAT, p2i(_valid_old_end));
126     log_debug(cds, reloc)("SharedDataRelocator::_valid_new_base = " PTR_FORMAT, p2i(_valid_new_base));
127     log_debug(cds, reloc)("SharedDataRelocator::_valid_new_end  = " PTR_FORMAT, p2i(_valid_new_end));
128   }
129 
130   bool do_bit(size_t offset);
131 };
132 
133 class DumpRegion {
134 private:
135   const char* _name;
136   char* _base;
137   char* _top;
138   char* _end;
139   uintx _max_delta;
140   bool _is_packed;
141   ReservedSpace* _rs;
142   VirtualSpace* _vs;
143 
144   void commit_to(char* newtop);
145 
146 public:
147   DumpRegion(const char* name, uintx max_delta = 0)
148     : _name(name), _base(nullptr), _top(nullptr), _end(nullptr),
149       _max_delta(max_delta), _is_packed(false) {}
150 
151   char* expand_top_to(char* newtop);
152   char* allocate(size_t num_bytes);
153 
154   void append_intptr_t(intptr_t n, bool need_to_mark = false) NOT_CDS_RETURN;
155 
156   char* base()      const { return _base;        }
157   char* top()       const { return _top;         }
158   char* end()       const { return _end;         }
159   size_t reserved() const { return _end - _base; }
160   size_t used()     const { return _top - _base; }
161   bool is_packed()  const { return _is_packed;   }
162   bool is_allocatable() const {
163     return !is_packed() && _base != nullptr;
164   }
165 
166   void print(size_t total_bytes) const;
167   void print_out_of_space_msg(const char* failing_region, size_t needed_bytes);
168 
169   void init(ReservedSpace* rs, VirtualSpace* vs);
170 
171   void pack(DumpRegion* next = nullptr);
172 
173   bool contains(char* p) {
174     return base() <= p && p < top();
175   }
176 };
177 
178 // Closure for serializing initialization data out to a data area to be
179 // written to the shared file.
180 
181 class WriteClosure : public SerializeClosure {
182 private:
183   DumpRegion* _dump_region;
184 
185 public:
186   WriteClosure(DumpRegion* r) {
187     _dump_region = r;
188   }
189 
190   void do_ptr(void** p);
191 
192   void do_u4(u4* p) {
193     _dump_region->append_intptr_t((intptr_t)(*p));
194   }
195 
196   void do_int(int* p) {
197     _dump_region->append_intptr_t((intptr_t)(*p));
198   }
199 
200   void do_bool(bool *p) {
201     _dump_region->append_intptr_t((intptr_t)(*p));
202   }
203 
204   void do_tag(int tag) {
205     _dump_region->append_intptr_t((intptr_t)tag);
206   }
207 
208   void do_region(u_char* start, size_t size);
209   bool reading() const { return false; }
210 };
211 
212 // Closure for serializing initialization data in from a data area
213 // (ptr_array) read from the shared file.
214 
215 class ReadClosure : public SerializeClosure {
216 private:
217   intptr_t** _ptr_array;
218 
219   inline intptr_t nextPtr() {
220     return *(*_ptr_array)++;
221   }
222 
223 public:
224   ReadClosure(intptr_t** ptr_array) { _ptr_array = ptr_array; }
225 
226   void do_ptr(void** p);
227   void do_u4(u4* p);
228   void do_int(int* p);
229   void do_bool(bool *p);
230   void do_tag(int tag);
231   void do_region(u_char* start, size_t size);
232   bool reading() const { return true; }
233 };
234 
235 class ArchiveUtils {
236 public:
237   static void log_to_classlist(BootstrapInfo* bootstrap_specifier, TRAPS) NOT_CDS_RETURN;
238 
239   template <typename T> static Array<T>* archive_array(GrowableArray<T>* tmp_array);
240 
241   // Used in logging: "boot", "boot2", "plat", "app" and "unreg";
242   static const char* class_category(Klass* k);
243 
244   static const char* builtin_loader_name_or_null(oop loader); // "boot", "platform", "app", or nullptr
245   static const char* builtin_loader_name(oop loader); // "boot", "platform", or "app". Asserts if not a built-in-loader.
246 
247   static bool builtin_loader_from_type(const char* loader_type, oop* value_ret);
248   static oop builtin_loader_from_type(int loader_type);
249 };
250 
251 #endif // SHARE_CDS_ARCHIVEUTILS_HPP