1 /*
  2  * Copyright (c) 2012, 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 #ifndef SHARE_CDS_AOTMETASPACE_HPP
 26 #define SHARE_CDS_AOTMETASPACE_HPP
 27 
 28 #include "memory/allocation.hpp"
 29 #include "memory/memRegion.hpp"
 30 #include "memory/reservedSpace.hpp"
 31 #include "memory/virtualspace.hpp"
 32 #include "oops/oop.hpp"
 33 #include "utilities/macros.hpp"
 34 
 35 class ArchiveBuilder;
 36 class ArchiveMappedHeapInfo;
 37 class ArchiveStreamedHeapInfo;
 38 class FileMapInfo;
 39 class Method;
 40 class outputStream;
 41 class SerializeClosure;
 42 class StaticArchiveBuilder;
 43 
 44 template<class E> class Array;
 45 
 46 enum MapArchiveResult {
 47   MAP_ARCHIVE_SUCCESS,
 48   MAP_ARCHIVE_MMAP_FAILURE,
 49   MAP_ARCHIVE_OTHER_FAILURE
 50 };
 51 
 52 // Class Data Sharing Support
 53 class AOTMetaspace : AllStatic {
 54   static ReservedSpace _symbol_rs;  // used only during -Xshare:dump
 55   static VirtualSpace _symbol_vs;   // used only during -Xshare:dump
 56   static bool _archive_loading_failed;
 57   static bool _remapped_readwrite;
 58   static void* _aot_metaspace_static_top;
 59   static intx _relocation_delta;
 60   static char* _requested_base_address;
 61   static bool _use_optimized_module_handling;
 62   static Array<Method*>* _archived_method_handle_intrinsics;
 63   static int volatile _preimage_static_archive_dumped;
 64   static FileMapInfo* _output_mapinfo;
 65 
 66  public:
 67   enum {
 68     // core archive spaces
 69     rw = 0,  // read-write
 70     ro = 1,  // read-only
 71     bm = 2,  // relocation bitmaps (freed after file mapping is finished)
 72     hp = 3,  // heap region
 73     ac = 4,  // aot code
 74     num_core_region = 2,       // rw and ro
 75     n_regions = 5              // total number of regions
 76   };
 77 
 78   static void dump_static_archive(TRAPS) NOT_CDS_RETURN;
 79 #ifdef _LP64
 80   static void adjust_heap_sizes_for_dumping() NOT_CDS_JAVA_HEAP_RETURN;
 81 #endif
 82 
 83 private:
 84   static void exercise_runtime_cds_code(TRAPS) NOT_CDS_RETURN;
 85   static void dump_static_archive_impl(StaticArchiveBuilder& builder, TRAPS) NOT_CDS_RETURN;
 86   static void load_classes(TRAPS) NOT_CDS_RETURN;
 87 
 88 public:
 89   static Symbol* symbol_rs_base() {
 90     return (Symbol*)_symbol_rs.base();
 91   }
 92 
 93   static void initialize_for_static_dump() NOT_CDS_RETURN;
 94   static void initialize_runtime_shared_and_meta_spaces() NOT_CDS_RETURN;
 95   static void post_initialize(TRAPS) NOT_CDS_RETURN;
 96 
 97   static void print_on(outputStream* st);
 98 
 99   static void set_archive_loading_failed() {
100     _archive_loading_failed = true;
101   }
102 
103   static void initialize_shared_spaces() NOT_CDS_RETURN;
104 
105   // Return true if given address is in the shared metaspace regions (i.e., excluding the
106   // mapped heap region.)
107   static bool in_aot_cache(const void* p) {
108     return MetaspaceObj::in_aot_cache((const MetaspaceObj*)p);
109   }
110 
111   static void set_aot_metaspace_range(void* base, void *static_top, void* top) NOT_CDS_RETURN;
112 
113   // inside the metaspace of the AOT cache, or the static CDS archive
114   static bool in_aot_cache_static_region(void* p) NOT_CDS_RETURN_(false);
115 
116   // inside the metaspace of the dynamic static CDS archive
117   static bool in_aot_cache_dynamic_region(void* p) NOT_CDS_RETURN_(false);
118 
119   static bool preimage_static_archive_dumped() NOT_CDS_RETURN_(false);
120 
121   static void unrecoverable_loading_error(const char* message = "unrecoverable error");
122   static void report_loading_error(const char* format, ...) ATTRIBUTE_PRINTF(1, 0);
123   static void unrecoverable_writing_error(const char* message = nullptr);
124   static void writing_error(const char* message = nullptr);
125 
126   static void make_method_handle_intrinsics_shareable() NOT_CDS_RETURN;
127   static void write_method_handle_intrinsics() NOT_CDS_RETURN;
128   static Array<Method*>* archived_method_handle_intrinsics() { return _archived_method_handle_intrinsics; }
129   static void early_serialize(SerializeClosure* sc) NOT_CDS_RETURN;
130   static void serialize(SerializeClosure* sc) NOT_CDS_RETURN;
131 
132   // JVM/TI RedefineClasses() support:
133   // Remap the shared readonly space to shared readwrite, private if
134   // sharing is enabled. Simply returns true if sharing is not enabled
135   // or if the remapping has already been done by a prior call.
136   static bool remap_shared_readonly_as_readwrite() NOT_CDS_RETURN_(true);
137   static bool remapped_readwrite() {
138     CDS_ONLY(return _remapped_readwrite);
139     NOT_CDS(return false);
140   }
141 
142   static bool try_link_class(JavaThread* current, InstanceKlass* ik);
143   static void link_all_loaded_classes(JavaThread* current);
144   static void link_shared_classes(TRAPS) NOT_CDS_RETURN;
145   static bool may_be_eagerly_linked(InstanceKlass* ik) NOT_CDS_RETURN_(false);
146 
147 #if INCLUDE_CDS
148   // Alignment for the 2 core CDS regions (RW/RO) only.
149   // (Heap region alignments are decided by GC).
150   static size_t core_region_alignment();
151   static size_t protection_zone_size();
152   static void rewrite_bytecodes_and_calculate_fingerprints(Thread* thread, InstanceKlass* ik);
153   // print loaded classes names to file.
154   static void dump_loaded_classes(const char* file_name, TRAPS);
155 #endif
156 
157   // Allocate a block of memory from the temporary "symbol" region.
158   static char* symbol_space_alloc(size_t num_bytes);
159 
160   // This is the base address as specified by -XX:SharedBaseAddress during -Xshare:dump.
161   // Both the base/top archives are written using this as their base address.
162   //
163   // During static dump: _requested_base_address == SharedBaseAddress.
164   //
165   // During dynamic dump: _requested_base_address is not always the same as SharedBaseAddress:
166   // - SharedBaseAddress is used for *reading the base archive*. I.e., CompactHashtable uses
167   //   it to convert offsets to pointers to Symbols in the base archive.
168   //   The base archive may be mapped to an OS-selected address due to ASLR. E.g.,
169   //   you may have SharedBaseAddress == 0x00ff123400000000.
170   // - _requested_base_address is used for *writing the output archive*. It's usually
171   //   0x800000000 (unless it was set by -XX:SharedBaseAddress during -Xshare:dump).
172   static char* requested_base_address() {
173     return _requested_base_address;
174   }
175 
176   // Non-zero if the archive(s) need to be mapped a non-default location due to ASLR.
177   static intx relocation_delta() { return _relocation_delta; }
178 
179   static bool use_windows_memory_mapping() {
180     const bool is_windows = (NOT_WINDOWS(false) WINDOWS_ONLY(true));
181     //const bool is_windows = true; // enable this to allow testing the windows mmap semantics on Linux, etc.
182     return is_windows;
183   }
184 
185   // Can we skip some expensive operations related to modules?
186   static bool use_optimized_module_handling() { return NOT_CDS(false) CDS_ONLY(_use_optimized_module_handling); }
187   static void disable_optimized_module_handling() { _use_optimized_module_handling = false; }
188 
189 private:
190   static void read_extra_data(JavaThread* current, const char* filename) NOT_CDS_RETURN;
191   static void fork_and_dump_final_static_archive(TRAPS);
192   static void open_output_mapinfo();
193   static bool write_static_archive(ArchiveBuilder* builder,
194                                    FileMapInfo* map_info,
195                                    ArchiveMappedHeapInfo* mapped_heap_info,
196                                    ArchiveStreamedHeapInfo* streamed_heap_info);
197   static FileMapInfo* open_static_archive();
198   static FileMapInfo* open_dynamic_archive();
199   // use_requested_addr: If true (default), attempt to map at the address the
200   static MapArchiveResult map_archives(FileMapInfo* static_mapinfo, FileMapInfo* dynamic_mapinfo,
201                                        bool use_requested_addr);
202   static char* reserve_address_space_for_archives(FileMapInfo* static_mapinfo,
203                                                   FileMapInfo* dynamic_mapinfo,
204                                                   bool use_archive_base_addr,
205                                                   ReservedSpace& total_space_rs,
206                                                   ReservedSpace& archive_space_rs,
207                                                   ReservedSpace& class_space_rs);
208  static void release_reserved_spaces(ReservedSpace& total_space_rs,
209                                      ReservedSpace& archive_space_rs,
210                                      ReservedSpace& class_space_rs);
211   static MapArchiveResult map_archive(FileMapInfo* mapinfo, char* mapped_base_address, ReservedSpace rs);
212   static void unmap_archive(FileMapInfo* mapinfo);
213   static void get_default_classlist(char* default_classlist, const size_t buf_size);
214 };
215 #endif // SHARE_CDS_AOTMETASPACE_HPP