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       if (oak->is_refined_objArray_klass()) {
375         oak = ObjArrayKlass::cast(oak->super());
376       }
377       Klass* elem = oak->element_klass();
378       if (AOTMetaspace::in_aot_cache_static_region(elem)) {
379         // Only capture the array klass whose element_klass is in the static archive.
380         // During run time, setup (see DynamicArchive::setup_array_klasses()) is needed
381         // so that the element_klass can find its array klasses from the dynamic archive.
382         DynamicArchive::append_array_klass(oak);
383       } else {
384         // The element_klass and its array klasses are in the same archive.
385         assert(!AOTMetaspace::in_aot_cache_static_region(oak),
386           "we should not gather klasses that are already in the static archive");
387       }
388     }
389   }
390   log_debug(aot)("Total array klasses gathered for dynamic archive: %d", DynamicArchive::num_array_klasses());
391 }
392 
393 class VM_PopulateDynamicDumpSharedSpace: public VM_Heap_Sync_Operation {
394   DynamicArchiveBuilder _builder;
395 public:
396   VM_PopulateDynamicDumpSharedSpace(const char* archive_name)
397   : VM_Heap_Sync_Operation(), _builder(archive_name) {}
398   VMOp_Type type() const { return VMOp_PopulateDumpSharedSpace; }
399   void doit() {
400     ResourceMark rm;
401     AOTClassLocationConfig::dumptime_check_nonempty_dirs();
402     _builder.doit();
403   }
404   ~VM_PopulateDynamicDumpSharedSpace() {
405     RegeneratedClasses::cleanup();
406   }
407 };
408 
409 // _array_klasses and _dynamic_archive_array_klasses only hold the array klasses
410 // which have element klass in the static archive.
411 GrowableArray<ObjArrayKlass*>* DynamicArchive::_array_klasses = nullptr;
412 Array<ObjArrayKlass*>* DynamicArchive::_dynamic_archive_array_klasses = nullptr;
413 
414 void DynamicArchive::serialize(SerializeClosure* soc) {
415   SymbolTable::serialize_shared_table_header(soc, false);
416   SystemDictionaryShared::serialize_dictionary_headers(soc, false);
417   soc->do_ptr(&_dynamic_archive_array_klasses);
418 }
419 
420 void DynamicArchive::append_array_klass(ObjArrayKlass* ak) {
421   if (_array_klasses == nullptr) {
422     _array_klasses = new (mtClassShared) GrowableArray<ObjArrayKlass*>(50, mtClassShared);
423   }
424   _array_klasses->append(ak);
425 }
426 
427 void DynamicArchive::dump_array_klasses() {
428   assert(CDSConfig::is_dumping_dynamic_archive(), "sanity");
429   if (_array_klasses != nullptr) {
430     ArchiveBuilder* builder = ArchiveBuilder::current();
431     int num_array_klasses = _array_klasses->length();
432     _dynamic_archive_array_klasses =
433         ArchiveBuilder::new_ro_array<ObjArrayKlass*>(num_array_klasses);
434     for (int i = 0; i < num_array_klasses; i++) {
435       builder->write_pointer_in_buffer(_dynamic_archive_array_klasses->adr_at(i), _array_klasses->at(i));
436     }
437   }
438 }
439 
440 void DynamicArchive::setup_array_klasses() {
441   if (_dynamic_archive_array_klasses != nullptr) {
442     for (int i = 0; i < _dynamic_archive_array_klasses->length(); i++) {
443       ObjArrayKlass* oak = _dynamic_archive_array_klasses->at(i);
444       Klass* elm = oak->element_klass();
445       assert(AOTMetaspace::in_aot_cache_static_region((void*)elm), "must be");
446       // Higher dimension may have been set when doing setup on ObjArrayKlass
447       if (!oak->is_refined_objArray_klass()) {
448         if (elm->is_instance_klass()) {
449           assert(InstanceKlass::cast(elm)->array_klasses() == nullptr, "must be");
450           InstanceKlass::cast(elm)->set_array_klasses(oak);
451         } else {
452           assert(elm->is_array_klass(), "sanity");
453           assert(ArrayKlass::cast(elm)->higher_dimension() == nullptr, "must be");
454           ArrayKlass::cast(elm)->set_higher_dimension(oak);
455         }
456       }
457     }
458     log_debug(aot)("Total array klasses read from dynamic archive: %d", _dynamic_archive_array_klasses->length());
459   }
460 }
461 
462 void DynamicArchive::make_array_klasses_shareable() {
463   if (_array_klasses != nullptr) {
464     int num_array_klasses = _array_klasses->length();
465     for (int i = 0; i < num_array_klasses; i++) {
466       ObjArrayKlass* k = ArchiveBuilder::current()->get_buffered_addr(_array_klasses->at(i));
467       k->remove_unshareable_info();
468     }
469   }
470 }
471 
472 void DynamicArchive::post_dump() {
473   if (_array_klasses != nullptr) {
474     delete _array_klasses;
475     _array_klasses = nullptr;
476   }
477 }
478 
479 int DynamicArchive::num_array_klasses() {
480   return _array_klasses != nullptr ? _array_klasses->length() : 0;
481 }
482 
483 void DynamicArchive::dump_impl(bool jcmd_request, const char* archive_name, TRAPS) {
484   AOTMetaspace::link_shared_classes(CHECK);
485   if (!jcmd_request && CDSConfig::is_dumping_regenerated_lambdaform_invokers()) {
486     LambdaFormInvokers::regenerate_holder_classes(CHECK);
487   }
488 
489   VM_PopulateDynamicDumpSharedSpace op(archive_name);
490   VMThread::execute(&op);
491 }
492 
493 void DynamicArchive::dump_at_exit(JavaThread* current) {
494   ExceptionMark em(current);
495   ResourceMark rm(current);
496   CDSConfig::DumperThreadMark dumper_thread_mark(current);
497 
498   const char* archive_name = CDSConfig::output_archive_path();
499   if (!CDSConfig::is_dumping_dynamic_archive() || archive_name == nullptr) {
500     return;
501   }
502 
503   log_info(cds, dynamic)("Preparing for dynamic dump at exit in thread %s", current->name());
504 
505   JavaThread* THREAD = current; // For TRAPS processing related to link_shared_classes
506   dump_impl(/*jcmd_request=*/false, archive_name, THREAD);
507   if (HAS_PENDING_EXCEPTION) {
508     // One of the prepatory steps failed
509     oop ex = current->pending_exception();
510     aot_log_error(aot)("Dynamic dump has failed");
511     aot_log_error(aot)("%s: %s", ex->klass()->external_name(),
512                    java_lang_String::as_utf8_string(java_lang_Throwable::message(ex)));
513     CLEAR_PENDING_EXCEPTION;
514     CDSConfig::disable_dumping_dynamic_archive();  // Just for good measure
515   }
516 }
517 
518 // This is called by "jcmd VM.cds dynamic_dump"
519 void DynamicArchive::dump_for_jcmd(const char* archive_name, TRAPS) {
520   CDSConfig::DumperThreadMark dumper_thread_mark(THREAD);
521   assert(CDSConfig::is_using_archive() && RecordDynamicDumpInfo, "already checked in arguments.cpp");
522   assert(ArchiveClassesAtExit == nullptr, "already checked in arguments.cpp");
523   assert(CDSConfig::is_dumping_dynamic_archive(), "already checked by check_for_dynamic_dump() during VM startup");
524   dump_impl(/*jcmd_request=*/true, archive_name, CHECK);
525 }
526 
527 bool DynamicArchive::validate(FileMapInfo* dynamic_info) {
528   assert(!dynamic_info->is_static(), "must be");
529   // Check if the recorded base archive matches with the current one
530   FileMapInfo* base_info = FileMapInfo::current_info();
531   DynamicArchiveHeader* dynamic_header = dynamic_info->dynamic_header();
532 
533   // Check the header crc
534   if (dynamic_header->base_header_crc() != base_info->crc()) {
535     aot_log_warning(aot)("Dynamic archive cannot be used: static archive header checksum verification failed.");
536     return false;
537   }
538 
539   // Check each space's crc
540   for (int i = 0; i < AOTMetaspace::n_regions; i++) {
541     if (dynamic_header->base_region_crc(i) != base_info->region_crc(i)) {
542       aot_log_warning(aot)("Dynamic archive cannot be used: static archive region #%d checksum verification failed.", i);
543       return false;
544     }
545   }
546 
547   return true;
548 }
549 
550 void DynamicArchiveHeader::print(outputStream* st) {
551   ResourceMark rm;
552 
553   st->print_cr("- base_header_crc:                0x%08x", base_header_crc());
554   for (int i = 0; i < NUM_CDS_REGIONS; i++) {
555     st->print_cr("- base_region_crc[%d]:             0x%08x", i, base_region_crc(i));
556   }
557 }