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 FileMapInfo::fail_continue("Required classpath entry does not exist: %s", name);
241 ok = false;
242 } else if (is_dir() && is_class_path) {
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_shared_path_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_module_path_entries = ClassLoader::num_module_path_entries();
283 int num_entries = num_boot_classpath_entries + num_app_classpath_entries + num_module_path_entries;
284 size_t bytes = entry_size * num_entries;
285
286 _shared_path_table = MetadataFactory::new_array<u8>(loader_data, (int)(bytes + 7 / 8), THREAD);
287 _shared_path_table_size = num_entries;
288 _shared_path_entry_size = entry_size;
289
290 // 1. boot class path
291 int i = 0;
292 ClassPathEntry* cpe = jrt;
293 while (cpe != NULL) {
294 const char* type = ((cpe == jrt) ? "jrt" : (cpe->is_jar_file() ? "jar" : "dir"));
295 log_info(class, path)("add main shared path (%s) %s", type, cpe->name());
296 SharedClassPathEntry* ent = shared_path(i);
297 ent->init(cpe->name(), THREAD);
298 if (cpe != jrt) { // No need to do jimage.
299 EXCEPTION_MARK; // The following call should never throw, but would exit VM on error.
300 SharedClassUtil::update_shared_classpath(cpe, ent, THREAD);
301 }
302 cpe = ClassLoader::get_next_boot_classpath_entry(cpe);
303 i++;
304 }
305 assert(i == num_boot_classpath_entries,
306 "number of boot class path entry mismatch");
307
308 // 2. app class path
309 ClassPathEntry *acpe = ClassLoader::app_classpath_entries();
310 while (acpe != NULL) {
311 log_info(class, path)("add app shared path %s", acpe->name());
312 SharedClassPathEntry* ent = shared_path(i);
313 ent->init(acpe->name(), THREAD);
314 EXCEPTION_MARK;
315 SharedClassUtil::update_shared_classpath(acpe, ent, THREAD);
316 acpe = acpe->next();
317 i++;
318 }
319
320 // 3. module path
321 ClassPathEntry *mpe = ClassLoader::module_path_entries();
322 while (mpe != NULL) {
323 log_info(class, path)("add module path %s",mpe->name());
324 SharedClassPathEntry* ent = shared_path(i);
325 ent->init(mpe->name(), THREAD);
326 EXCEPTION_MARK;
327 SharedClassUtil::update_shared_classpath(mpe, ent, THREAD);
328 mpe = mpe->next();
329 i++;
330 }
331 assert(i == num_entries, "number of shared path entry mismatch");
332 }
333
334 bool FileMapInfo::validate_shared_path_table() {
335 _validating_shared_path_table = true;
336
337 _shared_path_table = _header->_shared_path_table;
338 _shared_path_entry_size = _header->_shared_path_entry_size;
339 _shared_path_table_size = _header->_shared_path_table_size;
340
341 // Note: _app_module_paths_start_index may not have a valid value if the UseAppCDS flag
342 // wasn't enabled during dump time. Therefore, we need to use the smaller of
343 // _shared_path_table_size and _app_module_paths_start_index for the _app_module_paths_start_index.
344 FileMapHeaderExt* header = (FileMapHeaderExt*)FileMapInfo::current_info()->header();
345 int module_paths_start_index = (header->_app_module_paths_start_index >= _shared_path_table_size) ?
346 _shared_path_table_size : header->_app_module_paths_start_index;
347
348 int count = _shared_path_table_size;
349
350 for (int i=0; i<count; i++) {
351 if (i < module_paths_start_index) {
352 if (shared_path(i)->validate()) {
353 log_info(class, path)("ok");
354 }
355 } else if (i >= module_paths_start_index) {
356 if (shared_path(i)->validate(false /* not a class path entry */)) {
357 log_info(class, path)("ok");
358 }
359 } else if (!PrintSharedArchiveAndExit) {
360 _validating_shared_path_table = false;
361 _shared_path_table = NULL;
362 _shared_path_table_size = 0;
363 return false;
364 }
365 }
366
367 _validating_shared_path_table = false;
368 return true;
369 }
370
371 // Read the FileMapInfo information from the file.
372
373 bool FileMapInfo::init_from_file(int fd) {
374 size_t sz = _header->data_size();
375 char* addr = _header->data();
376 size_t n = os::read(fd, addr, (unsigned int)sz);
377 if (n != sz) {
378 fail_continue("Unable to read the file header.");
379 return false;
380 }
381 if (_header->_version != current_version()) {
382 fail_continue("The shared archive file has the wrong version.");
383 return false;
384 }
385 _file_offset = (long)n;
386
387 size_t info_size = _header->_paths_misc_info_size;
388 _paths_misc_info = NEW_C_HEAP_ARRAY_RETURN_NULL(char, info_size, mtClass);
389 if (_paths_misc_info == NULL) {
390 fail_continue("Unable to read the file header.");
933 size_t used = si->_used;
934 size_t size = align_up(used, os::vm_allocation_granularity());
935
936 if (used == 0) {
937 return;
938 }
939
940 char* addr = _header->region_addr(i);
941 if (!os::unmap_memory(addr, size)) {
942 fail_stop("Unable to unmap shared space.");
943 }
944 }
945
946 void FileMapInfo::assert_mark(bool check) {
947 if (!check) {
948 fail_stop("Mark mismatch while restoring from shared file.");
949 }
950 }
951
952 void FileMapInfo::metaspace_pointers_do(MetaspaceClosure* it) {
953 it->push(&_shared_path_table);
954 for (int i=0; i<_shared_path_table_size; i++) {
955 shared_path(i)->metaspace_pointers_do(it);
956 }
957 }
958
959
960 FileMapInfo* FileMapInfo::_current_info = NULL;
961 Array<u8>* FileMapInfo::_shared_path_table = NULL;
962 int FileMapInfo::_shared_path_table_size = 0;
963 size_t FileMapInfo::_shared_path_entry_size = 0x1234baad;
964 bool FileMapInfo::_validating_shared_path_table = false;
965
966 // Open the shared archive file, read and validate the header
967 // information (version, boot classpath, etc.). If initialization
968 // fails, shared spaces are disabled and the file is closed. [See
969 // fail_continue.]
970 //
971 // Validation of the archive is done in two steps:
972 //
973 // [1] validate_header() - done here. This checks the header, including _paths_misc_info.
974 // [2] validate_shared_path_table - this is done later, because the table is in the RW
975 // region of the archive, which is not mapped yet.
976 bool FileMapInfo::initialize() {
977 assert(UseSharedSpaces, "UseSharedSpaces expected.");
978
979 if (!open_for_read()) {
980 return false;
981 }
982
983 init_from_file(_fd);
984 if (!validate_header()) {
985 return false;
986 }
987 return true;
988 }
989
990 char* FileMapInfo::FileMapHeader::region_addr(int idx) {
991 if (MetaspaceShared::is_heap_region(idx)) {
992 return _space[idx]._used > 0 ?
993 (char*)((void*)CompressedOops::decode_not_null((narrowOop)_space[idx]._addr._offset)) : NULL;
994 } else {
|