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