1 /* 2 * Copyright (c) 2019, 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_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 37 class BootstrapInfo; 38 class ReservedSpace; 39 class VirtualSpace; 40 41 template<class E> class Array; 42 template<class E> class GrowableArray; 43 44 // ArchivePtrMarker is used to mark the location of pointers embedded in a CDS archive. E.g., when an 45 // InstanceKlass k is dumped, we mark the location of the k->_name pointer by effectively calling 46 // mark_pointer(/*ptr_loc=*/&k->_name). It's required that (_prt_base <= ptr_loc < _ptr_end). _ptr_base is 47 // fixed, but _ptr_end can be expanded as more objects are dumped. 48 class ArchivePtrMarker : AllStatic { 49 static CHeapBitMap* _ptrmap; 50 static CHeapBitMap* _rw_ptrmap; 51 static CHeapBitMap* _ro_ptrmap; 52 static CHeapBitMap* _cc_ptrmap; 53 static VirtualSpace* _vs; 54 55 // Once _ptrmap is compacted, we don't allow bit marking anymore. This is to 56 // avoid unintentional copy operations after the bitmap has been finalized and written. 57 static bool _compacted; 58 59 static address* ptr_base() { return (address*)_vs->low(); } // committed lower bound (inclusive) 60 static address* ptr_end() { return (address*)_vs->high(); } // committed upper bound (exclusive) 61 62 public: 63 static void initialize(CHeapBitMap* ptrmap, VirtualSpace* vs); 64 static void initialize_rw_ro_cc_maps(CHeapBitMap* rw_ptrmap, CHeapBitMap* ro_ptrmap, CHeapBitMap* cc_ptrmap); 65 static void mark_pointer(address* ptr_loc); 66 static void clear_pointer(address* ptr_loc); 67 static void compact(address relocatable_base, address relocatable_end); 68 static void compact(size_t max_non_null_offset); 69 70 template <typename T> 71 static void mark_pointer(T* ptr_loc) { 72 mark_pointer((address*)ptr_loc); 73 } 74 75 template <typename T> 76 static void set_and_mark_pointer(T* ptr_loc, T ptr_value) { 77 *ptr_loc = ptr_value; 78 mark_pointer(ptr_loc); 79 } 80 81 static CHeapBitMap* ptrmap() { 82 return _ptrmap; 83 } 84 85 static CHeapBitMap* rw_ptrmap() { 86 return _rw_ptrmap; 87 } 88 89 static CHeapBitMap* ro_ptrmap() { 90 return _ro_ptrmap; 91 } 92 93 static CHeapBitMap* cc_ptrmap() { 94 return _cc_ptrmap; 95 } 96 97 static void reset_map_and_vs() { 98 _ptrmap = nullptr; 99 _rw_ptrmap = nullptr; 100 _ro_ptrmap = nullptr; 101 _cc_ptrmap = nullptr; 102 _vs = nullptr; 103 } 104 }; 105 106 // SharedDataRelocator is used to shift pointers in the CDS archive. 107 // 108 // The CDS archive is basically a contiguous block of memory (divided into several regions) 109 // that contains multiple objects. The objects may contain direct pointers that point to other objects 110 // within the archive (e.g., InstanceKlass::_name points to a Symbol in the archive). During dumping, we 111 // built a bitmap that marks the locations of all these pointers (using ArchivePtrMarker, see comments above). 112 // 113 // The contents of the archive assumes that it’s mapped at the default SharedBaseAddress (e.g. 0x800000000). 114 // If the archive ends up being mapped at a different address (e.g. 0x810000000), SharedDataRelocator 115 // is used to shift each marked pointer by a delta (0x10000000 in this example), so that it points to 116 // the actually mapped location of the target object. 117 class SharedDataRelocator: public BitMapClosure { 118 // for all (address** p), where (is_marked(p) && _patch_base <= p && p < _patch_end) { *p += delta; } 119 120 // Patch all pointers within this region that are marked. 121 address* _patch_base; 122 address* _patch_end; 123 124 // Before patching, all pointers must point to this region. 125 address _valid_old_base; 126 address _valid_old_end; 127 128 // After patching, all pointers must point to this region. 129 address _valid_new_base; 130 address _valid_new_end; 131 132 // How much to relocate for each pointer. 133 intx _delta; 134 135 public: 136 SharedDataRelocator(address* patch_base, address* patch_end, 137 address valid_old_base, address valid_old_end, 138 address valid_new_base, address valid_new_end, intx delta) : 139 _patch_base(patch_base), _patch_end(patch_end), 140 _valid_old_base(valid_old_base), _valid_old_end(valid_old_end), 141 _valid_new_base(valid_new_base), _valid_new_end(valid_new_end), 142 _delta(delta) { 143 log_debug(cds, reloc)("SharedDataRelocator::_patch_base = " PTR_FORMAT, p2i(_patch_base)); 144 log_debug(cds, reloc)("SharedDataRelocator::_patch_end = " PTR_FORMAT, p2i(_patch_end)); 145 log_debug(cds, reloc)("SharedDataRelocator::_valid_old_base = " PTR_FORMAT, p2i(_valid_old_base)); 146 log_debug(cds, reloc)("SharedDataRelocator::_valid_old_end = " PTR_FORMAT, p2i(_valid_old_end)); 147 log_debug(cds, reloc)("SharedDataRelocator::_valid_new_base = " PTR_FORMAT, p2i(_valid_new_base)); 148 log_debug(cds, reloc)("SharedDataRelocator::_valid_new_end = " PTR_FORMAT, p2i(_valid_new_end)); 149 } 150 151 bool do_bit(size_t offset); 152 }; 153 154 class DumpRegion { 155 private: 156 const char* _name; 157 char* _base; 158 char* _top; 159 char* _end; 160 uintx _max_delta; 161 bool _is_packed; 162 ReservedSpace* _rs; 163 VirtualSpace* _vs; 164 165 void commit_to(char* newtop); 166 167 public: 168 DumpRegion(const char* name, uintx max_delta = 0) 169 : _name(name), _base(nullptr), _top(nullptr), _end(nullptr), 170 _max_delta(max_delta), _is_packed(false), 171 _rs(NULL), _vs(NULL) {} 172 173 char* expand_top_to(char* newtop); 174 char* allocate(size_t num_bytes, size_t alignment = 0); 175 176 void append_intptr_t(intptr_t n, bool need_to_mark = false) NOT_CDS_RETURN; 177 178 char* base() const { return _base; } 179 char* top() const { return _top; } 180 char* end() const { return _end; } 181 size_t reserved() const { return _end - _base; } 182 size_t used() const { return _top - _base; } 183 bool is_packed() const { return _is_packed; } 184 bool is_allocatable() const { 185 return !is_packed() && _base != nullptr; 186 } 187 188 void print(size_t total_bytes) const; 189 void print_out_of_space_msg(const char* failing_region, size_t needed_bytes); 190 191 void init(ReservedSpace* rs, VirtualSpace* vs); 192 193 void pack(DumpRegion* next = nullptr); 194 195 bool contains(char* p) { 196 return base() <= p && p < top(); 197 } 198 }; 199 200 // Closure for serializing initialization data out to a data area to be 201 // written to the shared file. 202 203 class WriteClosure : public SerializeClosure { 204 private: 205 DumpRegion* _dump_region; 206 207 public: 208 WriteClosure(DumpRegion* r) { 209 _dump_region = r; 210 } 211 212 void do_ptr(void** p); 213 214 void do_u4(u4* p) { 215 _dump_region->append_intptr_t((intptr_t)(*p)); 216 } 217 218 void do_int(int* p) { 219 _dump_region->append_intptr_t((intptr_t)(*p)); 220 } 221 222 void do_bool(bool *p) { 223 _dump_region->append_intptr_t((intptr_t)(*p)); 224 } 225 226 void do_tag(int tag) { 227 _dump_region->append_intptr_t((intptr_t)tag); 228 } 229 230 char* region_top() { 231 return _dump_region->top(); 232 } 233 234 bool reading() const { return false; } 235 }; 236 237 // Closure for serializing initialization data in from a data area 238 // (ptr_array) read from the shared file. 239 240 class ReadClosure : public SerializeClosure { 241 private: 242 intptr_t** _ptr_array; 243 intptr_t _base_address; 244 inline intptr_t nextPtr() { 245 return *(*_ptr_array)++; 246 } 247 248 public: 249 ReadClosure(intptr_t** ptr_array, intptr_t base_address) : 250 _ptr_array(ptr_array), _base_address(base_address) {} 251 252 void do_ptr(void** p); 253 void do_u4(u4* p); 254 void do_int(int* p); 255 void do_bool(bool *p); 256 void do_tag(int tag); 257 bool reading() const { return true; } 258 char* region_top() { return nullptr; } 259 }; 260 261 class ArchiveUtils { 262 public: 263 static const uintx MAX_SHARED_DELTA = 0x7FFFFFFF; 264 static void log_to_classlist(BootstrapInfo* bootstrap_specifier, TRAPS) NOT_CDS_RETURN; 265 static bool has_aot_initialized_mirror(InstanceKlass* src_ik); 266 template <typename T> static Array<T>* archive_array(GrowableArray<T>* tmp_array); 267 268 static const char* builtin_loader_name_or_null(oop loader); // "boot", "platform", "app", or nullptr 269 static const char* builtin_loader_name(oop loader); // "boot", "platform", or "app". Asserts if not a built-in-loader. 270 271 static bool builtin_loader_from_type(const char* loader_type, oop* value_ret); 272 static oop builtin_loader_from_type(int loader_type); 273 274 // offset must represent an object of type T in the mapped shared space. Return 275 // a direct pointer to this object. 276 template <typename T> T static from_offset(u4 offset) { 277 T p = (T)(SharedBaseAddress + offset); 278 assert(Metaspace::is_in_shared_metaspace(p), "must be"); 279 return p; 280 } 281 282 // p must be an archived object. Get its offset from SharedBaseAddress 283 template <typename T> static u4 to_offset(T p) { 284 uintx pn = (uintx)p; 285 uintx base = (uintx)SharedBaseAddress; 286 assert(Metaspace::is_in_shared_metaspace(p), "must be"); 287 assert(pn > base, "sanity"); // No valid object is stored at 0 offset from SharedBaseAddress 288 uintx offset = pn - base; 289 assert(offset <= MAX_SHARED_DELTA, "range check"); 290 return static_cast<u4>(offset); 291 } 292 }; 293 294 class HeapRootSegments { 295 private: 296 size_t _base_offset; 297 size_t _count; 298 int _roots_count; 299 int _max_size_in_bytes; 300 int _max_size_in_elems; 301 302 public: 303 size_t base_offset() { return _base_offset; } 304 size_t count() { return _count; } 305 int roots_count() { return _roots_count; } 306 int max_size_in_bytes() { return _max_size_in_bytes; } 307 int max_size_in_elems() { return _max_size_in_elems; } 308 309 size_t size_in_bytes(size_t seg_idx); 310 int size_in_elems(size_t seg_idx); 311 size_t segment_offset(size_t seg_idx); 312 313 // Trivial copy assignments are allowed to copy the entire object representation. 314 // We also inline this class into archive header. Therefore, it is important to make 315 // sure any gaps in object representation are initialized to zeroes. This is why 316 // constructors memset before doing field assignments. 317 HeapRootSegments() { 318 memset(this, 0, sizeof(*this)); 319 } 320 HeapRootSegments(size_t base_offset, int roots_count, int max_size_in_bytes, int max_size_in_elems) { 321 memset(this, 0, sizeof(*this)); 322 _base_offset = base_offset; 323 _count = (roots_count + max_size_in_elems - 1) / max_size_in_elems; 324 _roots_count = roots_count; 325 _max_size_in_bytes = max_size_in_bytes; 326 _max_size_in_elems = max_size_in_elems; 327 } 328 329 // This class is trivially copyable and assignable. 330 HeapRootSegments(const HeapRootSegments&) = default; 331 HeapRootSegments& operator=(const HeapRootSegments&) = default; 332 }; 333 334 #endif // SHARE_CDS_ARCHIVEUTILS_HPP