src/share/vm/memory/filemap.cpp

Tue, 09 Oct 2012 10:09:34 -0700

author
mikael
date
Tue, 09 Oct 2012 10:09:34 -0700
changeset 4153
b9a9ed0f8eeb
parent 4037
da91efe96a93
child 4193
716c64bda5ba
permissions
-rw-r--r--

7197424: update copyright year to match last edit in jdk8 hotspot repository
Summary: Update copyright year to 2012 for relevant files
Reviewed-by: dholmes, coleenp

     1 /*
     2  * Copyright (c) 2003, 2012, 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/symbolTable.hpp"
    28 #include "memory/filemap.hpp"
    29 #include "runtime/arguments.hpp"
    30 #include "runtime/java.hpp"
    31 #include "runtime/os.hpp"
    32 #include "utilities/defaultStream.hpp"
    34 # include <sys/stat.h>
    35 # include <errno.h>
    37 #ifndef O_BINARY       // if defined (Win32) use binary files.
    38 #define O_BINARY 0     // otherwise do nothing.
    39 #endif
    42 extern address JVM_FunctionAtStart();
    43 extern address JVM_FunctionAtEnd();
    45 // Complain and stop. All error conditions occurring during the writing of
    46 // an archive file should stop the process.  Unrecoverable errors during
    47 // the reading of the archive file should stop the process.
    49 static void fail(const char *msg, va_list ap) {
    50   // This occurs very early during initialization: tty is not initialized.
    51   jio_fprintf(defaultStream::error_stream(),
    52               "An error has occurred while processing the"
    53               " shared archive file.\n");
    54   jio_vfprintf(defaultStream::error_stream(), msg, ap);
    55   jio_fprintf(defaultStream::error_stream(), "\n");
    56   vm_exit_during_initialization("Unable to use shared archive.", NULL);
    57 }
    60 void FileMapInfo::fail_stop(const char *msg, ...) {
    61         va_list ap;
    62   va_start(ap, msg);
    63   fail(msg, ap);        // Never returns.
    64   va_end(ap);           // for completeness.
    65 }
    68 // Complain and continue.  Recoverable errors during the reading of the
    69 // archive file may continue (with sharing disabled).
    70 //
    71 // If we continue, then disable shared spaces and close the file.
    73 void FileMapInfo::fail_continue(const char *msg, ...) {
    74   va_list ap;
    75   va_start(ap, msg);
    76   if (RequireSharedSpaces) {
    77     fail(msg, ap);
    78   }
    79   va_end(ap);
    80   UseSharedSpaces = false;
    81   close();
    82 }
    85 // Fill in the fileMapInfo structure with data about this VM instance.
    87 void FileMapInfo::populate_header(size_t alignment) {
    88   _header._magic = 0xf00baba2;
    89   _header._version = _current_version;
    90   _header._alignment = alignment;
    92   // The following fields are for sanity checks for whether this archive
    93   // will function correctly with this JVM and the bootclasspath it's
    94   // invoked with.
    96   // JVM version string ... changes on each build.
    97   const char *vm_version = VM_Version::internal_vm_info_string();
    98   if (strlen(vm_version) < (JVM_IDENT_MAX-1)) {
    99     strcpy(_header._jvm_ident, vm_version);
   100   } else {
   101     fail_stop("JVM Ident field for shared archive is too long"
   102               " - truncated to <%s>", _header._jvm_ident);
   103   }
   105   // Build checks on classpath and jar files
   106   _header._num_jars = 0;
   107   ClassPathEntry *cpe = ClassLoader::classpath_entry(0);
   108   for ( ; cpe != NULL; cpe = cpe->next()) {
   110     if (cpe->is_jar_file()) {
   111       if (_header._num_jars >= JVM_SHARED_JARS_MAX) {
   112         fail_stop("Too many jar files to share.", NULL);
   113       }
   115       // Jar file - record timestamp and file size.
   116       struct stat st;
   117       const char *path = cpe->name();
   118       if (os::stat(path, &st) != 0) {
   119         // If we can't access a jar file in the boot path, then we can't
   120         // make assumptions about where classes get loaded from.
   121         fail_stop("Unable to open jar file %s.", path);
   122       }
   123       _header._jar[_header._num_jars]._timestamp = st.st_mtime;
   124       _header._jar[_header._num_jars]._filesize = st.st_size;
   125       _header._num_jars++;
   126     } else {
   128       // If directories appear in boot classpath, they must be empty to
   129       // avoid having to verify each individual class file.
   130       const char* name = ((ClassPathDirEntry*)cpe)->name();
   131       if (!os::dir_is_empty(name)) {
   132         fail_stop("Boot classpath directory %s is not empty.", name);
   133       }
   134     }
   135   }
   136 }
   139 // Read the FileMapInfo information from the file.
   141 bool FileMapInfo::init_from_file(int fd) {
   143   size_t n = read(fd, &_header, sizeof(struct FileMapHeader));
   144   if (n != sizeof(struct FileMapHeader)) {
   145     fail_continue("Unable to read the file header.");
   146     return false;
   147   }
   148   if (_header._version != current_version()) {
   149     fail_continue("The shared archive file has the wrong version.");
   150     return false;
   151   }
   152   _file_offset = (long)n;
   153   return true;
   154 }
   157 // Read the FileMapInfo information from the file.
   158 bool FileMapInfo::open_for_read() {
   159   _full_path = Arguments::GetSharedArchivePath();
   160   int fd = open(_full_path, O_RDONLY | O_BINARY, 0);
   161   if (fd < 0) {
   162     if (errno == ENOENT) {
   163       // Not locating the shared archive is ok.
   164       fail_continue("Specified shared archive not found.");
   165     } else {
   166       fail_continue("Failed to open shared archive file (%s).",
   167                     strerror(errno));
   168     }
   169     return false;
   170   }
   172   _fd = fd;
   173   _file_open = true;
   174   return true;
   175 }
   178 // Write the FileMapInfo information to the file.
   180 void FileMapInfo::open_for_write() {
   181  _full_path = Arguments::GetSharedArchivePath();
   182   if (PrintSharedSpaces) {
   183     tty->print_cr("Dumping shared data to file: ");
   184     tty->print_cr("   %s", _full_path);
   185   }
   187   // Remove the existing file in case another process has it open.
   188   remove(_full_path);
   189   int fd = open(_full_path, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0444);
   190   if (fd < 0) {
   191     fail_stop("Unable to create shared archive file %s.", _full_path);
   192   }
   193   _fd = fd;
   194   _file_offset = 0;
   195   _file_open = true;
   196 }
   199 // Write the header to the file, seek to the next allocation boundary.
   201 void FileMapInfo::write_header() {
   202   write_bytes_aligned(&_header, sizeof(FileMapHeader));
   203 }
   206 // Dump shared spaces to file.
   208 void FileMapInfo::write_space(int i, Metaspace* space, bool read_only) {
   209   align_file_position();
   210   size_t used = space->used_words(Metaspace::NonClassType) * BytesPerWord;
   211   size_t capacity = space->capacity_words(Metaspace::NonClassType) * BytesPerWord;
   212   struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[i];
   213   write_region(i, (char*)space->bottom(), used, capacity, read_only, false);
   214 }
   217 // Dump region to file.
   219 void FileMapInfo::write_region(int region, char* base, size_t size,
   220                                size_t capacity, bool read_only,
   221                                bool allow_exec) {
   222   struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[region];
   224   if (_file_open) {
   225     guarantee(si->_file_offset == _file_offset, "file offset mismatch.");
   226     if (PrintSharedSpaces) {
   227       tty->print_cr("Shared file region %d: 0x%6x bytes, addr " INTPTR_FORMAT
   228                     " file offset 0x%6x", region, size, base, _file_offset);
   229     }
   230   } else {
   231     si->_file_offset = _file_offset;
   232   }
   233   si->_base = base;
   234   si->_used = size;
   235   si->_capacity = capacity;
   236   si->_read_only = read_only;
   237   si->_allow_exec = allow_exec;
   238   write_bytes_aligned(base, (int)size);
   239 }
   242 // Dump bytes to file -- at the current file position.
   244 void FileMapInfo::write_bytes(const void* buffer, int nbytes) {
   245   if (_file_open) {
   246     int n = ::write(_fd, buffer, nbytes);
   247     if (n != nbytes) {
   248       // It is dangerous to leave the corrupted shared archive file around,
   249       // close and remove the file. See bug 6372906.
   250       close();
   251       remove(_full_path);
   252       fail_stop("Unable to write to shared archive file.", NULL);
   253     }
   254   }
   255   _file_offset += nbytes;
   256 }
   259 // Align file position to an allocation unit boundary.
   261 void FileMapInfo::align_file_position() {
   262   long new_file_offset = align_size_up(_file_offset, os::vm_allocation_granularity());
   263   if (new_file_offset != _file_offset) {
   264     _file_offset = new_file_offset;
   265     if (_file_open) {
   266       // Seek one byte back from the target and write a byte to insure
   267       // that the written file is the correct length.
   268       _file_offset -= 1;
   269       if (lseek(_fd, _file_offset, SEEK_SET) < 0) {
   270         fail_stop("Unable to seek.", NULL);
   271       }
   272       char zero = 0;
   273       write_bytes(&zero, 1);
   274     }
   275   }
   276 }
   279 // Dump bytes to file -- at the current file position.
   281 void FileMapInfo::write_bytes_aligned(const void* buffer, int nbytes) {
   282   align_file_position();
   283   write_bytes(buffer, nbytes);
   284   align_file_position();
   285 }
   288 // Close the shared archive file.  This does NOT unmap mapped regions.
   290 void FileMapInfo::close() {
   291   if (_file_open) {
   292     if (::close(_fd) < 0) {
   293       fail_stop("Unable to close the shared archive file.");
   294     }
   295     _file_open = false;
   296     _fd = -1;
   297   }
   298 }
   301 // JVM/TI RedefineClasses() support:
   302 // Remap the shared readonly space to shared readwrite, private.
   303 bool FileMapInfo::remap_shared_readonly_as_readwrite() {
   304   struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[0];
   305   if (!si->_read_only) {
   306     // the space is already readwrite so we are done
   307     return true;
   308   }
   309   size_t used = si->_used;
   310   size_t size = align_size_up(used, os::vm_allocation_granularity());
   311   if (!open_for_read()) {
   312     return false;
   313   }
   314   char *base = os::remap_memory(_fd, _full_path, si->_file_offset,
   315                                 si->_base, size, false /* !read_only */,
   316                                 si->_allow_exec);
   317   close();
   318   if (base == NULL) {
   319     fail_continue("Unable to remap shared readonly space (errno=%d).", errno);
   320     return false;
   321   }
   322   if (base != si->_base) {
   323     fail_continue("Unable to remap shared readonly space at required address.");
   324     return false;
   325   }
   326   si->_read_only = false;
   327   return true;
   328 }
   330 // Map the whole region at once, assumed to be allocated contiguously.
   331 ReservedSpace FileMapInfo::reserve_shared_memory() {
   332   struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[0];
   333   char* requested_addr = si->_base;
   334   size_t alignment = os::vm_allocation_granularity();
   336   size_t size = align_size_up(SharedReadOnlySize + SharedReadWriteSize +
   337                               SharedMiscDataSize + SharedMiscCodeSize,
   338                               alignment);
   340   // Reserve the space first, then map otherwise map will go right over some
   341   // other reserved memory (like the code cache).
   342   ReservedSpace rs(size, alignment, false, requested_addr);
   343   if (!rs.is_reserved()) {
   344     fail_continue(err_msg("Unable to reserved shared space at required address " INTPTR_FORMAT, requested_addr));
   345     return rs;
   346   }
   347   return rs;
   348 }
   350 // Memory map a region in the address space.
   352 char* FileMapInfo::map_region(int i, ReservedSpace rs) {
   353   struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[i];
   354   size_t used = si->_used;
   355   size_t size = align_size_up(used, os::vm_allocation_granularity());
   357   ReservedSpace mapped_rs = rs.first_part(size, true, true);
   358   ReservedSpace unmapped_rs = rs.last_part(size);
   359   mapped_rs.release();
   361   return map_region(i);
   362 }
   365 // Memory map a region in the address space.
   366 static const char* shared_region_name[] = { "ReadOnly", "ReadWrite", "MiscData", "MiscCode"};
   368 char* FileMapInfo::map_region(int i) {
   369   struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[i];
   370   size_t used = si->_used;
   371   size_t alignment = os::vm_allocation_granularity();
   372   size_t size = align_size_up(used, alignment);
   373   char *requested_addr = si->_base;
   375   // map the contents of the CDS archive in this memory
   376   char *base = os::map_memory(_fd, _full_path, si->_file_offset,
   377                               requested_addr, size, si->_read_only,
   378                               si->_allow_exec);
   379   if (base == NULL || base != si->_base) {
   380     fail_continue(err_msg("Unable to map %s shared space at required address.", shared_region_name[i]));
   381     return NULL;
   382   }
   383   return base;
   384 }
   387 // Unmap a memory region in the address space.
   389 void FileMapInfo::unmap_region(int i) {
   390   struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[i];
   391   size_t used = si->_used;
   392   size_t size = align_size_up(used, os::vm_allocation_granularity());
   393   if (!os::unmap_memory(si->_base, size)) {
   394     fail_stop("Unable to unmap shared space.");
   395   }
   396 }
   399 void FileMapInfo::assert_mark(bool check) {
   400   if (!check) {
   401     fail_stop("Mark mismatch while restoring from shared file.", NULL);
   402   }
   403 }
   406 FileMapInfo* FileMapInfo::_current_info = NULL;
   409 // Open the shared archive file, read and validate the header
   410 // information (version, boot classpath, etc.).  If initialization
   411 // fails, shared spaces are disabled and the file is closed. [See
   412 // fail_continue.]
   413 bool FileMapInfo::initialize() {
   414   assert(UseSharedSpaces, "UseSharedSpaces expected.");
   416   if (JvmtiExport::can_modify_any_class() || JvmtiExport::can_walk_any_space()) {
   417     fail_continue("Tool agent requires sharing to be disabled.");
   418     return false;
   419   }
   421   if (!open_for_read()) {
   422     return false;
   423   }
   425   init_from_file(_fd);
   426   if (!validate()) {
   427     return false;
   428   }
   430   SharedReadOnlySize =  _header._space[0]._capacity;
   431   SharedReadWriteSize = _header._space[1]._capacity;
   432   SharedMiscDataSize =  _header._space[2]._capacity;
   433   SharedMiscCodeSize =  _header._space[3]._capacity;
   434   return true;
   435 }
   438 bool FileMapInfo::validate() {
   439   if (_header._version != current_version()) {
   440     fail_continue("The shared archive file is the wrong version.");
   441     return false;
   442   }
   443   if (_header._magic != (int)0xf00baba2) {
   444     fail_continue("The shared archive file has a bad magic number.");
   445     return false;
   446   }
   447   if (strncmp(_header._jvm_ident, VM_Version::internal_vm_info_string(),
   448               JVM_IDENT_MAX-1) != 0) {
   449     fail_continue("The shared archive file was created by a different"
   450                   " version or build of HotSpot.");
   451     return false;
   452   }
   454   // Cannot verify interpreter yet, as it can only be created after the GC
   455   // heap has been initialized.
   457   if (_header._num_jars >= JVM_SHARED_JARS_MAX) {
   458     fail_continue("Too many jar files to share.");
   459     return false;
   460   }
   462   // Build checks on classpath and jar files
   463   int num_jars_now = 0;
   464   ClassPathEntry *cpe = ClassLoader::classpath_entry(0);
   465   for ( ; cpe != NULL; cpe = cpe->next()) {
   467     if (cpe->is_jar_file()) {
   468       if (num_jars_now < _header._num_jars) {
   470         // Jar file - verify timestamp and file size.
   471         struct stat st;
   472         const char *path = cpe->name();
   473         if (os::stat(path, &st) != 0) {
   474           fail_continue("Unable to open jar file %s.", path);
   475           return false;
   476         }
   477         if (_header._jar[num_jars_now]._timestamp != st.st_mtime ||
   478             _header._jar[num_jars_now]._filesize != st.st_size) {
   479           fail_continue("A jar file is not the one used while building"
   480                         " the shared archive file.");
   481           return false;
   482         }
   483       }
   484       ++num_jars_now;
   485     } else {
   487       // If directories appear in boot classpath, they must be empty to
   488       // avoid having to verify each individual class file.
   489       const char* name = ((ClassPathDirEntry*)cpe)->name();
   490       if (!os::dir_is_empty(name)) {
   491         fail_continue("Boot classpath directory %s is not empty.", name);
   492         return false;
   493       }
   494     }
   495   }
   496   if (num_jars_now < _header._num_jars) {
   497     fail_continue("The number of jar files in the boot classpath is"
   498                   " less than the number the shared archive was created with.");
   499     return false;
   500   }
   502   return true;
   503 }
   505 // The following method is provided to see whether a given pointer
   506 // falls in the mapped shared space.
   507 // Param:
   508 // p, The given pointer
   509 // Return:
   510 // True if the p is within the mapped shared space, otherwise, false.
   511 bool FileMapInfo::is_in_shared_space(const void* p) {
   512   for (int i = 0; i < MetaspaceShared::n_regions; i++) {
   513     if (p >= _header._space[i]._base &&
   514         p < _header._space[i]._base + _header._space[i]._used) {
   515       return true;
   516     }
   517   }
   519   return false;
   520 }

mercurial