src/share/vm/memory/filemap.cpp

changeset 0
f90c822e73f8
child 6876
710a3c8b516e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/memory/filemap.cpp	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,610 @@
     1.4 +/*
     1.5 + * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#include "precompiled.hpp"
    1.29 +#include "classfile/classLoader.hpp"
    1.30 +#include "classfile/symbolTable.hpp"
    1.31 +#include "classfile/altHashing.hpp"
    1.32 +#include "memory/filemap.hpp"
    1.33 +#include "runtime/arguments.hpp"
    1.34 +#include "runtime/java.hpp"
    1.35 +#include "runtime/os.hpp"
    1.36 +#include "services/memTracker.hpp"
    1.37 +#include "utilities/defaultStream.hpp"
    1.38 +
    1.39 +# include <sys/stat.h>
    1.40 +# include <errno.h>
    1.41 +
    1.42 +#ifndef O_BINARY       // if defined (Win32) use binary files.
    1.43 +#define O_BINARY 0     // otherwise do nothing.
    1.44 +#endif
    1.45 +
    1.46 +PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
    1.47 +
    1.48 +extern address JVM_FunctionAtStart();
    1.49 +extern address JVM_FunctionAtEnd();
    1.50 +
    1.51 +// Complain and stop. All error conditions occurring during the writing of
    1.52 +// an archive file should stop the process.  Unrecoverable errors during
    1.53 +// the reading of the archive file should stop the process.
    1.54 +
    1.55 +static void fail(const char *msg, va_list ap) {
    1.56 +  // This occurs very early during initialization: tty is not initialized.
    1.57 +  jio_fprintf(defaultStream::error_stream(),
    1.58 +              "An error has occurred while processing the"
    1.59 +              " shared archive file.\n");
    1.60 +  jio_vfprintf(defaultStream::error_stream(), msg, ap);
    1.61 +  jio_fprintf(defaultStream::error_stream(), "\n");
    1.62 +  // Do not change the text of the below message because some tests check for it.
    1.63 +  vm_exit_during_initialization("Unable to use shared archive.", NULL);
    1.64 +}
    1.65 +
    1.66 +
    1.67 +void FileMapInfo::fail_stop(const char *msg, ...) {
    1.68 +        va_list ap;
    1.69 +  va_start(ap, msg);
    1.70 +  fail(msg, ap);        // Never returns.
    1.71 +  va_end(ap);           // for completeness.
    1.72 +}
    1.73 +
    1.74 +
    1.75 +// Complain and continue.  Recoverable errors during the reading of the
    1.76 +// archive file may continue (with sharing disabled).
    1.77 +//
    1.78 +// If we continue, then disable shared spaces and close the file.
    1.79 +
    1.80 +void FileMapInfo::fail_continue(const char *msg, ...) {
    1.81 +  va_list ap;
    1.82 +  va_start(ap, msg);
    1.83 +  if (RequireSharedSpaces) {
    1.84 +    fail(msg, ap);
    1.85 +  }
    1.86 +  va_end(ap);
    1.87 +  UseSharedSpaces = false;
    1.88 +  close();
    1.89 +}
    1.90 +
    1.91 +// Fill in the fileMapInfo structure with data about this VM instance.
    1.92 +
    1.93 +// This method copies the vm version info into header_version.  If the version is too
    1.94 +// long then a truncated version, which has a hash code appended to it, is copied.
    1.95 +//
    1.96 +// Using a template enables this method to verify that header_version is an array of
    1.97 +// length JVM_IDENT_MAX.  This ensures that the code that writes to the CDS file and
    1.98 +// the code that reads the CDS file will both use the same size buffer.  Hence, will
    1.99 +// use identical truncation.  This is necessary for matching of truncated versions.
   1.100 +template <int N> static void get_header_version(char (&header_version) [N]) {
   1.101 +  assert(N == JVM_IDENT_MAX, "Bad header_version size");
   1.102 +
   1.103 +  const char *vm_version = VM_Version::internal_vm_info_string();
   1.104 +  const int version_len = (int)strlen(vm_version);
   1.105 +
   1.106 +  if (version_len < (JVM_IDENT_MAX-1)) {
   1.107 +    strcpy(header_version, vm_version);
   1.108 +
   1.109 +  } else {
   1.110 +    // Get the hash value.  Use a static seed because the hash needs to return the same
   1.111 +    // value over multiple jvm invocations.
   1.112 +    unsigned int hash = AltHashing::murmur3_32(8191, (const jbyte*)vm_version, version_len);
   1.113 +
   1.114 +    // Truncate the ident, saving room for the 8 hex character hash value.
   1.115 +    strncpy(header_version, vm_version, JVM_IDENT_MAX-9);
   1.116 +
   1.117 +    // Append the hash code as eight hex digits.
   1.118 +    sprintf(&header_version[JVM_IDENT_MAX-9], "%08x", hash);
   1.119 +    header_version[JVM_IDENT_MAX-1] = 0;  // Null terminate.
   1.120 +  }
   1.121 +}
   1.122 +
   1.123 +void FileMapInfo::populate_header(size_t alignment) {
   1.124 +  _header._magic = 0xf00baba2;
   1.125 +  _header._version = _current_version;
   1.126 +  _header._alignment = alignment;
   1.127 +  _header._obj_alignment = ObjectAlignmentInBytes;
   1.128 +
   1.129 +  // The following fields are for sanity checks for whether this archive
   1.130 +  // will function correctly with this JVM and the bootclasspath it's
   1.131 +  // invoked with.
   1.132 +
   1.133 +  // JVM version string ... changes on each build.
   1.134 +  get_header_version(_header._jvm_ident);
   1.135 +
   1.136 +  // Build checks on classpath and jar files
   1.137 +  _header._num_jars = 0;
   1.138 +  ClassPathEntry *cpe = ClassLoader::classpath_entry(0);
   1.139 +  for ( ; cpe != NULL; cpe = cpe->next()) {
   1.140 +
   1.141 +    if (cpe->is_jar_file()) {
   1.142 +      if (_header._num_jars >= JVM_SHARED_JARS_MAX) {
   1.143 +        fail_stop("Too many jar files to share.", NULL);
   1.144 +      }
   1.145 +
   1.146 +      // Jar file - record timestamp and file size.
   1.147 +      struct stat st;
   1.148 +      const char *path = cpe->name();
   1.149 +      if (os::stat(path, &st) != 0) {
   1.150 +        // If we can't access a jar file in the boot path, then we can't
   1.151 +        // make assumptions about where classes get loaded from.
   1.152 +        fail_stop("Unable to open jar file %s.", path);
   1.153 +      }
   1.154 +      _header._jar[_header._num_jars]._timestamp = st.st_mtime;
   1.155 +      _header._jar[_header._num_jars]._filesize = st.st_size;
   1.156 +      _header._num_jars++;
   1.157 +    } else {
   1.158 +
   1.159 +      // If directories appear in boot classpath, they must be empty to
   1.160 +      // avoid having to verify each individual class file.
   1.161 +      const char* name = ((ClassPathDirEntry*)cpe)->name();
   1.162 +      if (!os::dir_is_empty(name)) {
   1.163 +        fail_stop("Boot classpath directory %s is not empty.", name);
   1.164 +      }
   1.165 +    }
   1.166 +  }
   1.167 +}
   1.168 +
   1.169 +
   1.170 +// Read the FileMapInfo information from the file.
   1.171 +
   1.172 +bool FileMapInfo::init_from_file(int fd) {
   1.173 +
   1.174 +  size_t n = read(fd, &_header, sizeof(struct FileMapHeader));
   1.175 +  if (n != sizeof(struct FileMapHeader)) {
   1.176 +    fail_continue("Unable to read the file header.");
   1.177 +    return false;
   1.178 +  }
   1.179 +  if (_header._version != current_version()) {
   1.180 +    fail_continue("The shared archive file has the wrong version.");
   1.181 +    return false;
   1.182 +  }
   1.183 +  size_t len = lseek(fd, 0, SEEK_END);
   1.184 +  struct FileMapInfo::FileMapHeader::space_info* si =
   1.185 +    &_header._space[MetaspaceShared::mc];
   1.186 +  if (si->_file_offset >= len || len - si->_file_offset < si->_used) {
   1.187 +    fail_continue("The shared archive file has been truncated.");
   1.188 +    return false;
   1.189 +  }
   1.190 +  _file_offset = n;
   1.191 +  return true;
   1.192 +}
   1.193 +
   1.194 +
   1.195 +// Read the FileMapInfo information from the file.
   1.196 +bool FileMapInfo::open_for_read() {
   1.197 +  _full_path = Arguments::GetSharedArchivePath();
   1.198 +  int fd = open(_full_path, O_RDONLY | O_BINARY, 0);
   1.199 +  if (fd < 0) {
   1.200 +    if (errno == ENOENT) {
   1.201 +      // Not locating the shared archive is ok.
   1.202 +      fail_continue("Specified shared archive not found.");
   1.203 +    } else {
   1.204 +      fail_continue("Failed to open shared archive file (%s).",
   1.205 +                    strerror(errno));
   1.206 +    }
   1.207 +    return false;
   1.208 +  }
   1.209 +
   1.210 +  _fd = fd;
   1.211 +  _file_open = true;
   1.212 +  return true;
   1.213 +}
   1.214 +
   1.215 +
   1.216 +// Write the FileMapInfo information to the file.
   1.217 +
   1.218 +void FileMapInfo::open_for_write() {
   1.219 + _full_path = Arguments::GetSharedArchivePath();
   1.220 +  if (PrintSharedSpaces) {
   1.221 +    tty->print_cr("Dumping shared data to file: ");
   1.222 +    tty->print_cr("   %s", _full_path);
   1.223 +  }
   1.224 +
   1.225 +#ifdef _WINDOWS  // On Windows, need WRITE permission to remove the file.
   1.226 +  chmod(_full_path, _S_IREAD | _S_IWRITE);
   1.227 +#endif
   1.228 +
   1.229 +  // Use remove() to delete the existing file because, on Unix, this will
   1.230 +  // allow processes that have it open continued access to the file.
   1.231 +  remove(_full_path);
   1.232 +  int fd = open(_full_path, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0444);
   1.233 +  if (fd < 0) {
   1.234 +    fail_stop("Unable to create shared archive file %s.", _full_path);
   1.235 +  }
   1.236 +  _fd = fd;
   1.237 +  _file_offset = 0;
   1.238 +  _file_open = true;
   1.239 +}
   1.240 +
   1.241 +
   1.242 +// Write the header to the file, seek to the next allocation boundary.
   1.243 +
   1.244 +void FileMapInfo::write_header() {
   1.245 +  write_bytes_aligned(&_header, sizeof(FileMapHeader));
   1.246 +}
   1.247 +
   1.248 +
   1.249 +// Dump shared spaces to file.
   1.250 +
   1.251 +void FileMapInfo::write_space(int i, Metaspace* space, bool read_only) {
   1.252 +  align_file_position();
   1.253 +  size_t used = space->used_bytes_slow(Metaspace::NonClassType);
   1.254 +  size_t capacity = space->capacity_bytes_slow(Metaspace::NonClassType);
   1.255 +  struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[i];
   1.256 +  write_region(i, (char*)space->bottom(), used, capacity, read_only, false);
   1.257 +}
   1.258 +
   1.259 +
   1.260 +// Dump region to file.
   1.261 +
   1.262 +void FileMapInfo::write_region(int region, char* base, size_t size,
   1.263 +                               size_t capacity, bool read_only,
   1.264 +                               bool allow_exec) {
   1.265 +  struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[region];
   1.266 +
   1.267 +  if (_file_open) {
   1.268 +    guarantee(si->_file_offset == _file_offset, "file offset mismatch.");
   1.269 +    if (PrintSharedSpaces) {
   1.270 +      tty->print_cr("Shared file region %d: 0x%6x bytes, addr " INTPTR_FORMAT
   1.271 +                    " file offset 0x%6x", region, size, base, _file_offset);
   1.272 +    }
   1.273 +  } else {
   1.274 +    si->_file_offset = _file_offset;
   1.275 +  }
   1.276 +  si->_base = base;
   1.277 +  si->_used = size;
   1.278 +  si->_capacity = capacity;
   1.279 +  si->_read_only = read_only;
   1.280 +  si->_allow_exec = allow_exec;
   1.281 +  si->_crc = ClassLoader::crc32(0, base, (jint)size);
   1.282 +  write_bytes_aligned(base, (int)size);
   1.283 +}
   1.284 +
   1.285 +
   1.286 +// Dump bytes to file -- at the current file position.
   1.287 +
   1.288 +void FileMapInfo::write_bytes(const void* buffer, int nbytes) {
   1.289 +  if (_file_open) {
   1.290 +    int n = ::write(_fd, buffer, nbytes);
   1.291 +    if (n != nbytes) {
   1.292 +      // It is dangerous to leave the corrupted shared archive file around,
   1.293 +      // close and remove the file. See bug 6372906.
   1.294 +      close();
   1.295 +      remove(_full_path);
   1.296 +      fail_stop("Unable to write to shared archive file.", NULL);
   1.297 +    }
   1.298 +  }
   1.299 +  _file_offset += nbytes;
   1.300 +}
   1.301 +
   1.302 +
   1.303 +// Align file position to an allocation unit boundary.
   1.304 +
   1.305 +void FileMapInfo::align_file_position() {
   1.306 +  size_t new_file_offset = align_size_up(_file_offset,
   1.307 +                                         os::vm_allocation_granularity());
   1.308 +  if (new_file_offset != _file_offset) {
   1.309 +    _file_offset = new_file_offset;
   1.310 +    if (_file_open) {
   1.311 +      // Seek one byte back from the target and write a byte to insure
   1.312 +      // that the written file is the correct length.
   1.313 +      _file_offset -= 1;
   1.314 +      if (lseek(_fd, (long)_file_offset, SEEK_SET) < 0) {
   1.315 +        fail_stop("Unable to seek.", NULL);
   1.316 +      }
   1.317 +      char zero = 0;
   1.318 +      write_bytes(&zero, 1);
   1.319 +    }
   1.320 +  }
   1.321 +}
   1.322 +
   1.323 +
   1.324 +// Dump bytes to file -- at the current file position.
   1.325 +
   1.326 +void FileMapInfo::write_bytes_aligned(const void* buffer, int nbytes) {
   1.327 +  align_file_position();
   1.328 +  write_bytes(buffer, nbytes);
   1.329 +  align_file_position();
   1.330 +}
   1.331 +
   1.332 +
   1.333 +// Close the shared archive file.  This does NOT unmap mapped regions.
   1.334 +
   1.335 +void FileMapInfo::close() {
   1.336 +  if (_file_open) {
   1.337 +    if (::close(_fd) < 0) {
   1.338 +      fail_stop("Unable to close the shared archive file.");
   1.339 +    }
   1.340 +    _file_open = false;
   1.341 +    _fd = -1;
   1.342 +  }
   1.343 +}
   1.344 +
   1.345 +
   1.346 +// JVM/TI RedefineClasses() support:
   1.347 +// Remap the shared readonly space to shared readwrite, private.
   1.348 +bool FileMapInfo::remap_shared_readonly_as_readwrite() {
   1.349 +  struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[0];
   1.350 +  if (!si->_read_only) {
   1.351 +    // the space is already readwrite so we are done
   1.352 +    return true;
   1.353 +  }
   1.354 +  size_t used = si->_used;
   1.355 +  size_t size = align_size_up(used, os::vm_allocation_granularity());
   1.356 +  if (!open_for_read()) {
   1.357 +    return false;
   1.358 +  }
   1.359 +  char *base = os::remap_memory(_fd, _full_path, si->_file_offset,
   1.360 +                                si->_base, size, false /* !read_only */,
   1.361 +                                si->_allow_exec);
   1.362 +  close();
   1.363 +  if (base == NULL) {
   1.364 +    fail_continue("Unable to remap shared readonly space (errno=%d).", errno);
   1.365 +    return false;
   1.366 +  }
   1.367 +  if (base != si->_base) {
   1.368 +    fail_continue("Unable to remap shared readonly space at required address.");
   1.369 +    return false;
   1.370 +  }
   1.371 +  si->_read_only = false;
   1.372 +  return true;
   1.373 +}
   1.374 +
   1.375 +// Map the whole region at once, assumed to be allocated contiguously.
   1.376 +ReservedSpace FileMapInfo::reserve_shared_memory() {
   1.377 +  struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[0];
   1.378 +  char* requested_addr = si->_base;
   1.379 +
   1.380 +  size_t size = FileMapInfo::shared_spaces_size();
   1.381 +
   1.382 +  // Reserve the space first, then map otherwise map will go right over some
   1.383 +  // other reserved memory (like the code cache).
   1.384 +  ReservedSpace rs(size, os::vm_allocation_granularity(), false, requested_addr);
   1.385 +  if (!rs.is_reserved()) {
   1.386 +    fail_continue(err_msg("Unable to reserve shared space at required address " INTPTR_FORMAT, requested_addr));
   1.387 +    return rs;
   1.388 +  }
   1.389 +  // the reserved virtual memory is for mapping class data sharing archive
   1.390 +  MemTracker::record_virtual_memory_type((address)rs.base(), mtClassShared);
   1.391 +
   1.392 +  return rs;
   1.393 +}
   1.394 +
   1.395 +// Memory map a region in the address space.
   1.396 +static const char* shared_region_name[] = { "ReadOnly", "ReadWrite", "MiscData", "MiscCode"};
   1.397 +
   1.398 +char* FileMapInfo::map_region(int i) {
   1.399 +  struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[i];
   1.400 +  size_t used = si->_used;
   1.401 +  size_t alignment = os::vm_allocation_granularity();
   1.402 +  size_t size = align_size_up(used, alignment);
   1.403 +  char *requested_addr = si->_base;
   1.404 +
   1.405 +  // map the contents of the CDS archive in this memory
   1.406 +  char *base = os::map_memory(_fd, _full_path, si->_file_offset,
   1.407 +                              requested_addr, size, si->_read_only,
   1.408 +                              si->_allow_exec);
   1.409 +  if (base == NULL || base != si->_base) {
   1.410 +    fail_continue(err_msg("Unable to map %s shared space at required address.", shared_region_name[i]));
   1.411 +    return NULL;
   1.412 +  }
   1.413 +#ifdef _WINDOWS
   1.414 +  // This call is Windows-only because the memory_type gets recorded for the other platforms
   1.415 +  // in method FileMapInfo::reserve_shared_memory(), which is not called on Windows.
   1.416 +  MemTracker::record_virtual_memory_type((address)base, mtClassShared);
   1.417 +#endif
   1.418 +  return base;
   1.419 +}
   1.420 +
   1.421 +bool FileMapInfo::verify_region_checksum(int i) {
   1.422 +  if (!VerifySharedSpaces) {
   1.423 +    return true;
   1.424 +  }
   1.425 +  const char* buf = _header._space[i]._base;
   1.426 +  size_t sz = _header._space[i]._used;
   1.427 +  int crc = ClassLoader::crc32(0, buf, (jint)sz);
   1.428 +  if (crc != _header._space[i]._crc) {
   1.429 +    fail_continue("Checksum verification failed.");
   1.430 +    return false;
   1.431 +  }
   1.432 +  return true;
   1.433 +}
   1.434 +
   1.435 +// Unmap a memory region in the address space.
   1.436 +
   1.437 +void FileMapInfo::unmap_region(int i) {
   1.438 +  struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[i];
   1.439 +  size_t used = si->_used;
   1.440 +  size_t size = align_size_up(used, os::vm_allocation_granularity());
   1.441 +  if (!os::unmap_memory(si->_base, size)) {
   1.442 +    fail_stop("Unable to unmap shared space.");
   1.443 +  }
   1.444 +}
   1.445 +
   1.446 +
   1.447 +void FileMapInfo::assert_mark(bool check) {
   1.448 +  if (!check) {
   1.449 +    fail_stop("Mark mismatch while restoring from shared file.", NULL);
   1.450 +  }
   1.451 +}
   1.452 +
   1.453 +
   1.454 +FileMapInfo* FileMapInfo::_current_info = NULL;
   1.455 +
   1.456 +
   1.457 +// Open the shared archive file, read and validate the header
   1.458 +// information (version, boot classpath, etc.).  If initialization
   1.459 +// fails, shared spaces are disabled and the file is closed. [See
   1.460 +// fail_continue.]
   1.461 +bool FileMapInfo::initialize() {
   1.462 +  assert(UseSharedSpaces, "UseSharedSpaces expected.");
   1.463 +
   1.464 +  if (JvmtiExport::can_modify_any_class() || JvmtiExport::can_walk_any_space()) {
   1.465 +    fail_continue("Tool agent requires sharing to be disabled.");
   1.466 +    return false;
   1.467 +  }
   1.468 +
   1.469 +  if (!open_for_read()) {
   1.470 +    return false;
   1.471 +  }
   1.472 +
   1.473 +  init_from_file(_fd);
   1.474 +  if (!validate()) {
   1.475 +    return false;
   1.476 +  }
   1.477 +
   1.478 +  SharedReadOnlySize =  _header._space[0]._capacity;
   1.479 +  SharedReadWriteSize = _header._space[1]._capacity;
   1.480 +  SharedMiscDataSize =  _header._space[2]._capacity;
   1.481 +  SharedMiscCodeSize =  _header._space[3]._capacity;
   1.482 +  return true;
   1.483 +}
   1.484 +
   1.485 +int FileMapInfo::compute_header_crc() {
   1.486 +  char* header = (char*)&_header;
   1.487 +  // start computing from the field after _crc
   1.488 +  char* buf = (char*)&_header._crc + sizeof(int);
   1.489 +  size_t sz = sizeof(FileMapInfo::FileMapHeader) - (buf - header);
   1.490 +  int crc = ClassLoader::crc32(0, buf, (jint)sz);
   1.491 +  return crc;
   1.492 +}
   1.493 +
   1.494 +bool FileMapInfo::validate() {
   1.495 +  if (VerifySharedSpaces && compute_header_crc() != _header._crc) {
   1.496 +    fail_continue("Header checksum verification failed.");
   1.497 +    return false;
   1.498 +  }
   1.499 +  if (_header._version != current_version()) {
   1.500 +    fail_continue("The shared archive file is the wrong version.");
   1.501 +    return false;
   1.502 +  }
   1.503 +  if (_header._magic != (int)0xf00baba2) {
   1.504 +    fail_continue("The shared archive file has a bad magic number.");
   1.505 +    return false;
   1.506 +  }
   1.507 +  char header_version[JVM_IDENT_MAX];
   1.508 +  get_header_version(header_version);
   1.509 +  if (strncmp(_header._jvm_ident, header_version, JVM_IDENT_MAX-1) != 0) {
   1.510 +    fail_continue("The shared archive file was created by a different"
   1.511 +                  " version or build of HotSpot.");
   1.512 +    return false;
   1.513 +  }
   1.514 +  if (_header._obj_alignment != ObjectAlignmentInBytes) {
   1.515 +    fail_continue("The shared archive file's ObjectAlignmentInBytes of %d"
   1.516 +                  " does not equal the current ObjectAlignmentInBytes of %d.",
   1.517 +                  _header._obj_alignment, ObjectAlignmentInBytes);
   1.518 +    return false;
   1.519 +  }
   1.520 +
   1.521 +  // Cannot verify interpreter yet, as it can only be created after the GC
   1.522 +  // heap has been initialized.
   1.523 +
   1.524 +  if (_header._num_jars >= JVM_SHARED_JARS_MAX) {
   1.525 +    fail_continue("Too many jar files to share.");
   1.526 +    return false;
   1.527 +  }
   1.528 +
   1.529 +  // Build checks on classpath and jar files
   1.530 +  int num_jars_now = 0;
   1.531 +  ClassPathEntry *cpe = ClassLoader::classpath_entry(0);
   1.532 +  for ( ; cpe != NULL; cpe = cpe->next()) {
   1.533 +
   1.534 +    if (cpe->is_jar_file()) {
   1.535 +      if (num_jars_now < _header._num_jars) {
   1.536 +
   1.537 +        // Jar file - verify timestamp and file size.
   1.538 +        struct stat st;
   1.539 +        const char *path = cpe->name();
   1.540 +        if (os::stat(path, &st) != 0) {
   1.541 +          fail_continue("Unable to open jar file %s.", path);
   1.542 +          return false;
   1.543 +        }
   1.544 +        if (_header._jar[num_jars_now]._timestamp != st.st_mtime ||
   1.545 +            _header._jar[num_jars_now]._filesize != st.st_size) {
   1.546 +          fail_continue("A jar file is not the one used while building"
   1.547 +                        " the shared archive file.");
   1.548 +          return false;
   1.549 +        }
   1.550 +      }
   1.551 +      ++num_jars_now;
   1.552 +    } else {
   1.553 +
   1.554 +      // If directories appear in boot classpath, they must be empty to
   1.555 +      // avoid having to verify each individual class file.
   1.556 +      const char* name = ((ClassPathDirEntry*)cpe)->name();
   1.557 +      if (!os::dir_is_empty(name)) {
   1.558 +        fail_continue("Boot classpath directory %s is not empty.", name);
   1.559 +        return false;
   1.560 +      }
   1.561 +    }
   1.562 +  }
   1.563 +  if (num_jars_now < _header._num_jars) {
   1.564 +    fail_continue("The number of jar files in the boot classpath is"
   1.565 +                  " less than the number the shared archive was created with.");
   1.566 +    return false;
   1.567 +  }
   1.568 +
   1.569 +  return true;
   1.570 +}
   1.571 +
   1.572 +// The following method is provided to see whether a given pointer
   1.573 +// falls in the mapped shared space.
   1.574 +// Param:
   1.575 +// p, The given pointer
   1.576 +// Return:
   1.577 +// True if the p is within the mapped shared space, otherwise, false.
   1.578 +bool FileMapInfo::is_in_shared_space(const void* p) {
   1.579 +  for (int i = 0; i < MetaspaceShared::n_regions; i++) {
   1.580 +    if (p >= _header._space[i]._base &&
   1.581 +        p < _header._space[i]._base + _header._space[i]._used) {
   1.582 +      return true;
   1.583 +    }
   1.584 +  }
   1.585 +
   1.586 +  return false;
   1.587 +}
   1.588 +
   1.589 +void FileMapInfo::print_shared_spaces() {
   1.590 +  gclog_or_tty->print_cr("Shared Spaces:");
   1.591 +  for (int i = 0; i < MetaspaceShared::n_regions; i++) {
   1.592 +    struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[i];
   1.593 +    gclog_or_tty->print("  %s " INTPTR_FORMAT "-" INTPTR_FORMAT,
   1.594 +                        shared_region_name[i],
   1.595 +                        si->_base, si->_base + si->_used);
   1.596 +  }
   1.597 +}
   1.598 +
   1.599 +// Unmap mapped regions of shared space.
   1.600 +void FileMapInfo::stop_sharing_and_unmap(const char* msg) {
   1.601 +  FileMapInfo *map_info = FileMapInfo::current_info();
   1.602 +  if (map_info) {
   1.603 +    map_info->fail_continue(msg);
   1.604 +    for (int i = 0; i < MetaspaceShared::n_regions; i++) {
   1.605 +      if (map_info->_header._space[i]._base != NULL) {
   1.606 +        map_info->unmap_region(i);
   1.607 +        map_info->_header._space[i]._base = NULL;
   1.608 +      }
   1.609 +    }
   1.610 +  } else if (DumpSharedSpaces) {
   1.611 +    fail_stop(msg, NULL);
   1.612 +  }
   1.613 +}

mercurial