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 #include "precompiled.hpp" 26 #include "jvm.h" 27 #include "classfile/classFileStream.hpp" 28 #include "classfile/classLoader.inline.hpp" 29 #include "classfile/classLoaderData.inline.hpp" 30 #include "classfile/classLoaderExt.hpp" 31 #include "classfile/symbolTable.hpp" 32 #include "classfile/systemDictionaryShared.hpp" 33 #include "classfile/altHashing.hpp" 34 #include "logging/log.hpp" 35 #include "logging/logStream.hpp" 36 #include "logging/logMessage.hpp" 37 #include "memory/dynamicArchive.hpp" 38 #include "memory/filemap.hpp" 39 #include "memory/heapShared.inline.hpp" 40 #include "memory/iterator.inline.hpp" 41 #include "memory/metadataFactory.hpp" 42 #include "memory/metaspaceClosure.hpp" 43 #include "memory/metaspaceShared.hpp" 44 #include "memory/oopFactory.hpp" 45 #include "memory/universe.hpp" 46 #include "oops/compressedOops.hpp" 47 #include "oops/compressedOops.inline.hpp" 48 #include "oops/objArrayOop.hpp" 49 #include "oops/oop.inline.hpp" 50 #include "prims/jvmtiExport.hpp" 51 #include "runtime/arguments.hpp" 52 #include "runtime/java.hpp" 53 #include "runtime/mutexLocker.hpp" 54 #include "runtime/os.inline.hpp" 55 #include "runtime/vm_version.hpp" 56 #include "services/memTracker.hpp" 57 #include "utilities/align.hpp" 58 #include "utilities/defaultStream.hpp" 59 #if INCLUDE_G1GC 60 #include "gc/g1/g1CollectedHeap.hpp" 61 #include "gc/g1/heapRegion.hpp" 62 #endif 63 64 # include <sys/stat.h> 65 # include <errno.h> 66 67 #ifndef O_BINARY // if defined (Win32) use binary files. 68 #define O_BINARY 0 // otherwise do nothing. 69 #endif 70 71 extern address JVM_FunctionAtStart(); 72 extern address JVM_FunctionAtEnd(); 73 74 // Complain and stop. All error conditions occurring during the writing of 75 // an archive file should stop the process. Unrecoverable errors during 76 // the reading of the archive file should stop the process. 77 78 static void fail_exit(const char *msg, va_list ap) { 79 // This occurs very early during initialization: tty is not initialized. 80 jio_fprintf(defaultStream::error_stream(), 81 "An error has occurred while processing the" 82 " shared archive file.\n"); 83 jio_vfprintf(defaultStream::error_stream(), msg, ap); 84 jio_fprintf(defaultStream::error_stream(), "\n"); 85 // Do not change the text of the below message because some tests check for it. 86 vm_exit_during_initialization("Unable to use shared archive.", NULL); 87 } 88 89 90 void FileMapInfo::fail_stop(const char *msg, ...) { 91 va_list ap; 92 va_start(ap, msg); 93 fail_exit(msg, ap); // Never returns. 94 va_end(ap); // for completeness. 95 } 96 97 98 // Complain and continue. Recoverable errors during the reading of the 99 // archive file may continue (with sharing disabled). 100 // 101 // If we continue, then disable shared spaces and close the file. 102 103 void FileMapInfo::fail_continue(const char *msg, ...) { 104 va_list ap; 105 va_start(ap, msg); 106 if (_dynamic_archive_info == NULL) { 107 MetaspaceShared::set_archive_loading_failed(); 108 } else { 109 // _dynamic_archive_info has been setup after mapping the base archive 110 DynamicArchive::disable(); 111 } 112 if (PrintSharedArchiveAndExit && _validating_shared_path_table) { 113 // If we are doing PrintSharedArchiveAndExit and some of the classpath entries 114 // do not validate, we can still continue "limping" to validate the remaining 115 // entries. No need to quit. 116 tty->print("["); 117 tty->vprint(msg, ap); 118 tty->print_cr("]"); 119 } else { 120 if (RequireSharedSpaces) { 121 fail_exit(msg, ap); 122 } else { 123 if (log_is_enabled(Info, cds)) { 124 ResourceMark rm; 125 LogStream ls(Log(cds)::info()); 126 ls.print("UseSharedSpaces: "); 127 ls.vprint_cr(msg, ap); 128 } 129 } 130 if (_dynamic_archive_info == NULL) { 131 UseSharedSpaces = false; 132 assert(current_info() != NULL, "singleton must be registered"); 133 current_info()->close(); 134 } else { 135 // We are failing when loading the top archive, but the base archive should 136 // continue to work. 137 log_warning(cds, dynamic)("Unable to use shared archive. The top archive failed to load: %s", _dynamic_archive_info->_full_path); 138 } 139 } 140 va_end(ap); 141 } 142 143 // Fill in the fileMapInfo structure with data about this VM instance. 144 145 // This method copies the vm version info into header_version. If the version is too 146 // long then a truncated version, which has a hash code appended to it, is copied. 147 // 148 // Using a template enables this method to verify that header_version is an array of 149 // length JVM_IDENT_MAX. This ensures that the code that writes to the CDS file and 150 // the code that reads the CDS file will both use the same size buffer. Hence, will 151 // use identical truncation. This is necessary for matching of truncated versions. 152 template <int N> static void get_header_version(char (&header_version) [N]) { 153 assert(N == JVM_IDENT_MAX, "Bad header_version size"); 154 155 const char *vm_version = VM_Version::internal_vm_info_string(); 156 const int version_len = (int)strlen(vm_version); 157 158 if (version_len < (JVM_IDENT_MAX-1)) { 159 strcpy(header_version, vm_version); 160 161 } else { 162 // Get the hash value. Use a static seed because the hash needs to return the same 163 // value over multiple jvm invocations. 164 unsigned int hash = AltHashing::murmur3_32(8191, (const jbyte*)vm_version, version_len); 165 166 // Truncate the ident, saving room for the 8 hex character hash value. 167 strncpy(header_version, vm_version, JVM_IDENT_MAX-9); 168 169 // Append the hash code as eight hex digits. 170 sprintf(&header_version[JVM_IDENT_MAX-9], "%08x", hash); 171 header_version[JVM_IDENT_MAX-1] = 0; // Null terminate. 172 } 173 } 174 175 FileMapInfo::FileMapInfo(bool is_static) { 176 memset((void*)this, 0, sizeof(FileMapInfo)); 177 _is_static = is_static; 178 size_t header_size; 179 if (is_static) { 180 assert(_current_info == NULL, "must be singleton"); // not thread safe 181 _current_info = this; 182 header_size = sizeof(FileMapHeader); 183 } else { 184 assert(_dynamic_archive_info == NULL, "must be singleton"); // not thread safe 185 _dynamic_archive_info = this; 186 header_size = sizeof(DynamicArchiveHeader); 187 } 188 _header = (FileMapHeader*)os::malloc(header_size, mtInternal); 189 memset((void*)_header, 0, header_size); 190 _header->_header_size = header_size; 191 _header->_version = INVALID_CDS_ARCHIVE_VERSION; 192 _header->_has_platform_or_app_classes = true; 193 _file_offset = 0; 194 _file_open = false; 195 } 196 197 FileMapInfo::~FileMapInfo() { 198 if (_is_static) { 199 assert(_current_info == this, "must be singleton"); // not thread safe 200 _current_info = NULL; 201 } else { 202 assert(_dynamic_archive_info == this, "must be singleton"); // not thread safe 203 _dynamic_archive_info = NULL; 204 } 205 } 206 207 void FileMapInfo::populate_header(size_t alignment) { 208 _header->populate(this, alignment); 209 } 210 211 void FileMapHeader::populate(FileMapInfo* mapinfo, size_t alignment) { 212 if (DynamicDumpSharedSpaces) { 213 _magic = CDS_DYNAMIC_ARCHIVE_MAGIC; 214 } else { 215 _magic = CDS_ARCHIVE_MAGIC; 216 } 217 _version = CURRENT_CDS_ARCHIVE_VERSION; 218 _alignment = alignment; 219 _obj_alignment = ObjectAlignmentInBytes; 220 _compact_strings = CompactStrings; 221 _narrow_oop_mode = CompressedOops::mode(); 222 _narrow_oop_base = CompressedOops::base(); 223 _narrow_oop_shift = CompressedOops::shift(); 224 _max_heap_size = MaxHeapSize; 225 _narrow_klass_base = CompressedKlassPointers::base(); 226 _narrow_klass_shift = CompressedKlassPointers::shift(); 227 _shared_path_table = mapinfo->_shared_path_table; 228 if (HeapShared::is_heap_object_archiving_allowed()) { 229 _heap_reserved = Universe::heap()->reserved_region(); 230 } 231 232 // The following fields are for sanity checks for whether this archive 233 // will function correctly with this JVM and the bootclasspath it's 234 // invoked with. 235 236 // JVM version string ... changes on each build. 237 get_header_version(_jvm_ident); 238 239 ClassLoaderExt::finalize_shared_paths_misc_info(); 240 _app_class_paths_start_index = ClassLoaderExt::app_class_paths_start_index(); 241 _app_module_paths_start_index = ClassLoaderExt::app_module_paths_start_index(); 242 _num_module_paths = ClassLoader::num_module_path_entries(); 243 _max_used_path_index = ClassLoaderExt::max_used_path_index(); 244 245 _verify_local = BytecodeVerificationLocal; 246 _verify_remote = BytecodeVerificationRemote; 247 _has_platform_or_app_classes = ClassLoaderExt::has_platform_or_app_classes(); 248 _shared_base_address = SharedBaseAddress; 249 _allow_archiving_with_java_agent = AllowArchivingWithJavaAgent; 250 // the following 2 fields will be set in write_header for dynamic archive header 251 _base_archive_name_size = 0; 252 _base_archive_is_default = false; 253 } 254 255 void SharedClassPathEntry::init(bool is_modules_image, 256 ClassPathEntry* cpe, TRAPS) { 257 assert(DumpSharedSpaces || DynamicDumpSharedSpaces, "dump time only"); 258 _timestamp = 0; 259 _filesize = 0; 260 _from_class_path_attr = false; 261 262 struct stat st; 263 if (os::stat(cpe->name(), &st) == 0) { 264 if ((st.st_mode & S_IFMT) == S_IFDIR) { 265 _type = dir_entry; 266 } else { 267 // The timestamp of the modules_image is not checked at runtime. 268 if (is_modules_image) { 269 _type = modules_image_entry; 270 } else { 271 _type = jar_entry; 272 _timestamp = st.st_mtime; 273 _from_class_path_attr = cpe->from_class_path_attr(); 274 } 275 _filesize = st.st_size; 276 } 277 } else { 278 // The file/dir must exist, or it would not have been added 279 // into ClassLoader::classpath_entry(). 280 // 281 // If we can't access a jar file in the boot path, then we can't 282 // make assumptions about where classes get loaded from. 283 FileMapInfo::fail_stop("Unable to open file %s.", cpe->name()); 284 } 285 286 size_t len = strlen(cpe->name()) + 1; 287 _name = MetadataFactory::new_array<char>(ClassLoaderData::the_null_class_loader_data(), (int)len, THREAD); 288 strcpy(_name->data(), cpe->name()); 289 } 290 291 bool SharedClassPathEntry::validate(bool is_class_path) { 292 assert(UseSharedSpaces, "runtime only"); 293 294 struct stat st; 295 const char* name; 296 297 // In order to validate the runtime modules image file size against the archived 298 // size information, we need to obtain the runtime modules image path. The recorded 299 // dump time modules image path in the archive may be different from the runtime path 300 // if the JDK image has beed moved after generating the archive. 301 if (is_modules_image()) { 302 name = ClassLoader::get_jrt_entry()->name(); 303 } else { 304 name = this->name(); 305 } 306 307 bool ok = true; 308 log_info(class, path)("checking shared classpath entry: %s", name); 309 if (os::stat(name, &st) != 0 && is_class_path) { 310 // If the archived module path entry does not exist at runtime, it is not fatal 311 // (no need to invalid the shared archive) because the shared runtime visibility check 312 // filters out any archived module classes that do not have a matching runtime 313 // module path location. 314 FileMapInfo::fail_continue("Required classpath entry does not exist: %s", name); 315 ok = false; 316 } else if (is_dir()) { 317 if (!os::dir_is_empty(name)) { 318 FileMapInfo::fail_continue("directory is not empty: %s", name); 319 ok = false; 320 } 321 } else if ((has_timestamp() && _timestamp != st.st_mtime) || 322 _filesize != st.st_size) { 323 ok = false; 324 if (PrintSharedArchiveAndExit) { 325 FileMapInfo::fail_continue(_timestamp != st.st_mtime ? 326 "Timestamp mismatch" : 327 "File size mismatch"); 328 } else { 329 FileMapInfo::fail_continue("A jar file is not the one used while building" 330 " the shared archive file: %s", name); 331 } 332 } 333 334 if (PrintSharedArchiveAndExit && !ok) { 335 // If PrintSharedArchiveAndExit is enabled, don't report failure to the 336 // caller. Please see above comments for more details. 337 ok = true; 338 MetaspaceShared::set_archive_loading_failed(); 339 } 340 return ok; 341 } 342 343 void SharedClassPathEntry::metaspace_pointers_do(MetaspaceClosure* it) { 344 it->push(&_name); 345 it->push(&_manifest); 346 } 347 348 void SharedPathTable::metaspace_pointers_do(MetaspaceClosure* it) { 349 it->push(&_table); 350 for (int i=0; i<_size; i++) { 351 path_at(i)->metaspace_pointers_do(it); 352 } 353 } 354 355 void SharedPathTable::dumptime_init(ClassLoaderData* loader_data, Thread* THREAD) { 356 size_t entry_size = sizeof(SharedClassPathEntry); 357 int num_boot_classpath_entries = ClassLoader::num_boot_classpath_entries(); 358 int num_app_classpath_entries = ClassLoader::num_app_classpath_entries(); 359 int num_module_path_entries = ClassLoader::num_module_path_entries(); 360 int num_entries = num_boot_classpath_entries + num_app_classpath_entries + num_module_path_entries; 361 size_t bytes = entry_size * num_entries; 362 363 _table = MetadataFactory::new_array<u8>(loader_data, (int)(bytes + 7 / 8), THREAD); 364 _size = num_entries; 365 } 366 367 void FileMapInfo::allocate_shared_path_table() { 368 assert(DumpSharedSpaces || DynamicDumpSharedSpaces, "Sanity"); 369 370 Thread* THREAD = Thread::current(); 371 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data(); 372 ClassPathEntry* jrt = ClassLoader::get_jrt_entry(); 373 374 assert(jrt != NULL, 375 "No modular java runtime image present when allocating the CDS classpath entry table"); 376 377 _shared_path_table.dumptime_init(loader_data, THREAD); 378 379 // 1. boot class path 380 int i = 0; 381 ClassPathEntry* cpe = jrt; 382 while (cpe != NULL) { 383 bool is_jrt = (cpe == jrt); 384 const char* type = (is_jrt ? "jrt" : (cpe->is_jar_file() ? "jar" : "dir")); 385 log_info(class, path)("add main shared path (%s) %s", type, cpe->name()); 386 SharedClassPathEntry* ent = shared_path(i); 387 ent->init(is_jrt, cpe, THREAD); 388 if (!is_jrt) { // No need to do the modules image. 389 EXCEPTION_MARK; // The following call should never throw, but would exit VM on error. 390 update_shared_classpath(cpe, ent, THREAD); 391 } 392 cpe = ClassLoader::get_next_boot_classpath_entry(cpe); 393 i++; 394 } 395 assert(i == ClassLoader::num_boot_classpath_entries(), 396 "number of boot class path entry mismatch"); 397 398 // 2. app class path 399 ClassPathEntry *acpe = ClassLoader::app_classpath_entries(); 400 while (acpe != NULL) { 401 log_info(class, path)("add app shared path %s", acpe->name()); 402 SharedClassPathEntry* ent = shared_path(i); 403 ent->init(false, acpe, THREAD); 404 EXCEPTION_MARK; 405 update_shared_classpath(acpe, ent, THREAD); 406 acpe = acpe->next(); 407 i++; 408 } 409 410 // 3. module path 411 ClassPathEntry *mpe = ClassLoader::module_path_entries(); 412 while (mpe != NULL) { 413 log_info(class, path)("add module path %s",mpe->name()); 414 SharedClassPathEntry* ent = shared_path(i); 415 ent->init(false, mpe, THREAD); 416 EXCEPTION_MARK; 417 update_shared_classpath(mpe, ent, THREAD); 418 mpe = mpe->next(); 419 i++; 420 } 421 assert(i == _shared_path_table.size(), "number of shared path entry mismatch"); 422 } 423 424 void FileMapInfo::check_nonempty_dir_in_shared_path_table() { 425 assert(DumpSharedSpaces || DynamicDumpSharedSpaces, "dump time only"); 426 427 bool has_nonempty_dir = false; 428 429 int last = _shared_path_table.size() - 1; 430 if (last > ClassLoaderExt::max_used_path_index()) { 431 // no need to check any path beyond max_used_path_index 432 last = ClassLoaderExt::max_used_path_index(); 433 } 434 435 for (int i = 0; i <= last; i++) { 436 SharedClassPathEntry *e = shared_path(i); 437 if (e->is_dir()) { 438 const char* path = e->name(); 439 if (!os::dir_is_empty(path)) { 440 tty->print_cr("Error: non-empty directory '%s'", path); 441 has_nonempty_dir = true; 442 } 443 } 444 } 445 446 if (has_nonempty_dir) { 447 ClassLoader::exit_with_path_failure("Cannot have non-empty directory in paths", NULL); 448 } 449 } 450 451 class ManifestStream: public ResourceObj { 452 private: 453 u1* _buffer_start; // Buffer bottom 454 u1* _buffer_end; // Buffer top (one past last element) 455 u1* _current; // Current buffer position 456 457 public: 458 // Constructor 459 ManifestStream(u1* buffer, int length) : _buffer_start(buffer), 460 _current(buffer) { 461 _buffer_end = buffer + length; 462 } 463 464 static bool is_attr(u1* attr, const char* name) { 465 return strncmp((const char*)attr, name, strlen(name)) == 0; 466 } 467 468 static char* copy_attr(u1* value, size_t len) { 469 char* buf = NEW_RESOURCE_ARRAY(char, len + 1); 470 strncpy(buf, (char*)value, len); 471 buf[len] = 0; 472 return buf; 473 } 474 475 // The return value indicates if the JAR is signed or not 476 bool check_is_signed() { 477 u1* attr = _current; 478 bool isSigned = false; 479 while (_current < _buffer_end) { 480 if (*_current == '\n') { 481 *_current = '\0'; 482 u1* value = (u1*)strchr((char*)attr, ':'); 483 if (value != NULL) { 484 assert(*(value+1) == ' ', "Unrecognized format" ); 485 if (strstr((char*)attr, "-Digest") != NULL) { 486 isSigned = true; 487 break; 488 } 489 } 490 *_current = '\n'; // restore 491 attr = _current + 1; 492 } 493 _current ++; 494 } 495 return isSigned; 496 } 497 }; 498 499 void FileMapInfo::update_shared_classpath(ClassPathEntry *cpe, SharedClassPathEntry* ent, TRAPS) { 500 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data(); 501 ResourceMark rm(THREAD); 502 jint manifest_size; 503 504 if (cpe->is_jar_file()) { 505 assert(ent->is_jar(), "the shared class path entry is not a JAR file"); 506 char* manifest = ClassLoaderExt::read_manifest(cpe, &manifest_size, CHECK); 507 if (manifest != NULL) { 508 ManifestStream* stream = new ManifestStream((u1*)manifest, 509 manifest_size); 510 if (stream->check_is_signed()) { 511 ent->set_is_signed(); 512 } else { 513 // Copy the manifest into the shared archive 514 manifest = ClassLoaderExt::read_raw_manifest(cpe, &manifest_size, CHECK); 515 Array<u1>* buf = MetadataFactory::new_array<u1>(loader_data, 516 manifest_size, 517 THREAD); 518 char* p = (char*)(buf->data()); 519 memcpy(p, manifest, manifest_size); 520 ent->set_manifest(buf); 521 } 522 } 523 } 524 } 525 526 char* FileMapInfo::skip_first_path_entry(const char* path) { 527 size_t path_sep_len = strlen(os::path_separator()); 528 char* p = strstr((char*)path, os::path_separator()); 529 if (p != NULL) { 530 debug_only( { 531 size_t image_name_len = strlen(MODULES_IMAGE_NAME); 532 assert(strncmp(p - image_name_len, MODULES_IMAGE_NAME, image_name_len) == 0, 533 "first entry must be the modules image"); 534 } ); 535 p += path_sep_len; 536 } else { 537 debug_only( { 538 assert(ClassLoader::string_ends_with(path, MODULES_IMAGE_NAME), 539 "first entry must be the modules image"); 540 } ); 541 } 542 return p; 543 } 544 545 int FileMapInfo::num_paths(const char* path) { 546 if (path == NULL) { 547 return 0; 548 } 549 int npaths = 1; 550 char* p = (char*)path; 551 while (p != NULL) { 552 char* prev = p; 553 p = strstr((char*)p, os::path_separator()); 554 if (p != NULL) { 555 p++; 556 // don't count empty path 557 if ((p - prev) > 1) { 558 npaths++; 559 } 560 } 561 } 562 return npaths; 563 } 564 565 GrowableArray<char*>* FileMapInfo::create_path_array(const char* path) { 566 GrowableArray<char*>* path_array = new(ResourceObj::RESOURCE_AREA, mtInternal) 567 GrowableArray<char*>(10); 568 char* begin_ptr = (char*)path; 569 char* end_ptr = strchr((char*)path, os::path_separator()[0]); 570 if (end_ptr == NULL) { 571 end_ptr = strchr((char*)path, '\0'); 572 } 573 while (end_ptr != NULL) { 574 if ((end_ptr - begin_ptr) > 1) { 575 struct stat st; 576 char* temp_name = NEW_RESOURCE_ARRAY(char, (size_t)(end_ptr - begin_ptr + 1)); 577 strncpy(temp_name, begin_ptr, end_ptr - begin_ptr); 578 temp_name[end_ptr - begin_ptr] = '\0'; 579 if (os::stat(temp_name, &st) == 0) { 580 path_array->append(temp_name); 581 } 582 } 583 if (end_ptr < (path + strlen(path))) { 584 begin_ptr = ++end_ptr; 585 end_ptr = strchr(begin_ptr, os::path_separator()[0]); 586 if (end_ptr == NULL) { 587 end_ptr = strchr(begin_ptr, '\0'); 588 } 589 } else { 590 break; 591 } 592 } 593 return path_array; 594 } 595 596 bool FileMapInfo::fail(const char* msg, const char* name) { 597 ClassLoader::trace_class_path(msg, name); 598 MetaspaceShared::set_archive_loading_failed(); 599 return false; 600 } 601 602 bool FileMapInfo::check_paths(int shared_path_start_idx, int num_paths, GrowableArray<char*>* rp_array) { 603 int i = 0; 604 int j = shared_path_start_idx; 605 bool mismatch = false; 606 while (i < num_paths && !mismatch) { 607 while (shared_path(j)->from_class_path_attr()) { 608 // shared_path(j) was expanded from the JAR file attribute "Class-Path:" 609 // during dump time. It's not included in the -classpath VM argument. 610 j++; 611 } 612 if (!os::same_files(shared_path(j)->name(), rp_array->at(i))) { 613 mismatch = true; 614 } 615 i++; 616 j++; 617 } 618 return mismatch; 619 } 620 621 bool FileMapInfo::validate_boot_class_paths() { 622 // 623 // - Archive contains boot classes only - relaxed boot path check: 624 // Extra path elements appended to the boot path at runtime are allowed. 625 // 626 // - Archive contains application or platform classes - strict boot path check: 627 // Validate the entire runtime boot path, which must be compatible 628 // with the dump time boot path. Appending boot path at runtime is not 629 // allowed. 630 // 631 632 // The first entry in boot path is the modules_image (guaranteed by 633 // ClassLoader::setup_boot_search_path()). Skip the first entry. The 634 // path of the runtime modules_image may be different from the dump 635 // time path (e.g. the JDK image is copied to a different location 636 // after generating the shared archive), which is acceptable. For most 637 // common cases, the dump time boot path might contain modules_image only. 638 char* runtime_boot_path = Arguments::get_sysclasspath(); 639 char* rp = skip_first_path_entry(runtime_boot_path); 640 assert(shared_path(0)->is_modules_image(), "first shared_path must be the modules image"); 641 int dp_len = _header->_app_class_paths_start_index - 1; // ignore the first path to the module image 642 bool mismatch = false; 643 644 bool relaxed_check = !header()->has_platform_or_app_classes(); 645 if (dp_len == 0 && rp == NULL) { 646 return true; // ok, both runtime and dump time boot paths have modules_images only 647 } else if (dp_len == 0 && rp != NULL) { 648 if (relaxed_check) { 649 return true; // ok, relaxed check, runtime has extra boot append path entries 650 } else { 651 mismatch = true; 652 } 653 } else if (dp_len > 0 && rp != NULL) { 654 int num; 655 ResourceMark rm; 656 GrowableArray<char*>* rp_array = create_path_array(rp); 657 int rp_len = rp_array->length(); 658 if (rp_len >= dp_len) { 659 if (relaxed_check) { 660 // only check the leading entries in the runtime boot path, up to 661 // the length of the dump time boot path 662 num = dp_len; 663 } else { 664 // check the full runtime boot path, must match with dump time 665 num = rp_len; 666 } 667 mismatch = check_paths(1, num, rp_array); 668 } 669 } 670 671 if (mismatch) { 672 // The paths are different 673 return fail("[BOOT classpath mismatch, actual =", runtime_boot_path); 674 } 675 return true; 676 } 677 678 bool FileMapInfo::validate_app_class_paths(int shared_app_paths_len) { 679 const char *appcp = Arguments::get_appclasspath(); 680 assert(appcp != NULL, "NULL app classpath"); 681 int rp_len = num_paths(appcp); 682 bool mismatch = false; 683 if (rp_len < shared_app_paths_len) { 684 return fail("Run time APP classpath is shorter than the one at dump time: ", appcp); 685 } 686 if (shared_app_paths_len != 0 && rp_len != 0) { 687 // Prefix is OK: E.g., dump with -cp foo.jar, but run with -cp foo.jar:bar.jar. 688 ResourceMark rm; 689 GrowableArray<char*>* rp_array = create_path_array(appcp); 690 if (rp_array->length() == 0) { 691 // None of the jar file specified in the runtime -cp exists. 692 return fail("None of the jar file specified in the runtime -cp exists: -Djava.class.path=", appcp); 693 } 694 int j = _header->_app_class_paths_start_index; 695 mismatch = check_paths(j, shared_app_paths_len, rp_array); 696 if (mismatch) { 697 return fail("[APP classpath mismatch, actual: -Djava.class.path=", appcp); 698 } 699 } 700 return true; 701 } 702 703 bool FileMapInfo::validate_shared_path_table() { 704 assert(UseSharedSpaces, "runtime only"); 705 706 _validating_shared_path_table = true; 707 708 // Load the shared path table info from the archive header 709 _shared_path_table = _header->_shared_path_table; 710 if (DynamicDumpSharedSpaces) { 711 // Only support dynamic dumping with the usage of the default CDS archive 712 // or a simple base archive. 713 // If the base layer archive contains additional path component besides 714 // the runtime image and the -cp, dynamic dumping is disabled. 715 // 716 // When dynamic archiving is enabled, the _shared_path_table is overwritten 717 // to include the application path and stored in the top layer archive. 718 assert(shared_path(0)->is_modules_image(), "first shared_path must be the modules image"); 719 if (_header->_app_class_paths_start_index > 1) { 720 DynamicDumpSharedSpaces = false; 721 warning( 722 "Dynamic archiving is disabled because base layer archive has appended boot classpath"); 723 } 724 if (_header->_num_module_paths > 0) { 725 DynamicDumpSharedSpaces = false; 726 warning( 727 "Dynamic archiving is disabled because base layer archive has module path"); 728 } 729 } 730 731 int module_paths_start_index = _header->_app_module_paths_start_index; 732 int shared_app_paths_len = 0; 733 734 // validate the path entries up to the _max_used_path_index 735 for (int i=0; i < _header->_max_used_path_index + 1; i++) { 736 if (i < module_paths_start_index) { 737 if (shared_path(i)->validate()) { 738 // Only count the app class paths not from the "Class-path" attribute of a jar manifest. 739 if (!shared_path(i)->from_class_path_attr() && i >= _header->_app_class_paths_start_index) { 740 shared_app_paths_len++; 741 } 742 log_info(class, path)("ok"); 743 } else { 744 if (_dynamic_archive_info != NULL && _dynamic_archive_info->_is_static) { 745 assert(!UseSharedSpaces, "UseSharedSpaces should be disabled"); 746 } 747 return false; 748 } 749 } else if (i >= module_paths_start_index) { 750 if (shared_path(i)->validate(false /* not a class path entry */)) { 751 log_info(class, path)("ok"); 752 } else { 753 if (_dynamic_archive_info != NULL && _dynamic_archive_info->_is_static) { 754 assert(!UseSharedSpaces, "UseSharedSpaces should be disabled"); 755 } 756 return false; 757 } 758 } 759 } 760 761 if (_header->_max_used_path_index == 0) { 762 // default archive only contains the module image in the bootclasspath 763 assert(shared_path(0)->is_modules_image(), "first shared_path must be the modules image"); 764 } else { 765 if (!validate_boot_class_paths() || !validate_app_class_paths(shared_app_paths_len)) { 766 fail_continue("shared class paths mismatch (hint: enable -Xlog:class+path=info to diagnose the failure)"); 767 return false; 768 } 769 } 770 771 _validating_shared_path_table = false; 772 773 #if INCLUDE_JVMTI 774 if (_classpath_entries_for_jvmti != NULL) { 775 os::free(_classpath_entries_for_jvmti); 776 } 777 size_t sz = sizeof(ClassPathEntry*) * get_number_of_shared_paths(); 778 _classpath_entries_for_jvmti = (ClassPathEntry**)os::malloc(sz, mtClass); 779 memset((void*)_classpath_entries_for_jvmti, 0, sz); 780 #endif 781 782 return true; 783 } 784 785 bool FileMapInfo::check_archive(const char* archive_name, bool is_static) { 786 int fd = os::open(archive_name, O_RDONLY | O_BINARY, 0); 787 if (fd < 0) { 788 // do not vm_exit_during_initialization here because Arguments::init_shared_archive_paths() 789 // requires a shared archive name. The open_for_read() function will log a message regarding 790 // failure in opening a shared archive. 791 return false; 792 } 793 794 size_t sz = is_static ? sizeof(FileMapHeader) : sizeof(DynamicArchiveHeader); 795 void* header = os::malloc(sz, mtInternal); 796 memset(header, 0, sz); 797 size_t n = os::read(fd, header, (unsigned int)sz); 798 if (n != sz) { 799 os::free(header); 800 os::close(fd); 801 vm_exit_during_initialization("Unable to read header from shared archive", archive_name); 802 return false; 803 } 804 if (is_static) { 805 FileMapHeader* static_header = (FileMapHeader*)header; 806 if (static_header->_magic != CDS_ARCHIVE_MAGIC) { 807 os::free(header); 808 os::close(fd); 809 vm_exit_during_initialization("Not a base shared archive", archive_name); 810 return false; 811 } 812 } else { 813 DynamicArchiveHeader* dynamic_header = (DynamicArchiveHeader*)header; 814 if (dynamic_header->_magic != CDS_DYNAMIC_ARCHIVE_MAGIC) { 815 os::free(header); 816 os::close(fd); 817 vm_exit_during_initialization("Not a top shared archive", archive_name); 818 return false; 819 } 820 } 821 os::free(header); 822 os::close(fd); 823 return true; 824 } 825 826 bool FileMapInfo::get_base_archive_name_from_header(const char* archive_name, 827 int* size, char** base_archive_name) { 828 int fd = os::open(archive_name, O_RDONLY | O_BINARY, 0); 829 if (fd < 0) { 830 *size = 0; 831 return false; 832 } 833 834 // read the header as a dynamic archive header 835 size_t sz = sizeof(DynamicArchiveHeader); 836 DynamicArchiveHeader* dynamic_header = (DynamicArchiveHeader*)os::malloc(sz, mtInternal); 837 size_t n = os::read(fd, dynamic_header, (unsigned int)sz); 838 if (n != sz) { 839 fail_continue("Unable to read the file header."); 840 os::free(dynamic_header); 841 os::close(fd); 842 return false; 843 } 844 if (dynamic_header->_magic != CDS_DYNAMIC_ARCHIVE_MAGIC) { 845 // Not a dynamic header, no need to proceed further. 846 *size = 0; 847 os::free(dynamic_header); 848 os::close(fd); 849 return false; 850 } 851 if (dynamic_header->_base_archive_is_default) { 852 *base_archive_name = Arguments::get_default_shared_archive_path(); 853 } else { 854 // skip over the _paths_misc_info 855 sz = dynamic_header->_paths_misc_info_size; 856 lseek(fd, (long)sz, SEEK_CUR); 857 // read the base archive name 858 size_t name_size = dynamic_header->_base_archive_name_size; 859 if (name_size == 0) { 860 os::free(dynamic_header); 861 os::close(fd); 862 return false; 863 } 864 *base_archive_name = NEW_C_HEAP_ARRAY(char, name_size, mtInternal); 865 n = os::read(fd, *base_archive_name, (unsigned int)name_size); 866 if (n != name_size) { 867 fail_continue("Unable to read the base archive name from the header."); 868 FREE_C_HEAP_ARRAY(char, *base_archive_name); 869 *base_archive_name = NULL; 870 os::free(dynamic_header); 871 os::close(fd); 872 return false; 873 } 874 } 875 876 os::free(dynamic_header); 877 os::close(fd); 878 return true; 879 } 880 881 void FileMapInfo::restore_shared_path_table() { 882 _shared_path_table = _current_info->_header->_shared_path_table; 883 } 884 885 // Read the FileMapInfo information from the file. 886 887 bool FileMapInfo::init_from_file(int fd, bool is_static) { 888 size_t sz = is_static ? sizeof(FileMapHeader) : sizeof(DynamicArchiveHeader); 889 size_t n = os::read(fd, _header, (unsigned int)sz); 890 if (n != sz) { 891 fail_continue("Unable to read the file header."); 892 return false; 893 } 894 if (_header->_version != CURRENT_CDS_ARCHIVE_VERSION) { 895 fail_continue("The shared archive file has the wrong version."); 896 return false; 897 } 898 _file_offset = n; 899 900 size_t info_size = _header->_paths_misc_info_size; 901 _paths_misc_info = NEW_C_HEAP_ARRAY(char, info_size, mtClass); 902 n = os::read(fd, _paths_misc_info, (unsigned int)info_size); 903 if (n != info_size) { 904 fail_continue("Unable to read the shared path info header."); 905 FREE_C_HEAP_ARRAY(char, _paths_misc_info); 906 _paths_misc_info = NULL; 907 return false; 908 } 909 _file_offset += n + _header->_base_archive_name_size; // accounts for the size of _base_archive_name 910 911 if (is_static) { 912 if (_header->_magic != CDS_ARCHIVE_MAGIC) { 913 fail_continue("Incorrect static archive magic number"); 914 return false; 915 } 916 // just checking the last region is sufficient since the archive is written 917 // in sequential order 918 size_t len = lseek(fd, 0, SEEK_END); 919 CDSFileMapRegion* si = space_at(MetaspaceShared::last_valid_region); 920 // The last space might be empty 921 if (si->_file_offset > len || len - si->_file_offset < si->_used) { 922 fail_continue("The shared archive file has been truncated."); 923 return false; 924 } 925 926 SharedBaseAddress = _header->_shared_base_address; 927 } 928 929 return true; 930 } 931 932 933 // Read the FileMapInfo information from the file. 934 bool FileMapInfo::open_for_read(const char* path) { 935 if (_file_open) { 936 return true; 937 } 938 if (path == NULL) { 939 _full_path = Arguments::GetSharedArchivePath(); 940 } else { 941 _full_path = path; 942 } 943 int fd = os::open(_full_path, O_RDONLY | O_BINARY, 0); 944 if (fd < 0) { 945 if (errno == ENOENT) { 946 // Not locating the shared archive is ok. 947 fail_continue("Specified shared archive not found (%s).", _full_path); 948 } else { 949 fail_continue("Failed to open shared archive file (%s).", 950 os::strerror(errno)); 951 } 952 return false; 953 } 954 955 _fd = fd; 956 _file_open = true; 957 return true; 958 } 959 960 // Write the FileMapInfo information to the file. 961 962 void FileMapInfo::open_for_write(const char* path) { 963 if (path == NULL) { 964 _full_path = Arguments::GetSharedArchivePath(); 965 } else { 966 _full_path = path; 967 } 968 LogMessage(cds) msg; 969 if (msg.is_info()) { 970 msg.info("Dumping shared data to file: "); 971 msg.info(" %s", _full_path); 972 } 973 974 #ifdef _WINDOWS // On Windows, need WRITE permission to remove the file. 975 chmod(_full_path, _S_IREAD | _S_IWRITE); 976 #endif 977 978 // Use remove() to delete the existing file because, on Unix, this will 979 // allow processes that have it open continued access to the file. 980 remove(_full_path); 981 int fd = os::open(_full_path, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0444); 982 if (fd < 0) { 983 fail_stop("Unable to create shared archive file %s: (%s).", _full_path, 984 os::strerror(errno)); 985 } 986 _fd = fd; 987 _file_offset = 0; 988 _file_open = true; 989 } 990 991 992 // Write the header to the file, seek to the next allocation boundary. 993 994 void FileMapInfo::write_header() { 995 int info_size = ClassLoader::get_shared_paths_misc_info_size(); 996 997 _header->_paths_misc_info_size = info_size; 998 999 char* base_archive_name = NULL; 1000 if (_header->_magic == CDS_DYNAMIC_ARCHIVE_MAGIC) { 1001 base_archive_name = (char*)Arguments::GetSharedArchivePath(); 1002 _header->_base_archive_name_size = (int)strlen(base_archive_name) + 1; 1003 _header->_base_archive_is_default = FLAG_IS_DEFAULT(SharedArchiveFile); 1004 } 1005 1006 assert(is_file_position_aligned(), "must be"); 1007 write_bytes(_header, _header->_header_size); 1008 write_bytes(ClassLoader::get_shared_paths_misc_info(), (size_t)info_size); 1009 if (base_archive_name != NULL) { 1010 write_bytes(base_archive_name, (size_t)_header->_base_archive_name_size); 1011 } 1012 align_file_position(); 1013 } 1014 1015 // Dump region to file. 1016 // This is called twice for each region during archiving, once before 1017 // the archive file is open (_file_open is false) and once after. 1018 void FileMapInfo::write_region(int region, char* base, size_t size, 1019 bool read_only, bool allow_exec) { 1020 assert(DumpSharedSpaces || DynamicDumpSharedSpaces, "Dump time only"); 1021 1022 CDSFileMapRegion* si = space_at(region); 1023 char* target_base = base; 1024 if (DynamicDumpSharedSpaces) { 1025 target_base = DynamicArchive::buffer_to_target(base); 1026 } 1027 1028 if (_file_open) { 1029 guarantee(si->_file_offset == _file_offset, "file offset mismatch."); 1030 log_info(cds)("Shared file region %d: " SIZE_FORMAT_HEX_W(08) 1031 " bytes, addr " INTPTR_FORMAT " file offset " SIZE_FORMAT_HEX_W(08), 1032 region, size, p2i(target_base), _file_offset); 1033 } else { 1034 si->_file_offset = _file_offset; 1035 } 1036 1037 if (HeapShared::is_heap_region(region)) { 1038 assert((target_base - (char*)CompressedKlassPointers::base()) % HeapWordSize == 0, "Sanity"); 1039 if (target_base != NULL) { 1040 si->_addr._offset = (intx)CompressedOops::encode_not_null((oop)target_base); 1041 } else { 1042 si->_addr._offset = 0; 1043 } 1044 } else { 1045 si->_addr._base = target_base; 1046 } 1047 si->_used = size; 1048 si->_read_only = read_only; 1049 si->_allow_exec = allow_exec; 1050 1051 // Use the current 'base' when computing the CRC value and writing out data 1052 si->_crc = ClassLoader::crc32(0, base, (jint)size); 1053 if (base != NULL) { 1054 write_bytes_aligned(base, size); 1055 } 1056 } 1057 1058 // Write out the given archive heap memory regions. GC code combines multiple 1059 // consecutive archive GC regions into one MemRegion whenever possible and 1060 // produces the 'heap_mem' array. 1061 // 1062 // If the archive heap memory size is smaller than a single dump time GC region 1063 // size, there is only one MemRegion in the array. 1064 // 1065 // If the archive heap memory size is bigger than one dump time GC region size, 1066 // the 'heap_mem' array may contain more than one consolidated MemRegions. When 1067 // the first/bottom archive GC region is a partial GC region (with the empty 1068 // portion at the higher address within the region), one MemRegion is used for 1069 // the bottom partial archive GC region. The rest of the consecutive archive 1070 // GC regions are combined into another MemRegion. 1071 // 1072 // Here's the mapping from (archive heap GC regions) -> (GrowableArray<MemRegion> *regions). 1073 // + We have 1 or more archive heap regions: ah0, ah1, ah2 ..... ahn 1074 // + We have 1 or 2 consolidated heap memory regions: r0 and r1 1075 // 1076 // If there's a single archive GC region (ah0), then r0 == ah0, and r1 is empty. 1077 // Otherwise: 1078 // 1079 // "X" represented space that's occupied by heap objects. 1080 // "_" represented unused spaced in the heap region. 1081 // 1082 // 1083 // |ah0 | ah1 | ah2| ...... | ahn| 1084 // |XXXXXX|__ |XXXXX|XXXX|XXXXXXXX|XXXX| 1085 // |<-r0->| |<- r1 ----------------->| 1086 // ^^^ 1087 // | 1088 // +-- gap 1089 size_t FileMapInfo::write_archive_heap_regions(GrowableArray<MemRegion> *heap_mem, 1090 GrowableArray<ArchiveHeapOopmapInfo> *oopmaps, 1091 int first_region_id, int max_num_regions, 1092 bool print_log) { 1093 assert(max_num_regions <= 2, "Only support maximum 2 memory regions"); 1094 1095 int arr_len = heap_mem == NULL ? 0 : heap_mem->length(); 1096 if(arr_len > max_num_regions) { 1097 fail_stop("Unable to write archive heap memory regions: " 1098 "number of memory regions exceeds maximum due to fragmentation. " 1099 "Please increase java heap size " 1100 "(current MaxHeapSize is " SIZE_FORMAT ", InitialHeapSize is " SIZE_FORMAT ").", 1101 MaxHeapSize, InitialHeapSize); 1102 } 1103 1104 size_t total_size = 0; 1105 for (int i = first_region_id, arr_idx = 0; 1106 i < first_region_id + max_num_regions; 1107 i++, arr_idx++) { 1108 char* start = NULL; 1109 size_t size = 0; 1110 if (arr_idx < arr_len) { 1111 start = (char*)heap_mem->at(arr_idx).start(); 1112 size = heap_mem->at(arr_idx).byte_size(); 1113 total_size += size; 1114 } 1115 1116 if (print_log) { 1117 log_info(cds)("Archive heap region %d " INTPTR_FORMAT " - " INTPTR_FORMAT " = " SIZE_FORMAT_W(8) " bytes", 1118 i, p2i(start), p2i(start + size), size); 1119 } 1120 write_region(i, start, size, false, false); 1121 if (size > 0) { 1122 space_at(i)->_oopmap = oopmaps->at(arr_idx)._oopmap; 1123 space_at(i)->_oopmap_size_in_bits = oopmaps->at(arr_idx)._oopmap_size_in_bits; 1124 } 1125 } 1126 return total_size; 1127 } 1128 1129 // Dump bytes to file -- at the current file position. 1130 1131 void FileMapInfo::write_bytes(const void* buffer, size_t nbytes) { 1132 if (_file_open) { 1133 size_t n = os::write(_fd, buffer, (unsigned int)nbytes); 1134 if (n != nbytes) { 1135 // If the shared archive is corrupted, close it and remove it. 1136 close(); 1137 remove(_full_path); 1138 fail_stop("Unable to write to shared archive file."); 1139 } 1140 } 1141 _file_offset += nbytes; 1142 } 1143 1144 bool FileMapInfo::is_file_position_aligned() const { 1145 return _file_offset == align_up(_file_offset, 1146 os::vm_allocation_granularity()); 1147 } 1148 1149 // Align file position to an allocation unit boundary. 1150 1151 void FileMapInfo::align_file_position() { 1152 size_t new_file_offset = align_up(_file_offset, 1153 os::vm_allocation_granularity()); 1154 if (new_file_offset != _file_offset) { 1155 _file_offset = new_file_offset; 1156 if (_file_open) { 1157 // Seek one byte back from the target and write a byte to insure 1158 // that the written file is the correct length. 1159 _file_offset -= 1; 1160 if (lseek(_fd, (long)_file_offset, SEEK_SET) < 0) { 1161 fail_stop("Unable to seek."); 1162 } 1163 char zero = 0; 1164 write_bytes(&zero, 1); 1165 } 1166 } 1167 } 1168 1169 1170 // Dump bytes to file -- at the current file position. 1171 1172 void FileMapInfo::write_bytes_aligned(const void* buffer, size_t nbytes) { 1173 align_file_position(); 1174 write_bytes(buffer, nbytes); 1175 align_file_position(); 1176 } 1177 1178 1179 // Close the shared archive file. This does NOT unmap mapped regions. 1180 1181 void FileMapInfo::close() { 1182 if (_file_open) { 1183 if (::close(_fd) < 0) { 1184 fail_stop("Unable to close the shared archive file."); 1185 } 1186 _file_open = false; 1187 _fd = -1; 1188 } 1189 } 1190 1191 1192 // JVM/TI RedefineClasses() support: 1193 // Remap the shared readonly space to shared readwrite, private. 1194 bool FileMapInfo::remap_shared_readonly_as_readwrite() { 1195 int idx = MetaspaceShared::ro; 1196 CDSFileMapRegion* si = space_at(idx); 1197 if (!si->_read_only) { 1198 // the space is already readwrite so we are done 1199 return true; 1200 } 1201 size_t used = si->_used; 1202 size_t size = align_up(used, os::vm_allocation_granularity()); 1203 if (!open_for_read()) { 1204 return false; 1205 } 1206 char *addr = region_addr(idx); 1207 char *base = os::remap_memory(_fd, _full_path, si->_file_offset, 1208 addr, size, false /* !read_only */, 1209 si->_allow_exec); 1210 close(); 1211 // These have to be errors because the shared region is now unmapped. 1212 if (base == NULL) { 1213 log_error(cds)("Unable to remap shared readonly space (errno=%d).", errno); 1214 vm_exit(1); 1215 } 1216 if (base != addr) { 1217 log_error(cds)("Unable to remap shared readonly space (errno=%d).", errno); 1218 vm_exit(1); 1219 } 1220 si->_read_only = false; 1221 return true; 1222 } 1223 1224 // Map the whole region at once, assumed to be allocated contiguously. 1225 ReservedSpace FileMapInfo::reserve_shared_memory() { 1226 char* requested_addr = region_addr(0); 1227 size_t size = FileMapInfo::core_spaces_size(); 1228 1229 // Reserve the space first, then map otherwise map will go right over some 1230 // other reserved memory (like the code cache). 1231 ReservedSpace rs(size, os::vm_allocation_granularity(), false, requested_addr); 1232 if (!rs.is_reserved()) { 1233 fail_continue("Unable to reserve shared space at required address " 1234 INTPTR_FORMAT, p2i(requested_addr)); 1235 return rs; 1236 } 1237 // the reserved virtual memory is for mapping class data sharing archive 1238 MemTracker::record_virtual_memory_type((address)rs.base(), mtClassShared); 1239 1240 return rs; 1241 } 1242 1243 // Memory map a region in the address space. 1244 static const char* shared_region_name[] = { "MiscData", "ReadWrite", "ReadOnly", "MiscCode", 1245 "String1", "String2", "OpenArchive1", "OpenArchive2" }; 1246 1247 char* FileMapInfo::map_regions(int regions[], char* saved_base[], size_t len) { 1248 char* prev_top = NULL; 1249 char* curr_base; 1250 char* curr_top; 1251 int i = 0; 1252 for (i = 0; i < (int)len; i++) { 1253 curr_base = map_region(regions[i], &curr_top); 1254 if (curr_base == NULL) { 1255 return NULL; 1256 } 1257 if (i > 0) { 1258 // We require that mc->rw->ro->md to be laid out consecutively, with no 1259 // gaps between them. That way, we can ensure that the OS won't be able to 1260 // allocate any new memory spaces inside _shared_metaspace_{base,top}, which 1261 // would mess up the simple comparision in MetaspaceShared::is_in_shared_metaspace(). 1262 assert(curr_base == prev_top, "must be"); 1263 } 1264 log_info(cds)("Mapped region #%d at base %p top %p", regions[i], curr_base, curr_top); 1265 saved_base[i] = curr_base; 1266 prev_top = curr_top; 1267 } 1268 return curr_top; 1269 } 1270 1271 char* FileMapInfo::map_region(int i, char** top_ret) { 1272 assert(!HeapShared::is_heap_region(i), "sanity"); 1273 CDSFileMapRegion* si = space_at(i); 1274 size_t used = si->_used; 1275 size_t alignment = os::vm_allocation_granularity(); 1276 size_t size = align_up(used, alignment); 1277 char *requested_addr = region_addr(i); 1278 1279 #ifdef _WINDOWS 1280 // Windows cannot remap read-only shared memory to read-write when required for 1281 // RedefineClasses, which is also used by JFR. Always map windows regions as RW. 1282 si->_read_only = false; 1283 #else 1284 // If a tool agent is in use (debugging enabled), or JFR, we must map the address space RW 1285 if (JvmtiExport::can_modify_any_class() || JvmtiExport::can_walk_any_space() || 1286 Arguments::has_jfr_option()) { 1287 si->_read_only = false; 1288 } 1289 #endif // _WINDOWS 1290 1291 // map the contents of the CDS archive in this memory 1292 char *base = os::map_memory(_fd, _full_path, si->_file_offset, 1293 requested_addr, size, si->_read_only, 1294 si->_allow_exec); 1295 if (base == NULL || base != requested_addr) { 1296 fail_continue("Unable to map %s shared space at required address.", shared_region_name[i]); 1297 _memory_mapping_failed = true; 1298 return NULL; 1299 } 1300 #ifdef _WINDOWS 1301 // This call is Windows-only because the memory_type gets recorded for the other platforms 1302 // in method FileMapInfo::reserve_shared_memory(), which is not called on Windows. 1303 MemTracker::record_virtual_memory_type((address)base, mtClassShared); 1304 #endif 1305 1306 if (VerifySharedSpaces && !verify_region_checksum(i)) { 1307 return NULL; 1308 } 1309 1310 *top_ret = base + size; 1311 return base; 1312 } 1313 1314 size_t FileMapInfo::read_bytes(void* buffer, size_t count) { 1315 assert(_file_open, "Archive file is not open"); 1316 size_t n = os::read(_fd, buffer, (unsigned int)count); 1317 if (n != count) { 1318 // Close the file if there's a problem reading it. 1319 close(); 1320 return 0; 1321 } 1322 _file_offset += count; 1323 return count; 1324 } 1325 1326 address FileMapInfo::decode_start_address(CDSFileMapRegion* spc, bool with_current_oop_encoding_mode) { 1327 if (with_current_oop_encoding_mode) { 1328 return (address)CompressedOops::decode_not_null(offset_of_space(spc)); 1329 } else { 1330 return (address)HeapShared::decode_from_archive(offset_of_space(spc)); 1331 } 1332 } 1333 1334 static MemRegion *closed_archive_heap_ranges = NULL; 1335 static MemRegion *open_archive_heap_ranges = NULL; 1336 static int num_closed_archive_heap_ranges = 0; 1337 static int num_open_archive_heap_ranges = 0; 1338 1339 #if INCLUDE_CDS_JAVA_HEAP 1340 bool FileMapInfo::has_heap_regions() { 1341 return (_header->_space[MetaspaceShared::first_closed_archive_heap_region]._used > 0); 1342 } 1343 1344 // Returns the address range of the archived heap regions computed using the 1345 // current oop encoding mode. This range may be different than the one seen at 1346 // dump time due to encoding mode differences. The result is used in determining 1347 // if/how these regions should be relocated at run time. 1348 MemRegion FileMapInfo::get_heap_regions_range_with_current_oop_encoding_mode() { 1349 address start = (address) max_uintx; 1350 address end = NULL; 1351 1352 for (int i = MetaspaceShared::first_closed_archive_heap_region; 1353 i <= MetaspaceShared::last_valid_region; 1354 i++) { 1355 CDSFileMapRegion* si = space_at(i); 1356 size_t size = si->_used; 1357 if (size > 0) { 1358 address s = start_address_as_decoded_with_current_oop_encoding_mode(si); 1359 address e = s + size; 1360 if (start > s) { 1361 start = s; 1362 } 1363 if (end < e) { 1364 end = e; 1365 } 1366 } 1367 } 1368 assert(end != NULL, "must have at least one used heap region"); 1369 return MemRegion((HeapWord*)start, (HeapWord*)end); 1370 } 1371 1372 // 1373 // Map the closed and open archive heap objects to the runtime java heap. 1374 // 1375 // The shared objects are mapped at (or close to ) the java heap top in 1376 // closed archive regions. The mapped objects contain no out-going 1377 // references to any other java heap regions. GC does not write into the 1378 // mapped closed archive heap region. 1379 // 1380 // The open archive heap objects are mapped below the shared objects in 1381 // the runtime java heap. The mapped open archive heap data only contains 1382 // references to the shared objects and open archive objects initially. 1383 // During runtime execution, out-going references to any other java heap 1384 // regions may be added. GC may mark and update references in the mapped 1385 // open archive objects. 1386 void FileMapInfo::map_heap_regions_impl() { 1387 if (!HeapShared::is_heap_object_archiving_allowed()) { 1388 log_info(cds)("CDS heap data is being ignored. UseG1GC, " 1389 "UseCompressedOops and UseCompressedClassPointers are required."); 1390 return; 1391 } 1392 1393 if (JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()) { 1394 ShouldNotReachHere(); // CDS should have been disabled. 1395 // The archived objects are mapped at JVM start-up, but we don't know if 1396 // j.l.String or j.l.Class might be replaced by the ClassFileLoadHook, 1397 // which would make the archived String or mirror objects invalid. Let's be safe and not 1398 // use the archived objects. These 2 classes are loaded during the JVMTI "early" stage. 1399 // 1400 // If JvmtiExport::has_early_class_hook_env() is false, the classes of some objects 1401 // in the archived subgraphs may be replaced by the ClassFileLoadHook. But that's OK 1402 // because we won't install an archived object subgraph if the klass of any of the 1403 // referenced objects are replaced. See HeapShared::initialize_from_archived_subgraph(). 1404 } 1405 1406 MemRegion heap_reserved = Universe::heap()->reserved_region(); 1407 1408 log_info(cds)("CDS archive was created with max heap size = " SIZE_FORMAT "M, and the following configuration:", 1409 max_heap_size()/M); 1410 log_info(cds)(" narrow_klass_base = " PTR_FORMAT ", narrow_klass_shift = %d", 1411 p2i(narrow_klass_base()), narrow_klass_shift()); 1412 log_info(cds)(" narrow_oop_mode = %d, narrow_oop_base = " PTR_FORMAT ", narrow_oop_shift = %d", 1413 narrow_oop_mode(), p2i(narrow_oop_base()), narrow_oop_shift()); 1414 1415 log_info(cds)("The current max heap size = " SIZE_FORMAT "M, HeapRegion::GrainBytes = " SIZE_FORMAT, 1416 heap_reserved.byte_size()/M, HeapRegion::GrainBytes); 1417 log_info(cds)(" narrow_klass_base = " PTR_FORMAT ", narrow_klass_shift = %d", 1418 p2i(CompressedKlassPointers::base()), CompressedKlassPointers::shift()); 1419 log_info(cds)(" narrow_oop_mode = %d, narrow_oop_base = " PTR_FORMAT ", narrow_oop_shift = %d", 1420 CompressedOops::mode(), p2i(CompressedOops::base()), CompressedOops::shift()); 1421 1422 if (narrow_klass_base() != CompressedKlassPointers::base() || 1423 narrow_klass_shift() != CompressedKlassPointers::shift()) { 1424 log_info(cds)("CDS heap data cannot be used because the archive was created with an incompatible narrow klass encoding mode."); 1425 return; 1426 } 1427 1428 if (narrow_oop_mode() != CompressedOops::mode() || 1429 narrow_oop_base() != CompressedOops::base() || 1430 narrow_oop_shift() != CompressedOops::shift()) { 1431 log_info(cds)("CDS heap data need to be relocated because the archive was created with an incompatible oop encoding mode."); 1432 _heap_pointers_need_patching = true; 1433 } else { 1434 MemRegion range = get_heap_regions_range_with_current_oop_encoding_mode(); 1435 if (!heap_reserved.contains(range)) { 1436 log_info(cds)("CDS heap data need to be relocated because"); 1437 log_info(cds)("the desired range " PTR_FORMAT " - " PTR_FORMAT, p2i(range.start()), p2i(range.end())); 1438 log_info(cds)("is outside of the heap " PTR_FORMAT " - " PTR_FORMAT, p2i(heap_reserved.start()), p2i(heap_reserved.end())); 1439 _heap_pointers_need_patching = true; 1440 } 1441 } 1442 1443 ptrdiff_t delta = 0; 1444 if (_heap_pointers_need_patching) { 1445 // dumptime heap end ------------v 1446 // [ |archived heap regions| ] runtime heap end ------v 1447 // [ |archived heap regions| ] 1448 // |<-----delta-------------------->| 1449 // 1450 // At dump time, the archived heap regions were near the top of the heap. 1451 // At run time, they may not be inside the heap, so we move them so 1452 // that they are now near the top of the runtime time. This can be done by 1453 // the simple math of adding the delta as shown above. 1454 address dumptime_heap_end = (address)_header->_heap_reserved.end(); 1455 address runtime_heap_end = (address)heap_reserved.end(); 1456 delta = runtime_heap_end - dumptime_heap_end; 1457 } 1458 1459 log_info(cds)("CDS heap data relocation delta = " INTX_FORMAT " bytes", delta); 1460 HeapShared::init_narrow_oop_decoding(narrow_oop_base() + delta, narrow_oop_shift()); 1461 1462 CDSFileMapRegion* si = space_at(MetaspaceShared::first_closed_archive_heap_region); 1463 address relocated_closed_heap_region_bottom = start_address_as_decoded_from_archive(si); 1464 if (!is_aligned(relocated_closed_heap_region_bottom, HeapRegion::GrainBytes)) { 1465 // Align the bottom of the closed archive heap regions at G1 region boundary. 1466 // This will avoid the situation where the highest open region and the lowest 1467 // closed region sharing the same G1 region. Otherwise we will fail to map the 1468 // open regions. 1469 size_t align = size_t(relocated_closed_heap_region_bottom) % HeapRegion::GrainBytes; 1470 delta -= align; 1471 log_info(cds)("CDS heap data need to be relocated lower by a further " SIZE_FORMAT 1472 " bytes to " INTX_FORMAT " to be aligned with HeapRegion::GrainBytes", 1473 align, delta); 1474 HeapShared::init_narrow_oop_decoding(narrow_oop_base() + delta, narrow_oop_shift()); 1475 _heap_pointers_need_patching = true; 1476 relocated_closed_heap_region_bottom = start_address_as_decoded_from_archive(si); 1477 } 1478 assert(is_aligned(relocated_closed_heap_region_bottom, HeapRegion::GrainBytes), 1479 "must be"); 1480 1481 // Map the closed_archive_heap regions, GC does not write into the regions. 1482 if (map_heap_data(&closed_archive_heap_ranges, 1483 MetaspaceShared::first_closed_archive_heap_region, 1484 MetaspaceShared::max_closed_archive_heap_region, 1485 &num_closed_archive_heap_ranges)) { 1486 HeapShared::set_closed_archive_heap_region_mapped(); 1487 1488 // Now, map open_archive heap regions, GC can write into the regions. 1489 if (map_heap_data(&open_archive_heap_ranges, 1490 MetaspaceShared::first_open_archive_heap_region, 1491 MetaspaceShared::max_open_archive_heap_region, 1492 &num_open_archive_heap_ranges, 1493 true /* open */)) { 1494 HeapShared::set_open_archive_heap_region_mapped(); 1495 } 1496 } 1497 } 1498 1499 void FileMapInfo::map_heap_regions() { 1500 if (has_heap_regions()) { 1501 map_heap_regions_impl(); 1502 } 1503 1504 if (!HeapShared::closed_archive_heap_region_mapped()) { 1505 assert(closed_archive_heap_ranges == NULL && 1506 num_closed_archive_heap_ranges == 0, "sanity"); 1507 } 1508 1509 if (!HeapShared::open_archive_heap_region_mapped()) { 1510 assert(open_archive_heap_ranges == NULL && num_open_archive_heap_ranges == 0, "sanity"); 1511 } 1512 } 1513 1514 bool FileMapInfo::map_heap_data(MemRegion **heap_mem, int first, 1515 int max, int* num, bool is_open_archive) { 1516 MemRegion * regions = new MemRegion[max]; 1517 CDSFileMapRegion* si; 1518 int region_num = 0; 1519 1520 for (int i = first; 1521 i < first + max; i++) { 1522 si = space_at(i); 1523 size_t size = si->_used; 1524 if (size > 0) { 1525 HeapWord* start = (HeapWord*)start_address_as_decoded_from_archive(si); 1526 regions[region_num] = MemRegion(start, size / HeapWordSize); 1527 region_num ++; 1528 log_info(cds)("Trying to map heap data: region[%d] at " INTPTR_FORMAT ", size = " SIZE_FORMAT_W(8) " bytes", 1529 i, p2i(start), size); 1530 } 1531 } 1532 1533 if (region_num == 0) { 1534 return false; // no archived java heap data 1535 } 1536 1537 // Check that ranges are within the java heap 1538 if (!G1CollectedHeap::heap()->check_archive_addresses(regions, region_num)) { 1539 log_info(cds)("UseSharedSpaces: Unable to allocate region, range is not within java heap."); 1540 return false; 1541 } 1542 1543 // allocate from java heap 1544 if (!G1CollectedHeap::heap()->alloc_archive_regions( 1545 regions, region_num, is_open_archive)) { 1546 log_info(cds)("UseSharedSpaces: Unable to allocate region, java heap range is already in use."); 1547 return false; 1548 } 1549 1550 // Map the archived heap data. No need to call MemTracker::record_virtual_memory_type() 1551 // for mapped regions as they are part of the reserved java heap, which is 1552 // already recorded. 1553 for (int i = 0; i < region_num; i++) { 1554 si = space_at(first + i); 1555 char* addr = (char*)regions[i].start(); 1556 char* base = os::map_memory(_fd, _full_path, si->_file_offset, 1557 addr, regions[i].byte_size(), si->_read_only, 1558 si->_allow_exec); 1559 if (base == NULL || base != addr) { 1560 // dealloc the regions from java heap 1561 dealloc_archive_heap_regions(regions, region_num, is_open_archive); 1562 log_info(cds)("UseSharedSpaces: Unable to map at required address in java heap. " 1563 INTPTR_FORMAT ", size = " SIZE_FORMAT " bytes", 1564 p2i(addr), regions[i].byte_size()); 1565 return false; 1566 } 1567 1568 if (VerifySharedSpaces && !region_crc_check(addr, regions[i].byte_size(), si->_crc)) { 1569 // dealloc the regions from java heap 1570 dealloc_archive_heap_regions(regions, region_num, is_open_archive); 1571 log_info(cds)("UseSharedSpaces: mapped heap regions are corrupt"); 1572 return false; 1573 } 1574 } 1575 1576 // the shared heap data is mapped successfully 1577 *heap_mem = regions; 1578 *num = region_num; 1579 return true; 1580 } 1581 1582 void FileMapInfo::patch_archived_heap_embedded_pointers() { 1583 if (!_heap_pointers_need_patching) { 1584 return; 1585 } 1586 1587 patch_archived_heap_embedded_pointers(closed_archive_heap_ranges, 1588 num_closed_archive_heap_ranges, 1589 MetaspaceShared::first_closed_archive_heap_region); 1590 1591 patch_archived_heap_embedded_pointers(open_archive_heap_ranges, 1592 num_open_archive_heap_ranges, 1593 MetaspaceShared::first_open_archive_heap_region); 1594 } 1595 1596 void FileMapInfo::patch_archived_heap_embedded_pointers(MemRegion* ranges, int num_ranges, 1597 int first_region_idx) { 1598 for (int i=0; i<num_ranges; i++) { 1599 CDSFileMapRegion* si = space_at(i + first_region_idx); 1600 HeapShared::patch_archived_heap_embedded_pointers(ranges[i], (address)si->_oopmap, 1601 si->_oopmap_size_in_bits); 1602 } 1603 } 1604 1605 // This internally allocates objects using SystemDictionary::Object_klass(), so it 1606 // must be called after the well-known classes are resolved. 1607 void FileMapInfo::fixup_mapped_heap_regions() { 1608 // If any closed regions were found, call the fill routine to make them parseable. 1609 // Note that closed_archive_heap_ranges may be non-NULL even if no ranges were found. 1610 if (num_closed_archive_heap_ranges != 0) { 1611 assert(closed_archive_heap_ranges != NULL, 1612 "Null closed_archive_heap_ranges array with non-zero count"); 1613 G1CollectedHeap::heap()->fill_archive_regions(closed_archive_heap_ranges, 1614 num_closed_archive_heap_ranges); 1615 } 1616 1617 // do the same for mapped open archive heap regions 1618 if (num_open_archive_heap_ranges != 0) { 1619 assert(open_archive_heap_ranges != NULL, "NULL open_archive_heap_ranges array with non-zero count"); 1620 G1CollectedHeap::heap()->fill_archive_regions(open_archive_heap_ranges, 1621 num_open_archive_heap_ranges); 1622 } 1623 } 1624 1625 // dealloc the archive regions from java heap 1626 void FileMapInfo::dealloc_archive_heap_regions(MemRegion* regions, int num, bool is_open) { 1627 if (num > 0) { 1628 assert(regions != NULL, "Null archive ranges array with non-zero count"); 1629 G1CollectedHeap::heap()->dealloc_archive_regions(regions, num, is_open); 1630 } 1631 } 1632 #endif // INCLUDE_CDS_JAVA_HEAP 1633 1634 bool FileMapInfo::region_crc_check(char* buf, size_t size, int expected_crc) { 1635 int crc = ClassLoader::crc32(0, buf, (jint)size); 1636 if (crc != expected_crc) { 1637 fail_continue("Checksum verification failed."); 1638 return false; 1639 } 1640 return true; 1641 } 1642 1643 bool FileMapInfo::verify_region_checksum(int i) { 1644 assert(VerifySharedSpaces, "sanity"); 1645 1646 size_t sz = space_at(i)->_used; 1647 1648 if (sz == 0) { 1649 return true; // no data 1650 } 1651 1652 return region_crc_check(region_addr(i), sz, space_at(i)->_crc); 1653 } 1654 1655 void FileMapInfo::unmap_regions(int regions[], char* saved_base[], size_t len) { 1656 for (int i = 0; i < (int)len; i++) { 1657 if (saved_base[i] != NULL) { 1658 unmap_region(regions[i]); 1659 } 1660 } 1661 } 1662 1663 // Unmap a memory region in the address space. 1664 1665 void FileMapInfo::unmap_region(int i) { 1666 assert(!HeapShared::is_heap_region(i), "sanity"); 1667 CDSFileMapRegion* si = space_at(i); 1668 size_t used = si->_used; 1669 size_t size = align_up(used, os::vm_allocation_granularity()); 1670 1671 if (used == 0) { 1672 return; 1673 } 1674 1675 char* addr = region_addr(i); 1676 if (!os::unmap_memory(addr, size)) { 1677 fail_stop("Unable to unmap shared space."); 1678 } 1679 } 1680 1681 void FileMapInfo::assert_mark(bool check) { 1682 if (!check) { 1683 fail_stop("Mark mismatch while restoring from shared file."); 1684 } 1685 } 1686 1687 void FileMapInfo::metaspace_pointers_do(MetaspaceClosure* it) { 1688 _shared_path_table.metaspace_pointers_do(it); 1689 } 1690 1691 FileMapInfo* FileMapInfo::_current_info = NULL; 1692 FileMapInfo* FileMapInfo::_dynamic_archive_info = NULL; 1693 bool FileMapInfo::_heap_pointers_need_patching = false; 1694 SharedPathTable FileMapInfo::_shared_path_table; 1695 bool FileMapInfo::_validating_shared_path_table = false; 1696 bool FileMapInfo::_memory_mapping_failed = false; 1697 1698 // Open the shared archive file, read and validate the header 1699 // information (version, boot classpath, etc.). If initialization 1700 // fails, shared spaces are disabled and the file is closed. [See 1701 // fail_continue.] 1702 // 1703 // Validation of the archive is done in two steps: 1704 // 1705 // [1] validate_header() - done here. This checks the header, including _paths_misc_info. 1706 // [2] validate_shared_path_table - this is done later, because the table is in the RW 1707 // region of the archive, which is not mapped yet. 1708 bool FileMapInfo::initialize(bool is_static) { 1709 assert(UseSharedSpaces, "UseSharedSpaces expected."); 1710 1711 if (JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()) { 1712 // CDS assumes that no classes resolved in SystemDictionary::resolve_well_known_classes 1713 // are replaced at runtime by JVMTI ClassFileLoadHook. All of those classes are resolved 1714 // during the JVMTI "early" stage, so we can still use CDS if 1715 // JvmtiExport::has_early_class_hook_env() is false. 1716 FileMapInfo::fail_continue("CDS is disabled because early JVMTI ClassFileLoadHook is in use."); 1717 return false; 1718 } 1719 1720 if (!open_for_read()) { 1721 return false; 1722 } 1723 1724 init_from_file(_fd, is_static); 1725 if (!validate_header(is_static)) { 1726 return false; 1727 } 1728 return true; 1729 } 1730 1731 char* FileMapInfo::region_addr(int idx) { 1732 CDSFileMapRegion* si = space_at(idx); 1733 if (HeapShared::is_heap_region(idx)) { 1734 assert(DumpSharedSpaces, "The following doesn't work at runtime"); 1735 return si->_used > 0 ? 1736 (char*)start_address_as_decoded_with_current_oop_encoding_mode(si) : NULL; 1737 } else { 1738 return si->_addr._base; 1739 } 1740 } 1741 1742 int FileMapHeader::compute_crc() { 1743 char* start = (char*)this; 1744 // start computing from the field after _crc 1745 char* buf = (char*)&_crc + sizeof(_crc); 1746 size_t sz = _header_size - (buf - start); 1747 int crc = ClassLoader::crc32(0, buf, (jint)sz); 1748 return crc; 1749 } 1750 1751 // This function should only be called during run time with UseSharedSpaces enabled. 1752 bool FileMapHeader::validate() { 1753 if (VerifySharedSpaces && compute_crc() != _crc) { 1754 FileMapInfo::fail_continue("Header checksum verification failed."); 1755 return false; 1756 } 1757 1758 if (!Arguments::has_jimage()) { 1759 FileMapInfo::fail_continue("The shared archive file cannot be used with an exploded module build."); 1760 return false; 1761 } 1762 1763 if (_version != CURRENT_CDS_ARCHIVE_VERSION) { 1764 FileMapInfo::fail_continue("The shared archive file is the wrong version."); 1765 return false; 1766 } 1767 if (_magic != CDS_ARCHIVE_MAGIC && _magic != CDS_DYNAMIC_ARCHIVE_MAGIC) { 1768 FileMapInfo::fail_continue("The shared archive file has a bad magic number."); 1769 return false; 1770 } 1771 char header_version[JVM_IDENT_MAX]; 1772 get_header_version(header_version); 1773 if (strncmp(_jvm_ident, header_version, JVM_IDENT_MAX-1) != 0) { 1774 log_info(class, path)("expected: %s", header_version); 1775 log_info(class, path)("actual: %s", _jvm_ident); 1776 FileMapInfo::fail_continue("The shared archive file was created by a different" 1777 " version or build of HotSpot"); 1778 return false; 1779 } 1780 if (_obj_alignment != ObjectAlignmentInBytes) { 1781 FileMapInfo::fail_continue("The shared archive file's ObjectAlignmentInBytes of %d" 1782 " does not equal the current ObjectAlignmentInBytes of " INTX_FORMAT ".", 1783 _obj_alignment, ObjectAlignmentInBytes); 1784 return false; 1785 } 1786 if (_compact_strings != CompactStrings) { 1787 FileMapInfo::fail_continue("The shared archive file's CompactStrings setting (%s)" 1788 " does not equal the current CompactStrings setting (%s).", 1789 _compact_strings ? "enabled" : "disabled", 1790 CompactStrings ? "enabled" : "disabled"); 1791 return false; 1792 } 1793 1794 // This must be done after header validation because it might change the 1795 // header data 1796 const char* prop = Arguments::get_property("java.system.class.loader"); 1797 if (prop != NULL) { 1798 warning("Archived non-system classes are disabled because the " 1799 "java.system.class.loader property is specified (value = \"%s\"). " 1800 "To use archived non-system classes, this property must not be set", prop); 1801 _has_platform_or_app_classes = false; 1802 } 1803 1804 // For backwards compatibility, we don't check the verification setting 1805 // if the archive only contains system classes. 1806 if (_has_platform_or_app_classes && 1807 ((!_verify_local && BytecodeVerificationLocal) || 1808 (!_verify_remote && BytecodeVerificationRemote))) { 1809 FileMapInfo::fail_continue("The shared archive file was created with less restrictive " 1810 "verification setting than the current setting."); 1811 return false; 1812 } 1813 1814 // Java agents are allowed during run time. Therefore, the following condition is not 1815 // checked: (!_allow_archiving_with_java_agent && AllowArchivingWithJavaAgent) 1816 // Note: _allow_archiving_with_java_agent is set in the shared archive during dump time 1817 // while AllowArchivingWithJavaAgent is set during the current run. 1818 if (_allow_archiving_with_java_agent && !AllowArchivingWithJavaAgent) { 1819 FileMapInfo::fail_continue("The setting of the AllowArchivingWithJavaAgent is different " 1820 "from the setting in the shared archive."); 1821 return false; 1822 } 1823 1824 if (_allow_archiving_with_java_agent) { 1825 warning("This archive was created with AllowArchivingWithJavaAgent. It should be used " 1826 "for testing purposes only and should not be used in a production environment"); 1827 } 1828 1829 return true; 1830 } 1831 1832 bool FileMapInfo::validate_header(bool is_static) { 1833 bool status = _header->validate(); 1834 1835 if (status) { 1836 if (!ClassLoader::check_shared_paths_misc_info(_paths_misc_info, _header->_paths_misc_info_size, is_static)) { 1837 if (!PrintSharedArchiveAndExit) { 1838 fail_continue("shared class paths mismatch (hint: enable -Xlog:class+path=info to diagnose the failure)"); 1839 status = false; 1840 } 1841 } 1842 } 1843 1844 if (_paths_misc_info != NULL) { 1845 FREE_C_HEAP_ARRAY(char, _paths_misc_info); 1846 _paths_misc_info = NULL; 1847 } 1848 return status; 1849 } 1850 1851 // Check if a given address is within one of the shared regions 1852 bool FileMapInfo::is_in_shared_region(const void* p, int idx) { 1853 assert(idx == MetaspaceShared::ro || 1854 idx == MetaspaceShared::rw || 1855 idx == MetaspaceShared::mc || 1856 idx == MetaspaceShared::md, "invalid region index"); 1857 char* base = region_addr(idx); 1858 if (p >= base && p < base + space_at(idx)->_used) { 1859 return true; 1860 } 1861 return false; 1862 } 1863 1864 // Unmap mapped regions of shared space. 1865 void FileMapInfo::stop_sharing_and_unmap(const char* msg) { 1866 MetaspaceShared::set_shared_metaspace_range(NULL, NULL); 1867 1868 FileMapInfo *map_info = FileMapInfo::current_info(); 1869 if (map_info) { 1870 map_info->fail_continue("%s", msg); 1871 for (int i = 0; i < MetaspaceShared::num_non_heap_spaces; i++) { 1872 if (!HeapShared::is_heap_region(i)) { 1873 char *addr = map_info->region_addr(i); 1874 if (addr != NULL) { 1875 map_info->unmap_region(i); 1876 map_info->space_at(i)->_addr._base = NULL; 1877 } 1878 } 1879 } 1880 // Dealloc the archive heap regions only without unmapping. The regions are part 1881 // of the java heap. Unmapping of the heap regions are managed by GC. 1882 map_info->dealloc_archive_heap_regions(open_archive_heap_ranges, 1883 num_open_archive_heap_ranges, 1884 true); 1885 map_info->dealloc_archive_heap_regions(closed_archive_heap_ranges, 1886 num_closed_archive_heap_ranges, 1887 false); 1888 } else if (DumpSharedSpaces) { 1889 fail_stop("%s", msg); 1890 } 1891 } 1892 1893 #if INCLUDE_JVMTI 1894 ClassPathEntry** FileMapInfo::_classpath_entries_for_jvmti = NULL; 1895 1896 ClassPathEntry* FileMapInfo::get_classpath_entry_for_jvmti(int i, TRAPS) { 1897 ClassPathEntry* ent = _classpath_entries_for_jvmti[i]; 1898 if (ent == NULL) { 1899 if (i == 0) { 1900 ent = ClassLoader:: get_jrt_entry(); 1901 assert(ent != NULL, "must be"); 1902 } else { 1903 SharedClassPathEntry* scpe = shared_path(i); 1904 assert(scpe->is_jar(), "must be"); // other types of scpe will not produce archived classes 1905 1906 const char* path = scpe->name(); 1907 struct stat st; 1908 if (os::stat(path, &st) != 0) { 1909 char *msg = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, strlen(path) + 128); ; 1910 jio_snprintf(msg, strlen(path) + 127, "error in opening JAR file %s", path); 1911 THROW_MSG_(vmSymbols::java_io_IOException(), msg, NULL); 1912 } else { 1913 ent = ClassLoader::create_class_path_entry(path, &st, /*throw_exception=*/true, false, false, CHECK_NULL); 1914 } 1915 } 1916 1917 MutexLocker mu(CDSClassFileStream_lock, THREAD); 1918 if (_classpath_entries_for_jvmti[i] == NULL) { 1919 _classpath_entries_for_jvmti[i] = ent; 1920 } else { 1921 // Another thread has beat me to creating this entry 1922 delete ent; 1923 ent = _classpath_entries_for_jvmti[i]; 1924 } 1925 } 1926 1927 return ent; 1928 } 1929 1930 ClassFileStream* FileMapInfo::open_stream_for_jvmti(InstanceKlass* ik, Handle class_loader, TRAPS) { 1931 int path_index = ik->shared_classpath_index(); 1932 assert(path_index >= 0, "should be called for shared built-in classes only"); 1933 assert(path_index < (int)get_number_of_shared_paths(), "sanity"); 1934 1935 ClassPathEntry* cpe = get_classpath_entry_for_jvmti(path_index, CHECK_NULL); 1936 assert(cpe != NULL, "must be"); 1937 1938 Symbol* name = ik->name(); 1939 const char* const class_name = name->as_C_string(); 1940 const char* const file_name = ClassLoader::file_name_for_class_name(class_name, 1941 name->utf8_length()); 1942 ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(class_loader()); 1943 ClassFileStream* cfs = cpe->open_stream_for_loader(file_name, loader_data, THREAD); 1944 assert(cfs != NULL, "must be able to read the classfile data of shared classes for built-in loaders."); 1945 log_debug(cds, jvmti)("classfile data for %s [%d: %s] = %d bytes", class_name, path_index, 1946 cfs->source(), cfs->length()); 1947 return cfs; 1948 } 1949 1950 #endif