< prev index next >

src/share/vm/memory/filemap.cpp

Print this page




  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/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 "runtime/vm_version.hpp"
  39 #include "services/memTracker.hpp"
  40 #include "utilities/defaultStream.hpp"
  41 
  42 # include <sys/stat.h>
  43 # include <errno.h>
  44 
  45 #ifndef O_BINARY       // if defined (Win32) use binary files.
  46 #define O_BINARY 0     // otherwise do nothing.
  47 #endif
  48 
  49 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
  50 extern address JVM_FunctionAtStart();


 148 }
 149 
 150 FileMapInfo::~FileMapInfo() {
 151   assert(_current_info == this, "must be singleton"); // not thread safe
 152   _current_info = NULL;
 153 }
 154 
 155 void FileMapInfo::populate_header(size_t alignment) {
 156   _header->populate(this, alignment);
 157 }
 158 
 159 size_t FileMapInfo::FileMapHeader::data_size() {
 160   return SharedClassUtil::file_map_header_size() - sizeof(FileMapInfo::FileMapHeaderBase);
 161 }
 162 
 163 void FileMapInfo::FileMapHeader::populate(FileMapInfo* mapinfo, size_t alignment) {
 164   _magic = 0xf00baba2;
 165   _version = _current_version;
 166   _alignment = alignment;
 167   _obj_alignment = ObjectAlignmentInBytes;



 168   _classpath_entry_table_size = mapinfo->_classpath_entry_table_size;
 169   _classpath_entry_table = mapinfo->_classpath_entry_table;
 170   _classpath_entry_size = mapinfo->_classpath_entry_size;
 171 
 172   // The following fields are for sanity checks for whether this archive
 173   // will function correctly with this JVM and the bootclasspath it's
 174   // invoked with.
 175 
 176   // JVM version string ... changes on each build.
 177   get_header_version(_jvm_ident);
 178 }
 179 
 180 void FileMapInfo::allocate_classpath_entry_table() {
 181   int bytes = 0;
 182   int count = 0;
 183   char* strptr = NULL;
 184   char* strptr_max = NULL;
 185   Thread* THREAD = Thread::current();
 186 
 187   ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();


 423   write_region(i, (char*)space->bottom(), used, capacity, read_only, false);
 424 }
 425 
 426 
 427 // Dump region to file.
 428 
 429 void FileMapInfo::write_region(int region, char* base, size_t size,
 430                                size_t capacity, bool read_only,
 431                                bool allow_exec) {
 432   struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[region];
 433 
 434   if (_file_open) {
 435     guarantee(si->_file_offset == _file_offset, "file offset mismatch.");
 436     if (PrintSharedSpaces) {
 437       tty->print_cr("Shared file region %d: 0x%6x bytes, addr " INTPTR_FORMAT
 438                     " file offset 0x%6x", region, size, base, _file_offset);
 439     }
 440   } else {
 441     si->_file_offset = _file_offset;
 442   }
 443   si->_base = base;









 444   si->_used = size;
 445   si->_capacity = capacity;
 446   si->_read_only = read_only;
 447   si->_allow_exec = allow_exec;
 448   si->_crc = ClassLoader::crc32(0, base, (jint)size);
 449   write_bytes_aligned(base, (int)size);
 450 }
 451 
































 452 
 453 // Dump bytes to file -- at the current file position.
 454 
 455 void FileMapInfo::write_bytes(const void* buffer, int nbytes) {
 456   if (_file_open) {
 457     int n = ::write(_fd, buffer, nbytes);
 458     if (n != nbytes) {
 459       // It is dangerous to leave the corrupted shared archive file around,
 460       // close and remove the file. See bug 6372906.
 461       close();
 462       remove(_full_path);
 463       fail_stop("Unable to write to shared archive file.");
 464     }
 465   }
 466   _file_offset += nbytes;
 467 }
 468 
 469 
 470 // Align file position to an allocation unit boundary.
 471 


 496   align_file_position();
 497 }
 498 
 499 
 500 // Close the shared archive file.  This does NOT unmap mapped regions.
 501 
 502 void FileMapInfo::close() {
 503   if (_file_open) {
 504     if (::close(_fd) < 0) {
 505       fail_stop("Unable to close the shared archive file.");
 506     }
 507     _file_open = false;
 508     _fd = -1;
 509   }
 510 }
 511 
 512 
 513 // JVM/TI RedefineClasses() support:
 514 // Remap the shared readonly space to shared readwrite, private.
 515 bool FileMapInfo::remap_shared_readonly_as_readwrite() {
 516   struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[0];

 517   if (!si->_read_only) {
 518     // the space is already readwrite so we are done
 519     return true;
 520   }
 521   size_t used = si->_used;
 522   size_t size = align_size_up(used, os::vm_allocation_granularity());
 523   if (!open_for_read()) {
 524     return false;
 525   }

 526   char *base = os::remap_memory(_fd, _full_path, si->_file_offset,
 527                                 si->_base, size, false /* !read_only */,
 528                                 si->_allow_exec);
 529   close();
 530   if (base == NULL) {
 531     fail_continue("Unable to remap shared readonly space (errno=%d).", errno);
 532     return false;
 533   }
 534   if (base != si->_base) {
 535     fail_continue("Unable to remap shared readonly space at required address.");
 536     return false;
 537   }
 538   si->_read_only = false;
 539   return true;
 540 }
 541 
 542 // Map the whole region at once, assumed to be allocated contiguously.
 543 ReservedSpace FileMapInfo::reserve_shared_memory() {
 544   struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[0];
 545   char* requested_addr = si->_base;
 546 
 547   size_t size = FileMapInfo::shared_spaces_size();
 548 
 549   // Reserve the space first, then map otherwise map will go right over some
 550   // other reserved memory (like the code cache).
 551   ReservedSpace rs(size, os::vm_allocation_granularity(), false, requested_addr);
 552   if (!rs.is_reserved()) {
 553     fail_continue("Unable to reserve shared space at required address " INTPTR_FORMAT, requested_addr);
 554     return rs;
 555   }
 556   // the reserved virtual memory is for mapping class data sharing archive
 557   MemTracker::record_virtual_memory_type((address)rs.base(), mtClassShared);
 558 
 559   return rs;
 560 }
 561 
 562 // Memory map a region in the address space.
 563 static const char* shared_region_name[] = { "ReadOnly", "ReadWrite", "MiscData", "MiscCode"};

 564 
 565 char* FileMapInfo::map_region(int i) {

 566   struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i];
 567   size_t used = si->_used;
 568   size_t alignment = os::vm_allocation_granularity();
 569   size_t size = align_size_up(used, alignment);
 570   char *requested_addr = si->_base;
 571 
 572   // map the contents of the CDS archive in this memory
 573   char *base = os::map_memory(_fd, _full_path, si->_file_offset,
 574                               requested_addr, size, si->_read_only,
 575                               si->_allow_exec);
 576   if (base == NULL || base != si->_base) {
 577     fail_continue("Unable to map %s shared space at required address.", shared_region_name[i]);
 578     return NULL;
 579   }
 580 #ifdef _WINDOWS
 581   // This call is Windows-only because the memory_type gets recorded for the other platforms
 582   // in method FileMapInfo::reserve_shared_memory(), which is not called on Windows.
 583   MemTracker::record_virtual_memory_type((address)base, mtClassShared);
 584 #endif

 585   return base;
 586 }
 587 



























































































 588 bool FileMapInfo::verify_region_checksum(int i) {
 589   if (!VerifySharedSpaces) {
 590     return true;
 591   }
 592   const char* buf = _header->_space[i]._base;

 593   size_t sz = _header->_space[i]._used;








 594   int crc = ClassLoader::crc32(0, buf, (jint)sz);
 595   if (crc != _header->_space[i]._crc) {
 596     fail_continue("Checksum verification failed.");
 597     return false;
 598   }
 599   return true;
 600 }
 601 
 602 // Unmap a memory region in the address space.
 603 
 604 void FileMapInfo::unmap_region(int i) {

 605   struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i];
 606   size_t used = si->_used;
 607   size_t size = align_size_up(used, os::vm_allocation_granularity());
 608   if (!os::unmap_memory(si->_base, size)) {







 609     fail_stop("Unable to unmap shared space.");
 610   }
 611 }
 612 















 613 
 614 void FileMapInfo::assert_mark(bool check) {
 615   if (!check) {
 616     fail_stop("Mark mismatch while restoring from shared file.");
 617   }
 618 }
 619 
 620 
 621 FileMapInfo* FileMapInfo::_current_info = NULL;
 622 SharedClassPathEntry* FileMapInfo::_classpath_entry_table = NULL;
 623 int FileMapInfo::_classpath_entry_table_size = 0;
 624 size_t FileMapInfo::_classpath_entry_size = 0x1234baad;
 625 bool FileMapInfo::_validating_classpath_entry_table = false;
 626 
 627 // Open the shared archive file, read and validate the header
 628 // information (version, boot classpath, etc.).  If initialization
 629 // fails, shared spaces are disabled and the file is closed. [See
 630 // fail_continue.]
 631 //
 632 // Validation of the archive is done in two steps:


 641     fail_continue("Tool agent requires sharing to be disabled.");
 642     return false;
 643   }
 644 
 645   if (!open_for_read()) {
 646     return false;
 647   }
 648 
 649   init_from_file(_fd);
 650   if (!validate_header()) {
 651     return false;
 652   }
 653 
 654   SharedReadOnlySize =  _header->_space[0]._capacity;
 655   SharedReadWriteSize = _header->_space[1]._capacity;
 656   SharedMiscDataSize =  _header->_space[2]._capacity;
 657   SharedMiscCodeSize =  _header->_space[3]._capacity;
 658   return true;
 659 }
 660 









 661 int FileMapInfo::FileMapHeader::compute_crc() {
 662   char* header = data();
 663   // start computing from the field after _crc
 664   char* buf = (char*)&_crc + sizeof(int);
 665   size_t sz = data_size() - (buf - header);
 666   int crc = ClassLoader::crc32(0, buf, (jint)sz);
 667   return crc;
 668 }
 669 
 670 bool FileMapInfo::FileMapHeader::validate() {
 671   if (VerifySharedSpaces && compute_crc() != _crc) {
 672     fail_continue("Header checksum verification failed.");
 673     return false;
 674   }
 675 
 676   if (_version != current_version()) {
 677     FileMapInfo::fail_continue("The shared archive file is the wrong version.");
 678     return false;
 679   }
 680   if (_magic != (int)0xf00baba2) {


 712         status = false;
 713       }
 714     }
 715   }
 716 
 717   if (_paths_misc_info != NULL) {
 718     FREE_C_HEAP_ARRAY(char, _paths_misc_info);
 719     _paths_misc_info = NULL;
 720   }
 721   return status;
 722 }
 723 
 724 // The following method is provided to see whether a given pointer
 725 // falls in the mapped shared space.
 726 // Param:
 727 // p, The given pointer
 728 // Return:
 729 // True if the p is within the mapped shared space, otherwise, false.
 730 bool FileMapInfo::is_in_shared_space(const void* p) {
 731   for (int i = 0; i < MetaspaceShared::n_regions; i++) {
 732     if (p >= _header->_space[i]._base &&
 733         p < _header->_space[i]._base + _header->_space[i]._used) {




 734       return true;
 735     }
 736   }
 737 
 738   return false;
 739 }
 740 
 741 void FileMapInfo::print_shared_spaces() {
 742   gclog_or_tty->print_cr("Shared Spaces:");
 743   for (int i = 0; i < MetaspaceShared::n_regions; i++) {
 744     struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i];

 745     gclog_or_tty->print("  %s " INTPTR_FORMAT "-" INTPTR_FORMAT,
 746                         shared_region_name[i],
 747                         si->_base, si->_base + si->_used);
 748   }
 749 }
 750 
 751 // Unmap mapped regions of shared space.
 752 void FileMapInfo::stop_sharing_and_unmap(const char* msg) {
 753   FileMapInfo *map_info = FileMapInfo::current_info();
 754   if (map_info) {
 755     map_info->fail_continue("%s", msg);
 756     for (int i = 0; i < MetaspaceShared::n_regions; i++) {
 757       if (map_info->_header->_space[i]._base != NULL) {

 758         map_info->unmap_region(i);
 759         map_info->_header->_space[i]._base = NULL;
 760       }
 761     }

 762   } else if (DumpSharedSpaces) {
 763     fail_stop("%s", msg);
 764   }
 765 }


  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/sharedClassUtil.hpp"
  28 #include "classfile/symbolTable.hpp"
  29 #include "classfile/systemDictionaryShared.hpp"
  30 #include "classfile/altHashing.hpp"
  31 #if INCLUDE_ALL_GCS
  32 #include "gc/g1/g1CollectedHeap.hpp"
  33 #endif
  34 #include "memory/filemap.hpp"
  35 #include "memory/metadataFactory.hpp"
  36 #include "memory/oopFactory.hpp"
  37 #include "oops/objArrayOop.hpp"
  38 #include "runtime/arguments.hpp"
  39 #include "runtime/java.hpp"
  40 #include "runtime/os.hpp"
  41 #include "runtime/vm_version.hpp"
  42 #include "services/memTracker.hpp"
  43 #include "utilities/defaultStream.hpp"
  44 
  45 # include <sys/stat.h>
  46 # include <errno.h>
  47 
  48 #ifndef O_BINARY       // if defined (Win32) use binary files.
  49 #define O_BINARY 0     // otherwise do nothing.
  50 #endif
  51 
  52 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
  53 extern address JVM_FunctionAtStart();


 151 }
 152 
 153 FileMapInfo::~FileMapInfo() {
 154   assert(_current_info == this, "must be singleton"); // not thread safe
 155   _current_info = NULL;
 156 }
 157 
 158 void FileMapInfo::populate_header(size_t alignment) {
 159   _header->populate(this, alignment);
 160 }
 161 
 162 size_t FileMapInfo::FileMapHeader::data_size() {
 163   return SharedClassUtil::file_map_header_size() - sizeof(FileMapInfo::FileMapHeaderBase);
 164 }
 165 
 166 void FileMapInfo::FileMapHeader::populate(FileMapInfo* mapinfo, size_t alignment) {
 167   _magic = 0xf00baba2;
 168   _version = _current_version;
 169   _alignment = alignment;
 170   _obj_alignment = ObjectAlignmentInBytes;
 171   _narrow_oop_mode = Universe::narrow_oop_mode();
 172   _narrow_oop_shift = Universe::narrow_oop_shift();
 173   _max_heap_size = MaxHeapSize;
 174   _classpath_entry_table_size = mapinfo->_classpath_entry_table_size;
 175   _classpath_entry_table = mapinfo->_classpath_entry_table;
 176   _classpath_entry_size = mapinfo->_classpath_entry_size;
 177 
 178   // The following fields are for sanity checks for whether this archive
 179   // will function correctly with this JVM and the bootclasspath it's
 180   // invoked with.
 181 
 182   // JVM version string ... changes on each build.
 183   get_header_version(_jvm_ident);
 184 }
 185 
 186 void FileMapInfo::allocate_classpath_entry_table() {
 187   int bytes = 0;
 188   int count = 0;
 189   char* strptr = NULL;
 190   char* strptr_max = NULL;
 191   Thread* THREAD = Thread::current();
 192 
 193   ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();


 429   write_region(i, (char*)space->bottom(), used, capacity, read_only, false);
 430 }
 431 
 432 
 433 // Dump region to file.
 434 
 435 void FileMapInfo::write_region(int region, char* base, size_t size,
 436                                size_t capacity, bool read_only,
 437                                bool allow_exec) {
 438   struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[region];
 439 
 440   if (_file_open) {
 441     guarantee(si->_file_offset == _file_offset, "file offset mismatch.");
 442     if (PrintSharedSpaces) {
 443       tty->print_cr("Shared file region %d: 0x%6x bytes, addr " INTPTR_FORMAT
 444                     " file offset 0x%6x", region, size, base, _file_offset);
 445     }
 446   } else {
 447     si->_file_offset = _file_offset;
 448   }
 449   if (MetaspaceShared::is_string_region(region)) {
 450     assert((base - (char*)Universe::narrow_oop_base()) % HeapWordSize == 0, "Sanity");
 451     if (base != NULL) {
 452       si->_addr._offset = (intx)oopDesc::encode_heap_oop_not_null((oop)base);
 453     } else {
 454       si->_addr._offset = 0;
 455     }
 456   } else {
 457     si->_addr._base = base;
 458   }
 459   si->_used = size;
 460   si->_capacity = capacity;
 461   si->_read_only = read_only;
 462   si->_allow_exec = allow_exec;
 463   si->_crc = ClassLoader::crc32(0, base, (jint)size);
 464   write_bytes_aligned(base, (int)size);
 465 }
 466 
 467 // Write the string space. The string space contains one or multiple GC(G1) regions.
 468 // When the total string space size is smaller than one GC region of the dump time,
 469 // only one string region is used for shared strings.
 470 //
 471 // If the total string space size is bigger than one GC region, there would be more
 472 // than one GC regions allocated for shared strings. The first/bottom GC region might
 473 // be a partial GC region with the empty portion at the higher address within that region.
 474 // The non-empty portion of the first region is written into the archive as one string
 475 // region. The rest are consecutive full GC regions if they exist, which can be written
 476 // out in one chunk as another string region.
 477 void FileMapInfo::write_string_regions(GrowableArray<MemRegion> *regions) {
 478   for (int i = MetaspaceShared::first_string;
 479            i < MetaspaceShared::first_string + MetaspaceShared::max_strings; i++) {
 480     char* start = NULL;
 481     size_t size = 0;
 482     if (regions->is_nonempty()) {
 483       if (i == MetaspaceShared::first_string) {
 484         MemRegion first = regions->first();
 485         start = (char*)first.start();
 486         size = first.byte_size();
 487       } else {
 488         int len = regions->length();
 489         if (len > 1) {
 490           start = (char*)regions->at(1).start();
 491           size = (char*)regions->at(len - 1).end() - start;
 492         }
 493       }
 494     }
 495     write_region(i, start, size, size, false, false);
 496   }
 497 }
 498 
 499 
 500 // Dump bytes to file -- at the current file position.
 501 
 502 void FileMapInfo::write_bytes(const void* buffer, int nbytes) {
 503   if (_file_open) {
 504     int n = ::write(_fd, buffer, nbytes);
 505     if (n != nbytes) {
 506       // It is dangerous to leave the corrupted shared archive file around,
 507       // close and remove the file. See bug 6372906.
 508       close();
 509       remove(_full_path);
 510       fail_stop("Unable to write to shared archive file.");
 511     }
 512   }
 513   _file_offset += nbytes;
 514 }
 515 
 516 
 517 // Align file position to an allocation unit boundary.
 518 


 543   align_file_position();
 544 }
 545 
 546 
 547 // Close the shared archive file.  This does NOT unmap mapped regions.
 548 
 549 void FileMapInfo::close() {
 550   if (_file_open) {
 551     if (::close(_fd) < 0) {
 552       fail_stop("Unable to close the shared archive file.");
 553     }
 554     _file_open = false;
 555     _fd = -1;
 556   }
 557 }
 558 
 559 
 560 // JVM/TI RedefineClasses() support:
 561 // Remap the shared readonly space to shared readwrite, private.
 562 bool FileMapInfo::remap_shared_readonly_as_readwrite() {
 563   int idx = 0;
 564   struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[idx];
 565   if (!si->_read_only) {
 566     // the space is already readwrite so we are done
 567     return true;
 568   }
 569   size_t used = si->_used;
 570   size_t size = align_size_up(used, os::vm_allocation_granularity());
 571   if (!open_for_read()) {
 572     return false;
 573   }
 574   char *addr = _header->region_addr(idx);
 575   char *base = os::remap_memory(_fd, _full_path, si->_file_offset,
 576                                 addr, size, false /* !read_only */,
 577                                 si->_allow_exec);
 578   close();
 579   if (base == NULL) {
 580     fail_continue("Unable to remap shared readonly space (errno=%d).", errno);
 581     return false;
 582   }
 583   if (base != addr) {
 584     fail_continue("Unable to remap shared readonly space at required address.");
 585     return false;
 586   }
 587   si->_read_only = false;
 588   return true;
 589 }
 590 
 591 // Map the whole region at once, assumed to be allocated contiguously.
 592 ReservedSpace FileMapInfo::reserve_shared_memory() {
 593   struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[0];
 594   char* requested_addr = _header->region_addr(0);
 595 
 596   size_t size = FileMapInfo::shared_spaces_size();
 597 
 598   // Reserve the space first, then map otherwise map will go right over some
 599   // other reserved memory (like the code cache).
 600   ReservedSpace rs(size, os::vm_allocation_granularity(), false, requested_addr);
 601   if (!rs.is_reserved()) {
 602     fail_continue("Unable to reserve shared space at required address " INTPTR_FORMAT, requested_addr);
 603     return rs;
 604   }
 605   // the reserved virtual memory is for mapping class data sharing archive
 606   MemTracker::record_virtual_memory_type((address)rs.base(), mtClassShared);
 607 
 608   return rs;
 609 }
 610 
 611 // Memory map a region in the address space.
 612 static const char* shared_region_name[] = { "ReadOnly", "ReadWrite", "MiscData", "MiscCode",
 613                                             "String1", "String2"};
 614 
 615 char* FileMapInfo::map_region(int i) {
 616   assert(!MetaspaceShared::is_string_region(i), "sanity");
 617   struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i];
 618   size_t used = si->_used;
 619   size_t alignment = os::vm_allocation_granularity();
 620   size_t size = align_size_up(used, alignment);
 621   char *requested_addr = _header->region_addr(i); 
 622 
 623   // map the contents of the CDS archive in this memory
 624   char *base = os::map_memory(_fd, _full_path, si->_file_offset,
 625                               requested_addr, size, si->_read_only,
 626                               si->_allow_exec);
 627   if (base == NULL || base != requested_addr) {
 628     fail_continue("Unable to map %s shared space at required address.", shared_region_name[i]);
 629     return NULL;
 630   }
 631 #ifdef _WINDOWS
 632   // This call is Windows-only because the memory_type gets recorded for the other platforms
 633   // in method FileMapInfo::reserve_shared_memory(), which is not called on Windows.
 634   MemTracker::record_virtual_memory_type((address)base, mtClassShared);
 635 #endif
 636 
 637   return base;
 638 }
 639 
 640 MemRegion *string_ranges = NULL;
 641 int num_ranges = 0;
 642 bool FileMapInfo::map_string_regions() {
 643 #if INCLUDE_ALL_GCS
 644   if (UseG1GC && UseCompressedOops && UseCompressedClassPointers) {
 645     if (narrow_oop_mode() == Universe::narrow_oop_mode() &&
 646         narrow_oop_shift() == Universe::narrow_oop_shift()) {
 647       string_ranges = new MemRegion[MetaspaceShared::max_strings];
 648       struct FileMapInfo::FileMapHeader::space_info* si;
 649 
 650       for (int i = MetaspaceShared::first_string;
 651                i < MetaspaceShared::first_string + MetaspaceShared::max_strings; i++) {
 652         si = &_header->_space[i];
 653         size_t used = si->_used;
 654         if (used > 0) {
 655           size_t size = used;
 656           char* requested_addr = (char*)((void*)oopDesc::decode_heap_oop_not_null(
 657                                                  (narrowOop)si->_addr._offset));
 658           string_ranges[num_ranges] = MemRegion((HeapWord*)requested_addr, size / HeapWordSize);
 659           num_ranges ++;
 660         }
 661       }
 662 
 663       // Check that ranges are within the java heap
 664       if (!G1CollectedHeap::heap()->check_archive_addresses(string_ranges, num_ranges)) {
 665         fail_continue("Unable to allocate shared string space: range is not "
 666                       "within java heap.");
 667         return false;
 668       }
 669 
 670       // allocate from java heap
 671       if (!G1CollectedHeap::heap()->alloc_archive_regions(string_ranges, num_ranges)) {
 672         fail_continue("Unable to allocate shared string space: range is "
 673                       "already in use.");
 674         return false;
 675       }
 676 
 677       // Map the string data. No need to call MemTracker::record_virtual_memory_type()
 678       // for mapped string regions as they are part of the reserved java heap, which
 679       // is already recorded.
 680       for (int i = 0; i < num_ranges; i++) {
 681         si = &_header->_space[MetaspaceShared::first_string + i];
 682         char* addr = (char*)string_ranges[i].start();
 683         char* base = os::map_memory(_fd, _full_path, si->_file_offset,
 684                                     addr, string_ranges[i].byte_size(), si->_read_only,
 685                                     si->_allow_exec);
 686         if (base == NULL || base != addr) {
 687           fail_continue("Unable to map shared string space at required address.");
 688           return false;
 689         }
 690       }
 691       return true; // the shared string data is mapped successfuly
 692     } else {
 693       //  narrow oop encoding differ, the shared string data are not used
 694       if (PrintSharedSpaces && _header->_space[MetaspaceShared::first_string]._used > 0) {
 695         tty->print_cr("Shared string data from the CDS archive is being ignored. "
 696                      "The current CompressedOops encoding differs from that archived "
 697                      "due to heap size change. The archive was dumped using max heap "
 698                      "size %dM.", max_heap_size() >> 20);
 699       }
 700     }
 701   } else {
 702     if (PrintSharedSpaces && _header->_space[MetaspaceShared::first_string]._used > 0) {
 703       tty->print_cr("Shared string data from the CDS archive is being ignored. UseG1GC, "
 704                     "UseCompressedOops and UseCompressedClassPointers are required.");
 705     }
 706   }
 707 
 708   // if we get here, the shared string data is not mapped
 709   assert(string_ranges == NULL && num_ranges == 0, "sanity");
 710   StringTable::ignore_shared_strings(true);
 711 #endif
 712   return true;
 713 }
 714 
 715 bool FileMapInfo::verify_string_regions() {
 716   for (int i = MetaspaceShared::first_string;
 717            i < MetaspaceShared::first_string + MetaspaceShared::max_strings; i++) {
 718     if (!verify_region_checksum(i)) {
 719       return false;
 720     }
 721   }
 722   return true;
 723 }
 724 
 725 void FileMapInfo::fixup_string_regions() {
 726   if (string_ranges != NULL) {
 727     G1CollectedHeap::heap()->fill_archive_regions(string_ranges, num_ranges);
 728   }
 729 }
 730 
 731 bool FileMapInfo::verify_region_checksum(int i) {
 732   if (!VerifySharedSpaces) {
 733     return true;
 734   }
 735 
 736   const char* buf;
 737   size_t sz = _header->_space[i]._used;
 738 
 739   if (sz == 0) {
 740     return true; // no data
 741   }
 742   if (MetaspaceShared::is_string_region(i) && StringTable::shared_string_ignored()) {
 743     return true; // shared string data are not mapped
 744   }
 745   buf = _header->region_addr(i);
 746   int crc = ClassLoader::crc32(0, buf, (jint)sz);
 747   if (crc != _header->_space[i]._crc) {
 748     fail_continue("Checksum verification failed.");
 749     return false;
 750   }
 751   return true;
 752 }
 753 
 754 // Unmap a memory region in the address space.
 755 
 756 void FileMapInfo::unmap_region(int i) {
 757   assert(!MetaspaceShared::is_string_region(i), "sanity");
 758   struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i];
 759   size_t used = si->_used;
 760   size_t size = align_size_up(used, os::vm_allocation_granularity());
 761   char* addr;
 762 
 763   if (used == 0) {
 764     return;
 765   }
 766 
 767   addr = _header->region_addr(i);
 768   if (!os::unmap_memory(addr, size)) {
 769     fail_stop("Unable to unmap shared space.");
 770   }
 771 }
 772 
 773 void FileMapInfo::unmap_string_regions() {
 774   for (int i = MetaspaceShared::first_string;
 775            i < MetaspaceShared::first_string + MetaspaceShared::max_strings; i++) {
 776     struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i];
 777     size_t used = si->_used;
 778     if (used > 0) {
 779       size_t size = align_size_up(used, os::vm_allocation_granularity());
 780       char* addr = (char*)((void*)oopDesc::decode_heap_oop_not_null(
 781                                              (narrowOop)si->_addr._offset));
 782       if (!os::unmap_memory(addr, size)) {
 783         fail_stop("Unable to unmap shared space.");
 784       }
 785     }
 786   }
 787 }
 788 
 789 void FileMapInfo::assert_mark(bool check) {
 790   if (!check) {
 791     fail_stop("Mark mismatch while restoring from shared file.");
 792   }
 793 }
 794 
 795 
 796 FileMapInfo* FileMapInfo::_current_info = NULL;
 797 SharedClassPathEntry* FileMapInfo::_classpath_entry_table = NULL;
 798 int FileMapInfo::_classpath_entry_table_size = 0;
 799 size_t FileMapInfo::_classpath_entry_size = 0x1234baad;
 800 bool FileMapInfo::_validating_classpath_entry_table = false;
 801 
 802 // Open the shared archive file, read and validate the header
 803 // information (version, boot classpath, etc.).  If initialization
 804 // fails, shared spaces are disabled and the file is closed. [See
 805 // fail_continue.]
 806 //
 807 // Validation of the archive is done in two steps:


 816     fail_continue("Tool agent requires sharing to be disabled.");
 817     return false;
 818   }
 819 
 820   if (!open_for_read()) {
 821     return false;
 822   }
 823 
 824   init_from_file(_fd);
 825   if (!validate_header()) {
 826     return false;
 827   }
 828 
 829   SharedReadOnlySize =  _header->_space[0]._capacity;
 830   SharedReadWriteSize = _header->_space[1]._capacity;
 831   SharedMiscDataSize =  _header->_space[2]._capacity;
 832   SharedMiscCodeSize =  _header->_space[3]._capacity;
 833   return true;
 834 }
 835 
 836 char* FileMapInfo::FileMapHeader::region_addr(int idx) {
 837   if (MetaspaceShared::is_string_region(idx)) {
 838     return (char*)((void*)oopDesc::decode_heap_oop_not_null(
 839               (narrowOop)_space[idx]._addr._offset));
 840   } else {
 841     return _space[idx]._addr._base;
 842   }
 843 }
 844 
 845 int FileMapInfo::FileMapHeader::compute_crc() {
 846   char* header = data();
 847   // start computing from the field after _crc
 848   char* buf = (char*)&_crc + sizeof(int);
 849   size_t sz = data_size() - (buf - header);
 850   int crc = ClassLoader::crc32(0, buf, (jint)sz);
 851   return crc;
 852 }
 853 
 854 bool FileMapInfo::FileMapHeader::validate() {
 855   if (VerifySharedSpaces && compute_crc() != _crc) {
 856     fail_continue("Header checksum verification failed.");
 857     return false;
 858   }
 859 
 860   if (_version != current_version()) {
 861     FileMapInfo::fail_continue("The shared archive file is the wrong version.");
 862     return false;
 863   }
 864   if (_magic != (int)0xf00baba2) {


 896         status = false;
 897       }
 898     }
 899   }
 900 
 901   if (_paths_misc_info != NULL) {
 902     FREE_C_HEAP_ARRAY(char, _paths_misc_info);
 903     _paths_misc_info = NULL;
 904   }
 905   return status;
 906 }
 907 
 908 // The following method is provided to see whether a given pointer
 909 // falls in the mapped shared space.
 910 // Param:
 911 // p, The given pointer
 912 // Return:
 913 // True if the p is within the mapped shared space, otherwise, false.
 914 bool FileMapInfo::is_in_shared_space(const void* p) {
 915   for (int i = 0; i < MetaspaceShared::n_regions; i++) {
 916     char *base;
 917     if (MetaspaceShared::is_string_region(i) && _header->_space[i]._used == 0) {
 918       continue;
 919     }
 920     base = _header->region_addr(i);
 921     if (p >= base && p < base + _header->_space[i]._used) {
 922       return true;
 923     }
 924   }
 925 
 926   return false;
 927 }
 928 
 929 void FileMapInfo::print_shared_spaces() {
 930   gclog_or_tty->print_cr("Shared Spaces:");
 931   for (int i = 0; i < MetaspaceShared::n_regions; i++) {
 932     struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i];
 933     char *base = _header->region_addr(i);
 934     gclog_or_tty->print("  %s " INTPTR_FORMAT "-" INTPTR_FORMAT,
 935                         shared_region_name[i],
 936                         base, base + si->_used);
 937   }
 938 }
 939 
 940 // Unmap mapped regions of shared space.
 941 void FileMapInfo::stop_sharing_and_unmap(const char* msg) {
 942   FileMapInfo *map_info = FileMapInfo::current_info();
 943   if (map_info) {
 944     map_info->fail_continue("%s", msg);
 945     for (int i = 0; i < MetaspaceShared::num_non_strings; i++) {
 946       char *addr = map_info->_header->region_addr(i);
 947       if (addr != NULL && !MetaspaceShared::is_string_region(i)) {
 948         map_info->unmap_region(i);
 949         map_info->_header->_space[i]._addr._base = NULL;
 950       }
 951     }
 952     map_info->unmap_string_regions();
 953   } else if (DumpSharedSpaces) {
 954     fail_stop("%s", msg);
 955   }
 956 }
< prev index next >