1.1 --- a/src/share/vm/memory/filemap.cpp Tue Aug 12 11:12:25 2014 -0700 1.2 +++ b/src/share/vm/memory/filemap.cpp Tue Aug 12 17:46:16 2014 -0400 1.3 @@ -177,7 +177,14 @@ 1.4 fail_continue("The shared archive file has the wrong version."); 1.5 return false; 1.6 } 1.7 - _file_offset = (long)n; 1.8 + size_t len = lseek(fd, 0, SEEK_END); 1.9 + struct FileMapInfo::FileMapHeader::space_info* si = 1.10 + &_header._space[MetaspaceShared::mc]; 1.11 + if (si->_file_offset >= len || len - si->_file_offset < si->_used) { 1.12 + fail_continue("The shared archive file has been truncated."); 1.13 + return false; 1.14 + } 1.15 + _file_offset = n; 1.16 return true; 1.17 } 1.18 1.19 @@ -268,6 +275,7 @@ 1.20 si->_capacity = capacity; 1.21 si->_read_only = read_only; 1.22 si->_allow_exec = allow_exec; 1.23 + si->_crc = ClassLoader::crc32(0, base, (jint)size); 1.24 write_bytes_aligned(base, (int)size); 1.25 } 1.26 1.27 @@ -292,14 +300,15 @@ 1.28 // Align file position to an allocation unit boundary. 1.29 1.30 void FileMapInfo::align_file_position() { 1.31 - long new_file_offset = align_size_up(_file_offset, os::vm_allocation_granularity()); 1.32 + size_t new_file_offset = align_size_up(_file_offset, 1.33 + os::vm_allocation_granularity()); 1.34 if (new_file_offset != _file_offset) { 1.35 _file_offset = new_file_offset; 1.36 if (_file_open) { 1.37 // Seek one byte back from the target and write a byte to insure 1.38 // that the written file is the correct length. 1.39 _file_offset -= 1; 1.40 - if (lseek(_fd, _file_offset, SEEK_SET) < 0) { 1.41 + if (lseek(_fd, (long)_file_offset, SEEK_SET) < 0) { 1.42 fail_stop("Unable to seek.", NULL); 1.43 } 1.44 char zero = 0; 1.45 @@ -406,6 +415,19 @@ 1.46 return base; 1.47 } 1.48 1.49 +bool FileMapInfo::verify_region_checksum(int i) { 1.50 + if (!VerifySharedSpaces) { 1.51 + return true; 1.52 + } 1.53 + const char* buf = _header._space[i]._base; 1.54 + size_t sz = _header._space[i]._used; 1.55 + int crc = ClassLoader::crc32(0, buf, (jint)sz); 1.56 + if (crc != _header._space[i]._crc) { 1.57 + fail_continue("Checksum verification failed."); 1.58 + return false; 1.59 + } 1.60 + return true; 1.61 +} 1.62 1.63 // Unmap a memory region in the address space. 1.64 1.65 @@ -457,8 +479,20 @@ 1.66 return true; 1.67 } 1.68 1.69 +int FileMapInfo::compute_header_crc() { 1.70 + char* header = (char*)&_header; 1.71 + // start computing from the field after _crc 1.72 + char* buf = (char*)&_header._crc + sizeof(int); 1.73 + size_t sz = sizeof(FileMapInfo::FileMapHeader) - (buf - header); 1.74 + int crc = ClassLoader::crc32(0, buf, (jint)sz); 1.75 + return crc; 1.76 +} 1.77 1.78 bool FileMapInfo::validate() { 1.79 + if (VerifySharedSpaces && compute_header_crc() != _header._crc) { 1.80 + fail_continue("Header checksum verification failed."); 1.81 + return false; 1.82 + } 1.83 if (_header._version != current_version()) { 1.84 fail_continue("The shared archive file is the wrong version."); 1.85 return false;