1 | /* |
2 | * Copyright (c) 2003, 2019, 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_MEMORY_FILEMAP_HPP |
26 | #define SHARE_MEMORY_FILEMAP_HPP |
27 | |
28 | #include "classfile/classLoader.hpp" |
29 | #include "include/cds.h" |
30 | #include "memory/metaspaceShared.hpp" |
31 | #include "memory/metaspace.hpp" |
32 | #include "oops/compressedOops.hpp" |
33 | #include "utilities/align.hpp" |
34 | |
35 | // Layout of the file: |
36 | // header: dump of archive instance plus versioning info, datestamp, etc. |
37 | // [magic # = 0xF00BABA2] |
38 | // ... padding to align on page-boundary |
39 | // read-write space |
40 | // read-only space |
41 | // misc data (block offset table, string table, symbols, dictionary, etc.) |
42 | // tag(666) |
43 | |
44 | static const int JVM_IDENT_MAX = 256; |
45 | |
46 | class SharedClassPathEntry { |
47 | enum { |
48 | modules_image_entry, |
49 | jar_entry, |
50 | signed_jar_entry, |
51 | dir_entry, |
52 | unknown_entry |
53 | }; |
54 | protected: |
55 | u1 _type; |
56 | time_t _timestamp; // jar timestamp, 0 if is directory, modules image or other |
57 | long _filesize; // jar/jimage file size, -1 if is directory, -2 if other |
58 | Array<char>* _name; |
59 | Array<u1>* _manifest; |
60 | |
61 | public: |
62 | void init(const char* name, bool is_modules_image, TRAPS); |
63 | void metaspace_pointers_do(MetaspaceClosure* it); |
64 | bool validate(bool is_class_path = true); |
65 | |
66 | // The _timestamp only gets set for jar files. |
67 | bool has_timestamp() { |
68 | return _timestamp != 0; |
69 | } |
70 | bool is_dir() { return _type == dir_entry; } |
71 | bool is_modules_image() { return _type == modules_image_entry; } |
72 | bool is_jar() { return _type == jar_entry; } |
73 | bool is_signed() { return _type == signed_jar_entry; } |
74 | void set_is_signed() { |
75 | _type = signed_jar_entry; |
76 | } |
77 | time_t timestamp() const { return _timestamp; } |
78 | long filesize() const { return _filesize; } |
79 | const char* name() const { return _name->data(); } |
80 | const char* manifest() const { |
81 | return (_manifest == NULL) ? NULL : (const char*)_manifest->data(); |
82 | } |
83 | int manifest_size() const { |
84 | return (_manifest == NULL) ? 0 : _manifest->length(); |
85 | } |
86 | void set_manifest(Array<u1>* manifest) { |
87 | _manifest = manifest; |
88 | } |
89 | }; |
90 | |
91 | struct ArchiveHeapOopmapInfo { |
92 | address _oopmap; // bitmap for relocating embedded oops |
93 | size_t _oopmap_size_in_bits; |
94 | }; |
95 | |
96 | class SharedPathTable { |
97 | Array<u8>* _table; |
98 | int _size; |
99 | public: |
100 | void dumptime_init(ClassLoaderData* loader_data, Thread* THREAD); |
101 | void metaspace_pointers_do(MetaspaceClosure* it); |
102 | |
103 | int size() { |
104 | return _size; |
105 | } |
106 | SharedClassPathEntry* path_at(int index) { |
107 | if (index < 0) { |
108 | return NULL; |
109 | } |
110 | assert(index < _size, "sanity" ); |
111 | char* p = (char*)_table->data(); |
112 | p += sizeof(SharedClassPathEntry) * index; |
113 | return (SharedClassPathEntry*)p; |
114 | } |
115 | Array<u8>* table() {return _table;} |
116 | void set_table(Array<u8>* table) {_table = table;} |
117 | |
118 | }; |
119 | |
120 | struct : public CDSFileMapHeaderBase { |
121 | size_t ; |
122 | size_t ; // how shared archive should be aligned |
123 | int ; // value of ObjectAlignmentInBytes |
124 | address ; // compressed oop encoding base |
125 | int ; // compressed oop encoding shift |
126 | bool ; // value of CompactStrings |
127 | uintx ; // java max heap size during dumping |
128 | CompressedOops::Mode ; // compressed oop encoding mode |
129 | int ; // save narrow klass base and shift |
130 | address ; |
131 | char* ; |
132 | char* ; |
133 | address ; |
134 | size_t ; |
135 | size_t ; // number of bytes allocated by the core spaces |
136 | // (mc, md, ro, rw and od). |
137 | MemRegion ; // reserved region for the entire heap at dump time. |
138 | bool ; // indicates if the base archive is the system default one |
139 | |
140 | // The following fields are all sanity checks for whether this archive |
141 | // will function correctly with this JVM and the bootclasspath it's |
142 | // invoked with. |
143 | char [JVM_IDENT_MAX]; // identifier for jvm |
144 | |
145 | // size of the base archive name including NULL terminator |
146 | int ; |
147 | |
148 | // The _paths_misc_info is a variable-size structure that records "miscellaneous" |
149 | // information during dumping. It is generated and validated by the |
150 | // SharedPathsMiscInfo class. See SharedPathsMiscInfo.hpp for |
151 | // detailed description. |
152 | // |
153 | // The _paths_misc_info data is stored as a byte array in the archive file header, |
154 | // immediately after the _header field. This information is used only when |
155 | // checking the validity of the archive and is deallocated after the archive is loaded. |
156 | // |
157 | // Note that the _paths_misc_info does NOT include information for JAR files |
158 | // that existed during dump time. Their information is stored in _shared_path_table. |
159 | int ; |
160 | |
161 | // The following is a table of all the class path entries that were used |
162 | // during dumping. At run time, we require these files to exist and have the same |
163 | // size/modification time, or else the archive will refuse to load. |
164 | // |
165 | // All of these entries must be JAR files. The dumping process would fail if a non-empty |
166 | // directory was specified in the classpaths. If an empty directory was specified |
167 | // it is checked by the _paths_misc_info as described above. |
168 | // |
169 | // FIXME -- if JAR files in the tail of the list were specified but not used during dumping, |
170 | // they should be removed from this table, to save space and to avoid spurious |
171 | // loading failures during runtime. |
172 | SharedPathTable ; |
173 | |
174 | jshort ; // Index of first app classpath entry |
175 | jshort ; // Index of first module path entry |
176 | jshort ; // number of module path entries |
177 | jshort ; // max path index referenced during CDS dump |
178 | bool ; // BytecodeVerificationLocal setting |
179 | bool ; // BytecodeVerificationRemote setting |
180 | bool ; // Archive contains app classes |
181 | size_t ; // SharedBaseAddress used at dump time |
182 | bool ; // setting of the AllowArchivingWithJavaAgent option |
183 | |
184 | void (bool v) { |
185 | _has_platform_or_app_classes = v; |
186 | } |
187 | bool () { return _has_platform_or_app_classes; } |
188 | jshort () { return _max_used_path_index; } |
189 | jshort () { return _app_module_paths_start_index; } |
190 | |
191 | bool (); |
192 | int (); |
193 | |
194 | CDSFileMapRegion* (int i) { |
195 | assert(i >= 0 && i < NUM_CDS_REGIONS, "invalid region" ); |
196 | return &_space[i]; |
197 | } |
198 | public: |
199 | void (FileMapInfo* info, size_t alignment); |
200 | }; |
201 | |
202 | class FileMapInfo : public CHeapObj<mtInternal> { |
203 | private: |
204 | friend class ManifestStream; |
205 | friend class VMStructs; |
206 | friend struct FileMapHeader; |
207 | |
208 | bool _is_static; |
209 | bool _file_open; |
210 | int _fd; |
211 | size_t _file_offset; |
212 | |
213 | private: |
214 | // TODO: Probably change the following to be non-static |
215 | static SharedPathTable _shared_path_table; |
216 | static bool _validating_shared_path_table; |
217 | |
218 | // FileMapHeader describes the shared space data in the file to be |
219 | // mapped. This structure gets written to a file. It is not a class, so |
220 | // that the compilers don't add any compiler-private data to it. |
221 | |
222 | public: |
223 | struct : public CHeapObj<mtClass> { |
224 | // Need to put something here. Otherwise, in product build, because CHeapObj has no virtual |
225 | // methods, we would get sizeof(FileMapHeaderBase) == 1 with gcc. |
226 | intx ; |
227 | }; |
228 | |
229 | |
230 | FileMapHeader * ; |
231 | |
232 | const char* _full_path; |
233 | char* _paths_misc_info; |
234 | char* _base_archive_name; |
235 | |
236 | static FileMapInfo* _current_info; |
237 | static FileMapInfo* _dynamic_archive_info; |
238 | static bool _heap_pointers_need_patching; |
239 | static bool _memory_mapping_failed; |
240 | static bool (const char* archive_name, |
241 | int* size, char** base_archive_name); |
242 | static bool check_archive(const char* archive_name, bool is_static); |
243 | static bool same_files(const char* file1, const char* file2); |
244 | void restore_shared_path_table(); |
245 | bool init_from_file(int fd, bool is_static); |
246 | static void metaspace_pointers_do(MetaspaceClosure* it); |
247 | |
248 | public: |
249 | FileMapInfo(bool is_static); |
250 | ~FileMapInfo(); |
251 | |
252 | int () { return _header->compute_crc(); } |
253 | void (int crc) { _header->_crc = crc; } |
254 | int space_crc(int i) { return space_at(i)->_crc; } |
255 | void (size_t alignment); |
256 | bool (bool is_static); |
257 | void invalidate(); |
258 | int crc() { return _header->_crc; } |
259 | int version() { return _header->_version; } |
260 | size_t alignment() { return _header->_alignment; } |
261 | CompressedOops::Mode narrow_oop_mode() { return _header->_narrow_oop_mode; } |
262 | address narrow_oop_base() const { return _header->_narrow_oop_base; } |
263 | int narrow_oop_shift() const { return _header->_narrow_oop_shift; } |
264 | uintx max_heap_size() const { return _header->_max_heap_size; } |
265 | address narrow_klass_base() const { return _header->_narrow_klass_base; } |
266 | int narrow_klass_shift() const { return _header->_narrow_klass_shift; } |
267 | struct FileMapHeader* () { return _header; } |
268 | char* misc_data_patching_start() { return _header->_misc_data_patching_start; } |
269 | void set_misc_data_patching_start(char* p) { _header->_misc_data_patching_start = p; } |
270 | char* read_only_tables_start() { return _header->_read_only_tables_start; } |
271 | void set_read_only_tables_start(char* p) { _header->_read_only_tables_start = p; } |
272 | |
273 | bool is_file_position_aligned() const; |
274 | void align_file_position(); |
275 | |
276 | address cds_i2i_entry_code_buffers() { |
277 | return _header->_cds_i2i_entry_code_buffers; |
278 | } |
279 | void set_cds_i2i_entry_code_buffers(address addr) { |
280 | _header->_cds_i2i_entry_code_buffers = addr; |
281 | } |
282 | size_t cds_i2i_entry_code_buffers_size() { |
283 | return _header->_cds_i2i_entry_code_buffers_size; |
284 | } |
285 | void set_cds_i2i_entry_code_buffers_size(size_t s) { |
286 | _header->_cds_i2i_entry_code_buffers_size = s; |
287 | } |
288 | void set_core_spaces_size(size_t s) { _header->_core_spaces_size = s; } |
289 | size_t core_spaces_size() { return _header->_core_spaces_size; } |
290 | |
291 | static FileMapInfo* current_info() { |
292 | CDS_ONLY(return _current_info;) |
293 | NOT_CDS(return NULL;) |
294 | } |
295 | |
296 | static void set_current_info(FileMapInfo* info) { |
297 | CDS_ONLY(_current_info = info;) |
298 | } |
299 | |
300 | static FileMapInfo* dynamic_info() { |
301 | CDS_ONLY(return _dynamic_archive_info;) |
302 | NOT_CDS(return NULL;) |
303 | } |
304 | |
305 | static void assert_mark(bool check); |
306 | |
307 | // File manipulation. |
308 | bool initialize(bool is_static) NOT_CDS_RETURN_(false); |
309 | bool open_for_read(const char* path = NULL); |
310 | void open_for_write(const char* path = NULL); |
311 | void (); |
312 | void write_region(int region, char* base, size_t size, |
313 | bool read_only, bool allow_exec); |
314 | size_t write_archive_heap_regions(GrowableArray<MemRegion> *heap_mem, |
315 | GrowableArray<ArchiveHeapOopmapInfo> *oopmaps, |
316 | int first_region_id, int max_num_regions, |
317 | bool print_log); |
318 | void write_bytes(const void* buffer, size_t count); |
319 | void write_bytes_aligned(const void* buffer, size_t count); |
320 | size_t read_bytes(void* buffer, size_t count); |
321 | char* map_regions(int regions[], char* saved_base[], size_t len); |
322 | char* map_region(int i, char** top_ret); |
323 | void map_heap_regions_impl() NOT_CDS_JAVA_HEAP_RETURN; |
324 | void map_heap_regions() NOT_CDS_JAVA_HEAP_RETURN; |
325 | void fixup_mapped_heap_regions() NOT_CDS_JAVA_HEAP_RETURN; |
326 | void patch_archived_heap_embedded_pointers() NOT_CDS_JAVA_HEAP_RETURN; |
327 | void patch_archived_heap_embedded_pointers(MemRegion* ranges, int num_ranges, |
328 | int first_region_idx) NOT_CDS_JAVA_HEAP_RETURN; |
329 | bool has_heap_regions() NOT_CDS_JAVA_HEAP_RETURN_(false); |
330 | MemRegion get_heap_regions_range_with_current_oop_encoding_mode() NOT_CDS_JAVA_HEAP_RETURN_(MemRegion()); |
331 | void unmap_regions(int regions[], char* saved_base[], size_t len); |
332 | void unmap_region(int i); |
333 | bool verify_region_checksum(int i); |
334 | void close(); |
335 | bool is_open() { return _file_open; } |
336 | ReservedSpace reserve_shared_memory(); |
337 | |
338 | // JVM/TI RedefineClasses() support: |
339 | // Remap the shared readonly space to shared readwrite, private. |
340 | bool remap_shared_readonly_as_readwrite(); |
341 | |
342 | // Errors. |
343 | static void fail_stop(const char *msg, ...) ATTRIBUTE_PRINTF(1, 2); |
344 | static void fail_continue(const char *msg, ...) ATTRIBUTE_PRINTF(1, 2); |
345 | static bool memory_mapping_failed() { |
346 | CDS_ONLY(return _memory_mapping_failed;) |
347 | NOT_CDS(return false;) |
348 | } |
349 | bool is_in_shared_region(const void* p, int idx) NOT_CDS_RETURN_(false); |
350 | |
351 | // Stop CDS sharing and unmap CDS regions. |
352 | static void stop_sharing_and_unmap(const char* msg); |
353 | |
354 | static void allocate_shared_path_table(); |
355 | static void check_nonempty_dir_in_shared_path_table(); |
356 | bool validate_shared_path_table(); |
357 | static void update_shared_classpath(ClassPathEntry *cpe, SharedClassPathEntry* ent, TRAPS); |
358 | |
359 | #if INCLUDE_JVMTI |
360 | static ClassFileStream* open_stream_for_jvmti(InstanceKlass* ik, Handle class_loader, TRAPS); |
361 | #endif |
362 | |
363 | static SharedClassPathEntry* shared_path(int index) { |
364 | return _shared_path_table.path_at(index); |
365 | } |
366 | |
367 | static const char* shared_path_name(int index) { |
368 | assert(index >= 0, "Sanity" ); |
369 | return shared_path(index)->name(); |
370 | } |
371 | |
372 | static int get_number_of_shared_paths() { |
373 | return _shared_path_table.size(); |
374 | } |
375 | |
376 | char* region_addr(int idx); |
377 | |
378 | private: |
379 | bool map_heap_data(MemRegion **heap_mem, int first, int max, int* num, |
380 | bool is_open = false) NOT_CDS_JAVA_HEAP_RETURN_(false); |
381 | bool region_crc_check(char* buf, size_t size, int expected_crc) NOT_CDS_RETURN_(false); |
382 | void dealloc_archive_heap_regions(MemRegion* regions, int num, bool is_open) NOT_CDS_JAVA_HEAP_RETURN; |
383 | |
384 | CDSFileMapRegion* space_at(int i) { |
385 | return _header->space_at(i); |
386 | } |
387 | |
388 | narrowOop offset_of_space(CDSFileMapRegion* spc) { |
389 | return (narrowOop)(spc->_addr._offset); |
390 | } |
391 | |
392 | // The starting address of spc, as calculated with CompressedOop::decode_non_null() |
393 | address start_address_as_decoded_with_current_oop_encoding_mode(CDSFileMapRegion* spc) { |
394 | return decode_start_address(spc, true); |
395 | } |
396 | |
397 | // The starting address of spc, as calculated with HeapShared::decode_from_archive() |
398 | address start_address_as_decoded_from_archive(CDSFileMapRegion* spc) { |
399 | return decode_start_address(spc, false); |
400 | } |
401 | |
402 | address decode_start_address(CDSFileMapRegion* spc, bool with_current_oop_encoding_mode); |
403 | |
404 | #if INCLUDE_JVMTI |
405 | static ClassPathEntry** _classpath_entries_for_jvmti; |
406 | static ClassPathEntry* get_classpath_entry_for_jvmti(int i, TRAPS); |
407 | #endif |
408 | }; |
409 | |
410 | #endif // SHARE_MEMORY_FILEMAP_HPP |
411 | |