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 #include "precompiled.hpp" 26 #include "cds/archiveBuilder.hpp" 27 #include "cds/archiveHeapLoader.inline.hpp" 28 #include "cds/archiveUtils.hpp" 29 #include "cds/cdsConfig.hpp" 30 #include "cds/classListParser.hpp" 31 #include "cds/classListWriter.hpp" 32 #include "cds/dynamicArchive.hpp" 33 #include "cds/filemap.hpp" 34 #include "cds/heapShared.hpp" 35 #include "cds/metaspaceShared.hpp" 36 #include "classfile/systemDictionaryShared.hpp" 37 #include "classfile/vmClasses.hpp" 38 #include "interpreter/bootstrapInfo.hpp" 39 #include "memory/metaspaceUtils.hpp" 40 #include "memory/resourceArea.hpp" 41 #include "oops/compressedOops.inline.hpp" 42 #include "runtime/arguments.hpp" 43 #include "utilities/bitMap.inline.hpp" 44 #include "utilities/debug.hpp" 45 #include "utilities/formatBuffer.hpp" 46 #include "utilities/globalDefinitions.hpp" 47 48 CHeapBitMap* ArchivePtrMarker::_ptrmap = nullptr; 49 CHeapBitMap* ArchivePtrMarker::_rw_ptrmap = nullptr; 50 CHeapBitMap* ArchivePtrMarker::_ro_ptrmap = nullptr; 51 VirtualSpace* ArchivePtrMarker::_vs; 52 53 bool ArchivePtrMarker::_compacted; 54 55 void ArchivePtrMarker::initialize(CHeapBitMap* ptrmap, VirtualSpace* vs) { 56 assert(_ptrmap == nullptr, "initialize only once"); 57 assert(_rw_ptrmap == nullptr, "initialize only once"); 58 assert(_ro_ptrmap == nullptr, "initialize only once"); 59 _vs = vs; 60 _compacted = false; 61 _ptrmap = ptrmap; 62 63 // Use this as initial guesstimate. We should need less space in the 64 // archive, but if we're wrong the bitmap will be expanded automatically. 65 size_t estimated_archive_size = MetaspaceGC::capacity_until_GC(); 66 // But set it smaller in debug builds so we always test the expansion code. 67 // (Default archive is about 12MB). 68 DEBUG_ONLY(estimated_archive_size = 6 * M); 69 70 // We need one bit per pointer in the archive. 71 _ptrmap->initialize(estimated_archive_size / sizeof(intptr_t)); 72 } 73 74 void ArchivePtrMarker::initialize_rw_ro_maps(CHeapBitMap* rw_ptrmap, CHeapBitMap* ro_ptrmap) { 75 address* rw_bottom = (address*)ArchiveBuilder::current()->rw_region()->base(); 76 address* ro_bottom = (address*)ArchiveBuilder::current()->ro_region()->base(); 77 78 _rw_ptrmap = rw_ptrmap; 79 _ro_ptrmap = ro_ptrmap; 80 81 size_t rw_size = ArchiveBuilder::current()->rw_region()->used() / sizeof(address); 82 size_t ro_size = ArchiveBuilder::current()->ro_region()->used() / sizeof(address); 83 // ro_start is the first bit in _ptrmap that covers the pointer that would sit at ro_bottom. 84 // E.g., if rw_bottom = (address*)100 85 // ro_bottom = (address*)116 86 // then for 64-bit platform: 87 // ro_start = ro_bottom - rw_bottom = (116 - 100) / sizeof(address) = 2; 88 size_t ro_start = ro_bottom - rw_bottom; 89 90 // Note: ptrmap is big enough only to cover the last pointer in ro_region. 91 // See ArchivePtrMarker::compact() 92 _rw_ptrmap->initialize(rw_size); 93 _ro_ptrmap->initialize(_ptrmap->size() - ro_start); 94 95 for (size_t rw_bit = 0; rw_bit < _rw_ptrmap->size(); rw_bit++) { 96 _rw_ptrmap->at_put(rw_bit, _ptrmap->at(rw_bit)); 97 } 98 99 for(size_t ro_bit = ro_start; ro_bit < _ptrmap->size(); ro_bit++) { 100 _ro_ptrmap->at_put(ro_bit-ro_start, _ptrmap->at(ro_bit)); 101 } 102 assert(_ptrmap->size() - ro_start == _ro_ptrmap->size(), "must be"); 103 } 104 105 void ArchivePtrMarker::mark_pointer(address* ptr_loc) { 106 assert(_ptrmap != nullptr, "not initialized"); 107 assert(!_compacted, "cannot mark anymore"); 108 109 if (ptr_base() <= ptr_loc && ptr_loc < ptr_end()) { 110 address value = *ptr_loc; 111 // We don't want any pointer that points to very bottom of the archive, otherwise when 112 // MetaspaceShared::default_base_address()==0, we can't distinguish between a pointer 113 // to nothing (null) vs a pointer to an objects that happens to be at the very bottom 114 // of the archive. 115 assert(value != (address)ptr_base(), "don't point to the bottom of the archive"); 116 117 if (value != nullptr) { 118 assert(uintx(ptr_loc) % sizeof(intptr_t) == 0, "pointers must be stored in aligned addresses"); 119 size_t idx = ptr_loc - ptr_base(); 120 if (_ptrmap->size() <= idx) { 121 _ptrmap->resize((idx + 1) * 2); 122 } 123 assert(idx < _ptrmap->size(), "must be"); 124 _ptrmap->set_bit(idx); 125 //tty->print_cr("Marking pointer [" PTR_FORMAT "] -> " PTR_FORMAT " @ " SIZE_FORMAT_W(5), p2i(ptr_loc), p2i(*ptr_loc), idx); 126 } 127 } 128 } 129 130 void ArchivePtrMarker::clear_pointer(address* ptr_loc) { 131 assert(_ptrmap != nullptr, "not initialized"); 132 assert(!_compacted, "cannot clear anymore"); 133 134 assert(ptr_base() <= ptr_loc && ptr_loc < ptr_end(), "must be"); 135 assert(uintx(ptr_loc) % sizeof(intptr_t) == 0, "pointers must be stored in aligned addresses"); 136 size_t idx = ptr_loc - ptr_base(); 137 assert(idx < _ptrmap->size(), "cannot clear pointers that have not been marked"); 138 _ptrmap->clear_bit(idx); 139 //tty->print_cr("Clearing pointer [" PTR_FORMAT "] -> " PTR_FORMAT " @ " SIZE_FORMAT_W(5), p2i(ptr_loc), p2i(*ptr_loc), idx); 140 } 141 142 class ArchivePtrBitmapCleaner: public BitMapClosure { 143 CHeapBitMap* _ptrmap; 144 address* _ptr_base; 145 address _relocatable_base; 146 address _relocatable_end; 147 size_t _max_non_null_offset; 148 149 public: 150 ArchivePtrBitmapCleaner(CHeapBitMap* ptrmap, address* ptr_base, address relocatable_base, address relocatable_end) : 151 _ptrmap(ptrmap), _ptr_base(ptr_base), 152 _relocatable_base(relocatable_base), _relocatable_end(relocatable_end), _max_non_null_offset(0) {} 153 154 bool do_bit(size_t offset) { 155 address* ptr_loc = _ptr_base + offset; 156 address ptr_value = *ptr_loc; 157 if (ptr_value != nullptr) { 158 assert(_relocatable_base <= ptr_value && ptr_value < _relocatable_end, "do not point to arbitrary locations!"); 159 if (_max_non_null_offset < offset) { 160 _max_non_null_offset = offset; 161 } 162 } else { 163 _ptrmap->clear_bit(offset); 164 DEBUG_ONLY(log_trace(cds, reloc)("Clearing pointer [" PTR_FORMAT "] -> null @ " SIZE_FORMAT_W(9), p2i(ptr_loc), offset)); 165 } 166 167 return true; 168 } 169 170 size_t max_non_null_offset() const { return _max_non_null_offset; } 171 }; 172 173 void ArchivePtrMarker::compact(address relocatable_base, address relocatable_end) { 174 assert(!_compacted, "cannot compact again"); 175 ArchivePtrBitmapCleaner cleaner(_ptrmap, ptr_base(), relocatable_base, relocatable_end); 176 _ptrmap->iterate(&cleaner); 177 compact(cleaner.max_non_null_offset()); 178 } 179 180 void ArchivePtrMarker::compact(size_t max_non_null_offset) { 181 assert(!_compacted, "cannot compact again"); 182 _ptrmap->resize(max_non_null_offset + 1); 183 _compacted = true; 184 } 185 186 char* DumpRegion::expand_top_to(char* newtop) { 187 assert(is_allocatable(), "must be initialized and not packed"); 188 assert(newtop >= _top, "must not grow backwards"); 189 if (newtop > _end) { 190 ArchiveBuilder::current()->report_out_of_space(_name, newtop - _top); 191 ShouldNotReachHere(); 192 } 193 194 commit_to(newtop); 195 _top = newtop; 196 197 if (_max_delta > 0) { 198 uintx delta = ArchiveBuilder::current()->buffer_to_offset((address)(newtop-1)); 199 if (delta > _max_delta) { 200 // This is just a sanity check and should not appear in any real world usage. This 201 // happens only if you allocate more than 2GB of shared objects and would require 202 // millions of shared classes. 203 log_error(cds)("Out of memory in the CDS archive: Please reduce the number of shared classes."); 204 MetaspaceShared::unrecoverable_writing_error(); 205 } 206 } 207 208 return _top; 209 } 210 211 void DumpRegion::commit_to(char* newtop) { 212 assert(CDSConfig::is_dumping_archive(), "sanity"); 213 char* base = _rs->base(); 214 size_t need_committed_size = newtop - base; 215 size_t has_committed_size = _vs->committed_size(); 216 if (need_committed_size < has_committed_size) { 217 return; 218 } 219 220 size_t min_bytes = need_committed_size - has_committed_size; 221 size_t preferred_bytes = 1 * M; 222 size_t uncommitted = _vs->reserved_size() - has_committed_size; 223 224 size_t commit = MAX2(min_bytes, preferred_bytes); 225 commit = MIN2(commit, uncommitted); 226 assert(commit <= uncommitted, "sanity"); 227 228 if (!_vs->expand_by(commit, false)) { 229 log_error(cds)("Failed to expand shared space to " SIZE_FORMAT " bytes", 230 need_committed_size); 231 MetaspaceShared::unrecoverable_writing_error(); 232 } 233 234 const char* which; 235 if (_rs->base() == (char*)MetaspaceShared::symbol_rs_base()) { 236 which = "symbol"; 237 } else { 238 which = "shared"; 239 } 240 log_debug(cds)("Expanding %s spaces by " SIZE_FORMAT_W(7) " bytes [total " SIZE_FORMAT_W(9) " bytes ending at %p]", 241 which, commit, _vs->actual_committed_size(), _vs->high()); 242 } 243 244 245 char* DumpRegion::allocate(size_t num_bytes) { 246 char* p = (char*)align_up(_top, (size_t)SharedSpaceObjectAlignment); 247 char* newtop = p + align_up(num_bytes, (size_t)SharedSpaceObjectAlignment); 248 expand_top_to(newtop); 249 memset(p, 0, newtop - p); 250 return p; 251 } 252 253 void DumpRegion::append_intptr_t(intptr_t n, bool need_to_mark) { 254 assert(is_aligned(_top, sizeof(intptr_t)), "bad alignment"); 255 intptr_t *p = (intptr_t*)_top; 256 char* newtop = _top + sizeof(intptr_t); 257 expand_top_to(newtop); 258 *p = n; 259 if (need_to_mark) { 260 ArchivePtrMarker::mark_pointer(p); 261 } 262 } 263 264 void DumpRegion::print(size_t total_bytes) const { 265 log_debug(cds)("%s space: " SIZE_FORMAT_W(9) " [ %4.1f%% of total] out of " SIZE_FORMAT_W(9) " bytes [%5.1f%% used] at " INTPTR_FORMAT, 266 _name, used(), percent_of(used(), total_bytes), reserved(), percent_of(used(), reserved()), 267 p2i(ArchiveBuilder::current()->to_requested(_base))); 268 } 269 270 void DumpRegion::print_out_of_space_msg(const char* failing_region, size_t needed_bytes) { 271 log_error(cds)("[%-8s] " PTR_FORMAT " - " PTR_FORMAT " capacity =%9d, allocated =%9d", 272 _name, p2i(_base), p2i(_top), int(_end - _base), int(_top - _base)); 273 if (strcmp(_name, failing_region) == 0) { 274 log_error(cds)(" required = %d", int(needed_bytes)); 275 } 276 } 277 278 void DumpRegion::init(ReservedSpace* rs, VirtualSpace* vs) { 279 _rs = rs; 280 _vs = vs; 281 // Start with 0 committed bytes. The memory will be committed as needed. 282 if (!_vs->initialize(*_rs, 0)) { 283 fatal("Unable to allocate memory for shared space"); 284 } 285 _base = _top = _rs->base(); 286 _end = _rs->end(); 287 } 288 289 void DumpRegion::pack(DumpRegion* next) { 290 assert(!is_packed(), "sanity"); 291 _end = (char*)align_up(_top, MetaspaceShared::core_region_alignment()); 292 _is_packed = true; 293 if (next != nullptr) { 294 next->_rs = _rs; 295 next->_vs = _vs; 296 next->_base = next->_top = this->_end; 297 next->_end = _rs->end(); 298 } 299 } 300 301 void WriteClosure::do_ptr(void** p) { 302 // Write ptr into the archive; ptr can be: 303 // (a) null -> written as 0 304 // (b) a "buffered" address -> written as is 305 // (c) a "source" address -> convert to "buffered" and write 306 // The common case is (c). E.g., when writing the vmClasses into the archive. 307 // We have (b) only when we don't have a corresponding source object. E.g., 308 // the archived c++ vtable entries. 309 address ptr = *(address*)p; 310 if (ptr != nullptr && !ArchiveBuilder::current()->is_in_buffer_space(ptr)) { 311 ptr = ArchiveBuilder::current()->get_buffered_addr(ptr); 312 } 313 // null pointers do not need to be converted to offsets 314 if (ptr != nullptr) { 315 ptr = (address)ArchiveBuilder::current()->buffer_to_offset(ptr); 316 } 317 _dump_region->append_intptr_t((intptr_t)ptr, false); 318 } 319 320 void ReadClosure::do_ptr(void** p) { 321 assert(*p == nullptr, "initializing previous initialized pointer."); 322 intptr_t obj = nextPtr(); 323 assert((intptr_t)obj >= 0 || (intptr_t)obj < -100, 324 "hit tag while initializing ptrs."); 325 *p = (void*)obj != nullptr ? (void*)(SharedBaseAddress + obj) : (void*)obj; 326 } 327 328 void ReadClosure::do_u4(u4* p) { 329 intptr_t obj = nextPtr(); 330 *p = (u4)(uintx(obj)); 331 } 332 333 void ReadClosure::do_int(int* p) { 334 intptr_t obj = nextPtr(); 335 *p = (int)(intx(obj)); 336 } 337 338 void ReadClosure::do_bool(bool* p) { 339 intptr_t obj = nextPtr(); 340 *p = (bool)(uintx(obj)); 341 } 342 343 void ReadClosure::do_tag(int tag) { 344 int old_tag; 345 old_tag = (int)(intptr_t)nextPtr(); 346 // do_int(&old_tag); 347 assert(tag == old_tag, "old tag doesn't match"); 348 FileMapInfo::assert_mark(tag == old_tag); 349 } 350 351 void ArchiveUtils::log_to_classlist(BootstrapInfo* bootstrap_specifier, TRAPS) { 352 if (ClassListWriter::is_enabled()) { 353 if (SystemDictionaryShared::is_supported_invokedynamic(bootstrap_specifier)) { 354 const constantPoolHandle& pool = bootstrap_specifier->pool(); 355 if (SystemDictionaryShared::is_builtin_loader(pool->pool_holder()->class_loader_data())) { 356 // Currently lambda proxy classes are supported only for the built-in loaders. 357 ResourceMark rm(THREAD); 358 int pool_index = bootstrap_specifier->bss_index(); 359 ClassListWriter w; 360 w.stream()->print("%s %s", ClassListParser::lambda_proxy_tag(), pool->pool_holder()->name()->as_C_string()); 361 CDSIndyInfo cii; 362 ClassListParser::populate_cds_indy_info(pool, pool_index, &cii, CHECK); 363 GrowableArray<const char*>* indy_items = cii.items(); 364 for (int i = 0; i < indy_items->length(); i++) { 365 w.stream()->print(" %s", indy_items->at(i)); 366 } 367 w.stream()->cr(); 368 } 369 } 370 } 371 }