79 }
80
81
82 void FileMapInfo::fail_stop(const char *msg, ...) {
83 va_list ap;
84 va_start(ap, msg);
85 fail(msg, ap); // Never returns.
86 va_end(ap); // for completeness.
87 }
88
89
90 // Complain and continue. Recoverable errors during the reading of the
91 // archive file may continue (with sharing disabled).
92 //
93 // If we continue, then disable shared spaces and close the file.
94
95 void FileMapInfo::fail_continue(const char *msg, ...) {
96 va_list ap;
97 va_start(ap, msg);
98 MetaspaceShared::set_archive_loading_failed();
99 if (PrintSharedArchiveAndExit && _validating_classpath_entry_table) {
100 // If we are doing PrintSharedArchiveAndExit and some of the classpath entries
101 // do not validate, we can still continue "limping" to validate the remaining
102 // entries. No need to quit.
103 tty->print("[");
104 tty->vprint(msg, ap);
105 tty->print_cr("]");
106 } else {
107 if (RequireSharedSpaces) {
108 fail(msg, ap);
109 } else {
110 if (log_is_enabled(Info, cds)) {
111 ResourceMark rm;
112 LogStream ls(Log(cds)::info());
113 ls.print("UseSharedSpaces: ");
114 ls.vprint_cr(msg, ap);
115 }
116 }
117 UseSharedSpaces = false;
118 assert(current_info() != NULL, "singleton must be registered");
119 current_info()->close();
171 void FileMapInfo::populate_header(size_t alignment) {
172 _header->populate(this, alignment);
173 }
174
175 size_t FileMapInfo::FileMapHeader::data_size() {
176 return SharedClassUtil::file_map_header_size() - sizeof(FileMapInfo::FileMapHeaderBase);
177 }
178
179 void FileMapInfo::FileMapHeader::populate(FileMapInfo* mapinfo, size_t alignment) {
180 _magic = 0xf00baba2;
181 _version = _current_version;
182 _alignment = alignment;
183 _obj_alignment = ObjectAlignmentInBytes;
184 _compact_strings = CompactStrings;
185 _narrow_oop_mode = Universe::narrow_oop_mode();
186 _narrow_oop_base = Universe::narrow_oop_base();
187 _narrow_oop_shift = Universe::narrow_oop_shift();
188 _max_heap_size = MaxHeapSize;
189 _narrow_klass_base = Universe::narrow_klass_base();
190 _narrow_klass_shift = Universe::narrow_klass_shift();
191 _classpath_entry_table_size = mapinfo->_classpath_entry_table_size;
192 _classpath_entry_table = mapinfo->_classpath_entry_table;
193 _classpath_entry_size = mapinfo->_classpath_entry_size;
194
195 // The following fields are for sanity checks for whether this archive
196 // will function correctly with this JVM and the bootclasspath it's
197 // invoked with.
198
199 // JVM version string ... changes on each build.
200 get_header_version(_jvm_ident);
201 }
202
203 void SharedClassPathEntry::init(const char* name, TRAPS) {
204 _timestamp = 0;
205 _filesize = 0;
206
207 struct stat st;
208 if (os::stat(name, &st) == 0) {
209 if ((st.st_mode & S_IFMT) == S_IFDIR) {
210 if (!os::dir_is_empty(name)) {
211 ClassLoader::exit_with_path_failure(
212 "Cannot have non-empty directory in archived classpaths", name);
213 }
214 _is_dir = true;
215 } else {
216 _is_dir = false;
217 _timestamp = st.st_mtime;
218 _filesize = st.st_size;
219 }
220 } else {
221 // The file/dir must exist, or it would not have been added
222 // into ClassLoader::classpath_entry().
223 //
224 // If we can't access a jar file in the boot path, then we can't
225 // make assumptions about where classes get loaded from.
226 FileMapInfo::fail_stop("Unable to open file %s.", name);
227 }
228
229 size_t len = strlen(name) + 1;
230 _name = MetadataFactory::new_array<char>(ClassLoaderData::the_null_class_loader_data(), (int)len, THREAD);
231 strcpy(_name->data(), name);
232 }
233
234 bool SharedClassPathEntry::validate() {
235 struct stat st;
236 const char* name = this->name();
237 bool ok = true;
238 log_info(class, path)("checking shared classpath entry: %s", name);
239 if (os::stat(name, &st) != 0) {
240 FileMapInfo::fail_continue("Required classpath entry does not exist: %s", name);
241 ok = false;
242 } else if (is_dir()) {
243 if (!os::dir_is_empty(name)) {
244 FileMapInfo::fail_continue("directory is not empty: %s", name);
245 ok = false;
246 }
247 } else if (is_jar_or_bootimage()) {
248 if (_timestamp != st.st_mtime ||
249 _filesize != st.st_size) {
250 ok = false;
251 if (PrintSharedArchiveAndExit) {
252 FileMapInfo::fail_continue(_timestamp != st.st_mtime ?
253 "Timestamp mismatch" :
254 "File size mismatch");
255 } else {
256 FileMapInfo::fail_continue("A jar/jimage file is not the one used while building"
257 " the shared archive file: %s", name);
258 }
259 }
260 }
261 return ok;
262 }
263
264 void SharedClassPathEntry::metaspace_pointers_do(MetaspaceClosure* it) {
265 it->push(&_name);
266 it->push(&_manifest);
267 }
268
269 void FileMapInfo::allocate_classpath_entry_table() {
270 assert(DumpSharedSpaces, "Sanity");
271
272 Thread* THREAD = Thread::current();
273 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
274 ClassPathEntry* jrt = ClassLoader::get_jrt_entry();
275
276 assert(jrt != NULL,
277 "No modular java runtime image present when allocating the CDS classpath entry table");
278
279 size_t entry_size = SharedClassUtil::shared_class_path_entry_size(); // assert ( should be 8 byte aligned??)
280 int num_boot_classpath_entries = ClassLoader::num_boot_classpath_entries();
281 int num_app_classpath_entries = ClassLoader::num_app_classpath_entries();
282 int num_entries = num_boot_classpath_entries + num_app_classpath_entries;
283 size_t bytes = entry_size * num_entries;
284
285 _classpath_entry_table = MetadataFactory::new_array<u8>(loader_data, (int)(bytes + 7 / 8), THREAD);
286 _classpath_entry_table_size = num_entries;
287 _classpath_entry_size = entry_size;
288
289 // 1. boot class path
290 int i = 0;
291 ClassPathEntry* cpe = jrt;
292 while (cpe != NULL) {
293 const char* type = ((cpe == jrt) ? "jrt" : (cpe->is_jar_file() ? "jar" : "dir"));
294 log_info(class, path)("add main shared path (%s) %s", type, cpe->name());
295 SharedClassPathEntry* ent = shared_classpath(i);
296 ent->init(cpe->name(), THREAD);
297 if (cpe != jrt) { // No need to do jimage.
298 EXCEPTION_MARK; // The following call should never throw, but would exit VM on error.
299 SharedClassUtil::update_shared_classpath(cpe, ent, THREAD);
300 }
301 cpe = ClassLoader::get_next_boot_classpath_entry(cpe);
302 i++;
303 }
304 assert(i == num_boot_classpath_entries,
305 "number of boot class path entry mismatch");
306
307 // 2. app class path
308 ClassPathEntry *acpe = ClassLoader::app_classpath_entries();
309 while (acpe != NULL) {
310 log_info(class, path)("add app shared path %s", acpe->name());
311 SharedClassPathEntry* ent = shared_classpath(i);
312 ent->init(acpe->name(), THREAD);
313 EXCEPTION_MARK;
314 SharedClassUtil::update_shared_classpath(acpe, ent, THREAD);
315 acpe = acpe->next();
316 i ++;
317 }
318 assert(i == num_entries, "number of app class path entry mismatch");
319 }
320
321 bool FileMapInfo::validate_classpath_entry_table() {
322 _validating_classpath_entry_table = true;
323
324 int count = _header->_classpath_entry_table_size;
325
326 _classpath_entry_table = _header->_classpath_entry_table;
327 _classpath_entry_size = _header->_classpath_entry_size;
328 _classpath_entry_table_size = _header->_classpath_entry_table_size;
329
330 for (int i=0; i<count; i++) {
331 if (shared_classpath(i)->validate()) {
332 log_info(class, path)("ok");
333 } else if (!PrintSharedArchiveAndExit) {
334 _validating_classpath_entry_table = false;
335 _classpath_entry_table = NULL;
336 _classpath_entry_table_size = 0;
337 return false;
338 }
339 }
340
341 _validating_classpath_entry_table = false;
342 return true;
343 }
344
345
346 // Read the FileMapInfo information from the file.
347
348 bool FileMapInfo::init_from_file(int fd) {
349 size_t sz = _header->data_size();
350 char* addr = _header->data();
351 size_t n = os::read(fd, addr, (unsigned int)sz);
352 if (n != sz) {
353 fail_continue("Unable to read the file header.");
354 return false;
355 }
356 if (_header->_version != current_version()) {
357 fail_continue("The shared archive file has the wrong version.");
358 return false;
359 }
360 _file_offset = (long)n;
361
362 size_t info_size = _header->_paths_misc_info_size;
363 _paths_misc_info = NEW_C_HEAP_ARRAY_RETURN_NULL(char, info_size, mtClass);
364 if (_paths_misc_info == NULL) {
365 fail_continue("Unable to read the file header.");
908 size_t used = si->_used;
909 size_t size = align_up(used, os::vm_allocation_granularity());
910
911 if (used == 0) {
912 return;
913 }
914
915 char* addr = _header->region_addr(i);
916 if (!os::unmap_memory(addr, size)) {
917 fail_stop("Unable to unmap shared space.");
918 }
919 }
920
921 void FileMapInfo::assert_mark(bool check) {
922 if (!check) {
923 fail_stop("Mark mismatch while restoring from shared file.");
924 }
925 }
926
927 void FileMapInfo::metaspace_pointers_do(MetaspaceClosure* it) {
928 it->push(&_classpath_entry_table);
929 for (int i=0; i<_classpath_entry_table_size; i++) {
930 shared_classpath(i)->metaspace_pointers_do(it);
931 }
932 }
933
934
935 FileMapInfo* FileMapInfo::_current_info = NULL;
936 Array<u8>* FileMapInfo::_classpath_entry_table = NULL;
937 int FileMapInfo::_classpath_entry_table_size = 0;
938 size_t FileMapInfo::_classpath_entry_size = 0x1234baad;
939 bool FileMapInfo::_validating_classpath_entry_table = false;
940
941 // Open the shared archive file, read and validate the header
942 // information (version, boot classpath, etc.). If initialization
943 // fails, shared spaces are disabled and the file is closed. [See
944 // fail_continue.]
945 //
946 // Validation of the archive is done in two steps:
947 //
948 // [1] validate_header() - done here. This checks the header, including _paths_misc_info.
949 // [2] validate_classpath_entry_table - this is done later, because the table is in the RW
950 // region of the archive, which is not mapped yet.
951 bool FileMapInfo::initialize() {
952 assert(UseSharedSpaces, "UseSharedSpaces expected.");
953
954 if (!open_for_read()) {
955 return false;
956 }
957
958 init_from_file(_fd);
959 if (!validate_header()) {
960 return false;
961 }
962 return true;
963 }
964
965 char* FileMapInfo::FileMapHeader::region_addr(int idx) {
966 if (MetaspaceShared::is_heap_region(idx)) {
967 return _space[idx]._used > 0 ?
968 (char*)((void*)CompressedOops::decode_not_null((narrowOop)_space[idx]._addr._offset)) : NULL;
969 } else {
|
79 }
80
81
82 void FileMapInfo::fail_stop(const char *msg, ...) {
83 va_list ap;
84 va_start(ap, msg);
85 fail(msg, ap); // Never returns.
86 va_end(ap); // for completeness.
87 }
88
89
90 // Complain and continue. Recoverable errors during the reading of the
91 // archive file may continue (with sharing disabled).
92 //
93 // If we continue, then disable shared spaces and close the file.
94
95 void FileMapInfo::fail_continue(const char *msg, ...) {
96 va_list ap;
97 va_start(ap, msg);
98 MetaspaceShared::set_archive_loading_failed();
99 if (PrintSharedArchiveAndExit && _validating_shared_path_table) {
100 // If we are doing PrintSharedArchiveAndExit and some of the classpath entries
101 // do not validate, we can still continue "limping" to validate the remaining
102 // entries. No need to quit.
103 tty->print("[");
104 tty->vprint(msg, ap);
105 tty->print_cr("]");
106 } else {
107 if (RequireSharedSpaces) {
108 fail(msg, ap);
109 } else {
110 if (log_is_enabled(Info, cds)) {
111 ResourceMark rm;
112 LogStream ls(Log(cds)::info());
113 ls.print("UseSharedSpaces: ");
114 ls.vprint_cr(msg, ap);
115 }
116 }
117 UseSharedSpaces = false;
118 assert(current_info() != NULL, "singleton must be registered");
119 current_info()->close();
171 void FileMapInfo::populate_header(size_t alignment) {
172 _header->populate(this, alignment);
173 }
174
175 size_t FileMapInfo::FileMapHeader::data_size() {
176 return SharedClassUtil::file_map_header_size() - sizeof(FileMapInfo::FileMapHeaderBase);
177 }
178
179 void FileMapInfo::FileMapHeader::populate(FileMapInfo* mapinfo, size_t alignment) {
180 _magic = 0xf00baba2;
181 _version = _current_version;
182 _alignment = alignment;
183 _obj_alignment = ObjectAlignmentInBytes;
184 _compact_strings = CompactStrings;
185 _narrow_oop_mode = Universe::narrow_oop_mode();
186 _narrow_oop_base = Universe::narrow_oop_base();
187 _narrow_oop_shift = Universe::narrow_oop_shift();
188 _max_heap_size = MaxHeapSize;
189 _narrow_klass_base = Universe::narrow_klass_base();
190 _narrow_klass_shift = Universe::narrow_klass_shift();
191 _shared_path_table_size = mapinfo->_shared_path_table_size;
192 _shared_path_table = mapinfo->_shared_path_table;
193 _shared_path_entry_size = mapinfo->_shared_path_entry_size;
194
195 // The following fields are for sanity checks for whether this archive
196 // will function correctly with this JVM and the bootclasspath it's
197 // invoked with.
198
199 // JVM version string ... changes on each build.
200 get_header_version(_jvm_ident);
201 }
202
203 void SharedClassPathEntry::init(const char* name, TRAPS) {
204 _timestamp = 0;
205 _filesize = 0;
206
207 struct stat st;
208 if (os::stat(name, &st) == 0) {
209 if ((st.st_mode & S_IFMT) == S_IFDIR) {
210 if (!os::dir_is_empty(name)) {
211 ClassLoader::exit_with_path_failure(
212 "Cannot have non-empty directory in archived classpaths", name);
213 }
214 _is_dir = true;
215 } else {
216 _is_dir = false;
217 _timestamp = st.st_mtime;
218 _filesize = st.st_size;
219 }
220 } else {
221 // The file/dir must exist, or it would not have been added
222 // into ClassLoader::classpath_entry().
223 //
224 // If we can't access a jar file in the boot path, then we can't
225 // make assumptions about where classes get loaded from.
226 FileMapInfo::fail_stop("Unable to open file %s.", name);
227 }
228
229 size_t len = strlen(name) + 1;
230 _name = MetadataFactory::new_array<char>(ClassLoaderData::the_null_class_loader_data(), (int)len, THREAD);
231 strcpy(_name->data(), name);
232 }
233
234 bool SharedClassPathEntry::validate(bool is_class_path) {
235 struct stat st;
236 const char* name = this->name();
237 bool ok = true;
238 log_info(class, path)("checking shared classpath entry: %s", name);
239 if (os::stat(name, &st) != 0 && is_class_path) {
240 // If the archived module path entry does not exist at runtime, it is not fatal
241 // (no need to invalid the shared archive) because the shared runtime visibility check
242 // filters out any archived module classes that do not have a matching runtime
243 // module path location.
244 FileMapInfo::fail_continue("Required classpath entry does not exist: %s", name);
245 ok = false;
246 } else if (is_dir()) {
247 if (!os::dir_is_empty(name)) {
248 FileMapInfo::fail_continue("directory is not empty: %s", name);
249 ok = false;
250 }
251 } else if (is_jar_or_bootimage()) {
252 if (_timestamp != st.st_mtime ||
253 _filesize != st.st_size) {
254 ok = false;
255 if (PrintSharedArchiveAndExit) {
256 FileMapInfo::fail_continue(_timestamp != st.st_mtime ?
257 "Timestamp mismatch" :
258 "File size mismatch");
259 } else {
260 FileMapInfo::fail_continue("A jar/jimage file is not the one used while building"
261 " the shared archive file: %s", name);
262 }
263 }
264 }
265 return ok;
266 }
267
268 void SharedClassPathEntry::metaspace_pointers_do(MetaspaceClosure* it) {
269 it->push(&_name);
270 it->push(&_manifest);
271 }
272
273 void FileMapInfo::allocate_shared_path_table() {
274 assert(DumpSharedSpaces, "Sanity");
275
276 Thread* THREAD = Thread::current();
277 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
278 ClassPathEntry* jrt = ClassLoader::get_jrt_entry();
279
280 assert(jrt != NULL,
281 "No modular java runtime image present when allocating the CDS classpath entry table");
282
283 size_t entry_size = SharedClassUtil::shared_class_path_entry_size(); // assert ( should be 8 byte aligned??)
284 int num_boot_classpath_entries = ClassLoader::num_boot_classpath_entries();
285 int num_app_classpath_entries = ClassLoader::num_app_classpath_entries();
286 int num_module_path_entries = ClassLoader::num_module_path_entries();
287 int num_entries = num_boot_classpath_entries + num_app_classpath_entries + num_module_path_entries;
288 size_t bytes = entry_size * num_entries;
289
290 _shared_path_table = MetadataFactory::new_array<u8>(loader_data, (int)(bytes + 7 / 8), THREAD);
291 _shared_path_table_size = num_entries;
292 _shared_path_entry_size = entry_size;
293
294 // 1. boot class path
295 int i = 0;
296 ClassPathEntry* cpe = jrt;
297 while (cpe != NULL) {
298 const char* type = ((cpe == jrt) ? "jrt" : (cpe->is_jar_file() ? "jar" : "dir"));
299 log_info(class, path)("add main shared path (%s) %s", type, cpe->name());
300 SharedClassPathEntry* ent = shared_path(i);
301 ent->init(cpe->name(), THREAD);
302 if (cpe != jrt) { // No need to do jimage.
303 EXCEPTION_MARK; // The following call should never throw, but would exit VM on error.
304 SharedClassUtil::update_shared_classpath(cpe, ent, THREAD);
305 }
306 cpe = ClassLoader::get_next_boot_classpath_entry(cpe);
307 i++;
308 }
309 assert(i == num_boot_classpath_entries,
310 "number of boot class path entry mismatch");
311
312 // 2. app class path
313 ClassPathEntry *acpe = ClassLoader::app_classpath_entries();
314 while (acpe != NULL) {
315 log_info(class, path)("add app shared path %s", acpe->name());
316 SharedClassPathEntry* ent = shared_path(i);
317 ent->init(acpe->name(), THREAD);
318 EXCEPTION_MARK;
319 SharedClassUtil::update_shared_classpath(acpe, ent, THREAD);
320 acpe = acpe->next();
321 i++;
322 }
323
324 // 3. module path
325 ClassPathEntry *mpe = ClassLoader::module_path_entries();
326 while (mpe != NULL) {
327 log_info(class, path)("add module path %s",mpe->name());
328 SharedClassPathEntry* ent = shared_path(i);
329 ent->init(mpe->name(), THREAD);
330 EXCEPTION_MARK;
331 SharedClassUtil::update_shared_classpath(mpe, ent, THREAD);
332 mpe = mpe->next();
333 i++;
334 }
335 assert(i == num_entries, "number of shared path entry mismatch");
336 }
337
338 bool FileMapInfo::validate_shared_path_table() {
339 _validating_shared_path_table = true;
340
341 _shared_path_table = _header->_shared_path_table;
342 _shared_path_entry_size = _header->_shared_path_entry_size;
343 _shared_path_table_size = _header->_shared_path_table_size;
344
345 // Note: _app_module_paths_start_index may not have a valid value if the UseAppCDS flag
346 // wasn't enabled during dump time. Therefore, we need to use the smaller of
347 // _shared_path_table_size and _app_module_paths_start_index for the _app_module_paths_start_index.
348 FileMapHeaderExt* header = (FileMapHeaderExt*)FileMapInfo::current_info()->header();
349 int module_paths_start_index = (header->_app_module_paths_start_index >= _shared_path_table_size) ?
350 _shared_path_table_size : header->_app_module_paths_start_index;
351
352 int count = _shared_path_table_size;
353
354 for (int i=0; i<count; i++) {
355 if (i < module_paths_start_index) {
356 if (shared_path(i)->validate()) {
357 log_info(class, path)("ok");
358 }
359 } else if (i >= module_paths_start_index) {
360 if (shared_path(i)->validate(false /* not a class path entry */)) {
361 log_info(class, path)("ok");
362 }
363 } else if (!PrintSharedArchiveAndExit) {
364 _validating_shared_path_table = false;
365 _shared_path_table = NULL;
366 _shared_path_table_size = 0;
367 return false;
368 }
369 }
370
371 _validating_shared_path_table = false;
372 return true;
373 }
374
375 // Read the FileMapInfo information from the file.
376
377 bool FileMapInfo::init_from_file(int fd) {
378 size_t sz = _header->data_size();
379 char* addr = _header->data();
380 size_t n = os::read(fd, addr, (unsigned int)sz);
381 if (n != sz) {
382 fail_continue("Unable to read the file header.");
383 return false;
384 }
385 if (_header->_version != current_version()) {
386 fail_continue("The shared archive file has the wrong version.");
387 return false;
388 }
389 _file_offset = (long)n;
390
391 size_t info_size = _header->_paths_misc_info_size;
392 _paths_misc_info = NEW_C_HEAP_ARRAY_RETURN_NULL(char, info_size, mtClass);
393 if (_paths_misc_info == NULL) {
394 fail_continue("Unable to read the file header.");
937 size_t used = si->_used;
938 size_t size = align_up(used, os::vm_allocation_granularity());
939
940 if (used == 0) {
941 return;
942 }
943
944 char* addr = _header->region_addr(i);
945 if (!os::unmap_memory(addr, size)) {
946 fail_stop("Unable to unmap shared space.");
947 }
948 }
949
950 void FileMapInfo::assert_mark(bool check) {
951 if (!check) {
952 fail_stop("Mark mismatch while restoring from shared file.");
953 }
954 }
955
956 void FileMapInfo::metaspace_pointers_do(MetaspaceClosure* it) {
957 it->push(&_shared_path_table);
958 for (int i=0; i<_shared_path_table_size; i++) {
959 shared_path(i)->metaspace_pointers_do(it);
960 }
961 }
962
963
964 FileMapInfo* FileMapInfo::_current_info = NULL;
965 Array<u8>* FileMapInfo::_shared_path_table = NULL;
966 int FileMapInfo::_shared_path_table_size = 0;
967 size_t FileMapInfo::_shared_path_entry_size = 0x1234baad;
968 bool FileMapInfo::_validating_shared_path_table = false;
969
970 // Open the shared archive file, read and validate the header
971 // information (version, boot classpath, etc.). If initialization
972 // fails, shared spaces are disabled and the file is closed. [See
973 // fail_continue.]
974 //
975 // Validation of the archive is done in two steps:
976 //
977 // [1] validate_header() - done here. This checks the header, including _paths_misc_info.
978 // [2] validate_shared_path_table - this is done later, because the table is in the RW
979 // region of the archive, which is not mapped yet.
980 bool FileMapInfo::initialize() {
981 assert(UseSharedSpaces, "UseSharedSpaces expected.");
982
983 if (!open_for_read()) {
984 return false;
985 }
986
987 init_from_file(_fd);
988 if (!validate_header()) {
989 return false;
990 }
991 return true;
992 }
993
994 char* FileMapInfo::FileMapHeader::region_addr(int idx) {
995 if (MetaspaceShared::is_heap_region(idx)) {
996 return _space[idx]._used > 0 ?
997 (char*)((void*)CompressedOops::decode_not_null((narrowOop)_space[idx]._addr._offset)) : NULL;
998 } else {
|