1 /* 2 * Copyright (c) 2003, 2016, 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 "classfile/classLoader.hpp" 27 #include "classfile/compactHashtable.inline.hpp" 28 #include "classfile/sharedClassUtil.hpp" 29 #include "classfile/symbolTable.hpp" 30 #include "classfile/systemDictionaryShared.hpp" 31 #include "classfile/altHashing.hpp" 32 #if INCLUDE_ALL_GCS 33 #include "gc/g1/g1CollectedHeap.hpp" 34 #endif 35 #include "memory/filemap.hpp" 36 #include "memory/metadataFactory.hpp" 37 #include "memory/oopFactory.hpp" 38 #include "oops/objArrayOop.hpp" 39 #include "prims/jvmtiExport.hpp" 40 #include "runtime/arguments.hpp" 41 #include "runtime/java.hpp" 42 #include "runtime/os.hpp" 43 #include "runtime/vm_version.hpp" 44 #include "services/memTracker.hpp" 45 #include "utilities/defaultStream.hpp" 46 47 # include <sys/stat.h> 48 # include <errno.h> 49 50 #ifndef O_BINARY // if defined (Win32) use binary files. 51 #define O_BINARY 0 // otherwise do nothing. 52 #endif 53 54 extern address JVM_FunctionAtStart(); 55 extern address JVM_FunctionAtEnd(); 56 57 // Complain and stop. All error conditions occurring during the writing of 58 // an archive file should stop the process. Unrecoverable errors during 59 // the reading of the archive file should stop the process. 60 61 static void fail(const char *msg, va_list ap) { 62 // This occurs very early during initialization: tty is not initialized. 63 jio_fprintf(defaultStream::error_stream(), 64 "An error has occurred while processing the" 65 " shared archive file.\n"); 66 jio_vfprintf(defaultStream::error_stream(), msg, ap); 67 jio_fprintf(defaultStream::error_stream(), "\n"); 68 // Do not change the text of the below message because some tests check for it. 69 vm_exit_during_initialization("Unable to use shared archive.", NULL); 70 } 71 72 73 void FileMapInfo::fail_stop(const char *msg, ...) { 74 va_list ap; 75 va_start(ap, msg); 76 fail(msg, ap); // Never returns. 77 va_end(ap); // for completeness. 78 } 79 80 81 // Complain and continue. Recoverable errors during the reading of the 82 // archive file may continue (with sharing disabled). 83 // 84 // If we continue, then disable shared spaces and close the file. 85 86 void FileMapInfo::fail_continue(const char *msg, ...) { 87 va_list ap; 88 va_start(ap, msg); 89 MetaspaceShared::set_archive_loading_failed(); 90 if (PrintSharedArchiveAndExit && _validating_classpath_entry_table) { 91 // If we are doing PrintSharedArchiveAndExit and some of the classpath entries 92 // do not validate, we can still continue "limping" to validate the remaining 93 // entries. No need to quit. 94 tty->print("["); 95 tty->vprint(msg, ap); 96 tty->print_cr("]"); 97 } else { 98 if (RequireSharedSpaces) { 99 fail(msg, ap); 100 } else { 101 if (PrintSharedSpaces) { 102 tty->print("UseSharedSpaces: "); 103 tty->vprint_cr(msg, ap); 104 } 105 } 106 UseSharedSpaces = false; 107 assert(current_info() != NULL, "singleton must be registered"); 108 current_info()->close(); 109 } 110 va_end(ap); 111 } 112 113 // Fill in the fileMapInfo structure with data about this VM instance. 114 115 // This method copies the vm version info into header_version. If the version is too 116 // long then a truncated version, which has a hash code appended to it, is copied. 117 // 118 // Using a template enables this method to verify that header_version is an array of 119 // length JVM_IDENT_MAX. This ensures that the code that writes to the CDS file and 120 // the code that reads the CDS file will both use the same size buffer. Hence, will 121 // use identical truncation. This is necessary for matching of truncated versions. 122 template <int N> static void get_header_version(char (&header_version) [N]) { 123 assert(N == JVM_IDENT_MAX, "Bad header_version size"); 124 125 const char *vm_version = VM_Version::internal_vm_info_string(); 126 const int version_len = (int)strlen(vm_version); 127 128 if (version_len < (JVM_IDENT_MAX-1)) { 129 strcpy(header_version, vm_version); 130 131 } else { 132 // Get the hash value. Use a static seed because the hash needs to return the same 133 // value over multiple jvm invocations. 134 unsigned int hash = AltHashing::murmur3_32(8191, (const jbyte*)vm_version, version_len); 135 136 // Truncate the ident, saving room for the 8 hex character hash value. 137 strncpy(header_version, vm_version, JVM_IDENT_MAX-9); 138 139 // Append the hash code as eight hex digits. 140 sprintf(&header_version[JVM_IDENT_MAX-9], "%08x", hash); 141 header_version[JVM_IDENT_MAX-1] = 0; // Null terminate. 142 } 143 } 144 145 FileMapInfo::FileMapInfo() { 146 assert(_current_info == NULL, "must be singleton"); // not thread safe 147 _current_info = this; 148 memset(this, 0, sizeof(FileMapInfo)); 149 _file_offset = 0; 150 _file_open = false; 151 _header = SharedClassUtil::allocate_file_map_header(); 152 _header->_version = _invalid_version; 153 } 154 155 FileMapInfo::~FileMapInfo() { 156 assert(_current_info == this, "must be singleton"); // not thread safe 157 _current_info = NULL; 158 } 159 160 void FileMapInfo::populate_header(size_t alignment) { 161 _header->populate(this, alignment); 162 } 163 164 size_t FileMapInfo::FileMapHeader::data_size() { 165 return SharedClassUtil::file_map_header_size() - sizeof(FileMapInfo::FileMapHeaderBase); 166 } 167 168 void FileMapInfo::FileMapHeader::populate(FileMapInfo* mapinfo, size_t alignment) { 169 _magic = 0xf00baba2; 170 _version = _current_version; 171 _alignment = alignment; 172 _obj_alignment = ObjectAlignmentInBytes; 173 _compact_strings = CompactStrings; 174 _narrow_oop_mode = Universe::narrow_oop_mode(); 175 _narrow_oop_shift = Universe::narrow_oop_shift(); 176 _max_heap_size = MaxHeapSize; 177 _narrow_klass_base = Universe::narrow_klass_base(); 178 _narrow_klass_shift = Universe::narrow_klass_shift(); 179 _classpath_entry_table_size = mapinfo->_classpath_entry_table_size; 180 _classpath_entry_table = mapinfo->_classpath_entry_table; 181 _classpath_entry_size = mapinfo->_classpath_entry_size; 182 183 // The following fields are for sanity checks for whether this archive 184 // will function correctly with this JVM and the bootclasspath it's 185 // invoked with. 186 187 // JVM version string ... changes on each build. 188 get_header_version(_jvm_ident); 189 } 190 191 void FileMapInfo::allocate_classpath_entry_table() { 192 int bytes = 0; 193 int count = 0; 194 char* strptr = NULL; 195 char* strptr_max = NULL; 196 Thread* THREAD = Thread::current(); 197 198 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data(); 199 size_t entry_size = SharedClassUtil::shared_class_path_entry_size(); 200 201 for (int pass=0; pass<2; pass++) { 202 ClassPathEntry *cpe = ClassLoader::classpath_entry(0); 203 204 for (int cur_entry = 0 ; cpe != NULL; cpe = cpe->next(), cur_entry++) { 205 const char *name = cpe->name(); 206 int name_bytes = (int)(strlen(name) + 1); 207 208 if (pass == 0) { 209 count ++; 210 bytes += (int)entry_size; 211 bytes += name_bytes; 212 log_info(class, path)("add main shared path (%s) %s", (cpe->is_jar_file() ? "jar" : "dir"), name); 213 } else { 214 SharedClassPathEntry* ent = shared_classpath(cur_entry); 215 if (cpe->is_jar_file()) { 216 struct stat st; 217 if (os::stat(name, &st) != 0) { 218 // The file/dir must exist, or it would not have been added 219 // into ClassLoader::classpath_entry(). 220 // 221 // If we can't access a jar file in the boot path, then we can't 222 // make assumptions about where classes get loaded from. 223 FileMapInfo::fail_stop("Unable to open jar file %s.", name); 224 } 225 226 EXCEPTION_MARK; // The following call should never throw, but would exit VM on error. 227 SharedClassUtil::update_shared_classpath(cpe, ent, st.st_mtime, st.st_size, THREAD); 228 } else { 229 struct stat st; 230 if (os::stat(name, &st) == 0) { 231 if (cpe->is_jrt()) { 232 // it's the "modules" jimage 233 ent->_timestamp = st.st_mtime; 234 ent->_filesize = st.st_size; 235 } else if ((st.st_mode & S_IFDIR) == S_IFDIR) { 236 if (!os::dir_is_empty(name)) { 237 ClassLoader::exit_with_path_failure( 238 "Cannot have non-empty directory in archived classpaths", name); 239 } 240 ent->_filesize = -1; 241 } 242 } 243 if (ent->_filesize == 0) { 244 // unknown 245 ent->_filesize = -2; 246 } 247 } 248 ent->_name = strptr; 249 if (strptr + name_bytes <= strptr_max) { 250 strncpy(strptr, name, (size_t)name_bytes); // name_bytes includes trailing 0. 251 strptr += name_bytes; 252 } else { 253 assert(0, "miscalculated buffer size"); 254 } 255 } 256 } 257 258 if (pass == 0) { 259 EXCEPTION_MARK; // The following call should never throw, but would exit VM on error. 260 Array<u8>* arr = MetadataFactory::new_array<u8>(loader_data, (bytes + 7)/8, THREAD); 261 strptr = (char*)(arr->data()); 262 strptr_max = strptr + bytes; 263 SharedClassPathEntry* table = (SharedClassPathEntry*)strptr; 264 strptr += entry_size * count; 265 266 _classpath_entry_table_size = count; 267 _classpath_entry_table = table; 268 _classpath_entry_size = entry_size; 269 } 270 } 271 } 272 273 bool FileMapInfo::validate_classpath_entry_table() { 274 _validating_classpath_entry_table = true; 275 276 int count = _header->_classpath_entry_table_size; 277 278 _classpath_entry_table = _header->_classpath_entry_table; 279 _classpath_entry_size = _header->_classpath_entry_size; 280 281 for (int i=0; i<count; i++) { 282 SharedClassPathEntry* ent = shared_classpath(i); 283 struct stat st; 284 const char* name = ent->_name; 285 bool ok = true; 286 log_info(class, path)("checking shared classpath entry: %s", name); 287 if (os::stat(name, &st) != 0) { 288 fail_continue("Required classpath entry does not exist: %s", name); 289 ok = false; 290 } else if (ent->is_dir()) { 291 if (!os::dir_is_empty(name)) { 292 fail_continue("directory is not empty: %s", name); 293 ok = false; 294 } 295 } else if (ent->is_jar_or_bootimage()) { 296 if (ent->_timestamp != st.st_mtime || 297 ent->_filesize != st.st_size) { 298 ok = false; 299 if (PrintSharedArchiveAndExit) { 300 fail_continue(ent->_timestamp != st.st_mtime ? 301 "Timestamp mismatch" : 302 "File size mismatch"); 303 } else { 304 fail_continue("A jar/jimage file is not the one used while building" 305 " the shared archive file: %s", name); 306 } 307 } 308 } 309 if (ok) { 310 log_info(class, path)("ok"); 311 } else if (!PrintSharedArchiveAndExit) { 312 _validating_classpath_entry_table = false; 313 return false; 314 } 315 } 316 317 _classpath_entry_table_size = _header->_classpath_entry_table_size; 318 _validating_classpath_entry_table = false; 319 return true; 320 } 321 322 323 // Read the FileMapInfo information from the file. 324 325 bool FileMapInfo::init_from_file(int fd) { 326 size_t sz = _header->data_size(); 327 char* addr = _header->data(); 328 size_t n = os::read(fd, addr, (unsigned int)sz); 329 if (n != sz) { 330 fail_continue("Unable to read the file header."); 331 return false; 332 } 333 if (_header->_version != current_version()) { 334 fail_continue("The shared archive file has the wrong version."); 335 return false; 336 } 337 _file_offset = (long)n; 338 339 size_t info_size = _header->_paths_misc_info_size; 340 _paths_misc_info = NEW_C_HEAP_ARRAY_RETURN_NULL(char, info_size, mtClass); 341 if (_paths_misc_info == NULL) { 342 fail_continue("Unable to read the file header."); 343 return false; 344 } 345 n = os::read(fd, _paths_misc_info, (unsigned int)info_size); 346 if (n != info_size) { 347 fail_continue("Unable to read the shared path info header."); 348 FREE_C_HEAP_ARRAY(char, _paths_misc_info); 349 _paths_misc_info = NULL; 350 return false; 351 } 352 353 size_t len = lseek(fd, 0, SEEK_END); 354 struct FileMapInfo::FileMapHeader::space_info* si = 355 &_header->_space[MetaspaceShared::mc]; 356 if (si->_file_offset >= len || len - si->_file_offset < si->_used) { 357 fail_continue("The shared archive file has been truncated."); 358 return false; 359 } 360 361 _file_offset += (long)n; 362 return true; 363 } 364 365 366 // Read the FileMapInfo information from the file. 367 bool FileMapInfo::open_for_read() { 368 _full_path = Arguments::GetSharedArchivePath(); 369 int fd = open(_full_path, O_RDONLY | O_BINARY, 0); 370 if (fd < 0) { 371 if (errno == ENOENT) { 372 // Not locating the shared archive is ok. 373 fail_continue("Specified shared archive not found."); 374 } else { 375 fail_continue("Failed to open shared archive file (%s).", 376 os::strerror(errno)); 377 } 378 return false; 379 } 380 381 _fd = fd; 382 _file_open = true; 383 return true; 384 } 385 386 387 // Write the FileMapInfo information to the file. 388 389 void FileMapInfo::open_for_write() { 390 _full_path = Arguments::GetSharedArchivePath(); 391 if (PrintSharedSpaces) { 392 tty->print_cr("Dumping shared data to file: "); 393 tty->print_cr(" %s", _full_path); 394 } 395 396 #ifdef _WINDOWS // On Windows, need WRITE permission to remove the file. 397 chmod(_full_path, _S_IREAD | _S_IWRITE); 398 #endif 399 400 // Use remove() to delete the existing file because, on Unix, this will 401 // allow processes that have it open continued access to the file. 402 remove(_full_path); 403 int fd = open(_full_path, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0444); 404 if (fd < 0) { 405 fail_stop("Unable to create shared archive file %s: (%s).", _full_path, 406 os::strerror(errno)); 407 } 408 _fd = fd; 409 _file_offset = 0; 410 _file_open = true; 411 } 412 413 414 // Write the header to the file, seek to the next allocation boundary. 415 416 void FileMapInfo::write_header() { 417 int info_size = ClassLoader::get_shared_paths_misc_info_size(); 418 419 _header->_paths_misc_info_size = info_size; 420 421 align_file_position(); 422 size_t sz = _header->data_size(); 423 char* addr = _header->data(); 424 write_bytes(addr, (int)sz); // skip the C++ vtable 425 write_bytes(ClassLoader::get_shared_paths_misc_info(), info_size); 426 align_file_position(); 427 } 428 429 430 // Dump shared spaces to file. 431 432 void FileMapInfo::write_space(int i, Metaspace* space, bool read_only) { 433 align_file_position(); 434 size_t used = space->used_bytes_slow(Metaspace::NonClassType); 435 size_t capacity = space->capacity_bytes_slow(Metaspace::NonClassType); 436 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i]; 437 write_region(i, (char*)space->bottom(), used, capacity, read_only, false); 438 } 439 440 441 // Dump region to file. 442 443 void FileMapInfo::write_region(int region, char* base, size_t size, 444 size_t capacity, bool read_only, 445 bool allow_exec) { 446 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[region]; 447 448 if (_file_open) { 449 guarantee(si->_file_offset == _file_offset, "file offset mismatch."); 450 if (PrintSharedSpaces) { 451 tty->print_cr("Shared file region %d: " SIZE_FORMAT_HEX_W(6) " bytes, addr " INTPTR_FORMAT 452 " file offset " SIZE_FORMAT_HEX_W(6), region, size, p2i(base), _file_offset); 453 } 454 } else { 455 si->_file_offset = _file_offset; 456 } 457 if (MetaspaceShared::is_string_region(region)) { 458 assert((base - (char*)Universe::narrow_oop_base()) % HeapWordSize == 0, "Sanity"); 459 if (base != NULL) { 460 si->_addr._offset = (intx)oopDesc::encode_heap_oop_not_null((oop)base); 461 } else { 462 si->_addr._offset = 0; 463 } 464 } else { 465 si->_addr._base = base; 466 } 467 si->_used = size; 468 si->_capacity = capacity; 469 si->_read_only = read_only; 470 si->_allow_exec = allow_exec; 471 si->_crc = ClassLoader::crc32(0, base, (jint)size); 472 write_bytes_aligned(base, (int)size); 473 } 474 475 // Write the string space. The string space contains one or multiple GC(G1) regions. 476 // When the total string space size is smaller than one GC region of the dump time, 477 // only one string region is used for shared strings. 478 // 479 // If the total string space size is bigger than one GC region, there would be more 480 // than one GC regions allocated for shared strings. The first/bottom GC region might 481 // be a partial GC region with the empty portion at the higher address within that region. 482 // The non-empty portion of the first region is written into the archive as one string 483 // region. The rest are consecutive full GC regions if they exist, which can be written 484 // out in one chunk as another string region. 485 void FileMapInfo::write_string_regions(GrowableArray<MemRegion> *regions) { 486 for (int i = MetaspaceShared::first_string; 487 i < MetaspaceShared::first_string + MetaspaceShared::max_strings; i++) { 488 char* start = NULL; 489 size_t size = 0; 490 if (regions->is_nonempty()) { 491 if (i == MetaspaceShared::first_string) { 492 MemRegion first = regions->first(); 493 start = (char*)first.start(); 494 size = first.byte_size(); 495 } else { 496 int len = regions->length(); 497 if (len > 1) { 498 start = (char*)regions->at(1).start(); 499 size = (char*)regions->at(len - 1).end() - start; 500 } 501 } 502 } 503 write_region(i, start, size, size, false, false); 504 } 505 } 506 507 508 // Dump bytes to file -- at the current file position. 509 510 void FileMapInfo::write_bytes(const void* buffer, int nbytes) { 511 if (_file_open) { 512 int n = ::write(_fd, buffer, nbytes); 513 if (n != nbytes) { 514 // It is dangerous to leave the corrupted shared archive file around, 515 // close and remove the file. See bug 6372906. 516 close(); 517 remove(_full_path); 518 fail_stop("Unable to write to shared archive file."); 519 } 520 } 521 _file_offset += nbytes; 522 } 523 524 525 // Align file position to an allocation unit boundary. 526 527 void FileMapInfo::align_file_position() { 528 size_t new_file_offset = align_size_up(_file_offset, 529 os::vm_allocation_granularity()); 530 if (new_file_offset != _file_offset) { 531 _file_offset = new_file_offset; 532 if (_file_open) { 533 // Seek one byte back from the target and write a byte to insure 534 // that the written file is the correct length. 535 _file_offset -= 1; 536 if (lseek(_fd, (long)_file_offset, SEEK_SET) < 0) { 537 fail_stop("Unable to seek."); 538 } 539 char zero = 0; 540 write_bytes(&zero, 1); 541 } 542 } 543 } 544 545 546 // Dump bytes to file -- at the current file position. 547 548 void FileMapInfo::write_bytes_aligned(const void* buffer, int nbytes) { 549 align_file_position(); 550 write_bytes(buffer, nbytes); 551 align_file_position(); 552 } 553 554 555 // Close the shared archive file. This does NOT unmap mapped regions. 556 557 void FileMapInfo::close() { 558 if (_file_open) { 559 if (::close(_fd) < 0) { 560 fail_stop("Unable to close the shared archive file."); 561 } 562 _file_open = false; 563 _fd = -1; 564 } 565 } 566 567 568 // JVM/TI RedefineClasses() support: 569 // Remap the shared readonly space to shared readwrite, private. 570 bool FileMapInfo::remap_shared_readonly_as_readwrite() { 571 int idx = 0; 572 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[idx]; 573 if (!si->_read_only) { 574 // the space is already readwrite so we are done 575 return true; 576 } 577 size_t used = si->_used; 578 size_t size = align_size_up(used, os::vm_allocation_granularity()); 579 if (!open_for_read()) { 580 return false; 581 } 582 char *addr = _header->region_addr(idx); 583 char *base = os::remap_memory(_fd, _full_path, si->_file_offset, 584 addr, size, false /* !read_only */, 585 si->_allow_exec); 586 close(); 587 if (base == NULL) { 588 fail_continue("Unable to remap shared readonly space (errno=%d).", errno); 589 return false; 590 } 591 if (base != addr) { 592 fail_continue("Unable to remap shared readonly space at required address."); 593 return false; 594 } 595 si->_read_only = false; 596 return true; 597 } 598 599 // Map the whole region at once, assumed to be allocated contiguously. 600 ReservedSpace FileMapInfo::reserve_shared_memory() { 601 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[0]; 602 char* requested_addr = _header->region_addr(0); 603 604 size_t size = FileMapInfo::shared_spaces_size(); 605 606 // Reserve the space first, then map otherwise map will go right over some 607 // other reserved memory (like the code cache). 608 ReservedSpace rs(size, os::vm_allocation_granularity(), false, requested_addr); 609 if (!rs.is_reserved()) { 610 fail_continue("Unable to reserve shared space at required address " 611 INTPTR_FORMAT, p2i(requested_addr)); 612 return rs; 613 } 614 // the reserved virtual memory is for mapping class data sharing archive 615 MemTracker::record_virtual_memory_type((address)rs.base(), mtClassShared); 616 617 return rs; 618 } 619 620 // Memory map a region in the address space. 621 static const char* shared_region_name[] = { "ReadOnly", "ReadWrite", "MiscData", "MiscCode", 622 "String1", "String2" }; 623 624 char* FileMapInfo::map_region(int i) { 625 assert(!MetaspaceShared::is_string_region(i), "sanity"); 626 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i]; 627 size_t used = si->_used; 628 size_t alignment = os::vm_allocation_granularity(); 629 size_t size = align_size_up(used, alignment); 630 char *requested_addr = _header->region_addr(i); 631 632 // If a tool agent is in use (debugging enabled), we must map the address space RW 633 if (JvmtiExport::can_modify_any_class() || JvmtiExport::can_walk_any_space()) { 634 si->_read_only = false; 635 } 636 637 // map the contents of the CDS archive in this memory 638 char *base = os::map_memory(_fd, _full_path, si->_file_offset, 639 requested_addr, size, si->_read_only, 640 si->_allow_exec); 641 if (base == NULL || base != requested_addr) { 642 fail_continue("Unable to map %s shared space at required address.", shared_region_name[i]); 643 return NULL; 644 } 645 #ifdef _WINDOWS 646 // This call is Windows-only because the memory_type gets recorded for the other platforms 647 // in method FileMapInfo::reserve_shared_memory(), which is not called on Windows. 648 MemTracker::record_virtual_memory_type((address)base, mtClassShared); 649 #endif 650 651 return base; 652 } 653 654 static MemRegion *string_ranges = NULL; 655 static int num_ranges = 0; 656 bool FileMapInfo::map_string_regions() { 657 #if INCLUDE_ALL_GCS 658 if (UseG1GC && UseCompressedOops && UseCompressedClassPointers) { 659 // Check that all the narrow oop and klass encodings match the archive 660 if (narrow_oop_mode() != Universe::narrow_oop_mode() || 661 narrow_oop_shift() != Universe::narrow_oop_shift() || 662 narrow_klass_base() != Universe::narrow_klass_base() || 663 narrow_klass_shift() != Universe::narrow_klass_shift()) { 664 if (PrintSharedSpaces && _header->_space[MetaspaceShared::first_string]._used > 0) { 665 tty->print_cr("Shared string data from the CDS archive is being ignored. " 666 "The current CompressedOops/CompressedClassPointers encoding differs from " 667 "that archived due to heap size change. The archive was dumped using max heap " 668 "size " UINTX_FORMAT "M.", max_heap_size()/M); 669 } 670 } else { 671 string_ranges = new MemRegion[MetaspaceShared::max_strings]; 672 struct FileMapInfo::FileMapHeader::space_info* si; 673 674 for (int i = MetaspaceShared::first_string; 675 i < MetaspaceShared::first_string + MetaspaceShared::max_strings; i++) { 676 si = &_header->_space[i]; 677 size_t used = si->_used; 678 if (used > 0) { 679 size_t size = used; 680 char* requested_addr = (char*)((void*)oopDesc::decode_heap_oop_not_null( 681 (narrowOop)si->_addr._offset)); 682 string_ranges[num_ranges] = MemRegion((HeapWord*)requested_addr, size / HeapWordSize); 683 num_ranges ++; 684 } 685 } 686 687 if (num_ranges == 0) { 688 StringTable::ignore_shared_strings(true); 689 return true; // no shared string data 690 } 691 692 // Check that ranges are within the java heap 693 if (!G1CollectedHeap::heap()->check_archive_addresses(string_ranges, num_ranges)) { 694 fail_continue("Unable to allocate shared string space: range is not " 695 "within java heap."); 696 return false; 697 } 698 699 // allocate from java heap 700 if (!G1CollectedHeap::heap()->alloc_archive_regions(string_ranges, num_ranges)) { 701 fail_continue("Unable to allocate shared string space: range is " 702 "already in use."); 703 return false; 704 } 705 706 // Map the string data. No need to call MemTracker::record_virtual_memory_type() 707 // for mapped string regions as they are part of the reserved java heap, which 708 // is already recorded. 709 for (int i = 0; i < num_ranges; i++) { 710 si = &_header->_space[MetaspaceShared::first_string + i]; 711 char* addr = (char*)string_ranges[i].start(); 712 char* base = os::map_memory(_fd, _full_path, si->_file_offset, 713 addr, string_ranges[i].byte_size(), si->_read_only, 714 si->_allow_exec); 715 if (base == NULL || base != addr) { 716 // dealloc the string regions from java heap 717 dealloc_string_regions(); 718 fail_continue("Unable to map shared string space at required address."); 719 return false; 720 } 721 } 722 723 if (!verify_string_regions()) { 724 // dealloc the string regions from java heap 725 dealloc_string_regions(); 726 fail_continue("Shared string regions are corrupt"); 727 return false; 728 } 729 730 // the shared string data is mapped successfully 731 return true; 732 } 733 } else { 734 if (PrintSharedSpaces && _header->_space[MetaspaceShared::first_string]._used > 0) { 735 tty->print_cr("Shared string data from the CDS archive is being ignored. UseG1GC, " 736 "UseCompressedOops and UseCompressedClassPointers are required."); 737 } 738 } 739 740 // if we get here, the shared string data is not mapped 741 assert(string_ranges == NULL && num_ranges == 0, "sanity"); 742 StringTable::ignore_shared_strings(true); 743 #endif 744 return true; 745 } 746 747 bool FileMapInfo::verify_string_regions() { 748 for (int i = MetaspaceShared::first_string; 749 i < MetaspaceShared::first_string + MetaspaceShared::max_strings; i++) { 750 if (!verify_region_checksum(i)) { 751 return false; 752 } 753 } 754 return true; 755 } 756 757 void FileMapInfo::fixup_string_regions() { 758 #if INCLUDE_ALL_GCS 759 // If any string regions were found, call the fill routine to make them parseable. 760 // Note that string_ranges may be non-NULL even if no ranges were found. 761 if (num_ranges != 0) { 762 assert(string_ranges != NULL, "Null string_ranges array with non-zero count"); 763 G1CollectedHeap::heap()->fill_archive_regions(string_ranges, num_ranges); 764 } 765 #endif 766 } 767 768 bool FileMapInfo::verify_region_checksum(int i) { 769 if (!VerifySharedSpaces) { 770 return true; 771 } 772 773 size_t sz = _header->_space[i]._used; 774 775 if (sz == 0) { 776 return true; // no data 777 } 778 if (MetaspaceShared::is_string_region(i) && StringTable::shared_string_ignored()) { 779 return true; // shared string data are not mapped 780 } 781 const char* buf = _header->region_addr(i); 782 int crc = ClassLoader::crc32(0, buf, (jint)sz); 783 if (crc != _header->_space[i]._crc) { 784 fail_continue("Checksum verification failed."); 785 return false; 786 } 787 return true; 788 } 789 790 // Unmap a memory region in the address space. 791 792 void FileMapInfo::unmap_region(int i) { 793 assert(!MetaspaceShared::is_string_region(i), "sanity"); 794 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i]; 795 size_t used = si->_used; 796 size_t size = align_size_up(used, os::vm_allocation_granularity()); 797 798 if (used == 0) { 799 return; 800 } 801 802 char* addr = _header->region_addr(i); 803 if (!os::unmap_memory(addr, size)) { 804 fail_stop("Unable to unmap shared space."); 805 } 806 } 807 808 // dealloc the archived string region from java heap 809 void FileMapInfo::dealloc_string_regions() { 810 #if INCLUDE_ALL_GCS 811 if (num_ranges > 0) { 812 assert(string_ranges != NULL, "Null string_ranges array with non-zero count"); 813 G1CollectedHeap::heap()->dealloc_archive_regions(string_ranges, num_ranges); 814 } 815 #endif 816 } 817 818 void FileMapInfo::assert_mark(bool check) { 819 if (!check) { 820 fail_stop("Mark mismatch while restoring from shared file."); 821 } 822 } 823 824 825 FileMapInfo* FileMapInfo::_current_info = NULL; 826 SharedClassPathEntry* FileMapInfo::_classpath_entry_table = NULL; 827 int FileMapInfo::_classpath_entry_table_size = 0; 828 size_t FileMapInfo::_classpath_entry_size = 0x1234baad; 829 bool FileMapInfo::_validating_classpath_entry_table = false; 830 831 // Open the shared archive file, read and validate the header 832 // information (version, boot classpath, etc.). If initialization 833 // fails, shared spaces are disabled and the file is closed. [See 834 // fail_continue.] 835 // 836 // Validation of the archive is done in two steps: 837 // 838 // [1] validate_header() - done here. This checks the header, including _paths_misc_info. 839 // [2] validate_classpath_entry_table - this is done later, because the table is in the RW 840 // region of the archive, which is not mapped yet. 841 bool FileMapInfo::initialize() { 842 assert(UseSharedSpaces, "UseSharedSpaces expected."); 843 844 if (!open_for_read()) { 845 return false; 846 } 847 848 init_from_file(_fd); 849 if (!validate_header()) { 850 return false; 851 } 852 853 SharedReadOnlySize = _header->_space[0]._capacity; 854 SharedReadWriteSize = _header->_space[1]._capacity; 855 SharedMiscDataSize = _header->_space[2]._capacity; 856 SharedMiscCodeSize = _header->_space[3]._capacity; 857 return true; 858 } 859 860 char* FileMapInfo::FileMapHeader::region_addr(int idx) { 861 if (MetaspaceShared::is_string_region(idx)) { 862 return (char*)((void*)oopDesc::decode_heap_oop_not_null( 863 (narrowOop)_space[idx]._addr._offset)); 864 } else { 865 return _space[idx]._addr._base; 866 } 867 } 868 869 int FileMapInfo::FileMapHeader::compute_crc() { 870 char* header = data(); 871 // start computing from the field after _crc 872 char* buf = (char*)&_crc + sizeof(int); 873 size_t sz = data_size() - (buf - header); 874 int crc = ClassLoader::crc32(0, buf, (jint)sz); 875 return crc; 876 } 877 878 bool FileMapInfo::FileMapHeader::validate() { 879 if (VerifySharedSpaces && compute_crc() != _crc) { 880 fail_continue("Header checksum verification failed."); 881 return false; 882 } 883 884 if (Arguments::get_xpatchprefix() != NULL) { 885 FileMapInfo::fail_continue("The shared archive file cannot be used with -Xpatch."); 886 return false; 887 } 888 889 if (_version != current_version()) { 890 FileMapInfo::fail_continue("The shared archive file is the wrong version."); 891 return false; 892 } 893 if (_magic != (int)0xf00baba2) { 894 FileMapInfo::fail_continue("The shared archive file has a bad magic number."); 895 return false; 896 } 897 char header_version[JVM_IDENT_MAX]; 898 get_header_version(header_version); 899 if (strncmp(_jvm_ident, header_version, JVM_IDENT_MAX-1) != 0) { 900 log_info(class, path)("expected: %s", header_version); 901 log_info(class, path)("actual: %s", _jvm_ident); 902 FileMapInfo::fail_continue("The shared archive file was created by a different" 903 " version or build of HotSpot"); 904 return false; 905 } 906 if (_obj_alignment != ObjectAlignmentInBytes) { 907 FileMapInfo::fail_continue("The shared archive file's ObjectAlignmentInBytes of %d" 908 " does not equal the current ObjectAlignmentInBytes of " INTX_FORMAT ".", 909 _obj_alignment, ObjectAlignmentInBytes); 910 return false; 911 } 912 if (_compact_strings != CompactStrings) { 913 FileMapInfo::fail_continue("The shared archive file's CompactStrings setting (%s)" 914 " does not equal the current CompactStrings setting (%s).", 915 _compact_strings ? "enabled" : "disabled", 916 CompactStrings ? "enabled" : "disabled"); 917 return false; 918 } 919 920 return true; 921 } 922 923 bool FileMapInfo::validate_header() { 924 bool status = _header->validate(); 925 926 if (status) { 927 if (!ClassLoader::check_shared_paths_misc_info(_paths_misc_info, _header->_paths_misc_info_size)) { 928 if (!PrintSharedArchiveAndExit) { 929 fail_continue("shared class paths mismatch (hint: enable -Xlog:class+path=info to diagnose the failure)"); 930 status = false; 931 } 932 } 933 } 934 935 if (_paths_misc_info != NULL) { 936 FREE_C_HEAP_ARRAY(char, _paths_misc_info); 937 _paths_misc_info = NULL; 938 } 939 return status; 940 } 941 942 // The following method is provided to see whether a given pointer 943 // falls in the mapped shared space. 944 // Param: 945 // p, The given pointer 946 // Return: 947 // True if the p is within the mapped shared space, otherwise, false. 948 bool FileMapInfo::is_in_shared_space(const void* p) { 949 for (int i = 0; i < MetaspaceShared::n_regions; i++) { 950 char *base; 951 if (MetaspaceShared::is_string_region(i) && _header->_space[i]._used == 0) { 952 continue; 953 } 954 base = _header->region_addr(i); 955 if (p >= base && p < base + _header->_space[i]._used) { 956 return true; 957 } 958 } 959 960 return false; 961 } 962 963 // Check if a given address is within one of the shared regions (ro, rw, md, mc) 964 bool FileMapInfo::is_in_shared_region(const void* p, int idx) { 965 assert((idx >= MetaspaceShared::ro) && (idx <= MetaspaceShared::mc), "invalid region index"); 966 char* base = _header->region_addr(idx); 967 if (p >= base && p < base + _header->_space[idx]._used) { 968 return true; 969 } 970 return false; 971 } 972 973 void FileMapInfo::print_shared_spaces() { 974 tty->print_cr("Shared Spaces:"); 975 for (int i = 0; i < MetaspaceShared::n_regions; i++) { 976 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i]; 977 char *base = _header->region_addr(i); 978 tty->print(" %s " INTPTR_FORMAT "-" INTPTR_FORMAT, 979 shared_region_name[i], 980 p2i(base), p2i(base + si->_used)); 981 } 982 } 983 984 // Unmap mapped regions of shared space. 985 void FileMapInfo::stop_sharing_and_unmap(const char* msg) { 986 FileMapInfo *map_info = FileMapInfo::current_info(); 987 if (map_info) { 988 map_info->fail_continue("%s", msg); 989 for (int i = 0; i < MetaspaceShared::num_non_strings; i++) { 990 char *addr = map_info->_header->region_addr(i); 991 if (addr != NULL && !MetaspaceShared::is_string_region(i)) { 992 map_info->unmap_region(i); 993 map_info->_header->_space[i]._addr._base = NULL; 994 } 995 } 996 // Dealloc the string regions only without unmapping. The string regions are part 997 // of the java heap. Unmapping of the heap regions are managed by GC. 998 map_info->dealloc_string_regions(); 999 } else if (DumpSharedSpaces) { 1000 fail_stop("%s", msg); 1001 } 1002 }