duke@435: /* drchase@6680: * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "classfile/classLoader.hpp" iklam@7089: #include "classfile/sharedClassUtil.hpp" stefank@2314: #include "classfile/symbolTable.hpp" iklam@7089: #include "classfile/systemDictionaryShared.hpp" hseigel@4293: #include "classfile/altHashing.hpp" stefank@2314: #include "memory/filemap.hpp" iklam@7089: #include "memory/metadataFactory.hpp" iklam@7089: #include "memory/oopFactory.hpp" iklam@7089: #include "oops/objArrayOop.hpp" stefank@2314: #include "runtime/arguments.hpp" stefank@2314: #include "runtime/java.hpp" stefank@2314: #include "runtime/os.hpp" zgu@4193: #include "services/memTracker.hpp" stefank@2314: #include "utilities/defaultStream.hpp" stefank@2314: duke@435: # include duke@435: # include duke@435: duke@435: #ifndef O_BINARY // if defined (Win32) use binary files. duke@435: #define O_BINARY 0 // otherwise do nothing. duke@435: #endif duke@435: drchase@6680: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC duke@435: extern address JVM_FunctionAtStart(); duke@435: extern address JVM_FunctionAtEnd(); duke@435: twisti@1040: // Complain and stop. All error conditions occurring during the writing of duke@435: // an archive file should stop the process. Unrecoverable errors during duke@435: // the reading of the archive file should stop the process. duke@435: duke@435: static void fail(const char *msg, va_list ap) { duke@435: // This occurs very early during initialization: tty is not initialized. duke@435: jio_fprintf(defaultStream::error_stream(), twisti@1040: "An error has occurred while processing the" duke@435: " shared archive file.\n"); duke@435: jio_vfprintf(defaultStream::error_stream(), msg, ap); duke@435: jio_fprintf(defaultStream::error_stream(), "\n"); hseigel@5625: // Do not change the text of the below message because some tests check for it. duke@435: vm_exit_during_initialization("Unable to use shared archive.", NULL); duke@435: } duke@435: duke@435: duke@435: void FileMapInfo::fail_stop(const char *msg, ...) { duke@435: va_list ap; duke@435: va_start(ap, msg); duke@435: fail(msg, ap); // Never returns. duke@435: va_end(ap); // for completeness. duke@435: } duke@435: duke@435: duke@435: // Complain and continue. Recoverable errors during the reading of the duke@435: // archive file may continue (with sharing disabled). duke@435: // duke@435: // If we continue, then disable shared spaces and close the file. duke@435: duke@435: void FileMapInfo::fail_continue(const char *msg, ...) { duke@435: va_list ap; duke@435: va_start(ap, msg); iklam@7089: MetaspaceShared::set_archive_loading_failed(); iklam@7089: if (PrintSharedArchiveAndExit && _validating_classpath_entry_table) { iklam@7089: // If we are doing PrintSharedArchiveAndExit and some of the classpath entries iklam@7089: // do not validate, we can still continue "limping" to validate the remaining iklam@7089: // entries. No need to quit. iklam@7089: tty->print("["); iklam@7089: tty->vprint(msg, ap); iklam@7089: tty->print_cr("]"); iklam@7089: } else { iklam@7089: if (RequireSharedSpaces) { iklam@7089: fail(msg, ap); iklam@7089: } else { iklam@7089: if (PrintSharedSpaces) { iklam@7089: tty->print_cr("UseSharedSpaces: %s", msg); iklam@7089: } iklam@7089: } iklam@7414: UseSharedSpaces = false; iklam@7414: assert(current_info() != NULL, "singleton must be registered"); iklam@7414: current_info()->close(); duke@435: } duke@435: va_end(ap); duke@435: } duke@435: hseigel@4293: // Fill in the fileMapInfo structure with data about this VM instance. duke@435: hseigel@4293: // This method copies the vm version info into header_version. If the version is too hseigel@4293: // long then a truncated version, which has a hash code appended to it, is copied. hseigel@4293: // hseigel@4293: // Using a template enables this method to verify that header_version is an array of hseigel@4293: // length JVM_IDENT_MAX. This ensures that the code that writes to the CDS file and hseigel@4293: // the code that reads the CDS file will both use the same size buffer. Hence, will hseigel@4293: // use identical truncation. This is necessary for matching of truncated versions. hseigel@4293: template static void get_header_version(char (&header_version) [N]) { hseigel@4293: assert(N == JVM_IDENT_MAX, "Bad header_version size"); hseigel@4293: hseigel@4293: const char *vm_version = VM_Version::internal_vm_info_string(); hseigel@4293: const int version_len = (int)strlen(vm_version); hseigel@4293: hseigel@4293: if (version_len < (JVM_IDENT_MAX-1)) { hseigel@4293: strcpy(header_version, vm_version); hseigel@4293: hseigel@4293: } else { hseigel@4293: // Get the hash value. Use a static seed because the hash needs to return the same hseigel@4293: // value over multiple jvm invocations. hseigel@4293: unsigned int hash = AltHashing::murmur3_32(8191, (const jbyte*)vm_version, version_len); hseigel@4293: hseigel@4293: // Truncate the ident, saving room for the 8 hex character hash value. hseigel@4293: strncpy(header_version, vm_version, JVM_IDENT_MAX-9); hseigel@4293: hseigel@4293: // Append the hash code as eight hex digits. hseigel@4293: sprintf(&header_version[JVM_IDENT_MAX-9], "%08x", hash); hseigel@4293: header_version[JVM_IDENT_MAX-1] = 0; // Null terminate. hseigel@4293: } hseigel@4293: } duke@435: iklam@7089: FileMapInfo::FileMapInfo() { iklam@7089: assert(_current_info == NULL, "must be singleton"); // not thread safe iklam@7089: _current_info = this; iklam@7089: memset(this, 0, sizeof(FileMapInfo)); iklam@7089: _file_offset = 0; iklam@7089: _file_open = false; iklam@7089: _header = SharedClassUtil::allocate_file_map_header(); iklam@7089: _header->_version = _invalid_version; iklam@7089: } iklam@7089: iklam@7089: FileMapInfo::~FileMapInfo() { iklam@7089: assert(_current_info == this, "must be singleton"); // not thread safe iklam@7089: _current_info = NULL; iklam@7089: } iklam@7089: duke@435: void FileMapInfo::populate_header(size_t alignment) { iklam@7089: _header->populate(this, alignment); iklam@7089: } iklam@7089: iklam@7089: size_t FileMapInfo::FileMapHeader::data_size() { iklam@7089: return SharedClassUtil::file_map_header_size() - sizeof(FileMapInfo::FileMapHeaderBase); iklam@7089: } iklam@7089: iklam@7089: void FileMapInfo::FileMapHeader::populate(FileMapInfo* mapinfo, size_t alignment) { iklam@7089: _magic = 0xf00baba2; iklam@7089: _version = _current_version; iklam@7089: _alignment = alignment; iklam@7089: _obj_alignment = ObjectAlignmentInBytes; iklam@7089: _classpath_entry_table_size = mapinfo->_classpath_entry_table_size; iklam@7089: _classpath_entry_table = mapinfo->_classpath_entry_table; iklam@7089: _classpath_entry_size = mapinfo->_classpath_entry_size; duke@435: duke@435: // The following fields are for sanity checks for whether this archive duke@435: // will function correctly with this JVM and the bootclasspath it's duke@435: // invoked with. duke@435: duke@435: // JVM version string ... changes on each build. iklam@7089: get_header_version(_jvm_ident); iklam@7089: } duke@435: iklam@7089: void FileMapInfo::allocate_classpath_entry_table() { iklam@7089: int bytes = 0; iklam@7089: int count = 0; iklam@7089: char* strptr = NULL; iklam@7089: char* strptr_max = NULL; iklam@7089: Thread* THREAD = Thread::current(); duke@435: iklam@7089: ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data(); iklam@7089: size_t entry_size = SharedClassUtil::shared_class_path_entry_size(); iklam@7089: iklam@7089: for (int pass=0; pass<2; pass++) { iklam@7089: ClassPathEntry *cpe = ClassLoader::classpath_entry(0); iklam@7089: iklam@7089: for (int cur_entry = 0 ; cpe != NULL; cpe = cpe->next(), cur_entry++) { iklam@7089: const char *name = cpe->name(); iklam@7089: int name_bytes = (int)(strlen(name) + 1); iklam@7089: iklam@7089: if (pass == 0) { iklam@7089: count ++; iklam@7089: bytes += (int)entry_size; iklam@7089: bytes += name_bytes; iklam@7089: if (TraceClassPaths || (TraceClassLoading && Verbose)) { iklam@7089: tty->print_cr("[Add main shared path (%s) %s]", (cpe->is_jar_file() ? "jar" : "dir"), name); iklam@7089: } iklam@7089: } else { iklam@7089: SharedClassPathEntry* ent = shared_classpath(cur_entry); iklam@7089: if (cpe->is_jar_file()) { iklam@7089: struct stat st; iklam@7089: if (os::stat(name, &st) != 0) { iklam@7089: // The file/dir must exist, or it would not have been added iklam@7089: // into ClassLoader::classpath_entry(). iklam@7089: // iklam@7089: // If we can't access a jar file in the boot path, then we can't iklam@7089: // make assumptions about where classes get loaded from. iklam@7089: FileMapInfo::fail_stop("Unable to open jar file %s.", name); iklam@7089: } iklam@7089: iklam@7089: EXCEPTION_MARK; // The following call should never throw, but would exit VM on error. iklam@7089: SharedClassUtil::update_shared_classpath(cpe, ent, st.st_mtime, st.st_size, THREAD); iklam@7089: } else { iklam@7089: ent->_filesize = -1; iklam@7089: if (!os::dir_is_empty(name)) { iklam@7089: ClassLoader::exit_with_path_failure("Cannot have non-empty directory in archived classpaths", name); iklam@7089: } iklam@7089: } iklam@7089: ent->_name = strptr; iklam@7089: if (strptr + name_bytes <= strptr_max) { iklam@7089: strncpy(strptr, name, (size_t)name_bytes); // name_bytes includes trailing 0. iklam@7089: strptr += name_bytes; iklam@7089: } else { iklam@7089: assert(0, "miscalculated buffer size"); iklam@7089: } duke@435: } iklam@7089: } duke@435: iklam@7089: if (pass == 0) { iklam@7089: EXCEPTION_MARK; // The following call should never throw, but would exit VM on error. iklam@7089: Array* arr = MetadataFactory::new_array(loader_data, (bytes + 7)/8, THREAD); iklam@7089: strptr = (char*)(arr->data()); iklam@7089: strptr_max = strptr + bytes; iklam@7089: SharedClassPathEntry* table = (SharedClassPathEntry*)strptr; iklam@7089: strptr += entry_size * count; duke@435: iklam@7089: _classpath_entry_table_size = count; iklam@7089: _classpath_entry_table = table; iklam@7089: _classpath_entry_size = entry_size; duke@435: } duke@435: } duke@435: } duke@435: iklam@7089: bool FileMapInfo::validate_classpath_entry_table() { iklam@7089: _validating_classpath_entry_table = true; iklam@7089: iklam@7089: int count = _header->_classpath_entry_table_size; iklam@7089: iklam@7089: _classpath_entry_table = _header->_classpath_entry_table; iklam@7089: _classpath_entry_size = _header->_classpath_entry_size; iklam@7089: iklam@7089: for (int i=0; i_name; iklam@7089: bool ok = true; iklam@7089: if (TraceClassPaths || (TraceClassLoading && Verbose)) { iklam@7089: tty->print_cr("[Checking shared classpath entry: %s]", name); iklam@7089: } iklam@7089: if (os::stat(name, &st) != 0) { iklam@7089: fail_continue("Required classpath entry does not exist: %s", name); iklam@7089: ok = false; iklam@7089: } else if (ent->is_dir()) { iklam@7089: if (!os::dir_is_empty(name)) { iklam@7089: fail_continue("directory is not empty: %s", name); iklam@7089: ok = false; iklam@7089: } iklam@7089: } else { iklam@7089: if (ent->_timestamp != st.st_mtime || iklam@7089: ent->_filesize != st.st_size) { iklam@7089: ok = false; iklam@7089: if (PrintSharedArchiveAndExit) { iklam@7089: fail_continue(ent->_timestamp != st.st_mtime ? iklam@7089: "Timestamp mismatch" : iklam@7089: "File size mismatch"); iklam@7089: } else { iklam@7089: fail_continue("A jar file is not the one used while building" iklam@7089: " the shared archive file: %s", name); iklam@7089: } iklam@7089: } iklam@7089: } iklam@7089: if (ok) { iklam@7089: if (TraceClassPaths || (TraceClassLoading && Verbose)) { iklam@7089: tty->print_cr("[ok]"); iklam@7089: } iklam@7089: } else if (!PrintSharedArchiveAndExit) { iklam@7089: _validating_classpath_entry_table = false; iklam@7089: return false; iklam@7089: } iklam@7089: } iklam@7089: iklam@7089: _classpath_entry_table_size = _header->_classpath_entry_table_size; iklam@7089: _validating_classpath_entry_table = false; iklam@7089: return true; iklam@7089: } iklam@7089: duke@435: duke@435: // Read the FileMapInfo information from the file. duke@435: duke@435: bool FileMapInfo::init_from_file(int fd) { iklam@7089: size_t sz = _header->data_size(); iklam@7089: char* addr = _header->data(); iklam@7089: size_t n = os::read(fd, addr, (unsigned int)sz); iklam@7089: if (n != sz) { duke@435: fail_continue("Unable to read the file header."); duke@435: return false; duke@435: } iklam@7089: if (_header->_version != current_version()) { duke@435: fail_continue("The shared archive file has the wrong version."); duke@435: return false; duke@435: } iklam@7089: iklam@7089: size_t info_size = _header->_paths_misc_info_size; iklam@7089: _paths_misc_info = NEW_C_HEAP_ARRAY_RETURN_NULL(char, info_size, mtClass); iklam@7089: if (_paths_misc_info == NULL) { iklam@7089: fail_continue("Unable to read the file header."); iklam@7089: return false; iklam@7089: } iklam@7089: n = os::read(fd, _paths_misc_info, (unsigned int)info_size); iklam@7089: if (n != info_size) { iklam@7089: fail_continue("Unable to read the shared path info header."); iklam@7089: FREE_C_HEAP_ARRAY(char, _paths_misc_info, mtClass); iklam@7089: _paths_misc_info = NULL; iklam@7089: return false; iklam@7089: } iklam@7089: jiangli@6868: size_t len = lseek(fd, 0, SEEK_END); jiangli@6868: struct FileMapInfo::FileMapHeader::space_info* si = jiangli@7241: &_header->_space[MetaspaceShared::mc]; jiangli@6868: if (si->_file_offset >= len || len - si->_file_offset < si->_used) { jiangli@6868: fail_continue("The shared archive file has been truncated."); jiangli@6868: return false; jiangli@6868: } jiangli@7241: iklam@7089: _file_offset += (long)n; duke@435: return true; duke@435: } duke@435: duke@435: duke@435: // Read the FileMapInfo information from the file. duke@435: bool FileMapInfo::open_for_read() { duke@435: _full_path = Arguments::GetSharedArchivePath(); duke@435: int fd = open(_full_path, O_RDONLY | O_BINARY, 0); duke@435: if (fd < 0) { duke@435: if (errno == ENOENT) { duke@435: // Not locating the shared archive is ok. duke@435: fail_continue("Specified shared archive not found."); duke@435: } else { duke@435: fail_continue("Failed to open shared archive file (%s).", duke@435: strerror(errno)); duke@435: } duke@435: return false; duke@435: } duke@435: duke@435: _fd = fd; duke@435: _file_open = true; duke@435: return true; duke@435: } duke@435: duke@435: duke@435: // Write the FileMapInfo information to the file. duke@435: duke@435: void FileMapInfo::open_for_write() { duke@435: _full_path = Arguments::GetSharedArchivePath(); duke@435: if (PrintSharedSpaces) { duke@435: tty->print_cr("Dumping shared data to file: "); duke@435: tty->print_cr(" %s", _full_path); duke@435: } duke@435: hseigel@4521: #ifdef _WINDOWS // On Windows, need WRITE permission to remove the file. hseigel@4521: chmod(_full_path, _S_IREAD | _S_IWRITE); hseigel@4521: #endif hseigel@4521: hseigel@4521: // Use remove() to delete the existing file because, on Unix, this will hseigel@4521: // allow processes that have it open continued access to the file. duke@435: remove(_full_path); duke@435: int fd = open(_full_path, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0444); duke@435: if (fd < 0) { duke@435: fail_stop("Unable to create shared archive file %s.", _full_path); duke@435: } duke@435: _fd = fd; duke@435: _file_offset = 0; duke@435: _file_open = true; duke@435: } duke@435: duke@435: duke@435: // Write the header to the file, seek to the next allocation boundary. duke@435: duke@435: void FileMapInfo::write_header() { iklam@7089: int info_size = ClassLoader::get_shared_paths_misc_info_size(); iklam@7089: iklam@7089: _header->_paths_misc_info_size = info_size; iklam@7089: iklam@7089: align_file_position(); iklam@7089: size_t sz = _header->data_size(); iklam@7089: char* addr = _header->data(); iklam@7089: write_bytes(addr, (int)sz); // skip the C++ vtable iklam@7089: write_bytes(ClassLoader::get_shared_paths_misc_info(), info_size); iklam@7089: align_file_position(); duke@435: } duke@435: duke@435: duke@435: // Dump shared spaces to file. duke@435: coleenp@4037: void FileMapInfo::write_space(int i, Metaspace* space, bool read_only) { duke@435: align_file_position(); jmasa@5015: size_t used = space->used_bytes_slow(Metaspace::NonClassType); jmasa@5015: size_t capacity = space->capacity_bytes_slow(Metaspace::NonClassType); iklam@7089: struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i]; coleenp@4037: write_region(i, (char*)space->bottom(), used, capacity, read_only, false); duke@435: } duke@435: duke@435: duke@435: // Dump region to file. duke@435: duke@435: void FileMapInfo::write_region(int region, char* base, size_t size, duke@435: size_t capacity, bool read_only, duke@435: bool allow_exec) { iklam@7089: struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[region]; duke@435: duke@435: if (_file_open) { duke@435: guarantee(si->_file_offset == _file_offset, "file offset mismatch."); duke@435: if (PrintSharedSpaces) { coleenp@4037: tty->print_cr("Shared file region %d: 0x%6x bytes, addr " INTPTR_FORMAT coleenp@4037: " file offset 0x%6x", region, size, base, _file_offset); duke@435: } duke@435: } else { duke@435: si->_file_offset = _file_offset; duke@435: } duke@435: si->_base = base; duke@435: si->_used = size; duke@435: si->_capacity = capacity; duke@435: si->_read_only = read_only; duke@435: si->_allow_exec = allow_exec; jiangli@6868: si->_crc = ClassLoader::crc32(0, base, (jint)size); duke@435: write_bytes_aligned(base, (int)size); duke@435: } duke@435: duke@435: duke@435: // Dump bytes to file -- at the current file position. duke@435: duke@435: void FileMapInfo::write_bytes(const void* buffer, int nbytes) { duke@435: if (_file_open) { duke@435: int n = ::write(_fd, buffer, nbytes); duke@435: if (n != nbytes) { duke@435: // It is dangerous to leave the corrupted shared archive file around, duke@435: // close and remove the file. See bug 6372906. duke@435: close(); duke@435: remove(_full_path); duke@435: fail_stop("Unable to write to shared archive file.", NULL); duke@435: } duke@435: } duke@435: _file_offset += nbytes; duke@435: } duke@435: duke@435: duke@435: // Align file position to an allocation unit boundary. duke@435: duke@435: void FileMapInfo::align_file_position() { jiangli@6868: size_t new_file_offset = align_size_up(_file_offset, jiangli@6868: os::vm_allocation_granularity()); duke@435: if (new_file_offset != _file_offset) { duke@435: _file_offset = new_file_offset; duke@435: if (_file_open) { duke@435: // Seek one byte back from the target and write a byte to insure duke@435: // that the written file is the correct length. duke@435: _file_offset -= 1; jiangli@6868: if (lseek(_fd, (long)_file_offset, SEEK_SET) < 0) { duke@435: fail_stop("Unable to seek.", NULL); duke@435: } duke@435: char zero = 0; duke@435: write_bytes(&zero, 1); duke@435: } duke@435: } duke@435: } duke@435: duke@435: duke@435: // Dump bytes to file -- at the current file position. duke@435: duke@435: void FileMapInfo::write_bytes_aligned(const void* buffer, int nbytes) { duke@435: align_file_position(); duke@435: write_bytes(buffer, nbytes); duke@435: align_file_position(); duke@435: } duke@435: duke@435: duke@435: // Close the shared archive file. This does NOT unmap mapped regions. duke@435: duke@435: void FileMapInfo::close() { duke@435: if (_file_open) { duke@435: if (::close(_fd) < 0) { duke@435: fail_stop("Unable to close the shared archive file."); duke@435: } duke@435: _file_open = false; duke@435: _fd = -1; duke@435: } duke@435: } duke@435: duke@435: duke@435: // JVM/TI RedefineClasses() support: duke@435: // Remap the shared readonly space to shared readwrite, private. duke@435: bool FileMapInfo::remap_shared_readonly_as_readwrite() { iklam@7089: struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[0]; duke@435: if (!si->_read_only) { duke@435: // the space is already readwrite so we are done duke@435: return true; duke@435: } duke@435: size_t used = si->_used; duke@435: size_t size = align_size_up(used, os::vm_allocation_granularity()); duke@435: if (!open_for_read()) { duke@435: return false; duke@435: } duke@435: char *base = os::remap_memory(_fd, _full_path, si->_file_offset, duke@435: si->_base, size, false /* !read_only */, duke@435: si->_allow_exec); duke@435: close(); duke@435: if (base == NULL) { duke@435: fail_continue("Unable to remap shared readonly space (errno=%d).", errno); duke@435: return false; duke@435: } duke@435: if (base != si->_base) { duke@435: fail_continue("Unable to remap shared readonly space at required address."); duke@435: return false; duke@435: } duke@435: si->_read_only = false; duke@435: return true; duke@435: } duke@435: coleenp@4037: // Map the whole region at once, assumed to be allocated contiguously. coleenp@4037: ReservedSpace FileMapInfo::reserve_shared_memory() { iklam@7089: struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[0]; coleenp@4037: char* requested_addr = si->_base; coleenp@4037: hseigel@5528: size_t size = FileMapInfo::shared_spaces_size(); coleenp@4037: coleenp@4037: // Reserve the space first, then map otherwise map will go right over some coleenp@4037: // other reserved memory (like the code cache). hseigel@5528: ReservedSpace rs(size, os::vm_allocation_granularity(), false, requested_addr); coleenp@4037: if (!rs.is_reserved()) { coleenp@4804: fail_continue(err_msg("Unable to reserve shared space at required address " INTPTR_FORMAT, requested_addr)); coleenp@4037: return rs; coleenp@4037: } zgu@4193: // the reserved virtual memory is for mapping class data sharing archive hseigel@4396: MemTracker::record_virtual_memory_type((address)rs.base(), mtClassShared); hseigel@4396: coleenp@4037: return rs; coleenp@4037: } duke@435: duke@435: // Memory map a region in the address space. coleenp@4037: static const char* shared_region_name[] = { "ReadOnly", "ReadWrite", "MiscData", "MiscCode"}; duke@435: coleenp@4037: char* FileMapInfo::map_region(int i) { iklam@7089: struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i]; duke@435: size_t used = si->_used; coleenp@4037: size_t alignment = os::vm_allocation_granularity(); coleenp@4037: size_t size = align_size_up(used, alignment); coleenp@4037: char *requested_addr = si->_base; coleenp@4037: coleenp@4037: // map the contents of the CDS archive in this memory duke@435: char *base = os::map_memory(_fd, _full_path, si->_file_offset, duke@435: requested_addr, size, si->_read_only, duke@435: si->_allow_exec); coleenp@4037: if (base == NULL || base != si->_base) { coleenp@4037: fail_continue(err_msg("Unable to map %s shared space at required address.", shared_region_name[i])); duke@435: return NULL; duke@435: } hseigel@4396: #ifdef _WINDOWS hseigel@4396: // This call is Windows-only because the memory_type gets recorded for the other platforms hseigel@4396: // in method FileMapInfo::reserve_shared_memory(), which is not called on Windows. hseigel@4396: MemTracker::record_virtual_memory_type((address)base, mtClassShared); hseigel@4396: #endif duke@435: return base; duke@435: } duke@435: jiangli@6868: bool FileMapInfo::verify_region_checksum(int i) { jiangli@6868: if (!VerifySharedSpaces) { jiangli@6868: return true; jiangli@6868: } jiangli@7241: const char* buf = _header->_space[i]._base; jiangli@7241: size_t sz = _header->_space[i]._used; jiangli@6868: int crc = ClassLoader::crc32(0, buf, (jint)sz); jiangli@7241: if (crc != _header->_space[i]._crc) { jiangli@6868: fail_continue("Checksum verification failed."); jiangli@6868: return false; jiangli@6868: } jiangli@6868: return true; jiangli@6868: } duke@435: duke@435: // Unmap a memory region in the address space. duke@435: duke@435: void FileMapInfo::unmap_region(int i) { iklam@7089: struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i]; duke@435: size_t used = si->_used; duke@435: size_t size = align_size_up(used, os::vm_allocation_granularity()); duke@435: if (!os::unmap_memory(si->_base, size)) { duke@435: fail_stop("Unable to unmap shared space."); duke@435: } duke@435: } duke@435: duke@435: duke@435: void FileMapInfo::assert_mark(bool check) { duke@435: if (!check) { duke@435: fail_stop("Mark mismatch while restoring from shared file.", NULL); duke@435: } duke@435: } duke@435: duke@435: duke@435: FileMapInfo* FileMapInfo::_current_info = NULL; iklam@7089: SharedClassPathEntry* FileMapInfo::_classpath_entry_table = NULL; iklam@7089: int FileMapInfo::_classpath_entry_table_size = 0; iklam@7089: size_t FileMapInfo::_classpath_entry_size = 0x1234baad; iklam@7089: bool FileMapInfo::_validating_classpath_entry_table = false; duke@435: duke@435: // Open the shared archive file, read and validate the header duke@435: // information (version, boot classpath, etc.). If initialization duke@435: // fails, shared spaces are disabled and the file is closed. [See duke@435: // fail_continue.] iklam@7089: // iklam@7089: // Validation of the archive is done in two steps: iklam@7089: // iklam@7089: // [1] validate_header() - done here. This checks the header, including _paths_misc_info. iklam@7089: // [2] validate_classpath_entry_table - this is done later, because the table is in the RW iklam@7089: // region of the archive, which is not mapped yet. duke@435: bool FileMapInfo::initialize() { duke@435: assert(UseSharedSpaces, "UseSharedSpaces expected."); duke@435: duke@435: if (JvmtiExport::can_modify_any_class() || JvmtiExport::can_walk_any_space()) { duke@435: fail_continue("Tool agent requires sharing to be disabled."); duke@435: return false; duke@435: } duke@435: duke@435: if (!open_for_read()) { duke@435: return false; duke@435: } duke@435: duke@435: init_from_file(_fd); iklam@7089: if (!validate_header()) { duke@435: return false; duke@435: } duke@435: iklam@7089: SharedReadOnlySize = _header->_space[0]._capacity; iklam@7089: SharedReadWriteSize = _header->_space[1]._capacity; iklam@7089: SharedMiscDataSize = _header->_space[2]._capacity; iklam@7089: SharedMiscCodeSize = _header->_space[3]._capacity; duke@435: return true; duke@435: } duke@435: jiangli@7241: int FileMapInfo::FileMapHeader::compute_crc() { jiangli@7241: char* header = data(); jiangli@6868: // start computing from the field after _crc jiangli@7241: char* buf = (char*)&_crc + sizeof(int); jiangli@7241: size_t sz = data_size() - (buf - header); jiangli@6868: int crc = ClassLoader::crc32(0, buf, (jint)sz); jiangli@6868: return crc; jiangli@6868: } duke@435: jiangli@7241: int FileMapInfo::compute_header_crc() { jiangli@7241: return _header->compute_crc(); jiangli@7241: } jiangli@7241: iklam@7089: bool FileMapInfo::FileMapHeader::validate() { jiangli@7241: if (_magic != (int)0xf00baba2) { jiangli@7241: FileMapInfo::fail_continue("The shared archive file has a bad magic number."); jiangli@7241: return false; jiangli@7241: } jiangli@7241: if (VerifySharedSpaces && compute_crc() != _crc) { jiangli@6868: fail_continue("Header checksum verification failed."); jiangli@6868: return false; jiangli@6868: } iklam@7089: if (_version != current_version()) { iklam@7089: FileMapInfo::fail_continue("The shared archive file is the wrong version."); jiangli@7241: duke@435: return false; duke@435: } hseigel@4293: char header_version[JVM_IDENT_MAX]; hseigel@4293: get_header_version(header_version); iklam@7089: if (strncmp(_jvm_ident, header_version, JVM_IDENT_MAX-1) != 0) { iklam@7089: if (TraceClassPaths) { iklam@7089: tty->print_cr("Expected: %s", header_version); iklam@7089: tty->print_cr("Actual: %s", _jvm_ident); iklam@7089: } iklam@7089: FileMapInfo::fail_continue("The shared archive file was created by a different" iklam@7089: " version or build of HotSpot"); duke@435: return false; duke@435: } iklam@7089: if (_obj_alignment != ObjectAlignmentInBytes) { iklam@7089: FileMapInfo::fail_continue("The shared archive file's ObjectAlignmentInBytes of %d" hseigel@4397: " does not equal the current ObjectAlignmentInBytes of %d.", iklam@7089: _obj_alignment, ObjectAlignmentInBytes); duke@435: return false; duke@435: } duke@435: duke@435: return true; duke@435: } duke@435: iklam@7089: bool FileMapInfo::validate_header() { iklam@7089: bool status = _header->validate(); iklam@7089: iklam@7089: if (status) { iklam@7089: if (!ClassLoader::check_shared_paths_misc_info(_paths_misc_info, _header->_paths_misc_info_size)) { iklam@7089: if (!PrintSharedArchiveAndExit) { iklam@7089: fail_continue("shared class paths mismatch (hint: enable -XX:+TraceClassPaths to diagnose the failure)"); iklam@7089: status = false; iklam@7089: } iklam@7089: } iklam@7089: } iklam@7089: iklam@7089: if (_paths_misc_info != NULL) { iklam@7089: FREE_C_HEAP_ARRAY(char, _paths_misc_info, mtClass); iklam@7089: _paths_misc_info = NULL; iklam@7089: } iklam@7089: return status; iklam@7089: } iklam@7089: duke@435: // The following method is provided to see whether a given pointer duke@435: // falls in the mapped shared space. duke@435: // Param: duke@435: // p, The given pointer duke@435: // Return: duke@435: // True if the p is within the mapped shared space, otherwise, false. duke@435: bool FileMapInfo::is_in_shared_space(const void* p) { coleenp@4037: for (int i = 0; i < MetaspaceShared::n_regions; i++) { iklam@7089: if (p >= _header->_space[i]._base && iklam@7089: p < _header->_space[i]._base + _header->_space[i]._used) { duke@435: return true; duke@435: } duke@435: } duke@435: duke@435: return false; duke@435: } iklam@5329: iklam@5329: void FileMapInfo::print_shared_spaces() { iklam@5329: gclog_or_tty->print_cr("Shared Spaces:"); iklam@5329: for (int i = 0; i < MetaspaceShared::n_regions; i++) { iklam@7089: struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i]; iklam@5329: gclog_or_tty->print(" %s " INTPTR_FORMAT "-" INTPTR_FORMAT, iklam@5329: shared_region_name[i], iklam@5329: si->_base, si->_base + si->_used); iklam@5329: } iklam@5329: } hseigel@5528: hseigel@5528: // Unmap mapped regions of shared space. hseigel@5528: void FileMapInfo::stop_sharing_and_unmap(const char* msg) { hseigel@5528: FileMapInfo *map_info = FileMapInfo::current_info(); hseigel@5528: if (map_info) { hseigel@5528: map_info->fail_continue(msg); hseigel@5528: for (int i = 0; i < MetaspaceShared::n_regions; i++) { iklam@7089: if (map_info->_header->_space[i]._base != NULL) { hseigel@5528: map_info->unmap_region(i); iklam@7089: map_info->_header->_space[i]._base = NULL; hseigel@5528: } hseigel@5528: } hseigel@5528: } else if (DumpSharedSpaces) { hseigel@5528: fail_stop(msg, NULL); hseigel@5528: } hseigel@5528: }