0N/A/*
1879N/A * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#include "precompiled.hpp"
1879N/A#include "classfile/classLoader.hpp"
1879N/A#include "classfile/symbolTable.hpp"
1879N/A#include "memory/filemap.hpp"
1879N/A#include "runtime/arguments.hpp"
1879N/A#include "runtime/java.hpp"
1879N/A#include "runtime/os.hpp"
4064N/A#include "services/memTracker.hpp"
1879N/A#include "utilities/defaultStream.hpp"
1879N/A
0N/A# include <sys/stat.h>
0N/A# include <errno.h>
0N/A
0N/A#ifndef O_BINARY // if defined (Win32) use binary files.
0N/A#define O_BINARY 0 // otherwise do nothing.
0N/A#endif
0N/A
0N/A
0N/Aextern address JVM_FunctionAtStart();
0N/Aextern address JVM_FunctionAtEnd();
0N/A
605N/A// Complain and stop. All error conditions occurring during the writing of
0N/A// an archive file should stop the process. Unrecoverable errors during
0N/A// the reading of the archive file should stop the process.
0N/A
0N/Astatic void fail(const char *msg, va_list ap) {
0N/A // This occurs very early during initialization: tty is not initialized.
0N/A jio_fprintf(defaultStream::error_stream(),
605N/A "An error has occurred while processing the"
0N/A " shared archive file.\n");
0N/A jio_vfprintf(defaultStream::error_stream(), msg, ap);
0N/A jio_fprintf(defaultStream::error_stream(), "\n");
0N/A vm_exit_during_initialization("Unable to use shared archive.", NULL);
0N/A}
0N/A
0N/A
0N/Avoid FileMapInfo::fail_stop(const char *msg, ...) {
0N/A va_list ap;
0N/A va_start(ap, msg);
0N/A fail(msg, ap); // Never returns.
0N/A va_end(ap); // for completeness.
0N/A}
0N/A
0N/A
0N/A// Complain and continue. Recoverable errors during the reading of the
0N/A// archive file may continue (with sharing disabled).
0N/A//
0N/A// If we continue, then disable shared spaces and close the file.
0N/A
0N/Avoid FileMapInfo::fail_continue(const char *msg, ...) {
0N/A va_list ap;
0N/A va_start(ap, msg);
0N/A if (RequireSharedSpaces) {
0N/A fail(msg, ap);
0N/A }
0N/A va_end(ap);
0N/A UseSharedSpaces = false;
0N/A close();
0N/A}
0N/A
0N/A
0N/A// Fill in the fileMapInfo structure with data about this VM instance.
0N/A
0N/Avoid FileMapInfo::populate_header(size_t alignment) {
0N/A _header._magic = 0xf00baba2;
0N/A _header._version = _current_version;
0N/A _header._alignment = alignment;
0N/A
0N/A // The following fields are for sanity checks for whether this archive
0N/A // will function correctly with this JVM and the bootclasspath it's
0N/A // invoked with.
0N/A
0N/A // JVM version string ... changes on each build.
0N/A const char *vm_version = VM_Version::internal_vm_info_string();
0N/A if (strlen(vm_version) < (JVM_IDENT_MAX-1)) {
0N/A strcpy(_header._jvm_ident, vm_version);
0N/A } else {
0N/A fail_stop("JVM Ident field for shared archive is too long"
0N/A " - truncated to <%s>", _header._jvm_ident);
0N/A }
0N/A
0N/A // Build checks on classpath and jar files
0N/A _header._num_jars = 0;
0N/A ClassPathEntry *cpe = ClassLoader::classpath_entry(0);
0N/A for ( ; cpe != NULL; cpe = cpe->next()) {
0N/A
0N/A if (cpe->is_jar_file()) {
0N/A if (_header._num_jars >= JVM_SHARED_JARS_MAX) {
0N/A fail_stop("Too many jar files to share.", NULL);
0N/A }
0N/A
0N/A // Jar file - record timestamp and file size.
0N/A struct stat st;
0N/A const char *path = cpe->name();
0N/A if (os::stat(path, &st) != 0) {
0N/A // If we can't access a jar file in the boot path, then we can't
0N/A // make assumptions about where classes get loaded from.
0N/A fail_stop("Unable to open jar file %s.", path);
0N/A }
0N/A _header._jar[_header._num_jars]._timestamp = st.st_mtime;
0N/A _header._jar[_header._num_jars]._filesize = st.st_size;
0N/A _header._num_jars++;
0N/A } else {
0N/A
0N/A // If directories appear in boot classpath, they must be empty to
0N/A // avoid having to verify each individual class file.
0N/A const char* name = ((ClassPathDirEntry*)cpe)->name();
0N/A if (!os::dir_is_empty(name)) {
0N/A fail_stop("Boot classpath directory %s is not empty.", name);
0N/A }
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/A// Read the FileMapInfo information from the file.
0N/A
0N/Abool FileMapInfo::init_from_file(int fd) {
0N/A
0N/A size_t n = read(fd, &_header, sizeof(struct FileMapHeader));
0N/A if (n != sizeof(struct FileMapHeader)) {
0N/A fail_continue("Unable to read the file header.");
0N/A return false;
0N/A }
0N/A if (_header._version != current_version()) {
0N/A fail_continue("The shared archive file has the wrong version.");
0N/A return false;
0N/A }
0N/A _file_offset = (long)n;
0N/A return true;
0N/A}
0N/A
0N/A
0N/A// Read the FileMapInfo information from the file.
0N/Abool FileMapInfo::open_for_read() {
0N/A _full_path = Arguments::GetSharedArchivePath();
0N/A int fd = open(_full_path, O_RDONLY | O_BINARY, 0);
0N/A if (fd < 0) {
0N/A if (errno == ENOENT) {
0N/A // Not locating the shared archive is ok.
0N/A fail_continue("Specified shared archive not found.");
0N/A } else {
0N/A fail_continue("Failed to open shared archive file (%s).",
0N/A strerror(errno));
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A _fd = fd;
0N/A _file_open = true;
0N/A return true;
0N/A}
0N/A
0N/A
0N/A// Write the FileMapInfo information to the file.
0N/A
0N/Avoid FileMapInfo::open_for_write() {
0N/A _full_path = Arguments::GetSharedArchivePath();
0N/A if (PrintSharedSpaces) {
0N/A tty->print_cr("Dumping shared data to file: ");
0N/A tty->print_cr(" %s", _full_path);
0N/A }
0N/A
0N/A // Remove the existing file in case another process has it open.
0N/A remove(_full_path);
0N/A int fd = open(_full_path, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0444);
0N/A if (fd < 0) {
0N/A fail_stop("Unable to create shared archive file %s.", _full_path);
0N/A }
0N/A _fd = fd;
0N/A _file_offset = 0;
0N/A _file_open = true;
0N/A}
0N/A
0N/A
0N/A// Write the header to the file, seek to the next allocation boundary.
0N/A
0N/Avoid FileMapInfo::write_header() {
0N/A write_bytes_aligned(&_header, sizeof(FileMapHeader));
0N/A}
0N/A
0N/A
0N/A// Dump shared spaces to file.
0N/A
0N/Avoid FileMapInfo::write_space(int i, CompactibleSpace* space, bool read_only) {
0N/A align_file_position();
0N/A struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[i];
0N/A write_region(i, (char*)space->bottom(), space->used(),
0N/A space->capacity(), read_only, false);
0N/A}
0N/A
0N/A
0N/A// Dump region to file.
0N/A
0N/Avoid FileMapInfo::write_region(int region, char* base, size_t size,
0N/A size_t capacity, bool read_only,
0N/A bool allow_exec) {
0N/A struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[region];
0N/A
0N/A if (_file_open) {
0N/A guarantee(si->_file_offset == _file_offset, "file offset mismatch.");
0N/A if (PrintSharedSpaces) {
0N/A tty->print_cr("Shared file region %d: 0x%x bytes, addr 0x%x,"
0N/A " file offset 0x%x", region, size, base, _file_offset);
0N/A }
0N/A } else {
0N/A si->_file_offset = _file_offset;
0N/A }
0N/A si->_base = base;
0N/A si->_used = size;
0N/A si->_capacity = capacity;
0N/A si->_read_only = read_only;
0N/A si->_allow_exec = allow_exec;
0N/A write_bytes_aligned(base, (int)size);
0N/A}
0N/A
0N/A
0N/A// Dump bytes to file -- at the current file position.
0N/A
0N/Avoid FileMapInfo::write_bytes(const void* buffer, int nbytes) {
0N/A if (_file_open) {
0N/A int n = ::write(_fd, buffer, nbytes);
0N/A if (n != nbytes) {
0N/A // It is dangerous to leave the corrupted shared archive file around,
0N/A // close and remove the file. See bug 6372906.
0N/A close();
0N/A remove(_full_path);
0N/A fail_stop("Unable to write to shared archive file.", NULL);
0N/A }
0N/A }
0N/A _file_offset += nbytes;
0N/A}
0N/A
0N/A
0N/A// Align file position to an allocation unit boundary.
0N/A
0N/Avoid FileMapInfo::align_file_position() {
0N/A long new_file_offset = align_size_up(_file_offset, os::vm_allocation_granularity());
0N/A if (new_file_offset != _file_offset) {
0N/A _file_offset = new_file_offset;
0N/A if (_file_open) {
0N/A // Seek one byte back from the target and write a byte to insure
0N/A // that the written file is the correct length.
0N/A _file_offset -= 1;
0N/A if (lseek(_fd, _file_offset, SEEK_SET) < 0) {
0N/A fail_stop("Unable to seek.", NULL);
0N/A }
0N/A char zero = 0;
0N/A write_bytes(&zero, 1);
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/A// Dump bytes to file -- at the current file position.
0N/A
0N/Avoid FileMapInfo::write_bytes_aligned(const void* buffer, int nbytes) {
0N/A align_file_position();
0N/A write_bytes(buffer, nbytes);
0N/A align_file_position();
0N/A}
0N/A
0N/A
0N/A// Close the shared archive file. This does NOT unmap mapped regions.
0N/A
0N/Avoid FileMapInfo::close() {
0N/A if (_file_open) {
0N/A if (::close(_fd) < 0) {
0N/A fail_stop("Unable to close the shared archive file.");
0N/A }
0N/A _file_open = false;
0N/A _fd = -1;
0N/A }
0N/A}
0N/A
0N/A
0N/A// Memory map a shared space from the archive file.
0N/A
0N/Abool FileMapInfo::map_space(int i, ReservedSpace rs, ContiguousSpace* space) {
0N/A struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[i];
0N/A if (space != NULL) {
0N/A if (si->_base != (char*)space->bottom() ||
0N/A si->_capacity != space->capacity()) {
0N/A fail_continue("Shared space base address does not match.");
0N/A return false;
0N/A }
0N/A }
0N/A bool result = (map_region(i, rs) != NULL);
0N/A if (space != NULL && result) {
0N/A space->set_top((HeapWord*)(si->_base + si->_used));
0N/A space->set_saved_mark();
0N/A }
0N/A return result;
0N/A}
0N/A
0N/A
0N/A// JVM/TI RedefineClasses() support:
0N/A// Remap the shared readonly space to shared readwrite, private.
0N/Abool FileMapInfo::remap_shared_readonly_as_readwrite() {
0N/A struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[0];
0N/A if (!si->_read_only) {
0N/A // the space is already readwrite so we are done
0N/A return true;
0N/A }
0N/A size_t used = si->_used;
0N/A size_t size = align_size_up(used, os::vm_allocation_granularity());
0N/A if (!open_for_read()) {
0N/A return false;
0N/A }
0N/A char *base = os::remap_memory(_fd, _full_path, si->_file_offset,
0N/A si->_base, size, false /* !read_only */,
0N/A si->_allow_exec);
0N/A close();
0N/A if (base == NULL) {
0N/A fail_continue("Unable to remap shared readonly space (errno=%d).", errno);
0N/A return false;
0N/A }
0N/A if (base != si->_base) {
0N/A fail_continue("Unable to remap shared readonly space at required address.");
0N/A return false;
0N/A }
0N/A si->_read_only = false;
0N/A return true;
0N/A}
0N/A
0N/A
0N/A// Memory map a region in the address space.
0N/A
0N/Achar* FileMapInfo::map_region(int i, ReservedSpace rs) {
0N/A struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[i];
0N/A size_t used = si->_used;
0N/A size_t size = align_size_up(used, os::vm_allocation_granularity());
0N/A
0N/A ReservedSpace mapped_rs = rs.first_part(size, true, true);
0N/A ReservedSpace unmapped_rs = rs.last_part(size);
0N/A mapped_rs.release();
0N/A
4064N/A // This memory still belongs to JavaHeap
4064N/A MemTracker::record_virtual_memory_type((address)unmapped_rs.base(), mtJavaHeap);
4064N/A char* mapped_addr = map_region(i, true);
4064N/A if (mapped_addr != NULL) {
4064N/A MemTracker::record_virtual_memory_type((address)mapped_addr, mtJavaHeap);
4064N/A }
4064N/A return mapped_addr;
0N/A}
0N/A
0N/A
0N/A// Memory map a region in the address space.
0N/A
0N/Achar* FileMapInfo::map_region(int i, bool address_must_match) {
0N/A struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[i];
0N/A size_t used = si->_used;
0N/A size_t size = align_size_up(used, os::vm_allocation_granularity());
0N/A char *requested_addr = 0;
0N/A if (address_must_match) {
0N/A requested_addr = si->_base;
0N/A }
0N/A char *base = os::map_memory(_fd, _full_path, si->_file_offset,
0N/A requested_addr, size, si->_read_only,
0N/A si->_allow_exec);
0N/A if (base == NULL) {
0N/A fail_continue("Unable to map shared space.");
0N/A return NULL;
0N/A }
0N/A if (address_must_match) {
0N/A if (base != si->_base) {
0N/A fail_continue("Unable to map shared space at required address.");
0N/A return NULL;
0N/A }
0N/A } else {
0N/A si->_base = base; // save mapped address for unmapping.
0N/A }
0N/A return base;
0N/A}
0N/A
0N/A
0N/A// Unmap a memory region in the address space.
0N/A
0N/Avoid FileMapInfo::unmap_region(int i) {
0N/A struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[i];
0N/A size_t used = si->_used;
0N/A size_t size = align_size_up(used, os::vm_allocation_granularity());
0N/A if (!os::unmap_memory(si->_base, size)) {
0N/A fail_stop("Unable to unmap shared space.");
0N/A }
0N/A}
0N/A
0N/A
0N/Avoid FileMapInfo::assert_mark(bool check) {
0N/A if (!check) {
0N/A fail_stop("Mark mismatch while restoring from shared file.", NULL);
0N/A }
0N/A}
0N/A
0N/A
0N/AFileMapInfo* FileMapInfo::_current_info = NULL;
0N/A
0N/A
0N/A// Open the shared archive file, read and validate the header
0N/A// information (version, boot classpath, etc.). If initialization
0N/A// fails, shared spaces are disabled and the file is closed. [See
0N/A// fail_continue.]
0N/A
0N/A
0N/Abool FileMapInfo::initialize() {
0N/A assert(UseSharedSpaces, "UseSharedSpaces expected.");
0N/A
0N/A if (JvmtiExport::can_modify_any_class() || JvmtiExport::can_walk_any_space()) {
0N/A fail_continue("Tool agent requires sharing to be disabled.");
0N/A return false;
0N/A }
0N/A
0N/A if (!open_for_read()) {
0N/A return false;
0N/A }
0N/A
0N/A init_from_file(_fd);
0N/A if (!validate()) {
0N/A return false;
0N/A }
0N/A
0N/A SharedReadOnlySize = _header._space[0]._capacity;
0N/A SharedReadWriteSize = _header._space[1]._capacity;
0N/A SharedMiscDataSize = _header._space[2]._capacity;
0N/A SharedMiscCodeSize = _header._space[3]._capacity;
0N/A return true;
0N/A}
0N/A
0N/A
0N/Abool FileMapInfo::validate() {
0N/A if (_header._version != current_version()) {
0N/A fail_continue("The shared archive file is the wrong version.");
0N/A return false;
0N/A }
0N/A if (_header._magic != (int)0xf00baba2) {
0N/A fail_continue("The shared archive file has a bad magic number.");
0N/A return false;
0N/A }
0N/A if (strncmp(_header._jvm_ident, VM_Version::internal_vm_info_string(),
0N/A JVM_IDENT_MAX-1) != 0) {
0N/A fail_continue("The shared archive file was created by a different"
0N/A " version or build of HotSpot.");
0N/A return false;
0N/A }
0N/A
0N/A // Cannot verify interpreter yet, as it can only be created after the GC
0N/A // heap has been initialized.
0N/A
0N/A if (_header._num_jars >= JVM_SHARED_JARS_MAX) {
0N/A fail_continue("Too many jar files to share.");
0N/A return false;
0N/A }
0N/A
0N/A // Build checks on classpath and jar files
0N/A int num_jars_now = 0;
0N/A ClassPathEntry *cpe = ClassLoader::classpath_entry(0);
0N/A for ( ; cpe != NULL; cpe = cpe->next()) {
0N/A
0N/A if (cpe->is_jar_file()) {
0N/A if (num_jars_now < _header._num_jars) {
0N/A
0N/A // Jar file - verify timestamp and file size.
0N/A struct stat st;
0N/A const char *path = cpe->name();
0N/A if (os::stat(path, &st) != 0) {
0N/A fail_continue("Unable to open jar file %s.", path);
0N/A return false;
0N/A }
0N/A if (_header._jar[num_jars_now]._timestamp != st.st_mtime ||
0N/A _header._jar[num_jars_now]._filesize != st.st_size) {
0N/A fail_continue("A jar file is not the one used while building"
0N/A " the shared archive file.");
0N/A return false;
0N/A }
0N/A }
0N/A ++num_jars_now;
0N/A } else {
0N/A
0N/A // If directories appear in boot classpath, they must be empty to
0N/A // avoid having to verify each individual class file.
0N/A const char* name = ((ClassPathDirEntry*)cpe)->name();
0N/A if (!os::dir_is_empty(name)) {
0N/A fail_continue("Boot classpath directory %s is not empty.", name);
0N/A return false;
0N/A }
0N/A }
0N/A }
0N/A if (num_jars_now < _header._num_jars) {
0N/A fail_continue("The number of jar files in the boot classpath is"
0N/A " less than the number the shared archive was created with.");
0N/A return false;
0N/A }
0N/A
0N/A return true;
0N/A}
0N/A
0N/A// The following method is provided to see whether a given pointer
0N/A// falls in the mapped shared space.
0N/A// Param:
0N/A// p, The given pointer
0N/A// Return:
0N/A// True if the p is within the mapped shared space, otherwise, false.
0N/Abool FileMapInfo::is_in_shared_space(const void* p) {
0N/A for (int i = 0; i < CompactingPermGenGen::n_regions; i++) {
0N/A if (p >= _header._space[i]._base &&
0N/A p < _header._space[i]._base + _header._space[i]._used) {
0N/A return true;
0N/A }
0N/A }
0N/A
0N/A return false;
0N/A}