1 /* 2 * Copyright (c) 2003, 2015, 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/sharedClassUtil.hpp" 28 #include "classfile/symbolTable.hpp" 29 #include "classfile/systemDictionaryShared.hpp" 30 #include "classfile/altHashing.hpp" 31 #include "memory/filemap.hpp" 32 #include "memory/metadataFactory.hpp" 33 #include "memory/oopFactory.hpp" 34 #include "oops/objArrayOop.hpp" 35 #include "runtime/arguments.hpp" 36 #include "runtime/java.hpp" 37 #include "runtime/os.hpp" 38 #include "runtime/vm_version.hpp" 39 #include "services/memTracker.hpp" 40 #include "utilities/defaultStream.hpp" 41 42 # include <sys/stat.h> 43 # include <errno.h> 44 45 #ifndef O_BINARY // if defined (Win32) use binary files. 46 #define O_BINARY 0 // otherwise do nothing. 47 #endif 48 49 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC 50 extern address JVM_FunctionAtStart(); 51 extern address JVM_FunctionAtEnd(); 52 53 // Complain and stop. All error conditions occurring during the writing of 54 // an archive file should stop the process. Unrecoverable errors during 55 // the reading of the archive file should stop the process. 56 57 static void fail(const char *msg, va_list ap) { 58 // This occurs very early during initialization: tty is not initialized. 59 jio_fprintf(defaultStream::error_stream(), 60 "An error has occurred while processing the" 61 " shared archive file.\n"); 62 jio_vfprintf(defaultStream::error_stream(), msg, ap); 63 jio_fprintf(defaultStream::error_stream(), "\n"); 64 // Do not change the text of the below message because some tests check for it. 65 vm_exit_during_initialization("Unable to use shared archive.", NULL); 66 } 67 68 69 void FileMapInfo::fail_stop(const char *msg, ...) { 70 va_list ap; 71 va_start(ap, msg); 72 fail(msg, ap); // Never returns. 73 va_end(ap); // for completeness. 74 } 75 76 77 // Complain and continue. Recoverable errors during the reading of the 78 // archive file may continue (with sharing disabled). 79 // 80 // If we continue, then disable shared spaces and close the file. 81 82 void FileMapInfo::fail_continue(const char *msg, ...) { 83 va_list ap; 84 va_start(ap, msg); 85 MetaspaceShared::set_archive_loading_failed(); 86 if (PrintSharedArchiveAndExit && _validating_classpath_entry_table) { 87 // If we are doing PrintSharedArchiveAndExit and some of the classpath entries 88 // do not validate, we can still continue "limping" to validate the remaining 89 // entries. No need to quit. 90 tty->print("["); 91 tty->vprint(msg, ap); 92 tty->print_cr("]"); 93 } else { 94 if (RequireSharedSpaces) { 95 fail(msg, ap); 96 } else { 97 if (PrintSharedSpaces) { 98 tty->print_cr("UseSharedSpaces: %s", msg); 99 } 100 } 101 UseSharedSpaces = false; 102 assert(current_info() != NULL, "singleton must be registered"); 103 current_info()->close(); 104 } 105 va_end(ap); 106 } 107 108 // Fill in the fileMapInfo structure with data about this VM instance. 109 110 // This method copies the vm version info into header_version. If the version is too 111 // long then a truncated version, which has a hash code appended to it, is copied. 112 // 113 // Using a template enables this method to verify that header_version is an array of 114 // length JVM_IDENT_MAX. This ensures that the code that writes to the CDS file and 115 // the code that reads the CDS file will both use the same size buffer. Hence, will 116 // use identical truncation. This is necessary for matching of truncated versions. 117 template <int N> static void get_header_version(char (&header_version) [N]) { 118 assert(N == JVM_IDENT_MAX, "Bad header_version size"); 119 120 const char *vm_version = VM_Version::internal_vm_info_string(); 121 const int version_len = (int)strlen(vm_version); 122 123 if (version_len < (JVM_IDENT_MAX-1)) { 124 strcpy(header_version, vm_version); 125 126 } else { 127 // Get the hash value. Use a static seed because the hash needs to return the same 128 // value over multiple jvm invocations. 129 unsigned int hash = AltHashing::murmur3_32(8191, (const jbyte*)vm_version, version_len); 130 131 // Truncate the ident, saving room for the 8 hex character hash value. 132 strncpy(header_version, vm_version, JVM_IDENT_MAX-9); 133 134 // Append the hash code as eight hex digits. 135 sprintf(&header_version[JVM_IDENT_MAX-9], "%08x", hash); 136 header_version[JVM_IDENT_MAX-1] = 0; // Null terminate. 137 } 138 } 139 140 FileMapInfo::FileMapInfo() { 141 assert(_current_info == NULL, "must be singleton"); // not thread safe 142 _current_info = this; 143 memset(this, 0, sizeof(FileMapInfo)); 144 _file_offset = 0; 145 _file_open = false; 146 _header = SharedClassUtil::allocate_file_map_header(); 147 _header->_version = _invalid_version; 148 } 149 150 FileMapInfo::~FileMapInfo() { 151 assert(_current_info == this, "must be singleton"); // not thread safe 152 _current_info = NULL; 153 } 154 155 void FileMapInfo::populate_header(size_t alignment) { 156 _header->populate(this, alignment); 157 } 158 159 size_t FileMapInfo::FileMapHeader::data_size() { 160 return SharedClassUtil::file_map_header_size() - sizeof(FileMapInfo::FileMapHeaderBase); 161 } 162 163 void FileMapInfo::FileMapHeader::populate(FileMapInfo* mapinfo, size_t alignment) { 164 _magic = 0xf00baba2; 165 _version = _current_version; 166 _alignment = alignment; 167 _obj_alignment = ObjectAlignmentInBytes; 168 _classpath_entry_table_size = mapinfo->_classpath_entry_table_size; 169 _classpath_entry_table = mapinfo->_classpath_entry_table; 170 _classpath_entry_size = mapinfo->_classpath_entry_size; 171 172 // The following fields are for sanity checks for whether this archive 173 // will function correctly with this JVM and the bootclasspath it's 174 // invoked with. 175 176 // JVM version string ... changes on each build. 177 get_header_version(_jvm_ident); 178 } 179 180 void FileMapInfo::allocate_classpath_entry_table() { 181 int bytes = 0; 182 int count = 0; 183 char* strptr = NULL; 184 char* strptr_max = NULL; 185 Thread* THREAD = Thread::current(); 186 187 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data(); 188 size_t entry_size = SharedClassUtil::shared_class_path_entry_size(); 189 190 for (int pass=0; pass<2; pass++) { 191 ClassPathEntry *cpe = ClassLoader::classpath_entry(0); 192 193 for (int cur_entry = 0 ; cpe != NULL; cpe = cpe->next(), cur_entry++) { 194 const char *name = cpe->name(); 195 int name_bytes = (int)(strlen(name) + 1); 196 197 if (pass == 0) { 198 count ++; 199 bytes += (int)entry_size; 200 bytes += name_bytes; 201 if (TraceClassPaths || (TraceClassLoading && Verbose)) { 202 tty->print_cr("[Add main shared path (%s) %s]", (cpe->is_jar_file() ? "jar" : "dir"), name); 203 } 204 } else { 205 SharedClassPathEntry* ent = shared_classpath(cur_entry); 206 if (cpe->is_jar_file()) { 207 struct stat st; 208 if (os::stat(name, &st) != 0) { 209 // The file/dir must exist, or it would not have been added 210 // into ClassLoader::classpath_entry(). 211 // 212 // If we can't access a jar file in the boot path, then we can't 213 // make assumptions about where classes get loaded from. 214 FileMapInfo::fail_stop("Unable to open jar file %s.", name); 215 } 216 217 EXCEPTION_MARK; // The following call should never throw, but would exit VM on error. 218 SharedClassUtil::update_shared_classpath(cpe, ent, st.st_mtime, st.st_size, THREAD); 219 } else { 220 struct stat st; 221 if ((os::stat(name, &st) == 0) && ((st.st_mode & S_IFDIR) == S_IFDIR)) { 222 if (!os::dir_is_empty(name)) { 223 ClassLoader::exit_with_path_failure("Cannot have non-empty directory in archived classpaths", name); 224 } 225 ent->_filesize = -1; 226 } else { 227 ent->_filesize = -2; 228 } 229 } 230 ent->_name = strptr; 231 if (strptr + name_bytes <= strptr_max) { 232 strncpy(strptr, name, (size_t)name_bytes); // name_bytes includes trailing 0. 233 strptr += name_bytes; 234 } else { 235 assert(0, "miscalculated buffer size"); 236 } 237 } 238 } 239 240 if (pass == 0) { 241 EXCEPTION_MARK; // The following call should never throw, but would exit VM on error. 242 Array<u8>* arr = MetadataFactory::new_array<u8>(loader_data, (bytes + 7)/8, THREAD); 243 strptr = (char*)(arr->data()); 244 strptr_max = strptr + bytes; 245 SharedClassPathEntry* table = (SharedClassPathEntry*)strptr; 246 strptr += entry_size * count; 247 248 _classpath_entry_table_size = count; 249 _classpath_entry_table = table; 250 _classpath_entry_size = entry_size; 251 } 252 } 253 } 254 255 bool FileMapInfo::validate_classpath_entry_table() { 256 _validating_classpath_entry_table = true; 257 258 int count = _header->_classpath_entry_table_size; 259 260 _classpath_entry_table = _header->_classpath_entry_table; 261 _classpath_entry_size = _header->_classpath_entry_size; 262 263 for (int i=0; i<count; i++) { 264 SharedClassPathEntry* ent = shared_classpath(i); 265 struct stat st; 266 const char* name = ent->_name; 267 bool ok = true; 268 if (TraceClassPaths || (TraceClassLoading && Verbose)) { 269 tty->print_cr("[Checking shared classpath entry: %s]", name); 270 } 271 if (os::stat(name, &st) != 0) { 272 fail_continue("Required classpath entry does not exist: %s", name); 273 ok = false; 274 } else if (ent->is_dir()) { 275 if (!os::dir_is_empty(name)) { 276 fail_continue("directory is not empty: %s", name); 277 ok = false; 278 } 279 } else if (ent->is_jar()) { 280 if (ent->_timestamp != st.st_mtime || 281 ent->_filesize != st.st_size) { 282 ok = false; 283 if (PrintSharedArchiveAndExit) { 284 fail_continue(ent->_timestamp != st.st_mtime ? 285 "Timestamp mismatch" : 286 "File size mismatch"); 287 } else { 288 fail_continue("A jar file is not the one used while building" 289 " the shared archive file: %s", name); 290 } 291 } 292 } 293 if (ok) { 294 if (TraceClassPaths || (TraceClassLoading && Verbose)) { 295 tty->print_cr("[ok]"); 296 } 297 } else if (!PrintSharedArchiveAndExit) { 298 _validating_classpath_entry_table = false; 299 return false; 300 } 301 } 302 303 _classpath_entry_table_size = _header->_classpath_entry_table_size; 304 _validating_classpath_entry_table = false; 305 return true; 306 } 307 308 309 // Read the FileMapInfo information from the file. 310 311 bool FileMapInfo::init_from_file(int fd) { 312 size_t sz = _header->data_size(); 313 char* addr = _header->data(); 314 size_t n = os::read(fd, addr, (unsigned int)sz); 315 if (n != sz) { 316 fail_continue("Unable to read the file header."); 317 return false; 318 } 319 if (_header->_version != current_version()) { 320 fail_continue("The shared archive file has the wrong version."); 321 return false; 322 } 323 _file_offset = (long)n; 324 325 size_t info_size = _header->_paths_misc_info_size; 326 _paths_misc_info = NEW_C_HEAP_ARRAY_RETURN_NULL(char, info_size, mtClass); 327 if (_paths_misc_info == NULL) { 328 fail_continue("Unable to read the file header."); 329 return false; 330 } 331 n = os::read(fd, _paths_misc_info, (unsigned int)info_size); 332 if (n != info_size) { 333 fail_continue("Unable to read the shared path info header."); 334 FREE_C_HEAP_ARRAY(char, _paths_misc_info); 335 _paths_misc_info = NULL; 336 return false; 337 } 338 339 size_t len = lseek(fd, 0, SEEK_END); 340 struct FileMapInfo::FileMapHeader::space_info* si = 341 &_header->_space[MetaspaceShared::mc]; 342 if (si->_file_offset >= len || len - si->_file_offset < si->_used) { 343 fail_continue("The shared archive file has been truncated."); 344 return false; 345 } 346 347 _file_offset += (long)n; 348 return true; 349 } 350 351 352 // Read the FileMapInfo information from the file. 353 bool FileMapInfo::open_for_read() { 354 _full_path = Arguments::GetSharedArchivePath(); 355 int fd = open(_full_path, O_RDONLY | O_BINARY, 0); 356 if (fd < 0) { 357 if (errno == ENOENT) { 358 // Not locating the shared archive is ok. 359 fail_continue("Specified shared archive not found."); 360 } else { 361 fail_continue("Failed to open shared archive file (%s).", 362 strerror(errno)); 363 } 364 return false; 365 } 366 367 _fd = fd; 368 _file_open = true; 369 return true; 370 } 371 372 373 // Write the FileMapInfo information to the file. 374 375 void FileMapInfo::open_for_write() { 376 _full_path = Arguments::GetSharedArchivePath(); 377 if (PrintSharedSpaces) { 378 tty->print_cr("Dumping shared data to file: "); 379 tty->print_cr(" %s", _full_path); 380 } 381 382 #ifdef _WINDOWS // On Windows, need WRITE permission to remove the file. 383 chmod(_full_path, _S_IREAD | _S_IWRITE); 384 #endif 385 386 // Use remove() to delete the existing file because, on Unix, this will 387 // allow processes that have it open continued access to the file. 388 remove(_full_path); 389 int fd = open(_full_path, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0444); 390 if (fd < 0) { 391 fail_stop("Unable to create shared archive file %s: %s.", _full_path, 392 strerror(errno)); 393 } 394 _fd = fd; 395 _file_offset = 0; 396 _file_open = true; 397 } 398 399 400 // Write the header to the file, seek to the next allocation boundary. 401 402 void FileMapInfo::write_header() { 403 int info_size = ClassLoader::get_shared_paths_misc_info_size(); 404 405 _header->_paths_misc_info_size = info_size; 406 407 align_file_position(); 408 size_t sz = _header->data_size(); 409 char* addr = _header->data(); 410 write_bytes(addr, (int)sz); // skip the C++ vtable 411 write_bytes(ClassLoader::get_shared_paths_misc_info(), info_size); 412 align_file_position(); 413 } 414 415 416 // Dump shared spaces to file. 417 418 void FileMapInfo::write_space(int i, Metaspace* space, bool read_only) { 419 align_file_position(); 420 size_t used = space->used_bytes_slow(Metaspace::NonClassType); 421 size_t capacity = space->capacity_bytes_slow(Metaspace::NonClassType); 422 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i]; 423 write_region(i, (char*)space->bottom(), used, capacity, read_only, false); 424 } 425 426 427 // Dump region to file. 428 429 void FileMapInfo::write_region(int region, char* base, size_t size, 430 size_t capacity, bool read_only, 431 bool allow_exec) { 432 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[region]; 433 434 if (_file_open) { 435 guarantee(si->_file_offset == _file_offset, "file offset mismatch."); 436 if (PrintSharedSpaces) { 437 tty->print_cr("Shared file region %d: 0x%6x bytes, addr " INTPTR_FORMAT 438 " file offset 0x%6x", region, size, base, _file_offset); 439 } 440 } else { 441 si->_file_offset = _file_offset; 442 } 443 si->_base = base; 444 si->_used = size; 445 si->_capacity = capacity; 446 si->_read_only = read_only; 447 si->_allow_exec = allow_exec; 448 si->_crc = ClassLoader::crc32(0, base, (jint)size); 449 write_bytes_aligned(base, (int)size); 450 } 451 452 453 // Dump bytes to file -- at the current file position. 454 455 void FileMapInfo::write_bytes(const void* buffer, int nbytes) { 456 if (_file_open) { 457 int n = ::write(_fd, buffer, nbytes); 458 if (n != nbytes) { 459 // It is dangerous to leave the corrupted shared archive file around, 460 // close and remove the file. See bug 6372906. 461 close(); 462 remove(_full_path); 463 fail_stop("Unable to write to shared archive file."); 464 } 465 } 466 _file_offset += nbytes; 467 } 468 469 470 // Align file position to an allocation unit boundary. 471 472 void FileMapInfo::align_file_position() { 473 size_t new_file_offset = align_size_up(_file_offset, 474 os::vm_allocation_granularity()); 475 if (new_file_offset != _file_offset) { 476 _file_offset = new_file_offset; 477 if (_file_open) { 478 // Seek one byte back from the target and write a byte to insure 479 // that the written file is the correct length. 480 _file_offset -= 1; 481 if (lseek(_fd, (long)_file_offset, SEEK_SET) < 0) { 482 fail_stop("Unable to seek."); 483 } 484 char zero = 0; 485 write_bytes(&zero, 1); 486 } 487 } 488 } 489 490 491 // Dump bytes to file -- at the current file position. 492 493 void FileMapInfo::write_bytes_aligned(const void* buffer, int nbytes) { 494 align_file_position(); 495 write_bytes(buffer, nbytes); 496 align_file_position(); 497 } 498 499 500 // Close the shared archive file. This does NOT unmap mapped regions. 501 502 void FileMapInfo::close() { 503 if (_file_open) { 504 if (::close(_fd) < 0) { 505 fail_stop("Unable to close the shared archive file."); 506 } 507 _file_open = false; 508 _fd = -1; 509 } 510 } 511 512 513 // JVM/TI RedefineClasses() support: 514 // Remap the shared readonly space to shared readwrite, private. 515 bool FileMapInfo::remap_shared_readonly_as_readwrite() { 516 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[0]; 517 if (!si->_read_only) { 518 // the space is already readwrite so we are done 519 return true; 520 } 521 size_t used = si->_used; 522 size_t size = align_size_up(used, os::vm_allocation_granularity()); 523 if (!open_for_read()) { 524 return false; 525 } 526 char *base = os::remap_memory(_fd, _full_path, si->_file_offset, 527 si->_base, size, false /* !read_only */, 528 si->_allow_exec); 529 close(); 530 if (base == NULL) { 531 fail_continue("Unable to remap shared readonly space (errno=%d).", errno); 532 return false; 533 } 534 if (base != si->_base) { 535 fail_continue("Unable to remap shared readonly space at required address."); 536 return false; 537 } 538 si->_read_only = false; 539 return true; 540 } 541 542 // Map the whole region at once, assumed to be allocated contiguously. 543 ReservedSpace FileMapInfo::reserve_shared_memory() { 544 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[0]; 545 char* requested_addr = si->_base; 546 547 size_t size = FileMapInfo::shared_spaces_size(); 548 549 // Reserve the space first, then map otherwise map will go right over some 550 // other reserved memory (like the code cache). 551 ReservedSpace rs(size, os::vm_allocation_granularity(), false, requested_addr); 552 if (!rs.is_reserved()) { 553 fail_continue("Unable to reserve shared space at required address " INTPTR_FORMAT, requested_addr); 554 return rs; 555 } 556 // the reserved virtual memory is for mapping class data sharing archive 557 MemTracker::record_virtual_memory_type((address)rs.base(), mtClassShared); 558 559 return rs; 560 } 561 562 // Memory map a region in the address space. 563 static const char* shared_region_name[] = { "ReadOnly", "ReadWrite", "MiscData", "MiscCode"}; 564 565 char* FileMapInfo::map_region(int i) { 566 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i]; 567 size_t used = si->_used; 568 size_t alignment = os::vm_allocation_granularity(); 569 size_t size = align_size_up(used, alignment); 570 char *requested_addr = si->_base; 571 572 // map the contents of the CDS archive in this memory 573 char *base = os::map_memory(_fd, _full_path, si->_file_offset, 574 requested_addr, size, si->_read_only, 575 si->_allow_exec); 576 if (base == NULL || base != si->_base) { 577 fail_continue("Unable to map %s shared space at required address.", shared_region_name[i]); 578 return NULL; 579 } 580 #ifdef _WINDOWS 581 // This call is Windows-only because the memory_type gets recorded for the other platforms 582 // in method FileMapInfo::reserve_shared_memory(), which is not called on Windows. 583 MemTracker::record_virtual_memory_type((address)base, mtClassShared); 584 #endif 585 return base; 586 } 587 588 bool FileMapInfo::verify_region_checksum(int i) { 589 if (!VerifySharedSpaces) { 590 return true; 591 } 592 const char* buf = _header->_space[i]._base; 593 size_t sz = _header->_space[i]._used; 594 int crc = ClassLoader::crc32(0, buf, (jint)sz); 595 if (crc != _header->_space[i]._crc) { 596 fail_continue("Checksum verification failed."); 597 return false; 598 } 599 return true; 600 } 601 602 // Unmap a memory region in the address space. 603 604 void FileMapInfo::unmap_region(int i) { 605 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i]; 606 size_t used = si->_used; 607 size_t size = align_size_up(used, os::vm_allocation_granularity()); 608 if (!os::unmap_memory(si->_base, size)) { 609 fail_stop("Unable to unmap shared space."); 610 } 611 } 612 613 614 void FileMapInfo::assert_mark(bool check) { 615 if (!check) { 616 fail_stop("Mark mismatch while restoring from shared file."); 617 } 618 } 619 620 621 FileMapInfo* FileMapInfo::_current_info = NULL; 622 SharedClassPathEntry* FileMapInfo::_classpath_entry_table = NULL; 623 int FileMapInfo::_classpath_entry_table_size = 0; 624 size_t FileMapInfo::_classpath_entry_size = 0x1234baad; 625 bool FileMapInfo::_validating_classpath_entry_table = false; 626 627 // Open the shared archive file, read and validate the header 628 // information (version, boot classpath, etc.). If initialization 629 // fails, shared spaces are disabled and the file is closed. [See 630 // fail_continue.] 631 // 632 // Validation of the archive is done in two steps: 633 // 634 // [1] validate_header() - done here. This checks the header, including _paths_misc_info. 635 // [2] validate_classpath_entry_table - this is done later, because the table is in the RW 636 // region of the archive, which is not mapped yet. 637 bool FileMapInfo::initialize() { 638 assert(UseSharedSpaces, "UseSharedSpaces expected."); 639 640 if (JvmtiExport::can_modify_any_class() || JvmtiExport::can_walk_any_space()) { 641 fail_continue("Tool agent requires sharing to be disabled."); 642 return false; 643 } 644 645 if (!open_for_read()) { 646 return false; 647 } 648 649 init_from_file(_fd); 650 if (!validate_header()) { 651 return false; 652 } 653 654 SharedReadOnlySize = _header->_space[0]._capacity; 655 SharedReadWriteSize = _header->_space[1]._capacity; 656 SharedMiscDataSize = _header->_space[2]._capacity; 657 SharedMiscCodeSize = _header->_space[3]._capacity; 658 return true; 659 } 660 661 int FileMapInfo::FileMapHeader::compute_crc() { 662 char* header = data(); 663 // start computing from the field after _crc 664 char* buf = (char*)&_crc + sizeof(int); 665 size_t sz = data_size() - (buf - header); 666 int crc = ClassLoader::crc32(0, buf, (jint)sz); 667 return crc; 668 } 669 670 bool FileMapInfo::FileMapHeader::validate() { 671 if (VerifySharedSpaces && compute_crc() != _crc) { 672 fail_continue("Header checksum verification failed."); 673 return false; 674 } 675 676 if (_version != current_version()) { 677 FileMapInfo::fail_continue("The shared archive file is the wrong version."); 678 return false; 679 } 680 if (_magic != (int)0xf00baba2) { 681 FileMapInfo::fail_continue("The shared archive file has a bad magic number."); 682 return false; 683 } 684 char header_version[JVM_IDENT_MAX]; 685 get_header_version(header_version); 686 if (strncmp(_jvm_ident, header_version, JVM_IDENT_MAX-1) != 0) { 687 if (TraceClassPaths) { 688 tty->print_cr("Expected: %s", header_version); 689 tty->print_cr("Actual: %s", _jvm_ident); 690 } 691 FileMapInfo::fail_continue("The shared archive file was created by a different" 692 " version or build of HotSpot"); 693 return false; 694 } 695 if (_obj_alignment != ObjectAlignmentInBytes) { 696 FileMapInfo::fail_continue("The shared archive file's ObjectAlignmentInBytes of %d" 697 " does not equal the current ObjectAlignmentInBytes of %d.", 698 _obj_alignment, ObjectAlignmentInBytes); 699 return false; 700 } 701 702 return true; 703 } 704 705 bool FileMapInfo::validate_header() { 706 bool status = _header->validate(); 707 708 if (status) { 709 if (!ClassLoader::check_shared_paths_misc_info(_paths_misc_info, _header->_paths_misc_info_size)) { 710 if (!PrintSharedArchiveAndExit) { 711 fail_continue("shared class paths mismatch (hint: enable -XX:+TraceClassPaths to diagnose the failure)"); 712 status = false; 713 } 714 } 715 } 716 717 if (_paths_misc_info != NULL) { 718 FREE_C_HEAP_ARRAY(char, _paths_misc_info); 719 _paths_misc_info = NULL; 720 } 721 return status; 722 } 723 724 // The following method is provided to see whether a given pointer 725 // falls in the mapped shared space. 726 // Param: 727 // p, The given pointer 728 // Return: 729 // True if the p is within the mapped shared space, otherwise, false. 730 bool FileMapInfo::is_in_shared_space(const void* p) { 731 for (int i = 0; i < MetaspaceShared::n_regions; i++) { 732 if (p >= _header->_space[i]._base && 733 p < _header->_space[i]._base + _header->_space[i]._used) { 734 return true; 735 } 736 } 737 738 return false; 739 } 740 741 void FileMapInfo::print_shared_spaces() { 742 gclog_or_tty->print_cr("Shared Spaces:"); 743 for (int i = 0; i < MetaspaceShared::n_regions; i++) { 744 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i]; 745 gclog_or_tty->print(" %s " INTPTR_FORMAT "-" INTPTR_FORMAT, 746 shared_region_name[i], 747 si->_base, si->_base + si->_used); 748 } 749 } 750 751 // Unmap mapped regions of shared space. 752 void FileMapInfo::stop_sharing_and_unmap(const char* msg) { 753 FileMapInfo *map_info = FileMapInfo::current_info(); 754 if (map_info) { 755 map_info->fail_continue("%s", msg); 756 for (int i = 0; i < MetaspaceShared::n_regions; i++) { 757 if (map_info->_header->_space[i]._base != NULL) { 758 map_info->unmap_region(i); 759 map_info->_header->_space[i]._base = NULL; 760 } 761 } 762 } else if (DumpSharedSpaces) { 763 fail_stop("%s", msg); 764 } 765 }