1 /*
2 * Copyright (c) 2020, 2025, 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 "cds/aotArtifactFinder.hpp"
26 #include "cds/aotClassLinker.hpp"
27 #include "cds/aotLogging.hpp"
28 #include "cds/aotMapLogger.hpp"
29 #include "cds/aotMetaspace.hpp"
30 #include "cds/archiveBuilder.hpp"
31 #include "cds/archiveUtils.hpp"
32 #include "cds/cdsConfig.hpp"
33 #include "cds/cppVtables.hpp"
34 #include "cds/dumpAllocStats.hpp"
35 #include "cds/dynamicArchive.hpp"
36 #include "cds/heapShared.hpp"
37 #include "cds/regeneratedClasses.hpp"
38 #include "classfile/classLoader.hpp"
39 #include "classfile/classLoaderDataShared.hpp"
40 #include "classfile/javaClasses.hpp"
41 #include "classfile/symbolTable.hpp"
42 #include "classfile/systemDictionaryShared.hpp"
43 #include "classfile/vmClasses.hpp"
44 #include "code/aotCodeCache.hpp"
45 #include "interpreter/abstractInterpreter.hpp"
46 #include "jvm.h"
47 #include "logging/log.hpp"
48 #include "memory/allStatic.hpp"
49 #include "memory/memoryReserver.hpp"
50 #include "memory/memRegion.hpp"
51 #include "memory/resourceArea.hpp"
52 #include "oops/compressedKlass.inline.hpp"
53 #include "oops/instanceKlass.hpp"
54 #include "oops/methodCounters.hpp"
55 #include "oops/methodData.hpp"
56 #include "oops/objArrayKlass.hpp"
57 #include "oops/objArrayOop.inline.hpp"
58 #include "oops/oopHandle.inline.hpp"
59 #include "oops/trainingData.hpp"
60 #include "runtime/arguments.hpp"
61 #include "runtime/globals_extension.hpp"
62 #include "runtime/javaThread.hpp"
63 #include "runtime/sharedRuntime.hpp"
64 #include "utilities/align.hpp"
65 #include "utilities/bitMap.inline.hpp"
66 #include "utilities/formatBuffer.hpp"
67
68 ArchiveBuilder* ArchiveBuilder::_current = nullptr;
69
70 ArchiveBuilder::OtherROAllocMark::~OtherROAllocMark() {
71 char* newtop = ArchiveBuilder::current()->_ro_region.top();
72 ArchiveBuilder::alloc_stats()->record_other_type(int(newtop - _oldtop), true);
73 }
74
75 ArchiveBuilder::SourceObjList::SourceObjList() : _ptrmap(16 * K, mtClassShared) {
76 _total_bytes = 0;
77 _objs = new (mtClassShared) GrowableArray<SourceObjInfo*>(128 * K, mtClassShared);
78 }
79
80 ArchiveBuilder::SourceObjList::~SourceObjList() {
81 delete _objs;
82 }
83
84 void ArchiveBuilder::SourceObjList::append(SourceObjInfo* src_info) {
85 // Save this source object for copying
86 src_info->set_id(_objs->length());
87 _objs->append(src_info);
88
89 // Prepare for marking the pointers in this source object
90 assert(is_aligned(_total_bytes, sizeof(address)), "must be");
91 src_info->set_ptrmap_start(_total_bytes / sizeof(address));
92 _total_bytes = align_up(_total_bytes + (uintx)src_info->size_in_bytes(), sizeof(address));
93 src_info->set_ptrmap_end(_total_bytes / sizeof(address));
94
95 BitMap::idx_t bitmap_size_needed = BitMap::idx_t(src_info->ptrmap_end());
96 if (_ptrmap.size() <= bitmap_size_needed) {
97 _ptrmap.resize((bitmap_size_needed + 1) * 2);
98 }
99 }
100
101 void ArchiveBuilder::SourceObjList::remember_embedded_pointer(SourceObjInfo* src_info, MetaspaceClosure::Ref* ref) {
102 // src_obj contains a pointer. Remember the location of this pointer in _ptrmap,
103 // so that we can copy/relocate it later.
104 src_info->set_has_embedded_pointer();
105 address src_obj = src_info->source_addr();
106 address* field_addr = ref->addr();
107 assert(src_info->ptrmap_start() < _total_bytes, "sanity");
108 assert(src_info->ptrmap_end() <= _total_bytes, "sanity");
109 assert(*field_addr != nullptr, "should have checked");
110
111 intx field_offset_in_bytes = ((address)field_addr) - src_obj;
112 DEBUG_ONLY(int src_obj_size = src_info->size_in_bytes();)
113 assert(field_offset_in_bytes >= 0, "must be");
114 assert(field_offset_in_bytes + intx(sizeof(intptr_t)) <= intx(src_obj_size), "must be");
115 assert(is_aligned(field_offset_in_bytes, sizeof(address)), "must be");
116
117 BitMap::idx_t idx = BitMap::idx_t(src_info->ptrmap_start() + (uintx)(field_offset_in_bytes / sizeof(address)));
118 _ptrmap.set_bit(BitMap::idx_t(idx));
119 }
120
121 class RelocateEmbeddedPointers : public BitMapClosure {
122 ArchiveBuilder* _builder;
123 address _buffered_obj;
124 BitMap::idx_t _start_idx;
125 public:
126 RelocateEmbeddedPointers(ArchiveBuilder* builder, address buffered_obj, BitMap::idx_t start_idx) :
127 _builder(builder), _buffered_obj(buffered_obj), _start_idx(start_idx) {}
128
129 bool do_bit(BitMap::idx_t bit_offset) {
130 size_t field_offset = size_t(bit_offset - _start_idx) * sizeof(address);
131 address* ptr_loc = (address*)(_buffered_obj + field_offset);
132
133 address old_p_with_tags = *ptr_loc;
134 assert(old_p_with_tags != nullptr, "null ptrs shouldn't have been marked");
135
136 address old_p = MetaspaceClosure::strip_tags(old_p_with_tags);
137 uintx tags = MetaspaceClosure::decode_tags(old_p_with_tags);
138 address new_p = _builder->get_buffered_addr(old_p);
139
140 bool nulled;
141 if (new_p == nullptr) {
142 // old_p had a FollowMode of set_to_null
143 nulled = true;
144 } else {
145 new_p = MetaspaceClosure::add_tags(new_p, tags);
146 nulled = false;
147 }
148
149 log_trace(aot)("Ref: [" PTR_FORMAT "] -> " PTR_FORMAT " => " PTR_FORMAT " %zu",
150 p2i(ptr_loc), p2i(old_p) + tags, p2i(new_p), tags);
151
152 ArchivePtrMarker::set_and_mark_pointer(ptr_loc, new_p);
153 ArchiveBuilder::current()->count_relocated_pointer(tags != 0, nulled);
154 return true; // keep iterating the bitmap
155 }
156 };
157
158 void ArchiveBuilder::SourceObjList::relocate(int i, ArchiveBuilder* builder) {
159 SourceObjInfo* src_info = objs()->at(i);
160 assert(src_info->should_copy(), "must be");
161 BitMap::idx_t start = BitMap::idx_t(src_info->ptrmap_start()); // inclusive
162 BitMap::idx_t end = BitMap::idx_t(src_info->ptrmap_end()); // exclusive
163
164 RelocateEmbeddedPointers relocator(builder, src_info->buffered_addr(), start);
165 _ptrmap.iterate(&relocator, start, end);
166 }
167
168 ArchiveBuilder::ArchiveBuilder() :
169 _current_dump_region(nullptr),
170 _buffer_bottom(nullptr),
171 _requested_static_archive_bottom(nullptr),
172 _requested_static_archive_top(nullptr),
173 _requested_dynamic_archive_bottom(nullptr),
174 _requested_dynamic_archive_top(nullptr),
175 _mapped_static_archive_bottom(nullptr),
176 _mapped_static_archive_top(nullptr),
177 _buffer_to_requested_delta(0),
178 _pz_region("pz", MAX_SHARED_DELTA), // protection zone -- used only during dumping; does NOT exist in cds archive.
179 _rw_region("rw", MAX_SHARED_DELTA),
180 _ro_region("ro", MAX_SHARED_DELTA),
181 _ac_region("ac", MAX_SHARED_DELTA),
182 _ptrmap(mtClassShared),
183 _rw_ptrmap(mtClassShared),
184 _ro_ptrmap(mtClassShared),
185 _rw_src_objs(),
186 _ro_src_objs(),
187 _src_obj_table(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE),
188 _buffered_to_src_table(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE),
189 _total_heap_region_size(0)
190 {
191 _klasses = new (mtClassShared) GrowableArray<Klass*>(4 * K, mtClassShared);
192 _symbols = new (mtClassShared) GrowableArray<Symbol*>(256 * K, mtClassShared);
193 _entropy_seed = 0x12345678;
194 _relocated_ptr_info._num_ptrs = 0;
195 _relocated_ptr_info._num_tagged_ptrs = 0;
196 _relocated_ptr_info._num_nulled_ptrs = 0;
197 assert(_current == nullptr, "must be");
198 _current = this;
199 }
200
201 ArchiveBuilder::~ArchiveBuilder() {
202 assert(_current == this, "must be");
203 _current = nullptr;
204
205 for (int i = 0; i < _symbols->length(); i++) {
206 _symbols->at(i)->decrement_refcount();
207 }
208
209 delete _klasses;
210 delete _symbols;
211 if (_shared_rs.is_reserved()) {
212 MemoryReserver::release(_shared_rs);
213 }
214
215 AOTArtifactFinder::dispose();
216 }
217
218 // Returns a deterministic sequence of pseudo random numbers. The main purpose is NOT
219 // for randomness but to get good entropy for the identity_hash() of archived Symbols,
220 // while keeping the contents of static CDS archives deterministic to ensure
221 // reproducibility of JDK builds.
222 int ArchiveBuilder::entropy() {
223 assert(SafepointSynchronize::is_at_safepoint(), "needed to ensure deterministic sequence");
224 _entropy_seed = os::next_random(_entropy_seed);
225 return static_cast<int>(_entropy_seed);
226 }
227
228 class GatherKlassesAndSymbols : public UniqueMetaspaceClosure {
229 ArchiveBuilder* _builder;
230
231 public:
232 GatherKlassesAndSymbols(ArchiveBuilder* builder) : _builder(builder) {}
233
234 virtual bool do_unique_ref(Ref* ref, bool read_only) {
235 return _builder->gather_klass_and_symbol(ref, read_only);
236 }
237 };
238
239 bool ArchiveBuilder::gather_klass_and_symbol(MetaspaceClosure::Ref* ref, bool read_only) {
240 if (ref->obj() == nullptr) {
241 return false;
242 }
243 if (get_follow_mode(ref) != make_a_copy) {
244 return false;
245 }
246 if (ref->msotype() == MetaspaceObj::ClassType) {
247 Klass* klass = (Klass*)ref->obj();
248 assert(klass->is_klass(), "must be");
249 if (!is_excluded(klass)) {
250 _klasses->append(klass);
251 if (klass->is_hidden()) {
252 assert(klass->is_instance_klass(), "must be");
253 }
254 }
255 } else if (ref->msotype() == MetaspaceObj::SymbolType) {
256 // Make sure the symbol won't be GC'ed while we are dumping the archive.
257 Symbol* sym = (Symbol*)ref->obj();
258 sym->increment_refcount();
259 _symbols->append(sym);
260 }
261
262 return true; // recurse
263 }
264
265 void ArchiveBuilder::gather_klasses_and_symbols() {
266 ResourceMark rm;
267
268 AOTArtifactFinder::initialize();
269 AOTArtifactFinder::find_artifacts();
270
271 aot_log_info(aot)("Gathering classes and symbols ... ");
272 GatherKlassesAndSymbols doit(this);
273 iterate_roots(&doit);
274 #if INCLUDE_CDS_JAVA_HEAP
275 if (CDSConfig::is_dumping_full_module_graph()) {
276 ClassLoaderDataShared::iterate_symbols(&doit);
277 }
278 #endif
279 doit.finish();
280
281 if (CDSConfig::is_dumping_static_archive()) {
282 // To ensure deterministic contents in the static archive, we need to ensure that
283 // we iterate the MetaspaceObjs in a deterministic order. It doesn't matter where
284 // the MetaspaceObjs are located originally, as they are copied sequentially into
285 // the archive during the iteration.
286 //
287 // The only issue here is that the symbol table and the system directories may be
288 // randomly ordered, so we copy the symbols and klasses into two arrays and sort
289 // them deterministically.
290 //
291 // During -Xshare:dump, the order of Symbol creation is strictly determined by
292 // the SharedClassListFile (class loading is done in a single thread and the JIT
293 // is disabled). Also, Symbols are allocated in monotonically increasing addresses
294 // (see Symbol::operator new(size_t, int)). So if we iterate the Symbols by
295 // ascending address order, we ensure that all Symbols are copied into deterministic
296 // locations in the archive.
297 //
298 // TODO: in the future, if we want to produce deterministic contents in the
299 // dynamic archive, we might need to sort the symbols alphabetically (also see
300 // DynamicArchiveBuilder::sort_methods()).
301 aot_log_info(aot)("Sorting symbols ... ");
302 _symbols->sort(compare_symbols_by_address);
303 sort_klasses();
304 }
305
306 AOTClassLinker::add_candidates();
307 }
308
309 int ArchiveBuilder::compare_symbols_by_address(Symbol** a, Symbol** b) {
310 if (a[0] < b[0]) {
311 return -1;
312 } else {
313 assert(a[0] > b[0], "Duplicated symbol %s unexpected", (*a)->as_C_string());
314 return 1;
315 }
316 }
317
318 int ArchiveBuilder::compare_klass_by_name(Klass** a, Klass** b) {
319 return a[0]->name()->fast_compare(b[0]->name());
320 }
321
322 void ArchiveBuilder::sort_klasses() {
323 aot_log_info(aot)("Sorting classes ... ");
324 _klasses->sort(compare_klass_by_name);
325 }
326
327 address ArchiveBuilder::reserve_buffer() {
328 // AOTCodeCache::max_aot_code_size() accounts for aot code region.
329 size_t buffer_size = LP64_ONLY(CompressedClassSpaceSize) NOT_LP64(256 * M) + AOTCodeCache::max_aot_code_size();
330 ReservedSpace rs = MemoryReserver::reserve(buffer_size,
331 AOTMetaspace::core_region_alignment(),
332 os::vm_page_size(),
333 mtNone);
334 if (!rs.is_reserved()) {
335 aot_log_error(aot)("Failed to reserve %zu bytes of output buffer.", buffer_size);
336 AOTMetaspace::unrecoverable_writing_error();
337 }
338
339 // buffer_bottom is the lowest address of the 2 core regions (rw, ro) when
340 // we are copying the class metadata into the buffer.
341 address buffer_bottom = (address)rs.base();
342 aot_log_info(aot)("Reserved output buffer space at " PTR_FORMAT " [%zu bytes]",
343 p2i(buffer_bottom), buffer_size);
344 _shared_rs = rs;
345
346 _buffer_bottom = buffer_bottom;
347
348 if (CDSConfig::is_dumping_static_archive()) {
349 _current_dump_region = &_pz_region;
350 } else {
351 _current_dump_region = &_rw_region;
352 }
353 _current_dump_region->init(&_shared_rs, &_shared_vs);
354
355 ArchivePtrMarker::initialize(&_ptrmap, &_shared_vs);
356
357 // The bottom of the static archive should be mapped at this address by default.
358 _requested_static_archive_bottom = (address)AOTMetaspace::requested_base_address();
359
360 // The bottom of the archive (that I am writing now) should be mapped at this address by default.
361 address my_archive_requested_bottom;
362
363 if (CDSConfig::is_dumping_static_archive()) {
364 my_archive_requested_bottom = _requested_static_archive_bottom;
365 } else {
366 _mapped_static_archive_bottom = (address)MetaspaceObj::aot_metaspace_base();
367 _mapped_static_archive_top = (address)MetaspaceObj::aot_metaspace_top();
368 assert(_mapped_static_archive_top >= _mapped_static_archive_bottom, "must be");
369 size_t static_archive_size = _mapped_static_archive_top - _mapped_static_archive_bottom;
370
371 // At run time, we will mmap the dynamic archive at my_archive_requested_bottom
372 _requested_static_archive_top = _requested_static_archive_bottom + static_archive_size;
373 my_archive_requested_bottom = align_up(_requested_static_archive_top, AOTMetaspace::core_region_alignment());
374
375 _requested_dynamic_archive_bottom = my_archive_requested_bottom;
376 }
377
378 _buffer_to_requested_delta = my_archive_requested_bottom - _buffer_bottom;
379
380 address my_archive_requested_top = my_archive_requested_bottom + buffer_size;
381 if (my_archive_requested_bottom < _requested_static_archive_bottom ||
382 my_archive_requested_top <= _requested_static_archive_bottom) {
383 // Size overflow.
384 aot_log_error(aot)("my_archive_requested_bottom = " INTPTR_FORMAT, p2i(my_archive_requested_bottom));
385 aot_log_error(aot)("my_archive_requested_top = " INTPTR_FORMAT, p2i(my_archive_requested_top));
386 aot_log_error(aot)("SharedBaseAddress (" INTPTR_FORMAT ") is too high. "
387 "Please rerun java -Xshare:dump with a lower value", p2i(_requested_static_archive_bottom));
388 AOTMetaspace::unrecoverable_writing_error();
389 }
390
391 if (CDSConfig::is_dumping_static_archive()) {
392 // We don't want any valid object to be at the very bottom of the archive.
393 // See ArchivePtrMarker::mark_pointer().
394 _pz_region.allocate(AOTMetaspace::protection_zone_size());
395 start_dump_region(&_rw_region);
396 }
397
398 return buffer_bottom;
399 }
400
401 void ArchiveBuilder::iterate_sorted_roots(MetaspaceClosure* it) {
402 int num_symbols = _symbols->length();
403 for (int i = 0; i < num_symbols; i++) {
404 it->push(_symbols->adr_at(i));
405 }
406
407 int num_klasses = _klasses->length();
408 for (int i = 0; i < num_klasses; i++) {
409 it->push(_klasses->adr_at(i));
410 }
411
412 iterate_roots(it);
413 }
414
415 class GatherSortedSourceObjs : public MetaspaceClosure {
416 ArchiveBuilder* _builder;
417
418 public:
419 GatherSortedSourceObjs(ArchiveBuilder* builder) : _builder(builder) {}
420
421 virtual bool do_ref(Ref* ref, bool read_only) {
422 return _builder->gather_one_source_obj(ref, read_only);
423 }
424 };
425
426 bool ArchiveBuilder::gather_one_source_obj(MetaspaceClosure::Ref* ref, bool read_only) {
427 address src_obj = ref->obj();
428 if (src_obj == nullptr) {
429 return false;
430 }
431
432 remember_embedded_pointer_in_enclosing_obj(ref);
433 if (RegeneratedClasses::has_been_regenerated(src_obj)) {
434 // No need to copy it. We will later relocate it to point to the regenerated klass/method.
435 return false;
436 }
437
438 FollowMode follow_mode = get_follow_mode(ref);
439 SourceObjInfo src_info(ref, read_only, follow_mode);
440 bool created;
441 SourceObjInfo* p = _src_obj_table.put_if_absent(src_obj, src_info, &created);
442 if (created) {
443 if (_src_obj_table.maybe_grow()) {
444 log_info(aot, hashtables)("Expanded _src_obj_table table to %d", _src_obj_table.table_size());
445 }
446 }
447
448 #ifdef ASSERT
449 if (ref->msotype() == MetaspaceObj::MethodType) {
450 Method* m = (Method*)ref->obj();
451 assert(!RegeneratedClasses::has_been_regenerated((address)m->method_holder()),
452 "Should not archive methods in a class that has been regenerated");
453 }
454 #endif
455
456 if (ref->msotype() == MetaspaceObj::MethodDataType) {
457 MethodData* md = (MethodData*)ref->obj();
458 md->clean_method_data(false /* always_clean */);
459 }
460
461 assert(p->read_only() == src_info.read_only(), "must be");
462
463 if (created && src_info.should_copy()) {
464 if (read_only) {
465 _ro_src_objs.append(p);
466 } else {
467 _rw_src_objs.append(p);
468 }
469 return true; // Need to recurse into this ref only if we are copying it
470 } else {
471 return false;
472 }
473 }
474
475 void ArchiveBuilder::record_regenerated_object(address orig_src_obj, address regen_src_obj) {
476 // Record the fact that orig_src_obj has been replaced by regen_src_obj. All calls to get_buffered_addr(orig_src_obj)
477 // should return the same value as get_buffered_addr(regen_src_obj).
478 SourceObjInfo* p = _src_obj_table.get(regen_src_obj);
479 assert(p != nullptr, "regenerated object should always be dumped");
480 SourceObjInfo orig_src_info(orig_src_obj, p);
481 bool created;
482 _src_obj_table.put_if_absent(orig_src_obj, orig_src_info, &created);
483 assert(created, "We shouldn't have archived the original copy of a regenerated object");
484 }
485
486 // Remember that we have a pointer inside ref->enclosing_obj() that points to ref->obj()
487 void ArchiveBuilder::remember_embedded_pointer_in_enclosing_obj(MetaspaceClosure::Ref* ref) {
488 assert(ref->obj() != nullptr, "should have checked");
489
490 address enclosing_obj = ref->enclosing_obj();
491 if (enclosing_obj == nullptr) {
492 return;
493 }
494
495 // We are dealing with 3 addresses:
496 // address o = ref->obj(): We have found an object whose address is o.
497 // address* mpp = ref->mpp(): The object o is pointed to by a pointer whose address is mpp.
498 // I.e., (*mpp == o)
499 // enclosing_obj : If non-null, it is the object which has a field that points to o.
500 // mpp is the address if that field.
501 //
502 // Example: We have an array whose first element points to a Method:
503 // Method* o = 0x0000abcd;
504 // Array<Method*>* enclosing_obj = 0x00001000;
505 // enclosing_obj->at_put(0, o);
506 //
507 // We the MetaspaceClosure iterates on the very first element of this array, we have
508 // ref->obj() == 0x0000abcd (the Method)
509 // ref->mpp() == 0x00001008 (the location of the first element in the array)
510 // ref->enclosing_obj() == 0x00001000 (the Array that contains the Method)
511 //
512 // We use the above information to mark the bitmap to indicate that there's a pointer on address 0x00001008.
513 SourceObjInfo* src_info = _src_obj_table.get(enclosing_obj);
514 if (src_info == nullptr || !src_info->should_copy()) {
515 // source objects of point_to_it/set_to_null types are not copied
516 // so we don't need to remember their pointers.
517 } else {
518 if (src_info->read_only()) {
519 _ro_src_objs.remember_embedded_pointer(src_info, ref);
520 } else {
521 _rw_src_objs.remember_embedded_pointer(src_info, ref);
522 }
523 }
524 }
525
526 void ArchiveBuilder::gather_source_objs() {
527 ResourceMark rm;
528 aot_log_info(aot)("Gathering all archivable objects ... ");
529 gather_klasses_and_symbols();
530 GatherSortedSourceObjs doit(this);
531 iterate_sorted_roots(&doit);
532 doit.finish();
533 }
534
535 bool ArchiveBuilder::is_excluded(Klass* klass) {
536 if (klass->is_instance_klass()) {
537 InstanceKlass* ik = InstanceKlass::cast(klass);
538 return SystemDictionaryShared::is_excluded_class(ik);
539 } else if (klass->is_objArray_klass()) {
540 Klass* bottom = ObjArrayKlass::cast(klass)->bottom_klass();
541 if (CDSConfig::is_dumping_dynamic_archive() && AOTMetaspace::in_aot_cache_static_region(bottom)) {
542 // The bottom class is in the static archive so it's clearly not excluded.
543 return false;
544 } else if (bottom->is_instance_klass()) {
545 return SystemDictionaryShared::is_excluded_class(InstanceKlass::cast(bottom));
546 }
547 }
548
549 return false;
550 }
551
552 ArchiveBuilder::FollowMode ArchiveBuilder::get_follow_mode(MetaspaceClosure::Ref *ref) {
553 address obj = ref->obj();
554 if (CDSConfig::is_dumping_dynamic_archive() && AOTMetaspace::in_aot_cache(obj)) {
555 // Don't dump existing shared metadata again.
556 return point_to_it;
557 } else if (ref->msotype() == MetaspaceObj::MethodDataType ||
558 ref->msotype() == MetaspaceObj::MethodCountersType ||
559 ref->msotype() == MetaspaceObj::KlassTrainingDataType ||
560 ref->msotype() == MetaspaceObj::MethodTrainingDataType ||
561 ref->msotype() == MetaspaceObj::CompileTrainingDataType) {
562 return (TrainingData::need_data() || TrainingData::assembling_data()) ? make_a_copy : set_to_null;
563 } else if (ref->msotype() == MetaspaceObj::AdapterHandlerEntryType) {
564 return CDSConfig::is_dumping_adapters() ? make_a_copy : set_to_null;
565 } else {
566 if (ref->msotype() == MetaspaceObj::ClassType) {
567 Klass* klass = (Klass*)ref->obj();
568 assert(klass->is_klass(), "must be");
569 if (RegeneratedClasses::has_been_regenerated(klass)) {
570 klass = RegeneratedClasses::get_regenerated_object(klass);
571 }
572 if (is_excluded(klass)) {
573 ResourceMark rm;
574 log_debug(cds, dynamic)("Skipping class (excluded): %s", klass->external_name());
575 return set_to_null;
576 }
577 }
578
579 return make_a_copy;
580 }
581 }
582
583 void ArchiveBuilder::start_dump_region(DumpRegion* next) {
584 current_dump_region()->pack(next);
585 _current_dump_region = next;
586 }
587
588 char* ArchiveBuilder::ro_strdup(const char* s) {
589 char* archived_str = ro_region_alloc((int)strlen(s) + 1);
590 strcpy(archived_str, s);
591 return archived_str;
592 }
593
594 // The objects that have embedded pointers will sink
595 // towards the end of the list. This ensures we have a maximum
596 // number of leading zero bits in the relocation bitmap.
597 int ArchiveBuilder::compare_src_objs(SourceObjInfo** a, SourceObjInfo** b) {
598 if ((*a)->has_embedded_pointer() && !(*b)->has_embedded_pointer()) {
599 return 1;
600 } else if (!(*a)->has_embedded_pointer() && (*b)->has_embedded_pointer()) {
601 return -1;
602 } else {
603 // This is necessary to keep the sorting order stable. Otherwise the
604 // archive's contents may not be deterministic.
605 return (*a)->id() - (*b)->id();
606 }
607 }
608
609 void ArchiveBuilder::sort_metadata_objs() {
610 _rw_src_objs.objs()->sort(compare_src_objs);
611 _ro_src_objs.objs()->sort(compare_src_objs);
612 }
613
614 void ArchiveBuilder::dump_rw_metadata() {
615 ResourceMark rm;
616 aot_log_info(aot)("Allocating RW objects ... ");
617 make_shallow_copies(&_rw_region, &_rw_src_objs);
618
619 #if INCLUDE_CDS_JAVA_HEAP
620 if (CDSConfig::is_dumping_full_module_graph()) {
621 // Archive the ModuleEntry's and PackageEntry's of the 3 built-in loaders
622 char* start = rw_region()->top();
623 ClassLoaderDataShared::allocate_archived_tables();
624 alloc_stats()->record_modules(rw_region()->top() - start, /*read_only*/false);
625 }
626 #endif
627 }
628
629 void ArchiveBuilder::dump_ro_metadata() {
630 ResourceMark rm;
631 aot_log_info(aot)("Allocating RO objects ... ");
632
633 start_dump_region(&_ro_region);
634 make_shallow_copies(&_ro_region, &_ro_src_objs);
635
636 #if INCLUDE_CDS_JAVA_HEAP
637 if (CDSConfig::is_dumping_full_module_graph()) {
638 char* start = ro_region()->top();
639 ClassLoaderDataShared::init_archived_tables();
640 alloc_stats()->record_modules(ro_region()->top() - start, /*read_only*/true);
641 }
642 #endif
643
644 RegeneratedClasses::record_regenerated_objects();
645 }
646
647 void ArchiveBuilder::make_shallow_copies(DumpRegion *dump_region,
648 const ArchiveBuilder::SourceObjList* src_objs) {
649 for (int i = 0; i < src_objs->objs()->length(); i++) {
650 make_shallow_copy(dump_region, src_objs->objs()->at(i));
651 }
652 aot_log_info(aot)("done (%d objects)", src_objs->objs()->length());
653 }
654
655 void ArchiveBuilder::make_shallow_copy(DumpRegion *dump_region, SourceObjInfo* src_info) {
656 address src = src_info->source_addr();
657 int bytes = src_info->size_in_bytes(); // word-aligned
658 size_t alignment = SharedSpaceObjectAlignment; // alignment for the dest pointer
659
660 char* oldtop = dump_region->top();
661 if (src_info->msotype() == MetaspaceObj::ClassType) {
662 // Allocate space for a pointer directly in front of the future InstanceKlass, so
663 // we can do a quick lookup from InstanceKlass* -> RunTimeClassInfo*
664 // without building another hashtable. See RunTimeClassInfo::get_for()
665 // in systemDictionaryShared.cpp.
666 Klass* klass = (Klass*)src;
667 if (klass->is_instance_klass()) {
668 SystemDictionaryShared::validate_before_archiving(InstanceKlass::cast(klass));
669 dump_region->allocate(sizeof(address));
670 }
671 #ifdef _LP64
672 // More strict alignments needed for UseCompressedClassPointers
673 if (UseCompressedClassPointers) {
674 alignment = nth_bit(ArchiveBuilder::precomputed_narrow_klass_shift());
675 }
676 #endif
677 } else if (src_info->msotype() == MetaspaceObj::SymbolType) {
678 // Symbols may be allocated by using AllocateHeap, so their sizes
679 // may be less than size_in_bytes() indicates.
680 bytes = ((Symbol*)src)->byte_size();
681 }
682
683 char* dest = dump_region->allocate(bytes, alignment);
684 memcpy(dest, src, bytes);
685
686 // Update the hash of buffered sorted symbols for static dump so that the symbols have deterministic contents
687 if (CDSConfig::is_dumping_static_archive() && (src_info->msotype() == MetaspaceObj::SymbolType)) {
688 Symbol* buffered_symbol = (Symbol*)dest;
689 assert(((Symbol*)src)->is_permanent(), "archived symbols must be permanent");
690 buffered_symbol->update_identity_hash();
691 }
692
693 {
694 bool created;
695 _buffered_to_src_table.put_if_absent((address)dest, src, &created);
696 assert(created, "must be");
697 if (_buffered_to_src_table.maybe_grow()) {
698 log_info(aot, hashtables)("Expanded _buffered_to_src_table table to %d", _buffered_to_src_table.table_size());
699 }
700 }
701
702 intptr_t* archived_vtable = CppVtables::get_archived_vtable(src_info->msotype(), (address)dest);
703 if (archived_vtable != nullptr) {
704 *(address*)dest = (address)archived_vtable;
705 ArchivePtrMarker::mark_pointer((address*)dest);
706 }
707
708 log_trace(aot)("Copy: " PTR_FORMAT " ==> " PTR_FORMAT " %d", p2i(src), p2i(dest), bytes);
709 src_info->set_buffered_addr((address)dest);
710
711 char* newtop = dump_region->top();
712 _alloc_stats.record(src_info->msotype(), int(newtop - oldtop), src_info->read_only());
713
714 DEBUG_ONLY(_alloc_stats.verify((int)dump_region->used(), src_info->read_only()));
715 }
716
717 // This is used by code that hand-assembles data structures, such as the LambdaProxyClassKey, that are
718 // not handled by MetaspaceClosure.
719 void ArchiveBuilder::write_pointer_in_buffer(address* ptr_location, address src_addr) {
720 assert(is_in_buffer_space(ptr_location), "must be");
721 if (src_addr == nullptr) {
722 *ptr_location = nullptr;
723 ArchivePtrMarker::clear_pointer(ptr_location);
724 } else {
725 *ptr_location = get_buffered_addr(src_addr);
726 ArchivePtrMarker::mark_pointer(ptr_location);
727 }
728 }
729
730 void ArchiveBuilder::mark_and_relocate_to_buffered_addr(address* ptr_location) {
731 assert(*ptr_location != nullptr, "sanity");
732 if (!is_in_mapped_static_archive(*ptr_location)) {
733 *ptr_location = get_buffered_addr(*ptr_location);
734 }
735 ArchivePtrMarker::mark_pointer(ptr_location);
736 }
737
738 bool ArchiveBuilder::has_been_archived(address src_addr) const {
739 SourceObjInfo* p = _src_obj_table.get(src_addr);
740 if (p == nullptr) {
741 // This object has never been seen by ArchiveBuilder
742 return false;
743 }
744 if (p->buffered_addr() == nullptr) {
745 // ArchiveBuilder has seen this object, but decided not to archive it. So
746 // Any reference to this object will be modified to nullptr inside the buffer.
747 assert(p->follow_mode() == set_to_null, "must be");
748 return false;
749 }
750
751 DEBUG_ONLY({
752 // This is a class/method that belongs to one of the "original" classes that
753 // have been regenerated by lambdaFormInvokers.cpp. We must have archived
754 // the "regenerated" version of it.
755 if (RegeneratedClasses::has_been_regenerated(src_addr)) {
756 address regen_obj = RegeneratedClasses::get_regenerated_object(src_addr);
757 precond(regen_obj != nullptr && regen_obj != src_addr);
758 assert(has_been_archived(regen_obj), "must be");
759 assert(get_buffered_addr(src_addr) == get_buffered_addr(regen_obj), "must be");
760 }});
761
762 return true;
763 }
764
765 address ArchiveBuilder::get_buffered_addr(address src_addr) const {
766 SourceObjInfo* p = _src_obj_table.get(src_addr);
767 assert(p != nullptr, "src_addr " INTPTR_FORMAT " is used but has not been archived",
768 p2i(src_addr));
769
770 return p->buffered_addr();
771 }
772
773 address ArchiveBuilder::get_source_addr(address buffered_addr) const {
774 assert(is_in_buffer_space(buffered_addr), "must be");
775 address* src_p = _buffered_to_src_table.get(buffered_addr);
776 assert(src_p != nullptr && *src_p != nullptr, "must be");
777 return *src_p;
778 }
779
780 void ArchiveBuilder::relocate_embedded_pointers(ArchiveBuilder::SourceObjList* src_objs) {
781 for (int i = 0; i < src_objs->objs()->length(); i++) {
782 src_objs->relocate(i, this);
783 }
784 }
785
786 void ArchiveBuilder::relocate_metaspaceobj_embedded_pointers() {
787 aot_log_info(aot)("Relocating embedded pointers in core regions ... ");
788 relocate_embedded_pointers(&_rw_src_objs);
789 relocate_embedded_pointers(&_ro_src_objs);
790 log_info(cds)("Relocating %zu pointers, %zu tagged, %zu nulled",
791 _relocated_ptr_info._num_ptrs,
792 _relocated_ptr_info._num_tagged_ptrs,
793 _relocated_ptr_info._num_nulled_ptrs);
794 }
795
796 #define ADD_COUNT(x) \
797 x += 1; \
798 x ## _a += aotlinked ? 1 : 0; \
799 x ## _i += inited ? 1 : 0;
800
801 #define DECLARE_INSTANCE_KLASS_COUNTER(x) \
802 int x = 0; \
803 int x ## _a = 0; \
804 int x ## _i = 0;
805
806 void ArchiveBuilder::make_klasses_shareable() {
807 DECLARE_INSTANCE_KLASS_COUNTER(num_instance_klasses);
808 DECLARE_INSTANCE_KLASS_COUNTER(num_boot_klasses);
809 DECLARE_INSTANCE_KLASS_COUNTER(num_vm_klasses);
810 DECLARE_INSTANCE_KLASS_COUNTER(num_platform_klasses);
811 DECLARE_INSTANCE_KLASS_COUNTER(num_app_klasses);
812 DECLARE_INSTANCE_KLASS_COUNTER(num_old_klasses);
813 DECLARE_INSTANCE_KLASS_COUNTER(num_hidden_klasses);
814 DECLARE_INSTANCE_KLASS_COUNTER(num_enum_klasses);
815 DECLARE_INSTANCE_KLASS_COUNTER(num_unregistered_klasses);
816 int num_unlinked_klasses = 0;
817 int num_obj_array_klasses = 0;
818 int num_type_array_klasses = 0;
819
820 int boot_unlinked = 0;
821 int platform_unlinked = 0;
822 int app_unlinked = 0;
823 int unreg_unlinked = 0;
824
825 for (int i = 0; i < klasses()->length(); i++) {
826 // Some of the code in ConstantPool::remove_unshareable_info() requires the classes
827 // to be in linked state, so it must be call here before the next loop, which returns
828 // all classes to unlinked state.
829 Klass* k = get_buffered_addr(klasses()->at(i));
830 if (k->is_instance_klass()) {
831 InstanceKlass::cast(k)->constants()->remove_unshareable_info();
832 }
833 }
834
835 for (int i = 0; i < klasses()->length(); i++) {
836 const char* type;
837 const char* unlinked = "";
838 const char* kind = "";
839 const char* hidden = "";
840 const char* old = "";
841 const char* generated = "";
842 const char* aotlinked_msg = "";
843 const char* inited_msg = "";
844 Klass* k = get_buffered_addr(klasses()->at(i));
845 bool inited = false;
846 k->remove_java_mirror();
847 #ifdef _LP64
848 if (UseCompactObjectHeaders) {
849 Klass* requested_k = to_requested(k);
850 address narrow_klass_base = _requested_static_archive_bottom; // runtime encoding base == runtime mapping start
851 const int narrow_klass_shift = precomputed_narrow_klass_shift();
852 narrowKlass nk = CompressedKlassPointers::encode_not_null_without_asserts(requested_k, narrow_klass_base, narrow_klass_shift);
853 k->set_prototype_header(markWord::prototype().set_narrow_klass(nk));
854 }
855 #endif //_LP64
856 if (k->is_objArray_klass()) {
857 // InstanceKlass and TypeArrayKlass will in turn call remove_unshareable_info
858 // on their array classes.
859 num_obj_array_klasses ++;
860 type = "array";
861 } else if (k->is_typeArray_klass()) {
862 num_type_array_klasses ++;
863 type = "array";
864 k->remove_unshareable_info();
865 } else {
866 assert(k->is_instance_klass(), " must be");
867 InstanceKlass* ik = InstanceKlass::cast(k);
868 InstanceKlass* src_ik = get_source_addr(ik);
869 bool aotlinked = AOTClassLinker::is_candidate(src_ik);
870 inited = ik->has_aot_initialized_mirror();
871 ADD_COUNT(num_instance_klasses);
872 if (ik->is_hidden()) {
873 ADD_COUNT(num_hidden_klasses);
874 hidden = " hidden";
875 oop loader = k->class_loader();
876 if (loader == nullptr) {
877 type = "boot";
878 ADD_COUNT(num_boot_klasses);
879 } else if (loader == SystemDictionary::java_platform_loader()) {
880 type = "plat";
881 ADD_COUNT(num_platform_klasses);
882 } else if (loader == SystemDictionary::java_system_loader()) {
883 type = "app";
884 ADD_COUNT(num_app_klasses);
885 } else {
886 type = "bad";
887 assert(0, "shouldn't happen");
888 }
889 if (CDSConfig::is_dumping_method_handles()) {
890 assert(HeapShared::is_archivable_hidden_klass(ik), "sanity");
891 } else {
892 // Legacy CDS support for lambda proxies
893 CDS_JAVA_HEAP_ONLY(assert(HeapShared::is_lambda_proxy_klass(ik), "sanity");)
894 }
895 } else if (ik->defined_by_boot_loader()) {
896 type = "boot";
897 ADD_COUNT(num_boot_klasses);
898 } else if (ik->defined_by_platform_loader()) {
899 type = "plat";
900 ADD_COUNT(num_platform_klasses);
901 } else if (ik->defined_by_app_loader()) {
902 type = "app";
903 ADD_COUNT(num_app_klasses);
904 } else {
905 assert(ik->defined_by_other_loaders(), "must be");
906 type = "unreg";
907 ADD_COUNT(num_unregistered_klasses);
908 }
909
910 if (AOTClassLinker::is_vm_class(src_ik)) {
911 ADD_COUNT(num_vm_klasses);
912 }
913
914 if (!ik->is_linked()) {
915 num_unlinked_klasses ++;
916 unlinked = " unlinked";
917 if (ik->defined_by_boot_loader()) {
918 boot_unlinked ++;
919 } else if (ik->defined_by_platform_loader()) {
920 platform_unlinked ++;
921 } else if (ik->defined_by_app_loader()) {
922 app_unlinked ++;
923 } else {
924 unreg_unlinked ++;
925 }
926 }
927
928 if (ik->is_interface()) {
929 kind = " interface";
930 } else if (src_ik->is_enum_subclass()) {
931 kind = " enum";
932 ADD_COUNT(num_enum_klasses);
933 }
934
935 if (CDSConfig::is_old_class_for_verifier(ik)) {
936 ADD_COUNT(num_old_klasses);
937 old = " old";
938 }
939
940 if (ik->is_aot_generated_class()) {
941 generated = " generated";
942 }
943 if (aotlinked) {
944 aotlinked_msg = " aot-linked";
945 }
946 if (inited) {
947 if (InstanceKlass::cast(k)->static_field_size() == 0) {
948 inited_msg = " inited (no static fields)";
949 } else {
950 inited_msg = " inited";
951 }
952 }
953
954 AOTMetaspace::rewrite_bytecodes_and_calculate_fingerprints(Thread::current(), ik);
955 ik->remove_unshareable_info();
956 }
957
958 if (aot_log_is_enabled(Debug, aot, class)) {
959 ResourceMark rm;
960 aot_log_debug(aot, class)("klasses[%5d] = " PTR_FORMAT " %-5s %s%s%s%s%s%s%s%s", i,
961 p2i(to_requested(k)), type, k->external_name(),
962 kind, hidden, old, unlinked, generated, aotlinked_msg, inited_msg);
963 }
964 }
965
966 #define STATS_FORMAT "= %5d, aot-linked = %5d, inited = %5d"
967 #define STATS_PARAMS(x) num_ ## x, num_ ## x ## _a, num_ ## x ## _i
968
969 aot_log_info(aot)("Number of classes %d", num_instance_klasses + num_obj_array_klasses + num_type_array_klasses);
970 aot_log_info(aot)(" instance classes " STATS_FORMAT, STATS_PARAMS(instance_klasses));
971 aot_log_info(aot)(" boot " STATS_FORMAT, STATS_PARAMS(boot_klasses));
972 aot_log_info(aot)(" vm " STATS_FORMAT, STATS_PARAMS(vm_klasses));
973 aot_log_info(aot)(" platform " STATS_FORMAT, STATS_PARAMS(platform_klasses));
974 aot_log_info(aot)(" app " STATS_FORMAT, STATS_PARAMS(app_klasses));
975 aot_log_info(aot)(" unregistered " STATS_FORMAT, STATS_PARAMS(unregistered_klasses));
976 aot_log_info(aot)(" (enum) " STATS_FORMAT, STATS_PARAMS(enum_klasses));
977 aot_log_info(aot)(" (hidden) " STATS_FORMAT, STATS_PARAMS(hidden_klasses));
978 aot_log_info(aot)(" (old) " STATS_FORMAT, STATS_PARAMS(old_klasses));
979 aot_log_info(aot)(" (unlinked) = %5d, boot = %d, plat = %d, app = %d, unreg = %d",
980 num_unlinked_klasses, boot_unlinked, platform_unlinked, app_unlinked, unreg_unlinked);
981 aot_log_info(aot)(" obj array classes = %5d", num_obj_array_klasses);
982 aot_log_info(aot)(" type array classes = %5d", num_type_array_klasses);
983 aot_log_info(aot)(" symbols = %5d", _symbols->length());
984
985 #undef STATS_FORMAT
986 #undef STATS_PARAMS
987
988 DynamicArchive::make_array_klasses_shareable();
989 }
990
991 void ArchiveBuilder::make_training_data_shareable() {
992 auto clean_td = [&] (address& src_obj, SourceObjInfo& info) {
993 if (!is_in_buffer_space(info.buffered_addr())) {
994 return;
995 }
996
997 if (info.msotype() == MetaspaceObj::KlassTrainingDataType ||
998 info.msotype() == MetaspaceObj::MethodTrainingDataType ||
999 info.msotype() == MetaspaceObj::CompileTrainingDataType) {
1000 TrainingData* buffered_td = (TrainingData*)info.buffered_addr();
1001 buffered_td->remove_unshareable_info();
1002 } else if (info.msotype() == MetaspaceObj::MethodDataType) {
1003 MethodData* buffered_mdo = (MethodData*)info.buffered_addr();
1004 buffered_mdo->remove_unshareable_info();
1005 } else if (info.msotype() == MetaspaceObj::MethodCountersType) {
1006 MethodCounters* buffered_mc = (MethodCounters*)info.buffered_addr();
1007 buffered_mc->remove_unshareable_info();
1008 }
1009 };
1010 _src_obj_table.iterate_all(clean_td);
1011 }
1012
1013 uintx ArchiveBuilder::buffer_to_offset(address p) const {
1014 address requested_p = to_requested(p);
1015 assert(requested_p >= _requested_static_archive_bottom, "must be");
1016 return requested_p - _requested_static_archive_bottom;
1017 }
1018
1019 uintx ArchiveBuilder::any_to_offset(address p) const {
1020 if (is_in_mapped_static_archive(p)) {
1021 assert(CDSConfig::is_dumping_dynamic_archive(), "must be");
1022 return p - _mapped_static_archive_bottom;
1023 }
1024 if (!is_in_buffer_space(p)) {
1025 // p must be a "source" address
1026 p = get_buffered_addr(p);
1027 }
1028 return buffer_to_offset(p);
1029 }
1030
1031 address ArchiveBuilder::offset_to_buffered_address(u4 offset) const {
1032 address requested_addr = _requested_static_archive_bottom + offset;
1033 address buffered_addr = requested_addr - _buffer_to_requested_delta;
1034 assert(is_in_buffer_space(buffered_addr), "bad offset");
1035 return buffered_addr;
1036 }
1037
1038 void ArchiveBuilder::start_ac_region() {
1039 ro_region()->pack();
1040 start_dump_region(&_ac_region);
1041 }
1042
1043 void ArchiveBuilder::end_ac_region() {
1044 _ac_region.pack();
1045 }
1046
1047 #if INCLUDE_CDS_JAVA_HEAP
1048 narrowKlass ArchiveBuilder::get_requested_narrow_klass(Klass* k) {
1049 assert(CDSConfig::is_dumping_heap(), "sanity");
1050 k = get_buffered_klass(k);
1051 Klass* requested_k = to_requested(k);
1052 const int narrow_klass_shift = ArchiveBuilder::precomputed_narrow_klass_shift();
1053 #ifdef ASSERT
1054 const size_t klass_alignment = MAX2(SharedSpaceObjectAlignment, (size_t)nth_bit(narrow_klass_shift));
1055 assert(is_aligned(k, klass_alignment), "Klass " PTR_FORMAT " misaligned.", p2i(k));
1056 #endif
1057 address narrow_klass_base = _requested_static_archive_bottom; // runtime encoding base == runtime mapping start
1058 // Note: use the "raw" version of encode that takes explicit narrow klass base and shift. Don't use any
1059 // of the variants that do sanity checks, nor any of those that use the current - dump - JVM's encoding setting.
1060 return CompressedKlassPointers::encode_not_null_without_asserts(requested_k, narrow_klass_base, narrow_klass_shift);
1061 }
1062 #endif // INCLUDE_CDS_JAVA_HEAP
1063
1064 // RelocateBufferToRequested --- Relocate all the pointers in rw/ro,
1065 // so that the archive can be mapped to the "requested" location without runtime relocation.
1066 //
1067 // - See ArchiveBuilder header for the definition of "buffer", "mapped" and "requested"
1068 // - ArchivePtrMarker::ptrmap() marks all the pointers in the rw/ro regions
1069 // - Every pointer must have one of the following values:
1070 // [a] nullptr:
1071 // No relocation is needed. Remove this pointer from ptrmap so we don't need to
1072 // consider it at runtime.
1073 // [b] Points into an object X which is inside the buffer:
1074 // Adjust this pointer by _buffer_to_requested_delta, so it points to X
1075 // when the archive is mapped at the requested location.
1076 // [c] Points into an object Y which is inside mapped static archive:
1077 // - This happens only during dynamic dump
1078 // - Adjust this pointer by _mapped_to_requested_static_archive_delta,
1079 // so it points to Y when the static archive is mapped at the requested location.
1080 template <bool STATIC_DUMP>
1081 class RelocateBufferToRequested : public BitMapClosure {
1082 ArchiveBuilder* _builder;
1083 address _buffer_bottom;
1084 intx _buffer_to_requested_delta;
1085 intx _mapped_to_requested_static_archive_delta;
1086 size_t _max_non_null_offset;
1087
1088 public:
1089 RelocateBufferToRequested(ArchiveBuilder* builder) {
1090 _builder = builder;
1091 _buffer_bottom = _builder->buffer_bottom();
1092 _buffer_to_requested_delta = builder->buffer_to_requested_delta();
1093 _mapped_to_requested_static_archive_delta = builder->requested_static_archive_bottom() - builder->mapped_static_archive_bottom();
1094 _max_non_null_offset = 0;
1095
1096 address bottom = _builder->buffer_bottom();
1097 address top = _builder->buffer_top();
1098 address new_bottom = bottom + _buffer_to_requested_delta;
1099 address new_top = top + _buffer_to_requested_delta;
1100 aot_log_debug(aot)("Relocating archive from [" INTPTR_FORMAT " - " INTPTR_FORMAT "] to "
1101 "[" INTPTR_FORMAT " - " INTPTR_FORMAT "]",
1102 p2i(bottom), p2i(top),
1103 p2i(new_bottom), p2i(new_top));
1104 }
1105
1106 bool do_bit(size_t offset) {
1107 address* p = (address*)_buffer_bottom + offset;
1108 assert(_builder->is_in_buffer_space(p), "pointer must live in buffer space");
1109
1110 if (*p == nullptr) {
1111 // todo -- clear bit, etc
1112 ArchivePtrMarker::ptrmap()->clear_bit(offset);
1113 } else {
1114 if (STATIC_DUMP) {
1115 assert(_builder->is_in_buffer_space(*p), "old pointer must point inside buffer space");
1116 *p += _buffer_to_requested_delta;
1117 assert(_builder->is_in_requested_static_archive(*p), "new pointer must point inside requested archive");
1118 } else {
1119 if (_builder->is_in_buffer_space(*p)) {
1120 *p += _buffer_to_requested_delta;
1121 // assert is in requested dynamic archive
1122 } else {
1123 assert(_builder->is_in_mapped_static_archive(*p), "old pointer must point inside buffer space or mapped static archive");
1124 *p += _mapped_to_requested_static_archive_delta;
1125 assert(_builder->is_in_requested_static_archive(*p), "new pointer must point inside requested archive");
1126 }
1127 }
1128 _max_non_null_offset = offset;
1129 }
1130
1131 return true; // keep iterating
1132 }
1133
1134 void doit() {
1135 ArchivePtrMarker::ptrmap()->iterate(this);
1136 ArchivePtrMarker::compact(_max_non_null_offset);
1137 }
1138 };
1139
1140 #ifdef _LP64
1141 int ArchiveBuilder::precomputed_narrow_klass_shift() {
1142 // Legacy Mode:
1143 // We use 32 bits for narrowKlass, which should cover the full 4G Klass range. Shift can be 0.
1144 // CompactObjectHeader Mode:
1145 // narrowKlass is much smaller, and we use the highest possible shift value to later get the maximum
1146 // Klass encoding range.
1147 //
1148 // Note that all of this may change in the future, if we decide to correct the pre-calculated
1149 // narrow Klass IDs at archive load time.
1150 assert(UseCompressedClassPointers, "Only needed for compressed class pointers");
1151 return UseCompactObjectHeaders ? CompressedKlassPointers::max_shift() : 0;
1152 }
1153 #endif // _LP64
1154
1155 void ArchiveBuilder::relocate_to_requested() {
1156 if (!ro_region()->is_packed()) {
1157 ro_region()->pack();
1158 }
1159 size_t my_archive_size = buffer_top() - buffer_bottom();
1160
1161 if (CDSConfig::is_dumping_static_archive()) {
1162 _requested_static_archive_top = _requested_static_archive_bottom + my_archive_size;
1163 RelocateBufferToRequested<true> patcher(this);
1164 patcher.doit();
1165 } else {
1166 assert(CDSConfig::is_dumping_dynamic_archive(), "must be");
1167 _requested_dynamic_archive_top = _requested_dynamic_archive_bottom + my_archive_size;
1168 RelocateBufferToRequested<false> patcher(this);
1169 patcher.doit();
1170 }
1171 }
1172
1173 void ArchiveBuilder::print_stats() {
1174 _alloc_stats.print_stats(int(_ro_region.used()), int(_rw_region.used()));
1175 }
1176
1177 void ArchiveBuilder::write_archive(FileMapInfo* mapinfo, ArchiveMappedHeapInfo* mapped_heap_info, ArchiveStreamedHeapInfo* streamed_heap_info) {
1178 // Make sure NUM_CDS_REGIONS (exported in cds.h) agrees with
1179 // AOTMetaspace::n_regions (internal to hotspot).
1180 assert(NUM_CDS_REGIONS == AOTMetaspace::n_regions, "sanity");
1181
1182 ResourceMark rm;
1183
1184 write_region(mapinfo, AOTMetaspace::rw, &_rw_region, /*read_only=*/false,/*allow_exec=*/false);
1185 write_region(mapinfo, AOTMetaspace::ro, &_ro_region, /*read_only=*/true, /*allow_exec=*/false);
1186 write_region(mapinfo, AOTMetaspace::ac, &_ac_region, /*read_only=*/false,/*allow_exec=*/false);
1187
1188 // Split pointer map into read-write and read-only bitmaps
1189 ArchivePtrMarker::initialize_rw_ro_maps(&_rw_ptrmap, &_ro_ptrmap);
1190
1191 size_t bitmap_size_in_bytes;
1192 char* bitmap = mapinfo->write_bitmap_region(ArchivePtrMarker::rw_ptrmap(),
1193 ArchivePtrMarker::ro_ptrmap(),
1194 mapped_heap_info,
1195 streamed_heap_info,
1196 bitmap_size_in_bytes);
1197
1198 if (mapped_heap_info != nullptr && mapped_heap_info->is_used()) {
1199 _total_heap_region_size = mapinfo->write_mapped_heap_region(mapped_heap_info);
1200 } else if (streamed_heap_info != nullptr && streamed_heap_info->is_used()) {
1201 _total_heap_region_size = mapinfo->write_streamed_heap_region(streamed_heap_info);
1202 }
1203
1204 print_region_stats(mapinfo, mapped_heap_info, streamed_heap_info);
1205
1206 mapinfo->set_requested_base((char*)AOTMetaspace::requested_base_address());
1207 mapinfo->set_header_crc(mapinfo->compute_header_crc());
1208 // After this point, we should not write any data into mapinfo->header() since this
1209 // would corrupt its checksum we have calculated before.
1210 mapinfo->write_header();
1211 mapinfo->close();
1212
1213 if (log_is_enabled(Info, aot)) {
1214 log_info(aot)("Full module graph = %s", CDSConfig::is_dumping_full_module_graph() ? "enabled" : "disabled");
1215 print_stats();
1216 }
1217
1218 if (log_is_enabled(Info, aot, map)) {
1219 AOTMapLogger::dumptime_log(this, mapinfo, mapped_heap_info, streamed_heap_info, bitmap, bitmap_size_in_bytes);
1220 }
1221 CDS_JAVA_HEAP_ONLY(HeapShared::destroy_archived_object_cache());
1222 FREE_C_HEAP_ARRAY(char, bitmap);
1223 }
1224
1225 void ArchiveBuilder::write_region(FileMapInfo* mapinfo, int region_idx, DumpRegion* dump_region, bool read_only, bool allow_exec) {
1226 mapinfo->write_region(region_idx, dump_region->base(), dump_region->used(), read_only, allow_exec);
1227 }
1228
1229 void ArchiveBuilder::count_relocated_pointer(bool tagged, bool nulled) {
1230 _relocated_ptr_info._num_ptrs ++;
1231 _relocated_ptr_info._num_tagged_ptrs += tagged ? 1 : 0;
1232 _relocated_ptr_info._num_nulled_ptrs += nulled ? 1 : 0;
1233 }
1234
1235 void ArchiveBuilder::print_region_stats(FileMapInfo *mapinfo,
1236 ArchiveMappedHeapInfo* mapped_heap_info,
1237 ArchiveStreamedHeapInfo* streamed_heap_info) {
1238 // Print statistics of all the regions
1239 const size_t bitmap_used = mapinfo->region_at(AOTMetaspace::bm)->used();
1240 const size_t bitmap_reserved = mapinfo->region_at(AOTMetaspace::bm)->used_aligned();
1241 const size_t total_reserved = _ro_region.reserved() + _rw_region.reserved() +
1242 bitmap_reserved +
1243 _total_heap_region_size;
1244 const size_t total_bytes = _ro_region.used() + _rw_region.used() +
1245 bitmap_used +
1246 _total_heap_region_size;
1247 const double total_u_perc = percent_of(total_bytes, total_reserved);
1248
1249 _rw_region.print(total_reserved);
1250 _ro_region.print(total_reserved);
1251 _ac_region.print(total_reserved);
1252
1253 print_bitmap_region_stats(bitmap_used, total_reserved);
1254
1255 if (mapped_heap_info != nullptr && mapped_heap_info->is_used()) {
1256 print_heap_region_stats(mapped_heap_info->buffer_start(), mapped_heap_info->buffer_byte_size(), total_reserved);
1257 } else if (streamed_heap_info != nullptr && streamed_heap_info->is_used()) {
1258 print_heap_region_stats(streamed_heap_info->buffer_start(), streamed_heap_info->buffer_byte_size(), total_reserved);
1259 }
1260
1261 aot_log_debug(aot)("total : %9zu [100.0%% of total] out of %9zu bytes [%5.1f%% used]",
1262 total_bytes, total_reserved, total_u_perc);
1263 }
1264
1265 void ArchiveBuilder::print_bitmap_region_stats(size_t size, size_t total_size) {
1266 aot_log_debug(aot)("bm space: %9zu [ %4.1f%% of total] out of %9zu bytes [100.0%% used]",
1267 size, size/double(total_size)*100.0, size);
1268 }
1269
1270 void ArchiveBuilder::print_heap_region_stats(char* start, size_t size, size_t total_size) {
1271 char* top = start + size;
1272 aot_log_debug(aot)("hp space: %9zu [ %4.1f%% of total] out of %9zu bytes [100.0%% used] at " INTPTR_FORMAT,
1273 size, size/double(total_size)*100.0, size, p2i(start));
1274 }
1275
1276 void ArchiveBuilder::report_out_of_space(const char* name, size_t needed_bytes) {
1277 // This is highly unlikely to happen on 64-bits because we have reserved a 4GB space.
1278 // On 32-bit we reserve only 256MB so you could run out of space with 100,000 classes
1279 // or so.
1280 _rw_region.print_out_of_space_msg(name, needed_bytes);
1281 _ro_region.print_out_of_space_msg(name, needed_bytes);
1282
1283 log_error(aot)("Unable to allocate from '%s' region: Please reduce the number of shared classes.", name);
1284 AOTMetaspace::unrecoverable_writing_error();
1285 }