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