1 /*
  2  * Copyright (c) 2019, 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/aotClassLocation.hpp"
 28 #include "cds/aotLogging.hpp"
 29 #include "cds/aotMetaspace.hpp"
 30 #include "cds/archiveBuilder.hpp"
 31 #include "cds/archiveHeapWriter.hpp"
 32 #include "cds/archiveUtils.inline.hpp"
 33 #include "cds/cds_globals.hpp"
 34 #include "cds/cdsConfig.hpp"
 35 #include "cds/dynamicArchive.hpp"
 36 #include "cds/lambdaFormInvokers.hpp"
 37 #include "cds/lambdaProxyClassDictionary.hpp"
 38 #include "cds/regeneratedClasses.hpp"
 39 #include "classfile/classLoader.hpp"
 40 #include "classfile/classLoaderData.inline.hpp"
 41 #include "classfile/symbolTable.hpp"
 42 #include "classfile/systemDictionaryShared.hpp"
 43 #include "classfile/vmSymbols.hpp"
 44 #include "gc/shared/collectedHeap.hpp"
 45 #include "gc/shared/gc_globals.hpp"
 46 #include "gc/shared/gcVMOperations.hpp"
 47 #include "jvm.h"
 48 #include "logging/log.hpp"
 49 #include "memory/metaspaceClosure.hpp"
 50 #include "memory/resourceArea.hpp"
 51 #include "oops/klass.inline.hpp"
 52 #include "runtime/arguments.hpp"
 53 #include "runtime/os.hpp"
 54 #include "runtime/sharedRuntime.hpp"
 55 #include "runtime/vmOperations.hpp"
 56 #include "runtime/vmThread.hpp"
 57 #include "utilities/align.hpp"
 58 #include "utilities/bitMap.inline.hpp"
 59 
 60 
 61 class DynamicArchiveBuilder : public ArchiveBuilder {
 62   const char* _archive_name;
 63 public:
 64   DynamicArchiveBuilder(const char* archive_name) : _archive_name(archive_name) {}
 65   void mark_pointer(address* ptr_loc) {
 66     ArchivePtrMarker::mark_pointer(ptr_loc);
 67   }
 68 
 69   static int dynamic_dump_method_comparator(Method* a, Method* b) {
 70     Symbol* a_name = a->name();
 71     Symbol* b_name = b->name();
 72 
 73     if (a_name == b_name) {
 74       return 0;
 75     }
 76 
 77     u4 a_offset = ArchiveBuilder::current()->any_to_offset_u4(a_name);
 78     u4 b_offset = ArchiveBuilder::current()->any_to_offset_u4(b_name);
 79 
 80     if (a_offset < b_offset) {
 81       return -1;
 82     } else {
 83       assert(a_offset > b_offset, "must be");
 84       return 1;
 85     }
 86   }
 87 
 88 public:
 89   DynamicArchiveHeader *_header;
 90 
 91   void init_header();
 92   void release_header();
 93   void post_dump();
 94   void sort_methods();
 95   void sort_methods(InstanceKlass* ik) const;
 96   void remark_pointers_for_instance_klass(InstanceKlass* k, bool should_mark) const;
 97   void write_archive(char* serialized_data, AOTClassLocationConfig* cl_config);
 98   void gather_array_klasses();
 99 
100 public:
101   // Do this before and after the archive dump to see if any corruption
102   // is caused by dynamic dumping.
103   void verify_universe(const char* info) {
104     if (VerifyBeforeExit) {
105       log_info(aot)("Verify %s", info);
106       // Among other things, this ensures that Eden top is correct.
107       Universe::heap()->prepare_for_verify();
108       Universe::verify(info);
109     }
110   }
111 
112   void doit() {
113     CDSConfig::set_is_at_aot_safepoint(true);
114     doit_inner();
115     CDSConfig::set_is_at_aot_safepoint(false);
116   }
117 
118   void doit_inner() {
119     verify_universe("Before CDS dynamic dump");
120     DEBUG_ONLY(SystemDictionaryShared::NoClassLoadingMark nclm);
121 
122     // Block concurrent class unloading from changing the _dumptime_table
123     MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
124 
125     if (SystemDictionaryShared::is_dumptime_table_empty()) {
126       log_warning(cds, dynamic)("There is no class to be included in the dynamic archive.");
127       return;
128     }
129 
130     log_info(cds, dynamic)("CDS dynamic dump: clinit = " JLONG_FORMAT "ms)",
131                            ClassLoader::class_init_time_ms());
132 
133     init_header();
134     gather_source_objs();
135     gather_array_klasses();
136     reserve_buffer();
137 
138     log_info(cds, dynamic)("Copying %d klasses and %d symbols",
139                            klasses()->length(), symbols()->length());
140     dump_rw_metadata();
141     dump_ro_metadata();
142     relocate_metaspaceobj_embedded_pointers();
143 
144     sort_methods();
145 
146     log_info(aot)("Make classes shareable");
147     make_klasses_shareable();
148 
149     char* serialized_data;
150     AOTClassLocationConfig* cl_config;
151     {
152       // Write the symbol table and system dictionaries to the RO space.
153       // Note that these tables still point to the *original* objects, so
154       // they would need to call DynamicArchive::original_to_target() to
155       // get the correct addresses.
156       assert(current_dump_region() == ro_region(), "Must be RO space");
157       SymbolTable::write_to_archive(symbols());
158 
159       ArchiveBuilder::OtherROAllocMark mark;
160       SystemDictionaryShared::write_to_archive(false);
161       cl_config = AOTClassLocationConfig::dumptime()->write_to_archive();
162       DynamicArchive::dump_array_klasses();
163 
164       serialized_data = ro_region()->top();
165       WriteClosure wc(ro_region());
166       DynamicArchive::serialize(&wc);
167     }
168 
169     if (CDSConfig::is_dumping_lambdas_in_legacy_mode()) {
170       log_info(aot)("Adjust lambda proxy class dictionary");
171       LambdaProxyClassDictionary::adjust_dumptime_table();
172     }
173 
174     relocate_to_requested();
175 
176     write_archive(serialized_data, cl_config);
177     release_header();
178     DynamicArchive::post_dump();
179 
180     post_dump();
181 
182     verify_universe("After CDS dynamic dump");
183   }
184 
185   virtual void iterate_roots(MetaspaceClosure* it) {
186     AOTArtifactFinder::all_cached_classes_do(it);
187     SystemDictionaryShared::dumptime_classes_do(it);
188     iterate_primitive_array_klasses(it);
189   }
190 
191   void iterate_primitive_array_klasses(MetaspaceClosure* it) {
192     for (int i = T_BOOLEAN; i <= T_LONG; i++) {
193       assert(is_java_primitive((BasicType)i), "sanity");
194       Klass* k = Universe::typeArrayKlass((BasicType)i);  // this give you "[I", etc
195       assert(AOTMetaspace::in_aot_cache_static_region((void*)k),
196         "one-dimensional primitive array should be in static archive");
197       ArrayKlass* ak = ArrayKlass::cast(k);
198       while (ak != nullptr && ak->in_aot_cache()) {
199         Klass* next_k = ak->array_klass_or_null();
200         if (next_k != nullptr) {
201           ak = ArrayKlass::cast(next_k);
202         } else {
203           ak = nullptr;
204         }
205       }
206       if (ak != nullptr) {
207         assert(ak->dimension() > 1, "sanity");
208         // this is the lowest dimension that's not in the static archive
209         it->push(&ak);
210       }
211     }
212   }
213 };
214 
215 void DynamicArchiveBuilder::init_header() {
216   FileMapInfo* mapinfo = new FileMapInfo(_archive_name, false);
217   assert(FileMapInfo::dynamic_info() == mapinfo, "must be");
218   FileMapInfo* base_info = FileMapInfo::current_info();
219   // header only be available after populate_header
220   mapinfo->populate_header(base_info->core_region_alignment());
221   _header = mapinfo->dynamic_header();
222 
223   _header->set_base_header_crc(base_info->crc());
224   for (int i = 0; i < AOTMetaspace::n_regions; i++) {
225     _header->set_base_region_crc(i, base_info->region_crc(i));
226   }
227 }
228 
229 void DynamicArchiveBuilder::release_header() {
230   // We temporarily allocated a dynamic FileMapInfo for dumping, which makes it appear we
231   // have mapped a dynamic archive, but we actually have not. We are in a safepoint now.
232   // Let's free it so that if class loading happens after we leave the safepoint, nothing
233   // bad will happen.
234   assert(SafepointSynchronize::is_at_safepoint(), "must be");
235   FileMapInfo *mapinfo = FileMapInfo::dynamic_info();
236   assert(mapinfo != nullptr && _header == mapinfo->dynamic_header(), "must be");
237   delete mapinfo;
238   assert(!DynamicArchive::is_mapped(), "must be");
239   _header = nullptr;
240 }
241 
242 void DynamicArchiveBuilder::post_dump() {
243   ArchivePtrMarker::reset_map_and_vs();
244   AOTClassLinker::dispose();
245 }
246 
247 void DynamicArchiveBuilder::sort_methods() {
248   InstanceKlass::disable_method_binary_search();
249   for (int i = 0; i < klasses()->length(); i++) {
250     Klass* k = get_buffered_addr(klasses()->at(i));
251     if (k->is_instance_klass()) {
252       sort_methods(InstanceKlass::cast(k));
253     }
254   }
255 }
256 
257 // The address order of the copied Symbols may be different than when the original
258 // klasses were created. Re-sort all the tables. See Method::sort_methods().
259 void DynamicArchiveBuilder::sort_methods(InstanceKlass* ik) const {
260   assert(ik != nullptr, "DynamicArchiveBuilder currently doesn't support dumping the base archive");
261   if (AOTMetaspace::in_aot_cache(ik)) {
262     // We have reached a supertype that's already in the base archive
263     return;
264   }
265   assert(is_in_buffer_space(ik), "method sorting must be done on buffered class, not original class");
266   if (ik->java_mirror() == nullptr) {
267     // null mirror means this class has already been visited and methods are already sorted
268     return;
269   }
270   ik->remove_java_mirror();
271 
272   if (log_is_enabled(Debug, cds, dynamic)) {
273     ResourceMark rm;
274     log_debug(cds, dynamic)("sorting methods for " PTR_FORMAT " (" PTR_FORMAT ") %s",
275                             p2i(ik), p2i(to_requested(ik)), ik->external_name());
276   }
277 
278   // Method sorting may re-layout the [iv]tables, which would change the offset(s)
279   // of the locations in an InstanceKlass that would contain pointers. Let's clear
280   // all the existing pointer marking bits, and re-mark the pointers after sorting.
281   remark_pointers_for_instance_klass(ik, false);
282 
283   // Make sure all supertypes have been sorted
284   sort_methods(ik->super());
285   Array<InstanceKlass*>* interfaces = ik->local_interfaces();
286   int len = interfaces->length();
287   for (int i = 0; i < len; i++) {
288     sort_methods(interfaces->at(i));
289   }
290 
291 #ifdef ASSERT
292   if (ik->methods() != nullptr) {
293     for (int m = 0; m < ik->methods()->length(); m++) {
294       Symbol* name = ik->methods()->at(m)->name();
295       assert(AOTMetaspace::in_aot_cache(name) || is_in_buffer_space(name), "must be");
296     }
297   }
298   if (ik->default_methods() != nullptr) {
299     for (int m = 0; m < ik->default_methods()->length(); m++) {
300       Symbol* name = ik->default_methods()->at(m)->name();
301       assert(AOTMetaspace::in_aot_cache(name) || is_in_buffer_space(name), "must be");
302     }
303   }
304 #endif
305 
306   Method::sort_methods(ik->methods(), /*set_idnums=*/true, dynamic_dump_method_comparator);
307   if (ik->default_methods() != nullptr) {
308     Method::sort_methods(ik->default_methods(), /*set_idnums=*/false, dynamic_dump_method_comparator);
309   }
310   if (ik->is_linked()) {
311     // If the class has already been linked, we must relayout the i/v tables, whose order depends
312     // on the method sorting order.
313     // If the class is unlinked, we cannot layout the i/v tables yet. This is OK, as the
314     // i/v tables will be initialized at runtime after bytecode verification.
315     ik->vtable().initialize_vtable();
316     ik->itable().initialize_itable();
317   }
318 
319   // Set all the pointer marking bits after sorting.
320   remark_pointers_for_instance_klass(ik, true);
321 }
322 
323 template<bool should_mark>
324 class PointerRemarker: public MetaspaceClosure {
325 public:
326   virtual bool do_ref(Ref* ref, bool read_only) {
327     if (should_mark) {
328       ArchivePtrMarker::mark_pointer(ref->addr());
329     } else {
330       ArchivePtrMarker::clear_pointer(ref->addr());
331     }
332     return false; // don't recurse
333   }
334 };
335 
336 void DynamicArchiveBuilder::remark_pointers_for_instance_klass(InstanceKlass* k, bool should_mark) const {
337   if (should_mark) {
338     PointerRemarker<true> marker;
339     k->metaspace_pointers_do(&marker);
340     marker.finish();
341   } else {
342     PointerRemarker<false> marker;
343     k->metaspace_pointers_do(&marker);
344     marker.finish();
345   }
346 }
347 
348 void DynamicArchiveBuilder::write_archive(char* serialized_data, AOTClassLocationConfig* cl_config) {
349   _header->set_class_location_config(cl_config);
350   _header->set_serialized_data(serialized_data);
351 
352   FileMapInfo* dynamic_info = FileMapInfo::dynamic_info();
353   assert(dynamic_info != nullptr, "Sanity");
354 
355   dynamic_info->open_as_output();
356   ArchiveHeapInfo no_heap_for_dynamic_dump;
357   ArchiveBuilder::write_archive(dynamic_info, &no_heap_for_dynamic_dump);
358 
359   address base = _requested_dynamic_archive_bottom;
360   address top  = _requested_dynamic_archive_top;
361   size_t file_size = pointer_delta(top, base, sizeof(char));
362 
363   log_info(cds, dynamic)("Written dynamic archive " PTR_FORMAT " - " PTR_FORMAT
364                          " [" UINT32_FORMAT " bytes header, %zu bytes total]",
365                          p2i(base), p2i(top), _header->header_size(), file_size);
366 
367   log_info(cds, dynamic)("%d klasses; %d symbols", klasses()->length(), symbols()->length());
368 }
369 
370 void DynamicArchiveBuilder::gather_array_klasses() {
371   for (int i = 0; i < klasses()->length(); i++) {
372     if (klasses()->at(i)->is_objArray_klass()) {
373       ObjArrayKlass* oak = ObjArrayKlass::cast(klasses()->at(i));
374       Klass* elem = oak->element_klass();
375       if (AOTMetaspace::in_aot_cache_static_region(elem)) {
376         // Only capture the array klass whose element_klass is in the static archive.
377         // During run time, setup (see DynamicArchive::setup_array_klasses()) is needed
378         // so that the element_klass can find its array klasses from the dynamic archive.
379         DynamicArchive::append_array_klass(oak);
380       } else {
381         // The element_klass and its array klasses are in the same archive.
382         assert(!AOTMetaspace::in_aot_cache_static_region(oak),
383           "we should not gather klasses that are already in the static archive");
384       }
385     }
386   }
387   log_debug(aot)("Total array klasses gathered for dynamic archive: %d", DynamicArchive::num_array_klasses());
388 }
389 
390 class VM_PopulateDynamicDumpSharedSpace: public VM_Heap_Sync_Operation {
391   DynamicArchiveBuilder _builder;
392 public:
393   VM_PopulateDynamicDumpSharedSpace(const char* archive_name)
394   : VM_Heap_Sync_Operation(), _builder(archive_name) {}
395   VMOp_Type type() const { return VMOp_PopulateDumpSharedSpace; }
396   void doit() {
397     ResourceMark rm;
398     AOTClassLocationConfig::dumptime_check_nonempty_dirs();
399     _builder.doit();
400   }
401   ~VM_PopulateDynamicDumpSharedSpace() {
402     RegeneratedClasses::cleanup();
403   }
404 };
405 
406 // _array_klasses and _dynamic_archive_array_klasses only hold the array klasses
407 // which have element klass in the static archive.
408 GrowableArray<ObjArrayKlass*>* DynamicArchive::_array_klasses = nullptr;
409 Array<ObjArrayKlass*>* DynamicArchive::_dynamic_archive_array_klasses = nullptr;
410 
411 void DynamicArchive::serialize(SerializeClosure* soc) {
412   SymbolTable::serialize_shared_table_header(soc, false);
413   SystemDictionaryShared::serialize_dictionary_headers(soc, false);
414   soc->do_ptr(&_dynamic_archive_array_klasses);
415 }
416 
417 void DynamicArchive::append_array_klass(ObjArrayKlass* ak) {
418   if (_array_klasses == nullptr) {
419     _array_klasses = new (mtClassShared) GrowableArray<ObjArrayKlass*>(50, mtClassShared);
420   }
421   _array_klasses->append(ak);
422 }
423 
424 void DynamicArchive::dump_array_klasses() {
425   assert(CDSConfig::is_dumping_dynamic_archive(), "sanity");
426   if (_array_klasses != nullptr) {
427     ArchiveBuilder* builder = ArchiveBuilder::current();
428     int num_array_klasses = _array_klasses->length();
429     _dynamic_archive_array_klasses =
430         ArchiveBuilder::new_ro_array<ObjArrayKlass*>(num_array_klasses);
431     for (int i = 0; i < num_array_klasses; i++) {
432       builder->write_pointer_in_buffer(_dynamic_archive_array_klasses->adr_at(i), _array_klasses->at(i));
433     }
434   }
435 }
436 
437 void DynamicArchive::setup_array_klasses() {
438   if (_dynamic_archive_array_klasses != nullptr) {
439     for (int i = 0; i < _dynamic_archive_array_klasses->length(); i++) {
440       ObjArrayKlass* oak = _dynamic_archive_array_klasses->at(i);
441       Klass* elm = oak->element_klass();
442       assert(AOTMetaspace::in_aot_cache_static_region((void*)elm), "must be");
443 
444       if (elm->is_instance_klass()) {
445         assert(InstanceKlass::cast(elm)->array_klasses() == nullptr, "must be");
446         InstanceKlass::cast(elm)->set_array_klasses(oak);
447       } else {
448         assert(elm->is_array_klass(), "sanity");
449         assert(ArrayKlass::cast(elm)->higher_dimension() == nullptr, "must be");
450         ArrayKlass::cast(elm)->set_higher_dimension(oak);
451       }
452     }
453     log_debug(aot)("Total array klasses read from dynamic archive: %d", _dynamic_archive_array_klasses->length());
454   }
455 }
456 
457 void DynamicArchive::make_array_klasses_shareable() {
458   if (_array_klasses != nullptr) {
459     int num_array_klasses = _array_klasses->length();
460     for (int i = 0; i < num_array_klasses; i++) {
461       ObjArrayKlass* k = ArchiveBuilder::current()->get_buffered_addr(_array_klasses->at(i));
462       k->remove_unshareable_info();
463     }
464   }
465 }
466 
467 void DynamicArchive::post_dump() {
468   if (_array_klasses != nullptr) {
469     delete _array_klasses;
470     _array_klasses = nullptr;
471   }
472 }
473 
474 int DynamicArchive::num_array_klasses() {
475   return _array_klasses != nullptr ? _array_klasses->length() : 0;
476 }
477 
478 void DynamicArchive::dump_impl(bool jcmd_request, const char* archive_name, TRAPS) {
479   AOTMetaspace::link_shared_classes(CHECK);
480   if (!jcmd_request && CDSConfig::is_dumping_regenerated_lambdaform_invokers()) {
481     LambdaFormInvokers::regenerate_holder_classes(CHECK);
482   }
483 
484   VM_PopulateDynamicDumpSharedSpace op(archive_name);
485   VMThread::execute(&op);
486 }
487 
488 void DynamicArchive::dump_at_exit(JavaThread* current) {
489   ExceptionMark em(current);
490   ResourceMark rm(current);
491   CDSConfig::DumperThreadMark dumper_thread_mark(current);
492 
493   const char* archive_name = CDSConfig::output_archive_path();
494   if (!CDSConfig::is_dumping_dynamic_archive() || archive_name == nullptr) {
495     return;
496   }
497 
498   log_info(cds, dynamic)("Preparing for dynamic dump at exit in thread %s", current->name());
499 
500   JavaThread* THREAD = current; // For TRAPS processing related to link_shared_classes
501   dump_impl(/*jcmd_request=*/false, archive_name, THREAD);
502   if (HAS_PENDING_EXCEPTION) {
503     // One of the prepatory steps failed
504     oop ex = current->pending_exception();
505     aot_log_error(aot)("Dynamic dump has failed");
506     aot_log_error(aot)("%s: %s", ex->klass()->external_name(),
507                    java_lang_String::as_utf8_string(java_lang_Throwable::message(ex)));
508     CLEAR_PENDING_EXCEPTION;
509     CDSConfig::disable_dumping_dynamic_archive();  // Just for good measure
510   }
511 }
512 
513 // This is called by "jcmd VM.cds dynamic_dump"
514 void DynamicArchive::dump_for_jcmd(const char* archive_name, TRAPS) {
515   CDSConfig::DumperThreadMark dumper_thread_mark(THREAD);
516   assert(CDSConfig::is_using_archive() && RecordDynamicDumpInfo, "already checked in arguments.cpp");
517   assert(ArchiveClassesAtExit == nullptr, "already checked in arguments.cpp");
518   assert(CDSConfig::is_dumping_dynamic_archive(), "already checked by check_for_dynamic_dump() during VM startup");
519   dump_impl(/*jcmd_request=*/true, archive_name, CHECK);
520 }
521 
522 bool DynamicArchive::validate(FileMapInfo* dynamic_info) {
523   assert(!dynamic_info->is_static(), "must be");
524   // Check if the recorded base archive matches with the current one
525   FileMapInfo* base_info = FileMapInfo::current_info();
526   DynamicArchiveHeader* dynamic_header = dynamic_info->dynamic_header();
527 
528   // Check the header crc
529   if (dynamic_header->base_header_crc() != base_info->crc()) {
530     aot_log_warning(aot)("Dynamic archive cannot be used: static archive header checksum verification failed.");
531     return false;
532   }
533 
534   // Check each space's crc
535   for (int i = 0; i < AOTMetaspace::n_regions; i++) {
536     if (dynamic_header->base_region_crc(i) != base_info->region_crc(i)) {
537       aot_log_warning(aot)("Dynamic archive cannot be used: static archive region #%d checksum verification failed.", i);
538       return false;
539     }
540   }
541 
542   return true;
543 }
544 
545 void DynamicArchiveHeader::print(outputStream* st) {
546   ResourceMark rm;
547 
548   st->print_cr("- base_header_crc:                0x%08x", base_header_crc());
549   for (int i = 0; i < NUM_CDS_REGIONS; i++) {
550     st->print_cr("- base_region_crc[%d]:             0x%08x", i, base_region_crc(i));
551   }
552 }