1 /*
   2  * Copyright (c) 2003, 2013, 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  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/classLoader.hpp"
  27 #include "classfile/symbolTable.hpp"
  28 #include "classfile/altHashing.hpp"
  29 #include "memory/filemap.hpp"
  30 #include "runtime/arguments.hpp"
  31 #include "runtime/java.hpp"
  32 #include "runtime/os.hpp"
  33 #include "services/memTracker.hpp"
  34 #include "utilities/defaultStream.hpp"
  35 
  36 # include <sys/stat.h>
  37 # include <errno.h>
  38 
  39 #ifndef O_BINARY       // if defined (Win32) use binary files.
  40 #define O_BINARY 0     // otherwise do nothing.
  41 #endif
  42 
  43 
  44 extern address JVM_FunctionAtStart();
  45 extern address JVM_FunctionAtEnd();
  46 
  47 // Complain and stop. All error conditions occurring during the writing of
  48 // an archive file should stop the process.  Unrecoverable errors during
  49 // the reading of the archive file should stop the process.
  50 
  51 static void fail(const char *msg, va_list ap) {
  52   // This occurs very early during initialization: tty is not initialized.
  53   jio_fprintf(defaultStream::error_stream(),
  54               "An error has occurred while processing the"
  55               " shared archive file.\n");
  56   jio_vfprintf(defaultStream::error_stream(), msg, ap);
  57   jio_fprintf(defaultStream::error_stream(), "\n");
  58   vm_exit_during_initialization("Unable to use shared archive.", NULL);
  59 }
  60 
  61 
  62 void FileMapInfo::fail_stop(const char *msg, ...) {
  63         va_list ap;
  64   va_start(ap, msg);
  65   fail(msg, ap);        // Never returns.
  66   va_end(ap);           // for completeness.
  67 }
  68 
  69 
  70 // Complain and continue.  Recoverable errors during the reading of the
  71 // archive file may continue (with sharing disabled).
  72 //
  73 // If we continue, then disable shared spaces and close the file.
  74 
  75 void FileMapInfo::fail_continue(const char *msg, ...) {
  76   va_list ap;
  77   va_start(ap, msg);
  78   if (RequireSharedSpaces) {
  79     fail(msg, ap);
  80   }
  81   va_end(ap);
  82   UseSharedSpaces = false;
  83   close();
  84 }
  85 
  86 // Fill in the fileMapInfo structure with data about this VM instance.
  87 
  88 // This method copies the vm version info into header_version.  If the version is too
  89 // long then a truncated version, which has a hash code appended to it, is copied.
  90 //
  91 // Using a template enables this method to verify that header_version is an array of
  92 // length JVM_IDENT_MAX.  This ensures that the code that writes to the CDS file and
  93 // the code that reads the CDS file will both use the same size buffer.  Hence, will
  94 // use identical truncation.  This is necessary for matching of truncated versions.
  95 template <int N> static void get_header_version(char (&header_version) [N]) {
  96   assert(N == JVM_IDENT_MAX, "Bad header_version size");
  97 
  98   const char *vm_version = VM_Version::internal_vm_info_string();
  99   const int version_len = (int)strlen(vm_version);
 100 
 101   if (version_len < (JVM_IDENT_MAX-1)) {
 102     strcpy(header_version, vm_version);
 103 
 104   } else {
 105     // Get the hash value.  Use a static seed because the hash needs to return the same
 106     // value over multiple jvm invocations.
 107     unsigned int hash = AltHashing::murmur3_32(8191, (const jbyte*)vm_version, version_len);
 108 
 109     // Truncate the ident, saving room for the 8 hex character hash value.
 110     strncpy(header_version, vm_version, JVM_IDENT_MAX-9);
 111 
 112     // Append the hash code as eight hex digits.
 113     sprintf(&header_version[JVM_IDENT_MAX-9], "%08x", hash);
 114     header_version[JVM_IDENT_MAX-1] = 0;  // Null terminate.
 115   }
 116 }
 117 
 118 void FileMapInfo::populate_header(size_t alignment) {
 119   _header._magic = 0xf00baba2;
 120   _header._version = _current_version;
 121   _header._alignment = alignment;
 122   _header._obj_alignment = ObjectAlignmentInBytes;
 123 
 124   // The following fields are for sanity checks for whether this archive
 125   // will function correctly with this JVM and the bootclasspath it's
 126   // invoked with.
 127 
 128   // JVM version string ... changes on each build.
 129   get_header_version(_header._jvm_ident);
 130 
 131   // Build checks on classpath and jar files
 132   _header._num_jars = 0;
 133   ClassPathEntry *cpe = ClassLoader::classpath_entry(0);
 134   for ( ; cpe != NULL; cpe = cpe->next()) {
 135 
 136     if (cpe->is_jar_file()) {
 137       if (_header._num_jars >= JVM_SHARED_JARS_MAX) {
 138         fail_stop("Too many jar files to share.", NULL);
 139       }
 140 
 141       // Jar file - record timestamp and file size.
 142       struct stat st;
 143       const char *path = cpe->name();
 144       if (os::stat(path, &st) != 0) {
 145         // If we can't access a jar file in the boot path, then we can't
 146         // make assumptions about where classes get loaded from.
 147         fail_stop("Unable to open jar file %s.", path);
 148       }
 149       _header._jar[_header._num_jars]._timestamp = st.st_mtime;
 150       _header._jar[_header._num_jars]._filesize = st.st_size;
 151       _header._num_jars++;
 152     } else {
 153 
 154       // If directories appear in boot classpath, they must be empty to
 155       // avoid having to verify each individual class file.
 156       const char* name = ((ClassPathDirEntry*)cpe)->name();
 157       if (!os::dir_is_empty(name)) {
 158         fail_stop("Boot classpath directory %s is not empty.", name);
 159       }
 160     }
 161   }
 162 }
 163 
 164 
 165 // Read the FileMapInfo information from the file.
 166 
 167 bool FileMapInfo::init_from_file(int fd) {
 168 
 169   size_t n = read(fd, &_header, sizeof(struct FileMapHeader));
 170   if (n != sizeof(struct FileMapHeader)) {
 171     fail_continue("Unable to read the file header.");
 172     return false;
 173   }
 174   if (_header._version != current_version()) {
 175     fail_continue("The shared archive file has the wrong version.");
 176     return false;
 177   }
 178   _file_offset = (long)n;
 179   return true;
 180 }
 181 
 182 
 183 // Read the FileMapInfo information from the file.
 184 bool FileMapInfo::open_for_read() {
 185   _full_path = Arguments::GetSharedArchivePath();
 186   int fd = open(_full_path, O_RDONLY | O_BINARY, 0);
 187   if (fd < 0) {
 188     if (errno == ENOENT) {
 189       // Not locating the shared archive is ok.
 190       fail_continue("Specified shared archive not found.");
 191     } else {
 192       fail_continue("Failed to open shared archive file (%s).",
 193                     strerror(errno));
 194     }
 195     return false;
 196   }
 197 
 198   _fd = fd;
 199   _file_open = true;
 200   return true;
 201 }
 202 
 203 
 204 // Write the FileMapInfo information to the file.
 205 
 206 void FileMapInfo::open_for_write() {
 207  _full_path = Arguments::GetSharedArchivePath();
 208   if (PrintSharedSpaces) {
 209     tty->print_cr("Dumping shared data to file: ");
 210     tty->print_cr("   %s", _full_path);
 211   }
 212 
 213 #ifdef _WINDOWS  // On Windows, need WRITE permission to remove the file.
 214   chmod(_full_path, _S_IREAD | _S_IWRITE);
 215 #endif
 216 
 217   // Use remove() to delete the existing file because, on Unix, this will
 218   // allow processes that have it open continued access to the file.
 219   remove(_full_path);
 220   int fd = open(_full_path, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0444);
 221   if (fd < 0) {
 222     fail_stop("Unable to create shared archive file %s.", _full_path);
 223   }
 224   _fd = fd;
 225   _file_offset = 0;
 226   _file_open = true;
 227 }
 228 
 229 
 230 // Write the header to the file, seek to the next allocation boundary.
 231 
 232 void FileMapInfo::write_header() {
 233   write_bytes_aligned(&_header, sizeof(FileMapHeader));
 234 }
 235 
 236 
 237 // Dump shared spaces to file.
 238 
 239 void FileMapInfo::write_space(int i, Metaspace* space, bool read_only) {
 240   align_file_position();
 241   size_t used = space->used_bytes_slow(Metaspace::NonClassType);
 242   size_t capacity = space->capacity_bytes_slow(Metaspace::NonClassType);
 243   struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[i];
 244   write_region(i, (char*)space->bottom(), used, capacity, read_only, false);
 245 }
 246 
 247 
 248 // Dump region to file.
 249 
 250 void FileMapInfo::write_region(int region, char* base, size_t size,
 251                                size_t capacity, bool read_only,
 252                                bool allow_exec) {
 253   struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[region];
 254 
 255   if (_file_open) {
 256     guarantee(si->_file_offset == _file_offset, "file offset mismatch.");
 257     if (PrintSharedSpaces) {
 258       tty->print_cr("Shared file region %d: 0x%6x bytes, addr " INTPTR_FORMAT
 259                     " file offset 0x%6x", region, size, base, _file_offset);
 260     }
 261   } else {
 262     si->_file_offset = _file_offset;
 263   }
 264   si->_base = base;
 265   si->_used = size;
 266   si->_capacity = capacity;
 267   si->_read_only = read_only;
 268   si->_allow_exec = allow_exec;
 269   write_bytes_aligned(base, (int)size);
 270 }
 271 
 272 
 273 // Dump bytes to file -- at the current file position.
 274 
 275 void FileMapInfo::write_bytes(const void* buffer, int nbytes) {
 276   if (_file_open) {
 277     int n = ::write(_fd, buffer, nbytes);
 278     if (n != nbytes) {
 279       // It is dangerous to leave the corrupted shared archive file around,
 280       // close and remove the file. See bug 6372906.
 281       close();
 282       remove(_full_path);
 283       fail_stop("Unable to write to shared archive file.", NULL);
 284     }
 285   }
 286   _file_offset += nbytes;
 287 }
 288 
 289 
 290 // Align file position to an allocation unit boundary.
 291 
 292 void FileMapInfo::align_file_position() {
 293   long new_file_offset = align_size_up(_file_offset, os::vm_allocation_granularity());
 294   if (new_file_offset != _file_offset) {
 295     _file_offset = new_file_offset;
 296     if (_file_open) {
 297       // Seek one byte back from the target and write a byte to insure
 298       // that the written file is the correct length.
 299       _file_offset -= 1;
 300       if (lseek(_fd, _file_offset, SEEK_SET) < 0) {
 301         fail_stop("Unable to seek.", NULL);
 302       }
 303       char zero = 0;
 304       write_bytes(&zero, 1);
 305     }
 306   }
 307 }
 308 
 309 
 310 // Dump bytes to file -- at the current file position.
 311 
 312 void FileMapInfo::write_bytes_aligned(const void* buffer, int nbytes) {
 313   align_file_position();
 314   write_bytes(buffer, nbytes);
 315   align_file_position();
 316 }
 317 
 318 
 319 // Close the shared archive file.  This does NOT unmap mapped regions.
 320 
 321 void FileMapInfo::close() {
 322   if (_file_open) {
 323     if (::close(_fd) < 0) {
 324       fail_stop("Unable to close the shared archive file.");
 325     }
 326     _file_open = false;
 327     _fd = -1;
 328   }
 329 }
 330 
 331 
 332 // JVM/TI RedefineClasses() support:
 333 // Remap the shared readonly space to shared readwrite, private.
 334 bool FileMapInfo::remap_shared_readonly_as_readwrite() {
 335   struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[0];
 336   if (!si->_read_only) {
 337     // the space is already readwrite so we are done
 338     return true;
 339   }
 340   size_t used = si->_used;
 341   size_t size = align_size_up(used, os::vm_allocation_granularity());
 342   if (!open_for_read()) {
 343     return false;
 344   }
 345   char *base = os::remap_memory(_fd, _full_path, si->_file_offset,
 346                                 si->_base, size, false /* !read_only */,
 347                                 si->_allow_exec);
 348   close();
 349   if (base == NULL) {
 350     fail_continue("Unable to remap shared readonly space (errno=%d).", errno);
 351     return false;
 352   }
 353   if (base != si->_base) {
 354     fail_continue("Unable to remap shared readonly space at required address.");
 355     return false;
 356   }
 357   si->_read_only = false;
 358   return true;
 359 }
 360 
 361 // Map the whole region at once, assumed to be allocated contiguously.
 362 ReservedSpace FileMapInfo::reserve_shared_memory() {
 363   struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[0];
 364   char* requested_addr = si->_base;
 365 
 366   size_t size = FileMapInfo::shared_spaces_size();
 367 
 368   // Reserve the space first, then map otherwise map will go right over some
 369   // other reserved memory (like the code cache).
 370   ReservedSpace rs(size, os::vm_allocation_granularity(), false, requested_addr);
 371   if (!rs.is_reserved()) {
 372     fail_continue(err_msg("Unable to reserve shared space at required address " INTPTR_FORMAT, requested_addr));
 373     return rs;
 374   }
 375   // the reserved virtual memory is for mapping class data sharing archive
 376   MemTracker::record_virtual_memory_type((address)rs.base(), mtClassShared);
 377 
 378   return rs;
 379 }
 380 
 381 // Memory map a region in the address space.
 382 static const char* shared_region_name[] = { "ReadOnly", "ReadWrite", "MiscData", "MiscCode"};
 383 
 384 char* FileMapInfo::map_region(int i) {
 385   struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[i];
 386   size_t used = si->_used;
 387   size_t alignment = os::vm_allocation_granularity();
 388   size_t size = align_size_up(used, alignment);
 389   char *requested_addr = si->_base;
 390 
 391   // map the contents of the CDS archive in this memory
 392   char *base = os::map_memory(_fd, _full_path, si->_file_offset,
 393                               requested_addr, size, si->_read_only,
 394                               si->_allow_exec);
 395   if (base == NULL || base != si->_base) {
 396     fail_continue(err_msg("Unable to map %s shared space at required address.", shared_region_name[i]));
 397     return NULL;
 398   }
 399 #ifdef _WINDOWS
 400   // This call is Windows-only because the memory_type gets recorded for the other platforms
 401   // in method FileMapInfo::reserve_shared_memory(), which is not called on Windows.
 402   MemTracker::record_virtual_memory_type((address)base, mtClassShared);
 403 #endif
 404   return base;
 405 }
 406 
 407 
 408 // Unmap a memory region in the address space.
 409 
 410 void FileMapInfo::unmap_region(int i) {
 411   struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[i];
 412   size_t used = si->_used;
 413   size_t size = align_size_up(used, os::vm_allocation_granularity());
 414   if (!os::unmap_memory(si->_base, size)) {
 415     fail_stop("Unable to unmap shared space.");
 416   }
 417 }
 418 
 419 
 420 void FileMapInfo::assert_mark(bool check) {
 421   if (!check) {
 422     fail_stop("Mark mismatch while restoring from shared file.", NULL);
 423   }
 424 }
 425 
 426 
 427 FileMapInfo* FileMapInfo::_current_info = NULL;
 428 
 429 
 430 // Open the shared archive file, read and validate the header
 431 // information (version, boot classpath, etc.).  If initialization
 432 // fails, shared spaces are disabled and the file is closed. [See
 433 // fail_continue.]
 434 bool FileMapInfo::initialize() {
 435   assert(UseSharedSpaces, "UseSharedSpaces expected.");
 436 
 437   if (JvmtiExport::can_modify_any_class() || JvmtiExport::can_walk_any_space()) {
 438     fail_continue("Tool agent requires sharing to be disabled.");
 439     return false;
 440   }
 441 
 442   if (!open_for_read()) {
 443     return false;
 444   }
 445 
 446   init_from_file(_fd);
 447   if (!validate()) {
 448     return false;
 449   }
 450 
 451   SharedReadOnlySize =  _header._space[0]._capacity;
 452   SharedReadWriteSize = _header._space[1]._capacity;
 453   SharedMiscDataSize =  _header._space[2]._capacity;
 454   SharedMiscCodeSize =  _header._space[3]._capacity;
 455   return true;
 456 }
 457 
 458 
 459 bool FileMapInfo::validate() {
 460   if (_header._version != current_version()) {
 461     fail_continue("The shared archive file is the wrong version.");
 462     return false;
 463   }
 464   if (_header._magic != (int)0xf00baba2) {
 465     fail_continue("The shared archive file has a bad magic number.");
 466     return false;
 467   }
 468   char header_version[JVM_IDENT_MAX];
 469   get_header_version(header_version);
 470   if (strncmp(_header._jvm_ident, header_version, JVM_IDENT_MAX-1) != 0) {
 471     fail_continue("The shared archive file was created by a different"
 472                   " version or build of HotSpot.");
 473     return false;
 474   }
 475   if (_header._obj_alignment != ObjectAlignmentInBytes) {
 476     fail_continue("The shared archive file's ObjectAlignmentInBytes of %d"
 477                   " does not equal the current ObjectAlignmentInBytes of %d.",
 478                   _header._obj_alignment, ObjectAlignmentInBytes);
 479     return false;
 480   }
 481 
 482   // Cannot verify interpreter yet, as it can only be created after the GC
 483   // heap has been initialized.
 484 
 485   if (_header._num_jars >= JVM_SHARED_JARS_MAX) {
 486     fail_continue("Too many jar files to share.");
 487     return false;
 488   }
 489 
 490   // Build checks on classpath and jar files
 491   int num_jars_now = 0;
 492   ClassPathEntry *cpe = ClassLoader::classpath_entry(0);
 493   for ( ; cpe != NULL; cpe = cpe->next()) {
 494 
 495     if (cpe->is_jar_file()) {
 496       if (num_jars_now < _header._num_jars) {
 497 
 498         // Jar file - verify timestamp and file size.
 499         struct stat st;
 500         const char *path = cpe->name();
 501         if (os::stat(path, &st) != 0) {
 502           fail_continue("Unable to open jar file %s.", path);
 503           return false;
 504         }
 505         if (_header._jar[num_jars_now]._timestamp != st.st_mtime ||
 506             _header._jar[num_jars_now]._filesize != st.st_size) {
 507           fail_continue("A jar file is not the one used while building"
 508                         " the shared archive file.");
 509           return false;
 510         }
 511       }
 512       ++num_jars_now;
 513     } else {
 514 
 515       // If directories appear in boot classpath, they must be empty to
 516       // avoid having to verify each individual class file.
 517       const char* name = ((ClassPathDirEntry*)cpe)->name();
 518       if (!os::dir_is_empty(name)) {
 519         fail_continue("Boot classpath directory %s is not empty.", name);
 520         return false;
 521       }
 522     }
 523   }
 524   if (num_jars_now < _header._num_jars) {
 525     fail_continue("The number of jar files in the boot classpath is"
 526                   " less than the number the shared archive was created with.");
 527     return false;
 528   }
 529 
 530   return true;
 531 }
 532 
 533 // The following method is provided to see whether a given pointer
 534 // falls in the mapped shared space.
 535 // Param:
 536 // p, The given pointer
 537 // Return:
 538 // True if the p is within the mapped shared space, otherwise, false.
 539 bool FileMapInfo::is_in_shared_space(const void* p) {
 540   for (int i = 0; i < MetaspaceShared::n_regions; i++) {
 541     if (p >= _header._space[i]._base &&
 542         p < _header._space[i]._base + _header._space[i]._used) {
 543       return true;
 544     }
 545   }
 546 
 547   return false;
 548 }
 549 
 550 void FileMapInfo::print_shared_spaces() {
 551   gclog_or_tty->print_cr("Shared Spaces:");
 552   for (int i = 0; i < MetaspaceShared::n_regions; i++) {
 553     struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[i];
 554     gclog_or_tty->print("  %s " INTPTR_FORMAT "-" INTPTR_FORMAT,
 555                         shared_region_name[i],
 556                         si->_base, si->_base + si->_used);
 557   }
 558 }
 559 
 560 // Unmap mapped regions of shared space.
 561 void FileMapInfo::stop_sharing_and_unmap(const char* msg) {
 562   FileMapInfo *map_info = FileMapInfo::current_info();
 563   if (map_info) {
 564     map_info->fail_continue(msg);
 565     for (int i = 0; i < MetaspaceShared::n_regions; i++) {
 566       if (map_info->_header._space[i]._base != NULL) {
 567         map_info->unmap_region(i);
 568         map_info->_header._space[i]._base = NULL;
 569       }
 570     }
 571   } else if (DumpSharedSpaces) {
 572     fail_stop(msg, NULL);
 573   }
 574 }