src/share/vm/memory/filemap.cpp

Tue, 30 Oct 2012 10:23:55 -0700

author
jmasa
date
Tue, 30 Oct 2012 10:23:55 -0700
changeset 4234
3fadc0e8cffe
parent 4193
716c64bda5ba
child 4293
fe81517cfb77
permissions
-rw-r--r--

8000988: VM deadlock when running btree006 on windows-i586
Reviewed-by: johnc, jcoomes, ysr

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

mercurial