src/share/vm/memory/filemap.cpp

Tue, 16 Feb 2016 21:42:29 +0000

author
poonam
date
Tue, 16 Feb 2016 21:42:29 +0000
changeset 8308
6acf14e730dd
parent 7414
0558eb13dcf3
child 7535
7ae4e26cb1e0
child 10008
fd3484fadbe3
permissions
-rw-r--r--

8072725: Provide more granular levels for GC verification
Summary: Add option VerifySubSet to selectively verify the memory sub-systems
Reviewed-by: kevinw, jmasa

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

mercurial