duke@435: /* coleenp@4037: * Copyright (c) 2003, 2012, 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" stefank@2314: #include "classfile/symbolTable.hpp" hseigel@4293: #include "classfile/altHashing.hpp" stefank@2314: #include "memory/filemap.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: duke@435: 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"); 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); duke@435: if (RequireSharedSpaces) { duke@435: fail(msg, ap); duke@435: } duke@435: va_end(ap); duke@435: UseSharedSpaces = false; duke@435: close(); 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: duke@435: void FileMapInfo::populate_header(size_t alignment) { duke@435: _header._magic = 0xf00baba2; duke@435: _header._version = _current_version; duke@435: _header._alignment = alignment; hseigel@4397: _header._obj_alignment = ObjectAlignmentInBytes; 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. hseigel@4293: get_header_version(_header._jvm_ident); duke@435: duke@435: // Build checks on classpath and jar files duke@435: _header._num_jars = 0; duke@435: ClassPathEntry *cpe = ClassLoader::classpath_entry(0); duke@435: for ( ; cpe != NULL; cpe = cpe->next()) { duke@435: duke@435: if (cpe->is_jar_file()) { duke@435: if (_header._num_jars >= JVM_SHARED_JARS_MAX) { duke@435: fail_stop("Too many jar files to share.", NULL); duke@435: } duke@435: duke@435: // Jar file - record timestamp and file size. duke@435: struct stat st; duke@435: const char *path = cpe->name(); duke@435: if (os::stat(path, &st) != 0) { duke@435: // If we can't access a jar file in the boot path, then we can't duke@435: // make assumptions about where classes get loaded from. duke@435: fail_stop("Unable to open jar file %s.", path); duke@435: } duke@435: _header._jar[_header._num_jars]._timestamp = st.st_mtime; duke@435: _header._jar[_header._num_jars]._filesize = st.st_size; duke@435: _header._num_jars++; duke@435: } else { duke@435: duke@435: // If directories appear in boot classpath, they must be empty to duke@435: // avoid having to verify each individual class file. duke@435: const char* name = ((ClassPathDirEntry*)cpe)->name(); duke@435: if (!os::dir_is_empty(name)) { duke@435: fail_stop("Boot classpath directory %s is not empty.", name); duke@435: } duke@435: } duke@435: } duke@435: } duke@435: duke@435: duke@435: // Read the FileMapInfo information from the file. duke@435: duke@435: bool FileMapInfo::init_from_file(int fd) { duke@435: duke@435: size_t n = read(fd, &_header, sizeof(struct FileMapHeader)); duke@435: if (n != sizeof(struct FileMapHeader)) { duke@435: fail_continue("Unable to read the file header."); duke@435: return false; duke@435: } duke@435: if (_header._version != current_version()) { duke@435: fail_continue("The shared archive file has the wrong version."); duke@435: return false; duke@435: } duke@435: _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: duke@435: // Remove the existing file in case another process has it open. duke@435: remove(_full_path); hseigel@4396: #ifdef _WINDOWS // if 0444 is used on Windows, then remove() will fail. hseigel@4396: int fd = open(_full_path, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0744); hseigel@4396: #else duke@435: int fd = open(_full_path, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0444); hseigel@4396: #endif 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() { duke@435: write_bytes_aligned(&_header, sizeof(FileMapHeader)); 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(); coleenp@4037: size_t used = space->used_words(Metaspace::NonClassType) * BytesPerWord; coleenp@4037: size_t capacity = space->capacity_words(Metaspace::NonClassType) * BytesPerWord; duke@435: 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) { duke@435: 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; 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() { duke@435: long new_file_offset = align_size_up(_file_offset, 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; duke@435: if (lseek(_fd, _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() { duke@435: 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() { coleenp@4037: struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[0]; coleenp@4037: char* requested_addr = si->_base; coleenp@4037: size_t alignment = os::vm_allocation_granularity(); coleenp@4037: coleenp@4037: size_t size = align_size_up(SharedReadOnlySize + SharedReadWriteSize + coleenp@4037: SharedMiscDataSize + SharedMiscCodeSize, coleenp@4037: alignment); 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). coleenp@4037: ReservedSpace rs(size, alignment, false, requested_addr); coleenp@4037: if (!rs.is_reserved()) { coleenp@4037: fail_continue(err_msg("Unable to reserved 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) { duke@435: 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: duke@435: duke@435: // Unmap a memory region in the address space. duke@435: duke@435: void FileMapInfo::unmap_region(int i) { duke@435: 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; duke@435: 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.] 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); duke@435: if (!validate()) { duke@435: return false; duke@435: } duke@435: duke@435: SharedReadOnlySize = _header._space[0]._capacity; duke@435: SharedReadWriteSize = _header._space[1]._capacity; duke@435: SharedMiscDataSize = _header._space[2]._capacity; duke@435: SharedMiscCodeSize = _header._space[3]._capacity; duke@435: return true; duke@435: } duke@435: duke@435: duke@435: bool FileMapInfo::validate() { duke@435: if (_header._version != current_version()) { duke@435: fail_continue("The shared archive file is the wrong version."); duke@435: return false; duke@435: } duke@435: if (_header._magic != (int)0xf00baba2) { duke@435: fail_continue("The shared archive file has a bad magic number."); duke@435: return false; duke@435: } hseigel@4293: char header_version[JVM_IDENT_MAX]; hseigel@4293: get_header_version(header_version); hseigel@4293: if (strncmp(_header._jvm_ident, header_version, JVM_IDENT_MAX-1) != 0) { duke@435: fail_continue("The shared archive file was created by a different" duke@435: " version or build of HotSpot."); duke@435: return false; duke@435: } hseigel@4397: if (_header._obj_alignment != ObjectAlignmentInBytes) { hseigel@4397: fail_continue("The shared archive file's ObjectAlignmentInBytes of %d" hseigel@4397: " does not equal the current ObjectAlignmentInBytes of %d.", hseigel@4397: _header._obj_alignment, ObjectAlignmentInBytes); hseigel@4397: return false; hseigel@4397: } duke@435: duke@435: // Cannot verify interpreter yet, as it can only be created after the GC duke@435: // heap has been initialized. duke@435: duke@435: if (_header._num_jars >= JVM_SHARED_JARS_MAX) { duke@435: fail_continue("Too many jar files to share."); duke@435: return false; duke@435: } duke@435: duke@435: // Build checks on classpath and jar files duke@435: int num_jars_now = 0; duke@435: ClassPathEntry *cpe = ClassLoader::classpath_entry(0); duke@435: for ( ; cpe != NULL; cpe = cpe->next()) { duke@435: duke@435: if (cpe->is_jar_file()) { duke@435: if (num_jars_now < _header._num_jars) { duke@435: duke@435: // Jar file - verify timestamp and file size. duke@435: struct stat st; duke@435: const char *path = cpe->name(); duke@435: if (os::stat(path, &st) != 0) { duke@435: fail_continue("Unable to open jar file %s.", path); duke@435: return false; duke@435: } duke@435: if (_header._jar[num_jars_now]._timestamp != st.st_mtime || duke@435: _header._jar[num_jars_now]._filesize != st.st_size) { duke@435: fail_continue("A jar file is not the one used while building" duke@435: " the shared archive file."); duke@435: return false; duke@435: } duke@435: } duke@435: ++num_jars_now; duke@435: } else { duke@435: duke@435: // If directories appear in boot classpath, they must be empty to duke@435: // avoid having to verify each individual class file. duke@435: const char* name = ((ClassPathDirEntry*)cpe)->name(); duke@435: if (!os::dir_is_empty(name)) { duke@435: fail_continue("Boot classpath directory %s is not empty.", name); duke@435: return false; duke@435: } duke@435: } duke@435: } duke@435: if (num_jars_now < _header._num_jars) { duke@435: fail_continue("The number of jar files in the boot classpath is" duke@435: " less than the number the shared archive was created with."); duke@435: return false; duke@435: } duke@435: duke@435: return true; duke@435: } duke@435: 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++) { duke@435: if (p >= _header._space[i]._base && duke@435: 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: }