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